- Home /
GL.Lines over Game Objects
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.
Answer by Bunny83 · Apr 09, 2013 at 12:33 PM
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
}
}
}
}");
Hey i am facing the same issue. Im drawing a shape with GL . but I want a transparency effect when i used the above shader it makes and opaque black shape. I change the render type to transparent still its the same. $$anonymous$$y current version of the shader is
public static Color color = new Color(0,0,0,0.3f);
static void CreateLine$$anonymous$$aterial(){
if( !line$$anonymous$$aterial ) {
line$$anonymous$$aterial = new $$anonymous$$aterial( "Shader \"Lines/Colored Blended\" {" +
"SubShader { Tags { \"RenderType\"=\"Transparent\" } Pass { " +
" Blend SrcAlpha One$$anonymous$$inusSrcAlpha " +
" ZWrite On ZTest LEqual Cull Off Fog { $$anonymous$$ode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }" );
line$$anonymous$$aterial.hideFlags = HideFlags.HideAndDontSave;
line$$anonymous$$aterial.shader.hideFlags = HideFlags.HideAndDontSave;
}
}
IF i remove the blend line in the pass tag. it draws an opaque shape. the two version are below first with the line and second without the blend line in the pass tag.
I want the the effect like the first image but it draws over everything. I wan ti to be over the groudn and some other objects.