Skip to content

InstancedMesh ​

Renderable node that draws many copies of the same mesh efficiently.

InstancedMesh stores a single geometry/material pair and renders it multiple times in a single draw call using per-instance transforms and colors. This dramatically improves performance when drawing large numbers of identical objects by reducing CPU overhead and state changes.

Per-instance data is stored in instance-rate buffer attributes. Transforms default to identity and colors to white. Each instance is addressed by a zero-based index. Updates through SetTransformAt and SetColorAt mark the underlying attribute for re-upload.

Custom per-instance data can be added with AddInstanceAttribute and consumed by declaring a matching attribute in the shader code of a ShaderMaterial. The built-in materials only use the instance transform and color attributes.

cpp
const auto geometry = vglx::BoxGeometry::Create({1.0f, 1.0f, 1.0f});
const auto material = vglx::PhongMaterial::Create({.color = 0xFFFFFFu});

auto boxes = my_scene->Add(vglx::InstancedMesh::Create(
  geometry, material, 2500
));

for (auto i = 0; i < 50; ++i) {
  for (auto j = 0; j < 50; ++j) {
    vglx::Transform3 t {};
    t.SetPosition({i * 2.0f - 49.0f, j * 2.0f - 49.0f, 0.0f});
    boxes->SetTransformAt(j * 50 + i, t.Get());
  }
}

Out-of-range indices are invalid and result in undefined behavior. Culling is performed using a single bounding volume that determines visibility of the instance set as a whole.

INFO

Derives from Mesh and inherits all public properties and methods.

Construction ​

Constructors ​


InstancedMesh() ​

Constructs an instanced mesh.

Instance transforms are initialized to identity and instance colors to white.

cpp
InstancedMesh::InstancedMesh(std::shared_ptr<Geometry> geometry, std::shared_ptr<Material> material, std::size_t count);
ParameterDescription
geometryShared geometry used for every instance.
materialShared material used for every instance.
countNumber of instances to allocate.

Factories preferred ​


InstancedMesh::Create() std::unique_ptr<InstancedMesh> ​

Creates an instance of InstancedMesh.

cpp
static std::unique_ptr<InstancedMesh> InstancedMesh::Create(std::shared_ptr<Geometry> geometry, std::shared_ptr<Material> material, std::size_t count);
ParameterDescription
geometryShared geometry used across all instances.
materialShared material used across all instances.
countNumber of instances to allocate.

Functions ​

AddInstanceAttribute() void ​

Adds a custom per-instance attribute to this mesh.

The attribute must have an instance rate, a unique name within the mesh and an element count that matches the instance count. Attributes that violate these constraints are reported and rejected.

cpp
void InstancedMesh::AddInstanceAttribute(std::shared_ptr<BufferAttribute> attribute);
ParameterDescription
attributeBuffer attribute to add.

BoundingBox() Box3 virtual ​

Computes a bounding box that encloses all instances.

cpp
Box3 InstancedMesh::BoundingBox() override;

BoundingSphere() Sphere virtual ​

Computes a bounding sphere that encloses all instances.

cpp
Sphere InstancedMesh::BoundingSphere() override;

ColorAt() Color ​

Returns the per-instance color at the given index.

cpp
Color InstancedMesh::ColorAt(std::size_t idx) const;
ParameterDescription
idxInstance index.

GetCount() size_t ​

Returns the number of allocated instances.

cpp
size_t InstancedMesh::GetCount() const;

GetDrawCount() size_t ​

Returns the number of instances to draw.

cpp
size_t InstancedMesh::GetDrawCount() const;

GetInstanceAttribute() std::shared_ptr<BufferAttribute> ​

Returns the instance attribute with the given name.

cpp
std::shared_ptr<BufferAttribute> InstancedMesh::GetInstanceAttribute(std::string_view name) const;
ParameterDescription
nameAttribute name to look up.

GetInstanceAttributes() const std::vector<std::shared_ptr< BufferAttribute> >& ​

Returns the list of instance attributes stored on this mesh.

cpp
const std::vector<std::shared_ptr< BufferAttribute> >& InstancedMesh::GetInstanceAttributes() const;

GetNodeType() Node::Type virtual ​

Identifies this node as Node::Type::InstancedMesh.

cpp
Type vglx::InstancedMesh::GetNodeType() const override;

SetColorAt() void ​

Sets the per-instance color at the given index.

cpp
void InstancedMesh::SetColorAt(std::size_t idx, const Color& color);
ParameterDescription
idxInstance index.
colorNew color for the instance.

SetDrawCount() void ​

Sets the number of instances to draw.

cpp
void InstancedMesh::SetDrawCount(std::size_t draw_count);
ParameterDescription
draw_countNumber of instances. Must not exceed the initial allocation.

SetTransformAt() void ​

Sets the per-instance transform at the given index.

cpp
void InstancedMesh::SetTransformAt(std::size_t idx, const Matrix4& matrix);
ParameterDescription
idxInstance index.
matrixA world-space transform matrix for the instance.

TransformAt() Matrix4 ​

Returns the per-instance transform matrix at the given index.

cpp
Matrix4 InstancedMesh::TransformAt(std::size_t idx) const;
ParameterDescription
idxInstance index.

Released under the MIT License.