2

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

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
Rosa90
  • 21
  • 4

1 Answers1

3

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)

}

Karl Sigiscar
  • 289
  • 1
  • 7
  • Thank you for your response! Tried to implement this code.. but it would not work unfortunately :-( same problem: if I try to make the sphere more transparent, the red light does the same – Rosa90 Sep 14 '16 at 16:33
  • I think you can solve the problem with [categorybitmask](https://developer.apple.com/reference/scenekit/scnlight/1523669-categorybitmask) – Karl Sigiscar Sep 14 '16 at 17:13
  • I think we are taking the problem from the wrong side. Don't we expect the color / transparency of the spot to be blended with that of the sphere ? This is what the renderer does. – Karl Sigiscar Sep 15 '16 at 08:51
  • Yes probably.. Is there a way to create a separate render path for the spot? – Rosa90 Sep 15 '16 at 11:40
  • As the documentation states: Use SceneKit lights only for dynamic light sources or lights that affect moving objects. For statically lit portions of your scene, create a light map texture in an external 3D authoring tool (also known as baked lighting) and apply it to objects in the scene using the multiply material property. >>> Is your scene dynamic or static ? – Karl Sigiscar Sep 15 '16 at 13:20