---
id: "shallow-water-equations"
title: "Shallow Water Equations (Height-field Water)"
aliases: [SWE, height-field water, virtual pipes]
domain: "volumes"
stage: "volume"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, cuda, cpu]
year_introduced: 1990
inputs: [heightfield]
outputs: [heightfield, normals]
style: "both"
dynamic: "dynamic"
cost: "cheap"
authority: "Kass & Miller, 1990"
url: "https://texelatlas.vercel.app/t/shallow-water-equations/"
sources_verified_at: "2026-11-29"
---
# Shallow Water Equations (Height-field Water) (Kass & Miller, 1990)

> Simulate water as a 2D height field plus a velocity field, so waves, ripples and shoreline flow update on a grid with one cheap pass per step.

## Reach for it when

- interactive ripples when objects touch water
- pool or river flow that reacts to the player
- flood or shoreline movement on a grid
- cheap water simulation for mobile or webgl
- splashes and waves on a height field

## How it works

The 3D flow is vertically averaged, which leaves a water height h and horizontal velocity per grid cell. Momentum is changed by the gradient of the water surface plus terrain height (gravity pushes water downhill) and mass moves as the divergence of the flux. A simple form stores a height and a vertical velocity per cell and sets acceleration proportional to the average of the four neighbour heights minus the cell height, then damps; a better form uses per-edge flux (virtual pipes) to stay mass-conserving over a terrain. Each step is explicit and needs a time step below the wave speed over cell size (CFL). Interaction is easy: add a bump to h for a drop or a moving object. The height field becomes a displaced grid with normals from central differences, and the same grid can store foam and flow for shading.

## When to use it

Choose it for interactive ripples, splashes, pools and river or flood flow where the surface is single-valued and interaction by objects is required, at a cost that fits a mobile or WebGL budget.

Prefer [FFT Ocean (Tessendorf)](https://texelatlas.vercel.app/t/fft-ocean.md) for an open-sea surface, and [Gerstner (Trochoidal) Waves](https://texelatlas.vercel.app/t/gerstner-waves.md) for analytic swells. Prefer a particle or grid-based 3D fluid solver when you need breaking waves, overturning or splashes that leave the surface. Use [Real-time Water Caustics](https://texelatlas.vercel.app/t/water-caustics-realtime.md) to light the floor under it.

## Cost

cheap — One or two grid passes per step; resolution and number of substeps are the cost control.
Measured GPU Weight: 35.07× baseline (heavy) — 1.1539 ms median / 1.191 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 33.77× baseline (heavy) — 0.5369 ms median (2.15× faster on L4).
Ampere A100 scaling: 24.74× baseline (heavy) — 0.7124 ms median (1.62× faster on A100).

## Failure modes

- Instability or explosion when the time step violates the CFL condition
- Energy and mass drift with the simple 4-neighbour wave equation, giving ever-growing or dying ripples
- Cannot represent overturning waves, splashes or vertical surfaces because the surface is a single-valued height
- Reflection from the grid boundary unless an absorbing sponge layer is added
- Shoreline wet/dry cells produce negative depths and flickering without careful handling

## Engine support

- unreal: official — `Enable Niagara Fluids plugin; Niagara system from the Shallow Water template` (evidence: https://dev.epicgames.com/documentation/en-us/unreal-engine/niagara-fluids-reference-in-unreal-engine, checked 2026-12-01)

## Dataflow

- Inputs: heightfield
- Outputs: heightfield, normals
- Platforms: webgl2, webgpu, desktop-gpu, cuda, cpu

## Presets

### Shallow water equations — interactive rippling wave interference & pool caustics (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_WAVE_AMP": 1,
  "P_DAMPING": 0.55,
  "P_CAUSTICS": 1.15,
  "P_SPEED": 1
}
```

```glsl
const float P_WAVE_AMP = 1.0; // P Wave Height [0.2, 2.2, 0.05]
const float P_DAMPING = 0.55; // P Spatial Damping [0.1, 1.4, 0.04]
const float P_CAUSTICS = 1.15; // P Floor Caustics [0.0, 2.2, 0.05]
const float P_SPEED = 1.0; // P Wave Speed c [0.0, 2.5, 0.05]

// Evaluate shallow-water wave height h(x,z,t) solving d2h/dt2 = c^2 * laplacian(h) - damp * dh/dt
// Superposition of periodic droplet impulses, orbiting wake source, and basin-wall boundary reflections
float wavePacket(vec2 p, vec2 src, float t, float k, float c, float damp) {
  float r = length(p - src);
  float phase = k * (r - c * t);
  float env = exp(-r * damp * 1.15) / (0.35 + r * 1.1);
  return sin(phase) * env;
}

float evalHeight(vec2 p, float t, float amp, float damp, float c) {
  float h = 0.0;
  // 4 staggered droplet impulse centers with shallow-water phase speed c = sqrt(g * H)
  vec2 s0 = vec2(-0.52, -0.26);
  vec2 s1 = vec2(0.56, 0.34);
  vec2 s2 = vec2(0.18 * cos(t * 0.85), 0.32 * sin(t * 0.65)); // moving wake emitter
  vec2 s3 = vec2(-0.28, 0.44);

  h += 0.34 * wavePacket(p, s0, t, 14.0, c * 0.42, damp);
  h += 0.30 * wavePacket(p, s1, t + 1.3, 16.5, c * 0.46, damp);
  h += 0.38 * wavePacket(p, s2, t * 1.15, 19.0, c * 0.38, damp * 1.1);
  h += 0.24 * wavePacket(p, s3, t + 2.7, 12.5, c * 0.44, damp);

  // Mirror-image boundary reflections from square basin walls at |x|=1.4, |z|=1.4
  h += 0.16 * wavePacket(p, vec2(2.8 - s1.x, s1.y), t + 1.3, 16.5, c * 0.46, damp);
  h += 0.16 * wavePacket(p, vec2(-2.8 - s0.x, s0.y), t, 14.0, c * 0.42, damp);

  // Circular diffraction scatter off submerged limestone pillar at (-0.12, -0.05)
  vec2 pil = vec2(-0.12, -0.05);
  float rp = length(p - pil);
  h += 0.22 * sin(18.0 * (rp - c * 0.4 * t)) * exp(-abs(rp - 0.24) * 3.8);

  return h * amp * 0.11;
}

vec3 evalNormal(vec2 p, float t, float amp, float damp, float c) {
  float e = 0.012;
  float hx = evalHeight(p + vec2(e, 0.0), t, amp, damp, c) - evalHeight(p - vec2(e, 0.0), t, amp, damp, c);
  float hz = evalHeight(p + vec2(0.0, e), t, amp, damp, c) - evalHeight(p - vec2(0.0, e), t, amp, damp, c);
  return normalize(vec3(-hx, 2.0 * e, -hz));
}

vec3 aces(vec3 x) {
  x = max(x, 0.0);
  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;

  float amp = clamp(P_WAVE_AMP, 0.1, 2.5);
  float damp = clamp(P_DAMPING, 0.08, 1.6);
  float caustGain = clamp(P_CAUSTICS, 0.0, 2.4);
  float spd = max(P_SPEED, 0.0);
  float t = iTime * spd;

  // Elevated isometric-perspective view looking down into a sunlit turquoise water basin
  vec3 ro = vec3(0.0, 2.15, 2.05);
  vec3 ta = vec3(0.0, -0.15, 0.0);
  vec3 ww = normalize(ta - ro);
  vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
  vec3 vv = cross(uu, ww);
  vec3 rd = normalize(uv.x * uu + uv.y * vv + 1.55 * ww);

  vec3 sunDir = normalize(vec3(0.45, 0.85, -0.35));

  // Intersect water plane y = 0
  float tWater = -ro.y / min(rd.y, -0.001);
  vec3 pW = ro + rd * tWater;
  vec2 wp = pW.xz;

  // Check raised cylindrical stone pillar piercing the water surface at (-0.12, -0.05)
  vec2 pilC = vec2(-0.12, -0.05);
  float pilRad = 0.24;
  float hW = evalHeight(wp, t, amp, damp, 1.0);
  vec3 nW = evalNormal(wp, t, amp, damp, 1.0);

  // Refract view ray into shallow water depth H = 0.52 down to tiled seabed
  vec3 refr = refract(rd, nW, 0.75);
  float depth = 0.52 + hW;
  vec2 floorP = wp + refr.xz * (depth / max(-refr.y, 0.2));

  // Sunlit ceramic pool tiles on basin floor
  vec2 tileGrid = abs(fract(floorP * 4.5 - 0.5) - 0.5);
  float grout = smoothstep(0.06, 0.015, min(tileGrid.x, tileGrid.y));
  vec2 tid = floor(floorP * 4.5);
  float tileVar = 0.92 + 0.08 * sin(tid.x * 7.1 + tid.y * 11.3);
  vec3 tileCol = mix(vec3(0.14, 0.62, 0.72), vec3(0.07, 0.36, 0.48), grout) * tileVar;

  // Refractive caustics from surface Laplacian curvature (focusing of parallel sun rays!)
  float eC = 0.024;
  float hC = evalHeight(floorP, t, amp, damp, 1.0);
  float lap = (
    evalHeight(floorP + vec2(eC, 0.0), t, amp, damp, 1.0) +
    evalHeight(floorP - vec2(eC, 0.0), t, amp, damp, 1.0) +
    evalHeight(floorP + vec2(0.0, eC), t, amp, damp, 1.0) +
    evalHeight(floorP - vec2(0.0, eC), t, amp, damp, 1.0) - 4.0 * hC
  ) / (eC * eC);
  float caustic = pow(clamp(-lap * 0.065 + 0.25, 0.0, 2.2), 2.2) * caustGain;
  // Subtle chromatic dispersion on caustic edges
  float causticB = pow(clamp(-lap * 0.072 + 0.20, 0.0, 2.2), 2.5) * caustGain;

  vec3 floorLit = tileCol * 0.82 + vec3(0.95, 1.02, 0.88) * caustic * 0.62 + vec3(0.15, 0.65, 1.0) * causticB * 0.22;

  // Beer-Lambert shallow water absorption
  vec3 absorb = exp(-vec3(0.45, 0.14, 0.09) * depth * 1.5);
  vec3 underWater = floorLit * absorb;

  // Fresnel sky reflection & crisp sun sparkle on rippling surface
  float ndv = max(dot(nW, -rd), 0.001);
  float fres = 0.025 + 0.975 * pow(1.0 - ndv, 5.0);
  vec3 refl = reflect(rd, nW);
  vec3 skyRefl = mix(vec3(0.74, 0.86, 0.96), vec3(0.18, 0.45, 0.82), clamp(refl.y, 0.0, 1.0));
  vec3 hVec = normalize(sunDir - rd);
  float sunSpec = pow(max(dot(nW, hVec), 0.0), 160.0) * 4.2;

  vec3 col = mix(underWater, skyRefl, clamp(fres * 1.15, 0.0, 0.92)) + vec3(1.0, 0.95, 0.84) * sunSpec;

  // Subtle submerged pillar occlusion + emergent limestone column cylinder
  float dPilFloor = length(floorP - pilC);
  col *= mix(0.45, 1.0, smoothstep(pilRad * 0.8, pilRad * 1.85, dPilFloor));

  // Ray-cylinder hit for stone pillar sticking out above water (y in [-0.5, 0.32])
  vec2 oc = ro.xz - pilC;
  float a = dot(rd.xz, rd.xz);
  float b = dot(oc, rd.xz);
  float cEq = dot(oc, oc) - pilRad * pilRad;
  float disc = b * b - a * cEq;
  if (disc > 0.0) {
    float tp = (-b - sqrt(disc)) / a;
    vec3 pp = ro + rd * tp;
    if (tp > 0.0 && tp < tWater + 0.2 && pp.y > hW - 0.02 && pp.y < 0.28) {
      vec3 pn = normalize(vec3(pp.x - pilC.x, 0.0, pp.z - pilC.y));
      float ndl = max(dot(pn, sunDir), 0.0);
      vec3 stone = vec3(0.76, 0.72, 0.65) * (0.28 + 0.95 * ndl);
      // Wet darkening waterline + foam collar
      float wet = smoothstep(hW + 0.08, hW, pp.y);
      stone *= mix(1.0, 0.58, wet);
      col = mix(stone, vec3(0.92, 0.97, 1.0), smoothstep(0.025, 0.0, abs(pp.y - hW)) * 0.7);
    }
  }
  // Pillar cap at y = 0.28
  float tCap = (0.28 - ro.y) / min(rd.y, -0.001);
  vec3 pCap = ro + rd * tCap;
  if (tCap > 0.0 && length(pCap.xz - pilC) < pilRad) {
    float rRing = smoothstep(pilRad - 0.03, pilRad, length(pCap.xz - pilC));
    col = mix(vec3(0.84, 0.80, 0.73), vec3(0.62, 0.58, 0.52), rRing);
  }

  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).

## Combines with

- [Real-time Water Caustics](https://texelatlas.vercel.app/t/water-caustics-realtime.md): Provides the surface normals that the caustic pass refracts.
- [FFT Ocean (Tessendorf)](https://texelatlas.vercel.app/t/fft-ocean.md): Interactive ripples from the SWE grid are added as local displacement over the FFT ocean.

## Sources

1. [primary] Michael Kass, Gavin Miller (1990) Rapid, stable fluid dynamics for computer graphics. Proceedings of the 17th annual conference on Computer graphics and interactive techniques. https://doi.org/10.1145/97879.97884 (verified via crossref on 2026-11-29)
2. [implementation] evanw (2015) evanw/webgl-water. https://github.com/evanw/webgl-water (verified via github on 2026-11-29)
3. [implementation] mrdoob (2010) mrdoob/three.js. https://github.com/mrdoob/three.js License: MIT. (verified via github on 2026-11-29)

## Cite this entry

Texel. "Shallow Water Equations (Height-field Water)." https://texelatlas.vercel.app/t/shallow-water-equations/ (accessed 2026-12-01).
