Skip to content

The sea

How the ocean and lakes are drawn: the wave field, the sea state that drives it, the shore, the filtering that keeps it from aliasing, and the reflection, glint and foam terms that light it. Where water is (the water map, lake levels, the sea-versus-terrain decision) is on Terrain; the sky the sea reflects is on Atmosphere and sky.

Where the ocean path runs

sat_sky.frag takes the ocean branch for a pixel when the terrain march found no land and the surface is water: by the march's own shoreline where it ran (waterPx), else by the water map (r > 0.5). See Classifying the hit.

A lake takes the same path at its own surface level waterLevelM (0 for the sea). Every height above the water is formed as

\[ h = h_{\text{eye}} + 2 - \text{waterLevelM} + t \cdot d_z \]

rather than length(p) - R_EARTH: at 6.4e6 m a float position resolves only about 0.5 m, coarser than the waves.

The wave field

The field is adapted from "Seascape" by Alexander Alekseev (TDM, 2014). seaHeight() sums octaves of a ridged value-noise pattern (seaOctave(): a noise-warped triangle-wave ridge raised to a choppiness power):

Constant Value Meaning
kSeaFreq 0.056 per m octave-0 cell 17.9 m north-south; x is stretched by 0.75 (23.8 m east-west)
kSeaHeight 2 octave-0 amplitude, x the sea state
kSeaChoppy 3 crest sharpness, plus the sea state's term
ridge period 6 cells about 107 m between ridges
kSeaSpeed 0.75 the ridges travel at about 13 m/s, a real 107 m wave's speed
octave step x 2.24, turned 26.6 deg amplitude x 0.22 per octave; time x 1.9

Each octave adds two ridge patterns, one on the octave's lattice and one on the 45-degree lattice \( [1\,1;\,-1\,1] \), so ridges do not line up into a square grid. Before the octaves, the whole field is warped by two periodic 2D noises (24 and 8 cells, about 430 m and 140 m; "Sea warp" and "Sea warp detail"). The warp's local shear stays below 1, so it cannot fold the field.

seaMap() (the trace) evaluates "Sea octaves" octaves; seaMapDetail() (the normal) "Detail octaves".

Exactly periodic

The field repeats exactly over \( 28571.4 \times 21428.6 \) m (kSeaPeriodX, kSeaPeriodY), i.e. kSeaCells = 1200 octave-0 cells:

  • the noise lattice is hashed modulo 1200 cells;
  • ridges come every 6 cells, which divides 1200;
  • the octave matrix \( [2\,1;\,-1\,2] \) and the per-octave scales kSeaOctA = {1, 2, 3, 5, 8, 14, 24, 41} are integers;
  • every octave's argument is reduced modulo 1200 cells, and the warp lattices (periods 50 and 150) divide it.

Because of that, the observer's position can be wrapped into one period without any seam. The CPU accumulates the observer's east/north travel in double (cityOffsetEastM/NorthM, ignoring jumps over 200 km) and sends it modulo the period as cloud.oceanState.xy; the shader reads waves at hitPt.xy + oceanState.xy. The arguments stay small however far the observer flies, so the waves never lose float precision and never slide against the land.

Invariant: keep the field periodic

Any new octave, warp or ripple must have an integer period that divides kSeaCells, and its argument must be reduced modulo that period. A non-integer factor breaks the wrap: the pattern jumps whenever the wrapped offset crosses a period.

Sea state

oceanSeaState() returns the wave amplitude as a multiple of the base sea, from the surface direction:

  • Weather: the clouds' weather cube at mip 3 (about 40 km), turned by the cloud drift. storm = ss(0.45, 0.85, cover) x (0.35 + 0.65 ss(0.35, 0.8, convective type)) + 0.6 x rain. See Weather.
  • Westerly belts: a band of \( |\sin \text{lat}| \) from 0.5-0.71 to 0.87-0.94 (roughly 30-45 deg to 60-70 deg).
  • A slow regional wind: two long sinusoids over the globe.
\[ s = \frac{0.3 + 0.35\,\text{wind} + 0.35\,\text{belt} + 0.9\,\text{storm}}{0.65}, \qquad \text{seaState} = \max(0.25,\; 1 + (s - 1) \cdot g) \]

with \( g \) = "Sea state from weather" (0 gives a constant sea). The amplitude scales by seaState, and the choppiness rises with it (clamp((seaState - 1) x 1.2, -1.2, 2)).

Tropical cyclones. The CPU passes the two nearest cyclones (cloud.seaCycA/B: centre, radius of maximum wind, peak wind, reach). Their wind rises to the peak at the radius of maximum wind, falls as \( (r_\text{max}/r)^{0.6} \) beyond it, is calm in the eye and fades over the outer 40 % of the reach. The sea state there is at least \( \min((U / 9\,\text{m/s})^{1.3}, 5) \): a wind sea's height grows with \(U^2\), its slopes less, so a Category 3 eyewall makes waves about five times the base sea, whitecapped, with surf on a coast it makes landfall on. Above about 20 m/s the wind also counts as rain for the storm sea trails. The cyclones themselves are on Tropical cyclones.

The CPU keeps a mirror of this rule, shipSeaState(), for the swell the landing ships ride (heave, pitch and roll from three sinusoids of 6-14 s scaled by the sea state at the ship; see Launch visuals). The CPU has no weather cube, so its storm term comes from the cloud map's coverage at that point.

The shore

With the detail on and the water within 20 km, shoreSignedDist() gives the distance to the waterline in metres, from the same water map and coves (tdShoreOffset()) the land uses, so sea and land meet on one line.

  • Shoaling: wave amplitude falls to 0.4 x over the last 150 m.
  • Breakers: lines rolling in, wave^8 of a sinusoid in the shore distance travelling shoreward, between 3 m and 10-90 m from the line, scaled with the sea state.
  • Swash: full surf within 4 m of the line.
  • Surf fades out as the pixel footprint passes 4-30 m.
  • The waterline is anti-aliased half from each side: on the water side the colour blends toward wet sand over one pixel footprint; on the land side the beach blends toward a dark water albedo (Beaches).

Storm surge

