DepthMask

From Unify Community Wiki
Revision as of 14:37, 28 May 2006 by NCarter (Talk | contribs)

Jump to: navigation, search

Author: Neil Carter (NCarter)

Contents

Description

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.

Usage

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: boat_3.zip, 210KB

ShaderLab - DepthMask.shader

<shaderlab> Shader "DepthMask" {

   SubShader
   {
       // 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
Namespaces

Variants
Actions
Navigation
Extras
Toolbox