1

I'm trying to create a material for my SCNBox with colored borders and transparent sides. Here's the code I use for creating material:

class CubeSide: UIView {
    override func draw(_ rect: CGRect) {
        let path = UIBezierPath(rect: rect)

        UIColor.yellow.setStroke()
        path.stroke()
    }
}

let sideView = CubeSide(frame: rect)
sideView.backgroundColor = UIColor.clear

let material = SCNMaterial()
material.diffuse.contents = sideView

material.isDoubleSided = true
box.materials = [material]

It works fine but the center part is white instead of transparent. I've tried a lot of experiments with SCNMaterial's transparent property and transparentModes, but with my limited understanding of 3D rendering I couldn't get it working. What am I missing here?

Thank you!

Rinat Khanov
  • 1,566
  • 10
  • 32

1 Answers1

3

From Apples documentation on SCNMaterialProperty.contents:

SceneKit cannot use a layer that is already being displayed elsewhere (for example, the backing layer of a UIView object).

So we need to create a new layer ourself:

// Create the geometry
let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)

// Create the layer
let layer = CALayer()
layer.frame = CGRect(x: 0, y: 0, width: 1000, height: 1000)
layer.borderColor = UIColor.red.cgColor
layer.borderWidth = 10

// Create a material from the layer and assign it
let material = SCNMaterial()
material.diffuse.contents = layer
material.isDoubleSided = true
box.materials = [material]

This should result in something like this:

enter image description here

jlsiewert
  • 3,494
  • 18
  • 41