Prompt specification
Build or customize the Three.js 3D scene "Three-Level Parcel Lift Hub" 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: Three-Level Parcel Lift Hub
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/three-level-parcel-lift-hub-x2095
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: three-level parcel lift hub. Build an original three-level parcel depot with conveyors and a capacity-limited lift. Track parcel destinations, cabin occupancy, door alignment, and waiting queues through actual vertical transfers. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (three-level-parcel-lift-hub-x2095):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vertex Parcel Hub</title>
<style>
:root {
--bg: #071116;
--panel: rgba(11, 24, 30, 0.86);
--panel-2: rgba(15, 31, 37, 0.76);
--line: rgba(170, 225, 226, 0.17);
--text: #e8f3f1;
--muted: #86a2a5;
--mint: #56e0c3;
--amber: #ffbd58;
--violet: #c991ff;
--red: #ff6f74;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 68% 24%, rgba(44, 104, 108, 0.25), transparent 36%),
linear-gradient(155deg, #0b1b21 0%, var(--bg) 64%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, canvas { -webkit-tap-highlight-color: transparent; }
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active { cursor: grabbing; }
#hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.panel {
pointer-events: auto;
position: absolute;
border: 1px solid var(--line);
background:
linear-gradient(135deg, rgba(255,255,255,0.045), transparent 46%),
var(--panel);
box-shadow:
0 20px 50px rgba(0,0,0,0.26),
inset 0 1px 0 rgba(255,255,255,0.045);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
border-radius: 14px;
}
.brand {
top: 22px;
left: 22px;
width: 348px;
padding: 18px 18px 16px;
}
.eyebrow {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: var(--mint);
font-size: 10px;
line-height: 1;
letter-spacing: 0.2em;
text-transform: uppercase;
font-weight: 800;
}
.status-dot {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--muted);
letter-spacing: 0.12em;
}
.status-dot::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 12px var(--mint);
}
.brand h1 {
margin: 10px 0 5px;
font-size: 26px;
line-height: 1;
letter-spacing: -0.035em;
font-weight: 760;
}
.brand h1 span { color: var(--mint); }
.brand p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
max-width: 290px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 14px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255,255,255,0.045);
color: var(--text);
padding: 8px 10px;
font: 700 10px/1 ui-sans-serif, system-ui, sans-serif;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: background 150ms, border-color 150ms, transform 150ms;
}
button:hover {
background: rgba(86,224,195,0.11);
border-color: rgba(86,224,195,0.42);
}
button:active { transform: translateY(1px); }
.telemetry {
top: 22px;
right: 22px;
width: 326px;
padding: 17px;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.16em;
text-transform: uppercase;
font-weight: 800;
}
.section-title strong {
color: var(--text);
font-size: 10px;
}
.lift-grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 8px;
margin-bottom: 13px;
}
.metric {
min-width: 0;
padding: 10px;
border: 1px solid rgba(255,255,255,0.065);
border-radius: 9px;
background: rgba(255,255,255,0.035);
}
.metric label {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.11em;
text-transform: uppercase;
margin-bottom: 6px;
}
.metric b {
display: block;
font-size: 18px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.metric small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 9px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.occupancy {
display: flex;
align-items: center;
gap: 7px;
margin: 2px 0 12px;
}
.slot {
width: 23px;
height: 23px;
border: 1px solid rgba(255,255,255,0.15);
border-radius: 5px;
background: rgba(255,255,255,0.025);
transition: 180ms;
}
.slot.filled {
background: var(--mint);
border-color: var(--mint);
box-shadow: 0 0 14px rgba(86,224,195,0.38);
}
.capacity {
margin-left: 5px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.09em;
}
.route {
position: relative;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
padding-top: 13px;
}
.route::before {
content: "";
position: absolute;
top: 24px;
left: 16%;
right: 16%;
height: 1px;
background: linear-gradient(90deg, var(--mint), rgba(86,224,195,0.12));
}
.floor-node {
position: relative;
z-index: 1;
padding: 7px 5px;
text-align: center;
border: 1px solid var(--line);
border-radius: 7px;
background: #0c191e;
color: var(--muted);
font-size: 10px;
font-weight: 800;
}
.floor-node.active {
color: #071613;
border-color: var(--mint);
background: var(--mint);
box-shadow: 0 0 18px rgba(86,224,195,0.2);
}
.floor-node.target {
color: var(--amber);
border-color: rgba(255,189,88,0.55);
}
.alignment {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--line);
font-size: 10px;
}
.alignment span { color: var(--muted); }
.alignment b {
color: var(--mint);
letter-spacing: 0.04em;
text-align: right;
}
.queues {
left: 22px;
top: 252px;
width: 348px;
padding: 17px;
}
.queue-row {
display: grid;
grid-template-columns: 44px 1fr 29px;
align-items: center;
gap: 10px;
padding: 10px 0;
border-top: 1px solid rgba(255,255,255,0.055);
}
.queue-row:first-of-type { border-top: 0; }
.floor-badge {
width: 34px;
height: 27px;
display: grid;
place-items: center;
border-radius: 6px;
background: rgba(255,255,255,0.06);
color: var(--text);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
}
.queue-track {
height: 6px;
overflow: hidden;
border-radius: 5px;
background: rgba(255,255,255,0.055);
}
.queue-fill {
height: 100%;
width: 0;
border-radius: inherit;
background: linear-gradient(90deg, var(--mint), #9ce8c8);
transition: width 200ms;
}
.dest-chips {
display: flex;
gap: 4px;
margin-top: 6px;
min-height: 5px;
}
.chip {
width: 18px;
height: 5px;
border-radius: 2px;
opacity: 0.95;
}
.queue-count {
text-align: right;
font-size: 16px;
font-weight: 750;
font-variant-numeric: tabular-nums;
}
.manifest {
right: 22px;
top: 358px;
width: 326px;
padding: 17px;
}
.manifest-list {
max-height: 215px;
overflow: auto;
scrollbar-width: thin;
scrollbar-color: rgba(86,224,195,0.3) transparent;
}
.manifest-item {
display: grid;
grid-template-columns: 7px 1fr auto;
align-items: center;
gap: 9px;
padding: 9px 3px;
border-top: 1px solid rgba(255,255,255,0.055);
cursor: pointer;
}
.manifest-item:first-child { border-top: 0; }
.manifest-item:hover, .manifest-item.selected { background: rgba(255,255,255,0.035); }
.dest-dot {
width: 7px;
height: 24px;
border-radius: 3px;
}
.manifest-id {
font-size: 10px;
font-weight: 800;
letter-spacing: 0.05em;
}
.manifest-route {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.manifest-state {
color: var(--muted);
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
text-align: right;
}
.selection {
left: 22px;
bottom: 22px;
width: 348px;
padding: 14px 17px;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 13px;
}
.selection label {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
margin-bottom: 4px;
}
.selection b {
font-size: 11px;
letter-spacing: 0.03em;
}
.legend {
right: 22px;
bottom: 22px;
padding: 12px 15px;
display: flex;
align-items: center;
gap: 14px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-dot {
width: 8px;
height: 8px;
border-radius: 2px;
}
.help {
left: 50%;
bottom: 24px;
transform: translateX(-50%);
color: rgba(196,219,219,0.54);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
text-shadow: 0 2px 10px #000;
}
@media (max-width: 900px) {
.brand, .queues, .selection { width: 294px; }
.telemetry, .manifest { width: 286px; }
.help { display: none; }
.selection { bottom: 70px; }
.legend { left: 22px; right: auto; bottom: 20px; }
}
@media (max-width: 680px) {
.brand { width: calc(100vw - 28px); top: 14px; left: 14px; padding: 14px; }
.brand p { display: none; }
.brand h1 { font-size: 21px; }
.controls { margin-top: 10px; }
.telemetry { top: 151px; right: 14px; width: 260px; padding: 13px; }
.queues { display: none; }
.manifest { display: none; }
.selection { left: 14px; bottom: 65px; width: 260px; padding: 11px; }
.legend { left: 14px; bottom: 15px; gap: 8px; }
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="hud">
<section class="panel brand">
<div class="eyebrow">
<span>Vertex logistics / live depot</span>
<span class="status-dot" id="runStatus">Running</span>
</div>
<h1>PARCEL <span>HUB</span></h1>
<p>Three-level voxel depot with conveyor intake, destination routing, and a three-slot vertical lift.</p>
<div class="controls">
<button id="pauseBtn">Pause</button>
<button id="speedBtn">1× Speed</button>
<button id="resetBtn">Reset</button>
<button id="viewBtn">Reset View</button>
</div>
</section>
<section class="panel telemetry">
<div class="section-title">
<span>Lift telemetry</span>
<strong id="clock">00:00</strong>
</div>
<div class="lift-grid">
<div class="metric">
<label>Cabin</label>
<b id="cabinFloor">L0</b>
<small id="doorState">Door closed</small>
</div>
<div class="metric">
<label>Occupancy</label>
<b id="occupancyText">0 / 3</b>
<small id="capacityState">3-slot limit</small>
</div>
<div class="metric">
<label>Delivered</label>
<b id="deliveredText">0</b>
<small id="tripsText">0 lift trips</small>
</div>
</div>
<div class="occupancy" id="occupancySlots">
<span class="slot"></span>
<span class="slot"></span>
<span class="slot"></span>
<span class="capacity">CABIN SLOTS</span>
</div>
<div class="route" id="route">
<div class="floor-node">L0</div>
<div class="floor-node">L1</div>
<div class="floor-node">L2</div>
</div>
<div class="alignment">
<span>Door alignment</span>
<b id="alignmentText">L0 / L0</b>
</div>
</section>
<section class="panel queues">
<div class="section-title">
<span>Waiting queues</span>
<strong id="queueTotal">0 parcels</strong>
</div>
<div id="queueRows"></div>
</section>
<section class="panel manifest">
<div class="section-title">
<span>Live manifest</span>
<strong id="inSystem">0 in system</strong>
</div>
<div class="manifest-list" id="manifestList"></div>
</section>
<section class="panel selection">
<div>
<label>Selected parcel</label>
<b id="selectedId">Click a voxel parcel</b>
</div>
<div>
<label>Routing</label>
<b id="selectedRoute">—</b>
</div>
</section>
<section class="panel legend">
<div class="legend-item"><span class="legend-dot" style="background:#56e0c3"></span>L0</div>
<div class="legend-item"><span class="legend-dot" style="background:#ffbd58"></span>L1</div>
<div class="legend-item"><span class="legend-dot" style="background:#c991ff"></span>L2</div>
</section>
<div class="help">Drag to orbit · Shift/right-drag to pan · Wheel to zoom · Click parcel to inspect</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071116);
scene.fog = new THREE.Fog(0x071116, 30, 68);
const renderer = new THREE.WebGLRenderer({ antialias: true, 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;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.querySelector("#scene").appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(42, window.innerWidth / window.innerHeight, 0.1, 120);
const cameraTarget = new THREE.Vector3(-0.5, 5.1, 0);
let cameraTheta = 0.63;
let cameraPhi = 1.04;
let cameraRadius = 37;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xbbe9ef, 0x182127, 2.05));
const keyLight = new THREE.DirectionalLight(0xfff1d6, 3.15);
keyLight.position.set(14, 24, 17);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -23;
keyLight.shadow.camera.right = 23;
keyLight.shadow.camera.top = 23;
keyLight.shadow.camera.bottom = -23;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 60;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x55d8dc, 1.15);
rimLight.position.set(-18, 12, -14);
scene.add(rimLight);
const liftLight = new THREE.PointLight(0x62e4cf, 13, 18, 2);
liftLight.position.set(-4, 8, 2);
scene.add(liftLight);
const materialCache = new Map();
function material(color, options = {}) {
const key = color + JSON.stringify(options);
if (!materialCache.has(key)) {
materialCache.set(key, new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.08,
flatShading: true,
...options
}));
}
return materialCache.get(key);
}
function box(w, h, d, color, x, y, z, parent = scene, mat = null) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
mat || material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextSprite(text, width = 4, height = 0.9, accent = "#56e0c3") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(8, 20, 25, 0.90)";
ctx.fillRect(0, 0, 512, 128);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 12, 128);
ctx.strokeStyle = "rgba(188, 235, 230, 0.32)";
ctx.strokeRect(2, 2, 508, 124);
ctx.fillStyle = "#e8f3f1";
ctx.font = "800 50px ui-sans-serif, system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 260, 67);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false
}));
sprite.scale.set(width, height, 1);
sprite.renderOrder = 8;
return sprite;
}
// -------------------------------------------------------------------------
// Depot geometry
// -------------------------------------------------------------------------
const LEVELS = [
{ name: "L0", y: 0, color: 0x56e0c3, css: "#56e0c3", title: "GROUND SORT" },
{ name: "L1", y: 5, color: 0xffbd58, css: "#ffbd58", title: "REGIONAL" },
{ name: "L2", y: 10, color: 0xc991ff, css: "#c991ff", title: "SKY HUB" }
];
const DEST_COLORS = ["#56e0c3", "#ffbd58", "#c991ff"];
const DEST_HEX = [0x56e0c3, 0xffbd58, 0xc991ff];
const ground = box(52, 0.65, 39, 0x10232a, 0, -0.85, 0);
ground.receiveShadow = true;
const grid = new THREE.GridHelper(52, 52, 0x31525a, 0x183039);
grid.position.y = -0.51;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
function buildFloor(level, index) {
const y = level.y;
const slab = index === 0 ? 0x263a40 : 0x1c3036;
// The central shaft remains open.
box(7, 0.4, 16, slab, -10.5, y - 0.2, 0);
box(15, 0.4, 16, slab, 6.5, y - 0.2, 0);
box(6, 0.4, 6, slab, -4, y - 0.2, -5);
box(6, 0.4, 6, slab, -4, y - 0.2, 5);
box(7.2, 0.26, 0.35, 0x3b5258, -10.5, y - 0.48, 2.6);
box(15.2, 0.26, 0.35, 0x3b5258, 6.5, y - 0.48, 2.6);
// Rear wall and inset panels.
box(28, 4.2, 0.35, index === 0 ? 0x1b3037 : 0x16292f, 0, y + 2.05, -7.72);
for (let x = -12; x <= 12; x += 4) {
box(2.6, 1.55, 0.12, 0x26434b, x, y + 2.15, -7.48);
box(1.8, 0.12, 0.16, level.color, x, y + 1.4, -7.38);
}
const label = makeTextSprite(`${level.name} / ${level.title}`, 4.4, 0.9, level.css);
label.position.set(-10.8, y + 3.7, -7.28);
scene.add(label);
const intake = makeTextSprite("INTAKE →", 2.7, 0.64, level.css);
intake.position.set(10.3, y + 2.2, -0.9);
scene.add(intake);
const sort = makeTextSprite("SORT BAY", 2.6, 0.64, level.css);
sort.position.set(-11.6, y + 2.1, 4);
scene.add(sort);
}
LEVELS.forEach(buildFloor);
// Structural columns and beams.
for (const x of [-13.6, 13.6]) {
for (const z of [-7.35, 7.35]) {
box(0.5, 14.8, 0.5, 0x30484f, x, 6.7, z);
box(0.74, 0.22, 0.74, 0x486269, x, 0.02, z);
}
}
for (let i = 0; i < 3; i++) {
const y = LEVELS[i].y;
box(27.5, 0.32, 0.42, 0x30484f, 0, y + 3.65, -7.35);
box(27.5, 0.32, 0.42, 0x30484f, 0, y + 3.65, 7.35);
box(0.38, 0.32, 14.5, 0x30484f, -13.6, y + 3.65, 0);
box(0.38, 0.32, 14.5, 0x30484f, 13.6, y + 3.65, 0);
}
// Top canopy and roof equipment.
box(28.2, 0.32, 15.2, 0x243b42, 0, 13.75, 0);
for (let x = -10; x <= 10; x += 5) {
box(2.2, 0.8, 2.0, 0x355058, x, 14.35, -2.8);
box(1.35, 0.25, 1.2, 0x4c6a70, x, 14.85, -2.8);
}
// Shaft frame.
for (const x of [-7.25, -0.78]) {
for (const z of [-2.12, 2.12]) {
box(0.28, 14.8, 0.28, 0x49666d, x, 6.7, z);
}
}
for (const z of [-1.7, 1.7]) {
box(0.14, 14.5, 0.14, 0x88a4a8, -6.7, 6.7, z);
}
const shaftLabel = makeTextSprite("VERTEX LIFT / CAP 3", 5.4, 1.0, "#56e0c3");
shaftLabel.position.set(-4, 14.55, 0);
scene.add(shaftLabel);
// Conveyor construction.
const beltMarkers = [];
function makeBeltX(a, b, z, y, accent, direction = 1) {
const length = Math.abs(b - a);
const center = (a + b) / 2;
box(length, 0.25, 1.28, 0x17272c, center, y + 0.4, z);
box(length, 0.12, 0.12, 0x53666b, center, y + 0.66, z - 0.67);
box(length, 0.12, 0.12, 0x53666b, center, y + 0.66, z + 0.67);
const count = Math.max(2, Math.floor(length / 0.65));
for (let i = 0; i < count; i++) {
const x = a + (b - a) * ((i + 0.5) / count);
box(0.17, 0.11, 0.94, 0x5a6b6d, x, y + 0.57, z);
}
const markerCount = Math.max(2, Math.floor(length / 1.6));
for (let i = 0; i < markerCount; i++) {
const marker = box(0.16, 0.075, 0.72, accent, 0, y + 0.66, z);
beltMarkers.push({
mesh: marker,
axis: "x",
a,
b,
offset: i / markerCount,
speed: 0.12 * direction
});
}
}
function makeBeltZ(a, b, x, y, accent, direction = 1) {
const length = Math.abs(b - a);
const center = (a + b) / 2;
box(1.28, 0.25, length, 0x17272c, x, y + 0.4, center);
box(0.12, 0.12, length, 0x53666b, x - 0.67, y + 0.66, center);
box(0.12, 0.12, length, 0x53666b, x + 0.67, y + 0.66, center);
const count = Math.max(2, Math.floor(length / 0.65));
for (let i = 0; i < count; i++) {
const z = a + (b - a) * ((i + 0.5) / count);
box(0.94, 0.11, 0.17, 0x5a6b6d, x, y + 0.57, z);
}
const markerCount = Math.max(1, Math.floor(length / 1.4));
for (let i = 0; i < markerCount; i++) {
const marker = box(0.72, 0.075, 0.16, accent, x, y + 0.66, 0);
beltMarkers.push({
mesh: marker,
axis: "z",
a,
b,
offset: i / markerCount,
speed: 0.14 * direction
});
}
}
const landingBeacons = [];
for (const level of LEVELS) {
const y = level.y;
makeBeltX(0.8, 11.2, 0, y, level.color, -1);
makeBeltZ(0, 4, 1, y, level.color, 1);
makeBeltX(-11.2, 1, 4, y, level.color, -1);
// Queue landing pads.
for (let i = 0; i < 4; i++) {
const x = 5.25 - i * 1.25;
box(0.88, 0.065, 1.0, 0xd7aa43, x, y + 0.63, 0);
box(0.58, 0.07, 0.72, 0x26393e, x, y + 0.68, 0);
}
// Fixed landing frame and status beacon.
box(0.22, 2.9, 0.22, 0x6d868b, -0.85, y + 1.45, -1.55);
box(0.22, 2.9, 0.22, 0x6d868b, -0.85, y + 1.45, 1.55);
box(0.28, 0.24, 3.3, 0x40585e, -0.85, y + 2.92, 0);
const beaconMat = new THREE.MeshStandardMaterial({
color: 0x26433f,
emissive: 0x000000,
emissiveIntensity: 1.2,
roughness: 0.45
});
const beacon = box(0.18, 0.32, 0.42, 0xffffff, -0.69, y + 2.55, 1.55, scene, beaconMat);
landingBeacons.push({ mesh: beacon, material: beaconMat, level: LEVELS.indexOf(level) });
// Sorted destination cubbies.
for (let d = 0; d < 3; d++) {
const x = -12.8 + d * 1.15;
box(0.9, 1.25, 1.1, 0x26383d, x, y + 0.65, 5.6);
box(0.68, 0.58, 0.09, DEST_HEX[d], x, y + 0.83, 5.01);
}
}
// Decorative pallets and voxel inventory.
for (let i = 0; i < 3; i++) {
const x = -10.2 + i * 2.3;
box(1.7, 0.18, 1.2, 0x735d43, x, 0.12, -5.4);
for (let j = 0; j < 3; j++) {
box(0.75, 0.65, 0.78, DEST_HEX[(i + j) % 3], x - 0.42 + j * 0.43, 0.54, -5.35);
}
}
// -------------------------------------------------------------------------
// Cabin and landing mechanisms
// -------------------------------------------------------------------------
const cabin = new THREE.Group();
scene.add(cabin);
const cabinGlass = new THREE.MeshStandardMaterial({
color: 0x73bdc4,
transparent: true,
opacity: 0.17,
roughness: 0.25,
metalness: 0.08,
depthWrite: false
});
box(5.7, 0.24, 3.45, 0x35545b, -4, 0.16, 0, cabin);
box(0.16, 2.9, 3.45, 0x5b8188, -6.8, 1.65, 0, cabin, cabinGlass);
box(5.7, 2.9, 0.14, 0x5b8188, -4, 1.65, -1.68, cabin, cabinGlass);
box(5.7, 2.9, 0.14, 0x5b8188, -4, 1.65, 1.68, cabin, cabinGlass);
box(5.9, 0.16, 3.6, 0x4a6970, -4, 3.08, 0, cabin);
const doorPanelMaterial = new THREE.MeshStandardMaterial({
color: 0x74a6ac,
transparent: true,
opacity: 0.38,
roughness: 0.32,
metalness: 0.18
});
const doorPanelA = box(0.16, 2.5, 1.22, 0xffffff, -1.02, 1.48, -0.62, cabin, doorPanelMaterial);
const doorPanelB = box(0.16, 2.5, 1.22, 0xffffff, -1.02, 1.48, 0.62, cabin, doorPanelMaterial);
const cabinLampMat = new THREE.MeshStandardMaterial({
color: 0x8fffe4,
emissive: 0x56e0c3,
emissiveIntensity: 1.5
});
box(2.8, 0.08, 0.16, 0xffffff, -4, 2.92, 0, cabin, cabinLampMat);
// -------------------------------------------------------------------------
// Simulation state
// -------------------------------------------------------------------------
const CAPACITY = 3;
const QUEUE_LIMIT = 4;
const parcels = [];
const queues = [[], [], []];
const clickableParcels = [];
let parcelCounter = 0;
let selectedId = null;
let delivered = 0;
let trips = 0;
let simTime = 0;
let running = true;
let simSpeed = 1;
const spawnTimers = [0.8, 2.6, 4.5];
const spawnCounter = [0, 0, 0];
const destinationPreference = [
[1, 2, 1, 2],
[2, 0, 2, 0],
[1, 0, 1, 0]
];
const lift = {
level: 0,
target: 0,
phase: "closed",
door: 0,
visualY: 0,
fromY: 0,
toY: 0,
moveT: 0,
moveDuration: 2.6,
openIdle: 0,
occupants: []
};
const queuePositions = [
new THREE.Vector3(5.25, 1.02, 0),
new THREE.Vector3(4.0, 1.02, 0),
new THREE.Vector3(2.75, 1.02, 0),
new THREE.Vector3(1.5, 1.02, 0)
];
function createParcel(origin, destination, queued = false) {
const id = `PX-${String(++parcelCounter).padStart(3, "0")}`;
const group = new THREE.Group();
group.userData.parcelId = id;
const body = box(0.78, 0.68, 0.78, DEST_HEX[destination], 0, 0, 0, group);
const tape = box(0.17, 0.705, 0.805, 0xe9dfc4, 0, 0, 0, group);
const topTag = box(0.45, 0.055, 0.45, DEST_HEX[destination], 0.16, 0.37, 0, group);
for (let i = 0; i < 4; i++) {
box(0.045, 0.19, 0.025, 0x1b292c, -0.24 + i * 0.11, -0.05, 0.402, group);
}
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(0.83, 0.73, 0.83)),
new THREE.LineBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.95 })
);
edge.visible = false;
group.add(edge);
scene.add(group);
const parcel = {
id,
origin,
destination,
state: queued ? "queue" : "arriving",
mesh: group,
body,
tape,
topTag,
edge,
position: new THREE.Vector3(11.5, LEVELS[origin].y + 1.02, 0),
slot: -1,
boardT: 0,
boardStart: new THREE.Vector3(),
unloadT: 0,
unloadDuration: 1,
unloadPath: [],
outboundT: 0,
outboundDuration: 1,
outboundPath: []
};
parcels.push(parcel);
clickableParcels.push(group);
if (queued) {
queues[origin].push(parcel);
const index = queues[origin].length - 1;
parcel.position.copy(queuePositions[Math.min(index, QUEUE_LIMIT - 1)]);
}
return parcel;
}
function disposeParcel(parcel) {
scene.remove(parcel.mesh);
parcel.mesh.traverse(child => {
if (child.geometry) child.geometry.dispose();
if (child.material && child.material !== materialCache.get(child.material.color.getHexString())) {
// Shared materials are retained; line-only materials are disposable.
if (child.isLineSegments) child.material.dispose();
}
});
const clickIndex = clickableParcels.indexOf(parcel.mesh);
if (clickIndex >= 0) clickableParcels.splice(clickIndex, 1);
}
function slotPosition(index, y = lift.visualY) {
return new THREE.Vector3(
-5.35 + index * 1.3,
y + 0.94,
-0.48
);
}
function updateSpawning(dt) {
for (let origin = 0; origin < 3; origin++) {
spawnTimers[origin] -= dt;
if (spawnTimers[origin] > 0) continue;
spawnTimers[origin] = 4.4 + Math.random() * 2.1;
const floorLoad = queues[origin].length +
parcels.filter(p => p.origin === origin && p.state === "arriving").length;
if (parcels.length >= 18 || floorLoad >= 5) continue;
const pref = destinationPreference[origin];
const destination = pref[spawnCounter[origin] % pref.length];
spawnCounter[origin]++;
createParcel(origin, destination, false);
}
}
function updateQueues(dt) {
const blend = 1 - Math.exp(-dt * 10);
for (let level = 0; level < 3; level++) {
queues[level].forEach((parcel, index) => {
if (index >= queuePositions.length) return;
parcel.position.lerp(queuePositions[index], blend);
});
}
}
function chooseOccupantTarget() {
const counts = [0, 0, 0];
for (const parcel of lift.occupants) {
if (parcel.state === "cabin") counts[parcel.destination]++;
}
let best = -1;
let bestDistance = Infinity;
for (let d = 0; d < 3; d++) {
if (!counts[d] || d === lift.level) continue;
const distance = Math.abs(d - lift.level);
if (distance < bestDistance) {
bestDistance = distance;
best = d;
}
}
return best;
}
function chooseQueueRequest() {
let best = -1;
let bestDistance = Infinity;
for (let level = 0; level < 3; level++) {
if (level === lift.level || queues[level].length === 0) continue;
const distance = Math.abs(level - lift.level);
if (distance < bestDistance) {
bestDistance = distance;
best = level;
}
}
return best;
}
function beginBoarding() {
const queue = queues[lift.level];
if (!queue.length || lift.occupants.length >= CAPACITY) return false;
const parcel = queue.shift();
const usedSlots = new Set(lift.occupants.map(p => p.slot));
let slot = 0;
while (usedSlots.has(slot)) slot++;
parcel.state = "boarding";
parcel.slot = slot;
parcel.boardT = 0;
parcel.boardStart.copy(parcel.position);
lift.occupants.push(parcel);
return true;
}
function beginUnloading() {
const parcel = lift.occupants.find(p => p.state === "cabin" && p.destination === lift.level);
if (!parcel) return false;
const y = LEVELS[lift.level].y + 1.02;
parcel.state = "unloading";
parcel.unloadT = 0;
parcel.unloadPath = [
parcel.position.clone(),
new THREE.Vector3(-0.55, y, -0.48),
new THREE.Vector3(-0.55, y, 0),
new THREE.Vector3(1.1, y, 0)
];
let length = 0;
for (let i = 1; i < parcel.unloadPath.length; i++) {
length += parcel.unloadPath[i].distanceTo(parcel.unloadPath[i - 1]);
}
parcel.unloadDuration = Math.max(1.3, length / 2.3);
return true;
}
function startMoving(target) {
if (target < 0 || target === lift.level) return;
lift.target = target;
lift.phase = "moving";
lift.fromY = lift.visualY;
lift.toY = LEVELS[target].y;
lift.moveT = 0;
lift.moveDuration = 1.65 + Math.abs(target - lift.level) * 0.48;
}
function updateLift(dt) {
switch (lift.phase) {
case "closed": {
const unloadCandidate = lift.occupants.some(
p => p.state === "cabin" && p.destination === lift.level
);
if (unloadCandidate) {
lift.phase = "opening";
} else if (lift.occupants.length < CAPACITY && queues[lift.level].length > 0) {
lift.phase = "opening";
} else if (lift.occupants.length > 0) {
const target = chooseOccupantTarget();
if (target >= 0) startMoving(target);
} else {
const request = chooseQueueRequest();
if (request >= 0) startMoving(request);
}
break;
}
case "opening": {
lift.door = Math.min(1, lift.door + dt / 0.72);
if (lift.door >= 1) {
lift.phase = "open";
lift.openIdle = 0;
}
break;
}
case "open": {
const activeTransfer = lift.occupants.some(
p => p.state === "boarding" || p.state === "unloading"
);
if (activeTransfer) break;
const unloadCandidate = lift.occupants.some(
p => p.state === "cabin" && p.destination === lift.level
);
if (unloadCandidate) {
beginUnloading();
break;
}
if (lift.occupants.length < CAPACITY && queues[lift.level].length > 0) {
beginBoarding();
break;
}
lift.openIdle += dt;
if (lift.openIdle >= 0.72) {
const target = lift.occupants.length > 0
? chooseOccupantTarget()
: chooseQueueRequest();
if (target >= 0 && target !== lift.level) lift.target = target;
lift.phase = "closing";
}
break;
}
case "closing": {
lift.door = Math.max(0, lift.door - dt / 0.68);
if (lift.door <= 0) lift.phase = "closed";
break;
}
case "moving": {
lift.moveT += dt / lift.moveDuration;
const t = Math.min(1, lift.moveT);
const smooth = t * t * (3 - 2 * t);
lift.visualY = THREE.MathUtils.lerp(lift.fromY, lift.toY, smooth);
if (t >= 1) {
lift.level = lift.target;
lift.visualY = LEVELS[lift.level].y;
trips++;
lift.phase = "closed";
}
break;
}
}
cabin.position.y = lift.visualY;
doorPanelA.position.z = -0.62 - lift.door * 1.02;
doorPanelB.position.z = 0.62 + lift.door * 1.02;
}
function pointAlongPath(path, t) {
if (path.length === 1) return path[0].clone();
const lengths = [];
let total = 0;
for (let i = 1; i < path.length; i++) {
const length = path[i].distanceTo(path[i - 1]);
lengths.push(length);
total += length;
}
let distance = t * total;
for (let i = 0; i < lengths.length; i++) {
if (distance <= lengths[i] || i === lengths.length - 1) {
const local = lengths[i] === 0 ? 0 : distance / lengths[i];
return path[i].clone().lerp(path[i + 1], Math.min(1, local));
}
distance -= lengths[i];
}
return path[path.length - 1].clone();
}
function updateParcels(dt) {
for (const parcel of [...parcels]) {
if (parcel.state === "arriving") {
parcel.position.x -= dt * 1.72;
if (parcel.position.x <= 5.45 && queues[parcel.origin].length < QUEUE_LIMIT) {
parcel.state = "queue";
queues[parcel.origin].push(parcel);
} else {
parcel.position.x = Math.max(parcel.position.x, 5.48);
}
}
if (parcel.state === "boarding") {
parcel.boardT += dt / 1.72;
const t = Math.min(1, parcel.boardT);
const slot = slotPosition(parcel.slot, LEVELS[lift.level].y);
const door = new THREE.Vector3(0.12, LEVELS[lift.level].y + 1.02, 0);
if (t < 0.54) {
parcel.position.copy(parcel.boardStart).lerp(door, t / 0.54);
} else {
parcel.position.copy(door).lerp(slot, (t - 0.54) / 0.46);
}
if (t >= 1) {
parcel.state = "cabin";
parcel.position.copy(slot);
}
}
if (parcel.state === "cabin") {
parcel.position.copy(slotPosition(parcel.slot, lift.visualY));
}
if (parcel.state === "unloading") {
parcel.unloadT += dt / parcel.unloadDuration;
parcel.position.copy(pointAlongPath(parcel.unloadPath, Math.min(1, parcel.unloadT)));
if (parcel.unloadT >= 1) {
const occupantIndex = lift.occupants.indexOf(parcel);
if (occupantIndex >= 0) lift.occupants.splice(occupantIndex, 1);
const y = LEVELS[lift.level].y + 1.02;
parcel.state = "outbound";
parcel.outboundT = 0;
parcel.outboundPath = [
new THREE.Vector3(1.1, y, 0),
new THREE.Vector3(1.1, y, 4),
new THREE.Vector3(-11.0, y, 4)
];
let length = 0;
for (let i = 1; i < parcel.outboundPath.length; i++) {
length += parcel.outboundPath[i].distanceTo(parcel.outboundPath[i - 1]);
}
parcel.outboundDuration = length / 3.15;
}
}
if (parcel.state === "outbound") {
parcel.outboundT += dt / parcel.outboundDuration;
parcel.position.copy(pointAlongPath(parcel.outboundPath, Math.min(1, parcel.outboundT)));
if (parcel.outboundT >= 1) {
delivered++;
if (selectedId === parcel.id) selectedId = null;
const index = parcels.indexOf(parcel);
if (index >= 0) parcels.splice(index, 1);
disposeParcel(parcel);
continue;
}
}
parcel.mesh.position.copy(parcel.position);
parcel.mesh.rotation.y = parcel.state === "outbound"
? Math.PI / 2
: 0;
}
}
function updateBeltMarkers() {
for (const marker of beltMarkers) {
const length = Math.abs(marker.b - marker.a);
const progress = (simTime * marker.speed + marker.offset) % 1;
const value = THREE.MathUtils.lerp(marker.a, marker.b, progress);
if (marker.axis === "x") marker.mesh.position.x = value;
else marker.mesh.position.z = value;
}
}
function updateBeaconLights() {
for (const beacon of landingBeacons) {
const aligned = lift.phase !== "moving" && beacon.level === lift.level;
const target = beacon.level === lift.target;
beacon.material.color.setHex(aligned ? 0x56e0c3 : target ? 0xffbd58 : 0x29413f);
beacon.material.emissive.setHex(aligned ? 0x56e0c3 : target ? 0xffbd58 : 0x000000);
beacon.material.emissiveIntensity = aligned || target ? 1.8 : 0;
}
const doorMaterial = lift.phase === "moving"
? 0xffbd58
: lift.door > 0.85
? 0x56e0c3
: 0x74a6ac;
doorPanelMaterial.color.setHex(doorMaterial);
doorPanelMaterial.emissive = new THREE.Color(doorMaterial);
doorPanelMaterial.emissiveIntensity = lift.door > 0.85 ? 0.12 : 0;
}
function resetSimulation() {
for (const parcel of [...parcels]) disposeParcel(parcel);
parcels.length = 0;
queues.forEach(queue => queue.length = 0);
lift.level = 0;
lift.target = 0;
lift.phase = "closed";
lift.door = 0;
lift.visualY = 0;
lift.fromY = 0;
lift.toY = 0;
lift.moveT = 0;
lift.openIdle = 0;
lift.occupants.length = 0;
parcelCounter = 0;
selectedId = null;
delivered = 0;
trips = 0;
simTime = 0;
spawnTimers.splice(0, spawnTimers.length, 0.8, 2.6, 4.5);
spawnCounter.splice(0, spawnCounter.length, 0, 0, 0);
createParcel(0, 1, true);
createParcel(0, 2, true);
createParcel(1, 2, true);
createParcel(2, 1, true);
cabin.position.y = 0;
doorPanelA.position.z = -0.62;
doorPanelB.position.z = 0.62;
renderUI();
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const ui = {
runStatus: document.querySelector("#runStatus"),
clock: document.querySelector("#clock"),
cabinFloor: document.querySelector("#cabinFloor"),
doorState: document.querySelector("#doorState"),
occupancyText: document.querySelector("#occupancyText"),
capacityState: document.querySelector("#capacityState"),
deliveredText: document.querySelector("#deliveredText"),
tripsText: document.querySelector("#tripsText"),
occupancySlots: [...document.querySelectorAll("#occupancySlots .slot")],
route: [...document.querySelectorAll("#route .floor-node")],
alignmentText: document.querySelector("#alignmentText"),
queueRows: document.querySelector("#queueRows"),
queueTotal: document.querySelector("#queueTotal"),
manifestList: document.querySelector("#manifestList"),
inSystem: document.querySelector("#inSystem"),
selectedId: document.querySelector("#selectedId"),
selectedRoute: document.querySelector("#selectedRoute"),
pauseBtn: document.querySelector("#pauseBtn"),
speedBtn: document.querySelector("#speedBtn"),
resetBtn: document.querySelector("#resetBtn"),
viewBtn: document.querySelector("#viewBtn")
};
ui.queueRows.innerHTML = LEVELS.map(level => `
<div class="queue-row" id="queue-${level.name}">
<div class="floor-badge">${level.name}</div>
<div>
<div class="queue-track"><div class="queue-fill" id="queue-fill-${level.name}"></div></div>
<div class="dest-chips" id="queue-chips-${level.name}"></div>
</div>
<div class="queue-count" id="queue-count-${level.name}">0</div>
</div>
`).join("");
const stateLabels = {
arriving: "Intake",
queue: "Waiting",
boarding: "Loading",
cabin: "In lift",
unloading: "Unloading",
outbound: "To sort"
};
function renderUI() {
const totalWaiting = queues.reduce((sum, queue) => sum + queue.length, 0);
const minutes = Math.floor(simTime / 60).toString().padStart(2, "0");
const seconds = Math.floor(simTime % 60).toString().padStart(2, "0");
ui.runStatus.textContent = running ? "Running" : "Paused";
ui.runStatus.style.color = running ? "" : "#ffbd58";
ui.clock.textContent = `${minutes}:${seconds}`;
ui.cabinFloor.textContent = `L${lift.level}`;
if (lift.phase === "moving") {
ui.doorState.textContent = `Travelling L${lift.target}`;
ui.alignmentText.textContent = `L${lift.level} → L${lift.target}`;
ui.alignmentText.style.color = "#ffbd58";
} else {
ui.doorState.textContent = lift.door > 0.85 ? "Door open" : "Door closed";
ui.alignmentText.textContent = `L${lift.level} / LANDING L${lift.level}`;
ui.alignmentText.style.color = "#56e0c3";
}
ui.occupancyText.textContent = `${lift.occupants.length} / ${CAPACITY}`;
ui.capacityState.textContent = lift.occupants.length >= CAPACITY
? "At capacity"
: `${CAPACITY - lift.occupants.length} slot${CAPACITY - lift.occupants.length === 1 ? "" : "s"} open`;
ui.occupancySlots.forEach((slot, index) => {
slot.classList.toggle("filled", index < lift.occupants.length);
});
ui.deliveredText.textContent = delivered;
ui.tripsText.textContent = `${trips} lift trip${trips === 1 ? "" : "s"}`;
ui.queueTotal.textContent = `${totalWaiting} parcel${totalWaiting === 1 ? "" : "s"}`;
ui.inSystem.textContent = `${parcels.length} in system`;
ui.route.forEach((node, index) => {
node.classList.toggle("active", lift.phase !== "moving" && index === lift.level);
node.classList.toggle("target", lift.phase === "moving" && index === lift.target);
});
queues.forEach((queue, level) => {
document.querySelector(`#queue-count-L${level}`).textContent = queue.length;
document.querySelector(`#queue-fill-L${level}`).style.width = `${queue.length / QUEUE_LIMIT * 100}%`;
const counts = [0, 0, 0];
queue.forEach(p => counts[p.destination]++);
document.querySelector(`#queue-chips-L${level}`).innerHTML = counts.map((count, d) =>
count ? `<span class="chip" style="background:${DEST_COLORS[d]};width:${Math.max(8, count * 6)}px"></span>` : ""
).join("");
});
const visibleParcels = [...parcels].sort((a, b) => b.id.localeCompare(a.id)).slice(0, 9);
ui.manifestList.innerHTML = visibleParcels.map(p => `
<div class="manifest-item ${selectedId === p.id ? "selected" : ""}" data-id="${p.id}">
<span class="dest-dot" style="background:${DEST_COLORS[p.destination]}"></span>
<div>
<div class="manifest-id">${p.id}</div>
<div class="manifest-route">L${p.origin} → L${p.destination}</div>
</div>
<div class="manifest-state">${stateLabels[p.state] || p.state}</div>
</div>
`).join("");
ui.manifestList.querySelectorAll(".manifest-item").forEach(item => {
item.addEventListener("click", () => selectParcel(item.dataset.id));
});
const selected = parcels.find(p => p.id === selectedId);
if (selected) {
ui.selectedId.textContent = `${selected.id} · ${stateLabels[selected.state]}`;
ui.selectedRoute.textContent = `L${selected.origin} → L${selected.destination}`;
ui.selectedRoute.style.color = DEST_COLORS[selected.destination];
} else {
ui.selectedId.textContent = "Click a voxel parcel";
ui.selectedRoute.textContent = "—";
ui.selectedRoute.style.color = "";
}
}
function selectParcel(id) {
selectedId = id;
for (const parcel of parcels) {
parcel.edge.visible = parcel.id === id;
}
renderUI();
}
ui.pauseBtn.addEventListener("click", () => {
running = !running;
ui.pauseBtn.textContent = running ? "Pause" : "Resume";
renderUI();
});
ui.speedBtn.addEventListener("click", () => {
simSpeed = simSpeed === 1 ? 2 : simSpeed === 2 ? 0.5 : 1;
ui.speedBtn.textContent = `${simSpeed}× Speed`;
});
ui.resetBtn.addEventListener("click", resetSimulation);
ui.viewBtn.addEventListener("click", () => {
cameraTheta = 0.63;
cameraPhi = 1.04;
cameraRadius = 37;
cameraTarget.set(-0.5, 5.1, 0);
updateCamera();
});
// -------------------------------------------------------------------------
// Direct camera controls and parcel picking
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let dragButton = 0;
let lastX = 0;
let lastY = 0;
let pointerTravel = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
dragButton = event.button;
lastX = event.clientX;
lastY = event.clientY;
pointerTravel = 0;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
pointerTravel += Math.abs(dx) + Math.abs(dy);
if (event.shiftKey || dragButton === 2) {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const scale = cameraRadius * 0.0014;
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.addScaledVector(up, dy * scale);
cameraTarget.x = THREE.MathUtils.clamp(cameraTarget.x, -13, 13);
cameraTarget.y = THREE.MathUtils.clamp(cameraTarget.y, 1, 13);
cameraTarget.z = THREE.MathUtils.clamp(cameraTarget.z, -10, 10);
} else {
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi - dy * 0.005, 0.32, 1.43);
}
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
renderer.domElement.releasePointerCapture(event.pointerId);
if (pointerTravel > 6) return;
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 intersections = raycaster.intersectObjects(clickableParcels, true);
if (intersections.length) {
let object = intersections[0].object;
while (object && !object.userData.parcelId) object = object.parent;
if (object?.userData.parcelId) selectParcel(object.userData.parcelId);
}
});
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(cameraRadius * Math.exp(event.deltaY * 0.001), 19, 55);
updateCamera();
}, { passive: false });
window.addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
ui.pauseBtn.click();
}
if (event.key.toLowerCase() === "r") resetSimulation();
if (event.key.toLowerCase() === "h") ui.viewBtn.click();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
resetSimulation();
const clock = new THREE.Clock();
let uiTimer = 0;
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (running) {
const dt = delta * simSpeed;
simTime += dt;
updateSpawning(dt);
updateQueues(dt);
updateLift(dt);
updateParcels(dt);
updateBeltMarkers();
updateBeaconLights();
}
uiTimer += delta;
if (uiTimer >= 0.1) {
uiTimer = 0;
renderUI();
}
renderer.render(scene, camera);
});
</script>
</body>
</html>
```