A tropical cyclone's onshore wind piles the sea up on the coast to the right of its track (left in the southern hemisphere). The rise and the shoreline it moves are part of the shared height function, so the march, both depth passes, the observer's ground and the shore all agree; the model is on Terrain. For the sea:

  • Only the sea rises (water at level 0 and sea-level water). Its surface stands at the surge's level, tapered to zero from 0.45 to 0.95 of the water map's reach offshore, so the open ocean is drawn as before. A ray that steps over that raised surface is re-intersected with the sphere at its level (waterLevelM), and the raised sea is a "lake" hit for the march.
  • Flooded land is shallow water. Over land the surge has covered (gSeaFlood, fading out about 8 km offshore, so the old coastline does not show as an outline from orbit), the waves are depth-limited (the depth falls from the surge's level at the old coast to zero at the moved shore) and the water is muddy brown.
  • No beach on a flood line: shoreSignedDist() takes the moved shore, and the beach fades out where the shore moved more than 30-150 m (gShoreSurgeShift).

Gated by a uniform first (cloud.surgeInfo.x, no cyclone with a surge) and then by each storm's reach, so a frame far from a cyclone pays nothing. Over the open sea past 0.975 of the water map's reach the surge is not evaluated at all (the rise is zero there whatever the field). Not in SKY_LITE or SKY_ENV.

Finding the wave surface

Within 5 km and below about 8 km of eye altitude, the ray is traced against the waves around the sea-sphere hit, over \( \pm \min(60\,\text{m},\; 2.5 \max(1, \text{seaState}) / \cos\theta) \) along the ray:

  1. five coarse steps to the first crossing (a secant alone converges on the back faces of crests);
  2. up to six secant steps, stopping at 1 mm.

Beyond, the sea-sphere hit is used and the waves are carried by the normal.

The normal, and filtering by footprint

The normal is a finite difference of seaMapDetail(). Without filtering, a rough sea under a low Sun aliases into crawling glitter: the sharp glint and below-horizon reflection amplify any normal noise.

  • Footprint along the view. \( f = \text{pixAngle} \cdot t / \max(|d \cdot \text{up}|, 0.02) \times \) "Sea wave sharpness". Each octave i is weighted by clamp(cell_i / f - 1, 0, 1), so it fades as its cell approaches two along-view footprints, and the loop stops at the first octave fully filtered out.
  • Lost slope becomes roughness. The slope the filter removes is not dropped: seaRough = clamp(log2(f / 2) / 3, 0, 1) x clamp(seaState, 0.25, 2) / 2, computed everywhere so there is no line where the waves end.
  • Wind roughness. A base roughness from the sea state that grows toward grazing views, \( (0.3 + 0.2\,\text{seaState}) \cdot (1 - \text{ss}(0.03, 0.4, |d \cdot \text{up}|)) \): the wind ripples (Cox and Munk) the field does not model. Without it a calm sea mirrors clouds and stars crisply to the horizon.
  • Wave range. The waves fade out between (1 - fade) x range and "Sea wave range" (100 km, fade share 0.4).
  • Altitude. The waves fade with eye altitude over 20-60 km (the footprint filter does the rest); the trace, mirrored features and satellite glints fade over 3-8 km.

Far-sea ripple

Where every wave octave is filtered out, the far band would be a mirror. A ripple normal fills it: two levels of periodic noise (seaRippleLevel()), with cells of about "Far sea ripple size" (1.5) along-view footprints, so they are always resolvable, plus a component on the 45-degree lattice. Each level is one function of its index, and the blend between adjacent levels is renormalised for contrast, so no rings form around the observer. The ripple tilts the normal mostly along the view: long, thin bands on screen, like the far sea.

Shading

Term Form
Fresnel \( \min((1 - n \cdot v)^3, 0.5) \) for the reflection weight
Reflected direction reflect(dir, n) tilted up by 0.12 x seaRough, darkened by 1 - 0.45 seaRough (half of the roughened lobe sees the next wave's water)
Below-horizon facets a steep facet mirrors the next wave, not the sky: the reflection falls to 0.2 x the horizon's light over a few degrees below it
Sky reflection its own atmosphere march along the reflected ray, "Refl samples" (6) samples on u-squared spacing. Uniform samples over a 1000 km grazing path overestimate extinction and turn the reflected horizon tan. With the multiple-scattering sky model it takes the same multiple scattering and exact, ozone-bearing Sun path as the sky (Atmosphere and sky), so the reflected twilight matches the sky above it.
Body kSeaBase x seaE plus a forward-scatter term, where seaE is the Sun and sky irradiance on the sea (with the cloud shadow), like the foam. The sky part is the terrain's sky light (Terrain): the sky hemisphere's irradiance, through the cloud over the point. Mixed with the reflection by the Fresnel.
Crest a little water colour above the mean height, fading within about 300 m
Moon glint a Phong lobe (power 120) x the Moon's brightness, by night

Reflected clouds

The clouds in the reflection are read from the on-screen half-resolution cloud targets at the reflected direction's projection (not in environment probes):

  • five taps along the screen's vertical, spread by distance and roughness;
  • weighted per texel by opacity, since the filtered alpha mixes in the no-cloud marker;
  • faded over 30% past the frame edge, so the fade lies outside the frame;
  • leaning back toward the clear-sky march with roughness and past 3-30 km;
  • under an overcast, the clear-sky share is darkened by the sea point's own cloud shadow (mix(0.35, 1, cloudShadow)), so the sea is not brighter than the cloud deck over it. It reads the filtered shadow (the [1 2 1] tent over the half-res cloud target): the raw tap carries the cloud march's 2 × 2 ordered shadow offsets, which under broken cloud would print a grid of white dots on the reflection that shimmers as the waves move;
  • texels lit only by a nearby light are dropped where they cannot lie on the reflected ray. The lookup at the mirrored direction assumes what it finds is at infinity. A texel holding only emissive or plume light (a flame, a rocket's lit exhaust, a re-entry trail) is flagged and carries that light's distance; it is dropped when the reflected ray, seen from this sea point, misses the light's position by more than 2-6 deg. A flame composited in front of a real cloud sits in that cloud's unflagged texel, so the same test is also run against the engine-light list: where the mirrored direction falls within a light's flame and glow on screen (at least 3 deg, about 600 m) and the flame dominates the texel against the cloud just outside it, the texels there are dropped too. The lights themselves are reflected from where they really are (next section). Real clouds are not tested: their screen-space reflection stays an approximation.

Engine lights and floodlights

The sea mirrors every engine, floodlight and ship light in the engine-light list (the list that also lights clouds and haze; see Launch visuals) from the light's real position. After the Sun glint, each light is evaluated with the same Beckmann lobe, its direction taken from the sea point and its irradiance \( I / (d^2 + r_0^2) \) there, dimmed by the on-screen clouds in front of the light (cloudPointVisibilityAt()). A light whose lobe peak could not reach \(10^{-5}\) is skipped before the lobe is evaluated.

A light near the eye therefore mirrors at its true specular point. For a source at height \(H\) and horizontal distance \(D\), seen from an eye at height \(h\), the mirror point lies \( D h / (h + H) \) from the eye, at a depression of \( \arctan((H + h)/D) \): always deeper below the horizon than the source is above it. Satellite glints keep the infinite-distance geometry, which is under 1 deg off at hundreds of km. Not in SKY_LITE or the environment probes. Cost is within run-to-run noise.

Storm sea trails

On a rough sea the sky TAA deliberately keeps more history: the crests smear into streaks, which reads as wind-driven spray. sat_sky.frag marks sea pixels with a trail share

\[ \text{trail} = g \cdot \text{ss}(1.3, 1.9, \text{seaState}) \cdot \text{ss}(0.1, 0.4, \text{rain}) \cdot \text{altFade} \]

where rain is the weather map's rain over that point of the sea (gSeaRain, the march's own rule) and \(g\) is "Storm sea trails". sky_taa.comp blends the history toward unclipped by that share and holds the still weight, but only while the view moves little: the share fades out over 1-4 px of parallax a frame. Without the rain gate and the parallax fade, every stormy sea would drag the clouds above it across the water whenever the eye moved.

The Milky Way and the aurora are reflected too (Aurora and airglow): the Milky Way at mip \( 3 + 4\,\text{seaRough} \), scaled by "Ocean MW refl" (Atmosphere tab); the aurora by a 6-step march along the reflected ray. Both are hidden behind reflected clouds and fade with the wave range.

The Sun glint

A Beckmann microfacet lobe about the resolved wave normal, in the surface units (\( \pi L / E \)):

\[ \sigma^2 = 0.003 + 0.00512\,W + 0.06\,\text{seaRoughFoot} + 2 \times 10^{-5}, \qquad W = 7\,\text{m/s} \times \text{clamp}(\text{seaState}, 0.2, 2.5) \]

the Cox and Munk slope variance, plus the slope the footprint filter removed, plus the Sun's disc. Schlick Fresnel (F0 = 0.02) and Smith shadowing-masking (Walter's rational fit), times the Sun's real transmittance (sunTransMax, since the tint is hue-normalised) and "Sea glitter (Sun)" (3 by default; 1 is the physical lobe). The shadowing and masking are the whole wave field's, taken against the mean sea surface (the Cox-Munk glitter model, after Saunders), since the slope variance is the whole field's. Taken against the resolved wave normal instead, every back slope facing away from a low Sun would go black and the glitter shrink to lines along the crests. Because the filtered slope widens the lobe instead of flattening it, the glitter path survives at every distance, from orbit included.

Sparkles. The lobe is a pixel's mean. A sea is millions of facets, and the few tilted right each mirror the Sun's whole disc, so where they cover a small share of the pixel the light arrives as flashes. In the full variants:

  • the facets' share of the pixel is \( f_s = \text{glint} / (F \cdot 1.8\times10^{5}) \) (the disc's radiance in these units), and a pixel holds \( N = \max(150, (\text{footprint} / 0.3\,\text{m})^2) \) facets;
  • sparkles are 2 × 2-pixel cells: a cell lights with chance \( q = \operatorname{clamp}(4 f_s N, 0.02, 1) \), redrawn six times a second of sim time by an integer hash of the cell and the time step;
  • 40 % of the glint stays the smooth path and 60 % rides in the sparkles, a lit cell carrying \(0.6/q\) of the mean (up to 30 times it), so the energy is kept; the glint is capped at 4000;
  • from orbit a pixel covers millions of facets, \(q\) reaches 1 and the glint is its smooth mean.

