{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "underwater-caustics",
  "type": "registry:ui",
  "title": "Underwater Caustics",
  "author": "Rishabh Bose <https://theb0se.vercel.app/>",
  "description": "Shimmering underwater caustic light network powered by a real-time WebGL fragment shader with subtle chromatic dispersion, diagonal volumetric God-ray sunbeams, and buoyant rising bubbles.",
  "dependencies": [
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "components/ui/underwater-caustics.tsx",
      "content": "\"use client\";\n\nimport React, { useEffect, useRef, useState } from \"react\";\nimport { Sun, Sparkles, CircleDot } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface UnderwaterCausticsProps {\n  /** Base underwater hex color. Default: \"#0369A1\" */\n  waterColor?: string;\n  /** Refracted cellular caustic light network brightness (0.2 to 2.0). Default: 1.0 */\n  causticIntensity?: number;\n  /** Animation speed multiplier. Default: 1.0 */\n  speed?: number;\n  /** Number of buoyant rising bubbles with horizontal sine wobble. Default: 35 */\n  bubbleCount?: number;\n  /** Render diagonal volumetric sunbeams from the water surface. Default: true */\n  sunbeams?: boolean;\n  /** Optional foreground content */\n  children?: React.ReactNode;\n  className?: string;\n}\n\nfunction hexToRgbNormalized(hex: string): [number, number, number] {\n  const clean = hex.replace(\"#\", \"\").trim();\n  if (clean.length === 3) {\n    return [\n      (parseInt(clean[0] + clean[0], 16) || 3) / 255,\n      (parseInt(clean[1] + clean[1], 16) || 105) / 255,\n      (parseInt(clean[2] + clean[2], 16) || 161) / 255,\n    ];\n  }\n  const num = parseInt(clean, 16);\n  if (Number.isNaN(num)) return [3 / 255, 105 / 255, 161 / 255];\n  return [\n    ((num >> 16) & 255) / 255,\n    ((num >> 8) & 255) / 255,\n    (num & 255) / 255,\n  ];\n}\n\nconst VERTEX_SHADER = `\n  attribute vec2 a_position;\n  varying vec2 v_uv;\n  void main() {\n    v_uv = a_position * 0.5 + 0.5;\n    gl_Position = vec4(a_position, 0.0, 1.0);\n  }\n`;\n\nconst FRAGMENT_SHADER = `\n  precision highp float;\n  varying vec2 v_uv;\n  uniform vec2 u_resolution;\n  uniform float u_time;\n  uniform vec3 u_waterColor;\n  uniform float u_intensity;\n  uniform float u_sunbeams;\n\n  // Dual-scale domain-warped cellular wave-interference caustic solver with sharp cusps\n  float causticSolver(vec2 uv, float t) {\n    vec2 p = mod(uv * 6.28318530718, 6.28318530718) - 250.0;\n    vec2 i = vec2(p);\n    float c = 1.0;\n    float inten = 0.0046;\n\n    for (int n = 0; n < 5; n++) {\n      float fn = float(n);\n      float timeShift = t * (1.0 - (3.1 / (fn + 1.0)));\n      i = p + vec2(\n        cos(timeShift - i.x) + sin(timeShift + i.y),\n        sin(timeShift - i.y) + cos(timeShift + i.x)\n      );\n      c += 1.0 / length(vec2(\n        p.x / (sin(i.x + timeShift) / inten),\n        p.y / (cos(i.y + timeShift) / inten)\n      ));\n    }\n\n    c /= 5.0;\n    c = 1.17 - pow(c, 1.42);\n    return pow(abs(c), 8.5);\n  }\n\n  void main() {\n    vec2 aspect = vec2(u_resolution.x / max(1.0, u_resolution.y), 1.0);\n    // Perspective foreshortening + gentle underwater current refraction warp\n    vec2 swellWarp = vec2(\n      sin(v_uv.y * 5.0 + u_time * 0.7) * 0.012,\n      cos(v_uv.x * 4.5 + u_time * 0.55) * 0.012\n    );\n    vec2 uv = (v_uv + swellWarp) * aspect;\n\n    // Chromatic dispersion across RGB channels at sharp focal cusps\n    vec2 disp = vec2(0.0045, 0.0022);\n    float cR1 = causticSolver(uv * 0.92 + disp, u_time * 0.52);\n    float cG1 = causticSolver(uv * 0.92, u_time * 0.52);\n    float cB1 = causticSolver(uv * 0.92 - disp, u_time * 0.52);\n\n    // Secondary finer interference harmonic layer\n    float cG2 = causticSolver(uv * 1.65 + vec2(1.7, 2.3), -u_time * 0.38) * 0.45;\n\n    vec3 causticLight = vec3(\n      cR1 * 0.85 + cG2 * 0.6,\n      cG1 * 1.04 + cG2 * 0.95,\n      cB1 * 1.18 + cG2 * 1.1\n    ) * u_intensity;\n\n    // Marine depth gradient: sunlit turquoise surface (v_uv.y = 1) to deep abyssal floor (v_uv.y = 0)\n    vec3 abyssColor = u_waterColor * 0.12 + vec3(0.005, 0.02, 0.05);\n    vec3 midWater = u_waterColor * 0.58 + vec3(0.01, 0.09, 0.14);\n    vec3 surfaceGlow = u_waterColor * 1.05 + vec3(0.12, 0.34, 0.42);\n\n    vec3 col = mix(abyssColor, midWater, smoothstep(0.0, 0.65, v_uv.y));\n    col = mix(col, surfaceGlow, pow(v_uv.y, 2.2));\n\n    // Undulating surface shimmer near top edge\n    float surfaceRipple = pow(v_uv.y, 5.5) * (0.6 + 0.4 * sin(uv.x * 14.0 + u_time * 1.8));\n    col += vec3(0.55, 0.92, 1.0) * surfaceRipple * 0.32 * u_intensity;\n\n    // Volumetric God-ray sunbeams angling down from the undulating surface\n    if (u_sunbeams > 0.5) {\n      float slantCoord = (uv.x - (1.0 - v_uv.y) * 0.42);\n      float ray1 = 0.5 + 0.5 * sin(slantCoord * 11.0 + u_time * 0.42);\n      float ray2 = 0.5 + 0.5 * cos(slantCoord * 19.0 - u_time * 0.58 + 1.2);\n      float ray3 = 0.5 + 0.5 * sin(slantCoord * 31.0 + u_time * 0.85);\n      float combinedRays = pow(ray1 * 0.55 + ray2 * 0.3 + ray3 * 0.15, 2.8);\n      // Attenuate rays smoothly with depth\n      float depthAtten = pow(v_uv.y, 1.25) * smoothstep(0.02, 0.95, v_uv.y);\n      col += vec3(0.48, 0.92, 1.0) * combinedRays * depthAtten * 0.52 * u_intensity;\n    }\n\n    // Add refracted caustic network (brightest on the floor and near sunlit upper water)\n    float causticMask = 0.55 + 0.45 * v_uv.y;\n    col += causticLight * causticMask;\n\n    // Subtle abyssal radial vignette\n    float vig = smoothstep(1.18, 0.22, length(v_uv - vec2(0.5, 0.52)));\n    col *= 0.68 + 0.32 * vig;\n\n    gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);\n  }\n`;\n\ninterface Bubble {\n  x: number;\n  y: number;\n  r: number;\n  speed: number;\n  wobbleAmp: number;\n  wobbleFreq: number;\n  phase: number;\n  opacity: number;\n}\n\ninterface MarineSnow {\n  x: number;\n  y: number;\n  size: number;\n  vx: number;\n  vy: number;\n  alpha: number;\n  phase: number;\n}\n\nconst CAUSTIC_PRESETS = [\n  { name: \"Lagoon\", color: \"#0369A1\", intensity: 1.05, beams: true },\n  { name: \"Cenote\", color: \"#0D9488\", intensity: 1.25, beams: true },\n  { name: \"Abyssal\", color: \"#1E3A8A\", intensity: 0.85, beams: false },\n];\n\nexport function UnderwaterCaustics({\n  waterColor = \"#0369A1\",\n  causticIntensity = 1.0,\n  speed = 1.0,\n  bubbleCount = 35,\n  sunbeams = true,\n  children,\n  className,\n}: UnderwaterCausticsProps) {\n  const containerRef = useRef<HTMLDivElement>(null);\n  const glCanvasRef = useRef<HTMLCanvasElement>(null);\n  const bubbleCanvasRef = useRef<HTMLCanvasElement>(null);\n\n  const [liveColor, setLiveColor] = useState(waterColor);\n  const [liveIntensity, setLiveIntensity] = useState(causticIntensity);\n  const [liveSpeed, setLiveSpeed] = useState(speed);\n  const [liveBubbles, setLiveBubbles] = useState(bubbleCount);\n  const [liveSunbeams, setLiveSunbeams] = useState(sunbeams);\n\n  useEffect(() => setLiveColor(waterColor), [waterColor]);\n  useEffect(() => setLiveIntensity(causticIntensity), [causticIntensity]);\n  useEffect(() => setLiveSpeed(speed), [speed]);\n  useEffect(() => setLiveBubbles(bubbleCount), [bubbleCount]);\n  useEffect(() => setLiveSunbeams(sunbeams), [sunbeams]);\n\n  const paramsRef = useRef({\n    color: liveColor,\n    intensity: liveIntensity,\n    speed: liveSpeed,\n    bubbles: liveBubbles,\n    sunbeams: liveSunbeams,\n  });\n\n  useEffect(() => {\n    paramsRef.current = {\n      color: liveColor,\n      intensity: liveIntensity,\n      speed: liveSpeed,\n      bubbles: liveBubbles,\n      sunbeams: liveSunbeams,\n    };\n  }, [liveColor, liveIntensity, liveSpeed, liveBubbles, liveSunbeams]);\n\n  useEffect(() => {\n    const container = containerRef.current;\n    const glCanvas = glCanvasRef.current;\n    const bubbleCanvas = bubbleCanvasRef.current;\n    if (!container || !glCanvas || !bubbleCanvas) return;\n\n    const gl = glCanvas.getContext(\"webgl\", {\n      alpha: false,\n      antialias: false,\n      powerPreference: \"high-performance\",\n    });\n    const ctx2d = bubbleCanvas.getContext(\"2d\");\n\n    const compileShader = (type: number, src: string) => {\n      if (!gl) return null;\n      const shader = gl.createShader(type);\n      if (!shader) return null;\n      gl.shaderSource(shader, src);\n      gl.compileShader(shader);\n      if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n        gl.deleteShader(shader);\n        return null;\n      }\n      return shader;\n    };\n\n    let program: WebGLProgram | null = null;\n    let uResLoc: WebGLUniformLocation | null = null;\n    let uTimeLoc: WebGLUniformLocation | null = null;\n    let uColorLoc: WebGLUniformLocation | null = null;\n    let uIntensityLoc: WebGLUniformLocation | null = null;\n    let uSunbeamsLoc: WebGLUniformLocation | null = null;\n\n    if (gl) {\n      const vs = compileShader(gl.VERTEX_SHADER, VERTEX_SHADER);\n      const fs = compileShader(gl.FRAGMENT_SHADER, FRAGMENT_SHADER);\n      if (vs && fs) {\n        program = gl.createProgram();\n        if (program) {\n          gl.attachShader(program, vs);\n          gl.attachShader(program, fs);\n          gl.linkProgram(program);\n          gl.useProgram(program);\n\n          const buf = gl.createBuffer();\n          gl.bindBuffer(gl.ARRAY_BUFFER, buf);\n          gl.bufferData(\n            gl.ARRAY_BUFFER,\n            new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]),\n            gl.STATIC_DRAW\n          );\n          const posLoc = gl.getAttribLocation(program, \"a_position\");\n          gl.enableVertexAttribArray(posLoc);\n          gl.vertexAttribPointer(posLoc, 2, gl.FLOAT, false, 0, 0);\n\n          uResLoc = gl.getUniformLocation(program, \"u_resolution\");\n          uTimeLoc = gl.getUniformLocation(program, \"u_time\");\n          uColorLoc = gl.getUniformLocation(program, \"u_waterColor\");\n          uIntensityLoc = gl.getUniformLocation(program, \"u_intensity\");\n          uSunbeamsLoc = gl.getUniformLocation(program, \"u_sunbeams\");\n        }\n      }\n    }\n\n    let width = 1;\n    let height = 1;\n    let dpr = 1;\n    const bubbles: Bubble[] = [];\n    const snowParticles: MarineSnow[] = [];\n\n    const initMarineSnow = () => {\n      snowParticles.length = 0;\n      for (let i = 0; i < 65; i++) {\n        snowParticles.push({\n          x: Math.random() * width,\n          y: Math.random() * height,\n          size: 0.7 + Math.random() * 1.6,\n          vx: (Math.random() - 0.5) * 0.28,\n          vy: -0.12 - Math.random() * 0.25,\n          alpha: 0.18 + Math.random() * 0.45,\n          phase: Math.random() * Math.PI * 2,\n        });\n      }\n    };\n\n    const syncBubbles = (targetCount: number) => {\n      while (bubbles.length < targetCount) {\n        bubbles.push({\n          x: Math.random() * width,\n          y: Math.random() * height,\n          r: 2.2 + Math.random() * 7.5,\n          speed: 0.55 + Math.random() * 1.45,\n          wobbleAmp: 7 + Math.random() * 18,\n          wobbleFreq: 1.4 + Math.random() * 2.4,\n          phase: Math.random() * Math.PI * 2,\n          opacity: 0.3 + Math.random() * 0.5,\n        });\n      }\n      if (bubbles.length > targetCount) {\n        bubbles.length = targetCount;\n      }\n    };\n\n    const resize = () => {\n      const rect = container.getBoundingClientRect();\n      width = Math.max(1, rect.width);\n      height = Math.max(1, rect.height);\n      dpr = Math.min(window.devicePixelRatio || 1, 1.75);\n\n      glCanvas.width = Math.floor(width * dpr);\n      glCanvas.height = Math.floor(height * dpr);\n      if (gl) {\n        gl.viewport(0, 0, glCanvas.width, glCanvas.height);\n      }\n\n      bubbleCanvas.width = Math.floor(width * dpr);\n      bubbleCanvas.height = Math.floor(height * dpr);\n      if (ctx2d) {\n        ctx2d.setTransform(dpr, 0, 0, dpr, 0, 0);\n      }\n      syncBubbles(paramsRef.current.bubbles);\n      if (snowParticles.length === 0) initMarineSnow();\n    };\n\n    resize();\n    const ro = new ResizeObserver(resize);\n    ro.observe(container);\n\n    let rafId = 0;\n    let time = 0;\n\n    const render = () => {\n      const {\n        color: curColor,\n        intensity: curIntensity,\n        speed: curSpeed,\n        bubbles: curBubbles,\n        sunbeams: curSunbeams,\n      } = paramsRef.current;\n\n      time += 0.016 * curSpeed;\n      syncBubbles(curBubbles);\n\n      // 1. Render WebGL Caustic + Volumetric Sunbeam Shader\n      if (gl && program) {\n        const [r, g, b] = hexToRgbNormalized(curColor);\n        gl.useProgram(program);\n        gl.uniform2f(uResLoc, glCanvas.width, glCanvas.height);\n        gl.uniform1f(uTimeLoc, time);\n        gl.uniform3f(uColorLoc, r, g, b);\n        gl.uniform1f(uIntensityLoc, curIntensity);\n        gl.uniform1f(uSunbeamsLoc, curSunbeams ? 1.0 : 0.0);\n        gl.drawArrays(gl.TRIANGLES, 0, 6);\n      }\n\n      // 2. Render suspended marine snow & 3D Fresnel glass bubbles on overlay 2D canvas\n      if (ctx2d) {\n        ctx2d.clearRect(0, 0, width, height);\n\n        // Suspended marine snow / bioluminescent plankton micro-particles\n        for (let i = 0; i < snowParticles.length; i++) {\n          const sp = snowParticles[i];\n          sp.x += sp.vx + Math.sin(time * 0.9 + sp.phase) * 0.18;\n          sp.y += sp.vy * (0.6 + curSpeed * 0.4);\n          if (sp.y < -5) sp.y = height + 5;\n          if (sp.x < -5) sp.x = width + 5;\n          if (sp.x > width + 5) sp.x = -5;\n\n          const twinkle = 0.55 + 0.45 * Math.sin(time * 2.4 + sp.phase);\n          ctx2d.fillStyle = `rgba(210, 248, 255, ${sp.alpha * twinkle})`;\n          ctx2d.beginPath();\n          ctx2d.arc(sp.x, sp.y, sp.size, 0, Math.PI * 2);\n          ctx2d.fill();\n        }\n\n        // 3D Glass-like rising bubbles with elliptical hydrodynamic wobble & Fresnel rim\n        for (let i = 0; i < bubbles.length; i++) {\n          const b = bubbles[i];\n          b.y -= b.speed * (0.65 + curSpeed * 0.55);\n          if (b.y + b.r < -12) {\n            b.y = height + b.r + Math.random() * 30;\n            b.x = Math.random() * width;\n          }\n\n          const osc = time * b.wobbleFreq + b.phase;\n          const bx = b.x + Math.sin(osc) * b.wobbleAmp;\n          // Hydrodynamic oblate spheroid deformation as bubble rises\n          const squash = 1 + Math.sin(osc * 2.1) * 0.11;\n          const rx = b.r * squash;\n          const ry = b.r / squash;\n\n          // Internal Fresnel rim gradient (transparent core, glowing cyan/white rim)\n          const bubGrad = ctx2d.createRadialGradient(\n            bx - rx * 0.22,\n            b.y - ry * 0.22,\n            b.r * 0.1,\n            bx,\n            b.y,\n            Math.max(rx, ry)\n          );\n          bubGrad.addColorStop(0, \"rgba(255, 255, 255, 0.02)\");\n          bubGrad.addColorStop(0.68, `rgba(140, 230, 255, ${b.opacity * 0.18})`);\n          bubGrad.addColorStop(0.92, `rgba(215, 250, 255, ${b.opacity * 0.65})`);\n          bubGrad.addColorStop(1, \"rgba(255, 255, 255, 0)\");\n\n          ctx2d.save();\n          ctx2d.beginPath();\n          ctx2d.ellipse(bx, b.y, rx, ry, 0, 0, Math.PI * 2);\n          ctx2d.fillStyle = bubGrad;\n          ctx2d.fill();\n\n          // Outer refractive glass contour\n          ctx2d.strokeStyle = `rgba(225, 250, 255, ${b.opacity * 0.78})`;\n          ctx2d.lineWidth = 1.0;\n          ctx2d.stroke();\n\n          // Secondary bottom-right internal caustic reflection arc\n          ctx2d.beginPath();\n          ctx2d.ellipse(\n            bx,\n            b.y,\n            rx * 0.78,\n            ry * 0.78,\n            0,\n            Math.PI * 0.15,\n            Math.PI * 0.75\n          );\n          ctx2d.strokeStyle = `rgba(180, 245, 255, ${b.opacity * 0.55})`;\n          ctx2d.lineWidth = Math.max(0.8, b.r * 0.14);\n          ctx2d.stroke();\n\n          // Primary crisp top-left specular highlight dot\n          ctx2d.beginPath();\n          ctx2d.ellipse(\n            bx - rx * 0.36,\n            b.y - ry * 0.36,\n            Math.max(0.8, rx * 0.26),\n            Math.max(0.6, ry * 0.18),\n            -Math.PI * 0.25,\n            0,\n            Math.PI * 2\n          );\n          ctx2d.fillStyle = `rgba(255, 255, 255, ${Math.min(0.98, b.opacity * 1.55)})`;\n          ctx2d.fill();\n          ctx2d.restore();\n        }\n      }\n\n      rafId = requestAnimationFrame(render);\n    };\n\n    rafId = requestAnimationFrame(render);\n\n    return () => {\n      cancelAnimationFrame(rafId);\n      ro.disconnect();\n    };\n  }, []);\n\n  return (\n    <div\n      ref={containerRef}\n      data-fullscreen-scroll\n      className={cn(\n        \"relative flex min-h-[520px] w-full flex-col justify-between overflow-hidden rounded-2xl border border-white/10 bg-[#02182B] p-6 text-white shadow-2xl\",\n        className\n      )}\n    >\n      <canvas\n        ref={glCanvasRef}\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 h-full w-full\"\n      />\n      <canvas\n        ref={bubbleCanvasRef}\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 h-full w-full\"\n      />\n\n      {children && (\n        <div className=\"relative z-10 flex min-h-[520px] w-full flex-col\">\n          {children}\n        </div>\n      )}\n    </div>\n  );\n}\n\nexport default UnderwaterCaustics;\n",
      "type": "registry:ui",
      "target": "components/ui/underwater-caustics.tsx"
    },
    {
      "path": "lib/utils.ts",
      "content": "import { type ClassValue, clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport async function copyToClipboard(text: string): Promise<boolean> {\n  if (typeof window === \"undefined\") return false;\n\n  try {\n    if (navigator.clipboard && window.isSecureContext) {\n      await navigator.clipboard.writeText(text);\n      return true;\n    }\n\n    const textArea = document.createElement(\"textarea\");\n    textArea.value = text;\n    textArea.style.position = \"fixed\";\n    textArea.style.left = \"-999999px\";\n    textArea.style.top = \"-999999px\";\n    document.body.appendChild(textArea);\n    textArea.focus();\n    textArea.select();\n    const succeeded = document.execCommand(\"copy\");\n    textArea.remove();\n    return succeeded;\n  } catch {\n    return false;\n  }\n}\n",
      "type": "registry:lib",
      "target": "lib/utils.ts"
    }
  ]
}
