# 06. InstancedMesh: 10,000 cubes in one draw call, explained

**Goal:** draw thousands of copies of the same object efficiently, each with its own position, rotation, scale and color.

## Why instancing?

Every separate `Mesh` costs at least one **draw call** (a command from the CPU to the GPU). A few hundred draw calls are fine; tens of thousands will cap your frame rate on the CPU even if the GPU is idle. `InstancedMesh` draws N copies of one geometry + material in **one** draw call, reading a per-instance matrix (and optional color) from a buffer.

| Technique | Draw calls for 10,000 cubes | Per-object flexibility |
| --- | --- | --- |
| 10,000 `Mesh` objects | ~10,000 | Full (own material, own geometry) |
| `InstancedMesh` | 1 | Same geometry and material; own transform and color |
| Merged geometry (`BufferGeometryUtils.mergeGeometries`) | 1 | Static; moving one object means rebuilding |
| `BatchedMesh` | 1 per material | Different geometries, same material |

## Walkthrough

```js
const mesh = new THREE.InstancedMesh(new THREE.BoxGeometry(0.6, 0.6, 0.6), new THREE.MeshStandardMaterial(), COUNT);
```
The third argument is the maximum number of instances. You can render fewer by setting `mesh.count`.

```js
const dummy = new THREE.Object3D();
dummy.position.setFromSphericalCoords(r, phi, theta);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
```
Instances are stored as 4x4 matrices. The easiest way to build one is to position, rotate and scale a throwaway `Object3D` and copy its `matrix`. `r = 30 * Math.cbrt(Math.random())` spreads points evenly through a sphere's volume (plain `Math.random()` would cluster them at the center).

```js
mesh.setColorAt(i, color.setHSL(r / 30, 0.7, 0.55));
```
Per-instance colors multiply with the material color. Calling `setColorAt` creates the `instanceColor` buffer automatically.

```js
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
...
mesh.instanceMatrix.needsUpdate = true;
```
Tell WebGL the buffer changes often, update matrices each frame, then flag the buffer for upload. For static instances, set matrices once and skip this.

```js
renderer.info.render.calls
```
`renderer.info` reports draw calls, triangles and memory use for the last frame. It is the first thing to check when a scene is slow.

## Performance notes

- Updating 10,000 matrices in JavaScript each frame is fine on desktops; on low-end phones, animate fewer instances or move motion into the vertex shader.
- Raycasting works with `InstancedMesh`: hits include `instanceId`.
- Frustum culling uses the bounds of all instances together; call `mesh.computeBoundingSphere()` after moving instances far from their original positions.

## Try this

1. Raise `COUNT` to 100,000 and watch the frame rate.
2. Replace the matrix loop with a static layout and remove `DynamicDrawUsage`.
3. Combine with example 07: raycast and recolor the hovered instance with `setColorAt(hit.instanceId, ...)` followed by `mesh.instanceColor.needsUpdate = true`.
