Writing a fixed function shader to render occluded GL lines with a different alpha

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:

75595-order2.png

Swapping the two Passes in the shader, I can get the occluded part to render now, but the visible parts are now missing:

75596-order1.png

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().

Well, you only render one pass of your shader. You have to do this:

void OnPostRender()
{
    GL.PushMatrix();
    for (int i = 0; i < glMat.passCount; i++)
    {
        glMat.SetPass(i);
        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();
}

A lot people seem to not fully understand what a second pass actually means. It means the whole object has to be drawn again, just with a different pass. That’s why a second pass usually causes another “drawcall”. That’s also why Unity now seperates those in the stats window and lists “SetPass calls” seperately.