Hydraulic and thermal erosion implement in Unity using compute shaders.
This is an example implementation of hydraulic and thermal erosion with shallow water equations. My initial motivation was to implement a game mechanic like in the From Dust game.
This project is still in progress. Hydraulic erosion requires a bit more parameter tweaking and revisiting the actual implementation. But overall it works.
Main Scene and use the mouse to draw modify terrain/water.
How it works
Explanation is still in progress…
Data / notation
We first need to list all the data we will operate with during the simulation. Since we are using a grid-based simulation we will need some per-cell information. Assuming we have a grid of size and current cell coordinates are .
- Simulation parameters
- - grid width
- - grid height
- - simulation time delta
- - gravity
- - cell size along axis
- - cell size along axis
- - pipe length
- - pip cross-section area
- - evaporation rate
- - rain rate
- Hydraulic erosion parameters
- - sediment capacity
- - soil suspension rate
- - soil deposition rate
- - erosion depth limit
- Thermal erosion parameters
- - thermal erosion rate
- - talus angle scale
- - talus angle bias
- - height of terrain in cell , must be positive
- - height of water in cell , must be positive
- - suspended sediment amount in cell
- - terrain hardness in cell
- , , , - water flow (flux) in each direction in cell
- , , , - terrain mass flow (flux) in each direction in cell
- - water velocity in direction in cell
- - water velocity in direction in cell
Step 1. Water sources
We need to increase the water amount from water sources (e.g. rain). Right now the brush-drawing is omitted for clarity, but it happens in this step.
Step 2. Water flow computation
Water flow is proportional to the height difference in each cell because of pressure emerging from the height of water. Thus, to compute flow in each direction of cell we need to calculate the height differences:
And repeat for other directions. Then we need to compute the outgoing amount of water from the current cell to the neighbors (in each direction) which is proportional to the volume diefference - height difference multiplied by cell area ():
and also repeat the computation for each direction. - is a special scaling factor to prevent situations when total outflow in 4 directions is higher than water volume in cell (since each direction is computed independently). Thus is defined to split the scale the outflow accordingly - cell water volume divided by total outflow per single step:
If outflow will exceed the total volume, the fraction will be less than 1 thus flow will be reduced.
If we need the water to bounce off the walls we need to disable outgouing flow at the boundaries:
At the end of this step, we got all the outgouing flow computed at each cell so we now need to use this flow information to adjust the height of the water.
Step 3. Appling the water flow
The water height in each should increase by the total amount of incoming flow and decrease by the amount of total outgoing flow.
outgoing flow is the same as at the previous step.
incoming flow is the outgoing flow from neighbor cells in opposite directions.
The total volume change of the column is:
Finally apply the volume change to the water column, since we store the height and not the volume we need to divide by cell area:
And that’s it for water flow. It is also called shallow water equations using pipe model. At the end of this step, the water can slide down the terrain, create vertical waves and so on. But to apply erosion we will need to do more stuff.
Step 3.5. Compute water velocity
In further computation we will need the information about the water velocity in each cell. We can compute it using information about the water flow:
the velocity in each axis is the average total flow in each pipe along that axis. So for axis we have 2 neighbor cells: and , thus we can compute the total flow for each neighbor and average across neighbors.
Note: This is only partially physically accurate. For the true velocity - this amount should be scaled by something (include pipe area and length).
Step 4. Hydraulic erosion and deposition
While water flows over terrain it takes (erodes) and transports some amount of soil. After a while, some suspended sediment will be deposited to the ground. This process is mostly defined by the sediment transport capacity of the water flow. There are many complex models regarding these processes, but we will use the simple empirical equation:
Step 5. Thermal Erosion
Shaders/Erosion.compute- all computational stuff happening there in form of separate compute kernels (functions) acting like passes and responsible for different things. Look through that file if you are interested in the actual algorithm implementation.
Shaders/Water.shader- Surface shader for rendering water plane. In vertex shader vertex positions are updated from state texture and normals are computed. It has basic lighting and alpha decay depending on depth.
Shaders/Surface.shader- A lit shader to render the terrain surface. In vertex shader vertex positions are updated from state texture and normals are computed.
Shaders/InitHeightmap.shader- A special shader to initialize initial state from common grayscale heightmap texture. Since state texture is a float texture and operates with values higher than 1 the original heightmap texture should be scaled. This shader is used in the special material used in
Scripts/CustomTerrain.cs- main Monobehavior responsible for mesh creation, compute shader setup, dispatching computation to the GPU, texture creation and parameter sharing.
- Mei, Xing, Philippe Decaudin, and Bao-Gang Hu. “Fast hydraulic erosion simulation and visualization on GPU.” 15th Pacific Conference on Computer Graphics and Applications (PG’07). IEEE, 2007. http://www.nlpr.ia.ac.cn/2007papers/gjhy/gh116.pdf
- Jákó, Balázs, and Balázs Tóth. “Fast Hydraulic and Thermal Erosion on GPU.” Eurographics (Short Papers). 2011. http://old.cescg.org/CESCG-2011/papers/TUBudapest-Jako-Balazs.pdf
Warning: poor paper quality along with math mistakes. But has nice ideas.
- Št’ava, Ondřej, et al. “Interactive terrain modeling using hydraulic erosion.” Proceedings of the 2008 ACM SIGGRAPH/Eurographics Symposium on Computer Animation. Eurographics Association, 2008. http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.173.5239&rep=rep1&type=pdf
- Better explanation of the implementation
- More descriptive comments in code
- Quality of life:
- Better editor - camera controls and better brush controls
- Different initial state loaders (from terrain data, from 16bit textures, from
- Terrain chunks to simplify rendering of distant terrain parts