Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

scenekit

When I create a new Scene Kit Game using the Swift language, there is already some come which gives this result : enter image description here
I want to turn off the ambient light which is lighting the cube but I don't get how to do that since there isn't any light explicitly attached to any node.

Her's the Game View Controller code :

import SceneKit
import QuartzCore

class GameViewController: NSViewController {

    @IBOutlet var gameView: GameView

    override func awakeFromNib(){
        // create a new scene
        let scene = SCNScene()

        // create and add a camera to the scene
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        scene.rootNode.addChildNode(cameraNode)

        // place the camera
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 2)

        // create and add a 3d box to the scene
        let boxNode = SCNNode()
        boxNode.geometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.02)
        scene.rootNode.addChildNode(boxNode)

        // create and configure a material
        let material = SCNMaterial()
        material.diffuse.contents = NSImage(named: "texture")
        material.specular.contents = NSColor.whiteColor()
        material.specular.intensity = 0.2
        material.locksAmbientWithDiffuse = true

        // set the material to the 3d object geometry
        boxNode.geometry.firstMaterial = material

        // animate the 3d object
        let animation: CABasicAnimation = CABasicAnimation(keyPath: "rotation")
        animation.toValue = NSValue(SCNVector4: SCNVector4(x: 1, y: 1, z: 0, w: M_PI*2))
        animation.duration = 5
        animation.repeatCount = MAXFLOAT //repeat forever
        boxNode.addAnimation(animation, forKey: "")

        // set the scene to the view
        self.gameView!.scene = scene

        // allows the user to manipulate the camera
        self.gameView!.allowsCameraControl = true

        // show statistics such as fps and timing information
        self.gameView!.showsStatistics = true

        // configure the view
        self.gameView!.backgroundColor = NSColor.blackColor()
    }

}
like image 933
Pop Flamingo Avatar asked Jul 11 '14 12:07

Pop Flamingo


People also ask

How do I change the color of the ambient light source?

Select the ambient Color source as color. Or if you have a Night skybox you can select the source as skybox. Set the color as black. All objects other than the ones with emission/glowing materials should do dark. If you have any questions, leave it in the comment box below.

How to turn off skybox lighting?

You can disable the skybox by selecting the Main Camera in the hierarchy. You can see that the gameobjects are lit even after the skybox is disabled. You need to change the ambient lighting to change the lighting on gameobject. Go to Windows>Rendering>Lighting. Select the ambient Color source as color.

What happens when you disable the lights in a unity scene?

When you disable the lights in your Unity scene, the scene doesn’t go dark. Instead, what you see is that, your gameobjects are still getting lit from the ambient light. So, how do you make your scene dark ?

Is there a widget for environment lighting in Blender?

Bookmark this question. Show activity on this post. My version of Blender does not have a widget for environment lighting under the world tab. I've disabled all lights in my render, but it's still not pitch black. How do I disable the default environment lighting? Show activity on this post.


1 Answers

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.

like image 133
David Rönnqvist Avatar answered Nov 16 '22 00:11

David Rönnqvist