{
  "name": "speaker",
  "title": "Speaker",
  "type": "registry:component",
  "description": "A loudspeaker facing +Z: a studio monitor on a stand, a floor PA cabinet with two woofers under a horn, or a guitar amp combo with grille cloth and knobs. Seeded kind and finish. One cuboid collider.",
  "dependencies": [
    "@react-three/rapier@^2.2.0",
    "@runek/core@^0.18.0",
    "three@^0.184.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "Speaker.tsx",
      "content": "import { CuboidCollider, RigidBody } from '@react-three/rapier'\nimport { pick, rng, useMaterials, useWorld, type Vec3, type WorldComponentProps } from '@runek/core'\nimport { DoubleSide, type Material } from 'three'\n\n/** A studio `monitor` on a stand, a floor `pa` cabinet, or a guitar `amp` combo. */\nexport type SpeakerKind = 'monitor' | 'pa' | 'amp'\n\nexport interface SpeakerProps extends WorldComponentProps {\n  /** Seeded when unset. */\n  kind?: SpeakerKind\n  /** The cabinet. Seeded from a few finishes for its kind when unset. */\n  color?: string\n}\n\nconst KINDS: SpeakerKind[] = ['monitor', 'pa', 'amp']\nconst FINISHES: Record<SpeakerKind, string[]> = {\n  monitor: ['#1b1b1d', '#1b1b1d', '#efece6', '#3a3d42'],\n  pa: ['#1b1b1d', '#26272a'],\n  amp: ['#151515', '#151515', '#e8dcc2', '#7a1f1f', '#2f4858'],\n}\n// Cabinet [width, height, depth] in meters, and the stand a monitor sits on.\nconst SIZES: Record<SpeakerKind, Vec3> = {\n  monitor: [0.22, 0.34, 0.27],\n  pa: [0.56, 1.05, 0.45],\n  amp: [0.62, 0.5, 0.26],\n}\nconst STAND = 0.95\n\n/** A cone driver on a cabinet's front face, `r` its radius in meters, facing +Z from `p`: a dark\n *  surround, the cone and a dust cap, each standing a hair proud of the last. */\nfunction Driver({\n  r,\n  p,\n  u,\n  m,\n}: {\n  r: number\n  p: Vec3\n  u: number\n  m: Record<'dark' | 'cone' | 'chrome', Material>\n}) {\n  return (\n    <group position={p} rotation={[Math.PI / 2, 0, 0]} scale={u}>\n      <mesh material={m.dark} position={[0, 0.002, 0]}>\n        <cylinderGeometry args={[r, r, 0.004, 28]} />\n      </mesh>\n      <mesh material={m.cone} position={[0, 0.007, 0]}>\n        <cylinderGeometry args={[r * 0.3, r * 0.86, 0.006, 28, 1, true]} />\n      </mesh>\n      <mesh material={m.chrome} position={[0, 0.008, 0]} scale={[1, 0.4, 1]}>\n        <sphereGeometry args={[r * 0.3, 14, 8, 0, Math.PI * 2, 0, Math.PI / 2]} />\n      </mesh>\n    </group>\n  )\n}\n\n/**\n * A loudspeaker facing local +Z, in three kinds: a studio `monitor` at ear height on a stand,\n * a woofer under a dome tweeter; a floor `pa` cabinet with two woofers under a horn and steel\n * corners; or a guitar `amp` combo with grille cloth, a strip of knobs and a handle. Seeded:\n * the kind and the finish. One cuboid collider round it.\n */\nexport function Speaker({\n  position = [0, 0, 0],\n  rotation = [0, 0, 0],\n  seed = 1,\n  kind,\n  color,\n}: SpeakerProps) {\n  const { unit: u } = useWorld()\n  const next = rng(seed)\n  const type = kind ?? pick(next, KINDS)\n  const finish = color ?? pick(next, FINISHES[type])\n  const light = finish === '#e8dcc2' || finish === '#efece6'\n  const m = useMaterials({\n    cabinet: { color: finish, roughness: type === 'amp' ? 0.85 : 0.6 },\n    dark: { color: '#1d1d1f', roughness: 0.5 },\n    horn: { color: '#1d1d1f', roughness: 0.5, side: DoubleSide },\n    cone: { color: '#1b1b1b', roughness: 0.7 },\n    chrome: { slot: 'metal.bare', color: '#c9ccd1', roughness: 0.3, metalness: 0.8 },\n    cloth: { color: light ? '#b9a27a' : '#3a3632', roughness: 1 },\n  })\n  const [w, h, d] = SIZES[type]\n  const base = type === 'monitor' ? STAND : type === 'pa' ? 0.03 : 0\n  const at = (x: number, y: number): Vec3 => [x * u, (base + y) * u, (d / 2) * u]\n  const total = base + h\n\n  return (\n    <group position={position} rotation={rotation}>\n      <RigidBody type=\"fixed\" colliders={false}>\n        <CuboidCollider\n          args={[(Math.max(w, 0.3) / 2) * u, (total / 2) * u, (Math.max(d, 0.3) / 2) * u]}\n          position={[0, (total / 2) * u, 0]}\n        />\n      </RigidBody>\n      <mesh material={m.cabinet} castShadow receiveShadow position={[0, (base + h / 2) * u, 0]}>\n        <boxGeometry args={[w * u, h * u, d * u]} />\n      </mesh>\n\n      {type === 'monitor' && (\n        <>\n          <mesh material={m.dark} receiveShadow position={[0, 0.015 * u, 0]}>\n            <boxGeometry args={[0.3 * u, 0.03 * u, 0.3 * u]} />\n          </mesh>\n          <mesh material={m.dark} castShadow position={[0, (STAND / 2) * u, 0]}>\n            <boxGeometry args={[0.07 * u, STAND * u, 0.07 * u]} />\n          </mesh>\n          <mesh material={m.dark} position={[0, (STAND - 0.01) * u, 0]}>\n            <boxGeometry args={[0.2 * u, 0.02 * u, 0.22 * u]} />\n          </mesh>\n          <Driver r={0.075} p={at(0, 0.12)} u={u} m={m} />\n          <Driver r={0.025} p={at(0, 0.27)} u={u} m={m} />\n        </>\n      )}\n\n      {type === 'pa' && (\n        <>\n          <Driver r={0.19} p={at(0, 0.21)} u={u} m={m} />\n          <Driver r={0.19} p={at(0, 0.58)} u={u} m={m} />\n          <group position={[0, (base + 0.91) * u, (d / 2 + 0.03) * u]} scale={[1.5, 1, 1]}>\n            <mesh material={m.horn} rotation={[Math.PI / 2, Math.PI / 4, 0]}>\n              <cylinderGeometry args={[0.16 * u, 0.03 * u, 0.06 * u, 4, 1, true]} />\n            </mesh>\n          </group>\n          {[-1, 1].flatMap((sx) =>\n            [0, h].map((y) => (\n              <mesh\n                key={`${sx}${y}`}\n                material={m.chrome}\n                position={[sx * (w / 2 - 0.02) * u, (base + y) * u, (d / 2 - 0.02) * u]}\n              >\n                <boxGeometry args={[0.05 * u, 0.05 * u, 0.05 * u]} />\n              </mesh>\n            )),\n          )}\n          {[-1, 1].map((sx) => (\n            <mesh key={sx} material={m.dark} position={[sx * (w / 2 - 0.08) * u, 0.015 * u, 0]}>\n              <boxGeometry args={[0.08 * u, 0.03 * u, (d - 0.06) * u]} />\n            </mesh>\n          ))}\n        </>\n      )}\n\n      {type === 'amp' && (\n        <>\n          <mesh material={m.cloth} position={[0, 0.2 * u, (d / 2 + 0.002) * u]}>\n            <boxGeometry args={[(w - 0.06) * u, 0.34 * u, 0.004 * u]} />\n          </mesh>\n          <mesh material={m.chrome} position={[0, 0.43 * u, (d / 2 + 0.002) * u]}>\n            <boxGeometry args={[(w - 0.06) * u, 0.07 * u, 0.004 * u]} />\n          </mesh>\n          {Array.from({ length: 7 }, (_, i) => (\n            <mesh\n              key={i}\n              material={m.dark}\n              position={[(-0.21 + i * 0.06) * u, 0.43 * u, (d / 2 + 0.012) * u]}\n              rotation={[Math.PI / 2, 0, 0]}\n            >\n              <cylinderGeometry args={[0.013 * u, 0.015 * u, 0.018 * u, 12]} />\n            </mesh>\n          ))}\n          <mesh material={m.dark} castShadow position={[0, (h + 0.025) * u, 0]}>\n            <boxGeometry args={[0.2 * u, 0.025 * u, 0.035 * u]} />\n          </mesh>\n        </>\n      )}\n    </group>\n  )\n}\n\nSpeaker.groundSitting = true\n",
      "type": "registry:component"
    }
  ]
}
