Boids flocking
Emergent flocking from three local steering rules per agent: separation, alignment and cohesion, computed from neighbours within a view radius.
In the family Simulation & animation, crowds & agents
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | Official add-on | examples/webgpu_compute_birds.html docs Official GPU flocking example; not a library. |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | |
| Unity | None found | |
| Unreal 5.7 | Built in | Niagara GPU neighbor-grid & flocking particle modules docs |
| Blender 5.2 | Built in | Particle Properties > Physics > Boids docs |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Each agent looks at neighbours within a radius and field of view. Separation steers away from nearby neighbours, alignment steers toward their average heading, and cohesion steers toward their centre. The three steering vectors are weighted, summed and clamped to a maximum acceleration, then integrated into velocity and position with a speed limit. Obstacle avoidance, goal seeking and predator flee are extra steering terms. Neighbour lookups use a spatial hash or grid; on the GPU the algorithm maps to one thread per boid with a sorted-cell neighbour search. Orientation is set from velocity with a banking term for believable flight.
When to choose it
Reach for it when
- want birds or fish to move together as a believable group
- crowd of agents needs natural swarming motion
- individual agents collide or scatter instead of flocking
- emergent group movement from simple local rules
- cheap schooling behaviour for ambient creatures
Choose it for birds, fish, swarms and crowds where plausible group motion matters more than individual intent. Prefer Physarum agent transport networks when agents should leave trails and form networks, Verlet integration with relaxation constraints for physically linked groups, and navmesh/path planning libraries when individuals need to reach distinct goals.
Field marks
- O(n^2) neighbour search unless a grid or hash is used
- Flocks split or collapse into a ball with badly balanced weights
- Jitter and oscillation when separation and cohesion fight at fixed radius
- Agents pass through concave obstacles unless explicit avoidance is added
Presets
Boids flocking — separation, alignment & cohesion murmuration with wake trails
{
"P_SPEED": 1,
"P_COHESION": 0.85,
"P_SEPARATION": 0.65,
"P_TRAIL_LENGTH": 0.75
}Show GLSL (125 lines)
const float P_SPEED = 1.0; // P Speed [0.0, 2.5, 0.05]
const float P_COHESION = 0.85; // P Cohesion [0.1, 2.0, 0.05]
const float P_SEPARATION = 0.65; // P Separation [0.1, 2.0, 0.05]
const float P_TRAIL_LENGTH = 0.75; // P Trail Length [0.0, 1.8, 0.05]
vec2 hash21(float p) {
vec3 p3 = fract(vec3(p) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
// Compute smooth murmuration trajectory with cohesive flock ribbon + local separation offset
vec2 boidPos(int idx, float t, vec2 attr) {
float fi = float(idx);
vec2 h = hash21(fi * 17.3 + 3.1);
float flockId = step(0.5, fract(fi * 0.5));
float phase = t * (0.65 + 0.12 * h.x) + fi * 0.24 + flockId * 3.14159;
// Ribbon guide path driven by cohesion toward moving attractor
float coh = clamp(P_COHESION, 0.1, 2.2);
float spread = mix(0.42, 0.11, clamp((coh - 0.1) / 1.8, 0.0, 1.0));
vec2 guide = vec2(
0.46 * sin(phase * 0.78 + flockId * 1.4) + 0.18 * cos(phase * 1.55),
0.34 * cos(phase * 0.92) + 0.16 * sin(phase * 1.38 + flockId)
);
guide = mix(guide, attr * 0.75, 0.25 * clamp(coh, 0.0, 1.5));
// Separation micro-orbit around ribbon center
float sep = max(P_SEPARATION, 0.05) * spread;
float ang = fi * 2.39996 + t * (1.2 + 0.6 * h.y);
vec2 offset = vec2(cos(ang), sin(ang)) * sep * (0.45 + 0.55 * h.y);
offset += 0.05 * sep * vec2(sin(t * 2.1 + fi), cos(t * 1.9 - fi * 1.3));
vec2 p = guide + offset;
// Soft obstacle avoidance around center beacon
vec2 dObs = p - vec2(0.0, 0.0);
float distObs = length(dObs) + 1e-4;
float avoid = smoothstep(0.24, 0.08, distObs);
p += (dObs / distObs) * avoid * 0.14;
return p;
}
float sdDart(vec2 p, vec2 dir, float size) {
vec2 n = normalize(dir + vec2(1e-5, 0.0));
vec2 perp = vec2(-n.y, n.x);
vec2 q = vec2(dot(p, n), dot(p, perp)) / size;
// Sleek swept-wing boid silhouette
float body = max(abs(q.y) - mix(0.42, 0.02, clamp((q.x + 0.65) / 1.65, 0.0, 1.0)), abs(q.x - 0.15) - 0.85);
float notch = -(q.x + 0.45 - abs(q.y) * 0.85);
return max(body, -notch) * size;
}
vec3 aces(vec3 x) {
x = max(x, 0.0);
return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float px = 1.0 / iResolution.y;
float t = iTime * max(P_SPEED, 0.0);
vec2 attr = vec2(0.28 * cos(t * 0.45), 0.22 * sin(t * 0.62));
if (iMouse.z > 0.5 || length(iMouse.xy) > 1.0) {
attr = (iMouse.xy - 0.5 * iResolution.xy) / iResolution.y;
}
// Deep twilight radar backdrop with subtle spatial-hash grid
vec3 bg = mix(vec3(0.025, 0.045, 0.085), vec3(0.06, 0.11, 0.19), smoothstep(-0.55, 0.55, uv.y));
vec2 g = abs(fract(uv * 8.0 - 0.5) - 0.5) / (8.0 * px);
float gridLine = smoothstep(1.4, 0.0, min(g.x, g.y));
bg += vec3(0.04, 0.09, 0.14) * gridLine * 0.32;
// Obstacle beacon at origin
float rObs = length(uv);
bg += vec3(0.16, 0.24, 0.34) * smoothstep(0.012, 0.0, abs(rObs - 0.13)) * 0.45;
bg += vec3(0.05, 0.09, 0.15) * smoothstep(0.13, 0.0, rObs);
vec3 col = bg;
float trailLen = max(P_TRAIL_LENGTH, 0.0);
for (int i = 0; i < 32; i++) {
vec2 p0 = boidPos(i, t, attr);
vec2 pPrev = boidPos(i, t - 0.04, attr);
vec2 vel = p0 - pPrev;
float speed = length(vel) + 1e-4;
vec2 dir = vel / speed;
float flockId = step(0.5, fract(float(i) * 0.5));
vec3 boidTint = mix(vec3(0.32, 0.85, 1.0), vec3(1.0, 0.64, 0.28), flockId);
// Cohesion/alignment wake trail (4 trail segments)
if (trailLen > 0.01) {
vec2 a = p0;
for (int s = 1; s <= 4; s++) {
float fs = float(s);
vec2 b = boidPos(i, t - fs * 0.055 * trailLen, attr);
vec2 pa = uv - a, ba = b - a;
float h = clamp(dot(pa, ba) / (dot(ba, ba) + 1e-5), 0.0, 1.0);
float dSeg = length(pa - ba * h);
float fade = (1.0 - fs * 0.22);
col += boidTint * exp(-dSeg * 115.0) * 0.18 * fade * min(trailLen, 1.2);
a = b;
}
}
// Leader perception & separation radius rings for first two agents
if (i < 2) {
float dRing = length(uv - p0);
float perceive = smoothstep(2.0 * px, 0.0, abs(dRing - 0.12));
float sepRing = smoothstep(2.0 * px, 0.0, abs(dRing - 0.048 * max(P_SEPARATION, 0.2)));
col += boidTint * perceive * 0.28;
col += vec3(1.0, 0.35, 0.35) * sepRing * 0.25;
}
// Crisp anti-aliased dart silhouette + subtle glow
float d = sdDart(uv - p0, dir, 0.024);
float mask = smoothstep(1.5 * px, -1.5 * px, d);
float glow = exp(-max(d, 0.0) * 85.0) * 0.26;
col += boidTint * glow;
col = mix(col, mix(boidTint, vec3(1.0), 0.45), mask);
}
col *= 1.0 - 0.25 * dot(uv, uv);
fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| boids-live | Tesla T4 1920x1080 | 355× baseline (extreme) · 11.7 ms / 11.8 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| boids-live | NVIDIA L4 1920x1080 | 322× baseline (extreme) · 5.12 ms / 5.27 ms · 2.3× faster on L4 | opengl-timer-query · Cross-architecture validation |
| boids-live | NVIDIA A100-SXM4-40GB 1920x1080 | 247× baseline (extreme) · 7.10 ms / 7.10 ms · 1.6× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
From the citation graph (Crossref reference lists): cited by Physarum agent transport networks.
Crosses
- Brush strokes that flock: A painterly renderer whose strokes swarm, drift and settle like a murmuration while still describing the 3D form. Each stroke particle is a boid tethered to its surface anchor, so one slider moves the look from still painting to living paint.
Sources
- primaryCraig W. Reynolds (1987). Flocks, herds and schools: A distributed behavioral model. ACM SIGGRAPH Computer Graphics.Verified via crossref · 2026-11-29doi:10.1145/37402.37406
- tutorialCraig Reynolds. Boids (Flocks, Herds, and Schools: a Distributed Behavioral Model). red3d.com.Verified via live page · 2026-11-29