Prompt specification
Build or customize the Three.js 3D scene "Passenger Loading Changes a Toy Balance" 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: Passenger Loading Changes a Toy Balance
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/passenger-loading-changes-a-toy-balance-x2100
Scene Description Prompt:
Please make a voxel Three.js scene in one standalone HTML file using cdnjs. Create a simplified funicular model with adjustable cabin loads. Compare ideal balance forces while keeping the kinematic coupling unchanged and avoiding real operating guidance.
Working Three.js Starter (passenger-loading-changes-a-toy-balance-x2100):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Funicular Balance Lab</title>
<style>
:root {
--ink: #17212b;
--muted: #697583;
--panel: rgba(250, 251, 252, 0.94);
--line: #dce2e7;
--yellow: #f4b942;
--cyan: #39a9bd;
--green: #2d9b74;
--orange: #e47b45;
--shadow: 0 18px 60px rgba(28, 43, 55, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8edf0;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 28px;
max-width: calc(100vw - 430px);
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 9px;
padding: 7px 11px;
border: 1px solid rgba(23, 33, 43, 0.12);
border-radius: 999px;
background: rgba(255, 255, 255, 0.76);
color: #44515d;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--yellow);
box-shadow: 10px 0 0 var(--cyan);
margin-right: 10px;
}
h1 {
margin: 13px 0 7px;
max-width: 660px;
font-size: clamp(28px, 4vw, 54px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.subtitle {
max-width: 520px;
margin: 0;
color: #56636f;
font-size: 14px;
line-height: 1.55;
}
.scene-note {
position: absolute;
left: 28px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100vw - 430px);
}
.scene-note span {
padding: 7px 10px;
border: 1px solid rgba(23, 33, 43, 0.1);
border-radius: 8px;
background: rgba(255, 255, 255, 0.75);
color: #586571;
font-size: 11px;
backdrop-filter: blur(10px);
}
.panel {
position: absolute;
top: 20px;
right: 20px;
bottom: 20px;
width: 372px;
padding: 18px;
overflow: auto;
border: 1px solid rgba(255, 255, 255, 0.85);
border-radius: 22px;
background: var(--panel);
box-shadow: var(--shadow);
pointer-events: auto;
backdrop-filter: blur(18px);
scrollbar-width: thin;
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
padding-bottom: 15px;
border-bottom: 1px solid var(--line);
}
.panel-kicker {
margin: 0 0 4px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.panel h2 {
margin: 0;
font-size: 20px;
letter-spacing: -0.035em;
}
.lock-badge {
flex: none;
padding: 7px 9px;
border-radius: 8px;
background: #e6f4ee;
color: #26765d;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section {
padding: 16px 0;
border-bottom: 1px solid var(--line);
}
.section:last-child {
border-bottom: 0;
padding-bottom: 0;
}
.section-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 11px;
color: #4f5c68;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.load-card {
margin-top: 10px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 13px;
background: #fff;
}
.load-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
.load-name {
display: flex;
align-items: center;
gap: 9px;
font-size: 13px;
font-weight: 750;
}
.swatch {
width: 11px;
height: 11px;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.load-value {
font-variant-numeric: tabular-nums;
font-size: 13px;
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 5px 0 3px;
border-radius: 999px;
background: #e6eaee;
accent-color: var(--yellow);
cursor: pointer;
}
#loadB {
accent-color: var(--cyan);
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #8a949e;
font-size: 9px;
}
.button-row {
display: flex;
gap: 8px;
margin-top: 11px;
}
button {
min-height: 36px;
padding: 0 12px;
border: 1px solid #d8dee4;
border-radius: 9px;
background: #fff;
color: #35424e;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
border-color 120ms ease,
background 120ms ease;
}
button:hover {
border-color: #aeb8c1;
background: #f7f9fa;
transform: translateY(-1px);
}
button.primary {
border-color: #17212b;
background: #17212b;
color: #fff;
}
button.primary:hover {
background: #273541;
}
.toggles {
display: grid;
gap: 9px;
margin-top: 12px;
}
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
color: #52606c;
font-size: 12px;
}
.toggle input {
width: 17px;
height: 17px;
accent-color: #263541;
}
.force-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.metric {
min-width: 0;
padding: 11px;
border: 1px solid var(--line);
border-radius: 12px;
background: #fff;
}
.metric-label {
margin-bottom: 5px;
color: #78838d;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.metric-value {
font-size: 20px;
font-weight: 850;
font-variant-numeric: tabular-nums;
letter-spacing: -0.04em;
}
.metric-unit {
margin-left: 3px;
color: #7b8791;
font-size: 10px;
font-weight: 700;
letter-spacing: 0;
}
.bar-track {
height: 6px;
margin-top: 8px;
overflow: hidden;
border-radius: 999px;
background: #e9edf0;
}
.bar-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--yellow);
transition: width 160ms ease;
}
#barB {
background: var(--cyan);
}
.status {
margin-top: 10px;
padding: 11px 12px;
border-radius: 11px;
background: #e8f4ef;
color: #26765d;
font-size: 12px;
font-weight: 800;
}
.status[data-state="offset"] {
background: #fff0e7;
color: #a6532e;
}
.equation {
margin-top: 10px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: #f5f7f8;
color: #46535f;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
line-height: 1.75;
}
.equation strong {
color: #1e2b35;
}
.coupling {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
.coupling > div {
padding: 10px;
border-radius: 10px;
background: #edf2f4;
}
.coupling span {
display: block;
margin-bottom: 4px;
color: #7a8690;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.coupling strong {
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.note {
margin: 0;
color: #78838d;
font-size: 10px;
line-height: 1.55;
}
.disclaimer {
padding: 11px 12px;
border-left: 3px solid var(--orange);
border-radius: 7px;
background: #fff5ef;
color: #75513f;
font-size: 10px;
line-height: 1.55;
}
.label {
position: absolute;
min-width: 112px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 10px;
background: rgba(255, 255, 255, 0.92);
box-shadow: 0 8px 24px rgba(28, 43, 55, 0.16);
transform: translate(-50%, -100%);
text-align: center;
backdrop-filter: blur(10px);
}
.label::after {
content: "";
position: absolute;
left: 50%;
bottom: -6px;
width: 10px;
height: 10px;
border-right: 1px solid rgba(255, 255, 255, 0.8);
border-bottom: 1px solid rgba(255, 255, 255, 0.8);
background: rgba(255, 255, 255, 0.92);
transform: translateX(-50%) rotate(45deg);
}
.label-name {
font-size: 10px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.label-force {
margin-top: 2px;
color: #63707c;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
#labelA {
border-top: 3px solid var(--yellow);
}
#labelB {
border-top: 3px solid var(--cyan);
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: #e8edf0;
color: #53616d;
font-size: 13px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 800px) {
.brand {
top: 14px;
left: 14px;
max-width: calc(100vw - 28px);
}
h1 {
max-width: 390px;
font-size: 31px;
}
.subtitle {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 54vh;
padding: 15px;
border-radius: 18px;
}
.scene-note {
top: 112px;
bottom: auto;
left: 14px;
max-width: calc(100vw - 28px);
}
.scene-note span:last-child {
display: none;
}
}
</style>
</head>
<body>
<div id="loading">Loading voxel model…</div>
<div id="scene" aria-label="Interactive voxel funicular balance model"></div>
<div class="hud">
<header class="brand">
<div class="eyebrow">Balance Lab / Funicular Study</div>
<h1>Same motion.<br>Different forces.</h1>
<p class="subtitle">
Adjust the two cabin loads to compare ideal, opposing slope-force
contributions while the cable geometry and kinematic coupling stay fixed.
</p>
</header>
<div class="scene-note">
<span>Drag to orbit</span>
<span>Scroll to zoom</span>
<span>Double-click to reset the view</span>
</div>
<div id="labelA" class="label">
<div class="label-name">Cabin A</div>
<div id="labelForceA" class="label-force">0.00 kN</div>
</div>
<div id="labelB" class="label">
<div class="label-name">Cabin B</div>
<div id="labelForceB" class="label-force">0.00 kN</div>
</div>
</div>
<aside class="panel">
<div class="panel-head">
<div>
<p class="panel-kicker">Interactive comparison</p>
<h2>Cabin load model</h2>
</div>
<div class="lock-badge">Kinematics locked</div>
</div>
<section class="section">
<div class="section-title">
<span>01 / Payload inputs</span>
<span>Hypothetical</span>
</div>
<div class="load-card">
<div class="load-row">
<label class="load-name" for="loadA">
<span class="swatch" style="background:#f4b942"></span>
Cabin A load
</label>
<output id="valueA" class="load-value" for="loadA">900 kg</output>
</div>
<input
id="loadA"
type="range"
min="0"
max="1500"
step="50"
value="900"
aria-label="Cabin A aggregate load"
>
<div class="range-ends">
<span>0 kg</span>
<span>1500 kg</span>
</div>
</div>
<div class="load-card">
<div class="load-row">
<label class="load-name" for="loadB">
<span class="swatch" style="background:#39a9bd"></span>
Cabin B load
</label>
<output id="valueB" class="load-value" for="loadB">750 kg</output>
</div>
<input
id="loadB"
type="range"
min="0"
max="1500"
step="50"
value="750"
aria-label="Cabin B aggregate load"
>
<div class="range-ends">
<span>0 kg</span>
<span>1500 kg</span>
</div>
</div>
<div class="button-row">
<button id="equalize" class="primary" type="button">Set B equal to A</button>
<button id="resetLoads" type="button">Reset loads</button>
</div>
<div class="toggles">
<label class="toggle" for="showForces">
<span>Show ideal force vectors</span>
<input id="showForces" type="checkbox" checked>
</label>
<label class="toggle" for="pauseMotion">
<span>Pause coupled travel</span>
<input id="pauseMotion" type="checkbox">
</label>
</div>
</section>
<section class="section">
<div class="section-title">
<span>02 / Ideal force comparison</span>
<span>Opposing convention</span>
</div>
<div class="force-grid">
<div class="metric">
<div class="metric-label">Cabin A · F*</div>
<div>
<span id="forceA" class="metric-value">0.00</span>
<span class="metric-unit">kN</span>
</div>
<div class="bar-track">
<div id="barA" class="bar-fill"></div>
</div>
</div>
<div class="metric">
<div class="metric-label">Cabin B · F*</div>
<div>
<span id="forceB" class="metric-value">0.00</span>
<span class="metric-unit">kN</span>
</div>
<div class="bar-track">
<div id="barB" class="bar-fill"></div>
</div>
</div>
</div>
<div id="balanceState" class="status" data-state="balanced">
Ideal contributions are balanced
</div>
<div class="equation">
<div><strong>F* = m g sin(θ)</strong></div>
<div id="equationValue">θ = 15.64°</div>
<div>ΔF* = |F*A − F*B|</div>
<div>Residual difference: <strong id="residual">0.00 kN</strong></div>
</div>
</section>
<section class="section">
<div class="section-title">
<span>03 / Kinematic lock</span>
<span>Independent of load</span>
</div>
<div class="coupling">
<div>
<span>Travel parameter</span>
<strong id="travelValue">0%</strong>
</div>
<div>
<span>Position sum</span>
<strong id="positionSum">xA + xB = 0.0 m</strong>
</div>
</div>
<p class="note" style="margin-top:10px">
The cabins retain mirrored positions and the same track relationship.
Changing either load changes only the force comparison and the visible
cargo blocks—not the cable path, slope, speed, or coupling.
</p>
</section>
<section class="section">
<div class="disclaimer">
<strong>Illustrative model only.</strong>
This sandbox omits friction, drive dynamics, cable stretch, structural
effects, and many real operating factors. It is not suitable for
equipment sizing, dispatch, maintenance, or operating decisions.
</div>
<div class="button-row">
<button id="resetView" type="button">Reset camera</button>
</div>
</section>
</aside>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene setup
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8edf0);
scene.fog = new THREE.Fog(0xe8edf0, 35, 65);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.querySelector("#scene").appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xf7fbff, 0x9aa39d, 2.25));
const sun = new THREE.DirectionalLight(0xfff4dc, 3.1);
sun.position.set(10, 19, 14);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9bd8ef, 0.7);
fill.position.set(-15, 10, -12);
scene.add(fill);
// ------------------------------------------------------------
// Shared voxel utilities
// ------------------------------------------------------------
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.83,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function voxel(
parent,
sx,
sy,
sz,
color,
x = 0,
y = 0,
z = 0,
rz = 0
) {
const mesh = new THREE.Mesh(cubeGeometry, material(color));
mesh.scale.set(sx, sy, sz);
mesh.position.set(x, y, z);
mesh.rotation.z = rz;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beam(parent, from, to, width, color) {
const direction = new THREE.Vector3().subVectors(to, from);
const midpoint = new THREE.Vector3()
.addVectors(from, to)
.multiplyScalar(0.5);
const mesh = new THREE.Mesh(cubeGeometry, material(color));
mesh.scale.set(width, direction.length(), width);
mesh.position.copy(midpoint);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function pseudoRandom(index) {
const value = Math.sin(index * 127.1 + 311.7) * 43758.5453123;
return value - Math.floor(value);
}
// ------------------------------------------------------------
// Funicular geometry constants
// ------------------------------------------------------------
const TRACK_MIN = -10;
const TRACK_MAX = 10;
const SLOPE = 0.28;
const SLOPE_ANGLE = Math.atan(SLOPE);
const SIN_THETA = SLOPE / Math.sqrt(1 + SLOPE * SLOPE);
const GRAVITY = 9.81;
const MAX_MASS = 1500;
const LANE_A = -1.45;
const LANE_B = 1.45;
function trackY(x) {
return 1.15 + (x - TRACK_MIN) * SLOPE;
}
const model = new THREE.Group();
scene.add(model);
// ------------------------------------------------------------
// Voxel terrain
// ------------------------------------------------------------
const terrain = new THREE.Group();
model.add(terrain);
voxel(terrain, 31, 0.65, 21, 0xc4cdd1, 0, -0.45, 0);
const groundColors = [
0xd7ddd9,
0xd1d9d5,
0xdae0dc,
0xcbd4d0,
0xe0e3df,
0xd2dad5
];
let tileIndex = 0;
for (let x = -15; x <= 15; x += 1.5) {
for (let z = -9; z <= 9; z += 1.5) {
const r = pseudoRandom(tileIndex++);
const height = 0.26 + r * 0.24;
const color = groundColors[Math.floor(r * groundColors.length)];
voxel(
terrain,
1.43,
height,
1.43,
color,
x,
-0.1 + height / 2,
z
);
}
}
// Small block-built trees around the perimeter.
const treePositions = [
[-13, -7], [-12, 7], [-9, 8], [13, -7],
[12.5, 7], [8.5, 8], [-4, -8], [5, -8]
];
treePositions.forEach(([x, z], index) => {
const h = 0.75 + pseudoRandom(index + 80) * 0.45;
voxel(terrain, 0.22, h, 0.22, 0x796b56, x, h / 2 + 0.1, z);
const leaf = index % 2 ? 0x7fa68e : 0x91ad82;
voxel(terrain, 0.8, 0.55, 0.8, leaf, x, h + 0.15, z);
voxel(terrain, 0.55, 0.45, 0.55, leaf, x, h + 0.58, z);
voxel(terrain, 0.35, 0.32, 0.35, leaf, x + 0.12, h + 0.88, z);
});
// ------------------------------------------------------------
// Inclined twin track
// ------------------------------------------------------------
const track = new THREE.Group();
model.add(track);
const laneZ = [LANE_A, LANE_B];
laneZ.forEach((z) => {
// Sleepers
for (let x = -9.75; x <= 9.76; x += 0.65) {
voxel(
track,
0.22,
0.13,
1.62,
0x68747b,
x,
trackY(x) + 0.02,
z,
SLOPE_ANGLE
);
}
// Twin rails, built from small repeated blocks.
for (let i = 0; i < 44; i++) {
const x = TRACK_MIN + i * 0.46;
const y = trackY(x);
[-0.49, 0.49].forEach((offset) => {
voxel(
track,
0.49,
0.09,
0.10,
0x39464f,
x,
y + 0.16,
z + offset,
SLOPE_ANGLE
);
});
// Dark central cable.
voxel(
track,
0.49,
0.045,
0.045,
0x25323a,
x,
y + 0.24,
z,
SLOPE_ANGLE
);
}
});
// Trestles and cross members.
[-8, -4, 0, 4, 8].forEach((x, index) => {
const y = trackY(x);
laneZ.forEach((z) => {
voxel(track, 0.22, y - 0.1, 0.22, 0x748087, x, (y - 0.1) / 2, z);
});
voxel(track, 0.28, 0.22, 3.8, 0x68747b, x, y - 0.08, 0);
if (index < 4) {
beam(
track,
new THREE.Vector3(x, 0.25, LANE_A),
new THREE.Vector3(x + 1.8, trackY(x + 1.8) - 0.1, LANE_A),
0.10,
0x879198
);
beam(
track,
new THREE.Vector3(x + 1.8, 0.25, LANE_B),
new THREE.Vector3(x, trackY(x) - 0.1, LANE_B),
0.10,
0x879198
);
}
});
// End platforms.
voxel(track, 3.4, 0.45, 7.2, 0xb4bdc1, -11.4, 0.72, 0);
voxel(track, 3.4, 0.45, 7.2, 0xb4bdc1, 11.4, 0.72, 0);
// ------------------------------------------------------------
// Block-built end pulleys
// ------------------------------------------------------------
function makePulley(x, z) {
const group = new THREE.Group();
group.position.set(x, trackY(x), z);
track.add(group);
const radius = 1.02;
for (let i = 0; i < 18; i++) {
const angle = (i / 18) * Math.PI * 2;
voxel(
group,
0.34,
0.34,
0.46,
i % 3 === 0 ? 0x46545d : 0x35434c,
Math.cos(angle) * radius,
Math.sin(angle) * radius,
0,
angle
);
}
voxel(group, 0.24, 0.24, 0.72, 0xf0b743, 0, 0, 0);
voxel(group, 0.10, 0.10, 1.45, 0x8c989e, 0, 0, 0);
return group;
}
[TRACK_MIN, TRACK_MAX].forEach((x) => {
laneZ.forEach((z) => makePulley(x, z));
voxel(
track,
0.16,
0.16,
3.6,
0x8b979d,
x,
trackY(x),
0
);
});
// ------------------------------------------------------------
// Cabins and visible payload blocks
// ------------------------------------------------------------
function makeCabin(color) {
const group = new THREE.Group();
model.add(group);
const cargo = [];
voxel(group, 2.25, 0.22, 1.48, 0x36444d, 0, 0.26, 0);
voxel(group, 1.92, 1.10, 1.24, color, 0, 0.92, 0);
voxel(group, 2.05, 0.18, 1.42, 0xf4f1e7, 0, 1.55, 0);
// Window panels on both sides.
voxel(group, 1.40, 0.43, 0.035, 0x385261, 0, 1.04, 0.64);
voxel(group, 1.40, 0.43, 0.035, 0x385261, 0, 1.04, -0.64);
// Window dividers.
[-0.44, 0, 0.44].forEach((x) => {
voxel(group, 0.07, 0.46, 0.055, 0xe4e9e7, x, 1.04, 0.67);
voxel(group, 0.07, 0.46, 0.055, 0xe4e9e7, x, 1.04, -0.67);
});
// End panels.
voxel(group, 0.045, 0.43, 0.88, 0x385261, 0.98, 1.04, 0);
voxel(group, 0.045, 0.43, 0.88, 0x385261, -0.98, 1.04, 0);
// Small roof marker.
voxel(group, 0.18, 0.18, 0.18, color, 0.62, 1.75, 0);
// Six payload blocks, progressively shown with the load.
const cargoColors = [0xd58b46, 0x8a6cb2, 0x4c9b76, 0xd0aa3e, 0x4c84b5, 0xb76864];
for (let i = 0; i < 6; i++) {
const col = i % 3;
const row = Math.floor(i / 3);
const block = voxel(
group,
0.38,
0.30,
0.38,
cargoColors[i],
-0.58 + col * 0.58,
1.79,
-0.29 + row * 0.58
);
cargo.push(block);
}
return { group, cargo };
}
const cabinA = makeCabin(0xf4b942);
const cabinB = makeCabin(0x39a9bd);
// ------------------------------------------------------------
// Force vectors
// ------------------------------------------------------------
const cosTheta = Math.cos(SLOPE_ANGLE);
const sinTheta = Math.sin(SLOPE_ANGLE);
const arrowA = new THREE.ArrowHelper(
new THREE.Vector3(-cosTheta, -sinTheta, 0),
new THREE.Vector3(),
2,
0xe0a128,
0.55,
0.34
);
const arrowB = new THREE.ArrowHelper(
new THREE.Vector3(cosTheta, sinTheta, 0),
new THREE.Vector3(),
2,
0x258fa5,
0.55,
0.34
);
function styleArrow(arrow) {
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.92;
arrow.line.material.depthTest = false;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.95;
arrow.cone.material.depthTest = false;
arrow.line.renderOrder = 8;
arrow.cone.renderOrder = 8;
}
styleArrow(arrowA);
styleArrow(arrowB);
scene.add(arrowA, arrowB);
// ------------------------------------------------------------
// Camera controls implemented directly
// ------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, 2.8, 0),
radius: 27,
theta: 0.68,
phi: 1.13
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.target.set(0, 2.8, 0);
orbit.radius = 27;
orbit.theta = 0.68;
orbit.phi = 1.13;
updateCamera();
}
const canvas = renderer.domElement;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.30, 1.46);
updateCamera();
});
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();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.018,
17,
43
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
updateCamera();
// ------------------------------------------------------------
// UI and model state
// ------------------------------------------------------------
const ui = {
loadA: document.querySelector("#loadA"),
loadB: document.querySelector("#loadB"),
valueA: document.querySelector("#valueA"),
valueB: document.querySelector("#valueB"),
forceA: document.querySelector("#forceA"),
forceB: document.querySelector("#forceB"),
labelForceA: document.querySelector("#labelForceA"),
labelForceB: document.querySelector("#labelForceB"),
barA: document.querySelector("#barA"),
barB: document.querySelector("#barB"),
residual: document.querySelector("#residual"),
balanceState: document.querySelector("#balanceState"),
showForces: document.querySelector("#showForces"),
pauseMotion: document.querySelector("#pauseMotion"),
travelValue: document.querySelector("#travelValue"),
positionSum: document.querySelector("#positionSum"),
equationValue: document.querySelector("#equationValue")
};
const state = {
massA: Number(ui.loadA.value),
massB: Number(ui.loadB.value),
showForces: true,
paused: false,
time: 0,
travel: 0,
xA: 0,
xB: 0
};
function idealForce(mass) {
return mass * GRAVITY * SIN_THETA;
}
function updateCargo(cabin, mass) {
const count = Math.min(6, Math.ceil(mass / 250));
cabin.cargo.forEach((block, index) => {
block.visible = index < count;
});
}
function updateControls() {
state.massA = Number(ui.loadA.value);
state.massB = Number(ui.loadB.value);
ui.valueA.textContent = `${state.massA.toLocaleString()} kg`;
ui.valueB.textContent = `${state.massB.toLocaleString()} kg`;
updateCargo(cabinA, state.massA);
updateCargo(cabinB, state.massB);
const forceA = idealForce(state.massA);
const forceB = idealForce(state.massB);
const residual = Math.abs(forceA - forceB);
ui.forceA.textContent = (forceA / 1000).toFixed(2);
ui.forceB.textContent = (forceB / 1000).toFixed(2);
ui.labelForceA.textContent = `${(forceA / 1000).toFixed(2)} kN ideal`;
ui.labelForceB.textContent = `${(forceB / 1000).toFixed(2)} kN ideal`;
ui.residual.textContent = `${(residual / 1000).toFixed(2)} kN`;
ui.barA.style.width = `${(state.massA / MAX_MASS) * 100}%`;
ui.barB.style.width = `${(state.massB / MAX_MASS) * 100}%`;
if (state.massA === state.massB) {
ui.balanceState.dataset.state = "balanced";
ui.balanceState.textContent = "Ideal contributions are balanced";
} else {
const leader = state.massA > state.massB ? "Cabin A" : "Cabin B";
ui.balanceState.dataset.state = "offset";
ui.balanceState.textContent =
`${leader} exceeds the other by ${(residual / 1000).toFixed(2)} kN`;
}
ui.equationValue.textContent =
`θ = ${(SLOPE_ANGLE * 180 / Math.PI).toFixed(2)}° · ` +
`sin(θ) = ${SIN_THETA.toFixed(3)}`;
}
ui.loadA.addEventListener("input", updateControls);
ui.loadB.addEventListener("input", updateControls);
ui.showForces.addEventListener("change", () => {
state.showForces = ui.showForces.checked;
});
ui.pauseMotion.addEventListener("change", () => {
state.paused = ui.pauseMotion.checked;
});
document.querySelector("#equalize").addEventListener("click", () => {
ui.loadB.value = ui.loadA.value;
updateControls();
});
document.querySelector("#resetLoads").addEventListener("click", () => {
ui.loadA.value = 900;
ui.loadB.value = 750;
updateControls();
});
document.querySelector("#resetView").addEventListener("click", resetCamera);
updateControls();
// ------------------------------------------------------------
// Kinematic update
// ------------------------------------------------------------
function setArrowLength(arrow, mass) {
const force = idealForce(mass);
const length = 0.8 + (mass / MAX_MASS) * 5.0;
arrow.setLength(length, 0.56, 0.36);
return force;
}
function updateKinematics(dt) {
if (!state.paused) {
state.time += dt;
}
// Mirrored travel is independent of mass and force state.
const travel = 0.5 - 0.46 * Math.cos(state.time * 0.38);
state.travel = travel;
state.xA = -8.7 + travel * 17.4;
state.xB = 8.7 - travel * 17.4;
cabinA.group.position.set(state.xA, trackY(state.xA), LANE_A);
cabinB.group.position.set(state.xB, trackY(state.xB), LANE_B);
cabinA.group.rotation.z = SLOPE_ANGLE;
cabinB.group.rotation.z = SLOPE_ANGLE;
arrowA.position.set(state.xA, trackY(state.xA) + 1.75, LANE_A - 0.75);
arrowB.position.set(state.xB, trackY(state.xB) + 1.75, LANE_B + 0.75);
setArrowLength(arrowA, state.massA);
setArrowLength(arrowB, state.massB);
arrowA.visible = state.showForces && state.massA > 0;
arrowB.visible = state.showForces && state.massB > 0;
ui.travelValue.textContent = `${Math.round(travel * 100)}%`;
ui.positionSum.textContent =
`xA + xB = ${(state.xA + state.xB).toFixed(1)} m`;
}
// ------------------------------------------------------------
// Projected cabin labels
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (visible) {
element.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
}
const labelA = document.querySelector("#labelA");
const labelB = document.querySelector("#labelB");
const labelPosition = new THREE.Vector3();
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(0.05, (now - previousTime) / 1000);
previousTime = now;
updateKinematics(dt);
scene.updateMatrixWorld(true);
labelPosition.set(0, 2.55, 0).applyMatrix4(cabinA.group.matrixWorld);
placeLabel(labelA, labelPosition);
labelPosition.set(0, 2.55, 0).applyMatrix4(cabinB.group.matrixWorld);
placeLabel(labelB, labelPosition);
renderer.render(scene, camera);
}
document.querySelector("#loading").remove();
requestAnimationFrame(animate);
</script>
</body>
</html>
```