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 call commit()
to finished generating the mesh. If an ArrayMesh 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
# 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)
st.index()
GDScript
If you don’t add custom normals yourself, you can add them using generate_normals()
. The same goes for tangents.
GDScript
st.generate_normals()
st.generate_tangents()
By default, when generating normals, they will be calculated on a per-face basis. If you want smooth vertex normals, when adding vertices, call add_smooth_group()
. add_smooth_group()
needs to be called while building the geometry, e.g. before the call to add_vertex()
(if non-indexed) or add_index()
(if indexed).