By Steve

2017-07-03 20:16:01 8 Comments

How can I use the horizontal and vertical planes tracked by ARKit to hide objects behind walls/ behind real objects? Currently the 3D added objects can be seen through walls when you leave a room and/ or in front of objects that they should be behind. So is it possible to use the data ARKit gives me to provide a more natural AR experience without the objects appearing through walls?


@kakashy 2018-09-28 02:34:29

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] = SCNNode(geometry: boxGeometry)
    // Set rendering order to present this box in front of the other models = -1

@ARGeo 2018-06-10 13:51:57

For hold-out operation (a.k.a. blackhole or blocking material) you can use instance property, called .colorBufferWriteMask, in iOS 11 and macOS 13. This property writes depth channel information when rendering the material. You can use it this way:

torus.geometry?.firstMaterial?.colorBufferWriteMask = .alpha

or this way:

func blockingMaterial() -> SCNMaterial {
    let holdout = SCNMaterial()
    holdout.isDoubleSided = true
    holdout.diffuse.contents =
    holdout.colorBufferWriteMask = SCNColorMask(rawValue: 0)
    return holdout
torus.geometry?.firstMaterial = blockingMaterial()

You can easily use this property for shading your invisible virtual walls. To position these invisible virtual walls to a right place help you ARWorldMap class (it works in iOS 12) and ARAnchors.

session.getCurrentWorldMap { worldMap, error in 
    guard let worldMap = worldMap else {
let configuration = ARWorldTrackingConfiguration()
configuration.initialWorldMap = worldMap 

ARWorldMap is the space-mapping state and set of ARAnchors from a world-tracking AR session. Read about it here.

@MoD 2018-05-30 09:51:01

Great solution:

GitHub: arkit-occlusion

Worked for me.

But in my case i wanted to set the walls by code. So if you don't want to set the Walls by user -> use the plane detection to detect walls and set the walls by code.

Or in a range of 4 meters the iphone depht sensor works and you can detect obstacles with ARHitTest.

@rickster 2017-07-05 01:54:25

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.)

@Steve 2017-07-05 13:11:45

So the example code in the documentation has the option for configuration.planeDetection =.horizontal that does nothing?

@rickster 2017-07-05 15:08:48

That code turns plane detection on. Without it, ARKit doesn't report planes at all.

@Steve 2017-07-11 11:43:14

Yes, but I meant that if you could set plane detection to horizontal you should be able to set it to vertical

@PDK 2017-08-04 14:06:53

@Steve (In Xcode) you can Jump to the Definition (⌃⌘click) of .horizontal, and you'll find no other options there. I wouldn't be surprised if Apple extends the option set with "vertical"—and possibly other types of planes in the future though.

@Benjohn 2017-09-04 17:20:49

Hi Rick, would partial occlusion be significantly more complex than this technique?

@mnuages 2017-09-04 19:13:58

@Benjohn what do you mean by partial occlusion ? Only the part that’s actually behind the occluder (from the camera’s perspective) won’t be visible.

@Benjohn 2017-09-04 21:15:10

I was unclear, sorry: I am asking about simulating a partially transparent "occluder" rather than a completely opaque one. The occluded part of an object might be rendered with reduced alpha, for example. So in the case of a table occluder with a block pushing in to it, you would still see the piece of block under the table, but it would be faint.

@Benjohn 2017-09-05 12:40:33

Ah! Got it working. As noted here it is also necessary to set the occluder to be rendered before the other objects with: occluderNode.renderingOrder = -1 – should this be part of the answer?

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How to align SCNScene to a physical table using ARKit?

3 Answered Questions

[SOLVED] Is it possible to track objects in ARKit like in Vuforia?

1 Answered Questions

[SOLVED] Can ARKit detect specific surfaces as planes?

1 Answered Questions

2 Answered Questions

3D objects keep moving ARKit

2 Answered Questions

ARKit plane with real world object above it

0 Answered Questions

ARKit - create world boundaries/get position at edge of visible plane?

1 Answered Questions

0 Answered Questions

Spatial Mapping using ARKit ARCamera.transform's current position

1 Answered Questions

Sponsored Content