{
  "name": "newspapers",
  "title": "Newspapers",
  "type": "registry:component",
  "description": "The day's papers on a table: a pile of folded newspapers with headlines, photos and columns, and magazines fanned beside it, as one instanced mesh. Seeded covers. Decorative.",
  "dependencies": [
    "@runek/core@^0.18.0",
    "three@^0.184.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "Newspapers.tsx",
      "content": "import {\n  pick,\n  type Rng,\n  range,\n  rng,\n  useMaterial,\n  useWorld,\n  type WorldComponentProps,\n} from '@runek/core'\nimport { useLayoutEffect, useMemo, useRef } from 'react'\nimport { Color, Euler, type InstancedMesh, Matrix4, Quaternion, Vector3 } from 'three'\n\nexport interface NewspapersProps extends WorldComponentProps {\n  /** Folded newspapers in the pile, 0 to 4. */\n  papers?: number\n  /** Magazines fanned beside it, 0 to 4. */\n  magazines?: number\n}\n\n/** Magazine covers, shared with `MagazineRack`. */\nexport const COVERS = [\n  '#d1495b',\n  '#edae49',\n  '#00798c',\n  '#30638e',\n  '#3c8d5a',\n  '#8e4585',\n  '#1b1b1d',\n  '#f2efe8',\n]\nconst NEWSPRINT = '#ece8dc'\nconst INK = '#26262a'\nconst COLUMN = '#aca89d'\nconst PHOTO = '#77736b'\n\n/** One box of print: a matrix that places and sizes a unit cube, and its colour. */\nexport interface Sheet {\n  matrix: Matrix4\n  color: string\n}\n\n/** Sizes in meters, [width, thickness, depth]. */\nexport const PAPER: [number, number, number] = [0.29, 0.012, 0.38]\nexport const MAGAZINE: [number, number, number] = [0.21, 0.006, 0.28]\n\nconst LIFT = 0.0006\n\n/** A box `w` by `h` by `d` sitting on `at`'s XZ plane, centred at (x, z) in its frame. */\nfunction box(at: Matrix4, x: number, y: number, z: number, w: number, h: number, d: number) {\n  return at\n    .clone()\n    .multiply(\n      new Matrix4().compose(new Vector3(x, y + h / 2, z), new Quaternion(), new Vector3(w, h, d)),\n    )\n}\n\n/**\n * A folded newspaper lying on the plane of `at`, in meters, its masthead toward -Z: newsprint\n * with a headline bar, a photo and columns of type printed on top.\n */\nexport function newspaper(at: Matrix4, next: Rng): Sheet[] {\n  const [w, t, d] = PAPER\n  const top = t + LIFT\n  const out: Sheet[] = [{ matrix: box(at, 0, 0, 0, w, t, d), color: NEWSPRINT }]\n  out.push({ matrix: box(at, 0, top, -d / 2 + 0.035, w * 0.86, 0.001, 0.03), color: INK })\n  const cols = 4\n  const colW = (w * 0.86) / cols\n  const photo = Math.floor(next() * (cols - 1))\n  for (let i = 0; i < cols; i++) {\n    const x = -w * 0.43 + colW * (i + 0.5)\n    if (i === photo || i === photo + 1) continue\n    out.push({ matrix: box(at, x, top, 0.03, colW * 0.8, 0.001, d * 0.66), color: COLUMN })\n  }\n  const px = -w * 0.43 + colW * (photo + 1)\n  out.push({ matrix: box(at, px, top, -0.02, colW * 1.85, 0.001, d * 0.3), color: PHOTO })\n  out.push({ matrix: box(at, px, top, 0.12, colW * 1.85, 0.001, d * 0.24), color: COLUMN })\n  return out\n}\n\n/**\n * A magazine lying on the plane of `at`, in meters, its masthead toward -Z: a seeded cover with\n * a masthead band, a cover picture and a cover line.\n */\nexport function magazine(at: Matrix4, next: Rng): Sheet[] {\n  const [w, t, d] = MAGAZINE\n  const top = t + LIFT\n  const cover = pick(next, COVERS)\n  const light = cover === '#f2efe8' || cover === '#edae49'\n  const picture = pick(\n    next,\n    COVERS.filter((c) => c !== cover),\n  )\n  return [\n    { matrix: box(at, 0, 0, 0, w, t, d), color: cover },\n    {\n      matrix: box(at, 0, top, -d / 2 + 0.028, w * 0.9, 0.001, 0.04),\n      color: light ? INK : '#f7f4ee',\n    },\n    { matrix: box(at, 0.015, top, 0.02, w * 0.66, 0.001, d * 0.46), color: picture },\n    {\n      matrix: box(at, -0.03, top, d / 2 - 0.035, w * 0.6, 0.001, 0.016),\n      color: light ? INK : '#f7f4ee',\n    },\n  ]\n}\n\n/** A frame at (x, y, z) in meters, scaled to world units, turned by `turn`. */\nexport function frame(u: number, x: number, y: number, z: number, turn: Euler = new Euler()) {\n  return new Matrix4().compose(\n    new Vector3(x * u, y * u, z * u),\n    new Quaternion().setFromEuler(turn),\n    new Vector3(u, u, u),\n  )\n}\n\n/** Paper sheets drawn as one instanced box, each in its own colour: one material, one draw.\n *  They cast no shadow: paper this thin only shadows itself. */\nexport function Sheets({ sheets }: { sheets: Sheet[] }) {\n  const ref = useRef<InstancedMesh>(null)\n  const material = useMaterial({ color: '#ffffff', roughness: 0.8 })\n\n  useLayoutEffect(() => {\n    const mesh = ref.current\n    if (!mesh) return\n    const c = new Color()\n    sheets.forEach((s, i) => {\n      mesh.setMatrixAt(i, s.matrix)\n      mesh.setColorAt(i, c.set(s.color))\n    })\n    mesh.instanceMatrix.needsUpdate = true\n    if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true\n    mesh.computeBoundingSphere()\n  }, [sheets])\n\n  if (!sheets.length) return null\n  return (\n    <instancedMesh\n      key={sheets.length}\n      ref={ref}\n      args={[undefined, undefined, sheets.length]}\n      material={material}\n      receiveShadow\n    >\n      <boxGeometry args={[1, 1, 1]} />\n    </instancedMesh>\n  )\n}\n\n/**\n * The day's papers and a few magazines left on a coffee table or a counter: a pile of folded\n * newspapers, each with a headline bar, a photo and columns, and magazines fanned out beside\n * it. Its origin is on the top it lies on. Seeded: the covers and how squarely each one lies.\n * Drawn as one instanced mesh. Decorative: no collider.\n */\nexport function Newspapers({\n  position = [0, 0, 0],\n  rotation = [0, 0, 0],\n  seed = 1,\n  papers = 2,\n  magazines = 2,\n}: NewspapersProps) {\n  const { unit: u } = useWorld()\n  const nPapers = Math.max(0, Math.min(4, Math.round(papers)))\n  const nMagazines = Math.max(0, Math.min(4, Math.round(magazines)))\n\n  const sheets = useMemo(() => {\n    const next = rng(seed)\n    const out: Sheet[] = []\n    const px = nMagazines ? -0.13 : 0\n    let y = 0\n    for (let i = 0; i < nPapers; i++) {\n      const turn = new Euler(0, range(next, -0.12, 0.12), 0)\n      out.push(\n        ...newspaper(\n          frame(u, px + range(next, -0.015, 0.015), y, range(next, -0.015, 0.015), turn),\n          next,\n        ),\n      )\n      y += PAPER[1] + 0.0025\n    }\n    const mx = nPapers ? 0.17 : 0\n    y = 0\n    for (let i = 0; i < nMagazines; i++) {\n      const turn = new Euler(0, -0.25 + i * 0.28 + range(next, -0.06, 0.06), 0)\n      out.push(...magazine(frame(u, mx + i * 0.025, y, i * 0.02, turn), next))\n      y += MAGAZINE[1] + 0.0025\n    }\n    return out\n  }, [seed, nPapers, nMagazines, u])\n\n  return (\n    <group position={position} rotation={rotation}>\n      <Sheets sheets={sheets} />\n    </group>\n  )\n}\n",
      "type": "registry:component"
    }
  ]
}