A lit sparkle where \(q < 0.5\) is flagged to the sky TAA (alpha 0.6), which takes it whole instead of averaging it into the history: a real flash on the waves in a still view. The hash must be an integer hash: a sin-based hash of large arguments draws repeating diagonal streaks across the glint. SKY_LITE keeps the smooth lobe, capped at 400. The same physics for the Sun's own disc is on The Sun as an object.

Foam, whitecaps, surf and wet sand

All four are diffusers: lit by the Sun on the facet, the terrain's sky irradiance and the Moon, never by the mirrored sky (whitewater showing a reflected image reads as liquid metal).

  • Whitecaps grow over sea state 1.05-2.3. Near, crest foam where the crest value passes 0.6-0.76 (the top few percent), broken by noise; far, the mean foam fraction (up to 12% x "Whitecaps", after Monahan) as albedo.
  • Surf and wet sand: see The shore.

Satellite glints on the water

sat_flare.comp lists up to 512 satellites bright enough to glint (effectFlare of at least 1) in OceanGlintBuf. For each sea pixel, sat_sky.frag adds a sharp lobe (power 80) where the reflected view direction meets a listed satellite, scaled by its brightness. Per entry: skipped below "Flare refl floor" (about 37.9), below the limb, more than about 26 deg off the reflected ray, or behind the camera; then hidden by on-screen cloud and terrain at its projection.

