Index · Object → RefCounted → Resource → MultiMesh

MultiMesh

Provides high-performance drawing of a mesh multiple times using GPU instancing.

Class Description

MultiMesh provides low-level mesh instancing. Drawing thousands of MeshInstance3D nodes can be slow, since each object is submitted to the GPU then drawn individually.

MultiMesh is much faster as it can draw thousands of instances with a single draw call, resulting in less API overhead.

As a drawback, if the instances are too far away from each other, performance may be reduced as every single instance will always render (they are spatially indexed as one, for the whole object).

Since instances may have any behavior, the AABB used for visibility must be provided by the user.

Note: A MultiMesh is a single object, therefore the same maximum lights per object restriction applies. This means, that once the maximum lights are consumed by one or more instances, the rest of the MultiMesh instances will not receive any lighting.

Note: Blend Shapes will be ignored if used in a MultiMesh.

Properties

var buffer: PackedFloat32Array = PackedFloat32Array()
var color_array: PackedColorArray

Array containing each Color used by all instances of this mesh.

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

Custom AABB for this MultiMesh resource. Setting this manually prevents costly runtime AABB recalculations.

var custom_data_array: PackedColorArray

Array containing each custom data value used by all instances of this mesh, as a PackedColorArray.

var instance_count: int = 0

Number of instances that will get drawn. This clears and (re)sizes the buffers. Setting data format or flags afterwards will have no effect.

By default, all instances are drawn but you can limit this with visible_instance_count.

var mesh: Mesh

Mesh resource to be instanced.

The looks of the individual instances can be modified using set_instance_color() and set_instance_custom_data().

var physics_interpolation_quality: MultiMesh.PhysicsInterpolationQuality = 0

Choose whether to use an interpolation method that favors speed or quality.

When using low physics tick rates (typically below 20) or high rates of object rotation, you may get better results from the high quality setting.

Note: Fast quality does not equate to low quality. Except in the special cases mentioned above, the quality should be comparable to high quality.

var transform_2d_array: PackedVector2Array

Array containing each Transform2D value used by all instances of this mesh, as a PackedVector2Array. Each transform is divided into 3 Vector2 values corresponding to the transforms' x, y, and origin.

var transform_array: PackedVector3Array

Array containing each Transform3D value used by all instances of this mesh, as a PackedVector3Array. Each transform is divided into 4 Vector3 values corresponding to the transforms' x, y, z, and origin.

var transform_format: MultiMesh.TransformFormat = 0

Format of transform used to transform mesh, either 2D or 3D.

var use_colors: bool = false

If true, the MultiMesh will use color data (see set_instance_color()). Can only be set when instance_count is 0 or less. This means that you need to call this method before setting the instance count, or temporarily reset it to 0.

var use_custom_data: bool = false

If true, the MultiMesh will use custom data (see set_instance_custom_data()). Can only be set when instance_count is 0 or less. This means that you need to call this method before setting the instance count, or temporarily reset it to 0.

var visible_instance_count: int = -1

Limits the number of instances drawn, -1 draws all instances. Changing this does not change the sizes of the buffers.

Inherited from Resource (4)

Methods

func get_aabb() -> AABB

Returns the visibility axis-aligned bounding box in local space.

func get_instance_color(instance: int) -> Color

Gets a specific instance's color multiplier.

func get_instance_custom_data(instance: int) -> Color

Returns the custom data that has been set for a specific instance.

func get_instance_transform(instance: int) -> Transform3D

Returns the Transform3D of a specific instance.

func get_instance_transform_2d(instance: int) -> Transform2D

Returns the Transform2D of a specific instance.

func reset_instance_physics_interpolation(instance: int) -> void

When using physics interpolation, this function allows you to prevent interpolation on an instance in the current physics tick.

This allows you to move instances instantaneously, and should usually be used when initially placing an instance such as a bullet to prevent graphical glitches.

func reset_instances_physics_interpolation() -> void

When using physics interpolation, this function allows you to prevent interpolation for all instances in the current physics tick.

This allows you to move all instances instantaneously, and should usually be used when initially placing instances to prevent graphical glitches.

func set_buffer_interpolated(buffer_curr: PackedFloat32Array, buffer_prev: PackedFloat32Array) -> void

An alternative to setting the buffer property, which can be used with physics interpolation. This method takes two arrays, and can set the data for the current and previous tick in one go. The renderer will automatically interpolate the data at each frame.

This is useful for situations where the order of instances may change from physics tick to tick, such as particle systems.

When the order of instances is coherent, the simpler alternative of setting buffer can still be used with interpolation.

func set_instance_color(instance: int, color: Color) -> void

Sets the color of a specific instance by multiplying the mesh's existing vertex colors. This allows for different color tinting per instance.

Note: Each component is stored in 32 bits in the Forward+ and Mobile rendering methods, but is packed into 16 bits in the Compatibility rendering method.

For the color to take effect, ensure that use_colors is true on the MultiMesh and BaseMaterial3D.vertex_color_use_as_albedo is true on the material. If you intend to set an absolute color instead of tinting, make sure the material's albedo color is set to pure white (Color(1, 1, 1)).

func set_instance_custom_data(instance: int, custom_data: Color) -> void

Sets custom data for a specific instance. custom_data is a Color type only to contain 4 floating-point numbers.

Note: Each number is stored in 32 bits in the Forward+ and Mobile rendering methods, but is packed into 16 bits in the Compatibility rendering method.

For the custom data to be used, ensure that use_custom_data is true.

This custom instance data has to be manually accessed in your custom shader using INSTANCE_CUSTOM.

func set_instance_transform(instance: int, transform: Transform3D) -> void

Sets the Transform3D for a specific instance.

func set_instance_transform_2d(instance: int, transform: Transform2D) -> void

Sets the Transform2D for a specific instance.

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)

Enums & Constants

enum TransformFormat
TRANSFORM_2D = 0

Use this when using 2D transforms.

TRANSFORM_3D = 1

Use this when using 3D transforms.

enum PhysicsInterpolationQuality
INTERP_QUALITY_FAST = 0

Always interpolate using Basis lerping, which can produce warping artifacts in some situations.

INTERP_QUALITY_HIGH = 1

Attempt to interpolate using Basis slerping (spherical linear interpolation) where possible, otherwise fall back to lerping.