How do I make the Cylinders transparent so you always see the green wall through them, regardless of the green's alpha value?
(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.
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
At certain rotation angles you can see the green through the cylinder (the right-wall cylinder in this example)
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)
The issue is in the order of rendering which is very important to achieve correct transparency. There are lot of ways to solve this, one easy option is to design your geometry to not overlap each other which makes it hard for the ordering. Or set manual order of rendering using renderingOrder
There is a post that speaks about this topic in detail. https://stackoverflow.com/a/60840898/409315