{
  "name": "beach-chair",
  "title": "BeachChair",
  "type": "registry:component",
  "description": "A sun lounger for a beach or pool deck: a slatted frame on four legs along local Z, a flat bed and a back raised on a hinge (recline), cushioned. Seeded cushion and teak or white frame. One cuboid collider round the bed.",
  "dependencies": [
    "@react-three/rapier@^2.2.0",
    "@runek/core@^0.18.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "BeachChair.tsx",
      "content": "import { CuboidCollider, RigidBody } from '@react-three/rapier'\nimport { pick, rng, useMaterials, useWorld, type Vec3, type WorldComponentProps } from '@runek/core'\n\nexport interface BeachChairProps extends WorldComponentProps {\n  /** Along local Z, in units. */\n  length?: number\n  width?: number\n  /** To the top of the cushion on the bed, in units. */\n  height?: number\n  /** How far the back is raised from flat, in radians. */\n  recline?: number\n  /** The cushion. Seeded from beach colours when unset. */\n  color?: string\n  /** The frame. Seeded teak or white when unset. */\n  frameColor?: string\n}\n\nconst CUSHIONS = ['#f4f1ea', '#2a9d8f', '#e76f51', '#264653', '#e9c46a', '#7fb7be', '#d1495b']\nconst FRAMES = ['#9a6b3f', '#9a6b3f', '#f2f0eb']\nconst BACK = 0.62\nconst CUSHION = 0.06\n\n/**\n * A sun lounger for a beach or a pool deck: a slatted frame on four legs lying along local Z,\n * a flat bed, and a back raised on a hinge at the -Z end, with a cushion over both. A\n * `Person` in the `lie` pose lies along it at `height`, head toward the back (lower `recline`\n * to lay the back flat). Seeded: the cushion and the frame. One cuboid collider round the bed.\n */\nexport function BeachChair({\n  position = [0, 0, 0],\n  rotation = [0, 0, 0],\n  seed = 1,\n  length = 1.9,\n  width = 0.66,\n  height = 0.36,\n  recline = 0.7,\n  color,\n  frameColor,\n}: BeachChairProps) {\n  const { unit: u } = useWorld()\n  const next = rng(seed)\n  const cushion = color ?? pick(next, CUSHIONS)\n  const frame = frameColor ?? pick(next, FRAMES)\n  const m = useMaterials({\n    frame: { slot: 'wood', color: frame, roughness: 0.75 },\n    cushion: { slot: 'fabric', color: cushion, roughness: 0.95 },\n  })\n  const L = length * u\n  const W = width * u\n  const top = height * u\n  const c = CUSHION * u\n  const bed = top - c\n  const rail = 0.04 * u\n  const back = Math.min(BACK * u, L * 0.4)\n  const hinge = -L / 2 + back\n  const leg = 0.05 * u\n  const lx = W / 2 - leg / 2\n  const legs: Vec3[] = [\n    [-lx, (bed - rail) / 2, -L / 2 + leg],\n    [lx, (bed - rail) / 2, -L / 2 + leg],\n    [-lx, (bed - rail) / 2, L / 2 - leg],\n    [lx, (bed - rail) / 2, L / 2 - leg],\n  ]\n  const bedL = L / 2 - hinge\n\n  return (\n    <RigidBody type=\"fixed\" colliders={false} position={position} rotation={rotation}>\n      <CuboidCollider\n        args={[W / 2, top / 2, bedL / 2]}\n        position={[0, top / 2, (hinge + L / 2) / 2]}\n      />\n\n      {[-1, 1].map((s) => (\n        <mesh\n          key={`rail${s}`}\n          material={m.frame}\n          castShadow\n          position={[s * (W / 2 - rail / 2), bed - rail / 2, 0]}\n        >\n          <boxGeometry args={[rail, rail, L]} />\n        </mesh>\n      ))}\n      {legs.map((p) => (\n        <mesh key={p.join()} material={m.frame} castShadow position={p}>\n          <boxGeometry args={[leg, bed - rail, leg]} />\n        </mesh>\n      ))}\n      {Array.from({ length: 7 }, (_, i) => (\n        <mesh\n          key={`slat${i}`}\n          material={m.frame}\n          receiveShadow\n          position={[0, bed - 0.01 * u, hinge + (bedL * (i + 0.5)) / 7]}\n        >\n          <boxGeometry args={[W - rail * 2, 0.02 * u, (bedL / 7) * 0.7]} />\n        </mesh>\n      ))}\n      <mesh\n        material={m.cushion}\n        castShadow\n        receiveShadow\n        position={[0, bed + c / 2, (hinge + L / 2) / 2]}\n      >\n        <boxGeometry args={[W - rail * 0.5, c, bedL - 0.01 * u]} />\n      </mesh>\n\n      <group position={[0, bed, hinge]} rotation={[recline, 0, 0]}>\n        <mesh material={m.frame} castShadow position={[0, -0.01 * u, -back / 2]}>\n          <boxGeometry args={[W - rail * 2, 0.02 * u, back]} />\n        </mesh>\n        <mesh material={m.cushion} castShadow receiveShadow position={[0, c / 2, -back / 2]}>\n          <boxGeometry args={[W - rail * 0.5, c, back - 0.01 * u]} />\n        </mesh>\n      </group>\n      {recline > 0.05 &&\n        [-1, 1].map((s) => {\n          // A prop from the frame up to the underside of the raised back, two thirds along it.\n          const along = back * 0.66\n          const y = Math.sin(recline) * along\n          const z = hinge - Math.cos(recline) * along\n          const foot = hinge - back * 0.9\n          const dz = z - foot\n          return (\n            <mesh\n              key={`prop${s}`}\n              material={m.frame}\n              castShadow\n              position={[s * (W / 2 - rail * 1.5), bed + y / 2 - 0.02 * u, (z + foot) / 2]}\n              rotation={[Math.atan2(dz, y), 0, 0]}\n            >\n              <boxGeometry args={[0.025 * u, Math.hypot(y, dz), 0.025 * u]} />\n            </mesh>\n          )\n        })}\n    </RigidBody>\n  )\n}\n\nBeachChair.groundSitting = true\n",
      "type": "registry:component"
    }
  ]
}
