DepthMask

From Unify Community Wiki
Revision as of 21:03, 17 February 2009 by Danielbrauer (Talk | contribs)

Jump to: navigation, search

Author: Neil Carter (NCarter) and Daniel Brauer (Danielbrauer)

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.

Rendering order is handled automatically using Render Queues.

Anything that needs holes cut in it must use a shader that renders after the mask. Examples of how to make the normal diffuse and specular shaders work with masks are given below.

Example Project

Unity 1.2.2 project: boat_3.zip, 210KB (made obsolete by shaders below in 2.1 project, coming soon!)

ShaderLab - DepthMask.shader

<shaderlab> Shader "Masked/Mask" {

   SubShader {

// Render the mask after regular geometry, but before masked geometry and // transparent things.

Tags {"Queue" = "Geometry+10" }

       // 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>

ShaderLab - MaskedDiffuse.shader

<shaderlab> Shader "Masked/Diffuse Masked" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { // This shader does the same thing as the Diffuse shader, but after masks // and before transparent things Tags {"Queue" = "Geometry+20" } UsePass "Diffuse/BASE" UsePass "Diffuse/PPL" } FallBack "Diffuse", 1 } </shaderlab>

ShaderLab - MaskedSpecular.shader

<shaderlab> Shader "Masked/Specular Masked" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1) _Shininess ("Shininess", Range (0.01, 1)) = 0.078125 _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { // This shader does the same thing as the Specular shader, but after masks // and before transparent things Tags {"Queue" = "Geometry+20" } UsePass "Specular/BASE" UsePass "Specular/PPL" } FallBack "Diffuse", 1 } </shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox