#version 300 es
// Vanta Liquid. Five drops of black liquid glass, ray-marched in 3D, merging
// and parting as they orbit. The surface is a mirror with a thin-film coat, so
// it shows the studio it sits in: one long cyan softbox overhead (the Vanta
// light), a violet kicker low right, and nothing else. Everything the lights
// miss stays true black, so the AMOLED keeps most pixels off.
//
// This file is shared verbatim with design/render (WebGL2), so the browser
// render is the wallpaper, not a picture of it.
precision highp float;

uniform vec2 uRes;        // render-target size in px (half the screen)
uniform float uTime;      // seconds, wrapped on the CPU
uniform float uIntensity; // 0..1
uniform float uDay;       // 0 = deep night, 1 = midday
uniform float uWake;      // 0..1 progress of the wake surge, <0 = idle
uniform vec2 uOffset;     // parallax: home page + device tilt, uv units

out vec4 fragColor;

const vec3 CYAN = vec3(0.208, 0.878, 1.0);
const vec3 VIOLET = vec3(0.47, 0.33, 1.0);
const vec3 ICE = vec3(0.80, 0.95, 1.0);

float hash(vec2 p) {
    p = fract(p * vec2(123.34, 456.21));
    p += dot(p, p + 45.32);
    return fract(p.x * p.y);
}

float smin(float a, float b, float k) {
    float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
    return mix(b, a, h) - k * h * (1.0 - h);
}

vec3 drop(int i, float t) {
    float f = float(i);
    return vec3(
        sin(t * 0.19 + f * 1.71) * 1.05,
        cos(t * 0.15 + f * 2.31) * 1.45 + 0.55,
        sin(t * 0.11 + f * 0.93) * 0.70
    );
}

float map(vec3 p, float t, float surge) {
    float d = 1e5;
    for (int i = 0; i < 5; i++) {
        float f = float(i);
        float r = 0.30 + 0.09 * sin(t * 0.23 + f * 2.7) + 0.08 * surge;
        d = smin(d, length(p - drop(i, t)) - r, 0.62);
    }
    // Surface tension ripple: tiny, slow, only enough to break perfect spheres.
    d += 0.012 * sin(p.x * 7.0 + t * 0.9) * sin(p.y * 6.0 - t * 0.7) * sin(p.z * 5.0 + t * 0.5);
    return d;
}

vec3 normalAt(vec3 p, float t, float surge) {
    const vec2 k = vec2(1.0, -1.0);
    const float e = 0.0015;
    return normalize(
        k.xyy * map(p + k.xyy * e, t, surge) +
        k.yyx * map(p + k.yyx * e, t, surge) +
        k.yxy * map(p + k.yxy * e, t, surge) +
        k.xxx * map(p + k.xxx * e, t, surge));
}

vec3 studio(vec3 d, float day) {
    vec3 key = mix(CYAN, ICE, 0.35 * day);
    // Long softbox overhead, slightly left: a hard-edged strip, not a blob,
    // which is what makes the highlights read as glass instead of plastic.
    float strip = smoothstep(0.62, 0.70, d.y) * smoothstep(0.85, 0.55, abs(d.x + 0.25 - d.z * 0.1));
    vec3 c = key * strip * 3.6;
    c += key * pow(max(d.y, 0.0), 5.0) * 0.35;
    float kick = pow(max(dot(d, normalize(vec3(0.85, -0.35, 0.40))), 0.0), 14.0);
    c += VIOLET * kick * 2.2;
    float rimR = smoothstep(0.93, 0.99, dot(d, normalize(vec3(-0.9, 0.1, -0.4))));
    c += ICE * rimR * 0.8;
    return c;
}

vec3 thinFilm(float ndv) {
    float th = 1.8 + 1.6 * ndv;
    return 0.5 + 0.5 * cos(6.28318 * (vec3(0.0, 0.33, 0.67) + th));
}

void main() {
    vec2 frag = gl_FragCoord.xy;
    vec2 p = (frag - 0.5 * uRes) / uRes.y;
    float t = uTime;
    float day = clamp(uDay, 0.0, 1.0);
    float surge = uWake >= 0.0 ? sin(3.14159265 * clamp(uWake, 0.0, 1.0)) : 0.0;

    vec3 ro = vec3(uOffset.x * 1.6, 0.30 + uOffset.y * 1.2, 6.4);
    vec3 ta = vec3(0.0, 0.25, 0.0);
    vec3 fw = normalize(ta - ro);
    vec3 rt = normalize(cross(fw, vec3(0.0, 1.0, 0.0)));
    vec3 up = cross(rt, fw);
    vec3 rd = normalize(p.x * rt + p.y * up + 1.75 * fw);

    float dist = 0.0;
    float halo = 0.0;
    bool hit = false;
    for (int i = 0; i < 72; i++) {
        vec3 q = ro + rd * dist;
        float d = map(q, t, surge);
        halo += exp(-d * 9.0) * 0.012;
        if (d < 0.0012) { hit = true; break; }
        dist += d * 0.9;
        if (dist > 9.0) break;
    }

    vec3 col = vec3(0.0);
    if (hit) {
        vec3 pos = ro + rd * dist;
        vec3 n = normalAt(pos, t, surge);
        float ndv = clamp(dot(n, -rd), 0.0, 1.0);
        float fres = 0.06 + 0.94 * pow(1.0 - ndv, 4.0);
        vec3 film = thinFilm(ndv);
        vec3 refl = studio(reflect(rd, n), day);
        col = refl * mix(vec3(1.0), film, 0.35 * (1.0 - ndv)) * (0.30 + fres);
        // Light that got inside: a faint cyan body glow toward the thick middle.
        col += CYAN * pow(ndv, 3.0) * 0.035;
        col += film * pow(1.0 - ndv, 5.0) * 0.16;
    }
    col += mix(CYAN, VIOLET, 0.35) * halo * 0.55;

    // Depth haze: a violet floor and a cyan ceiling, far below the drops, so
    // glass laid over the wallpaper always has colour to frost.
    vec2 u = frag / uRes;
    float haze = exp(-pow((u.x - 0.78) * 1.6, 2.0) - pow((u.y - 0.18) * 2.4, 2.0));
    float haze2 = exp(-pow((u.x - 0.15) * 1.8, 2.0) - pow((u.y - 0.62) * 2.2, 2.0));
    col += VIOLET * haze * 0.16 + CYAN * haze2 * 0.10;

    float gain = mix(0.72, 1.0, day) * (0.35 + 0.65 * uIntensity) * (1.0 + 0.6 * surge);
    col *= gain;

    // Film grain, fixed per pixel so it does not crawl on OLED.
    col += (hash(frag) - 0.5) * 0.012;

    vec2 v = frag / uRes - 0.5;
    col *= smoothstep(0.95, 0.25, length(v * vec2(1.0, 1.15)));

    col = col / (1.0 + col * 0.35);
    fragColor = vec4(max(col, 0.0), 1.0);
}
