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.

    1. st.commit(mesh)
    2. # Or:
    3. 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

    1. # Creates a quad from four corner vertices.
    2. # Add_index does not need to be called before add_vertex.
    3. st.add_index(1)
    4. st.add_index(2)
    5. st.add_index(1)
    6. st.add_index(3)
    7. # Alternatively:
    8. 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

    1. st.generate_normals()
    2. st.generate_tangents()