[SOLVED] Drawing lines in game not working

I’m trying to draw some lines in game to eventually create a simple grid. From the research I’ve done it seems GL is the best way to get this done but I’m having trouble making it work.

public class Grid : MonoBehaviour
{
    public Shader shader;

    public void AddLine(Vector3 s, Vector3 e, bool tmp)
    {
        lp[0] = s;
        sp[0] = e;
    }

    private static GameObject g;
    private static Material m;

    private Vector3[] lp = new Vector3[100];
    private Vector3[] sp = new Vector3[100];

    private void Start()
    {
        g = gameObject;
        m = new Material(shader);
        AddLine(new Vector3(0, 0, 0), new Vector3(10, 0, 0), false);
    }

    private void OnPostRender()
    {
        m.SetPass(0);
        GL.PushMatrix();

        GL.MultMatrix(g.transform.transform.localToWorldMatrix);
        GL.Begin(GL.LINES);
        GL.Color(new Color(0, 0, 0, 0.4f));

        for (int i = 0; i < lp.Length; i++)
        {
            GL.Vertex3(lp[i].x, lp[i].y, lp[i].z);
        }

        GL.Color(new Color(0, 0, 0, 0.1f));

        for (int i = 0; i < sp.Length; i++)
        {
            GL.Vertex3(sp[i].x, sp[i].y, sp[i].z);
        }

        GL.End();
        GL.PopMatrix();
    }
}

GL.Vertex needs to take points in pairs, start-end-start-end-start-end etc
Also get rid of line 29, it’s not needed for drawing connecting world points

Don’t know what you mean on “GL.Vertex needs to take points in pairs, start-end-start-end-start-end etc”

I don’t see what I’m doing wrong from reading the Unity docs.

Asuming your arrays lp and sp are start points and end points (void AddLine has ‘s’ and ‘e’ - start and end ?)
You are calling Vertex3: first with all the start points, then with all the end points
since Vertex3 expects you to pass pairs of points (one start and then one end), it’s trying to draw a line between the start of line1 and the start of line 2, etc

Can you show me what you mean? According to the docs GL.Vertex3 takes a start X, Y, and Z.

What he means is that if you wanted to draw a line between two points, you’d do this:

GL.Vertex3(startPoint.x,startPoint.y,startPoint.z);
GL.Vertex3(endPoint.x,endPoint.y,endPoint.z);

Instead what you’re doing is this:

for (int i = 0; i < lp.Length; i++)
        {
            GL.Vertex3(lp[i].x, lp[i].y, lp[i].z);
        }

So what you’re doing is saying “connect the first start point to the second start point, etc.,” instead of saying “connect the first start point to the first END point.” What you’re really doing is connecting the start points to all the other start points, then connecting the end points to all the other end points. What you really want to do is connect the start points to the end points something like this:

private void OnPostRender()
    {
        m.SetPass(0);
        GL.PushMatrix();

        GL.MultMatrix(g.transform.transform.localToWorldMatrix);
        GL.Begin(GL.LINES);

        for (int i = 0; i < lp.Length; i++)
        {
            GL.Color(new Color(0, 0, 0, 0.1f));
            GL.Vertex3(sp[i].x, sp[i].y, sp[i].z);
            GL.Color(new Color(0, 0, 0, 0.4f));
            GL.Vertex3(lp[i].x, lp[i].y, lp[i].z);

        }
        GL.End();
        GL.PopMatrix();
    }

See how this version uses two GL.Vertex3 calls in the loop instead of just one? If you were doing triangles (three verts instead of two) you’d want three verts in there.

1 Like

@Todd-Wasson Thank you that worked!

1 Like