Instanced Mesh GPU
App: apps/instanced_mesh_gpu/
Demonstrates true GPU instancing via Vulkan SSBO. A 3D grid of up to 1,000 cube instances is rendered in a single draw call. Instance positions are updated each frame with a sine-wave animation.
Run from root
Section titled “Run from root”% ./plume3d instanced_mesh_gpuor
Run from app directory
Section titled “Run from app directory”% ./../plume3d .What it does
Section titled “What it does”- Creates a cube mesh programmatically (
Mesh.new(vertices, "triangles")). - Creates an
InstancedMeshviaScene.createInstancedMesh("gpu_cubes"). - Enables GPU instancing:
instanced.useGpuInstancing = true— a single Vulkan draw call handles all instances via SSBO. - Sets a PBR material:
instanced.setMaterial(r, g, b, a, metallic, roughness). - Populates a 3D grid with
addInstance(x, y, z). - Each frame in
update(dt), usessetInstancePosition(index, x, y, z)to apply a sine-wave offset per column.
Key code pattern
Section titled “Key code pattern”// Setup_instanced = _scene.createInstancedMesh("gpu_cubes")_instanced.setMesh(_cubeMesh)_instanced.useGpuInstancing = true_instanced.setMaterial(0.2, 0.6, 0.9, 1.0, 0.0, 0.3)
for (i in 0..._instanceCount) { _instanced.addInstance(x, 0, z)}
// Per framefor (i in 0..._instanced.instanceCount) { var wave = Math.sin(_time * 2 + x * 0.3 + z * 0.3) * 0.3 _instanced.setInstancePosition(i, x, y + wave, z)}- InstancedMesh —
createInstancedMesh,setMesh,useGpuInstancing,setMaterial,addInstance,setInstancePosition,instanceCount. - Scene —
createInstancedMesh. - Mesh —
new(vertices, drawMode)for procedural cube. - Math —
sin.
Scaling up
Section titled “Scaling up”- Replace the cube mesh with any loaded
.blendmesh viasetMeshFromNode(node). - Use
setInstanceTransform(index, matrix)to apply full rotation and scale per instance. - Use
setInstanceTransforms(matrices, count)for bulk updates from a physics simulation. - Combine with ParticleEmitter — particles also use instanced meshes internally, driven by Jolt physics bodies.