Class reference

SurfaceTool

Inherits RefCounted

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))
		
		var st = new SurfaceTool();
		st.Begin(Mesh.PrimitiveType.Triangles);
		st.SetColor(new Color(1, 0, 0));
		st.SetUV(new Vector2(0, 0));
		st.AddVertex(new 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: Redot uses clockwise winding order for front faces of triangle primitive modes.

Methods

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_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 clear()

Clear all information passed into the surface tool so far.

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)

Deprecated.

This method is unused internally, as it does not preserve normals or UVs. Consider using ImporterMesh.generate_lods() instead.

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

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 is 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 already has UVs and normals set (see generate_normals()).

AABB get_aabb() const

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

int get_custom_format(int channel_index) const

Returns the format for custom channel_index (currently up to 4). Returns CUSTOM_MAX if this custom channel is unused.

int get_skin_weight_count() const

By default, returns SKIN_4_WEIGHTS to indicate only 4 bone influences per vertex are used. Returns SKIN_8_WEIGHTS if up to 8 influences are used. Note: This function returns an enum, not the exact number of weights.

void index()

Shrinks the vertex array by creating an index array. This can improve performance by avoiding vertex reuse.

void set_bones(PackedInt32Array bones)

Specifies an array of bones to use for the next vertex. bones must contain 4 integers.

void set_color(Color color)

Specifies a Color to use for the next vertex. If every vertex needs to have this information set and you fail to submit it for the first vertex, this information may not be used at all. Note: The material must have BaseMaterial3D.vertex_color_use_as_albedo enabled for the vertex color to be visible.

void set_custom(int channel_index, Color custom_color)

Sets the custom value on this vertex for channel_index. set_custom_format() must be called first for this channel_index. Formats which are not RGBA will ignore other color channels.

void set_normal(Vector3 normal)

Specifies a normal to use for the next vertex. If every vertex needs to have this information set and you fail to submit it for the first vertex, this information may not be used at all.

void set_skin_weight_count(int count)

Set to SKIN_8_WEIGHTS to indicate that up to 8 bone influences per vertex may be used. By default, only 4 bone influences are used (SKIN_4_WEIGHTS). Note: This function takes an enum, not the exact number of weights.

void set_smooth_group(int index)

Specifies the smooth group to use for the next vertex. If this is never called, all vertices will have the default smooth group of 0 and will be smoothed with adjacent vertices of the same group. To produce a mesh with flat normals, set the smooth group to -1. Note: This function actually takes a uint32_t, so C# users should use uint32.MaxValue instead of -1 to produce a mesh with flat normals.

void set_tangent(Plane tangent)

Specifies a tangent to use for the next vertex. If every vertex needs to have this information set and you fail to submit it for the first vertex, this information may not be used at all.

void set_uv(Vector2 uv)

Specifies a set of UV coordinates to use for the next vertex. If every vertex needs to have this information set and you fail to submit it for the first vertex, this information may not be used at all.

void set_uv2(Vector2 uv2)

Specifies an optional second set of UV coordinates to use for the next vertex. If every vertex needs to have this information set and you fail to submit it for the first vertex, this information may not be used at all.

void set_weights(PackedFloat32Array weights)

Specifies weight values to use for the next vertex. weights must contain 4 values. If every vertex needs to have this information set and you fail to submit it for the first vertex, this information may not be used at all.

Constants

CUSTOM_MAX = 8

Enum: CustomFormat

Used to indicate a disabled custom channel.

SKIN_4_WEIGHTS = 0

Enum: SkinWeightCount

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

SKIN_8_WEIGHTS = 1

Enum: SkinWeightCount

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

Tutorials

Source revision 5592dc3a7a61
Connection interrupted. Reload×

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.