How to draw GL Lines over GameObjects/Images?

I am drawing lines between GameObjects to show their connection (showing a graph) using GL Lines and it always worked fine until I created a background Image using a Sprite. GameObjects are always hiding the Lines and because of the Background Image I can’t see the lines anymore. Someone else had the reverse problem here: GL.Lines over Game Objects - Questions & Answers - Unity Discussions and I tried to use this shader but it doesn’t work for me.

The code I have under OnPostRender() looks like this:

Shader shader = Shader.Find("Lines/Colored Blended");
Material lineMat = new Material(shader);
GL.PushMatrix();
lineMat.SetPass(0);
GL.LoadOrtho();
for (int i = 0; i < drawingGraph.edges.Count; i++)
{
    Vector3 start = startObj.transform.position;
    Vector3 end = endObj.transform.position;
    GL.Begin(GL.LINES);
        GL.Vertex3(start.x / Screen.width, start.y / Screen.height, start.z);
        GL.Vertex3(end.x / Screen.width, end.y / Screen.height, end.z);
    GL.End();
}
GL.PopMatrix();

The shader I’m using is this from the other post:

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 
			}
		}
	} 
}

And that’s how it looks without the background GameObject:
82546-graph2.png
And here with the background:
82547-graph1.png

Can you guys tell me what I’m doing wrong?
Thanks!

It looks like your lines are under your background.

1 Answer

1

I solved it!
In case anyone else has that problem: The canvas render mode was set to “Screen Space - Overlay”. I switched to “Screen Space - Camera” and it worked.