Aaalllright one more from me. Got it working!
Here’s the final result:
As you can see, the opaque Hand correctly occludes UI, while the UI still occludes normal objects as it should.
When the Material of the Mesh is translucent, the Custom Depth Buffer doesn’t care about it, and so the translucency works as expected: overlaying itself rather than ‘cutting out’ the UI below it.
I’ll detail the full process now for total clarity:
-
Created a Blueprint with a 3D Widget in it. Browsed to the Material it was using (a Material Instance), and browsed up the parent chain until I had the ‘Widget3DPassThrough’ Material they were derived from. Duplicated that, and am now using it as my base for my own Material.
-
Edited the Material like so:
Key here is to note the constant going into the bitmask – this is the Custom Depth Stencil that this Material is ‘listening’ for. I’ll probably parameterise it and turn it into a Material Instance so I can edit this on the fly in Blueprints.
My modification is to test if the Custom Depth is ever smaller than the Scene Depth. If it is, we’re dealing with a translucent Material, and so we skip the part where we cut out our UI in the places where it overlaps our Custom Stencil (i.e. choose 1 in the multiply node if A < B).
-
Note that ‘Allow Custom Depth’ is true for both the UI Material and the one on my translucent Hand (I’m using CD for backface culling on the Hand; unrelated to this).
-
In the Character Blueprint, turn on Render CustomDepth → Stencil value equal to the constant going into the bitmask (254 in my case).
That’s it! No need for PP Mats, although you can layer them on top, like I have done. Only one Mat for the UI element, and only turn on CD settings for the character.