{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"glass-drift","type":"registry:ui","title":"Glass Drift","description":"Translucent glass shards drift in layers over soft glowing color, bending the light with refraction, dispersion and slow cursor parallax. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/glass-drift.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface GlassDriftProps {\n  /** Three glow colors behind the glass. */\n  colors?: string[];\n  /** Drift speed multiplier; 0 freezes the motion. */\n  speed?: number;\n  /** Shard size multiplier. */\n  scale?: number;\n  /** Share of grid cells holding a shard (0 to 1). */\n  density?: number;\n  /** Strength of the refraction through the glass. */\n  refraction?: number;\n  className?: string;\n}\n\nconst VERT = \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\";\n\nconst FRAG = `\nprecision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uLook;\nuniform vec3 uC0;\nuniform vec3 uC1;\nuniform vec3 uC2;\nuniform float uScale;\nuniform float uDensity;\nuniform float uRefract;\n\nfloat hash(vec2 p) {\n  p = fract(p * vec2(123.34, 456.21));\n  p += dot(p, p + 45.32);\n  return fract(p.x * p.y);\n}\nmat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }\n\n// The scene seen through the glass: soft glowing color fields on deep navy.\nvec3 backdrop(vec2 p) {\n  float t = uTime * 0.12;\n  vec3 col = mix(vec3(0.012, 0.018, 0.04), vec3(0.03, 0.04, 0.09), smoothstep(-0.6, 0.8, p.y));\n  vec2 a = vec2(sin(t * 1.3) * 0.7, cos(t * 0.9) * 0.3 + 0.25);\n  vec2 b = vec2(cos(t * 0.7 + 1.0) * 0.8, sin(t * 1.1) * 0.35 - 0.25);\n  vec2 c = vec2(sin(t * 0.5 + 2.0) * 0.5, cos(t * 0.6 + 1.0) * 0.4);\n  col += uC0 * 0.6 * exp(-dot(p - a, p - a) * 2.6);\n  col += uC1 * 0.55 * exp(-dot(p - b, p - b) * 2.2);\n  col += uC2 * 0.42 * exp(-dot(p - c, p - c) * 3.5);\n  // Faint flowing light strands give the glass something to bend.\n  float w = sin(p.y * 11.0 + sin(p.x * 2.3 + t * 2.0) * 1.6 + p.x * 1.5);\n  col += mix(uC0, uC1, 0.5 + 0.5 * sin(p.x * 1.7)) * smoothstep(0.08, 0.0, abs(w)) * 0.08;\n  return col;\n}\n\n// Signed distance to a triangle (negative inside).\nfloat sdTri(vec2 p, vec2 p0, vec2 p1, vec2 p2) {\n  vec2 e0 = p1 - p0, e1 = p2 - p1, e2 = p0 - p2;\n  vec2 v0 = p - p0, v1 = p - p1, v2 = p - p2;\n  vec2 q0 = v0 - e0 * clamp(dot(v0, e0) / dot(e0, e0), 0.0, 1.0);\n  vec2 q1 = v1 - e1 * clamp(dot(v1, e1) / dot(e1, e1), 0.0, 1.0);\n  vec2 q2 = v2 - e2 * clamp(dot(v2, e2) / dot(e2, e2), 0.0, 1.0);\n  float s = sign(e0.x * e2.y - e0.y * e2.x);\n  vec2 d = min(min(vec2(dot(q0, q0), s * (v0.x * e0.y - v0.y * e0.x)),\n                   vec2(dot(q1, q1), s * (v1.x * e1.y - v1.y * e1.x))),\n                   vec2(dot(q2, q2), s * (v2.x * e2.y - v2.y * e2.x)));\n  return -sqrt(d.x) * sign(d.y);\n}\n\nvoid main() {\n  vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;\n  float px = 1.0 / uRes.y;\n  float t = uTime;\n  vec3 col = backdrop(p);\n\n  // Four layers, far to near: nearer shards are larger, faster and shift more with the cursor.\n  for (int i = 0; i < 4; i++) {\n    float L = float(i);\n    float k = L / 3.0;\n    float cell = mix(0.42, 0.95, k) * uScale;\n    vec2 q = (p + uLook * (0.015 + 0.05 * k)) / cell + vec2(0.02, 0.05 + 0.05 * k) * t / cell + L * 7.31;\n    vec2 id = floor(q);\n    float h = hash(id + L * 13.1);\n    if (h > uDensity) continue;\n\n    vec2 o = (vec2(hash(id + 1.7), hash(id + 4.3)) - 0.5) * 0.25;\n    float spin = (hash(id + 8.8) - 0.5) * 0.5;\n    mat2 R = rot(h * 40.0 + t * spin);\n    vec2 lp = R * (fract(q) - 0.5 - o);\n\n    float sz = mix(0.14, 0.25, hash(id + 5.5));\n    vec2 v0 = sz * vec2(cos(0.0 + h * 2.0), sin(0.0 + h * 2.0) * 1.5) * mix(0.65, 1.0, hash(id + 2.2));\n    vec2 v1 = sz * vec2(cos(2.1 + h * 3.0), sin(2.1 + h * 3.0) * 1.5) * mix(0.65, 1.0, hash(id + 3.3));\n    vec2 v2 = sz * vec2(cos(4.2 - h * 2.0), sin(4.2 - h * 2.0) * 1.5) * mix(0.65, 1.0, hash(id + 6.6));\n\n    float e = 0.004;\n    float d0 = sdTri(lp, v0, v1, v2);\n    float d = d0 * cell;\n    float a = smoothstep(px, -px, d);\n    if (a <= 0.0) continue;\n\n    vec2 grad = vec2(sdTri(lp + vec2(e, 0.0), v0, v1, v2) - d0, sdTri(lp + vec2(0.0, e), v0, v1, v2) - d0);\n    grad = normalize(grad + 1e-6) * R;\n    float inner = -d;\n    float bevel = 1.0 - smoothstep(0.0, 0.02 + 0.02 * k, inner);\n\n    // Refraction: a tilted face shifts the backdrop; the bevel bends it hard near the edges.\n    vec2 tilt = rot(t * spin * 0.7) * (vec2(hash(id + 9.1), hash(id + 7.7)) - 0.5);\n    vec2 off = (tilt * 0.16 + grad * bevel * 0.07) * uRefract;\n    vec3 glass = vec3(backdrop(p + off).r, backdrop(p + off * 1.12).g, backdrop(p + off * 1.25).b);\n    glass = glass * 1.25 + vec3(0.025, 0.035, 0.06);\n\n    float sweepPos = sin(t * 0.4 + h * 20.0) * sz;\n    float sweep = smoothstep(sz * 0.12, 0.0, abs(dot(lp, vec2(0.8, 0.6)) - sweepPos));\n    float rim = exp(-inner / (px * 1.6));\n    glass += vec3(0.85, 0.92, 1.0) * (rim * 0.55 + bevel * 0.12 + sweep * 0.12);\n\n    col = mix(col, glass, a * mix(0.5, 0.92, k));\n  }\n\n  vec2 v = gl_FragCoord.xy / uRes - 0.5;\n  col *= 1.0 - dot(v, v) * 0.9;\n  col += (hash(gl_FragCoord.xy + fract(uTime) * 71.0) - 0.5) / 160.0;\n  gl_FragColor = vec4(col, 1.0);\n}\n`;\n\nfunction rgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, \"$&$&\");\n  const n = parseInt(h.padEnd(6, \"0\").slice(0, 6), 16) || 0;\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\n/** Compiles a full-screen-triangle program and returns a cached uniform lookup, or null. */\nfunction fullscreen(gl: WebGLRenderingContext, frag: string) {\n  const prog = gl.createProgram();\n  if (!prog) return null;\n  for (const [type, src] of [\n    [gl.VERTEX_SHADER, VERT],\n    [gl.FRAGMENT_SHADER, frag],\n  ] as const) {\n    const sh = gl.createShader(type);\n    if (!sh) return null;\n    gl.shaderSource(sh, src);\n    gl.compileShader(sh);\n    if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) console.warn(gl.getShaderInfoLog(sh));\n    gl.attachShader(prog, sh);\n  }\n  gl.bindAttribLocation(prog, 0, \"p\");\n  gl.linkProgram(prog);\n  if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) return null;\n  gl.useProgram(prog);\n  gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n  gl.enableVertexAttribArray(0);\n  gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n  const cache = new Map<string, WebGLUniformLocation | null>();\n  return (name: string) => {\n    if (!cache.has(name)) cache.set(name, gl.getUniformLocation(prog, name));\n    return cache.get(name) ?? null;\n  };\n}\n\nexport function GlassDrift({\n  colors = [\"#38bdf8\", \"#a78bfa\", \"#f0abfc\"],\n  speed = 1,\n  scale = 1,\n  density = 0.55,\n  refraction = 1,\n  className,\n}: GlassDriftProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ colors, speed, scale, density, refraction });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { colors, speed, scale, density, refraction };\n    redraw.current?.();\n  }, [colors, speed, scale, density, refraction]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const canvas = document.createElement(\"canvas\");\n    canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n    el.appendChild(canvas);\n    const gl = canvas.getContext(\"webgl\", { antialias: false, powerPreference: \"low-power\" });\n    const u = gl && fullscreen(gl, FRAG);\n    if (!gl || !u) {\n      canvas.remove();\n      return;\n    }\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const look = { x: 0, y: 0, tx: 0, ty: 0 };\n    let raf = 0;\n    let visible = true;\n    let time = 12;\n    let last = 0;\n\n    const draw = () => {\n      const o = opts.current;\n      const list = o.colors.length ? o.colors : [\"#ffffff\"];\n      for (let i = 0; i < 3; i++) gl.uniform3fv(u(`uC${i}`), rgb(list[i % list.length]));\n      gl.uniform2f(u(\"uRes\"), canvas.width, canvas.height);\n      gl.uniform1f(u(\"uTime\"), time);\n      gl.uniform2f(u(\"uLook\"), look.x, look.y);\n      gl.uniform1f(u(\"uScale\"), Math.max(o.scale, 0.2));\n      gl.uniform1f(u(\"uDensity\"), Math.min(Math.max(o.density, 0), 1));\n      gl.uniform1f(u(\"uRefract\"), o.refraction);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n    const loop = (now: number) => {\n      time += (Math.min(now - last, 50) / 1000) * opts.current.speed;\n      last = now;\n      look.x += (look.tx - look.x) * 0.04;\n      look.y += (look.ty - look.y) * 0.04;\n      draw();\n      raf = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (raf || !visible || reduced) return;\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    };\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.max(1, Math.round(el.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(el.clientHeight * dpr));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      draw();\n    };\n    const onMove = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      look.tx = ((e.clientX - r.left) / r.width) * 2 - 1;\n      look.ty = 1 - ((e.clientY - r.top) / r.height) * 2;\n    };\n    const onLeave = () => {\n      look.tx = 0;\n      look.ty = 0;\n    };\n\n    const target = el.parentElement ?? el;\n    target.addEventListener(\"pointermove\", onMove, { passive: true });\n    target.addEventListener(\"pointerleave\", onLeave);\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) play();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    redraw.current = draw;\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      target.removeEventListener(\"pointermove\", onMove);\n      target.removeEventListener(\"pointerleave\", onLeave);\n      redraw.current = null;\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    };\n  }, []);\n\n  return (\n    <div\n      ref={host}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}