|
HighMap library (C++)
|
This document details the simulation model, kinematics, sediment transport equations, and implementation of particle-based hydraulic erosion (hydraulic_particle) in HighMap.
Hydraulic erosion models the carving of terrain by surface water runoff. In HighMap, particle-based hydraulic erosion uses a Lagrangian formulation where virtual water droplets (particles) are spawned across the heightfield, navigate downslope according to local terrain kinematics, transport suspended sediment, and iteratively carve channels and deposit alluvial fans.

Each particle is defined by a continuous 2D position $\mathbf{x} = (x, y) \in [0, N_x - 1] \times [0, N_y - 1]$. The integer grid coordinates $(i, j)$ and fractional offsets $(u, v)$ are given by:
$$i = \lfloor x \rfloor, \quad j = \lfloor y \rfloor, \quad u = x - i, \quad v = y - j$$
Elevation $z(\mathbf{x})$ is obtained through bilinear interpolation over the four enclosing heightfield vertices:
$$z(u, v) = (1 - u)(1 - v) z_{i, j} + u (1 - v) z_{i+1, j} + (1 - u) v z_{i, j+1} + u v z_{i+1, j+1}$$
The continuous terrain surface gradient $\nabla z = (g_x, g_y)$ is computed analytically across the cell:
$$g_x = (1 - v)(z_{i+1, j} - z_{i, j}) + v(z_{i+1, j+1} - z_{i, j+1})$$
$$g_y = (1 - u)(z_{i, j+1} - z_{i, j}) + u(z_{i+1, j+1} - z_{i+1, j})$$
The steepest downhill unit vector is $-\frac{\nabla z}{|\nabla z|}$. To simulate fluid momentum and prevent abrupt artificial turning, the particle's heading direction $\mathbf{d}_t$ blends the downhill gradient vector with its previous momentum direction $\mathbf{d}_{t-1}$ via inertia coefficient $c_{\text{inertia}} \in [0, 1]$:
$$\mathbf{d}_t = \mathrm{normalize}\left((1 - c_{\text{inertia}}) \left(-\frac{\nabla z}{|\nabla z|}\right) + c_{\text{inertia}} \, \mathbf{d}_{t-1}\right)$$
Across flat plateaus or local minima where $|\nabla z| \le \epsilon_{\text{slope}}$, momentum dominates ($c_{\text{eff}} = \max(c_{\text{inertia}}, 0.9)$), enabling particles to glide over shallow sills rather than stalling immediately.
Position integration advances the particle along its current heading:
$$\mathbf{x}_{t+1} = \mathbf{x}_t + \Delta t \, \mathbf{d}_t$$
As a droplet moves from elevation $z_{\text{prev}}$ to $z_{\text{curr}}$, the elevation difference $\Delta z = z_{\text{prev}} - z_{\text{curr}}$ drives acceleration or deceleration under gravity coefficient $g$:
$$v_{t+1} = \sqrt{\max\left(0, v_t^2 - \Delta z \cdot g_{\text{eff}}\right)}$$
For downhill movement ($\Delta z > 0$), kinetic energy increases. For uphill climbs ($\Delta z < 0$, e.g., inertia-driven movement across a local dip), gravitational deceleration applies. Kinetic friction dampens velocity each step:
$$v_{t+1} \leftarrow v_{t+1} \cdot (1 - \mu_{\text{drag}})$$
Volume decreases due to evaporation:
$$V_{t+1} = V_t \cdot (1 - \mu_{\text{evap}})$$
At each step, the droplet carries an internal sediment load $s$. The maximum sediment capacity $s_c$ is governed by the stream power equation:
$$s_c = \max\left(0, c_{\text{capacity}} \cdot V \cdot v \cdot \Delta z\right)$$
The transport imbalance $\Delta s_c = \Delta t (s_c - s)$ determines whether erosion or deposition occurs.
When the particle slows down, reaches flatter slopes, or moves uphill ($\Delta s_c < 0$ or $\Delta z < 0$), it drops sediment:
$$\Delta s_{\text{drop}} = c_{\text{deposition}} \min(s, \text{to_deposit})$$
$$s \leftarrow s - \Delta s_{\text{drop}}$$
The dropped sediment is distributed across the grid cell using bilinear weights:
$$w_{00} = (1 - u)(1 - v), \quad w_{10} = u(1 - v), \quad w_{01} = (1 - u)v, \quad w_{11} = uv$$
$$z_{i + \delta_i, j + \delta_j} \leftarrow z_{i + \delta_i, j + \delta_j} + w_{\delta_i, \delta_j} \Delta s_{\text{drop}}$$
When the flow is under-saturated ($\Delta s_c > 0$ and $\Delta z > 0$), material is carved from the surface:
$$\Delta s_{\text{lift}} = c_{\text{erosion}} \min(\Delta s_c, \Delta z)$$
$$s \leftarrow s + \Delta s_{\text{lift}}$$
Material is subtracted bilinearly from the surrounding vertices. When an optional bedrock map $z_{\text{bedrock}}$ is provided:
$$z_{i, j} \leftarrow \max(z_{\text{bedrock}, i, j}, z_{i, j})$$
Localized incision leaves steep vertical trench walls. To simulate natural valley widening and talus formation, bank collapse relaxes over-steepened slopes adjacent to carved cells:
For each of the 8 neighbors $k \in [0, 7]$ of cell $(i, j)$:
$$\text{slope}_k = \frac{z_k - z_c}{\text{dist}_k}$$
If $\text{slope}_k > \sigma_{\text{talus}}$, material collapses into the channel:
$$\Delta z_{\text{collapse}} = (\text{slope}_k - \sigma_{\text{talus}}) \cdot r_{\text{collapse}}$$
$$\Delta z_{\text{collapse}} \leftarrow \min\left(\Delta z_{\text{collapse}}, \max(0, z_k - z_{\text{bedrock}, k})\right)$$
$$z_k \leftarrow z_k - \frac{1}{2} \Delta z_{\text{collapse}}, \quad z_c \leftarrow z_c + \frac{1}{2} \Delta z_{\text{collapse}}$$
Running all droplets in a single parallel OpenCL dispatch causes particles to ignore concurrent modifications made by peers. HighMap splits the particle budget into sequential iterations:
Sequential passes allow tributaries to naturally converge into coherent dendritic river systems.
To form continental-scale valleys without millions of high-resolution particle steps, hydraulic_particle_multiscale downsamples the terrain into a multi-resolution pyramid:
The primary interface resides in hydraulic_erosion.hpp: