{
  "name": "umbrella",
  "title": "Umbrella",
  "type": "registry:component",
  "description": "A beach umbrella: a weighted base, a wooden pole and a canopy of alternating panels on ribs, sagging a little between them. Leans with tilt; the pole runs up through a Table. One cylinder collider round the pole.",
  "dependencies": [
    "@react-three/rapier@^2.2.0",
    "@runek/core@^0.18.0",
    "three@^0.184.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "Umbrella.tsx",
      "content": "import { CylinderCollider, RigidBody } from '@react-three/rapier'\nimport { pick, rng, useMaterials, useWorld, type WorldComponentProps } from '@runek/core'\nimport { useMemo } from 'react'\nimport { BufferGeometry, DoubleSide, Float32BufferAttribute, Quaternion, Vector3 } from 'three'\n\nexport interface UmbrellaProps extends WorldComponentProps {\n  /** Canopy radius, in units. */\n  radius?: number\n  /** From the ground to the canopy's rim, in units. */\n  height?: number\n  /** Lean toward local +Z, in radians, from the foot of the pole. */\n  tilt?: number\n  /** Every other panel. Seeded from beach colours when unset. */\n  color?: string\n  /** The panels between. */\n  stripeColor?: string\n  /** Canopy panels, rounded to an even count so the stripes alternate all the way round. */\n  panels?: number\n}\n\nconst CANVAS = ['#d1495b', '#00798c', '#edae49', '#30638e', '#2a9d8f', '#e76f51', '#3c8d5a']\nconst UP = new Vector3(0, 1, 0)\nconst RADIAL = 6\nconst ACROSS = 6\n\n/** How high the canopy stands over its rim at `t` of the way out along a rib, 0 to 1. */\nconst profile = (t: number, rise: number) => rise * (1 - t ** 1.3)\n\n/** Every other panel of the canopy, starting at `first`, as one geometry around the pole,\n *  with its rim at y = 0. The canvas sags a little between the ribs. */\nfunction panelGeometry(R: number, rise: number, count: number, first: number) {\n  const pos: number[] = []\n  const index: number[] = []\n  const step = (Math.PI * 2) / count\n  for (let k = first; k < count; k += 2) {\n    const base = pos.length / 3\n    for (let i = 0; i <= RADIAL; i++) {\n      const t = i / RADIAL\n      for (let j = 0; j <= ACROSS; j++) {\n        const f = j / ACROSS\n        const a = (k + f) * step\n        const dip = Math.sin(Math.PI * f)\n        const r = t * R * (1 - 0.03 * dip)\n        pos.push(Math.sin(a) * r, profile(t, rise) - 0.06 * R * t * dip, Math.cos(a) * r)\n      }\n    }\n    for (let i = 0; i < RADIAL; i++) {\n      for (let j = 0; j < ACROSS; j++) {\n        const p00 = base + i * (ACROSS + 1) + j\n        const p10 = p00 + ACROSS + 1\n        index.push(p00, p10, p00 + 1, p10, p10 + 1, p00 + 1)\n      }\n    }\n  }\n  const g = new BufferGeometry()\n  g.setAttribute('position', new Float32BufferAttribute(pos, 3))\n  g.setIndex(index)\n  g.computeVertexNormals()\n  return g\n}\n\n/** A rod's centre, turn and length to run from `a` to `b`. */\nfunction rod(a: Vector3, b: Vector3) {\n  const d = b.clone().sub(a)\n  return {\n    position: a.clone().add(b).multiplyScalar(0.5),\n    quaternion: new Quaternion().setFromUnitVectors(UP, d.clone().normalize()),\n    length: d.length(),\n  }\n}\n\n/**\n * A beach umbrella: a weighted base, a wooden pole, and a canopy of panels in alternating\n * colours stretched over ribs, sagging a little between them, with a finial on top. `tilt`\n * leans the pole and canopy from its foot to shade against a low sun; the pole is thin enough\n * to run up through a `Table`. Seeded: the canvas colour. One cylinder collider round the pole.\n */\nexport function Umbrella({\n  position = [0, 0, 0],\n  rotation = [0, 0, 0],\n  seed = 1,\n  radius = 1.1,\n  height = 2.1,\n  tilt = 0,\n  color,\n  stripeColor = '#f4f1ea',\n  panels = 8,\n}: UmbrellaProps) {\n  const { unit: u, palette } = useWorld()\n  const m = useMaterials({\n    canvas: {\n      slot: 'fabric',\n      color: color ?? pick(rng(seed), CANVAS),\n      roughness: 0.9,\n      side: DoubleSide,\n    },\n    stripe: { slot: 'fabric', color: stripeColor, roughness: 0.9, side: DoubleSide },\n    wood: { slot: 'wood', color: palette.wood, roughness: 0.85 },\n    base: { slot: 'stone', color: palette.stone, roughness: 0.9 },\n  })\n  const R = radius * u\n  const H = height * u\n  const rise = 0.3 * R\n  const count = Math.max(6, Math.round(panels / 2) * 2)\n  const pole = 0.02 * u\n  const top = H + rise\n\n  const { canvas, stripe, ribs } = useMemo(() => {\n    const ribs = Array.from({ length: count }, (_, k) => {\n      const a = (k * Math.PI * 2) / count\n      const out = (t: number, drop: number) =>\n        new Vector3(Math.sin(a) * t * R, H + profile(t, rise) - drop, Math.cos(a) * t * R)\n      return [\n        rod(out(0.03, 0.02 * u), out(0.99, 0.015 * u)),\n        rod(new Vector3(0, H - 0.25 * R, 0), out(0.5, 0.06 * R)),\n      ]\n    }).flat()\n    return {\n      canvas: panelGeometry(R, rise, count, 0),\n      stripe: panelGeometry(R, rise, count, 1),\n      ribs,\n    }\n  }, [R, H, rise, count, u])\n\n  return (\n    <group position={position} rotation={rotation}>\n      <RigidBody type=\"fixed\" colliders={false}>\n        <CylinderCollider\n          args={[top / 2, pole * 2]}\n          position={[0, (Math.cos(tilt) * top) / 2, (Math.sin(tilt) * top) / 2]}\n          rotation={[tilt, 0, 0]}\n        />\n      </RigidBody>\n      <mesh material={m.base} castShadow receiveShadow position={[0, 0.04 * u, 0]}>\n        <cylinderGeometry args={[0.18 * u, 0.22 * u, 0.08 * u, 20]} />\n      </mesh>\n      <group rotation={[tilt, 0, 0]}>\n        <mesh material={m.wood} castShadow position={[0, top / 2, 0]}>\n          <cylinderGeometry args={[pole, pole, top, 8]} />\n        </mesh>\n        <mesh material={m.canvas} geometry={canvas} castShadow position={[0, H, 0]} />\n        <mesh material={m.stripe} geometry={stripe} castShadow position={[0, H, 0]} />\n        {ribs.map((r) => (\n          <mesh\n            key={r.position.toArray().join()}\n            material={m.wood}\n            position={r.position}\n            quaternion={r.quaternion}\n          >\n            <cylinderGeometry args={[0.006 * u, 0.006 * u, r.length, 5]} />\n          </mesh>\n        ))}\n        <mesh material={m.wood} position={[0, H - 0.25 * R, 0]}>\n          <cylinderGeometry args={[pole * 1.8, pole * 1.8, 0.08 * u, 10]} />\n        </mesh>\n        <mesh material={m.wood} castShadow position={[0, top + 0.04 * u, 0]}>\n          <sphereGeometry args={[0.035 * u, 12, 8]} />\n        </mesh>\n      </group>\n    </group>\n  )\n}\n\nUmbrella.groundSitting = true\n",
      "type": "registry:component"
    }
  ]
}
