Open ocean

glint · Examples · Tutorials · Gallery

A sunset sea you can fly over: hundreds of waves from a real sea-state spectrum (wind sea and swell) in four repeating cascades, choppy crests, whitecaps that linger and drift into streaks, sun glitter and a sky of air, haze and two cloud layers, with bloom. Drag to look round; click the picture, then W A S D to move, E / Q up and down, Shift for speed.

Open ocean: a GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

9 passes, drawn in this order each frame, in GLSL.

Air: fragment shader

#version 300 es
precision highp float;
precision highp int;

uniform mat3 cubeFace;
uniform vec2 resolution;
uniform float time;
uniform float sunElevation;
uniform float sunAzimuth;
uniform float windAzimuth;
uniform float haze;
uniform float cloudCover;

out vec4 fragColor;

// The clear sky (air and haze, no clouds) all round, small: it changes slowly across the sky, so 64 x 64 a face
// is plenty, and the other passes look it up instead of working it out per pixel.
void main() {
    vec3 d = normalize(cubeFace * vec3(gl_FragCoord.xy / resolution * 2.0 - 1.0, 1.0));
    fragColor = vec4(atmosphere(d, fromAngles(sunAzimuth, sunElevation), haze), 1.0);
}

Sky cube: fragment shader

#version 300 es
precision highp float;
precision highp int;

uniform mat3 cubeFace;
uniform vec2 resolution;
uniform samplerCube pass0;
uniform float time;
uniform float sunElevation;
uniform float sunAzimuth;
uniform float windAzimuth;
uniform float haze;
uniform float cloudCover;

out vec4 fragColor;

// The sky all round with its clouds, as a cube map, for the sea's reflections (no sun disc: the water draws the
// sun's glitter itself). Its blurrier mipmaps are what rough water reflects.
void main() {
    vec3 d = normalize(cubeFace * vec3(gl_FragCoord.xy / resolution * 2.0 - 1.0, 1.0));
    vec3 s = fromAngles(sunAzimuth, sunElevation);
    vec2 wind = fromAngles(windAzimuth, 0.0).xz;
    vec3 sky = textureLod(pass0, vec3(d.x, max(d.y, 0.0), d.z), 0.0).rgb;
    vec4 cl = clouds(d, s, cloudCover, haze, time, wind, 2.0 / resolution.x);
    fragColor = vec4(sky * (1.0 - cl.a) + cl.rgb, 1.0);
}

Waves: fragment shader

#version 300 es
precision highp float;
precision highp int;

uniform vec2 resolution;
uniform float time;
uniform sampler2D feedback;
uniform float windSpeed;
uniform float windAzimuth;
uniform float swellHeight;
uniform float choppiness;

out vec4 fragColor;

// The waves, summed from a sea-state spectrum (JONSWAP, with Donelan-Banner spreading, plus a long swell) into
// four repeating tiles. Every wave fits its tile exactly (a whole number of wavelengths across), so the tiles repeat
// seamlessly. Out: (sideways x, height, sideways z, divergence) in metres; divergence below 0 means the surface is
// being squeezed together, where crests break into foam.
//
// The spectrum (each wave's height, phase and speed) only changes with the settings, so it's worked out into a
// small table above the atlas (13 x 13 per cascade) and read from last frame's copy.

float omega(float k) { return sqrt(G * k * (1.0 + k * k / (370.0 * 370.0))); }   // deep water, with ripples' surface tension
float dOmega(float k) { return G * (1.0 + 3.0 * k * k / (370.0 * 370.0)) / (2.0 * omega(k)); }

// Wind sea: JONSWAP for a 100 km fetch, energy per unit of wavenumber area at (k, angle from the wind).
float windSea(float k, float th) {
    float U = max(windSpeed, 0.5), F = 100000.0;
    float w = omega(k);
    float alpha = 0.076 * pow(U * U / (F * G), 0.22);
    float wp = 22.0 * pow(G * G / (U * F), 1.0 / 3.0);
    float sig = w <= wp ? 0.07 : 0.09;
    float r = exp(-(w - wp) * (w - wp) / (2.0 * sig * sig * wp * wp));
    float S = alpha * G * G / pow(w, 5.0) * exp(-1.25 * pow(wp / w, 4.0)) * pow(3.3, r);
    float x = w / wp;
    float beta = x < 0.95 ? 2.61 * pow(x, 1.3) : x < 1.6 ? 2.28 * pow(x, -1.3) : pow(10.0, -0.4 + 0.8393 * exp(-0.567 * log(x * x)));
    float ch = cosh(beta * th);
    float D = beta / (2.0 * tanh(beta * PI)) / (ch * ch);
    return S * D * dOmega(k) / k;
}

ivec2 tableTexel(int c, ivec2 n, float N) { return ivec2(c * 13 + n.x + 6, int(4.0 * N) + n.y + 6); }

