Why drawing GL quads is so slow?
Hi, in my game I'm trying to draw a curve with a specified width. I divided it to small rectangle pieces. I'm using GL.QUADS from this example https://docs.unity3d.com/ScriptReference/GL.QUADS.html. Everything works fine on PC Windows, but when I try to run it on my Xperia E4G the game crashes or plays at really low fps. There are around 300 quads at the same time rendered. Here is my rendering function:
void OnPostRender()
{
GL.PushMatrix();
GL.LoadProjectionMatrix(GetComponent<Camera>().projectionMatrix);
var node = extendedNodes.First;
while (node != null)
{
GL.Begin(GL.QUADS);
GL.Color(new Color(0, 199, 0));
for (int i = 0; i < 4 && node != null; i++)
{
GL.Vertex(node.Value);
node = node.Next;
}
GL.End();
}
GL.PopMatrix();
}
Have you got any ideas why the problem exists (I thought that calling GL functions should be fast, I don't want to use LineRenderer because it is slow too)?
I'm wondering if it wouldn't be faster just to make a $$anonymous$$esh and let Unity draw it? For that matter, you could probably speed up what you have using GL tri-strips.
Usual caveat: only if what you have isn't good enough already.
Thanks for commenting. Curve is generated by a mathematical function and it will be rendered continously through whole game level (of course only visible part will be rendered). If you know any other faster method let me know :)
Just with the method you're using now, switching to GL.Begin(TRI_STRIP)
should be a speed-up. You send every vertex once, ins$$anonymous$$d of twice (but you have to figure out how they work. You'd be faking a quad-strip using a tri-strip.)
If the curve changes every frame, looking at the Unity $$anonymous$$esh class doesn't seem worth it.
Thanks, I changed to TRI_STRIP and it seems to be faster now.
Answer by fullspeed · Mar 29, 2017 at 12:13 PM
I figured out what was wrong... I was calling GL.Begin(GL.QUADS) every 4 vertexes I was drawing. Here is fixed code:
void OnPostRender()
{
GL.PushMatrix();
GL.LoadProjectionMatrix(GetComponent<Camera>().projectionMatrix);
GL.Begin(GL.QUADS);
GL.Color(new Color(0, 199, 0));
var node = extendedNodes.First;
while (node != null)
{
GL.Vertex(node.Value);
node = node.Next;
}
GL.End();
GL.PopMatrix();
}