{
  "name": "piano",
  "title": "Piano",
  "type": "registry:component",
  "description": "A grand piano: a lacquered case on three legs, the lid propped open, eighty-eight keys, a pedal lyre and a bench a Person can sit on. One cuboid collider round the case.",
  "dependencies": [
    "@react-three/rapier@^2.2.0",
    "@runek/core@^0.18.0",
    "three@^0.184.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "Piano.tsx",
      "content": "import { CuboidCollider, RigidBody } from '@react-three/rapier'\nimport { useMaterials, useWorld, type WorldComponentProps } from '@runek/core'\nimport { useEffect, useLayoutEffect, useMemo, useRef } from 'react'\nimport { ExtrudeGeometry, type InstancedMesh, Object3D, Shape } from 'three'\n\nexport interface PianoProps extends WorldComponentProps {\n  /** How far the lid is propped open, in radians; 0 shuts it. */\n  lid?: number\n  /** A bench in front of the keys. */\n  bench?: boolean\n  /** The case. Black lacquer unless given. */\n  color?: string\n}\n\nconst WIDTH = 1.5\nconst LENGTH = 1.9\nconst RIM = 0.3\nconst RIM_BOTTOM = 0.62\nconst WHITE_KEYS = 52\nconst KEYBOARD = 1.22\nconst KEY_W = KEYBOARD / WHITE_KEYS\n\n/** The case seen from above: a straight bass side, a tail, and the bent treble side curving\n *  back to the keys. y runs back from the keyboard. */\nfunction outline(): Shape {\n  const w = WIDTH / 2\n  const s = new Shape()\n  s.moveTo(-w, 0)\n  s.lineTo(w, 0)\n  s.lineTo(w, 0.45)\n  s.bezierCurveTo(w, 0.95, -0.05, 0.85, -0.12, LENGTH - 0.15)\n  s.quadraticCurveTo(-0.2, LENGTH, -w + 0.25, LENGTH)\n  s.quadraticCurveTo(-w, LENGTH, -w, LENGTH - 0.3)\n  s.lineTo(-w, 0)\n  return s\n}\n\n// Black keys sit between white ones: A0 has one, then C to B repeats with none after E and B.\nconst BLACK_KEYS = Array.from({ length: WHITE_KEYS - 1 }, (_, i) => i)\n  .filter((i) => {\n    const note = (i + 5) % 7\n    return note !== 2 && note !== 6\n  })\n  .map((i) => -KEYBOARD / 2 + KEY_W * (i + 1))\n\nconst LEGS: [number, number][] = [\n  [-(WIDTH / 2 - 0.1), -0.1],\n  [WIDTH / 2 - 0.1, -0.1],\n  [-0.3, -(LENGTH - 0.2)],\n]\n\n/**\n * A grand piano standing on its origin with the keyboard toward local +Z: a lacquered case on\n * three legs, the lid propped open on the bass side's hinge, a music desk, eighty-eight keys\n * and a pedal lyre, and a bench in front where a player sits facing -Z. One cuboid collider\n * round the case; the bench has none, so a `Person` can sit on it.\n */\nexport function Piano({\n  position = [0, 0, 0],\n  rotation = [0, 0, 0],\n  lid = 0.6,\n  bench = true,\n  color = '#0e0e10',\n}: PianoProps) {\n  const { unit } = useWorld()\n  const m = useMaterials({\n    lacquer: { color, roughness: 0.18, metalness: 0.15 },\n    brass: { slot: 'metal.bare', color: '#c9a24a', roughness: 0.3, metalness: 0.8 },\n    ivory: { color: '#f4f1e8', roughness: 0.35 },\n    ebony: { color: '#141414', roughness: 0.3 },\n  })\n  const { body, top } = useMemo(() => {\n    const shape = outline()\n    const extrude = (depth: number) =>\n      new ExtrudeGeometry(shape, { depth, bevelEnabled: false, curveSegments: 24 })\n    return { body: extrude(RIM), top: extrude(0.02) }\n  }, [])\n  useEffect(\n    () => () => {\n      body.dispose()\n      top.dispose()\n    },\n    [body, top],\n  )\n\n  const keys = useRef<InstancedMesh>(null)\n  useLayoutEffect(() => {\n    const mesh = keys.current\n    if (!mesh) return\n    const dummy = new Object3D()\n    BLACK_KEYS.forEach((x, i) => {\n      dummy.position.set(x * unit, 0, 0)\n      dummy.updateMatrix()\n      mesh.setMatrixAt(i, dummy.matrix)\n    })\n    mesh.instanceMatrix.needsUpdate = true\n  }, [unit])\n\n  const keyY = 0.72 * unit\n  // The prop stick stands on the treble rim and meets the lid's underside near its edge.\n  const prop = useMemo(() => {\n    if (lid <= 0.05) return null\n    const rim = RIM_BOTTOM + RIM\n    const [x0, y0] = [WIDTH / 2 - 0.2, rim]\n    const [x1, y1] = [-WIDTH / 2 + 1.3 * Math.cos(lid), rim + 1.3 * Math.sin(lid)]\n    return {\n      x: (x0 + x1) / 2,\n      y: (y0 + y1) / 2,\n      length: Math.hypot(x1 - x0, y1 - y0),\n      tilt: Math.atan2(x0 - x1, y1 - y0),\n    }\n  }, [lid])\n\n  return (\n    <group position={position} rotation={rotation}>\n      <RigidBody type=\"fixed\" colliders={false}>\n        <CuboidCollider\n          args={[(WIDTH / 2) * unit, 0.5 * unit, (LENGTH / 2) * unit]}\n          position={[0, 0.5 * unit, -(LENGTH / 2) * unit]}\n        />\n      </RigidBody>\n      <mesh\n        geometry={body}\n        material={m.lacquer}\n        castShadow\n        receiveShadow\n        position={[0, RIM_BOTTOM * unit, 0]}\n        rotation={[-Math.PI / 2, 0, 0]}\n        scale={unit}\n      />\n      <group position={[-(WIDTH / 2) * unit, (RIM_BOTTOM + RIM) * unit, 0]} rotation={[0, 0, lid]}>\n        <mesh\n          geometry={top}\n          material={m.lacquer}\n          castShadow\n          position={[(WIDTH / 2) * unit, 0, 0]}\n          rotation={[-Math.PI / 2, 0, 0]}\n          scale={unit}\n        />\n      </group>\n      {prop && (\n        <mesh\n          material={m.lacquer}\n          position={[prop.x * unit, prop.y * unit, -0.75 * unit]}\n          rotation={[0, 0, prop.tilt]}\n        >\n          <cylinderGeometry args={[0.012 * unit, 0.012 * unit, prop.length * unit, 6]} />\n        </mesh>\n      )}\n      {LEGS.map(([x, z]) => (\n        <mesh\n          key={`${x}${z}`}\n          material={m.lacquer}\n          castShadow\n          position={[x * unit, (RIM_BOTTOM / 2) * unit, z * unit]}\n        >\n          <cylinderGeometry args={[0.05 * unit, 0.035 * unit, RIM_BOTTOM * unit, 10]} />\n        </mesh>\n      ))}\n      <mesh material={m.lacquer} position={[0, 0.12 * unit, -0.2 * unit]}>\n        <boxGeometry args={[0.14 * unit, 0.24 * unit, 0.05 * unit]} />\n      </mesh>\n      {[-0.04, 0, 0.04].map((x) => (\n        <mesh key={x} material={m.brass} position={[x * unit, 0.03 * unit, -0.14 * unit]}>\n          <boxGeometry args={[0.025 * unit, 0.01 * unit, 0.08 * unit]} />\n        </mesh>\n      ))}\n      <mesh material={m.lacquer} castShadow position={[0, keyY - 0.05 * unit, 0.13 * unit]}>\n        <boxGeometry args={[1.4 * unit, 0.1 * unit, 0.3 * unit]} />\n      </mesh>\n      <mesh material={m.ivory} position={[0, keyY + 0.012 * unit, 0.16 * unit]}>\n        <boxGeometry args={[KEYBOARD * unit, 0.024 * unit, 0.15 * unit]} />\n      </mesh>\n      <instancedMesh\n        ref={keys}\n        args={[undefined, undefined, BLACK_KEYS.length]}\n        material={m.ebony}\n        position={[0, keyY + 0.032 * unit, 0.12 * unit]}\n      >\n        <boxGeometry args={[KEY_W * 0.6 * unit, 0.02 * unit, 0.09 * unit]} />\n      </instancedMesh>\n      {[-1, 1].map((s) => (\n        <mesh\n          key={s}\n          material={m.lacquer}\n          castShadow\n          position={[s * 0.66 * unit, keyY + 0.03 * unit, 0.14 * unit]}\n        >\n          <boxGeometry args={[0.08 * unit, 0.1 * unit, 0.3 * unit]} />\n        </mesh>\n      ))}\n      <mesh\n        material={m.lacquer}\n        castShadow\n        position={[0, (RIM_BOTTOM + RIM + 0.14) * unit, -0.1 * unit]}\n        rotation={[-0.25, 0, 0]}\n      >\n        <boxGeometry args={[0.7 * unit, 0.3 * unit, 0.02 * unit]} />\n      </mesh>\n      {bench && (\n        <group position={[0, 0, 0.75 * unit]}>\n          <mesh material={m.lacquer} castShadow receiveShadow position={[0, 0.47 * unit, 0]}>\n            <boxGeometry args={[0.8 * unit, 0.07 * unit, 0.36 * unit]} />\n          </mesh>\n          {[-1, 1].flatMap((sx) =>\n            [-1, 1].map((sz) => (\n              <mesh\n                key={`${sx}${sz}`}\n                material={m.lacquer}\n                castShadow\n                position={[sx * 0.34 * unit, 0.22 * unit, sz * 0.14 * unit]}\n              >\n                <boxGeometry args={[0.04 * unit, 0.44 * unit, 0.04 * unit]} />\n              </mesh>\n            )),\n          )}\n        </group>\n      )}\n    </group>\n  )\n}\n\nPiano.groundSitting = true\n",
      "type": "registry:component"
    }
  ]
}
