MultiMesh provides low-level mesh instancing. Drawing thousands of godot.MeshInstance
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.
Constructor
Variables
customDataFormat:MultiMesh_CustomDataFormatEnum
Format of custom data in custom data array that gets passed to shader.
instanceCount:Int
Number of instances that will get drawn. This clears and (re)sizes the buffers. By default, all instances are drawn but you can limit this with godot.MultiMesh.visibleInstanceCount
.
transformFormat:MultiMesh_TransformFormatEnum
Format of transform used to transform mesh, either 2D or 3D.
visibleInstanceCount:Int
Limits the number of instances drawn, -1 draws all instances. Changing this does not change the sizes of the buffers.
Methods
getAabb():AABB
Returns the visibility axis-aligned bounding box in local space. See also godot.VisualInstance.getTransformedAabb
.
getInstanceCustomData(instance:Int):Color
Returns the custom data that has been set for a specific instance.
getInstanceTransform2d(instance:Int):Transform2D
Returns the godot.Transform2D
of a specific instance.
setAsBulkArray(array:HaxeArray<Single>):Void
Sets all data related to the instances in one go. This is especially useful when loading the data from disk or preparing the data from GDNative.
All data is packed in one large float array. An array may look like this: Transform for instance 1, color data for instance 1, custom data for instance 1, transform for instance 2, color data for instance 2, etc...
godot.Transform
is stored as 12 floats, godot.Transform2D
is stored as 8 floats, COLOR_8BIT
/ CUSTOM_DATA_8BIT
is stored as 1 float (4 bytes as is) and COLOR_FLOAT
/ CUSTOM_DATA_FLOAT
is stored as 4 floats.
setInstanceColor(instance:Int, color:Color):Void
Sets the color of a specific instance by multiplying the mesh's existing vertex colors.
For the color to take effect, ensure that godot.MultiMesh.colorFormat
is non-null
on the godot.MultiMesh
and godot.SpatialMaterial.vertexColorUseAsAlbedo
is true
on the material.
setInstanceCustomData(instance:Int, customData:Color):Void
Sets custom data for a specific instance. Although godot.Color
is used, it is just a container for 4 floating point numbers. The format of the number can change depending on the godot.MultiMesh_CustomDataFormatEnum
used.
setInstanceTransform(instance:Int, transform:Transform):Void
Sets the godot.Transform
for a specific instance.
setInstanceTransform2d(instance:Int, transform:Transform2D):Void
Sets the godot.Transform2D
for a specific instance.