3

I an new to SceneKit. Trying to figure out, how I can reset the pointOfView camera to it original zoom/position so it would cover all nodes in the scene?

Kashif
  • 4,642
  • 7
  • 44
  • 97

3 Answers3

2
@IBOutlet weak var sceneKitView: SCNView! //Your SCNView

var myCamera:SCNNode!  //An object to hold your scene's camera

In viewDidLoad, load it with hyour camera's name:

myCamera:SCNNode = scene.rootNode.childNode(withName: "sceneCamera", recursively: true)!
// sceneCamera is my camera node's name in .scn file

sceneKitView.allowsCameraControl = true //Allow camera control

When you want to reset it:

sceneKitView.allowsCameraControl = false
sceneKitView.pointOfView = myCamera

Keep coding......... :)

Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
1

Just setup a default camera position and orientation to desired values. Use the following code:

func resetCameraToDefaultPosition() {

    sceneView.pointOfView?.position = SCNVector3(x: 5, y: 0, z: 5)
    sceneView.pointOfView?.orientation = SCNVector4(x: 0, y: 1, z: 0, w: .pi/4)
}

Also, as @mnuages proposed, you can use defaultCameraController instance property to frame all the nodes with 3D geometry in your scene:

func resetCameraToDefaultPosition() {

    sceneView.defaultCameraController.frameNodes([coneNode, sphereNode, cubeNode])
}

But the better way is to create a new camera. Here's what Apple documentation says:

Use a node with an SCNCamera instance assigned to its camera property to view a scene. The node provides the position and direction of a virtual camera, and the camera object provides rendering parameters such as field of view and focus.

let cameraNode = SCNNode()
cameraNode.position = SCNVector3(x: CGFloat, y: CGFloat, z: CGFloat)
cameraNode.rotation = SCNVector4(x: CGFloat, y: CGFloat, z: CGFloat, w: CGFloat)

let camera = SCNCamera()
camera.focalLength = 24
cameraNode.camera = camera

Hope this helps.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
1

you can use the frameNodes(_:) method on SCNCameraController to place a camera so that a set of nodes becomes visible.

mnuages
  • 13,049
  • 2
  • 23
  • 40
  • 1
    This is a good and simple idea. But `self.sceneView.defaultCameraController.frameNodes(self.scene.rootNode.childNodes)` just isn't taking effect. – Kashif Oct 26 '18 at 02:06