Three.js Diffusion-Limited Aggregation (Fractal Growth)

3D diffusion-limited aggregation in Three.js where random walkers stick to a growing cluster, producing frost-, coral- and lightning-like fractals from a point, a ground plane, a disc or a line seed.

Key topics: three.js dla, diffusion limited aggregation, fractal growth, random walk, brownian motion, fractal dimension

What this code does

- Random Walkers: particles diffuse from outside the cluster and freeze on contact, so tips grow fastest and screen the interior.
- Adaptive Steps: far from the cluster a walker jumps (distance − 2) at once, which is statistically equivalent and much faster.
- Spatial Hash: contact checks use a hashed grid of 2-unit cells, so each test only looks at the 27 neighbouring cells.
- Stickiness: lowering the sticking probability lets walkers penetrate further and makes the cluster denser and less branchy.
- Fractal Dimension: the HUD estimates D from N ∝ R^D, about 2.5 for 3D DLA and 1.71 in 2D.
- Controls: seed geometry, sticking probability, walk steps per frame, walker count, color mode, walkers on/off, spin and restart.

Continue Learning

JavaScript Implementation

ES6+
// 2D diffusion-limited aggregation on a lattice
const N = 301
const grid = new Uint8Array(N * N)
const c = (N - 1) / 2
grid[c + c * N] = 1
let radius = 2
const cubes = new THREE.InstancedMesh(new THREE.BoxGeometry(1, 1, 1), new THREE.MeshNormalMaterial(), 8000)
cubes.count = 0
scene.add(cubes)

function addWalker () {
  const a = Math.random() * Math.PI * 2
  let x = Math.round(c + Math.cos(a) * (radius + 3))
  let y = Math.round(c + Math.sin(a) * (radius + 3))
  for (;;) {
    const dir = Math.floor(Math.random() * 4)
    x += [1, -1, 0, 0][dir]
    y += [0, 0, 1, -1][dir]
    if (Math.hypot(x - c, y - c) > radius + 20) return // wandered off: drop it
    if (grid[x + 1 + y * N] || grid[x - 1 + y * N] || grid[x + (y + 1) * N] || grid[x + (y - 1) * N]) {
      grid[x + y * N] = 1
      radius = Math.max(radius, Math.hypot(x - c, y - c))
      cubes.setMatrixAt(cubes.count++, new THREE.Matrix4().makeTranslation(x - c, y - c, 0))
      cubes.instanceMatrix.needsUpdate = true
      return
    }
  }
}