DepthMask

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
m (Reverted edits by Joseph05408 (Talk); changed back to last version by Danielbrauer)
Line 1: Line 1:
 +
Author: Neil Carter ([[:User:NCarter|NCarter]]) and Daniel Brauer ([[:User:Danielbrauer|Danielbrauer]])
  
 +
== Description ==
 +
 +
[[Image:DepthMaskShader.png|thumb|100px|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 2.1 project: [http://torch.cs.dal.ca/~brauer/Boat%204.zip 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>

Revision as of 19:16, 19 October 2009

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