{
  "name": "kitchen-units",
  "title": "KitchenUnits",
  "type": "registry:component",
  "description": "A fitted kitchen run: seeded door and drawer fronts under one worktop, an optional sink and a hob over an oven, wall units with a hood, and an overhang for an island. One cuboid collider.",
  "dependencies": [
    "@react-three/rapier@^2.2.0",
    "@runek/core@^0.18.0",
    "three@^0.184.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "KitchenUnits.tsx",
      "content": "import { CuboidCollider, RigidBody } from '@react-three/rapier'\nimport { pick, rng, sub, useMaterials, useWorld, type WorldComponentProps } from '@runek/core'\nimport { useMemo } from 'react'\nimport type { Material } from 'three'\n\nexport interface KitchenUnitsProps extends WorldComponentProps {\n  /** Length along local X, in units, rounded to whole units. */\n  length?: number\n  /** One unit's width, in units. */\n  unitWidth?: number\n  /** Worktop height, in units. */\n  height?: number\n  /** Depth along local Z, in units. The fronts face +Z. */\n  depth?: number\n  /** Which unit, counted from the -X end, holds the sink. */\n  sink?: number\n  /** Which unit holds the hob, with the oven under it and the hood over it. */\n  hob?: number\n  /** Wall units over the run, with a hood over the hob. */\n  wall?: boolean\n  /** How far the worktop reaches past the back, in units: knee room for stools along an\n   *  island. */\n  overhang?: number\n  /** Fronts. Seeded from a few kitchen finishes when unset. */\n  color?: string\n  /** The worktop. */\n  topColor?: string\n  /** Handles, sink, tap and hood. */\n  metalColor?: string\n}\n\nconst FINISHES = ['#2f3e46', '#e9e5dc', '#4a6b5d', '#1f2a36', '#8c6e54']\nconst KICK = 0.1\nconst TOP = 0.04\n// The wall units' bottom and top over the floor, and their depth.\nconst WALL_FROM = 1.5\nconst WALL_TO = 2.3\nconst WALL_D = 0.35\nconst BURNERS: [number, number, number][] = [\n  [-1, -1, 0.075],\n  [1, -1, 0.06],\n  [-1, 1, 0.06],\n  [1, 1, 0.075],\n]\n\ntype Front = 'door' | 'doors' | 'drawers'\n\n/**\n * A fitted kitchen run: base units on a kickboard under one worktop, lying along local X on\n * their origin and fronted toward +Z, each a door, a pair of doors or a stack of drawers with\n * bar handles. One unit can hold a sink with a tap, and one a hob over an oven. With `wall`,\n * units hang over the run and a steel hood takes their place over the hob. An `overhang` past\n * the back makes an island to sit at. Seeded: the finish and which fronts are drawers. One\n * cuboid collider round the base.\n */\nexport function KitchenUnits({\n  position = [0, 0, 0],\n  rotation = [0, 0, 0],\n  seed = 1,\n  length = 3,\n  unitWidth = 0.6,\n  height = 0.9,\n  depth = 0.6,\n  sink,\n  hob,\n  wall = false,\n  overhang = 0,\n  color,\n  topColor = '#d9d5cc',\n  metalColor = '#b9bdc1',\n}: KitchenUnitsProps) {\n  const { unit } = useWorld()\n  const finish = color ?? pick(rng(seed), FINISHES)\n  const m = useMaterials({\n    kick: { color: '#3a3a3c', roughness: 0.8 },\n    finish: { color: finish, roughness: 0.55 },\n    top: { color: topColor, roughness: 0.35 },\n    steel: { slot: 'metal.bare', color: metalColor, roughness: 0.25, metalness: 0.8 },\n    glass: { slot: 'glass', color: '#141416', roughness: 0.12, metalness: 0.3 },\n    iron: { color: '#3a3a3e', roughness: 0.4, metalness: 0.4 },\n  })\n  const count = Math.max(1, Math.round(length / unitWidth))\n  const U = unitWidth * unit\n  const L = count * U\n  const H = height * unit\n  const D = depth * unit\n  const kick = KICK * unit\n  const top = TOP * unit\n  const body = H - top - kick\n  const gap = 0.004 * unit\n  const wallH = (WALL_TO - WALL_FROM) * unit\n\n  const fronts = useMemo<Front[]>(() => {\n    const next = rng(sub(seed, 2))\n    return Array.from({ length: count }, (_, i) =>\n      i === sink ? 'doors' : i === hob ? 'drawers' : next() < 0.4 ? 'drawers' : 'door',\n    )\n  }, [seed, count, sink, hob])\n\n  const xOf = (i: number) => -L / 2 + U * (i + 0.5)\n  const panel = { face: m.finish, metal: m.steel, unit }\n\n  return (\n    <RigidBody type=\"fixed\" colliders={false} position={position} rotation={rotation}>\n      <CuboidCollider args={[L / 2, H / 2, D / 2]} position={[0, H / 2, 0]} />\n      <mesh material={m.kick} receiveShadow position={[0, kick / 2, -0.03 * unit]}>\n        <boxGeometry args={[L, kick, D - 0.06 * unit]} />\n      </mesh>\n      {/* The carcass in the finish too, so an island's back is panelled like its front. */}\n      <mesh\n        material={m.finish}\n        castShadow\n        receiveShadow\n        position={[0, kick + body / 2, -0.01 * unit]}\n      >\n        <boxGeometry args={[L, body, D - 0.02 * unit]} />\n      </mesh>\n      <mesh\n        material={m.top}\n        castShadow\n        receiveShadow\n        position={[0, H - top / 2, (0.01 - overhang / 2) * unit]}\n      >\n        <boxGeometry args={[L + 0.02 * unit, top, D + (0.02 + overhang) * unit]} />\n      </mesh>\n\n      {fronts.map((front, i) => {\n        const x = xOf(i)\n        const z = D / 2 - 0.005 * unit\n        const w = U - gap * 2\n        if (i === hob) {\n          const drawer = body * 0.22\n          const ovenH = body - drawer\n          return (\n            <group key={x}>\n              <FrontPanel\n                {...panel}\n                x={x}\n                y={H - top - drawer / 2 - gap}\n                z={z}\n                w={w}\n                h={drawer - gap * 2}\n              />\n              <group position={[x, kick + ovenH / 2, z + 0.006 * unit]}>\n                <mesh material={m.iron}>\n                  <boxGeometry args={[w, ovenH - gap * 2, 0.012 * unit]} />\n                </mesh>\n                <mesh material={m.glass} position={[0, -0.02 * unit, 0.007 * unit]}>\n                  <planeGeometry args={[w * 0.72, ovenH * 0.45]} />\n                </mesh>\n                <mesh\n                  material={m.steel}\n                  position={[0, ovenH * 0.36, 0.03 * unit]}\n                  rotation={[0, 0, Math.PI / 2]}\n                >\n                  <cylinderGeometry args={[0.01 * unit, 0.01 * unit, w * 0.7, 10]} />\n                </mesh>\n              </group>\n            </group>\n          )\n        }\n        if (front === 'drawers') {\n          let y = H - top\n          return (\n            <group key={x}>\n              {[0.24, 0.34, 0.42].map((share) => {\n                const h = body * share\n                y -= h\n                return (\n                  <FrontPanel\n                    key={share}\n                    {...panel}\n                    x={x}\n                    y={y + h / 2}\n                    z={z}\n                    w={w}\n                    h={h - gap * 2}\n                  />\n                )\n              })}\n            </group>\n          )\n        }\n        if (front === 'doors') {\n          return (\n            <group key={x}>\n              {[-1, 1].map((s) => (\n                <FrontPanel\n                  key={s}\n                  {...panel}\n                  x={x + (s * U) / 4}\n                  y={kick + body / 2}\n                  z={z}\n                  w={U / 2 - gap * 2}\n                  h={body - gap * 2}\n                  handle=\"side\"\n                  side={-s}\n                />\n              ))}\n            </group>\n          )\n        }\n        return (\n          <FrontPanel key={x} {...panel} x={x} y={kick + body / 2} z={z} w={w} h={body - gap * 2} />\n        )\n      })}\n\n      {sink !== undefined && sink < count && (\n        <group position={[xOf(sink), H, 0.02 * unit]}>\n          <mesh material={m.steel} receiveShadow position={[0, 0.002 * unit, 0]}>\n            <boxGeometry args={[U * 0.82, 0.004 * unit, D * 0.66]} />\n          </mesh>\n          <mesh material={m.iron} position={[0, 0.0045 * unit, 0]} rotation={[-Math.PI / 2, 0, 0]}>\n            <planeGeometry args={[U * 0.7, D * 0.52]} />\n          </mesh>\n          <group position={[0, 0, -D * 0.4]}>\n            <mesh material={m.steel} position={[0, 0.15 * unit, 0]}>\n              <cylinderGeometry args={[0.015 * unit, 0.02 * unit, 0.3 * unit, 12]} />\n            </mesh>\n            <mesh\n              material={m.steel}\n              position={[0, 0.3 * unit, 0.09 * unit]}\n              rotation={[Math.PI / 2, 0, 0]}\n            >\n              <cylinderGeometry args={[0.012 * unit, 0.012 * unit, 0.19 * unit, 10]} />\n            </mesh>\n          </group>\n        </group>\n      )}\n\n      {hob !== undefined && hob < count && (\n        <group position={[xOf(hob), H + 0.003 * unit, 0.02 * unit]}>\n          <mesh material={m.glass} receiveShadow>\n            <boxGeometry args={[U * 0.9, 0.006 * unit, D * 0.84]} />\n          </mesh>\n          {BURNERS.map(([sx, sz, r]) => (\n            <mesh\n              key={`${sx}${sz}`}\n              material={m.iron}\n              position={[sx * U * 0.22, 0.004 * unit, sz * 0.12 * unit]}\n              rotation={[-Math.PI / 2, 0, 0]}\n            >\n              <ringGeometry args={[r * unit * 0.8, r * unit, 32]} />\n            </mesh>\n          ))}\n        </group>\n      )}\n\n      {wall &&\n        fronts.map((_, i) => {\n          const x = xOf(i)\n          const z = -D / 2 + (WALL_D / 2) * unit\n          if (i === hob) {\n            return (\n              <group key={`wall${x}`} position={[x, WALL_FROM * unit, z]}>\n                <mesh material={m.steel} castShadow position={[0, 0.06 * unit, 0.04 * unit]}>\n                  <boxGeometry args={[U * 0.98, 0.12 * unit, WALL_D * 1.25 * unit]} />\n                </mesh>\n                <mesh\n                  material={m.steel}\n                  castShadow\n                  position={[0, wallH / 2 + 0.06 * unit, -0.02 * unit]}\n                >\n                  <boxGeometry args={[U * 0.4, wallH - 0.12 * unit, WALL_D * 0.6 * unit]} />\n                </mesh>\n              </group>\n            )\n          }\n          return (\n            <group key={`wall${x}`}>\n              <mesh\n                material={m.finish}\n                castShadow\n                receiveShadow\n                position={[x, WALL_FROM * unit + wallH / 2, z - 0.01 * unit]}\n              >\n                <boxGeometry args={[U, wallH, WALL_D * unit - 0.02 * unit]} />\n              </mesh>\n              <FrontPanel\n                {...panel}\n                x={x}\n                y={WALL_FROM * unit + wallH / 2}\n                z={z + (WALL_D / 2) * unit - 0.01 * unit}\n                w={U - gap * 2}\n                h={wallH - gap * 2}\n                handle=\"bottom\"\n              />\n            </group>\n          )\n        })}\n    </RigidBody>\n  )\n}\n\n/** One door or drawer front, with its bar handle along the top, the bottom or a side. */\nfunction FrontPanel({\n  x,\n  y,\n  z,\n  w,\n  h,\n  face,\n  metal,\n  unit,\n  handle = 'top',\n  side = 1,\n}: {\n  x: number\n  y: number\n  z: number\n  w: number\n  h: number\n  face: Material\n  metal: Material\n  unit: number\n  handle?: 'top' | 'bottom' | 'side'\n  side?: number\n}) {\n  const bar = 0.008 * unit\n  const vertical = handle === 'side'\n  const length = vertical ? Math.min(h * 0.3, 0.2 * unit) : Math.min(w * 0.5, 0.22 * unit)\n  const at: [number, number, number] = vertical\n    ? [side * (w / 2 - 0.04 * unit), h * 0.3, 0.015 * unit]\n    : [0, (handle === 'top' ? 1 : -1) * (h / 2 - 0.04 * unit), 0.015 * unit]\n  return (\n    <group position={[x, y, z]}>\n      <mesh material={face} receiveShadow>\n        <boxGeometry args={[w, h, 0.018 * unit]} />\n      </mesh>\n      <mesh material={metal} position={at} rotation={vertical ? [0, 0, 0] : [0, 0, Math.PI / 2]}>\n        <cylinderGeometry args={[bar, bar, length, 8]} />\n      </mesh>\n    </group>\n  )\n}\n\nKitchenUnits.groundSitting = true\n",
      "type": "registry:component"
    }
  ]
}
