From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Links, and comments in the shader source.)
(Renamed HullCover.shader in the example project)
Line 20: Line 20:
== Example Project ==
== Example Project ==
[, 209KB]
Unity 1.2.2 project: [, 210KB]
''Note that the example project gives the shader the name 'HullCover.shader'.  See the [[:Talk:DepthMask|discussion]] page regarding this.''
==ShaderLab - DepthMask.shader==
==ShaderLab - DepthMask.shader==

Revision as of 14:37, 28 May 2006

Author: Neil Carter (NCarter)



DepthMask.shader in use on the example project.

This shader draws faces which are invisible, but which still appear in the depth buffer. This makes it possible to prevent subsequently-drawn objects from appearing behind those faces.


The example project shows how to use the shader to cut a hole in a water plane to prevent the water from appearing inside a boat's hull.

It is necessary to use three cameras to force the rendering order:

  • The first draws the boat hull and all other normal objects.
  • The second draws a skin over the top of the boat's hull using the DepthMask shader. The skin objects must be in a special layer which only this camera draws.
  • The third draws only the water. The water must be in its own layer and is only drawn by this camera.

The second and third cameras do not clear the colour or depth buffers. Of course, all three cameras must also move as one, so you may want to put them in an empty GameObject to keep them together.

Example Project

Unity 1.2.2 project:, 210KB

ShaderLab - DepthMask.shader

<shaderlab> Shader "DepthMask" {

       // Turn off lighting, because it's expensive and the thing is supposed to be
       // invisible anyway.
       Lighting Off
       // Draw into the depth buffer in the usual way.  This is probably the default,
       // but it doesn't hurt to be explicit.
       ZTest LEqual
       ZWrite On
       // Don't draw the RGB colour channels, just the alpha channel.  This means
       // that nothing visible is drawn.  Ideally, I would have liked to set ColorMask
       // to draw nothing at all, but it doesn't seem to be possible to say anything
       // like ColorMask None.
       ColorMask A
       // Do nothing specific in the pass:
       Pass {}

} </shaderlab>

Personal tools