Prompt specification
Build or customize the Three.js 3D scene "Returning Shopping Carts" 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: Returning Shopping Carts
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/returning-shopping-carts-x3270
Scene Description Prompt:
Animate returning a voxel shopping cart from a blocked walkway to its marked bay. Build standalone HTML using Three.js from cdnjs.
Working Three.js Starter (returning-shopping-carts-x3270):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Return Bay</title>
<style>
:root {
--ink: #18302d;
--muted: #64736f;
--panel: rgba(249, 252, 248, 0.9);
--green: #22a57a;
--orange: #ef7445;
--border: rgba(24, 48, 45, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8eeea;
color: var(--ink);
font-family:
ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.top-left {
top: 24px;
left: 24px;
max-width: min(440px, calc(100vw - 48px));
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
padding: 7px 10px;
border: 1px solid var(--border);
border-radius: 7px;
background: rgba(249, 252, 248, 0.78);
backdrop-filter: blur(12px);
color: #41625b;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
}
.live-dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--green);
box-shadow: 0 0 0 4px rgba(34, 165, 122, 0.13);
animation: blink 1.8s ease-in-out infinite;
}
@keyframes blink {
50% {
opacity: 0.45;
}
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.02;
letter-spacing: -0.055em;
text-shadow: 0 2px 12px rgba(255, 255, 255, 0.6);
}
.subtitle {
max-width: 390px;
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.65;
}
.controls {
top: 24px;
right: 24px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
min-height: 42px;
padding: 0 14px;
border: 1px solid var(--border);
border-radius: 8px;
background: var(--panel);
color: var(--ink);
box-shadow: 0 8px 28px rgba(30, 55, 49, 0.1);
backdrop-filter: blur(12px);
font: 800 11px/1 ui-monospace, monospace;
letter-spacing: 0.04em;
cursor: pointer;
transition:
transform 160ms ease,
background 160ms ease,
opacity 160ms ease;
}
button:hover {
transform: translateY(-1px);
background: #fff;
}
button:active {
transform: translateY(1px);
}
button:disabled {
cursor: default;
opacity: 0.45;
transform: none;
}
button.primary {
border-color: #178963;
background: #168b68;
color: white;
}
button.primary:hover {
background: #117b5c;
}
.status-panel {
left: 24px;
right: 24px;
bottom: 24px;
display: grid;
grid-template-columns: minmax(250px, 1fr) minmax(240px, 0.7fr);
gap: 18px;
padding: 16px 18px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 15px 45px rgba(30, 55, 49, 0.13);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.status-main {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.status-icon {
display: grid;
flex: 0 0 42px;
width: 42px;
height: 42px;
place-items: center;
border-radius: 9px;
background: #dcefe8;
color: #168b68;
font-size: 20px;
font-weight: 900;
}
.status-copy {
min-width: 0;
}
.status-label {
margin-bottom: 5px;
color: #73817d;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#statusText {
overflow: hidden;
color: var(--ink);
font-size: 13px;
font-weight: 800;
text-overflow: ellipsis;
white-space: nowrap;
}
.progress-wrap {
display: flex;
flex-direction: column;
justify-content: center;
gap: 8px;
}
.progress-meta {
display: flex;
justify-content: space-between;
color: #73817d;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
}
.progress-track {
position: relative;
height: 8px;
overflow: hidden;
border-radius: 3px;
background: #dce4df;
}
.progress-fill {
position: absolute;
inset: 0;
transform: scaleX(0);
transform-origin: left center;
background: linear-gradient(90deg, #168b68, #44c496);
}
.camera-hint {
right: 28px;
bottom: 112px;
padding: 7px 9px;
border-radius: 6px;
background: rgba(249, 252, 248, 0.72);
color: #60706b;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
}
@media (max-width: 700px) {
.top-left {
top: 16px;
left: 16px;
}
.subtitle {
display: none;
}
.controls {
top: 16px;
right: 16px;
flex-direction: column;
}
button {
min-height: 36px;
padding: 0 10px;
font-size: 9px;
}
.status-panel {
left: 12px;
right: 12px;
bottom: 12px;
grid-template-columns: 1fr;
gap: 12px;
padding: 13px;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="hud top-left">
<div class="eyebrow">
<span class="live-dot"></span>
FACILITY FLOW / ROUTE 04
</div>
<h1>Return to the marked bay.</h1>
<p class="subtitle">
A voxel cart is rerouting from the blocked walkway and returning to its
assigned parking bay.
</p>
</header>
<nav class="hud controls" aria-label="Animation controls">
<button id="resetView" type="button">RESET VIEW</button>
<button id="pause" type="button">PAUSE</button>
<button id="replay" class="primary" type="button">REPLAY ROUTE</button>
</nav>
<div class="hud camera-hint">DRAG TO ORBIT · SCROLL TO ZOOM · DOUBLE-CLICK TO RESET</div>
<section class="hud status-panel" aria-live="polite">
<div class="status-main">
<div class="status-icon" id="statusIcon">↗</div>
<div class="status-copy">
<div class="status-label">Current status</div>
<div id="statusText">Cart detected in blocked walkway</div>
</div>
</div>
<div class="progress-wrap">
<div class="progress-meta">
<span>RETURN ROUTE</span>
<span id="progressValue">0%</span>
</div>
<div class="progress-track">
<div class="progress-fill" id="progressFill"></div>
</div>
</div>
</section>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, and camera
// -------------------------------------------------------------------------
const app = document.querySelector("#app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8eeea);
scene.fog = new THREE.Fog(0xe8eeea, 30, 58);
const camera = new THREE.OrthographicCamera(-15, 15, 10, -10, 0.1, 100);
const cameraState = {
azimuth: 0.62,
polar: 0.88,
viewHeight: 15,
target: new THREE.Vector3(0, 0.7, -0.7)
};
const defaultCamera = { ...cameraState };
function updateCamera() {
const { azimuth, polar, target } = cameraState;
const radius = 32;
camera.position.set(
target.x + radius * Math.sin(polar) * Math.sin(azimuth),
target.y + radius * Math.cos(polar),
target.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(target);
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
const effectiveHeight = Math.max(cameraState.viewHeight, 20 / aspect);
camera.left = (-effectiveHeight * aspect) / 2;
camera.right = (effectiveHeight * aspect) / 2;
camera.top = effectiveHeight / 2;
camera.bottom = -effectiveHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
updateCamera();
resize();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf7fff8, 0x7d9188, 2.35));
const sun = new THREE.DirectionalLight(0xfff3dc, 3.1);
sun.position.set(-10, 18, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -19;
sun.shadow.camera.right = 19;
sun.shadow.camera.top = 17;
sun.shadow.camera.bottom = -17;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbde8e3, 1.1);
fill.position.set(13, 8, -10);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and voxel helper functions
// -------------------------------------------------------------------------
const palette = {
floor: new THREE.MeshStandardMaterial({ color: 0xdce4df, roughness: 0.95 }),
floorAlt: new THREE.MeshStandardMaterial({ color: 0xd4ddd7, roughness: 0.95 }),
wall: new THREE.MeshStandardMaterial({ color: 0xd9e2dc, roughness: 0.95 }),
frame: new THREE.MeshStandardMaterial({ color: 0x38534e, roughness: 0.72 }),
dark: new THREE.MeshStandardMaterial({ color: 0x233b38, roughness: 0.8 }),
rubber: new THREE.MeshStandardMaterial({ color: 0x172320, roughness: 0.9 }),
metal: new THREE.MeshStandardMaterial({ color: 0xaebbb5, roughness: 0.48, metalness: 0.45 }),
cream: new THREE.MeshStandardMaterial({ color: 0xf2efe2, roughness: 0.7 }),
orange: new THREE.MeshStandardMaterial({ color: 0xed7442, roughness: 0.7 }),
orangeLight: new THREE.MeshStandardMaterial({ color: 0xffa34f, roughness: 0.65 }),
green: new THREE.MeshStandardMaterial({ color: 0x1a9a72, roughness: 0.65 }),
greenLight: new THREE.MeshStandardMaterial({
color: 0x55d2a3,
roughness: 0.55,
emissive: 0x0b563d,
emissiveIntensity: 0.22
}),
red: new THREE.MeshStandardMaterial({ color: 0xd95142, roughness: 0.65 }),
yellow: new THREE.MeshStandardMaterial({ color: 0xf0c84b, roughness: 0.7 }),
blue: new THREE.MeshStandardMaterial({ color: 0x4f8fc4, roughness: 0.7 }),
mint: new THREE.MeshStandardMaterial({ color: 0x8ed6b8, roughness: 0.7 }),
cardboard: new THREE.MeshStandardMaterial({ color: 0xc89562, roughness: 0.95 }),
cardboardLight: new THREE.MeshStandardMaterial({ color: 0xe0b77e, roughness: 0.95 })
};
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function canvasTexture(width, height, draw) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
draw(ctx, width, height);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(4, renderer.capabilities.getMaxAnisotropy());
return texture;
}
function createSign(title, subtitle, width, height, position) {
const group = new THREE.Group();
group.position.set(...position);
scene.add(group);
const texture = canvasTexture(1024, 256, (ctx, w, h) => {
ctx.fillStyle = "#176f58";
ctx.fillRect(0, 0, w, h);
ctx.strokeStyle = "#87e0bd";
ctx.lineWidth = 10;
ctx.strokeRect(10, 10, w - 20, h - 20);
ctx.fillStyle = "#ffffff";
ctx.font = "800 78px monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(title, w / 2, subtitle ? 102 : h / 2);
if (subtitle) {
ctx.fillStyle = "#b9f0d7";
ctx.font = "700 29px monospace";
ctx.fillText(subtitle, w / 2, 185);
}
});
addBox(
group,
[width + 0.12, height + 0.12, 0.12],
[0, 0, -0.07],
palette.dark
);
const face = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({ map: texture })
);
face.position.z = 0.005;
group.add(face);
return group;
}
function createFloorLabel(text, width, depth, position, color = "#1d8063") {
const texture = canvasTexture(768, 192, (ctx, w, h) => {
ctx.clearRect(0, 0, w, h);
ctx.fillStyle = color;
ctx.font = "900 72px monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, w / 2, h / 2);
});
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(width, depth), material);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(...position);
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Floor, grid, walls, and warehouse shelving
// -------------------------------------------------------------------------
addBox(scene, [30, 0.2, 20], [0, -0.12, 0], palette.floor, false);
addBox(scene, [30, 4.2, 0.35], [0, 2.0, -10.0], palette.wall);
addBox(scene, [30, 0.28, 0.5], [0, 0.15, -9.8], palette.frame);
const gridPositions = [];
for (let x = -15; x <= 15; x++) {
gridPositions.push(x, 0.005, -10, x, 0.005, 10);
}
for (let z = -10; z <= 10; z++) {
gridPositions.push(-15, 0.005, z, 15, 0.005, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x73877f,
transparent: true,
opacity: 0.16
})
);
scene.add(grid);
// Voxel windows along the rear wall.
for (let x = -12; x <= 12; x += 6) {
addBox(scene, [3.4, 1.2, 0.08], [x, 2.8, -9.78], palette.mint, false);
addBox(scene, [0.08, 1.25, 0.1], [x, 2.8, -9.7], palette.frame, false);
addBox(scene, [3.45, 0.08, 0.1], [x, 2.8, -9.7], palette.frame, false);
}
let seed = 731;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
const cartonMaterials = [
palette.cardboard,
palette.cardboardLight,
palette.mint,
palette.blue,
palette.yellow
];
function createShelf(x, z, width) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
const depth = 1.35;
for (const px of [-width / 2, width / 2]) {
for (const pz of [-depth / 2, depth / 2]) {
addBox(group, [0.18, 3.25, 0.18], [px, 1.63, pz], palette.frame);
}
}
for (const y of [0.22, 1.22, 2.22]) {
addBox(group, [width + 0.2, 0.14, depth + 0.15], [0, y, 0], palette.orange);
const cartonCount = Math.max(2, Math.floor(width / 1.35));
for (let i = 0; i < cartonCount; i++) {
if (random() < 0.18) continue;
const boxWidth = 0.65 + random() * 0.42;
const boxHeight = 0.55 + random() * 0.35;
const boxDepth = 0.65 + random() * 0.28;
const boxX =
-width / 2 +
0.75 +
(i * (width - 1.5)) / Math.max(1, cartonCount - 1);
addBox(
group,
[boxWidth, boxHeight, boxDepth],
[
boxX + (random() - 0.5) * 0.16,
y + 0.08 + boxHeight / 2,
(random() - 0.5) * 0.12
],
cartonMaterials[Math.floor(random() * cartonMaterials.length)]
);
}
}
}
createShelf(-9.2, -8.55, 6.2);
createShelf(9.6, -8.6, 4.4);
// Structural columns.
for (const x of [-13.5, 0, 13.5]) {
addBox(scene, [0.55, 4.2, 0.55], [x, 2.1, -8.75], palette.frame);
addBox(scene, [0.7, 0.3, 0.7], [x, 0.15, -8.75], palette.cream);
}
// -------------------------------------------------------------------------
// Blocked walkway
// -------------------------------------------------------------------------
addBox(
scene,
[5.1, 0.025, 3.0],
[-6.1, -0.006, 4.0],
new THREE.MeshStandardMaterial({
color: 0xef7445,
transparent: true,
opacity: 0.16,
roughness: 1
}),
false
);
// Hazard stripes in the blocked zone.
for (let i = 0; i < 6; i++) {
const stripe = addBox(
scene,
[0.26, 0.025, 3.15],
[-8.2 + i * 0.82, 0.015, 4.0],
i % 2 === 0 ? palette.orange : palette.orangeLight,
false
);
stripe.rotation.y = -0.55;
}
createFloorLabel("NO PASSAGE", 4.1, 0.85, [-6.1, 0.045, 5.05], "#b84d31");
// Barrier posts.
for (const x of [-8.55, -3.75]) {
addBox(scene, [0.72, 0.12, 0.72], [x, 0.07, 2.75], palette.dark);
addBox(scene, [0.18, 1.05, 0.18], [x, 0.6, 2.75], palette.cream);
}
// Alternating orange and cream barrier blocks.
for (let i = 0; i < 8; i++) {
addBox(
scene,
[0.58, 0.31, 0.18],
[-8.27 + i * 0.6, 0.94, 2.75],
i % 2 === 0 ? palette.orange : palette.cream
);
}
// Small "BLOCKED" board.
const blockedSign = new THREE.Group();
blockedSign.position.set(-6.15, 1.75, 2.73);
scene.add(blockedSign);
const blockedTexture = canvasTexture(512, 192, (ctx, w, h) => {
ctx.fillStyle = "#e7613e";
ctx.fillRect(0, 0, w, h);
ctx.strokeStyle = "#ffe2a2";
ctx.lineWidth = 10;
ctx.strokeRect(9, 9, w - 18, h - 18);
ctx.fillStyle = "#fff7df";
ctx.font = "900 70px monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("BLOCKED", w / 2, h / 2);
});
addBox(blockedSign, [2.5, 0.95, 0.12], [0, 0, -0.07], palette.dark);
const blockedFace = new THREE.Mesh(
new THREE.PlaneGeometry(2.4, 0.85),
new THREE.MeshBasicMaterial({ map: blockedTexture })
);
blockedFace.position.z = 0.005;
blockedSign.add(blockedFace);
// Voxel traffic cones.
function createCone(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
addBox(group, [0.58, 0.09, 0.58], [0, 0.055, 0], palette.dark);
addBox(group, [0.42, 0.22, 0.42], [0, 0.21, 0], palette.orange);
addBox(group, [0.34, 0.12, 0.34], [0, 0.38, 0], palette.cream);
addBox(group, [0.25, 0.2, 0.25], [0, 0.54, 0], palette.orange);
addBox(group, [0.16, 0.08, 0.16], [0, 0.68, 0], palette.orange);
}
createCone(-8.65, 3.55);
createCone(-3.55, 3.45);
createCone(-3.5, 1.75);
// -------------------------------------------------------------------------
// Marked return bay
// -------------------------------------------------------------------------
addBox(
scene,
[4.35, 0.025, 3.75],
[6, -0.003, -3],
new THREE.MeshStandardMaterial({
color: 0x27a77c,
transparent: true,
opacity: 0.14,
roughness: 1
}),
false
);
// Thick green perimeter.
addBox(scene, [4.55, 0.05, 0.13], [6, 0.035, -4.86], palette.green, false);
addBox(scene, [4.55, 0.05, 0.13], [6, 0.035, -1.14], palette.green, false);
addBox(scene, [0.13, 0.05, 3.85], [3.76, 0.035, -3], palette.green, false);
addBox(scene, [0.13, 0.05, 3.85], [8.24, 0.035, -3], palette.green, false);
// Corner blocks make the bay look like a voxel parking target.
for (const x of [3.76, 8.24]) {
for (const z of [-4.86, -1.14]) {
addBox(scene, [0.34, 0.075, 0.34], [x, 0.06, z], palette.greenLight, false);
}
}
createFloorLabel("BAY 04", 3.15, 0.66, [6, 0.055, -1.48], "#19785d");
createFloorLabel("RETURN", 3.5, 0.62, [6, 0.055, -4.38], "#19785d");
// Signposts for the bay.
for (const x of [4.6, 7.4]) {
addBox(scene, [0.15, 2.65, 0.15], [x, 1.33, -5.65], palette.frame);
}
createSign(
"RETURN BAY",
"ASSIGNED STORAGE / 04",
3.55,
0.9,
[6, 2.55, -5.62]
);
// Green beacon beside the sign.
addBox(scene, [0.5, 0.18, 0.5], [8.45, 0.12, -5.55], palette.dark);
addBox(scene, [0.25, 0.55, 0.25], [8.45, 0.45, -5.55], palette.green);
addBox(scene, [0.31, 0.16, 0.31], [8.45, 0.64, -5.55], palette.greenLight);
// -------------------------------------------------------------------------
// Cart route
// -------------------------------------------------------------------------
const route = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-6.0, 0.03, 4.0),
new THREE.Vector3(-2.85, 0.03, 3.72),
new THREE.Vector3(-2.25, 0.03, 1.55),
new THREE.Vector3(0.35, 0.03, 0.05),
new THREE.Vector3(4.35, 0.03, -0.25),
new THREE.Vector3(6.0, 0.03, -1.65),
new THREE.Vector3(6.0, 0.03, -3.0)
],
false,
"catmullrom",
0.28
);
const routeLength = route.getLength();
// Low-poly route studs.
const routeStudMaterial = new THREE.MeshStandardMaterial({
color: 0x53b995,
emissive: 0x154c38,
emissiveIntensity: 0.16,
roughness: 0.8
});
for (let i = 0; i <= 36; i++) {
const p = route.getPointAt(i / 36);
const stud = addBox(
scene,
[0.12, 0.035, 0.12],
[p.x, 0.045, p.z],
routeStudMaterial,
false
);
stud.rotation.y = i * 0.21;
}
// Moving route pulses.
const routePulses = [];
for (let i = 0; i < 7; i++) {
const pulse = addBox(
scene,
[0.17, 0.08, 0.17],
[0, 0.09, 0],
palette.greenLight,
false
);
routePulses.push(pulse);
}
// -------------------------------------------------------------------------
// Voxel shopping cart
// -------------------------------------------------------------------------
const cart = new THREE.Group();
scene.add(cart);
const cartModel = new THREE.Group();
cart.add(cartModel);
// Lower frame and deck.
addBox(cartModel, [1.48, 0.11, 1.68], [0, 0.58, 0], palette.metal);
addBox(cartModel, [1.18, 0.09, 1.35], [0, 0.67, 0], palette.cream);
for (let i = -2; i <= 2; i++) {
addBox(
cartModel,
[0.08, 0.06, 1.35],
[i * 0.26, 0.73, 0],
palette.metal
);
}
// Basket corner uprights.
for (const x of [-0.69, 0.69]) {
for (const z of [-0.73, 0.73]) {
addBox(cartModel, [0.085, 1.18, 0.085], [x, 1.37, z], palette.metal);
}
}
// Horizontal basket rails.
for (const y of [0.96, 1.32, 1.69, 2.02]) {
addBox(cartModel, [1.48, 0.065, 0.065], [0, y, 0.74], palette.metal);
addBox(cartModel, [1.48, 0.065, 0.065], [0, y, -0.74], palette.metal);
addBox(cartModel, [0.065, 0.065, 1.56], [-0.71, y, 0], palette.metal);
addBox(cartModel, [0.065, 0.065, 1.56], [0.71, y, 0], palette.metal);
}
// Vertical basket bars.
for (let i = -2; i <= 2; i++) {
const x = i * 0.29;
addBox(cartModel, [0.052, 1.05, 0.052], [x, 1.49, 0.74], palette.metal);
addBox(cartModel, [0.052, 1.05, 0.052], [x, 1.49, -0.74], palette.metal);
}
for (let i = -1; i <= 1; i++) {
const z = i * 0.48;
addBox(cartModel, [0.052, 1.05, 0.052], [-0.71, 1.49, z], palette.metal);
addBox(cartModel, [0.052, 1.05, 0.052], [0.71, 1.49, z], palette.metal);
}
// Red-orange handle structure.
addBox(cartModel, [0.085, 0.77, 0.085], [-0.64, 1.96, -0.92], palette.red);
addBox(cartModel, [0.085, 0.77, 0.085], [0.64, 1.96, -0.92], palette.red);
addBox(cartModel, [1.55, 0.13, 0.13], [0, 2.36, -0.92], palette.red);
// Small front identification plate.
addBox(cartModel, [0.44, 0.22, 0.06], [0, 1.04, 0.81], palette.green);
// Voxel groceries.
addBox(cartModel, [0.42, 0.42, 0.42], [-0.3, 1.03, 0.22], palette.yellow);
addBox(cartModel, [0.38, 0.58, 0.36], [0.25, 1.1, 0.25], palette.blue);
addBox(cartModel, [0.34, 0.34, 0.42], [-0.08, 1.06, -0.25], palette.red);
addBox(cartModel, [0.32, 0.46, 0.3], [0.48, 1.02, -0.2], palette.mint);
// Four wheels with visible square hubs.
const wheelPivots = [];
function createWheel(x, z, outward) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.28, z);
cartModel.add(pivot);
addBox(pivot, [0.27, 0.4, 0.4], [0, 0, 0], palette.rubber);
addBox(
pivot,
[0.055, 0.17, 0.17],
[outward * 0.155, 0, 0],
palette.cream
);
addBox(
pivot,
[0.06, 0.29, 0.055],
[outward * 0.16, 0, 0],
palette.metal
);
wheelPivots.push(pivot);
}
createWheel(-0.77, -0.59, -1);
createWheel(0.77, -0.59, 1);
createWheel(-0.77, 0.59, -1);
createWheel(0.77, 0.59, 1);
// -------------------------------------------------------------------------
// Animation state
// -------------------------------------------------------------------------
const state = {
progress: 0,
playing: true,
duration: 10.5,
lastTime: performance.now(),
wheelAngle: 0,
lastYaw: 0
};
const initialTangent = route.getTangentAt(0);
state.lastYaw = Math.atan2(initialTangent.x, initialTangent.z);
cart.rotation.y = state.lastYaw;
const statusText = document.querySelector("#statusText");
const statusIcon = document.querySelector("#statusIcon");
const progressFill = document.querySelector("#progressFill");
const progressValue = document.querySelector("#progressValue");
const pauseButton = document.querySelector("#pause");
function smoothStep(value) {
return value * value * (3 - 2 * value);
}
function updateStatus(progress) {
const percent = Math.round(progress * 100);
progressValue.textContent = `${percent}%`;
progressFill.style.transform = `scaleX(${progress})`;
if (progress >= 0.999) {
statusText.textContent = "Cart parked safely in Bay 04";
statusIcon.textContent = "✓";
} else if (progress > 0.78) {
statusText.textContent = "Aligning with the marked return bay";
statusIcon.textContent = "⌖";
} else if (progress > 0.34) {
statusText.textContent = "Following the marked return route";
statusIcon.textContent = "→";
} else if (progress > 0.08) {
statusText.textContent = "Navigating around the blocked barrier";
statusIcon.textContent = "↗";
} else {
statusText.textContent = "Cart detected in the blocked walkway";
statusIcon.textContent = "!";
}
}
function placeCart(progress) {
const eased = smoothStep(progress);
const point = route.getPointAt(eased);
const tangent = route.getTangentAt(eased);
cart.position.copy(point);
const targetYaw = Math.atan2(tangent.x, tangent.z);
const yawDifference = Math.atan2(
Math.sin(targetYaw - state.lastYaw),
Math.cos(targetYaw - state.lastYaw)
);
state.lastYaw += yawDifference;
cart.rotation.y = state.lastYaw;
const speedFactor = 6 * eased * (1 - eased);
const bob = Math.sin(progress * routeLength * 8) * 0.018 * speedFactor;
cartModel.position.y = bob;
cartModel.rotation.z = Math.sin(progress * routeLength * 4.2) * 0.012 * speedFactor;
state.wheelAngle = -(progress * routeLength) / 0.2;
for (const wheel of wheelPivots) {
wheel.rotation.x = state.wheelAngle;
}
}
function replay() {
state.progress = 0;
state.playing = true;
state.lastTime = performance.now();
const initialTangent = route.getTangentAt(0);
state.lastYaw = Math.atan2(initialTangent.x, initialTangent.z);
cart.rotation.y = state.lastYaw;
pauseButton.textContent = "PAUSE";
pauseButton.disabled = false;
placeCart(0);
updateStatus(0);
}
pauseButton.addEventListener("click", () => {
if (state.progress >= 1) return;
state.playing = !state.playing;
pauseButton.textContent = state.playing ? "PAUSE" : "RESUME";
});
document.querySelector("#replay").addEventListener("click", replay);
// -------------------------------------------------------------------------
// Direct camera controls — no Three.js addons
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + dy * 0.005,
0.42,
1.28
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.viewHeight = THREE.MathUtils.clamp(
cameraState.viewHeight * Math.exp(event.deltaY * 0.001),
8,
24
);
resize();
},
{ passive: false }
);
function resetCamera() {
cameraState.azimuth = defaultCamera.azimuth;
cameraState.polar = defaultCamera.polar;
cameraState.viewHeight = defaultCamera.viewHeight;
updateCamera();
resize();
}
document.querySelector("#resetView").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
function animate(now) {
const delta = Math.min((now - state.lastTime) / 1000, 0.05);
state.lastTime = now;
if (state.playing) {
state.progress = Math.min(
1,
state.progress + delta / state.duration
);
placeCart(state.progress);
updateStatus(state.progress);
if (state.progress >= 1) {
state.playing = false;
pauseButton.textContent = "COMPLETE";
pauseButton.disabled = true;
}
}
const time = now * 0.001;
for (let i = 0; i < routePulses.length; i++) {
const pulseProgress = (time * 0.09 + i / routePulses.length) % 1;
const point = route.getPointAt(pulseProgress);
const pulse = routePulses[i];
pulse.position.set(point.x, 0.09 + Math.sin(time * 4 + i) * 0.025, point.z);
const scale = 0.72 + Math.sin(time * 5 + i) * 0.16;
pulse.scale.setScalar(scale);
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
placeCart(0);
updateStatus(0);
requestAnimationFrame(animate);
</script>
</body>
</html>
```