简体   繁体   中英

Calling all parent and child nodes - SceneKit

I have a 3D car object with some pan and tap gestures on it and everything is working fine. The problem is that I found myself need specific angels for the camera to look at each part when I'm zooming it in as its a car and has so many parts, So I wrote all parent and child nodes to specify each of them. But I feel that it's not a good practice. Is there another way to do that?

let parentNodeName = result.node.parent?.name
        
        
        if parentNodeName == "up-side"{
            scnCameraOrbit.eulerAngles.x = 2.3 + Float.pi
            scnCameraOrbit.eulerAngles.y =  Float.pi * 1.0
        }
        else if parentNodeName == "front-side"{
            if nodeName == "window-front"{
                scnCameraOrbit.eulerAngles.x = 2.8 + Float.pi
                scnCameraOrbit.eulerAngles.y =  Float.pi + 3.16
                           }
          else  if nodeName == "hood"{
                scnCameraOrbit.eulerAngles.x = 2.95 + Float.pi
                scnCameraOrbit.eulerAngles.y =  Float.pi + 3.16
            }
          else  if nodeName == "bumper-front"{
                scnCameraOrbit.eulerAngles.x = 3.45 + Float.pi
                scnCameraOrbit.eulerAngles.y =  Float.pi + 3.16
            }
            else{
                scnCameraOrbit.eulerAngles.x = 3.45 + Float.pi
                scnCameraOrbit.eulerAngles.y =  Float.pi + 3.16
            }
        }
            else if parentNodeName == "back-side"{
            if nodeName == "window-rear"{
                scnCameraOrbit.eulerAngles.x = 2.9 + Float.pi
                scnCameraOrbit.eulerAngles.y =  Float.pi * 1.0
            }
            else if nodeName == "bumper-rear"{
                scnCameraOrbit.eulerAngles.x = 3.4 + Float.pi
                scnCameraOrbit.eulerAngles.y =  Float.pi * 1.0
            }
            else if nodeName == "trunk"{
                scnCameraOrbit.eulerAngles.x = 3.0 + Float.pi
                scnCameraOrbit.eulerAngles.y =  Float.pi * 1.0
            }
            else{
                 scnCameraOrbit.eulerAngles.x = 3.4 + Float.pi
                scnCameraOrbit.eulerAngles.y =  Float.pi * 1.0
            }
            }
            else if parentNodeName == "left-side"{
            scnCameraOrbit.eulerAngles.x = -Float.pi * 0.0
            scnCameraOrbit.eulerAngles.y =  -3 * Float.pi * 1.2    
            }
            else if parentNodeName == "right-side"{
            scnCameraOrbit.eulerAngles.x = -Float.pi * 0.0
            scnCameraOrbit.eulerAngles.y =  -2 * Float.pi * 1.2
            }
        else{
            return
        }

The IF tests are only called one time on hit test (click), right? If so...

To avoid some of the nested IF's complexity, you might try some enumeration techniques < 24701075 >, quite a few examples in this post - read carefully, some of this is dated and new Swift features are available. You'll need 2 sets (I believe), one for the parent node names and then one set for the node names. Not sure about efficiency, but clarity would improve... opinions vary, but code readability and avoiding debug problems is usually a higher priority for me unless performance is a real problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM