{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"luminescent-flows","type":"registry:ui","title":"Luminescent Flows","description":"Glowing currents drift like bioluminescent tides, with light pulses traveling along them and specks of plankton. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/luminescent-flows.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface LuminescentFlowsProps {\n  /** First current color. */\n  colorA?: string;\n  /** Second current color; each strand drifts between the two. */\n  colorB?: string;\n  /** Number of glowing currents, 1 to 12. */\n  count?: number;\n  /** Flow speed multiplier; 0 freezes the motion. */\n  speed?: number;\n  /** Size of the curves; higher means broader sweeps. */\n  scale?: number;\n  /** Overall brightness. */\n  intensity?: 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 uA;\nuniform vec3 uB;\nuniform float uCount;\nuniform float uScale;\nuniform float uIntensity;\n\nfloat hash(vec2 p) {\n  p = fract(p * vec2(234.34, 435.345));\n  p += dot(p, p + 34.23);\n  return fract(p.x * p.y);\n}\n\nvoid main() {\n  vec2 uv = gl_FragCoord.xy / uRes;\n  vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y / uScale;\n  float t = uTime;\n  // Gentle domain warp so the currents never look like pure sines.\n  p.y += 0.035 * sin(p.x * 2.3 + t * 0.21);\n  p.x += 0.03 * sin(p.y * 3.1 - t * 0.17);\n\n  vec3 col = vec3(0.004, 0.01, 0.022);\n  float n = max(uCount, 1.0);\n  for (int i = 0; i < 12; i++) {\n    float fi = float(i);\n    if (fi >= n) break;\n    float k = n > 1.0 ? fi / (n - 1.0) - 0.5 : 0.0;\n    float r = fract(sin(fi * 91.7) * 43758.5);\n    float y = k * 0.42\n      + 0.22 * sin(p.x * (1.1 + r * 0.6) + t * (0.28 + r * 0.2) + fi * 1.7)\n      + 0.09 * sin(p.x * (2.7 + r) - t * (0.43 + r * 0.3) + fi * 2.9)\n      + 0.035 * sin(p.x * 6.3 + t * 0.9 + fi * 4.1);\n    float d = abs(p.y - y);\n    float width = 0.0016 + 0.0014 * (0.5 + 0.5 * sin(p.x * 2.4 - t * 0.6 + fi * 3.0));\n    float line = width / (d + 0.0025);\n    // Bright pulses travel along each current.\n    float pulse = pow(0.5 + 0.5 * sin(p.x * 3.2 - t * (1.1 + r * 0.9) + fi * 5.3), 8.0);\n    vec3 c = mix(uA, uB, 0.5 + 0.5 * sin(fi * 1.9 + p.x * 0.9 + t * 0.15));\n    col += c * line * (0.22 + 0.9 * pulse);\n    col += c * exp(-d * d * 180.0) * (0.035 + 0.06 * pulse);\n  }\n\n  // Drifting plankton specks that glow near the currents.\n  vec2 g = (p + vec2(t * 0.015, sin(t * 0.1) * 0.02)) * 55.0;\n  vec2 id = floor(g);\n  vec2 f = fract(g) - 0.5;\n  float h = hash(id);\n  if (h > 0.93) {\n    vec2 o = vec2(hash(id + 3.1), hash(id + 7.7)) - 0.5;\n    float s = exp(-dot(f - o * 0.6, f - o * 0.6) * 90.0);\n    float tw = 0.5 + 0.5 * sin(t * (1.5 + h * 3.0) + h * 40.0);\n    col += mix(uA, uB, hash(id + 1.3)) * s * tw * 0.55;\n  }\n\n  col = 1.0 - exp(-col * uIntensity * 1.4);\n  vec2 v = uv - 0.5;\n  col *= 1.0 - dot(v, v) * 1.1;\n  gl_FragColor = vec4(max(col, 0.0), 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 LuminescentFlows({\n  colorA = \"#2dd4bf\",\n  colorB = \"#818cf8\",\n  count = 7,\n  speed = 1,\n  scale = 1,\n  intensity = 1,\n  className,\n}: LuminescentFlowsProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ colorA, colorB, count, speed, scale, intensity });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { colorA, colorB, count, speed, scale, intensity };\n    redraw.current?.();\n  }, [colorA, colorB, count, speed, scale, intensity]);\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 = 30;\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(\"uA\"), rgb(o.colorA));\n      gl.uniform3fv(u(\"uB\"), rgb(o.colorB));\n      gl.uniform1f(u(\"uCount\"), Math.min(12, Math.max(1, Math.round(o.count))));\n      gl.uniform1f(u(\"uScale\"), Math.max(o.scale, 0.1));\n      gl.uniform1f(u(\"uIntensity\"), o.intensity);\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"}]}