{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "globe",
  "type": "registry:component",
  "title": "Globe",
  "description": "A dotted globe that turns slowly and can be dragged to spin, with optional highlighted markers by latitude and longitude.",
  "categories": [
    "three"
  ],
  "dependencies": [
    "@react-three/fiber",
    "three"
  ],
  "devDependencies": [
    "@types/three"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/theme-color"
  ],
  "files": [
    {
      "path": "registry/three/globe.tsx",
      "type": "registry:component",
      "target": "components/three/globe.tsx",
      "content": "\"use client\";\n/**\n * @name globe\n * @category three\n * @description A dotted globe that turns slowly and can be dragged to spin, with optional highlighted markers by latitude and longitude.\n * @when Showing reach or places at a glance: offices, regions, where requests come from. Use it inside scene. For exact values per place use a table or bar-chart; the globe is not a chart.\n * @example\n * <Scene aspect={1} className=\"max-w-lg\" label=\"A globe with markers on Amsterdam, Kochi and San Francisco\">\n *   <Globe\n *     markers={[\n *       { lat: 52.37, lon: 4.9 },\n *       { lat: 9.93, lon: 76.27 },\n *       { lat: 37.77, lon: -122.42 },\n *     ]}\n *   />\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 Group } from \"three\";\nimport { useThemeColors } from \"@/lib/theme-color\";\n\nexport type GlobeMarker = {\n  /** Degrees north (south is negative). */\n  lat: number;\n  /** Degrees east (west is negative). */\n  lon: number;\n};\n\nexport type GlobeProps = {\n  /** Places to highlight. */\n  markers?: readonly GlobeMarker[];\n  /** How many dots make the globe. */\n  count?: number;\n  /** Radius in scene units. */\n  radius?: number;\n  /** The CSS variable the dots take their colour from. */\n  color?: string;\n  /** The CSS variable the markers take their colour from. */\n  markerColor?: string;\n  /** Diameter of a dot in scene units. */\n  dotSize?: number;\n  /** Diameter of a marker in scene units. */\n  markerSize?: number;\n  /** Turning speed in radians per second. 0 stops it; dragging still works. */\n  speed?: number;\n  /** How far the axis leans toward the viewer, in radians. */\n  tilt?: number;\n  /** Let the pointer spin it. Horizontal drags spin; vertical touches still scroll the page. */\n  draggable?: boolean;\n  /** Where it sits in the scene. */\n  position?: [number, number, number];\n};\n\nconst VERTEX = /* glsl */ `\nuniform float uSize;\nuniform float uScale;\nvarying float vFacing;\n\nvoid main() {\n  vec4 viewPosition = modelViewMatrix * vec4(position, 1.0);\n  gl_Position = projectionMatrix * viewPosition;\n  gl_PointSize = max(1.5, uSize * uScale * projectionMatrix[1][1] / max(0.001, -viewPosition.z));\n  // On a sphere centred on its own origin, the position is the normal.\n  vec3 outward = normalize(normalMatrix * normalize(position));\n  vFacing = dot(outward, normalize(-viewPosition.xyz));\n}\n`;\n\nconst FRAGMENT = /* glsl */ `\nuniform vec3 uColor;\nuniform float uBack;\nvarying float vFacing;\n\nvoid main() {\n  float d = length(gl_PointCoord - vec2(0.5));\n  float disc = 1.0 - smoothstep(0.38, 0.5, d);\n  // The far side shows through, dimmed, so the globe reads as a sphere and not a disc.\n  float alpha = disc * mix(uBack, 1.0, smoothstep(-0.12, 0.12, vFacing));\n  if (alpha < 0.01) discard;\n  gl_FragColor = vec4(uColor, alpha);\n  #include <colorspace_fragment>\n}\n`;\n\nconst DEGREES = Math.PI / 180;\n\nfunction place(target: Float32Array, index: number, lat: number, lon: number, radius: number): void {\n  const phi = (90 - lat) * DEGREES;\n  const theta = (lon + 180) * DEGREES;\n  target[index * 3] = -radius * Math.sin(phi) * Math.cos(theta);\n  target[index * 3 + 1] = radius * Math.cos(phi);\n  target[index * 3 + 2] = radius * Math.sin(phi) * Math.sin(theta);\n}\n\nfunction pointUniforms(): { uSize: { value: number }; uScale: { value: number }; uBack: { value: number }; uColor: { value: Color } } {\n  return { uSize: { value: 0 }, uScale: { value: 1 }, uBack: { value: 0.2 }, uColor: { value: new Color() } };\n}\n\nexport function Globe({\n  markers,\n  count = 1800,\n  radius = 2,\n  color = \"--foreground\",\n  markerColor = \"--highlight\",\n  dotSize = 0.035,\n  markerSize = 0.13,\n  speed = 0.12,\n  tilt = 0.3,\n  draggable = true,\n  position,\n}: GlobeProps) {\n  const group = useRef<Group>(null);\n  const motion = useRef({ spin: 0, lift: 0, velocity: 0, dragging: false, pointer: -1, x: 0, y: 0, at: 0 });\n  const canvas = useThree((state) => state.gl.domElement);\n  const invalidate = useThree((state) => state.invalidate);\n  const colors: Record<string, string> = useThemeColors([color, markerColor], canvas);\n  const dotFill = colors[color] ?? \"\";\n  const markerFill = colors[markerColor] ?? \"\";\n  const total = Math.max(1, Math.round(count));\n  const markerKey = (markers ?? []).map((marker) => `${marker.lat},${marker.lon}`).join(\";\");\n\n  // Typed arrays only; the geometries that hold them are declared below and disposed by react-three-fiber.\n  const dots = useMemo(() => {\n    // A Fibonacci lattice: each dot a golden angle on from the last, evenly spaced from pole to pole.\n    const array = new Float32Array(total * 3);\n    const golden = Math.PI * (3 - Math.sqrt(5));\n    for (let index = 0; index < total; index += 1) {\n      const y = 1 - ((index + 0.5) / total) * 2;\n      const ring = Math.sqrt(1 - y * y);\n      const angle = index * golden;\n      array[index * 3] = Math.cos(angle) * ring * radius;\n      array[index * 3 + 1] = y * radius;\n      array[index * 3 + 2] = Math.sin(angle) * ring * radius;\n    }\n    return array;\n  }, [total, radius]);\n\n  const pins = useMemo(() => {\n    const list = markerKey === \"\" ? [] : markerKey.split(\";\");\n    const array = new Float32Array(list.length * 3);\n    list.forEach((entry, index) => {\n      const [lat, lon] = entry.split(\",\");\n      // Just above the surface, so a marker is never hidden behind the dot it lands on.\n      place(array, index, Number(lat), Number(lon), radius * 1.01);\n    });\n    return array;\n  }, [markerKey, radius]);\n\n  const dotUniforms = useMemo(() => pointUniforms(), []);\n  const pinUniforms = useMemo(() => pointUniforms(), []);\n\n  useEffect(() => {\n    dotUniforms.uColor.value.set(dotFill);\n    dotUniforms.uSize.value = dotSize;\n    dotUniforms.uBack.value = 0.22;\n    pinUniforms.uColor.value.set(markerFill);\n    pinUniforms.uSize.value = markerSize;\n    pinUniforms.uBack.value = 0.3;\n    invalidate();\n  }, [dotUniforms, pinUniforms, dotFill, markerFill, dotSize, markerSize, invalidate]);\n\n  useEffect(() => {\n    if (!draggable) return;\n    const state = motion.current;\n    const before = { touchAction: canvas.style.touchAction, cursor: canvas.style.cursor };\n    canvas.style.touchAction = \"pan-y\";\n    canvas.style.cursor = \"grab\";\n\n    const down = (event: PointerEvent): void => {\n      if (event.pointerType === \"mouse\" && event.button !== 0) return;\n      state.dragging = true;\n      state.pointer = event.pointerId;\n      state.x = event.clientX;\n      state.y = event.clientY;\n      state.at = event.timeStamp;\n      state.velocity = 0;\n      canvas.setPointerCapture(event.pointerId);\n      canvas.style.cursor = \"grabbing\";\n    };\n    const move = (event: PointerEvent): void => {\n      if (!state.dragging || event.pointerId !== state.pointer) return;\n      const turn = (event.clientX - state.x) * 0.006;\n      const seconds = Math.max(1, event.timeStamp - state.at) / 1000;\n      state.spin += turn;\n      state.lift = Math.max(-0.9, Math.min(0.9, state.lift + (event.clientY - state.y) * 0.006));\n      state.velocity = state.velocity * 0.6 + (turn / seconds) * 0.4;\n      state.x = event.clientX;\n      state.y = event.clientY;\n      state.at = event.timeStamp;\n      invalidate();\n    };\n    const up = (event: PointerEvent): void => {\n      if (event.pointerId !== state.pointer) return;\n      state.dragging = false;\n      state.pointer = -1;\n      // Held still before letting go: no throw.\n      if (event.timeStamp - state.at > 100) state.velocity = 0;\n      state.velocity = Math.max(-8, Math.min(8, state.velocity));\n      if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);\n      canvas.style.cursor = \"grab\";\n    };\n\n    canvas.addEventListener(\"pointerdown\", down);\n    canvas.addEventListener(\"pointermove\", move);\n    canvas.addEventListener(\"pointerup\", up);\n    canvas.addEventListener(\"pointercancel\", up);\n    return () => {\n      canvas.removeEventListener(\"pointerdown\", down);\n      canvas.removeEventListener(\"pointermove\", move);\n      canvas.removeEventListener(\"pointerup\", up);\n      canvas.removeEventListener(\"pointercancel\", up);\n      canvas.style.touchAction = before.touchAction;\n      canvas.style.cursor = before.cursor;\n      state.dragging = false;\n    };\n  }, [canvas, draggable, invalidate]);\n\n  useFrame((state, delta) => {\n    const dt = Math.min(delta, 0.1);\n    const scale = state.size.height * state.viewport.dpr * 0.5;\n    dotUniforms.uScale.value = scale;\n    pinUniforms.uScale.value = scale;\n    const current = motion.current;\n    if (!current.dragging) {\n      current.spin += (speed + current.velocity) * dt;\n      current.velocity *= Math.exp(-dt * 2.5);\n    }\n    const root = group.current;\n    if (root) {\n      root.rotation.x = tilt + current.lift;\n      root.rotation.y = current.spin;\n    }\n  });\n\n  return (\n    <group ref={group} position={position}>\n      <points key={`dots:${total}:${radius}`} frustumCulled={false}>\n        <bufferGeometry>\n          <bufferAttribute attach=\"attributes-position\" args={[dots, 3]} />\n        </bufferGeometry>\n        <shaderMaterial args={[{ uniforms: dotUniforms, vertexShader: VERTEX, fragmentShader: FRAGMENT, transparent: true, depthWrite: false }]} />\n      </points>\n      {pins.length > 0 ? (\n        <points key={`pins:${markerKey}:${radius}`} frustumCulled={false} renderOrder={1}>\n          <bufferGeometry>\n            <bufferAttribute attach=\"attributes-position\" args={[pins, 3]} />\n          </bufferGeometry>\n          <shaderMaterial args={[{ uniforms: pinUniforms, vertexShader: VERTEX, fragmentShader: FRAGMENT, transparent: true, depthWrite: false }]} />\n        </points>\n      ) : null}\n    </group>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "three",
    "when": "Showing reach or places at a glance: offices, regions, where requests come from. Use it inside scene. For exact values per place use a table or bar-chart; the globe is not a chart.",
    "example": "<Scene aspect={1} className=\"max-w-lg\" label=\"A globe with markers on Amsterdam, Kochi and San Francisco\">\n  <Globe\n    markers={[\n      { lat: 52.37, lon: 4.9 },\n      { lat: 9.93, lon: 76.27 },\n      { lat: 37.77, lon: -122.42 },\n    ]}\n  />\n</Scene>",
    "client": true
  }
}
