Using the SurfaceTool
The SurfaceTool also provides some useful helper functions like and generate_normals()
.
Attributes are added before each vertex is added:
GDScript
When finished generating your geometry with the SurfaceTool call commit()
to finish generating the mesh. If an is passed to commit()
then it appends a new surface to the end of the ArrayMesh. While if nothing is passed in, commit()
returns an ArrayMesh.
st.commit(mesh)
# Or:
var mesh = st.commit()
Code creates a triangle with indices
GDScript
You can optionally add an index array, either by calling add_index()
and adding vertices to the index array or by calling index()
which shrinks the vertex array to remove duplicate vertices.
GDScript
# Creates a quad from four corner vertices.
# Add_index does not need to be called before add_vertex.
st.add_index(1)
st.add_index(2)
st.add_index(1)
st.add_index(3)
# Alternatively:
st.index()
GDScript
If you don’t add custom normals yourself, you can add them using generate_normals()
, which should be called after generating geometry and before committing the mesh using or commit_to_arrays()
. Calling generate_normals(true)
will flip the resulting normals. As a side note, generate_normals()
only works if the primitive type is set to Mesh.PRIMITIVE_TRIANGLES
.
If you don’t add custom tangents, they can be added with generate_tangents()
, but it requires that each vertex have UVs and normals set already.
GDScript
st.generate_normals()
st.generate_tangents()