CodeRigor

LESSON

Hydraulic erosion

Roll droplets over a height map: sample it bilinearly, spread erosion with a brush, carry and deposit sediment, and prove the total height is conserved.

Noise terrain looks like soft blobs: bumps with no valleys that join up, no ridges, no fans of soil where a slope flattens. Real land is shaped by water, which picks material up where it runs fast and drops it where it slows. This lesson builds the smallest model of that, one raindrop at a time. It pairs with Coding Adventure: Hydraulic Erosion and Coding Adventure: Compute Shaders by Sebastian Lague, linked under Watch & read; the second covers compute shaders and has a chapter on erosion simulation issues. You build five NumPy functions: smooth sampling of a height map, a bilinear deposit, an erosion brush, one droplet, and a loop of droplets, and you prove that no height is created or lost.

A droplet lives at a fractional position, so the map has to be read between its nodes. With the position at column x and row y, take the four surrounding cells, let u and v be the fractions of the way across and down, and blend: height = (1 - u)(1 - v)·a + u(1 - v)·b + (1 - u)v·c + uv·d, where a b are the top corners and c d the bottom ones. The slope comes from the same four numbers: gx = (b - a)(1 - v) + (d - c)v and gy = (c - a)(1 - u) + (d - b)u. Depositing uses the same four weights in the other direction, adding an amount to the corners in those shares.

Python
import numpy as np

h = np.array([[0.0, 1.0], [2.0, 5.0]])
u, v = 0.25, 0.5                      # a quarter across, halfway down
a, b, c, d = h[0, 0], h[0, 1], h[1, 0], h[1, 1]
print(a * (1 - u) * (1 - v) + b * u * (1 - v) + c * (1 - u) * v + d * u * v)
print((b - a) * (1 - v) + (d - c) * v, (c - a) * (1 - u) + (d - b) * u)
print((1 - u) * (1 - v), u * (1 - v), (1 - u) * v, u * v)
One deposit lands on four cells
Depositing 1.0 at column 1.25, row 2.5 of a 4 × 4 map. The right-hand column gets u = 0.25 of each row's share, the lower row gets v = 0.5, and the four shares add to 1.
One deposit lands on four cells
Depositing 1.0 at column 1.25, row 2.5 of a 4 × 4 map. The right-hand column gets u = 0.25 of each row's share, the lower row gets v = 0.5, and the four shares add to 1.

Each step of a droplet follows the same plan. Blend the old direction with the downhill direction: direction = direction · inertia - slope · (1 - inertia), then scale it to length 1 and move one cell. If the slope is zero and there is no old direction, the length is 0: stop, because there is nothing to divide by. Let dh be the height at the new position minus the height here. The droplet carries a load, and its capacity is max(-dh · speed · water · capacity, minimum). Steep and fast gives a large capacity; flat or uphill gives only the small minimum.

When the load is above the capacity, or the droplet is climbing, it drops some load at its current position. Otherwise it erodes: it takes min((capacity - load) · erode rate, -dh) from the ground and adds it to its load. The second term of that min is the safety rule: a droplet never digs more than the drop to the place it is moving to, so it cannot carve a pit below its own next step. Taking the amount from one cell would leave spikes, so it is spread over a brush: every cell within a radius, weighted by radius minus distance, with the weights scaled to add to 1 after the brush is cut off at the map edge.

Python
def step(dh, load, speed=1.0, water=1.0):
    cap = max(-dh * speed * water * 4.0, 0.01)
    if dh > 0 or load > cap:
        return "deposit", round(min(dh, load) if dh > 0 else (load - cap) * 0.3, 4)
    return "erode", round(min((cap - load) * 0.3, -dh), 4)

print(step(-1.0, 0.0))     # steep: the wish is 1.2 but the drop is only 1
print(step(-0.1, 0.0))     # gentle: capacity 0.4, wish 0.12, drop 0.1
print(step(-0.1, 0.5))     # over capacity: drops 30 percent of the excess
print(step(0.2, 0.5))      # climbing: fills the pit by the rise
One droplet: it picks up load on the slope and drops it on the flat
A droplet starts on a ramp that falls 0.8 per column and ends in a flat basin at height 2. The load is the droplet's carried amount after each step, measured by running the real droplet for 0 to 16 steps.
One droplet: it picks up load on the slope and drops it on the flat
A droplet starts on a ramp that falls 0.8 per column and ends in a flat basin at height 2. The load is the droplet's carried amount after each step, measured by running the real droplet for 0 to 16 steps.

Mass balance is the property to hold on to. Erosion moves exactly the amount it takes into the load; deposit hands out exactly the amount it removes from it. So whatever a droplet does, the map's total height falls by exactly the load it still carries when it stops. That is a number you can test to 1e-9.

Mistakes people make. Using dh instead of -dh in the capacity: downhill then gives a negative number, the floor wins, and the droplet barely erodes where it should erode most, while climbing is given a large capacity. Mixing up x and y in the deposit shares puts the soil in the wrong corner; a test at a point where u equals v cannot see it, because the two weights are the same. Forgetting to normalise the brush after clipping makes edge cells lose or gain mass. Dropping the min with the drop lets one step cut a hole below the next position.

How is it checked? The height is compared with PyTorch's bilinear sampler and the slope with a measured slope. The deposit is checked by its centre of mass, which must sit at the point. The brush is compared with a brute-force loop at every centre. Mass balance runs over a hundred random droplets. These checks show that the code does what this page says. They do not show that the terrain looks like real erosion: the parameters are tuned by eye, and a model with the right mass balance can still carve ugly valleys.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →