Prompt specification
Build or customize the Three.js 3D scene "Market basket inventory scene" 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: Market basket inventory scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/market-basket-inventory-scene-x1707
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: market basket inventory scene. Compose a small stall with baskets and item tokens moving through a counting station; preserve object identities so additions and removals update the count correctly. Return one self-contained HTML file.
Working Three.js Starter (market-basket-inventory-scene-x1707):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Market — Basket Inventory</title>
<style>
:root {
--ink: #173d35;
--muted: #6e7f75;
--paper: #fff8e9;
--cream: #f4e7ce;
--coral: #e96f51;
--gold: #e5b94f;
--mint: #78b69d;
--line: rgba(23, 61, 53, 0.16);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eadcc4;
color: var(--ink);
font-family: "Courier New", Courier, monospace;
}
#app, canvas {
width: 100%;
height: 100%;
display: block;
}
canvas {
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.brand {
position: fixed;
top: 22px;
left: 24px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 0 rgba(255,255,255,.5);
}
.brand-mark {
display: flex;
align-items: center;
gap: 10px;
font-size: 12px;
font-weight: 800;
letter-spacing: 2px;
}
.brand-cube {
width: 18px;
height: 18px;
background: var(--coral);
border: 3px solid var(--ink);
box-shadow: 4px 4px 0 var(--gold);
}
.brand small {
display: block;
margin-top: 8px;
color: var(--muted);
font-size: 10px;
letter-spacing: 1px;
}
.panel {
position: fixed;
z-index: 10;
top: 20px;
right: 20px;
width: 340px;
max-height: calc(100vh - 40px);
overflow: auto;
padding: 20px;
background: rgba(255, 248, 233, 0.94);
border: 2px solid var(--ink);
box-shadow: 7px 7px 0 rgba(23, 61, 53, 0.17);
backdrop-filter: blur(8px);
scrollbar-width: thin;
scrollbar-color: var(--mint) transparent;
}
.eyebrow {
margin: 0 0 5px;
color: var(--coral);
font-size: 10px;
font-weight: 800;
letter-spacing: 2px;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 25px;
line-height: 1.05;
letter-spacing: -1px;
}
.count-card {
margin: 18px 0 14px;
padding: 15px 16px;
color: var(--paper);
background: var(--ink);
border: 3px solid var(--ink);
box-shadow: 4px 4px 0 var(--coral);
}
.count-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
}
.count-label {
padding-bottom: 4px;
font-size: 11px;
font-weight: 700;
line-height: 1.45;
letter-spacing: 1px;
text-transform: uppercase;
}
#stockCount {
display: inline-block;
font-size: 46px;
font-weight: 800;
line-height: .9;
color: #ffe3a1;
}
#stockCount.bump { animation: bump .35s steps(3, end); }
@keyframes bump {
0% { transform: translateY(0) scale(1); }
35% { transform: translateY(-5px) scale(1.12); }
100% { transform: translateY(0) scale(1); }
}
.count-meta {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid rgba(255,255,255,.2);
color: #d9e6dc;
font-size: 10px;
}
.type-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
margin-bottom: 16px;
}
.type-chip {
padding: 8px 4px 7px;
text-align: center;
background: #efe3ca;
border: 1px solid var(--line);
font-size: 9px;
font-weight: 700;
text-transform: uppercase;
}
.type-chip b {
display: block;
margin-top: 3px;
font-size: 18px;
}
.type-chip .dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 4px;
border: 1px solid rgba(0,0,0,.25);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
appearance: none;
border: 2px solid var(--ink);
border-radius: 0;
font: 800 10px/1.2 "Courier New", Courier, monospace;
letter-spacing: .4px;
text-transform: uppercase;
cursor: pointer;
transition: transform .08s ease, box-shadow .08s ease, background .08s ease;
}
button:hover:not(:disabled) {
transform: translate(-1px, -1px);
box-shadow: 3px 3px 0 var(--ink);
}
button:active:not(:disabled) {
transform: translate(2px, 2px);
box-shadow: none;
}
button:disabled {
opacity: .38;
cursor: not-allowed;
}
.action {
min-height: 43px;
padding: 9px;
color: var(--ink);
background: #f1dfb9;
}
.action.primary {
color: var(--paper);
background: var(--coral);
}
.action.full {
grid-column: 1 / -1;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin: 20px 0 8px;
padding-top: 15px;
border-top: 2px solid var(--line);
font-size: 11px;
font-weight: 800;
letter-spacing: 1px;
text-transform: uppercase;
}
.section-title span {
color: var(--muted);
font-size: 9px;
font-weight: 400;
letter-spacing: 0;
text-transform: none;
}
#selectedLabel {
min-height: 26px;
margin-bottom: 8px;
padding: 7px 8px;
color: var(--muted);
background: rgba(23,61,53,.06);
border-left: 3px solid var(--mint);
font-size: 9px;
line-height: 1.35;
}
#registry {
max-height: 230px;
overflow-y: auto;
border: 1px solid var(--line);
background: rgba(255,255,255,.36);
scrollbar-width: thin;
}
.item-row {
display: grid;
grid-template-columns: 12px 1fr auto 28px;
align-items: center;
gap: 7px;
min-height: 40px;
padding: 6px 7px;
border-bottom: 1px solid rgba(23,61,53,.09);
cursor: pointer;
font-size: 9px;
}
.item-row:last-child { border-bottom: 0; }
.item-row:hover { background: rgba(120,182,157,.13); }
.item-row.selected { background: rgba(229,185,79,.25); }
.item-dot {
width: 9px;
height: 9px;
border: 1px solid rgba(0,0,0,.3);
}
.item-id { font-weight: 800; }
.item-type {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
text-transform: uppercase;
}
.item-status {
color: var(--muted);
font-size: 7px;
font-weight: 800;
letter-spacing: .5px;
text-transform: uppercase;
}
.item-remove {
width: 25px;
height: 25px;
padding: 0;
color: var(--coral);
background: transparent;
border-color: rgba(233,111,81,.45);
font-size: 14px;
}
.item-remove:hover:not(:disabled) {
box-shadow: 2px 2px 0 var(--coral);
}
.hint {
position: fixed;
z-index: 4;
left: 24px;
bottom: 20px;
padding: 9px 11px;
color: var(--ink);
background: rgba(255,248,233,.82);
border: 1px solid var(--line);
font-size: 9px;
line-height: 1.5;
pointer-events: none;
}
.hint b { color: var(--coral); }
#toast {
position: fixed;
z-index: 20;
left: 50%;
bottom: 26px;
max-width: min(420px, calc(100vw - 40px));
padding: 11px 15px;
color: var(--paper);
background: var(--ink);
border: 2px solid var(--ink);
box-shadow: 4px 4px 0 var(--gold);
font-size: 10px;
font-weight: 700;
text-align: center;
transform: translate(-50%, 20px);
opacity: 0;
pointer-events: none;
transition: opacity .18s ease, transform .18s ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 720px) {
.brand {
top: 12px;
left: 14px;
}
.brand small { display: none; }
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 47vh;
padding: 14px;
}
h1 { font-size: 20px; }
.count-card { margin: 12px 0; padding: 11px 13px; }
#stockCount { font-size: 36px; }
.type-grid { display: none; }
#registry { max-height: 130px; }
.hint { display: none; }
#toast { bottom: calc(47vh + 22px); }
}
</style>
</head>
<body>
<div id="app"></div>
<div class="brand">
<div class="brand-mark"><span class="brand-cube"></span> VOXEL MARKET</div>
<small>BASKET INVENTORY / LIVE COUNTING STATION</small>
</div>
<aside class="panel">
<p class="eyebrow">Station 04 · West Produce</p>
<h1>Basket ledger</h1>
<section class="count-card">
<div class="count-row">
<div class="count-label">Items in stock<br />confirmed at counter</div>
<div id="stockCount">00</div>
</div>
<div class="count-meta">
<span id="trackedLabel">0 OBJECTS TRACKED</span>
<span id="scanCount">0 SCANNED</span>
</div>
</section>
<div class="type-grid" id="typeGrid"></div>
<div class="actions">
<button class="action primary" id="addButton">+ Add stock</button>
<button class="action" id="dispatchButton">Send next item</button>
<button class="action full" id="removeButton" disabled>Remove selected object</button>
</div>
<div class="section-title">
Object registry
<span>exact IDs preserved</span>
</div>
<div id="selectedLabel">Click a token in the scene or select an object below.</div>
<div id="registry"></div>
</aside>
<div class="hint">
<b>DRAG</b> rotate · <b>SCROLL</b> zoom · <b>CLICK</b> select<br />
A add · D dispatch · Delete remove · R reset view
</div>
<div id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
const app = document.getElementById('app');
const stockCountEl = document.getElementById('stockCount');
const trackedLabelEl = document.getElementById('trackedLabel');
const scanCountEl = document.getElementById('scanCount');
const typeGridEl = document.getElementById('typeGrid');
const registryEl = document.getElementById('registry');
const selectedLabelEl = document.getElementById('selectedLabel');
const toastEl = document.getElementById('toast');
const addButton = document.getElementById('addButton');
const dispatchButton = document.getElementById('dispatchButton');
const removeButton = document.getElementById('removeButton');
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeadcc4);
scene.fog = new THREE.Fog(0xeadcc4, 17, 33);
const camera = new THREE.PerspectiveCamera(39, innerWidth / innerHeight, 0.1, 80);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
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;
app.appendChild(renderer.domElement);
const ambient = new THREE.HemisphereLight(0xfff4d6, 0x668276, 2.15);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xfff0ce, 3.2);
sun.position.set(-5, 12, 8);
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 = 35;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa7d8d0, 0.9);
fill.position.set(8, 5, -7);
scene.add(fill);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(color, new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true
}));
}
return materialCache.get(color);
}
function addBox(w, h, d, color, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material(color));
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createTextTexture(text, bg = '#173d35', fg = '#fff2cf') {
const canvas = document.createElement('canvas');
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext('2d');
ctx.fillStyle = bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = fg;
ctx.lineWidth = 8;
ctx.strokeRect(8, 8, canvas.width - 16, canvas.height - 16);
ctx.fillStyle = fg;
ctx.font = '900 48px monospace';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 2, canvas.width - 35);
const texture = new THREE.CanvasTexture(canvas);
texture.minFilter = THREE.NearestFilter;
texture.magFilter = THREE.NearestFilter;
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
function addSign(text, width, height, x, y, z, bg, fg) {
const texture = createTextTexture(text, bg, fg);
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({ map: texture })
);
mesh.position.set(x, y, z);
scene.add(mesh);
return mesh;
}
// Tiled voxel ground.
addBox(17.4, 0.24, 13.2, 0xc8b99d, 0, -0.18, -0.4);
const tileGeometry = new THREE.BoxGeometry(1.08, 0.08, 1.08);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const tileCountX = 15;
const tileCountZ = 10;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCountX * tileCountZ);
tiles.receiveShadow = true;
const tileColors = [0xe4d3b3, 0xddc9a6, 0xe9d9ba, 0xd7c39e];
const dummy = new THREE.Object3D();
let tileIndex = 0;
for (let z = 0; z < tileCountZ; z++) {
for (let x = 0; x < tileCountX; x++) {
dummy.position.set((x - 7) * 1.08, -0.015, (z - 4.5) * 1.08);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
tiles.setColorAt(tileIndex, new THREE.Color(tileColors[(x + z * 3) % tileColors.length]));
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
scene.add(tiles);
// Market stall platform and structure.
addBox(5.8, 0.36, 3.05, 0xb9784e, -3.15, 0.18, 0.3);
addBox(5.8, 0.08, 0.18, 0xe3ae70, -3.15, 0.4, 1.84);
addBox(5.55, 2.65, 0.16, 0xd99d63, -3.15, 1.73, -1.18);
const postPositions = [
[-5.62, -1.05], [-0.68, -1.05],
[-5.62, 1.68], [-0.68, 1.68]
];
for (const [x, z] of postPositions) {
addBox(0.24, 3.35, 0.24, 0x704833, x, 2.02, z);
addBox(0.34, 0.16, 0.34, 0xf0c478, x, 0.47, z);
}
for (let i = 0; i < 9; i++) {
const color = i % 2 === 0 ? 0xe96f51 : 0xffe6bb;
addBox(0.66, 0.18, 3.05, color, -5.83 + i * 0.66, 3.73, 0.3);
addBox(0.66, 0.28, 0.14, color, -5.83 + i * 0.66, 3.53, 1.87);
}
addSign('MARKET / COUNT', 3.35, 0.78, -3.15, 3.08, 1.94, '#173d35', '#ffe8b4');
// Decorative back-wall produce blocks.
const wallProduce = [
[-4.95, 2.15, 0xb94d3e], [-4.48, 2.15, 0xe7a83d],
[-3.95, 2.15, 0x6f9c52], [-3.42, 2.15, 0xc35f3c],
[-2.9, 2.15, 0xe7b346], [-2.37, 2.15, 0x789d4f]
];
for (const [x, y, color] of wallProduce) {
addBox(0.32, 0.32, 0.28, color, x, y, -1.03);
}
function createBasket(x, z, baseY, width = 1.15, depth = 0.94, color = 0xa86f3e) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
const height = 0.62;
addBox(width, 0.12, depth, color, 0, baseY + 0.06, 0, group);
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
addBox(0.09, height, 0.09, color,
sx * (width / 2 - 0.045), baseY + height / 2, sz * (depth / 2 - 0.045), group);
}
}
for (const level of [0.3, 0.69]) {
for (const sz of [-1, 1]) {
addBox(width, 0.075, 0.075, color,
0, baseY + height * level, sz * (depth / 2 - 0.035), group);
}
for (const sx of [-1, 1]) {
addBox(0.075, 0.075, depth, color,
sx * (width / 2 - 0.035), baseY + height * level, 0, group);
}
}
return group;
}
const sourceBasketXs = [-4.55, -3.25, -1.95];
sourceBasketXs.forEach((x, i) => {
createBasket(x, 0.72, 0.36, 1.13, 0.92, i % 2 ? 0xbd7e45 : 0xa96d3d);
});
// Conveyor belt.
const beltStart = -1.45;
const beltEnd = 2.85;
const beltLength = beltEnd - beltStart;
const beltCenter = (beltStart + beltEnd) / 2;
addBox(beltLength + 0.25, 0.18, 0.92, 0x526a61, beltCenter, 0.68, 0.45);
addBox(beltLength, 0.08, 0.78, 0x263f39, beltCenter, 0.82, 0.45);
for (const x of [-1.05, 0.45, 2.35]) {
addBox(0.18, 0.58, 0.62, 0x6d7770, x, 0.29, 0.45);
addBox(0.38, 0.08, 0.72, 0x394f48, x, 0.07, 0.45);
}
const beltSlats = [];
for (let i = 0; i < 15; i++) {
const slat = addBox(0.055, 0.035, 0.71, i % 2 ? 0x789188 : 0x648077,
beltStart + i * 0.29, 0.875, 0.45);
beltSlats.push(slat);
}
// Counting station.
const scannerX = 1.68;
addBox(0.28, 2.15, 0.28, 0x264b43, scannerX, 1.72, -0.43);
addBox(0.28, 2.15, 0.28, 0x264b43, scannerX, 1.72, 1.33);
addBox(0.32, 0.28, 2.05, 0x264b43, scannerX, 2.78, 0.45);
addBox(0.4, 0.12, 2.18, 0xe4b84f, scannerX, 2.98, 0.45);
const scannerGlassMaterial = new THREE.MeshStandardMaterial({
color: 0x73dfc7,
emissive: 0x2f9c86,
emissiveIntensity: 0.65,
transparent: true,
opacity: 0.16,
roughness: 0.3,
depthWrite: false
});
const scannerGlass = new THREE.Mesh(
new THREE.BoxGeometry(0.07, 1.72, 1.38),
scannerGlassMaterial
);
scannerGlass.position.set(scannerX, 1.75, 0.45);
scene.add(scannerGlass);
const scanLineMaterial = new THREE.MeshBasicMaterial({
color: 0xb8fff0,
transparent: true,
opacity: 0.9
});
const scanLine = new THREE.Mesh(new THREE.BoxGeometry(0.075, 0.035, 0.78), scanLineMaterial);
scanLine.position.set(scannerX, 0.91, 0.45);
scene.add(scanLine);
const scanPulseMaterial = new THREE.MeshBasicMaterial({
color: 0x75f2d0,
transparent: true,
opacity: 0.35,
depthWrite: false
});
const scanPulse = new THREE.Group();
scanPulse.position.x = scannerX;
addBox(0.035, 0.035, 1.48, 0xffffff, 0, 0.84, 0, scanPulse);
addBox(0.035, 0.035, 1.48, 0xffffff, 0, 2.62, 0, scanPulse);
addBox(0.035, 1.78, 0.035, 0xffffff, 0, 1.73, -0.72, scanPulse);
addBox(0.035, 1.78, 0.035, 0xffffff, 0, 1.73, 1.62, scanPulse);
scanPulse.traverse(child => {
if (child.isMesh) {
child.material = scanPulseMaterial;
child.castShadow = false;
child.receiveShadow = false;
}
});
scene.add(scanPulse);
const scannerLampMaterial = new THREE.MeshStandardMaterial({
color: 0x8be5b4,
emissive: 0x3cc77d,
emissiveIntensity: 1.1,
roughness: 0.5
});
addBox(0.18, 0.18, 0.11, 0xffffff, scannerX, 2.77, 1.51).material = scannerLampMaterial;
addSign('COUNT', 1.25, 0.42, scannerX, 3.25, 1.12, '#264b43', '#ffe8b4');
// Receiving baskets.
createBasket(3.88, 1.20, 0.08, 1.62, 1.18, 0x6f9b70);
createBasket(3.88, -0.65, 0.08, 1.62, 1.18, 0x8b704b);
// Small voxel tree and crates.
addBox(0.42, 1.6, 0.42, 0x805437, -6.25, 0.8, 2.7);
const leafColors = [0x507b4e, 0x638f55, 0x76a45d];
const leafBlocks = [
[-6.25, 1.85, 2.7, 1.35, 0.85, 1.25],
[-6.65, 1.55, 2.65, 0.9, 0.75, 0.9],
[-5.85, 1.55, 2.72, 0.9, 0.75, 0.9],
[-6.25, 1.55, 3.18, 0.9, 0.75, 0.8],
[-6.25, 1.55, 2.2, 0.9, 0.75, 0.8]
];
leafBlocks.forEach((b, i) => addBox(b[3], b[4], b[5], leafColors[i % 3], b[0], b[1], b[2]));
for (let i = 0; i < 3; i++) {
addBox(0.62, 0.48, 0.62, i % 2 ? 0xb77a47 : 0x9f683d,
5.5 + (i % 2) * 0.72, 0.24 + Math.floor(i / 2) * 0.48, -2.65);
}
const itemTypes = {
apple: { label: 'Apple', code: 'AP', color: 0xd94f3d, css: '#d94f3d' },
carrot: { label: 'Carrot', code: 'CR', color: 0xed8a32, css: '#ed8a32' },
cheese: { label: 'Cheese', code: 'CH', color: 0xe7bd45, css: '#e7bd45' },
bread: { label: 'Bread', code: 'BR', color: 0xb9763e, css: '#b9763e' }
};
const typeOrder = Object.keys(itemTypes);
const items = new Map();
const removalItems = [];
const pickables = [];
let serial = 100;
let sourceIndex = 0;
let addCounter = 0;
let scannedThisSession = 0;
let selectedId = null;
function buildItemMesh(type) {
const group = new THREE.Group();
const color = itemTypes[type].color;
if (type === 'apple') {
addBox(0.33, 0.30, 0.31, color, 0, 0, 0, group);
addBox(0.07, 0.12, 0.07, 0x735033, 0, 0.2, 0, group);
addBox(0.14, 0.045, 0.08, 0x5f9148, 0.055, 0.25, 0, group);
} else if (type === 'carrot') {
addBox(0.18, 0.34, 0.18, color, 0, -0.02, 0, group);
addBox(0.09, 0.18, 0.08, 0x5f9148, 0, 0.23, 0, group);
addBox(0.08, 0.13, 0.08, 0x79a951, 0.055, 0.2, 0, group);
addBox(0.08, 0.13, 0.08, 0x79a951, -0.055, 0.2, 0, group);
} else if (type === 'cheese') {
addBox(0.38, 0.24, 0.3, color, 0, 0, 0, group);
addBox(0.065, 0.065, 0.025, 0xc58e28, -0.09, 0.01, 0.158, group);
addBox(0.05, 0.05, 0.025, 0xc58e28, 0.08, -0.04, 0.158, group);
addBox(0.05, 0.025, 0.05, 0xc58e28, 0.06, 0.128, 0.04, group);
} else {
addBox(0.37, 0.25, 0.31, color, 0, 0, 0, group);
addBox(0.3, 0.09, 0.27, 0xd39a55, 0, 0.16, 0, group);
addBox(0.04, 0.18, 0.025, 0xe0ad69, 0.09, 0.01, 0.162, group);
addBox(0.04, 0.13, 0.025, 0xe0ad69, -0.08, 0.01, 0.162, group);
}
group.traverse(child => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
child.userData.activePick = true;
pickables.push(child);
}
});
return group;
}
function supplyPosition(index, target) {
const perBasket = 2;
const basket = Math.floor(index / perBasket);
const slot = index % perBasket;
const x = sourceBasketXs[basket] + (slot === 0 ? -0.23 : 0.23);
const z = 0.72 + (basket % 2 === 0 ? -0.16 : 0.16);
return target.set(x, 0.72, z);
}
function outputPosition(index, target) {
const crate = Math.floor(index / 16);
const within = index % 16;
const col = within % 4;
const row = Math.floor(within / 4);
const zCenter = crate === 0 ? 1.2 : -0.65;
return target.set(
3.88 + (col - 1.5) * 0.38,
0.45,
zCenter + (row - 1.5) * 0.28
);
}
function createItem(type, counted = false, stocked = false) {
serial += 1;
const id = `${itemTypes[type].code}-${String(serial).padStart(3, '0')}`;
const mesh = buildItemMesh(type);
const item = {
id,
type,
mesh,
counted,
state: stocked ? 'stocked' : counted ? 'settling' : 'supply',
createdAt: performance.now(),
motionTime: 0,
motionPhase: '',
startPosition: new THREE.Vector3(),
targetPosition: new THREE.Vector3(),
outputIndex: 0,
removeTime: 0
};
mesh.userData.itemId = id;
scene.add(mesh);
if (counted) {
outputPosition(0, item.targetPosition);
mesh.position.copy(item.targetPosition);
if (!stocked) {
mesh.position.y += 1.1;
mesh.scale.setScalar(0.15);
}
} else {
supplyPosition(sourceIndex++, item.targetPosition);
mesh.position.copy(item.targetPosition);
}
items.set(id, item);
return item;
}
function layoutCountedItems() {
const stocked = [...items.values()]
.filter(item => item.counted && item.state !== 'removing')
.sort((a, b) => a.createdAt - b.createdAt);
stocked.forEach((item, index) => {
item.outputIndex = index;
outputPosition(index, item.targetPosition);
if (item.state === 'stocked') {
item.mesh.position.copy(item.targetPosition);
}
});
}
// Initial inventory: eight confirmed items and six incoming basket items.
['apple', 'carrot', 'cheese', 'bread', 'apple', 'carrot', 'cheese', 'bread']
.forEach(type => createItem(type, true, true));
['apple', 'carrot', 'cheese', 'bread', 'carrot', 'bread']
.forEach(type => createItem(type, false, false));
layoutCountedItems();
const selectionHelper = new THREE.BoxHelper(new THREE.Object3D(), 0xffd35c);
selectionHelper.visible = false;
selectionHelper.material.depthTest = false;
selectionHelper.material.transparent = true;
selectionHelper.material.opacity = 0.95;
selectionHelper.renderOrder = 20;
scene.add(selectionHelper);
function countStock() {
let count = 0;
for (const item of items.values()) if (item.counted) count += 1;
return count;
}
function typeCounts() {
const result = { apple: 0, carrot: 0, cheese: 0, bread: 0 };
for (const item of items.values()) {
if (item.counted) result[item.type] += 1;
}
return result;
}
function itemStatus(item) {
if (item.state === 'moving') return item.counted ? 'CROSSING' : 'TO SCAN';
if (item.state === 'supply') return 'INCOMING';
if (item.state === 'settling') return 'STOCKED';
return 'IN STOCK';
}
function renderInventory() {
const stock = countStock();
const counts = typeCounts();
stockCountEl.textContent = String(stock).padStart(2, '0');
trackedLabelEl.textContent = `${items.size} OBJECT${items.size === 1 ? '' : 'S'} TRACKED`;
scanCountEl.textContent = `${scannedThisSession} SCANNED`;
typeGridEl.innerHTML = typeOrder.map(type => `
<div class="type-chip">
<span class="dot" style="background:${itemTypes[type].css}"></span>${itemTypes[type].label}
<b>${counts[type]}</b>
</div>
`).join('');
const hasIncoming = [...items.values()].some(item => item.state === 'supply');
dispatchButton.disabled = !hasIncoming;
removeButton.disabled = !selectedId || !items.has(selectedId);
const sorted = [...items.values()].sort((a, b) => b.createdAt - a.createdAt);
registryEl.innerHTML = sorted.map(item => `
<div class="item-row ${item.id === selectedId ? 'selected' : ''}" data-id="${item.id}">
<span class="item-dot" style="background:${itemTypes[item.type].css}"></span>
<span>
<span class="item-id">${item.id}</span>
<span class="item-type">${itemTypes[item.type].label}</span>
</span>
<span class="item-status">${itemStatus(item)}</span>
<button class="item-remove" data-remove="${item.id}" aria-label="Remove ${item.id}">×</button>
</div>
`).join('');
const selected = selectedId ? items.get(selectedId) : null;
selectedLabelEl.textContent = selected
? `SELECTED ${selected.id} · ${itemTypes[selected.type].label} · ${itemStatus(selected)}`
: 'Click a token in the scene or select an object below.';
}
function pulseCount() {
stockCountEl.classList.remove('bump');
void stockCountEl.offsetWidth;
stockCountEl.classList.add('bump');
}
let toastTimer;
function toast(message) {
toastEl.textContent = message;
toastEl.classList.add('visible');
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toastEl.classList.remove('visible'), 2200);
}
function selectItem(id) {
if (!items.has(id)) return;
selectedId = id;
selectionHelper.visible = true;
selectionHelper.setFromObject(items.get(id).mesh);
renderInventory();
}
function removeItem(id) {
const item = items.get(id);
if (!item) return;
const wasCounted = item.counted;
item.counted = false;
item.state = 'removing';
item.removeTime = 0;
item.mesh.traverse(child => {
if (child.isMesh) child.userData.activePick = false;
});
items.delete(id);
removalItems.push(item);
if (selectedId === id) {
selectedId = null;
selectionHelper.visible = false;
}
layoutCountedItems();
renderInventory();
if (wasCounted) pulseCount();
toast(`${id} removed${wasCounted ? ' · stock count decreased' : ' from incoming basket'}.`);
}
function triggerScan(item) {
if (item.counted) return;
item.counted = true;
scannedThisSession += 1;
layoutCountedItems();
renderInventory();
pulseCount();
scannerLampMaterial.emissiveIntensity = 3;
toast(`${item.id} scanned · confirmed in stock.`);
}
function dispatchNext() {
const item = [...items.values()].find(candidate => candidate.state === 'supply');
if (!item) {
toast('The incoming baskets are empty.');
return;
}
item.state = 'moving';
item.motionPhase = 'approach';
item.motionTime = 0;
item.startPosition.copy(item.mesh.position);
renderInventory();
toast(`${item.id} is moving to the counting station.`);
}
function addStock() {
const type = typeOrder[addCounter % typeOrder.length];
addCounter += 1;
const item = createItem(type, true, false);
item.state = 'settling';
item.motionTime = 0;
layoutCountedItems();
renderInventory();
pulseCount();
selectItem(item.id);
toast(`${item.id} added · stock count increased.`);
}
addButton.addEventListener('click', addStock);
dispatchButton.addEventListener('click', dispatchNext);
removeButton.addEventListener('click', () => {
if (selectedId) removeItem(selectedId);
});
registryEl.addEventListener('click', event => {
const removeButtonTarget = event.target.closest('[data-remove]');
if (removeButtonTarget) {
event.stopPropagation();
removeItem(removeButtonTarget.dataset.remove);
return;
}
const row = event.target.closest('[data-id]');
if (row) selectItem(row.dataset.id);
});
// Direct camera controls; no examples/addons are used.
const cameraTarget = new THREE.Vector3(0.15, 1.35, 0.15);
let cameraTheta = 0.57;
let cameraPhi = 1.03;
let cameraRadius = 12.6;
let drag = null;
function updateCamera() {
camera.position.set(
cameraTarget.x + cameraRadius * Math.sin(cameraPhi) * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * Math.sin(cameraPhi) * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0.57;
cameraPhi = 1.03;
cameraRadius = 12.6;
updateCamera();
}
renderer.domElement.addEventListener('pointerdown', event => {
if (event.button !== 0) return;
drag = { x: event.clientX, y: event.clientY, moved: false };
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener('pointermove', event => {
if (!drag) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
if (Math.abs(dx) + Math.abs(dy) > 4) drag.moved = true;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi - dy * 0.005, 0.52, 1.35);
drag.x = event.clientX;
drag.y = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener('pointerup', event => {
if (drag && !drag.moved) {
const rect = renderer.domElement.getBoundingClientRect();
const pointer = new THREE.Vector2(
((event.clientX - rect.left) / rect.width) * 2 - 1,
-((event.clientY - rect.top) / rect.height) * 2 + 1
);
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(pointer, camera);
const activePickables = pickables.filter(mesh => mesh.userData.activePick);
const hits = raycaster.intersectObjects(activePickables, false);
if (hits.length) {
let node = hits[0].object;
while (node && !node.userData.itemId) node = node.parent;
if (node && node.userData.itemId) selectItem(node.userData.itemId);
} else {
selectedId = null;
selectionHelper.visible = false;
renderInventory();
}
}
drag = null;
});
renderer.domElement.addEventListener('pointercancel', () => { drag = null; });
renderer.domElement.addEventListener('wheel', event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(cameraRadius + event.deltaY * 0.008, 7.4, 18);
updateCamera();
}, { passive: false });
document.addEventListener('keydown', event => {
if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement) return;
if (event.key.toLowerCase() === 'a') addStock();
if (event.key.toLowerCase() === 'd') dispatchNext();
if (event.key.toLowerCase() === 'r') resetCamera();
if ((event.key === 'Delete' || event.key === 'Backspace') && selectedId) {
event.preventDefault();
removeItem(selectedId);
}
});
const beltPosition = new THREE.Vector3(-1.35, 1.04, 0.45);
const scannerPosition = new THREE.Vector3(scannerX, 1.04, 0.45);
const temp = new THREE.Vector3();
const clock = new THREE.Clock();
function easeOutCubic(t) {
return 1 - Math.pow(1 - t, 3);
}
function animateItems(dt) {
for (const item of items.values()) {
if (item.state === 'settling') {
item.motionTime += dt;
const t = Math.min(item.motionTime / 0.55, 1);
const e = easeOutCubic(t);
item.mesh.position.lerpVectors(
new THREE.Vector3(item.targetPosition.x, item.targetPosition.y + 1.1, item.targetPosition.z),
item.targetPosition,
e
);
item.mesh.scale.setScalar(0.15 + 0.85 * e);
if (t >= 1) {
item.state = 'stocked';
item.mesh.scale.setScalar(1);
renderInventory();
}
} else if (item.state === 'moving') {
item.motionTime += dt;
item.mesh.rotation.y += dt * 0.65;
if (item.motionPhase === 'approach') {
const t = Math.min(item.motionTime / 0.78, 1);
const e = easeOutCubic(t);
temp.lerpVectors(item.startPosition, beltPosition, e);
temp.y += Math.sin(t * Math.PI) * 0.38;
item.mesh.position.copy(temp);
if (t >= 1) {
item.motionPhase = 'belt';
item.motionTime = 0;
item.startPosition.copy(item.mesh.position);
}
} else if (item.motionPhase === 'belt') {
const t = Math.min(item.motionTime / 2.65, 1);
temp.lerpVectors(beltPosition, scannerPosition, t);
temp.y += Math.sin(t * Math.PI) * 0.025;
item.mesh.position.copy(temp);
if (t >= 1) {
triggerScan(item);
item.motionPhase = 'after';
item.motionTime = 0;
item.startPosition.copy(item.mesh.position);
}
} else if (item.motionPhase === 'after') {
const t = Math.min(item.motionTime / 1.05, 1);
const e = easeOutCubic(t);
temp.lerpVectors(scannerPosition, item.targetPosition, e);
temp.y += Math.sin(t * Math.PI) * 0.24;
item.mesh.position.copy(temp);
if (t >= 1) {
item.state = 'stocked';
item.mesh.position.copy(item.targetPosition);
renderInventory();
}
}
}
}
for (let i = removalItems.length - 1; i >= 0; i--) {
const item = removalItems[i];
item.removeTime += dt;
const t = Math.min(item.removeTime / 0.32, 1);
item.mesh.scale.setScalar(Math.max(0.001, 1 - t));
item.mesh.rotation.y += dt * 5;
item.mesh.position.y += dt * 0.55;
if (t >= 1) {
scene.remove(item.mesh);
item.mesh.traverse(child => {
if (child.isMesh) child.geometry.dispose();
});
removalItems.splice(i, 1);
}
}
}
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
for (const slat of beltSlats) {
slat.position.x -= dt * 0.54;
if (slat.position.x < beltStart) slat.position.x += beltLength;
}
scannerGlassMaterial.opacity = 0.12 + (Math.sin(time * 3.2) + 1) * 0.035;
scanPulseMaterial.opacity = 0.18 + (Math.sin(time * 3.2) + 1) * 0.12;
const pulseScale = 1 + (Math.sin(time * 3.2) + 1) * 0.025;
scanPulse.scale.set(pulseScale, pulseScale, pulseScale);
scannerLampMaterial.emissiveIntensity +=
((Math.sin(time * 3.2) > 0.5 ? 2.3 : 0.8) - scannerLampMaterial.emissiveIntensity) * 0.12;
animateItems(dt);
if (selectedId && items.has(selectedId)) {
selectionHelper.setFromObject(items.get(selectedId).mesh);
} else {
selectionHelper.visible = false;
}
renderer.render(scene, camera);
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
window.addEventListener('resize', resize);
updateCamera();
renderInventory();
animate();
</script>
</body>
</html>
```