// The voice as a waterfall: the live waveform is the front line, on an edge; every line before it has tipped over // and falls like water, keeping its shape, so together they draw a waterfall you never see itself. They fall a fixed distance // and are gone well before its end, so no screen size shows a floor. One WebGL2 shader on one canvas behind // the page: every line is a row of the voice's live waveform + band envelope, kept in a ring-buffer texture; the vertex shader // places each row on the path (horizon -> edge -> down). Drawn only while sound plays (the page's on-demand loop), then the last // frame stays still. Always in the current voice's colour. import { BANDS } from "./visual.js"; const SMALL = matchMedia("(max-width: 760px)").matches || (navigator.hardwareConcurrency || 8) <= 4; const COLS = SMALL ? 64 : 128; // samples along each line const ROWS = SMALL ? 44 : 72; // lines falling from the edge (the newest is the edge itself) const ROW_EVERY = 1 / 30; // a new row at the horizon 30 times a second const EASE_DOWN_S = 0.057; // after a voice: back to the slow idle stream in 57 ms const win = (u) => Math.pow(Math.max(0, Math.sin(Math.PI * u)), 0.6); const cr = (a, p) => { const n = a.length, i = Math.floor(p), t = p - i, at = (k) => a[k < 0 ? 0 : k >= n ? n - 1 : k]; const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2); return p1 + 0.5 * t * (p2 - p0 + t * (2 * p0 - 5 * p1 + 4 * p2 - p3 + t * (3 * (p1 - p2) + p3 - p0))); }; const VS = `#version 300 es precision highp float; layout(location=0) in vec2 aQ; // quad corner: x 0/1 along the segment, y -1/+1 across it uniform sampler2D uTex; // ROWS x COLS heights, ring buffer, newest row at uHead uniform float uHead, uRows, uCols, uFrac, uWidth; uniform vec2 uCanvas; // canvas size, px uniform vec4 uStage; // the terrain's box inside the canvas: x, y, w, h (px) uniform float uFallLen, uStreamX; // how far the water falls (px); where it gathers (px) uniform float uRiseLen, uMix, uRiseDy; // how high the mountains rise (px); 0 = waterfall (nano), 1 = mountains (tiny); // how far below the waterfall's edge the mountains start (px) out float vA; out float vAcross; out float vHot; float hAt(float row, float col){ return texture(uTex, vec2((col + 0.5) / uCols, (mod(uHead - row + uRows, uRows) + 0.5) / uRows)).r; } float hash(float n){ return fract(sin(n * 12.9898) * 43758.5453); } // the terrain: a camera above the front edge, looking toward the horizon; returns px vec2 terrain(float x, float y, float z){ float d = 1.25 - z, aspect = uStage.z / uStage.w; vec2 s = vec2(x * 2.7, y * 2.1 - 2.17) / d; s.y += 1.04; s.x /= aspect; return uStage.xy + vec2((s.x + 1.0) * 0.5 * uStage.z, (1.0 - (s.y + 1.0) * 0.5) * uStage.w); } const float SPAN = 1.25; // half width of the terrain (world units) float noise(vec2 p){ // smooth value noise vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f); float n = i.x + i.y * 57.0; return mix(mix(hash(n), hash(n + 1.0), f.x), mix(hash(n + 57.0), hash(n + 58.0), f.x), f.y); } // a point of a line that went over the edge: the whole waveform line keeps its shape, tips over the lip and falls like water, // sagging in the middle, each point a little faster or slower (turbulence), drifting, then breaking up as it fades // a: how far it has fallen (rows, fractional, for its position); k: which stored line it is (integer, for its shape). Reading the // shape at a would morph each line toward its neighbour between row steps and snap back at the next one: the jitter. vec2 falling(float a, float k, float c, out float alpha, out float hot){ float u = c / (uCols - 1.0), x = (u * 2.0 - 1.0) * SPAN, s = clamp(a / uRows, 0.0, 1.0); float id = mod(uHead - k + uRows, uRows); // which line this is, so its own turbulence travels with it // the wave smooths out as it falls (water evens the jagged waveform), and flattens float h = mix(hAt(k, c), (hAt(k, c - 1.5) + hAt(k, c) + hAt(k, c + 1.5) + hAt(k - 1.0, c) + hAt(k + 1.0, c)) / 5.0, smoothstep(0.0, 0.3, s)); vec2 e = terrain(x, h * 0.3 * pow(1.0 - s, 1.6), 0.0); // where it left the edge (the live line on top, full height) float speed = 1.0 + 0.2 * sin(3.14159 * u) + 0.24 * (noise(vec2(u * 3.0, id * 0.37)) - 0.5) + 0.2 * abs(h); // sags in the middle float ss = min(1.0, s * speed); // off the lip like real water under the same camera: carried forward toward you while gravity pulls it down; the curl over // the edge comes from the projection. Its screen path is scaled so the fall ends uFallLen below the edge. vec2 lip = terrain(x, 0.0, 0.0), dropped = terrain(x, -1.6 * ss * ss, 0.38 * ss), bottom = terrain(x, -1.6, 0.38); float drop = (dropped.y - lip.y) / max(1.0, bottom.y - lip.y) * uFallLen; float spread = 1.25 / (1.25 - 0.38 * ss); // the perspective scale: a line coming toward you gets wider float narrow = mix(1.0, 0.72, smoothstep(0.1, 0.9, s)) * mix(1.0, spread, 0.55); float turb = (noise(vec2(u * 7.0 + id, s * 5.0)) - 0.5) * 30.0 * s; float xs = uStreamX + (e.x - uStreamX) * narrow + turb; // lower down the line breaks into pieces, like spray float pieces = mix(1.0, smoothstep(0.32, 0.62, noise(vec2(u * 16.0 + id * 1.7, s * 7.0))), smoothstep(0.25, 0.9, s)); // the same columns stay a little brighter all the way down: the eye reads streams of water, still only lines float streams = mix(1.0, 0.45 + 0.75 * noise(vec2(u * 11.0, 3.7)), smoothstep(0.0, 0.25, s)); // gone well before the end of the fall: no floor on any screen // a new line fades in where it appears instead of popping up alpha = smoothstep(0.0, 2.5, a) * smoothstep(0.0, 0.18, min(u, 1.0 - u)) * pow(1.0 - s, 1.25) * (1.0 - smoothstep(0.45, 0.85, s)) * pieces * streams; hot = max(1.0 - s / 0.06, 0.0) + (1.0 - s) * (1.0 - s) * 0.6; // the lip glows where the lines tip over return vec2(xs, e.y + drop); } // tiny: the same lines as hills, rising from the edge up the page toward a far horizon, closer together and smaller as they go // tiny: the "horizon" landscape from the design. One bright live waveform in front; behind it a dense, finely rippled terrain that // recedes quickly to a glowing horizon; above the horizon every line carries on into the sky, and all of them trace one mountain // field (the same peaks for neighbouring lines), so together they draw ranges: dense near the horizon, wider apart and fainter as // they rise. The voice still bends every line a little. const float GROUND = 0.38; // the share of a line's life on the terrain (short: it reaches the horizon fast) float fbm(vec2 p){ float r = 0.0, a = 0.55; for (int o = 0; o < 4; o++) { r += a * noise(p); p = p * 2.03 + 11.7; a *= 0.5; } return r; } // the mountain field: sharp-ish ridges (folded noise) on broad rises, peaking in the middle of the picture float mountains(float u, float v){ float broad = fbm(vec2(u * 2.1 + 3.0, v * 1.3 + 7.0)); float ridge = 1.0 - abs(2.0 * fbm(vec2(u * 4.6 + 1.0, v * 2.2 + 2.0)) - 1.0); float body = smoothstep(0.0, 0.35, min(u, 1.0 - u)); return (0.3 * broad + 1.05 * ridge * ridge * ridge) * body; // folded noise cubed: sharp peaks, not curves } vec2 rising(float a, float k, float c, out float alpha, out float hot){ float u = c / (uCols - 1.0), x = (u * 2.0 - 1.0) * SPAN, s = clamp(a / uRows, 0.0, 1.0); float id = mod(uHead - k + uRows, uRows); float edges = smoothstep(0.0, 0.16, min(u, 1.0 - u)), fadeIn = smoothstep(0.0, 2.0, a); float t = min(s / GROUND, 1.0); // 0 = front, 1 = horizon float front = max(1.0 - a / 3.0, 0.0); // the newest lines: the bright live waveform, where the glow is // the terrain: the voice's waveform, big in front and calming fast; a fine ripple so the ground reads as a surface float h = hAt(k, c) * mix(0.3, 0.1, smoothstep(0.0, 0.5, t)); // as tall as the water's front line float ripple = (noise(vec2(u * 26.0 + id * 0.9, id * 0.37)) - 0.5) * 0.11 * (1.0 - 0.6 * t); float spread = 1.0 + 2.4 * t; vec2 g = terrain(x * spread, h + ripple, -3.2 * t); g.x = uStreamX + (g.x - uStreamX) * (1.0 + 0.15 * t); // lines get fainter as perspective packs them toward the horizon, so they don't add up to a bright band there; the same faintness // carries straight on into the mountain lines (no switch where the ground ends). The glow is on the live line in front. float far = mix(1.0, 0.16, smoothstep(0.35, 1.0, t)); if (s <= GROUND) { alpha = fadeIn * edges * (far + 1.8 * front); hot = front; return vec2(g.x, g.y + uRiseDy); } // the sky: every line goes on as a contour of the same ranges; spacing grows with height (dense at the horizon) float v = (s - GROUND) / (1.0 - GROUND); float lift = uRiseLen * 0.62 * pow(v, 1.35); float peak = mountains(u, v) * mix(50.0, 190.0, pow(v, 0.6)); float voice = hAt(k, c) * 26.0 * (1.0 - v); float pieces = mix(1.0, smoothstep(0.32, 0.62, noise(vec2(u * 16.0 + id * 1.7, v * 7.0))), smoothstep(0.12, 0.7, v)); float streams = mix(1.0, 0.45 + 0.75 * noise(vec2(u * 11.0, 3.7)), smoothstep(0.0, 0.25, v)); alpha = edges * mix(0.16, 0.55, smoothstep(0.0, 0.18, v)) * pow(1.0 - v, 1.25) * (1.0 - smoothstep(0.35, 0.72, v)) * pieces * streams; // starts at the ground's 0.16 hot = 0.0; return vec2(g.x, g.y + uRiseDy - lift - (peak + voice) * smoothstep(0.0, 0.12, v)); } void main(){ // every line: segments along it; the oldest (lowest in the fall) first float seg = float(gl_InstanceID), row = floor(seg / (uCols - 1.0)), col = mod(seg, uCols - 1.0); float k = uRows - 1.0 - row, a = k + uFrac; // which line; how far it has fallen since it was the live one on the edge float al0, al1, h0, h1; vec2 p0, p1; p0 = falling(a, k, col, al0, h0); p1 = falling(a, k, col + 1.0, al1, h1); if (uMix > 0.001) { float ar0, ar1, r0, r1; vec2 q0 = rising(a, k, col, ar0, r0), q1 = rising(a, k, col + 1.0, ar1, r1); p0 = mix(p0, q0, uMix); p1 = mix(p1, q1, uMix); al0 = mix(al0, ar0, uMix); al1 = mix(al1, ar1, uMix); h0 = mix(h0, r0, uMix); h1 = mix(h1, r1, uMix); } vec2 dir = normalize(p1 - p0 + 1e-6); vec2 nrm = vec2(-dir.y, dir.x); vec2 p = mix(p0, p1, aQ.x) + nrm * aQ.y * uWidth * mix(1.0, 0.4, clamp(a / uRows, 0.0, 1.0)); // lines thin as they go gl_Position = vec4(p.x / uCanvas.x * 2.0 - 1.0, 1.0 - p.y / uCanvas.y * 2.0, 0.0, 1.0); vA = mix(al0, al1, aQ.x); vHot = mix(h0, h1, aQ.x); vAcross = aQ.y; }`; const FS = `#version 300 es precision highp float; uniform vec3 uCol; uniform float uLevel, uGain; in float vA; in float vAcross; in float vHot; out vec4 o; void main(){ float k = 1.0 - abs(vAcross); float a = vA * k * k * uGain * (0.6 + 0.7 * uLevel * vHot); vec3 c = mix(uCol, vec3(1.0), 0.45 * vHot * k); o = vec4(c * a, a); }`; export class Waterfall { constructor(canvas) { this.cv = canvas; this.acc = 0; this.head = 0; this.box = null; const gl = canvas.getContext("webgl2", { premultipliedAlpha: true, alpha: true, antialias: true }); this.ok = !!gl; if (!gl) return; this.gl = gl; const p = gl.createProgram(); for (const [t, s] of [[gl.VERTEX_SHADER, VS], [gl.FRAGMENT_SHADER, FS]]) { const x = gl.createShader(t); gl.shaderSource(x, s); gl.compileShader(x); if (!gl.getShaderParameter(x, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(x)); gl.attachShader(p, x); } gl.linkProgram(p); if (!gl.getProgramParameter(p, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(p)); this.p = p; this.u = Object.fromEntries(["uTex", "uRows", "uHead", "uCols", "uFrac", "uWidth", "uLevel", "uCanvas", "uStage", "uFallLen", "uStreamX", "uRiseLen", "uMix", "uRiseDy", "uCol", "uGain"] .map((n) => [n, gl.getUniformLocation(p, n)])); this.vao = gl.createVertexArray(); gl.bindVertexArray(this.vao); gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); // one quad, instanced per segment gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0, -1, 1, -1, 0, 1, 1, 1]), gl.STATIC_DRAW); gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); gl.bindVertexArray(null); this.tex = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, this.tex); gl.texImage2D(gl.TEXTURE_2D, 0, gl.R16F, COLS, ROWS, 0, gl.RED, gl.FLOAT, new Float32Array(COLS * ROWS)); for (const [k, v] of [[gl.TEXTURE_MIN_FILTER, gl.LINEAR], [gl.TEXTURE_MAG_FILTER, gl.LINEAR], [gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE], [gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE]]) gl.texParameteri(gl.TEXTURE_2D, k, v); this.row = new Float32Array(COLS); this.scope = new Float32Array(COLS); this.col = [0.2, 0.83, 0.6]; this.flow = 0; this.t = 0; this.mode = 0; this.mix = 0; // t: the water's own clock (advances with its speed) // flow: 0 = the slow idle stream, 1 = a voice speaking; eased, never a jump this.seed(); } /** a quiet landscape to start from, so the still page isn't empty */ seed() { const gl = this.gl, all = new Float32Array(COLS * ROWS); for (let r = 0; r < ROWS; r++) for (let i = 0; i < COLS; i++) { const u = i / (COLS - 1); all[r * COLS + i] = 0.07 * win(u) * (Math.sin(u * 7.3 + r * 0.29) * 0.7 + Math.sin(u * 17.1 - r * 0.17) * 0.3); } gl.bindTexture(gl.TEXTURE_2D, this.tex); gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, COLS, ROWS, gl.RED, gl.FLOAT, all); } setColor(hex) { this.col = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255); } /** where the terrain sits inside the canvas (px, CSS) and where the stream falls */ place(box) { this.box = box; } /** 0: the waterfall (nano), 1: the rising hills (tiny); the picture eases over */ setMode(m) { this.mode = m; } _sample(L) { const sc = this.scope; L.scope(sc, 24000, 4.5); let mean = 0; for (let i = 0; i < COLS; i++) mean += Math.max(0, cr(L.soft, (i / (COLS - 1)) * BANDS - 0.5)) / COLS; // the band envelope around its mean, so the terrain stays a sheet with ridges instead of bulging into a mound for (let i = 0; i < COLS; i++) { const u = i / (COLS - 1), env = Math.max(0, cr(L.soft, u * BANDS - 0.5)) - mean; const w0 = (sc[Math.max(0, i - 1)] + 2 * sc[i] + sc[Math.min(COLS - 1, i + 1)]) / 4; this.row[i] = (0.4 * w0 + 1.1 * env) * win(u); } } /** active: the water moves (a new line every 1/30 s); sounding: that line carries the voice, else it is flat */ draw(L, dt, active, sounding) { if (!this.ok || !this.box) return; const gl = this.gl, cv = this.cv, dpr = Math.min(SMALL ? 1.5 : 2, devicePixelRatio || 1); const w = Math.round(cv.clientWidth * dpr), h = Math.round(cv.clientHeight * dpr); if (!w || !h) return; if (cv.width !== w || cv.height !== h) { cv.width = w; cv.height = h; } gl.viewport(0, 0, w, h); gl.clearColor(0, 0, 0, 0); gl.clear(gl.COLOR_BUFFER_BIT); gl.enable(gl.BLEND); gl.blendFunc(gl.ONE, gl.ONE); gl.bindTexture(gl.TEXTURE_2D, this.tex); if (active) { // the water never stops: a slow gentle stream when nobody speaks, speeding up smoothly when a voice comes in, and back to // the slow stream 57 ms after it ends, with the waveform's height growing and shrinking along with the speed if (sounding) this.flow += (1 - this.flow) * (1 - Math.exp(-3.2 * dt)); else this.flow = Math.max(0, this.flow - dt / EASE_DOWN_S); const f = this.flow * this.flow * (3 - 2 * this.flow), speed = 0.04 + 0.96 * f; this.t += dt * speed; if (L && f > 0.01) this._sample(L); else this.row.fill(0); for (let i = 0; i < COLS; i++) { const u = i / (COLS - 1), t = this.t; const calm = 0.05 * win(u) * (Math.sin(u * 6.2 + t * 1.6) * 0.6 + Math.sin(u * 14.7 - t * 2.6) * 0.4); this.row[i] = calm * (1 - f) + this.row[i] * f; } this.acc += dt * speed * (1 - 0.5 * this.mix); // tiny's mountains travel at half the water's pace (they cross a short ground) while (this.acc >= ROW_EVERY) { this.acc -= ROW_EVERY; this.head = (this.head + 1) % ROWS; gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, this.head, COLS, 1, gl.RED, gl.FLOAT, this.row); } } const b = this.box, u = this.u; gl.useProgram(this.p); gl.uniform1i(u.uTex, 0); gl.uniform1f(u.uHead, this.head); gl.uniform1f(u.uRows, ROWS); gl.uniform1f(u.uCols, COLS); gl.uniform1f(u.uFrac, active ? this.acc / ROW_EVERY : 0); gl.uniform1f(u.uLevel, L ? L.level * this.flow : 0); gl.uniform2f(u.uCanvas, w, h); gl.uniform4f(u.uStage, b.x * dpr, b.y * dpr, b.w * dpr, b.h * dpr); this.mix = this.mode > this.mix ? Math.min(this.mode, this.mix + dt / 1.2) : Math.max(this.mode, this.mix - dt / 1.2); const glide = this.mix * this.mix * (3 - 2 * this.mix); gl.uniform1f(u.uRiseLen, (b.rise ?? 0) * dpr); gl.uniform1f(u.uMix, glide); gl.uniform1f(u.uRiseDy, (b.riseDy ?? 0) * dpr); gl.uniform1f(u.uFallLen, b.fall * dpr); gl.uniform1f(u.uStreamX, b.streamX * dpr); gl.uniform3fv(u.uCol, this.col); gl.bindVertexArray(this.vao); // every line, terrain and fall in one go: a wide soft bloom, a halo, then the thin bright line for (const [width, gain] of [[16, 0.04], [6, 0.1], [1.2, 1.0]]) { gl.uniform1f(u.uWidth, width * dpr); gl.uniform1f(u.uGain, gain); gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, ROWS * (COLS - 1)); } gl.bindVertexArray(null); } }