MultiMesh
Provides high-performance drawing of a mesh multiple times using GPU instancing.
MultiMesh.swift:22class MultiMesh
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.
Superclasses
class Resource
Base class for serializable objects.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Types
Type members
Instance members
var buffer: PackedFloat32Array
var instanceCount: Int32
Number of instances that will get drawn. This clears and (re)sizes the buffers. Setting data format or flags afterwards will have no effect.
var mesh: Mesh?
Mesh
resource to be instanced.var transformFormat: MultiMesh.TransformFormat
Format of transform used to transform mesh, either 2D or 3D.
var useColors: Bool
If
true
, theMultiMesh
will use color data (seesetInstanceColor(instance:color:)
). Can only be set wheninstanceCount
is0
or less. This means that you need to call this method before setting the instance count, or temporarily reset it to0
.var useCustomData: Bool
If
true
, theMultiMesh
will use custom data (seesetInstanceCustomData(instance:customData:)
). Can only be set wheninstanceCount
is0
or less. This means that you need to call this method before setting the instance count, or temporarily reset it to0
.var visibleInstanceCount: Int32
Limits the number of instances drawn, -1 draws all instances. Changing this does not change the sizes of the buffers.
func getAabb(
) -> AABB Returns the visibility axis-aligned bounding box in local space.
func getInstanceColor(instance: Int32
) -> Color Gets a specific instance’s color multiplier.
func getInstanceCustomData(instance: Int32
) -> Color Returns the custom data that has been set for a specific instance.
func getInstanceTransform(instance: Int32
) -> Transform3D Returns the
Transform3D
of a specific instance.func getInstanceTransform2d(instance: Int32
) -> Transform2D Returns the
Transform2D
of a specific instance.func setInstanceColor(instance: Int32, 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.
func setInstanceCustomData(instance: Int32, customData: Color
) Sets custom data for a specific instance. Although
Color
is used, it is just a container for 4 floating point numbers.func setInstanceTransform(instance: Int32, transform: Transform3D
) Sets the
Transform3D
for a specific instance.func setInstanceTransform2d(instance: Int32, transform: Transform2D
) Sets the
Transform2D
for a specific instance.