Prompt specification
Build or customize the Three.js 3D scene "Pallet Between Truck and Rail Platform" 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: Pallet Between Truck and Rail Platform
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pallet-between-truck-and-rail-platform-x2101
Scene Description Prompt:
Show a voxel pallet between truck and rail platform, exposing transfer heights and handling clearance. Three.js from cdnjs in one HTML file.
Working Three.js Starter (pallet-between-truck-and-rail-platform-x2101):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Intermodal Transfer Study</title>
<style>
:root {
--ink: #14272d;
--muted: #607379;
--paper: #eef2ed;
--panel: rgba(249, 251, 247, 0.9);
--line: rgba(20, 39, 45, 0.16);
--orange: #ee6a3b;
--teal: #168b87;
--purple: #7057b8;
--yellow: #e9b949;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
#leaders {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 3;
}
.ui {
position: fixed;
z-index: 5;
}
.panel {
background: var(--panel);
border: 1px solid rgba(20, 39, 45, 0.1);
box-shadow: 0 18px 50px rgba(26, 47, 51, 0.08);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
header {
top: 22px;
left: 24px;
width: min(410px, calc(100vw - 48px));
padding: 19px 21px 17px;
border-radius: 3px 18px 18px 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 25px;
height: 3px;
background: var(--orange);
content: "";
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 35px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1.04;
}
header p {
max-width: 340px;
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.readouts {
top: 22px;
right: 24px;
display: grid;
grid-template-columns: repeat(2, minmax(122px, 1fr));
gap: 8px;
width: 286px;
}
.readout {
min-height: 76px;
padding: 13px 14px;
border-radius: 12px;
}
.readout span {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.readout strong {
font-size: 24px;
font-weight: 760;
letter-spacing: -0.04em;
line-height: 1;
}
.readout em {
color: var(--muted);
font-size: 11px;
font-style: normal;
font-weight: 650;
letter-spacing: 0;
}
.readout[data-accent="orange"] strong {
color: var(--orange);
}
.readout[data-accent="purple"] strong {
color: var(--purple);
}
.readout[data-accent="teal"] strong {
color: var(--teal);
}
.controls {
left: 50%;
bottom: 22px;
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 7px;
width: min(900px, calc(100vw - 32px));
padding: 9px;
border-radius: 15px;
transform: translateX(-50%);
}
.controls button {
min-height: 38px;
padding: 0 14px;
border: 1px solid transparent;
border-radius: 9px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition:
color 160ms ease,
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
.controls button:hover {
color: var(--ink);
background: rgba(20, 39, 45, 0.055);
}
.controls button:active {
transform: translateY(1px);
}
.controls button.active,
.controls button.on {
border-color: rgba(20, 39, 45, 0.08);
background: var(--ink);
color: white;
}
.controls .divider {
width: 1px;
margin: 5px 3px;
background: var(--line);
}
.hint {
left: 24px;
bottom: 27px;
z-index: 4;
color: rgba(20, 39, 45, 0.62);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.03em;
}
.axis-key {
right: 24px;
bottom: 27px;
z-index: 4;
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.axis-key i {
width: 31px;
height: 4px;
background: linear-gradient(90deg, var(--teal), var(--orange));
border-radius: 2px;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
}
.callout {
position: absolute;
display: flex;
align-items: center;
gap: 9px;
min-width: 148px;
padding: 9px 11px;
border: 1px solid rgba(20, 39, 45, 0.1);
border-left: 4px solid var(--teal);
border-radius: 8px;
background: rgba(250, 251, 248, 0.94);
box-shadow: 0 8px 24px rgba(20, 39, 45, 0.1);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
will-change: left, top;
}
.callout .index {
display: grid;
width: 23px;
height: 23px;
flex: 0 0 auto;
place-items: center;
border-radius: 6px;
background: color-mix(in srgb, var(--accent) 14%, white);
color: var(--accent);
font-size: 9px;
font-weight: 850;
}
.callout b {
display: block;
margin-bottom: 2px;
font-size: 10px;
font-weight: 820;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.callout small {
display: block;
color: var(--muted);
font-size: 9px;
white-space: nowrap;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: var(--paper);
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 820px) {
header {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
header p {
display: none;
}
.readouts {
top: 116px;
right: 12px;
width: 142px;
grid-template-columns: 1fr;
}
.readout {
min-height: 55px;
padding: 9px 11px;
}
.readout span {
margin-bottom: 4px;
}
.readout strong {
font-size: 19px;
}
.controls {
bottom: 12px;
width: calc(100vw - 24px);
}
.controls button {
padding: 0 9px;
font-size: 9px;
}
.hint,
.axis-key {
display: none;
}
.callout {
min-width: 122px;
padding: 7px 8px;
}
.callout small {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<svg id="leaders" aria-hidden="true"></svg>
<div id="labels" aria-hidden="true"></div>
<header class="ui panel">
<div class="eyebrow">Intermodal transfer study</div>
<h1>Road-to-rail<br />handling envelope</h1>
<p>
Voxel pallet positioned on a variable-height transfer deck between the
truck bed and rail platform. Dimensions remain visible in plan and elevation.
</p>
</header>
<aside class="ui readouts" aria-label="Transfer dimensions">
<div class="ui readout panel" data-accent="teal">
<span>Truck deck</span>
<strong>1.20 <em>m</em></strong>
</div>
<div class="ui readout panel" data-accent="orange">
<span>Rail deck</span>
<strong>1.80 <em>m</em></strong>
</div>
<div class="ui readout panel">
<span>Deck step</span>
<strong>0.60 <em>m</em></strong>
</div>
<div class="ui readout panel" data-accent="purple">
<span>Vertical clearance</span>
<strong id="clearanceValue">1.70 <em>m</em></strong>
</div>
</aside>
<nav class="ui controls panel" aria-label="Model controls">
<button class="active" data-camera="overview">Axon</button>
<button data-camera="elevation">Elevation</button>
<button data-camera="plan">Plan</button>
<span class="divider"></span>
<button id="liftButton">Raise to rail</button>
<button id="clearanceButton" class="on">Clearance envelope</button>
</nav>
<div class="ui hint">Drag to orbit · Wheel to zoom · Shift-drag to pan</div>
<div class="ui axis-key"><i></i> Transfer axis</div>
<div id="loading">Building transfer study</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const loading = document.querySelector("#loading");
const leaderSvg = document.querySelector("#leaders");
const labelsRoot = document.querySelector("#labels");
const clearanceValue = document.querySelector("#clearanceValue");
const liftButton = document.querySelector("#liftButton");
const clearanceButton = document.querySelector("#clearanceButton");
const COLORS = {
ink: 0x14272d,
teal: 0x168b87,
orange: 0xee6a3b,
purple: 0x7057b8,
yellow: 0xe9b949,
wood: 0xb98045,
woodLight: 0xd2a05e,
concrete: 0xc8cec8,
concreteDark: 0x9ca7a1,
steel: 0x77878c,
white: 0xe8ede8
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9eee8);
scene.fog = new THREE.Fog(0xe9eee8, 24, 48);
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.05;
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
target: new THREE.Vector3(-0.45, 1.45, 0),
radius: 19,
theta: 0.63,
phi: 1.02
};
const desiredCamera = {
target: cameraState.target.clone(),
radius: cameraState.radius,
theta: cameraState.theta,
phi: cameraState.phi
};
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.05,
...options
});
}
const materialCache = new Map();
function cachedMaterial(color, options = {}) {
const key = `${color}-${JSON.stringify(options)}`;
if (!materialCache.has(key)) {
materialCache.set(key, standardMaterial(color, options));
}
return materialCache.get(key);
}
function box(
parent,
width,
height,
depth,
x,
y,
z,
color,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
options.material || cachedMaterial(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function cylinder(
parent,
radius,
depth,
x,
y,
z,
color,
radialSegments = 12
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, depth, radialSegments),
cachedMaterial(color)
);
mesh.rotation.x = Math.PI / 2;
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function line(points, color, opacity = 1, dashed = false) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map((point) => new THREE.Vector3(...point))
);
const options = {
color,
transparent: opacity < 1,
opacity
};
const material = dashed
? new THREE.LineDashedMaterial({
...options,
dashSize: 0.18,
gapSize: 0.11
})
: new THREE.LineBasicMaterial(options);
const object = new THREE.Line(geometry, material);
if (dashed) object.computeLineDistances();
scene.add(object);
return object;
}
// Lighting
scene.add(new THREE.HemisphereLight(0xf7fff8, 0x718083, 2.15));
const keyLight = new THREE.DirectionalLight(0xfff6e5, 3.2);
keyLight.position.set(-7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 13;
keyLight.shadow.camera.bottom = -13;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 38;
keyLight.shadow.normalBias = 0.035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x9ac9d2, 1.05);
fillLight.position.set(8, 7, -9);
scene.add(fillLight);
// Ground and grid
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
standardMaterial(0xe3e9e2, { roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(44, 44, 0x9eaaa4, 0xcbd3cd);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
// Small site pads
box(scene, 8.4, 0.035, 4.7, -6.3, 0.025, 0, 0xdde3dc);
box(scene, 7.2, 0.035, 4.2, 5.6, 0.025, 0, 0xdde3dc);
// ------------------------------------------------------------------
// Truck: cutaway voxel trailer and cab
// ------------------------------------------------------------------
const truck = new THREE.Group();
scene.add(truck);
// Trailer chassis
box(truck, 6.55, 0.28, 0.22, -6.2, 0.78, 0, COLORS.ink);
box(truck, 6.55, 0.16, 0.18, -6.2, 0.56, 0, COLORS.steel);
box(truck, 6.2, 0.18, 2.35, -6.2, 1.11, 0, COLORS.steel);
// Trailer shell, open on camera-facing side and roof
box(truck, 6.2, 2.55, 0.12, -6.2, 2.475, -1.18, COLORS.white);
box(truck, 0.12, 2.55, 2.35, -9.25, 2.475, 0, COLORS.white);
box(truck, 6.2, 0.62, 0.12, -6.2, 1.51, 1.18, COLORS.white);
for (const x of [-9.18, -7.6, -6.0, -4.4, -3.08]) {
box(truck, 0.08, 2.56, 0.09, x, 2.47, -1.11, COLORS.steel);
}
for (const x of [-9.18, -7.6, -6.0, -4.4, -3.08]) {
box(truck, 0.07, 2.56, 0.07, x, 2.47, 1.16, COLORS.steel);
}
box(truck, 6.2, 0.09, 0.1, -6.2, 3.72, -1.16, COLORS.steel);
box(truck, 6.2, 0.09, 0.1, -6.2, 3.72, 1.16, COLORS.steel);
box(truck, 0.1, 0.09, 2.35, -9.22, 3.72, 0, COLORS.steel);
box(truck, 0.1, 0.09, 2.35, -3.08, 3.72, 0, COLORS.steel);
// Trailer interior cargo
box(truck, 1.25, 1.0, 1.45, -7.7, 1.7, -0.15, 0x668f91);
box(truck, 1.05, 0.76, 1.12, -5.9, 1.58, 0.15, 0xc49458);
box(truck, 0.82, 0.56, 0.82, -4.55, 1.48, -0.25, 0x9c6d53);
// Rear bumper and dock hardware
box(truck, 0.16, 0.18, 2.35, -3.02, 0.92, 0, COLORS.orange);
box(truck, 0.18, 0.28, 0.25, -3.01, 1.04, 0.86, COLORS.ink);
box(truck, 0.18, 0.28, 0.25, -3.01, 1.04, -0.86, COLORS.ink);
// Cab
box(truck, 2.05, 1.72, 2.2, -10.25, 1.87, 0, COLORS.teal);
box(truck, 2.12, 0.22, 2.26, -10.25, 1.04, 0, COLORS.ink);
box(truck, 0.72, 0.12, 2.28, -10.63, 2.79, 0, COLORS.teal);
// Windshield and side glazing
box(
truck,
0.035,
0.72,
1.86,
-11.29,
2.18,
0,
0x27434b,
{ roughness: 0.28, metalness: 0.18 }
);
box(
truck,
1.03,
0.66,
0.035,
-10.2,
2.19,
1.115,
0x27434b,
{ roughness: 0.28, metalness: 0.18 }
);
box(truck, 0.08, 0.25, 1.88, -11.3, 1.48, 0, COLORS.ink);
box(truck, 0.1, 0.18, 0.34, -11.33, 1.26, 0.72, 0xffe6a7);
box(truck, 0.1, 0.18, 0.34, -11.33, 1.26, -0.72, 0xffe6a7);
// Truck wheels
for (const x of [-9.95, -8.45, -4.55, -3.75]) {
for (const z of [-1.17, 1.17]) {
cylinder(truck, 0.49, 0.34, x, 0.51, z, 0x25343a);
cylinder(truck, 0.22, 0.36, x, 0.51, z, 0x9ca7a8);
}
}
// ------------------------------------------------------------------
// Rail platform / flatcar
// ------------------------------------------------------------------
const railPlatform = new THREE.Group();
scene.add(railPlatform);
box(railPlatform, 5.55, 0.26, 2.65, 5.55, 1.67, 0, COLORS.concrete);
box(railPlatform, 5.62, 0.18, 2.7, 5.55, 1.46, 0, COLORS.steel);
box(railPlatform, 0.22, 0.48, 2.78, 2.88, 1.54, 0, COLORS.ink);
// Hazard edge at transfer side
for (let i = 0; i < 12; i++) {
const z = -1.25 + i * 0.225;
box(
railPlatform,
0.22,
0.028,
0.21,
2.89,
1.812,
z,
i % 2 === 0 ? COLORS.orange : 0x26363b
);
}
// Underframe and bogies
box(railPlatform, 4.8, 0.23, 0.28, 5.55, 1.25, 0, COLORS.ink);
for (const x of [4.15, 6.9]) {
box(railPlatform, 1.25, 0.24, 1.95, x, 1.06, 0, COLORS.ink);
for (const z of [-0.88, 0.88]) {
cylinder(railPlatform, 0.35, 0.22, x - 0.34, 0.53, z, 0x303d42);
cylinder(railPlatform, 0.35, 0.22, x + 0.34, 0.53, z, 0x303d42);
}
}
// Rails and sleepers
for (const z of [-0.88, 0.88]) {
box(scene, 9.2, 0.08, 0.1, 5.25, 0.13, z, 0x67777b);
}
for (let x = 1.6; x <= 9.2; x += 0.55) {
box(scene, 0.18, 0.07, 2.3, x, 0.045, 0, 0x7c7669);
}
// End bumper and far-side safety rail
box(railPlatform, 0.22, 0.32, 2.78, 8.36, 1.37, 0, COLORS.ink);
for (const x of [3.8, 5.6, 7.4]) {
box(railPlatform, 0.07, 0.9, 0.07, x, 2.27, -1.28, COLORS.steel);
}
box(railPlatform, 3.7, 0.07, 0.07, 5.6, 2.68, -1.28, COLORS.steel);
// Cargo already on the rail deck
box(railPlatform, 1.2, 1.05, 1.45, 6.45, 2.325, -0.1, 0x79948f);
box(railPlatform, 0.95, 0.78, 1.1, 4.8, 2.19, 0.18, 0xc58a4c);
box(railPlatform, 0.05, 1.06, 1.48, 6.45, 2.325, -0.1, COLORS.ink);
// ------------------------------------------------------------------
// Variable-height transfer deck
// ------------------------------------------------------------------
const liftBase = new THREE.Group();
scene.add(liftBase);
box(liftBase, 1.55, 0.18, 1.26, -0.15, 0.2, 0, COLORS.ink);
box(liftBase, 1.32, 0.08, 1.05, -0.15, 0.33, 0, COLORS.orange);
for (const x of [-0.7, 0.4]) {
for (const z of [-0.55, 0.55]) {
cylinder(liftBase, 0.16, 0.12, x, 0.12, z, 0x334248);
}
}
function createLiftBar(z, color) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 1, 0.075),
cachedMaterial(color, { metalness: 0.22 })
);
mesh.castShadow = true;
scene.add(mesh);
return { mesh, z };
}
const liftBars = [
createLiftBar(0.49, COLORS.steel),
createLiftBar(0.49, COLORS.steel),
createLiftBar(-0.49, COLORS.steel),
createLiftBar(-0.49, COLORS.steel)
];
function updateLiftBar(bar, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
bar.mesh.position.copy(a).add(b).multiplyScalar(0.5);
bar.mesh.scale.y = length;
bar.mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
}
const pallet = new THREE.Group();
scene.add(pallet);
// Moving lift plate
box(pallet, 1.42, 0.1, 1.18, -0.15, -0.05, 0, COLORS.steel);
// Voxel pallet: runners, blocks, and top deck boards
for (const z of [-0.43, 0, 0.43]) {
box(pallet, 1.2, 0.075, 0.18, -0.15, 0.038, z, COLORS.wood);
}
for (const x of [-0.62, -0.15, 0.32]) {
for (const z of [-0.42, 0, 0.42]) {
box(pallet, 0.19, 0.085, 0.18, x, 0.115, z, COLORS.woodLight);
}
}
for (let i = 0; i < 5; i++) {
const z = -0.46 + i * 0.23;
box(pallet, 1.2, 0.065, 0.17, -0.15, 0.19, z, COLORS.woodLight);
}
// Instanced voxel cargo: eight small crates, each four cells wide.
const cellSize = 0.115;
const cratePositions = [];
for (const x of [-0.255, 0.255]) {
for (const z of [-0.255, 0.255]) {
for (const y of [0.21, 0.69]) {
cratePositions.push({ x, y, z });
}
}
}
const voxelCount = cratePositions.length * 4 * 4 * 4;
const voxelGeometry = new THREE.BoxGeometry(
cellSize * 0.94,
cellSize * 0.94,
cellSize * 0.94
);
const voxelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.86,
metalness: 0.02
});
const cargoVoxels = new THREE.InstancedMesh(
voxelGeometry,
voxelMaterial,
voxelCount
);
cargoVoxels.castShadow = true;
cargoVoxels.receiveShadow = true;
const dummy = new THREE.Object3D();
const voxelColor = new THREE.Color();
const cratePalette = [
[0xe77542, 0xf08a55],
[0x287f86, 0x38949a],
[0xd0a15b, 0xe1b46d],
[0x667f7a, 0x78938c]
];
let voxelIndex = 0;
cratePositions.forEach((crate, crateIndex) => {
const palette = cratePalette[crateIndex % cratePalette.length];
for (let ix = 0; ix < 4; ix++) {
for (let iy = 0; iy < 4; iy++) {
for (let iz = 0; iz < 4; iz++) {
dummy.position.set(
crate.x + (ix - 1.5) * cellSize,
crate.y + (iy + 0.5) * cellSize,
crate.z + (iz - 1.5) * cellSize
);
dummy.updateMatrix();
cargoVoxels.setMatrixAt(voxelIndex, dummy.matrix);
const edge =
ix === 0 || ix === 3 || iy === 0 || iy === 3 || iz === 0 || iz === 3;
const alternate = (ix + iy + iz + crateIndex) % 3 === 0;
voxelColor.set(
alternate
? palette[1]
: edge
? palette[0]
: palette[1]
);
voxelColor.offsetHSL(0, 0, alternate ? 0.025 : -0.025);
cargoVoxels.setColorAt(voxelIndex, voxelColor);
voxelIndex++;
}
}
}
});
cargoVoxels.instanceMatrix.needsUpdate = true;
cargoVoxels.instanceColor.needsUpdate = true;
pallet.add(cargoVoxels);
// Cargo straps
for (const x of [-0.45, 0.15]) {
box(pallet, 0.045, 0.98, 0.03, x, 0.68, 0.493, COLORS.ink);
box(pallet, 0.045, 0.98, 0.03, x, 0.68, -0.493, COLORS.ink);
}
box(pallet, 1.06, 0.045, 0.035, -0.15, 0.91, 0.505, COLORS.ink);
box(pallet, 1.06, 0.045, 0.035, -0.15, 0.91, -0.505, COLORS.ink);
// ------------------------------------------------------------------
// Handling-clearance envelope
// ------------------------------------------------------------------
const clearanceGroup = new THREE.Group();
scene.add(clearanceGroup);
const envelopeWidth = 1.8;
const envelopeDepth = 1.65;
const overheadHeight = 4.05;
const envelopeGeometry = new THREE.BoxGeometry(
envelopeWidth,
overheadHeight,
envelopeDepth
);
const envelopeMaterial = new THREE.MeshBasicMaterial({
color: COLORS.yellow,
transparent: true,
opacity: 0.055,
depthWrite: false
});
const envelopeMesh = new THREE.Mesh(envelopeGeometry, envelopeMaterial);
envelopeMesh.position.set(-0.15, overheadHeight / 2, 0);
envelopeMesh.renderOrder = 2;
clearanceGroup.add(envelopeMesh);
const envelopeEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(envelopeGeometry),
new THREE.LineBasicMaterial({
color: COLORS.yellow,
transparent: true,
opacity: 0.82
})
);
envelopeEdges.position.copy(envelopeMesh.position);
envelopeEdges.renderOrder = 3;
clearanceGroup.add(envelopeEdges);
// Overhead datum
line(
[
[-1.08, overheadHeight, 0.84],
[0.78, overheadHeight, 0.84]
],
COLORS.purple,
0.8,
true
);
// ------------------------------------------------------------------
// Dimension helpers
// ------------------------------------------------------------------
function createVerticalDimension(x, z, y1, y2, color) {
const group = new THREE.Group();
scene.add(group);
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, y1, z),
new THREE.Vector3(x, y2, z)
]);
const mainLine = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({ color })
);
group.add(mainLine);
const capGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x - 0.13, y1, z),
new THREE.Vector3(x + 0.13, y1, z)
]);
const cap = new THREE.Line(
capGeometry,
new THREE.LineBasicMaterial({ color })
);
group.add(cap);
const arrowGeometry = new THREE.ConeGeometry(0.045, 0.13, 8);
const lowerArrow = new THREE.Mesh(
arrowGeometry,
new THREE.MeshBasicMaterial({ color })
);
const upperArrow = new THREE.Mesh(
arrowGeometry,
new THREE.MeshBasicMaterial({ color })
);
lowerArrow.rotation.z = Math.PI;
group.add(lowerArrow, upperArrow);
function update(newY1, newY2) {
const points = geometry.attributes.position;
points.setY(0, newY1);
points.setY(1, newY2);
points.needsUpdate = true;
geometry.computeBoundingSphere();
const capPoints = capGeometry.attributes.position;
capPoints.setY(0, newY1);
capPoints.setY(1, newY1);
capPoints.needsUpdate = true;
geometry.computeBoundingSphere();
lowerArrow.position.set(x, newY1 + 0.07, z);
upperArrow.position.set(x, newY2 - 0.07, z);
}
update(y1, y2);
return { group, update };
}
createVerticalDimension(-2.72, 1.55, 0, 1.2, COLORS.teal);
createVerticalDimension(3.08, 1.55, 0, 1.8, COLORS.orange);
createVerticalDimension(2.6, 1.94, 1.2, 1.8, COLORS.orange);
const clearanceDimension = createVerticalDimension(
-0.78,
0.86,
2.35,
overheadHeight,
COLORS.purple
);
// Side-clearance dimension
line(
[
[-0.15, 1.45, 0.49],
[-0.15, 1.45, 0.825]
],
COLORS.purple
);
line(
[
[-0.22, 1.45, 0.49],
[-0.08, 1.45, 0.49]
],
COLORS.purple
);
line(
[
[-0.22, 1.45, 0.825],
[-0.08, 1.45, 0.825]
],
COLORS.purple
);
// Ground transfer-axis arrow
line(
[
[-2.6, 0.055, 1.9],
[2.45, 0.055, 1.9]
],
COLORS.orange,
0.9,
true
);
const axisArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.12, 0.38, 8),
new THREE.MeshBasicMaterial({ color: COLORS.orange })
);
axisArrow.rotation.z = -Math.PI / 2;
axisArrow.position.set(2.55, 0.055, 1.9);
scene.add(axisArrow);
// ------------------------------------------------------------------
// HTML callouts with projected leader lines
// ------------------------------------------------------------------
const callouts = [];
function addCallout({
number,
title,
detail,
color,
anchor,
offset,
detailElement
}) {
const element = document.createElement("div");
element.className = "callout";
element.style.setProperty("--accent", color);
element.innerHTML = `
<span class="index">${number}</span>
<div>
<b>${title}</b>
<small>${detail}</small>
</div>
`;
labelsRoot.appendChild(element);
const leader = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
leader.setAttribute("stroke", color);
leader.setAttribute("stroke-width", "1.35");
leader.setAttribute("stroke-dasharray", "3 4");
leader.setAttribute("opacity", "0.78");
leaderSvg.appendChild(leader);
callouts.push({
element,
leader,
anchor,
offset,
detailElement
});
}
const cssColor = {
teal: "#168b87",
orange: "#ee6a3b",
purple: "#7057b8"
};
addCallout({
number: "01",
title: "Truck bed",
detail: "1.20 m above grade",
color: cssColor.teal,
anchor: () => new THREE.Vector3(-2.72, 0.9, 1.55),
offset: [-210, -44]
});
addCallout({
number: "02",
title: "Transfer deck",
detail: "0 → 0.60 m lift",
color: cssColor.orange,
anchor: () => new THREE.Vector3(-0.15, pallet.position.y + 0.25, 0.28),
offset: [-190, 64]
});
addCallout({
number: "03",
title: "Rail platform",
detail: "1.80 m above grade",
color: cssColor.orange,
anchor: () => new THREE.Vector3(3.08, 1.33, 1.55),
offset: [68, -52]
});
addCallout({
number: "04",
title: "Vertical clearance",
detail: "1.70 m current",
color: cssColor.purple,
anchor: () =>
new THREE.Vector3(
-0.78,
(pallet.position.y + 1.15 + overheadHeight) / 2,
0.86
),
offset: [64, -12],
detailElement: "clearance"
});
addCallout({
number: "05",
title: "Side clearance",
detail: "0.33 m per side",
color: cssColor.purple,
anchor: () => new THREE.Vector3(-0.15, pallet.position.y + 0.45, 0.825),
offset: [76, 62]
});
const projection = new THREE.Vector3();
function updateCallouts() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const callout of callouts) {
projection.copy(callout.anchor()).project(camera);
const anchorX = (projection.x * 0.5 + 0.5) * width;
const anchorY = (-projection.y * 0.5 + 0.5) * height;
const boxWidth = callout.element.offsetWidth;
const boxHeight = callout.element.offsetHeight;
let left = anchorX + callout.offset[0];
let top = anchorY + callout.offset[1];
left = THREE.MathUtils.clamp(left, 12, width - boxWidth - 12);
top = THREE.MathUtils.clamp(top, 112, height - boxHeight - 105);
callout.element.style.left = `${left}px`;
callout.element.style.top = `${top}px`;
let targetX = left + boxWidth / 2;
let targetY = top + boxHeight / 2;
if (anchorX < left) targetX = left;
else if (anchorX > left + boxWidth) targetX = left + boxWidth;
if (anchorY < top) targetY = top;
else if (anchorY > top + boxHeight) targetY = top + boxHeight;
callout.leader.setAttribute("x1", anchorX);
callout.leader.setAttribute("y1", anchorY);
callout.leader.setAttribute("x2", targetX);
callout.leader.setAttribute("y2", targetY);
const visible =
projection.z > -1 &&
projection.z < 1 &&
anchorX > -100 &&
anchorX < width + 100 &&
anchorY > -100 &&
anchorY < height + 100;
callout.element.style.opacity = visible ? "1" : "0";
callout.leader.style.opacity = visible ? "0.78" : "0";
}
}
// ------------------------------------------------------------------
// Camera controls implemented directly, without add-ons
// ------------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (event.shiftKey || event.button === 2) {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0)
.normalize();
const forward = camera.getWorldDirection(new THREE.Vector3());
forward.y = 0;
forward.normalize();
const panScale = desiredCamera.radius * 0.00125;
desiredCamera.target.addScaledVector(right, -dx * panScale);
desiredCamera.target.addScaledVector(forward, dy * panScale);
} else {
desiredCamera.theta -= dx * 0.006;
desiredCamera.phi = THREE.MathUtils.clamp(
desiredCamera.phi - dy * 0.005,
0.13,
1.5
);
}
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.radius = THREE.MathUtils.clamp(
desiredCamera.radius * Math.exp(event.deltaY * 0.001),
7,
28
);
},
{ passive: false }
);
const cameraPresets = {
overview: {
target: new THREE.Vector3(-0.45, 1.45, 0),
radius: 19,
theta: 0.63,
phi: 1.02
},
elevation: {
target: new THREE.Vector3(-0.15, 1.65, 0),
radius: 15,
theta: 0.12,
phi: 1.37
},
plan: {
target: new THREE.Vector3(-0.15, 1.25, 0),
radius: 16,
theta: 0.001,
phi: 0.16
}
};
document.querySelectorAll("[data-camera]").forEach((button) => {
button.addEventListener("click", () => {
const preset = cameraPresets[button.dataset.camera];
desiredCamera.target.copy(preset.target);
desiredCamera.radius = preset.radius;
desiredCamera.theta = preset.theta;
desiredCamera.phi = preset.phi;
document
.querySelectorAll("[data-camera]")
.forEach((other) => other.classList.remove("active"));
button.classList.add("active");
});
});
// ------------------------------------------------------------------
// Lift and visibility controls
// ------------------------------------------------------------------
let lift = 0;
let liftTarget = 0;
liftButton.addEventListener("click", () => {
liftTarget = liftTarget > 0.5 ? 0 : 1;
liftButton.textContent =
liftTarget > 0.5 ? "Lower to truck" : "Raise to rail";
liftButton.classList.toggle("on", liftTarget > 0.5);
});
clearanceButton.addEventListener("click", () => {
clearanceGroup.visible = !clearanceGroup.visible;
clearanceButton.classList.toggle("on", clearanceGroup.visible);
});
function updateLift(delta) {
lift = THREE.MathUtils.damp(lift, liftTarget, 4.4, delta);
const palletBaseY = 1.2 + lift * 0.6;
pallet.position.y = palletBaseY;
const baseY = 0.35;
const topY = palletBaseY - 0.1;
updateLiftBar(
liftBars[0],
new THREE.Vector3(-0.66, baseY, 0.49),
new THREE.Vector3(0.36, topY, 0.49)
);
updateLiftBar(
liftBars[1],
new THREE.Vector3(0.36, baseY, 0.49),
new THREE.Vector3(-0.66, topY, 0.49)
);
updateLiftBar(
liftBars[2],
new THREE.Vector3(-0.66, baseY, -0.49),
new THREE.Vector3(0.36, topY, -0.49)
);
updateLiftBar(
liftBars[3],
new THREE.Vector3(0.36, baseY, -0.49),
new THREE.Vector3(-0.66, topY, -0.49)
);
const cargoTop = palletBaseY + 1.15;
const clearance = overheadHeight - cargoTop;
clearanceDimension.update(cargoTop, overheadHeight);
clearanceValue.innerHTML = `${clearance.toFixed(2)} <em>m</em>`;
for (const callout of callouts) {
if (callout.detailElement === "clearance") {
callout.element.querySelector("small").textContent =
`${clearance.toFixed(2)} m current`;
}
}
}
// ------------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
leaderSvg.setAttribute("viewBox", `0 0 ${window.innerWidth} ${window.innerHeight}`);
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
const spherical = new THREE.Spherical();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
cameraState.target.lerp(desiredCamera.target, 0.12);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
desiredCamera.radius,
0.12
);
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
desiredCamera.theta,
0.12
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
desiredCamera.phi,
0.12
);
spherical.set(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraState.target);
camera.lookAt(cameraState.target);
updateLift(delta);
updateCallouts();
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
</script>
</body>
</html>
```