- Home /
Drawing 3D Lines with GL?
I've seen a bunch of example answers here for drawing lines in 3D using GL in Unity and I am just having no luck at all, I typically only get one line run across my screen, I can never get more than one line and it also doesn't appear to be running in 3D rather it looks orthographic on my screen (although I am not loading ortho).
Can anyone explain if I need to draw my lines on a 3D surface (such as a plane) or can they simply be drawn in world space?
As an example to test what can or can't be done I am attempting to draw a square in world space from 0,0,0 that is 10 units in x and 10 units in y.
I'd post my code but there isn't a lot of point as I've typically been copying other threads such as http://answers.unity3d.com/questions/482128/draw-grid-lines-in-game-view.html and http://answers.unity3d.com/questions/149210/creating-gl-lines.html.
still posting code might help because 1 line change might give a totally different output in gl...
Answer by Bunny83 · May 08, 2014 at 11:20 AM
I think you expect too much from GL lines ;). OpenGL is a very mathematic / physic based API. A line has actually no width at all it's infinite thin, otherwise it would have an area and therefore is not a line anymore. Of course to see / render a line it has to have some width. In OpenGL you could set the linewidth, in Unity it's always 1. So each line you draw always is 1 pixel wide, no matter if it's 10000 units away or 0.01 infront of your camera.
Actually everything the GPU renders is rendered in 2d-screen-space. Things rendered in distance appear smaller because the projected vertex-positions are closer to each other. A line however never look thicker since it doesn't have any thickness. All the GPU does is projecting the two endpoints into screenspace and draw a 1-pixel-line between them.
If you want a line to appear thicker when it's closer to the screen you have to draw them as quad-billoards like the LineRenderer does. There are several assets in the asset store which can draw lines, one of the best know is eric's Vectrosity.
Keep in mind that billboard lines (quads) always look / behave a bit "strange" when you view them from different angles due to the billboarding. If the view is always the same you don't notice this. Here's an example of Quake3
the answer is pretty good but to add to it, if you still want to draw a line of certain thickness you have to create a polygon around the line render it as a quad.
look at this image
or to avoid confusion you can just use this script from here this wiki which does nearly the same!
Answer by Kaz_Yamof · May 08, 2014 at 12:07 PM
You just have to set the position of the initial point and the final point. You also need to write two commands of draw lines in sequence. If you have a vector with your points, do something like this:
//Script attached on camera main
private void OnPostRender()
{
Vector3[] points = new Vector3[10];
//points = initialize some values to array...
for (int i = 1; i < points.Length; i++)
{
GL.Vertex(points[i - 1]);
GL.Vertex(points[i]);
}
}
Instead to do this (will not work):
[...]
for (int i = 0; i < points.Length; i++)
{
GL.Vertex(points[i]);
}
But keep in mind that it will just draw one line on screen each time. If you want more than that, create a vector/list of Vector3[] array, with the coordinates of each line, and iterate by the list, and then iterate by the coordinates of the actual array to draw.
foreach (Vector3[] points in ListPoints)
{
for( int i =0; i < points.Length; i++)
{
GL.Vertex(points[i - 1]);
GL.Vertex(points[i]);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613144519im_/https://answers.unity.com/themes/thub/images/avi.jpg)