← Back to blog
July 2, 2026Shaders

GLSL Ocean

Live shaderReal-time GLSL running in your browser

Shader not running? It needs a browser with WebGL2. If it's slow, open Controls and set Quality to Low.

Source ocean.fragView and edit the GLSL, live

An endless open ocean I set up as a GLSL TOP in TouchDesigner, ported to WebGL2 so it runs right here in the page. It's an adaptation of “Seascape” by Alexander Alekseev (TDM), used under the CC BY-NC-SA 3.0 license, and this version is shared under the same license. The same shader also runs behind the headline on this site's home page.

The newest addition is a ship's wake: a trail of churned, turquoise water and foam stretching back to the horizon. Turn up Wake amount in Controls to see it. Here's how the whole thing works.

Open ocean under a pale sky, with deep blue swells and small whitecaps
The default view: rolling swell, whitecaps on the steepest crests and a pale sky.

The waves

There's no mesh. The sea is a height field, a function that returns the water height at any point, and the shader finds where each pixel's view ray hits it. The height comes from stacked octaves of a choppy wave shape:

  • The wave shape: each octave warps its coordinates with noise, then blends 1 − |sin| with |cos| and sharpens the result with a power. That gives pointed crests and wide, soft troughs, like real wind waves.
  • Octaves: each one is about twice the frequency and a fifth of the height of the last, and gets a little less choppy, so big swells carry smaller ripples.
  • Two directions: every octave is added twice, travelling forward and back, so the surface looks like it's sloshing instead of sliding past.
  • My changes to the original: quintic gradient noise with a second warp pass instead of the original's value noise, and each octave's direction rotated by an odd angle, so the pattern never lines up into a grid.

Finding the surface

Ray-marching a height field step by step would be slow, so the shader brackets the answer instead. It checks the height at the camera and 1,000 units away, then repeatedly guesses where the crossing sits between them, narrowing the gap each time (a false-position search). Thirty-two steps is plenty.

The search only uses 3 octaves, the broad shapes that decide where the ray lands. The shading uses all 7, so the fine ripples cost nothing during the search. Normals come from finite differences, and the sample spacing grows with distance. Far-off waves get smoothed for free, which hides most of the shimmer you'd otherwise see near the horizon.

Colour and light

  • Fresnel: water facing the camera shows the deep blue below it; grazing angles reflect the sky. That's what makes the distance go pale.
  • Height tint: crests are lifted toward a sea-green colour, so the swells read even when the light is flat.
  • Specular: a sharp sun highlight that gets broader with distance, so far-off glints don't sparkle into noise.
  • Subsurface scattering: when you look toward the sun, light glows through the thin tops of the waves.
  • Foam: whitecaps form where the surface is both steep and high. The Foam control moves the threshold rather than the brightness, so low values keep foam on only the sharpest peaks.
Side by side: the ocean with no foam on the left and heavy foam on the right
Foam at 0 (left) and 1 (right).

The ship's wake

The camera already moves forward at a steady speed while looking back the way it came, so it's as if you're standing at the stern of a ship. That made a wake the natural next step. I worked from photos of real ones. The early versions gave the wake its own churning surface and its own foam texture, and it read as a strip of noise laid over the sea, the same from the stern to the horizon. What makes a real wake read as real turned out to be:

  • Its own water: the wake knocks the ocean's swell down by more than half (most of all just behind the stern) and replaces it with its own churn: long mounds drawn out along the path, roughest near the ship and settling as it ages. The swell still shows through faintly, so the wake rises and falls with the sea around it. A fine, fast chop keeps the water from looking glassy.
  • A narrow source: it leaves the stern about the width of the hull and spreads to its full width over the first few dozen units, then keeps widening very slowly.
  • Foam follows the surface: it's whitest up on the crests of the churn and the swell and thins in the troughs, so its shape comes from the water, not from a texture.
  • Lanes: across the width, the foam runs in bands along the path: a bright centre lane from the propellers, a lane down each side and a ragged, broken line at each edge. Between them, turquoise water shows through. The lanes meander a little, and because they're laid out in the world, perspective draws them together toward the horizon.
  • Streaks: inside the lanes the foam is drawn out into long streaks along the path (noise stretched about fifteen times along the path, and bent sideways by a slower noise so the streaks wander), then torn into ragged pieces with gaps along them and bubbly holes, so they never run as solid white lines.
  • It changes with age: right behind the stern, the lanes merge into one wide, churned mass of clumps and lace (a warped Voronoi net of bubble walls, broken so it never closes into cells). Further back it settles into streaky lanes. Far away only the centre lane is left, a bright line running to the horizon.
  • Turquoise water: bubbles under the surface scatter light back up, so the wash glows turquoise, brightest where the swell lifts it toward the light and in the fresh water behind the stern, and fading to the dark sea at the sides. Only the underwater colour changes, so sky reflections and sun glints still play across it.
  • Foam is lit: dense foam is white, thin foam is tinted by the turquoise under it, and it's shaded softly by the surface, bright on faces toward the sky and grey-blue where it turns away.
  • Spray and a V: spray lands in flecks just outside the sides, mostly near the stern, and a V of diverging waves (a Kelvin wake, about 19.5° each side) fans out behind the ship and travels with it. The ocean's own whitecaps are switched off inside the wake, where they'd turn the churned water into white blotches.
  • Distance: the finest detail fades out with distance so it doesn't shimmer, and nothing is computed far behind the ship.
A ship's wake of turquoise water and white foam riding the ocean swell, narrowing from the camera to a line at the horizon
Wake amount at 1: churned foam behind the stern settling into streaky lanes over the swell, and a single line at the horizon.

Building the wake turned up an old bug, too. A handful of pixels were going black, with or without the wake. There were two culprits. The wave function raises 1 − x to a power, and rounding can push that base a hair below zero; a power of a negative number is undefined in GLSL. And the noise's random gradient could, very rarely, come out as a zero vector, which can't be normalized. Both produced NaN pixels. Clamping the first and guarding the second fixed them, for the home page background as well.

Controls

Drag on the water to look around, scroll or pinch to zoom, and double-click to reset the view. Open Controls in the top-right corner for:

  • Quality: Low, Medium, High (the default on desktop) and Extreme, which renders at twice the resolution and scales it down for smoother edges.
  • Foam, Zoom and Wave speed.
  • Wake amount: 0 is open sea, 1 is a full wake. Values in between give a faded, older-looking wake.
  • Pause and Reset.

Under the hood

  • One source: the shader lives in my TouchDesigner project. The site's build script converts it to WebGL2 automatically, swapping TouchDesigner's built-ins (the texture coordinates and output resolution) for their browser equivalents. Any change I make in TouchDesigner shows up here on the next build.
  • The wake in TouchDesigner: it's a single new uniform, uWake. Left unset, it's 0, so the TouchDesigner version looks exactly as before until I wire it to a slider.
  • Home page: the background behind the headline is this shader at reduced resolution with the wake off, paused whenever it scrolls out of view.

The GLSL source sits below the shader on this page, and you can edit it live. Try changing SEA_HEIGHT, SEA_CHOPPY or the wake's width and see what happens.

— CGDavidB