I’m trying to write a multi-pass shader that I’ll be using for colouring lines and shapes drawn by the various GL
methods. The intention of the shader is to render visible parts of the shapes using the supplied colour in GL.Color()
, and rendering occluded parts using a transparent variant of that colour. However, for some reason my shader can only achieve one of those two goals, and not both at the same time.
Here’s my current shader:
Shader "Custom/GLShader" {
SubShader {
BindChannels {
Bind "vertex", vertex
Bind "color", color
}
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
Fog { Mode Off }
}
Pass
{
Blend SrcAlpha One
ZTest Greater
ZWrite Off
Cull Off
Fog { Mode Off }
}
}
Fallback "Diffuse"
}
And here’s the relevant parts of the C# script attached to my main camera:
public Shader glShader;
private Material glMat;
void Awake()
{
glMat = new Material(glShader);
}
void OnPostRender()
{
glMat.SetPass(0);
GL.PushMatrix();
GL.Begin(GL.QUADS);
GL.Color(Color.red);
GL.Vertex3(0, 0, 0);
GL.Vertex3(5F, 0, 0);
GL.Vertex3(5f, 5F, 0);
GL.Vertex3(0, 5f, 0);
GL.End();
GL.PopMatrix();
}
This results in the visible parts of the shape being rendered properly, but not the occluded parts:
Swapping the two Passes in the shader, I can get the occluded part to render now, but the visible parts are now missing:
I feel like I’ve misunderstood how a Pass works, and need some clarification on how I can tweak the shader to make this work - or if that isn’t possible, what the best approach for this would be that I could still use in conjunction with my C# code.
I did find some similar questions like the shader provided at Render Occluded pixels to gray color, but I’m not sure how to adapt that to how I’m currently drawing shapes (since they won’t have a texture, only a colour given by GL.Color()
.