Index · Object → RefCounted → Resource → Mesh → ArrayMesh

ArrayMesh

Mesh type that provides utility for constructing a surface from arrays.

Class Description

The ArrayMesh is used to construct a Mesh by specifying the attributes as arrays.

The most basic example is the creation of a single triangle:

var vertices = PackedVector3Array()
vertices.push_back(Vector3(0, 1, 0))
vertices.push_back(Vector3(1, 0, 0))
vertices.push_back(Vector3(0, 0, 1))

# Initialize the ArrayMesh.
var arr_mesh = ArrayMesh.new()
var arrays = []
arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = vertices

# Create the Mesh.
arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
var m = MeshInstance3D.new()
m.mesh = arr_mesh

The MeshInstance3D is ready to be added to the SceneTree to be shown.

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

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

Properties

var blend_shape_mode: Mesh.BlendShapeMode = 1

The blend shape mode.

var custom_aabb: AABB = AABB(0, 0, 0, 0, 0, 0)

Overrides the AABB with one defined by user for use with frustum culling. Especially useful to avoid unexpected culling when using a shader to offset vertices.

var shadow_mesh: ArrayMesh

An optional mesh which can be used for rendering shadows and the depth prepass. Can be used to increase performance by supplying a mesh with fused vertices and only vertex position data (without normals, UVs, colors, etc.).

