Rivers that cannot run uphill: procedural rivers, lakes and waterfalls in plain WebGL2
Procedural rivers usually fail in the same three ways. The river sits on the ground like a blue ribbon. It climbs a hill somewhere along the way. Or it runs straight like a canal.
I hit all three while adding rivers to Wallhammer Web, a 3D engine for the browser where the whole world is written in code: plain WebGL2, no graphics library, no model files, no textures, no images. So I threw the first attempt away and started again from one rule.
Everything in the pictures comes from a seed: the island, the lakes, the rivers, the waterfalls, the woods. The same seed always gives the same island.
The rule: water is never drawn onto the ground
The sea is easy. It has a level, and it is wherever the ground lies below that level. Nobody models the shoreline of a sea; the ground makes it. The rule of the example is that rivers and lakes work the same way:
- A plan gives a level of water along every river and for every lake.
- The ground is only ever cut down to that plan. It is never raised.
- Water is wherever the ground lies under the level that may stand there.
That is all. A river cannot stand above its banks, because the banks are the ground and the water is only where the ground is lower. It cannot run uphill, because its level only ever falls from spring to mouth. And every shore is the real contour of the ground, not the edge of a mesh strip.
The plan
The plan is a pure function of the seed, computed once, on a coarse grid of 20 m:
- Fill the hollows from the sea inward (a priority flood), so every node drains to a neighbour. A filled hollow that is large and deep enough becomes a lake; smaller ones are cut through.
- Sum the flow downstream. Nodes with enough basin behind them form lines, chains from a spring to a mouth. A tributary ends on its parent.
- Find the middle of the valley. The grid only says roughly where the valley is, so the chain is smoothed and slid sideways into the lowest ground, measured on the real ground.
- Give each line a level, station by station (every 6 m): the lowest natural ground across the whole valley floor, minus a margin, made to fall monotonically, to stand at the level of every lake it runs through and to arrive at the level of what it joins.
The profile of one river shows the rule at work. The grey line is the natural ground, the blue one the water's level, the brown one the bed. The blue line never rises, and on land it never lies above the grey one; at the right end the river has reached the sea.
Meanders: a sine along the river's own length
A river on level ground does not wobble at random. Its bends have a shape, and that shape is well described by letting the heading of the channel swing as a sine of the distance travelled along it:
// Walk the channel in small steps; the heading swings as a sine along its own length.
phase += (2 * Math.PI * STEP) / bendLength;
const heading = swing * Math.sin(phase);
With a small swing you get gentle curves; with a large one the loops nearly close on themselves, the way real meanders do.
In the example the swing depends on the ground:
- on a steep stretch it is zero, so a mountain stream runs straight;
- on level ground it grows, but only as far as the level ground beside the line allows, so a bend never cuts into a hillside;
- it dies out toward lakes, junctions and the sea, so the river still meets what it joins.
The length of one bend follows the width of the channel, so a small creek makes small bends and the main river makes large ones.
Waterfalls: stairs cut into the slope
Water may run down a slope only so steeply. Where the level would fall by more than 25 %, the stretch becomes stairs: treads cut into the slope, each as low as its own lower end, each beginning under a fall. A tread ends where the ground itself drops most, so the falls sit on the natural steps of the terrain. Under a fall the pool is made wider and deeper.
Nothing special draws a waterfall. The water is one sheet over the same grid as the ground, and at a fall that sheet simply hangs from the upper level to the lower one. The shader sees the slope of the sheet itself and draws a veil of falling streaks where it hangs, foam where the water runs steeply, and a boil at the foot. The mist is a handful of looping particles.
One sheet, one shader, one draw
The sea, the lakes and the rivers are one surface, so one material draws all of them in a single blended pass:
- the water's own colour by the depth down to the bed, so a shallow shows its bed and a deep is dark;
- the mirror of the sky by Fresnel, and a glint of the sun, on small waves;
- waves that are carried along the stream where the water runs (two layers of the same waves, each carried for a few seconds and faded out before it is set back, so nothing stretches and nothing jumps);
- white water from the slope of the sheet;
- a breathing line of foam where the sea meets its shore.
The bank is not a mesh either. Every vertex of the ground carries its height over the water that may stand there, and the ground's shader draws wet gravel, sand and silt per pixel from that number. The bank's edge is therefore a contour of the ground and not the saw of the vertex grid.
What it costs
- Building the island (3.2 km, a grid of 1281 × 1281 points every 2.5 m) takes about 2 seconds on the main thread of an Apple M4 Pro. It runs in slices of 40 ms behind the engine's progress bar, so the page stays responsive.
- A frame at 1600 × 900 on the same machine costs 1 to 4.5 ms in the close views and about 12 ms for the whole island at once. The example has no levels of detail: it draws the full grid, about 3.3 million ground triangles, and the woods on top.
- The checks run over eight seeds: 16 to 22 rivers and 8 to 22 falls per island, the highest fall 38 m, and 0 stretches where a level rises.
What is still weak
I would rather say it than have you find it:
- Where a tributary joins a larger river on very flat ground, the larger river can back up into the tributary's valley and leave a film of water a few centimetres deep in places (0 to 48 points of about 30 000 wet points per island).
- A short inlet of a lake can still be fairly straight.
- The trees are a few triangles each. They are there so the island is not bare, not as vegetation worth an article.
Try it
- Source of the example:
examples/riversin the engine repository. The plan is one file,src/plan.ts. - The engine: Wallhammer Web on GitHub,
npm install wallhammer-web, and all live demos: a planet you can fly down to, a village with villagers, destruction, an endless streamed world. All of it from code and a seed, on plain WebGL2, MIT licensed.
If you build something with it, or you know a better way to make a river behave, I would like to hear about it.
Comments
No comments yet. Start the discussion.