Prompt specification
Build or customize the Three.js 3D scene "Restock after purchases" 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: Restock after purchases
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/restock-after-purchases-x0534
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: restock after purchases. Animate customers removing front boxes and a worker refilling from a crate; both shelf and reserve counts update consistently for every visible transfer. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (restock-after-purchases-x0534):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Low-Poly Restock Simulator</title>
<style>
:root {
--ink: #17324d;
--muted: #63788c;
--paper: rgba(255, 255, 255, 0.9);
--shelf: #2677d8;
--reserve: #e38a27;
--cart: #13a38b;
--sold: #7458c7;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8ee;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.panel {
position: absolute;
top: 20px;
left: 20px;
width: min(390px, calc(100vw - 40px));
padding: 18px;
border: 1px solid rgba(23, 50, 77, 0.12);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 14px 35px rgba(35, 65, 90, 0.14);
backdrop-filter: blur(10px);
pointer-events: auto;
}
.eyebrow {
color: var(--shelf);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 4px 0 3px;
font-size: 25px;
line-height: 1.05;
letter-spacing: -0.04em;
}
.sub {
margin: 0 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.counts {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.count {
min-width: 0;
padding: 10px 8px 9px;
border-radius: 12px;
background: #f4f7fa;
border: 1px solid rgba(23, 50, 77, 0.07);
}
.count .name {
overflow: hidden;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.count .value {
margin-top: 2px;
font-size: 24px;
font-weight: 900;
line-height: 1;
}
.count.shelf .value { color: var(--shelf); }
.count.reserve .value { color: var(--reserve); }
.count.cart .value { color: var(--cart); }
.count.sold .value { color: var(--sold); }
.status {
display: flex;
align-items: center;
gap: 9px;
min-height: 37px;
margin-top: 13px;
padding: 9px 11px;
border-radius: 11px;
background: #edf5ff;
color: #315574;
font-size: 12px;
font-weight: 700;
}
.dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--shelf);
box-shadow: 0 0 0 4px rgba(38, 119, 216, 0.13);
animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse {
50% { transform: scale(0.75); opacity: 0.65; }
}
.ledger {
margin-top: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
text-align: center;
}
.controls {
position: absolute;
right: 20px;
top: 20px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
border: 1px solid rgba(23, 50, 77, 0.13);
border-radius: 11px;
padding: 10px 13px;
background: rgba(255, 255, 255, 0.91);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
box-shadow: 0 7px 18px rgba(35, 65, 90, 0.1);
}
button:hover { background: white; transform: translateY(-1px); }
button:active { transform: translateY(0); }
.hint {
position: absolute;
left: 20px;
bottom: 18px;
padding: 9px 12px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.82);
color: #587086;
font-size: 10px;
font-weight: 700;
backdrop-filter: blur(8px);
}
.world-tag {
position: absolute;
z-index: 3;
padding: 5px 9px;
border: 1px solid rgba(255,255,255,.7);
border-radius: 999px;
background: rgba(255,255,255,.86);
box-shadow: 0 5px 14px rgba(27,50,72,.12);
color: var(--ink);
font-size: 10px;
font-weight: 900;
letter-spacing: .04em;
pointer-events: none;
transform: translate(-50%, -100%);
white-space: nowrap;
}
.world-tag::after {
content: "";
position: absolute;
left: 50%;
bottom: -5px;
width: 8px;
height: 8px;
border-right: 1px solid rgba(255,255,255,.7);
border-bottom: 1px solid rgba(255,255,255,.7);
background: rgba(255,255,255,.86);
transform: translateX(-50%) rotate(45deg);
}
@media (max-width: 620px) {
.panel { top: 12px; left: 12px; width: calc(100vw - 24px); padding: 14px; }
h1 { font-size: 21px; }
.sub { margin-bottom: 10px; }
.count { padding: 8px 5px; }
.count .value { font-size: 20px; }
.controls { right: 12px; top: 195px; }
.hint { display: none; }
}
</style>
</head>
<body>
<div id="ui">
<section class="panel">
<div class="eyebrow">Warehouse / live operations</div>
<h1>Restock after purchases</h1>
<p class="sub">Customers take front-shelf boxes. The worker moves units from the reserve crate into every vacated slot.</p>
<div class="counts">
<div class="count shelf"><div class="name">Shelf</div><div class="value" id="shelfCount">0</div></div>
<div class="count reserve"><div class="name">Reserve</div><div class="value" id="reserveCount">0</div></div>
<div class="count cart"><div class="name">Carts</div><div class="value" id="cartCount">0</div></div>
<div class="count sold"><div class="name">Sold</div><div class="value" id="soldCount">0</div></div>
</div>
<div class="status"><span class="dot"></span><span id="statusText">Preparing warehouse…</span></div>
<div class="ledger" id="ledgerText">Shelf + reserve + carts + in transit + sold = 11</div>
</section>
<div class="controls">
<button id="replay">↻ Replay</button>
<button id="viewReset">Reset view</button>
</div>
<div class="hint">Drag to orbit · Scroll / pinch to zoom · Double-click to reset the camera</div>
<div class="world-tag" id="shelfTag">FRONT SHELF</div>
<div class="world-tag" id="reserveTag">RESERVE CRATE</div>
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8ee);
scene.fog = new THREE.Fog(0xdce8ee, 19, 34);
const camera = new THREE.PerspectiveCamera(42, innerWidth / innerHeight, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xeaf7ff, 0x8b7e69, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.1);
sun.position.set(-6, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const mats = {
floor: new THREE.MeshStandardMaterial({ color: 0xd7cbb8, roughness: 0.92, flatShading: true }),
floorEdge: new THREE.MeshStandardMaterial({ color: 0xb7aa98, roughness: 0.9, flatShading: true }),
wall: new THREE.MeshStandardMaterial({ color: 0xe9f0ef, roughness: 0.95, flatShading: true }),
wallTrim: new THREE.MeshStandardMaterial({ color: 0x7896a4, roughness: 0.8, flatShading: true }),
steel: new THREE.MeshStandardMaterial({ color: 0x486170, roughness: 0.6, metalness: 0.16, flatShading: true }),
steelLight: new THREE.MeshStandardMaterial({ color: 0x90a6ad, roughness: 0.58, metalness: 0.12, flatShading: true }),
wood: new THREE.MeshStandardMaterial({ color: 0xb77d49, roughness: 0.88, flatShading: true }),
woodLight: new THREE.MeshStandardMaterial({ color: 0xd7a365, roughness: 0.85, flatShading: true }),
rubber: new THREE.MeshStandardMaterial({ color: 0x263846, roughness: 0.85, flatShading: true }),
belt: new THREE.MeshStandardMaterial({ color: 0x3f5360, roughness: 0.84, flatShading: true }),
orange: new THREE.MeshStandardMaterial({ color: 0xee8e31, roughness: 0.75, flatShading: true }),
teal: new THREE.MeshStandardMaterial({ color: 0x18a58d, roughness: 0.72, flatShading: true }),
blue: new THREE.MeshStandardMaterial({ color: 0x287ed8, roughness: 0.68, flatShading: true }),
purple: new THREE.MeshStandardMaterial({ color: 0x7458c7, roughness: 0.72, flatShading: true }),
skin: new THREE.MeshStandardMaterial({ color: 0xd99b72, roughness: 0.9, flatShading: true }),
dark: new THREE.MeshStandardMaterial({ color: 0x253845, roughness: 0.9, flatShading: true }),
white: new THREE.MeshStandardMaterial({ color: 0xf6f1e8, roughness: 0.85, flatShading: true }),
yellow: new THREE.MeshStandardMaterial({ color: 0xf3c54b, roughness: 0.8, flatShading: true })
};
function mesh(geometry, material, x = 0, y = 0, z = 0, parent = scene) {
const m = new THREE.Mesh(geometry, material);
m.position.set(x, y, z);
m.castShadow = true;
m.receiveShadow = true;
parent.add(m);
return m;
}
function box(w, h, d, material, x = 0, y = 0, z = 0, parent = scene) {
return mesh(new THREE.BoxGeometry(w, h, d), material, x, y, z, parent);
}
// Warehouse shell.
box(22, 0.35, 16, mats.floor, 0, -0.2, 0);
box(22.4, 0.22, 16.4, mats.floorEdge, 0, -0.41, 0);
box(22, 7, 0.3, mats.wall, 0, 3.3, -5.1);
box(22.3, 0.22, 0.42, mats.wallTrim, 0, 0.12, -4.9);
box(22.3, 0.22, 0.42, mats.wallTrim, 0, 6.75, -4.9);
// Painted floor lanes.
const laneMat = new THREE.MeshStandardMaterial({ color: 0xe8ddc9, roughness: 1 });
for (let x = -8; x <= 8; x += 2.5) {
box(1.25, 0.012, 0.12, laneMat, x, -0.012, 3.25);
}
box(0.12, 0.014, 7.5, laneMat, -4.3, -0.01, 0.3);
// Low-poly wall sign.
box(4.1, 0.92, 0.16, mats.blue, 0.7, 4.6, -4.88);
box(0.12, 0.65, 0.03, mats.white, -0.8, 4.6, -4.78);
box(0.12, 0.65, 0.03, mats.white, -0.42, 4.6, -4.78);
box(1.35, 0.12, 0.03, mats.white, 0.8, 4.76, -4.78);
box(1.1, 0.12, 0.03, mats.white, 0.75, 4.48, -4.78);
// Shelf frame.
const shelf = new THREE.Group();
scene.add(shelf);
box(8.8, 0.18, 1.8, mats.steel, 0, 0.2, -2.65, shelf);
box(8.8, 0.18, 1.8, mats.steel, 0, 2.25, -2.65, shelf);
box(8.8, 0.14, 1.65, mats.steelLight, 0, 4.2, -2.65, shelf);
for (const x of [-4.25, -1.42, 1.42, 4.25]) {
box(0.17, 4.25, 0.17, mats.steel, x, 2.2, -1.86, shelf);
box(0.17, 4.25, 0.17, mats.steel, x, 2.2, -3.42, shelf);
}
box(8.4, 0.12, 0.14, mats.steelLight, 0, 1.08, -1.83, shelf);
box(8.4, 0.12, 0.14, mats.steelLight, 0, 3.14, -1.83, shelf);
// Upper decorative cartons.
for (let i = 0; i < 7; i++) {
const m = box(0.72, 0.55, 0.78, i % 2 ? mats.woodLight : mats.wood,
-3.45 + i * 1.12, 2.68, -2.73, shelf);
m.rotation.y = (i % 3 - 1) * 0.05;
}
// Front shelf slots.
const shelfSlotPositions = [
new THREE.Vector3(-3.35, 1.34, -2.03),
new THREE.Vector3(-2.02, 1.34, -2.03),
new THREE.Vector3(-0.69, 1.34, -2.03),
new THREE.Vector3(0.64, 1.34, -2.03),
new THREE.Vector3(1.97, 1.34, -2.03),
new THREE.Vector3(3.30, 1.34, -2.03)
];
// Open reserve crate.
const crate = new THREE.Group();
crate.position.set(-6.05, 0, -0.45);
scene.add(crate);
box(2.8, 0.14, 1.9, mats.wood, 0, 0.18, 0, crate);
box(2.8, 0.18, 0.13, mats.woodLight, 0, 0.31, -0.91, crate);
box(2.8, 0.18, 0.13, mats.woodLight, 0, 0.31, 0.91, crate);
box(0.13, 0.18, 1.9, mats.woodLight, -1.34, 0.31, 0, crate);
box(0.13, 0.18, 1.9, mats.woodLight, 1.34, 0.31, 0, crate);
for (const x of [-1.28, 1.28]) {
box(0.15, 0.65, 0.15, mats.wood, x, 0.42, -0.82, crate);
box(0.15, 0.65, 0.15, mats.wood, x, 0.42, 0.82, crate);
}
box(2.45, 0.12, 0.12, mats.woodLight, 0, 0.75, -0.91, crate);
box(2.45, 0.12, 0.12, mats.woodLight, 0, 0.75, 0.91, crate);
// Reserve positions, front-most first.
const reservePositions = [
new THREE.Vector3(-6.05, 0.83, 0.06),
new THREE.Vector3(-5.35, 0.83, 0.08),
new THREE.Vector3(-6.05, 0.83, -0.52),
new THREE.Vector3(-5.35, 0.83, -0.52),
new THREE.Vector3(-6.05, 1.48, -0.2)
];
// Checkout and outbound conveyor.
box(3.5, 0.95, 1.6, mats.steelLight, 6.25, 0.48, 2.25);
box(3.65, 0.16, 1.72, mats.belt, 6.25, 1.02, 2.25);
box(3.4, 0.13, 0.65, mats.belt, 8.55, 0.82, 3.85);
box(0.12, 0.82, 0.65, mats.steel, 6.85, 1.42, 2.25);
box(1.0, 0.12, 0.72, mats.orange, 6.85, 1.83, 2.25);
box(0.72, 0.75, 0.62, mats.blue, 8.75, 0.4, 4.65);
box(0.83, 0.11, 0.72, mats.white, 8.75, 0.83, 4.65);
// A small screen on the checkout.
box(0.85, 0.55, 0.08, mats.dark, 6.85, 2.05, 2.27);
box(0.68, 0.37, 0.025, mats.teal, 6.85, 2.05, 2.32);
function carton(color = 0xc48754) {
const g = new THREE.Group();
const bodyMat = new THREE.MeshStandardMaterial({ color, roughness: 0.82, flatShading: true });
const tapeMat = new THREE.MeshStandardMaterial({ color: 0xf0d49a, roughness: 0.9, flatShading: true });
box(0.82, 0.62, 0.66, bodyMat, 0, 0, 0, g);
box(0.15, 0.012, 0.67, tapeMat, 0, 0.316, 0, g);
box(0.15, 0.64, 0.012, tapeMat, 0, 0, 0.336, g);
box(0.42, 0.16, 0.014, mats.white, 0.08, 0.02, 0.342, g);
return g;
}
const cartonColors = [0xc7834d, 0xba7147, 0xd1995b, 0xb87349, 0xc98c52, 0xad6d48];
function createPerson(shirtColor, role = 'customer') {
const g = new THREE.Group();
const shirt = new THREE.MeshStandardMaterial({ color: shirtColor, roughness: 0.8, flatShading: true });
const trousers = new THREE.MeshStandardMaterial({ color: role === 'worker' ? 0x344d61 : 0x455363, roughness: 0.9, flatShading: true });
const body = mesh(new THREE.CylinderGeometry(0.34, 0.42, 0.78, 6), shirt, 0, 1.08, 0, g);
body.scale.z = 0.72;
mesh(new THREE.SphereGeometry(0.29, 8, 6), mats.skin, 0, 1.78, 0, g);
box(0.25, 0.06, 0.04, mats.dark, -0.09, 1.81, 0.263, g);
box(0.25, 0.06, 0.04, mats.dark, 0.09, 1.81, 0.263, g);
const legL = new THREE.Group();
const legR = new THREE.Group();
legL.position.set(-0.17, 0.72, 0);
legR.position.set(0.17, 0.72, 0);
g.add(legL, legR);
box(0.19, 0.58, 0.21, trousers, 0, -0.29, 0, legL);
box(0.19, 0.58, 0.21, trousers, 0, -0.29, 0, legR);
box(0.21, 0.13, 0.31, mats.dark, 0, -0.60, 0.05, legL);
box(0.21, 0.13, 0.31, mats.dark, 0, -0.60, 0.05, legR);
const armL = new THREE.Group();
const armR = new THREE.Group();
armL.position.set(-0.4, 1.37, 0);
armR.position.set(0.4, 1.37, 0);
g.add(armL, armR);
box(0.16, 0.60, 0.17, shirt, 0, -0.27, 0, armL);
box(0.16, 0.60, 0.17, shirt, 0, -0.27, 0, armR);
mesh(new THREE.SphereGeometry(0.10, 6, 5), mats.skin, 0, -0.60, 0, armL);
mesh(new THREE.SphereGeometry(0.10, 6, 5), mats.skin, 0, -0.60, 0, armR);
if (role === 'worker') {
box(0.49, 0.66, 0.04, mats.yellow, 0, 1.08, 0.34, g);
box(0.56, 0.10, 0.46, mats.yellow, 0, 2.01, 0.01, g);
box(0.42, 0.06, 0.25, mats.yellow, 0, 2.05, 0.19, g);
} else {
mesh(new THREE.SphereGeometry(0.30, 8, 5), shirt, 0, 1.98, -0.015, g);
}
let basket = null;
if (role === 'customer') {
basket = new THREE.Group();
basket.position.set(0.56, 0.75, 0.32);
g.add(basket);
box(0.46, 0.36, 0.36, mats.woodLight, 0, 0, 0, basket);
box(0.50, 0.08, 0.40, mats.wood, 0, 0.19, 0, basket);
box(0.08, 0.32, 0.08, mats.wood, -0.21, 0.17, 0, basket);
box(0.08, 0.32, 0.08, mats.wood, 0.21, 0.17, 0, basket);
box(0.08, 0.32, 0.08, mats.wood, -0.21, 0.17, 0, basket);
}
g.userData = { legL, legR, armL, armR, basket, walking: false, phase: Math.random() * 6 };
scene.add(g);
return g;
}
const customerA = createPerson(0x18a58d, 'customer');
customerA.position.set(7.7, 0, 4.55);
customerA.rotation.y = Math.PI;
const customerB = createPerson(0x7458c7, 'customer');
customerB.position.set(8.65, 0, 5.25);
customerB.rotation.y = Math.PI;
const worker = createPerson(0xe58a2d, 'worker');
worker.position.set(-7.6, 0, 1.0);
worker.rotation.y = Math.PI / 2;
// Inventory ledger: every physical box is always in exactly one bucket.
const counts = {
shelf: 6,
reserve: 5,
cart: 0,
sold: 0,
transit: 0
};
const TOTAL = shelfSlotPositions.length + reservePositions.length;
let shelfBoxes = [];
let reserveBoxes = [];
let soldBoxes = [];
let followers = [];
const els = {
shelf: document.getElementById('shelfCount'),
reserve: document.getElementById('reserveCount'),
cart: document.getElementById('cartCount'),
sold: document.getElementById('soldCount'),
status: document.getElementById('statusText'),
ledger: document.getElementById('ledgerText'),
shelfTag: document.getElementById('shelfTag'),
reserveTag: document.getElementById('reserveTag')
};
function updateHUD() {
els.shelf.textContent = counts.shelf;
els.reserve.textContent = counts.reserve;
els.cart.textContent = counts.cart;
els.sold.textContent = counts.sold;
const accounted = counts.shelf + counts.reserve + counts.cart + counts.sold + counts.transit;
els.ledger.textContent =
`Shelf ${counts.shelf} + reserve ${counts.reserve} + carts ${counts.cart} + ` +
`in transit ${counts.transit} + sold ${counts.sold} = ${accounted}/${TOTAL}`;
}
function setStatus(text) {
els.status.textContent = text;
}
function adjust(key, amount) {
counts[key] += amount;
updateHUD();
}
function clearInventoryMeshes() {
for (const b of [...shelfBoxes, ...reserveBoxes, ...soldBoxes]) {
scene.remove(b);
}
shelfBoxes = [];
reserveBoxes = [];
soldBoxes = [];
followers = [];
}
function populateInventory() {
clearInventoryMeshes();
Object.assign(counts, {
shelf: shelfSlotPositions.length, reserve: reservePositions.length,
cart: 0, sold: 0, transit: 0
});
shelfSlotPositions.forEach((p, i) => {
const b = carton(cartonColors[i]);
b.position.copy(p);
b.userData.slot = i;
scene.add(b);
shelfBoxes.push(b);
});
reservePositions.forEach((p, i) => {
const b = carton(cartonColors[(i + 2) % cartonColors.length]);
b.position.copy(p);
scene.add(b);
reserveBoxes.push(b);
});
}
// Custom tween engine.
let tweens = [];
let elapsed = 0;
const easeInOut = t => t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
function tween({ duration, update, complete }) {
return new Promise(resolve => {
tweens.push({
start: elapsed,
duration,
update,
complete,
resolve,
cancelled: false
});
});
}
function cancelTweens() {
for (const t of tweens) {
t.cancelled = true;
t.resolve(false);
}
tweens = [];
}
function updateTweens() {
for (let i = tweens.length - 1; i >= 0; i--) {
const t = tweens[i];
if (t.cancelled) continue;
const p = THREE.MathUtils.clamp((elapsed - t.start) / t.duration, 0, 1);
t.update(easeInOut(p), p);
if (p >= 1) {
tweens.splice(i, 1);
if (t.complete) t.complete();
t.resolve(true);
}
}
}
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function shortestAngle(from, to) {
return Math.atan2(Math.sin(to - from), Math.cos(to - from));
}
async function movePerson(person, target, duration) {
const from = person.position.clone();
const to = target.clone();
const dx = to.x - from.x;
const dz = to.z - from.z;
const targetYaw = Math.atan2(dx, dz);
const fromYaw = person.rotation.y;
const deltaYaw = shortestAngle(fromYaw, targetYaw);
person.userData.walking = true;
const ok = await tween({
duration,
update: t => {
person.position.lerpVectors(from, to, t);
person.rotation.y = fromYaw + deltaYaw * t;
},
complete: () => { person.userData.walking = false; }
});
return ok;
}
async function turnPerson(person, yaw, duration = 0.45) {
const start = person.rotation.y;
const delta = shortestAngle(start, yaw);
return tween({
duration,
update: t => { person.rotation.y = start + delta * t; }
});
}
async function transferBox(item, destination, duration = 1.1, arc = 1.0, endRotation = 0) {
const from = item.position.clone();
const to = destination.clone();
const mid = from.clone().lerp(to, 0.5);
mid.y += arc;
return tween({
duration,
update: t => {
const a = from.clone().lerp(mid, t);
const b = mid.clone().lerp(to, t);
item.position.lerpVectors(a, b, t);
item.rotation.y = THREE.MathUtils.lerp(item.rotation.y, endRotation, 0.08);
}
});
}
function worldPosition(object) {
const p = new THREE.Vector3();
object.getWorldPosition(p);
return p;
}
function followObject(item, target, offset = new THREE.Vector3(0, 0.1, 0)) {
followers.push({ item, target, offset });
}
function updateFollowers() {
for (const f of followers) {
f.item.position.copy(worldPosition(f.target).add(f.offset));
}
}
async function customerPurchase(customer, label) {
setStatus(`${label} is approaching the front shelf…`);
const reach = new THREE.Vector3(1.7, 0, 0.22);
if (!(await movePerson(customer, reach, 1.35))) return false;
setStatus(`${label} is removing a box from the front shelf.`);
await wait(250);
const item = shelfBoxes.shift();
const slot = item.userData.slot;
const start = item.position.clone();
adjust('shelf', -1);
adjust('transit', 1);
const basketWorld = worldPosition(customer.userData.basket).add(new THREE.Vector3(0, 0.25, 0));
if (!(await transferBox(item, basketWorld, 0.9, 0.65))) return false;
adjust('transit', -1);
adjust('cart', 1);
followObject(item, customer.userData.basket, new THREE.Vector3(0, 0.25, 0));
// The worker replenishes the exact slot that was just vacated.
await wait(300);
setStatus('The worker is pulling a box from the reserve crate…');
const reservePick = new THREE.Vector3(-5.95, 0, 1.05);
if (!(await movePerson(worker, reservePick, 1.15))) return false;
await turnPerson(worker, Math.PI, 0.35);
await wait(250);
const reserveItem = reserveBoxes.shift();
const reserveStart = reserveItem.position.clone();
adjust('reserve', -1);
adjust('transit', 1);
const shelfTarget = shelfSlotPositions[slot].clone();
if (!(await transferBox(reserveItem, shelfTarget, 1.45, 1.35))) return false;
adjust('transit', -1);
adjust('shelf', 1);
reserveItem.userData.slot = slot;
shelfBoxes.push(reserveItem);
setStatus('Reserve → shelf transfer complete. Counts are reconciled.');
await wait(650);
setStatus(`${label} is taking the purchased box to checkout…`);
const checkout = new THREE.Vector3(5.2, 0, 3.25);
if (!(await movePerson(customer, checkout, 1.65))) return false;
await turnPerson(customer, -Math.PI / 2, 0.4);
await wait(250);
setStatus(`${label} is placing the box on the outbound belt.`);
adjust('cart', -1);
adjust('transit', 1);
const follower = followers.find(f => f.item === item);
if (follower) followers.splice(followers.indexOf(follower), 1);
const soldTarget = new THREE.Vector3(
8.55 + (soldBoxes.length % 3) * 0.34,
1.24 + Math.floor(soldBoxes.length / 3) * 0.64,
3.85
);
if (!(await transferBox(item, soldTarget, 0.95, 0.85))) return false;
adjust('transit', -1);
adjust('sold', 1);
soldBoxes.push(item);
setStatus('Purchase complete. Shelf, reserve, cart, and sold totals balance.');
await wait(850);
return true;
}
async function resetCustomer(customer, home) {
await movePerson(customer, home, 1.2);
}
let runId = 0;
async function demo(id) {
populateInventory();
updateHUD();
setStatus('Shift started. Front shelf and reserve crate are ready.');
const aHome = customerA.position.clone();
const bHome = customerB.position.clone();
await wait(700);
if (id !== runId) return;
if (await customerPurchase(customerA, 'Customer A')) {
if (id !== runId) return;
await resetCustomer(customerA, aHome);
}
if (id !== runId) return;
if (await customerPurchase(customerB, 'Customer B')) {
if (id !== runId) return;
await resetCustomer(customerB, bHome);
}
if (id !== runId) return;
setStatus('Cycle complete — ready to replay the restock sequence.');
await wait(1000);
if (id === runId) demo(++runId);
}
document.getElementById('replay').addEventListener('click', () => {
runId++;
cancelTweens();
clearInventoryMeshes();
customerA.position.set(7.7, 0, 4.55);
customerA.rotation.y = Math.PI;
customerB.position.set(8.65, 0, 5.25);
customerB.rotation.y = Math.PI;
worker.position.set(-7.6, 0, 1.0);
worker.rotation.y = Math.PI / 2;
demo(runId);
});
// Camera controls implemented directly without Three.js addons.
const orbit = {
yaw: 0.56,
pitch: 0.54,
distance: 16.8,
target: new THREE.Vector3(0, 1.15, -0.15)
};
const defaultOrbit = { ...orbit, target: orbit.target.clone() };
const pointers = new Map();
let pinchDistance = 0;
function updateCamera() {
const cp = Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * cp * orbit.distance,
orbit.target.y + Math.sin(orbit.pitch) * orbit.distance,
orbit.target.z + Math.cos(orbit.yaw) * cp * orbit.distance
);
camera.lookAt(orbit.target);
}
function resetView() {
orbit.yaw = defaultOrbit.yaw;
orbit.pitch = defaultOrbit.pitch;
orbit.distance = defaultOrbit.distance;
orbit.target.copy(defaultOrbit.target);
updateCamera();
}
document.getElementById('viewReset').addEventListener('click', resetView);
renderer.domElement.addEventListener('pointerdown', e => {
renderer.domElement.setPointerCapture(e.pointerId);
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size === 2) {
const p = [...pointers.values()];
pinchDistance = Math.hypot(p[0].x - p[1].x, p[0].y - p[1].y);
}
});
renderer.domElement.addEventListener('pointermove', e => {
if (!pointers.has(e.pointerId)) return;
const old = pointers.get(e.pointerId);
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size === 2) {
const p = [...pointers.values()];
const d = Math.hypot(p[0].x - p[1].x, p[0].y - p[1].y);
if (pinchDistance) {
orbit.distance = THREE.MathUtils.clamp(orbit.distance * pinchDistance / d, 9, 25);
}
pinchDistance = d;
} else {
orbit.yaw -= (e.clientX - old.x) * 0.006;
orbit.pitch = THREE.MathUtils.clamp(orbit.pitch + (e.clientY - old.y) * 0.004, 0.25, 1.05);
}
});
function endPointer(e) {
pointers.delete(e.pointerId);
if (pointers.size < 2) pinchDistance = 0;
}
renderer.domElement.addEventListener('pointerup', endPointer);
renderer.domElement.addEventListener('pointercancel', endPointer);
renderer.domElement.addEventListener('wheel', e => {
e.preventDefault();
orbit.distance = THREE.MathUtils.clamp(orbit.distance + e.deltaY * 0.012, 9, 25);
}, { passive: false });
renderer.domElement.addEventListener('dblclick', resetView);
// Project simple labels onto their world anchors.
function positionTag(element, world) {
const p = world.clone().project(camera);
const visible = p.z > -1 && p.z < 1;
element.style.display = visible ? 'block' : 'none';
element.style.left = `${(p.x * 0.5 + 0.5) * innerWidth}px`;
element.style.top = `${(-p.y * 0.5 + 0.5) * innerHeight}px`;
}
function animatePeople(time) {
for (const person of [customerA, customerB, worker]) {
const u = person.userData;
const speed = u.walking ? 10 : 1.6;
u.phase += time * speed;
const swing = u.walking ? Math.sin(u.phase) * 0.52 : Math.sin(u.phase) * 0.025;
u.legL.rotation.x = swing;
u.legR.rotation.x = -swing;
u.armL.rotation.x = -swing * 0.75;
u.armR.rotation.x = swing * 0.75;
const bob = u.walking ? Math.abs(Math.sin(u.phase)) * 0.035 : 0;
u.armL.position.y = 1.37 + bob;
u.armR.position.y = 1.37 + bob;
}
}
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
updateTweens();
updateFollowers();
animatePeople(dt);
updateCamera();
positionTag(els.shelfTag, new THREE.Vector3(0, 4.65, -2.5));
positionTag(els.reserveTag, new THREE.Vector3(-6.05, 1.9, -0.45));
renderer.render(scene, camera);
});
populateInventory();
updateHUD();
updateCamera();
demo(runId);
</script>
</body>
</html>
```