ARKit hide objects behind walls ARKit hide objects behind walls ios ios

ARKit hide objects behind walls


You have two issues here.

(And you didn't even use regular expressions!)

How to create occlusion geometry for ARKit/SceneKit?

If you set a SceneKit material's colorBufferWriteMask to an empty value ([] in Swift), any objects using that material won't appear in the view, but they'll still write to the z-buffer during rendering, which affects the rendering of other objects. In effect, you'll get a "hole" shaped like your object, through which the background shows (the camera feed, in the case of ARSCNView), but which can still obscure other SceneKit objects.

You'll also need to make sure that an occluded renders before any other nodes it's supposed to obscure. You can do this using node hierarchy ( I can't remember offhand whether parent nodes render before their children or the other way around, but it's easy enough to test). Nodes that are peers in the hierarchy don't have a deterministic order, but you can force an order regardless of hierarchy with the renderingOrder property. That property defaults to zero, so setting it to -1 will render before everything. (Or for finer control, set the renderingOrders for several nodes to a sequence of values.)

How to detect walls/etc so you know where to put occlusion geometry?

In iOS 11.3 and later (aka "ARKit 1.5"), you can turn on vertical plane detection. (Note that when you get vertical plane anchors back from that, they're automatically rotated. So if you attach models to the anchor, their local "up" direction is normal to the plane.) Also new in iOS 11.3, you can get a more detailed shape estimate for each detected plane (see ARSCNPlaneGeometry), regardless of its orientation.

However, even if you have the horizontal and the vertical, the outer limits of a plane are just estimates that change over time. That is, ARKit can quickly detect where part of a wall is, but it doesn't know where the edges of the wall are without the user spending some time waving the device around to map out the space. And even then, the mapped edges might not line up precisely with those of the real wall.

So... if you use detected vertical planes to occlude virtual geometry, you might find places where virtual objects that are supposed to be hidden show through, either by being not quite hiding right at the edge of the wall, or being visible through places where ARKit hasn't mapped the entire real wall. (The latter issue you might be able to solve by assuming a larger extent than ARKit does.)


In order to create an occlusion material it's really simple

    let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)    // Define a occlusion material     let occlusionMaterial = SCNMaterial()    occlusionMaterial.colorBufferWriteMask = []    boxGeometry.materials = [occlusionMaterial]    self.box = SCNNode(geometry: boxGeometry)    // Set rendering order to present this box in front of the other models    self.box.renderingOrder = -1


For creating an occlusion material (also known as blackhole material or blocking material) you have to use the following instance properties: .colorBufferWriteMask, .readsFromDepthBuffer, .writesToDepthBuffer and .renderingOrder.

You can use them this way:

plane.geometry?.firstMaterial?.isDoubleSided = trueplane.geometry?.firstMaterial?.colorBufferWriteMask = .alpha  plane.geometry?.firstMaterial?.writesToDepthBuffer = trueplane.geometry?.firstMaterial?.readsFromDepthBuffer = trueplane.renderingOrder = -100

...or this way:

func occlusion() -> SCNMaterial {    let occlusionMaterial = SCNMaterial()    occlusionMaterial.isDoubleSided = true    occlusionMaterial.colorBufferWriteMask = []    occlusionMaterial.readsFromDepthBuffer = true    occlusionMaterial.writesToDepthBuffer = true    return occlusionMaterial}plane.geometry?.firstMaterial = occlusion()plane.renderingOrder = -100