Prompt specification
Build or customize the Three.js 3D scene "Conveyor Corner and Item Spacing" 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: Conveyor Corner and Item Spacing
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/conveyor-corner-and-item-spacing-x3525
Scene Description Prompt:
A voxel conveyor carries three evenly spaced blocks around a right-angle corner without intersecting. Create one HTML file with Three.js from cdnjs.
Working Three.js Starter (conveyor-corner-and-item-spacing-x3525):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Corner Conveyor</title>
<style>
:root {
color-scheme: dark;
--cyan: #56e5d0;
--orange: #ffad5c;
--violet: #b39cff;
--panel: rgba(8, 18, 32, 0.72);
--line: rgba(139, 190, 211, 0.22);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111f;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
color: #e8f6ff;
}
.panel {
position: absolute;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.title-panel {
top: 24px;
left: 24px;
min-width: 270px;
padding: 17px 19px;
border-radius: 4px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
animation: pulse 1.8s ease-in-out infinite;
}
h1 {
margin: 0;
font-size: clamp(20px, 3vw, 31px);
line-height: 1;
letter-spacing: -0.04em;
text-transform: uppercase;
}
.subtitle {
margin-top: 10px;
color: #91aabd;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
letter-spacing: 0.1em;
}
.cargo-panel {
top: 24px;
right: 24px;
padding: 14px 16px;
border-radius: 4px;
}
.cargo-label {
margin-bottom: 10px;
color: #7994a8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.cargo-row {
display: flex;
gap: 18px;
}
.cargo-item {
display: flex;
align-items: center;
gap: 7px;
color: #bfd0dc;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.status-panel {
bottom: 24px;
left: 24px;
display: grid;
grid-template-columns: repeat(3, auto);
gap: 22px;
padding: 13px 16px;
border-radius: 4px;
}
.stat strong {
display: block;
margin-bottom: 3px;
color: #e8f6ff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
font-weight: 600;
}
.stat span {
color: #6f899d;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.help-panel {
right: 24px;
bottom: 24px;
padding: 12px 15px;
border-radius: 4px;
color: #819aac;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.5;
text-align: right;
}
.help-panel b {
color: #c6d9e4;
font-weight: 600;
}
#loading {
position: fixed;
inset: 0;
z-index: 5;
display: grid;
place-items: center;
background: #07111f;
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 0.4s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@keyframes pulse {
0%, 100% { opacity: 0.55; transform: scale(0.85); }
50% { opacity: 1; transform: scale(1.15); }
}
@media (max-width: 720px) {
.title-panel {
top: 12px;
left: 12px;
min-width: 0;
width: calc(100% - 24px);
}
.cargo-panel {
display: none;
}
.status-panel {
left: 12px;
bottom: 12px;
right: 12px;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.help-panel {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="hud">
<section class="panel title-panel">
<div class="eyebrow">
<span class="live-dot"></span>
Automated voxel transfer
</div>
<h1>Corner 90°</h1>
<div class="subtitle">ARC-LENGTH LOCKED · 3 INDEPENDENT CARGOES</div>
</section>
<section class="panel cargo-panel">
<div class="cargo-label">Cargo manifest</div>
<div class="cargo-row">
<div class="cargo-item">
<span class="swatch" style="color:#56e5d0;background:#56e5d0"></span>
A-01
</div>
<div class="cargo-item">
<span class="swatch" style="color:#ffad5c;background:#ffad5c"></span>
B-02
</div>
<div class="cargo-item">
<span class="swatch" style="color:#b39cff;background:#b39cff"></span>
C-03
</div>
</div>
</section>
<section class="panel status-panel">
<div class="stat">
<strong>3.80 m</strong>
<span>Equal spacing</span>
</div>
<div class="stat">
<strong>1.05 m/s</strong>
<span>Belt speed</span>
</div>
<div class="stat">
<strong>90°</strong>
<span>Transfer</span>
</div>
</section>
<section class="panel help-panel">
<b>Drag</b> to orbit · <b>Scroll</b> to zoom<br>
<b>Double-click</b> to reset view
</section>
</div>
<div id="loading">Initializing conveyor geometry…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const loading = document.getElementById("loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 15, 31);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
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.15;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc7edff, 0x172033, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0d7, 3.4);
sun.position.set(3.5, 10, 5.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x64dfff, 1.25);
rimLight.position.set(-7, 4, -6);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa95e, 9, 9, 2);
warmLight.position.set(3.4, 2.2, 1.4);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Floor
// -------------------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x0b1726,
roughness: 0.92,
metalness: 0.08
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(38, 38),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.025;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(32, 32, 0x315064, 0x1a3041);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
// -------------------------------------------------------------------------
// Right-angle conveyor path
//
// The path uses:
// 1. A straight segment traveling in +X.
// 2. A quarter-circle fillet traveling from +X to +Z.
// 3. A straight segment traveling in +Z.
//
// Cargo positions are sampled by arc length, not by segment parameter.
// -------------------------------------------------------------------------
const START_X = -5.2;
const TURN_RADIUS = 1.75;
const END_Z = 5.2;
const STRAIGHT_1_LENGTH = -START_X;
const ARC_LENGTH = (Math.PI * TURN_RADIUS) / 2;
const STRAIGHT_2_LENGTH = END_Z - TURN_RADIUS;
const PATH_LENGTH =
STRAIGHT_1_LENGTH + ARC_LENGTH + STRAIGHT_2_LENGTH;
const CARGO_COUNT = 3;
const CARGO_SPACING = PATH_LENGTH / CARGO_COUNT;
const BELT_SPEED = 1.05;
const BELT_TOP = 0.65;
function samplePath(distance) {
const s = THREE.MathUtils.euclideanModulo(distance, PATH_LENGTH);
if (s <= STRAIGHT_1_LENGTH) {
return {
x: START_X + s,
z: 0,
tx: 1,
tz: 0
};
}
if (s <= STRAIGHT_1_LENGTH + ARC_LENGTH) {
const arcDistance = s - STRAIGHT_1_LENGTH;
const theta = arcDistance / TURN_RADIUS;
return {
x: TURN_RADIUS * Math.sin(theta),
z: TURN_RADIUS * (1 - Math.cos(theta)),
tx: Math.cos(theta),
tz: Math.sin(theta)
};
}
const finalDistance = s - STRAIGHT_1_LENGTH - ARC_LENGTH;
return {
x: TURN_RADIUS,
z: TURN_RADIUS + finalDistance,
tx: 0,
tz: 1
};
}
function yawFromTangent(tx, tz) {
// Local +Z is aligned with the direction of travel.
return Math.atan2(tx, tz);
}
// -------------------------------------------------------------------------
// Conveyor construction
// -------------------------------------------------------------------------
const beltMaterial = new THREE.MeshStandardMaterial({
color: 0x193348,
roughness: 0.72,
metalness: 0.42
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x101d2b,
roughness: 0.82,
metalness: 0.5
});
const railMaterial = new THREE.MeshStandardMaterial({
color: 0x284b61,
emissive: 0x071c26,
emissiveIntensity: 0.6,
roughness: 0.55,
metalness: 0.58
});
const cleatMaterial = new THREE.MeshStandardMaterial({
color: 0x48d7cf,
emissive: 0x1b8e8b,
emissiveIntensity: 1.5,
roughness: 0.38,
metalness: 0.35
});
const dummy = new THREE.Object3D();
const tileCount = Math.ceil(PATH_LENGTH / 0.24);
const tileGeometry = new THREE.BoxGeometry(1.62, 0.2, 0.22);
const baseGeometry = new THREE.BoxGeometry(1.72, 0.22, 0.27);
const railGeometry = new THREE.BoxGeometry(0.14, 0.22, 0.225);
const tiles = new THREE.InstancedMesh(tileGeometry, beltMaterial, tileCount);
const bases = new THREE.InstancedMesh(baseGeometry, baseMaterial, tileCount);
const leftRails = new THREE.InstancedMesh(railGeometry, railMaterial, tileCount);
const rightRails = new THREE.InstancedMesh(railGeometry, railMaterial, tileCount);
for (const mesh of [tiles, bases, leftRails, rightRails]) {
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
scene.add(mesh);
}
for (let i = 0; i < tileCount; i++) {
const s = (i / tileCount) * PATH_LENGTH;
const p = samplePath(s);
const yaw = yawFromTangent(p.tx, p.tz);
// Right normal in the XZ plane.
const nx = p.tz;
const nz = -p.tx;
dummy.position.set(p.x, 0.55, p.z);
dummy.rotation.set(0, yaw, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
tiles.setMatrixAt(i, dummy.matrix);
const shade = i % 3 === 0 ? 0x24465c : 0x193348;
tiles.setColorAt(i, new THREE.Color(shade));
dummy.position.set(p.x, 0.35, p.z);
dummy.updateMatrix();
bases.setMatrixAt(i, dummy.matrix);
dummy.position.set(p.x + nx * 0.84, 0.77, p.z + nz * 0.84);
dummy.updateMatrix();
leftRails.setMatrixAt(i, dummy.matrix);
dummy.position.set(p.x - nx * 0.84, 0.77, p.z - nz * 0.84);
dummy.updateMatrix();
rightRails.setMatrixAt(i, dummy.matrix);
}
tiles.instanceColor.needsUpdate = true;
// Moving transverse tread markers.
const cleatCount = 30;
const cleatGeometry = new THREE.BoxGeometry(1.25, 0.018, 0.055);
const cleats = new THREE.InstancedMesh(
cleatGeometry,
cleatMaterial,
cleatCount
);
cleats.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
cleats.castShadow = false;
cleats.receiveShadow = true;
scene.add(cleats);
// -------------------------------------------------------------------------
// Conveyor supports
// -------------------------------------------------------------------------
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x172a3b,
roughness: 0.65,
metalness: 0.65
});
const supportPositions = [];
for (let s = 0.65; s < PATH_LENGTH - 0.5; s += 1.25) {
supportPositions.push(s);
}
const legGeometry = new THREE.BoxGeometry(0.2, 0.34, 0.2);
const footGeometry = new THREE.BoxGeometry(0.38, 0.08, 0.38);
const braceGeometry = new THREE.BoxGeometry(1.32, 0.12, 0.18);
const legs = new THREE.InstancedMesh(
legGeometry,
supportMaterial,
supportPositions.length * 2
);
const feet = new THREE.InstancedMesh(
footGeometry,
supportMaterial,
supportPositions.length * 2
);
const braces = new THREE.InstancedMesh(
braceGeometry,
supportMaterial,
supportPositions.length
);
for (const mesh of [legs, feet, braces]) {
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
supportPositions.forEach((s, index) => {
const p = samplePath(s);
const yaw = yawFromTangent(p.tx, p.tz);
const nx = p.tz;
const nz = -p.tx;
for (let side = 0; side < 2; side++) {
const sign = side === 0 ? -1 : 1;
const x = p.x + nx * 0.57 * sign;
const z = p.z + nz * 0.57 * sign;
dummy.position.set(x, 0.18, z);
dummy.rotation.set(0, yaw, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
legs.setMatrixAt(index * 2 + side, dummy.matrix);
dummy.position.set(x, 0.04, z);
dummy.updateMatrix();
feet.setMatrixAt(index * 2 + side, dummy.matrix);
}
dummy.position.set(p.x, 0.36, p.z);
dummy.rotation.set(0, yaw, 0);
dummy.updateMatrix();
braces.setMatrixAt(index, dummy.matrix);
});
// -------------------------------------------------------------------------
// Voxel corner indicators
// -------------------------------------------------------------------------
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xff9b48,
emissive: 0x7d2c08,
emissiveIntensity: 0.65,
roughness: 0.55,
metalness: 0.25
});
const markerCount = 12;
const markerGeometry = new THREE.BoxGeometry(0.13, 0.035, 0.13);
const cornerMarkers = new THREE.InstancedMesh(
markerGeometry,
markerMaterial,
markerCount
);
for (let i = 0; i < markerCount; i++) {
const theta = (i / (markerCount - 1)) * (Math.PI / 2);
const x = (TURN_RADIUS + 1.13) * Math.sin(theta);
const z = TURN_RADIUS - (TURN_RADIUS + 1.13) * Math.cos(theta);
dummy.position.set(x, 0.025, z);
dummy.rotation.set(0, -theta, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
cornerMarkers.setMatrixAt(i, dummy.matrix);
}
cornerMarkers.receiveShadow = true;
scene.add(cornerMarkers);
// -------------------------------------------------------------------------
// Transfer gates at the path boundaries
// -------------------------------------------------------------------------
function createGate(distance, accent) {
const p = samplePath(distance);
const yaw = yawFromTangent(p.tx, p.tz);
const group = new THREE.Group();
group.position.set(p.x, 0, p.z);
group.rotation.y = yaw;
const postMaterial = new THREE.MeshStandardMaterial({
color: 0x29485e,
emissive: accent,
emissiveIntensity: 0.15,
roughness: 0.55,
metalness: 0.6
});
const lightMaterial = new THREE.MeshStandardMaterial({
color: accent,
emissive: accent,
emissiveIntensity: 2.2,
roughness: 0.3
});
function addBox(size, position, material) {
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 = true;
mesh.receiveShadow = true;
group.add(mesh);
return mesh;
}
addBox([0.16, 1.48, 0.18], [-1.02, 0.74, 0], postMaterial);
addBox([0.16, 1.48, 0.18], [1.02, 0.74, 0], postMaterial);
addBox([2.2, 0.16, 0.18], [0, 1.48, 0], postMaterial);
addBox([0.09, 0.09, 0.21], [-1.02, 1.36, 0], lightMaterial);
addBox([0.09, 0.09, 0.21], [1.02, 1.36, 0], lightMaterial);
scene.add(group);
}
createGate(0, 0x56e5d0);
createGate(PATH_LENGTH, 0xffad5c);
// -------------------------------------------------------------------------
// Three cargo blocks
//
// Each cargo is a 2×2×2 assembly of voxel cubes. The blocks are separated
// by exact path arc length, preventing intersection on the curved fillet.
// -------------------------------------------------------------------------
function createCargo(baseColor) {
const group = new THREE.Group();
const cubeSize = 0.34;
const pitch = 0.36;
const cubeGeometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const edgeGeometry = new THREE.EdgesGeometry(cubeGeometry);
const base = new THREE.Color(baseColor);
const light = base.clone().lerp(new THREE.Color(0xffffff), 0.2);
const dark = base.clone().multiplyScalar(0.72);
const materials = [
new THREE.MeshStandardMaterial({
color: base,
roughness: 0.48,
metalness: 0.18,
transparent: true
}),
new THREE.MeshStandardMaterial({
color: light,
roughness: 0.43,
metalness: 0.2,
transparent: true
}),
new THREE.MeshStandardMaterial({
color: dark,
roughness: 0.55,
metalness: 0.15,
transparent: true
})
];
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.18
});
for (let layer = 0; layer < 2; layer++) {
for (let x = 0; x < 2; x++) {
for (let z = 0; z < 2; z++) {
const materialIndex =
layer === 1 ? 1 : (x + z) % 2 === 0 ? 0 : 2;
const cube = new THREE.Mesh(cubeGeometry, materials[materialIndex]);
cube.position.set(
(x - 0.5) * pitch,
0.18 + layer * pitch,
(z - 0.5) * pitch
);
cube.castShadow = true;
cube.receiveShadow = true;
group.add(cube);
const edges = new THREE.LineSegments(edgeGeometry, edgeMaterial);
edges.position.copy(cube.position);
group.add(edges);
}
}
}
scene.add(group);
return {
group,
materials: [...materials, edgeMaterial]
};
}
const cargoColors = [0x56e5d0, 0xffad5c, 0xb39cff];
const cargos = cargoColors.map(createCargo);
// Initial phases place one block before the bend, one on the bend,
// and one beyond it, while preserving equal arc-length spacing.
const initialCargoDistance = 2.7;
const cargoState = cargos.map((cargo, index) => ({
cargo,
distance: (initialCargoDistance + index * CARGO_SPACING) % PATH_LENGTH
}));
function setCargoOpacity(cargo, opacity) {
cargo.group.visible = opacity > 0.01;
for (const material of cargo.materials) {
material.opacity = opacity;
}
}
function updateCargo(cargoState) {
const p = samplePath(cargoState.distance);
const yaw = yawFromTangent(p.tx, p.tz);
cargoState.cargo.group.position.set(p.x, BELT_TOP + 0.035, p.z);
cargoState.cargo.group.rotation.y = yaw;
// Cargo disappears into the transfer gate and reappears at the other
// gate. The underlying arc-length spacing never changes.
const boundaryDistance = Math.min(
cargoState.distance,
PATH_LENGTH - cargoState.distance
);
const opacity = THREE.MathUtils.smoothstep(boundaryDistance, 0, 0.52);
setCargoOpacity(cargoState.cargo, opacity);
}
cargoState.forEach(updateCargo);
// -------------------------------------------------------------------------
// Custom camera controls implemented directly here
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-1.45, 0.5, 1.7);
const orbit = {
desiredTheta: 0.77,
desiredPhi: 0.88,
theta: 0.77,
phi: 0.88,
desiredZoom: 1,
zoom: 1,
radius: 14.8,
fitBoost: 1
};
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
orbit.desiredTheta -= dx * 0.006;
orbit.desiredPhi = THREE.MathUtils.clamp(
orbit.desiredPhi - dy * 0.005,
0.35,
1.35
);
pointerX = event.clientX;
pointerY = event.clientY;
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredZoom = THREE.MathUtils.clamp(
orbit.desiredZoom * Math.exp(event.deltaY * 0.001),
0.62,
1.85
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.desiredTheta = 0.77;
orbit.desiredPhi = 0.88;
orbit.desiredZoom = 1;
});
function updateCamera() {
const damping = 0.085;
orbit.theta += (orbit.desiredTheta - orbit.theta) * damping;
orbit.phi += (orbit.desiredPhi - orbit.phi) * damping;
orbit.zoom += (orbit.desiredZoom - orbit.zoom) * damping;
const desiredRadius = 14.8 * orbit.zoom * orbit.fitBoost;
orbit.radius += (desiredRadius - orbit.radius) * damping;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
camera.aspect = aspect;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
// Keep the complete conveyor visible on narrow portrait displays.
orbit.fitBoost = Math.max(1, 0.9 / aspect);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let beltTravel = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
beltTravel = (beltTravel + BELT_SPEED * delta) % PATH_LENGTH;
// Update moving belt treads.
for (let i = 0; i < cleatCount; i++) {
const s = (i / cleatCount) * PATH_LENGTH + beltTravel;
const p = samplePath(s);
dummy.position.set(p.x, BELT_TOP + 0.018, p.z);
dummy.rotation.set(0, yawFromTangent(p.tx, p.tz), 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
cleats.setMatrixAt(i, dummy.matrix);
}
cleats.instanceMatrix.needsUpdate = true;
// Update cargo by identical arc-length travel, so there is no belt slip.
for (const state of cargoState) {
state.distance += BELT_SPEED * delta;
if (state.distance >= PATH_LENGTH) {
state.distance -= PATH_LENGTH;
}
updateCargo(state);
}
updateCamera();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 500);
});
</script>
</body>
</html>
```