Hi i'm having some problems editing mesh using MeshDataTool. The mesh (stylized cube) was imported from blender. For some reason the vertices got disconnected when i change the position. Is this normal behavior? how can i keep vertices connected?
Edit mesh in real-time
Heres the code and the file
var new_cube = get_node("cube").duplicate()
var mdt = MeshDataTool.new()
var mesh = new_cube.get_child(0).get_mesh()
mdt.create_from_surface(mesh, 0)
mdt.set_vertex(3,Vector3(0.0,-6.0,0.0))
mesh.clear_surfaces()
mdt.commit_to_surface(mesh)
add_child(new_cube)
- Edited
timoraes Ok. So once the mesh is imported, Godot needs to duplicate each vertex that is part of a hard edge. This happens as many times as there are faces with hard edges the particular vertex is contributing to. In the case of a cube, each corner vertex will appear three times in the vertex buffer.
The reason for this is the way gpu typically handles vertex data. The "same" vertex may occur in triangles with different normals. Since normal data is integral part of the vertex attribute data sent to the gpu, technically a new vertex (with same position but different normal) needs to be stored in the vertex buffer for each such case.
You can try it with a plain cube. It'll have 8 vertices in Blender but once you import it into Godot and ask MeshDataTool about the vertex count - it'll return 24.
I guess your best bet is to build a helper array/dictionary (using MDT queries, and manually checking distances) that holds groups of vertices that represent the same vertex from the original mesh, and move the whole group.
- Edited
timoraes If you wish to get all fancy and elegant, you could make a class that inherits MeshDataTool, and add this functionality to it. Whenever a new mesh is assigned (or topology changed), it (re)builds a dictionary of vertex groups each representing the same logical vertex. Provide some methods for querying and moving those groups.
Alternatively the class (or just a helper function) could use simpler brute force approach suggested by @synthnostate, avoiding indexing data altogether at the expense of some performance. It's basically the same thing just with more repeated iteration over vertices. Good enough for low poly meshes and/or meshes that are updated only once in a while.
Either way you can name the class TimoraesSmartMeshDataTool
Any way to godot identify those connected vertices in the future? it would be a good implementation. In fact is not effcient at all, considering that there may be vertices in the same position and they are not connected.
- Edited
timoraes Godot knows "connected" vertices in the moment of import, it just doesn't care about them afterwards because it's only interested in providing data in the format that pleases the gpu. And there's no concept of "connected vertices" on the gpu side of things.
Godot is a realtime rendering engine after all, not a modeling tool. Building an acceleration data structure as I described earlier can make the whole thing perform reasonably well. But if you want to do something like frame-by-frame vertex animation, this is not the way to do it, unless the moved vertex count is reasonably low.
timoraes "If you wish to get all fancy and elegant"....Just want to save time
Elegant and expressive code saves bunch of time in the long run