Hello all!
I really have no experience on shaders, but I’m trying to solve a problem:
I’m using a volumetic fog plugin (Fog Volume 3) that to correctly work need the objects inside the fog to have a shader that write into the Z-Buffer of the scene.
Well, I’d need to put into the fog some (many) transparent objects (texture with alpha channel), not cutout, and I know that standard tranparent shaders do not write into the Z-Buffer.
So, I’ve seen in the Unity manual that this could be achieved with this code
Shader "Transparent/Diffuse ZWrite" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
// extra pass that renders to depth buffer only
Pass {
ZWrite On
ColorMask 0
}
// paste in forward rendering passes from Transparent/Diffuse
UsePass "Transparent/Diffuse/FORWARD"
}
Fallback "Transparent/VertexLit"
}
As Unity manual say (Unity - Manual: ShaderLab: commands) this shader should write into the Z-Buffer.
But, unfortunately, this seem not to work: with the “Fog Volume 3” plugin come a Scene Depth Viewer, that do not show the objects with this shader (but correctly show the objects with non transparent shader).
Maybe I’m missing something somewhere else in Unity? Or, maybe, I’m using the wrong code?
Many thanks for your help!
This is incorrect. Objects do not need to write to the Z-Buffer, but rather the camera depth texture. Only objects that are in the opaque queues (0~2499) and have a shadowcaster pass write to the camera depth texture, but you can make a transparent shader with a queue of 2499 and a Fallback “VertexLit” (not “Transparent/VertexLit”) and it’ll show up in the camera depth texture. However this has serious implications elsewhere, such as transparency sorting and the directional shadow receiving. The sorting issue can be solved with some additional coding on your end (injecting your transparent objects into the depth texture manually), but the other two aren’t as easily solved with out changes to the Fog Volume 3 asset.
There are other volumetric assets that do support transparencies better, like Volumetric Fog & Mist and Hx Volumentric Lighting , but neither offer quite the features of Fog Volume 3. The real solution is getting @DavidMiranda to take a pass at supporting this in some way.
“Queue” = “Geometry+2499”
That’s a queue of 4499 since Geometry is 2000 already.
You also don’t need the extra shadowcaster pass there. The Fallback shader already has one, so the shader will use that. The one you have there isn’t properly defined anyway. See this page if you’re curious what it should look like:
That page also uses UsePass to include only the shadow caster pass from the VertexLit shader, which is probably more correct, but all of Unity’s built in shaders just use FallBack.