{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"cosmic-background","type":"registry:ui","title":"Cosmic Background","description":"Deep-space nebula clouds with parallax star layers, twinkling flares and the odd shooting star. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/cosmic-background.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface CosmicBackgroundProps {\n  /** Main nebula color. */\n  color?: string;\n  /** Secondary nebula color, mixed into the cloud edges. */\n  accent?: string;\n  /** Drift speed multiplier; 0 freezes the scene. */\n  speed?: number;\n  /** Size of the nebula clouds; higher means larger shapes. */\n  scale?: number;\n  /** Brightness of the nebula. */\n  intensity?: number;\n  /** Star density, 0..1. */\n  stars?: number;\n  /** Show an occasional shooting star. */\n  shootingStars?: boolean;\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 vec3 uAccent;\nuniform float uScale;\nuniform float uIntensity;\nuniform float uStars;\nuniform float uShoot;\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}\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  mat2 m = mat2(1.6, 1.2, -1.2, 1.6);\n  for (int i = 0; i < 6; i++) { v += a * noise(p); p = m * p; a *= 0.5; }\n  return v;\n}\n\n// One layer of stars: at most one star per grid cell.\nvec3 stars(vec2 p, float cells, float bright, float t) {\n  p *= cells;\n  vec2 id = floor(p);\n  vec2 f = fract(p) - 0.5;\n  float h = hash(id);\n  if (h > uStars) return vec3(0.0);\n  vec2 o = (vec2(hash(id + 1.3), hash(id + 7.1)) - 0.5) * 0.8;\n  vec2 d = f - o;\n  float r = length(d);\n  float size = mix(0.015, 0.05, pow(hash(id + 3.7), 3.0));\n  float core = exp(-r * r / (size * size));\n  // Bigger stars get a faint cross flare.\n  float flare = size > 0.035 ? max(0.0, 1.0 - abs(d.x) * 40.0) * max(0.0, 1.0 - abs(d.y) * 10.0)\n                             + max(0.0, 1.0 - abs(d.y) * 40.0) * max(0.0, 1.0 - abs(d.x) * 10.0) : 0.0;\n  float tw = 0.65 + 0.35 * sin(t * (1.5 + h * 4.0) + h * 60.0);\n  vec3 tint = mix(vec3(0.75, 0.85, 1.0), vec3(1.0, 0.85, 0.7), hash(id + 9.2));\n  float b = 0.25 + 0.75 * pow(hash(id + 5.5), 2.0);\n  return tint * (core + flare * 0.35) * tw * bright * b;\n}\n\nvoid main() {\n  vec2 uv = gl_FragCoord.xy / uRes;\n  vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;\n  float t = uTime;\n\n  // Nebula: domain-warped noise with dark dust lanes cut through it.\n  vec2 q = p * 1.6 / uScale + vec2(t * 0.012, t * 0.004);\n  vec2 w = vec2(fbm(q + vec2(0.0, t * 0.02)), fbm(q + vec2(5.2, 1.3) - t * 0.015));\n  float n = fbm(q + 2.2 * w);\n  float dust = fbm(q * 2.3 + w * 1.5 + 11.0);\n  // A soft diagonal band, like the plane of a galaxy, where the gas gathers.\n  float band = exp(-pow((p.y + p.x * 0.45 + 0.08 * sin(p.x * 2.5 + t * 0.03)) * 2.2 / uScale, 2.0));\n  float cloud = smoothstep(0.32, 0.85, n + band * 0.28 - 0.12) * (1.0 - smoothstep(0.5, 0.85, dust) * 0.7);\n  vec3 neb = mix(uColor, uAccent, smoothstep(0.35, 0.7, w.x));\n  vec3 col = vec3(0.004, 0.005, 0.016);\n  col += neb * cloud * cloud * 1.3 * uIntensity + neb * cloud * 0.25 * uIntensity;\n  col += mix(neb, vec3(1.0), 0.4) * pow(cloud, 5.0) * 0.8 * uIntensity;\n  col += uAccent * 0.05 * uIntensity * smoothstep(0.2, 0.9, w.y);\n  col += neb * band * 0.05 * uIntensity;\n\n  // Parallax star layers: nearer layers drift faster and shine brighter.\n  col += stars(p + vec2(t * 0.004, t * 0.001), 42.0, 0.55, t);\n  col += stars(p + vec2(t * 0.009, t * 0.002) + 3.1, 24.0, 0.85, t);\n  col += stars(p + vec2(t * 0.018, t * 0.004) + 7.7, 12.0, 1.1, t) * (0.6 + cloud);\n\n  // Shooting star: a new chance every few seconds.\n  if (uShoot > 0.5) {\n    float period = 4.5;\n    float k = floor(t / period);\n    float local = t - k * period;\n    if (hash(vec2(k, 3.7)) < 0.75 && local < 1.1) {\n      float aspect = uRes.x / uRes.y;\n      vec2 start = vec2((hash(vec2(k, 1.1)) * 0.9 - 0.1) * aspect, 0.25 + hash(vec2(k, 2.3)) * 0.3);\n      float ang = -0.35 - hash(vec2(k, 5.9)) * 0.4;\n      vec2 dir = vec2(-cos(ang), sin(ang));\n      vec2 head = start + dir * local * 1.1;\n      float len = 0.28;\n      vec2 rel = p - head;\n      float along = dot(rel, -dir);\n      float across = length(rel + dir * along);\n      float fade = smoothstep(0.0, 0.15, local) * smoothstep(1.1, 0.7, local);\n      if (along > 0.0 && along < len) {\n        float trail = pow(1.0 - along / len, 2.0);\n        col += vec3(0.85, 0.9, 1.0) * trail * exp(-across * across * 90000.0 / (1.0 + along * 40.0)) * fade;\n      }\n      col += vec3(0.9, 0.95, 1.0) * 0.0002 / (dot(rel, rel) + 0.0002) * fade * 0.5;\n    }\n  }\n\n  vec2 v = uv - 0.5;\n  col *= 1.0 - dot(v, v) * 1.1;\n  col = 1.0 - exp(-col * 1.4); // soft tonemap keeps bright cores from clipping\n  col += (hash(gl_FragCoord.xy + fract(t) * 91.7) - 0.5) * 0.006;\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 CosmicBackground({\n  color = \"#6d28d9\",\n  accent = \"#db2777\",\n  speed = 1,\n  scale = 1,\n  intensity = 1,\n  stars = 0.5,\n  shootingStars = true,\n  className,\n}: CosmicBackgroundProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ color, accent, speed, scale, intensity, stars, shootingStars });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { color, accent, speed, scale, intensity, stars, shootingStars };\n    redraw.current?.();\n  }, [color, accent, speed, scale, intensity, stars, shootingStars]);\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(\"uColor\"), rgb(o.color));\n      gl.uniform3fv(u(\"uAccent\"), rgb(o.accent));\n      gl.uniform1f(u(\"uScale\"), Math.max(o.scale, 0.05));\n      gl.uniform1f(u(\"uIntensity\"), o.intensity);\n      gl.uniform1f(u(\"uStars\"), Math.min(Math.max(o.stars, 0), 1));\n      gl.uniform1f(u(\"uShoot\"), o.shootingStars && !reduced ? 1 : 0);\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={{\n        position: \"absolute\",\n        inset: 0,\n        overflow: \"hidden\",\n        pointerEvents: \"none\",\n        // Shown only if WebGL is unavailable.\n        background: `radial-gradient(ellipse at 30% 40%, ${color}40, transparent 60%), radial-gradient(ellipse at 75% 65%, ${accent}33, transparent 55%), #03040b`,\n      }}\n    />\n  );\n}\n"}]}