Helper tool to access and edit Mesh data.
MeshDataTool provides access to individual vertices in a Mesh. It allows users to read and edit vertex data of meshes. It also creates an array of faces and edges.
To use MeshDataTool, load a mesh with create_from_surface(). When you are finished editing the data commit the data to a mesh with commit_to_surface().
Below is an example of how MeshDataTool may be used.
var mesh = ArrayMesh.new()
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, BoxMesh.new().get_mesh_arrays())
var mdt = MeshDataTool.new()
mdt.create_from_surface(mesh, 0)
for i in range(mdt.get_vertex_count()):
var vertex = mdt.get_vertex(i)
# In this example we extend the mesh by one unit, which results in separated faces as it is flat shaded.
vertex += mdt.get_vertex_normal(i)
# Save your change.
mdt.set_vertex(i, vertex)
mesh.clear_surfaces()
mdt.commit_to_surface(mesh)
var mi = MeshInstance.new()
mi.mesh = mesh
add_child(mi)See also ArrayMesh, ImmediateMesh and SurfaceTool for procedural geometry generation.
Note: Godot uses clockwise winding order for front faces of triangle primitive modes.
Adds a new surface to specified Mesh with edited data.
Uses specified surface of given Mesh to populate data for MeshDataTool.
Requires Mesh with primitive type Mesh.PRIMITIVE_TRIANGLES.
Returns the index of the specified vertex connected to the edge at index idx.
vertex can only be 0 or 1, as edges are composed of two vertices.
Returns the edge associated with the face at index idx.
edge argument must be either 0, 1, or 2 because a face only has three edges.
Returns the specified vertex index of the given face.
vertex must be either 0, 1, or 2 because faces contain three vertices.
var index = mesh_data_tool.get_face_vertex(0, 1) # Gets the index of the second vertex of the first face.
var position = mesh_data_tool.get_vertex(index)
var normal = mesh_data_tool.get_vertex_normal(index)Returns the Mesh's format as a combination of the Mesh.ArrayFormat flags. For example, a mesh containing both vertices and normals would return a format of 3 because Mesh.ARRAY_FORMAT_VERTEX is 1 and Mesh.ARRAY_FORMAT_NORMAL is 2.
Sets the material to be used by newly-constructed Mesh.