vec4 tableEntry(int c, ivec2 n) {
    vec2 wind = tileRot(c) * fromAngles(windAzimuth, 0.0).xz;
    vec2 swellDir = tileRot(c) * fromAngles(windAzimuth - 35.0, 0.0).xz;
    float L = tileSize(c), dk = 2.0 * PI / L;
    float nl = length(vec2(n));
    // Each cascade takes the wavelengths its tile holds best; together they cover 400 m down to 22 cm.
    if (nl < 1.0 || nl >= RATIO) return vec4(0.0);
    vec2 k = vec2(n) * dk;
    float kl = length(k);
    float th = acos(clamp(dot(k / kl, wind), -1.0, 1.0));
    float var = windSea(kl, th) * dk * dk;
    // Swell: 170 m waves from a little off the wind, shared out between the nearest few waves of the big tile.
    if (c == 0 && swellHeight > 0.0) {
        vec2 ns = swellDir * (2.0 * PI / 170.0) / dk;
        float wsum = 0.0;
        for (int j = -6; j <= 6; j++)
            for (int i = -6; i <= 6; i++) {
                float l = length(vec2(i, j));
                if (l >= 1.0 && l < RATIO) wsum += exp(-dot(vec2(i, j) - ns, vec2(i, j) - ns) / 0.5);
            }
        var += swellHeight * swellHeight / 16.0 * exp(-dot(vec2(n) - ns, vec2(n) - ns) / 0.5) / max(wsum, 1e-6);
    }
    // Short waves a little stronger than the spectrum says: they're what makes the water look wind-roughened close to.
    var *= c == 1 ? 1.5 : c >= 2 ? 2.6 : 1.0;
    // A random height (Rayleigh, mean square 1) and phase for each wave, fixed per wave.
    float r = sqrt(-log(max(hash3(vec3(vec2(n), float(c) + 0.5)), 1e-6)));
    float ph = 2.0 * PI * hash3(vec3(vec2(n) + 101.0, float(c) + 7.5));
    return vec4(sqrt(2.0 * var) * r, ph, omega(kl), 1.0);
}

void main() {
    ivec2 t = ivec2(gl_FragCoord.xy);
    float N = cellN(resolution);
    // The table row(s).
    if (t.y >= int(4.0 * N) && t.y < int(4.0 * N) + 13 && t.x < 52) {
        int c = t.x / 13;
        fragColor = tableEntry(c, ivec2(t.x - c * 13 - 6, t.y - int(4.0 * N) - 6));
        return;
    }
    if (t.x >= int(4.0 * N) || t.y >= int(4.0 * N)) { fragColor = vec4(0.0); return; }
    int c = (t.x >= int(2.0 * N) ? 1 : 0) + (t.y >= int(2.0 * N) ? 2 : 0);
    vec2 local = vec2(t) - cellOrigin(c, N);
    float L = tileSize(c), dk = 2.0 * PI / L;
    vec2 x = (local + 0.5 - 0.5 * N) / N * L;
    float h = 0.0, div = 0.0;
    vec2 D = vec2(0.0);
    for (int j = -6; j <= 6; j++)
        for (int i = -6; i <= 6; i++) {
            vec4 e = texelFetch(feedback, tableTexel(c, ivec2(i, j), N), 0);
            if (e.x <= 0.0) continue;
            vec2 k = vec2(i, j) * dk;
            float kl = length(k);
            float ph = dot(k, x) - e.z * time + e.y;
            float sn = sin(ph), cs = cos(ph);
            h += e.x * cs;
            D -= choppiness * e.x * (k / kl) * sn;
            div -= choppiness * e.x * kl * cs;
        }
    fragColor = vec4(D.x, h, D.y, div);
}

Slopes: fragment shader

#version 300 es
precision highp float;
precision highp int;

uniform vec2 resolution;
uniform sampler2D pass2;

out vec4 fragColor;

// The surface's slopes from the Waves atlas, by differences between neighbouring texels: the true slopes of the
// displaced (choppy) surface. Out: (slope x, slope z, slope x squared, slope z squared). Mipmapped, the squares'
// average minus the average's square is how much the slopes vary inside a pixel (LEAN mapping): far away, waves
// too small to draw become roughness instead of flicker.
vec3 at(int c, vec2 l, float N) {
    l = mod(l, 2.0 * N);
    return texelFetch(pass2, ivec2(cellOrigin(c, N) + l), 0).xyz;
}
void main() {
    ivec2 t = ivec2(gl_FragCoord.xy);
    float N = cellN(resolution);
    if (t.x >= int(4.0 * N) || t.y >= int(4.0 * N)) { fragColor = vec4(0.0); return; }
    int c = (t.x >= int(2.0 * N) ? 1 : 0) + (t.y >= int(2.0 * N) ? 2 : 0);
    vec2 l = vec2(t) - cellOrigin(c, N);
    float dx = tileSize(c) / N;
    vec3 ddx = (at(c, l + vec2(1.0, 0.0), N) - at(c, l - vec2(1.0, 0.0), N)) / (2.0 * dx);
    vec3 ddz = (at(c, l + vec2(0.0, 1.0), N) - at(c, l - vec2(0.0, 1.0), N)) / (2.0 * dx);
    vec3 tx = vec3(1.0 + ddx.x, ddx.y, ddx.z), tz = vec3(ddz.x, ddz.y, 1.0 + ddz.z);
    vec3 n = normalize(cross(tz, tx));
    vec2 s = -n.xz / max(n.y, 0.05);
    fragColor = vec4(s, s * s);
}

Foam and camera: fragment shader

#version 300 es
precision highp float;
precision highp int;

uniform vec2 resolution;
uniform float time;
uniform float timeDelta;
uniform int frame;
uniform vec2 mouse;
uniform vec3 mouseButtons;
uniform sampler2D keyboard;
uniform sampler2D feedback;
uniform sampler2D pass2;
uniform float sunAzimuth;
uniform float windAzimuth;
uniform float foamAmount;
uniform float moveSpeed;

out vec4 fragColor;

// Two jobs. The camera: drag to look round, W A S D to move, E / Space up, Q / C down, Shift for five times as
// fast (click the picture first, so keys go to it rather than the editor). It's kept in three texels at the top
// right, from frame to frame.
// And the foam: whitecaps start where the surface is squeezed together (the Waves pass's divergence), fade over a
// few seconds, drift downwind, and thin out into streaks. Kept in a map around the camera.

bool key(int code) { return texelFetch(keyboard, ivec2(code, 0), 0).x > 0.5; }

float waterHeight(vec2 xz, float N) {
    vec2 size = vec2(textureSize(pass2, 0));
    float h = 0.0;
    for (int c = 0; c < 4; c++) h += textureLod(pass2, atlasUV(xz, c, N, size), 0.0).y;
    return h;
}

