How to draw *many* lines

For one of the game ideas that I have, I need to render a lot of lines every frame with blending on. I know that msg.post("@render:", "draw_line"... is supposed to be just for debugging, but I tried it anyway and it is too slow (expected)
One can draw lines by drawing sprites that are W x 1 in resolution, and by adjusting scale, translation, and rotation for each line. Could be faster. Question: if I want to try this latter approach, how to I draw multiple such sprites with the same GO+script?
Any other, better ideas?
I would prefer to get 10,000+ alpha-blended lines per frame, whose positions are generated from scratch each frame.

2 Likes

If the lines are of different colors or alphas, then batching will break if you just use game objects. Maybe try a GUI scene for that? Still… 10k is a bit extreme. I’m pretty sure a good amount of those lines are procedurally generated. If so, maybe you can do some form of ray tracing/marching in a shader or some other sort of an approximation in a shader?

1 Like

Hm, something along these lines (pun intended) perhaps?

  • generate a mesh offline that contains 10k quads
  • generate a F32 render texture in the render script that contains your render properties: position, rotation, color and alpha.
  • each frame:
  • update texture with your current state
  • in the vertex shader, sample the property texture based on incoming vertex position and transform the vertex accordingly, and pass color properties to the fragment shader

It’s a bit more work than using sprites for sure, but you would get 1 draw call in total so that’s nice at least :slight_smile:

8 Likes

10k is a big number. Using quad models/sprites with factory create could be hard on performance. Theoretically it’s possible to write native extension that will render your lines into a render target using “instancing”, but it’s very hard. Recalculating line coordinates in C++ is also a boost for performance.

2 Likes

I already have c++ code for an xcode project, so that is hopefully not a problem :slight_smile:

1 Like

Makes sense! Thanks for this idea! Hope to get time to try that out some time soon. Are there any examples that update a texture from the script?

1 Like

A pure Lua version of drawing to a texture can be seen here: https://github.com/britzl/publicexamples/tree/master/examples/drag_to_scroll

For an example of how to pass the buffer from Lua to native code please check the DrawPixels extension in the asset portal.

3 Likes

If you create this NE, I will personally block it from the asset portal unless you call it “Draw the line”.

10 Likes