Procedural 3D objects

Hi,
I have to make a decision about moving my project from Unity 3D. So far the 2 candidates are Defold and Godot with each have it’s pros and cons. The main object in my project is a mesh generated by passing the vertex , normals and triangle face data to the engine. I hit 2 issues here: A) I can not generate the mesh during edit time. B) I have no control over the triangle generation and passing the vertices only creates unexpected results for the mesh faces.
If there is no way to control the mesh triangle vertex indices then Defold is no go for my project. You can check here how this looks like in Unity3D
Is there a way that I can work around at least the issue B?

Hi!
It is correct that currently, the mesh only supports a triangle list.
No one has yet to request index list support for it on our GitHub page.

“and passing the vertices only creates unexpected results for the mesh faces.”

My guess is that the list of vertices doesn’t represent a triangle list, and the results will be unexpected.

Depending on the number of vertices, I’d also consider making an extension to generate the vertex buffer.

I hope this helps!

Hi Matias,
Thanks for the swift reply. My goal is to create a cube out of 6 subdivided squares. I did that in Unity by creating the grid of vertices and passing the list of the vertices for each triangle following a simple rule for the vertex index ordering. Is it possible to create subdivided square in the manner of the cube /mesh/ example using the provided tools or extensions is unavoidable?

No one has yet to request index list support for it on our GitHub page.

I believe there is a feature request for adding index buffers to meshes: Add option for indexed vertices for buffers to be used with the mesh component. · Issue #5477 · defold/defold · GitHub

I’ve been playing around with procedural mesh generation as well, and wouldn’t mind having this on the roadmap. :grin:

2 Likes

Yeah I agree, it shouldn’t be that tricky to add, we should definitely look into it in a near future

5 Likes

For sure, you can update the buffer with new triangles.
It’s just a vertex buffer, interpreted as a triangles list. so add them as:

[t0v0, t0v1, t0v2, t1v0, t1v1, t1v2, ...]
1 Like

Thanks Mathias. In my case a typical count of the vertices is ~6400 per cube side /80x80 grid/ using the index list and it will triple that number using the approach that you propose. How this compares from performance perspective given the fact that the triangles count is the same?

I think the costly part will be to fill ~230k vertices in from Lua.
You will surely get better performance if you generate the buffer in a native extension (written in C/C++), and then sending the buffer back to Lua.
Especially if you also wish to modify the buffer at runtime (e.g. for deformation) so you don’t slow down the game.

2 Likes

The good thing in my case is that I am generating the mesh once in the beginning of the level and never modify it after that. I am aware also that there is no way to multithread it, but it takes usually couple of seconds, not that much for level loading time…I guess.
I’ll give it a try and see how it feels. I prefer to use Defold as it’s leaner, not over engineered and I love Lua.

You can, if you create it on a C++ thread.
Lua isn’t threaded of course, but the last bit of communication between the worker thread and the main thread is mostly a mem copy, so it’s very fast too.

“but it takes usually couple of seconds, not that much for level loading time”

That’s still a fair amount in the Defold world :wink:
Jokes aside, you would have to portion out the calculations by using coroutines, so that the main thread don’t stall (which would potentially trigger a “hang” + shutdown on your target system (e.g. Android))

Oh, I never considered phone target. The plan is desktop and maybe high end tablet , but desktop for now. Sure I will portion the process thru coroutines. Thanks for the “hang+shutdown” point , makes a lot of sense. Is there an example of creating native extension, that I can explore and decide how much extra knowledge this will require? C++ is not my forte tbh.

Sure, the manual is here and you can also create a newextension from your editor, when creating a new project.

Creating the vertices for the cube subdivided by 64, normalising to sphere , passing the vertex and normal buffers for 147456 vertices takes well below one second on a M2 MacBook Air . Given the target is a desktop, I will skip the extensions for now.
Thanks for the help Mathias, much appreciated!

2 Likes

If you make mesh in lua, every operation with buffer or stream is c function call from lua.

It is much better to prepare vertices array in lua, push array in native extension and create mesh in c++. It will be worked much much faster.

Here is example for uint8, it is easy to make same version for float.

4 Likes