The whole loop is skipped by day and above about 8 km ((1 - dayFrac) x altFade x gain is zero): 512 entries with two texture fetches each are several milliseconds of the sky pass from orbit.

Settings

Stored in settings.json under clouds. Ocean tab unless noted.

UI label Key Default Effect
Sea octaves ocean_sea_octaves 3 octaves in the wave trace
Detail octaves ocean_detail_octaves 5 octaves in the normal
Refl samples ocean_refl_samples 6 sky-reflection march samples
Sea state from weather ocean_sea_state 1 0 = a constant sea
Whitecaps ocean_whitecaps 1 foam amount
Sea wave range (km) ocean_wave_range_km 100 where the wave normal ends
Sea wave range fade ocean_wave_range_fade 0.4 share of the range it fades over
Sea wave sharpness ocean_wave_sharpness 0.1 x the footprint the octaves are filtered at (0.1-1); higher = softer
Far sea ripple ocean_far_ripple 1.7 ripple strength in the far band
Far sea ripple size (px) ocean_far_ripple_size 1.5 ripple cell in along-view footprints (1.5-16); lower = finer, more shimmer
Storm sea trails ocean_storm_trails 1 how much history the sky TAA keeps on a raining, rough sea
Sea glitter (Sun) ocean_sun_glitter 3 x the Sun's glint, its path and its sparkles (0-20; 1 = the physical lobe)
Sea warp ocean_warp 3.5 the 430 m warp
Sea warp detail ocean_warp_detail 1.2 the 140 m warp
Ocean flare refl ocean_glint_gain 0.02 satellite glints on the water
Flare refl floor ocean_glint_min_flux 37.9 faintest satellite (effectFlare) that glints
Ocean MW refl (Atmosphere tab) ocean_mw_refl_gain 0.4 Milky Way in the sea

"Storm surge" (clouds_v2.storm_surge, default 1, 0-2, no UI row) scales the cyclones' surge; 0 turns it off.

The graphics presets set the three sample counts (Planetarium and Potato: 3 / 5 / 3 and no ocean reflection, knockout bit 8; the others 3 / 5 / 6).

Debugging

Harness debugview:

Name Id Shows
oceanrefl 40 the reflection colour
oceanfresnel 41 Fresnel, reflection darkening, reflection weight
oceanstate 42 sea state, crest value, wave blend (1 = waves filtered out)
oceansurf 43 the surface before glints
oceannormal 44 the wave normal
oceanshore 45 the signed shore distance
oceanshadow 50 the cloud shadow on the sea and the reflected-cloud weight

Where in the code

File What
shaders/sat_sky.frag seaHeight(), seaMap(), seaMapDetail(), seaRippleLevel(), oceanSeaState(), shoreSignedDist(), the ocean branch of main() (glint, sparkles, flood water)
shaders/include/terrain_detail.glsl tdSurgeAt(), the surge in tdDemAt()
src/simulations/SatelliteSimWeather.cpp weatherFillSeaParams(): the nearest cyclones and their surge into CloudParams
shaders/sat_flare.comp the satellite ocean-glint list
shaders/sky_taa.comp the storm sea trails in the history blend
src/simulations/SatelliteSim.cpp the world offset and its wrap into oceanState
src/simulations/SatelliteSimLaunches.cpp engineLightAdd() (the light list the sea mirrors); shipSeaState(), the CPU mirror of the sea-state rule for the landing ships' swell