Breaking Sea Foam Web shader

breaking-sea-foam-web.glsl · 733 lines · GLSL ES 3.00 · MIT

Breaking Sea Foam WebWater & Ice
Breaking Sea Foam Web WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 733 linesSPEC GLSL ES 3.00

Seamless PNG Texture Baker
1-Click Engine Shader Export
WebGL2 .glsl
.glsl
Godot 4 .gdshader
Three.js GLSL3

Engine shader code & wrappers

Foams & Bubbles
breaking-sea-foam-web.glsl (733 lines)
#version 300 es
// Breaking Sea Foam Web (breaking-sea-foam-web.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/foams/lib/02_breaking_sea_foam_lace_web.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float), uOpen (0..1), uLightMode (0..3), uCamMode (0..1)
precision highp float;
precision highp int;

uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;

out vec4 fragColor;

const float PI = 3.14159265359;

// -----------------------------------------------------------------------------
// Hash & Noise Utilities
// -----------------------------------------------------------------------------
float hash12(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

vec2 hash22(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.xx + p3.yz) * p3.zy);
}

float vnoise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    float a = hash12(i);
    float b = hash12(i + vec2(1.0, 0.0));
    float c = hash12(i + vec2(0.0, 1.0));
    float d = hash12(i + vec2(1.0, 1.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}

float fbm(vec2 p) {
    float v = 0.0;
    float a = 0.5;
    mat2 m = mat2(0.80, -0.60, 0.60, 0.80);
    for (int i = 0; i < 4; i++) {
        v += a * vnoise(p);
        p = m * p * 2.03 + vec2(17.1, 31.7);
        a *= 0.5;
    }
    return v;
}

// True border-distance Voronoi:
// returns vec3(edgeDist, cellRadiusDist, cellIdHash)
vec3 voronoiEdge(vec2 x, float jitter) {
    vec2 n = floor(x);
    vec2 f = fract(x);

    vec2 mg = vec2(0.0);
    vec2 mr = vec2(0.0);
    float md = 8.0;
    float id = 0.0;

    for (int j = -1; j <= 1; j++) {
        for (int i = -1; i <= 1; i++) {
            vec2 g = vec2(float(i), float(j));
            vec2 o = hash22(n + g) * jitter + (1.0 - jitter) * 0.5;
            vec2 r = g + o - f;
            float d = dot(r, r);
            if (d < md) {
                md = d;
                mr = r;
                mg = g;
                id = hash12(n + g + vec2(13.7, 29.3));
            }
        }
    }

    float edge = 8.0;
    for (int j = -1; j <= 1; j++) {
        for (int i = -1; i <= 1; i++) {
            vec2 g = mg + vec2(float(i), float(j));
            vec2 o = hash22(n + g) * jitter + (1.0 - jitter) * 0.5;
            vec2 r = g + o - f;
            vec2 diff = r - mr;
            if (dot(diff, diff) > 0.00001) {
                edge = min(edge, dot(0.5 * (mr + r), normalize(diff)));
            }
        }
    }

    return vec3(edge, sqrt(md), id);
}

// Packed micro-bubble domes: returns vec3(domeHeight, bubbleId, radialDist)
vec3 microBubbles(vec2 x) {
    vec2 n = floor(x);
    vec2 f = fract(x);
    float md = 8.0;
    float id = 0.0;
    for (int j = -1; j <= 1; j++) {
        for (int i = -1; i <= 1; i++) {
            vec2 g = vec2(float(i), float(j));
            vec2 o = 0.15 + 0.70 * hash22(n + g);
            vec2 r = g + o - f;
            float d = dot(r, r);
            if (d < md) {
                md = d;
                id = hash12(n + g + vec2(5.1, 19.3));
            }
        }
    }
    float rad = sqrt(md);
    float rMax = mix(0.42, 0.58, id);
    float u = clamp(rad / rMax, 0.0, 1.0);
    float dome = sqrt(max(0.0, 1.0 - u * u));
    return vec3(dome, id, u);
}

// -----------------------------------------------------------------------------
// Breaking Sea Foam Lace Field
// -----------------------------------------------------------------------------
struct FoamSample {
    float height;      // 3D height of foam rib above water
    float density;     // 0 = open water, 1 = dense frothy white rib
    float film;        // thin gossamer translucent edge film (0..1)
    float bubbleDome;  // micro-bubble relief (0..1)
    float bubbleId;    // per-bubble variation
    float meniscus;    // subtle capillary lift of water near rib
    float waterRipple; // capillary ripple on open seawater
};

vec2 warpFoamDomain(vec2 xz) {
    float t = uTime * 0.12;
    // Coastal wave-sheared stretch along diagonal + gentle drift
    mat2 rot = mat2(0.92, -0.39, 0.39, 0.92);
    vec2 p = rot * xz;
    // Anisotropic stretch: breaking sea foam pulls into elongated lacy webs
    p.x *= 0.78;
    p.y *= 1.18;

    // Gentle fluid drift & organic curl-like swirl
    vec2 drift = vec2(t * 0.42, sin(t * 0.7 + p.x * 0.6) * 0.08);
    vec2 q = p + drift;
    float w1 = fbm(q * 1.15 + vec2(2.3, -1.4));
    float w2 = fbm(q * 1.15 + vec2(-3.1, 4.2));
    vec2 swirl = vec2(w1 - 0.5, w2 - 0.5) * 0.48;

    // Slight additional stretching with uOpen as burst windows pull filaments taut
    float openStretch = 1.0 + 0.16 * clamp(uOpen, 0.0, 1.0);
    return (q + swirl) * openStretch;
}

FoamSample evalFoamField(vec2 xz, float slabEdgeMask) {
    FoamSample fs;
    float openVal = clamp(uOpen, 0.0, 1.0);
    vec2 wp = warpFoamDomain(xz);

    // Subtle open-water capillary wavelets
    float t = uTime * 0.75;
    float rip1 = sin(wp.x * 6.5 + t) * cos(wp.y * 5.2 - t * 0.8);
    float rip2 = fbm(wp * 3.4 + vec2(t * 0.18, -t * 0.12)) - 0.5;
    fs.waterRipple = (rip1 * 0.0022 + rip2 * 0.0055) * slabEdgeMask;

    // Primary stretched cellular Voronoi lace
    vec3 v1 = voronoiEdge(wp * 2.35, 0.92);
    // Secondary finer lace filaments bridging cells
    vec3 v2 = voronoiEdge(wp * 5.10 + vec2(4.7, -2.9), 0.90);
    // Large burst-pocket modulator (tears open bigger windows as uOpen increases)
    vec3 vBurst = voronoiEdge(wp * 1.25 + vec2(-1.8, 3.4), 0.85);
    float burstNoise = fbm(wp * 1.65 + vec2(7.2, 1.9));

    // Organic thickness variation along ribs (thicker near triple junctions & foam clumps)
    float clump = fbm(wp * 2.6 - vec2(1.4, 5.1));
    float ribJitter = (clump - 0.5) * 0.032;

    // Primary rib width narrows gracefully as uOpen tears open larger burst windows
    float w1 = mix(0.115, 0.052, openVal) + ribJitter;
    // Smooth Plateau-like webbing at Voronoi triple junctions
    float nodeBoost = smoothstep(0.30, 0.0, v1.x) * smoothstep(0.36, 0.80, v1.y) * mix(0.065, 0.032, openVal);
    float d1 = v1.x - (w1 + nodeBoost);

    // Secondary lace web: selectively ruptures inside burst pockets when uOpen rises
    float burstMask = smoothstep(
        mix(0.18, 0.44, openVal),
        mix(0.55, 0.82, openVal),
        vBurst.y * 0.65 + burstNoise * 0.55
    );
    // Primary filaments also selectively tear in the largest burst windows at high uOpen
    float primaryTear = smoothstep(
        mix(0.96, 0.52, openVal),
        mix(1.18, 0.86, openVal),
        vBurst.y * 0.72 + (1.0 - clump) * 0.42
    );

    // Smooth, soft-edged rib profiles (no harsh step cliffs)
    float rib1 = smoothstep(0.075, -0.045, d1) * (1.0 - 0.85 * primaryTear);

    float w2 = mix(0.072, 0.032, openVal) + ribJitter * 0.4;
    float rib2 = smoothstep(0.065, -0.035, v2.x - w2) * (1.0 - 0.92 * burstMask);

    // Delicate gossamer micro-strands near primary ribs
    float rawWeb = max(rib1, rib2 * 0.78);

    // Packed micro-bubbles for shading relief & delicate beaded margins
    vec3 mb1 = microBubbles(wp * 17.0);
    vec3 mb2 = microBubbles(wp * 34.0 + vec2(11.3, 7.1));
    float bubbleField = mix(mb1.x, mb2.x, 0.42);

    // Gentle organic edge modulation (kept smooth for Lipschitz safety)
    float softBreakup = (clump - 0.5) * 0.14 + (mb1.x - 0.45) * 0.07;
    float denseWeb = smoothstep(0.26, 0.78, rawWeb + softBreakup);
    float thinFilm = smoothstep(0.06, 0.50, rawWeb + softBreakup * 0.5) * (1.0 - denseWeb * 0.82);

    // Taper cleanly inside the bevel-cut specimen slab perimeter
    denseWeb *= slabEdgeMask;
    thinFilm *= slabEdgeMask;

    // Low-profile, C1-smooth 3D foam heightfield (strictly Lipschitz-safe, no vertical cliffs)
    float smoothRib = denseWeb * denseWeb * (3.0 - 2.0 * denseWeb);
    float baseLift = smoothRib * (0.011 + 0.006 * clump + 0.004 * rib1);
    float filmLift = thinFilm * 0.0022;

    // Subtle capillary water meniscus lifting gently around foam ribs
    float meniscus = smoothstep(0.0, 0.42, rawWeb) * (1.0 - denseWeb) * 0.0025 * slabEdgeMask;

    fs.height = baseLift + filmLift;
    fs.density = denseWeb;
    fs.film = thinFilm;
    fs.bubbleDome = bubbleField;
    fs.bubbleId = mix(mb1.y, mb2.y, 0.35);
    fs.meniscus = meniscus;
    return fs;
}

// -----------------------------------------------------------------------------
// SDF Geometry: Bevel-Cut Seawater Specimen Slab + 3D Foam Lace Web
// -----------------------------------------------------------------------------
const vec3 SLAB_HALF = vec3(1.06, 0.22, 0.82);
const float SLAB_TOP = 0.44;

// Chamfered / bevel-cut rectangular museum specimen slab resting on y = 0
float sdBevelSlab(vec3 p) {
    vec3 q = p - vec3(0.0, SLAB_HALF.y, 0.0);
    vec3 a = abs(q) - SLAB_HALF;

    // Base rounded box
    float r = 0.032;
    float dBox = length(max(a + r, 0.0)) + min(max(a.x + r, max(a.y + r, a.z + r)), 0.0) - r;

    // Crisp 45-degree museum top bevel cut along perimeter
    float bevelTopX = (abs(q.x) - (SLAB_HALF.x - 0.055) + (q.y - (SLAB_HALF.y - 0.055))) * 0.70710678;
    float bevelTopZ = (abs(q.z) - (SLAB_HALF.z - 0.055) + (q.y - (SLAB_HALF.y - 0.055))) * 0.70710678;
    float bevelCorner = (abs(q.x) + abs(q.z) - (SLAB_HALF.x + SLAB_HALF.z - 0.14)) * 0.70710678;

    float d = max(dBox, max(bevelTopX, bevelTopZ));
    d = max(d, bevelCorner);
    return d;
}

float slabTopMask(vec2 xz) {
    vec2 d = abs(xz) - (SLAB_HALF.xz - vec2(0.085));
    float edge = max(d.x, d.y);
    float corner = (abs(xz.x) + abs(xz.y) - (SLAB_HALF.x + SLAB_HALF.z - 0.20)) * 0.70710678;
    float m = max(edge, corner);
    return smoothstep(0.015, -0.075, m);
}

float mapHero(vec3 p) {
    float dSlab = sdBevelSlab(p);

    // Bound foam evaluation strictly near the top face of the slab for speed & safety
    if (dSlab < 0.12 && p.y > SLAB_TOP - 0.10) {
        float mask = slabTopMask(p.xz);
        if (mask > 0.001) {
            FoamSample fs = evalFoamField(p.xz, mask);
            float disp = fs.height + fs.meniscus + fs.waterRipple;
            // Blend displacement smoothly onto the top plane of the bevel slab
            float topInfluence = smoothstep(SLAB_TOP - 0.07, SLAB_TOP - 0.005, p.y);
            dSlab -= disp * topInfluence * 0.85;
        }
    }
    return dSlab;
}

float mapScene(vec3 p) {
    return min(p.y, mapHero(p));
}

// Zero-bug rule (1): Unit-weight tetrahedral normals
vec3 calcNormalHero(vec3 p) {
    const vec2 k = vec2(1.0, -1.0);
    const float e = 0.0009;
    return normalize(
        k.xyy * mapHero(p + k.xyy * e) +
        k.yyx * mapHero(p + k.yyx * e) +
        k.yxy * mapHero(p + k.yxy * e) +
        k.xxx * mapHero(p + k.xxx * e)
    );
}

// Zero-bug rule (3): Clean hero-only soft shadow (no ph, bias p+N*0.014, tMin=0.025, step max(h,0.015))
float calcSoftShadow(vec3 ro, vec3 rd, float tMax, float k) {
    float res = 1.0;
    float t = 0.025;
    for (int i = 0; i < 64; i++) {
        float h = mapHero(ro + rd * t);
        if (h < 0.0005) return 0.0;
        res = min(res, k * h / t);
        t += max(h, 0.015);
        if (t > tMax) break;
    }
    return clamp(res, 0.0, 1.0);
}

float calcAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 1; i <= 5; i++) {
        float h = 0.012 + 0.042 * float(i);
        float d = mapScene(p + n * h);
        occ += (h - d) * sca;
        sca *= 0.72;
    }
    return clamp(1.0 - 2.4 * occ, 0.12, 1.0);
}

// -----------------------------------------------------------------------------
// Studio Lighting & Environment (Softboxes + Negative-Fill Flags)
// -----------------------------------------------------------------------------
struct LightRig {
    vec3 keyDir;
    vec3 keyCol;
    vec3 fillDir;
    vec3 fillCol;
    vec3 rimDir;
    vec3 rimCol;
    float envGain;
};

LightRig getLightRig() {
    LightRig rig;
    if (uLightMode == 1) {
        // Pure neutral grayscale studio rig
        rig.keyDir  = normalize(vec3(-0.62, 0.78, 0.45));
        rig.keyCol  = vec3(2.35);
        rig.fillDir = normalize(vec3(0.75, 0.48, 0.35));
        rig.fillCol = vec3(0.75);
        rig.rimDir  = normalize(vec3(0.32, 0.55, -0.82));
        rig.rimCol  = vec3(1.45);
        rig.envGain = 1.0;
    } else if (uLightMode == 2) {
        // Grazing raking light skimming across foam lace relief
        rig.keyDir  = normalize(vec3(-0.92, 0.25, 0.28));
        rig.keyCol  = vec3(3.10, 2.95, 2.75);
        rig.fillDir = normalize(vec3(0.70, 0.55, 0.45));
        rig.fillCol = vec3(0.50, 0.58, 0.68);
        rig.rimDir  = normalize(vec3(0.82, 0.24, -0.52));
        rig.rimCol  = vec3(1.70, 1.85, 2.05);
        rig.envGain = 0.78;
    } else if (uLightMode == 3) {
        // Backlit translucent seawater & glowing sea foam ribs
        rig.keyDir  = normalize(vec3(0.18, 0.44, -0.88));
        rig.keyCol  = vec3(2.90, 3.05, 3.20);
        rig.fillDir = normalize(vec3(-0.65, 0.72, 0.42));
        rig.fillCol = vec3(0.68, 0.74, 0.80);
        rig.rimDir  = normalize(vec3(-0.45, 0.38, -0.80));
        rig.rimCol  = vec3(1.90, 2.05, 2.15);
        rig.envGain = 0.85;
    } else {
        // Mode 0: Hero museum studio lighting
        rig.keyDir  = normalize(vec3(-0.58, 0.80, 0.46));
        rig.keyCol  = vec3(2.45, 2.40, 2.32);
        rig.fillDir = normalize(vec3(0.76, 0.50, 0.32));
        rig.fillCol = vec3(0.62, 0.72, 0.84);
        rig.rimDir  = normalize(vec3(0.38, 0.54, -0.78));
        rig.rimCol  = vec3(1.55, 1.72, 1.92);
        rig.envGain = 1.0;
    }
    return rig;
}

// Studio cyclorama sky with crisp overhead/side softboxes + dark negative-fill flags
vec3 sampleStudioEnv(vec3 rd, LightRig rig) {
    float y = clamp(rd.y, -1.0, 1.0);
    // Clean studio cyclorama gradient
    vec3 floorBounce = vec3(0.10, 0.11, 0.12);
    vec3 horizonCol  = vec3(0.24, 0.26, 0.29);
    vec3 zenithCol   = vec3(0.34, 0.37, 0.42);
    if (uLightMode == 1) {
        floorBounce = vec3(0.11);
        horizonCol  = vec3(0.25);
        zenithCol   = vec3(0.36);
    }

    vec3 env = mix(horizonCol, zenithCol, smoothstep(0.0, 0.75, max(y, 0.0)));
    env = mix(env, floorBounce, smoothstep(0.0, -0.45, min(y, 0.0)));

    // Overhead rectangular studio softbox
    vec2 ov = rd.xz / max(rd.y, 0.12);
    float overheadBox =
        smoothstep(0.95, 0.72, abs(ov.x + 0.18)) *
        smoothstep(0.75, 0.52, abs(ov.y - 0.10)) *
        step(0.12, rd.y);
    env += vec3(1.35, 1.38, 1.42) * overheadBox;

    // Key softbox panel aligned with keyDir
    float keyAlign = dot(rd, rig.keyDir);
    float keyBox = smoothstep(0.86, 0.955, keyAlign);
    env += rig.keyCol * 0.92 * keyBox;

    // Rim strip softbox aligned with rimDir
    float rimAlign = dot(rd, rig.rimDir);
    float rimStrip = smoothstep(0.90, 0.975, rimAlign);
    env += rig.rimCol * 0.95 * rimStrip;

    // Negative-fill studio flags (black velvet absorbers at sides for crisp contrast on wet water)
    float negFlagLeft  = smoothstep(0.68, 0.88, dot(rd, normalize(vec3(-0.92, 0.18, -0.35))));
    float negFlagRight = smoothstep(0.72, 0.90, dot(rd, normalize(vec3(0.88, 0.14, 0.45))));
    float negMask = clamp(max(negFlagLeft, negFlagRight), 0.0, 1.0);
    env *= mix(1.0, 0.16, negMask);

    env *= rig.envGain;
    if (uLightMode == 1) {
        float g = dot(env, vec3(0.2126, 0.7152, 0.0722));
        env = vec3(g);
    }
    return env;
}

// -----------------------------------------------------------------------------
// PBR & Optical Helpers
// -----------------------------------------------------------------------------
float D_GGX(float NoH, float a) {
    float a2 = a * a;
    float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
    return a2 / (PI * d * d + 1e-6);
}

float G_SchlickGGX(float NoV, float NoL, float rough) {
    float r = rough + 1.0;
    float k = (r * r) / 8.0;
    float gv = NoV / (NoV * (1.0 - k) + k + 1e-5);
    float gl = NoL / (NoL * (1.0 - k) + k + 1e-5);
    return gv * gl;
}

vec3 F_Schlick(float VoH, vec3 F0) {
    return F0 + (1.0 - F0) * pow(clamp(1.0 - VoH, 0.0, 1.0), 5.0);
}

vec3 evalSpec(vec3 N, vec3 V, vec3 L, vec3 lightCol, float rough, vec3 F0) {
    vec3 H = normalize(V + L);
    float NoL = max(dot(N, L), 0.0);
    float NoV = max(dot(N, V), 0.001);
    float NoH = max(dot(N, H), 0.0);
    float VoH = max(dot(V, H), 0.0);
    float a = max(rough * rough, 0.002);
    float D = D_GGX(NoH, a);
    float G = G_SchlickGGX(NoV, NoL, rough);
    vec3 F = F_Schlick(VoH, F0);
    return (D * G * F) / (4.0 * NoV * NoL + 1e-4) * NoL * lightCol;
}

// ACES Filmic Tone Mapping
vec3 acesToneMap(vec3 x) {
    const float a = 2.51;
    const float b = 0.03;
    const float c = 2.43;
    const float d = 0.59;
    const float e = 0.14;
    return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}

// -----------------------------------------------------------------------------
// Shading: Studio Bench & Dark Seawater + 3D Breaking Sea Foam Specimen
// -----------------------------------------------------------------------------
vec3 shadeBench(vec3 p, vec3 V, LightRig rig) {
    vec3 N = vec3(0.0, 1.0, 0.0);

    // Subtle dark architectural slate studio bench texture
    float grain1 = fbm(p.xz * 2.4);
    float grain2 = vnoise(p.xz * 28.0);
    float radial = exp(-0.085 * dot(p.xz, p.xz));
    vec3 albedo = mix(vec3(0.055, 0.062, 0.072), vec3(0.115, 0.125, 0.138), grain1 * 0.55 + grain2 * 0.12 + radial * 0.38);
    if (uLightMode == 1) {
        albedo = vec3(dot(albedo, vec3(0.2126, 0.7152, 0.0722)));
    }

    float shKey = calcSoftShadow(p + N * 0.014, rig.keyDir, 5.0, 9.0);
    float shRim = calcSoftShadow(p + N * 0.014, rig.rimDir, 5.0, 8.0);
    float ao = calcAO(p, N);

    // Subtle transmitted teal caustic glow right near the translucent slab perimeter
    float dSlabXZ = length(max(abs(p.xz) - SLAB_HALF.xz, 0.0));
    float causticBand = exp(-dSlabXZ * 5.0) * (1.0 - shKey * 0.55);
    float causticPat = smoothstep(0.35, 0.85, fbm(p.xz * 5.0 + vec2(uTime * 0.15, -uTime * 0.1)));
    vec3 causticCol = vec3(0.04, 0.16, 0.19) * causticBand * (0.45 + 0.55 * causticPat);
    if (uLightMode == 1) {
        causticCol = vec3(dot(causticCol, vec3(0.2126, 0.7152, 0.0722)));
    }

    vec3 diff = vec3(0.0);
    diff += albedo / PI * rig.keyCol * max(dot(N, rig.keyDir), 0.0) * shKey;
    diff += albedo / PI * rig.fillCol * max(dot(N, rig.fillDir), 0.0) * ao;
    diff += albedo / PI * rig.rimCol * max(dot(N, rig.rimDir), 0.0) * (0.25 + 0.75 * shRim);

    // Soft studio ambient bounce (modulated by contact AO for rich grounding shadow)
    vec3 envDiff = sampleStudioEnv(N, rig) * albedo * 0.30 * ao;

    // Subtle broad studio sheen on the dark slate bench
    vec3 R = reflect(-V, N);
    float fres = pow(1.0 - max(dot(N, V), 0.0), 5.0);
    vec3 envSpec = sampleStudioEnv(R, rig) * (0.018 + 0.06 * fres) * ao;
    vec3 spec = evalSpec(N, V, rig.keyDir, rig.keyCol, 0.44, vec3(0.025)) * shKey;

    return diff + envDiff + envSpec + spec + causticCol;
}

vec3 shadeSpecimen(vec3 p, vec3 N, vec3 V, LightRig rig) {
    float mask = slabTopMask(p.xz);
    float isTop = smoothstep(0.35, 0.82, N.y) * smoothstep(SLAB_TOP - 0.09, SLAB_TOP - 0.01, p.y);
    FoamSample fs = evalFoamField(p.xz, mask * isTop);

    // Perturb normal slightly for crisp micro-bubble specular glints on foam ribs
    vec3 Nfoam = N;
    if (fs.density + fs.film > 0.05) {
        float eps = 0.004;
        float hC = fs.bubbleDome;
        float hX = evalFoamField(p.xz + vec2(eps, 0.0), mask * isTop).bubbleDome;
        float hZ = evalFoamField(p.xz + vec2(0.0, eps), mask * isTop).bubbleDome;
        vec3 bGrad = vec3(hC - hX, 0.0, hC - hZ) / eps;
        Nfoam = normalize(N + bGrad * (0.14 * (fs.density + fs.film * 0.5)));
    }

    vec3 Nwater = N;
    float foamCoverage = clamp(fs.density + fs.film * 0.45, 0.0, 1.0);
    vec3 Nshade = normalize(mix(Nwater, Nfoam, foamCoverage));

    // Shadows & Ambient Occlusion (zero-bug rule 3: bias p + N * 0.014)
    float shKey = calcSoftShadow(p + N * 0.014, rig.keyDir, 4.0, 12.0);
    float shRim = calcSoftShadow(p + N * 0.014, rig.rimDir, 4.0, 10.0);
    float ao = calcAO(p, N);

    // -------------------------------------------------------------------------
    // 1. Dark Translucent Coastal Seawater Base (Windows & Bevel-Cut Sides)
    // -------------------------------------------------------------------------
    vec3 R_refr = refract(-V, Nwater, 1.0 / 1.333);
    // Estimate optical path through slab down to bench floor y = 0
    float tFloor = (R_refr.y < -0.02) ? (p.y / (-R_refr.y)) : 1.2;
    tFloor = clamp(tFloor, 0.08, 1.8);

    // Refracted bench floor & subtle underwater caustics visible through open-water windows
    vec2 floorHit = p.xz + R_refr.xz * tFloor;
    float inSlabFootprint = smoothstep(0.15, -0.05, max(abs(floorHit.x) - SLAB_HALF.x, abs(floorHit.y) - SLAB_HALF.z));
    float underCaustic = fbm(floorHit * 4.8 + vec2(uTime * 0.22, -uTime * 0.16));
    underCaustic = smoothstep(0.42, 0.86, underCaustic);

    // Submerged micro-bubble veil drifting just below the foam ribs
    vec2 subPos = warpFoamDomain(p.xz + R_refr.xz * 0.09);
    float subVeil = fbm(subPos * 6.5) * smoothstep(0.0, 0.65, fs.film + fs.meniscus * 80.0);

    // Beer-Lambert coastal seawater absorption
    vec3 sigma = vec3(3.10, 1.25, 0.98);
    vec3 transmittance = exp(-sigma * tFloor * 1.15);

    vec3 floorUnder = vec3(0.08, 0.14, 0.16) * (0.45 + 0.55 * inSlabFootprint)
                    + vec3(0.06, 0.24, 0.28) * underCaustic * (0.35 + 0.65 * shKey);
    vec3 deepScatter = vec3(0.015, 0.072, 0.095) * (0.55 + 0.45 * rig.envGain);

    // Forward/backlit transmission glow through the translucent bevel edges & slab body
    float backDot = pow(clamp(dot(-V, rig.keyDir), 0.0, 1.0), 3.0);
    float rimBack = pow(clamp(dot(-V, rig.rimDir), 0.0, 1.0), 3.0);
    float edgeThin = smoothstep(0.28, 0.02, p.y) + (1.0 - isTop) * 0.75;
    vec3 waterBackGlow = vec3(0.03, 0.20, 0.24) * (backDot * rig.keyCol + rimBack * rig.rimCol * 0.7) * (0.28 + 0.45 * edgeThin);

    vec3 waterBody = deepScatter + floorUnder * transmittance + waterBackGlow + vec3(0.08, 0.22, 0.25) * subVeil * 0.35;

    // Crisp Fresnel mirror reflection on dark open-water windows
    float NoV_w = max(dot(Nwater, V), 0.001);
    float fresW = 0.02 + 0.98 * pow(1.0 - NoV_w, 5.0);
    vec3 envReflW = sampleStudioEnv(reflect(-V, Nwater), rig);
    vec3 waterSpec =
        evalSpec(Nwater, V, rig.keyDir, rig.keyCol, 0.045, vec3(0.02)) * shKey +
        evalSpec(Nwater, V, rig.rimDir, rig.rimCol, 0.065, vec3(0.02)) * shRim +
        evalSpec(Nwater, V, rig.fillDir, rig.fillCol, 0.10, vec3(0.02));

    vec3 waterCol = waterBody * (1.0 - fresW * 0.75) + envReflW * fresW * 0.92 + waterSpec;

    // -------------------------------------------------------------------------
    // 2. 3D Frothy Breaking Sea Foam Lace Ribs
    // -------------------------------------------------------------------------
    // Albedo transitions from translucent milky-aqua at thin gossamer edges to bright sea-salt white at thick ribs
    vec3 foamCoreCol = vec3(0.88, 0.91, 0.93);
    vec3 foamEdgeCol = vec3(0.36, 0.56, 0.61);
    vec3 foamAlbedo  = mix(foamEdgeCol, foamCoreCol, smoothstep(0.15, 0.85, fs.density));

    // Micro-crevice darkening between individual packed bubbles
    float bubbleAO = mix(0.76, 1.04, fs.bubbleDome);
    float totalAO = ao * bubbleAO;

    // Soft wrap diffuse + subsurface scattering through airy micro-bubble matrix
    float wrapKey = clamp((dot(Nshade, rig.keyDir) + 0.42) / 1.42, 0.0, 1.0);
    float wrapFill = clamp((dot(Nshade, rig.fillDir) + 0.50) / 1.50, 0.0, 1.0);
    float wrapRim = clamp((dot(Nshade, rig.rimDir) + 0.38) / 1.38, 0.0, 1.0);

    // Subsurface transmission glow when backlit or side-lit
    float sssKey = pow(clamp(dot(V, -rig.keyDir + Nshade * 0.35), 0.0, 1.0), 2.5);
    float sssRim = pow(clamp(dot(V, -rig.rimDir + Nshade * 0.35), 0.0, 1.0), 2.5);
    vec3 foamSSS = vec3(0.68, 0.84, 0.88) * (sssKey * rig.keyCol * 0.28 + sssRim * rig.rimCol * 0.32) * (0.45 + 0.55 * fs.film);

    vec3 foamDiff =
        foamAlbedo / PI * (
            rig.keyCol * wrapKey * (0.28 + 0.72 * shKey) +
            rig.fillCol * wrapFill * totalAO +
            rig.rimCol * wrapRim * (0.35 + 0.65 * shRim)
        ) +
        foamAlbedo * sampleStudioEnv(Nshade, rig) * 0.34 * totalAO +
        foamSSS;

    // Wet specular glints along frothy ribs & micro-bubble domes
    float wetRough = mix(0.14, 0.06, fs.bubbleDome);
    vec3 wetSpec =
        evalSpec(Nfoam, V, rig.keyDir, rig.keyCol, wetRough, vec3(0.025)) * shKey +
        evalSpec(Nfoam, V, rig.rimDir, rig.rimCol, wetRough * 1.15, vec3(0.025)) * shRim +
        evalSpec(Nfoam, V, rig.fillDir, rig.fillCol, 0.18, vec3(0.025));

    // Subtle clearcoat studio reflection on wet bubble film + restrained thin-film sheen
    float NoV_f = max(dot(Nfoam, V), 0.001);
    float fresF = 0.025 + 0.975 * pow(1.0 - NoV_f, 5.0);
    vec3 irid = 0.5 + 0.5 * cos(6.28318 * (vec3(0.0, 0.33, 0.67) + fs.bubbleId * 0.6 + NoV_f * 1.4));
    vec3 wetEnv = sampleStudioEnv(reflect(-V, Nfoam), rig) * fresF * (0.55 + 0.20 * irid * (1.0 - fs.density * 0.5));

    vec3 foamCol = foamDiff + wetSpec * (0.75 + 0.55 * fs.bubbleDome) + wetEnv;

    // Composite frothy lace & gossamer film over the dark translucent seawater slab
    float mixFactor = clamp(fs.density + fs.film * 0.55, 0.0, 1.0);
    vec3 col = mix(waterCol, foamCol, mixFactor);

    if (uLightMode == 1) {
        float g = dot(col, vec3(0.2126, 0.7152, 0.0722));
        col = vec3(g);
    }
    return col;
}

// -----------------------------------------------------------------------------
// Raymarching & Camera
// -----------------------------------------------------------------------------
void getCamera(out vec3 ro, out vec3 ta, out float fov) {
    if (uCamMode == 1) {
        // ~2.5x Macro inspection of lacy foam filaments & dark open-water windows
        ro = vec3(0.82, 1.12, 1.04);
        ta = vec3(0.02, 0.42, 0.02);
        fov = 0.46; // ~26 deg
    } else {
        // Hero 3/4 museum specimen presentation framing full bevel-cut slab & bench shadow
        ro = vec3(2.95, 2.38, 3.68);
        ta = vec3(0.0, 0.18, 0.0);
        fov = 0.62; // ~35.5 deg
    }
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / uResolution.y;

    vec3 ro, ta;
    float fov;
    getCamera(ro, ta, fov);

    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(uu * uv.x + vv * uv.y + ww / tan(fov * 0.5));

    LightRig rig = getLightRig();

    // Lipschitz-safe raymarch with gentle step factor near the displaced foam surface
    float t = 0.0;
    float tMax = 20.0;
    bool hit = false;
    for (int i = 0; i < 180; i++) {
        vec3 p = ro + rd * t;
        float d = mapScene(p);
        if (d < 0.0004) {
            hit = true;
            break;
        }
        float stepScale = (d < 0.14 && p.y > SLAB_TOP - 0.12) ? 0.45 : 0.70;
        t += d * stepScale;
        if (t > tMax) break;
    }

    vec3 col;
    if (hit) {
        vec3 p = ro + rd * t;
        float dHero = mapHero(p);
        // Zero-bug rule (2): exact bench classification
        bool isBench = (p.y <= dHero + 0.001);

        if (isBench) {
            col = shadeBench(p, -rd, rig);
            // Smooth studio cyclorama horizon blend for distant bench
            float fog = 1.0 - exp(-0.018 * t * t);
            col = mix(col, sampleStudioEnv(rd, rig) * 0.85, clamp(fog, 0.0, 1.0));
        } else {
            vec3 N = calcNormalHero(p);
            col = shadeSpecimen(p, N, -rd, rig);
        }
    } else {
        col = sampleStudioEnv(rd, rig) * 0.85;
    }

    // Gentle exposure calibration + ACES Filmic tone mapping
    col *= 1.05;
    col = acesToneMap(col);

    // Subtle studio vignette
    vec2 q = gl_FragCoord.xy / uResolution;
    float vig = 0.90 + 0.10 * pow(16.0 * q.x * q.y * (1.0 - q.x) * (1.0 - q.y), 0.18);
    col *= vig;

    // Enforce strict achromatic output in uLightMode == 1
    if (uLightMode == 1) {
        float g = dot(col, vec3(0.2126, 0.7152, 0.0722));
        col = vec3(g);
    }

    // sRGB gamma encoding
    col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2));
    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierbreaking-sea-foam-web
CategoryWater & Ice Shaders · Foams & Bubbles
Upstream studyfoams/lib/02_breaking_sea_foam_lace_web.glsl (commit 6707823)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size733 lines · 27.6 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT water sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Voronoi/Worley cellular fields (Voronoi 1908; Worley 1996); GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation

Adapted upstream mathematical helpers

David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Inigo Quilez, tetrahedral SDF normal

All water & ice shaders