DepthMask

From Unify Community Wiki
Revision as of 17:00, 7 August 2011 by Podperson (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.

Alternate Use

If you're trying to create a "hole" or "window" in a scene (e.g. you want the skybox to show through and clip out any geometry behind a rendered object) use the DepthMask shader with the tags line commented out. This essentially turns the shaded object into an efficient "eraser" that knocks the pixels covered by the object out of the render.

Example Project

Unity 2.1 project: Boat 4.zip, 532KB

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 anything into the RGBA channels. This is an undocumented
       // argument to ColorMask which lets us avoid writing to anything except
       // the depth buffer.
       ColorMask 0
       // 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