Question

I am developing in Unity3D.

Using the Depth Mask shader defined here, objects behind the mask using the shader are not drawn (only objects in front of the mask are drawn). Using the referenced code, I attempted to create a reverse Depth Mask, which is intended to cause the camera to only draw objects behind it.

What I did was change

ZTest LEqual

into

ZTest GEqual

I also tried

ZTest Greater

However, what happens is that the camera draws all objects, be it behind or in front of the mask. How do I implement a reverse depth mask?

Was it helpful?

Solution

You have to use ZTest GEqual in the shader of the masked object, not the mask itself. Also note that the depth mask and the masked objects have to be rendered before all other objects. So you should use the following render queues:

  • Depth mask: Geometry - 20 or 2080
  • Masked objects: Geometry - 10 or 2090

(A more detailed description of depth masks can be found in the Unity Wiki.)

You should also disable z-buffer writes for the masked objects (ZWrite Off). This means that if the masked objects overlap, they won't render correctly. You'll probably also run into problems with the rest of the geometry. A better approach is to use stencil buffers which are available in the Pro version of Unity 4.2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top