iosswiftarkitscnnodescncamera

Swift -What is a SCNCamera's Definition and Purpose


I can't find a good explanation of what a SCNCamera is and it's purpose. This is Apple's definition:

A set of camera attributes that can be attached to a node to provide a point of view for displaying the scene.

This definition isn't clear because I set up the scene and added a SCNNode without attaching a SCNCamera to it. The point of view from the device's camera shows the SCNNode at the location I positioned it at with no problem and the scene is displayed fine.

What is the difference between the device's camera and a SCNCamera?

What is the benefit of attaching a SCNCamera to a SCNNode vs not using one?

If I have multiple SCNNodes (all detached no hierarchy amongst each other) does each node need it's own SCNCamera?

If I have multiple SCNNodes in a hierarchy (parent node with child nodes) does each node need it's own SCNCamera or does just the parent node?

lazy var sceneView: ARSCNView = {
    let sceneView = ARSCNView()
    sceneView.translatesAutoresizingMaskIntoConstraints = false
    sceneView.delegate = self
    return sceneView
}()

let configuration = ARWorldTrackingConfiguration()

override func viewDidLoad() {
    super.viewDidLoad()

    // pin sceneView to the view

    let material = SCNMaterial()
    material.diffuse.contents = UIImage(named: "earth")

    let plane = SCNPlane(width: 0.33, height: 0.33)
    plane.materials = [material]
    plane.firstMaterial?.isDoubleSided = true

    let myNode = SCNNode(geometry: plane)
    myNode.name = "earth"
    myNode.position = SCNVector3(0.0, 0.6, -0.9)

    sceneView.scene.rootNode.addChildNode(myNode)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    sceneView.session.run(configuration, options: [])
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillAppear(animated)

    sceneView.session.pause()
    sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
}

Solution

  • In SceneKit, the SCNCamera represents the point of view from which the user sees a scene. Ray Wenderlich provides a good explanation:

    Think back to the analogy of the movie set from Chapter 1: to shoot a scene, you’d position a camera looking at the scene and the resulting image of that scene would be from the camera’s perspective.

    Scene Kit works in a similar fashion; the position of the node that contains the camera determines the point of view from which you view the scene.

    You do not need to have a SCNCamera for each node. You should only need to have one camera for each angle that you want to show, or even just one. You can move one camera throughout the scene using its parent's position property.

    It looks like you're working with ARKit, which behaves a little differently. When using an ARSCNView, as opposed to a non-AR SCNView, you get the following behvior:

    • The view automatically renders the live video feed from the device camera as the scene background.
    • The world coordinate system of the view's SceneKit scene directly responds to the AR world coordinate system established by the session configuration.
    • The view automatically moves its SceneKit camera to match the real-world movement of the device.

    You do not need to worry as much about the scene's camera in this case, as it is automatically being controlled by the system so that it matches the device's movement for AR.

    For more detail, see Apple's documentation on SCNCamera: SCNCamera - SceneKit