use of GL matrices

I’m struggling for 2 days to draw a line using GL.Lines. I want to draw based on world space coords. I know GL code works in viewport space (0, 0) bottom left, (1, 1) top right. The documentation is dazzling as it mentions about camera space, viewport space and screen space, let alone the other spaces. So I think I need a matrix like worldToViewportMatrix; there’s Camera.worldToCameraMatrix but camera space is not explained.
Beside what matrix to use and where to get it from, another problem is that I don’t know how to do it: GL.Load…() or GL.MultMatrix().
Here’s what I tried so far:

 // called in Camera script, inside OnPostRender()
public static void DrawLine(Vector3 start, Vector3 end, Material mat)
            {
            //start = Camera.current.WorldToScreenPoint(start);
            //end = Camera.current.WorldToScreenPoint(end);

            start.x /= Screen.width;
            start.y /= Screen.height;
            end.x /= Screen.width;
            end.y /= Screen.height;

            Matrix4x4 W2CM = Camera.current.worldToCameraMatrix;

            MonoBehaviour.print("start = " + start);
            MonoBehaviour.print("end = " + end);
            MonoBehaviour.print(Screen.width + " " + Screen.height);

            GL.PushMatrix();
            GL.LoadProjectionMatrix(W2CM);
            //GL.MultMatrix(W2CM);

            mat.SetPass(0);
            //GL.LoadOrtho();
            GL.Begin(GL.LINES);
            GL.Color(Color.white);
            GL.Vertex(new Vector3(start.x, start.y, 0));
            GL.Vertex(new Vector3(end.x, end.y, 0));
//            GL.Vertex(new Vector3(0.25f, 0.1f, 0));
//            GL.Vertex(new Vector3(0.5f, 0.75f, 0));
            GL.End();
            GL.PopMatrix();
            }

Have you tried using the LineRenderer? Its much simpler.
What values are you passing in to start and end, are they world space?

Thank you for laying a hand.
I know of LineRenderer but I want to draw tens to hundreds of lines as visual indicators on top of regular gameobjects. In addition I also want to draw circles, n-gon’s, starbursts (concurrent lines) and text. Something like UI elements.
And yes, I want to draw them using world space coordinates. Ideally every gameobject’s script draws its own set of lines, circles etc. – hence the world space.
So I thought GL would be almost perfect if it weren’t for the text. But the cumbersome part is making every gameobject’s script “draw” (in fact the workaround is putting the primitives and their world coordinates in a structure) and at post-render time make the camera draw the structure (intrinsically transformed to viewport space by using a matrix loaded only one time for the whole structure, instead of explicitly transforming hundreds of coordinates).

The documentation contains two examples to draw lines. In case you didn’t find them, here are the links: GL.LINES and GL.

The following information is based on performance profiling I did using Unity 4.3, so it might be incorrect for Unity 5:
If you want something high-performance, forget the GL API. Submitting vertex data such as GL.Vertex, GL.Color, etc is slow.

PS: Maybe Vectrosity could be of interest?

1 Like

Thank you. I’ve already read the documentation including the links you provided. Yet I can’t relate the snippets or explanations in there, to my case. Those are exactly where I adapted and made my code from but I’m stuck in a damn matrix. And I can’t find any Neo around to come to rescue :sunglasses:
As for Vectrosity, it’s much more complex that what I need. I only need the code worth of drawing disappointingly basic lines, albeit many ones. I don’t have the bucks for Vectrosity but I’ve read it’d be fast. They should make a light version with simple colored lines, w/o beziere, line cap, width etc.