Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

use of GL matrices

Discussion in 'Shaders' started by LaJean, Sep 9, 2015.

  1. LaJean

    LaJean

    Joined:
    Sep 6, 2015
    Posts:
    10
    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:
    Code (CSharp):
    1.  // called in Camera script, inside OnPostRender()
    2. public static void DrawLine(Vector3 start, Vector3 end, Material mat)
    3.             {
    4.             //start = Camera.current.WorldToScreenPoint(start);
    5.             //end = Camera.current.WorldToScreenPoint(end);
    6.  
    7.             start.x /= Screen.width;
    8.             start.y /= Screen.height;
    9.             end.x /= Screen.width;
    10.             end.y /= Screen.height;
    11.  
    12.             Matrix4x4 W2CM = Camera.current.worldToCameraMatrix;
    13.  
    14.             MonoBehaviour.print("start = " + start);
    15.             MonoBehaviour.print("end = " + end);
    16.             MonoBehaviour.print(Screen.width + " " + Screen.height);
    17.  
    18.             GL.PushMatrix();
    19.             GL.LoadProjectionMatrix(W2CM);
    20.             //GL.MultMatrix(W2CM);
    21.  
    22.             mat.SetPass(0);
    23.             //GL.LoadOrtho();
    24.             GL.Begin(GL.LINES);
    25.             GL.Color(Color.white);
    26.             GL.Vertex(new Vector3(start.x, start.y, 0));
    27.             GL.Vertex(new Vector3(end.x, end.y, 0));
    28. //            GL.Vertex(new Vector3(0.25f, 0.1f, 0));
    29. //            GL.Vertex(new Vector3(0.5f, 0.75f, 0));
    30.             GL.End();
    31.             GL.PopMatrix();
    32.             }
     
    Last edited: Sep 10, 2015
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
    Have you tried using the LineRenderer? Its much simpler.
    What values are you passing in to start and end, are they world space?
     
  3. LaJean

    LaJean

    Joined:
    Sep 6, 2015
    Posts:
    10
    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).
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,450
    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?
     
    karl_jones likes this.
  5. LaJean

    LaJean

    Joined:
    Sep 6, 2015
    Posts:
    10
    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 :cool:
    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.