{
  "name": "magazine-rack",
  "title": "MagazineRack",
  "type": "registry:component",
  "description": "A freestanding magazine rack: side panels and a back leaning back, sloped tiers with a lip holding magazines covers out, and newspapers on the foot. Covers shared with Newspapers. One cuboid collider.",
  "dependencies": [
    "@react-three/rapier@^2.2.0",
    "@runek/core@^0.18.0",
    "three@^0.184.0"
  ],
  "registryDependencies": [
    "newspapers"
  ],
  "files": [
    {
      "path": "MagazineRack.tsx",
      "content": "import { CuboidCollider, RigidBody } from '@react-three/rapier'\nimport { int, range, rng, useMaterial, useWorld, type WorldComponentProps } from '@runek/core'\nimport { useMemo } from 'react'\nimport { Euler, ExtrudeGeometry, Matrix4, Shape } from 'three'\nimport { frame, MAGAZINE, magazine, newspaper, PAPER, type Sheet, Sheets } from './Newspapers'\n\nexport interface MagazineRackProps extends WorldComponentProps {\n  /** Across, in units. */\n  width?: number\n  /** Sloped tiers of magazines, 1 to 4. */\n  tiers?: number\n  /** Defaults to the world palette's `woodDark` slot. */\n  color?: string\n}\n\nconst LEAN = 0.18\nconst PITCH = 0.3\nconst FIRST = 0.3\nconst SIDE = 0.018\nconst FRONT = 0.2\nconst DEPTH = 0.36\nconst FOOT = 0.05\n\n/**\n * A freestanding magazine rack for a lounge or a waiting room, facing local +Z: two side\n * panels and a back leaning back, sloped tiers with a lip, each holding magazines leaning\n * back with their covers out, and folded newspapers lying on the foot. It shares its covers\n * with `Newspapers`. Seeded: the covers and how many on each tier. One cuboid collider.\n */\nexport function MagazineRack({\n  position = [0, 0, 0],\n  rotation = [0, 0, 0],\n  seed = 1,\n  width = 0.6,\n  tiers = 3,\n  color,\n}: MagazineRackProps) {\n  const { unit: u, palette } = useWorld()\n  const wood = useMaterial({ slot: 'wood', color: color ?? palette.woodDark, roughness: 0.85 })\n  const n = Math.max(1, Math.min(4, Math.round(tiers)))\n  const W = width\n  const inner = W - SIDE * 2\n  const back = FIRST + n * PITCH + 0.02\n  const H = back * Math.cos(LEAN)\n  const foot = FRONT - DEPTH\n  const top = foot - H * Math.tan(LEAN)\n  const shelves = Array.from({ length: n }, (_, i) => FIRST + i * PITCH)\n\n  const side = useMemo(() => {\n    const s = new Shape()\n    s.moveTo(FRONT, 0)\n    s.lineTo(FRONT, 0.09)\n    s.lineTo(top + 0.1, H)\n    s.lineTo(top - 0.02, H)\n    s.lineTo(foot - 0.02, 0)\n    s.closePath()\n    const g = new ExtrudeGeometry(s, { depth: SIDE, bevelEnabled: false })\n    g.rotateY(-Math.PI / 2)\n    return g\n  }, [H, top, foot])\n\n  const sheets = useMemo(() => {\n    const next = rng(seed)\n    const out: Sheet[] = []\n    const lean = frame(1, 0, 0, foot, new Euler(-LEAN, 0, 0))\n    const stand = new Matrix4().makeRotationX(Math.PI / 2)\n    for (let i = 0; i < n; i++) {\n      const s = FIRST + i * PITCH\n      const count = int(next, 2, 3)\n      for (let k = 0; k < count; k++) {\n        const x = (-0.5 + (k + 0.5) / count) * (inner - MAGAZINE[0]) * 1.1\n        const at = lean\n          .clone()\n          .multiply(frame(1, x, s + MAGAZINE[2] / 2 + 0.002, 0.004 + k * 0.009))\n          .multiply(stand)\n          .multiply(new Matrix4().makeRotationY(range(next, -0.04, 0.04)))\n        out.push(...magazine(at, next))\n      }\n    }\n    for (let i = 0; i < 2; i++) {\n      const turn = new Euler(0, Math.PI / 2 + range(next, -0.08, 0.08), 0)\n      const z = (foot + FRONT) / 2 + 0.01\n      out.push(\n        ...newspaper(\n          frame(1, range(next, -0.04, 0.04), FOOT + 0.0075 + i * (PAPER[1] + 0.003), z, turn),\n          next,\n        ),\n      )\n    }\n    return out\n  }, [seed, n, inner, foot])\n\n  return (\n    <RigidBody type=\"fixed\" colliders={false} position={position} rotation={rotation}>\n      <CuboidCollider\n        args={[(W / 2) * u, (H / 2) * u, ((FRONT - top + 0.02) / 2) * u]}\n        position={[0, (H / 2) * u, ((FRONT + top - 0.02) / 2) * u]}\n      />\n      <group scale={u}>\n        {[W / 2, -W / 2 + SIDE].map((x) => (\n          <mesh\n            key={x}\n            material={wood}\n            geometry={side}\n            castShadow\n            receiveShadow\n            position={[x, 0, 0]}\n          />\n        ))}\n        <mesh material={wood} receiveShadow position={[0, FOOT, (foot + FRONT) / 2]}>\n          <boxGeometry args={[inner, 0.015, DEPTH]} />\n        </mesh>\n        <mesh material={wood} castShadow position={[0, (FOOT + 0.09) / 2, FRONT - 0.0075]}>\n          <boxGeometry args={[inner, 0.09 - FOOT, 0.015]} />\n        </mesh>\n        <group position={[0, 0, foot]} rotation={[-LEAN, 0, 0]}>\n          <mesh material={wood} castShadow receiveShadow position={[0, back / 2, -0.006]}>\n            <boxGeometry args={[inner, back, 0.012]} />\n          </mesh>\n          {shelves.map((s) => (\n            <group key={s} position={[0, s, 0]}>\n              <mesh material={wood} receiveShadow position={[0, -0.0075, 0.04]}>\n                <boxGeometry args={[inner, 0.015, 0.08]} />\n              </mesh>\n              <mesh material={wood} castShadow position={[0, 0.0075, 0.074]}>\n                <boxGeometry args={[inner, 0.045, 0.012]} />\n              </mesh>\n            </group>\n          ))}\n        </group>\n        <Sheets sheets={sheets} />\n      </group>\n    </RigidBody>\n  )\n}\n\nMagazineRack.groundSitting = true\n",
      "type": "registry:component"
    }
  ]
}
