GL.LINES dashed (stippled) - why?

If I only use 2 vertices for GL.LINES, I get a solid line as expected. However for some reason if I use more than 2 vertices, the line ends up looking dashed. I’m no GL expert, but a Google found glLineStipple is a command to draw lines like this. Why would Unity be doing it automatically, and only when multiple vertices are specified? Am I missing something? Here is my glLine() function in its entirety:

private static Material glLineMaterial = null;

public static void glLine(Vector3[] vectors, Color color)
{
	if (glLineMaterial == null)
	{
        glLineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
            "SubShader { Pass { " +
            "    Blend SrcAlpha OneMinusSrcAlpha " +
            "    ZWrite Off Cull Off Fog { Mode Off } " +
            "    BindChannels {" +
            "      Bind \"vertex\", vertex Bind \"color\", color }" +
            "} } }" );
        glLineMaterial.hideFlags = HideFlags.HideAndDontSave;
        glLineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
	}
			
	GL.PushMatrix();
	glLineMaterial.SetPass(0);
	GL.LoadOrtho();
	GL.Color(color);
	GL.Begin(GL.LINES);
	foreach (Vector3 vector in vectors)
	{
		// The Vector3 values come in as screen coords, but need to be converted to 0.0-1.0 for OpenGL.
		Vector3 vectorGL = new Vector3(vector.x / Screen.width, vector.y / Screen.height, 0);
		GL.Vertex(vectorGL);			
	}
	GL.End();
	GL.PopMatrix();
}

Glad to hear! Just a note that you might have to review this if you ever add walls that are diagonal.

1 Answer

1

I figured it out, though it’s not documented. In order to have solid lines between each vertex, you need to have pairs of vertices for each actual line section. Here is a fixed version of the code to take care of that:

private static Material glLineMaterial = null;

public static void glLine(Vector3[] vectors, Color color)
{
	if (glLineMaterial == null)
	{
        glLineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
            "SubShader { Pass { " +
            "    Blend SrcAlpha OneMinusSrcAlpha " +
            "    ZWrite Off Cull Off Fog { Mode Off } " +
            "    BindChannels {" +
            "      Bind \"vertex\", vertex Bind \"color\", color }" +
            "} } }" );
        glLineMaterial.hideFlags = HideFlags.HideAndDontSave;
        glLineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
	}
			
	GL.PushMatrix();
	glLineMaterial.SetPass(0);
	GL.LoadOrtho();
	GL.Color(color);
	GL.Begin(GL.LINES);
	
	int i = 0;
	
	foreach (Vector3 vector in vectors)
	{
		// The Vector3 values come in as screen coords, but need to be converted to 0.0-1.0 for OpenGL.
		Vector3 vectorGL = new Vector3(vector.x / Screen.width, vector.y / Screen.height, 0);
		GL.Vertex(vectorGL);
		if (i > 0 && i < vectors.Length - 1)
		{
			GL.Vertex(vectorGL);				
		}
		i++;
	}
	GL.End();
	GL.PopMatrix();
}