Prompt specification
Build or customize the Three.js 3D scene "Corner Shop Restocking Delay" 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: Corner Shop Restocking Delay
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/corner-shop-restocking-delay-x3154
Scene Description Prompt:
Voxel customers buy jars; threshold-triggered orders arrive later on a restocking cart following its route. Create one HTML file using Three.js from cdnjs.
Working Three.js Starter (corner-shop-restocking-delay-x3154):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Pantry — Threshold Restocking</title>
<style>
:root {
--ink: #20312d;
--muted: #647772;
--paper: rgba(255, 255, 255, 0.88);
--green: #2f8f78;
--green-dark: #226a59;
--orange: #e68a45;
--line: rgba(38, 74, 65, 0.13);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e5;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.glass {
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow: 0 16px 45px rgba(40, 67, 60, 0.14);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
header {
position: fixed;
z-index: 5;
top: 18px;
left: 20px;
right: 20px;
display: flex;
justify-content: space-between;
align-items: flex-start;
pointer-events: none;
}
.brand {
padding: 13px 17px;
border-radius: 16px;
}
.brand h1 {
margin: 0;
font-size: 17px;
line-height: 1;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.brand p {
margin: 7px 0 0;
color: var(--muted);
font-size: 12px;
}
.stats {
display: flex;
gap: 8px;
}
.stat {
min-width: 104px;
padding: 11px 14px;
border-radius: 14px;
}
.stat label {
display: block;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.stat strong {
display: block;
margin-top: 3px;
font-size: 20px;
}
.order-card {
position: fixed;
z-index: 5;
top: 100px;
right: 20px;
width: 294px;
padding: 16px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
color: var(--green);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.order-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 7px;
}
#orderTitle {
font-size: 18px;
font-weight: 850;
}
.badge {
flex: none;
padding: 5px 8px;
border-radius: 999px;
background: #e5efec;
color: var(--green-dark);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge.moving { background: #fff0df; color: #a65b22; }
.badge.unloading { background: #e9e4fb; color: #6551a4; }
.badge.returning { background: #e1eef8; color: #376f9c; }
#orderDetail {
min-height: 38px;
margin-top: 8px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.meter-label {
display: flex;
justify-content: space-between;
margin: 12px 0 6px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.meter {
height: 8px;
overflow: hidden;
border-radius: 999px;
background: rgba(35, 88, 75, 0.1);
}
.meter span {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #54b89f, #2f8f78);
transition: width 0.25s ease;
}
#salesFill { background: linear-gradient(90deg, #f2b46d, #e68a45); }
.activity {
position: fixed;
z-index: 5;
left: 20px;
bottom: 64px;
width: 310px;
padding: 14px 15px;
border-radius: 17px;
pointer-events: none;
}
.activity h2 {
margin: 0 0 9px;
font-size: 11px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#log {
display: grid;
gap: 7px;
margin: 0;
padding: 0;
list-style: none;
}
#log li {
display: grid;
grid-template-columns: 48px 1fr;
gap: 8px;
color: #50645e;
font-size: 11px;
line-height: 1.35;
}
#log time {
color: #91a09c;
font-variant-numeric: tabular-nums;
}
.controls {
position: fixed;
z-index: 6;
left: 20px;
right: 20px;
bottom: 17px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.hint {
padding: 9px 12px;
border-radius: 12px;
color: #536762;
font-size: 11px;
}
.buttons {
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
border: 0;
border-radius: 12px;
padding: 10px 14px;
background: #24483f;
color: white;
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
box-shadow: 0 8px 20px rgba(29, 65, 56, 0.18);
}
button:hover { background: #173a32; }
button:active { transform: translateY(1px); }
@media (max-width: 760px) {
header { top: 10px; left: 10px; right: 10px; }
.brand { padding: 10px 12px; }
.brand p { display: none; }
.brand h1 { font-size: 13px; }
.stats { gap: 4px; }
.stat { min-width: 65px; padding: 9px; }
.stat label { font-size: 8px; }
.stat strong { font-size: 15px; }
.order-card { top: 83px; right: 10px; width: 235px; }
.activity { display: none; }
.controls { left: 10px; right: 10px; bottom: 10px; }
.hint { max-width: 170px; font-size: 9px; }
}
</style>
</head>
<body>
<div id="scene"></div>
<header>
<div class="brand glass">
<h1>Voxel Pantry</h1>
<p>Automatic jar replenishment, one threshold at a time.</p>
</div>
<div class="stats">
<div class="stat glass">
<label>Shelf stock</label>
<strong id="stockValue">6 / 6</strong>
</div>
<div class="stat glass">
<label>Sales / order</label>
<strong id="salesValue">0 / 3</strong>
</div>
<div class="stat glass">
<label>Orders</label>
<strong id="ordersValue">0</strong>
</div>
</div>
</header>
<section class="order-card glass">
<div class="eyebrow">Threshold order</div>
<div class="order-heading">
<div id="orderTitle">Waiting for sales</div>
<div id="phaseBadge" class="badge">Idle</div>
</div>
<div id="orderDetail">Three jar sales will trigger a delayed restocking order.</div>
<div class="meter-label">
<span>Sales threshold</span>
<span id="salesLabel">0 of 3</span>
</div>
<div class="meter"><span id="salesFill"></span></div>
<div class="meter-label">
<span>Restock route</span>
<span id="routeLabel">0%</span>
</div>
<div class="meter"><span id="routeFill"></span></div>
</section>
<section class="activity glass">
<h2>Store activity</h2>
<ul id="log"></ul>
</section>
<div class="controls">
<div class="hint glass">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div class="buttons">
<button id="speedButton" type="button">Speed 1×</button>
<button id="restartButton" type="button">Restart demo</button>
</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 sceneHost = document.getElementById("scene");
const stockValue = document.getElementById("stockValue");
const salesValue = document.getElementById("salesValue");
const ordersValue = document.getElementById("ordersValue");
const salesFill = document.getElementById("salesFill");
const salesLabel = document.getElementById("salesLabel");
const routeFill = document.getElementById("routeFill");
const routeLabel = document.getElementById("routeLabel");
const orderTitle = document.getElementById("orderTitle");
const orderDetail = document.getElementById("orderDetail");
const phaseBadge = document.getElementById("phaseBadge");
const logList = document.getElementById("log");
const app = {
token: 0,
speed: 1,
sales: 0,
orderCount: 0,
phase: "idle",
pendingEndsAt: 0,
orderQueued: false
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e5);
scene.fog = new THREE.Fog(0xdce9e5, 25, 43);
const camera = new THREE.PerspectiveCamera(
39,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
sceneHost.appendChild(renderer.domElement);
const cameraState = {
target: new THREE.Vector3(0, 1.25, 0),
radius: 21,
theta: 0.63,
phi: 1.02
};
const hemi = new THREE.HemisphereLight(0xf6fff9, 0x75857d, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff2d8, 3.2);
sun.position.set(-7, 14, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const materialCache = new Map();
function material(color) {
if (color && color.isMaterial) return color;
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({ color, roughness: 0.83, metalness: 0.0 })
);
}
return materialCache.get(color);
}
function addBox(parent, size, position, color, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material(color)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeWallSign(text, width, height, background = "#24483f", foreground = "#fffaf0") {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "rgba(255,255,255,.18)";
ctx.lineWidth = 12;
ctx.strokeRect(18, 18, canvas.width - 36, canvas.height - 36);
ctx.fillStyle = foreground;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "900 88px system-ui, sans-serif";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 3);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({ map: texture })
);
return mesh;
}
function makeSpriteLabel(text, background = "#24483f", foreground = "#ffffff") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = foreground;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "900 44px system-ui, sans-serif";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false
})
);
sprite.renderOrder = 10;
return sprite;
}
const shelfJars = [];
const soldJars = [];
const effects = [];
const popups = [];
const activeCustomers = new Set();
const jamColors = [
0xe66b5b,
0xe6ad4e,
0x5ea98f,
0x9a78bd,
0xe8894f,
0x6b9ecb
];
function makeJar(contentColor) {
const group = new THREE.Group();
addBox(group, [0.48, 0.53, 0.48], [0, 0.29, 0], contentColor);
addBox(group, [0.51, 0.12, 0.51], [0, 0.61, 0], 0xf3e8ce);
addBox(group, [0.28, 0.19, 0.025], [0, 0.30, 0.251], 0xfff7e8);
addBox(group, [0.12, 0.035, 0.028], [0, 0.31, 0.267], 0xd7a44b);
group.userData.contentColor = contentColor;
return group;
}
// Store architecture.
addBox(scene, [18, 0.4, 13.5], [0, -0.2, 0], 0xcbd8d3, false);
const tileColors = [0xf0eee4, 0xe6e6dc];
for (let x = 0; x < 12; x++) {
for (let z = 0; z < 9; z++) {
addBox(
scene,
[1.46, 0.035, 1.46],
[-8.25 + x * 1.5, 0.018, -6 + z * 1.5],
tileColors[(x + z) % 2],
false
);
}
}
addBox(scene, [18, 5.8, 0.25], [0, 2.9, -6.58], 0xe9e2d2);
addBox(scene, [0.25, 5.8, 7.4], [-8.86, 2.9, -2.75], 0xd9dfd3);
addBox(scene, [18, 0.18, 0.3], [0, 0.16, -6.4], 0xb5c8bd);
const pantrySign = makeWallSign("VOXEL PANTRY", 6.8, 1.25);
pantrySign.position.set(-2.8, 4.2, -6.43);
scene.add(pantrySign);
const subtitleSign = makeWallSign("JARS • DAILY • FRESH", 4.4, 0.52, "#d8894d", "#fffaf1");
subtitleSign.position.set(5.5, 3.95, -6.42);
scene.add(subtitleSign);
// Window and voxel framing.
addBox(scene, [4.25, 2.55, 0.09], [5.5, 2.15, -6.4], 0x9ec8d0, false);
addBox(scene, [0.12, 2.55, 0.14], [5.5, 2.15, -6.31], 0xf6f0df);
addBox(scene, [4.25, 0.12, 0.14], [5.5, 2.15, -6.31], 0xf6f0df);
addBox(scene, [4.55, 0.16, 0.2], [5.5, 3.47, -6.3], 0xf6f0df);
addBox(scene, [4.55, 0.16, 0.2], [5.5, 0.83, -6.3], 0xf6f0df);
// Jam shelf.
addBox(scene, [7.7, 3.4, 0.18], [-2.8, 1.85, -5.12], 0xc2a77c);
[-6.65, -2.8, 1.05].forEach(x => {
addBox(scene, [0.18, 3.55, 1.28], [x, 1.8, -4.55], 0x866d4d);
});
[0.55, 1.52, 2.49].forEach(y => {
addBox(scene, [7.9, 0.16, 1.34], [-2.8, y, -4.55], 0x9b754a);
addBox(scene, [7.7, 0.07, 1.26], [-2.8, y + 0.105, -4.55], 0xc18d59);
});
const shelfLabel = makeWallSign("JAM SHELF", 2.7, 0.55, "#e3d6ba", "#5f4d35");
shelfLabel.position.set(-2.8, 3.22, -5.0);
scene.add(shelfLabel);
const jarXPositions = [-5.6, -4.48, -3.36, -2.24, -1.12, 0.0];
jarXPositions.forEach((x, i) => {
const jar = makeJar(jamColors[i]);
jar.position.set(x, 0.64, -4.25);
scene.add(jar);
shelfJars.push(jar);
addBox(scene, [0.18, 0.12, 0.03], [x, 0.76, -3.865], 0xf5ead2);
});
// Decorative crates on the upper shelf.
for (let i = 0; i < 4; i++) {
const crate = addBox(
scene,
[1.05, 0.72, 0.82],
[-5.75 + i * 1.75, 2.88, -4.48],
i % 2 ? 0xb98d5b : 0xc59b65
);
crate.rotation.y = (i - 1.5) * 0.025;
addBox(scene, [1.08, 0.09, 0.86], [-5.75 + i * 1.75, 3.08, -4.48], 0xe1bd86);
}
// Checkout.
addBox(scene, [3.35, 1.22, 1.55], [3.55, 0.66, 0.15], 0x4c8f80);
addBox(scene, [3.55, 0.18, 1.72], [3.55, 1.36, 0.15], 0xf0d5a7);
addBox(scene, [2.6, 0.06, 0.95], [3.45, 1.47, 0.18], 0x455a55);
addBox(scene, [0.7, 0.12, 0.6], [3.8, 1.52, 0.05], 0x30443f);
addBox(scene, [0.55, 0.65, 0.16], [3.8, 1.88, -0.03], 0x344a45);
addBox(scene, [0.4, 0.25, 0.04], [3.8, 1.93, 0.07], 0x8dd5c4);
const checkoutSign = makeWallSign("CHECKOUT", 2.25, 0.52, "#f2e4c6", "#4a665d");
checkoutSign.position.set(3.45, 0.72, 0.94);
scene.add(checkoutSign);
// Entrance mat and service area.
addBox(scene, [3.5, 0.06, 1.45], [-5.9, 0.075, 5.2], 0x5e9888);
addBox(scene, [3.0, 0.07, 0.12], [-5.9, 0.12, 5.2], 0xe7d8b9);
addBox(scene, [2.5, 0.06, 2.3], [7.35, 0.07, -5.35], 0xbecdc7);
addBox(scene, [0.12, 0.07, 2.1], [7.35, 0.12, -5.35], 0xe3a365);
// Voxel plant.
addBox(scene, [0.72, 0.75, 0.72], [-7.55, 0.38, -3.6], 0xc77650);
addBox(scene, [0.16, 1.2, 0.16], [-7.55, 1.15, -3.6], 0x557c54);
addBox(scene, [0.78, 0.5, 0.62], [-7.15, 1.35, -3.65], 0x5c9864);
addBox(scene, [0.65, 0.58, 0.72], [-7.95, 1.6, -3.65], 0x74a86f);
// The restocking route.
const routeCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(7.45, 0.07, -5.45),
new THREE.Vector3(7.45, 0.07, -2.0),
new THREE.Vector3(7.15, 0.07, 2.55),
new THREE.Vector3(5.15, 0.07, 4.35),
new THREE.Vector3(1.0, 0.07, 4.25),
new THREE.Vector3(-2.8, 0.07, 3.35),
new THREE.Vector3(-2.8, 0.07, -3.0)
],
false,
"catmullrom",
0.18
);
const routeGeometry = new THREE.BufferGeometry().setFromPoints(routeCurve.getPoints(100));
const routeLine = new THREE.Line(
routeGeometry,
new THREE.LineDashedMaterial({
color: 0x4d9483,
dashSize: 0.3,
gapSize: 0.2,
transparent: true,
opacity: 0.66
})
);
routeLine.computeLineDistances();
scene.add(routeLine);
for (let i = 0; i < 15; i++) {
const p = routeCurve.getPointAt(i / 14);
addBox(scene, [0.13, 0.045, 0.13], [p.x, 0.075, p.z], 0x72ad9c, false);
}
// Restocking cart.
const cart = new THREE.Group();
scene.add(cart);
addBox(cart, [1.72, 0.22, 1.12], [0, 0.49, 0], 0x2f8f78);
addBox(cart, [1.82, 0.09, 1.2], [0, 0.63, 0], 0x65b49f);
[-0.65, 0.65].forEach(x => {
addBox(cart, [0.13, 0.95, 0.13], [x, 1.05, -0.43], 0x335c53);
});
addBox(cart, [1.42, 0.13, 0.13], [0, 1.5, -0.43], 0x335c53);
const cartWheels = [];
[-0.57, 0.57].forEach(x => {
[-0.35, 0.35].forEach(z => {
const wheel = addBox(cart, [0.31, 0.31, 0.31], [x, 0.23, z], 0x293b38);
cartWheels.push(wheel);
});
});
const cargoJars = [];
for (let i = 0; i < 3; i++) {
const jar = makeJar(jamColors[i]);
jar.scale.setScalar(0.8);
jar.position.set((i - 1) * 0.48, 0.69, 0);
cart.add(jar);
cargoJars.push(jar);
}
const cartOrderLabel = makeSpriteLabel("ORDER #1001");
cartOrderLabel.position.set(0, 1.92, 0);
cartOrderLabel.scale.set(2.3, 0.58, 1);
cart.add(cartOrderLabel);
cart.visible = false;
// Queued order parcel at the service hatch.
const orderParcel = new THREE.Group();
orderParcel.position.set(7.45, 0, -5.45);
scene.add(orderParcel);
addBox(orderParcel, [1.18, 0.82, 1.05], [0, 0.42, 0], 0xd49a62);
addBox(orderParcel, [0.15, 0.86, 1.08], [0, 0.43, 0], 0xf0d9aa);
addBox(orderParcel, [1.22, 0.13, 0.16], [0, 0.44, 0], 0xf0d9aa);
const parcelLabel = makeSpriteLabel("ORDER QUEUED", "#b66836");
parcelLabel.position.set(0, 1.28, 0);
parcelLabel.scale.set(2.15, 0.54, 1);
orderParcel.add(parcelLabel);
orderParcel.visible = false;
function updateCamera() {
const s = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * s * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * s * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.radius = 21;
cameraState.theta = 0.63;
cameraState.phi = 1.02;
}
// Direct camera controls: no addon imports.
let dragging = false;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi - dy * 0.005, 0.48, 1.38);
});
renderer.domElement.addEventListener("pointerup", () => {
dragging = false;
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.015,
12,
30
);
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
addEventListener("keydown", event => {
if (event.key === "ArrowLeft") cameraState.theta -= 0.08;
if (event.key === "ArrowRight") cameraState.theta += 0.08;
if (event.key === "ArrowUp") cameraState.phi = Math.max(0.48, cameraState.phi - 0.05);
if (event.key === "ArrowDown") cameraState.phi = Math.min(1.38, cameraState.phi + 0.05);
if (event.key === "+" || event.key === "=") cameraState.radius = Math.max(12, cameraState.radius - 1);
if (event.key === "-") cameraState.radius = Math.min(30, cameraState.radius + 1);
});
function createCustomer(index) {
const root = new THREE.Group();
const shirts = [0xe77b67, 0x5b8fc4, 0xd7aa45];
const skins = [0xe4b58d, 0x9f6549, 0xf0c7a3];
const hairs = [0x3e3029, 0x6c3f2b, 0x292d2e];
const shirt = shirts[index % shirts.length];
const skin = skins[index % skins.length];
const hair = hairs[index % hairs.length];
addBox(root, [0.72, 0.8, 0.43], [0, 1.05, 0], shirt);
addBox(root, [0.58, 0.58, 0.57], [0, 1.76, 0], skin);
addBox(root, [0.61, 0.19, 0.59], [0, 2.04, -0.01], hair);
addBox(root, [0.07, 0.08, 0.04], [-0.13, 1.82, 0.303], 0x26322f);
addBox(root, [0.07, 0.08, 0.04], [0.13, 1.82, 0.303], 0x26322f);
const leftLeg = addBox(root, [0.25, 0.65, 0.27], [-0.19, 0.42, 0], 0x3d5360);
const rightLeg = addBox(root, [0.25, 0.65, 0.27], [0.19, 0.42, 0], 0x3d5360);
addBox(root, [0.28, 0.16, 0.42], [-0.19, 0.1, 0.08], 0x30413e);
addBox(root, [0.28, 0.16, 0.42], [0.19, 0.1, 0.08], 0x30413e);
const leftArmPivot = new THREE.Group();
leftArmPivot.position.set(-0.48, 1.35, 0);
root.add(leftArmPivot);
addBox(leftArmPivot, [0.22, 0.66, 0.25], [0, -0.27, 0], shirt);
addBox(leftArmPivot, [0.22, 0.18, 0.25], [0, -0.64, 0], skin);
const rightArmPivot = new THREE.Group();
rightArmPivot.position.set(0.48, 1.35, 0);
root.add(rightArmPivot);
addBox(rightArmPivot, [0.22, 0.66, 0.25], [0, -0.27, 0], shirt);
addBox(rightArmPivot, [0.22, 0.18, 0.25], [0, -0.64, 0], skin);
const heldJar = makeJar(jamColors[index % jamColors.length]);
heldJar.scale.setScalar(0.78);
heldJar.position.set(0.1, 0.97, 0.67);
heldJar.visible = false;
root.add(heldJar);
root.userData = {
leftLeg,
rightLeg,
leftArmPivot,
rightArmPivot,
heldJar
};
scene.add(root);
activeCustomers.add(root);
return root;
}
function animateFor(duration, update, token) {
if (token !== app.token) return Promise.resolve(false);
return new Promise(resolve => {
let last = performance.now();
let elapsed = 0;
function frame(now) {
if (token !== app.token) {
resolve(false);
return;
}
const dt = Math.min(50, now - last);
last = now;
elapsed += dt * app.speed;
const p = duration <= 0 ? 1 : Math.min(1, elapsed / duration);
update(p);
if (p >= 1) resolve(true);
else requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
});
}
function waitReal(duration, token) {
return new Promise(resolve => {
const end = performance.now() + duration;
function frame(now) {
if (token !== app.token) {
resolve(false);
return;
}
if (now >= end) resolve(true);
else requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
});
}
async function walk(customer, points, token, speed = 2.1) {
const lengths = [];
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
const length = Math.hypot(
points[i + 1][0] - points[i][0],
points[i + 1][1] - points[i][1]
);
lengths.push(length);
total += length;
}
const ok = await animateFor((total / speed) * 1000, p => {
let distance = p * total;
let segment = lengths.length - 1;
for (let i = 0; i < lengths.length; i++) {
if (distance <= lengths[i]) {
segment = i;
break;
}
distance -= lengths[i];
}
const a = points[segment];
const b = points[segment + 1];
const t = lengths[segment] === 0 ? 0 : distance / lengths[segment];
customer.position.x = THREE.MathUtils.lerp(a[0], b[0], t);
customer.position.z = THREE.MathUtils.lerp(a[1], b[1], t);
customer.position.y = Math.abs(Math.sin(distance * 5.2)) * 0.05;
const dx = b[0] - a[0];
const dz = b[1] - a[1];
if (Math.abs(dx) + Math.abs(dz) > 0.001) {
customer.rotation.y = Math.atan2(dx, dz);
}
const swing = Math.sin(distance * 5.2) * 0.48;
customer.userData.leftLeg.rotation.x = swing;
customer.userData.rightLeg.rotation.x = -swing;
customer.userData.leftArmPivot.rotation.x = -swing * 0.7;
}, token);
customer.position.y = 0;
customer.userData.leftLeg.rotation.x = 0;
customer.userData.rightLeg.rotation.x = 0;
customer.userData.leftArmPivot.rotation.x = 0;
return ok;
}
function makeCoinBurst(position) {
for (let i = 0; i < 7; i++) {
const coin = addBox(
scene,
[0.11, 0.11, 0.11],
[position.x, position.y, position.z],
0xf1bd50,
false
);
effects.push({
mesh: coin,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 2.1,
1.7 + Math.random() * 1.4,
(Math.random() - 0.5) * 1.8
),
age: 0
});
}
}
function updateUI() {
const remaining = 6 - soldJars.length;
stockValue.textContent = `${remaining} / 6`;
salesValue.textContent = `${app.sales} / 3`;
ordersValue.textContent = String(app.orderCount);
salesFill.style.width = `${Math.min(100, (app.sales / 3) * 100)}%`;
salesLabel.textContent = `${Math.min(app.sales, 3)} of 3`;
}
function setPhase(phase, title, detail) {
app.phase = phase;
orderTitle.textContent = title;
orderDetail.textContent = detail;
phaseBadge.className = "badge";
if (phase === "moving") phaseBadge.classList.add("moving");
if (phase === "unloading") phaseBadge.classList.add("unloading");
if (phase === "returning") phaseBadge.classList.add("returning");
phaseBadge.textContent =
phase === "idle" ? "Idle" :
phase === "pending" ? "Queued" :
phase === "moving" ? "En route" :
phase === "unloading" ? "Unloading" :
phase === "returning" ? "Returning" :
"Ready";
}
function logEvent(text) {
const li = document.createElement("li");
const time = document.createElement("time");
const message = document.createElement("span");
const now = new Date();
time.textContent = now.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit"
});
message.textContent = text;
li.append(time, message);
logList.prepend(li);
while (logList.children.length > 5) {
logList.lastElementChild.remove();
}
}
function queueOrder() {
app.orderCount++;
app.orderQueued = true;
app.pendingEndsAt = performance.now() + 3500;
const id = 1000 + app.orderCount;
cartOrderLabel.material.map.dispose();
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const ctx = labelCanvas.getContext("2d");
ctx.fillStyle = "#24483f";
ctx.fillRect(0, 0, 512, 128);
ctx.fillStyle = "#ffffff";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "900 44px system-ui, sans-serif";
ctx.fillText(`ORDER #${id}`, 256, 66);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
cartOrderLabel.material.map = texture;
cartOrderLabel.material.needsUpdate = true;
orderParcel.visible = true;
setPhase(
"pending",
`ORDER #${id}`,
"Three-jar threshold reached. The order is queued for delayed dispatch."
);
logEvent(`Threshold reached: three jars sold. Order #${id} queued.`);
updateUI();
}
async function purchaseWave(token) {
const customerNames = ["Mara", "Pip", "Jules"];
for (let i = 0; i < 3; i++) {
const customer = createCustomer(i);
customer.position.set(-6.8, 0, 5.8);
const visible = shelfJars.filter(jar => jar.visible);
const selectedJar = visible[0];
if (!selectedJar) return false;
const jarX = selectedJar.position.x;
let ok = await walk(
customer,
[
[-6.8, 5.8],
[-5.8, 4.3],
[jarX - 0.5, 1.0],
[jarX, -3.0]
],
token,
2.0
);
if (!ok) return false;
customer.rotation.y = Math.PI;
selectedJar.visible = false;
soldJars.push(selectedJar);
customer.userData.heldJar.visible = true;
customer.userData.heldJar.userData.contentColor = selectedJar.userData.contentColor;
await animateFor(520, p => {
customer.userData.rightArmPivot.rotation.x = -Math.sin(p * Math.PI) * 1.08;
customer.userData.heldJar.position.z = 0.62 + Math.sin(p * Math.PI) * 0.18;
}, token);
customer.userData.rightArmPivot.rotation.x = 0;
ok = await walk(
customer,
[
[jarX, -2.8],
[0.9, -1.2],
[3.05, 1.25]
],
token,
2.2
);
if (!ok) return false;
customer.rotation.y = Math.PI;
await animateFor(440, p => {
customer.userData.rightArmPivot.rotation.x = -Math.sin(p * Math.PI) * 0.85;
}, token);
customer.userData.rightArmPivot.rotation.x = 0;
customer.userData.heldJar.visible = false;
app.sales++;
updateUI();
makeCoinBurst(new THREE.Vector3(
customer.position.x,
1.38,
customer.position.z - 0.25
));
logEvent(`${customerNames[i]} bought a jar at checkout.`);
if (app.sales >= 3 && !app.orderQueued) {
queueOrder();
}
await animateFor(250, () => {}, token);
ok = await walk(
customer,
[
[3.05, 1.45],
[5.25, 2.0],
[7.55, 5.85]
],
token,
2.25
);
if (!ok) return false;
activeCustomers.delete(customer);
scene.remove(customer);
}
return true;
}
function easeInOut(p) {
return p * p * (3 - 2 * p);
}
async function deliverOrder(token) {
const remainingDelay = Math.max(0, app.pendingEndsAt - performance.now());
if (remainingDelay > 0) {
const ok = await waitReal(remainingDelay, token);
if (!ok) return false;
}
orderParcel.visible = false;
cart.visible = true;
cargoJars.forEach(jar => jar.visible = true);
const outboundOK = await animateFor(10500, p => {
const t = easeInOut(p);
const point = routeCurve.getPointAt(t);
const tangent = routeCurve.getTangentAt(t);
cart.position.copy(point);
cart.rotation.y = Math.atan2(tangent.x, tangent.z);
cart.position.y = Math.abs(Math.sin(p * 45)) * 0.025;
routeFill.style.width = `${p * 100}%`;
routeLabel.textContent = `${Math.round(p * 100)}%`;
setPhase(
"moving",
`ORDER #${1000 + app.orderCount}`,
"The restocking cart is carrying three jars along the depot-to-shelf route."
);
}, token);
if (!outboundOK) return false;
setPhase(
"unloading",
`ORDER #${1000 + app.orderCount}`,
"Cart arrived. Transferring the ordered jars to the shelf."
);
routeFill.style.width = "100%";
routeLabel.textContent = "100%";
for (let i = 0; i < Math.min(3, soldJars.length); i++) {
const shelfJar = soldJars[0];
cargoJars[i].visible = false;
const destination = new THREE.Vector3();
shelfJar.getWorldPosition(destination);
const start = cart.position.clone().add(new THREE.Vector3((i - 1) * 0.38, 1.0, 0));
const flightJar = makeJar(shelfJar.userData.contentColor);
flightJar.scale.setScalar(0.78);
scene.add(flightJar);
const ok = await animateFor(680, p => {
flightJar.position.lerpVectors(start, destination, p);
flightJar.position.y += Math.sin(p * Math.PI) * 1.15;
}, token);
if (!ok) {
scene.remove(flightJar);
return false;
}
scene.remove(flightJar);
soldJars.shift();
shelfJar.visible = true;
shelfJar.scale.setScalar(0.01);
popups.push({ object: shelfJar, start: performance.now() });
updateUI();
setPhase(
"unloading",
`ORDER #${1000 + app.orderCount}`,
`Unloading jar ${i + 1} of 3 into the jam shelf.`
);
}
app.sales = 0;
app.orderQueued = false;
updateUI();
logEvent("Restock complete: the shelf is back to six jars.");
await waitReal(900, token);
const returnOK = await animateFor(8800, p => {
const t = 1 - easeInOut(p);
const point = routeCurve.getPointAt(t);
const tangent = routeCurve.getTangentAt(t).multiplyScalar(-1);
cart.position.copy(point);
cart.rotation.y = Math.atan2(tangent.x, tangent.z);
routeFill.style.width = `${(1 - p) * 100}%`;
routeLabel.textContent = `${Math.round((1 - p) * 100)}%`;
setPhase(
"returning",
`ORDER #${1000 + app.orderCount}`,
"Restock complete. The empty cart is returning to the service hatch."
);
}, token);
if (!returnOK) return false;
cart.visible = false;
routeFill.style.width = "0%";
routeLabel.textContent = "0%";
setPhase(
"idle",
"Ready for the next order",
"The shelf is stocked and threshold monitoring has restarted."
);
return true;
}
async function runDemo(token) {
setPhase(
"idle",
"Waiting for sales",
"Customers are shopping. Three jar sales will trigger a delayed order."
);
logEvent("Store opened. Shelf monitoring is active.");
while (token === app.token) {
const purchased = await purchaseWave(token);
if (!purchased) return;
const delivered = await deliverOrder(token);
if (!delivered) return;
await waitReal(700, token);
}
}
function clearDemo() {
app.token++;
app.sales = 0;
app.orderCount = 0;
app.phase = "idle";
app.orderQueued = false;
activeCustomers.forEach(customer => scene.remove(customer));
activeCustomers.clear();
soldJars.length = 0;
shelfJars.forEach(jar => {
jar.visible = true;
jar.scale.setScalar(1);
});
effects.splice(0).forEach(effect => scene.remove(effect.mesh));
popups.splice(0);
cart.visible = false;
orderParcel.visible = false;
routeFill.style.width = "0%";
routeLabel.textContent = "0%";
logList.innerHTML = "";
updateUI();
}
const speedButton = document.getElementById("speedButton");
const speedOptions = [1, 1.5, 2];
let speedIndex = 0;
speedButton.addEventListener("click", () => {
speedIndex = (speedIndex + 1) % speedOptions.length;
app.speed = speedOptions[speedIndex];
speedButton.textContent = `Speed ${app.speed}×`;
});
document.getElementById("restartButton").addEventListener("click", () => {
clearDemo();
runDemo(app.token);
});
function updateEffects(dt) {
for (let i = effects.length - 1; i >= 0; i--) {
const effect = effects[i];
effect.age += dt * app.speed;
effect.velocity.y -= 6.2 * dt * app.speed;
effect.mesh.position.addScaledVector(effect.velocity, dt * app.speed);
effect.mesh.rotation.x += dt * 6;
effect.mesh.rotation.y += dt * 8;
effect.mesh.scale.setScalar(Math.max(0.01, 1 - effect.age / 0.9));
if (effect.age > 0.9) {
scene.remove(effect.mesh);
effects.splice(i, 1);
}
}
}
function updatePopups(now) {
for (let i = popups.length - 1; i >= 0; i--) {
const popup = popups[i];
const t = Math.min(1, (now - popup.start) * app.speed / 420);
const c1 = 1.70158;
const c3 = c1 + 1;
const scale = 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2);
popup.object.scale.setScalar(Math.max(0.01, scale));
if (t >= 1) {
popup.object.scale.setScalar(1);
popups.splice(i, 1);
}
}
}
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(0.05, (now - (animate.lastTime || now)) / 1000);
animate.lastTime = now;
updateCamera();
updateEffects(dt);
updatePopups(now);
if (orderParcel.visible) {
const pulse = 1 + Math.sin(now * 0.005) * 0.045;
orderParcel.scale.set(pulse, 1 / pulse, pulse);
}
if (app.phase === "pending") {
const seconds = Math.max(0, (app.pendingEndsAt - now) / 1000);
orderDetail.textContent =
`Three-jar threshold reached. Delayed dispatch in ${seconds.toFixed(1)} seconds.`;
}
cartWheels.forEach((wheel, index) => {
wheel.rotation.x += dt * app.speed * (index % 2 ? 3 : -3);
});
renderer.render(scene, camera);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateUI();
updateCamera();
requestAnimationFrame(animate);
runDemo(app.token);
</script>
</body>
</html>
```