michiel
michiel

Reputation: 31

SceneKit and using a custom camera class

I am trying to create a custom Camera class to reuse across all levels in SceneKit.

I have defined the cameraNode Set the sceneView to use the cameraNode pointOfView

Define class:

class GameCamera: SCNCamera {

let cameraNodeHorizontal: SCNNode!

override init() {
    cameraNodeHorizontal = SCNScene(named: "/GameAssets.scnassets/Camera.scn")?.rootNode.childNode(withName: "GameCamera", recursively: true)

    super.init()
}

func setup(scnView: SCNView) {
    scnView.scene?.rootNode.addChildNode(cameraNodeHorizontal)
    scnView.pointOfView = cameraNodeHorizontal
}

}

Inside ViewController:

private var camera = GameCamera()

private func loadCamera() {
    camera.setup(scnView: self.scnView)
}

The scene renders from a default pointOfView other than the one I defined.

Wondering if anyone can help?

Upvotes: 0

Views: 332

Answers (1)

Voltan
Voltan

Reputation: 1213

I don't use .scn - but just a basic class, something like this:

var cameraEye = SCNNode()
var cameraFocus = SCNNode()

init()
{
    cameraEye.name = "Camera Eye"
    cameraFocus.name = "Camera Focus"

    cameraFocus.isHidden = true
    cameraFocus.position  =  SCNVector3(x: 0, y: 0, z: 0)

    cameraEye.camera = SCNCamera()
    cameraEye.constraints = []
    cameraEye.position = SCNVector3(x: 0, y: 15, z: 0.1)

    let vConstraint = SCNLookAtConstraint(target: cameraFocus)
    vConstraint.isGimbalLockEnabled = true
    cameraEye.constraints = [vConstraint]
}

// Add camera and focus nodes to your Scenekit nodes

gameNodes.addChildNode(camera.cameraEye)
gameNodes.addChildNode(camera.cameraFocus)

Upvotes: 0

Related Questions