Note: This mesh must have exactly the same vertex positions as the source mesh (including the source mesh's LODs, if present). If vertex positions differ, then the mesh will not draw correctly.

Inherited from Mesh (1)
var lightmap_size_hint: Vector2i = Vector2i(0, 0)
Inherited from Resource (4)

Methods

func add_blend_shape(name: StringName) -> void

Adds name for a blend shape that will be added with add_surface_from_arrays(). Must be called before surface is added.

func add_surface_from_arrays(primitive: Mesh.PrimitiveType, arrays: Array, blend_shapes: Array[void] = [], lods: Dictionary = {}, flags: Mesh.ArrayFormat = 0) -> void

Creates a new surface. Mesh.get_surface_count() will become the surf_idx for this new surface.

Surfaces are created to be rendered using a primitive, which may be any of the values defined in Mesh.PrimitiveType.

The arrays argument is an array of arrays. Each of the Mesh.ARRAY_MAX elements contains an array with some of the mesh data for this surface as described by the corresponding member of Mesh.ArrayType or null if it is not used by the surface. For example, arrays[0] is the array of vertices. That first vertex sub-array is always required; the others are optional. Adding an index array puts this surface into "index mode" where the vertex and other arrays become the sources of data and the index array defines the vertex order. All sub-arrays must have the same length as the vertex array (or be an exact multiple of the vertex array's length, when multiple elements of a sub-array correspond to a single vertex) or be empty, except for Mesh.ARRAY_INDEX if it is used.

The blend_shapes argument is an array of vertex data for each blend shape. Each element is an array of the same structure as arrays, but Mesh.ARRAY_VERTEX, Mesh.ARRAY_NORMAL, and Mesh.ARRAY_TANGENT are set if and only if they are set in arrays and all other entries are null.

The lods argument is a dictionary with float keys and PackedInt32Array values. Each entry in the dictionary represents an LOD level of the surface, where the value is the Mesh.ARRAY_INDEX array to use for the LOD level and the key is roughly proportional to the distance at which the LOD stats being used. I.e., increasing the key of an LOD also increases the distance that the objects has to be from the camera before the LOD is used.

The flags argument is the bitwise OR of, as required: One value of Mesh.ArrayCustomFormat left shifted by ARRAY_FORMAT_CUSTOMn_SHIFT for each custom channel in use, Mesh.ARRAY_FLAG_USE_DYNAMIC_UPDATE, Mesh.ARRAY_FLAG_USE_8_BONE_WEIGHTS, or Mesh.ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY.

Note: When using indices, it is recommended to only use points, lines, or triangles.

func clear_blend_shapes() -> void

Removes all blend shapes from this ArrayMesh.

func clear_surfaces() -> void

Removes all surfaces from this ArrayMesh.

func get_blend_shape_count() -> int

Returns the number of blend shapes that the ArrayMesh holds.

func get_blend_shape_name(index: int) -> StringName

Returns the name of the blend shape at this index.

func lightmap_unwrap(transform: Transform3D, texel_size: float) -> Error

Performs a UV unwrap on the ArrayMesh to prepare the mesh for lightmapping.

func regen_normal_maps() -> void

Regenerates tangents for each of the ArrayMesh's surfaces.

func set_blend_shape_name(index: int, name: StringName) -> void

Sets the name of the blend shape at this index.

func surface_find_by_name(name: String) -> int

Returns the index of the first surface with this name held within this ArrayMesh. If none are found, -1 is returned.

func surface_get_array_index_len(surf_idx: int) -> int

Returns the length in indices of the index array in the requested surface (see add_surface_from_arrays()).

func surface_get_array_len(surf_idx: int) -> int

Returns the length in vertices of the vertex array in the requested surface (see add_surface_from_arrays()).

func surface_get_format(surf_idx: int) -> Mesh.ArrayFormat

Returns the format mask of the requested surface (see add_surface_from_arrays()).

func surface_get_name(surf_idx: int) -> String

Gets the name assigned to this surface.

func surface_get_primitive_type(surf_idx: int) -> Mesh.PrimitiveType

Returns the primitive type of the requested surface (see add_surface_from_arrays()).

func surface_remove(surf_idx: int) -> void

Removes the surface at the given index from the Mesh, shifting surfaces with higher index down by one.

func surface_set_name(surf_idx: int, name: String) -> void

Sets a name for a given surface.

func surface_update_attribute_region(surf_idx: int, offset: int, data: PackedByteArray) -> void

Updates the attribute buffer of this mesh's surface with the given data. The expected data per attribute is 12 or 8 bytes (4 bytes per float, 2 floats per Vector2, and 3 floats per Vector3) depending on if the mesh is using Vector3 or Vector2 vertices. This value can be determined with RenderingServer.mesh_surface_get_format_attribute_stride().

The starting point of the updates can be changed with offset. The value of offset should be a multiple of 12 bytes in most cases to align to each attribute.

A PackedVector3Array of attribute locations can be converted into a PackedByteArray using PackedVector3Array.to_byte_array() for use in data.

func surface_update_skin_region(surf_idx: int, offset: int, data: PackedByteArray) -> void

Updates the skin buffer of this mesh's surface with the given data. The expected data per skin is 12 or 8 bytes (4 bytes per float, 2 floats per Vector2, and 3 floats per Vector3) depending on if the mesh is using Vector3 or Vector2 vertices. This value can be determined with RenderingServer.mesh_surface_get_format_skin_stride().

The starting point of the updates can be changed with offset. The value of offset should be a multiple of 12 bytes in most cases to align to each skin.

A PackedVector3Array of skin locations can be converted into a PackedByteArray using PackedVector3Array.to_byte_array() for use in data.

func surface_update_vertex_region(surf_idx: int, offset: int, data: PackedByteArray) -> void

Updates the vertex buffer of this mesh's surface with the given data. The expected data per vertex is 12 or 8 bytes (4 bytes per float, 2 floats per Vector2, and 3 floats per Vector3) depending on if the mesh is using Vector3 or Vector2 vertices. This value can be determined with RenderingServer.mesh_surface_get_format_vertex_stride().

The starting point of the updates can be changed with offset. The value of offset should be a multiple of 12 bytes in most cases to align to each vertex.

A PackedVector3Array of vertex locations can be converted into a PackedByteArray using PackedVector3Array.to_byte_array() for use in data.

Inherited from Mesh (26)
func _get_aabb() -> AABB
func _set_blend_shape_name(index: int, name: StringName) -> void
func _surface_get_arrays(index: int) -> Array
func _surface_get_format(index: int) -> int
func _surface_set_material(index: int, material: Material) -> void
func create_convex_shape(clean: bool = true, simplify: bool = false) -> ConvexPolygonShape3D
func create_outline(margin: float) -> Mesh
func get_aabb() -> AABB
func surface_get_arrays(surf_idx: int) -> Array
func surface_get_blend_shape_arrays(surf_idx: int) -> Array[void]
func surface_get_material(surf_idx: int) -> Material
func surface_set_material(surf_idx: int, material: Material) -> void
Inherited from Resource (18)
func _get_rid() -> RID
func _reset_state() -> void
func _set_path_cache(path: String) -> void
func _setup_local_to_scene() -> void
func copy_from_resource(resource: Resource) -> Error
func duplicate(deep: bool = false) -> Resource
func duplicate_deep(deep_subresources_mode: Resource.DeepDuplicateMode = 1) -> Resource
func emit_changed() -> void
func get_rid() -> RID
func is_built_in() -> bool
func reset_state() -> void
func set_id_for_path(path: String, id: String) -> void
func set_path_cache(path: String) -> void
func setup_local_to_scene() -> void
func take_over_path(path: String) -> void
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 Resource (2)
signal changed()
Inherited from Object (2)