Prompt specification
Build or customize the Three.js 3D scene "Selective demolition sorting courtyard" 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: Selective demolition sorting courtyard
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/selective-demolition-sorting-courtyard-x2183
Scene Description Prompt:
Sort voxel demolition components into timber, masonry, metal, and mixed-waste areas along a clear one-way yard route. Create standalone HTML with Three.js from cdnjs.
Working Three.js Starter (selective-demolition-sorting-courtyard-x2183):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Demolition Sorting Yard</title>
<style>
:root {
--ink: #17211f;
--muted: #63706c;
--panel: rgba(250, 252, 248, 0.91);
--border: rgba(23, 33, 31, 0.12);
--timber: #c8874b;
--masonry: #a3a8ad;
--metal: #4ea1ad;
--mixed: #8e6ac7;
--accent: #f3b844;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9ede7;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 14px 38px rgba(25, 38, 34, 0.12);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
#title-card {
top: 22px;
left: 22px;
width: min(460px, calc(100vw - 44px));
padding: 19px 21px 17px;
border-radius: 16px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: #52605c;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.live-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #45a875;
box-shadow: 0 0 0 5px rgba(69, 168, 117, 0.13);
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 31px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#legend-card {
left: 22px;
bottom: 22px;
width: min(570px, calc(100vw - 44px));
padding: 14px 16px;
border-radius: 14px;
}
.legend-title {
margin-bottom: 10px;
color: #65716e;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 8px;
}
.legend-item {
display: flex;
align-items: center;
min-width: 0;
gap: 8px;
padding: 8px 9px;
border: 1px solid rgba(23, 33, 31, 0.08);
border-radius: 9px;
background: rgba(255, 255, 255, 0.55);
font-size: 11px;
font-weight: 750;
white-space: nowrap;
}
.swatch {
flex: 0 0 auto;
width: 12px;
height: 12px;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.swatch.timber { background: var(--timber); }
.swatch.masonry { background: var(--masonry); }
.swatch.metal { background: var(--metal); }
.swatch.mixed { background: var(--mixed); }
#control-card {
top: 22px;
right: 22px;
width: 192px;
padding: 12px;
border-radius: 14px;
}
.status-row {
display: flex;
justify-content: space-between;
align-items: baseline;
margin: 2px 3px 11px;
}
.status-label {
color: #68736f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#counter {
font-size: 13px;
font-weight: 800;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
min-height: 38px;
border: 1px solid rgba(23, 33, 31, 0.13);
border-radius: 9px;
background: rgba(255, 255, 255, 0.72);
color: var(--ink);
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 800;
transition:
transform 140ms ease,
background 140ms ease;
}
button:hover {
background: white;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#pause-button {
grid-column: 1 / -1;
background: #1d2926;
color: white;
}
#hint {
position: fixed;
z-index: 8;
right: 22px;
bottom: 22px;
padding: 9px 12px;
border: 1px solid rgba(23, 33, 31, 0.1);
border-radius: 10px;
background: rgba(250, 252, 248, 0.78);
color: #68736f;
font-size: 11px;
pointer-events: none;
backdrop-filter: blur(10px);
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: #e9ede7;
color: #53605c;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle {
display: none;
}
#control-card {
top: auto;
right: 12px;
bottom: 128px;
width: 156px;
}
#legend-card {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 11px;
}
.legend {
grid-template-columns: repeat(2, 1fr);
}
.legend-item {
padding: 7px;
font-size: 10px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive 3D demolition material sorting yard"
></canvas>
<section id="title-card" class="panel">
<div class="eyebrow">
<span class="live-dot"></span>
Material recovery yard · flow 04
</div>
<h1>Voxel Demolition Sorting</h1>
<p class="subtitle">
A one-way yard route separates recovered demolition components into
timber, masonry, metal, and mixed-waste areas.
</p>
</section>
<section id="control-card" class="panel">
<div class="status-row">
<span class="status-label">Sorted loads</span>
<span id="counter">00</span>
</div>
<div class="button-row">
<button id="pause-button" type="button">Pause flow</button>
<button id="restart-button" type="button">Restart</button>
<button id="view-button" type="button">Reset view</button>
</div>
</section>
<section id="legend-card" class="panel">
<div class="legend-title">Material destination key</div>
<div class="legend">
<div class="legend-item">
<span class="swatch timber"></span>
Timber
</div>
<div class="legend-item">
<span class="swatch masonry"></span>
Masonry
</div>
<div class="legend-item">
<span class="swatch metal"></span>
Metal
</div>
<div class="legend-item">
<span class="swatch mixed"></span>
Mixed waste
</div>
</div>
</section>
<div id="hint">Drag to orbit · wheel to zoom · right-drag to pan</div>
<div id="loading">Building recovery yard…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const counter = document.querySelector("#counter");
const renderer = new THREE.WebGLRenderer({
canvas,
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.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9ede7);
scene.fog = new THREE.Fog(0xe9ede7, 48, 88);
const camera = new THREE.PerspectiveCamera(
48,
window.innerWidth / window.innerHeight,
0.1,
180
);
const hemisphere = new THREE.HemisphereLight(0xf7fbf4, 0x77847e, 2.35);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff6df, 3.25);
sun.position.set(-14, 24, 15);
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.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xd9edff, 1.15);
fill.position.set(18, 10, -18);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared geometry and materials
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const unitEdges = new THREE.EdgesGeometry(unitBox);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x17211f,
transparent: true,
opacity: 0.26
});
const materialCache = new Map();
function getMaterial(color, metal = false) {
const key = `${color}-${metal}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: metal ? 0.32 : 0.77,
metalness: metal ? 0.62 : 0.03
})
);
}
return materialCache.get(key);
}
function addBox(
parent,
dimensions,
position,
color,
options = {}
) {
const mesh = new THREE.Mesh(
unitBox,
options.material || getMaterial(color)
);
mesh.scale.set(...dimensions);
mesh.position.set(...position);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function addVoxel(
parent,
dimensions,
position,
color,
rotationY = 0,
metal = false
) {
const mesh = addBox(
parent,
dimensions,
position,
color,
{ material: getMaterial(color, metal) }
);
mesh.rotation.y = rotationY;
const outline = new THREE.LineSegments(unitEdges, edgeMaterial);
mesh.add(outline);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeLabel(
lines,
background = "#1d2926",
foreground = "#ffffff",
width = 5,
height = 1.25
) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 768;
labelCanvas.height = 192;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, labelCanvas.width, labelCanvas.height);
roundedRect(
ctx,
10,
10,
labelCanvas.width - 20,
labelCanvas.height - 20,
30
);
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.22)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = foreground;
if (lines.length === 1) {
ctx.font = "800 64px Inter, Arial, sans-serif";
ctx.fillText(lines[0], 384, 98);
} else {
ctx.font = "800 61px Inter, Arial, sans-serif";
ctx.fillText(lines[0], 384, 73);
ctx.globalAlpha = 0.78;
ctx.font = "700 31px Inter, Arial, sans-serif";
ctx.fillText(lines[1], 384, 132);
}
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 10;
return sprite;
}
// -------------------------------------------------------------------------
// Yard, road and conveyor
// -------------------------------------------------------------------------
addBox(scene, [54, 0.3, 30], [0, -0.18, 0], 0xb9c0b8);
addBox(scene, [46, 0.12, 3.35], [0, 0.03, 4.8], 0x59635f, {
castShadow: false
});
const grid = new THREE.GridHelper(54, 54, 0x7d8982, 0x8f9992);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.17;
scene.add(grid);
// Dashed road edge markings.
for (let x = -21; x <= 21; x += 3.1) {
addBox(
scene,
[1.65, 0.035, 0.09],
[x, 0.115, 3.28],
0xf1e7c5,
{ castShadow: false }
);
addBox(
scene,
[1.65, 0.035, 0.09],
[x, 0.115, 6.32],
0xf1e7c5,
{ castShadow: false }
);
}
function addRoadArrow(x, z, scale = 1) {
const shape = new THREE.Shape();
shape.moveTo(-0.75, -0.16);
shape.lineTo(0.08, -0.16);
shape.lineTo(0.08, -0.48);
shape.lineTo(0.85, 0);
shape.lineTo(0.08, 0.48);
shape.lineTo(0.08, 0.16);
shape.lineTo(-0.75, 0.16);
shape.closePath();
const geometry = new THREE.ShapeGeometry(shape);
const material = new THREE.MeshBasicMaterial({
color: 0xf3c75a,
side: THREE.DoubleSide
});
const arrow = new THREE.Mesh(geometry, material);
arrow.rotation.x = -Math.PI / 2;
arrow.position.set(x, 0.125, z);
arrow.scale.setScalar(scale);
scene.add(arrow);
}
for (let x = -19; x <= 19; x += 4.8) {
addRoadArrow(x, 4.8, 1.15);
}
const oneWaySign = makeLabel(
["ONE-WAY SORTING ROUTE →"],
"#27332f",
"#f4cd62",
6.2,
1.35
);
oneWaySign.position.set(-3.5, 3.5, 6.5);
scene.add(oneWaySign);
addBox(scene, [0.13, 3.2, 0.13], [-19.5, 1.6, 6.45], 0x39443f);
const smallOneWay = makeLabel(
["ONE WAY →"],
"#f3c75a",
"#17211f",
2.6,
0.9
);
smallOneWay.position.set(-19.5, 3.35, 6.45);
scene.add(smallOneWay);
// Conveyor structure.
addBox(scene, [32.5, 0.32, 3.45], [-0.5, 1.48, 0.45], 0x252d2a);
addBox(scene, [32.8, 0.13, 3.72], [-0.5, 1.25, 0.45], 0x69736e);
for (let x = -15; x <= 14; x += 3.2) {
addBox(scene, [0.17, 1.13, 0.17], [x, 0.57, -0.85], 0x4f5a55);
addBox(scene, [0.17, 1.13, 0.17], [x, 0.57, 1.75], 0x4f5a55);
addBox(scene, [0.16, 0.16, 2.65], [x, 1.02, 0.45], 0x69736e);
}
// Moving belt slats.
const beltSlats = [];
for (let i = 0; i < 29; i++) {
const slat = addBox(
scene,
[0.075, 0.035, 3.05],
[-16 + i * 1.1, 1.667, 0.45],
0x3a4541,
{ castShadow: false }
);
beltSlats.push(slat);
}
// Inbound arch.
addBox(scene, [0.23, 3.35, 0.23], [-16.2, 2.83, -1.18], 0x46524c);
addBox(scene, [0.23, 3.35, 0.23], [-16.2, 2.83, 2.08], 0x46524c);
addBox(scene, [0.25, 0.25, 3.5], [-16.2, 4.48, 0.45], 0x46524c);
const inboundSign = makeLabel(
["INBOUND", "DEMOLITION LOADS"],
"#27332f",
"#ffffff",
4.4,
1.25
);
inboundSign.position.set(-16.2, 5.15, 0.45);
scene.add(inboundSign);
// -------------------------------------------------------------------------
// Categories and receiving bays
// -------------------------------------------------------------------------
const categories = {
timber: {
key: "timber",
label: "TIMBER",
subtitle: "REUSE",
color: 0xc8874b,
css: "#c8874b",
groundY: 0.34
},
masonry: {
key: "masonry",
label: "MASONRY",
subtitle: "RECYCLE",
color: 0xa3a8ad,
css: "#a3a8ad",
groundY: 0.52
},
metal: {
key: "metal",
label: "METAL",
subtitle: "RECOVER",
color: 0x4ea1ad,
css: "#4ea1ad",
groundY: 0.31
},
mixed: {
key: "mixed",
label: "MIXED WASTE",
subtitle: "DISPOSE",
color: 0x8e6ac7,
css: "#8e6ac7",
groundY: 0.66
}
};
const stationOrder = ["timber", "masonry", "metal", "mixed"];
const stationXs = [-10, -3.4, 3.2, 9.8];
const stations = [];
function buildStation(category, x, index) {
const station = {
category,
x,
index,
activity: 0,
slotIndex: 0
};
// Bay floor.
addBox(
scene,
[6.1, 0.16, 6.1],
[x, 0.08, -5.75],
category.color
);
// Low receiving walls.
addBox(scene, [6.1, 1.05, 0.22], [x, 0.58, -8.75], category.color);
addBox(scene, [0.22, 1.05, 5.95], [x - 3.0, 0.58, -5.75], category.color);
addBox(scene, [0.22, 1.05, 5.95], [x + 3.0, 0.58, -5.75], category.color);
// Safety posts at the open side.
addBox(scene, [0.12, 1.2, 0.12], [x - 3.0, 0.66, -2.85], 0xf1b94c);
addBox(scene, [0.12, 1.2, 0.12], [x + 3.0, 0.66, -2.85], 0xf1b94c);
// Conveyor scanner gantry.
addBox(scene, [0.18, 3.05, 0.18], [x, 2.98, -1.22], 0x46524c);
addBox(scene, [0.18, 3.05, 0.18], [x, 2.98, 2.12], 0x46524c);
addBox(scene, [0.22, 0.22, 3.58], [x, 4.48, 0.45], 0x46524c);
const scanner = new THREE.Mesh(
new THREE.SphereGeometry(0.18, 16, 10),
new THREE.MeshStandardMaterial({
color: category.color,
emissive: category.color,
emissiveIntensity: 0.75,
roughness: 0.35
})
);
scanner.position.set(x, 4.49, 0.45);
scene.add(scanner);
station.scanner = scanner;
const numberSign = makeLabel(
[`0${index + 1}`],
category.css,
"#17211f",
0.9,
0.68
);
numberSign.position.set(x, 4.55, 2.48);
scene.add(numberSign);
// Destination sign.
const destination = makeLabel(
[category.label, category.subtitle],
category.css,
"#17211f",
category.key === "mixed" ? 5.2 : 4.7,
1.28
);
destination.position.set(x, 3.18, -6.15);
scene.add(destination);
// Diverter paddle.
const armPivot = new THREE.Group();
armPivot.position.set(x + 0.72, 2.04, 0.45);
scene.add(armPivot);
addBox(
armPivot,
[0.18, 0.18, 2.05],
[0, 0, -0.92],
0xf1b94c
);
const pivotCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.17, 0.17, 0.22, 14),
getMaterial(0x39443f)
);
pivotCap.rotation.z = Math.PI / 2;
pivotCap.position.set(0, 0, 0);
armPivot.add(pivotCap);
station.armPivot = armPivot;
// Moving loads use the forward side of each bay.
station.slots = [
[0.15, -4.15],
[0.85, -5.65],
[1.5, -7.0],
[0.45, -7.45],
[1.25, -4.55]
];
stations.push(station);
return station;
}
stationOrder.forEach((key, index) => {
buildStation(categories[key], stationXs[index], index);
});
// -------------------------------------------------------------------------
// Voxel cargo construction
// -------------------------------------------------------------------------
function createCargo(categoryKey) {
const group = new THREE.Group();
if (categoryKey === "timber") {
addVoxel(group, [1.7, 0.28, 0.28], [0, 0, 0], 0xb9773e);
addVoxel(
group,
[1.35, 0.24, 0.24],
[0.1, 0.25, -0.1],
0xd19a60,
0.25
);
addVoxel(
group,
[0.9, 0.24, 0.3],
[-0.35, 0.22, 0.18],
0xa96735,
-0.15
);
} else if (categoryKey === "masonry") {
addVoxel(group, [0.68, 0.68, 0.68], [-0.25, 0, 0.1], 0x969da2);
addVoxel(group, [0.62, 0.58, 0.62], [0.35, 0.02, -0.1], 0xb0b4b7);
addVoxel(group, [0.58, 0.55, 0.58], [0.02, 0.39, 0.04], 0x858d92);
addVoxel(group, [0.54, 0.5, 0.54], [0.28, 0.42, -0.2], 0xc0c3c5);
} else if (categoryKey === "metal") {
addVoxel(group, [1.75, 0.18, 0.18], [0, 0, 0], 0x4b9da8, 0, true);
addVoxel(
group,
[1.3, 0.17, 0.17],
[0.12, 0.19, 0.08],
0x75bdc5,
-0.12,
true
);
addVoxel(
group,
[0.9, 0.16, 0.16],
[-0.35, 0.17, -0.08],
0x397d87,
0.18,
true
);
} else {
addVoxel(group, [0.55, 0.55, 0.55], [-0.28, -0.22, 0.1], 0x8e6ac7);
addVoxel(group, [0.48, 0.5, 0.48], [0.32, -0.18, -0.12], 0xd17b68);
addVoxel(group, [0.44, 0.42, 0.44], [0.02, 0.18, 0.16], 0x4ea1ad);
addVoxel(group, [0.4, 0.4, 0.4], [0.27, 0.25, -0.17], 0xd3ae5e);
}
return group;
}
// Static examples already present in each destination area.
stations.forEach((station) => {
const staticSlots = [
[-1.6, -4.3],
[-0.45, -4.6],
[0.65, -4.2],
[-1.35, -6.25],
[-0.2, -6.45],
[0.8, -6.0],
[-0.6, -7.55],
[1.55, -7.45]
];
staticSlots.forEach((slot, index) => {
const cargo = createCargo(station.category.key);
cargo.position.set(
station.x + slot[0],
station.category.groundY,
slot[1]
);
cargo.rotation.y = (index % 3) * 0.55 + 0.12;
scene.add(cargo);
});
});
// -------------------------------------------------------------------------
// Animated sorting simulation
// -------------------------------------------------------------------------
const cargoList = [];
let paused = false;
let simulationTime = 0;
let spawnTimer = 0;
let spawnIndex = 0;
let sortedCount = 0;
function spawnCargo(initialX = null) {
if (cargoList.length >= 24) return;
const categoryKey = stationOrder[spawnIndex % stationOrder.length];
spawnIndex++;
const group = createCargo(categoryKey);
group.position.set(
initialX ?? -14.85 + Math.random() * 0.25,
2.13,
0.45 + (Math.random() - 0.5) * 0.16
);
group.rotation.y = (Math.random() - 0.5) * 0.18;
scene.add(group);
cargoList.push({
group,
categoryKey,
state: "belt",
speed: 2.25 + Math.random() * 0.25,
divertProgress: 0,
divertStart: new THREE.Vector3(),
divertTarget: new THREE.Vector3(),
parkedTime: 0
});
}
function resetSimulation() {
cargoList.forEach((cargo) => scene.remove(cargo.group));
cargoList.length = 0;
simulationTime = 0;
spawnTimer = 0;
spawnIndex = 0;
sortedCount = 0;
stations.forEach((station) => {
station.slotIndex = 0;
station.activity = 0;
station.armPivot.rotation.y = 0;
});
for (let i = 0; i < 9; i++) {
spawnCargo(-14.7 + i * 1.55);
}
counter.textContent = "00";
}
function updateCargo(dt) {
spawnTimer += dt;
if (spawnTimer >= 1.42) {
spawnTimer %= 1.42;
spawnCargo();
}
for (let i = cargoList.length - 1; i >= 0; i--) {
const cargo = cargoList[i];
const station = stations.find(
(item) => item.category.key === cargo.categoryKey
);
if (cargo.state === "belt") {
cargo.group.position.x += cargo.speed * dt;
cargo.group.rotation.y += dt * 0.045;
if (cargo.group.position.x >= station.x - 0.12) {
cargo.state = "divert";
cargo.divertProgress = 0;
cargo.divertStart.copy(cargo.group.position);
const slot = station.slots[
station.slotIndex++ % station.slots.length
];
cargo.divertTarget.set(
station.x + slot[0],
station.category.groundY,
slot[1]
);
station.activity = 1;
sortedCount++;
counter.textContent = String(sortedCount).padStart(2, "0");
}
} else if (cargo.state === "divert") {
cargo.divertProgress += dt / 1.18;
const t = Math.min(cargo.divertProgress, 1);
const eased = t * t * (3 - 2 * t);
cargo.group.position.lerpVectors(
cargo.divertStart,
cargo.divertTarget,
eased
);
cargo.group.position.y += Math.sin(t * Math.PI) * 0.55;
cargo.group.rotation.y += dt * 0.24;
if (t >= 1) {
cargo.group.position.copy(cargo.divertTarget);
cargo.state = "parked";
cargo.parkedTime = 0;
}
} else if (cargo.state === "parked") {
cargo.parkedTime += dt;
if (cargo.parkedTime > 7.5) {
scene.remove(cargo.group);
cargoList.splice(i, 1);
}
}
}
}
function updateStations(dt) {
stations.forEach((station) => {
station.activity = Math.max(0, station.activity - dt * 1.25);
const targetRotation = station.activity > 0 ? -0.72 : 0;
station.armPivot.rotation.y = THREE.MathUtils.lerp(
station.armPivot.rotation.y,
targetRotation,
1 - Math.exp(-8 * dt)
);
station.scanner.material.emissiveIntensity =
0.5 + station.activity * 1.7;
});
}
resetSimulation();
// -------------------------------------------------------------------------
// Custom orbit, zoom and pan controls
// -------------------------------------------------------------------------
const rig = {
target: new THREE.Vector3(0, 0.6, -1.35),
theta: 0.66,
phi: 0.73,
radius: window.innerWidth < 720 ? 49 : 39
};
function updateCamera() {
const horizontalRadius =
rig.radius * Math.cos(rig.phi);
camera.position.set(
rig.target.x + Math.sin(rig.theta) * horizontalRadius,
rig.target.y + Math.sin(rig.phi) * rig.radius,
rig.target.z + Math.cos(rig.theta) * horizontalRadius
);
camera.lookAt(rig.target);
}
function panCamera(dx, dy) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const up = new THREE.Vector3()
.crossVectors(right, forward)
.normalize();
const scale = rig.radius * 0.0015;
rig.target.addScaledVector(right, -dx * scale);
rig.target.addScaledVector(up, dy * scale);
rig.target.x = THREE.MathUtils.clamp(rig.target.x, -17, 17);
rig.target.y = THREE.MathUtils.clamp(rig.target.y, -1, 7);
rig.target.z = THREE.MathUtils.clamp(rig.target.z, -11, 10);
}
const pointers = new Map();
let gesture = null;
function pointerPair() {
const values = [...pointers.values()];
const a = values[0];
const b = values[1];
return {
distance: Math.hypot(a.x - b.x, a.y - b.y),
midX: (a.x + b.x) / 2,
midY: (a.y + b.y) / 2
};
}
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
event.preventDefault();
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
if (pointers.size === 1) {
gesture = {
type: event.button === 2 ? "pan" : "orbit",
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
const pair = pointerPair();
gesture = {
type: "pinch",
distance: pair.distance,
midX: pair.midX,
midY: pair.midY
};
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
if (pointers.size === 1 && gesture) {
const dx = event.clientX - gesture.x;
const dy = event.clientY - gesture.y;
if (gesture.type === "orbit") {
rig.theta -= dx * 0.006;
rig.phi = THREE.MathUtils.clamp(
rig.phi - dy * 0.005,
0.25,
1.32
);
} else {
panCamera(dx, dy);
}
gesture.x = event.clientX;
gesture.y = event.clientY;
} else if (pointers.size === 2 && gesture?.type === "pinch") {
const pair = pointerPair();
rig.radius = THREE.MathUtils.clamp(
rig.radius * (gesture.distance / pair.distance),
18,
68
);
panCamera(pair.midX - gesture.midX, pair.midY - gesture.midY);
gesture.distance = pair.distance;
gesture.midX = pair.midX;
gesture.midY = pair.midY;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
gesture = {
type: remaining.button === 2 ? "pan" : "orbit",
x: remaining.x,
y: remaining.y
};
} else if (pointers.size === 0) {
gesture = null;
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
rig.radius = THREE.MathUtils.clamp(
rig.radius * Math.exp(event.deltaY * 0.001),
18,
68
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
const pauseButton = document.querySelector("#pause-button");
const restartButton = document.querySelector("#restart-button");
const viewButton = document.querySelector("#view-button");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume flow" : "Pause flow";
});
restartButton.addEventListener("click", () => {
resetSimulation();
});
viewButton.addEventListener("click", () => {
rig.target.set(0, 0.6, -1.35);
rig.theta = 0.66;
rig.phi = 0.73;
rig.radius = window.innerWidth < 720 ? 49 : 39;
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
simulationTime += dt;
updateCargo(dt);
updateStations(dt);
beltSlats.forEach((slat, index) => {
slat.position.x =
-16.2 +
((index * 1.1 + simulationTime * 1.8) % 31.5);
});
}
updateCamera();
renderer.render(scene, camera);
}
updateCamera();
renderer.render(scene, camera);
loading.classList.add("hidden");
requestAnimationFrame(animate);
</script>
</body>
</html>
```