How to add vertex to an existing ArrayMesh?

Godot Version

4.2

Question

I’m discovering how procedural mesh generation works with Godot and I can’t modify a mesh without having to remove all the surfaces, modify them and then put them back.

Is this normal operation or is it possible to just add vertices and indexes of triangles without having to rebuild everything each time?

For example, I have a curve to which I add points as I move along it. I draw a line (Mesh.PrimitiveType.LineStrip) to visualize the curve and add points as I go. can’t I just add the new vertices without having to copy the surface, modify it, call “ClearSurfaces()” and then “AddSurfaceFromArrays(Mesh.PrimitiveType.LineStrip, modifiedSurface)”?

This example is relatively inexpensive and works very well, but I will soon tackle more complex geometries and I would like to start by understanding what is possible and what is not.

I’m not complaining, I’m just trying to understand the limitations in order to adapt my code accordingly and be as optimized as possible. And I may have missed something while digging through the documentation and doing my tests.

Thank you in advance for your answers.

There are multiple ways to generate procedural meshes at runtime. Each one with its pros and cons. Explanation and examples for each one here Procedural geometry — Godot Engine (stable) documentation in English

The RenderingServer API has specific functions to update an attribute/skin/vertex region of a mesh instead of the entire mesh. That is a very advanced use as you need to provide the correct stride in the byte array but it is an option.

That only goes for updates on an already created surface with its fixed size. If you need to change the size of the mesh all the rendering arrays need to be resized so at that point you really can just clear the surface and do it from scratch.

1 Like

Thanks, that’s what I thought but I still wanted confirmation.

I have already gone through the documentation extensively and looked at all the accessible methods, but the explanations are not always clear or not precise enough for certain scenarios.
But the link may be useful to those who pass here without reading the doc first.

After some reflexion and research I think the method "RenderingServer.MeshSurfaceUpdateVertexRegion would be usefull in my case to have a fixed number of vertexs and triangle indexes and just changing their values without having to clear and recreate all the surface?

But the source code is totally alien to me when the implementation say :

public static void MeshSurfaceUpdateVertexRegion(
Rid mesh,
int surface,
int offset,
byte data)
{
NativeCalls.godot_icall_4_898(RenderingServer.MethodBind57, GodotObject.GetPtr((GodotObject) RenderingServer.Singleton), mesh, surface, offset, data);
}

I understand that “MethodBind57” is some sort of method pointer but I don’t know how to find what it point to, to read the code of that method.

I’m using C# by the way but I understand C++ if I have to read the source code in that langage.

is there a place where I can learn how to use this kind of functions ?

I have to change the Vector3 values of the vertexs and the indexes of the triangles.

The idea is to procedurally generate a mesh along a curve and replace old parts by new one so the new vertexs wil be triangulated with the previously modified region etc…

what bugs me is how can I get or create the byte data and does this function only update the position of vertexs or the data contains all the attributes like color, normals, etc ?

I will continue searching but if you can enlight me I will be very gratfull.

If you convert a C# function name to snake_case you get 99% of the time the GDScript or C++ function name that you can search in the docs or source code.

The byte array that you need to create for the mesh region update is basically a sliced mesh array of what you use for the mesh surface creation just for that stride section that you want to update on the mesh.

Your stride step and data depends on your Mesh.FORMAT and the mesh arrays that you use for the surface. You can find the source code details that tell you how those arrays converted from and to bytes in the driver mesh_storage class of the rendering server. E.g. for the forward renderer it is found here godot/servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp at 36e943b6b20cb7a8a89bc30489c4a81c3e149d74 · godotengine/godot · GitHub.

1 Like

So if I understand correctly, the data requested to modify a vertex region are the position, normal and tangent for each vertex.

Thank you, I will be able to work with that and if I have a problem I will create another post.