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.

    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. # Add_index does not need to be called before add_vertex.
    2. st.add_index(1)
    3. st.add_index(2)
    4. st.add_index(1)
    5. st.add_index(3)
    6. st.index()

    GDScript

    If you don’t add custom normals yourself, you can add them using generate_normals(). The same goes for tangents.

    GDScript

    1. st.generate_normals()
    2. 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).