Documentation

Occluders in Unity

These steps will help you create an object that hides all geometry behind it, but is still transparent:

  • In the Project tab, create a new shader asset and name it Occluder
  • Open the shader and paste the following code in it:
Shader "Unlit/Occluder" {
    SubShader {
        Tags { "Queue" = "Geometry-1" }
        ColorMask 0 
        ZWrite On
        Pass { }
    }
}
  • Create a new material and name it Occluder as well
  • In the material inspector
    • Set the shader to Unlit/Occluder
    • Make sure that the Render Queue is set to From Shader. It should have a value of 1999
  • Create a new object that should act as an occluder.
  • In the Mesh Renderer component of the new occluder object, set the material to the Occluder material we just created.
  • The occluder object should hide all other objects behind it, but still draw the background.

If you want more control over which objects get hidden and which don't, you can set the Render Queue value of the occluder object to 2001 and the Render Queue value of the objects that should be occluded to 2002. This will keep all defaults objects visible and allow you to set the occlusion effect on just some objects. To set the Render Queue on other objects, you can either create a custom shader or add the following script to the objects:

using UnityEngine;

public class SetRenderQueue : MonoBehaviour {

    [SerializeField]
    protected int[] m_queues = new int[] { 2002 };

    protected void Awake() {
        Material[] materials = renderer.materials;
        for (int i = 0; i < materials.Length && i < m_queues.Length; ++i) {
            materials[i].renderQueue = m_queues[i];
        }
    }
}

Tested on Unity 5.5