Murmuration at Dusk
A winter starling murmuration over a reed-bed marsh at sunset, live in 3D: about 100,000 starlings, steered by 7-nearest-neighbour flocking, fold into dark smoky sheets, scatter from a peregrine and pour into the reeds as birdwatchers look on.
Made in Claude Code (Claude subscription) with claude-opus-5.5. The agent wrote code, looked at the preview and fixed it over several iterations. This is not a single-shot result: comparisons on this site show models in one attempt.
Starting prompt
Build "Murmuration at Dusk": a real-time simulation of a winter starling murmuration over a reed-bed nature reserve at sunset, filmed from the water's edge while a few birdwatchers on a boardwalk watch it too. About a hundred thousand starlings swirl, fold and pulse as one dark, smoky shape. The shape is never scripted: it emerges from local flocking rules. It should look like a nature-documentary shot and be breathtaking from the very first frame. Single self-contained HTML file; Three.js r170 from a CDN via an import map; build everything procedurally (2D-canvas textures are fine), with no external models, images or other files.
The birds (common starling, Sturnus vulgaris):
- True scale in metres: body about 0.21 m long, wingspan about 0.40 m, cruising speed 12.5–16 m/s (a personal cruise speed per agent), clamped to 10–20 m/s. The flock is 80–250 m across, flies 40–125 m above the reeds and about 280–360 m from the camera.
- Level of detail: simulate about 4,800 agents on the CPU (2,400 on a phone). Each agent stands for a loose knot of 22 starlings that fly with it, offset up to about ±3 m in the agent's own frame, with offsets that drift slowly. That gives about 105,000 starlings on a laptop and 53,000 on a phone.
- 8 of the 22 are tiny low-poly meshes in one instanced draw: pointed triangular wings, a short tail and a stubby body. The wings flap at 10.5–13.5 Hz in the vertex shader, with a per-bird phase hashed from gl_InstanceID. Birds glide with still wings for short spells. A mesh bird is never drawn smaller than about 1.5 px across.
- The other 14 are single dark points of 1.5–3 px whose size flickers with the wingbeat, so the flock shimmers.
- Each agent also draws one soft, dark, low-opacity disc about 4.5 m in radius. Its opacity follows the projected wing area (the absolute dot product of the banked wing normal and the view direction). Where the flock is seen edge-on, or banks towards the viewer, the discs stack into near-black smoke. Seen face-on, the flock stays a pale, grainy veil. This contrast gives a real murmuration its smoky, shifting look.
- Every frame, pack the agents' positions and visibility and their velocity and bank angle into two RGBA float DataTextures. The shaders read them with texelFetch.
- Birds, points and discs are near-black (linear about 0.003) with only a trace of distance haze. They stay crisp, with no bloom.
The flocking (the real science):
- Use topological rather than metric interaction (Ballerini et al., 2008): each agent responds to its 7 nearest neighbours, found with a hashed uniform grid (6 m cells, counting sort) rebuilt every step. Refresh a third of the neighbour lists per step, in turn.
- Separation within 6.5 m (an agent is a knot of birds), alignment of velocity, and cohesion weighted by centrality (Hemelrijk). An agent at the edge, whose neighbours all lie on one side, pulls inwards strongly; an interior agent barely does. Add speed relaxation to the personal cruise speed and a gentle, smooth random wander.
- The roost: an agent more than 95 m (horizontally) from a slowly wandering roost point turns back towards it. The roost point follows a figure-eight of ±72 m with a 30 s period. Use only lateral steering (about 5.5 m/s²), never a spring, so the flock folds back over itself instead of being squeezed into a ball. Add a weak pull back for stragglers more than 90 m from the flock centre, and a soft floor at 42 m and ceiling at 122 m.
- The sheet: a soft, critically damped pull towards a plane through the flock centre keeps the flock a sheet about 10–20 m thick. The plane's normal slowly tilts by up to about 50°, as the flock banks as a whole, so the sheet turns from a dark edge-on band into a pale face-on veil and back.
- Each agent banks into its turns (bank = atan(lateral acceleration / g)), and the banking drives the wing orientation of its birds.
- The predator: a peregrine falcon (wingspan about 1.1 m, swept wings, 4.6 Hz beats, gliding in the stoop) sweeps through the flock along a quadratic Bézier curve over 7.5 s, at film times 16 s and 41 s and whenever someone taps. Agents within 25 m flee and become fully alarmed, with an escape direction. The alarm spreads to neighbours with a delay of about 0.17 s per hop, carrying the escape direction with it, and decays over about 1.4 s. Alarmed agents swerve, roll and speed up by up to 25%. This launches a dark wave that rolls through the flock and often splits it into lobes that later rejoin. The falcon never catches anything.
The scene: a real place, with human scale
- A low winter sun 1.6° above the horizon, 14° to the right of the opening view. The sky is a gradient shader running from deep blue-violet overhead through violet and rose to burning orange at the horizon, with a Mie-like glow around the sun and a small gold disc. Add streaky cirrus from fbm on a cloud deck, lit pink to gold from below near the sun and darker away from it. Dither the output to avoid banding.
- Open still water across the whole middle ground, as a real mirror (three/addons Reflector at half resolution, with a custom shader):
- Fresnel: dark near the camera, bright towards the horizon.
- Slowly drifting wind patches that ripple and stretch the reflection vertically, with sun glitter on them (never on the water closest to the camera).
- It reflects the sky, the reeds, the boardwalk and the flock.
- Reeds (Phragmites) drawn on a 2D canvas into a four-tile atlas, with each tile clipped to its own area: tapered stems, drooping leaves and feathery plumes. Place them as camera-facing instanced cards that sway in the wind in the vertex shader:
- dense near stands framing the bottom corners;
- islands 80–290 m away, and low reeds along the boardwalk;
- a continuous far reed bed 430–520 m away, with two open channels.
They are true black silhouettes; only the plumes catch a faint backlit glow towards the sun. Use alpha-to-coverage with MSAA.
- Human scale, subtle and warm:
- A wooden boardwalk with posts and railings leads from the bottom-left corner about 50 m out to a viewing platform.
- A small bird hide on stilts, with a viewing slot, stands beside the platform.
- Four birdwatchers stand on the platform as simple, convincing side-on silhouettes drawn on a canvas: one with binoculars raised, one at a spotting scope on a tripod, one with hands in pockets, and a child pointing at the flock.
- The horizon:
- a few bare oaks and ashes on the far shore (canvas-drawn, with tapered limbs);
- a village 2.3 km away with a church spire, roofs and warm lit windows;
- a farm and a lone cottage with lit windows;
- low hills 6 km away.
The village, farm and hills are procedural silhouette bands drawn in a shader. Everything fades into a horizon haze whose colour comes from the sky in that direction.
- The light: ACES filmic tone mapping and sRGB output. Keep all colours in linear space. End every custom ShaderMaterial with the tonemapping_fragment and colorspace_fragment chunks, so nothing looks washed out. The scene is dramatic but readable, never muddy, with true black silhouettes. Add a soft CSS vignette.
Timeline (90 s, then it loops seamlessly). Drive it by wall-clock time (performance.now), never by a frame-capped dt, so a slow device drops frames instead of slowing the film:
1. 0–12 s, gathering: the flock is already airborne and swirling in mid-frame from the very first frame. Pre-roll about 6 s of simulation before the first frame. Small groups stream in from the edges and merge, and a loose stream of about 1,300 starlings passes fairly close above the camera.
2. 12–54 s, murmuration: folding sheets, lobes and ribbons, with two falcon passes and their waves and splits.
3. 54–76 s, going to roost: up to 72% of the birds peel off and pour down in a swirling funnel into the far reed bed, where they vanish. Meanwhile the light dims by about 30%, the sky deepens and the village windows brighten.
4. 66–90 s, and on into the first 10 s of the next loop: the birds that went down return as new groups streaming in from both sides and rebuild the flock, and the light recovers. Every scripted value is periodic in 90 s, so the loop never shows a cut.
Camera: low, about 2.25 m above the water at the water's edge, with a slow drift of about ±1.6 m along a path that repeats every 90 s.
- It looks towards the flock's smoothed centroid, with heavy damping and within limits, and keeps the whole flock in frame: it tilts up towards the elevation under which 99.5% of the birds fly (a cheap histogram each frame), but never so far that the horizon drops below 18% of the frame height; if that is not enough, it widens the field of view by up to 40%.
- The horizontal field of view is about 70° in landscape. In portrait, the vertical field of view goes up to 72° and the view leans left, so the boardwalk and the birdwatchers stay in frame.
- Drag to look around and use the wheel or a pinch to zoom. The camera eases back to its path 4 s after the last input. A tap sends the falcon on a pass right away.
Interface: a small, elegant caption at the bottom left.
- The title, in an italic Cormorant Garamond.
- One line: "no leader, no script · each agent follows its 7 nearest neighbours".
- Live readouts: the number of starlings, their mean speed in m/s, the flock width in metres and a phase label (gathering, murmuration, peregrine!, going to roost).
- A faint interaction hint at the bottom right, shown only while a mouse moves over the page.
- Nothing else on screen. Before the first WebGL frame, the page background is a CSS gradient in the scene's colours, never black.
Performance: steady 60 fps on a laptop at a device pixel ratio of 2.
- Use typed arrays for every agent quantity and allocate no per-frame objects.
- Simulate at most 0.1 s per frame, in two sub-steps when a frame takes more than 34 ms. The film clock itself never stops or slows.
- Cap the pixel ratio at 2. Lower it in steps of 0.25 if the average frame time stays above 24 ms.
- Before the clock starts, warm up every shader, the falcon included, with renderer.compileAsync: once with the mirror's render target bound and once for the screen. Meanwhile run the pre-roll in short slices that yield to the browser, then render one frame. No frame may take more than about 300 ms, even during start-up.
Engineering rules (the page must work on the first load, with no fixes):
- Single self-contained HTML file, with Three.js r170 and its addons only via this exact import map: <script type="importmap">{"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/"}}</script>. Google Fonts are allowed; there is no other fetch.
- It runs in a sandboxed iframe (allow-scripts only): no localStorage, sessionStorage, cookies, alert or window.top.
- It starts automatically and is visibly moving from the first frame, and loops forever. It works at 1380x620 and at 390x844 with touch.
- Use only methods that exist in Three.js r170. Give every custom ShaderMaterial `fog: false`, and declare every uniform, constant and varying its shaders use. GLSL ES 3.0 reserves words such as `patch` and `sample`; don't use them as names.
- Declare each identifier once. Never reuse one scratch vector for two values that are needed at the same time.
- Guard every animated value against NaN, normalising only vectors with a nonzero length and clamping speeds.
- No console errors.
- Answer with the complete file exactly once, in a single ```html code block: no drafts, partial snippets or notes.
Keep planning brief and write the code. Aim for roughly 850 to 1,050 lines, compact and well structured.