Skip to content
Benjamín Díaz
Back to projects

Boids & Reaction-Diffusion in Unity

Experiment

My experiments with flocking and reaction-diffusion using Unity compute shaders, with predators, prey, and competing patterns.

Benjamín Díaz · Published ·

  • Unity
  • C#
  • Compute Shaders
  • Simulation
  • Creative Coding
  • HLSL
Boids & Reaction-Diffusion in UnityExperiments

Shaders were one of the parts of Unity I had the hardest time understanding. I'd used the engine for years, including for Oso Sigiloso, but hadn't spent much time working directly with the GPU.

Videos by Sebastian Lague, Richard Lord, and Arsiliath made me want to try. I spent two weekends building boids and reaction-diffusion experiments with compute shaders.

Boids

Craig Reynolds' boids model gives each agent three rules: avoid crowding its neighbors, match their direction, and stay near the group. Flocking emerges from those local interactions.

I shared this short view of the Unity experiment in January 2026. The on-screen text introduces flocking and emergent behavior.

Boids in Unity47 seconds · Spanish screen text · English captions available
Drawing diagram…
Diagram source
flowchart LR
  N[For each neighbor] --> S{Too close?}
  S -- Yes --> A1[Steer away]
  S -- No --> A2[Accumulate direction]
  A2 --> A3[Accumulate position]
  A1 --> R[Adjust velocity]
  A3 --> R
  R --> N

Separation, alignment, cohesion. Everything else (the swirling, the splitting, the organic shapes) is emergent.

Boids simulation

My version runs hundreds of thousands of agents. About 1% are predators (red/orange), the rest prey (cyan). Prey follow the three classic rules plus one: see a predator, run. Predators don't flock. They chase.

Boids simulation

The prey gather into dense groups. When a predator crosses one, the group splits and reforms behind it. I didn't program a separate rule for splitting and regrouping; it follows from the local rules.

Boids simulation

To reduce neighbor-search work, a spatial hash grid divides 3D space into boxes. Each agent only checks its box and the 26 neighbors (3x3x3), avoiding an all-pairs search. The gain depends on how densely the agents occupy those boxes. Rendering uses GPU instancing (one draw call), with the vertex shader stretching meshes by velocity for a bioluminescent streak effect.

Boids simulation

Reaction-Diffusion

Reaction-diffusion systems combine local reactions with substances spreading through space. They can form spots, stripes, and branching shapes. I used the Gray-Scott model for this experiment.

This excerpt from the original Unity recording shows the evolving patterns and colored regions across the full scene. The browser version is linked below.

Reaction-diffusion experiments12 seconds · original Unity recording · no audio
Reaction-diffusion simulation

The Gray-Scott model: two chemicals, A and B, on a 2D grid. A spreads fast, B spreads slow. Where they meet, A gets eaten and B grows. A is replenished (feed rate f), B decays (kill rate k). Two equations, every pattern below comes from them:

dA/dt = Da*nabla^2*A - AB^2 + f(1-A)
dB/dt = Db*nabla^2*B + AB^2 - (k+f)B

Change f and k and you get spots that divide like cells, fingerprint stripes, spirals, pulsing blobs, coral branches. Same equation, different knobs.

I added three competing species, each with its own inhibitor, all feeding on the same A, suppressing each other on contact. The species form separate regions, meet at their borders, and sometimes replace one another.

Reaction-diffusion simulation

I added anisotropic diffusion weighted by a rotating noise field so patterns flow like currents, Perlin noise modulating feed/kill rates across the grid, and fake bump-mapped lighting that makes the flat 2D simulation look three-dimensional.

Reaction-diffusion simulation
Reaction-diffusion simulation

Trying it in the browser

I liked being able to change a few local rules and watch how the simulation responded.

I ended up building a browser version of these algorithms so you can play with them yourself.

Read the boids screen text

Today I'm experimenting with boids, an artificial-life simulation that imitates flocks of birds. It's a perfect example of emergent behavior. A meeting point for art, mathematics and biology. I made these using compute shaders in Unity.