// This frame's camera: last frame's, moved by the mouse and keys. (Every texel works it out, the same way.)
vec4 look;
Cam update(float N) {
    vec4 a = texelFetch(feedback, stateTexel(resolution, 0), 0);
    vec4 b = texelFetch(feedback, stateTexel(resolution, 1), 0);
    vec4 f = texelFetch(feedback, stateTexel(resolution, 2), 0);
    Cam c = Cam(b.xyz + f.xyz, a.x, a.y);
    look = vec4(a.xy, -1.0, -1.0);
    if (frame < 2 || b.w < 0.5) {
        c = Cam(vec3(0.0, 3.0, 0.0), radians(sunAzimuth), -0.07);
        look.xy = vec2(c.yaw, c.pitch);
        return c;
    }
    if (mouseButtons.x > 0.5) {
        if (a.z >= 0.0) {
            c.yaw += (mouse.x - a.z) * 3.0 * resolution.x / resolution.y;
            c.pitch = clamp(c.pitch + (mouse.y - a.w) * 2.4, -1.45, 1.45);
        }
        look.zw = mouse;
    }
    look.xy = vec2(c.yaw, c.pitch);
    mat3 B = camBasis(c.yaw, 0.0);
    vec3 move = B[2] * (float(key(87)) - float(key(83))) + B[0] * (float(key(68)) - float(key(65)));
    move.y += float(key(69) || key(32)) - float(key(81) || key(67));
    float speed = moveSpeed * (key(16) ? 5.0 : 1.0);
    c.pos += move * speed * min(timeDelta, 0.1);
    // Always above the water (and not so high that the sea is a dot).
    c.pos.y = clamp(c.pos.y, waterHeight(c.pos.xz, N) + 1.2, 4000.0);
    return c;
}

void main() {
    ivec2 t = ivec2(gl_FragCoord.xy);
    float N = cellN(resolution);
    Cam c = update(N);
    if (t == stateTexel(resolution, 0)) { fragColor = look; return; }
    vec3 coarse = floor(c.pos / 16.0) * 16.0;
    if (t == stateTexel(resolution, 1)) { fragColor = vec4(coarse, 1.0); return; }
    if (t == stateTexel(resolution, 2)) { fragColor = vec4(c.pos - coarse, 0.0); return; }

    float M = foamM(resolution);
    if (t.x >= int(M) || t.y >= int(M)) { fragColor = vec4(0.0); return; }
    float ts = FOAM_WORLD / M;
    vec2 centre = floor(c.pos.xz / ts) * ts;
    vec2 x = centre + (vec2(t) + 0.5 - 0.5 * M) * ts;

    // Last frame's foam at this spot, carried downwind.
    Cam prev = readCam(feedback);
    vec2 prevCentre = floor(prev.pos.xz / ts) * ts;
    float dt = min(timeDelta, 0.1);
    vec2 wind = fromAngles(windAzimuth, 0.0).xz;
    vec2 src = (x - wind * 0.7 * dt - prevCentre) / ts + 0.5 * M;
    vec2 old = (all(greaterThan(src, vec2(0.5))) && all(lessThan(src, vec2(M - 0.5)))) ? texture(feedback, src / resolution).xy : vec2(0.0);
    if (frame < 2) old = vec2(0.0);

    vec2 size = vec2(textureSize(pass2, 0));
    float div = 0.0;
    for (int k = 0; k < 4; k++) div += textureLod(pass2, atlasUV(x, k, N, size), 0.0).w;
    float J = 1.0 + div;
    float source = sstep(mix(0.15, 0.75, foamAmount), mix(-0.2, 0.35, foamAmount), J);
    float fresh = clamp(old.x * exp(-dt / 0.9) + source * dt * 7.0, 0.0, 1.0);
    float aged = clamp(max(old.y * exp(-dt / (1.5 + 7.0 * foamAmount)), fresh), 0.0, 1.0);
    fragColor = vec4(fresh, aged, 0.0, 1.0);
}

Sky: fragment shader

#version 300 es
precision highp float;
precision highp int;

uniform vec2 resolution;
uniform samplerCube pass0;
uniform sampler2D pass4;
uniform float fov;
uniform float time;
uniform float sunElevation;
uniform float sunAzimuth;
uniform float windAzimuth;
uniform float haze;
uniform float cloudCover;

out vec4 fragColor;

// The sky behind the sea, at full resolution: atmosphere, clouds, and the sun's disc (bright enough to bloom).
void main() {
    Cam c = readCam(pass4);
    vec3 d = viewRay(c, gl_FragCoord.xy, resolution, fov);
    vec3 s = fromAngles(sunAzimuth, sunElevation);
    vec2 wind = fromAngles(windAzimuth, 0.0).xz;
    float pixelAngle = 2.0 * tan(radians(fov) * 0.5) / resolution.y;
    vec3 sky = textureLod(pass0, vec3(d.x, max(d.y, 0.0), d.z), 0.0).rgb;
    vec4 cl = clouds(d, s, cloudCover, haze, time, wind, pixelAngle);
    // The sun: about half a degree across, darker at its rim, dimmed by the air it shines through.
    float cs = dot(d, s), rad = 0.00465;
    float r = sqrt(max(1.0 - cs * cs, 0.0)) / rad;
    float disc = sstep(1.0 + pixelAngle / rad, 1.0 - pixelAngle / rad, r) * step(0.0, cs);
    vec3 sun = sunLight(s, haze) / (PI * rad * rad) * disc * (0.6 + 0.4 * sqrt(max(1.0 - r * r, 0.0)));
    vec3 col = (sky + min(sun, vec3(30000.0))) * (1.0 - cl.a) + cl.rgb;
    fragColor = vec4(col, 1.0);
}

Ocean: vertex shader

#version 300 es
precision highp float;
precision highp int;

in vec2 a_texcoord;

uniform vec2 resolution;
uniform sampler2D pass2;
uniform sampler2D pass4;
uniform float fov;

out vec2 v_x0;
out vec3 v_pos;

// A projected grid: the mesh's rows and columns are spread evenly over the screen below the horizon, each vertex
// is put where its pixel's ray meets the sea, and then moved by the waves. So there are as many triangles near
// the camera as far away, never more than the screen can show.
// (Vertex shaders don't get the Common pass, so the helpers it needs are copied here.)
const float RATIO = 6.55;
float tileSize(int c) { return 400.0 / pow(RATIO, float(c)); }
float cellN(vec2 res) { float m = min(res.x, res.y); return m >= 525.0 ? 128.0 : m >= 269.0 ? 64.0 : m >= 141.0 ? 32.0 : 16.0; }
vec2 cellOrigin(int c, float N) { return vec2(float(c & 1), float(c >> 1)) * 2.0 * N; }
// Each cascade's tile is turned to its own angle, so the repeats of different tiles never line up into a grid.
mat2 tileRot(int c) { float a = float(c) * 1.13; return mat2(cos(a), sin(a), -sin(a), cos(a)); }
vec2 atlasUV(vec2 x, int c, float N, vec2 size) { return (cellOrigin(c, N) + 0.5 * N + fract(tileRot(c) * x / tileSize(c)) * N) / size; }
ivec2 stateTexel(vec2 res, int i) { return ivec2(int(res.x) - 1 - i, int(res.y) - 1); }
mat3 camBasis(float yaw, float pitch) {
    vec3 f = vec3(sin(yaw) * cos(pitch), sin(pitch), -cos(yaw) * cos(pitch));
    vec3 r = vec3(cos(yaw), 0.0, sin(yaw));
    return mat3(r, cross(r, f), f);
}

const float SEGMENTS = 512.0, NEAR = 0.05, FAR = 60000.0;

void main() {
    vec2 res3 = vec2(textureSize(pass4, 0));
    vec4 a = texelFetch(pass4, stateTexel(res3, 0), 0);
    vec3 cam = texelFetch(pass4, stateTexel(res3, 1), 0).xyz + texelFetch(pass4, stateTexel(res3, 2), 0).xyz;
    mat3 B = camBasis(a.x, a.y);
    float th = tan(radians(fov) * 0.5), aspect = resolution.x / resolution.y;

    // Where the horizon is on screen (no roll, so it's level): the rows go from below the screen up to it.
    vec3 ahead = vec3(sin(a.x), 0.0, -cos(a.x));
    float horizon = dot(ahead, B[1]) / dot(ahead, B[2]) / th;
    float bottom = -1.06, top = min(horizon, 1.06);
    if (top <= bottom) { gl_Position = vec4(2.0, 2.0, 2.0, 1.0); return; }
    // Wider than the screen at the bottom, where the waves' sideways movement could pull the edge into view.
    // The first few rows go far below the screen (down to nearly under the camera), so a crest passing beneath
    // can't lift the edge of the mesh into view; they're wider too, for the waves' sideways movement.
    const float SKIRT = 0.06;
    float v = a_texcoord.y;
    float y = v < SKIRT ? mix(-8.0, bottom, v / SKIRT) : mix(bottom, top, (v - SKIRT) / (1.0 - SKIRT));
    vec2 ndc = vec2((a_texcoord.x * 2.0 - 1.0) * (v < SKIRT ? mix(8.0, 2.06, v / SKIRT) : 1.25 + 0.8 * (1.0 - smoothstep(SKIRT, 0.45, v))), y);
    vec3 rd = normalize(B * vec3(ndc.x * th * aspect, ndc.y * th, 1.0));
    rd.y = min(rd.y, -1e-5);
    float t = min(cam.y / -rd.y, FAR * 0.8);
    vec2 x0 = cam.xz + rd.xz * t;

    // Waves, each cascade only where the grid is fine enough to show it (the shading adds the rest).
    vec2 size = vec2(textureSize(pass2, 0));
    float N = cellN(size);
    float cellAngle = (top - bottom) / (SEGMENTS * (1.0 - SKIRT)) * th;
    float footprint = t * cellAngle / sqrt(max(-rd.y, 0.02));
    vec3 D = vec3(0.0);
    for (int c = 0; c < 4; c++) {
        float lod = log2(max(footprint / (tileSize(c) / N), 1e-4));
        float w = 1.0 - smoothstep(1.5, 3.0, lod);
        if (w > 0.0) {
            vec3 d = textureLod(pass2, atlasUV(x0, c, N, size), clamp(lod, 0.0, log2(N) - 3.0)).xyz;
            D += w * vec3(transpose(tileRot(c)) * d.xz, d.y).xzy;
        }
    }
    vec3 P = vec3(x0.x + D.x, D.y, x0.y + D.z);
    v_x0 = x0;
    v_pos = P;
    vec3 e = transpose(B) * (P - cam);   // in the camera's frame: right, up, forward
    gl_Position = vec4(e.x / (th * aspect), e.y / th, (FAR + NEAR) / (FAR - NEAR) * e.z - 2.0 * FAR * NEAR / (FAR - NEAR), e.z);
}

Ocean: fragment shader

#version 300 es
precision highp float;
precision highp int;

uniform vec2 resolution;
uniform float time;
uniform samplerCube pass0;
uniform samplerCube pass1;
uniform sampler2D pass2;
uniform sampler2D pass3;
uniform sampler2D pass4;
uniform float fov;
uniform float sunElevation;
uniform float sunAzimuth;
uniform float windSpeed;
uniform float windAzimuth;
uniform float turbidity;
uniform float haze;

in vec2 v_x0;
in vec3 v_pos;

out vec4 fragColor;

// The water's colour: the sky's reflection (blurred by how much the slopes vary inside the pixel), the sun's
// glitter (the share of facets tilted just right to send the sun into the eye), the light coming up out of the
// water, light shining through the crests, foam, and haze with distance.

float fresnel(float c) { return 0.02 + 0.98 * pow(1.0 - clamp(c, 0.0, 1.0), 5.0); }
// Smith masking for a Beckmann slope spread of sigma, seen at elevation sine mu.
float lambda(float mu, float sigma) {
    float a = mu / (sigma * sqrt(max(1.0 - mu * mu, 1e-6)));
    return a >= 1.6 ? 0.0 : (1.0 - 1.259 * a + 0.396 * a * a) / (3.535 * a + 2.181 * a * a);
}

void main() {
    Cam c = readCam(pass4);
    vec3 toEye = c.pos - v_pos;
    float dist = length(toEye);
    vec3 V = toEye / dist;
    vec3 s = fromAngles(sunAzimuth, sunElevation);
    vec2 size = vec2(textureSize(pass2, 0));
    float N = cellN(size);

    // Slopes: mean and spread, from all four cascades (LEAN), plus the ripples smaller than any tile.
    // Gusts: patches of stronger and weaker wind drift over the sea, roughening the short waves more in some places
    // than others (it also hides the small tiles' repeats).
    vec2 wind = fromAngles(windAzimuth, 0.0).xz;
    float gust = 0.45 + 1.1 * fbm((v_x0 - wind * time * 4.0) / 70.0, 3, length(fwidth(v_x0)) / 70.0);
    vec2 m = vec2(0.0), var = vec2(0.0);
    for (int k = 0; k < 4; k++) {
        vec4 mo = atlasGrad(pass3, v_x0, k, N);
        float g = k >= 2 ? gust : 1.0;
        mat2 back = transpose(tileRot(k));
        m += back * mo.xy * g;
        vec2 v = max(mo.zw - mo.xy * mo.xy, 0.0) * g * g;
        vec2 cs = vec2(back[0][0], back[0][1]);   // the turn's cos and sin
        var += vec2(v.x * cs.x * cs.x + v.y * cs.y * cs.y, v.x * cs.y * cs.y + v.y * cs.x * cs.x);
    }
    var += vec2(0.0012 + 0.00025 * windSpeed);
    // Specular antialiasing: slopes that change a lot from one pixel to the next count as roughness too.
    vec2 gx = dFdx(m), gy = dFdy(m);
    var += 0.5 * (gx * gx + gy * gy);

    vec3 n = normalize(vec3(-m.x, 1.0, -m.y));
    // At a glancing angle you mostly see the sides of waves that face you (the rest are hidden behind crests), so
    // the average facet you see leans towards you: its reflection points higher, its Fresnel is lower.
    float rough = sqrt(var.x + var.y);
    vec3 Vh = normalize(vec3(V.x, 0.0, V.z) + 1e-5);
    n = normalize(n + Vh * rough * 0.9 * (1.0 - smoothstep(0.0, 0.5, V.y)));
    float NoV = max(dot(n, V), 0.0);
    float F = fresnel(NoV);
    // Rough water at a glancing angle can't show a perfect mirror: the reflection gets a little less bright.
    F *= 1.0 - 0.35 * smoothstep(0.0, 0.08, var.x + var.y) * (1.0 - NoV);

    // The sky's reflection; rays that point down would hit the next wave, so tilt them back up.
    vec3 r = reflect(-V, n);
    r.y = abs(r.y) + 0.01;
    float lod = clamp(log2(rough / 0.008), 0.0, 7.0);
    vec3 refl = textureLod(pass1, normalize(r), lod).rgb;

    // The sun's glitter: Beckmann over the slope spread (widened by the sun's own size), with Smith masking.
    vec3 sl = sunLight(s, haze);
    vec3 spec = vec3(0.0);
    if (s.y > -0.02) {
        vec3 h = normalize(s + V);
        vec2 hs = -h.xz / h.y;
        vec2 sig2 = var + 0.00002;
        vec2 d = hs - m;
        float p22 = exp(-0.5 * (d.x * d.x / sig2.x + d.y * d.y / sig2.y)) / (2.0 * PI * sqrt(sig2.x * sig2.y));
        float sigma = sqrt(0.5 * (sig2.x + sig2.y));
        float G2 = 1.0 / (1.0 + lambda(max(V.y, 0.001), sigma) + lambda(max(s.y, 0.001), sigma));
        spec = sl * fresnel(dot(h, V)) * p22 * G2 / (4.0 * pow(h.y, 4.0) * max(V.y, 0.02));
    }

    // Light from inside the water: turbid blue-green, lit by the whole sky and the sun.
    vec3 skyUp = textureLod(pass1, vec3(0.0, 1.0, 0.0), 6.0).rgb;
    vec3 body = mix(vec3(0.004, 0.022, 0.04), vec3(0.016, 0.075, 0.095), turbidity) * (sl * max(s.y, 0.0) + skyUp * 2.0);
    // Through the crests: when you look towards the sun, the tops of the waves glow green.
    float hgt = 0.0;
    for (int k = 0; k < 2; k++) hgt += atlasGrad(pass2, v_x0, k, N).y;
    vec3 sunH = normalize(vec3(s.x, 0.0, s.z));
    float back = pow(max(dot(-V, sunH), 0.0), 5.0) * smoothstep(-0.3, 1.0, hgt) * (1.0 - smoothstep(0.0, 0.5, s.y));
    body += vec3(0.02, 0.09, 0.07) * sl * back * mix(0.4, 1.0, turbidity);

    vec3 col = body * (1.0 - F) + refl * F + spec;

    // Foam: the map near the camera, the instant whitecaps further out; bubbles and streaks from noise.
    vec2 res3 = vec2(textureSize(pass4, 0));
    float M = foamM(res3), ts = FOAM_WORLD / M;
    vec2 centre = floor(c.pos.xz / ts) * ts;
    vec2 fuv = (v_x0 - centre) / ts + 0.5 * M;
    vec2 fm = texture(pass4, fuv / res3).xy;
    float inMap = sstep(0.5 * M, 0.42 * M, max(abs(fuv.x - 0.5 * M), abs(fuv.y - 0.5 * M)));
    float div = 0.0;
    for (int k = 0; k < 3; k++) div += atlasGrad(pass2, v_x0, k, N).w;
    float instant = sstep(0.35, -0.1, 1.0 + div) * 0.6;
    fm = mix(vec2(instant, instant * 0.8), fm, inMap);
    float fp = length(fwidth(v_x0));
    float bubbles = fbm(v_x0 * 6.0, 4, fp * 6.0);
    vec2 w = vec2(dot(v_x0, wind), dot(v_x0, vec2(-wind.y, wind.x)));   // along and across the wind
    float streaks = fbm(w * vec2(0.5, 2.6) + 3.1, 4, fp * 2.6);
    // Fresh foam is solid where it's thick and breaks into bubbles at its edges; old foam is a lace of streaks.
    float fine = fbm(v_x0 * 23.0, 3, fp * 23.0);
    float cover = smoothstep(0.4, 0.75, fm.x + (bubbles - 0.5) * 0.7) * (0.45 + 0.55 * fine);
    cover = max(cover, fm.y * smoothstep(0.58, 0.72, streaks * 0.6 + bubbles * 0.5) * (0.3 + 0.4 * fine));
    cover *= sstep(1.0, 0.2, fp * 0.5);
    vec3 foamCol = (skyUp * 2.0 + sl * max(s.y, 0.0) * 0.6) * 0.8;
    col = mix(col, foamCol, cover);

    // Haze: far water fades into the sky's colour just above the horizon.
    vec3 hor = textureLod(pass0, normalize(vec3(-V.x, 0.01, -V.z)), 0.0).rgb;
    col = mix(hor, col, exp(-dist * (0.2 + haze) / 30000.0));
    fragColor = vec4(col, 1.0);
}

Finish: fragment shader

#version 300 es
precision highp float;
precision highp int;

uniform vec2 resolution;
uniform int frame;
uniform sampler2D pass5;
uniform sampler2D pass6;
uniform float exposure;
uniform float bloom;
uniform float sunElevation;
uniform float sunAzimuth;
uniform float haze;

out vec4 fragColor;

// The finish: the sea over the sky, bloom (the picture's own mipmaps, blurrier and blurrier, added together), an
// AgX-style tone map (bright colours go to white instead of clipping), a slightly warm white balance, and dither.

vec3 comp(vec2 uv, float lod) {
    vec4 o = textureLod(pass6, uv, lod);
    return textureLod(pass5, uv, lod).rgb * (1.0 - o.a) + o.rgb;
}
vec3 blurred(vec2 uv, float lod) {
    vec2 px = exp2(lod) / resolution;
    return 0.25 * (comp(uv + px * vec2(0.6, 0.6), lod) + comp(uv + px * vec2(-0.6, 0.6), lod) + comp(uv + px * vec2(0.6, -0.6), lod) + comp(uv + px * vec2(-0.6, -0.6), lod));
}
// Log encoding, then an S curve in it, after pulling colours slightly towards grey so bright ones desaturate.
vec3 toneMap(vec3 c) {
    float l = dot(c, vec3(0.2126, 0.7152, 0.0722));
    c = max(mix(c, vec3(l), 0.1), 1e-10);
    vec3 x = clamp((log2(c) + 12.5) / 16.5, 0.0, 1.0);
    const float k = 7.5, mid = 0.6;
    vec3 y = 1.0 / (1.0 + exp(-k * (x - mid)));
    float lo = 1.0 / (1.0 + exp(k * mid)), hi = 1.0 / (1.0 + exp(-k * (1.0 - mid)));
    y = (y - lo) / (hi - lo);
    float ly = dot(y, vec3(0.2126, 0.7152, 0.0722));
    return clamp(mix(vec3(ly), y, 1.12), 0.0, 1.0);
}
void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    vec3 col = comp(uv, 0.0);
    vec3 b = vec3(0.0);
    float wsum = 0.0;
    for (int i = 2; i <= 7; i++) {
        float w = 1.0 / float(i);
        b += blurred(uv, float(i)) * w;
        wsum += w;
    }
    // Exposure follows the sunlight, as a camera's would: the same setting suits a sunset and noon.
    float sun = dot(sunLight(fromAngles(sunAzimuth, sunElevation), haze), vec3(0.2126, 0.7152, 0.0722));
    float auto = clamp(6.0 / (sun + 0.5), 0.25, 4.0);
    col = mix(col, b / wsum, bloom) * exposure * auto * vec3(1.03, 1.0, 0.96);
    col = toneMap(col);
    // Dither: a third of a step of noise, so gradients in the sky don't band.
    float n = fract(sin(dot(gl_FragCoord.xy + float(frame % 64) * 7.13, vec2(12.9898, 78.233))) * 43758.5453);
    fragColor = vec4(col + (n - 0.5) / 255.0, 1.0);
}

Common: shared by every pass

// Shared by every pass's fragment shader. (The ocean's vertex shader has its own copies of the few it needs.)
const float PI = 3.14159265;
const float G = 9.81;

// ---- Waves: four cascades, each a square tile that repeats: 400 m across, then 6.55 times smaller each time
// (61 m, 9.3 m, 1.4 m). 6.55 isn't a whole number, so the tiles never line up and the repeats don't show.
const float RATIO = 6.55;
float tileSize(int c) { return 400.0 / pow(RATIO, float(c)); }

// The wave atlas (Waves and Slopes passes): 2 x 2 cells of 2N x 2N texels at the bottom left. Each cell holds two
// periods of its tile, and lookups read the middle period, so filtering and mipmaps never reach a seam.
float cellN(vec2 res) { float m = min(res.x, res.y); return m >= 525.0 ? 128.0 : m >= 269.0 ? 64.0 : m >= 141.0 ? 32.0 : 16.0; }
vec2 cellOrigin(int c, float N) { return vec2(float(c & 1), float(c >> 1)) * 2.0 * N; }
// Each cascade's tile is turned to its own angle, so the repeats of different tiles never line up into a grid.
mat2 tileRot(int c) { float a = float(c) * 1.13; return mat2(cos(a), sin(a), -sin(a), cos(a)); }
vec2 atlasUV(vec2 x, int c, float N, vec2 size) { return (cellOrigin(c, N) + 0.5 * N + fract(tileRot(c) * x / tileSize(c)) * N) / size; }
// Filtered by the pixel's footprint (the mip is capped so it never mixes in a neighbouring cell).
vec4 atlasGrad(sampler2D tex, vec2 x, int c, float N) {
    vec2 size = vec2(textureSize(tex, 0));
    vec2 q = tileRot(c) * x / tileSize(c);
    vec2 gx = dFdx(q) * N, gy = dFdy(q) * N;
    float m = max(length(gx), length(gy)), cap = N / 8.0;
    if (m > cap) { gx *= cap / m; gy *= cap / m; }
    return textureGrad(tex, atlasUV(x, c, N, size), gx / size, gy / size);
}

// ---- The camera, kept by the Foam pass in its top-right texels: (yaw, pitch, drag x, drag y), then the position
// in two parts (whole 16 m steps, and the rest) because half floats can't hold a big number precisely.
struct Cam { vec3 pos; float yaw; float pitch; };
ivec2 stateTexel(vec2 res, int i) { return ivec2(int(res.x) - 1 - i, int(res.y) - 1); }
Cam readCam(sampler2D state) {
    vec2 res = vec2(textureSize(state, 0));
    vec4 a = texelFetch(state, stateTexel(res, 0), 0);
    vec4 b = texelFetch(state, stateTexel(res, 1), 0);
    vec4 f = texelFetch(state, stateTexel(res, 2), 0);
    return Cam(b.xyz + f.xyz, a.x, a.y);
}
// Columns: right, up, forward. Yaw 0 looks along -z; positive yaw turns right.
mat3 camBasis(float yaw, float pitch) {
    vec3 f = vec3(sin(yaw) * cos(pitch), sin(pitch), -cos(yaw) * cos(pitch));
    vec3 r = vec3(cos(yaw), 0.0, sin(yaw));
    return mat3(r, cross(r, f), f);
}
vec3 viewRay(Cam c, vec2 fragCoord, vec2 res, float fovDeg) {
    vec2 p = (fragCoord - 0.5 * res) / res.y;
    return normalize(camBasis(c.yaw, c.pitch) * vec3(p * 2.0 * tan(radians(fovDeg) * 0.5), 1.0));
}
// Directions from compass angles (same convention as yaw), in degrees.
vec3 fromAngles(float azimuth, float elevation) {
    float a = radians(azimuth), e = radians(elevation);
    return vec3(sin(a) * cos(e), sin(e), -cos(a) * cos(e));
}

// The foam map: M x M texels at the bottom left, FOAM_WORLD metres across, centred on the camera (snapped to whole
// texels, so it can follow the camera without smearing).
const float FOAM_WORLD = 256.0;
float foamM(vec2 res) { return exp2(floor(log2(min(min(res.x, res.y - 1.0), 1024.0)))); }

// smoothstep that also runs downhill (edge0 > edge1): GLSL leaves that case undefined.
float sstep(float e0, float e1, float x) { float t = clamp((x - e0) / (e1 - e0), 0.0, 1.0); return t * t * (3.0 - 2.0 * t); }

// ---- Hashing and noise (integer hash: lowbias32)
uint ihash(uint x) { x ^= x >> 16; x *= 0x7feb352du; x ^= x >> 15; x *= 0x846ca68bu; x ^= x >> 16; return x; }
float hash(vec2 p) { uvec2 q = uvec2(ivec2(floor(p)) + 32768); return float(ihash(q.x ^ ihash(q.y + 0x9e3779b9u))) / 4294967295.0; }
float hash3(vec3 p) { uvec3 q = uvec3(ivec3(floor(p)) + 32768); return float(ihash(q.x ^ ihash(q.y ^ ihash(q.z + 0x632be5abu)))) / 4294967295.0; }
float vnoise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0)), u.x), u.y);
}
// Fractal noise whose octaves fade out once they're smaller than the pixel (fp: the pixel's size in noise units).
float fbm(vec2 p, int octaves, float fp) {
    float s = 0.0, a = 0.5, f = 1.0;
    mat2 turn = mat2(0.8, 0.6, -0.6, 0.8) * 2.03;
    for (int i = 0; i < 8; i++) {
        if (i >= octaves) break;
        float w = sstep(1.0, 0.3, fp * f);
        s += a * mix(0.5, vnoise(p), w);
        p = turn * p + 17.31;
        a *= 0.5;
        f *= 2.03;
    }
    return s;
}

// ---- The sky: single scattering through a round atmosphere (Rayleigh air, Mie haze), the sun's light reaching
// each point found with an air-mass formula. Light arriving at the ground is SUN_E strong.
const float SUN_E = 20.0;
const float RE = 6360e3, RA = 6420e3;
const vec3 BR = vec3(5.8e-6, 13.5e-6, 33.1e-6);
float mieBeta(float haze) { return 4e-6 + 1.6e-5 * haze; }
// Relative air mass towards elevation sine mu (Kasten and Young).
float airmass(float mu) {
    float e = degrees(asin(clamp(mu, -1.0, 1.0)));
    return 1.0 / (max(mu, 0.0) + 0.50572 * pow(max(e + 6.07995, 0.6), -1.6364));
}
vec3 sunLight(vec3 s, float haze) {
    vec3 tau = BR * 8000.0 + mieBeta(haze) * 1.11 * 1200.0;
    return SUN_E * exp(-tau * airmass(s.y)) * smoothstep(-0.02, 0.01, s.y);
}
vec3 atmosphere(vec3 d, vec3 s, float haze) {
    d.y = max(d.y, 0.0);
    d = normalize(d);
    float bm = mieBeta(haze);
    vec3 o = vec3(0.0, RE + 2.0, 0.0);
    float b = dot(o, d);
    float len = -b + sqrt(b * b - dot(o, o) + RA * RA);
    vec3 sumR = vec3(0.0), sumM = vec3(0.0);
    float odR = 0.0, odM = 0.0, prev = 0.0;
    const int STEPS = 12;
    for (int i = 0; i < STEPS; i++) {
        float f = float(i + 1) / float(STEPS);
        float t = len * f * f;
        float ds = t - prev, mid = 0.5 * (t + prev);
        prev = t;
        vec3 x = o + d * mid;
        float r = length(x), h = r - RE;
        float dR = exp(-h / 8000.0) * ds, dM = exp(-h / 1200.0) * ds;
        odR += 0.5 * dR;
        odM += 0.5 * dM;
        float am = airmass(dot(x / r, s));
        vec3 tau = BR * (8000.0 * exp(-h / 8000.0) * am + odR) + bm * 1.11 * (1200.0 * exp(-h / 1200.0) * am + odM);
        vec3 T = exp(-tau);
        sumR += T * dR;
        sumM += T * dM;
        odR += 0.5 * dR;
        odM += 0.5 * dM;
    }
    float cs = dot(d, s);
    float pR = 3.0 / (16.0 * PI) * (1.0 + cs * cs);
    float g = 0.76;
    float pM = 3.0 / (8.0 * PI) * (1.0 - g * g) * (1.0 + cs * cs) / ((2.0 + g * g) * pow(1.0 + g * g - 2.0 * g * cs, 1.5));
    vec3 col = SUN_E * (sumR * BR * pR + sumM * bm * pM);
    // Light scattered more than once: a soft share of the sunlight, bluer up high, so the sky never goes black.
    vec3 sl = sunLight(s, haze);
    col += SUN_E * vec3(0.0025, 0.005, 0.010) * smoothstep(-0.12, 0.15, s.y) + sl * vec3(0.004, 0.005, 0.007);
    return col;
}

// ---- Clouds: a broken low layer of cumulus (1.5 km) under a thin fibrous high layer (6.5 km), drifting with the
// wind. Each is lit with self-shadowing towards the sun, forward scattering (silver edges against the sun) and a
// share of skylight, and fades into the haze with distance. Returns premultiplied colour and coverage.
float hg(float c, float g) { return (1.0 - g * g) / (4.0 * PI * pow(1.0 + g * g - 2.0 * g * c, 1.5)); }
float lowDensity(vec2 p, float cover, float fp) { return smoothstep(1.0 - cover, 1.0 - cover + 0.28, fbm(p, 6, fp)); }
vec4 clouds(vec3 d, vec3 s, float cover, float haze, float time, vec2 windDir, float pixelAngle) {
    if (d.y < 0.002 || cover <= 0.0) return vec4(0.0);
    vec3 sl = sunLight(s, haze) * (1.0 - 0.6 * sstep(0.3, -0.05, s.y));
    vec3 amb = sl * vec3(0.06, 0.08, 0.11) + vec3(0.03, 0.045, 0.07) * smoothstep(-0.1, 0.2, s.y) * SUN_E * 0.05;
    float cs = dot(d, s);
    float phase = mix(hg(cs, 0.55), hg(cs, -0.15), 0.35) * 4.0 * PI;
    vec2 sunH = length(s.xz) > 1e-3 ? normalize(s.xz) : vec2(0.0, -1.0);

    // Low layer
    float dist = 1500.0 / d.y;
    vec2 p = (d.xz * dist + windDir * time * 9.0) / 2600.0;
    float fp = dist * pixelAngle / d.y / 2600.0;
    float den = lowDensity(p, cover, fp);
    vec4 low = vec4(0.0);
    if (den > 0.0) {
        float sh = lowDensity(p + sunH * 0.035, cover, fp) + lowDensity(p + sunH * 0.09, cover, fp);
        float toSun = exp(-2.2 * sh) * (1.0 - exp(-den * 3.0)) * 1.4;
        vec3 col = sl * phase * toSun * 0.55 + amb * (1.3 - 0.5 * den);
        float a = 1.0 - exp(-den * 5.0 / sqrt(max(d.y, 0.03)));
        float fade = exp(-dist / (70000.0 / (0.6 + haze)));
        low = vec4(col * a * fade, a * fade);
    }
    // High layer: stretched along the wind, thin.
    float dist2 = 6500.0 / d.y;
    vec2 q = (d.xz * dist2 + windDir * time * 18.0) / 9000.0;
    q = mat2(windDir.x, windDir.y, -windDir.y, windDir.x) * q * vec2(0.35, 2.2);
    float fp2 = dist2 * pixelAngle / d.y / 9000.0 * 2.2;
    float den2 = smoothstep(0.55, 0.95, fbm(q + 3.7, 5, fp2)) * smoothstep(0.25, 0.75, cover + 0.2);
    float a2 = (1.0 - exp(-den2 * 1.1)) * exp(-dist2 / (140000.0 / (0.6 + haze)));
    vec3 col2 = (sl * phase * 0.7 + amb * 1.2) * a2;
    return vec4(low.rgb + (1.0 - low.a) * col2, low.a + (1.0 - low.a) * a2);
}

More Showpieces examples

Sanctum · Cumulus · Golden bulb · Event horizon · Storm sea