Prompt specification
Build or customize the Three.js 3D scene "One Parcel Changes From Bike to Train" 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 Parcel Changes From Bike to Train
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/one-parcel-changes-from-bike-to-train-x2102
Scene Description Prompt:
Animate a tracked parcel handed from a bicycle courier to a train service. Preserve identity and count transfer time separately from travel time. Build this with Three.js from cdnjs as a low-poly scene in one self-contained HTML file.
Working Three.js Starter (one-parcel-changes-from-bike-to-train-x2102):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Parcel Handoff — Three.js</title>
<style>
:root {
--ink: #17242d;
--muted: #63747f;
--panel: rgba(250, 253, 255, 0.91);
--line: rgba(33, 55, 68, 0.13);
--travel: #2563eb;
--transfer: #e97818;
--green: #16845b;
--shadow: 0 14px 40px rgba(26, 45, 58, 0.15);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dcebf2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: absolute;
z-index: 5;
background: var(--panel);
border: 1px solid rgba(255,255,255,0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#brand {
top: 20px;
left: 20px;
width: 270px;
padding: 15px 17px;
border-radius: 17px;
}
.eyebrow {
margin-bottom: 6px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#brand h1 {
margin: 0;
font-size: 21px;
line-height: 1.08;
letter-spacing: -0.035em;
}
#brand p {
margin: 7px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
#parcelPanel {
top: 126px;
left: 20px;
width: 270px;
padding: 16px;
border-radius: 17px;
}
.parcel-row {
display: flex;
align-items: center;
gap: 12px;
}
.parcel-icon {
position: relative;
width: 42px;
height: 42px;
flex: 0 0 42px;
border: 2px solid #b95310;
border-radius: 8px;
background: #f39a3d;
box-shadow: inset 0 -9px 0 rgba(150, 62, 6, 0.08);
}
.parcel-icon::before {
content: "";
position: absolute;
top: -2px;
bottom: -2px;
left: 15px;
width: 8px;
background: rgba(255, 228, 160, 0.82);
}
.parcel-icon::after {
content: "";
position: absolute;
width: 6px;
height: 6px;
top: 7px;
right: 6px;
border-radius: 50%;
background: #b9ffdb;
box-shadow: 0 0 10px #70efb4;
}
.parcel-id {
margin: 0;
font-size: 19px;
font-weight: 850;
letter-spacing: 0.02em;
}
.parcel-sub {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
}
.custody {
display: flex;
align-items: center;
gap: 8px;
margin-top: 14px;
padding: 10px 11px;
border-radius: 11px;
background: rgba(33, 99, 235, 0.075);
color: #214f9d;
font-size: 12px;
font-weight: 750;
}
.custody::before {
content: "";
width: 7px;
height: 7px;
flex: 0 0 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 0 4px rgba(33, 99, 235, 0.10);
}
#chain {
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.chain-row {
display: grid;
grid-template-columns: 46px 1fr;
gap: 8px;
margin: 7px 0;
color: var(--muted);
font-size: 10px;
}
.chain-row time {
font-variant-numeric: tabular-nums;
font-weight: 800;
}
.chain-row.active {
color: var(--ink);
}
.chain-row.active time {
color: var(--transfer);
}
#metrics {
top: 20px;
right: 20px;
width: 274px;
padding: 16px;
border-radius: 17px;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.metric {
padding: 12px;
border-radius: 12px;
background: rgba(28, 52, 68, 0.045);
}
.metric .label {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric .value {
margin-top: 5px;
font-size: 25px;
font-weight: 850;
letter-spacing: -0.045em;
font-variant-numeric: tabular-nums;
}
.metric.travel .value { color: var(--travel); }
.metric.transfer .value { color: var(--transfer); }
.metric-note {
margin-top: 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
#phase {
top: 20px;
left: 50%;
transform: translateX(-50%);
max-width: min(430px, calc(100vw - 620px));
padding: 11px 18px;
border-radius: 999px;
text-align: center;
pointer-events: none;
}
#phaseTitle {
font-size: 13px;
font-weight: 850;
}
#phaseDetail {
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
#timeline {
right: 20px;
bottom: 20px;
width: 440px;
padding: 15px 16px;
border-radius: 17px;
}
.timeline-head {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 10px;
}
.timeline-head strong {
font-size: 12px;
}
#totalTime {
color: var(--muted);
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.track {
display: flex;
height: 9px;
overflow: hidden;
border-radius: 999px;
background: #dfe8ed;
}
#travelBar {
width: 0;
background: var(--travel);
transition: width 0.08s linear;
}
#transferBar {
width: 0;
background: var(--transfer);
transition: width 0.08s linear;
}
.timeline-legend {
display: flex;
gap: 18px;
margin-top: 10px;
color: var(--muted);
font-size: 10px;
}
.timeline-legend span {
display: flex;
align-items: center;
gap: 6px;
}
.timeline-legend i {
width: 8px;
height: 8px;
border-radius: 50%;
}
.legend-travel { background: var(--travel); }
.legend-transfer { background: var(--transfer); }
#controls {
left: 20px;
bottom: 20px;
display: flex;
align-items: center;
gap: 8px;
padding: 9px;
border-radius: 15px;
}
button {
border: 0;
border-radius: 10px;
padding: 9px 11px;
color: var(--ink);
background: rgba(28, 52, 68, 0.075);
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 750;
transition: transform 0.15s, background 0.15s;
}
button:hover {
background: rgba(28, 52, 68, 0.13);
transform: translateY(-1px);
}
button.active {
color: white;
background: var(--travel);
}
#hint {
padding: 0 6px 0 4px;
color: var(--muted);
font-size: 10px;
white-space: nowrap;
}
#toast {
position: absolute;
z-index: 8;
left: 50%;
bottom: 98px;
transform: translate(-50%, 12px);
padding: 9px 13px;
border-radius: 999px;
background: rgba(23, 36, 45, 0.88);
color: white;
font-size: 11px;
opacity: 0;
pointer-events: none;
transition: opacity 0.2s, transform 0.2s;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 980px) {
#phase {
top: 172px;
max-width: calc(100vw - 40px);
}
#metrics {
top: 126px;
right: 20px;
}
#timeline {
width: calc(100vw - 40px);
bottom: 80px;
}
#hint { display: none; }
}
@media (max-width: 650px) {
#brand {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
#brand p { display: none; }
#parcelPanel {
top: 100px;
left: 12px;
width: 230px;
}
#metrics {
top: 100px;
right: 12px;
width: 150px;
}
.metric { padding: 9px; }
.metric .value { font-size: 20px; }
.metric-note { display: none; }
#phase {
top: 235px;
max-width: calc(100vw - 24px);
}
#chain { display: none; }
#timeline {
right: 12px;
bottom: 75px;
width: calc(100vw - 24px);
}
#controls {
left: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="brand" class="panel">
<div class="eyebrow">Urban logistics / live transfer</div>
<h1>Parcel Handoff</h1>
<p>A bicycle courier transfers a tracked parcel to a train service.</p>
</div>
<div id="parcelPanel" class="panel">
<div class="eyebrow">Tracked object</div>
<div class="parcel-row">
<div class="parcel-icon"></div>
<div>
<p class="parcel-id">PX-2048</p>
<div class="parcel-sub">Manifest 8C-19 • priority parcel</div>
</div>
</div>
<div id="custody" class="custody">Bicycle courier</div>
<div id="chain">
<div class="chain-row" data-chain="0">
<time>00:00</time><span>Courier custody</span>
</div>
<div class="chain-row" data-chain="1">
<time>00:09</time><span>Handoff begins</span>
</div>
<div class="chain-row" data-chain="2">
<time>00:17</time><span>Train custody</span>
</div>
<div class="chain-row" data-chain="3">
<time>00:29</time><span>Departure complete</span>
</div>
</div>
</div>
<div id="phase" class="panel">
<div id="phaseTitle">Courier en route</div>
<div id="phaseDetail">Travel time is being recorded</div>
</div>
<div id="metrics" class="panel">
<div class="eyebrow">Time accounting</div>
<div class="metric-grid">
<div class="metric travel">
<div class="label">Travel</div>
<div id="travelValue" class="value">0.0s</div>
</div>
<div class="metric transfer">
<div class="label">Transfer</div>
<div id="transferValue" class="value">0.0s</div>
</div>
</div>
<div class="metric-note">
Transfer time is counted only while custody is changing hands.
Travel and transfer clocks never run together.
</div>
</div>
<div id="timeline" class="panel">
<div class="timeline-head">
<strong>Simulation timeline</strong>
<span id="totalTime">0.0 / 29.0s</span>
</div>
<div class="track">
<div id="travelBar"></div>
<div id="transferBar"></div>
</div>
<div class="timeline-legend">
<span><i class="legend-travel"></i>Travel — courier + train</span>
<span><i class="legend-transfer"></i>Transfer — two custody handoffs</span>
</div>
</div>
<div id="controls" class="panel">
<button id="playButton" title="Space">Pause</button>
<button id="restartButton" title="R">Restart</button>
<button id="followButton" title="F">Follow parcel</button>
<button id="speedButton">1×</button>
<span id="hint">Drag to orbit • wheel to zoom • click parcel</span>
</div>
<div id="toast"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdcebf2);
scene.fog = new THREE.Fog(0xdcebf2, 36, 76);
const camera = new THREE.PerspectiveCamera(
45,
innerWidth / innerHeight,
0.1,
150
);
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.05;
document.body.prepend(renderer.domElement);
const ambient = new THREE.HemisphereLight(0xe9f7ff, 0x7c7968, 2.1);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xfff5df, 3.1);
sun.position.set(-15, 24, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -32;
sun.shadow.camera.right = 32;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.bias = -0.00025;
scene.add(sun);
const mat = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...extra
});
const materials = {
grass: mat(0x9fbf91),
road: mat(0x53636b),
roadEdge: mat(0xcfd6d5),
pavement: mat(0xc8d1cf),
platform: mat(0xd9dfdc),
cream: mat(0xf1e9d4),
steel: mat(0x83949b, { metalness: 0.25, roughness: 0.55 }),
dark: mat(0x243640),
rail: mat(0x647178, { metalness: 0.62, roughness: 0.38 }),
wood: mat(0x9c6944),
orange: mat(0xf28c35),
orangeDark: mat(0x9d4b13),
yellow: mat(0xf3c85a),
blue: mat(0x2776b8),
navy: mat(0x184b78),
glass: mat(0x7ec9df, {
metalness: 0.15,
roughness: 0.25,
emissive: 0x153b4c,
emissiveIntensity: 0.2
}),
leaf: mat(0x4f8b68),
leafLight: mat(0x72a875),
skin: mat(0xd8a179),
white: mat(0xf8faf8),
black: mat(0x18242a),
transfer: mat(0xf08a2c, {
emissive: 0x8d3f08,
emissiveIntensity: 0.18
})
};
function box(parent, size, position, material, cast = 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 = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(parent, a, b, radius, material, radialSegments = 7) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextSprite(text, options = {}) {
const {
background = "#17364b",
foreground = "#ffffff",
width = 512,
height = 128,
scale = [2.8, 0.7]
} = options;
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = "rgba(255,255,255,.22)";
ctx.lineWidth = 5;
ctx.strokeRect(3, 3, width - 6, height - 6);
ctx.fillStyle = foreground;
ctx.font = "800 47px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, width / 2, height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(scale[0], scale[1], 1);
sprite.renderOrder = 20;
return sprite;
}
// ------------------------------------------------------------
// Environment
// ------------------------------------------------------------
box(scene, [70, 0.35, 48], [0, -0.25, -2], materials.grass, false);
box(scene, [53, 0.08, 3.5], [0, 0, 4], materials.road, false);
for (let x = -25; x <= 25; x += 4) {
box(scene, [1.8, 0.025, 0.09], [x, 0.055, 4], materials.roadEdge, false);
}
box(scene, [53, 0.08, 3.1], [0, 0, 0.7], materials.pavement, false);
// Transfer zone under the courier-to-agent handoff.
const transferRing = new THREE.Mesh(
new THREE.RingGeometry(1.25, 1.42, 32),
new THREE.MeshBasicMaterial({
color: 0xf18a2c,
transparent: true,
opacity: 0.65,
side: THREE.DoubleSide
})
);
transferRing.rotation.x = -Math.PI / 2;
transferRing.position.set(-3.8, 0.08, 3.15);
scene.add(transferRing);
// Platform and rail bed.
box(scene, [18, 0.38, 2.65], [0, 0.17, -6.0], materials.platform);
box(scene, [50, 0.22, 3.1], [0, -0.02, -8.55], materials.dark, false);
for (const z of [-9.1, -7.9]) {
box(scene, [50, 0.12, 0.1], [0, 0.18, z], materials.rail, false);
for (let x = -24; x <= 24; x += 2.2) {
box(scene, [0.18, 0.12, 1.65], [x, 0.09, -8.5], materials.wood, false);
}
}
// Platform safety stripe.
box(scene, [18, 0.025, 0.13], [0, 0.39, -7.23], materials.yellow, false);
// Station details.
box(scene, [0.16, 3.2, 0.16], [-6.7, 1.6, -6.8], materials.steel);
box(scene, [0.16, 3.2, 0.16], [-4.7, 1.6, -6.8], materials.steel);
box(scene, [2.4, 0.16, 1.3], [-5.7, 3.18, -6.8], materials.navy);
const stationSign = makeTextSprite("CENTRAL", {
background: "#184b78",
scale: [2.45, 0.61]
});
stationSign.position.set(-5.7, 3.75, -6.65);
scene.add(stationSign);
// Clock.
const clockGroup = new THREE.Group();
clockGroup.position.set(2.8, 2.75, -6.85);
scene.add(clockGroup);
const clockFace = new THREE.Mesh(
new THREE.CircleGeometry(0.55, 24),
materials.white
);
clockGroup.add(clockFace);
const clockRim = new THREE.Mesh(
new THREE.TorusGeometry(0.56, 0.055, 6, 24),
materials.navy
);
clockGroup.add(clockRim);
cylinderBetween(clockGroup, [0, 0, 0.02], [0.22, 0.22, 0.02], 0.025, materials.dark);
cylinderBetween(clockGroup, [0, 0, 0.02], [0, -0.34, 0.02], 0.025, materials.dark);
cylinderBetween(scene, [2.8, 0.4, -6.85], [2.8, 2.8, -6.85], 0.065, materials.steel);
// Bench.
box(scene, [2.2, 0.15, 0.62], [5.7, 0.85, -6.4], materials.wood);
box(scene, [2.2, 0.75, 0.12], [5.7, 1.3, -6.72], materials.wood);
for (const x of [4.85, 6.55]) {
box(scene, [0.12, 0.62, 0.55], [x, 0.5, -6.4], materials.dark);
}
// Low-poly trees.
function addTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
scene.add(tree);
cylinderBetween(tree, [0, 0, 0], [0, 1.55, 0], 0.17, materials.wood);
const lower = new THREE.Mesh(
new THREE.ConeGeometry(1.05, 2.2, 7),
materials.leaf
);
lower.position.y = 2.0;
lower.castShadow = true;
tree.add(lower);
const upper = new THREE.Mesh(
new THREE.ConeGeometry(0.78, 1.7, 7),
materials.leafLight
);
upper.position.y = 3.0;
upper.castShadow = true;
tree.add(upper);
}
[
[-20, -11, 1.1], [-14, -13, 0.9], [-8, -12, 1.0],
[8, -12, 1.05], [14, -13, 1.2], [21, -11, 0.95],
[-20, 10, 1.2], [-12, 12, 0.9], [15, 11, 1.15]
].forEach(args => addTree(...args));
// ------------------------------------------------------------
// Parcel: one persistent Object3D for the entire journey
// ------------------------------------------------------------
const parcel = new THREE.Group();
parcel.name = "PX-2048";
const parcelBody = box(
parcel,
[0.78, 0.54, 0.62],
[0, 0, 0],
materials.transfer
);
parcelBody.userData.parcelId = "PX-2048";
box(parcel, [0.13, 0.56, 0.64], [0, 0, 0], materials.yellow);
box(parcel, [0.80, 0.54, 0.10], [0, 0, 0], materials.yellow);
const parcelEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(0.79, 0.55, 0.63)),
new THREE.LineBasicMaterial({
color: 0x7c3b0d,
transparent: true,
opacity: 0.55
})
);
parcel.add(parcelEdges);
const beacon = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 8, 6),
new THREE.MeshStandardMaterial({
color: 0xbaffdc,
emissive: 0x53e8a0,
emissiveIntensity: 2
})
);
beacon.position.set(0.27, 0.31, 0.25);
parcel.add(beacon);
const trackingRing = new THREE.Mesh(
new THREE.TorusGeometry(0.56, 0.025, 5, 30),
new THREE.MeshBasicMaterial({
color: 0xffa43d,
transparent: true,
opacity: 0.85,
depthTest: false
})
);
trackingRing.rotation.x = Math.PI / 2;
trackingRing.position.y = -0.34;
trackingRing.renderOrder = 15;
parcel.add(trackingRing);
const parcelLabel = makeTextSprite("PX-2048", {
background: "#8d430d",
foreground: "#ffffff",
scale: [1.65, 0.41]
});
parcelLabel.position.set(0, 0.88, 0);
parcel.add(parcelLabel);
// ------------------------------------------------------------
// Bicycle courier
// ------------------------------------------------------------
const bike = new THREE.Group();
bike.position.set(-22, 0, 4);
scene.add(bike);
const wheelPivots = [];
function addBikeWheel(x) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.53, 0);
bike.add(pivot);
wheelPivots.push(pivot);
const tire = new THREE.Mesh(
new THREE.TorusGeometry(0.46, 0.075, 6, 14),
materials.black
);
tire.castShadow = true;
pivot.add(tire);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.37, 0.025, 5, 14),
materials.steel
);
pivot.add(rim);
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
const spoke = box(
pivot,
[0.035, 0.72, 0.035],
[0, 0, 0],
materials.steel,
false
);
spoke.rotation.z = angle;
}
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.07, 0.07, 0.2, 8),
materials.dark
);
hub.rotation.x = Math.PI / 2;
pivot.add(hub);
}
addBikeWheel(-0.88);
addBikeWheel(0.88);
const framePoints = [
[-0.88, 0.53, 0],
[-0.25, 1.13, 0],
[0.35, 1.1, 0],
[0.88, 0.53, 0],
[-0.12, 0.63, 0]
];
for (const [a, b] of [
[0, 1], [1, 2], [2, 3], [0, 4], [4, 2], [2, 4]
]) {
cylinderBetween(
bike,
framePoints[a],
framePoints[b],
0.045,
materials.blue
);
}
cylinderBetween(
bike,
[0.35, 1.1, 0],
[0.78, 1.32, 0],
0.04,
materials.dark
);
cylinderBetween(
bike,
[-0.88, 0.53, 0],
[-0.25, 1.13, 0],
0.04,
materials.dark
);
box(bike, [0.42, 0.07, 0.32], [-0.87, 1.38, 0], materials.dark);
box(bike, [0.12, 0.08, 0.62], [0.78, 1.32, 0], materials.dark);
box(bike, [0.28, 0.12, 0.24], [-0.28, 1.16, 0], materials.dark);
// Courier.
const courier = new THREE.Group();
bike.add(courier);
box(courier, [0.52, 0.72, 0.43], [-0.06, 1.72, 0], materials.orange);
box(courier, [0.25, 0.54, 0.28], [-0.28, 1.16, 0], materials.navy);
box(courier, [0.25, 0.54, 0.28], [0.34, 1.17, 0], materials.navy);
cylinderBetween(courier, [-0.08, 1.95, 0.22], [0.66, 1.35, 0.22], 0.09, materials.orange);
cylinderBetween(courier, [-0.08, 1.95, -0.22], [0.66, 1.35, -0.22], 0.09, materials.orange);
const courierHead = new THREE.Mesh(
new THREE.SphereGeometry(0.24, 8, 6),
materials.skin
);
courierHead.position.set(0.05, 2.25, 0);
courierHead.castShadow = true;
courier.add(courierHead);
box(courier, [0.43, 0.10, 0.46], [0.03, 2.40, 0], materials.navy);
box(courier, [0.26, 0.06, 0.24], [0.20, 2.39, 0], materials.navy);
// ------------------------------------------------------------
// Train-service agent
// ------------------------------------------------------------
const agent = new THREE.Group();
agent.position.set(-1.8, 0, 1.8);
scene.add(agent);
box(agent, [0.52, 0.72, 0.38], [0, 1.07, 0], materials.orange);
box(agent, [0.54, 0.12, 0.40], [0, 1.12, 0], materials.yellow);
box(agent, [0.20, 0.60, 0.25], [-0.15, 0.45, 0], materials.navy);
box(agent, [0.20, 0.60, 0.25], [0.15, 0.45, 0], materials.navy);
cylinderBetween(agent, [-0.28, 1.36, 0], [-0.03, 0.89, 0.22], 0.085, materials.orange);
cylinderBetween(agent, [0.28, 1.36, 0], [0.03, 0.89, 0.22], 0.085, materials.orange);
const agentHead = new THREE.Mesh(
new THREE.SphereGeometry(0.23, 8, 6),
materials.skin
);
agentHead.position.set(0, 1.64, 0);
agentHead.castShadow = true;
agent.add(agentHead);
box(agent, [0.42, 0.09, 0.43], [0, 1.79, 0], materials.navy);
box(agent, [0.23, 0.05, 0.22], [0.15, 1.77, 0], materials.navy);
// A small service trolley waits near the train.
const trolley = new THREE.Group();
trolley.position.set(2.4, 0, -5.75);
scene.add(trolley);
box(trolley, [1.35, 0.12, 0.72], [0, 0.64, 0], materials.steel);
box(trolley, [0.08, 0.8, 0.08], [-0.52, 1.08, 0], materials.steel);
box(trolley, [0.08, 0.8, 0.08], [0.52, 1.08, 0], materials.steel);
box(trolley, [1.35, 0.08, 0.08], [0, 1.47, 0], materials.steel);
for (const x of [-0.48, 0.48]) {
for (const z of [-0.25, 0.25]) {
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.14, 0.14, 0.09, 8),
materials.black
);
wheel.rotation.x = Math.PI / 2;
wheel.position.set(x, 0.2, z);
trolley.add(wheel);
}
}
// ------------------------------------------------------------
// Train with an open service doorway
// ------------------------------------------------------------
const train = new THREE.Group();
train.position.set(0, 0, -8.5);
scene.add(train);
box(train, [12.2, 0.25, 2.1], [0, 0.48, 0], materials.navy);
box(train, [12.2, 0.22, 2.15], [0, 2.93, 0], materials.white);
box(train, [12.0, 1.9, 0.12], [0, 1.68, -1.02], materials.white);
// Near wall has a central opening from x = -0.9 to x = 0.9.
box(train, [5.1, 1.9, 0.12], [-3.45, 1.68, 1.02], materials.white);
box(train, [5.1, 1.9, 0.12], [3.45, 1.68, 1.02], materials.white);
for (const x of [-4.65, -2.25, 2.25, 4.65]) {
box(train, [1.32, 0.82, 0.06], [x, 1.82, 1.105], materials.glass);
box(train, [0.06, 0.84, 0.07], [x, 1.82, 1.145], materials.white);
}
box(train, [0.15, 2.1, 0.16], [-0.92, 1.68, 1.11], materials.blue);
box(train, [0.15, 2.1, 0.16], [0.92, 1.68, 1.11], materials.blue);
box(train, [1.98, 0.14, 0.16], [0, 2.73, 1.11], materials.blue);
box(train, [0.16, 2.7, 2.0], [-6.02, 1.48, 0], materials.white);
box(train, [0.16, 2.7, 2.0], [6.02, 1.48, 0], materials.white);
// Train front.
box(train, [0.18, 0.65, 1.65], [6.11, 1.52, 0], materials.glass);
box(train, [0.22, 0.22, 0.32], [6.14, 0.75, 0.66], materials.yellow);
// Stripe.
box(train, [12.0, 0.16, 0.04], [0, 0.87, 1.10], materials.blue);
box(train, [12.0, 0.16, 0.04], [0, 0.87, -1.10], materials.blue);
const trainWheelPivots = [];
for (const x of [-4.5, -3.3, 3.3, 4.5]) {
for (const z of [-0.83, 0.83]) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.34, z);
train.add(pivot);
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.32, 0.32, 0.18, 10),
materials.dark
);
wheel.rotation.x = Math.PI / 2;
wheel.castShadow = true;
pivot.add(wheel);
trainWheelPivots.push(pivot);
}
}
const trainLabel = makeTextSprite("TRAIN SERVICE", {
background: "#184b78",
scale: [2.8, 0.7]
});
trainLabel.position.set(-3.4, 2.35, 1.18);
train.add(trainLabel);
// Doorway interior light.
box(train, [1.7, 0.04, 0.8], [0, 2.65, 0.35], materials.yellow, false);
// ------------------------------------------------------------
// Animation state
// ------------------------------------------------------------
const phases = [
{ duration: 9.0, name: "Courier en route", detail: "Travel time is being recorded" },
{ duration: 2.4, name: "Courier → station agent", detail: "Transfer time is being recorded" },
{ duration: 5.6, name: "Station agent → train", detail: "Transfer time is being recorded" },
{ duration: 12.0, name: "Train departure", detail: "Travel time is being recorded" },
{ duration: 0.0, name: "Parcel received by train service", detail: "Journey complete" }
];
const TOTAL_TRAVEL = phases[0].duration + phases[3].duration;
const TOTAL_TRANSFER = phases[1].duration + phases[2].duration;
const TOTAL_TIME = TOTAL_TRAVEL + TOTAL_TRANSFER;
let phaseIndex = 0;
let phaseTime = 0;
let travelTime = 0;
let transferTime = 0;
let running = true;
let speed = 1;
const handoffStart = new THREE.Vector3();
const handoffEnd = new THREE.Vector3(-3.8, 1.25, 2.9);
const handoffControl = new THREE.Vector3();
const tempWorld = new THREE.Vector3();
const tempWorld2 = new THREE.Vector3();
const boardingCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-3.8, 0, 2.9),
new THREE.Vector3(-3.2, 0, 0.9),
new THREE.Vector3(-1.8, 0, -2.7),
new THREE.Vector3(-0.55, 0, -6.45),
new THREE.Vector3(0, 0, -6.85)
]);
const ui = {
travel: document.querySelector("#travelValue"),
transfer: document.querySelector("#transferValue"),
total: document.querySelector("#totalTime"),
travelBar: document.querySelector("#travelBar"),
transferBar: document.querySelector("#transferBar"),
custody: document.querySelector("#custody"),
phaseTitle: document.querySelector("#phaseTitle"),
phaseDetail: document.querySelector("#phaseDetail"),
play: document.querySelector("#playButton"),
follow: document.querySelector("#followButton"),
speed: document.querySelector("#speedButton"),
toast: document.querySelector("#toast")
};
function smoothstep(t) {
t = THREE.MathUtils.clamp(t, 0, 1);
return t * t * (3 - 2 * t);
}
function enterPhase(index) {
phaseIndex = index;
phaseTime = 0;
if (index === 1) {
// Detach without replacing the parcel object.
scene.attach(parcel);
scene.updateMatrixWorld(true);
parcel.getWorldPosition(handoffStart);
parcel.position.copy(handoffStart);
}
if (index === 2) {
// Parcel becomes a child of the station agent.
agent.updateMatrixWorld(true);
agent.attach(parcel);
}
if (index === 3) {
// Parcel becomes a child of the train at the doorway.
train.updateMatrixWorld(true);
train.attach(parcel);
}
}
function resetSimulation() {
phaseIndex = 0;
phaseTime = 0;
travelTime = 0;
transferTime = 0;
bike.position.set(-22, 0, 4);
agent.position.set(-1.8, 0, 1.8);
agent.rotation.set(0, 0, 0);
train.position.set(0, 0, -8.5);
parcel.removeFromParent();
bike.add(parcel);
parcel.position.set(-0.85, 1.55, 0);
parcel.rotation.set(0, 0, 0);
parcel.scale.set(1, 1, 1);
bike.updateMatrixWorld(true);
scene.updateMatrixWorld(true);
for (const wheel of wheelPivots) wheel.rotation.z = 0;
for (const wheel of trainWheelPivots) wheel.rotation.z = 0;
running = true;
ui.play.textContent = "Pause";
updateUI();
}
function updateSimulation(dt) {
if (running && phaseIndex < phases.length - 1) {
phaseTime += dt * speed;
while (
phaseIndex < phases.length - 1 &&
phaseTime >= phases[phaseIndex].duration
) {
const overflow = phaseTime - phases[phaseIndex].duration;
enterPhase(phaseIndex + 1);
phaseTime = overflow;
}
}
if (phaseIndex === 0) {
const p = THREE.MathUtils.clamp(
phaseTime / phases[0].duration,
0,
1
);
const eased = smoothstep(p);
bike.position.x = THREE.MathUtils.lerp(-22, -3, eased);
const distance = bike.position.x + 22;
for (const wheel of wheelPivots) {
wheel.rotation.z = -distance / 0.46;
}
}
if (phaseIndex === 1) {
const p = THREE.MathUtils.clamp(
phaseTime / phases[1].duration,
0,
1
);
const agentMove = smoothstep(p / 0.28);
agent.position.x = THREE.MathUtils.lerp(-1.8, -3.8, agentMove);
agent.position.z = THREE.MathUtils.lerp(1.8, 2.55, agentMove);
agent.rotation.set(0, 0, 0);
agent.updateMatrixWorld(true);
const parcelMove = smoothstep((p - 0.20) / 0.80);
handoffControl.copy(handoffStart).add(handoffEnd).multiplyScalar(0.5);
handoffControl.y += 0.75;
const a = (1 - parcelMove) * (1 - parcelMove);
const b = 2 * (1 - parcelMove) * parcelMove;
const c = parcelMove * parcelMove;
parcel.position.set(
a * handoffStart.x + b * handoffControl.x + c * handoffEnd.x,
a * handoffStart.y + b * handoffControl.y + c * handoffEnd.y,
a * handoffStart.z + b * handoffControl.z + c * handoffEnd.z
);
parcel.rotation.z = Math.sin(parcelMove * Math.PI) * 0.12;
}
if (phaseIndex === 2) {
const p = THREE.MathUtils.clamp(
phaseTime / phases[2].duration,
0,
1
);
boardingCurve.getPoint(p, tempWorld);
boardingCurve.getTangent(Math.min(p, 0.999), tempWorld2);
agent.position.copy(tempWorld);
agent.rotation.y = Math.atan2(tempWorld2.x, tempWorld2.z);
if (p > 0.85) {
agent.rotation.y = THREE.MathUtils.lerp(
agent.rotation.y,
Math.PI,
smoothstep((p - 0.85) / 0.15)
);
}
}
if (phaseIndex === 3) {
const p = THREE.MathUtils.clamp(
phaseTime / phases[3].duration,
0,
1
);
const eased = smoothstep(p);
train.position.x = eased * 16;
for (const wheel of trainWheelPivots) {
wheel.rotation.z = -(train.position.x / 0.32);
}
}
// Separate clocks: only one category can accumulate at a time.
if (phaseIndex === 0) {
travelTime = phaseTime;
transferTime = 0;
} else if (phaseIndex === 1) {
travelTime = phases[0].duration;
transferTime = phaseTime;
} else if (phaseIndex === 2) {
travelTime = phases[0].duration;
transferTime = phases[1].duration + phaseTime;
} else if (phaseIndex === 3) {
travelTime = phases[0].duration + phaseTime;
transferTime = phases[1].duration + phases[2].duration;
} else {
travelTime = TOTAL_TRAVEL;
transferTime = TOTAL_TRANSFER;
}
updateUI();
}
function updateUI() {
const phase = phases[phaseIndex];
ui.travel.textContent = `${travelTime.toFixed(1)}s`;
ui.transfer.textContent = `${transferTime.toFixed(1)}s`;
ui.total.textContent = `${(travelTime + transferTime).toFixed(1)} / ${TOTAL_TIME.toFixed(1)}s`;
ui.travelBar.style.width = `${(travelTime / TOTAL_TIME) * 100}%`;
ui.transferBar.style.width = `${(transferTime / TOTAL_TIME) * 100}%`;
ui.phaseTitle.textContent = phase.name;
ui.phaseDetail.textContent = phase.detail;
const custodyText = [
"Bicycle courier",
"Transferring • courier → station",
"Transferring • station → train",
"Train service",
"Train service • received"
][phaseIndex];
ui.custody.textContent = custodyText;
document.querySelectorAll(".chain-row").forEach(row => {
row.classList.remove("active");
});
const activeChain =
phaseIndex === 0 ? 0 :
phaseIndex === 1 ? 1 :
phaseIndex === 2 ? 1 :
phaseIndex === 3 ? 2 : 3;
document.querySelector(`[data-chain="${activeChain}"]`)
?.classList.add("active");
}
// ------------------------------------------------------------
// Direct camera controls — no addons
// ------------------------------------------------------------
const desiredTarget = new THREE.Vector3(-1, 1.1, -2.5);
const currentTarget = desiredTarget.clone();
const controls = {
radius: 29,
desiredRadius: 29,
theta: 0.62,
desiredTheta: 0.62,
phi: 0.94,
desiredPhi: 0.94
};
let followParcel = false;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
let pointerMoved = 0;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function showToast(message) {
ui.toast.textContent = message;
ui.toast.classList.add("visible");
clearTimeout(showToast.timeout);
showToast.timeout = setTimeout(() => {
ui.toast.classList.remove("visible");
}, 1800);
}
function setFollow(value) {
followParcel = value;
ui.follow.classList.toggle("active", followParcel);
controls.desiredRadius = followParcel ? 7.5 : 29;
if (followParcel) {
showToast("Following PX-2048");
}
}
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerMoved = 0;
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;
pointerMoved += Math.abs(dx) + Math.abs(dy);
pointerX = event.clientX;
pointerY = event.clientY;
controls.desiredTheta -= dx * 0.006;
controls.desiredPhi = THREE.MathUtils.clamp(
controls.desiredPhi - dy * 0.005,
0.35,
1.43
);
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (pointerMoved < 5) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObject(parcel, true);
if (hits.length) {
setFollow(true);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
controls.desiredRadius = THREE.MathUtils.clamp(
controls.desiredRadius * Math.exp(event.deltaY * 0.001),
4.5,
55
);
}, { passive: false });
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
ui.play.addEventListener("click", () => {
running = !running;
ui.play.textContent = running ? "Pause" : "Play";
});
document.querySelector("#restartButton").addEventListener("click", () => {
resetSimulation();
showToast("Simulation restarted");
});
ui.follow.addEventListener("click", () => {
setFollow(!followParcel);
});
ui.speed.addEventListener("click", () => {
speed = speed === 1 ? 2 : speed === 2 ? 0.5 : 1;
ui.speed.textContent = `${speed}×`;
});
addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
ui.play.click();
}
if (event.key.toLowerCase() === "r") {
resetSimulation();
}
if (event.key.toLowerCase() === "f") {
setFollow(!followParcel);
}
});
function updateCamera(dt) {
const damping = 1 - Math.exp(-dt * 7);
if (followParcel) {
parcel.getWorldPosition(tempWorld);
desiredTarget.set(tempWorld.x, tempWorld.y + 0.45, tempWorld.z);
} else {
desiredTarget.lerp(new THREE.Vector3(-1, 1.1, -2.5), damping * 0.15);
}
currentTarget.lerp(desiredTarget, damping);
controls.radius = THREE.MathUtils.lerp(
controls.radius,
controls.desiredRadius,
damping
);
controls.theta = THREE.MathUtils.lerp(
controls.theta,
controls.desiredTheta,
damping
);
controls.phi = THREE.MathUtils.lerp(
controls.phi,
controls.desiredPhi,
damping
);
const sinPhi = Math.sin(controls.phi);
camera.position.set(
currentTarget.x + controls.radius * sinPhi * Math.sin(controls.theta),
currentTarget.y + controls.radius * Math.cos(controls.phi),
currentTarget.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(currentTarget);
}
// ------------------------------------------------------------
// Main loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
updateSimulation(dt);
updateCamera(dt);
const pulse = 1 + Math.sin(performance.now() * 0.004) * 0.08;
trackingRing.scale.setScalar(pulse);
trackingRing.material.opacity = 0.68 + Math.sin(performance.now() * 0.004) * 0.17;
beacon.material.emissiveIntensity = 1.8 + Math.sin(performance.now() * 0.006) * 0.7;
renderer.render(scene, camera);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
resetSimulation();
animate();
</script>
</body>
</html>
```