Terrain erosion
Open the live demo · Read the source · View on GitHub
Ground made from a formula looks made from a formula: slopes steeper than
loose earth would hold, and no sign that rain has ever fallen on it.
flutter3d_sim has two kinds of erosion that work on a Heightfield.
erodeThermally lets scree slide until no step is steeper than the ground
would hold. erodeHydraulically drops rain on the field, one droplet at a
time, and lets each drop carry earth downhill. Both take a field and answer
a new one. Neither adds or removes ground: what leaves one sample is set
down on another.
This page draws one hill twice. The left one is as it was made, and the right one has been eroded. Erosion chooses the kind, and Droplets and Seed change the rain.
Step 1: A hill worth eroding #
Thirty-three samples on a side, a metre apart: a cone nine metres high with a ripple across it. The ripple makes small ridges and valleys for the water to find. Each slope drops more than half a metre per cell, which is steeper than the scree will hold.
/// A steep hill with a ridge across it: slopes past what loose ground
/// holds, and a valley for water to find. Heights in metres, a metre a
/// cell.
static Heightfield _hill() {
final heights = Float32List(_side * _side);
for (var z = 0; z < _side; z++) {
for (var x = 0; x < _side; x++) {
final double dx = x - 16.0, dz = z - 16.0;
final double r = math.sqrt(dx * dx + dz * dz);
heights[z * _side + x] =
math.max(0.0, 9.0 - 0.6 * r) +
1.2 * math.sin(x * 0.7) * math.cos(z * 0.5);
}
}
return Heightfield(
columns: _side,
rows: _side,
cellSize: 1.0,
heights: heights,
origin: WorldPosition(-16.0, 0.0, -16.0),
);
}
Step 2: Scree, then rain #
erodeThermally compares every sample with its four neighbours. Where it
stands more than talus metres above one, rate of the excess moves down
to the lower ones, in proportion to how much lower each is, pass after
pass. It is the slow kind: frost splitting a cliff and the pieces settling
at their angle.
erodeHydraulically follows Beyer's droplet model. A drop lands somewhere
the seed chooses and runs downhill, keeping a little of the way it was
going. It picks up earth where it runs fast and steep, sets it down where
it slows or climbs, and dries as it goes. Whatever it still carries when it
dries, or when it reaches the edge, is set down where it is. The same seed
cuts the same gullies. Both runs the scree first, then the rain.
/// The hill after [kind]: scree first, then rain, when it is both. Each
/// answers a new field and leaves the one it was given alone.
Heightfield _erode(Heightfield field) => switch (kind) {
0 => erodeThermally(field, talus: 0.7, passes: 60),
1 => erodeHydraulically(field, seed: seed, droplets: droplets.round()),
_ => erodeHydraulically(
erodeThermally(field, talus: 0.7, passes: 60),
seed: seed,
droplets: droplets.round(),
),
};
Step 3: What to measure #
Three numbers tell whether the ground moved the right way. The total of all samples is the ground's volume. The peak is the highest sample. The foot is everything at least twelve metres from the top, which is the ring the hill's slopes end on.
/// Every sample added up: the ground's volume, in cubic metres here.
static double _total(Heightfield f) =>
f.copyOfSamples().fold(0.0, (double a, double b) => a + b);
/// The volume of the samples at least [from] metres from the hill's top.
static double _foot(Heightfield f, {double from = 12.0}) {
var sum = 0.0;
for (var z = 0; z < f.rows; z++) {
for (var x = 0; x < f.columns; x++) {
final double dx = x - 16.0, dz = z - 16.0;
if (dx * dx + dz * dz >= from * from) sum += f.sample(x, z);
}
}
return sum;
}
/// The highest sample.
static double _peak(Heightfield f) => f.copyOfSamples().reduce(math.max);
Step 4: What has to hold #
For each kind on its own, and for both together: the volume has to stay within one part in ten thousand of what it was, the peak has to be lower, and the foot has to be higher. That is what "moved downhill" means here. Measured with the page's settings, the foot goes from about 247 to 274 cubic metres for scree, 350 for rain and 372 for both, and the volume changes in the seventh significant figure, which is float rounding.
// Each kind on its own, and both: the top is lower, the foot is higher,
// and none of the ground went anywhere.
for (var k = 0; k < 3; k++) {
kind = k;
final Heightfield after = _erode(_before);
final double was = _total(_before);
final double drift = (_total(after) - was).abs();
if (drift > was * 1e-4) {
throw StateError('erosion $k lost ${drift.toStringAsFixed(3)} m³');
}
if (!(_peak(after) < _peak(_before))) {
throw StateError('erosion $k left the top where it was');
}
if (!(_foot(after) > _foot(_before))) {
throw StateError('erosion $k put nothing down at the foot');
}
}
Note. Both kinds work on the whole field at once, on the thread that calls them. On a field like this one that takes a few tens of milliseconds, and the cost of rain grows with the number of droplets. A big field is better eroded off the thread, or once when the level is built, than every time it is opened. The droplets run on the samples. A gully narrower than a cell does not appear, and a drop that reaches the edge of the field sets its load down there, so the rim slowly gains ground.