{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"brass-mixing-desk-fader-in-webgl-with-weighted-detents-an-engraved-decibel-plate-and-a-backlit-vu-needle","type":"registry:block","title":"Brass Mixing Desk Fader in WebGL with Weighted Detents, an Engraved Decibel Plate and a Backlit VU Needle","description":"Free React volume slider: a heavy brass fader cap that clicks into engraved decibel marks, with a lamp-lit VU needle beside it, traced in a WebGL shader.","dependencies":[],"files":[{"path":"components/we-inc/brass-mixing-desk-fader-in-webgl-with-weighted-detents-an-engraved-decibel-plate-and-a-backlit-vu-needle.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// The channel. Positions run 0 to 100 along the slot; the engraved plate is drawn from MARKS.\nconst MAX = 100;\nconst START = 66; // opens at -5 dB, needle mid-scale and the plate fully lit\nconst MARKS: [number, number][] = [\n  [0, -90], [8, -60], [18, -40], [28, -30], [40, -20], [55, -10], [66, -5], [78, 0], [90, 5], [100, 10],\n];\nconst DETENT = 1.6; // how close, in positions, the cap must come to a mark before it drops into it\nconst DEMO = [78, 40, 92, START]; // where the self-running demo walks the fader\nconst notes: [number, string][] = [\n  [-999, \"Muted, the channel is off the bus\"],\n  [-50, \"A whisper under everything else\"],\n  [-25, \"Tucked well into the background\"],\n  [-12, \"Present, but leaving room around it\"],\n  [-3, \"Riding just under unity\"],\n  [-0.4, \"Unity, exactly as it was recorded\"],\n  [0.5, \"Just over unity, leaning forward\"],\n  [3, \"Pushed hard into the mix\"],\n  [8, \"All the way up, the needle pins\"],\n];\nconst FONT = '\"Inter Tight\", \"Inter\", ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", sans-serif';\nconst SERIF = '\"Iowan Old Style\", \"Palatino Linotype\", Palatino, \"Book Antiqua\", Georgia, serif';\n\n// Scene: the slot runs along z, far end is full level.\nconst Z0 = 1.85, Z1 = -1.85;\nconst zOf = (v: number) => Z0 + (Math.max(0, Math.min(MAX, v)) / MAX) * (Z1 - Z0);\nconst vOfZ = (z: number) => ((Z0 - z) / (Z0 - Z1)) * MAX;\nconst dbOf = (v: number) => {\n  for (let i = 1; i < MARKS.length; i++) {\n    const [a, da] = MARKS[i - 1], [b, db] = MARKS[i];\n    if (v <= b) return da + ((v - a) / (b - a)) * (db - da);\n  }\n  return 10;\n};\nconst label = (v: number) => {\n  if (v <= 0) return \"−∞\";\n  const d = dbOf(v), r = Math.round(d * 10) / 10;\n  return (r > 0 ? \"+\" : r < 0 ? \"−\" : \"\") + Math.abs(r).toFixed(1);\n};\nconst noteFor = (v: number) => {\n  const d = v <= 0 ? -999 : dbOf(v);\n  return notes.reduce((a, n) => (d >= n[0] ? n[1] : a), notes[0][1]);\n};\n// The meter face is linear in voltage, like the real thing: 0 VU sits well right of centre.\nconst P20 = Math.pow(10, -20 / 20), P3 = Math.pow(10, 3 / 20);\nconst angleOf = (db: number) => -0.82 + ((Math.pow(10, db / 20) - P20) / (P3 - P20)) * 1.64;\n\n// Meter geometry shared by the shader and the printed face.\nconst BX = 2.2, BZ = -0.1, WX = 0.84, WZ = 0.6, PIVZ = 0.72;\nconst PW = 0.8, PL = 2.25;\n\nconst VERT = `ATTR vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;\nconst TONE = `\nvec3 tone(vec3 x){ x *= 0.95; vec3 a = x * (2.51 * x + 0.03); vec3 b = x * (2.43 * x + 0.59) + 0.14; return pow(clamp(a / b, 0.0, 1.0), vec3(1.0 / 2.2)); }\n`;\n\nconst SCENE = `\nprecision highp float;\nuniform vec2 uRes; uniform vec4 uJit; uniform float uSeed;\nuniform vec3 uCam; uniform vec3 uTgt; uniform float uFoc; uniform vec2 uShift; uniform float uFocus;\nuniform float uCapZ; uniform float uLean; uniform float uLift; uniform float uNeedle; uniform float uLamp; uniform float uKey;\nuniform sampler2D uPlate; uniform sampler2D uFace; uniform float uDirect;\nconst vec3 LK = vec3(-0.3011, 0.6222, -0.7226);\nconst vec3 KEYC = vec3(1.0, 0.76, 0.5);\nconst float PW = ${PW}; const float PL = ${PL}; const float PT = 0.03;\nconst float SW = 0.05; const float SL = 2.04;\nconst float CY = 0.255;\nconst float BX = ${BX}; const float BZ = ${BZ}; const float WX = ${WX}; const float WZ = ${WZ};\nconst float GY = 0.1; const float FY = -0.06; const float NY = -0.03;\nconst vec2 PIV = vec2(${BX}, ${PIVZ});\nconst vec3 LAMP = vec3(${BX + 0.66}, 0.0, 0.95);\nconst vec3 BRASS = vec3(0.95, 0.68, 0.37);\n${TONE}\nfloat h11(float p){ p = fract(p * 0.1031); p *= p + 33.33; p *= p + p; return fract(p); }\nfloat h21(vec2 p){ vec3 q = fract(vec3(p.xyx) * 0.1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); }\nfloat n1(float x){ float i = floor(x); float f = fract(x); f = f * f * (3.0 - 2.0 * f); return mix(h11(i), h11(i + 1.0), f); }\nfloat n2(vec2 x){ vec2 i = floor(x); vec2 f = fract(x); f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(h21(i), h21(i + vec2(1.0, 0.0)), f.x), mix(h21(i + vec2(0.0, 1.0)), h21(i + vec2(1.0, 1.0)), f.x), f.y); }\nfloat sdBox(vec3 p, vec3 b){ vec3 q = abs(p) - b; return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); }\n\n// ---- geometry ----\nfloat sPlate(vec3 p){\n  float d = sdBox(p - vec3(0.0, -0.035, 0.0), vec3(PW - 0.006, 0.059, PL - 0.006)) - 0.006;\n  return max(d, -sdBox(p, vec3(SW, 0.6, SL)));\n}\nvec3 capLocal(vec3 p){\n  vec3 q = p - vec3(0.0, CY + uLift, uCapZ);\n  float c = cos(uLean); float s = sin(uLean);\n  return vec3(q.x, c * q.y - s * q.z, s * q.y + c * q.z);\n}\nfloat sCap(vec3 p){\n  vec3 q = capLocal(p);\n  float d = sdBox(q, vec3(0.4, 0.155, 0.24)) - 0.04;\n  d = max(d, (abs(q.z) + q.y * 0.34 - 0.3) * 0.95);\n  d = max(d, (abs(q.x) + q.y * 0.2 - 0.45) * 0.98);\n  d = max(d, 0.8 - length(q.yz - vec2(0.95, 0.0)));\n  float st = sdBox(q - vec3(0.0, -0.34, 0.0), vec3(0.026, 0.16, 0.05));\n  return min(d, st);\n}\nfloat sBez(vec3 p){\n  vec3 q = p - vec3(BX, 0.0, BZ);\n  float o = sdBox(q, vec3(WX + 0.09, 0.07, WZ + 0.09)) - 0.05;\n  float i = sdBox(q, vec3(WX, 0.6, WZ));\n  return max(o, -i);\n}\nfloat sLamp(vec3 p){\n  vec3 q = p - LAMP;\n  float dome = length(q - vec3(0.0, -0.02, 0.0)) - 0.072;\n  float ring = length(vec2(length(q.xz) - 0.088, q.y)) - 0.026;\n  return min(dome, ring);\n}\nfloat map(vec3 p){ return min(min(sPlate(p), sCap(p)), min(sBez(p), sLamp(p))); }\nvec3 calcN(vec3 p){\n  vec2 e = vec2(0.0009, -0.0009);\n  return normalize(e.xyy * map(p + e.xyy) + e.yyx * map(p + e.yyx) + e.yxy * map(p + e.yxy) + e.xxx * map(p + e.xxx));\n}\nvec2 boxSpan(vec3 ro, vec3 rd, vec3 lo, vec3 hi){\n  vec3 inv = 1.0 / (rd + vec3(1e-6)); vec3 a = (lo - ro) * inv; vec3 b = (hi - ro) * inv;\n  vec3 mn = min(a, b); vec3 mx = max(a, b);\n  return vec2(max(max(mn.x, mn.y), max(mn.z, 0.0)), min(mx.x, min(mx.y, mx.z)));\n}\nfloat march(vec3 ro, vec3 rd, float tmax){\n  vec2 tb = boxSpan(ro, rd, vec3(-0.86, -0.45, -2.3), vec3(3.32, 0.42, 1.1));\n  vec2 tc = boxSpan(ro, rd, vec3(-0.86, -0.45, -2.3), vec3(0.86, 0.42, 2.3));\n  tb = vec2(min(tb.x, tc.x), max(tb.y, tc.y));\n  if (tb.x > tb.y) return -1.0;\n  float t = tb.x; float hi = min(tb.y, tmax);\n  for (int i = 0; i < 110; i++) {\n    float d = map(ro + rd * t);\n    if (d < 0.0004 * (1.0 + t * 0.1)) return t;\n    t += d * 0.9;\n    if (t > hi) break;\n  }\n  return -1.0;\n}\nfloat shadow(vec3 ro){\n  if (ro.x < -1.6 || ro.x > 4.2 || ro.z < -2.6 || ro.z > 3.4) return 1.0;\n  float res = 1.0; float t = 0.015;\n  for (int i = 0; i < 30; i++) {\n    float h = map(ro + LK * t);\n    res = min(res, 9.0 * h / t);\n    t += clamp(h, 0.015, 0.22);\n    if (res < 0.002 || t > 3.5) break;\n  }\n  return clamp(res, 0.0, 1.0);\n}\nfloat ao(vec3 p, vec3 n){\n  float o = 0.0; float s = 1.0;\n  for (int i = 1; i <= 5; i++) { float h = 0.01 + 0.045 * float(i); o += (h - map(p + n * h)) * s; s *= 0.72; }\n  return clamp(1.0 - 2.4 * o, 0.0, 1.0);\n}\n\n// ---- the room: one warm softbox behind the desk, a cool window, a low warm card ----\nfloat rectL(vec3 ro, vec3 rd, vec3 c, vec3 n, vec2 hs, float soft){\n  float dn = dot(rd, n); if (dn > -1e-4) return 0.0;\n  float t = dot(c - ro, n) / dn; if (t < 0.0) return 0.0;\n  vec3 u = normalize(cross(vec3(0.0, 1.0, 0.0), n)); vec3 v = cross(n, u);\n  vec3 q = ro + rd * t - c; float s = soft * t;\n  vec2 d = abs(vec2(dot(q, u), dot(q, v))) - hs;\n  float cloth = 1.0 - 0.3 * clamp(length(vec2(dot(q, u), dot(q, v)) / hs), 0.0, 1.4);\n  return smoothstep(s, -s, d.x) * smoothstep(s, -s, d.y) * cloth;\n}\nvec3 env(vec3 ro, vec3 rd, float rough){\n  float soft = 0.025 + rough * 0.8;\n  vec3 c = vec3(0.014, 0.011, 0.009) * (0.6 + 0.4 * smoothstep(-0.2, 0.6, rd.y));\n  c += vec3(0.3, 0.17, 0.08) * 0.3 * exp(-pow((rd.y - 0.1) / 0.3, 2.0)) * smoothstep(0.2, -0.8, rd.z);\n  c += KEYC * 15.0 * rectL(ro, rd, LK * 9.0 + vec3(uKey, 0.0, 0.0), -LK, vec2(2.3, 1.2), soft);\n  c += vec3(1.0, 0.9, 0.78) * 2.2 * rectL(ro, rd, vec3(3.2 + uKey * 0.3, 8.5, -6.8), normalize(vec3(-0.15, -0.8, 0.6)), vec2(2.6, 0.32), soft);\n  c += vec3(0.55, 0.68, 0.95) * 2.4 * rectL(ro, rd, vec3(9.0, 3.0, -1.5), normalize(vec3(-1.0, -0.25, 0.2)), vec2(0.45, 2.2), soft);\n  c += vec3(1.0, 0.82, 0.62) * 1.5 * rectL(ro, rd, vec3(-5.0, 2.6, 7.5), normalize(vec3(0.6, -0.3, -1.0)), vec2(1.6, 0.9), soft);\n  return c;\n}\nfloat pool(vec3 p){ vec2 d = (p.xz - vec2(1.05, -0.35)) * vec2(0.78, 1.0); return 0.05 + 0.95 * exp(-dot(d, d) / 9.0); }\nvec3 fres(vec3 f0, float c){ return f0 + (1.0 - f0) * pow(1.0 - clamp(c, 0.0, 1.0), 5.0); }\nvec4 plateTex(vec2 xz){ return texture2D(uPlate, vec2((xz.x + PW) / (2.0 * PW), (xz.y + PL) / (2.0 * PL))); }\nbool inWin(vec2 xz){ return abs(xz.x - BX) < WX && abs(xz.y - BZ) < WZ; }\nvec3 panelAlb(vec3 gp){\n  float hm = n2(gp.xz * 34.0) * 0.6 + n2(gp.xz * 95.0) * 0.4;\n  vec3 a = vec3(0.05, 0.047, 0.043) * (0.86 + 0.28 * hm);\n  // the seams between channel strips\n  float sx = min(abs(gp.x + 1.2), abs(gp.x - 3.62));\n  return a * (0.35 + 0.65 * smoothstep(0.004, 0.012, sx));\n}\nfloat lampGlow(vec3 p){ vec3 q = p - LAMP; return uLamp / (1.0 + dot(q, q) * 70.0); }\nvec3 floorLite(vec3 gp){\n  float pl = pool(gp);\n  if (abs(gp.x) < PW && abs(gp.z) < PL) {\n    if (abs(gp.x) < SW && abs(gp.z) < SL) return vec3(0.002);\n    vec4 t = plateTex(gp.xz);\n    return BRASS * 0.5 * mix(vec3(1.0), vec3(0.06), max(t.r, t.g)) * (KEYC * 1.5 * LK.y * pl + 0.03);\n  }\n  if (inWin(gp.xz)) return vec3(0.55, 0.36, 0.17) * 0.7;\n  return panelAlb(gp) * (KEYC * 1.7 * LK.y * pl + vec3(0.03)) + vec3(1.0, 0.2, 0.06) * 0.12 * lampGlow(gp);\n}\nvec3 envAll(vec3 p, vec3 r, float rough){\n  if (r.y < -0.002) { float t = -p.y / r.y; if (t < 25.0) return floorLite(p + r * t); return vec3(0.004); }\n  return env(p, r, rough);\n}\n\n// ---- materials ----\nvec3 shadeBrass(vec3 p, vec3 n, vec3 gn, vec3 rd, vec3 f0, float rough, float a, float sh){\n  vec3 r = reflect(rd, n);\n  vec3 s = envAll(p + gn * 0.003, r, rough);\n  // a polished part sees the dark room close by: soften what it sees with the occlusion\n  vec3 dl = KEYC * 2.0 * max(dot(n, LK), 0.0) * sh * pool(p);\n  return fres(f0, dot(-rd, n)) * (s * (0.25 + 0.75 * a) + (0.06 + rough * 0.5) * dl + 0.01 * a);\n}\nfloat screw(vec3 p, inout vec3 n){\n  float k = 0.0;\n  for (int i = 0; i < 4; i++) {\n    vec2 c = vec2(i < 2 ? -0.6 : 0.6, mod(float(i), 2.0) < 0.5 ? -2.12 : 2.12);\n    vec2 d = p.xz - c; float r = length(d);\n    if (r < 0.052) {\n      float h = sqrt(max(0.0, 0.0028 - r * r));\n      n = normalize(vec3(d.x, h * 1.6, d.y));\n      float ang = 0.6 + float(i) * 1.3;\n      float sl = abs(d.x * cos(ang) + d.y * sin(ang));\n      k = smoothstep(0.009, 0.006, sl) * 0.85 + smoothstep(0.046, 0.054, r) * 0.6;\n    }\n  }\n  return k;\n}\nvec3 shadePlate(vec3 p, vec3 gn, vec3 rd, vec2 rnd){\n  vec3 n = gn; float paint = 0.0; float red = 0.0;\n  float a = ao(p, gn); float sh = shadow(p + gn * 0.003);\n  if (gn.y > 0.9) {\n    vec4 tx = plateTex(p.xz);\n    paint = max(tx.r, tx.g); red = tx.g;\n    float e = 0.004;\n    float gx = max(plateTex(p.xz + vec2(e, 0.0)).r, plateTex(p.xz + vec2(e, 0.0)).g) - max(plateTex(p.xz - vec2(e, 0.0)).r, plateTex(p.xz - vec2(e, 0.0)).g);\n    float gz = max(plateTex(p.xz + vec2(0.0, e)).r, plateTex(p.xz + vec2(0.0, e)).g) - max(plateTex(p.xz - vec2(0.0, e)).r, plateTex(p.xz - vec2(0.0, e)).g);\n    n = normalize(n + vec3(gx, 0.0, gz) * 0.9);\n    // brushed along the slot\n    n = normalize(n + vec3((n1(p.x * 1400.0 + n1(p.z * 3.0) * 4.0) - 0.5) * 0.05 + rnd.x * 0.05, 0.0, rnd.y * 0.012));\n    float sk = screw(p, n);\n    paint = max(paint, sk);\n  }\n  float tarn = n2(p.xz * vec2(2.2, 0.9)) * 0.6 + n2(p.xz * 9.0) * 0.4;\n  float wear = smoothstep(0.55, 0.8, max(abs(p.x) / PW, abs(p.z) / PL));\n  vec3 f0 = mix(BRASS * vec3(0.98, 0.9, 0.82), vec3(0.55, 0.38, 0.2), 0.35 * tarn + 0.35 * wear) * (0.82 + 0.18 * tarn);\n  vec3 metal = shadeBrass(p, n, gn, rd, f0, 0.16, a, sh);\n  vec3 pc = mix(vec3(0.016, 0.014, 0.012), vec3(0.42, 0.035, 0.02), red > 0.5 * paint ? 1.0 : 0.0);\n  vec3 pd = pc * (KEYC * 1.9 * max(dot(gn, LK), 0.0) * sh * pool(p) + 0.03 * a) + 0.03 * envAll(p, reflect(rd, gn), 0.3);\n  return mix(metal, pd, paint);\n}\nvec3 shadeCap(vec3 p, vec3 gn, vec3 rd, vec2 rnd){\n  vec3 q = capLocal(p);\n  float a = ao(p, gn); float sh = shadow(p + gn * 0.004);\n  vec3 n = gn; float rough = 0.05;\n  if (q.y > 0.12) {\n    // the dish is brushed across, by the thumbs that used it\n    n = normalize(n + vec3(0.0, 0.0, (n1(q.z * 1800.0) - 0.5) * 0.06) + vec3(rnd.x * 0.1, 0.0, rnd.y * 0.02));\n    rough = 0.12;\n  } else n = normalize(n + vec3(rnd, 0.0).xzy * 0.012);\n  vec3 col = shadeBrass(p, n, gn, rd, BRASS * vec3(1.02, 1.0, 0.98), rough, a, sh);\n  // the black index line across the top\n  float line = smoothstep(0.014, 0.01, abs(q.z)) * step(0.1, q.y);\n  vec3 lc = vec3(0.012) * (KEYC * 2.0 * max(dot(gn, LK), 0.0) * sh + 0.05) + 0.045 * envAll(p, reflect(rd, gn), 0.05);\n  return mix(col, lc, line);\n}\nvec3 shadeBez(vec3 p, vec3 gn, vec3 rd, vec2 rnd){\n  float a = ao(p, gn); float sh = shadow(p + gn * 0.003);\n  vec3 n = normalize(gn + vec3(rnd.x, 0.0, rnd.y) * 0.04);\n  float F = 0.045 + 0.955 * pow(1.0 - clamp(dot(-rd, n), 0.0, 1.0), 5.0);\n  vec3 alb = vec3(0.022, 0.018, 0.016);\n  // the inner walls pick up the meter lamps\n  vec3 spill = vec3(1.0, 0.62, 0.3) * 0.5 * smoothstep(0.05, -0.06, p.y) ;\n  return alb * (KEYC * 2.0 * max(dot(gn, LK), 0.0) * sh * pool(p) + 0.03 * a) + spill * alb * 4.0 + F * envAll(p, reflect(rd, n), 0.12) * (0.3 + 0.7 * a);\n}\nvec3 shadeLamp(vec3 p, vec3 gn, vec3 rd){\n  vec3 q = p - LAMP;\n  float a = ao(p, gn);\n  if (length(q.xz) > 0.07) return shadeBrass(p, gn, gn, rd, BRASS * 0.9, 0.08, a, shadow(p + gn * 0.003));\n  float F = 0.04 + 0.96 * pow(1.0 - clamp(dot(-rd, gn), 0.0, 1.0), 5.0);\n  float core = 1.0 - length(q.xz) / 0.075;\n  vec3 glow = vec3(1.0, 0.16, 0.04) * (0.04 + uLamp * (3.0 + 6.0 * core * core));\n  return glow * (1.0 - F) + F * envAll(p, reflect(rd, gn), 0.02);\n}\n\n// ---- the meter: glass, needle, printed face ----\nfloat needleD(vec2 xz, out float along){\n  vec2 d = vec2(sin(uNeedle), -cos(uNeedle)); vec2 q = xz - PIV;\n  along = clamp(dot(q, d), 0.2, 1.1);\n  return length(q - d * along);\n}\nvec3 shadeFace(vec3 f){\n  vec2 uv = vec2((f.x - (BX - WX)) / (2.0 * WX), (f.z - (BZ - WZ)) / (2.0 * WZ));\n  vec3 alb = texture2D(uFace, uv).rgb; alb *= alb;\n  float al;\n  vec2 sp = f.xz + LK.xz * ((NY - FY) / LK.y);\n  float shN = 1.0 - 0.75 * smoothstep(0.012, 0.003, needleD(sp, al));\n  float sh = shadow(f + vec3(0.0, 0.002, 0.0));\n  vec2 c = (uv - vec2(0.5, 0.95)) * vec2(1.1, 1.0);\n  float back = 0.35 + 0.75 * exp(-dot(c, c) * 2.2);\n  // edges of the window fall into the shade of the lip\n  float edge = smoothstep(0.0, 0.06, min(min(uv.x, 1.0 - uv.x), min(uv.y, 1.0 - uv.y)));\n  vec3 lamp = vec3(1.0, 0.64, 0.3) * 1.1 * back * (0.45 + 0.55 * edge);\n  return alb * (lamp + KEYC * 1.7 * LK.y * sh * shN * pool(f));\n}\nvec3 under(vec3 o, vec3 rd){\n  // needle first: it rides a little above the card\n  float tn = (NY - o.y) / rd.y; vec3 qn = o + rd * tn;\n  float al; float nd = needleD(qn.xz, al);\n  float w = mix(0.0075, 0.0028, (al - 0.2) / 0.9);\n  if (nd < w && inWin(qn.xz)) {\n    vec3 nc = vec3(0.012, 0.011, 0.01) * (1.0 + 2.0 * KEYC * LK.y) + vec3(0.08, 0.04, 0.015) * (1.0 - nd / w);\n    return nc;\n  }\n  float tf = (FY - o.y) / rd.y; vec3 f = o + rd * tf;\n  if (inWin(f.xz)) return shadeFace(f);\n  return vec3(0.03, 0.018, 0.008);\n}\nvec3 shadeGlass(vec3 o, vec3 rd, vec2 rnd){\n  vec2 sm = vec2(n2(o.xz * 6.0), n2(o.xz * 6.0 + 7.0)) - 0.5;\n  vec3 n = normalize(vec3(sm.x * 0.01 + rnd.x * 0.004, 1.0, sm.y * 0.01 + rnd.y * 0.004));\n  float F = 0.04 + 0.96 * pow(1.0 - clamp(dot(-rd, n), 0.0, 1.0), 5.0);\n  float dust = 0.6 + 0.4 * n2(o.xz * 14.0);\n  // a faint image of the lamp-lit room across the glass, heaviest toward the far edge\n  vec2 w = (o.xz - vec2(BX, BZ)) / vec2(WX, WZ);\n  float sheen = smoothstep(0.55, -0.2, w.y + w.x * 0.35) * smoothstep(-1.2, -0.3, w.y + w.x * 0.35) * 0.09 + 0.25 * smoothstep(0.03, 0.0, abs(w.x * 0.6 - w.y - 0.15)) * 0.0;\n  vec3 refl = env(o, reflect(rd, n), 0.0) * F + vec3(1.0, 0.85, 0.66) * sheen * dust;\n  return refl + (1.0 - F - sheen) * 0.97 * under(o, rd);\n}\nvec3 shadePanel(vec3 gp, vec3 rd, vec2 rnd){\n  vec3 gn = vec3(0.0, 1.0, 0.0);\n  float a = ao(gp, gn); float sh = shadow(gp + gn * 0.002);\n  vec3 alb = panelAlb(gp);\n  vec3 n = normalize(gn + vec3(n2(gp.xz * 40.0) - 0.5, 0.0, n2(gp.xz * 40.0 + 3.0) - 0.5) * 0.03 + vec3(rnd.x, 0.0, rnd.y) * 0.06);\n  float F = 0.04 + 0.96 * pow(1.0 - clamp(dot(-rd, n), 0.0, 1.0), 5.0);\n  vec3 col = alb * (KEYC * 2.0 * LK.y * sh * pool(gp) + vec3(0.025) * a) + vec3(1.0, 0.2, 0.06) * 0.22 * lampGlow(gp);\n  col += 0.5 * F * envAll(gp + gn * 0.002, reflect(rd, n), 0.32) * (0.25 + 0.75 * a) * (0.5 + 0.5 * sh);\n  return col;\n}\n\nvoid main(){\n  vec2 fc = gl_FragCoord.xy + uJit.xy;\n  vec2 uv = (fc - 0.5 * uRes) / uRes.y - uShift;\n  vec3 fw = normalize(uTgt - uCam); vec3 rt = normalize(cross(fw, vec3(0.0, 1.0, 0.0))); vec3 up = cross(rt, fw);\n  vec3 rd = normalize(fw * uFoc + rt * uv.x + up * uv.y);\n  vec3 ro = uCam;\n  vec3 fp = ro + rd * (uFocus / dot(rd, fw));\n  ro += rt * uJit.z + up * uJit.w; rd = normalize(fp - ro);\n  float ign = fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715))) + uSeed);\n  float ang = ign * 6.2831853; float rr = sqrt(fract(ign * 7.13 + uSeed * 3.7));\n  vec2 rnd = vec2(cos(ang), sin(ang)) * rr * step(0.001, uSeed);\n\n  float tpl = rd.y < 0.0 ? -ro.y / rd.y : 1e9;\n  vec3 pp = ro + rd * tpl;\n  bool slot = abs(pp.x) < SW && abs(pp.z) < SL;\n  if (slot) tpl = (-0.4 - ro.y) / rd.y;\n  float tgl = 1e9;\n  { float t = (GY - ro.y) / rd.y; vec3 q = ro + rd * t; if (t > 0.0 && inWin(q.xz)) tgl = t; }\n  float ts = march(ro, rd, min(tpl, tgl));\n  vec3 col;\n  if (ts > 0.0) {\n    vec3 p = ro + rd * ts; vec3 n = calcN(p);\n    float dP = sPlate(p); float dC = sCap(p); float dB = sBez(p); float dL = sLamp(p);\n    float m = min(min(dP, dC), min(dB, dL));\n    if (m == dC) col = shadeCap(p, n, rd, rnd);\n    else if (m == dP) col = shadePlate(p, n, rd, rnd);\n    else if (m == dB) col = shadeBez(p, n, rd, rnd);\n    else col = shadeLamp(p, n, rd);\n  }\n  else if (tgl < 1e8) col = shadeGlass(ro + rd * tgl, rd, rnd);\n  else if (slot) col = vec3(0.0015, 0.0012, 0.001);\n  else if (tpl < 1e8) col = shadePanel(ro + rd * tpl, rd, rnd);\n  else col = env(ro, rd, 0.0);\n  col = min(col, vec3(24.0));\n  if (uDirect > 0.5) col = tone(col);\n  outC = vec4(col, 1.0);\n}\n`;\n\nconst POST = `\nprecision highp float;\nuniform sampler2D uAcc; uniform vec2 uRes; uniform float uGrain; uniform vec2 uCentre;\n${TONE}\nfloat h21(vec2 p){ vec3 q = fract(vec3(p.xyx) * 0.1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); }\nvoid main(){\n  vec2 uv = gl_FragCoord.xy / uRes;\n  vec3 c = texture2D(uAcc, uv).rgb;\n  vec3 b = vec3(0.0);\n  for (int i = 0; i < 20; i++) {\n    float a = float(i) * 2.39996; float r = sqrt((float(i) + 0.5) / 20.0);\n    vec3 s = texture2D(uAcc, uv + vec2(cos(a), sin(a)) * r * 0.026 * vec2(uRes.y / uRes.x, 1.0)).rgb;\n    b += min(max(s - 1.2, 0.0), vec3(6.0)) * (1.0 - 0.6 * r);\n  }\n  c += b * 0.012 * vec3(1.0, 0.85, 0.65);\n  vec2 q = (uv - uCentre) * vec2(uRes.x / uRes.y, 1.0);\n  c *= 1.0 - 0.35 * smoothstep(0.35, 1.25, length(q));\n  c = tone(c);\n  float g = h21(gl_FragCoord.xy + uGrain * 91.7) - 0.5;\n  c += g * 0.022;\n  outC = vec4(c, 1.0);\n}\n`;\n\n// The engraved plate, drawn flat: red channel is black-filled engraving, green is red-filled.\nfunction platePrint() {\n  const k = 420, W = Math.round(PW * 2 * k), Hh = Math.round(PL * 2 * k);\n  const cv = document.createElement(\"canvas\");\n  cv.width = W; cv.height = Hh;\n  const g = cv.getContext(\"2d\");\n  if (!g) return cv;\n  g.fillStyle = \"#000\"; g.fillRect(0, 0, W, Hh);\n  const X = (x: number) => (x + PW) * k, Y = (z: number) => (z + PL) * k;\n  for (let v = 0; v <= MAX; v += 2) {\n    const major = MARKS.some((m) => m[0] === v);\n    if (major) continue;\n    g.fillStyle = \"#f00\";\n    g.fillRect(X(-0.19), Y(zOf(v)) - 0.006 * k, 0.07 * k, 0.012 * k);\n  }\n  g.textAlign = \"right\"; g.textBaseline = \"middle\";\n  for (const [v, d] of MARKS) {\n    const z = zOf(v), unity = d === 0;\n    g.fillStyle = unity ? \"#0f0\" : \"#f00\";\n    g.fillRect(X(-0.36), Y(z) - 0.011 * k, 0.29 * k, 0.022 * k);\n    g.fillRect(X(0.08), Y(z) - 0.011 * k, 0.2 * k, 0.022 * k);\n    g.font = `600 ${0.15 * k}px ${FONT}`;\n    const t = v === 0 ? \"∞\" : d > 0 ? `+${d}` : d === 0 ? \"0\" : String(-d);\n    g.fillText(t, X(-0.42), Y(z) + 0.006 * k);\n  }\n  g.fillStyle = \"#f00\"; g.textAlign = \"left\";\n  g.font = `600 ${0.1 * k}px ${FONT}`;\n  g.fillText(\"dB\", X(0.36), Y(zOf(100)));\n  g.save(); g.translate(X(0.5), Y(0.1)); g.rotate(Math.PI / 2);\n  g.textAlign = \"center\"; g.font = `500 ${0.085 * k}px ${FONT}`;\n  g.fillText(\"HALLOWAY SOUND  ·  DESK B\", 0, 0);\n  g.restore();\n  g.textAlign = \"center\"; g.font = `700 ${0.2 * k}px ${FONT}`;\n  g.fillText(\"07\", X(0.45), Y(1.98));\n  return cv;\n}\n\n// The meter card: cream stock, black and red scale, printed in the same geometry the needle uses.\nfunction facePrint() {\n  const k = 500, W = Math.round(WX * 2 * k), Hh = Math.round(WZ * 2 * k);\n  const cv = document.createElement(\"canvas\");\n  cv.width = W; cv.height = Hh;\n  const g = cv.getContext(\"2d\");\n  if (!g) return cv;\n  const grd = g.createLinearGradient(0, 0, 0, Hh);\n  grd.addColorStop(0, \"#f2e6c8\"); grd.addColorStop(1, \"#ead9b0\");\n  g.fillStyle = grd; g.fillRect(0, 0, W, Hh);\n  for (let i = 0; i < 2400; i++) {\n    g.fillStyle = `rgba(120,90,40,${Math.random() * 0.05})`;\n    g.fillRect(Math.random() * W, Math.random() * Hh, 1.5, 1.5);\n  }\n  const px = WX * k, py = (PIVZ - (BZ - WZ)) * k;\n  const at = (a: number, r: number): [number, number] => [px + Math.sin(a) * r * k, py - Math.cos(a) * r * k];\n  const arc = (r: number, a0: number, a1: number, w: number, col: string) => {\n    g.beginPath(); g.strokeStyle = col; g.lineWidth = w * k;\n    g.arc(px, py, r * k, a0 - Math.PI / 2, a1 - Math.PI / 2); g.stroke();\n  };\n  arc(0.96, angleOf(-20), angleOf(0), 0.008, \"#1b1712\");\n  arc(0.985, angleOf(0), angleOf(3), 0.05, \"#b3241a\");\n  const ticks = [-20, -10, -7, -5, -3, -2, -1, 0, 1, 2, 3];\n  g.textAlign = \"center\"; g.textBaseline = \"middle\";\n  for (const d of ticks) {\n    const a = angleOf(d), red = d > 0;\n    const [x0, y0] = at(a, 0.96), [x1, y1] = at(a, 1.05);\n    g.beginPath(); g.strokeStyle = red ? \"#b3241a\" : \"#1b1712\"; g.lineWidth = 0.009 * k;\n    g.moveTo(x0, y0); g.lineTo(x1, y1); g.stroke();\n    const [tx, ty] = at(a, 1.12);\n    g.fillStyle = red ? \"#b3241a\" : \"#1b1712\";\n    g.font = `600 ${0.072 * k}px ${FONT}`;\n    g.fillText(String(Math.abs(d)), tx, ty);\n  }\n  for (let d = -20; d <= 3; d += 0.5) {\n    if (ticks.includes(d)) continue;\n    if (d < -10 && d % 2 !== 0) continue;\n    const a = angleOf(d), [x0, y0] = at(a, 0.96), [x1, y1] = at(a, 1.0);\n    g.beginPath(); g.strokeStyle = d > 0 ? \"#b3241a\" : \"#1b1712\"; g.lineWidth = 0.005 * k;\n    g.moveTo(x0, y0); g.lineTo(x1, y1); g.stroke();\n  }\n  // the percentage scale under the arc\n  arc(0.84, angleOf(-20), angleOf(3), 0.004, \"#1b1712\");\n  g.font = `500 ${0.045 * k}px ${FONT}`; g.fillStyle = \"#1b1712\";\n  for (let pc = 0; pc <= 100; pc += 20) {\n    const a = -0.82 + ((pc / 100 - P20) / (P3 - P20)) * 1.64;\n    if (a < -0.85) continue;\n    const [x0, y0] = at(a, 0.84), [x1, y1] = at(a, 0.88);\n    g.beginPath(); g.lineWidth = 0.004 * k; g.moveTo(x0, y0); g.lineTo(x1, y1); g.stroke();\n    const [tx, ty] = at(a, 0.79);\n    g.fillText(String(pc), tx, ty);\n  }\n  g.fillStyle = \"#1b1712\"; g.font = `500 ${0.07 * k}px ${FONT}`;\n  const [mx, my] = at(-0.92, 1.12); g.fillText(\"−\", mx, my);\n  g.fillStyle = \"#b3241a\"; const [ppx, ppy] = at(0.92, 1.12); g.fillText(\"+\", ppx, ppy);\n  g.fillStyle = \"#1b1712\";\n  g.font = `italic 600 ${0.2 * k}px ${SERIF}`;\n  g.fillText(\"VU\", px, py - 0.5 * k);\n  g.font = `600 ${0.04 * k}px ${FONT}`;\n  g.fillText(\"H A L L O W A Y\", px, py - 0.37 * k);\n  return cv;\n}\n\n// No WebGL: the same channel strip, built from gradients.\nfunction Still({ value, needle }: { value: number; needle: number }) {\n  const f = value / MAX;\n  return (\n    <div aria-hidden=\"true\" className=\"absolute left-1/2 top-[32%] h-[520px] w-[520px] -translate-x-1/2 -translate-y-1/2 scale-[0.62] sm:scale-75 lg:left-[68%] lg:top-1/2 lg:scale-100\">\n      <div className=\"absolute left-[40px] top-[20px] h-[480px] w-[150px] rounded-[6px]\" style={{ background: \"linear-gradient(90deg, #6b4c1e 0%, #c9a052 30%, #e7c983 50%, #b88d3e 75%, #5e4318 100%)\", boxShadow: \"0 20px 40px rgba(0,0,0,0.6)\" }}>\n        <div className=\"absolute left-1/2 top-[24px] h-[432px] w-[10px] -translate-x-1/2 rounded-full bg-[#050403]\" />\n        <div className=\"absolute left-1/2 h-[54px] w-[120px] -translate-x-1/2 rounded-[8px]\" style={{ top: `${24 + (1 - f) * 380}px`, background: \"linear-gradient(180deg, #fff1c4 0%, #e0b35c 30%, #a87a2c 70%, #6a4a18 100%)\", boxShadow: \"0 16px 18px rgba(0,0,0,0.55), inset 0 -2px 0 rgba(0,0,0,0.3)\" }}>\n          <div className=\"absolute inset-x-2 top-1/2 h-[3px] -translate-y-1/2 bg-[#14110c]\" />\n        </div>\n      </div>\n      <div className=\"absolute left-[230px] top-[150px] h-[190px] w-[270px] overflow-hidden rounded-[14px] border-[12px] border-[#14110f]\" style={{ background: \"radial-gradient(120% 90% at 50% 100%, #fff1c9 0%, #f0dca9 50%, #c9a868 100%)\", boxShadow: \"0 20px 40px rgba(0,0,0,0.6)\" }}>\n        <div className=\"absolute bottom-[-40px] left-1/2 h-[190px] w-[2px] origin-bottom bg-[#120f0b]\" style={{ transform: `translateX(-50%) rotate(${needle}rad)` }} />\n        <p className=\"absolute inset-x-0 top-[60px] text-center text-[34px] italic text-[#1b1712]\" style={{ fontFamily: SERIF }}>VU</p>\n      </div>\n    </div>\n  );\n}\n\nexport default function BrassMixingDeskFader() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const hit = useRef<HTMLDivElement>(null);\n  const [value, setValue] = useState(START);\n  const [shown, setShown] = useState<\"wait\" | \"gl\" | \"still\">(\"wait\");\n  const valRef = useRef(START);\n  const api = useRef<{ go: (v: number, fromUser: boolean) => void } | null>(null);\n\n  useEffect(() => {\n    const box = stage.current, cv = canvas.current, pad = hit.current;\n    if (!box || !cv || !pad) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const opts = { antialias: false, alpha: false, powerPreference: \"high-performance\" as const };\n    const gl2 = cv.getContext(\"webgl2\", opts) as WebGL2RenderingContext | null;\n    const gl = (gl2 ?? cv.getContext(\"webgl\", opts)) as WebGLRenderingContext | null;\n    if (!gl) { setShown(\"still\"); return; }\n    const canAcc = !!gl2 && (!!gl2.getExtension(\"EXT_color_buffer_float\") || !!gl2.getExtension(\"EXT_color_buffer_half_float\"));\n    const info = gl.getExtension(\"WEBGL_debug_renderer_info\");\n    const renderer = info ? String(gl.getParameter(info.UNMASKED_RENDERER_WEBGL)) : \"\";\n    const software = /swiftshader|llvmpipe|software|basic render/i.test(renderer);\n    const still = reduce || software;\n\n    const head = gl2 ? \"#version 300 es\\n#define ATTR in\\n\" : \"#define ATTR attribute\\n\";\n    const fhead = gl2 ? \"#version 300 es\\n#define texture2D texture\\nprecision highp float;\\nout vec4 outC;\\n\" : \"#define outC gl_FragColor\\n\";\n    const compile = (type: number, src: string) => {\n      const sh = gl.createShader(type);\n      if (!sh) return null;\n      gl.shaderSource(sh, src); gl.compileShader(sh);\n      return gl.getShaderParameter(sh, gl.COMPILE_STATUS) ? sh : null;\n    };\n    const link = (frag: string) => {\n      const v = compile(gl.VERTEX_SHADER, head + VERT), f = compile(gl.FRAGMENT_SHADER, fhead + frag);\n      const p = gl.createProgram();\n      if (!v || !f || !p) return null;\n      gl.attachShader(p, v); gl.attachShader(p, f); gl.bindAttribLocation(p, 0, \"aPos\"); gl.linkProgram(p);\n      return gl.getProgramParameter(p, gl.LINK_STATUS) ? p : null;\n    };\n    const scene = link(SCENE), post = canAcc ? link(POST) : null;\n    if (!scene || (canAcc && !post)) { setShown(\"still\"); return; }\n    const U = (p: WebGLProgram, n: string) => gl.getUniformLocation(p, n);\n    const su = Object.fromEntries([\"uRes\", \"uJit\", \"uSeed\", \"uCam\", \"uTgt\", \"uFoc\", \"uShift\", \"uFocus\", \"uCapZ\", \"uLean\", \"uLift\", \"uNeedle\", \"uLamp\", \"uKey\", \"uPlate\", \"uFace\", \"uDirect\"].map((n) => [n, U(scene, n)]));\n    const pu = post ? Object.fromEntries([\"uAcc\", \"uRes\", \"uGrain\", \"uCentre\"].map((n) => [n, U(post, n)])) : {};\n\n    const quad = gl.createBuffer();\n    gl.bindBuffer(gl.ARRAY_BUFFER, quad);\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n    gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n\n    const aniso = gl.getExtension(\"EXT_texture_filter_anisotropic\");\n    const upload = (unit: number, img: HTMLCanvasElement) => {\n      const t = gl.createTexture();\n      gl.activeTexture(gl.TEXTURE0 + unit); gl.bindTexture(gl.TEXTURE_2D, t);\n      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);\n      if (gl2) gl.generateMipmap(gl.TEXTURE_2D);\n      if (aniso) gl.texParameterf(gl.TEXTURE_2D, aniso.TEXTURE_MAX_ANISOTROPY_EXT, 8);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl2 ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n    };\n    upload(0, platePrint());\n    upload(1, facePrint());\n\n    const acc = canAcc ? gl.createTexture() : null;\n    const fbo = canAcc ? gl.createFramebuffer() : null;\n    let W = 0, H = 0, scale = 1, cssW = 1, cssH = 1;\n    const s = {\n      z: zOf(START), vz: 0, lean: 0, vlean: 0, lift: 0, vlift: 0, target: zOf(START), drag: false, grab: 0,\n      nd: angleOf(dbOf(START) - 3.5), vn: 0, lamp: 0, lastV: START,\n      px: 0, py: 0, sx: 0, sy: 0, key: 0, count: 0, fresh: true, frame: 0, slow: 0, ft: 0, fn: 0, fps: 0,\n      visible: true, raf: 0, last: 0, t0: 0, touched: false, demoAt: 0, demoI: 0, first: true,\n    };\n    const size = () => {\n      const r = box.getBoundingClientRect();\n      cssW = Math.max(1, r.width); cssH = Math.max(1, r.height);\n      const dpr = Math.min(software ? 1 : 1.5, window.devicePixelRatio || 1) * scale;\n      const k = Math.min(1, Math.sqrt(1.3e6 / (cssW * cssH * dpr * dpr)));\n      const w = Math.max(2, Math.round(cssW * dpr * k)), h = Math.max(2, Math.round(cssH * dpr * k));\n      if (w === W && h === H) return;\n      W = w; H = h; cv.width = W; cv.height = H;\n      if (gl2 && acc && fbo) {\n        gl.activeTexture(gl.TEXTURE2); gl.bindTexture(gl.TEXTURE_2D, acc);\n        gl2.texImage2D(gl.TEXTURE_2D, 0, gl2.RGBA16F, W, H, 0, gl.RGBA, gl2.HALF_FLOAT, null);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n        gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);\n        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, acc, 0);\n        gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n      }\n      s.count = 0; s.fresh = true;\n    };\n    const halton = (i: number, b: number) => { let f = 1, r = 0; while (i > 0) { f /= b; r += f * (i % b); i = Math.floor(i / b); } return r; };\n\n    // A long lens, high over the desk and a little to the left, looking down the slot.\n    const camera = () => {\n      const wide = cssW >= 1024, aspect = cssW / cssH;\n      const el = (wide ? 0.9 : 0.98) - s.sy * 0.015, az = (wide ? -0.3 : -0.12) + s.sx * 0.04, dist = 15;\n      const tgt = wide ? [1.2, 0.0, -0.05] : [1.2, 0.0, 0.0];\n      const cam = [tgt[0] + dist * Math.sin(az) * Math.cos(el), tgt[1] + dist * Math.sin(el), tgt[2] + dist * Math.cos(az) * Math.cos(el)];\n      const foc = wide ? 2.55 * Math.min(1, aspect / 1.55) : 1.42 * Math.max(Math.min(1, aspect / 0.55), Math.min(1.55, aspect / 0.55));\n      const fw = [tgt[0] - cam[0], tgt[1] - cam[1], tgt[2] - cam[2]];\n      const fl = Math.hypot(fw[0], fw[1], fw[2]); fw[0] /= fl; fw[1] /= fl; fw[2] /= fl;\n      const rt = [-fw[2], 0, fw[0]]; const rl = Math.hypot(rt[0], rt[2]); rt[0] /= rl; rt[2] /= rl;\n      const up = [rt[1] * fw[2] - rt[2] * fw[1], rt[2] * fw[0] - rt[0] * fw[2], rt[0] * fw[1] - rt[1] * fw[0]];\n      const shift = wide ? [0.17 * aspect, 0.015] : [0, 0.13];\n      return { cam, tgt, fw, rt, up, foc, shift, wide };\n    };\n    // Where on the desk, at the height of the cap's top, a point on the screen lands.\n    const pick = (cx: number, cy: number) => {\n      const r = box.getBoundingClientRect(), c = camera();\n      const ux = (cx - r.left - r.width / 2) / r.height - c.shift[0], uy = (r.height / 2 - (cy - r.top)) / r.height - c.shift[1];\n      const d = [0, 1, 2].map((i) => c.fw[i] * c.foc + c.rt[i] * ux + c.up[i] * uy);\n      const t = (0.33 - c.cam[1]) / d[1];\n      return { x: c.cam[0] + d[0] * t, z: c.cam[2] + d[2] * t };\n    };\n\n    const draw = (alpha: number, sample: number) => {\n      const c = camera();\n      const jx = sample ? halton(sample, 2) - 0.5 : 0, jy = sample ? halton(sample, 3) - 0.5 : 0;\n      const la = halton(sample, 5) * Math.PI * 2, lr = sample ? Math.sqrt(halton(sample, 7)) * 0.13 : 0;\n      gl.useProgram(scene);\n      gl.bindFramebuffer(gl.FRAMEBUFFER, canAcc ? fbo : null);\n      gl.viewport(0, 0, W, H);\n      if (canAcc && alpha < 1) { gl.enable(gl.BLEND); gl.blendColor(0, 0, 0, alpha); gl.blendFunc(gl.CONSTANT_ALPHA, gl.ONE_MINUS_CONSTANT_ALPHA); } else gl.disable(gl.BLEND);\n      gl.uniform2f(su.uRes, W, H);\n      gl.uniform4f(su.uJit, canAcc ? jx : 0, canAcc ? jy : 0, canAcc ? Math.cos(la) * lr : 0, canAcc ? Math.sin(la) * lr : 0);\n      gl.uniform1f(su.uSeed, canAcc && sample ? (sample * 0.61803) % 1 || 0.5 : 0);\n      gl.uniform3f(su.uCam, c.cam[0], c.cam[1], c.cam[2]); gl.uniform3f(su.uTgt, c.tgt[0], c.tgt[1], c.tgt[2]);\n      gl.uniform1f(su.uFoc, c.foc);\n      gl.uniform2f(su.uShift, c.shift[0], c.shift[1]);\n      // focus rides on the cap\n      gl.uniform1f(su.uFocus, (0 - c.cam[0]) * c.fw[0] + (0.25 - c.cam[1]) * c.fw[1] + (s.z - c.cam[2]) * c.fw[2]);\n      gl.uniform1f(su.uCapZ, s.z); gl.uniform1f(su.uLean, s.lean); gl.uniform1f(su.uLift, s.lift);\n      gl.uniform1f(su.uNeedle, s.nd); gl.uniform1f(su.uLamp, s.lamp); gl.uniform1f(su.uKey, s.key);\n      gl.uniform1i(su.uPlate, 0); gl.uniform1i(su.uFace, 1);\n      gl.uniform1f(su.uDirect, canAcc ? 0 : 1);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n      if (canAcc && post) {\n        gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n        gl.disable(gl.BLEND);\n        gl.useProgram(post);\n        gl.uniform1i(pu.uAcc, 2); gl.uniform2f(pu.uRes, W, H);\n        gl.uniform1f(pu.uGrain, still ? 1 : s.frame % 64);\n        gl.uniform2f(pu.uCentre, c.wide ? 0.66 : 0.5, c.wide ? 0.5 : 0.68);\n        gl.drawArrays(gl.TRIANGLES, 0, 3);\n      }\n    };\n\n    const go = (v: number, fromUser: boolean) => {\n      const k = Math.max(0, Math.min(MAX, Math.round(v)));\n      if (fromUser) s.touched = true;\n      s.target = zOf(k);\n      if (k !== valRef.current) { valRef.current = k; setValue(k); }\n      if (reduce) { s.z = s.target; s.vz = 0; s.count = 0; s.fresh = true; }\n      wake();\n    };\n    api.current = { go };\n\n    // A program signal, roughly a song: a kick, a slow swell and some grit.\n    const program = (t: number) => {\n      const beat = Math.exp(-((t * 2.03) % 1) * 4.5), off = Math.exp(-(((t * 2.03) + 0.5) % 1) * 7);\n      return 0.56 + 0.2 * beat + 0.08 * off + 0.07 * Math.sin(t * 0.63) + 0.04 * Math.sin(t * 7.3) * Math.sin(t * 2.9);\n    };\n    const needleTarget = (t: number) => {\n      const v = vOfZ(s.z);\n      if (v < 0.3) return -0.96;\n      const db = 20 * Math.log10(Math.pow(10, dbOf(v) / 20) * program(t)) + 2.5;\n      return Math.max(-0.96, Math.min(0.97, angleOf(db)));\n    };\n\n    const frame = (now: number) => {\n      s.raf = 0;\n      if (!s.visible) return;\n      const dtRaw = s.last ? (now - s.last) / 1000 : 0.016;\n      const dt = Math.min(0.033, dtRaw);\n      s.last = now; s.frame++;\n      if (!s.t0) { s.t0 = now; s.demoAt = now + 3800; }\n      const t = (now - s.t0) / 1000;\n      s.ft += dtRaw; s.fn++; if (s.ft > 1) { s.fps = s.fn / s.ft; s.ft = 0; s.fn = 0; box.dataset.fps = s.fps.toFixed(0); }\n\n      if (!still && !s.touched && !s.drag && now > s.demoAt) {\n        go(DEMO[s.demoI % DEMO.length], false);\n        s.demoI++; s.demoAt = now + 3000;\n      }\n\n      let moving = false;\n      if (!reduce) {\n        const steps = 4, h = dt / steps;\n        for (let i = 0; i < steps; i++) {\n          // a heavy cap on greased rails: it lags the hand, then settles with one small overshoot\n          const stiff = s.drag ? 240 : 120, damp = s.drag ? 24 : 15;\n          const a = stiff * (s.target - s.z) - damp * s.vz;\n          s.vz += a * h; s.z += s.vz * h;\n          // it leans in the slot against the way it is pushed, and rights itself\n          s.vlean += (-260 * s.lean - 16 * s.vlean - a * 0.0028) * h; s.lean += s.vlean * h;\n          s.vlift += (-900 * s.lift - 26 * s.vlift) * h; s.lift += s.vlift * h;\n          // the meter: VU ballistics, a 300 ms rise with a touch of overshoot\n          const nt = still ? needleTarget(2.2) : needleTarget(t);\n          s.vn += (95 * (nt - s.nd) - 13 * s.vn) * h; s.nd += s.vn * h;\n          if (s.nd > 0.95) { s.nd = 0.95; s.vn = -Math.abs(s.vn) * 0.35; }\n          if (s.nd < -0.97) { s.nd = -0.97; s.vn = Math.abs(s.vn) * 0.3; }\n        }\n        // a detent: the cap drops into the notch with a clunk the needle feels too\n        const vNow = vOfZ(s.z);\n        for (const [m] of MARKS) {\n          if ((s.lastV - m) * (vNow - m) < 0 || (vNow === m && s.lastV !== m)) { s.vlift -= 0.5; s.vn += 0.6; s.vlean += Math.sign(s.vz) * 0.4; break; }\n        }\n        s.lastV = vNow;\n        if (Math.abs(s.vz) > 0.002 || Math.abs(s.target - s.z) > 0.0005 || Math.abs(s.lean) > 0.0004 || Math.abs(s.lift) > 0.0004 || Math.abs(s.vn) > 0.004) moving = true;\n        else { s.z = s.target; s.vz = 0; s.lean = 0; s.lift = 0; }\n      } else {\n        s.nd = needleTarget(2.2);\n      }\n      const peak = s.nd > angleOf(1.2) ? 1 : 0;\n      s.lamp += (peak - s.lamp) * (1 - Math.exp(-dt * (peak ? 30 : 4)));\n      if (!still) {\n        s.sx += (s.px - s.sx) * (1 - Math.exp(-dt * 3)); s.sy += (s.py - s.sy) * (1 - Math.exp(-dt * 3));\n        s.key = Math.sin(t * 0.35 + 0.4) * 0.5 + s.sx * 0.6;\n        moving = true;\n      }\n\n      const fast = Math.abs(s.vz) > 0.15 || Math.abs(s.vn) > 0.6;\n      if (s.fresh || !canAcc) { draw(1, 0); s.fresh = false; s.count = 1; }\n      else if (moving) { draw(fast ? 0.6 : still ? 0.5 : 0.22, 1 + (s.frame % 64)); s.count = still ? 1 : 6; }\n      else if (s.count < 40) { s.count++; draw(1 / s.count, s.count); }\n      if (s.first) { s.first = false; setShown(\"gl\"); }\n\n      if (!still && dtRaw > 0.026) s.slow++; else s.slow = Math.max(0, s.slow - 2);\n      if (s.slow > 45 && scale > 0.5) { scale *= 0.82; s.slow = 0; size(); }\n\n      if (moving || s.count < 40 || (!still && !s.touched)) s.raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!s.raf && s.visible) { s.last = 0; s.raf = requestAnimationFrame(frame); } };\n\n    size();\n    const ro = new ResizeObserver(() => { size(); wake(); });\n    ro.observe(box);\n    const io = new IntersectionObserver(([e]) => { s.visible = e.isIntersecting && !document.hidden; if (s.visible) wake(); }, { threshold: 0.05 });\n    io.observe(box);\n    const vis = () => { s.visible = !document.hidden; if (s.visible) wake(); };\n    document.addEventListener(\"visibilitychange\", vis);\n\n    // the cap drops into the nearest engraved mark when it passes close\n    const detent = (v: number) => {\n      for (const [m] of MARKS) if (Math.abs(v - m) < DETENT) return m;\n      return Math.round(v);\n    };\n    let id = -1;\n    const down = (e: PointerEvent) => {\n      const p = pick(e.clientX, e.clientY);\n      if (Math.abs(p.x) > 1.05 || Math.abs(p.z) > 2.4) return;\n      id = e.pointerId; s.drag = true; s.touched = true;\n      s.grab = Math.abs(p.z - s.z) < 0.3 ? s.z - p.z : 0;\n      try { pad.setPointerCapture(id); } catch { /* the pointer is already gone */ }\n      go(detent(vOfZ(p.z + s.grab)), true);\n    };\n    const move = (e: PointerEvent) => {\n      const r = box.getBoundingClientRect();\n      s.px = ((e.clientX - r.left) / r.width) * 2 - 1; s.py = ((e.clientY - r.top) / r.height) * 2 - 1;\n      if (s.drag && e.pointerId === id) {\n        const k = Math.max(0, Math.min(MAX, detent(vOfZ(pick(e.clientX, e.clientY).z + s.grab))));\n        s.target = zOf(k);\n        if (k !== valRef.current) { valRef.current = k; setValue(k); }\n      }\n      if (!still || s.drag) wake();\n    };\n    const upFn = (e: PointerEvent) => {\n      if (!s.drag || e.pointerId !== id) return;\n      s.drag = false; go(valRef.current, true);\n    };\n    const leave = () => { s.px = 0; s.py = 0; };\n    pad.addEventListener(\"pointerdown\", down);\n    box.addEventListener(\"pointermove\", move);\n    pad.addEventListener(\"pointerup\", upFn);\n    pad.addEventListener(\"pointercancel\", upFn);\n    box.addEventListener(\"pointerleave\", leave);\n    const lost = (e: Event) => { e.preventDefault(); setShown(\"still\"); };\n    cv.addEventListener(\"webglcontextlost\", lost);\n    wake();\n\n    return () => {\n      cancelAnimationFrame(s.raf); ro.disconnect(); io.disconnect();\n      document.removeEventListener(\"visibilitychange\", vis);\n      pad.removeEventListener(\"pointerdown\", down); box.removeEventListener(\"pointermove\", move);\n      pad.removeEventListener(\"pointerup\", upFn); pad.removeEventListener(\"pointercancel\", upFn);\n      box.removeEventListener(\"pointerleave\", leave); cv.removeEventListener(\"webglcontextlost\", lost);\n      api.current = null;\n    };\n  }, []);\n\n  const change = (v: number) => {\n    if (api.current) api.current.go(v, true); else { valRef.current = v; setValue(v); }\n  };\n  const db = label(value);\n  const stillNeedle = value <= 0 ? -0.96 : Math.max(-0.96, Math.min(0.95, angleOf(dbOf(value) - 1.6)));\n\n  return (\n    <section className=\"relative isolate overflow-hidden bg-[#0d0b09] text-[#f1e6d0]\" style={{ fontFamily: FONT }}>\n      <style>{`\n        .brass-fader-range { -webkit-appearance: none; appearance: none; background: transparent; height: 44px; cursor: pointer; }\n        .brass-fader-range::-webkit-slider-runnable-track { height: 2px; border-radius: 2px; background: linear-gradient(90deg, #d9a55a var(--fill), rgba(241,230,208,0.22) var(--fill)); }\n        .brass-fader-range::-moz-range-track { height: 2px; border-radius: 2px; background: rgba(241,230,208,0.22); }\n        .brass-fader-range::-moz-range-progress { height: 2px; background: #d9a55a; }\n        .brass-fader-range::-webkit-slider-thumb { -webkit-appearance: none; width: 22px; height: 14px; margin-top: -6px; border-radius: 3px; border: 0; background: linear-gradient(180deg, #fff0c2 0%, #d8a956 45%, #8a6223 100%); box-shadow: 0 2px 4px rgba(0,0,0,0.6), inset 0 -1px 0 rgba(0,0,0,0.3); transition: transform 160ms cubic-bezier(0.2, 0.9, 0.2, 1.4); }\n        .brass-fader-range::-moz-range-thumb { width: 22px; height: 14px; border-radius: 3px; border: 0; background: linear-gradient(180deg, #fff0c2 0%, #d8a956 45%, #8a6223 100%); box-shadow: 0 2px 4px rgba(0,0,0,0.6); }\n        .brass-fader-range:active::-webkit-slider-thumb { transform: scale(1.18); }\n        .brass-fader-range:focus-visible { outline: 2px solid #e2b064; outline-offset: 4px; border-radius: 6px; }\n        @media (prefers-reduced-motion: reduce) { .brass-fader-range::-webkit-slider-thumb { transition: none; } }\n      `}</style>\n      <div ref={stage} className=\"relative mx-auto h-[760px] w-full max-w-[1600px]\" style={{ background: \"radial-gradient(70% 70% at 66% 50%, #3a2a1a 0%, #1a130d 45%, #0d0b09 100%)\" }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className={`absolute inset-0 h-full w-full transition-opacity duration-700 motion-reduce:transition-none ${shown === \"gl\" ? \"opacity-100\" : \"opacity-0\"}`} />\n\n        {shown === \"still\" && <Still value={value} needle={stillNeedle} />}\n\n        <div ref={hit} className=\"absolute inset-x-0 top-0 h-[62%] cursor-grab select-none active:cursor-grabbing lg:bottom-0 lg:left-[36%] lg:top-0 lg:h-auto\" style={{ touchAction: \"none\" }} />\n\n        <div className=\"pointer-events-none absolute inset-0 flex flex-col justify-end px-6 pb-7 sm:px-10 lg:justify-center lg:px-16 lg:pb-0\">\n          <p className=\"hidden text-[11px] font-medium uppercase tracking-[0.34em] text-[#f1e6d0]/55 lg:block\">Halloway Sound <span className=\"mx-2 text-[#f1e6d0]/25\">/</span> channel 07</p>\n          <div className=\"lg:mt-8\">\n            <p className=\"flex items-start\">\n              <span className=\"text-[104px] font-semibold leading-[0.8] tracking-[-0.065em] tabular-nums sm:text-[140px] lg:text-[176px]\">{db}</span>\n              <span className=\"ml-3 mt-1 text-[22px] font-medium tracking-[-0.02em] text-[#e2b064] lg:ml-4 lg:mt-2 lg:text-[30px]\">dB</span>\n            </p>\n            <p className=\"mt-4 w-[24rem] max-w-[82vw] text-[19px] italic leading-snug text-[#f1e6d0]/75 lg:mt-6 lg:text-[23px]\" style={{ fontFamily: SERIF }}>{noteFor(value)}</p>\n            <div className=\"pointer-events-auto mt-4 flex w-[19rem] max-w-full items-center gap-4 lg:mt-6\">\n              <label htmlFor=\"brass-fader-level\" className=\"text-[11px] font-medium uppercase tracking-[0.3em] text-[#f1e6d0]/65\">Level</label>\n              <input\n                id=\"brass-fader-level\"\n                type=\"range\"\n                min={0}\n                max={MAX}\n                step={1}\n                value={value}\n                onChange={(e) => change(Number(e.target.value))}\n                aria-valuetext={`${value <= 0 ? \"minus infinity, muted\" : `${db.replace(\"−\", \"minus \")} decibels`}. ${noteFor(value)}`}\n                className=\"brass-fader-range min-w-0 flex-1\"\n                style={{ [\"--fill\" as string]: `${value}%` }}\n              />\n            </div>\n          </div>\n        </div>\n        <p className=\"pointer-events-none absolute bottom-7 right-6 hidden text-[11px] uppercase tracking-[0.3em] text-[#f1e6d0]/45 sm:right-10 lg:bottom-12 lg:right-16 lg:block\">Drag the cap</p>\n      </div>\n    </section>\n  );\n}\n"}]}
