Search code examples
iosswiftscenekit

Can you change the properties of scnView.autoenablesDefaultLighting?


I need lights to stay "stationary" in my scene. The best lighting method I've found so far is to actually to use scnView.autoenablesDefaultLighting = true however, I can't figure out if there's any way to control some of the attributes. The intensity of the light is a BIT too bright, the location of the light is a BIT different than where I'd like it to be, those kinds of properties.

I've tried using all sorts of other lights, coding them individually BUT since they add to the scene as nodes, the lights (in those cases) themselves will move when I set scnView.allowsCameraControl = true. The default lighting is the only one that will remain "stationary" once the user begins to move the camera around. Can you access/control the properties of the default lighting?


Solution

  • 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.