{
  "name": "lighthouse",
  "title": "Lighthouse",
  "type": "registry:component",
  "description": "A banded lighthouse with a gallery, a glazed lantern and a lamp whose two beams sweep round after dusk; the lamp lights its surroundings through the world's light pool. One cylinder collider.",
  "dependencies": [
    "@react-three/fiber@^9.6.1",
    "@react-three/rapier@^2.2.0",
    "@runek/core@^0.18.0",
    "three@^0.184.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "Lighthouse.tsx",
      "content": "import { useFrame } from '@react-three/fiber'\nimport { CylinderCollider, RigidBody } from '@react-three/rapier'\nimport {\n  Dynamic,\n  useLight,\n  useLuminance,\n  useMaterials,\n  useSun,\n  useWorld,\n  type WorldComponentProps,\n} from '@runek/core'\nimport { useEffect, useMemo, useRef } from 'react'\nimport { AdditiveBlending, BufferAttribute, ConeGeometry, DoubleSide, type Group } from 'three'\n\nexport interface LighthouseProps extends WorldComponentProps {\n  /** Height of the tower to the gallery, in units. */\n  height?: number\n  /** Radius at the foot, in units; it tapers to two thirds of this at the top. */\n  radius?: number\n  /** Bands of colour up the tower. */\n  bands?: number\n  color?: string\n  bandColor?: string\n  /** The lamp and its beams. */\n  lightColor?: string\n  /** How fast the beams sweep round, in radians a second. */\n  speed?: number\n  /** Shine only while the world's sun is down. */\n  nightOnly?: boolean\n}\n\nconst IRON = '#23262b'\n\n/**\n * A lighthouse standing on its origin: a tapered tower in bands, with a door and a few small\n * windows, a gallery with a rail, a glazed lantern under a cap, and a lamp whose two beams\n * sweep round after dusk. The beams are glowing cones rather than lights; the lamp itself\n * lights its surroundings. One cylinder collider round the tower.\n */\nexport function Lighthouse({\n  position = [0, 0, 0],\n  rotation = [0, 0, 0],\n  height = 12,\n  radius = 1.6,\n  bands = 5,\n  color = '#f4f1ea',\n  bandColor = '#c0392b',\n  lightColor = '#fff2c4',\n  speed = 0.6,\n  nightOnly = true,\n}: LighthouseProps) {\n  const { unit } = useWorld()\n  const { lamp: glow } = useLuminance()\n  const sun = useSun()\n  const lit = !nightOnly || !sun.day\n  const lamp = useLight({ color: lightColor, intensity: 30, distance: 40 * unit, decay: 1.4 }, lit)\n  const m = useMaterials({\n    plinth: { slot: 'concrete', color: '#8d8a83', roughness: 1 },\n    tower: { slot: 'plaster', color, roughness: 0.8 },\n    band: { color: bandColor, roughness: 0.8 },\n    door: { color: '#3a2a1e', roughness: 0.8 },\n    iron: { slot: 'metal.painted', color: IRON, roughness: 0.6, metalness: 0.4 },\n    glass: {\n      slot: 'glass',\n      color: '#cfe6ee',\n      transparent: true,\n      opacity: 0.3,\n      roughness: 0.05,\n      side: DoubleSide,\n    },\n    bulb: lit\n      ? { color: lightColor, emissive: lightColor, emissiveIntensity: 3 * glow, toneMapped: false }\n      : { color: '#d9d4ca' },\n  })\n  const beams = useRef<Group>(null)\n  useFrame((_, dt) => {\n    if (beams.current) beams.current.rotation.y += speed * Math.min(dt, 0.1)\n  })\n\n  const H = height * unit\n  const R0 = radius * unit\n  const R1 = R0 * 0.66\n  const band = H / bands\n  const rAt = (y: number) => R0 + (R1 - R0) * (y / H)\n  const lanternH = 1.5 * unit\n  const lanternR = R1 * 0.8\n  const beamLength = 14 * unit\n  // Bright at the lamp, fading to nothing at the far end: added onto the scene, black adds no light.\n  const beam = useMemo(() => {\n    const g = new ConeGeometry(2.2 * unit, beamLength, 24, 8, true)\n    const p = g.attributes.position\n    const colors = new Float32Array(p.count * 3)\n    for (let i = 0; i < p.count; i++) {\n      const near = (p.getY(i) / beamLength + 0.5) ** 1.6\n      colors.set([near, near, near], i * 3)\n    }\n    g.setAttribute('color', new BufferAttribute(colors, 3))\n    return g\n  }, [unit, beamLength])\n  useEffect(() => () => beam.dispose(), [beam])\n\n  return (\n    <RigidBody type=\"fixed\" colliders={false} position={position} rotation={rotation}>\n      <CylinderCollider args={[H / 2, R0]} position={[0, H / 2, 0]} />\n      <mesh material={m.plinth} castShadow receiveShadow position={[0, 0.2 * unit, 0]}>\n        <cylinderGeometry args={[R0 * 1.25, R0 * 1.35, 0.4 * unit, 20]} />\n      </mesh>\n      {Array.from({ length: bands }, (_, i) => {\n        const y0 = i * band\n        return (\n          <mesh\n            key={y0}\n            material={i % 2 === 0 ? m.tower : m.band}\n            castShadow\n            receiveShadow\n            position={[0, y0 + band / 2, 0]}\n          >\n            <cylinderGeometry args={[rAt(y0 + band), rAt(y0), band, 24]} />\n          </mesh>\n        )\n      })}\n      <mesh material={m.door} position={[0, 1.0 * unit, rAt(1 * unit) - 0.02 * unit]}>\n        <boxGeometry args={[0.8 * unit, 1.9 * unit, 0.1 * unit]} />\n      </mesh>\n      {[0.35, 0.6, 0.82].map((f) => (\n        <mesh key={f} material={m.iron} position={[0, H * f, rAt(H * f) - 0.03 * unit]}>\n          <boxGeometry args={[0.35 * unit, 0.55 * unit, 0.1 * unit]} />\n        </mesh>\n      ))}\n\n      <group position={[0, H, 0]}>\n        <mesh material={m.iron} castShadow receiveShadow position={[0, 0.08 * unit, 0]}>\n          <cylinderGeometry args={[R1 * 1.45, R1 * 1.3, 0.16 * unit, 24]} />\n        </mesh>\n        <mesh material={m.iron} position={[0, 1.05 * unit, 0]} rotation={[Math.PI / 2, 0, 0]}>\n          <torusGeometry args={[R1 * 1.42, 0.03 * unit, 6, 40]} />\n        </mesh>\n        {Array.from({ length: 12 }, (_, i) => {\n          const a = (i / 12) * Math.PI * 2\n          return (\n            <mesh\n              key={a}\n              material={m.iron}\n              position={[Math.sin(a) * R1 * 1.42, 0.6 * unit, Math.cos(a) * R1 * 1.42]}\n            >\n              <cylinderGeometry args={[0.02 * unit, 0.02 * unit, 0.9 * unit, 6]} />\n            </mesh>\n          )\n        })}\n        <mesh material={m.glass} position={[0, 0.16 * unit + lanternH / 2, 0]}>\n          <cylinderGeometry args={[lanternR, lanternR, lanternH, 16, 1, true]} />\n        </mesh>\n        {Array.from({ length: 8 }, (_, i) => {\n          const a = (i / 8) * Math.PI * 2\n          return (\n            <mesh\n              key={a}\n              material={m.iron}\n              position={[\n                Math.sin(a) * lanternR,\n                0.16 * unit + lanternH / 2,\n                Math.cos(a) * lanternR,\n              ]}\n            >\n              <boxGeometry args={[0.05 * unit, lanternH, 0.05 * unit]} />\n            </mesh>\n          )\n        })}\n        <mesh material={m.band} castShadow position={[0, 0.16 * unit + lanternH + 0.45 * unit, 0]}>\n          <coneGeometry args={[lanternR * 1.25, 0.9 * unit, 16]} />\n        </mesh>\n        <mesh material={m.iron} position={[0, 0.16 * unit + lanternH + 0.98 * unit, 0]}>\n          <sphereGeometry args={[0.12 * unit, 10, 8]} />\n        </mesh>\n\n        <group position={[0, 0.16 * unit + lanternH * 0.5, 0]}>\n          <mesh material={m.bulb}>\n            <sphereGeometry args={[0.28 * unit, 16, 12]} />\n          </mesh>\n          <object3D ref={lamp.ref} />\n          {lit && (\n            <Dynamic>\n              <group ref={beams}>\n                {[1, -1].map((s) => (\n                  <mesh\n                    key={s}\n                    position={[0, 0, (s * beamLength) / 2]}\n                    rotation={[(-s * Math.PI) / 2, 0, 0]}\n                    geometry={beam}\n                  >\n                    <meshBasicMaterial\n                      vertexColors\n                      color={lightColor}\n                      transparent\n                      opacity={0.35}\n                      blending={AdditiveBlending}\n                      depthWrite={false}\n                      side={DoubleSide}\n                      toneMapped={false}\n                    />\n                  </mesh>\n                ))}\n              </group>\n            </Dynamic>\n          )}\n        </group>\n      </group>\n    </RigidBody>\n  )\n}\n\nLighthouse.groundSitting = true\n",
      "type": "registry:component"
    }
  ]
}
