Rendering objects behind another object

Hey guys

I saw a similar question to this one already here, but the guy kinda answered it himself and didn’t really provide any real solutions.

I’m looking to see if its possible (and if so, how) to render objects only when they appear behind another object. Another way to think of this is that a “screen” reveals objects and those objects only appear when behind that screen. Think like “lens of truth” from Zelda.

Here are some images to explain this:

I’ve read some stuff about this sort of thing being possible with shaders. Could anyone point me in the right direction? Or, if there is some alternative, could someone point me in that direction?

Thank you for the help in advance! :slight_smile:

Well, there are lots of ways to do this, but it depends on your setup.

For example if you are in an interior map (so every pixel of your screen is filled with environment) you could first draw your invisible objects, then clear the depth buffer, render your “masking” object(s) (either a modified transparent shader of the DepthMask shader, just make sure you write to the depth buffer). And finally draw the rest of your scene. You probably need 3 cameras and 2 seperate layers (one for the invisible objects and one for the masking objects). It might be possible with 2 cams by combining the last two things into one. This could be done by modifiing the shader used for masking and put it first on the render queue.

Another way might be to first render the masking object(s), then using a shader that does a reverse ztest to render the invisible objects. So instead of the usual “ZTest LEqual” you use “ZTest Greater”. That way the inv. objects are only drawn where the z value is greater. This might result in strange ordering of the invisible objects since farther away objects will be rendered on top of closer ones. If invisible objects can’t overlap this is the best solution to keep the fillrate low :wink: As i said it entirely depends on your setup / gametype …