Prompt specification
Build or customize the Three.js 3D scene "Clockmaker's Cargo Elevator Puzzle" 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: Clockmaker's Cargo Elevator Puzzle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/clockmaker-s-cargo-elevator-puzzle-x3447
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Build an original workshop with a counterweighted lift and selectable cargo. Use an explicit idealized balance rule and constrain the lift to its vertical guide.
Working Three.js Starter (clockmaker-s-cargo-elevator-puzzle-x3447):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Moss & Iron — Counterweight Workshop</title>
<style>
:root {
--paper: #f2ead8;
--muted: #a8b1aa;
--panel: rgba(17, 25, 26, 0.94);
--line: rgba(224, 234, 220, 0.16);
--amber: #efb84b;
--amber-dark: #302719;
--teal: #68cfc0;
--red: #ed7a67;
--blue: #80aee8;
--shadow: 0 18px 60px rgba(0, 0, 0, 0.38);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #111719;
color: var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.brand {
position: fixed;
top: 24px;
left: 28px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 14px rgba(0, 0, 0, 0.65);
}
.eyebrow {
margin-bottom: 7px;
color: var(--amber);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-size: clamp(24px, 3vw, 42px);
line-height: 0.95;
letter-spacing: -0.045em;
text-transform: uppercase;
}
.brand p {
max-width: 390px;
margin: 10px 0 0;
color: #d1d6cc;
font-size: 13px;
line-height: 1.5;
}
.console {
position: fixed;
z-index: 10;
top: 24px;
right: 24px;
width: min(354px, calc(100vw - 48px));
overflow: hidden;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.console-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 17px 18px;
border-bottom: 1px solid var(--line);
background:
linear-gradient(135deg, rgba(239, 184, 75, 0.12), transparent 55%),
rgba(255, 255, 255, 0.018);
}
.console-title {
font: 800 12px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 12px currentColor;
}
.status[data-state="rise"] {
color: var(--teal);
}
.status[data-state="fall"] {
color: var(--red);
}
.status[data-state="hold"] {
color: var(--teal);
}
.status[data-state="brake"] .status-dot,
.status[data-state="limit"] .status-dot {
background: var(--amber);
}
.section {
padding: 16px 18px;
border-bottom: 1px solid var(--line);
}
.section:last-child {
border-bottom: 0;
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.section-label span:last-child {
color: #69746e;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: none;
}
.cargo-list {
display: grid;
gap: 7px;
}
.cargo-card {
display: grid;
grid-template-columns: 34px 1fr auto;
gap: 10px;
align-items: center;
width: 100%;
min-height: 48px;
padding: 7px 10px 7px 8px;
border: 1px solid transparent;
border-radius: 10px;
color: var(--paper);
text-align: left;
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
transition: 140ms ease;
}
.cargo-card:hover {
border-color: rgba(239, 184, 75, 0.42);
background: rgba(239, 184, 75, 0.075);
transform: translateX(-2px);
}
.cargo-card.active {
border-color: rgba(239, 184, 75, 0.65);
background: rgba(239, 184, 75, 0.11);
}
.cargo-swatch {
position: relative;
width: 29px;
height: 29px;
border-radius: 5px;
background: var(--swatch);
box-shadow:
inset -6px -6px 0 rgba(0, 0, 0, 0.16),
inset 0 0 0 2px rgba(255, 255, 255, 0.13);
}
.cargo-swatch.empty {
background: #46514d;
}
.cargo-swatch.empty::after {
content: "";
position: absolute;
inset: 8px;
border: 2px dashed #9ca59d;
border-radius: 50%;
}
.cargo-name {
display: block;
margin-bottom: 2px;
font-size: 12px;
font-weight: 750;
}
.cargo-kind {
display: block;
color: var(--muted);
font-size: 10px;
}
.cargo-mass {
padding: 5px 7px;
border-radius: 6px;
color: #d9dfd7;
background: rgba(0, 0, 0, 0.24);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.readouts {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 13px;
}
.readout {
padding: 9px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 9px;
background: rgba(0, 0, 0, 0.16);
}
.readout small {
display: block;
margin-bottom: 5px;
color: #7f8b84;
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readout strong {
font: 800 15px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.balance-scale {
position: relative;
height: 13px;
margin: 7px 3px 9px;
border-radius: 8px;
background:
linear-gradient(
90deg,
rgba(237, 122, 103, 0.58) 0%,
rgba(237, 122, 103, 0.13) 49.5%,
rgba(104, 207, 192, 0.13) 50.5%,
rgba(104, 207, 192, 0.58) 100%
);
border: 1px solid rgba(255, 255, 255, 0.09);
}
.balance-scale::after {
content: "";
position: absolute;
top: -3px;
bottom: -3px;
left: 50%;
width: 1px;
background: rgba(255, 255, 255, 0.5);
}
#balanceMarker {
position: absolute;
top: 50%;
left: 50%;
width: 8px;
height: 18px;
border: 2px solid #fff0c8;
border-radius: 4px;
background: var(--amber);
box-shadow: 0 0 12px rgba(239, 184, 75, 0.7);
transform: translate(-50%, -50%);
transition: left 220ms ease;
}
.scale-labels {
display: flex;
justify-content: space-between;
color: #77827b;
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.rule {
margin: 12px 0 0;
color: #aeb8b0;
font-size: 10.5px;
line-height: 1.52;
}
.rule strong {
color: var(--paper);
}
.guide-note {
margin-top: 7px;
color: #6f7b74;
font: 600 9px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.button-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
margin-top: 13px;
}
button.action {
min-height: 40px;
padding: 0 13px;
border: 1px solid rgba(239, 184, 75, 0.45);
border-radius: 9px;
color: #211b0f;
background: var(--amber);
font-size: 11px;
font-weight: 850;
cursor: pointer;
transition: 140ms ease;
}
button.action:hover {
background: #ffd16a;
transform: translateY(-1px);
}
button.action.released {
border-color: rgba(104, 207, 192, 0.4);
color: #d9fff8;
background: rgba(104, 207, 192, 0.15);
}
button.action.secondary {
border-color: var(--line);
color: #cbd3ca;
background: rgba(255, 255, 255, 0.045);
}
button.action.secondary:hover {
background: rgba(255, 255, 255, 0.09);
}
.help {
position: fixed;
z-index: 4;
left: 28px;
bottom: 24px;
max-width: 440px;
padding: 11px 13px;
border: 1px solid var(--line);
border-radius: 10px;
color: #c0c9c0;
background: rgba(15, 21, 22, 0.8);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
font-size: 11px;
line-height: 1.5;
pointer-events: none;
}
.help b {
color: var(--amber);
}
#tooltip {
position: fixed;
z-index: 30;
left: 0;
top: 0;
padding: 7px 9px;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 6px;
color: #f4f0e5;
background: rgba(12, 17, 18, 0.92);
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.3);
font-size: 10px;
pointer-events: none;
opacity: 0;
transform: translate(12px, 12px);
transition: opacity 100ms ease;
}
#tooltip.visible {
opacity: 1;
}
#toast {
position: fixed;
z-index: 40;
left: 50%;
bottom: 24px;
max-width: min(440px, calc(100vw - 32px));
padding: 11px 15px;
border: 1px solid rgba(239, 184, 75, 0.38);
border-radius: 9px;
color: #f5ead0;
background: rgba(27, 29, 24, 0.94);
box-shadow: var(--shadow);
font-size: 11px;
text-align: center;
pointer-events: none;
opacity: 0;
transform: translate(-50%, 12px);
transition: 180ms ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 16px;
}
.brand p {
display: none;
}
.console {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
max-height: 61vh;
overflow-y: auto;
border-radius: 14px;
}
.section {
padding: 12px 14px;
}
.cargo-list {
grid-template-columns: 1fr 1fr;
}
.cargo-card {
grid-template-columns: 29px 1fr;
}
.cargo-mass {
display: none;
}
.help {
display: none;
}
#toast {
bottom: calc(61vh + 24px);
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header class="brand">
<div class="eyebrow">Voxel workshop / bay 07</div>
<h1>Moss & Iron</h1>
<p>
A counterweight freight lift built from honest blocks, stubborn steel,
and one very explicit balance rule.
</p>
</header>
<aside class="console" aria-label="Lift control console">
<div class="console-header">
<div class="console-title">Counterweight console</div>
<div class="status" id="status" data-state="brake">
<span class="status-dot"></span>
<span id="statusText">Brake engaged</span>
</div>
</div>
<section class="section">
<div class="section-label">
<span>Select cargo</span>
<span>Click a crate or a card</span>
</div>
<div class="cargo-list" id="cargoList">
<button class="cargo-card active" data-cargo="" type="button">
<span class="cargo-swatch empty"></span>
<span>
<span class="cargo-name">Empty carriage</span>
<span class="cargo-kind">Unload current freight</span>
</span>
<span class="cargo-mass">0 kg</span>
</button>
<button class="cargo-card" data-cargo="brass" type="button">
<span class="cargo-swatch" style="--swatch:#c77942"></span>
<span>
<span class="cargo-name">Brass relay</span>
<span class="cargo-kind">Compact workshop freight</span>
</span>
<span class="cargo-mass">80 kg</span>
</button>
<button class="cargo-card" data-cargo="sapphire" type="button">
<span class="cargo-swatch" style="--swatch:#4c9e9a"></span>
<span>
<span class="cargo-name">Sapphire glass</span>
<span class="cargo-kind">Balanced reference load</span>
</span>
<span class="cargo-mass">140 kg</span>
</button>
<button class="cargo-card" data-cargo="iron" type="button">
<span class="cargo-swatch" style="--swatch:#58718f"></span>
<span>
<span class="cargo-name">Blue iron</span>
<span class="cargo-kind">Heavy cast freight</span>
</span>
<span class="cargo-mass">260 kg</span>
</button>
</div>
</section>
<section class="section">
<div class="section-label">
<span>Idealized balance</span>
<span>1:1 reeving</span>
</div>
<div class="readouts">
<div class="readout">
<small>Lift side</small>
<strong id="liftMass">420 kg</strong>
</div>
<div class="readout">
<small>Weight side</small>
<strong>560 kg</strong>
</div>
<div class="readout">
<small>Balance B</small>
<strong id="balanceValue">+140 kg</strong>
</div>
</div>
<div class="balance-scale" aria-label="Balance indicator">
<span id="balanceMarker"></span>
</div>
<div class="scale-labels">
<span>Lower</span>
<span>Hold</span>
<span>Raise</span>
</div>
<p class="rule">
<strong>B = counterweight − lift side.</strong>
If B > +15 kg, the carriage rises. If B < −15 kg, it lowers.
If |B| ≤ 15 kg, it holds. Motion speed is illustrative.
</p>
<div class="guide-note">
GUIDE LOCK: x/z fixed · vertical travel 1.45–8.00 m ·
counterweight travels equally in the opposite direction.
</div>
<div class="button-row">
<button class="action" id="brakeButton" type="button">
<span id="brakeLabel">Release brake</span>
</button>
<button class="action secondary" id="centerButton" type="button">
Recenter
</button>
</div>
</section>
</aside>
<div class="help">
<b>Controls:</b> drag to orbit · wheel/pinch to zoom · click cargo to load or unload ·
<b>R</b> brake · <b>C</b> recenter · <b>1–3</b> cargo
</div>
<div id="tooltip"></div>
<div id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Core setup
// -------------------------------------------------------------------------
const viewport = document.querySelector("#viewport");
const tooltip = document.querySelector("#tooltip");
const toast = document.querySelector("#toast");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 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.15;
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x141b1d);
scene.fog = new THREE.Fog(0x141b1d, 25, 48);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Direct camera controls; no addons are used.
const cameraTarget = new THREE.Vector3(0, 4.25, 0);
const cameraState = {
theta: 0.58,
phi: 0.98,
radius: 18.5,
dragging: false,
pointerDown: false,
moved: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
const { theta, phi, radius } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xb9d9dc, 0x33291f, 1.8);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffd9a1, 3.0);
sun.position.set(8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -16;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 42;
sun.shadow.bias = -0.00025;
scene.add(sun);
const liftLight = new THREE.PointLight(0x8fe5d7, 18, 13, 2);
liftLight.position.set(-3, 7.4, 2.5);
scene.add(liftLight);
// -------------------------------------------------------------------------
// Voxel construction helpers
// -------------------------------------------------------------------------
const materialCache = new Map();
function standardMaterial(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.04
})
);
}
return materialCache.get(color);
}
function box(parent, size, position, color, castShadow = true) {
const material =
color && color.isMaterial ? color : standardMaterial(color);
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 = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function group(parent = scene) {
const g = new THREE.Group();
parent.add(g);
return g;
}
function addSign(
parent,
title,
subtitle,
size,
position,
background = "#172322",
foreground = "#efd59a"
) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 256;
const context = canvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = foreground;
context.globalAlpha = 0.45;
context.lineWidth = 8;
context.strokeRect(14, 14, canvas.width - 28, canvas.height - 28);
context.globalAlpha = 1;
context.textAlign = "center";
context.fillStyle = foreground;
context.font = "800 70px monospace";
context.fillText(title, canvas.width / 2, 116);
if (subtitle) {
context.fillStyle = "#aebdb3";
context.font = "600 27px monospace";
context.fillText(subtitle, canvas.width / 2, 178);
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(size[0], size[1]),
new THREE.MeshBasicMaterial({
map: texture,
toneMapped: false
})
);
mesh.position.set(position[0], position[1], position[2]);
parent.add(mesh);
return mesh;
}
function addCrateLabel(parent, text, color) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const context = canvas.getContext("2d");
context.fillStyle = "#202827";
context.fillRect(0, 0, 256, 128);
context.strokeStyle = color;
context.lineWidth = 7;
context.strokeRect(7, 7, 242, 114);
context.fillStyle = "#f4ead3";
context.font = "800 48px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 128, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const label = new THREE.Mesh(
new THREE.PlaneGeometry(0.72, 0.36),
new THREE.MeshBasicMaterial({
map: texture,
toneMapped: false
})
);
label.position.set(0, 0, 0.461);
parent.add(label);
return label;
}
let randomSeed = 731;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
// -------------------------------------------------------------------------
// Workshop shell
// -------------------------------------------------------------------------
box(scene, [22.4, 0.6, 17.4], [0, -0.3, 0], 0x273033, false);
const floorColors = [0x46504d, 0x4b5550, 0x414b49, 0x515954, 0x47514e];
for (let x = -10; x <= 10; x += 2) {
for (let z = -7; z <= 7; z += 2) {
const color = floorColors[Math.floor(random() * floorColors.length)];
box(scene, [1.94, 0.08, 1.94], [x, 0.015, z], color, false);
}
}
const wallColors = [0x59605a, 0x535c56, 0x5f655d, 0x4c5651];
for (let row = 0; row < 8; row++) {
for (let column = 0; column < 11; column++) {
const color = wallColors[Math.floor(random() * wallColors.length)];
box(
scene,
[1.96, 0.96, 0.4],
[-10 + column * 2, 0.5 + row, -8.35],
color
);
}
for (let column = 0; column < 8; column++) {
const color = wallColors[Math.floor(random() * wallColors.length)];
box(
scene,
[0.4, 0.96, 1.96],
[-11.25, 0.5 + row, -7 + column * 2],
color
);
}
}
// Structural beams.
box(scene, [0.45, 8.5, 0.6], [-10.65, 4.25, -7.95], 0x303a37);
box(scene, [0.45, 8.5, 0.6], [10.65, 4.25, -7.95], 0x303a37);
box(scene, [21.5, 0.42, 0.55], [0, 8.2, -7.95], 0x303a37);
box(scene, [0.48, 8.2, 0.55], [-10.8, 4.1, 3.8], 0x303a37);
// Frosted workshop windows.
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0x6f9fa3,
emissive: 0x274d51,
emissiveIntensity: 0.85,
roughness: 0.35,
metalness: 0.08
});
for (const x of [-7.6, -3.8]) {
box(scene, [2.9, 2.2, 0.1], [x, 5.5, -8.08], windowMaterial);
box(scene, [3.12, 0.15, 0.16], [x, 5.5, -7.98], 0x252d2d);
box(scene, [0.15, 2.35, 0.16], [x, 5.5, -7.98], 0x252d2d);
box(scene, [3.12, 0.15, 0.28], [x, 4.35, -7.95], 0x303636);
}
addSign(
scene,
"MOSS & IRON",
"FREIGHT / REPAIRS / STRANGE METALS",
[6.4, 1.65],
[4.2, 6.95, -8.03],
"#1c2927",
"#efd08a"
);
// -------------------------------------------------------------------------
// Workbench, shelves, tools, furnace
// -------------------------------------------------------------------------
const bench = group(scene);
bench.position.set(-7.1, 0, -4.9);
box(bench, [4.4, 0.3, 1.65], [0, 1.55, 0], 0x806348);
box(bench, [4.15, 0.12, 1.48], [0, 1.75, 0], 0x9a7652);
for (const x of [-1.8, 1.8]) {
for (const z of [-0.55, 0.55]) {
box(bench, [0.22, 1.5, 0.22], [x, 0.75, z], 0x39433f);
}
}
box(bench, [0.75, 0.55, 0.65], [-1.2, 2.05, 0], 0x4b5c5d);
box(bench, [0.24, 0.7, 0.22], [-1.2, 2.4, 0], 0x6b7772);
box(bench, [0.95, 0.12, 0.3], [-1.2, 2.75, 0], 0x6b7772);
// Tool board.
box(bench, [4.3, 2.4, 0.16], [0, 3.35, -2.72], 0x785d45);
for (let i = 0; i < 6; i++) {
const x = -1.55 + i * 0.62;
box(bench, [0.11, 0.9 + (i % 3) * 0.15, 0.12], [x, 3.45, -2.58], 0xb7beb4);
box(bench, [0.34, 0.14, 0.12], [x, 2.94, -2.58], 0x8e9991);
}
// Back shelving.
const shelves = group(scene);
shelves.position.set(2.5, 0, -7.45);
for (const x of [-2.7, 2.7]) {
box(shelves, [0.18, 6.2, 0.75], [x, 3.1, 0], 0x3b4540);
}
for (const y of [1.2, 2.8, 4.4]) {
box(shelves, [5.6, 0.16, 1.25], [0, y, 0], 0x4b5550);
for (let i = 0; i < 5; i++) {
const crateColor = [0x8f6343, 0x566b61, 0x6d6855][i % 3];
box(
shelves,
[0.78, 0.75, 0.78],
[-2.1 + i * 1.05, y + 0.46, 0],
crateColor
);
}
}
// Furnace.
const furnace = group(scene);
furnace.position.set(8.1, 0, -6.15);
box(furnace, [3.25, 3.15, 2.6], [0, 1.58, 0], 0x795341);
box(furnace, [3.45, 0.28, 2.75], [0, 3.25, 0], 0x3d4541);
box(furnace, [2.4, 1.65, 0.18], [0, 1.35, 1.36], 0x262a28);
const furnaceGlowMaterial = new THREE.MeshStandardMaterial({
color: 0xff9b35,
emissive: 0xff5d12,
emissiveIntensity: 2.3,
roughness: 0.5
});
box(furnace, [1.8, 1.08, 0.12], [0, 1.25, 1.47], furnaceGlowMaterial);
box(furnace, [0.22, 2.45, 0.22], [-1.35, 4.45, -0.8], 0x4a514d);
box(furnace, [0.22, 2.45, 0.22], [1.35, 4.45, -0.8], 0x4a514d);
box(furnace, [2.95, 0.22, 0.22], [0, 5.65, -0.8], 0x4a514d);
// -------------------------------------------------------------------------
// Lift frame, guides, platform, and counterweight
// -------------------------------------------------------------------------
const PLATFORM_MIN = 1.45;
const PLATFORM_MAX = 8.0;
const PULLEY_TOP = 9.6;
// Idealized 1:1 rope constraint:
// platformAttachmentY + counterweightAttachmentY = LINK_SUM.
const LINK_SUM = 11.0;
let platformY = 4.7;
let counterweightY = LINK_SUM - platformY;
let brakeEngaged = true;
let atTravelLimit = false;
// Main structural frame.
box(scene, [0.5, 9.55, 0.62], [-4.9, 4.75, -0.9], 0x34413d);
box(scene, [0.5, 9.55, 0.62], [4.25, 4.75, -0.9], 0x34413d);
box(scene, [9.65, 0.52, 0.7], [-0.325, 9.75, -0.9], 0x3c4944);
// Platform guide rails. The carriage can only change Y.
for (const x of [-4.28, -1.72]) {
for (const z of [-0.72, 0.72]) {
box(scene, [0.2, 9.05, 0.2], [x, 4.85, z], 0x66716b);
box(scene, [0.38, 0.25, 0.38], [x, 0.33, z], 0x2d3734);
}
}
for (const y of [0.52, 9.2]) {
box(scene, [2.92, 0.22, 1.75], [-3, y, 0], 0x46534e);
}
// Counterweight guide rails.
for (const x of [2.18, 3.82]) {
for (const z of [-0.58, 0.58]) {
box(scene, [0.17, 9.25, 0.17], [x, 5.0, z], 0x59655f);
}
}
for (const y of [0.62, 9.65]) {
box(scene, [1.92, 0.18, 1.45], [3, y, 0], 0x3e4b46);
}
// Moving platform. Its group origin is the rope attachment height.
const platformGroup = group(scene);
platformGroup.position.set(-3, platformY, 0);
box(platformGroup, [2.52, 0.25, 2.14], [0, -0.125, 0], 0x65736d);
box(platformGroup, [2.25, 0.08, 1.92], [0, 0.025, 0], 0x7c857b);
for (let i = 0; i < 6; i++) {
box(
platformGroup,
[0.34, 0.045, 1.88],
[-0.95 + i * 0.38, 0.082, 0],
i % 2 ? 0x737f77 : 0x879087
);
}
for (const x of [-1.17, 1.17]) {
for (const z of [-0.92, 0.92]) {
box(platformGroup, [0.14, 1.18, 0.14], [x, 0.59, z], 0x394640);
box(platformGroup, [0.24, 0.16, 0.24], [x, 1.18, z], 0xe0a53c);
}
}
// Back and side safety rails; front remains open for loading.
box(platformGroup, [2.35, 0.11, 0.11], [0, 1.12, -0.92], 0x4a5a52);
box(platformGroup, [0.11, 0.11, 1.82], [-1.17, 1.12, 0], 0x4a5a52);
box(platformGroup, [0.11, 0.11, 1.82], [1.17, 1.12, 0], 0x4a5a52);
// Yellow loading edge.
box(platformGroup, [2.38, 0.09, 0.13], [0, 0.09, 1.02], 0xe3aa3d);
// Counterweight group origin is its rope attachment.
const counterweightGroup = group(scene);
counterweightGroup.position.set(3, counterweightY, 0);
box(counterweightGroup, [1.62, 2.12, 1.28], [0, -1.15, 0.1], 0x7b4f38);
box(counterweightGroup, [1.78, 0.18, 1.4], [0, -0.14, 0.1], 0xb37b47);
box(counterweightGroup, [1.78, 0.18, 1.4], [0, -1.08, 0.1], 0xb37b47);
box(counterweightGroup, [1.78, 0.18, 1.4], [0, -2.08, 0.1], 0xb37b47);
box(counterweightGroup, [0.14, 1.85, 0.14], [0, -1.12, -0.85], 0x303936);
addSign(
counterweightGroup,
"560 kg",
"FIXED COUNTERWEIGHT",
[1.22, 0.43],
[0, -1.12, 0.755],
"#2a302d",
"#efd08a"
);
// Top rope runs and pulleys.
box(scene, [6.18, 0.055, 0.055], [0, PULLEY_TOP, -0.85], 0x202725);
const pulleyLeft = group(scene);
const pulleyRight = group(scene);
pulleyLeft.position.set(-3, PULLEY_TOP, -0.85);
pulleyRight.position.set(3, PULLEY_TOP, -0.85);
function makePulley(parent) {
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.43, 0.22, 12),
standardMaterial(0x9a7748)
);
wheel.rotation.x = Math.PI / 2;
wheel.castShadow = true;
parent.add(wheel);
for (let i = 0; i < 3; i++) {
const spoke = box(
parent,
[0.78, 0.055, 0.055],
[0, 0, 0.125],
0x393e39
);
spoke.rotation.z = (Math.PI / 3) * i;
}
box(parent, [0.12, 0.12, 0.32], [0, 0, 0], 0x353d38);
}
makePulley(pulleyLeft);
makePulley(pulleyRight);
// Dynamic vertical rope sections.
const platformRope = box(
scene,
[0.055, 1, 0.055],
[-3, 7, -0.85],
0x202725
);
const counterweightRope = box(
scene,
[0.055, 1, 0.055],
[3, 7, -0.85],
0x202725
);
addSign(
scene,
"LIFT 07",
"1:1 REEVING / GUIDE LOCK",
[2.65, 0.9],
[-4.85, 7.6, -0.56],
"#25312e",
"#f1d083"
);
// Hazard markings around the platform.
for (let i = 0; i < 9; i++) {
for (const z of [-1.35, 1.35]) {
const stripe = box(
scene,
[0.34, 0.035, 0.22],
[-4.25 + i * 0.31, 0.085, z],
i % 2 ? 0x252d2b : 0xdca43b,
false
);
stripe.rotation.y = 0.38;
}
}
// -------------------------------------------------------------------------
// Selectable cargo
// -------------------------------------------------------------------------
const cargoDefinitions = {
brass: {
id: "brass",
name: "Brass relay",
mass: 80,
color: 0xb96b3d,
accent: 0xe0a34d,
home: new THREE.Vector3(6.25, 0.58, 3.85)
},
sapphire: {
id: "sapphire",
name: "Sapphire glass",
mass: 140,
color: 0x3d8583,
accent: 0x72d1c3,
home: new THREE.Vector3(7.72, 0.58, 3.85)
},
iron: {
id: "iron",
name: "Blue iron",
mass: 260,
color: 0x4b617e,
accent: 0x83a9d5,
home: new THREE.Vector3(9.18, 0.58, 3.85)
}
};
const cargoObjects = new Map();
const pickTargets = [];
function makeCargoCrate(definition) {
const crate = group(scene);
crate.position.copy(definition.home);
box(crate, [1.15, 0.76, 0.9], [0, 0, 0], definition.color);
box(crate, [1.24, 0.13, 0.98], [0, 0.38, 0], definition.accent);
box(crate, [1.24, 0.13, 0.98], [0, -0.38, 0], definition.accent);
for (const x of [-0.49, 0.49]) {
for (const z of [-0.46, 0.46]) {
box(crate, [0.11, 0.85, 0.11], [x, 0, z], definition.accent);
}
}
box(crate, [0.18, 0.18, 0.18], [0, 0.55, 0], 0xe4b24d);
addCrateLabel(crate, `${definition.mass} kg`, `#${definition.accent.toString(16)}`);
crate.userData.homePosition = definition.home.clone();
crate.userData.cargoId = definition.id;
crate.traverse((object) => {
if (object.isMesh) {
object.userData.cargoId = definition.id;
pickTargets.push(object);
}
});
cargoObjects.set(definition.id, crate);
return crate;
}
Object.values(cargoDefinitions).forEach(makeCargoCrate);
// Pallet under each cargo position.
for (const x of [6.25, 7.72, 9.18]) {
box(scene, [1.38, 0.12, 1.28], [x, 0.08, 3.85], 0x71533d, false);
box(scene, [1.18, 0.13, 0.18], [x, 0.2, 3.58], 0x8b6849, false);
box(scene, [1.18, 0.13, 0.18], [x, 0.2, 4.12], 0x8b6849, false);
}
box(scene, [4.65, 0.16, 0.18], [7.72, 1.8, 2.82], 0x39443f);
box(scene, [0.16, 1.6, 0.18], [5.48, 1.0, 2.82], 0x39443f);
box(scene, [0.16, 1.6, 0.18], [9.96, 1.0, 2.82], 0x39443f);
addSign(
scene,
"CARGO BAY",
"SELECT A FREIGHT CRATE",
[4.25, 0.85],
[7.72, 1.78, 2.92],
"#27322f",
"#eed083"
);
// -------------------------------------------------------------------------
// Balance model and UI
// -------------------------------------------------------------------------
const EMPTY_PLATFORM_MASS = 420;
const COUNTERWEIGHT_MASS = 560;
const BALANCE_DEADBAND = 15;
let loadedCargoId = null;
let balance = COUNTERWEIGHT_MASS - EMPTY_PLATFORM_MASS;
const liftMassElement = document.querySelector("#liftMass");
const balanceValueElement = document.querySelector("#balanceValue");
const balanceMarker = document.querySelector("#balanceMarker");
const statusElement = document.querySelector("#status");
const statusTextElement = document.querySelector("#statusText");
const brakeButton = document.querySelector("#brakeButton");
const brakeLabel = document.querySelector("#brakeLabel");
const cargoButtons = [...document.querySelectorAll(".cargo-card")];
function currentCargoMass() {
return loadedCargoId
? cargoDefinitions[loadedCargoId].mass
: 0;
}
function updateBalance() {
const liftMass = EMPTY_PLATFORM_MASS + currentCargoMass();
balance = COUNTERWEIGHT_MASS - liftMass;
liftMassElement.textContent = `${liftMass} kg`;
balanceValueElement.textContent = `${balance > 0 ? "+" : ""}${balance} kg`;
const normalized = THREE.MathUtils.clamp(balance / 280, -1, 1);
balanceMarker.style.left = `${50 + normalized * 50}%`;
cargoButtons.forEach((button) => {
const id = button.dataset.cargo || null;
button.classList.toggle("active", id === loadedCargoId);
});
}
function setBrake(engaged) {
brakeEngaged = engaged;
brakeLabel.textContent = engaged ? "Release brake" : "Apply brake";
brakeButton.classList.toggle("released", !engaged);
}
function showToast(message) {
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(showToast.timeout);
showToast.timeout = setTimeout(() => {
toast.classList.remove("visible");
}, 2300);
}
function unloadCargo(showMessage = true) {
if (!loadedCargoId) return;
const id = loadedCargoId;
const crate = cargoObjects.get(id);
platformGroup.remove(crate);
scene.add(crate);
crate.position.copy(crate.userData.homePosition);
crate.rotation.set(0, 0, 0);
loadedCargoId = null;
updateBalance();
if (showMessage) {
showToast(`${cargoDefinitions[id].name} returned to the cargo bay.`);
}
}
function loadCargo(id) {
if (!cargoDefinitions[id]) return;
if (loadedCargoId === id) {
unloadCargo();
return;
}
unloadCargo(false);
const crate = cargoObjects.get(id);
platformGroup.add(crate);
crate.position.set(0, 0.53, 0);
crate.rotation.set(0, 0, 0);
loadedCargoId = id;
updateBalance();
const outcome =
balance > BALANCE_DEADBAND
? "The counterweight is heavier, so the lift will rise."
: balance < -BALANCE_DEADBAND
? "The load side is heavier, so the lift will lower."
: "The load is balanced, so the lift will hold position.";
showToast(
`${cargoDefinitions[id].name} loaded. ${outcome} ` +
(brakeEngaged ? "Release the brake to act." : "")
);
}
cargoButtons.forEach((button) => {
button.addEventListener("click", () => {
const id = button.dataset.cargo || null;
if (!id) unloadCargo();
else loadCargo(id);
});
});
brakeButton.addEventListener("click", () => {
setBrake(!brakeEngaged);
showToast(brakeEngaged ? "Brake released." : "Brake engaged.");
});
document.querySelector("#centerButton").addEventListener("click", () => {
platformY = 4.7;
counterweightY = LINK_SUM - platformY;
setBrake(true);
showToast("Lift recentered and brake engaged.");
});
updateBalance();
setBrake(true);
// -------------------------------------------------------------------------
// Pointer picking and direct camera controls
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickCargo(event) {
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;
scene.updateMatrixWorld(true);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickTargets, false);
return hits.length ? hits[0].object.userData.cargoId : null;
}
function updateTooltip(event, cargoId) {
if (!cargoId) {
tooltip.classList.remove("visible");
renderer.domElement.style.cursor = cameraState.pointerDown
? "grabbing"
: "grab";
return;
}
const definition = cargoDefinitions[cargoId];
const action = loadedCargoId === cargoId ? "Unload" : "Load";
tooltip.textContent = `${action} ${definition.name} · ${definition.mass} kg`;
tooltip.style.transform = `translate(${event.clientX + 14}px, ${event.clientY + 14}px)`;
tooltip.classList.add("visible");
renderer.domElement.style.cursor = "pointer";
}
renderer.domElement.addEventListener("pointerdown", (event) => {
cameraState.pointerDown = true;
cameraState.dragging = false;
cameraState.moved = false;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
tooltip.classList.remove("visible");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (cameraState.pointerDown) {
const dx = event.clientX - cameraState.lastX;
const dy = event.clientY - cameraState.lastY;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
if (
!cameraState.dragging &&
Math.hypot(
event.clientX - cameraState.lastX,
event.clientY - cameraState.lastY
) > 0
) {
// The distance test below is performed against total movement.
}
if (
Math.abs(dx) + Math.abs(dy) > 0 ||
cameraState.dragging
) {
cameraState.dragging = true;
cameraState.moved = true;
}
if (cameraState.dragging) {
cameraState.theta = THREE.MathUtils.clamp(
cameraState.theta - dx * 0.006,
-0.35,
1.28
);
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.5,
1.28
);
updateCamera();
renderer.domElement.style.cursor = "grabbing";
}
} else {
const cargoId = pickCargo(event);
updateTooltip(event, cargoId);
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!cameraState.moved) {
const cargoId = pickCargo(event);
if (cargoId) {
if (loadedCargoId === cargoId) unloadCargo();
else loadCargo(cargoId);
}
}
cameraState.pointerDown = false;
cameraState.dragging = false;
renderer.domElement.style.cursor = "grab";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
cameraState.pointerDown = false;
cameraState.dragging = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!cameraState.pointerDown) {
tooltip.classList.remove("visible");
}
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
10.5,
29
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.style.cursor = "grab";
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
setBrake(!brakeEngaged);
} else if (event.key.toLowerCase() === "c") {
platformY = 4.7;
counterweightY = LINK_SUM - platformY;
setBrake(true);
showToast("Lift recentered and brake engaged.");
} else if (event.key === "1") {
loadCargo("brass");
} else if (event.key === "2") {
loadCargo("sapphire");
} else if (event.key === "3") {
loadCargo("iron");
} else if (event.key === "Escape") {
unloadCargo();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function updateRopes() {
const platformRopeLength = Math.max(0.02, PULLEY_TOP - platformY);
const counterweightRopeLength = Math.max(0.02, PULLEY_TOP - counterweightY);
platformRope.scale.y = platformRopeLength;
platformRope.position.y = platformY + platformRopeLength / 2;
counterweightRope.scale.y = counterweightRopeLength;
counterweightRope.position.y =
counterweightY + counterweightRopeLength / 2;
}
function updateStatus() {
let state = "brake";
let text = "Brake engaged";
if (!brakeEngaged) {
if (Math.abs(balance) <= BALANCE_DEADBAND) {
state = "hold";
text = "Balanced · holding";
} else if (atTravelLimit) {
state = "limit";
text = "Travel limit reached";
} else if (balance > 0) {
state = "rise";
text = "Counterweight · rising";
} else {
state = "fall";
text = "Load side · lowering";
}
}
statusElement.dataset.state = state;
statusTextElement.textContent = text;
}
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(0.05, (now - previousTime) / 1000);
previousTime = now;
atTravelLimit = false;
if (!brakeEngaged && Math.abs(balance) > BALANCE_DEADBAND) {
const direction = balance > 0 ? 1 : -1;
platformY += direction * 0.9 * dt;
if (platformY >= PLATFORM_MAX) {
platformY = PLATFORM_MAX;
atTravelLimit = balance > 0;
} else if (platformY <= PLATFORM_MIN) {
platformY = PLATFORM_MIN;
atTravelLimit = balance < 0;
}
}
// The carriage is constrained to its vertical guide.
platformGroup.position.y = platformY;
// Exact idealized 1:1 counterweight constraint.
counterweightY = LINK_SUM - platformY;
counterweightGroup.position.y = counterweightY;
updateRopes();
pulleyLeft.rotation.z = -(platformY - 4.7) * 0.8;
pulleyRight.rotation.z = (platformY - 4.7) * 0.8;
furnaceGlowMaterial.emissiveIntensity =
2.1 + Math.sin(now * 0.0035) * 0.35;
liftLight.intensity = 17 + Math.sin(now * 0.002) * 1.4;
updateStatus();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
});
</script>
</body>
</html>
```