The SurfaceTool provides a useful interface for constructing geometry. The interface is similar to the ImmediateGeometry node. You set each per-vertex attribute (e.g. normal, uv, color) and then when you add a vertex it captures the attributes.
The SurfaceTool also provides some useful helper functions like
Attributes are added before each vertex is added:
When finished generating your geometry with the SurfaceTool
commit() to finished generating the mesh. If an ArrayMesh is passed
commit() then it appends a new surface to the end of the ArrayMesh. While if nothing is passed
commit() returns an ArrayMesh.
Code creates a triangle with indices
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.
Similarly, if you have an index array, but you want each vertex to be unique (e.g. because
you want to use unique normals or colors per face instead of per-vertex), you can call
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
generate_normals(true) will flip the resulting normals. As a side
generate_normals() only works if the primitive type is set to
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.
By default, when generating normals, they will be calculated on a per-face basis. If you want
smooth vertex normals, when adding vertices, call
needs to be called while building the geometry, e.g. before the call to
(if non-indexed) or
add_index() (if indexed).