# Cellular automata for hydraulic erosion?

This topic is 1702 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I like toying around with World Machine and other procedural terrain generation tools. I'm also trying to learn more about general-purpose GPU programming, so I thought I would see if I could recreate one of my favorite parts of World Machine on the GPU: hydraulic erosion.

World Machine's hydraulic erosion function simulates the effect of precipitation on a heightmap, and has four outputs: an eroded heightmap, a water-flow map, a soil-deposition map, and a wear map (that shows where the erosion occurred). It does a pretty good job, and is relatively fast. The starter map you get on start up can be eroded with default settings in ~10 seconds.

I intend to replicate this basic functionality, but with an increase in speed. This is not meant to be boasting or impressive, just a consequence of using the GPU on a parallelizable problem. However I would like to improve upon it with a new feature: meanders and oxbow lakes. http://en.wikipedia.org/wiki/Meander Meanders are when rivers curve significantly, due to increased water flow and erosive force on the exterior of a bend in the river. Excessive curves may cause the river to erode an extra connection to itself, cutting off part of the river from a water source. This results in horse-shoe shaped lakes next to rivers (which may or may not dry up).

I would like to see if I can get this to happen as an emergent behavior from a properly constructed cellular automata. In order to see this sort of behavior I would need a model that allowed (1) water flowing next to a cell (which is higher than the water) to cause erosion in that cell, (2) water to flow faster on the outside of a bend, (3) faster moving water to be more erosive, (4) slower water to deposit more soil. I'm not entirely sure how to achieve that. Locally computing the gradient of water altitudes to determine flow rate each time step is the simplest method, but it also fails to achieve #2, which is an important property.

Does anyone have any ideas? I feel like I am too close to the problem and am overlooking something simple.

PS: I understand that what I am talking about is, strictly speaking, a finite element method instead of a proper CA. However, I have repeatedly seen this referred to as a CA elsewhere, so I do so here as well.

##### Share on other sites
The four items you mention each require communication from cell to cell. That in turn requires communication through shared memory or mutlipass processing, both are difficult under Cuda / OpenCL.

Shared memory is fast and can be accessed between some nodes but not between other nodes. Global memory is slow. There are rules about coalescing data that must be followed.

If instead you write all the communication to buffers, consolidate them after main processing, and then run another pass, you will quickly become bandwidth limited on all but the most trivial of world sizes.

There are definitely some hurdles to overcome. These are patterns known to be difficult on GPU processing. The hardware was designed for something different. The farther you deviate from single-pass algorithms that read much data and output little data, the less likely you will be to see positive results.

##### Share on other sites

I should have been more specific! Yes I know about the memory architecture and how that causes this problem to be just "parallelizable" instead of "embarrassingly parallel".

http://www.olcf.ornl.gov/tutorials/cuda-game-of-life/

I was intended to use the second method shown there (the one using __syncthreads() & shared memory) as a rough template, and then maybe play around with texture memory or OpenCL later.

What I need help on was the CA design, not the programming.

I frankly don't have the expertise to recreate that webGL interface. I also wonder how quickly it actually simulates erosion. Just waiting for precipitation causes very slow erosion, while adding water quickly saturates the landscape and results in smooth landscapes.

Compare that to the images at the above page. The first one has a fairly obvious straight channel running diagonally from top left to bottom right. These result when your initial heightmap is smooth in that area. This means that taking a hand-made heightmap with a rough outline of your terrain and then eroding it can cause nasty artifacts. I consider that a nasty limitation of the extant techniques, as it removes much of the benefit of procedural generation.

While the webGL implementation and the second image in the WM link both have curvy river-like flow patterns, they are curvy because they simply followed the gradient of the input heightmap. They weren't caused by any interesting fluid/erosion dynamics. That is what I'd like to change, and that involves crafting the cells' update rules appropriately.

This involves simulating the flow gradient of the water in a persistent, non-stochastic way. What I am really asking for direction on is how to do that.

PS: While the online demo is spiffy, non-real-time techniques are still useful. Because the erosion step dominates most terrain generation pipelines, if you can significantly reduce its duration then you open yourself to being able to completely procedurally generate large numbers of terrains. If you have some evaluation metric to compare them against, then you can select the best N of them to show to the artist, who can further tweak their parameters. These tweaked parameters can then be what is sent to the player, who generates the terrain on demand. This means that the amount of data which you ship to your end user to handle terrain is orders of magnitude smaller than it used to be, but still looks high quality because it has been hand-tweaked by an artist.