{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "particle-field",
  "type": "registry:component",
  "title": "Particle Field",
  "description": "A field of small round points drifting slowly, with depth that shifts as the pointer moves.",
  "categories": [
    "three"
  ],
  "dependencies": [
    "@react-three/fiber",
    "three"
  ],
  "devDependencies": [
    "@types/three"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/theme-color"
  ],
  "files": [
    {
      "path": "registry/three/particle-field.tsx",
      "type": "registry:component",
      "target": "components/three/particle-field.tsx",
      "content": "\"use client\";\n/**\n * @name particle-field\n * @category three\n * @description A field of small round points drifting slowly, with depth that shifts as the pointer moves.\n * @when A quiet moving ground behind a hero or a section. Use it inside scene and keep text on top readable. For one focal object use orb; for data use the viz components, never particles.\n * @example\n * <Scene className=\"absolute inset-0\">\n *   <ParticleField />\n * </Scene>\n *\n * <Scene aspect={2}>\n *   <ParticleField count={400} color=\"--foreground\" size={0.03} spread={10} />\n * </Scene>\n */\n/* eslint-disable react-hooks/immutability -- three.js objects (uniforms, the canvas, scene nodes) are changed in place by design, in effects and frames, never during render */\nimport { useFrame, useThree } from \"@react-three/fiber\";\nimport { useEffect, useMemo, useRef } from \"react\";\nimport { Color, type Points } from \"three\";\nimport { useThemeColors } from \"@/lib/theme-color\";\n\nexport type ParticleFieldProps = {\n  /** How many points. Halved when the canvas is narrower than 640px. */\n  count?: number;\n  /** The CSS variable the points take their colour from. */\n  color?: string;\n  /** Diameter of a point in scene units. */\n  size?: number;\n  /** Width of the field in scene units; it is two thirds as tall and as deep. */\n  spread?: number;\n  /** Drift speed. 1 is slow; 0 holds the field still (it still answers the pointer). */\n  speed?: number;\n  /** 0 to 1. */\n  opacity?: number;\n};\n\nconst VERTEX = /* glsl */ `\nuniform float uSize;\nuniform float uScale;\nvarying float vFade;\n\nvoid main() {\n  vec4 viewPosition = modelViewMatrix * vec4(position, 1.0);\n  gl_Position = projectionMatrix * viewPosition;\n  // World-sized points: half the drawing-buffer height, times the projection, over the distance.\n  gl_PointSize = max(1.5, uSize * uScale * projectionMatrix[1][1] / max(0.001, -viewPosition.z));\n  // Far points are fainter, which is what gives the field its depth.\n  vFade = clamp(1.6 - (-viewPosition.z) / 9.0, 0.25, 1.0);\n}\n`;\n\nconst FRAGMENT = /* glsl */ `\nuniform vec3 uColor;\nuniform float uOpacity;\nvarying float vFade;\n\nvoid main() {\n  float d = length(gl_PointCoord - vec2(0.5));\n  float alpha = (1.0 - smoothstep(0.38, 0.5, d)) * uOpacity * vFade;\n  if (alpha < 0.01) discard;\n  gl_FragColor = vec4(uColor, alpha);\n  #include <colorspace_fragment>\n}\n`;\n\n/** A small seeded generator, so the same count gives the same field on every mount. */\nfunction seeded(seed: number): () => number {\n  let state = seed >>> 0;\n  return () => {\n    state = (state + 1831565813) >>> 0;\n    let t = state;\n    t = Math.imul(t ^ (t >>> 15), t | 1);\n    t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n  };\n}\n\nexport function ParticleField({ count = 600, color = \"--highlight\", size = 0.045, spread = 12, speed = 1, opacity = 0.8 }: ParticleFieldProps) {\n  const points = useRef<Points>(null);\n  const drift = useRef(0);\n  const lean = useRef({ x: 0, y: 0 });\n  const canvas = useThree((state) => state.gl.domElement);\n  const invalidate = useThree((state) => state.invalidate);\n  const small = useThree((state) => state.size.width < 640);\n  const fill = useThemeColors([color], canvas)[color] ?? \"\";\n  const total = Math.max(1, Math.round(small ? count / 2 : count));\n\n  // A typed array only: the geometry that holds it is declared below and disposed by react-three-fiber.\n  const positions = useMemo(() => {\n    const random = seeded(total * 7919 + 17);\n    const array = new Float32Array(total * 3);\n    for (let index = 0; index < total; index += 1) {\n      array[index * 3] = (random() - 0.5) * spread;\n      array[index * 3 + 1] = (random() - 0.5) * spread * 0.66;\n      array[index * 3 + 2] = (random() - 0.5) * spread * 0.66;\n    }\n    return array;\n  }, [total, spread]);\n\n  const uniforms = useMemo(\n    () => ({\n      uSize: { value: 0 },\n      uScale: { value: 1 },\n      uOpacity: { value: 1 },\n      uColor: { value: new Color() },\n    }),\n    [],\n  );\n\n  useEffect(() => {\n    uniforms.uColor.value.set(fill);\n    uniforms.uSize.value = size;\n    uniforms.uOpacity.value = opacity;\n    invalidate();\n  }, [uniforms, fill, size, opacity, invalidate]);\n\n  useFrame((state, delta) => {\n    const dt = Math.min(delta, 0.1);\n    uniforms.uScale.value = state.size.height * state.viewport.dpr * 0.5;\n    drift.current += dt * speed * 0.03;\n    const ease = 1 - Math.exp(-dt * 2);\n    lean.current.x += (state.pointer.x - lean.current.x) * ease;\n    lean.current.y += (state.pointer.y - lean.current.y) * ease;\n    const object = points.current;\n    if (object) {\n      // Turning the whole field a little moves near points further than far ones: parallax.\n      object.rotation.y = drift.current + lean.current.x * 0.12;\n      object.rotation.x = -lean.current.y * 0.08;\n    }\n  });\n\n  return (\n    <points ref={points} key={`${total}:${spread}`} frustumCulled={false}>\n      <bufferGeometry>\n        <bufferAttribute attach=\"attributes-position\" args={[positions, 3]} />\n      </bufferGeometry>\n      <shaderMaterial args={[{ uniforms, vertexShader: VERTEX, fragmentShader: FRAGMENT, transparent: true, depthWrite: false }]} />\n    </points>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "three",
    "when": "A quiet moving ground behind a hero or a section. Use it inside scene and keep text on top readable. For one focal object use orb; for data use the viz components, never particles.",
    "example": "<Scene className=\"absolute inset-0\">\n  <ParticleField />\n</Scene>\n\n<Scene aspect={2}>\n  <ParticleField count={400} color=\"--foreground\" size={0.03} spread={10} />\n</Scene>",
    "client": true
  }
}
