Nagi
A flock of boids in WebGL that gathers into any word you type, then scatters. The challenge was keeping hundreds of agents smooth at 60 fps in React, and changing the simulation without changing how it feels.
Stack
- Rendering
- react-three-fiber (Three.js): a declarative scene that fits the rest of the React UI
- Drawing
- One InstancedMesh: a single draw call instead of one per boid
- Simulation
- Plain TypeScript, no React: testable and benchmarkable in Node
- State
- Zustand with selectors: controls update without re-rendering the scene
- Effects
- Bloom postprocessing: the neon glow
Notes
From letters to positions
The text you type is drawn on a hidden canvas, and its filled pixels become target points. Each boid is assigned a target and flies there with a formation style (snap, wave, spiral in or explode out), holds the shape, breathes, and dissolves back into the flock.
Keeping it fast
Neighbours are found with a spatial hash grid built on typed arrays, and each boid steers in a single pass over them. All boids are drawn with one instanced mesh, with per-instance colour and a glow value patched into the standard material, which replaced 500–1000 draw calls with one.
Anything that changes every frame stays in refs read by the render loop, never in React state, so the panels re-render only when you change a setting.
Changing it safely
The simulation uses randomness and wall-clock time inside the frame loop, so two runs never match. A benchmark harness replaces both with a seeded generator and a virtual clock, so a performance change can be proved to produce the exact same flock, hashed frame by frame, before anyone eyeballs it.