DepthMask
From Unify Community Wiki
Author: Neil Carter (NCarter) and Daniel Brauer (Danielbrauer)
Contents |
[edit] Description
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.
[edit] 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.
[edit] Example Project
Unity 2.1 project: Boat 4.zip, 532KB
[edit] ShaderLab - DepthMask.shader
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 {}
}
}
[edit] ShaderLab - MaskedDiffuse.shader
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
}
[edit] ShaderLab - MaskedSpecular.shader
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
}
