Three.js Hydraulic Erosion Terrain Simulator

Particle-based hydraulic erosion in Three.js where hundreds of thousands of raindrops carve gullies and lay down sediment fans on a procedural heightmap, with thermal slumping and visible droplet paths.

Key topics: three.js erosion, hydraulic erosion, terrain generation, heightmap, procedural landscape, thermal erosion

What this code does

- Droplet Model: each drop follows the bilinear height gradient with inertia, gaining speed downhill and losing water to evaporation.
- Sediment Capacity: capacity = −Δh · speed · water · c, so a drop erodes when below capacity and deposits the excess when it slows.
- Erosion Brush: material is removed over a radius-weighted disc, which avoids single-cell pits and keeps channels smooth.
- Thermal Erosion: cells steeper than the talus threshold shed material to their neighbours, softening cliffs.
- Change Map: deposits are tinted sand and eroded gullies darkened, so you can see where material moved.
- Controls: pause, droplets per frame, budget, inertia, capacity, erosion and deposition rates, evaporation, brush radius, thermal erosion, droplet paths, restore and new terrain.

Continue Learning

JavaScript Implementation

ES6+
// One hydraulic-erosion raindrop on a heightmap `map` (size S×S, values 0..1)

function droplet () {
  let x = Math.random() * (S - 2)
  let y = Math.random() * (S - 2)
  let dx = 0
  let dy = 0
  let speed = 1
  let water = 1
  let sediment = 0
  for (let life = 0; life < 30; life++) {
    const i = Math.floor(x) + Math.floor(y) * S
    const h = map[i]
    const gx = map[i + 1] - h
    const gy = map[i + S] - h
    dx = dx * 0.05 - gx * 0.95
    dy = dy * 0.05 - gy * 0.95
    const len = Math.hypot(dx, dy) || 1
    x += dx / len
    y += dy / len
    if (x < 0 || y < 0 || x >= S - 1 || y >= S - 1) return
    const dh = map[Math.floor(x) + Math.floor(y) * S] - h
    const capacity = Math.max(-dh * speed * water * 4, 0.0004)
    if (sediment > capacity || dh > 0) {
      const drop = dh > 0 ? Math.min(dh, sediment) : (sediment - capacity) * 0.3
      sediment -= drop
      map[i] += drop // deposit
    } else {
      const take = Math.min((capacity - sediment) * 0.3, -dh)
      sediment += take
      map[i] -= take // erode
    }
    speed = Math.sqrt(Math.max(0, speed * speed + dh * 4))
    water *= 0.985
  }
}
// Run thousands of droplets per frame, then copy `map` into a PlaneGeometry's heights.