Prompt specification
Build or customize the Three.js 3D scene "Shared kitchen resource planner" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Shared kitchen resource planner
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/shared-kitchen-resource-planner-x0144
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts reusable meal containers. Create a cooperative kitchen with finite ingredients, reusable boxes, and meal requests; users assemble portions while inventory counters decrement and unused ingredients remain visibly available. Low-poly.
Working Three.js Starter (shared-kitchen-resource-planner-x0144):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Common Table — Cooperative Kitchen</title>
<style>
:root {
--ink: #18332f;
--muted: #61746f;
--paper: rgba(255, 252, 244, 0.93);
--line: rgba(24, 51, 47, 0.14);
--green: #287d68;
--green-dark: #18594c;
--orange: #e8954d;
--shadow: 0 16px 40px rgba(36, 55, 48, 0.16);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background: #dbe5df;
}
button { font: inherit; }
#stage, #stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#stage canvas { touch-action: none; }
.topbar {
position: fixed;
z-index: 10;
top: 18px;
left: 20px;
right: 20px;
display: flex;
justify-content: space-between;
align-items: center;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
padding: 10px 15px 10px 11px;
border: 1px solid rgba(255,255,255,.55);
border-radius: 17px;
background: rgba(255, 252, 244, .89);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.brand-mark {
width: 42px;
height: 42px;
display: grid;
place-items: center;
border-radius: 13px;
background: var(--green);
color: white;
font-weight: 900;
letter-spacing: -2px;
box-shadow: inset 0 -3px 0 rgba(0,0,0,.12);
}
.brand h1 {
margin: 0;
font-size: 17px;
line-height: 1.05;
letter-spacing: -.02em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: .08em;
text-transform: uppercase;
}
.top-actions {
display: flex;
gap: 9px;
pointer-events: auto;
}
.pill-button {
border: 1px solid rgba(255,255,255,.65);
border-radius: 14px;
padding: 10px 13px;
color: var(--ink);
background: rgba(255, 252, 244, .91);
box-shadow: 0 8px 24px rgba(36, 55, 48, .11);
cursor: pointer;
font-size: 12px;
font-weight: 800;
backdrop-filter: blur(12px);
}
.pill-button:hover { transform: translateY(-1px); }
.panel {
position: fixed;
z-index: 8;
border: 1px solid rgba(255,255,255,.6);
border-radius: 20px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.panel-heading h2 {
margin: 0;
font-size: 13px;
letter-spacing: .08em;
text-transform: uppercase;
}
.panel-heading span {
color: var(--muted);
font-size: 11px;
font-weight: 700;
}
#requests-panel {
left: 20px;
top: 94px;
width: 292px;
max-height: calc(100vh - 286px);
padding: 15px;
overflow: auto;
scrollbar-width: thin;
}
.request-list { display: grid; gap: 8px; }
.request-card {
width: 100%;
display: grid;
grid-template-columns: 34px 1fr auto;
gap: 9px;
align-items: center;
padding: 10px;
text-align: left;
color: var(--ink);
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255,255,255,.58);
cursor: pointer;
transition: .18s ease;
}
.request-card:hover {
transform: translateY(-1px);
border-color: rgba(40,125,104,.45);
}
.request-card.selected {
border-color: var(--green);
background: #edf6ef;
box-shadow: inset 3px 0 0 var(--green);
}
.request-card.complete {
opacity: .66;
background: rgba(232, 239, 231, .8);
cursor: default;
}
.avatar {
width: 34px;
height: 34px;
display: grid;
place-items: center;
border-radius: 11px;
color: white;
font-size: 12px;
font-weight: 900;
box-shadow: inset 0 -3px 0 rgba(0,0,0,.1);
}
.request-title {
font-size: 12px;
font-weight: 850;
line-height: 1.15;
}
.request-owner {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.ingredient-chips {
display: flex;
flex-wrap: wrap;
gap: 4px;
margin-top: 6px;
}
.chip {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 3px 6px;
border-radius: 999px;
background: rgba(24,51,47,.07);
font-size: 9px;
font-weight: 800;
color: #46605a;
}
.chip.done {
color: var(--green-dark);
background: rgba(40,125,104,.15);
}
.dot {
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--dot, #ccc);
box-shadow: inset 0 -1px 0 rgba(0,0,0,.12);
}
.check {
width: 20px;
height: 20px;
display: grid;
place-items: center;
border-radius: 50%;
background: rgba(24,51,47,.08);
color: transparent;
font-size: 12px;
font-weight: 900;
}
.complete .check {
color: white;
background: var(--green);
}
#inventory-panel {
right: 20px;
top: 94px;
width: 254px;
padding: 15px;
}
.inventory-row {
display: grid;
grid-template-columns: 25px 1fr auto;
align-items: center;
gap: 9px;
padding: 8px 0;
border-bottom: 1px solid rgba(24,51,47,.07);
}
.inventory-row:last-child { border-bottom: 0; }
.food-icon {
width: 24px;
height: 24px;
display: grid;
place-items: center;
border-radius: 8px;
background: var(--food);
box-shadow: inset 0 -3px 0 rgba(0,0,0,.09);
}
.food-icon::after {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: rgba(255,255,255,.62);
}
.stock-name {
font-size: 11px;
font-weight: 800;
}
.stock-bar {
height: 3px;
margin-top: 5px;
overflow: hidden;
border-radius: 4px;
background: rgba(24,51,47,.08);
}
.stock-bar i {
display: block;
height: 100%;
border-radius: inherit;
background: var(--food);
transition: width .25s ease;
}
.stock-number {
min-width: 28px;
text-align: right;
font-size: 13px;
font-weight: 900;
}
.stock-number.empty { color: #bd5b4f; }
.box-stock {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 12px;
padding: 10px;
border-radius: 12px;
background: rgba(40,125,104,.1);
color: var(--green-dark);
font-size: 10px;
font-weight: 800;
}
#prep-panel {
left: 50%;
bottom: 20px;
width: min(460px, calc(100vw - 40px));
transform: translateX(-50%);
padding: 13px;
}
.prep-main {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 14px;
align-items: center;
}
.prep-title {
margin: 0;
font-size: 14px;
font-weight: 900;
}
.prep-subtitle {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.portion-row {
display: flex;
flex-wrap: wrap;
gap: 5px;
margin-top: 9px;
}
.portion-chip {
display: inline-flex;
align-items: center;
gap: 5px;
min-height: 25px;
padding: 4px 8px;
border-radius: 9px;
background: rgba(24,51,47,.07);
font-size: 10px;
font-weight: 800;
}
.portion-chip.ready {
color: var(--green-dark);
background: rgba(40,125,104,.17);
}
.prep-actions {
display: flex;
gap: 7px;
}
.action-button {
min-width: 88px;
padding: 10px 12px;
border: 0;
border-radius: 11px;
background: var(--green);
color: white;
font-size: 11px;
font-weight: 850;
cursor: pointer;
box-shadow: inset 0 -3px 0 rgba(0,0,0,.12);
}
.action-button.secondary {
color: var(--ink);
background: rgba(24,51,47,.08);
box-shadow: none;
}
.action-button:disabled {
opacity: .38;
cursor: not-allowed;
}
.progress-track {
height: 6px;
margin-top: 11px;
overflow: hidden;
border-radius: 6px;
background: rgba(24,51,47,.08);
}
.progress-track i {
display: block;
height: 100%;
width: 0;
border-radius: inherit;
background: linear-gradient(90deg, var(--green), #7fba83);
transition: width .3s ease;
}
#controls-hint {
position: fixed;
z-index: 6;
left: 20px;
bottom: 20px;
padding: 9px 12px;
border-radius: 12px;
color: #49625c;
background: rgba(255,255,255,.72);
border: 1px solid rgba(255,255,255,.6);
font-size: 10px;
font-weight: 750;
backdrop-filter: blur(10px);
pointer-events: none;
}
#toast {
position: fixed;
z-index: 30;
left: 50%;
top: 82px;
max-width: min(430px, calc(100vw - 32px));
transform: translate(-50%, -12px);
padding: 11px 15px;
border-radius: 13px;
color: white;
background: #18332f;
box-shadow: var(--shadow);
opacity: 0;
pointer-events: none;
font-size: 12px;
font-weight: 750;
text-align: center;
transition: .2s ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.warning { background: #9c5d31; }
#hover-tip {
position: fixed;
z-index: 25;
padding: 7px 9px;
border-radius: 8px;
color: white;
background: rgba(24,51,47,.9);
font-size: 10px;
font-weight: 800;
pointer-events: none;
opacity: 0;
transform: translate(12px, 12px);
}
#complete-panel {
position: fixed;
z-index: 40;
left: 50%;
top: 50%;
width: min(390px, calc(100vw - 32px));
padding: 28px;
transform: translate(-50%, -48%) scale(.94);
border: 1px solid rgba(255,255,255,.75);
border-radius: 24px;
text-align: center;
background: rgba(255,252,244,.96);
box-shadow: 0 25px 80px rgba(24,51,47,.25);
opacity: 0;
pointer-events: none;
transition: .3s ease;
}
#complete-panel.show {
opacity: 1;
transform: translate(-50%, -50%) scale(1);
pointer-events: auto;
}
.complete-icon {
width: 58px;
height: 58px;
display: grid;
place-items: center;
margin: 0 auto 13px;
border-radius: 18px;
color: white;
background: var(--green);
font-size: 26px;
}
#complete-panel h2 {
margin: 0;
font-size: 24px;
letter-spacing: -.04em;
}
#complete-panel p {
margin: 9px auto 20px;
max-width: 300px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
@media (max-width: 900px) {
#requests-panel { width: 238px; }
#inventory-panel { width: 220px; }
#controls-hint { display: none; }
.brand p { display: none; }
.prep-actions { flex-direction: column; }
.action-button { min-width: 76px; }
}
@media (max-width: 680px) {
.topbar { top: 10px; left: 10px; right: 10px; }
.brand { padding: 7px 10px 7px 7px; }
.brand-mark { width: 35px; height: 35px; }
.brand h1 { font-size: 14px; }
.pill-button { padding: 9px 10px; font-size: 10px; }
#requests-panel {
left: 10px;
top: 66px;
width: 190px;
max-height: 35vh;
padding: 10px;
}
#inventory-panel {
right: 10px;
top: 66px;
width: 150px;
padding: 10px;
}
.inventory-row { grid-template-columns: 20px 1fr auto; gap: 5px; }
.food-icon { width: 20px; height: 20px; }
.stock-name { font-size: 9px; }
.stock-number { font-size: 11px; }
#prep-panel { bottom: 10px; width: calc(100vw - 20px); }
.request-card { grid-template-columns: 28px 1fr auto; padding: 8px; }
.avatar { width: 28px; height: 28px; }
.ingredient-chips { display: none; }
}
</style>
</head>
<body>
<div id="stage" aria-label="Interactive cooperative kitchen"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark">CT</div>
<div>
<h1>Common Table</h1>
<p>Cooperative kitchen · shared pantry</p>
</div>
</div>
<div class="top-actions">
<button class="pill-button" id="reset-button">Reset kitchen</button>
<button class="pill-button" id="camera-button">Reset view</button>
</div>
</header>
<section class="panel" id="requests-panel" aria-label="Meal requests">
<div class="panel-heading">
<h2>Meal requests</h2>
<span id="request-progress">0 / 6 shared</span>
</div>
<div class="request-list" id="request-list"></div>
</section>
<section class="panel" id="inventory-panel" aria-label="Finite ingredient inventory">
<div class="panel-heading">
<h2>Pantry</h2>
<span>finite stock</span>
</div>
<div id="inventory-list"></div>
<div class="box-stock">
<span>Reusable boxes</span>
<strong id="box-count">5 ready</strong>
</div>
</section>
<section class="panel" id="prep-panel" aria-label="Current portion">
<div class="prep-main">
<div>
<p class="prep-title" id="prep-title">Choose a meal request</p>
<div class="prep-subtitle" id="prep-subtitle">Select a request, then tap ingredient bins.</div>
<div class="portion-row" id="portion-row"></div>
</div>
<div class="prep-actions">
<button class="action-button secondary" id="undo-button">Return</button>
<button class="action-button" id="pack-button">Pack box</button>
</div>
</div>
<div class="progress-track"><i id="overall-progress"></i></div>
</section>
<div id="controls-hint">Drag to orbit · Shift-drag to pan · Scroll to zoom · Tap bins and boxes</div>
<div id="toast" role="status" aria-live="polite"></div>
<div id="hover-tip"></div>
<div id="complete-panel">
<div class="complete-icon">✓</div>
<h2>Every meal shared.</h2>
<p id="complete-copy"></p>
<button class="action-button" id="play-again">Prepare another round</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbe5df);
scene.fog = new THREE.Fog(0xdbe5df, 24, 43);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
stage.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const controls = {
target: new THREE.Vector3(-0.15, 1.0, -0.65),
theta: 0.60,
phi: 0.88,
zoom: 9.1,
minZoom: 5.3,
maxZoom: 14
};
const hemi = new THREE.HemisphereLight(0xfff8df, 0x719486, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.0);
sun.position.set(7, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0007;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc5e6ed, 1.1);
fill.position.set(-8, 6, 3);
scene.add(fill);
const palette = {
floorA: 0xd6cbb8,
floorB: 0xe0d5c3,
wall: 0xeee6d7,
wallTrim: 0x7f9f8f,
cabinet: 0x668f7d,
cabinetDark: 0x446b5e,
counter: 0xf3e6ce,
wood: 0xb77e4c,
woodLight: 0xd3a26c,
metal: 0x8ca3a0,
teal: 0x2b8b78,
box: 0xf0c66c,
boxDark: 0xd59d4a,
orange: 0xe8954d,
dark: 0x24443e
};
const ingredientDefs = {
rice: {
name: "Rice", color: 0xf3ead6, css: "#f3ead6",
initial: 8, shape: "rice"
},
greens: {
name: "Greens", color: 0x68a74b, css: "#68a74b",
initial: 5, shape: "greens"
},
chicken: {
name: "Chicken", color: 0xdca36b, css: "#dca36b",
initial: 4, shape: "chicken"
},
beans: {
name: "Beans", color: 0x8d563d, css: "#8d563d",
initial: 4, shape: "beans"
},
tomato: {
name: "Tomato", color: 0xdf5947, css: "#df5947",
initial: 5, shape: "tomato"
},
sauce: {
name: "Sauce", color: 0xa83d35, css: "#a83d35",
initial: 3, shape: "sauce"
}
};
const requestTemplates = [
{
title: "Sunrise bowl", owner: "Maya", initial: "M", color: "#df8357",
meal: { rice: 1, greens: 1, tomato: 1 }
},
{
title: "Market plate", owner: "Leo", initial: "L", color: "#4b8a78",
meal: { rice: 1, chicken: 1, greens: 1 }
},
{
title: "Garden curry", owner: "Ari", initial: "A", color: "#8b6ba8",
meal: { rice: 1, beans: 1, tomato: 1, sauce: 1 }
},
{
title: "Power bowl", owner: "Noor", initial: "N", color: "#d0a036",
meal: { rice: 1, chicken: 1, beans: 1, tomato: 1, sauce: 1 }
},
{
title: "Sunrise bowl", owner: "Sam", initial: "S", color: "#4d8ca4",
meal: { rice: 1, greens: 1, tomato: 1 }
},
{
title: "Market plate", owner: "Inez", initial: "I", color: "#b66c74",
meal: { rice: 1, chicken: 1, greens: 1 }
}
];
let stock = {};
let requests = [];
let selectedRequestId = null;
let portion = {};
let portionMeshes = [];
let toastTimer;
let completedCount = 0;
const ingredientStations = [];
const clickables = [];
const people = [];
const reusableBoxes = [];
const rackSlots = [];
function mat(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...extra
});
}
function boxMesh(w, h, d, material, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderMesh(rTop, rBottom, h, material, x = 0, y = 0, z = 0, parent = scene, sides = 8) {
const mesh = new THREE.Mesh(new THREE.CylinderGeometry(rTop, rBottom, h, sides), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addEdges(mesh, color = 0x3f5b52, opacity = 0.22) {
const edges = new THREE.EdgesGeometry(mesh.geometry, 25);
const lines = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({ color, transparent: true, opacity })
);
mesh.add(lines);
}
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeLabel(text, width = 2, bg = "#24443e", fg = "#ffffff") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * 0.25, 1);
sprite.renderOrder = 20;
sprite.userData.texture = texture;
sprite.userData.canvas = canvas;
sprite.userData.context = ctx;
sprite.userData.bg = bg;
sprite.userData.fg = fg;
setLabelText(sprite, text, bg, fg);
return sprite;
}
function setLabelText(sprite, text, bg = sprite.userData.bg, fg = sprite.userData.fg) {
const canvas = sprite.userData.canvas;
const ctx = sprite.userData.context;
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 6, 6, 500, 116, 22);
ctx.fillStyle = bg;
ctx.fill();
ctx.fillStyle = fg;
ctx.font = "800 44px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 65, 455);
sprite.userData.texture.needsUpdate = true;
sprite.userData.bg = bg;
sprite.userData.fg = fg;
}
function buildRoom() {
const floorMatA = mat(palette.floorA);
const floorMatB = mat(palette.floorB);
const wallMat = mat(palette.wall);
const trimMat = mat(palette.wallTrim);
boxMesh(18.5, 0.22, 12.4, mat(0xb8aa94), 0, -0.17, 0);
for (let x = 0; x < 12; x++) {
for (let z = 0; z < 8; z++) {
const tile = boxMesh(
1.46, 0.035, 1.46,
(x + z) % 2 ? floorMatA : floorMatB,
-8.13 + x * 1.48,
0,
-5.32 + z * 1.48
);
tile.castShadow = false;
}
}
boxMesh(18.2, 5.2, 0.24, wallMat, 0, 2.48, -5.98);
boxMesh(0.24, 5.2, 12.2, wallMat, -9.08, 2.48, 0);
boxMesh(18.2, 0.20, 0.16, trimMat, 0, 0.25, -5.82);
boxMesh(0.16, 0.20, 12.1, trimMat, -8.92, 0.25, 0);
boxMesh(18.1, 0.12, 0.16, trimMat, 0, 4.75, -5.82);
// Window with low-poly sky and muntins.
const sky = new THREE.Mesh(
new THREE.PlaneGeometry(3.2, 1.9),
new THREE.MeshBasicMaterial({ color: 0xabd1d2 })
);
sky.position.set(-5.7, 3.35, -5.82);
scene.add(sky);
const frameMat = mat(0x6d8f80);
boxMesh(3.45, 0.12, 0.12, frameMat, -5.7, 4.34, -5.72);
boxMesh(3.45, 0.12, 0.12, frameMat, -5.7, 2.36, -5.72);
boxMesh(0.12, 2.05, 0.12, frameMat, -7.3, 3.35, -5.72);
boxMesh(0.12, 2.05, 0.12, frameMat, -4.1, 3.35, -5.72);
boxMesh(0.10, 1.9, 0.10, frameMat, -5.7, 3.35, -5.69);
// Wall request board.
const board = boxMesh(5.8, 1.7, 0.16, mat(0x315a50), -1.3, 3.25, -5.79);
addEdges(board, 0x183b33, 0.5);
const boardTitle = makeLabel("TODAY'S MEAL REQUESTS", 4.2, "#315a50", "#fff9e8");
boardTitle.position.set(-1.3, 3.82, -5.64);
scene.add(boardTitle);
const cardColors = [0xe89b58, 0x5d9a82, 0x8e74a7, 0xd3aa47, 0x5790a4, 0xbb7380];
for (let i = 0; i < 6; i++) {
const card = boxMesh(0.68, 0.72, 0.035, mat(cardColors[i]), -3.48 + i * 0.87, 3.12, -5.66);
const line1 = boxMesh(0.42, 0.07, 0.02, mat(0xf8edcf), 0, 0.15, 0.03, card);
const line2 = boxMesh(0.30, 0.05, 0.02, mat(0xf8edcf), -0.04, 0.02, 0.03, card);
const line3 = boxMesh(0.34, 0.05, 0.02, mat(0xf8edcf), -0.02, -0.12, 0.03, card);
}
const sign = makeLabel("SHARE WHAT'S LEFT", 2.6, "#e8954d", "#17372f");
sign.position.set(3.45, 3.83, -5.62);
scene.add(sign);
// Wall shelf and jars.
boxMesh(3.1, 0.12, 0.6, mat(palette.wood), 3.45, 2.55, -5.4);
for (let i = 0; i < 5; i++) {
cylinderMesh(
0.16, 0.18, 0.42,
mat([0xe0b965, 0xb66c4c, 0x769c73, 0xd8d0ad, 0x8b7558][i]),
2.35 + i * 0.53, 2.82, -5.38
);
}
// Hanging pendant lights.
for (const x of [-3.8, 0.2, 4.2]) {
cylinderMesh(0.025, 0.025, 1.45, mat(0x485b53), x, 4.05, -0.6, scene, 6);
const shade = new THREE.Mesh(
new THREE.ConeGeometry(0.48, 0.46, 10, 1, true),
mat(0xd89955, { side: THREE.DoubleSide })
);
shade.position.set(x, 3.14, -0.6);
shade.castShadow = true;
scene.add(shade);
const bulb = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.13, 1),
new THREE.MeshBasicMaterial({ color: 0xffe9a8 })
);
bulb.position.set(x, 2.96, -0.6);
scene.add(bulb);
}
}
function buildIngredientCounter() {
const cabinet = mat(palette.cabinet);
const cabinetDark = mat(palette.cabinetDark);
const counter = mat(palette.counter);
boxMesh(11.25, 1.15, 1.35, cabinet, -1.8, 0.58, -4.45);
boxMesh(11.5, 0.16, 1.52, counter, -1.8, 1.24, -4.42);
for (let i = 0; i < 7; i++) {
const drawer = boxMesh(1.4, 0.77, 0.055, cabinetDark, -6.7 + i * 1.48, 0.66, -3.75);
boxMesh(0.34, 0.06, 0.08, mat(palette.woodLight), 0, 0.16, 0.06, drawer);
}
const ids = Object.keys(ingredientDefs);
const xPositions = [-6.45, -4.68, -2.91, -1.14, 0.63, 2.40];
ids.forEach((id, index) => {
const def = ingredientDefs[id];
const group = new THREE.Group();
group.position.set(xPositions[index], 0, -4.18);
scene.add(group);
group.userData.pick = { kind: "ingredient", id };
const bin = boxMesh(1.14, 0.28, 0.84, mat(0xf8eddb), 0, 1.45, 0, group);
addEdges(bin, 0x846c4d, 0.22);
boxMesh(0.96, 0.08, 0.65, mat(0x7e6651), 0, 1.61, 0, group);
const pieces = [];
for (let i = 0; i < def.initial; i++) {
const piece = makeIngredientPiece(id);
piece.position.set(
((i % 3) - 1) * 0.25 + (i % 2) * 0.055,
1.70 + (i % 2) * 0.045,
(Math.floor(i / 3) - 0.5) * 0.19
);
piece.rotation.set(i * 0.31, i * 0.47, i * 0.19);
piece.castShadow = true;
group.add(piece);
pieces.push(piece);
}
const label = makeLabel(`${def.name.toUpperCase()} ${def.initial}`, 1.35, "#24443e", "#fff9e8");
label.position.set(0, 1.33, 0.47);
group.add(label);
ingredientStations.push({ id, group, pieces, label });
clickables.push(group);
});
const pantryLabel = makeLabel("SHARED PANTRY", 2.2, "#e8954d", "#17372f");
pantryLabel.position.set(-4.7, 2.25, -4.25);
scene.add(pantryLabel);
}
function makeIngredientPiece(id) {
const def = ingredientDefs[id];
let geometry;
const material = mat(def.color);
if (def.shape === "rice") {
geometry = new THREE.SphereGeometry(0.13, 7, 5);
const mesh = new THREE.Mesh(geometry, material);
mesh.scale.set(1.28, 0.55, 1);
return mesh;
}
if (def.shape === "greens") {
geometry = new THREE.DodecahedronGeometry(0.15, 0);
} else if (def.shape === "chicken") {
geometry = new THREE.BoxGeometry(0.22, 0.13, 0.17);
} else if (def.shape === "beans") {
geometry = new THREE.IcosahedronGeometry(0.12, 0);
} else if (def.shape === "tomato") {
geometry = new THREE.DodecahedronGeometry(0.135, 0);
} else {
geometry = new THREE.CylinderGeometry(0.085, 0.095, 0.18, 6);
}
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function buildPrepIsland() {
const base = boxMesh(4.35, 1.18, 1.75, mat(palette.cabinet), -0.45, 0.61, 0.92);
addEdges(base, 0x35584d, 0.28);
boxMesh(4.55, 0.17, 1.92, mat(palette.counter), -0.45, 1.28, 0.92);
boxMesh(3.35, 0.08, 1.15, mat(0xc88e58), -0.45, 1.405, 0.93);
const label = makeLabel("SHARED PREP TABLE", 2.05, "#f0c66c", "#17372f");
label.position.set(-0.45, 1.02, 1.82);
scene.add(label);
// Cutting board detail and utensil cup.
const board = boxMesh(2.2, 0.035, 0.72, mat(0xe2b777), -0.92, 1.46, 0.83);
addEdges(board, 0x8a5f39, 0.25);
cylinderMesh(0.16, 0.14, 0.25, mat(0x577b70), 1.35, 1.48, 0.72, scene, 8);
cylinderMesh(0.025, 0.025, 0.7, mat(0xcaa06a), 1.32, 1.86, 0.72, scene, 6);
cylinderMesh(0.025, 0.025, 0.62, mat(0xa87850), 1.43, 1.82, 0.73, scene, 6);
}
function buildRack() {
const frame = mat(0x5d7b70);
const shelf = mat(0x809b8f);
boxMesh(2.7, 0.12, 1.55, frame, 6.0, 1.13, 0.78);
boxMesh(2.7, 0.12, 1.22, shelf, 6.0, 1.83, 0.78);
boxMesh(0.12, 2.05, 0.12, frame, 4.72, 1.52, 0.15);
boxMesh(0.12, 2.05, 0.12, frame, 7.28, 1.52, 0.15);
boxMesh(0.12, 2.05, 0.12, frame, 4.72, 1.52, 1.40);
boxMesh(0.12, 2.05, 0.12, frame, 7.28, 1.52, 1.40);
const positions = [
new THREE.Vector3(5.10, 1.20, 1.15),
new THREE.Vector3(6.00, 1.20, 1.15),
new THREE.Vector3(6.90, 1.20, 1.15),
new THREE.Vector3(5.48, 1.90, 0.73),
new THREE.Vector3(6.52, 1.90, 0.73)
];
positions.forEach((position, index) => {
rackSlots.push({ position: position.clone(), occupant: null });
const box = makeReusableBox(index);
box.position.copy(position);
box.userData.home = index;
box.userData.status = "ready";
rackSlots[index].occupant = box;
reusableBoxes.push(box);
clickables.push(box);
});
const label = makeLabel("REUSABLE BOXES", 2.2, "#2b8b78", "#fff9e8");
label.position.set(6.0, 2.78, 0.72);
scene.add(label);
const hint = makeLabel("CLICK A CLEAN BOX", 1.65, "#f0c66c", "#17372f");
hint.position.set(6.0, 0.70, 1.49);
scene.add(hint);
}
function makeReusableBox(index) {
const group = new THREE.Group();
group.userData.pick = { kind: "box", ref: group };
group.userData.index = index;
const bodyMat = mat(palette.box);
const darkMat = mat(palette.boxDark);
const tealMat = mat(palette.teal);
boxMesh(0.64, 0.10, 0.52, darkMat, 0, 0.05, 0, group);
boxMesh(0.64, 0.25, 0.08, bodyMat, 0, 0.18, -0.22, group);
boxMesh(0.64, 0.25, 0.08, bodyMat, 0, 0.18, 0.22, group);
boxMesh(0.08, 0.25, 0.36, bodyMat, -0.28, 0.18, 0, group);
boxMesh(0.08, 0.25, 0.36, bodyMat, 0.28, 0.18, 0, group);
const lid = boxMesh(0.68, 0.07, 0.56, tealMat, 0, 0.38, -0.31, group);
lid.rotation.x = -1.10;
const handle = boxMesh(0.27, 0.05, 0.05, mat(0x38685d), 0, 0.43, -0.46, group);
handle.rotation.x = -1.10;
group.userData.fill = null;
return group;
}
function buildWashAndPass() {
const base = boxMesh(3.15, 1.12, 1.25, mat(palette.cabinetDark), 6.15, 0.58, -4.42);
addEdges(base, 0x24443e, 0.25);
boxMesh(3.35, 0.16, 1.38, mat(0xd5ded5), 6.15, 1.22, -4.42);
const sink = boxMesh(1.38, 0.08, 0.84, mat(0x4e6e69), 6.38, 1.33, -4.42);
addEdges(sink, 0x24443e, 0.4);
boxMesh(1.08, 0.06, 0.57, mat(0x78938d), 6.38, 1.38, -4.42);
cylinderMesh(0.045, 0.045, 0.78, mat(0x9cafaa), 6.38, 1.68, -4.74, scene, 8);
const faucet = cylinderMesh(0.045, 0.045, 0.46, mat(0x9cafaa), 6.38, 1.98, -4.56, scene, 8);
faucet.rotation.x = Math.PI / 2;
const label = makeLabel("WASH & REUSE", 1.85, "#4e8b7e", "#fff9e8");
label.position.set(6.18, 2.06, -4.4);
scene.add(label);
// Small pass-through counter.
boxMesh(2.1, 1.02, 1.05, mat(palette.cabinet), 3.95, 0.54, -2.85);
boxMesh(2.25, 0.14, 1.18, mat(palette.counter), 3.95, 1.13, -2.85);
const passLabel = makeLabel("PASS", 1.2, "#e8954d", "#17372f");
passLabel.position.set(3.95, 1.43, -2.30);
scene.add(passLabel);
}
function buildDecor() {
// Low-poly plant.
cylinderMesh(0.32, 0.25, 0.46, mat(0xc47755), -7.8, 0.24, -4.9, scene, 7);
const stemMat = mat(0x527c45);
cylinderMesh(0.04, 0.05, 1.0, stemMat, -7.8, 0.92, -4.9, scene, 6);
const leafMat = mat(0x6f9d50);
for (let i = 0; i < 6; i++) {
const leaf = new THREE.Mesh(new THREE.DodecahedronGeometry(0.31, 0), leafMat);
const angle = i * 1.05;
leaf.position.set(
-7.8 + Math.cos(angle) * 0.28,
1.18 + (i % 2) * 0.16,
-4.9 + Math.sin(angle) * 0.25
);
leaf.scale.set(1.25, 0.55, 0.7);
leaf.rotation.y = angle;
leaf.castShadow = true;
scene.add(leaf);
}
// Wall clock.
const clock = cylinderMesh(0.35, 0.35, 0.08, mat(0xf7edda), 7.8, 3.65, -5.78, scene, 16);
clock.rotation.x = Math.PI / 2;
boxMesh(0.035, 0.23, 0.04, mat(0x24443e), 7.8, 3.72, -5.69);
boxMesh(0.18, 0.035, 0.04, mat(0x24443e), 7.86, 3.65, -5.68);
// Small community notice tiles.
for (let i = 0; i < 4; i++) {
const tile = boxMesh(
0.38, 0.30, 0.03,
mat([0xe4a160, 0x6da18e, 0xe0c969, 0x789db0][i]),
7.35 + (i % 2) * 0.47,
2.48 - Math.floor(i / 2) * 0.42,
-5.76
);
tile.rotation.z = (i % 2 ? 1 : -1) * 0.06;
}
}
function makePerson(x, z, rotation, shirt, apron, skin, hair, name) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
const shirtMat = mat(shirt);
const apronMat = mat(apron);
const skinMat = mat(skin);
const hairMat = mat(hair);
const pantsMat = mat(0x435b54);
cylinderMesh(0.13, 0.15, 0.68, pantsMat, -0.17, 0.36, 0, group, 6);
cylinderMesh(0.13, 0.15, 0.68, pantsMat, 0.17, 0.36, 0, group, 6);
boxMesh(0.18, 0.11, 0.34, mat(0x314a43), -0.17, 0.05, 0.07, group);
boxMesh(0.18, 0.11, 0.34, mat(0x314a43), 0.17, 0.05, 0.07, group);
cylinderMesh(0.34, 0.29, 0.68, shirtMat, 0, 1.02, 0, group, 7);
boxMesh(0.43, 0.53, 0.06, apronMat, 0, 0.92, 0.29, group);
const head = new THREE.Mesh(new THREE.IcosahedronGeometry(0.27, 1), skinMat);
head.position.set(0, 1.59, 0);
head.castShadow = true;
group.add(head);
const hairMesh = new THREE.Mesh(new THREE.DodecahedronGeometry(0.285, 0), hairMat);
hairMesh.position.set(0, 1.70, -0.035);
hairMesh.scale.set(1, 0.65, 1);
group.add(hairMesh);
const arms = [];
for (const side of [-1, 1]) {
const pivot = new THREE.Group();
pivot.position.set(side * 0.34, 1.25, 0);
group.add(pivot);
cylinderMesh(0.09, 0.08, 0.53, shirtMat, 0, -0.22, 0, pivot, 6);
const hand = new THREE.Mesh(new THREE.IcosahedronGeometry(0.10, 0), skinMat);
hand.position.y = -0.52;
pivot.add(hand);
arms.push(pivot);
}
const nameLabel = makeLabel(name, 1.12, "#f7edda", "#24443e");
nameLabel.position.set(0, 2.15, 0);
group.add(nameLabel);
people.push({ group, arms, phase: Math.random() * 6 });
return group;
}
function buildPeople() {
makePerson(-4.55, -1.55, 0.3, 0xe1aa6d, 0x3f7567, 0xb87955, 0x3b302b, "PRIYA");
makePerson(3.00, -0.05, -0.6, 0x6c9eb4, 0xe5b04f, 0x8c543d, 0x292724, "JONAH");
}
buildRoom();
buildIngredientCounter();
buildPrepIsland();
buildRack();
buildWashAndPass();
buildDecor();
buildPeople();
function resetState() {
stock = {};
for (const [id, def] of Object.entries(ingredientDefs)) stock[id] = def.initial;
requests = requestTemplates.map((request, index) => ({
...request,
id: `request-${index}`,
status: "active"
}));
selectedRequestId = requests[0].id;
portion = {};
completedCount = 0;
for (const mesh of portionMeshes) scene.remove(mesh);
portionMeshes = [];
reusableBoxes.forEach((box, index) => {
if (box.userData.fill) {
box.remove(box.userData.fill);
box.userData.fill = null;
}
box.userData.journey = null;
box.userData.status = "ready";
box.userData.home = index;
box.position.copy(rackSlots[index].position);
box.rotation.set(0, 0, 0);
box.scale.setScalar(1);
rackSlots[index].occupant = box;
});
document.querySelector("#complete-panel").classList.remove("show");
updateStockVisuals();
updateUI();
}
function selectedRequest() {
return requests.find(request => request.id === selectedRequestId);
}
function requestIsExact(request) {
if (!request) return false;
const keys = new Set([...Object.keys(request.meal), ...Object.keys(portion)]);
for (const key of keys) {
if ((portion[key] || 0) !== (request.meal[key] || 0)) return false;
}
return true;
}
function updateStockVisuals() {
for (const station of ingredientStations) {
const count = stock[station.id];
station.pieces.forEach((piece, index) => {
piece.visible = index < count;
});
const def = ingredientDefs[station.id];
const bg = count === 0 ? "#9c5d31" : "#24443e";
setLabelText(station.label, `${def.name.toUpperCase()} ${count}`, bg, "#fff9e8");
}
}
function showToast(message, warning = false) {
const toast = document.querySelector("#toast");
toast.textContent = message;
toast.classList.toggle("warning", warning);
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2600);
}
function chooseRequest(id) {
const request = requests.find(item => item.id === id);
if (!request || request.status === "complete") return;
if (Object.keys(portion).length > 0 && selectedRequestId !== id) {
showToast("Return the current ingredients before choosing another meal.", true);
return;
}
selectedRequestId = id;
updateUI();
}
function addIngredient(id) {
const request = selectedRequest();
if (!request || request.status === "complete") {
showToast("Choose an active meal request first.", true);
return;
}
if (!(id in request.meal)) {
showToast(`${ingredientDefs[id].name} is not part of ${request.title}.`, true);
return;
}
const needed = request.meal[id];
const alreadyAdded = portion[id] || 0;
if (alreadyAdded >= needed) {
showToast(`${request.title} only needs ${needed} ${ingredientDefs[id].name.toLowerCase()}.`, true);
return;
}
if (stock[id] <= 0) {
showToast(`${ingredientDefs[id].name} is finished. Try another pantry item.`, true);
return;
}
stock[id] -= 1;
portion[id] = alreadyAdded + 1;
const piece = makeIngredientPiece(id);
piece.scale.setScalar(0.75);
const total = portionMeshes.length;
piece.position.set(
-1.47 + (total % 4) * 0.60,
1.61,
0.68 + Math.floor(total / 4) * 0.34
);
piece.rotation.y = total * 0.72;
piece.userData.spawnTime = performance.now();
scene.add(piece);
portionMeshes.push(piece);
updateStockVisuals();
updateUI();
if (requestIsExact(request)) {
showToast("Portion complete — pack it in a clean reusable box.");
}
}
function clearPortion(returnToStock = true) {
if (returnToStock) {
for (const [id, count] of Object.entries(portion)) {
stock[id] += count;
}
}
for (const mesh of portionMeshes) scene.remove(mesh);
portionMeshes = [];
portion = {};
updateStockVisuals();
updateUI();
}
function findFreeSlot(box) {
return rackSlots.findIndex(slot => slot.occupant === null || slot.occupant === box);
}
function addBoxFill(box, request) {
if (box.userData.fill) box.remove(box.userData.fill);
const fill = new THREE.Group();
const ids = Object.keys(request.meal);
ids.forEach((id, index) => {
const piece = makeIngredientPiece(id);
piece.scale.setScalar(0.56);
piece.position.set((index - (ids.length - 1) / 2) * 0.16, 0.27, 0);
piece.rotation.y = index;
fill.add(piece);
});
box.add(fill);
box.userData.fill = fill;
}
function packPortion() {
const request = selectedRequest();
if (!request || request.status === "complete") {
showToast("Choose a meal request first.", true);
return;
}
if (!Object.keys(portion).length) {
showToast("Add ingredients from the pantry before packing.", true);
return;
}
if (!requestIsExact(request)) {
const missing = Object.entries(request.meal)
.filter(([id, count]) => (portion[id] || 0) < count)
.map(([id]) => ingredientDefs[id].name);
showToast(`Still needed: ${missing.join(", ")}.`, true);
return;
}
const box = reusableBoxes.find(item => item.userData.status === "ready");
if (!box) {
showToast("No clean box is ready yet. Wait for one to finish washing.", true);
return;
}
const home = box.userData.home;
if (home !== null && rackSlots[home]) rackSlots[home].occupant = null;
box.userData.home = null;
box.userData.status = "busy";
box.userData.journey = {
start: performance.now(),
from: box.position.clone(),
requestId: request.id
};
addBoxFill(box, request);
const requestMeal = { ...request.meal };
clearPortion(false);
request.status = "complete";
completedCount += 1;
const next = requests.find(item => item.status === "active");
if (next) selectedRequestId = next.id;
updateUI();
showToast(`${request.title} is packed for ${request.owner}. The box will return after washing.`);
if (completedCount === requests.length) {
const leftovers = Object.values(stock).reduce((sum, count) => sum + count, 0);
setTimeout(() => {
document.querySelector("#complete-copy").textContent =
`Six neighbors have meals, the pantry still has ${leftovers} ingredients, and every box is reusable.`;
document.querySelector("#complete-panel").classList.add("show");
}, 1400);
}
}
function renderRequests() {
const list = document.querySelector("#request-list");
list.innerHTML = "";
requests.forEach(request => {
const card = document.createElement("button");
card.className = "request-card";
card.classList.toggle("selected", request.id === selectedRequestId);
card.classList.toggle("complete", request.status === "complete");
const chips = Object.entries(request.meal).map(([id, count]) => {
const have = request.status === "complete" ? count : (portion[id] || 0);
const done = have >= count;
return `
<span class="chip ${done ? "done" : ""}">
<i class="dot" style="--dot:${ingredientDefs[id].css}"></i>
${ingredientDefs[id].name} ${have}/${count}
</span>
`;
}).join("");
card.innerHTML = `
<span class="avatar" style="background:${request.color}">${request.initial}</span>
<span>
<span class="request-title">${request.title}</span>
<span class="request-owner">for ${request.owner}</span>
<span class="ingredient-chips">${chips}</span>
</span>
<span class="check">✓</span>
`;
card.addEventListener("click", () => chooseRequest(request.id));
list.appendChild(card);
});
document.querySelector("#request-progress").textContent = `${completedCount} / ${requests.length} shared`;
}
function renderInventory() {
const list = document.querySelector("#inventory-list");
list.innerHTML = "";
for (const [id, def] of Object.entries(ingredientDefs)) {
const row = document.createElement("div");
row.className = "inventory-row";
const percent = (stock[id] / def.initial) * 100;
row.innerHTML = `
<span class="food-icon" style="--food:${def.css}"></span>
<span>
<span class="stock-name">${def.name}</span>
<span class="stock-bar"><i style="width:${percent}%"></i></span>
</span>
<span class="stock-number ${stock[id] === 0 ? "empty" : ""}">${stock[id]}</span>
`;
list.appendChild(row);
}
const ready = reusableBoxes.filter(box => box.userData.status === "ready").length;
const busy = reusableBoxes.length - ready;
document.querySelector("#box-count").textContent = `${ready} ready${busy ? ` · ${busy} in use` : ""}`;
}
function renderPrep() {
const request = selectedRequest();
const title = document.querySelector("#prep-title");
const subtitle = document.querySelector("#prep-subtitle");
const row = document.querySelector("#portion-row");
const undo = document.querySelector("#undo-button");
const pack = document.querySelector("#pack-button");
if (!request || request.status === "complete") {
title.textContent = completedCount === requests.length ? "Kitchen complete" : "Choose a meal request";
subtitle.textContent = completedCount === requests.length
? "All requests are complete."
: "Select a request card to begin.";
row.innerHTML = "";
undo.disabled = true;
pack.disabled = true;
return;
}
title.textContent = `${request.title} · for ${request.owner}`;
subtitle.textContent = Object.keys(portion).length
? "Ingredients on the shared prep table."
: "Tap the pantry bins shown in this recipe.";
row.innerHTML = Object.entries(request.meal).map(([id, count]) => {
const have = portion[id] || 0;
return `
<span class="portion-chip ${have === count ? "ready" : ""}">
<i class="dot" style="--dot:${ingredientDefs[id].css}"></i>
${ingredientDefs[id].name} ${have}/${count}
</span>
`;
}).join("");
const hasIngredients = Object.keys(portion).length > 0;
const exact = requestIsExact(request);
const hasBox = reusableBoxes.some(box => box.userData.status === "ready");
undo.disabled = !hasIngredients;
pack.disabled = !exact || !hasBox;
pack.textContent = exact ? "Pack box" : "Not ready";
}
function updateUI() {
renderRequests();
renderInventory();
renderPrep();
document.querySelector("#overall-progress").style.width =
`${(completedCount / requests.length) * 100}%`;
}
// Raycasting and scene interaction.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const hoverTip = document.querySelector("#hover-tip");
function pickFromPointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(clickables, true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.pick) object = object.parent;
if (object?.userData.pick) return object.userData.pick;
}
return null;
}
function handlePick(pick) {
if (!pick) return;
if (pick.kind === "ingredient") {
addIngredient(pick.id);
} else if (pick.kind === "box") {
const box = pick.ref;
if (box.userData.status === "ready") {
packPortion();
} else {
showToast("That box is in use and will return after washing.", true);
}
}
}
// Direct camera controls; no external controls module is used.
const pointerState = {
down: false,
id: null,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: 0,
pan: false
};
function updateCameraProjection() {
const aspect = window.innerWidth / window.innerHeight;
camera.left = -controls.zoom * aspect;
camera.right = controls.zoom * aspect;
camera.top = controls.zoom;
camera.bottom = -controls.zoom;
camera.updateProjectionMatrix();
}
function updateCamera() {
const radius = 18;
const horizontal = Math.sin(controls.phi) * radius;
camera.position.set(
controls.target.x + horizontal * Math.sin(controls.theta),
controls.target.y + Math.cos(controls.phi) * radius,
controls.target.z + horizontal * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
updateCameraProjection();
}
function resetCamera() {
controls.target.set(-0.15, 1.0, -0.65);
controls.theta = 0.60;
controls.phi = 0.88;
controls.zoom = 9.1;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", event => {
pointerState.down = true;
pointerState.id = event.pointerId;
pointerState.startX = event.clientX;
pointerState.startY = event.clientY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
pointerState.moved = 0;
pointerState.pan = event.button === 2 || event.shiftKey;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (pointerState.down && pointerState.id === event.pointerId) {
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
pointerState.moved += Math.abs(dx) + Math.abs(dy);
if (pointerState.pan || event.shiftKey) {
const right = new THREE.Vector3(Math.cos(controls.theta), 0, -Math.sin(controls.theta));
const forward = new THREE.Vector3(-Math.sin(controls.theta), 0, -Math.cos(controls.theta));
const scale = controls.zoom / Math.min(window.innerWidth, window.innerHeight);
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(forward, -dy * scale);
} else {
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(controls.phi + dy * 0.005, 0.46, 1.34);
}
updateCamera();
hoverTip.style.opacity = 0;
return;
}
const pick = pickFromPointer(event);
renderer.domElement.style.cursor = pick ? "pointer" : "grab";
if (pick) {
if (pick.kind === "ingredient") {
hoverTip.textContent = `Add ${ingredientDefs[pick.id].name.toLowerCase()}`;
} else {
const ready = pick.ref.userData.status === "ready";
hoverTip.textContent = ready ? "Pack a reusable box" : "Box is washing";
}
hoverTip.style.left = `${event.clientX}px`;
hoverTip.style.top = `${event.clientY}px`;
hoverTip.style.opacity = 1;
} else {
hoverTip.style.opacity = 0;
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (pointerState.down && pointerState.id === event.pointerId) {
if (pointerState.moved < 7) {
handlePick(pickFromPointer(event));
}
}
pointerState.down = false;
pointerState.id = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerState.down = false;
pointerState.id = null;
});
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom + event.deltaY * 0.008,
controls.minZoom,
controls.maxZoom
);
updateCamera();
}, { passive: false });
document.querySelector("#undo-button").addEventListener("click", () => {
if (Object.keys(portion).length) {
clearPortion(true);
showToast("Ingredients returned to the pantry.");
}
});
document.querySelector("#pack-button").addEventListener("click", packPortion);
document.querySelector("#reset-button").addEventListener("click", () => {
resetState();
showToast("The kitchen is ready for a fresh round.");
});
document.querySelector("#camera-button").addEventListener("click", resetCamera);
document.querySelector("#play-again").addEventListener("click", resetState);
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
});
function smooth01(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function updateBoxJourneys(now) {
const prepPoint = new THREE.Vector3(-0.45, 1.47, 0.92);
const passPoint = new THREE.Vector3(3.95, 1.18, -2.85);
const washPoint = new THREE.Vector3(6.38, 1.40, -4.42);
for (const box of reusableBoxes) {
const journey = box.userData.journey;
if (!journey) continue;
const t = (now - journey.start) / 1000;
let destination;
if (t < 0.9) {
destination = prepPoint.clone().lerp(journey.from, 1 - smooth01(t / 0.9));
} else if (t < 1.8) {
destination = prepPoint.clone().lerp(passPoint, smooth01((t - 0.9) / 0.9));
} else if (t < 3.7) {
destination = passPoint.clone();
} else if (t < 5.0) {
destination = passPoint.clone().lerp(washPoint, smooth01((t - 3.7) / 1.3));
} else if (t < 5.7) {
destination = washPoint.clone();
if (box.userData.fill) {
const washScale = 1 - smooth01((t - 5.0) / 0.7) * 0.96;
box.userData.fill.scale.setScalar(washScale);
}
} else {
if (box.userData.fill) {
box.remove(box.userData.fill);
box.userData.fill = null;
}
const slotIndex = findFreeSlot(box);
const slot = rackSlots[slotIndex];
destination = washPoint.clone().lerp(slot.position, smooth01((t - 5.7) / 1.1));
if (t >= 6.8) {
box.position.copy(slot.position);
box.rotation.set(0, 0, 0);
box.scale.setScalar(1);
box.userData.home = slotIndex;
box.userData.status = "ready";
box.userData.journey = null;
slot.occupant = box;
updateUI();
}
}
box.position.copy(destination);
if (t < 1.8) box.rotation.y = Math.sin(t * 8) * 0.08;
else box.rotation.y = 0;
}
}
function animate(now) {
requestAnimationFrame(animate);
for (const person of people) {
const wave = Math.sin(now * 0.0024 + person.phase);
person.arms[0].rotation.x = wave * 0.28 - 0.12;
person.arms[1].rotation.x = -wave * 0.28 - 0.12;
person.group.position.y = Math.sin(now * 0.0015 + person.phase) * 0.012;
}
for (const piece of portionMeshes) {
if (piece.userData.spawnTime) {
const age = (now - piece.userData.spawnTime) / 260;
if (age < 1) {
const scale = 0.75 * smooth01(age);
piece.scale.setScalar(scale);
piece.position.y = 1.61 + Math.sin(age * Math.PI) * 0.22;
} else {
piece.scale.setScalar(0.75);
piece.position.y = 1.61;
piece.userData.spawnTime = 0;
}
}
}
updateBoxJourneys(now);
renderer.render(scene, camera);
}
resetState();
resetCamera();
requestAnimationFrame(animate);
</script>
</body>
</html>
```