Mesh batching in Codea?

Is it possible to batch together multiple mesh drawcalls into one drawcall?

I know that I can bunch up everything into one mesh and draw that with one call but the issue there is that I cannot draw only a couple of those meshes.

The application for this is terrain grass, there is alot of it and its everywhere and it is just duplications of the same mesh.

To draw 2000 grass meshes at runtime would not be as good as drawing 2000 grass ‘pieces’ in one mesh at runtime. But what if you only want to draw 500 of those 2000, and maybe the next frame you want to draw 540 of those.

I know it is possible to some extent on a computer but I do not know if it is possible here on Codea,or is it?

@Muffincoder - have a look at this technique, which tiles an image across a whole mesh without the need to break it up into small pieces.

It uses a “shader”, but you can just include that code like a black box and worry about understanding it later. Shaders are extremely useful for 3D terrain, so they are worth learning, and I wrote an ebook all about them.

@Muffincoder take a look at mesh:addRect(x,y,w,h,a)

@Ignatz What you’re explaining is a way to tile one image across a mesh, I’m talking about batching drawcalls for multiple meshes, I can’t really see if this has anything to do with what I meant or if I’m just that bad at explaining :S

@Luatee That’s one way to add a 2D polygon to one mesh, it seems to me a bit irrelevant in this topic.

Just to make things more clear, I’m talking about 3D meshes placed randomly in a 3D world, and I would like to draw many of those efficiently preferably using batching(if possible)

@Muffincoder - if they are each placed randomly, then you have to draw each of them separately, or else you need to keep adding/deleting vertices to a single mesh. There is no way to tell a mesh to only draw part of itself.

If your grass forms most of the terrain, one possible option is to tile it across the entire terrain, using the technique I mentioned above (if your terrain is flat and rectangular, this requires just two [2] triangles), then cover it with non grass texture where applicable.

@Muffincoder you could simulate what you want by changing the x coordinates of the rects you want to desactivate so they are very far and not drawn (say x = 100000). When you want them back you set the good x coordinate back. There would be another solution, setting the color of the rect to (0,0,0,0), but it would be less efficient.

@Muffincoder ahh I see you want multiple vector meshes… If they’re all different then there’s not really an efficient way of doing it, if they’re all blocks for example (even different sizes) you can use translate and rotate drawing the mesh after each translation, I don’t know if that helps.

I do know that on desktop openGl there is something called VBOs which means that you just upload one mesh to the GPU and then just send coordinates where to draw that mesh, not needing to send whole mesh again to the GPU. I am not sure that this is available to mobile devices though…

And creating one mesh per grass block/bunch is not feasible, the memory usage is way too great(I tested it…)

@Muffincoder - what did you think of my suggestion above?

As far as I understand there is no support for VBOs in Codea, but I think it is supported in iOS. Codeas interface is a bit simpler, so easier to use, but somewhat limited.

Here is a thread with some old grass code, if it can be of any help:

What you can do is create a mesh with 500 object an use a mesh:buffer to position them or disable drawing in the vertex shader by setting positions to 0 for the object.

Dont know how fast it is to replace the buffer with buffer:set, if you want to draw several calls to the 500 object mesh. Try it out and tell us! :slight_smile:

Sorry for replying this late but I had a break from coding a few days.

I’ve just come to the conclusion that I can just bunch up the grass objects into smaller meshes and draw the ones necessary, going any deeper into each grass object just isn’t worth it for such a simple cosmetic thing as grass.

Thanks for your input!

Through Codea it definitely does cache the vertices (and other buffers) in the GPU, so be wary of dynamically changing the content of buffers as it will have to pass the full buffer to the GPU each time you change it and that can be expensive.

If you have multiple meshes there is no way I am aware of to say draw them all in one go, you have to ask the gpu for each one. For performance you need to find the balance between many small meshes that are easy to control and manipulate and a massive mesh that you don’t change.

You can do mojo in shaders where you have a big mesh, and rather than sending new vertices you control them with uniforms or even a texture and dynamically manipulate the vertices in the vertex shader. This can give great performance if you have a use case that fits it. Some examples of this are:

And I’m sure I did one which was a flat mesh, but it dynamically set the hieghts based on a texture in the vertex shader, but I can’t find it in the forum…
Edit: ← apparently I put it on Codea Community…