How can I create a shader that "cuts" through all geometry, only rendering the clearing background in Unity? An example:

enter image description here

That's a prism in a huge white box-shaped room. The surface of the prism is just rendering the skybox of the scene (a starry sky). I used multiple cameras, which is pretty inefficient... There must be a way with just shaders.

How can I do this?


2 Answers 2


Ok, finally figured it out!

enter image description here

You see how the sphere mesh “cuts” through the cylinder and plane all the way to the skybox. This took me longer than expected to figure this out but thanks to a wiki page (DepthMask) I got it all sorted.

Before using the shader setup in the wiki page I was using three different cameras with different clearing/depth flags to achieve the same result. It worked but it totally choked on the Ouya. The above solution requires only one camera and runs much fasta.

So I setup the scene like such…

  1. added a realistic skybox (nasa)

  2. added some shapes

  3. added the SetRenderQueue script to the plane, sphere and cylinder

  4. set the queue order on the sphere to 2999 (the other objects are at 3000)

  5. then added the DepthMask shader to the sphere

That’s it. Any questions let me know!

  • 2
    \$\begingroup\$ D'oh, I was completely over-thinking the problem in my post above (I was thinking about flat hole meshes on walls/floors, really). The linked solution shows a 3D hole object, and the solution shown, for this case, is very simple - all you actually need to do is write the 'hole' into the z-buffer without writing any colour, and make sure the hole is drawn early. \$\endgroup\$
    – bluescrn
    Apr 6, 2014 at 9:49
  • \$\begingroup\$ @bluescrn No worries! I appreciate the detailed post. :) \$\endgroup\$
    – CalebHC
    Apr 6, 2014 at 18:46

Couple of options to get the effect you want - but they do come a a cost:

  • Write the holes into the stencil buffer or destination alpha channel, then draw the skybox last, using a suitable stencil test or alpha blend, and with no z-test?


  • Render the skybox to a texture at the start of the frame, and project that on to the 'hole geometry'

I suspect that there must be a way to do this by using/abusing the z-buffer, too - but you probably want to z-test the 'hole polygons' using their real Z, then write a different Z. You could probably use multiple passes and the stencil buffer - drawing the hole 3 times with renderstate set up like this might work:

  • Pass 1: No z-test, Clear stencil under hole polys
  • Pass 2: Normal z-test, Increment stencil value (where depth test passes)
  • Pass 3: Clear Z where stencil > 0

You'd still need to draw in a specific order - first the environment, then the holes, and finally the skybox last, though.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .