Ocean

glint · Examples · Tutorials · Gallery

A raymarched sea of octaved choppy waves with Fresnel sky reflection, subsurface green and foam; the bass whips up the swell.

Audio-reactive: it moves with music from a microphone, line-in or a file.

Ocean: an audio-reactive 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

One pass, in GLSL.

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform float choppy;
uniform float height;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float noise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    return -1.0 + 2.0 * mix(mix(hash(i), hash(i + vec2(1, 0)), u.x), mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), u.x), u.y);
}
// One octave of sharp-crested waves.
float octave(vec2 uv, float ch) {
    uv += noise(uv);
    vec2 wv = 1.0 - abs(sin(uv));
    vec2 swv = abs(cos(uv));
    wv = mix(wv, swv, wv);
    return pow(1.0 - pow(wv.x * wv.y, 0.65), ch);
}
float sea(vec3 p, int octaves) {
    float freq = 0.16, amp = height * (1.0 + spectrum.x), ch = choppy;
    vec2 uv = p.xz;
    uv.x *= 0.75;
    float h = 0.0;
    mat2 m = mat2(1.6, 1.2, -1.2, 1.6);
    float t = time * 0.8 + 1.0;
    for (int i = 0; i < 5; i++) {
        if (i >= octaves) break;
        float d = octave((uv + t) * freq, ch) + octave((uv - t) * freq, ch);
        h += d * amp;
        uv *= m;
        freq *= 1.9;
        amp *= 0.22;
        ch = mix(ch, 1.0, 0.2);
    }
    return p.y - h;
}
vec3 skyColor(vec3 e) {
    e.y = (max(e.y, 0.0) * 0.8 + 0.2) * 0.8;
    return vec3(pow(1.0 - e.y, 2.0), 1.0 - e.y, 0.6 + (1.0 - e.y) * 0.4) * 1.1;
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float yaw = (mouse.x - 0.5) * 2.0 + time * 0.05;
    vec3 ro = vec3(0.0, 3.5, time * 5.0);
    vec3 fw = normalize(vec3(sin(yaw), -0.15 + (mouse.y - 0.5) * 0.5, cos(yaw)));
    vec3 rt = normalize(cross(fw, vec3(0, 1, 0))), up = cross(rt, fw);
    vec3 rd = normalize(uv.x * rt + uv.y * up + 1.5 * fw);

    // Bisection-ish march on the heightfield.
    float tm = 0.0, tx = 1000.0;
    float hx = sea(ro + rd * tx, 3);
    vec3 col;
    if (hx > 0.0) col = skyColor(rd);
    else {
        float hm = sea(ro, 3), tmid = 0.0;
        for (int i = 0; i < 10; i++) {
            tmid = mix(tm, tx, hm / (hm - hx));
            float hmid = sea(ro + rd * tmid, 3);
            if (hmid < 0.0) { tx = tmid; hx = hmid; } else { tm = tmid; hm = hmid; }
        }
        vec3 p = ro + rd * tmid;
        float eps = 0.001 * tmid;
        vec3 n = normalize(vec3(sea(p + vec3(eps, 0, 0), 5) - sea(p, 5), eps, sea(p + vec3(0, 0, eps), 5) - sea(p, 5)));
        vec3 light = normalize(vec3(0.0, 1.0, 0.8));
        float fresnel = pow(clamp(1.0 - dot(n, -rd), 0.0, 1.0), 3.0) * 0.65;
        vec3 reflected = skyColor(reflect(rd, n));
        vec3 base = vec3(0.0, 0.09, 0.18);
        vec3 water = vec3(0.8, 0.9, 0.6) * 0.6;
        vec3 refracted = base + pow(dot(n, light) * 0.4 + 0.6, 80.0) * water * 0.12;
        col = mix(refracted, reflected, fresnel);
        col += water * (p.y - height) * 0.18 * max(1.0 - length(p - ro) * 0.001, 0.0);
        col += pow(max(dot(reflect(rd, n), light), 0.0), 60.0) * vec3(1.0);
        col += vec3(0.9) * smoothstep(0.55, 0.9, p.y / (height * 2.0)) * 0.25;   // foam on the crests
        col = mix(col, skyColor(rd), smoothstep(0.0, -0.02, rd.y) * 0.0 + 1.0 - exp(-0.0005 * tmid * tmid * 0.01));
    }
    fragColor = vec4(pow(col, vec3(0.65)), 1.0);
}

More Showpieces examples

Prism · Menger sponge · Mandelbulb · Volumetric clouds · KIFS tunnel · Truchet weave · Voronoi crystals · Domain warping