{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"haze-shafts","type":"registry:ui","title":"Haze Shafts","description":"Soft volumetric light beams slanting across a dark scene, drifting and breathing through noisy haze. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/haze-shafts.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface HazeShaftsProps {\n  /** Light color of the beams. */\n  color?: string;\n  /** Number of beams (1 to 16). */\n  count?: number;\n  /** Slant in degrees; 0 is straight down. */\n  angle?: number;\n  /** Drift speed multiplier; 0 freezes the motion. */\n  speed?: number;\n  /** Overall brightness. */\n  intensity?: number;\n  /** How much drifting haze breaks up the beams (0 to 1). */\n  haze?: 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 vec3 uColor;\nuniform float uCount;\nuniform float uAngle;\nuniform float uIntensity;\nuniform float uHaze;\nuniform float uSeed;\n\nfloat hash1(float n) { return fract(sin(n * 127.1) * 43758.5453); }\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}\nfloat noise(vec2 p) {\n  vec2 i = floor(p), f = fract(p);\n  vec2 u = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash(i), hash(i + vec2(1, 0)), u.x), mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), u.x), u.y);\n}\nfloat fbm(vec2 p) {\n  float v = 0.0, a = 0.5;\n  for (int i = 0; i < 4; i++) { v += a * noise(p); p = p * 2.03 + 17.1; a *= 0.5; }\n  return v;\n}\n\nvoid main() {\n  vec2 uv = gl_FragCoord.xy / uRes;\n  vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;\n  float ca = cos(uAngle), sa = sin(uAngle);\n  // q.x runs across the beams, q.y along them (source at the top).\n  vec2 q = vec2(ca * p.x - sa * p.y, sa * p.x + ca * p.y);\n  float t = uTime;\n  float span = uRes.x / uRes.y + 0.6;\n  float fromTop = 0.75 - q.y;\n\n  float sum = 0.0;\n  for (int i = 0; i < 16; i++) {\n    float fi = float(i);\n    if (fi >= uCount) break;\n    float h1 = hash1(fi + 1.3), h2 = hash1(fi * 3.7 + 2.1), h3 = hash1(fi * 7.9 + 5.4);\n    float c = ((fi + 0.5) / uCount - 0.5) * span + (h1 - 0.5) * span / uCount * 0.8\n            + sin(t * (0.12 + 0.1 * h2) + fi * 2.4) * 0.07;\n    float w = mix(0.012, 0.07, h2) * (0.6 + fromTop * 0.45);\n    float x = (q.x - c) / w;\n    float pulse = 0.6 + 0.4 * sin(t * (0.25 + 0.35 * h3) + fi * 1.7);\n    sum += exp(-x * x) * pulse * mix(0.4, 1.0, h3);\n    // a faint wide halo around each beam\n    sum += exp(-x * x * 0.04) * 0.05 * pulse;\n  }\n\n  // Haze drifts down the beams: stretched along them, fine across them.\n  float n = fbm(vec2(q.x * 9.0, q.y * 1.4 + t * 0.18));\n  float n2 = fbm(vec2(q.x * 3.0 - t * 0.05, q.y * 0.6 + t * 0.1));\n  float dust = mix(1.0, n * n2 * 3.2, uHaze);\n  // Dust motes glinting inside the light, drifting down the beams.\n  vec2 mg = vec2(q.x, q.y + t * 0.05) * 55.0;\n  vec2 mi = floor(mg);\n  float mh = hash(mi);\n  float md = length(fract(mg) - 0.5 - (vec2(hash(mi + 3.1), hash(mi + 7.7)) - 0.5) * 0.6);\n  float motes = step(0.8, mh) * smoothstep(0.13, 0.0, md) * (0.5 + 0.5 * sin(t * 2.0 + mh * 60.0)) * 3.0 * uHaze;\n\n  float fade = smoothstep(2.0, -0.1, fromTop);\n  float light = sum * dust * fade * uIntensity;\n\n  vec3 col = vec3(0.006, 0.007, 0.012) + uColor * 0.035 * (1.0 - uv.y * 0.6);\n  col += uColor * light * 0.55;\n  col += mix(uColor, vec3(1.0), 0.6) * (light * light * 0.16 + sum * fade * motes * uIntensity);\n  // soft source glow along the top edge\n  col += uColor * 0.12 * uIntensity * smoothstep(0.55, 1.1, uv.y + (n2 - 0.5) * 0.2);\n\n  col = 1.0 - exp(-col * 1.4);\n  col += (hash(gl_FragCoord.xy + uSeed * 57.3) - 0.5) / 128.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 HazeShafts({\n  color = \"#9fb4ff\",\n  count = 9,\n  angle = 28,\n  speed = 1,\n  intensity = 1,\n  haze = 0.7,\n  className,\n}: HazeShaftsProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ color, count, angle, speed, intensity, haze });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { color, count, angle, speed, intensity, haze };\n    redraw.current?.();\n  }, [color, count, angle, speed, intensity, haze]);\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    let raf = 0;\n    let visible = true;\n    let time = 8;\n    let last = 0;\n\n    const draw = () => {\n      const o = opts.current;\n      gl.uniform2f(u(\"uRes\"), canvas.width, canvas.height);\n      gl.uniform1f(u(\"uTime\"), time);\n      gl.uniform3fv(u(\"uColor\"), rgb(o.color));\n      gl.uniform1f(u(\"uCount\"), Math.round(Math.min(Math.max(o.count, 1), 16)));\n      gl.uniform1f(u(\"uAngle\"), (o.angle * Math.PI) / 180);\n      gl.uniform1f(u(\"uIntensity\"), o.intensity);\n      gl.uniform1f(u(\"uHaze\"), Math.min(Math.max(o.haze, 0), 1));\n      gl.uniform1f(u(\"uSeed\"), reduced ? 0 : Math.random());\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      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\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      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"}]}