// The voice under the orb: "horizon", a 3D spectrogram terrain of glowing lines receding under the orb, the front line shaped by // the voice, with a faint reflection. WebGL2, driven by the playing voice's frequency bands, loudness and // waveform (Listener in visual.js). Drawn only when asked: the page's on-demand loop calls draw() while sound plays; silent = the // last landscape as one still frame. import { BANDS } from "./visual.js"; const SMALL = matchMedia("(max-width: 760px)").matches || (navigator.hardwareConcurrency || 8) <= 4; const COLS = SMALL ? 72 : 144; // samples along each line const ROWS = SMALL ? 22 : 44; // history rows (lines toward the horizon) const ROW_EVERY = 1 / 30; // a new front row 30 times a second, whatever the frame rate 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 uRows; // ROWS x COLS heights, ring buffer uniform float uHead, uRowsN, uCols, uMirror, uAspect, uPx, uWidth, uFrac; out float vA; out float vAcross; out float vAge; vec2 proj(vec3 p){ // camera above the front edge, looking toward the horizon float d = 1.25 - p.z; vec2 s = vec2(p.x * 2.7, p.y * 2.1 - 2.17) / d; s.y += 1.04; return vec2(s.x / uAspect, s.y); } float hAt(float row, float col){ return texture(uRows, vec2((col + 0.5) / uCols, (mod(uHead - row + uRowsN, uRowsN) + 0.5) / uRowsN)).r; } void main(){ float seg = float(gl_InstanceID); float row = floor(seg / (uCols - 1.0)); float col = mod(seg, uCols - 1.0); float age = (row + uFrac) / uRowsN; float z = -age * 3.2; float c0 = col, c1 = col + 1.0; float x0 = (c0 / (uCols - 1.0) * 2.0 - 1.0) * 1.9, x1 = (c1 / (uCols - 1.0) * 2.0 - 1.0) * 1.9; float amp = 0.17 * (1.0 - 0.35 * age); float y0 = hAt(row, c0) * amp * uMirror, y1 = hAt(row, c1) * amp * uMirror; vec2 p0 = proj(vec3(x0, y0, z)), p1 = proj(vec3(x1, y1, z)); vec2 dir = normalize((p1 - p0) * vec2(uAspect, 1.0) + 1e-6); vec2 nrm = vec2(-dir.y, dir.x) / vec2(uAspect, 1.0); float wpx = uWidth * (row < 0.5 ? 1.3 : 1.0) * (1.0 - 0.5 * age); vec2 p = mix(p0, p1, aQ.x) + nrm * aQ.y * wpx * uPx; gl_Position = vec4(p, 0.0, 1.0); float edge = smoothstep(0.0, 0.18, min(c0, uCols - 1.0 - c0) / uCols); vA = pow(1.0 - age, 1.6) * edge * (uMirror < 0.0 ? 0.16 : 1.0); vAcross = aQ.y; vAge = age; }`; const FS = `#version 300 es precision highp float; uniform vec3 uCol, uCol2; uniform float uLevel; in float vA; in float vAcross; in float vAge; out vec4 o; void main(){ float k = 1.0 - abs(vAcross); float a = vA * (k * k) * (0.55 + 0.6 * uLevel * (1.0 - vAge)); vec3 c = mix(mix(uCol, uCol2, vAge * 0.8), vec3(1.0), (1.0 - vAge) * 0.35 * k); o = vec4(c * a, a); }`; export class WaveFX { constructor(canvas) { this.cv = canvas; this.acc = 0; this.head = 0; 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); this.p = p; this.u = Object.fromEntries(["uRows", "uHead", "uRowsN", "uCols", "uMirror", "uAspect", "uPx", "uWidth", "uFrac", "uCol", "uCol2", "uLevel"].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.col2 = [0.13, 0.83, 0.93]; } setColor(hex, hex2 = "#22d3ee") { const c = (h) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16) / 255); this.col = c(hex); this.col2 = c(hex2); } _fit() { const dpr = Math.min(SMALL ? 1.5 : 2, devicePixelRatio || 1), w = Math.round(this.cv.clientWidth * dpr), h = Math.round(this.cv.clientHeight * dpr); if (this.cv.width !== w || this.cv.height !== h) { this.cv.width = w; this.cv.height = h; } return { w, h, dpr }; } /** the front line: the live waveform (oscilloscope window) plus the band envelope, under a window that keeps both ends flat */ _sample(L, on) { const sc = this.scope; if (L && on) L.scope(sc, 24000, 4.5); else sc.fill(0); for (let i = 0; i < COLS; i++) { const u = i / (COLS - 1), env = L && on ? Math.max(0, cr(L.soft, u * BANDS - 0.5)) : 0; const w0 = (sc[Math.max(0, i - 1)] + 2 * sc[i] + sc[Math.min(COLS - 1, i + 1)]) / 4; this.row[i] = (0.45 * w0 + 0.8 * env) * win(u); } } draw(L, dt, active) { if (!this.ok) return; const gl = this.gl, { w, h, dpr } = this._fit(), on = !!active, u = this.u; 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 (on) { this._sample(L, on); this.acc += dt; // the terrain flows back while sound plays; silent keeps the last landscape 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); this.onRow?.(this.row); } } gl.useProgram(this.p); gl.uniform1i(u.uRows, 0); gl.uniform1f(u.uHead, this.head); gl.uniform1f(u.uRowsN, ROWS); gl.uniform1f(u.uCols, COLS); gl.uniform1f(u.uAspect, w / h); gl.uniform1f(u.uPx, 2 / h); gl.uniform1f(u.uWidth, 0.9 * dpr); gl.uniform1f(u.uFrac, on ? this.acc / ROW_EVERY : 0); gl.uniform1f(u.uLevel, L && on ? L.level : 0); gl.uniform3fv(u.uCol, this.col); gl.uniform3fv(u.uCol2, this.col2); gl.bindVertexArray(this.vao); for (const m of [-1, 1]) { gl.uniform1f(u.uMirror, m); gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, ROWS * (COLS - 1)); } gl.bindVertexArray(null); } } // The waterfall: the horizon's front lines keep flowing, leaving the terrain and running down the page behind everything, fainter // the further down they get. Plain 2D lines (one gradient stroke per row), drawn by the same on-demand loop: only while sound // plays, then one still frame. Always in the current voice's colour. const FALL_ROWS = SMALL ? 26 : 44, FALL_COLS = SMALL ? 48 : 90, FALL_EVERY = 3; // keep every 3rd terrain row export class Cascade { constructor(canvas) { this.cv = canvas; this.g = canvas.getContext("2d"); this.rows = []; this.n = 0; this.frac = 0; this.rgb = "52,211,153"; } setColor(hex) { this.rgb = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16)).join(","); } /** one terrain front row (COLS samples); every FALL_EVERY-th becomes a falling line */ push(row) { if (this.n++ % FALL_EVERY) return; const r = new Float32Array(FALL_COLS); for (let i = 0; i < FALL_COLS; i++) r[i] = row[Math.round((i / (FALL_COLS - 1)) * (row.length - 1))]; this.rows.unshift(r); if (this.rows.length > FALL_ROWS) this.rows.pop(); this.frac = 0; } draw(dt, active) { const cv = this.cv, w = cv.clientWidth, h = cv.clientHeight; if (!w || !h) return; if (cv.width !== w || cv.height !== h) { cv.width = w; cv.height = h; } // 1x: faint background lines, a big canvas const g = this.g; g.clearRect(0, 0, w, h); if (active) this.frac = Math.min(1, this.frac + dt * (30 / FALL_EVERY)); const gap = h / FALL_ROWS, amp = Math.min(70, gap * 2.2); g.lineWidth = 1.1; for (let k = 0; k < this.rows.length; k++) { const r = this.rows[k], age = (k + this.frac) / FALL_ROWS, y0 = (k + this.frac) * gap, a = Math.pow(1 - age, 1.7) * 0.55; if (a < 0.01) continue; // young lines pour from the right, under the terrain; older ones spread across the page const spread = Math.min(1, age * 1.6), grad = g.createLinearGradient(0, 0, w, 0); for (const u of [0, 0.15, 0.4, 0.65, 0.85, 1]) { const side = spread + (1 - spread) * Math.max(0, (u - 0.25) / 0.75), edge = Math.sin(Math.PI * u); grad.addColorStop(u, `rgba(${this.rgb},${(a * side * edge).toFixed(3)})`); } g.strokeStyle = grad; g.beginPath(); for (let i = 0; i < FALL_COLS; i++) { const x = (i / (FALL_COLS - 1)) * w, y = y0 - r[i] * amp * (1 - 0.4 * age); i ? g.lineTo(x, y) : g.moveTo(x, y); } g.stroke(); } } }