Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

SurfaceTool

Inherits: RefCounted < Object

Helper tool to create geometry.

Description

The SurfaceTool is used to construct a Mesh by specifying vertex attributes individually. It can be used to construct a Mesh from a script. All properties except indices need to be added before calling add_vertex. For example, to add vertex colors and UVs:

var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.set_color(Color(1, 0, 0))
st.set_uv(Vector2(0, 0))
st.add_vertex(Vector3(0, 0, 0))

The above SurfaceTool now contains one vertex of a triangle which has a UV coordinate and a specified Color. If another vertex were added without calling set_uv or set_color, then the last values would be used.

Vertex attributes must be passed before calling add_vertex. Failure to do so will result in an error when committing the vertex information to a mesh.

Additionally, the attributes used before the first vertex is added determine the format of the mesh. For example, if you only add UVs to the first vertex, you cannot add color to any of the subsequent vertices.

See also ArrayMesh, ImmediateMesh and MeshDataTool for procedural geometry generation.

Note: Godot uses clockwise winding order for front faces of triangle primitive modes.

Tutorials

Methods

void

add_index ( int index )

void

add_triangle_fan ( PackedVector3Array vertices, PackedVector2Array uvs=PackedVector2Array(), PackedColorArray colors=PackedColorArray(), PackedVector2Array uv2s=PackedVector2Array(), PackedVector3Array normals=PackedVector3Array(), Plane[] tangents=[] )

void

add_vertex ( Vector3 vertex )

void

append_from ( Mesh existing, int surface, Transform3D transform )

void

begin ( PrimitiveType primitive )

void

clear ( )

ArrayMesh

commit ( ArrayMesh existing=null, int flags=0 )

Array

commit_to_arrays ( )

void

create_from ( Mesh existing, int surface )

void

create_from_blend_shape ( Mesh existing, int surface, String blend_shape )

void

deindex ( )

PackedInt32Array

generate_lod ( float nd_threshold, int target_index_count=3 )

void

generate_normals ( bool flip=false )

void

generate_tangents ( )

AABB

get_aabb ( ) const

CustomFormat

get_custom_format ( int channel_index ) const

PrimitiveType

get_primitive_type ( ) const

SkinWeightCount

get_skin_weight_count ( ) const

void

index ( )

void

optimize_indices_for_cache ( )

void

set_bones ( PackedInt32Array bones )

void

set_color ( Color color )

void

set_custom ( int channel_index, Color custom_color )

void

set_custom_format ( int channel_index, CustomFormat format )

void

set_material ( Material material )

void

set_normal ( Vector3 normal )

void

set_skin_weight_count ( SkinWeightCount count )

void

set_smooth_group ( int index )

void

set_tangent ( Plane tangent )

void

set_uv ( Vector2 uv )

void

set_uv2 ( Vector2 uv2 )

void

set_weights ( PackedFloat32Array weights )


Enumerations

enum CustomFormat:

CustomFormat CUSTOM_RGBA8_UNORM = 0

Limits range of data passed to set_custom to unsigned normalized 0 to 1 stored in 8 bits per channel. See Mesh.ARRAY_CUSTOM_RGBA8_UNORM.

CustomFormat CUSTOM_RGBA8_SNORM = 1

Limits range of data passed to set_custom to signed normalized -1 to 1 stored in 8 bits per channel. See Mesh.ARRAY_CUSTOM_RGBA8_SNORM.

CustomFormat CUSTOM_RG_HALF = 2

Stores data passed to set_custom as half precision floats, and uses only red and green color channels. See Mesh.ARRAY_CUSTOM_RG_HALF.

CustomFormat CUSTOM_RGBA_HALF = 3

Stores data passed to set_custom as half precision floats and uses all color channels. See Mesh.ARRAY_CUSTOM_RGBA_HALF.

CustomFormat CUSTOM_R_FLOAT = 4

Stores data passed to set_custom as full precision floats, and uses only red color channel. See Mesh.ARRAY_CUSTOM_R_FLOAT.

CustomFormat CUSTOM_RG_FLOAT = 5

