Hi, I made a grid (like we can see in Unity) with GL.Lines, using this shader:
static void CreateLineMaterial()
{
if( lineMaterial == null )
{
lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
"SubShader {Tags { \"RenderType\"=\"Overdraw\" } Pass { " +
"ZWrite Off ZTest Always Cull Off Fog { Mode Off } " +
"BindChannels {" +
"Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
}
}
The problem is that the grid is always over the others game objects, i.e, the lines that I draw overwrite the others item in the scene. I believe this is cause of the shader, but I don’t understand much about it…
What do I have to do to stop this behavior?
Thanks in advance.
That’s simply because your shader doesn’t perform a depth test and doesn’t write to the z-buffer. Try this:
lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
"SubShader {Tags { \"RenderType\"=\"Opaque\" } Pass { " +
"ZWrite On ZTest LEqual Cull Off Fog { Mode Off } " +
"BindChannels {" +
"Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");
I simply set the ZTest to the default LEqual (less or equal) turned on writing to the z buffer “ZWrite On” and changed the RenderType to “Opaque”
btw here’s the same shader with the @-string-syntax which allows multiline strings so it’s more readable:
lineMaterial = new Material(
@"Shader ""Lines/Colored Blended"" {
SubShader {
Tags { ""RenderType""=""Opaque"" }
Pass {
ZWrite On
ZTest LEqual
Cull Off
Fog { Mode Off }
BindChannels {
Bind ""vertex"", vertex Bind ""color"", color
}
}
}
}");