ARKit – Rendering a 3D object under an invisible p

2019-08-11 11:11发布

问题:

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?

回答1:

You can achieve it using the following lines of code:

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

Choose one of two instance properties for .colorBufferWriteMask:

shadowsPlane.geometry?.materials.first?.colorBufferWriteMask = .alpha
// OR:
shadowsPlane.geometry?.materials.first?.colorBufferWriteMask = SCNColorMask(rawValue: 0)

Set a rendering order for your objects like:

shadowsPlane.renderingOrder = -1   // the nearest layer
shadowsPlane.renderingOrder = 100  // the farthest 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)