Jan Misker
Jan Misker

Reputation: 2149

Can I observe the camera transform of a SCNView?

I have a SCNView with it's allowsCameraControl property enabled. I would like to observe the camera orientation while it is changing (by user gestures), how can I do this?

Upvotes: 0

Views: 405

Answers (1)

PongBongoSaurus
PongBongoSaurus

Reputation: 7385

When you set allowsCameraControl to true, SceneKit adds a Camera as a child of the rootNode.

As such to access information from the camera you can do something like this in the following delegate callback:

 func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

    //1. Get The Camera From The ARSCNScene
    if let currentPointOfView = augmentedRealityView?.pointOfView{

        let pitch = currentPointOfView.eulerAngles.x
        let yaw =   currentPointOfView.eulerAngles.y
        let roll =  currentPointOfView.eulerAngles.z

        print("""
            Pitch = \(degreesFrom(pitch))
            Yaw = \(degreesFrom(yaw))
            Roll = \(degreesFrom(roll))
            """)

    }
}

/// Convert Radians To Degrees
///
/// - Parameter radian: Float
/// - Returns: Float
func degreesFrom( _ radian: Float) -> Float{

   return radian * Float(180.0 / Double.pi)

}

Whereby Pitch, Yaw & Roll refer to the following: enter image description here

Hope this gets you started...

Upvotes: 1

Related Questions