{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"photoreal-html-css-button-set-of-glazed-ceramic-brushed-steel-walnut-and-frosted-glass-keys-on-travertine-in-window-light","type":"registry:block","title":"Photoreal HTML CSS Button Set of Glazed Ceramic, Brushed Steel, Walnut and Frosted Glass Keys on Travertine in Window Light","description":"Free React button set rendered in WebGL: four pressable keys in glazed ceramic, brushed steel, walnut and frosted glass, sitting in a stone slab under window light.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/photoreal-html-css-button-set-of-glazed-ceramic-brushed-steel-walnut-and-frosted-glass-keys-on-travertine-in-window-light.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n/** The four buttons, left to right. `label` is cut into the key top and is the button's accessible name. */\nconst keys = [\n  { label: \"Publish\", role: \"Primary\", material: \"Oxblood glazed ceramic\" },\n  { label: \"Save draft\", role: \"Secondary\", material: \"Brushed steel\" },\n  { label: \"Preview\", role: \"Tertiary\", material: \"Oiled walnut\" },\n  { label: \"Cancel\", role: \"Ghost\", material: \"Frosted glass\" },\n];\n\nconst TRAVEL = 0.5; // how far a key goes down, in centimetres\nconst REST_TOUCH = 0.1; // how far a key dips while the pointer rests on it (0 to 1 of the travel)\nconst DEMO = true; // presses each key in turn until somebody touches the piece\nconst GROTESK = '\"Inter Tight\", \"Inter\", ui-sans-serif, system-ui, sans-serif';\n\n/* ---- photoreal kit (pasted, see PHOTOREAL-KIT.md) ---- */\nconst FULLSCREEN_VS = `#version 300 es\nout vec2 vUv;\nvoid main() { vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2)); vUv = p; gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); }`;\nconst GLSL_HEADER = `#version 300 es\nprecision highp float;\nuniform vec2 uRes;      // drawing buffer size in pixels\nuniform float uFrame;   // sample index since the last reset (drives every random number)\nuniform float uTime;    // seconds, for things that really move\nuniform vec3 uCamPos, uCamRt, uCamUp, uCamFw;\nuniform float uTan, uAspect, uAperture, uFocus;\nuniform vec2 uShift;    // lens shift (architectural framing): move the subject in frame without tilting verticals\nout vec4 o;`;\nconst GLSL_COMMON = `\n#define PI 3.14159265359\nfloat saturate(float x) { return clamp(x, 0.0, 1.0); }\nfloat hash12(vec2 p) { vec3 p3 = fract(vec3(p.xyx) * 0.1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); }\nfloat hash13(vec3 p3) { p3 = fract(p3 * 0.1031); p3 += dot(p3, p3.zyx + 31.32); return fract((p3.x + p3.y) * p3.z); }\nfloat ign(vec2 p) { return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715)))); }\nfloat vnoise(vec2 p) {\n  vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash12(i), hash12(i + vec2(1, 0)), f.x), mix(hash12(i + vec2(0, 1)), hash12(i + vec2(1, 1)), f.x), f.y);\n}\nfloat vnoise3(vec3 p) {\n  vec3 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(mix(hash13(i), hash13(i + vec3(1, 0, 0)), f.x), mix(hash13(i + vec3(0, 1, 0)), hash13(i + vec3(1, 1, 0)), f.x), f.y),\n             mix(mix(hash13(i + vec3(0, 0, 1)), hash13(i + vec3(1, 0, 1)), f.x), mix(hash13(i + vec3(0, 1, 1)), hash13(i + vec3(1, 1, 1)), f.x), f.y), f.z);\n}\nfloat fbm(vec2 p) { float a = 0.5, s = 0.0; for (int i = 0; i < 5; i++) { s += a * vnoise(p); p = mat2(1.6, 1.2, -1.2, 1.6) * p + 3.1; a *= 0.5; } return s; }\nfloat fbm3(vec3 p) { float a = 0.5, s = 0.0; for (int i = 0; i < 4; i++) { s += a * vnoise3(p); p = p * 2.03 + vec3(1.7, 9.2, 3.1); a *= 0.5; } return s; }\n// Clamp one sample before it is averaged: a single lucky glossy ray hitting the window would otherwise leave\n// a bright speck (\"firefly\") that takes hundreds of frames to fade. Call on the final colour in main().\nvec3 clampSample(vec3 c, float m) { float l = max(c.r, max(c.g, c.b)); return l > m ? c * (m / l) : c; }\n// A new well-spread 2D random point per pixel AND per frame: the R2 low-discrepancy sequence over frames,\n// rotated by a per-pixel white-noise offset (structured offsets such as ign() show diagonal hatching while moving).\n// Use a different salt for every decision (pixel jitter, lens, sun disc, gloss...). Accumulation averages them.\nvec2 rand2(float salt) {\n  vec2 f = gl_FragCoord.xy + salt * 17.13;\n  return fract(vec2(hash12(f), hash12(f + 71.7)) + uFrame * vec2(0.7548776662, 0.5698402910));\n}`;\nconst GLSL_SAMPLING = `\nmat3 basisOf(vec3 n) { vec3 t = abs(n.y) < 0.999 ? normalize(cross(vec3(0.0, 1.0, 0.0), n)) : vec3(1.0, 0.0, 0.0); return mat3(t, cross(n, t), n); }\nvec2 diskSample(vec2 u) { float r = sqrt(u.x), a = 6.2831853 * u.y; return r * vec2(cos(a), sin(a)); }\nvec3 cosineSample(vec3 n, vec2 u) { vec2 d = diskSample(u); return basisOf(n) * vec3(d, sqrt(max(0.0, 1.0 - dot(d, d)))); }\n// a microfacet normal from the GGX distribution: reflect about it for a rough reflection, refract through it for frosted glass\nvec3 ggxSample(vec3 n, float rough, vec2 u) {\n  float a = rough * rough;\n  float ct = sqrt((1.0 - u.x) / (1.0 + (a * a - 1.0) * u.x)), st = sqrt(max(0.0, 1.0 - ct * ct)), ph = 6.2831853 * u.y;\n  return normalize(basisOf(n) * vec3(st * cos(ph), st * sin(ph), ct));\n}`;\nconst GLSL_CAMERA = `\n// Thin-lens camera. Sub-pixel jitter = supersampling; a random point on the lens = true depth of field.\n// uAperture is the lens radius in scene units (0 = pinhole). uFocus is the distance along the view axis that is sharp.\nvoid cameraRay(out vec3 ro, out vec3 rd) {\n  vec2 px = rand2(1.0) - 0.5;\n  vec2 ndc = (gl_FragCoord.xy + px) / uRes * 2.0 - 1.0 - uShift;\n  vec3 dir = normalize(uCamFw + uCamRt * (ndc.x * uTan * uAspect) + uCamUp * (ndc.y * uTan));\n  vec3 fp = uCamPos + dir * (uFocus / dot(dir, uCamFw));\n  vec2 lens = diskSample(rand2(2.0)) * uAperture;\n  ro = uCamPos + uCamRt * lens.x + uCamUp * lens.y;\n  rd = normalize(fp - ro);\n}`;\n// the light rig, edited for this scene: a small, low window so one pool of sun lands across the keys\nconst GLSL_RIG_SETTINGS = `\nconst vec3 KEY_DIR = normalize(vec3(-0.3, 0.52, -0.8));\nconst vec3 KEY_COL = vec3(1.0, 0.83, 0.64) * 2.1;\nconst float KEY_SPREAD = 0.006;\nconst float WIN_DIST = 140.0;\nconst vec2 WIN_HALF = vec2(13.0, 16.0);\nconst vec2 WIN_OFF = vec2(2.0, 4.0);\nconst float BAR = 0.8;\nconst float BAR_Y = 3.0;\nconst vec3 SKY_COL = vec3(0.58, 0.66, 0.78) * 0.17;\nconst vec3 FILL_DIR = normalize(vec3(0.3, 0.42, 1.0));\nconst vec3 FILL_COL = vec3(1.0, 0.95, 0.88) * 0.13;\nconst vec3 CEIL_COL = vec3(0.95, 0.9, 0.84) * 0.42;`;\nconst GLSL_LIGHT_RIG = `\n// the window opening with one vertical bar and one transom, in window-plane units\nfloat paneMask(vec2 q, float pen) {\n  vec2 e = WIN_HALF - abs(q);\n  float open = smoothstep(-pen, pen, e.x) * smoothstep(-pen, pen, e.y);\n  float bars = smoothstep(-pen, pen, abs(q.x) - BAR) * smoothstep(-pen, pen, abs(q.y - BAR_Y) - BAR);\n  return open * bars;\n}\n// The gobo: how much of the window a point sees along KEY_DIR. This is what makes light look like it came\n// from a real room instead of a CG directional light: a pool with straight edges and bar shadows.\nfloat keyGobo(vec3 p) {\n  mat3 B = basisOf(KEY_DIR);\n  vec2 q = vec2(dot(p, B[0]), dot(p, B[1])) - WIN_OFF;\n  float d = max(WIN_DIST - dot(p, KEY_DIR), 1.0);\n  return paneMask(q, 0.25 + d * KEY_SPREAD);\n}\n// What a mirror sees. The window appears as a bright shape with bars: that shape in every reflection is the\n// single strongest \"this is a photograph\" cue. Everything else is dark so reflections have contrast.\n// envStudioRough: the same room seen in a ROUGH surface. Instead of firing random glossy rays at a small, very\n// bright window (the source of sand-like speckle and sparkling metal), the window and card are blurred\n// analytically by the roughness. Deterministic: zero noise, converges in one frame.\nvec3 envStudioRough(vec3 rd, float rough) {\n  float a = rough * rough;                       // GGX alpha: roughly the angular blur in radians\n  vec3 c = mix(vec3(0.028, 0.025, 0.022), CEIL_COL, smoothstep(0.1 - a, 0.95 + a, rd.y));\n  c = mix(c, vec3(0.09, 0.075, 0.06), smoothstep(0.0, -0.5 - a, rd.y));\n  float k = dot(rd, KEY_DIR);\n  if (k > 0.0) {\n    mat3 B = basisOf(KEY_DIR);\n    vec2 q = vec2(dot(rd, B[0]), dot(rd, B[1])) / k * WIN_DIST - WIN_OFF;\n    float pen = 1.2 + a * WIN_DIST;\n    // a blurred window spreads the same light over a larger area: keep its energy, so the peak drops\n    float spread = (WIN_HALF.x * WIN_HALF.y) / ((WIN_HALF.x + pen) * (WIN_HALF.y + pen));\n    c += vec3(1.0, 0.96, 0.9) * 26.0 * paneMask(q, pen) * mix(1.0, spread, saturate(a * 4.0));\n  }\n  float f = dot(rd, FILL_DIR);\n  if (f > 0.0) {\n    mat3 B = basisOf(FILL_DIR);\n    vec2 e = vec2(0.6, 0.36) - abs(vec2(dot(rd, B[0]), dot(rd, B[1])) / f);\n    float w = 0.18 + a;\n    c += FILL_COL * 7.0 * smoothstep(-w, w, e.x) * smoothstep(-w, w, e.y);\n  }\n  return c;\n}\nvec3 envStudio(vec3 rd) { return envStudioRough(rd, 0.0); }`;\nconst GLSL_MATERIALS = `\n// q = position in the object's own frame, in CENTIMETRES (scale noise accordingly if your units differ).\n// n = geometric normal. Every material returns a perturbed normal: real surfaces are never perfect.\nstruct Mat { vec3 albedo; float rough; float metal; float coat; vec3 n; };\nMat mk(vec3 a, float r, float m, float c, vec3 n) { Mat x; x.albedo = a; x.rough = r; x.metal = m; x.coat = c; x.n = n; return x; }\n\n// Metals: these are reflectance (F0) colours in linear light, not paint colours.\nconst vec3 BRASS = vec3(0.88, 0.69, 0.37);\nconst vec3 GOLD = vec3(1.0, 0.76, 0.33);\nconst vec3 STEEL = vec3(0.56, 0.56, 0.54);\nconst vec3 ALUMINIUM = vec3(0.91, 0.92, 0.92);\nconst vec3 COPPER = vec3(0.95, 0.62, 0.50);\n\n// Brushed: fine parallel scratches along axis. Tilting the normal ACROSS the brushing stretches highlights\n// across it, which is how brushed metal really looks.\nMat matBrushed(vec3 q, vec3 n, vec3 f0, vec3 axis) {\n  vec3 b = normalize(cross(n, axis) + 1e-4);\n  float a = dot(q, axis), c = dot(q, b);\n  float s = 0.6 * vnoise(vec2(a * 0.5, c * 240.0)) + 0.4 * vnoise(vec2(a * 0.1, c * 700.0));\n  return mk(f0 * (0.96 + 0.04 * s), 0.24 + 0.04 * s, 1.0, 0.0, normalize(n + b * (s - 0.5) * 0.012));\n}\n// Polished: near mirror, with faint handling haze in patches (no real polished object is uniform).\nMat matPolished(vec3 q, vec3 n, vec3 f0) {\n  float h = fbm(q.xz * 0.9 + q.y);\n  return mk(f0, 0.04 + 0.09 * smoothstep(0.45, 0.75, h), 1.0, 0.0, n);\n}\n// Glazed ceramic: coloured body under a glass coat; glaze pools and thins, a few pinholes, gentle waviness.\nMat matCeramic(vec3 q, vec3 n, vec3 glaze) {\n  float pool = fbm(q.xz * 0.6 + q.y * 0.4);\n  float pin = step(0.996, hash13(floor(q * 22.0)));\n  vec3 w = vec3(fbm(q.xy * 1.3) - 0.5, 0.0, fbm(q.zy * 1.3 + 4.0) - 0.5);\n  return mk(glaze * (0.78 + 0.4 * pool) * (1.0 - 0.6 * pin), 0.35, 0.0, 1.0, normalize(n + w * 0.025));\n}\n// Matte paper or card: fibres, never flat white.\nMat matPaper(vec3 q, vec3 n, vec3 tint) {\n  float f = fbm(q.xz * 9.0) * 0.6 + vnoise(q.xz * 60.0) * 0.4;\n  return mk(tint * (0.93 + 0.08 * f), 0.88, 0.0, 0.0, normalize(n + vec3(f - 0.5, 0.0, f - 0.5) * 0.02));\n}\n// Linen: plain weave with slubs (thicker uneven threads), about 18 threads per cm.\nMat matLinen(vec3 q, vec3 n, vec3 tint) {\n  vec2 u = q.xz * 18.0;\n  float wx = 0.5 + 0.5 * sin(u.x * PI), wy = 0.5 + 0.5 * sin(u.y * PI);\n  float over = mod(floor(u.x) + floor(u.y), 2.0);\n  float th = mix(wx, wy, over);\n  float slub = vnoise(vec2(floor(u.x) * 3.1, u.y * 0.08)) * vnoise(vec2(u.x * 0.08, floor(u.y) * 2.7));\n  vec3 a = tint * (0.8 + 0.2 * th) * (0.92 + 0.16 * slub);\n  return mk(a, 0.9, 0.0, 0.0, normalize(n + vec3(cos(u.x * PI) * (1.0 - over), 0.0, cos(u.y * PI) * over) * 0.08));\n}\n// Plaster / limewash: broad trowel clouds and fine pores.\nMat matPlaster(vec3 q, vec3 n, vec3 tint) {\n  float c = fbm(q.xy * 0.18 + q.z * 0.1), p = vnoise(q.xy * 35.0 + q.z);\n  return mk(tint * (0.86 + 0.22 * c) * (0.97 + 0.04 * p), 0.92, 0.0, 0.0, normalize(n + vec3(p - 0.5, c - 0.5, 0.0) * 0.03));\n}\n// Honed travertine: cream with soft bands along one direction and long shallow pits.\nMat matTravertine(vec3 q, vec3 n) {\n  float band = fbm(vec2(q.z * 0.35 + fbm(q.xz * 0.08) * 2.0, q.x * 0.03));\n  vec3 a = mix(vec3(0.66, 0.58, 0.47), vec3(0.78, 0.71, 0.6), band);\n  float pitN = vnoise(vec2(q.x * 1.6, q.z * 9.0) + 7.0) * vnoise(vec2(q.x * 0.5, q.z * 3.0));\n  float pit = smoothstep(0.5, 0.58, pitN);\n  a = mix(a, vec3(0.52, 0.44, 0.34), pit * 0.3);\n  a *= 0.96 + 0.08 * vnoise(q.xz * 30.0);\n  return mk(a, mix(0.42, 0.9, pit), 0.0, 0.0, normalize(n + vec3(0.0, 0.0, pit * 0.08)));\n}\n// Marble: soft warped veins, polished.\nMat matMarble(vec3 q, vec3 n, vec3 base, vec3 vein) {\n  // thin veins that wander but do not curl: low warp, a main vein family and a fainter hairline family\n  float w = fbm3(q * 0.15) * 3.2;\n  float v = 1.0 - smoothstep(0.0, 0.05, abs(sin(q.x * 0.35 + q.z * 0.18 + w)));\n  float hair = 1.0 - smoothstep(0.0, 0.03, abs(sin(q.x * 0.9 - q.z * 0.6 + w * 1.7)));\n  vec3 a = mix(base, vein, v * 0.6 + hair * 0.2) * (0.96 + 0.08 * fbm(q.xz * 1.5));\n  return mk(a, 0.12, 0.0, 0.6, n);\n}\n// Wood, grain running along x. Photographic wood is LOW contrast: rings within 25% brightness, not stripes.\nconst vec3 OAK_LIGHT = vec3(0.52, 0.36, 0.20), OAK_DARK = vec3(0.36, 0.23, 0.12);\nconst vec3 WALNUT_LIGHT = vec3(0.20, 0.11, 0.06), WALNUT_DARK = vec3(0.11, 0.055, 0.03);\nMat matWood(vec3 q, vec3 n, vec3 light, vec3 dark, float pores) {\n  float r = length(q.yz + vec2(9.0, 4.0)) * 3.2 + fbm3(q * vec3(0.05, 0.7, 0.7)) * 3.0;\n  float ring = smoothstep(0.4, 0.95, fract(r));\n  float streak = vnoise(vec2(q.x * 0.05, q.z * 11.0 + q.y * 11.0)) * 0.6 + vnoise(vec2(q.x * 0.2, q.z * 40.0)) * 0.4;\n  vec3 a = mix(light, dark, ring * 0.3 + streak * 0.35);\n  float pore = smoothstep(0.78, 0.86, vnoise(vec2(q.x * 1.4, (q.z + q.y) * 55.0))) * pores;\n  a *= 1.0 - pore * 0.35;\n  return mk(a, 0.45 + 0.2 * pore, 0.0, 0.35, normalize(n + vec3(0.0, 0.0, (streak - 0.5) * 0.03)));\n}\n\n// Dielectrics (glass, acrylic, resin). You trace the refracted ray yourself; these do the physics around it.\nfloat fresnelD(float c, float ior) { float r = (1.0 - ior) / (1.0 + ior); r *= r; return r + (1.0 - r) * pow(1.0 - saturate(c), 5.0); }\n// Light lost to the colour of the body over a path of length len. absorb is per unit length per channel:\n// clear soda glass vec3(0.05, 0.012, 0.03) per cm (green edges); resin uses strong values for deep colour.\nvec3 beer(vec3 absorb, float len) { return exp(-absorb * len); }`;\nconst GLSL_SDF = `\nfloat sdRoundBox(vec3 p, vec3 b, float r) { vec3 q = abs(p) - b + r; return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r; }\nfloat smax(float a, float b, float k) { float h = max(k - abs(a - b), 0.0); return max(a, b) + h * h * 0.25 / k; }\nfloat smin(float a, float b, float k) { float h = max(k - abs(a - b), 0.0); return min(a, b) - h * h * 0.25 / k; }\n// ray vs box centred at the origin: (t near, t far)\nvec2 boxI(vec3 ro, vec3 rd, vec3 b) {\n  rd = mix(rd, vec3(1e-5), lessThan(abs(rd), vec3(1e-5)));\n  vec3 m = 1.0 / rd, n = m * ro, k = abs(m) * b;\n  vec3 t1 = -n - k, t2 = -n + k;\n  return vec2(max(max(t1.x, t1.y), t1.z), min(min(t2.x, t2.y), t2.z));\n}\nvec3 boxFaceNormal(vec3 p, vec3 b) { vec3 d = abs(p) / b; return d.x > d.y && d.x > d.z ? vec3(sign(p.x), 0, 0) : d.y > d.z ? vec3(0, sign(p.y), 0) : vec3(0, 0, sign(p.z)); }`;\nconst GLSL_SHADOW_AO = `\n// needs: const float SCENE_TOP, AO_RADIUS; float map(vec3); float mapShadow(vec3); vec3 lightFilter(vec3 p, vec3 l)\n// Deterministic penumbra (no random sun points: random shadow rays are the main source of floor speckle).\n// SHADOW_SOFT is the penumbra angle in radians: sun-sharp is ~0.01, overcast-soft ~0.08.\nconst float SHADOW_SOFT = 0.03;\nfloat softShadow(vec3 ro, vec3 rd, float tmin, float tmax) {\n  float res = 1.0, t = tmin, ph = 1e10;\n  for (int i = 0; i < 48; i++) {\n    vec3 p = ro + rd * t;\n    if (p.y > SCENE_TOP && rd.y > 0.0) break;\n    float h = mapShadow(p);\n    if (h < 0.0004) return 0.0;\n    float y = h * h / (2.0 * ph), d = sqrt(max(h * h - y * y, 0.0));\n    res = min(res, d / (SHADOW_SOFT * max(1e-3, t - y)));\n    ph = h;\n    t += clamp(h, 0.01, 3.0);\n    if (t > tmax) break;\n  }\n  res = saturate(res);\n  return res * res * (3.0 - 2.0 * res);\n}\nvec3 shadowCol(vec3 ro, vec3 rd) { return softShadow(ro, rd, 0.01, 200.0) * lightFilter(ro, rd); }\nfloat calcAO(vec3 p, vec3 n) {\n  float occ = 0.0, s = 1.0;\n  for (int i = 0; i < 5; i++) { float h = AO_RADIUS * (0.06 + 0.94 * float(i) / 4.0); occ += (h - map(p + n * h)) * s; s *= 0.72; }\n  return saturate(1.0 - 1.4 * occ / AO_RADIUS);\n}\nvec3 calcNormal(vec3 p, float e) {\n  vec2 k = vec2(1.0, -1.0);\n  return normalize(k.xyy * map(p + k.xyy * e) + k.yyx * map(p + k.yyx * e) + k.yxy * map(p + k.yxy * e) + k.xxx * map(p + k.xxx * e));\n}`;\nconst GLSL_LIGHTING = `\n// One opaque surface under the rig. Diffuse from the key (through the window gobo, with smooth analytic\n// shadows), cool sky and the card; specular reflects the room along the mirror direction with the window\n// pre-blurred by roughness (envStudioRough). Everything here is deterministic, so it adds NO per-frame noise:\n// accumulation only has to clean up edges, depth of field and any glass you trace yourself.\n// coat adds a sharp glaze/lacquer layer.\nvec3 litOpaque(vec3 p, vec3 v, Mat m, float ao) {\n  vec3 n = m.n;\n  float NoV = max(dot(n, v), 1e-3);\n  vec3 L = KEY_DIR;\n  float NoL = max(dot(n, L), 0.0);\n  vec3 sun = NoL > 0.0 ? KEY_COL * NoL * keyGobo(p) * shadowCol(p + n * 0.015, L) : vec3(0.0);\n  vec3 f0 = mix(vec3(0.04), m.albedo, m.metal);\n  vec3 F = f0 + (1.0 - f0) * pow(1.0 - NoV, 5.0);\n  vec3 amb = SKY_COL * (0.55 + 0.45 * n.y) + FILL_COL * 1.6 * max(dot(n, FILL_DIR) + 0.25, 0.0);\n  vec3 col = (1.0 - F) * (1.0 - m.metal) * m.albedo * (sun + amb * ao);\n  vec3 r = reflect(-v, n);\n  // rough surfaces lose some of the grazing Fresnel boost\n  vec3 Fr = f0 + (max(vec3(1.0 - m.rough), f0) - f0) * pow(1.0 - NoV, 5.0);\n  col += Fr * envStudioRough(r, m.rough) * mix(vec3(1.0), shadowCol(p + n * 0.015, r), 1.0 - m.rough * 0.6) * mix(1.0, ao, 0.6);\n  if (m.coat > 0.0) {\n    vec3 rc = reflect(-v, n);\n    float Fc = (0.04 + 0.96 * pow(1.0 - NoV, 5.0)) * m.coat;\n    col = col * (1.0 - Fc) + Fc * envStudio(rc) * shadowCol(p + n * 0.015, rc);\n  }\n  return col;\n}\n// Light that a lit translucent body gives off from inside (resin, wax, jade, frosted acrylic).\nvec3 subsurface(vec3 p, vec3 n, vec3 scatterCol, float strength) {\n  return scatterCol * strength * (KEY_COL * keyGobo(p) * (0.35 + 0.65 * max(dot(n, KEY_DIR), 0.0)) + SKY_COL);\n}`;\nconst OUTPUT_FS = `#version 300 es\nprecision highp float;\nuniform sampler2D uAccum;\nuniform vec2 uPx;\nuniform float uExposure, uGrain, uVignette, uSeed, uHalation;\nin vec2 vUv;\nout vec4 o;\nfloat ign(vec2 p) { return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715)))); }\nfloat hash(vec2 p) { vec3 p3 = fract(vec3(p.xyx) * 0.1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); }\n// ACES filmic fit: highlights roll off instead of clipping, which is most of the \"camera\" look\nvec3 aces(vec3 x) { return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0); }\nvoid main() {\n  vec3 c = texture(uAccum, vUv).rgb;\n  // halation: the brightest highlights bleed a warm glow, like light scattering in film\n  vec3 g = vec3(0.0);\n  float rot = 6.2831853 * ign(gl_FragCoord.xy);\n  for (int k = 0; k < 12; k++) {\n    float a = float(k) * 2.39996 + rot, r = 2.0 + 24.0 * sqrt(float(k) / 12.0);\n    g += max(texture(uAccum, vUv + vec2(cos(a), sin(a)) * r * uPx).rgb - 2.0, 0.0);\n  }\n  c += g / 12.0 * vec3(0.42, 0.3, 0.22) * uHalation;\n  c = aces(c * uExposure);\n  vec2 q = vUv - 0.5;\n  c *= 1.0 - uVignette * dot(q, q) * 2.0;\n  c = pow(c, vec3(1.0 / 2.2));\n  // grain lives in the midtones, never in pure black or white\n  float l = dot(c, vec3(0.299, 0.587, 0.114));\n  // static grain (it does not crawl), meant to be barely visible: default amplitude 0.012\n  c += (hash(gl_FragCoord.xy + uSeed) - 0.5) * uGrain * (0.25 + 3.0 * l * (1.0 - l));\n  o = vec4(c, 1.0);\n}`;\ntype Vec3 = [number, number, number];\ntype Camera = { pos: Vec3; rt: Vec3; up: Vec3; fw: Vec3; tan: number; aspect: number; shift: [number, number]; aperture: number; focus: number };\nconst sub = (a: Vec3, b: Vec3): Vec3 => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];\nconst dot = (a: Vec3, b: Vec3) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];\nconst norm = (a: Vec3): Vec3 => { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; };\nconst cross = (a: Vec3, b: Vec3): Vec3 => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];\nfunction lookAt(pos: Vec3, target: Vec3, fovYdeg: number, aspect: number, aperture = 0, shift: [number, number] = [0, 0]): Camera {\n  const fw = norm(sub(target, pos));\n  const rt = norm(cross(fw, [0, 1, 0]));\n  const up = cross(rt, fw);\n  return { pos, rt, up, fw, tan: Math.tan((fovYdeg * Math.PI) / 360), aspect, shift, aperture, focus: Math.hypot(...sub(target, pos)) };\n}\nfunction project(cam: Camera, p: Vec3, cssW: number, cssH: number): [number, number, number] {\n  const d = sub(p, cam.pos);\n  const z = dot(d, cam.fw);\n  const x = dot(d, cam.rt) / (z * cam.tan * cam.aspect) + cam.shift[0];\n  const y = dot(d, cam.up) / (z * cam.tan) + cam.shift[1];\n  return [(x * 0.5 + 0.5) * cssW, (0.5 - y * 0.5) * cssH, z];\n}\nfunction overlayRect(cam: Camera, center: Vec3, half: Vec3, cssW: number, cssH: number) {\n  let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;\n  for (let i = 0; i < 8; i++) {\n    const [x, y] = project(cam, [center[0] + (i & 1 ? half[0] : -half[0]), center[1] + (i & 2 ? half[1] : -half[1]), center[2] + (i & 4 ? half[2] : -half[2])], cssW, cssH);\n    x0 = Math.min(x0, x); y0 = Math.min(y0, y); x1 = Math.max(x1, x); y1 = Math.max(y1, y);\n  }\n  return { left: x0, top: y0, width: x1 - x0, height: y1 - y0 };\n}\nfunction compileProgram(gl: WebGL2RenderingContext, vs: string, fs: string) {\n  const p = gl.createProgram()!;\n  for (const [type, src] of [[gl.VERTEX_SHADER, vs], [gl.FRAGMENT_SHADER, fs]] as const) {\n    const s = gl.createShader(type)!;\n    gl.shaderSource(s, src);\n    gl.compileShader(s);\n    if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s) || \"shader\");\n    gl.attachShader(p, s);\n  }\n  gl.linkProgram(p);\n  if (!gl.getProgramParameter(p, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(p) || \"link\");\n  return p;\n}\ntype StageOptions = {\n  canvas: HTMLCanvasElement;\n  wrap: HTMLElement;\n  /** full fragment shader: GLSL_HEADER + chunks + your scene. Writes linear HDR to `o`. */\n  sceneFS: string;\n  /** called once per CSS resize, before drawing: build your camera here and return it */\n  camera: (cssW: number, cssH: number) => Camera;\n  /** your uniforms, every rendered frame (program is in use) */\n  uniforms?: (gl: WebGL2RenderingContext, loc: (name: string) => WebGLUniformLocation | null, t: number) => void;\n  /** advance your simulation; return true while anything visible is still moving */\n  step?: (dt: number, t: number) => boolean;\n  /** samples per pixel when at rest (then drawing stops). Default 128; never below 96 */\n  samples?: number;\n  /** blend weight of each new frame while moving: 0.25 (default) keeps ~4 frames of history; raise for fast motion */\n  motionBlend?: number;\n  maxPixels?: number; // cap on drawing buffer pixels, default 2.2e6\n  maxDpr?: number; // default 2\n  exposure?: number; grain?: number; vignette?: number; halation?: number;\n  onFail?: () => void; // WebGL 2 missing, shader error or context lost: show your CSS still\n};\nfunction createPhotoStage(o: StageOptions) {\n  const { canvas, wrap } = o;\n  const fail = () => { o.onFail?.(); return null; };\n  const gl = canvas.getContext(\"webgl2\", { antialias: false, alpha: false, depth: false, powerPreference: \"high-performance\" });\n  if (!gl) return fail();\n  const float = !!gl.getExtension(\"EXT_color_buffer_float\");\n  let scene: WebGLProgram, out: WebGLProgram;\n  try {\n    scene = compileProgram(gl, FULLSCREEN_VS, o.sceneFS);\n    out = compileProgram(gl, FULLSCREEN_VS, OUTPUT_FS);\n  } catch (e) {\n    console.error(e);\n    return fail();\n  }\n  const SAMPLES = o.samples ?? 128;\n  const MOTION = o.motionBlend ?? 0.25;\n  let frameNo = 0;\n  const maxPixels = o.maxPixels ?? 2.2e6;\n  let scale = Math.min(window.devicePixelRatio || 1, o.maxDpr ?? 2);\n  const vao = gl.createVertexArray();\n  const fbo = gl.createFramebuffer();\n  const tex = gl.createTexture();\n  const locS = new Map<string, WebGLUniformLocation | null>();\n  const loc = (n: string) => { if (!locS.has(n)) locS.set(n, gl.getUniformLocation(scene, n)); return locS.get(n)!; };\n  const locO = (n: string) => gl.getUniformLocation(out, n);\n  let W = 0, H = 0, cssW = 1, cssH = 1, n = 0, cam: Camera | null = null;\n\n  const resize = () => {\n    cssW = Math.max(1, wrap.clientWidth); cssH = Math.max(1, wrap.clientHeight);\n    let k = scale;\n    if (cssW * cssH * k * k > maxPixels) k = Math.sqrt(maxPixels / (cssW * cssH));\n    cam = o.camera(cssW, cssH);\n    n = 0;\n    const w = Math.max(2, Math.round(cssW * k)), h = Math.max(2, Math.round(cssH * k));\n    if (w === W && h === H) return;\n    W = w; H = h; canvas.width = W; canvas.height = H;\n    gl.bindTexture(gl.TEXTURE_2D, tex);\n    gl.texImage2D(gl.TEXTURE_2D, 0, float ? gl.RGBA16F : gl.RGBA8, W, H, 0, gl.RGBA, float ? gl.HALF_FLOAT : gl.UNSIGNED_BYTE, 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, tex, 0);\n  };\n\n  const draw = (t: number, moving: boolean) => {\n    const c = cam!;\n    gl.bindVertexArray(vao);\n    gl.viewport(0, 0, W, H);\n    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);\n    gl.useProgram(scene);\n    gl.uniform2f(loc(\"uRes\"), W, H);\n    gl.uniform1f(loc(\"uFrame\"), frameNo++); // never repeats, so moving frames keep exploring new samples\n    gl.uniform1f(loc(\"uTime\"), t);\n    gl.uniform3fv(loc(\"uCamPos\"), c.pos); gl.uniform3fv(loc(\"uCamRt\"), c.rt); gl.uniform3fv(loc(\"uCamUp\"), c.up); gl.uniform3fv(loc(\"uCamFw\"), c.fw);\n    gl.uniform1f(loc(\"uTan\"), c.tan); gl.uniform1f(loc(\"uAspect\"), c.aspect); gl.uniform1f(loc(\"uAperture\"), c.aperture); gl.uniform1f(loc(\"uFocus\"), c.focus);\n    gl.uniform2fv(loc(\"uShift\"), c.shift);\n    o.uniforms?.(gl, loc, t);\n    // blend the new jittered frame into the running average: alpha 1/(n+1) is an exact mean;\n    // while moving keep a SHORT history instead of restarting: alpha 0.25 is about 4 frames (a touch of motion\n    // blur, no speckle). When it stops, carry on averaging from there: n continues at 1/MOTION samples.\n    const alpha = moving ? MOTION : 1 / (n + 1);\n    gl.enable(gl.BLEND);\n    gl.blendFunc(gl.CONSTANT_ALPHA, gl.ONE_MINUS_CONSTANT_ALPHA);\n    gl.blendColor(0, 0, 0, n === 0 ? 1 : alpha);\n    gl.drawArrays(gl.TRIANGLES, 0, 3);\n    gl.disable(gl.BLEND);\n    gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n    gl.useProgram(out);\n    gl.activeTexture(gl.TEXTURE0);\n    gl.bindTexture(gl.TEXTURE_2D, tex);\n    gl.uniform1i(locO(\"uAccum\"), 0);\n    gl.uniform2f(locO(\"uPx\"), 1 / W, 1 / H);\n    gl.uniform1f(locO(\"uExposure\"), o.exposure ?? 1);\n    gl.uniform1f(locO(\"uGrain\"), o.grain ?? 0.012);\n    gl.uniform1f(locO(\"uVignette\"), o.vignette ?? 0.35);\n    gl.uniform1f(locO(\"uHalation\"), o.halation ?? 0.6);\n    gl.uniform1f(locO(\"uSeed\"), 0);\n    gl.drawArrays(gl.TRIANGLES, 0, 3);\n    n = moving ? Math.round(1 / MOTION) : n + 1;\n  };\n\n  resize();\n  const ro = new ResizeObserver(resize);\n  ro.observe(wrap);\n\n  let raf = 0, last = performance.now(), slow = 0, onScreen = true, dead = false;\n  const t0 = last;\n  const tick = (now: number) => {\n    raf = 0;\n    const real = now - last;\n    last = now;\n    const dt = Math.min(real / 1000, 0.05);\n    const t = (now - t0) / 1000;\n    const moving = o.step ? o.step(dt, t) : false;\n    if (moving || n < SAMPLES) {\n      if (moving) {\n        // measure, do not guess: if moving frames run long, draw fewer pixels\n        slow = slow * 0.9 + (real > 28 ? 0.1 : 0);\n        if (slow > 0.55 && scale > 0.45) { scale *= 0.85; slow = 0; W = 0; resize(); }\n      }\n      draw(t, moving);\n    }\n    if (onScreen && !document.hidden) raf = requestAnimationFrame(tick);\n  };\n  const wake = () => { if (!raf && !dead && onScreen && !document.hidden) { last = performance.now(); raf = requestAnimationFrame(tick); } };\n  const io = new IntersectionObserver(([e]) => { onScreen = e.isIntersecting; wake(); });\n  io.observe(wrap);\n  document.addEventListener(\"visibilitychange\", wake);\n  const lost = (e: Event) => { e.preventDefault(); dead = true; o.onFail?.(); };\n  canvas.addEventListener(\"webglcontextlost\", lost);\n  wake();\n\n  return {\n    /** something changed that is not animated (theme, text, reduced-motion snap): restart accumulation */\n    invalidate() { n = 0; wake(); },\n    /** call after step() starts moving from rest (input), so the loop is running */\n    wake,\n    camera: () => cam!,\n    size: () => [cssW, cssH] as const,\n    dispose() {\n      dead = true;\n      cancelAnimationFrame(raf);\n      ro.disconnect(); io.disconnect();\n      document.removeEventListener(\"visibilitychange\", wake);\n      canvas.removeEventListener(\"webglcontextlost\", lost);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n    },\n  };\n}\nfunction springStep(s: { x: number; v: number }, target: number, dt: number, k = 900, damping = 38) {\n  const sub = Math.max(1, Math.ceil(dt / 0.004));\n  for (let i = 0; i < sub; i++) {\n    const h = dt / sub;\n    s.v += (k * (target - s.x) - damping * s.v) * h;\n    s.x += s.v * h;\n  }\n  if (Math.abs(s.v) < 0.02 && Math.abs(target - s.x) < 0.002) { s.x = target; s.v = 0; return false; }\n  return true;\n}\nfunction stillObject(face: string, opts: { radius?: number; depth?: number } = {}) {\n  const r = opts.radius ?? 14, d = opts.depth ?? 10;\n  return {\n    background: face,\n    borderRadius: r,\n    boxShadow: [\n      \"inset 0 1px 0 rgba(255,255,255,0.35)\",\n      \"inset 0 -2px 3px rgba(0,0,0,0.22)\",\n      `0 ${d * 0.4}px ${d * 0.3}px rgba(0,0,0,0.28)`,\n      `${d * 1.6}px ${d * 2.2}px ${d * 3}px rgba(20,12,4,0.30)`,\n    ].join(\", \"),\n  };\n}\nconst STILL_GRAIN =\n  \"url(\\\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.5 0 0 0 0 0.5 0 0 0 0 0.5 0 0 0 0.9 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>\\\")\";\nfunction stillWindowLight(warm = \"255,236,205\") {\n  return {\n    background: `linear-gradient(115deg, transparent 18%, rgba(${warm},0.55) 30%, rgba(${warm},0.55) 47%, transparent 48.5%, transparent 50%, rgba(${warm},0.5) 51.5%, rgba(${warm},0.45) 70%, transparent 82%)`,\n    filter: \"blur(6px)\",\n    mixBlendMode: \"soft-light\" as const,\n  };\n}\n/* ---- end of kit ---- */\n\n// The scene: four keys sitting in wells cut into a honed travertine slab, under window light. Units are centimetres.\nconst KX = 3.0; // half the key's width\nconst PITCH = 7.6; // key centre to key centre, in centimetres\nconst SCENE = `\nconst float SCENE_TOP = 1.2;\nconst float AO_RADIUS = 1.4;\nuniform float uPress[4];\nuniform vec2 uKP[4]; // where each key stands on the slab (x, z): a row on wide screens, two by two on phones\nuniform sampler2D uLegend;\nconst float KX = ${KX.toFixed(2)}, KYH = 0.75, KR = 0.42, PITCH = ${PITCH.toFixed(2)};\nconst vec3 GLASS_ABS = vec3(0.07, 0.018, 0.04);\n\nvec3 keyC(int i) { return vec3(uKP[i].x, 0.25 - ${TRAVEL.toFixed(2)} * uPress[i], uKP[i].y); }\nvec2 scene(vec3 p, int skip) {\n  float w = 1e9, d = 1e9, id = 0.0;\n  for (int i = 0; i < 4; i++) {\n    vec3 q = p - vec3(uKP[i].x, 0.0, uKP[i].y);\n    w = min(w, sdRoundBox(q - vec3(0.0, -0.4, 0.0), vec3(KX + 0.2, 1.0, KX + 0.2), 0.3));\n    if (i != skip) { float k = sdRoundBox(p - keyC(i), vec3(KX, KYH, KX), KR); if (k < d) { d = k; id = float(i + 1); } }\n  }\n  float slab = smax(p.y, -w, 0.16);\n  return slab < d ? vec2(slab, 0.0) : vec2(d, id);\n}\nfloat map(vec3 p) { return scene(p, -1).x; }\n// the glass key does not block the sun; lightFilter tints and focuses what passes through it instead\nfloat mapShadow(vec3 p) { return scene(p, 3).x; }\nvec3 lightFilter(vec3 p, vec3 l) {\n  vec3 c = keyC(3), b = vec3(KX - 0.15, KYH, KX - 0.15);\n  vec2 tb = boxI(p - c, l, b);\n  if (tb.y <= max(tb.x, 0.0)) return vec3(1.0);\n  float len = tb.y - max(tb.x, 0.0);\n  vec3 e = b - abs(p - c + l * (max(tb.x, 0.0) + tb.y) * 0.5);\n  float rim = min(e.x, e.z);\n  return beer(GLASS_ABS, len) * (0.35 + 0.95 * smoothstep(0.0, 0.7, rim));\n}\n`;\nconst SCENE_MAIN = `\nfloat legendAt(int i, vec2 uv) { uv = clamp(uv, 0.002, 0.998); return texture(uLegend, vec2((float(i) + uv.x) * 0.25, uv.y)).r; }\n// the lettering on a key top, and the slope of its walls (for engraved or raised letters)\nvec3 legend(int i, vec3 q, vec3 n) {\n  float top = smoothstep(0.8, 0.97, n.y);\n  if (top <= 0.0) return vec3(0.0);\n  vec2 uv = q.xz / (2.0 * (KX - 0.25)) + 0.5;\n  float e = 1.0 / 512.0;\n  return top * vec3(legendAt(i, uv), legendAt(i, uv + vec2(e, 0.0)) - legendAt(i, uv - vec2(e, 0.0)), legendAt(i, uv + vec2(0.0, e)) - legendAt(i, uv - vec2(0.0, e)));\n}\nvec3 shadeKey(int i, vec3 p, vec3 v, vec3 n, float ao) {\n  vec3 q = p - keyC(i);\n  vec3 lg = legend(i, q, n);\n  Mat m;\n  if (i == 0) {\n    // glaze over the whole top, lettering in a raised cream slip\n    m = matCeramic(q, normalize(n - vec3(lg.y, 0.0, lg.z) * 0.9), vec3(0.17, 0.02, 0.017));\n    m.albedo = mix(m.albedo, vec3(0.80, 0.70, 0.54), lg.x);\n    m.rough = mix(m.rough, 0.5, lg.x);\n  } else if (i == 1) {\n    // engraved and filled with black enamel\n    m = matBrushed(q, normalize(n + vec3(lg.y, 0.0, lg.z) * 0.9), ALUMINIUM * 0.82, vec3(1.0, 0.0, 0.0));\n    m.rough += 0.08;\n    m.albedo = mix(m.albedo, vec3(0.012), lg.x);\n    m.metal = 1.0 - lg.x;\n    m.rough = mix(m.rough, 0.45, lg.x);\n  } else {\n    // cut into the oiled wood and filled with cream paint\n    m = matWood(q + vec3(float(i) * 11.0, 0.0, 0.0), normalize(n + vec3(lg.y, 0.0, lg.z) * 0.9), WALNUT_LIGHT, WALNUT_DARK, 1.0);\n    m.albedo = mix(m.albedo, vec3(0.72, 0.62, 0.46), lg.x);\n    m.coat *= 1.0 - lg.x;\n  }\n  return litOpaque(p, v, m, ao);\n}\nvec3 shadeOpaque(int id, vec3 p, vec3 v) {\n  vec3 n = calcNormal(p, 0.002);\n  float ao = calcAO(p, n);\n  if (id == 0) return litOpaque(p, v, matTravertine(p, n), ao);\n  return shadeKey(id - 1, p, v, n, ao);\n}\n// what the bent ray sees under and beyond the glass key: the well, the stone, the next key\nvec3 traceBehind(vec3 ro, vec3 rd) {\n  float t = 0.0;\n  for (int s = 0; s < 72; s++) {\n    vec2 h = scene(ro + rd * t, 3);\n    if (h.x < 0.0004 * (t + 1.0)) return shadeOpaque(int(h.y + 0.5), ro + rd * t, -rd);\n    t += h.x;\n    if (t > 300.0 || (ro + rd * t).y > SCENE_TOP + 1.0) break;\n  }\n  return envStudio(rd);\n}\nvec3 shadeGlass(vec3 p, vec3 v, vec3 n) {\n  vec3 c = keyC(3), q = p - c, b = vec3(KX, KYH, KX);\n  vec3 lg = legend(3, q, n);\n  vec3 nb = normalize(n + vec3(lg.y, 0.0, lg.z) * 0.6);\n  // polished glass with a sandblasted legend: the blast is just a much rougher surface\n  float frost = mix(0.22, 0.6, lg.x);\n  // reflection is deterministic (window pre-blurred by the frost); only the refraction is sampled\n  float F = fresnelD(max(dot(v, nb), 0.0), 1.5);\n  vec3 r = reflect(-v, nb);\n  vec3 col = F * envStudioRough(r, frost) * shadowCol(p + n * 0.02, r);\n  vec3 h = ggxSample(nb, frost, rand2(5.0));\n  vec3 dir = refract(-v, h, 1.0 / 1.5), pos = q, thr = vec3(1.0 - F);\n  for (int k = 0; k < 3; k++) {\n    float len = max(boxI(pos, dir, b).y, 0.0);\n    thr *= beer(GLASS_ABS, len);\n    pos += dir * len;\n    vec3 ne = boxFaceNormal(pos, b);\n    vec3 od = refract(dir, -ne, 1.5);\n    if (dot(od, od) > 1e-4) {\n      thr *= 1.0 - fresnelD(dot(od, ne), 1.5);\n      col += thr * traceBehind(c + pos + od * 0.02, od);\n      break;\n    }\n    dir = reflect(dir, ne);\n    pos -= ne * 0.002;\n  }\n  // frosted glass scatters part of the light inside it: the body glows where the sun reaches it\n  col += subsurface(p, n, vec3(0.72, 0.84, 0.8), 0.13);\n  // light the blasted letters catch and scatter\n  col += lg.x * (KEY_COL * keyGobo(p) * 0.05 + SKY_COL * 0.6 + FILL_COL * 0.8) * vec3(0.9, 0.93, 0.92);\n  return col;\n}\nvoid main() {\n  vec3 ro, rd;\n  cameraRay(ro, rd);\n  float t = 0.0;\n  if (ro.y > SCENE_TOP) {\n    if (rd.y >= 0.0) { o = vec4(envStudio(rd), 1.0); return; }\n    t = (SCENE_TOP - ro.y) / rd.y;\n  }\n  vec2 h = vec2(1e9, 0.0);\n  bool hit = false;\n  for (int s = 0; s < 160; s++) {\n    h = scene(ro + rd * t, -1);\n    if (h.x < 0.00025 * t) { hit = true; break; }\n    t += h.x;\n    if (t > 900.0) break;\n  }\n  vec3 p = ro + rd * t, col;\n  int id = int(h.y + 0.5);\n  if (!hit) col = envStudio(rd);\n  else if (id == 4) col = shadeGlass(p, -rd, calcNormal(p, 0.002));\n  else col = shadeOpaque(id, p, -rd);\n  o = vec4(clampSample(col, 5.0), 1.0);\n}\n`;\n\nconst SCENE_FS = [GLSL_HEADER, GLSL_COMMON, GLSL_SAMPLING, GLSL_CAMERA, GLSL_RIG_SETTINGS, GLSL_LIGHT_RIG, GLSL_MATERIALS, GLSL_SDF, SCENE, GLSL_SHADOW_AO, GLSL_LIGHTING, SCENE_MAIN].join(\"\\n\");\n\n/** The lettering for all four key tops, white on black, one 512px tile per key. */\nfunction legendArt() {\n  const cv = document.createElement(\"canvas\");\n  cv.width = 2048;\n  cv.height = 512;\n  const ctx = cv.getContext(\"2d\")!;\n  ctx.fillStyle = \"#000\";\n  ctx.fillRect(0, 0, 2048, 512);\n  ctx.fillStyle = \"#fff\";\n  ctx.textAlign = \"center\";\n  ctx.textBaseline = \"middle\";\n  ctx.filter = \"blur(1.4px)\"; // soft walls, so the letters read as cut, not printed\n  keys.forEach((k, i) => {\n    let size = 92;\n    ctx.font = `600 ${size}px ${GROTESK}`;\n    const w = ctx.measureText(k.label).width;\n    if (w > 330) size = Math.floor((size * 330) / w);\n    ctx.font = `600 ${size}px ${GROTESK}`;\n    ctx.fillText(k.label, i * 512 + 256, 262);\n  });\n  return cv;\n}\n\ntype Sim = { press: { x: number; v: number }[]; held: boolean[]; hover: boolean[]; touched: boolean; layout: number[][] };\n\nexport default function PhotorealButtonSet() {\n  const reduce = useReducedMotion();\n  const wrapRef = useRef<HTMLDivElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const [mode, setMode] = useState<\"pending\" | \"gl\" | \"still\">(\"pending\");\n  const [rects, setRects] = useState<{ left: number; top: number; width: number; height: number }[]>([]);\n  const [last, setLast] = useState<string | null>(null);\n  const sim = useRef<Sim>({ press: keys.map(() => ({ x: 0, v: 0 })), held: keys.map(() => false), hover: keys.map(() => false), touched: false, layout: [] });\n\n  useEffect(() => {\n    const wrap = wrapRef.current, canvas = canvasRef.current;\n    if (!wrap || !canvas) return;\n    const s = sim.current;\n    let legendTex: WebGLTexture | null = null;\n    let demoAt = 3.0, step = 0, pressUntil = -1, demoKey = -1;\n    const stage = createPhotoStage({\n      canvas,\n      wrap,\n      sceneFS: SCENE_FS,\n      samples: 128,\n      exposure: 1.0,\n      vignette: 0.42,\n      onFail: () => setMode(\"still\"),\n      camera: (w, h) => {\n        const aspect = w / h;\n        const fov = 15;\n        const grid = w < 560;\n        s.layout = keys.map((_, i) => (grid ? [(i % 2 - 0.5) * PITCH, (Math.floor(i / 2) - 0.5) * PITCH * 1.12] : [(i - 1.5) * PITCH, 0]));\n        const half = grid ? 7.4 : 14.6; // the keys, plus a margin, must fit across\n        const dist = Math.max(grid ? 66 : 80, half / (Math.tan((fov * Math.PI) / 360) * aspect));\n        const el = 0.5;\n        const target: Vec3 = [0, 0, 0.6];\n        const cam = lookAt([0, Math.sin(el) * dist, 0.6 + Math.cos(el) * dist], target, fov, aspect, dist * 0.016, [0, aspect < 0.9 ? 0.02 : 0.05]);\n        setRects(keys.map((_, i) => overlayRect(cam, [s.layout[i][0], 0.25, s.layout[i][1]], [KX, 0.75, KX], w, h)));\n        return cam;\n      },\n      uniforms: (gl, loc) => {\n        gl.activeTexture(gl.TEXTURE1);\n        gl.bindTexture(gl.TEXTURE_2D, legendTex);\n        gl.uniform1i(loc(\"uLegend\"), 1);\n        gl.uniform1fv(loc(\"uPress\"), s.press.map((p) => Math.max(0, p.x)));\n        gl.uniform2fv(loc(\"uKP\"), s.layout.flat());\n      },\n      step: (dt, t) => {\n        if (DEMO && !reduce && !s.touched && t > demoAt) {\n          demoKey = step % keys.length;\n          pressUntil = t + 0.24;\n          step++;\n          demoAt = t + (demoKey === keys.length - 1 ? 2.4 : 1.15);\n        }\n        let moving = false;\n        s.press.forEach((p, i) => {\n          const goal = s.held[i] || (i === demoKey && t < pressUntil) ? 1 : s.hover[i] ? REST_TOUCH : 0;\n          if (reduce) {\n            if (p.x !== goal) { p.x = goal; p.v = 0; moving = true; }\n          } else if (springStep(p, goal, dt, 1100, 42)) moving = true;\n        });\n        return moving;\n      },\n    });\n    if (!stage) return;\n    const gl = canvas.getContext(\"webgl2\")!;\n    legendTex = gl.createTexture();\n    gl.activeTexture(gl.TEXTURE1);\n    gl.bindTexture(gl.TEXTURE_2D, legendTex);\n    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, legendArt());\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_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    stage.invalidate();\n    setMode(\"gl\");\n    return () => stage.dispose();\n  }, [reduce]);\n\n  const s = sim.current;\n  const touch = () => { s.touched = true; };\n  const hold = (i: number, on: boolean) => { touch(); s.held[i] = on; };\n\n  return (\n    <section className=\"relative overflow-hidden bg-[#2a221b]\" style={{ fontFamily: GROTESK }}>\n      <div ref={wrapRef} className=\"relative h-[600px] w-full sm:h-[720px]\">\n        <canvas ref={canvasRef} aria-hidden=\"true\" className={`absolute inset-0 h-full w-full ${mode === \"still\" ? \"hidden\" : \"\"}`} />\n        {mode === \"still\" && <div aria-hidden=\"true\" className=\"absolute inset-0\" style={{ background: \"radial-gradient(120% 90% at 30% 20%, #c9b79c 0%, #a99377 45%, #6d5b47 100%)\" }}><div className=\"absolute inset-0\" style={stillWindowLight()} /><div className=\"absolute inset-0 opacity-30 mix-blend-overlay\" style={{ backgroundImage: STILL_GRAIN }} /></div>}\n\n        <div className={mode === \"still\" ? \"absolute inset-0 flex flex-wrap items-center justify-center gap-4 p-6 sm:gap-6\" : \"absolute inset-0\"}>\n          {keys.map((k, i) => {\n            const r = rects[i];\n            const still = mode === \"still\";\n            const faces = [\"#5b0f0c\", \"linear-gradient(160deg,#d7d8d6,#8d8f8f)\", \"linear-gradient(160deg,#4a2a18,#24130a)\", \"rgba(220,235,228,0.35)\"];\n            return (\n              <button\n                key={k.label}\n                type=\"button\"\n                onPointerDown={(e) => { e.currentTarget.setPointerCapture(e.pointerId); hold(i, true); }}\n                onPointerUp={() => hold(i, false)}\n                onPointerCancel={() => hold(i, false)}\n                onPointerEnter={() => { s.hover[i] = true; }}\n                onPointerLeave={() => { s.hover[i] = false; s.held[i] = false; }}\n                onKeyDown={(e) => { if ((e.key === \" \" || e.key === \"Enter\") && !e.repeat) hold(i, true); }}\n                onKeyUp={(e) => { if (e.key === \" \" || e.key === \"Enter\") hold(i, false); }}\n                onBlur={() => { s.held[i] = false; }}\n                onClick={() => setLast(k.label)}\n                className={`touch-manipulation select-none rounded-[22%] outline-none transition-shadow focus-visible:ring-2 focus-visible:ring-[#f4e9d6] focus-visible:ring-offset-4 focus-visible:ring-offset-transparent ${still ? `grid h-24 w-24 place-items-center text-[13px] font-semibold sm:h-28 sm:w-28 ${i === 1 || i === 3 ? \"text-[#1c1a17]\" : \"text-[#f4e9d6]\"}` : \"absolute\"}`}\n                style={still ? stillObject(faces[i], { radius: 20, depth: 10 }) : r ? { left: r.left, top: r.top, width: r.width, height: r.height } : { display: \"none\" }}\n              >\n                {still ? k.label : <span className=\"sr-only\">{k.label}</span>}\n              </button>\n            );\n          })}\n        </div>\n\n        <div className=\"pointer-events-none absolute inset-x-0 bottom-0 flex flex-col gap-1 p-5 text-[#f4e9d6] sm:flex-row sm:items-end sm:justify-between sm:p-8\">\n          <p className=\"text-[11px] font-semibold uppercase tracking-[0.24em] text-[#f4e9d6]/80\">Ceramic, steel, walnut, frosted glass</p>\n          <p aria-live=\"polite\" className=\"text-[13px] tabular-nums text-[#f4e9d6]/90\">\n            {last ? `${last} pressed` : \"Press a key\"}\n          </p>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
