SceneKit: adding directional light to camera node has no effect

4.4k views Asked by At

Based on the advice in this answer, the goal is to add an ambient light to a scene along with a directional light to the camera.

This works in the Xcode Scene Editor, with the directional light set at 70% white and the ambient light at 50% white. The Euler angles for the directional light use default values.

Coding this arrangement fails. It's as if the directional light isn't being added. There is no effect on the scene; it's as if only the ambient light exists.

Different values for the x component of the Euler angles make no difference -- PI/2, PI, and other values were tried but still no change.

Adding the directional light to the scene's root node (scene.rootNode.addChildNode(directionalLightNode)), however, does indeed add the light to the scene.

fileprivate func addLightNode() {
    // Create ambient light
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = .ambient
    ambientLightNode.light!.color = UIColor(white: 0.70, alpha: 1.0)

    // Add ambient light to scene
    scene.rootNode.addChildNode(ambientLightNode)

    // Create directional light
    let directionalLight = SCNNode()
    directionalLight.light = SCNLight()
    directionalLight.light!.type = .directional
    directionalLight.light!.color = UIColor(white: 1.0, alpha: 1.0)
    directionalLight.eulerAngles = SCNVector3(x: 0, y: 0, z: 0)

    // Add directional light to camera
    let cameraNode = sceneView.pointOfView!
    cameraNode.addChildNode(directionalLight)
}
1

There are 1 answers

10
Xartec On BEST ANSWER

Create a SCNNode with for example the name cameraNode. Create a SCNCamera and assign it to the camera property of cameraNode. Add the cameraNode to the scene (as a child of the rootNode).

After that you can add the light node as a child node of cameraNode or, given it’s the only camera, as a child of the pointOfView node (which now represents the cameraNode you created and added to the scene). The default camera and its pointOfView node are not part of the scene’s object hierarchy.