Prompt specification
Build or customize the Three.js 3D scene "One Delivery Blocks a Narrow Path" 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: One Delivery Blocks a Narrow Path
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/one-delivery-blocks-a-narrow-path-x2158
Scene Description Prompt:
Animate a fictional delivery with two possible unloading positions. Compare pedestrian clearance while keeping package count and handling time unchanged. Use Three.js from cdnjs in a single HTML file, with visible voxel blocks.
Working Three.js Starter (one-delivery-blocks-a-narrow-path-x2158):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Delivery Clearance Study</title>
<style>
:root {
--ink: #17212b;
--muted: #647180;
--panel: rgba(249, 251, 252, 0.94);
--line: rgba(23, 33, 43, 0.12);
--orange: #ed6b32;
--orange-soft: #fff0e8;
--green: #15966d;
--green-soft: #e6f7f0;
--blue: #2979c8;
--shadow: 0 12px 35px rgba(23, 33, 43, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8ee;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(255, 255, 255, 0.72);
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#header {
top: 18px;
left: 18px;
width: min(420px, calc(100vw - 36px));
padding: 17px 19px;
border-radius: 16px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
content: "";
background: var(--orange);
box-shadow: 0 0 0 4px var(--orange-soft);
}
h1 {
margin: 0;
font-size: clamp(20px, 2.2vw, 28px);
line-height: 1.08;
letter-spacing: -0.035em;
}
#header p {
margin: 8px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
#comparison {
top: 18px;
right: 18px;
width: 330px;
padding: 16px;
border-radius: 16px;
}
.section-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.locked {
padding: 4px 7px;
border-radius: 999px;
color: var(--green);
background: var(--green-soft);
font-size: 9px;
letter-spacing: 0.05em;
}
.position-card {
position: relative;
width: 100%;
margin-top: 8px;
padding: 12px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 12px;
color: var(--ink);
background: rgba(255, 255, 255, 0.76);
text-align: left;
cursor: pointer;
transition: border-color 160ms, background 160ms, transform 160ms;
}
.position-card:hover {
transform: translateY(-1px);
border-color: rgba(23, 33, 43, 0.26);
}
.position-card.active[data-position="A"] {
border-color: var(--orange);
background: var(--orange-soft);
}
.position-card.active[data-position="B"] {
border-color: var(--green);
background: var(--green-soft);
}
.position-card::after {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 4px;
content: "";
background: transparent;
}
.position-card.active[data-position="A"]::after {
background: var(--orange);
}
.position-card.active[data-position="B"]::after {
background: var(--green);
}
.card-heading {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 10px;
}
.card-name {
font-size: 13px;
font-weight: 850;
}
.card-subtitle {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.clearance-number {
font-size: 22px;
font-weight: 900;
letter-spacing: -0.05em;
}
.clearance-number small {
margin-left: 2px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0;
}
.bar {
height: 6px;
margin-top: 10px;
overflow: hidden;
border-radius: 999px;
background: rgba(23, 33, 43, 0.09);
}
.bar-fill {
height: 100%;
border-radius: inherit;
transition: width 250ms ease;
}
[data-position="A"] .bar-fill {
width: 33.33%;
background: var(--orange);
}
[data-position="B"] .bar-fill {
width: 100%;
background: var(--green);
}
.fixed-metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.fixed-metric {
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.7);
}
.fixed-metric span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.fixed-metric strong {
display: block;
margin-top: 3px;
font-size: 18px;
letter-spacing: -0.03em;
}
.fixed-metric strong small {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0;
}
#insight {
margin: 12px 0 0;
padding: 10px 11px;
border-left: 3px solid var(--green);
border-radius: 4px 9px 9px 4px;
color: #385248;
background: rgba(21, 150, 109, 0.08);
font-size: 10px;
line-height: 1.45;
}
#controls {
bottom: 18px;
left: 50%;
width: min(790px, calc(100vw - 36px));
padding: 13px 14px 11px;
border-radius: 16px;
transform: translateX(-50%);
}
.control-row {
display: flex;
align-items: center;
gap: 10px;
}
.control-label {
margin-right: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
}
.segmented {
display: flex;
gap: 4px;
padding: 4px;
border-radius: 10px;
background: rgba(23, 33, 43, 0.07);
}
.segmented button,
.icon-button {
border: 0;
border-radius: 7px;
color: var(--ink);
background: transparent;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: background 150ms, color 150ms, box-shadow 150ms;
}
.segmented button {
padding: 8px 11px;
}
.segmented button.active[data-position="A"] {
color: #9b3e13;
background: #fff;
box-shadow: 0 2px 7px rgba(23, 33, 43, 0.12);
}
.segmented button.active[data-position="B"] {
color: #0b7552;
background: #fff;
box-shadow: 0 2px 7px rgba(23, 33, 43, 0.12);
}
.divider {
width: 1px;
height: 25px;
margin: 0 2px;
background: var(--line);
}
.icon-button {
padding: 8px 10px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.74);
}
.icon-button:hover {
background: #fff;
}
.spacer {
flex: 1;
}
#phase {
color: var(--muted);
font-size: 10px;
font-weight: 750;
text-align: right;
white-space: nowrap;
}
.timeline {
position: relative;
height: 5px;
margin-top: 12px;
overflow: hidden;
border-radius: 999px;
background: rgba(23, 33, 43, 0.09);
}
#timeline-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--blue);
}
.timeline-meta {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
#hint {
position: fixed;
right: 18px;
bottom: 18px;
z-index: 5;
padding: 7px 10px;
border-radius: 999px;
color: rgba(23, 33, 43, 0.7);
background: rgba(255, 255, 255, 0.78);
box-shadow: 0 5px 20px rgba(23, 33, 43, 0.09);
font-size: 9px;
font-weight: 750;
pointer-events: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 6px;
color: var(--ink);
background: rgba(255, 255, 255, 0.86);
box-shadow: 0 4px 12px rgba(23, 33, 43, 0.12);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.04em;
white-space: nowrap;
transform: translate(-50%, -50%);
will-change: left, top;
}
.world-label.bay-a {
border-left: 4px solid var(--orange);
}
.world-label.bay-b {
border-left: 4px solid var(--green);
}
.world-label.dimension {
color: white;
background: rgba(237, 107, 50, 0.92);
border: 0;
}
.world-label.dimension.good {
background: rgba(21, 150, 109, 0.92);
}
.world-label.walkway {
color: white;
background: rgba(41, 121, 200, 0.88);
border: 0;
}
@media (max-width: 850px) {
#header {
width: calc(100vw - 36px);
}
#header p {
display: none;
}
#comparison {
top: 118px;
right: 12px;
width: 270px;
padding: 12px;
}
#comparison .section-title,
#insight {
display: none;
}
#controls {
bottom: 12px;
}
.control-label,
#phase,
#hint {
display: none;
}
.control-row {
gap: 5px;
}
.segmented button {
padding: 8px 8px;
}
.icon-button {
padding: 8px;
}
}
@media (max-width: 560px) {
#comparison {
top: auto;
right: 12px;
bottom: 112px;
width: 218px;
}
#comparison .fixed-metrics {
gap: 5px;
}
.fixed-metric {
padding: 8px;
}
.fixed-metric strong {
font-size: 15px;
}
.position-card {
padding: 9px;
}
.clearance-number {
font-size: 18px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels" aria-hidden="true"></div>
<header id="header" class="panel">
<div class="eyebrow">North Loop · Fictional Delivery</div>
<h1>Same delivery. Better clearance.</h1>
<p>
Compare two unloading positions while package count and handling time remain fixed.
The animated gap measures from the unloading apron to the pedestrian-zone edge.
</p>
</header>
<aside id="comparison" class="panel">
<div class="section-title">
<span>Pedestrian clearance</span>
<span class="locked">Workload locked</span>
</div>
<button class="position-card active" data-position="A">
<div class="card-heading">
<div>
<div class="card-name">Position A · Curb-side</div>
<div class="card-subtitle">Shorter reach to the pedestrian zone</div>
</div>
<div class="clearance-number">0.8<small>m</small></div>
</div>
<div class="bar"><div class="bar-fill"></div></div>
</button>
<button class="position-card" data-position="B">
<div class="card-heading">
<div>
<div class="card-name">Position B · Set-back</div>
<div class="card-subtitle">More separation without changing handling</div>
</div>
<div class="clearance-number">2.4<small>m</small></div>
</div>
<div class="bar"><div class="bar-fill"></div></div>
</button>
<div class="fixed-metrics">
<div class="fixed-metric">
<span>Packages</span>
<strong>8 <small>fixed</small></strong>
</div>
<div class="fixed-metric">
<span>Handling</span>
<strong>24 <small>sec fixed</small></strong>
</div>
</div>
<p id="insight">
Position B provides 1.6 m more pedestrian clearance while still moving
the same 8 packages in the same 24-second handling window.
</p>
</aside>
<section id="controls" class="panel">
<div class="control-row">
<span class="control-label">Unloading position</span>
<div class="segmented" aria-label="Choose unloading position">
<button class="active" data-position="A" aria-pressed="true">A · Curb-side</button>
<button data-position="B" aria-pressed="false">B · Set-back</button>
</div>
<div class="divider"></div>
<button id="pause" class="icon-button">Pause</button>
<button id="replay" class="icon-button">Replay</button>
<button id="camera-reset" class="icon-button">Reset view</button>
<div class="spacer"></div>
<div id="phase">Arriving at Position A</div>
</div>
<div class="timeline">
<div id="timeline-fill"></div>
</div>
<div class="timeline-meta">
<span id="live-packages">0 / 8 packages transferred</span>
<span id="live-time">0.0 s handling elapsed</span>
</div>
</section>
<div id="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Configuration
// ------------------------------------------------------------
const PACKAGE_COUNT = 8;
const HANDLING_SECONDS = 24;
const ARRIVAL_SECONDS = 4;
const DEPARTURE_SECONDS = 4;
const CYCLE_SECONDS = ARRIVAL_SECONDS + HANDLING_SECONDS + DEPARTURE_SECONDS + 1;
const positions = {
A: {
id: "A",
name: "Curb-side",
x: -7,
truckZ: 1.7,
dockZ: 3.2,
clearance: 0.8,
color: 0xed6b32
},
B: {
id: "B",
name: "Set-back",
x: 6,
truckZ: 0.2,
dockZ: 1.6,
clearance: 2.4,
color: 0x15966d
}
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8ee);
scene.fog = new THREE.Fog(0xdce8ee, 27, 52);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
document.getElementById("scene").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xeaf7ff, 0x71806e, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.25);
sun.position.set(-10, 18, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -25;
sun.shadow.camera.right = 25;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb7d8ff, 0.85);
fill.position.set(12, 8, -15);
scene.add(fill);
// ------------------------------------------------------------
// Helpers
// ------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
...options
});
const materials = {
ground: material(0xaebd9f),
road: material(0x56616b),
roadEdge: material(0x3f4850),
sidewalk: material(0xd9d4c9),
walkway: material(0x87b9df, {
transparent: true,
opacity: 0.3,
depthWrite: false
}),
white: material(0xf3f0e7),
dark: material(0x26313a),
tire: material(0x182027),
glass: material(0x77b4c8, {
roughness: 0.35,
metalness: 0.12
}),
orange: material(0xed6b32),
green: material(0x15966d),
blue: material(0x2979c8),
yellow: material(0xf3c94f),
wood: material(0xb98358),
tape: material(0xe6d6b4)
};
function addBox(
width,
height,
depth,
mat,
x = 0,
y = 0,
z = 0,
parent = scene,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addLabel(text, className, position) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.textContent = text;
document.getElementById("labels").appendChild(element);
return {
element,
position: position.clone()
};
}
const projected = new THREE.Vector3();
function updateLabel(label) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
label.element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
// ------------------------------------------------------------
// Voxel environment
// ------------------------------------------------------------
addBox(44, 0.6, 30, materials.ground, 0, -0.35, 2);
// Road and curbs
addBox(42, 0.12, 8, materials.road, 0, 0.02, 0, scene, false);
addBox(42, 0.25, 0.25, materials.roadEdge, 0, 0.09, -4.05);
addBox(42, 0.25, 0.25, materials.roadEdge, 0, 0.09, 4.05);
// Sidewalk and pedestrian zone
addBox(42, 0.2, 3.2, materials.sidewalk, 0, 0.1, 5.6);
addBox(42, 0.03, 2.2, materials.walkway, 0, 0.215, 5.1, scene, false);
// Sidewalk tile divisions
for (let x = -20; x <= 20; x += 2) {
addBox(0.035, 0.025, 3.1, material(0xc9c3b8), x, 0.22, 5.6, scene, false);
}
// Lane dashes
for (let x = -20; x <= 20; x += 3.4) {
addBox(1.65, 0.025, 0.13, materials.white, x, 0.095, -2.55, scene, false);
}
// Crosswalk at the far left
for (let z = -3.6; z < 3.8; z += 0.75) {
addBox(1.45, 0.03, 0.42, materials.white, -17.4, 0.11, z, scene, false);
}
// Road direction blocks
for (const x of [-11, 1, 12]) {
addBox(0.95, 0.025, 0.12, materials.white, x, 0.105, -1.05, scene, false);
addBox(0.12, 0.025, 0.8, materials.white, x + 0.35, 0.105, -1.05, scene, false);
}
// Background buildings, constructed from visible voxel blocks
const buildingPalette = [
material(0xb9674f),
material(0x688b96),
material(0xd0a65f),
material(0x778c71),
material(0x9b7a91)
];
function addBuilding(x, z, w, d, h, colorIndex) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
addBox(w, h, d, buildingPalette[colorIndex % buildingPalette.length],
0, h / 2 + 0.2, 0, group);
addBox(w + 0.35, 0.35, d + 0.35, materials.dark,
0, h + 0.35, 0, group);
// Window blocks
const columns = Math.max(2, Math.floor(w / 1.7));
const rows = Math.max(2, Math.floor(h / 1.8));
for (let row = 0; row < rows; row++) {
for (let col = 0; col < columns; col++) {
const wx = -w / 2 + (col + 0.5) * (w / columns);
const wy = 1.15 + row * 1.55;
if (wy + 0.45 > h) continue;
addBox(
0.65,
0.7,
0.05,
material(row % 2 === 0 ? 0x9ed1dc : 0xd8b878),
wx,
wy,
d / 2 + 0.035,
group,
false
);
}
}
// Door
addBox(1.05, 1.65, 0.08, material(0x44565c),
0, 1.02, d / 2 + 0.05, group);
}
addBuilding(-14, 10.7, 6, 4.5, 5.2, 0);
addBuilding(-6.5, 11.2, 5.2, 4.2, 6.8, 1);
addBuilding(0.5, 10.9, 6.4, 4.6, 4.7, 2);
addBuilding(8, 11.3, 6.2, 4.3, 6.1, 3);
addBuilding(15, 10.8, 5.5, 4.4, 4.6, 4);
// Cubic trees
function addTree(x, z, trunkColor, leafColor) {
const trunk = material(trunkColor);
const leaves = material(leafColor);
addBox(0.35, 1.5, 0.35, trunk, x, 0.92, z);
addBox(1.55, 1.25, 1.45, leaves, x, 1.75, z);
addBox(1.0, 0.95, 1.05, leaves, x + 0.35, 2.65, z - 0.1);
addBox(1.25, 0.75, 1.2, leaves, x - 0.35, 2.15, z + 0.15);
}
addTree(-18.3, 7.2, 0x806044, 0x557e5b);
addTree(-11.8, 7.5, 0x806044, 0x62865d);
addTree(12.9, 7.35, 0x806044, 0x4f7858);
// Street lamps
for (const x of [-15, -3, 11]) {
addBox(0.13, 4.3, 0.13, materials.dark, x, 2.3, 7.3);
addBox(1.2, 0.12, 0.12, materials.dark, x + 0.55, 4.4, 7.3);
addBox(0.55, 0.18, 0.35, material(0xffe6a4), x + 1.0, 4.27, 7.3);
}
// Small parcel stacks as scenery
addBox(0.6, 0.55, 0.6, materials.wood, -15.7, 0.48, 7.0);
addBox(0.55, 0.5, 0.55, materials.wood, -15.1, 0.45, 7.15);
addBox(0.5, 0.45, 0.5, materials.wood, -14.6, 0.42, 7.0);
// ------------------------------------------------------------
// Unloading aprons, clearance zones, and dimensions
// ------------------------------------------------------------
const dockVisuals = {};
for (const position of Object.values(positions)) {
const group = new THREE.Group();
scene.add(group);
const colorMaterial = material(position.color, {
emissive: position.color,
emissiveIntensity: 0.05
});
// Apron surface behind the truck
addBox(
5.5,
0.055,
0.9,
material(0x858b89),
position.x,
0.14,
position.dockZ - 0.45,
group,
false
);
// Colored front strip
addBox(
5.5,
0.075,
0.12,
colorMaterial,
position.x,
0.17,
position.dockZ - 0.03,
group,
false
);
// Side edge blocks
for (const side of [-1, 1]) {
addBox(
0.12,
0.08,
0.9,
colorMaterial,
position.x + side * 2.7,
0.17,
position.dockZ - 0.45,
group,
false
);
}
// Pedestrian clearance buffer
const bufferDepth = 4.0 - position.dockZ;
const bufferMaterial = material(position.color, {
transparent: true,
opacity: 0.14,
depthWrite: false,
emissive: position.color,
emissiveIntensity: 0.05
});
const buffer = addBox(
5.5,
0.028,
bufferDepth,
bufferMaterial,
position.x,
0.245,
position.dockZ + bufferDepth / 2,
group,
false
);
// Clearance dimension line and end caps
const dimensionMaterial = material(position.color, {
emissive: position.color,
emissiveIntensity: 0.15
});
const dimX = position.x + 2.35;
addBox(
0.07,
0.08,
bufferDepth,
dimensionMaterial,
dimX,
0.3,
position.dockZ + bufferDepth / 2,
group,
false
);
for (const z of [position.dockZ, 4.0]) {
addBox(
0.5,
0.08,
0.07,
dimensionMaterial,
dimX,
0.3,
z,
group,
false
);
}
dockVisuals[position.id] = {
group,
buffer,
bufferMaterial,
colorMaterial,
dimensionMaterial
};
}
// ------------------------------------------------------------
// Voxel delivery truck
// ------------------------------------------------------------
const truck = new THREE.Group();
scene.add(truck);
// Chassis
addBox(4.75, 0.3, 1.65, materials.dark, 0, 0.58, 0, truck);
addBox(4.5, 0.22, 1.8, materials.orange, -0.05, 0.83, 0, truck);
// Cargo body
addBox(2.95, 2.05, 1.78, material(0xf1eee3), -0.65, 1.93, 0, truck);
addBox(2.72, 0.12, 1.62, materials.orange, -0.65, 2.98, 0, truck);
// Cargo door details
addBox(0.045, 1.7, 0.045, materials.dark, -0.65, 1.92, 0.92, truck);
addBox(0.28, 0.07, 0.06, materials.dark, -0.52, 1.78, 0.94, truck);
// Cabin
addBox(1.55, 1.8, 1.72, materials.orange, 1.42, 1.72, 0, truck);
addBox(1.22, 0.82, 0.06, materials.glass, 1.57, 2.02, 0.89, truck);
addBox(0.06, 0.82, 1.2, materials.glass, 2.21, 2.02, 0, truck);
addBox(1.35, 0.18, 1.82, materials.white, 1.42, 2.7, 0, truck);
// Bumpers and lights
addBox(0.18, 0.28, 1.85, materials.white, 2.28, 0.78, 0, truck);
addBox(0.08, 0.23, 0.28, material(0xffe87f), 2.39, 1.05, 0.58, truck);
addBox(0.08, 0.23, 0.28, material(0xffe87f), 2.39, 1.05, -0.58, truck);
// Cubic wheels
for (const x of [-1.55, 1.48]) {
for (const z of [-0.91, 0.91]) {
addBox(0.62, 0.62, 0.25, materials.tire, x, 0.48, z, truck);
addBox(0.25, 0.25, 0.27, materials.white, x, 0.48, z, truck);
}
}
// ------------------------------------------------------------
// Package cubes
// ------------------------------------------------------------
const packageMeshes = [];
for (let i = 0; i < PACKAGE_COUNT; i++) {
const group = new THREE.Group();
group.visible = false;
scene.add(group);
const parcelMaterial = material(
i % 3 === 0 ? 0xc99162 : i % 3 === 1 ? 0xd6b47a : 0xb88154
);
addBox(0.55, 0.5, 0.55, parcelMaterial, 0, 0, 0, group);
addBox(0.57, 0.07, 0.09, materials.tape, 0, 0.255, 0, group, false);
addBox(0.09, 0.07, 0.57, materials.tape, 0, 0.255, 0, group, false);
packageMeshes.push(group);
}
// ------------------------------------------------------------
// Voxel pedestrians
// ------------------------------------------------------------
function createPedestrian(shirtColor, skinColor, direction) {
const group = new THREE.Group();
scene.add(group);
const shirt = material(shirtColor);
const skin = material(skinColor);
const trousers = material(0x2d3945);
addBox(0.38, 0.62, 0.3, shirt, 0, 1.05, 0, group);
addBox(0.34, 0.34, 0.32, skin, 0, 1.53, 0, group);
addBox(0.38, 0.12, 0.35, material(0x34404b), 0, 1.73, 0, group);
const leftLeg = addBox(0.14, 0.58, 0.17, trousers, -0.11, 0.48, 0, group);
const rightLeg = addBox(0.14, 0.58, 0.17, trousers, 0.11, 0.48, 0, group);
const leftArm = addBox(0.12, 0.55, 0.15, shirt, -0.28, 1.03, 0, group);
const rightArm = addBox(0.12, 0.55, 0.15, shirt, 0.28, 1.03, 0, group);
group.userData = {
direction,
leftLeg,
rightLeg,
leftArm,
rightArm
};
return group;
}
const pedestrians = [
createPedestrian(0xe65e42, 0xd89a72, 1),
createPedestrian(0x347db4, 0x8f5e45, -1),
createPedestrian(0x8a62aa, 0xe0ad86, 1)
];
pedestrians[0].position.set(-13, 0.24, 4.58);
pedestrians[1].position.set(10, 0.24, 5.43);
pedestrians[2].position.set(-2, 0.24, 5.85);
// ------------------------------------------------------------
// World labels
// ------------------------------------------------------------
const labels = [
addLabel("POSITION A · CURB-SIDE", "bay-a", new THREE.Vector3(-7, 0.45, 3.42)),
addLabel("POSITION B · SET-BACK", "bay-b", new THREE.Vector3(6, 0.45, 1.82)),
addLabel("PEDESTRIAN ZONE", "walkway", new THREE.Vector3(11.5, 0.55, 5.15))
];
const dimensionLabels = {
A: addLabel(
"0.8 m CLEAR",
"dimension",
new THREE.Vector3(-4.65, 0.7, 3.6)
),
B: addLabel(
"2.4 m CLEAR",
"dimension good",
new THREE.Vector3(8.35, 0.7, 2.8)
)
};
// ------------------------------------------------------------
// Custom camera controls
// ------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 0.8, 2.1),
radius: 24,
theta: 0.62,
phi: 1.02
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.target.set(0, 0.8, 2.1);
cameraState.radius = 24;
cameraState.theta = 0.62;
cameraState.phi = 1.02;
updateCamera();
}
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (event.shiftKey) {
const panScale = cameraState.radius * 0.0014;
const right = new THREE.Vector3(
Math.cos(cameraState.theta),
0,
-Math.sin(cameraState.theta)
);
const forward = new THREE.Vector3(
Math.sin(cameraState.theta),
0,
Math.cos(cameraState.theta)
);
cameraState.target.addScaledVector(right, -dx * panScale);
cameraState.target.addScaledVector(forward, -dy * panScale);
} else {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.48,
1.42
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.015,
12,
42
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Delivery state and UI
// ------------------------------------------------------------
let selectedPosition = "A";
let elapsed = 0;
let paused = false;
let transferredCount = 0;
const positionButtons = document.querySelectorAll("[data-position]");
const pauseButton = document.getElementById("pause");
const replayButton = document.getElementById("replay");
const cameraResetButton = document.getElementById("camera-reset");
const phaseElement = document.getElementById("phase");
const livePackagesElement = document.getElementById("live-packages");
const liveTimeElement = document.getElementById("live-time");
const timelineFill = document.getElementById("timeline-fill");
function selectPosition(id) {
selectedPosition = id;
elapsed = 0;
transferredCount = 0;
for (const mesh of packageMeshes) {
mesh.visible = false;
}
for (const button of positionButtons) {
const active = button.dataset.position === id;
button.classList.toggle("active", active);
if (button.hasAttribute("aria-pressed")) {
button.setAttribute("aria-pressed", String(active));
}
}
for (const [key, visual] of Object.entries(dockVisuals)) {
const active = key === id;
visual.bufferMaterial.opacity = active ? 0.25 : 0.065;
visual.colorMaterial.emissiveIntensity = active ? 0.28 : 0.02;
visual.dimensionMaterial.emissiveIntensity = active ? 0.35 : 0.05;
}
for (const [key, label] of Object.entries(dimensionLabels)) {
label.element.classList.toggle("good", key === "B");
}
}
for (const button of positionButtons) {
button.addEventListener("click", () => {
selectPosition(button.dataset.position);
});
}
function togglePause() {
paused = !paused;
pauseButton.textContent = paused ? "Play" : "Pause";
}
function replay() {
elapsed = 0;
transferredCount = 0;
for (const mesh of packageMeshes) {
mesh.visible = false;
}
}
pauseButton.addEventListener("click", togglePause);
replayButton.addEventListener("click", replay);
cameraResetButton.addEventListener("click", resetCamera);
addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
} else if (event.key === "1") {
selectPosition("A");
} else if (event.key === "2") {
selectPosition("B");
} else if (event.key.toLowerCase() === "r") {
replay();
}
});
// ------------------------------------------------------------
// Animation helpers
// ------------------------------------------------------------
const clamp01 = (value) => THREE.MathUtils.clamp(value, 0, 1);
function easeInOut(value) {
const t = clamp01(value);
return t * t * (3 - 2 * t);
}
function setPackagePose(index, progress, position) {
const mesh = packageMeshes[index];
const source = new THREE.Vector3(
position.x - 0.72 + (index % 2) * 0.7,
1.28,
position.truckZ + 0.91
);
const target = new THREE.Vector3(
position.x - 1.45 + (index % 4) * 0.95,
0.51,
position.dockZ - 0.34 + Math.floor(index / 4) * 0.15
);
const t = easeInOut(progress);
mesh.position.lerpVectors(source, target, t);
mesh.position.y += Math.sin(Math.PI * t) * 0.85;
mesh.rotation.y = Math.sin(Math.PI * t) * 0.45;
}
function updatePackages(localHandlingTime, position) {
transferredCount = 0;
for (let i = 0; i < PACKAGE_COUNT; i++) {
const start = i * 3;
const duration = 0.9;
if (localHandlingTime < start) {
packageMeshes[i].visible = false;
continue;
}
packageMeshes[i].visible = true;
const progress = clamp01((localHandlingTime - start) / duration);
setPackagePose(i, progress, position);
if (localHandlingTime >= start + duration) {
transferredCount = i + 1;
}
}
}
function updatePedestrians(time) {
pedestrians.forEach((pedestrian, index) => {
const data = pedestrian.userData;
const speed = index === 1 ? 0.72 : 0.62;
const span = 40;
pedestrian.position.x += data.direction * speed * 0.016;
if (pedestrian.position.x > span / 2) {
pedestrian.position.x = -span / 2;
}
if (pedestrian.position.x < -span / 2) {
pedestrian.position.x = span / 2;
}
const stride = Math.sin(time * 7 + index * 1.7) * 0.55;
data.leftLeg.rotation.x = stride;
data.rightLeg.rotation.x = -stride;
data.leftArm.rotation.x = -stride * 0.75;
data.rightArm.rotation.x = stride * 0.75;
});
}
function updateInterface() {
const position = positions[selectedPosition];
let phaseText;
let handlingElapsed = 0;
if (elapsed < ARRIVAL_SECONDS) {
phaseText = `Arriving at Position ${position.id}`;
} else if (elapsed < ARRIVAL_SECONDS + HANDLING_SECONDS) {
handlingElapsed = elapsed - ARRIVAL_SECONDS;
phaseText = `Handling · ${transferredCount} of ${PACKAGE_COUNT} packages`;
} else if (elapsed < ARRIVAL_SECONDS + HANDLING_SECONDS + DEPARTURE_SECONDS) {
phaseText = `Departing Position ${position.id}`;
} else {
phaseText = "Preparing next delivery";
}
phaseElement.textContent = phaseText;
livePackagesElement.textContent =
`${transferredCount} / ${PACKAGE_COUNT} packages transferred`;
liveTimeElement.textContent =
`${handlingElapsed.toFixed(1)} s / ${HANDLING_SECONDS} s handling elapsed`;
timelineFill.style.width = `${(elapsed / CYCLE_SECONDS) * 100}%`;
}
// ------------------------------------------------------------
// Main loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
let worldTime = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
worldTime += delta;
if (!paused) {
elapsed += delta;
if (elapsed > CYCLE_SECONDS) {
elapsed = 0;
transferredCount = 0;
for (const mesh of packageMeshes) {
mesh.visible = false;
}
}
}
const position = positions[selectedPosition];
if (elapsed < ARRIVAL_SECONDS) {
const xProgress = easeInOut(elapsed / 3.65);
const zProgress = easeInOut((elapsed - 2.2) / 1.8);
truck.position.x = THREE.MathUtils.lerp(-18, position.x, xProgress);
truck.position.z = THREE.MathUtils.lerp(-1.8, position.truckZ, zProgress);
transferredCount = 0;
for (const mesh of packageMeshes) {
mesh.visible = false;
}
} else if (elapsed < ARRIVAL_SECONDS + HANDLING_SECONDS) {
truck.position.x = position.x;
truck.position.z = position.truckZ;
updatePackages(elapsed - ARRIVAL_SECONDS, position);
} else if (
elapsed <
ARRIVAL_SECONDS + HANDLING_SECONDS + DEPARTURE_SECONDS
) {
const departureTime =
elapsed - ARRIVAL_SECONDS - HANDLING_SECONDS;
const departureProgress = easeInOut(departureTime / DEPARTURE_SECONDS);
truck.position.x = THREE.MathUtils.lerp(
position.x,
18,
departureProgress
);
truck.position.z = THREE.MathUtils.lerp(
position.truckZ,
-1.8,
departureProgress
);
} else {
truck.position.x = -18;
truck.position.z = -1.8;
}
updatePedestrians(worldTime);
updateInterface();
for (const label of labels) {
updateLabel(label);
}
for (const label of Object.values(dimensionLabels)) {
updateLabel(label);
}
renderer.render(scene, camera);
}
function onResize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
}
addEventListener("resize", onResize);
selectPosition("A");
resetCamera();
updateCamera();
animate();
</script>
</body>
</html>
```