Access Stencil Buffer on non-translucent materials

I’m working on a stencil effect where a portion of an object can be “cut out” using another object as reference.


To do this I have the floor set up as a translucent material which tests the stencil value in the CustomStencil scene texture to determine it’s opacity.

The stencil buffer looks like this:


My current goal is to get this to work with a non-translucent material.
I can’t do this right now because non-translucent materials don’t have access to the custom depth buffer / stencil values.

Basically what I’d like to do is render all of the “Portals” (in this case a cube) to the stencil buffer (value = 1).
Then in the next pass render all of the “Blockers” (in this case the floor) the the stencil buffer (value = 2).
Then render the “Blockers” in the main pass while obeying the rule value == 2.
Then render the rest of the main pass.

I’m not sure where to start with this approach, any ideas?

did you find a solution?

Can you provide more detail or point me to a resource on how you accomplished the effect in your translucent material?

Were you ever able to solve this? I’ve been able to recreate using the a translucent material but it’s not desirable given the expense and limitations of that.

It’s recomme

It’s recommended you render another copy of the mesh. The other copy should have a fully opaque material, under render settings deselect render in main path and turn on render is custom render buffer. Then you can use that data for your needs either in the shader or as a postprocess.