Occlusion Material or Hold-Out Shader in ARKit and SceneKit
To generate a hold-out mask, also known as Occlusion Material
, use.colorBufferWriteMask
, instance property that writes depth data when rendering the material.
sphere.geometry?.firstMaterial?.colorBufferWriteMask = []
Then assign an appropriate object's rendering order:
sphere.renderingOrder = -100
And, at last, allow SceneKit to read from / write to depth buffer when rendering the material:
sphere.geometry?.firstMaterial?.writesToDepthBuffer = true
sphere.geometry?.firstMaterial?.readsFromDepthBuffer = true
sphere.geometry?.firstMaterial?.isDoubleSided = true
Glass effect in SceneKit material
Create a SCNMaterial
and configure the following properties and assign it to the bottle geometry of a SCNNode
:
.lightingModel = .blinn
.transparent.content = // an image/texture whose alpha channel defines
// the area of partial transparency (the glass)
// and the opaque part (the label).
.transparencyMode = .dualLayer
.fresnelExponent = 1.5
.isDoubleSide = true
.specular.contents = UIColor(white: 0.6, alpha: 1.0)
.diffuse.contents = // texture image including the label (rest can be gray)
.shininess = // somewhere between 25 and 100
.reflective.contents = // glass won’t look good unless it has something
// to reflect, so also configure this as well.
// To at least a gray color with value 0.7
// but preferably an image.
Depending on what else is in your scene, the background, and the lighting used, you will probably have to tune the values above to get the desired results. If you want a bottle without the label, use the .transparency property (set its contents to a gray color) instead of the .transparent property.
How to Add Material to ModelEntity programatically in RealityKit?
Updated: June 14, 2022
RealityKit materials
There are 6 types of materials in RealityKit 2.0 and RealityFoundation at the moment:
- SimpleMaterial
- UnlitMaterial
- OcclusionMaterial (read this post to find out how to setup SceneKit occlusion shader)
- VideoMaterial (look at this post to find out how to setup it)
- PhysicallyBasedMaterial
- CustomMaterial
To apply these materials use the following logic:
import Cocoa
import RealityKit
class ViewController: NSViewController {
@IBOutlet var arView: ARView!
override func awakeFromNib() {
let box = try! Experience.loadBox()
var simpleMat = SimpleMaterial()
simpleMat.color = .init(tint: .blue, texture: nil)
simpleMat.metallic = .init(floatLiteral: 0.7)
simpleMat.roughness = .init(floatLiteral: 0.2)
var pbr = PhysicallyBasedMaterial()
pbr.baseColor = .init(tint: .green, texture: nil)
let mesh: MeshResource = .generateBox(width: 0.5,
height: 0.5,
depth: 0.5,
cornerRadius: 0.02,
splitFaces: true)
let boxComponent = ModelComponent(mesh: mesh,
materials: [simpleMat, pbr])
box.steelBox?.children[0].components.set(boxComponent)
box.steelBox?.orientation = Transform(pitch: .pi/4,
yaw: .pi/4,
roll: 0).rotation
arView.scene.anchors.append(box)
}
}
Read this post to find out how to load a texture for RealityKit's shaders.
How to create RealityKit's shaders similar to SceneKit's shaders
We know that in SceneKit there are 5 different shading models, so we can use RealityKit's SimpleMaterial
, PhysicallyBasedMaterial
and UnlitMaterial
to generate all these five shaders that we've been accustomed to.
Let's see how it looks like:
SCNMaterial.LightingModel.blinn – SimpleMaterial(color: . gray,
roughness: .float(0.5),
isMetallic: false)
SCNMaterial.LightingModel.lambert – SimpleMaterial(color: . gray,
roughness: .float(1.0),
isMetallic: false)
SCNMaterial.LightingModel.phong – SimpleMaterial(color: . gray,
roughness: .float(0.0),
isMetallic: false)
SCNMaterial.LightingModel.physicallyBased – PhysicallyBasedMaterial()
// all three shaders (`.constant`, `UnlitMaterial` and `VideoMaterial `)
// don't depend on lighting
SCNMaterial.LightingModel.constant – UnlitMaterial(color: .gray)
– VideoMaterial(avPlayer: avPlayer)
Related Topics
Use a Function to Find Common Elements in Two Sequences in Swift
How to Convert a Swift String to Cfstring
Round Top Corners of a Uiview in Swift
Not Condition in 'If Case' Statement
When Should I Use Optionals and When Should I Use Non-Optionals with Default Values
Calling a Global Function Which Has the Same Name as a Member Function
How Do Closures Capture Values from Previous Calls
How to Rotate Object in a Scene with Pan Gesture - Scenekit
Why Can't I Pass an Implicitly Unwrapped Optional as an Unsafemutablepointer
How to Get Data from Observedobject with Onreceive in Swiftui
Swift Error: Guard Body Must Not Fall Through
Cannot Use Mutating Member on Immutable Value of Type
How to Setup a Second Component with a Uipickerview
Swiftui 2 Firebase Push Notification
Type Ccc Doesnt Conform to Protocol 'Nsobjectprotocol'
Why Does an @Objc Enum Have a Different Description Than a Pure Swift Enum