Babylon.js Thin Instances: Setup, Per-Instance Data, and Practical Boundaries
Babylon.js Thin Instances render thousands of identical meshes in one draw call via per-instance matrices and attributes in GPU buffers. Covers setup, colors, custom attributes, culling, picking, memory, and a 5,000-tree example with version-aware caveats.
21 Aug 2025, 17:38 UTC

The Problem and the Takeaway
Rendering thousands of identical meshes as separate Mesh objects issues a draw call per object, which quickly saturates the GPU bottleneck. Babylon.js Thin Instances solve this by uploading per-instance world matrices (and optional colors, custom attributes) into a single GPU vertex buffer so one source mesh renders N copies in a single draw call.
Use Thin Instances when you have many visually identical objects sharing a material and skeleton. Do not use them when instances need different materials, independent animations, or morph targets — those require separate meshes or shader branching on instance ID.
How Thin Instances Work
A Mesh with thin instances retains its original geometry buffers (positions, normals, UVs) and adds a per-instance vertex buffer that stores a 4x4 world matrix (16 floats, column-major) for each copy. The vertex shader reads instanceWorld, injected automatically by Babylon, and multiplies it with each vertex position. Optional per-instance color (instanceColor) and custom float or vector attributes are added as additional vertex attributes, declared in the shader and registered via Babylon APIs.
Enabling thin instances is a one-way mutation: after calling thinInstanceSetMatrices or thinInstanceSetMatrixAt, the mesh enters thin-instance mode. The original mesh remains in the scene unless you set mesh.isVisible = false.
Worked Example: A Forest of 5,000 Trees
// 1. Create a source mesh and hide the original
const tree = BABYLON.MeshBuilder.CreateBox("tree", { size: 1 }, scene);
tree.isVisible = false;
// 2. Prepare per-instance data
const count = 5000;
const matrices = new Float32Array(count * 16); // 16 floats per matrix, column-major
const colors = new Float32Array(count * 4); // RGBA per instance
const timeOffsets = new Float32Array(count); // custom float attribute
for (let i = 0; i < count; i++) {
const matrix = new BABYLON.Matrix();
// Random position within a 200x200 area
matrix.setTranslation(
(Math.random() - 0.5) * 200,
0,
(Math.random() - 0.5) * 200
);
// Random Y rotation
matrix.multiplyInPlace(new BABYLON.Matrix().RotationY(Math.random() * Math.PI * 2));
// Random uniform scale 0.8-1.2
const scale = 0.8 + Math.random() * 0.4;
matrix.scale(scale, scale, scale);
// Copy 16 column-major floats into the flat buffer
matrix.toArray(true, matrices, i * 16); // true = column-major
// Per-instance color: green tint with varying brightness
const brightness = 0.5 + Math.random() * 0.5;
colors[i * 4 + 0] = 0.1 * brightness;
colors[i * 4 + 1] = 0.6 * brightness;
colors[i * 4 + 2] = 0.1 * brightness;
colors[i * 4 + 3] = 1.0;
// Custom float: per-instance wind offset
timeOffsets[i] = Math.random() * 100;
}
// 3. Upload matrices in bulk
tree.thinInstanceSetMatrices(matrices);
// 4. Register the per-instance color attribute (built-in name)
tree.thinInstanceAddAttribute("instanceColor", 4); // 4 components = vec4
for (let i = 0; i < count; i++) {
tree.thinInstanceSetAttributeAt("instanceColor", i, new BABYLON.Color4(colors[i * 4], colors[i * 4 + 1], colors[i * 4 + 2], colors[i * 4 + 3]));
}
// 5. Register a custom float attribute
tree.thinInstanceAddAttribute("instanceTimeOffset", 1); // 1 component = float
for (let i = 0; i < count; i++) {
tree.thinInstanceSetAttributeAt("instanceTimeOffset", i, timeOffsets[i]);
}
// 6. Finalize: upload all buffers to GPU
tree.thinInstanceRefresh();
// 7. Verify instance count after refresh
console.log("Active instances:", tree.thinInstanceCount); // expected: 5000Expected check: Open the Babylon.js Inspector (F12 → Inspector → Statistics). The drawCalls counter should reflect a single draw call for this mesh regardless of count. The tree.thinInstanceCount property returns the active instance count after refresh().
Per-Instance Colors and Custom Attributes
Babylon.js recognizes instanceColor as a built-in attribute name. When registered via thinInstanceAddAttribute, the engine injects attribute vec4 instanceColor into the vertex shader. To use it, simply set values per instance as shown in the example.
For custom data — such as per-instance time offsets, scale factors, or texture indices — use any name and declare the attribute in your vertex shader or Node Material:
// Register once before first set — defines stride in the vertex buffer
tree.thinInstanceAddAttribute("instanceWindOffset", 1); // float
// Set per instance
tree.thinInstanceSetAttributeAt("instanceWindOffset", i, Math.random() * 10);
// In a vertex shader or Node Material vertex input:
// attribute float instanceWindOffset;
// Use it to drive animation or LOD decisionsEach custom attribute consumes vertex attribute slots. A 4x4 matrix uses 4 vec4 slots, instanceColor uses 1, each custom float uses 1. Mobile GPUs often limit the total to 16 vec4 slots — test on target devices and fallback to separate InstancedMesh draw calls if the limit is exceeded.
Frustum Culling and Picking
By default, the entire instance cloud is culled against the source mesh's bounding box. If instances span a large area, this may cause overdraw. Some Babylon.js versions provide per-instance culling through bounding spheres, but exact API availability varies by release; check the documentation for your target version.
For picking, the default scene.pick returns the source mesh with thinInstanceIndex = -1. Enabling per-instance picking (when available) may expose the instance index via pickInfo.thinInstanceIndex. Behavior depends on the Babylon.js version in use; verify the property name and return value for your specific build.
Memory, Performance, and GPU Limits
| Resource | Per-Instance Cost | 5,000 Instances | 100,000 Instances |
|---|---|---|---|
| Matrix buffer (16 floats) | 64 bytes | ≈320 KB | ≈6.4 MB |
| Color buffer (4 floats) | 16 bytes | ≈80 KB | ≈1.6 MB |
| Custom float attribute (1 float) | 4 bytes | ≈20 KB | ≈400 KB |
These estimates assume the instance data lives in GPU vertex buffers. Call engine.getGPUMemoryUsage() (Chrome DevTools) or inspect via WebGL debug extensions to verify buffer sizes. For static scenes, upload matrices and attributes once and reuse the buffers; avoid calling thinInstanceSetMatrixAt every frame, as it triggers a full buffer re-upload internally.
Limitations and Common Mistakes
- Shared material, skeleton, and morph targets. All instances draw using the source mesh's material and skeleton. Per-instance material swaps are not supported; if you need different materials, split into multiple thin-instance meshes grouped by material or use shader branching on gl_InstanceID.
- Partial updates are expensive. thinInstanceSetMatrixAt(index, matrix) updates a single instance but internally re-uploads the entire matrix buffer. For frequently moving subsets (e.g., particle systems), consider dividing instances into multiple thin-instance meshes updated on different schedules, or use InstancedMesh with separate draw calls.
- GLTF export does not serialize thin instances. GLTF2Export outputs only the source mesh. Re-create thin instances at runtime after loading the model.
- Shadow map draw calls. Thin instances participate in shadow passes as drawn meshes. Shadow map performance depends on light configuration, cascade splits, and the engine's shadow generation path. No universal guarantee exists on draw-call reduction for shadows.
- Mobile vertex attribute limits. Many mobile GPUs cap vertex attribute bindings at 16 vec4 slots. A matrix (4 slots) + color (1 slot) + a few custom floats can exceed this. Profile on target devices; if the limit is hit, fall back to InstancedMesh or reduce custom attributes.
- Matrix column-major convention. Babylon.js stores matrices as column-major float32 arrays. When copying matrices into the buffer, ensure the same convention is used in custom shaders; mismatched row/column major layout will produce incorrect transforms.
Verification Checklist
- Create a minimal scene with the example above. Open the Babylon.js Inspector and confirm drawCalls = 1 for the thin-instance mesh.
- Check that tree.thinInstanceCount matches the number of instances you set after calling thinInstanceRefresh().
- In a Node Material, add instanceWorld (mat4) and instanceColor (vec4) as vertex inputs; connect to the world and vColor outputs. Verify instances render with per-instance transforms and colors.
- Test picking behavior: add a pointer move listener and log pi.pickInfo.thinInstanceIndex. Note that index values and availability depend on your Babylon.js version; some builds may return -1 or the source mesh.
- Profile GPU memory usage before and after enabling thin instances to confirm buffer sizes align with the estimates above.
If all checks pass, Thin Instances are working as intended for your scene. For cases requiring per-instance materials, frequent dynamic updates, or mobile GPU attribute limits, plan a fallback to InstancedMesh or multiple thin-instance meshes grouped by material or update frequency.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.