Search code examples
ios3drotationscenekitcollada

Swift 3 - SceneKit get 3d model X Y Z rotation Value


I have a sceneView where I loaded a 3d model. The model is showing fine and I can controlling it touching the screen.

What I need is to get the values of X, Y, Z rotation while the 3d model is rotating.

How could I get this values in real-time?

I have trying using camera rotation with a gesture recognizer but they are not updating.

override func viewDidLoad() {
        super.viewDidLoad()

        sceneView.scene = SCNScene(named: "PKB2");

        sceneView.autoenablesDefaultLighting = true
        sceneView.allowsCameraControl = true

        // add a tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action:
            #selector(handleTap(_:)))
        sceneView.addGestureRecognizer(tapGesture)

    }


    func handleTap(_ gestureRecognize: UIGestureRecognizer) {
        // retrieve the SCNView

        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()

        print(cameraNode.rotation)

    }

Solution

  • In your handleTap(_:)you are creating a new camera. The position and rotation of this camera (or the rotation of the node containing the camera) has nothing to do with the rotation of the camera used to render the scene. When you are setting allowsCameraControl to true on your view SceneKit is creating a new camera that the user can control with touch events.

    You can get the node containing the camera that is currently used to render the view with the pointOfView property of the SCNSceneRenderer protocol that SCNView conforms to.

    func handleTap(_ gestureRecognize: UIGestureRecognizer) {
        let cameraNode: SCNNode? = sceneView.pointOfView
        print(cameraNode?.rotation ?? "There is no camera set to the view")
    }