DepthMask

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Example Project: Fixed link)
(Updated to use Render Queues)
Line 1: Line 1:
Author: Neil Carter ([[:User:NCarter|NCarter]])
+
Author: Neil Carter ([[:User:NCarter|NCarter]]) and Daniel Brauer ([[:User:Danielbrauer|Danielbrauer]])
  
 
== Description ==
 
== Description ==
Line 10: Line 10:
 
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.
 
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:
+
Rendering order is handled automatically using Render Queues.
  
*The first draws the boat hull and all other normal objects.
+
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.
*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 ==
 
== Example Project ==
  
Unity 1.2.2 project: [http://www.nether.org.uk/files/boat_3.zip boat_3.zip, 210KB]
+
Unity 1.2.2 project: [http://www.nether.org.uk/files/boat_3.zip boat_3.zip, 210KB] (made obsolete by shaders below in 2.1 project, coming soon!)
  
 
==ShaderLab - DepthMask.shader==
 
==ShaderLab - DepthMask.shader==
  
 
<shaderlab>
 
<shaderlab>
Shader "DepthMask"
+
Shader "Masked/Mask" {
{
+
     SubShader
+
     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
 
         // Turn off lighting, because it's expensive and the thing is supposed to be
 
         // invisible anyway.
 
         // invisible anyway.
 
+
 
         Lighting Off
 
         Lighting Off
  
Line 51: Line 51:
 
         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 Diffuse shader, but after masks
 +
// and before transparent things
 +
Tags {"Queue" = "Geometry+20" }
 +
UsePass "Specular/BASE"
 +
UsePass "Specular/PPL"
 +
}
 +
FallBack "Diffuse", 1
 
}
 
}
 
</shaderlab>
 
</shaderlab>

Revision as of 20:59, 17 February 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 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 Diffuse 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