Petr Holub
Petr Holub

Reputation: 85

ARKit – Rendering a 3D object under an invisible plane

I have an ARKit scene with an invisible SCNPlane:

plane.geometry?.firstMaterial?.colorBufferWriteMask = []

This plane is placed on the ground and is used to render deferred shadows from other objects placed in the scene.

I want to render another SCNPlane which should be on the same level as the invisible plane (same Z-coordinate). The problem is, that every time the new object is under the invisible plane, it is not rendered at all.

Is there any way to render the object when it is under the invisible plane?

Upvotes: 1

Views: 521

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58093

You can achieve it using the following lines of code:

shadowsPlane.geometry?.materials.first?.writesToDepthBuffer = true
shadowsPlane.geometry?.materials.first?.readsFromDepthBuffer = true

Choose one of two instance properties for .colorBufferWriteMask:

shadowsPlane.geometry?.materials.first?.colorBufferWriteMask = []

Set a rendering order for your objects like:

shadowsPlane.renderingOrder = -1   // the nearest layer

And, of course, use an appropriate .lightingModel instance property:

shadowsPlane.geometry?.materials.first?.lightingModel = .constant 

Remember, there will be some tiny air gap between two planes:

shadowsPlane.position = SCNVector3(x: 0, y: 0, z: 0)
floorPlane.position = SCNVector3(x: 0, y: -0.01, z: 0)

Upvotes: 2

Related Questions