11

The SCNView documentation says that setting allowsCameraControl to YES does not update the camera node position when the user changes the point of view. Is there any way to get the user-modified camera parameters when the user changes the point of view?

bugloaf
  • 2,890
  • 3
  • 30
  • 49

2 Answers2

20

When you enable allowsCameraControl, SceneKit inserts its own camera node into the scene (as an immediate child of the scene's root node), and sets that node as the view's pointOfView. Read the properties of that node to find out the camera's position and orientation, or the properties of the node's camera attribute to find the camera's field of view, depth limits, etc.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • I didn't see that! I must have been looking at some old documentation. – bugloaf Jul 19 '14 at 21:26
  • 1
    I just can't retrieve this node. How can I find it? – berfis Jul 20 '14 at 09:11
  • 1
    @rickster I can't find the camera as described "as immediate child of the scene's root node". It's always nil. "sceneView.scene?.rootNode.childNodes.first?.camera", "sceneView.scene?.rootNode.camera" and " sceneView.scene?.rootNode.enumerateChildNodes({ (node, stop) in print("camera: \(node.camera)") })" all return nil. – zumzum Jul 17 '17 at 01:41
  • 7
    If you are using default camera control, you can get the POV node from the SCNView itself! `let cameraNode = mySceneView.pointOfView` – 72A12F4E Sep 06 '17 at 22:26
  • 3
    This isn't quite true. It only inserts its own camera node if there isn't already a camera node in the scene. Apple's own SceneKit game template proves this. If you look at the default code, it first sets up a camera node and attaches it to the root. `allowsCameraControl` uses that camera. You can confirm this by changing its position in that setup code. If however, you comment out those lines, *then and only then* is a camera automatically inserted for you. – Mark A. Donohoe May 14 '20 at 18:13
  • You can use your own camera node if you set `view.cameraControlConfiguration.autoSwitchToFreeCamera = false` – Lachezar Todorov Dec 05 '22 at 17:06
10

Assuming mySCNView is the SCNView..

@property (weak, nonatomic) IBOutlet SCNView *mySCNView;

Then you can get the camera position from..

NSLog(@"Camera position: %f %f %f", _mySCNView.pointOfView.position.x, _mySCNView.pointOfView.position.y, _mySCNView.pointOfView.position.z);

NSLog(@"Camera rotation: %f %f %f %f", _mySCNView.pointOfView.rotation.x, _mySCNView.pointOfView.rotation.y, _mySCNView.pointOfView.rotation.z, _mySCNView.pointOfView.rotation.w);

NSLog(@"Camera orientat: %f %f %f %f", _mySCNView.pointOfView.orientation.x, _mySCNView.pointOfView.orientation.y, _mySCNView.pointOfView.orientation.z,_mySCNView.pointOfView.orientation.w);

In my case, I turned on allowsCameraControl and adjusted the view, noted down the values, and set them at the start of the app. I only needed to set position and orientation, not rotation.

CraicDesign
  • 631
  • 6
  • 5