Skip to content

MultiMesh

Inherits: Resource < RefCounted < Object

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

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.

Type Name Default
PackedFloat32Array buffer PackedFloat32Array()
AABB custom_aabb AABB(0, 0, 0, 0, 0, 0)
int instance_count 0
Mesh mesh
PhysicsInterpolationQuality physics_interpolation_quality 0
TransformFormat transform_format 0
bool use_colors false
bool use_custom_data false
int visible_instance_count -1
Returns Method
AABB get_aabb() const
Color get_instance_color(instance: int) const
Color get_instance_custom_data(instance: int) const
Transform3D get_instance_transform(instance: int) const
Transform2D get_instance_transform_2d(instance: int) const
void reset_instance_physics_interpolation(instance: int)
void reset_instances_physics_interpolation()
void set_buffer_interpolated(buffer_curr: PackedFloat32Array, buffer_prev: PackedFloat32Array)
void set_instance_color(instance: int, color: Color)
void set_instance_custom_data(instance: int, custom_data: Color)
void set_instance_transform(instance: int, transform: Transform3D)
void set_instance_transform_2d(instance: int, transform: Transform2D)

TransformFormat

enum TransformFormat

TRANSFORM_2D = 0

Use this when using 2D transforms.

TRANSFORM_3D = 1

Use this when using 3D transforms.

PhysicsInterpolationQuality

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.

buffer

PackedFloat32Array buffer = PackedFloat32Array()

custom_aabb

AABB custom_aabb = AABB(0, 0, 0, 0, 0, 0)

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

instance_count

int instance_count = 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.

mesh

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().

physics_interpolation_quality

PhysicsInterpolationQuality physics_interpolation_quality = 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.

transform_format

TransformFormat transform_format = 0

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

use_colors

bool use_colors = 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.

use_custom_data

bool use_custom_data = 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.

visible_instance_count

int visible_instance_count = -1

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

get_aabb

AABB get_aabb() const

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

get_instance_color

Color get_instance_color(instance: int) const

Gets a specific instance’s color multiplier.

get_instance_custom_data

Color get_instance_custom_data(instance: int) const

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

get_instance_transform

Transform3D get_instance_transform(instance: int) const

Returns the Transform3D of a specific instance.

get_instance_transform_2d

Transform2D get_instance_transform_2d(instance: int) const

Returns the Transform2D of a specific instance.

reset_instance_physics_interpolation

void reset_instance_physics_interpolation(instance: int)

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.

reset_instances_physics_interpolation

void reset_instances_physics_interpolation()

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.

set_buffer_interpolated

void set_buffer_interpolated(buffer_curr: PackedFloat32Array, buffer_prev: PackedFloat32Array)

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.

set_instance_color

void set_instance_color(instance: int, color: Color)

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

set_instance_custom_data

void set_instance_custom_data(instance: int, custom_data: Color)

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.

set_instance_transform

void set_instance_transform(instance: int, transform: Transform3D)

Sets the Transform3D for a specific instance.

set_instance_transform_2d

void set_instance_transform_2d(instance: int, transform: Transform2D)

Sets the Transform2D for a specific instance.