{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "particle-galaxy",
  "title": "Particle Galaxy",
  "author": "VenusUI",
  "description": "A WebGL spiral galaxy of glowing particles that turns and leans toward the pointer.",
  "dependencies": [
    "three",
    "@react-three/fiber"
  ],
  "devDependencies": [
    "@types/three"
  ],
  "registryDependencies": [
    "https://venusui.com/r/use-scene-state.json"
  ],
  "files": [
    {
      "path": "registry/default/ui/particle-galaxy.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { Canvas, useFrame, useThree } from \"@react-three/fiber\"\nimport { AdditiveBlending, Color, type Group, type ShaderMaterial } from \"three\"\n\nimport { cn } from \"@/lib/utils\"\nimport { useSceneState } from \"@/registry/default/hooks/use-scene-state\"\n\ninterface ParticleGalaxyProps {\n  /** Number of particles. Lower it for low-end phones. */\n  count?: number\n  /** Number of spiral arms. */\n  arms?: number\n  /** Colors from the core, through the middle, to the rim. Hex, rgb() or hsl(). */\n  colors?: [string, string, string]\n  /** Turning speed in radians per second. */\n  speed?: number\n  /** Lean the camera toward the pointer. */\n  followPointer?: boolean\n  /** Accessible description of the graphic. */\n  label?: string\n  className?: string\n}\n\nconst RADIUS = 5.5\n\nconst vertexShader = /* glsl */ `\n  uniform float uTime;\n  uniform float uSize;\n  attribute float aScale;\n  attribute float aRandom;\n  attribute vec3 aColor;\n  varying vec3 vColor;\n  void main() {\n    vec4 viewPosition = viewMatrix * modelMatrix * vec4(position, 1.0);\n    gl_Position = projectionMatrix * viewPosition;\n    float twinkle = 0.7 + 0.3 * sin(uTime * 1.7 + aRandom * 60.0);\n    gl_PointSize = uSize * aScale * twinkle / -viewPosition.z;\n    vColor = aColor;\n  }\n`\nconst fragmentShader = /* glsl */ `\n  varying vec3 vColor;\n  void main() {\n    float d = distance(gl_PointCoord, vec2(0.5)) * 2.0;\n    float strength = pow(clamp(1.0 - d, 0.0, 1.0), 2.6);\n    gl_FragColor = vec4(vColor * strength, strength);\n  }\n`\n\n/** Seeded random numbers, so the galaxy is the same shape on every load. */\nfunction random(seed: number) {\n  let s = seed\n  return () => {\n    s = (s * 16807) % 2147483647\n    return (s - 1) / 2147483646\n  }\n}\n\nfunction buildGalaxy(count: number, arms: number, colors: [string, string, string]) {\n  const rand = random(42)\n  const positions = new Float32Array(count * 3)\n  const tints = new Float32Array(count * 3)\n  const scales = new Float32Array(count)\n  const randoms = new Float32Array(count)\n  const [inner, middle, outer] = colors.map((c) => new Color(c)) as [Color, Color, Color]\n  const color = new Color()\n  const jitter = () => Math.pow(rand(), 3) * (rand() < 0.5 ? 1 : -1)\n  const coreCount = Math.round(count * 0.08)\n\n  for (let i = 0; i < count; i++) {\n    if (i < coreCount) {\n      // A dense, warm core.\n      const r = Math.pow(rand(), 2) * 0.9\n      const theta = rand() * Math.PI * 2\n      positions.set([Math.cos(theta) * r, jitter() * 0.18, Math.sin(theta) * r], i * 3)\n      color.copy(inner).lerp(new Color(\"#ffffff\"), 0.35)\n      tints.set([color.r, color.g, color.b], i * 3)\n      scales[i] = 0.8 + rand() * 1.4\n      randoms[i] = rand()\n      continue\n    }\n    const r = 0.3 + Math.pow(rand(), 1.4) * RADIUS\n    const angle = ((i % arms) / arms) * Math.PI * 2 + r * 1.15\n    const spread = 0.08 + r * 0.07\n    positions.set(\n      [Math.cos(angle) * r + jitter() * spread * 1.6, jitter() * spread * 0.5, Math.sin(angle) * r + jitter() * spread * 1.6],\n      i * 3\n    )\n    const t = r / RADIUS\n    if (t < 0.45) color.copy(inner).lerp(middle, t / 0.45)\n    else color.copy(middle).lerp(outer, (t - 0.45) / 0.55)\n    tints.set([color.r, color.g, color.b], i * 3)\n    scales[i] = 0.4 + rand() * 1.1\n    randoms[i] = rand()\n  }\n  return { positions, tints, scales, randoms }\n}\n\ninterface Pointer {\n  x: number\n  y: number\n}\n\nfunction Galaxy({\n  count,\n  arms,\n  colors,\n  speed,\n  pointer,\n}: {\n  count: number\n  arms: number\n  colors: [string, string, string]\n  speed: number\n  pointer: React.RefObject<Pointer | null>\n}) {\n  const group = React.useRef<Group>(null)\n  const material = React.useRef<ShaderMaterial>(null)\n  const gl = useThree((state) => state.gl)\n  const [inner, middle, outer] = colors\n  const data = React.useMemo(() => buildGalaxy(count, arms, [inner, middle, outer]), [count, arms, inner, middle, outer])\n  const uniforms = React.useMemo(() => ({ uTime: { value: 0 }, uSize: { value: 60 * gl.getPixelRatio() } }), [gl])\n\n  useFrame(({ camera }, delta) => {\n    const step = Math.min(delta, 0.05)\n    if (material.current) material.current.uniforms.uTime!.value += step\n    if (group.current) group.current.rotation.y += step * speed\n    const p = pointer.current ?? { x: 0, y: 0 }\n    const ease = 1 - Math.pow(0.04, step || 1)\n    camera.position.x += (p.x * 1.2 - camera.position.x) * ease\n    camera.position.y += (4.6 - p.y * 0.8 - camera.position.y) * ease\n    camera.lookAt(0, 0, 0)\n  })\n\n  return (\n    <group ref={group} rotation={[0.18, 0, -0.12]}>\n      <points>\n        <bufferGeometry>\n          <bufferAttribute attach=\"attributes-position\" args={[data.positions, 3]} />\n          <bufferAttribute attach=\"attributes-aColor\" args={[data.tints, 3]} />\n          <bufferAttribute attach=\"attributes-aScale\" args={[data.scales, 1]} />\n          <bufferAttribute attach=\"attributes-aRandom\" args={[data.randoms, 1]} />\n        </bufferGeometry>\n        <shaderMaterial\n          ref={material}\n          vertexShader={vertexShader}\n          fragmentShader={fragmentShader}\n          uniforms={uniforms}\n          transparent\n          depthWrite={false}\n          blending={AdditiveBlending}\n        />\n      </points>\n    </group>\n  )\n}\n\n/**\n * A spiral galaxy of glowing particles that slowly turns and leans toward\n * the pointer. Made for dark backgrounds; fill a `relative` section with it\n * as a backdrop. Rendering pauses off screen, and with reduced motion it\n * holds still.\n */\nfunction ParticleGalaxy({\n  count = 40000,\n  arms = 4,\n  colors = [\"#ffb48f\", \"#c084fc\", \"#38bdf8\"],\n  speed = 0.06,\n  followPointer = true,\n  label = \"Spiral galaxy of glowing particles\",\n  className,\n}: ParticleGalaxyProps) {\n  const ref = React.useRef<HTMLDivElement>(null)\n  const pointer = React.useRef<Pointer | null>(null)\n  const { active, reducedMotion } = useSceneState(ref)\n\n  React.useEffect(() => {\n    if (!followPointer) return\n    const onMove = (event: PointerEvent) => {\n      pointer.current = {\n        x: (event.clientX / window.innerWidth) * 2 - 1,\n        y: (event.clientY / window.innerHeight) * 2 - 1,\n      }\n    }\n    window.addEventListener(\"pointermove\", onMove, { passive: true })\n    return () => window.removeEventListener(\"pointermove\", onMove)\n  }, [followPointer])\n\n  return (\n    <div ref={ref} role=\"img\" aria-label={label} data-slot=\"particle-galaxy\" className={cn(\"relative h-96 w-full\", className)}>\n      <Canvas\n        camera={{ position: [0, 4.6, 6.4], fov: 55 }}\n        dpr={[1, 1.75]}\n        gl={{ antialias: false, alpha: true }}\n        frameloop={active ? \"always\" : \"demand\"}\n        fallback={<p className=\"p-4 text-sm text-muted-foreground\">3D graphics are not available in this browser.</p>}\n      >\n        <Galaxy count={count} arms={arms} colors={colors} speed={reducedMotion ? 0 : speed} pointer={pointer} />\n      </Canvas>\n    </div>\n  )\n}\n\nexport { ParticleGalaxy, type ParticleGalaxyProps }\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "version": "0.1.0"
  },
  "categories": [
    "3d",
    "backgrounds"
  ],
  "type": "registry:ui"
}