How to I Turn Off the Ambient Light in Scene Kit (With Swift)

How to I turn off the ambient light in Scene Kit (with Swift)?

It looks like you are seeing the "default" lighting.

You can explicitly disable it by calling

gameView.autoenablesDefaultLighting = false

It will also be disables as soon as you add your own lights to the scene.

SceneKit avoid lighting on specific node

SCNLight has a categoryBitMask property. This lets you choose which nodes are affected by the light (Although this is ignored for ambient lights). You could have 2 light source categories, one for your main scene, and another that only affects your lines.

Here is a simple example with 2 nodes, each lit with a different colour light:

struct LightType {
static let light1:Int = 0x1 << 1
static let light2:Int = 0x1 << 2
}

class GameViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let scene = SCNScene(named: "art.scnassets/scene.scn")!

let lightNode1 = SCNNode()
lightNode1.light = SCNLight()
lightNode1.light!.type = .omni
lightNode1.light!.color = UIColor.yellow
lightNode1.position = SCNVector3(x: 0, y: 10, z: 10)
lightNode1.light!.categoryBitMask = LightType.light1
scene.rootNode.addChildNode(lightNode1)

let lightNode2 = SCNNode()
lightNode2.light = SCNLight()
lightNode2.light!.type = .omni
lightNode2.light!.color = UIColor.red
lightNode2.position = SCNVector3(x: 0, y: 10, z: 10)
lightNode2.light!.categoryBitMask = LightType.light2
scene.rootNode.addChildNode(lightNode2)

let sphere1 = scene.rootNode.childNode(withName: "sphere1", recursively: true)!
sphere1.categoryBitMask = LightType.light1
let sphere2 = scene.rootNode.childNode(withName: "sphere2", recursively: true)!
sphere2.categoryBitMask = LightType.light2

let scnView = self.view as! SCNView
scnView.scene = scene
}
}

Sample Image

How can I get a light node in scene kit to be directly above the person at all times?

Attach your light node to the person node instead of the root node. If your person node rotates, then put the person node in a container node that doesn't rotate and also make your light a child of that. Use the container node for translation (of both person and light), use the person node if you need to rotate the person (taking a bow, say).

Can you change the properties of scnView.autoenablesDefaultLighting?

Forget allowsCameraControl and default cameras and lights if you want control of your scene.

let sceneView = SCNView()
let cameraNode = SCNNode() // the camera
var baseNode = SCNNode() // the basic model-root
let keyLight = SCNLight() ; let keyLightNode = SCNNode()
let ambientLight = SCNLight() ; let ambientLightNode = SCNNode()

func sceneSetup() {
let scene = SCNScene()
// add to an SCNView
sceneView.scene = scene

// add the container node containing all model elements
sceneView.scene!.rootNode.addChildNode(baseNode)

cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3Make(0, 0, 50)
scene.rootNode.addChildNode(cameraNode)

keyLight.type = SCNLightTypeOmni
keyLightNode.light = keyLight
keyLightNode.position = SCNVector3(x: 10, y: 10, z: 5)
cameraNode.addChildNode(keyLightNode)

ambientLight.type = SCNLightTypeAmbient
let shade: CGFloat = 0.40
ambientLight.color = UIColor(red: shade, green: shade, blue: shade, alpha: 1.0)
ambientLightNode.light = ambientLight
cameraNode.addChildNode(ambientLightNode)

// view the scene through your camera
sceneView.pointOfView = cameraNode

// add gesture recognizers here
}

Move or rotate cameraNode to effect motion in view. Or, move or rotate baseNode. Either way, your light stay fixed relative to the camera.

If you want your lights fixed relative to the model, make them children of baseNode instead of the camera.



Related Topics



Leave a reply



Submit