Is it possible to draw a clear SCNSphere with a bright red SCNSpotlight?

1.8k Views Asked by At

The last few days I am struggling a bit with SceneKit. I am trying to plot a clear/transparent SCNSphere, with a red spotlight. It appears that my red spotlight is becoming transparant as well if I set my SCNSphere to transpararent / clear colour. Is it possible to unlink the SCNLight Node to the SCNSphereNode, so that the bright red colour of the spot remains if the SCNSphere is transparant? Images of both spheres are below the code.

My code:

func setupView() {
    scene = SCNScene()
    caliView.scene = scene
    caliView.allowsCameraControl = true
    caliView.backgroundColor = UIColor.clearColor()
    let clearMaterial = SCNMaterial()
    clearMaterial.diffuse.contents = UIColor(white: 0.9, alpha: 0.5)
    clearMaterial.locksAmbientWithDiffuse = true


    let shape = SCNSphere(radius: 5)
    shape.materials = [clearMaterial]
    let shapeNode = SCNNode(geometry: shape)

    let spotLight = SCNLight()
    spotLight.type = SCNLightTypeSpot
    spotLight.color = UIColor.init(colorLiteralRed: 180, green: 0, blue: 0, alpha: 0.0)
    let lightNode = SCNNode()
    lightNode.light = spotLight

    lightNode.position = SCNVector3(x: 0.0, y:0.0, z:15.0)
    lightNode.orientation = SCNQuaternion(x: 0.0, y:0, z:30, w:0.0)


    let ambientLight = SCNLight()
    ambientLight.type = SCNLightTypeAmbient
    ambientLight.color = UIColor(white: 0.8, alpha: 0.2)
    let ambientNode = SCNNode()
    ambientNode.light = ambientLight

    shapeNode.position = SCNVector3(x: 0.0, y: 0.0, z: 0.0)

    scene.rootNode.addChildNode(ambientNode)
    scene.rootNode.addChildNode(shapeNode)
    shapeNode.addChildNode(lightNode)


    }

Darker sphere with bright red spotlight:

Darker sphere with bright red spotlight

More transparent sphere with soft red spotlight:

More transparent sphere with soft red spotlight

1

There are 1 best solutions below

5
On

In this line... shapeNode.addChildNode(lightNode) ...you added the light node to the sphere node.

If you want to un-link them while still having them move together, you can create an empty SCNNode and add the other two SCNNode instances to it as children (the one for the light and the one for the sphere):

func setupView() {
scene = SCNScene()
caliView.scene = scene
caliView.allowsCameraControl = true
caliView.backgroundColor = UIColor.clearColor()
let clearMaterial = SCNMaterial()
clearMaterial.diffuse.contents = UIColor(white: 0.9, alpha: 0.5)
clearMaterial.locksAmbientWithDiffuse = true

let emptyNode = SCNNode()

let shape = SCNSphere(radius: 5)
shape.materials = [clearMaterial]
let shapeNode = SCNNode(geometry: shape)

let spotLight = SCNLight()
spotLight.type = SCNLightTypeSpot
spotLight.color = UIColor.init(colorLiteralRed: 180, green: 0, blue: 0, alpha: 0.0)
let lightNode = SCNNode()
lightNode.light = spotLight

lightNode.position = SCNVector3(x: 0.0, y:0.0, z:15.0)
lightNode.orientation = SCNQuaternion(x: 0.0, y:0, z:30, w:0.0)

let ambientLight = SCNLight()
ambientLight.type = SCNLightTypeAmbient
ambientLight.color = UIColor(white: 0.8, alpha: 0.2)
let ambientNode = SCNNode()
ambientNode.light = ambientLight

shapeNode.position = SCNVector3(x: 0.0, y: 0.0, z: 0.0)

emptyNode.addChild(shapeNode)
emptyNode.addChild(lightNode)
scene.rootNode.addChildNode(emptyNode)
scene.rootNode.addChildNode(ambientNode)

}