Index · Object → RefCounted → SurfaceTool

SurfaceTool

Helper tool to create geometry.

Class 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.

Methods

func add_index(index: int) -> void

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

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

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

Requires the primitive type be set to Mesh.PRIMITIVE_TRIANGLES.

func add_vertex(vertex: Vector3) -> void

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

func append_from(existing: Mesh, surface: int, transform: Transform3D) -> void

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

func begin(primitive: Mesh.PrimitiveType) -> void

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

func clear() -> void

Clear all information passed into the surface tool so far.

func commit(existing: ArrayMesh = null, flags: int = 0) -> ArrayMesh

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.

The flags argument can be the bitwise OR of Mesh.ARRAY_FLAG_USE_DYNAMIC_UPDATE, Mesh.ARRAY_FLAG_USE_8_BONE_WEIGHTS, or Mesh.ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY.

func commit_to_arrays() -> Array

Commits the data to the same format used by ArrayMesh.add_surface_from_arrays(), ImporterMesh.add_surface(), and create_from_arrays(). This way you can further process the mesh data using the ArrayMesh or ImporterMesh APIs.

func create_from(existing: Mesh, surface: int) -> void

Creates a vertex array from an existing Mesh.

func create_from_arrays(arrays: Array, primitive_type: Mesh.PrimitiveType = 3) -> void

Creates this SurfaceTool from existing vertex arrays such as returned by commit_to_arrays(), Mesh.surface_get_arrays(), Mesh.surface_get_blend_shape_arrays(), ImporterMesh.get_surface_arrays(), and ImporterMesh.get_surface_blend_shape_arrays(). primitive_type controls the type of mesh data, defaulting to Mesh.PRIMITIVE_TRIANGLES.

func create_from_blend_shape(existing: Mesh, surface: int, blend_shape: String) -> void

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.

func deindex() -> void

Removes the index array by expanding the vertex array.

func generate_lod(nd_threshold: float, target_index_count: int = 3) -> PackedInt32Array

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

func generate_normals(flip: bool = false) -> void

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.

func generate_tangents() -> void

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

func get_aabb() -> AABB

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

func get_custom_format(channel_index: int) -> SurfaceTool.CustomFormat

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

func get_primitive_type() -> Mesh.PrimitiveType

Returns the type of mesh geometry, such as Mesh.PRIMITIVE_TRIANGLES.

func get_skin_weight_count() -> SurfaceTool.SkinWeightCount

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.

func index() -> void

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

func optimize_indices_for_cache() -> void

Optimizes triangle sorting for performance. Requires that get_primitive_type() is Mesh.PRIMITIVE_TRIANGLES.

func set_bones(bones: PackedInt32Array) -> void

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

func set_color(color: Color) -> void

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.

func set_custom(channel_index: int, custom_color: Color) -> void

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.

func set_custom_format(channel_index: int, format: SurfaceTool.CustomFormat) -> void

Sets the color format for this custom channel_index. Use CUSTOM_MAX to disable.

Must be invoked after begin() and should be set before commit() or commit_to_arrays().

func set_material(material: Material) -> void

Sets Material to be used by the Mesh you are constructing.

func set_normal(normal: Vector3) -> void

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.

func set_skin_weight_count(count: SurfaceTool.SkinWeightCount) -> void

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.

func set_smooth_group(index: int) -> void

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.

func set_tangent(tangent: Plane) -> void

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.

Note: Even though tangent is a Plane, it does not directly represent the tangent plane. Its Plane.x, Plane.y, and Plane.z represent the tangent vector and Plane.d should be either -1 or 1. See also Mesh.ARRAY_TANGENT.

func set_uv(uv: Vector2) -> void

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.

func set_uv2(uv2: Vector2) -> void

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.

func set_weights(weights: PackedFloat32Array) -> void

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.

Inherited from RefCounted (4)
func init_ref() -> bool
func reference() -> bool
func unreference() -> bool
Inherited from Object (62)
func _get(property: StringName) -> Variant
func _get_property_list() -> Dictionary[]
func _init() -> void
func _iter_get(iter: Variant) -> Variant
func _iter_init(iter: Array) -> bool
func _iter_next(iter: Array) -> bool
func _notification(what: int) -> void
func _set(property: StringName, value: Variant) -> bool
func _to_string() -> String
func _validate_property(property: Dictionary) -> void
func add_user_signal(signal: String, arguments: Array = []) -> void
func call(method: StringName) -> Variant
func callv(method: StringName, arg_array: Array) -> Variant
func cancel_free() -> void
func connect(signal: StringName, callable: Callable, flags: int = 0) -> Error
func disconnect(signal: StringName, callable: Callable) -> void
func emit_signal(signal: StringName) -> Error
func free() -> void
func get(property: StringName) -> Variant
func get_class() -> String
func get_incoming_connections() -> Dictionary[]
func get_indexed(property_path: NodePath) -> Variant
func get_meta(name: StringName, default: Variant = null) -> Variant
func get_meta_list() -> StringName[]
func get_method_list() -> Dictionary[]
func get_property_list() -> Dictionary[]
func get_signal_connection_list(signal: StringName) -> Dictionary[]
func get_signal_list() -> Dictionary[]
func has_meta(name: StringName) -> bool
func has_method(method: StringName) -> bool
func has_signal(signal: StringName) -> bool
func is_class(class: StringName) -> bool
func is_connected(signal: StringName, callable: Callable) -> bool
func notification(what: int, reversed: bool = false) -> void
func remove_meta(name: StringName) -> void
func remove_user_signal(signal: StringName) -> void
func set(property: StringName, value: Variant) -> void
func set_block_signals(enable: bool) -> void
func set_deferred(property: StringName, value: Variant) -> void
func set_indexed(property_path: NodePath, value: Variant) -> void
func set_message_translation(enable: bool) -> void
func set_meta(name: StringName, value: Variant) -> void
func set_script(script: Variant) -> void
func set_translation_domain(domain: StringName) -> void
func to_string() -> String
func tr(message: StringName, context: StringName = &"") -> String
func tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") -> String

Signals

Inherited from Object (2)

Enums & Constants

enum 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.

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.

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.

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.

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.

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.

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.

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.

CUSTOM_MAX = 8

Used to indicate a disabled custom channel.

enum SkinWeightCount
SKIN_4_WEIGHTS = 0

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

SKIN_8_WEIGHTS = 1

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