Skip to content
← All articles

Noa Lindqvist /three.js 7 min

Instanced meshes for UI: drawing 400 cubes in one call

How a GitHub-style contribution matrix went from 371 meshes to a single draw call — and gained flying cubes and water ripples on the way.

  • #three.js
  • #performance
  • #webgl

The first version of my contribution matrix created one Mesh per day, plus an outline for each. It looked fine and ran fine on my laptop. On a mid-range phone it dropped to 30 fps.

One mesh, many instances

InstancedMesh draws the same geometry many times with a different matrix and colour per instance. The CPU work moves into a tight loop that writes matrices into a typed array:

for (let i = 0; i < cubes.length; i++) {
  dummy.position.set(c.x, c.y, c.z + lift);
  dummy.updateMatrix();
  mesh.setMatrixAt(i, dummy.matrix);
}
mesh.instanceMatrix.needsUpdate = true;

Picking still works

The raycaster returns an instanceId, so tooltips keep working. Set a fixed boundingSphere if instances move far away during an entrance animation.

Pause when nobody is looking

An IntersectionObserver and the visibilitychange event stop the render loop when the section is off-screen. That single change saved more battery than every micro-optimisation together.