Stores data passed to set_custom as full precision floats, and uses only red and green color channels. See Mesh.ARRAY_CUSTOM_RG_FLOAT.

CustomFormat CUSTOM_RGB_FLOAT = 6

Stores data passed to set_custom as full precision floats, and uses only red, green and blue color channels. See Mesh.ARRAY_CUSTOM_RGB_FLOAT.

CustomFormat CUSTOM_RGBA_FLOAT = 7

Stores data passed to set_custom as full precision floats, and uses all color channels. See Mesh.ARRAY_CUSTOM_RGBA_FLOAT.

CustomFormat CUSTOM_MAX = 8

Used to indicate a disabled custom channel.


enum SkinWeightCount:

SkinWeightCount SKIN_4_WEIGHTS = 0

Each individual vertex can be influenced by only 4 bone weights.

SkinWeightCount SKIN_8_WEIGHTS = 1

Each individual vertex can be influenced by up to 8 bone weights.


Method Descriptions

void add_index ( int index )

Adds a vertex to index array if you are using indexed vertices. Does not need to be called before adding vertices.


void add_triangle_fan ( PackedVector3Array vertices, PackedVector2Array uvs=PackedVector2Array(), PackedColorArray colors=PackedColorArray(), PackedVector2Array uv2s=PackedVector2Array(), PackedVector3Array normals=PackedVector3Array(), Plane[] tangents=[] )

Inserts a triangle fan made of array data into Mesh being constructed.

Requires the primitive type be set to Mesh.PRIMITIVE_TRIANGLES.


void add_vertex ( Vector3 vertex )

Specifies the position of current vertex. Should be called after specifying other vertex properties (e.g. Color, UV).


void append_from ( Mesh existing, int surface, Transform3D transform )

Append vertices from a given Mesh surface onto the current vertex array with specified Transform3D.


void begin ( PrimitiveType primitive )

Called before adding any vertices. Takes the primitive type as an argument (e.g. Mesh.PRIMITIVE_TRIANGLES).


void clear ( )

Clear all information passed into the surface tool so far.


ArrayMesh commit ( ArrayMesh existing=null, int flags=0 )

Returns a constructed ArrayMesh from current information passed in. If an existing ArrayMesh is passed in as an argument, will add an extra surface to the existing ArrayMesh.

FIXME: Document possible values for flags, it changed in 4.0. Likely some combinations of ArrayFormat.


Array commit_to_arrays ( )

Commits the data to the same format used by ArrayMesh.add_surface_from_arrays. This way you can further process the mesh data using the ArrayMesh API.


void create_from ( Mesh existing, int surface )

Creates a vertex array from an existing Mesh.


void create_from_blend_shape ( Mesh existing, int surface, String blend_shape )

Creates a vertex array from the specified blend shape of an existing Mesh. This can be used to extract a specific pose from a blend shape.


void deindex ( )

Removes the index array by expanding the vertex array.


PackedInt32Array generate_lod ( float nd_threshold, int target_index_count=3 )

Generates a LOD for a given nd_threshold in linear units (square root of quadric error metric), using at most target_index_count indices.

Deprecated. Unused internally and fails to preserve normals or UVs. Consider using ImporterMesh.generate_lods instead.


void generate_normals ( bool flip=false )

Generates normals from vertices so you do not have to do it manually. If flip is true, the resulting normals will be inverted. generate_normals should be called after generating geometry and before committing the mesh using commit or commit_to_arrays. For correct display of normal-mapped surfaces, you will also have to generate tangents using generate_tangents.

Note: generate_normals only works if the primitive type to be set to Mesh.PRIMITIVE_TRIANGLES.

Note: generate_normals takes smooth groups into account. To generate smooth normals, set the smooth group to a value greater than or equal to 0 using set_smooth_group or leave the smooth group at the default of 0. To generate flat normals, set the smooth group to -1 using set_smooth_group prior to adding vertices.


void generate_tangents ( )

Generates a tangent vector for each vertex. Requires that each vertex have UVs and normals set already (see generate_normals).


AABB get_aabb ( ) const

Returns the axis-aligned bounding box of the vertex positions.