Search code examples
swiftscenekitarkit

SCNNode opacity and visibility


I have one node that is inside of another node. Both are loaded from .obj files. The outer node has an opacity of 0.01. The inner node has an opacity of 0.99. The inner node is not visible at all unless I change its opacity to 1 or the opacity of the outer node to 0. When I rotate the scene with the invisible node slightly, the node appears. When I rotate it back it disappears.

Any thoughts on how I can maintain visibility of the inner node?


Solution

  • You can easily control a visibility of an inner sphere using .renderingOrder instance property:

    var renderingOrder: Int { get set }       /*  By default it equals to zero  */
    

    Here's how it looks like in a code:

    // NESTED OBJECT
    let innerNode = SCNNode()
    innerNode.geometry = SCNSphere(radius: 0.5)
    innerNode.geometry?.firstMaterial?.diffuse.contents = UIColor(white: 0.0, 
                                                                  alpha: 0.99)
    scene.rootNode.addChildNode(innerNode)
        
    // OUTER OBJECT
    let outerSphere = scene.rootNode.childNode(withName: "ball", recursively: true)
    outerSphere?.geometry?.firstMaterial?.diffuse.contents = UIColor(white: 1.0, 
                                                                     alpha: 0.3)
    outerSphere?.renderingOrder = 1
    

    enter image description here