Table of Contents

Class MultiMesh

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

Inheritance
MultiMesh

Remarks

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.

See Also

Properties

buffer

var buffer : PackedFloat32Array = PackedFloat32Array()

Property Value

PackedFloat32Array

Remarks

color_array

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

var color_array : PackedColorArray

Property Value

PackedColorArray

custom_aabb

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

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

Property Value

AABB

Remarks

  • void set_custom_aabb(AABB value)
  • AABB get_custom_aabb

custom_data_array

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

var custom_data_array : PackedColorArray

Property Value

PackedColorArray

instance_count

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 instance_count : int = 0

Property Value

int

Remarks

  • void set_instance_count(int value)
  • int get_instance_count

mesh

Mesh resource to be instanced.

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

var mesh : Mesh

Property Value

Mesh

Remarks

  • void set_mesh(Mesh value)
  • Mesh get_mesh

physics_interpolation_quality

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 physics_interpolation_quality : int = 0

Property Value

int

Remarks

  • void set_physics_interpolation_quality(int value)
  • int get_physics_interpolation_quality

transform_2d_array

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_2d_array : PackedVector2Array

Property Value

PackedVector2Array

transform_array

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_array : PackedVector3Array

Property Value

PackedVector3Array

transform_format

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

var transform_format : int = 0

Property Value

int

Remarks

  • void set_transform_format(int value)
  • int get_transform_format

use_colors

If true, the MultiMesh will use color data (see MultiMesh.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_colors : bool = false

Property Value

bool

Remarks

  • void set_use_colors(bool value)
  • bool is_using_colors

use_custom_data

If true, the MultiMesh will use custom data (see MultiMesh.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 use_custom_data : bool = false

Property Value

bool

Remarks

  • void set_use_custom_data(bool value)
  • bool is_using_custom_data

visible_instance_count

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

var visible_instance_count : int = -1

Property Value

int

Remarks

  • void set_visible_instance_count(int value)
  • int get_visible_instance_count

Methods

get_aabb

Qualifiers: const

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

AABB get_aabb

get_instance_color(int)

Qualifiers: const

Gets a specific instance's color multiplier.

Color get_instance_color(int instance)

Parameters

instance int

get_instance_custom_data(int)

Qualifiers: const

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

Color get_instance_custom_data(int instance)

Parameters

instance int

get_instance_transform(int)

Qualifiers: const

Returns the Transform3D of a specific instance.

Transform3D get_instance_transform(int instance)

Parameters

instance int

get_instance_transform_2d(int)

Qualifiers: const

Returns the Transform2D of a specific instance.

Transform2D get_instance_transform_2d(int instance)

Parameters

instance int

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

void reset_instance_physics_interpolation(int instance)

Parameters

instance int

set_buffer_interpolated(PackedFloat32Array, 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.

void set_buffer_interpolated(PackedFloat32Array buffer_curr, PackedFloat32Array buffer_prev)

Parameters

buffer_curr PackedFloat32Array
buffer_prev PackedFloat32Array

set_instance_color(int, 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 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)).

void set_instance_color(int instance, Color color)

Parameters

instance int
color Color

set_instance_custom_data(int, 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.

void set_instance_custom_data(int instance, Color custom_data)

Parameters

instance int
custom_data Color

set_instance_transform(int, Transform3D)

Sets the Transform3D for a specific instance.

void set_instance_transform(int instance, Transform3D transform)

Parameters

instance int
transform Transform3D

set_instance_transform_2d(int, Transform2D)

Sets the Transform2D for a specific instance.

void set_instance_transform_2d(int instance, Transform2D transform)

Parameters

instance int
transform Transform2D