+3

Proper Depth / Depth Normal Rendering

rcalt2vt 10 years ago 0
Ran into an issue in Shaderforge 0.34 where an Alpha Clip shader I wrote was not rendering out to the depth texture. This issue has come up in a few various topics here but the exact reasons as to why it wasn't working or why adding _Cutoff property was fixing the issue in some cases but not all. So digging through everything, here's the details on exactly how the Depth and Depth Normal textures are created.

Internally, when set, Unity will use a replacement shader to render all visible objects to the depth textures. The replacement shaders can be found in the DefaultResources directory of the included Build-In Shaders download located here: http://unity3d.com/unity/download/archive

The exact replacement shader files are Camera-DepthNormalTexture.shader and Camera-DepthTexture.shader Those files contain replacement shaders for each of the various Unity render types: Opaque, TransparentCutout, TreeBark, etc. The replacement shader requires 3 parameters in all shaders in order to properly render the Depth / Depth Normal textures. Those are: _MainTex which contains an alpha channel, _Cutoff which is the alpha cutoff value, and _Color which is multiplied with _MainTex.

As long as any shader you create in Shaderforge has those 3 values, everything works as expected.

This also pointed out a minor unexpected result where Shaderforge was automatically subtracting 0.5 from the alpha clip value in the clip command: clip(node_12.a - 0.5); where it might be more appropriate to just use clip(node_12.a) and expect the user to subtract the _Cutoff slider before connecting it into the Alpha Clip connection.