Make an SCNNode transparent so you see other nodes through it

How do I make the Cylinders transparent so you always see the green wall through them?

Problem description

(full code at the end)

I have an SCNNode() defined as two squares (forming two sides of a cube) with a material defined as

var meshMaterial: SCNMaterial = {
    let mat = SCNMaterial()
    mat.diffuse.contents = UIColor.green
    mat.lightingModel = .constant
    return mat
}()

I place two cylinders on each wall, where the cylinder’s opacity is set to 0.3. You can see the green walls through the cylinders.

enter image description here

Issue

When I set the meshMaterial‘s alpha to anything other than 1.0

var meshMaterial: SCNMaterial = {
    let mat = SCNMaterial()
    mat.diffuse.contents = UIColor.green.withAlphaComponent(0.5)
    mat.lightingModel = .constant
    return mat
}()

You can no longer see the green walls through the cylinders. And instead you see the black background through the clyinders

enter image description here

At certain rotation angles you can see the green through the cylinder (the right-wall cylinder in this example)

enter image description here

Playground

Here is the full playground code to replicate the issue.

import UIKit
import SceneKit
import PlaygroundSupport
import SceneKit.ModelIO

// MARK: Set up Scene
let scene = SCNScene()

var sceneView: SCNView = {
    let sv = SCNView(frame: CGRect(x: 0, y: 0, width: 800, height: 800))
    sv.autoenablesDefaultLighting = false
    sv.allowsCameraControl = true
    sv.backgroundColor = .black
    sv.scene = scene
    return sv
}()

var cameraNode: SCNNode = {
    let n = SCNNode()
    n.camera = SCNCamera()
    n.camera?.contrast = 0.0
    n.camera?.wantsHDR = false
    n.position = SCNVector3(x: 0, y: 0, z: 10)
    //n.addChildNode(omniLightNode)
    return n
}()

var ambientLightNode: SCNNode = {
    let n = SCNNode()
    n.light = SCNLight()
    n.light!.type = SCNLight.LightType.ambient
    n.light!.intensity = 100
    return n
}()

private var omniLightNode: SCNNode = {
    let n = SCNNode()
    n.light = SCNLight()
    n.light!.type = SCNLight.LightType.directional
    n.light!.color = UIColor.white
    n.position = SCNVector3(x: 0, y: 10, z: 0)
    n.light!.intensity = 800
    return n
}()

scene.rootNode.addChildNode(omniLightNode)
scene.rootNode.addChildNode(ambientLightNode)
scene.rootNode.addChildNode(cameraNode)

PlaygroundPage.current.liveView = sceneView

// MARK: - Mesh
let vertices: [SCNVector3] = [
    // back square
    SCNVector3(-1, -1,  0),
    SCNVector3( 1, -1,  0),
    SCNVector3( 1,  1,  0),
    SCNVector3(-1,  1,  0),
    
    // right wall
    SCNVector3(1,  -1,  0),
    SCNVector3(1,  -1,  2),
    SCNVector3(1,   1,  2),
    SCNVector3(1,   1,  0)
]
let vertexSource = SCNGeometrySource(vertices: vertices)

let indices: [Int32] = [
    0,1,2,
    0,2,3,
    
    4,5,6,
    4,6,7
]
let indexElement = SCNGeometryElement(
    indices: indices,
    primitiveType: .triangles
)

let textures: [CGPoint] = [
    CGPoint(x: 0, y: 0),
    CGPoint(x: 0, y: 1),
    CGPoint(x: 1, y: 1),
    CGPoint(x: 1, y: 0),
    
    CGPoint(x: 0, y: 0),
    CGPoint(x: 0, y: 1),
    CGPoint(x: 1, y: 1),
    CGPoint(x: 1, y: 0)
]
let textureSource = SCNGeometrySource(textureCoordinates: textures)
let meshGeometry = SCNGeometry(
    sources: [vertexSource, textureSource],
    elements: [indexElement]
)


var meshMaterial: SCNMaterial = {
    let mat = SCNMaterial()
    mat.diffuse.contents = UIColor.green.withAlphaComponent(0.5) // <-- set alpha to 1.0 
    mat.lightingModel = .constant
    return mat
}()

meshGeometry.materials = [meshMaterial]
let meshNode = SCNNode(geometry: meshGeometry)
meshNode.position = SCNVector3(0, 0, 0)

// MARK: Cylinders

// Define a Node to hold the cylinders
let shapeNode = SCNNode()
shapeNode.position = SCNVector3(0, 0, 0)

let cylinderBack = SCNNode(
    geometry: SCNCylinder(
        radius: 0.1,
        height: 1.0
    )
)

let cylinderRight = SCNNode(
    geometry: SCNCylinder(
        radius: 0.1,
        height: 1.0
    )
)

let cylinderMaterial: SCNMaterial = {
    let mat = SCNMaterial()
    mat.lightingModel = .physicallyBased
    mat.diffuse.contents = UIColor.red
    mat.roughness.contents = 0.8
    mat.metalness.contents = 0.4
    return mat
}()

cylinderBack.geometry?.materials = [cylinderMaterial]
cylinderBack.position = SCNVector3(0, 0, 0.05)

cylinderRight.geometry?.materials = [cylinderMaterial]
cylinderRight.position = SCNVector3(1, 0, 1)

// Set Cylinders opacity
shapeNode.opacity = 0.3


sceneView.scene?.rootNode.addChildNode(meshNode)
sceneView.scene?.rootNode.addChildNode(shapeNode)

shapeNode.addChildNode(cylinderBack)
shapeNode.addChildNode(cylinderRight)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật