Prompt specification
Build or customize the Three.js 3D scene "Unequal Arm Gate 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: Unequal Arm Gate Balance
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/unequal-arm-gate-balance-x3446
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a technical cutaway of a tabletop gate controlled by an unequal-arm balance. Put a pivoted beam above the gate and provide marked positions for small weights on its short and long arms. Label each weight's mass and each position's distance from the pivot. The gate unlocks when the opposing torques match within a stated tolerance, not simply when the two sides hold equal masses. Let the user drag weights between the marked positions and watch the beam lean toward the larger torque.
Working Three.js Starter (unequal-arm-gate-balance-x3446):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Torque-Locked Tabletop Gate</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(8, 20, 35, 0.90);
--border: rgba(125, 211, 252, 0.28);
--text: #d9edff;
--muted: #86a5bd;
--cyan: #38bdf8;
--amber: #fbbf24;
--green: #52f2a2;
--red: #ff647c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene,
#labels {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#labels {
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
pointer-events: none;
user-select: none;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.slot-label {
padding: 3px 5px;
border: 1px solid rgba(148, 163, 184, 0.22);
border-radius: 4px;
background: rgba(5, 13, 24, 0.88);
color: #c9d9e8;
font-size: 10px;
letter-spacing: 0.02em;
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.28);
}
.slot-label.short {
border-color: rgba(56, 189, 248, 0.52);
color: #9bdcff;
}
.slot-label.long {
border-color: rgba(251, 191, 36, 0.52);
color: #ffe39a;
}
.weight-label {
min-width: 38px;
padding: 3px 5px;
border: 1px solid currentColor;
border-radius: 999px;
background: rgba(5, 13, 24, 0.94);
text-align: center;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.03em;
box-shadow:
0 0 0 2px rgba(4, 10, 18, 0.72),
0 5px 15px rgba(0, 0, 0, 0.38);
}
.arm-label {
padding: 4px 7px;
border: 1px solid currentColor;
border-radius: 4px;
background: rgba(5, 13, 24, 0.84);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.10em;
}
.gate-label {
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 5px;
background: rgba(5, 13, 24, 0.92);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.08em;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.4);
}
#title {
position: fixed;
top: 22px;
left: 24px;
width: min(460px, calc(100vw - 48px));
pointer-events: none;
text-shadow: 0 3px 18px rgba(0, 0, 0, 0.72);
}
#title .eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
}
#title h1 {
margin: 0;
font-size: clamp(23px, 3vw, 38px);
line-height: 1.02;
letter-spacing: -0.035em;
}
#title p {
max-width: 440px;
margin: 10px 0 0;
color: #9fb8cd;
font-size: 13px;
line-height: 1.45;
}
#readout {
position: fixed;
top: 20px;
right: 20px;
width: min(370px, calc(100vw - 40px));
padding: 15px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
box-shadow:
0 20px 70px rgba(0, 0, 0, 0.42),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(14px);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
pointer-events: none;
}
.readout-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding-bottom: 10px;
border-bottom: 1px solid rgba(148, 163, 184, 0.16);
}
.readout-title {
color: #e8f5ff;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.10em;
}
#status-chip {
padding: 4px 7px;
border: 1px solid currentColor;
border-radius: 999px;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.07em;
white-space: nowrap;
}
.criterion {
margin: 11px 0 12px;
padding: 9px 10px;
border: 1px solid rgba(125, 211, 252, 0.18);
border-radius: 7px;
background: rgba(56, 189, 248, 0.055);
color: #b9d4e8;
font-size: 11px;
line-height: 1.45;
}
.criterion strong {
display: block;
margin-top: 4px;
color: #ecf8ff;
font-size: 13px;
}
.torque-row {
margin-top: 11px;
}
.torque-heading {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 5px;
font-size: 11px;
font-weight: 800;
}
.torque-heading .value {
color: #f4f9ff;
font-size: 12px;
}
.bar {
height: 5px;
overflow: hidden;
border-radius: 999px;
background: rgba(148, 163, 184, 0.12);
}
.bar > div {
width: 0;
height: 100%;
border-radius: inherit;
transition: width 100ms ease-out;
}
#short-bar {
background: linear-gradient(90deg, #0ea5e9, #67e8f9);
}
#long-bar {
background: linear-gradient(90deg, #d97706, #fde047);
}
.load-list {
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid rgba(148, 163, 184, 0.14);
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.load-list b {
color: #dcecf8;
}
#difference {
margin-top: 9px;
color: #b9cbd9;
font-size: 11px;
}
#footer {
position: fixed;
left: 22px;
right: 22px;
bottom: 18px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 16px;
pointer-events: none;
}
.instructions {
max-width: 620px;
padding: 10px 12px;
border: 1px solid rgba(148, 163, 184, 0.18);
border-radius: 8px;
background: rgba(5, 13, 24, 0.82);
color: #9fb7ca;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
line-height: 1.5;
backdrop-filter: blur(10px);
}
.instructions strong {
color: #e6f4ff;
}
#reset {
pointer-events: auto;
padding: 10px 14px;
border: 1px solid rgba(56, 189, 248, 0.42);
border-radius: 7px;
background: rgba(8, 25, 42, 0.94);
color: #bce9ff;
font: 800 11px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
cursor: pointer;
transition: 140ms ease;
}
#reset:hover {
border-color: #67e8f9;
background: rgba(14, 50, 75, 0.96);
transform: translateY(-1px);
}
#hint {
margin-top: 4px;
color: #6f8da3;
font-size: 10px;
}
@media (max-width: 760px) {
#title {
top: 14px;
left: 14px;
}
#title p {
display: none;
}
#readout {
top: auto;
right: 12px;
bottom: 78px;
width: min(330px, calc(100vw - 24px));
padding: 11px;
}
.load-list {
display: none;
}
#footer {
left: 12px;
right: 12px;
bottom: 12px;
}
.instructions {
max-width: calc(100vw - 100px);
font-size: 9px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels"></div>
<header id="title">
<div class="eyebrow">MECHANISM STUDY / CUTAWAY 01</div>
<h1>Torque-Locked Tabletop Gate</h1>
<p>
An unequal-arm balance controls the latch. Move the hanging masses until
the opposing torques—not the masses—match.
</p>
</header>
<aside id="readout" aria-live="polite">
<div class="readout-head">
<div class="readout-title">LIVE TORQUE MONITOR</div>
<div id="status-chip">LOCKED</div>
</div>
<div class="criterion">
Gate unlocks only when:
<strong>|τshort − τlong| ≤ 0.05 N·m</strong>
Equal total masses are not required.
</div>
<div class="torque-row">
<div class="torque-heading">
<span style="color: var(--cyan)">SHORT-ARM TORQUE</span>
<span class="value" id="short-torque">0.00 N·m</span>
</div>
<div class="bar"><div id="short-bar"></div></div>
</div>
<div class="torque-row">
<div class="torque-heading">
<span style="color: var(--amber)">LONG-ARM TORQUE</span>
<span class="value" id="long-torque">0.00 N·m</span>
</div>
<div class="bar"><div id="long-bar"></div></div>
</div>
<div class="load-list">
<div><b>Short arm:</b> <span id="short-load">—</span></div>
<div><b>Long arm:</b> <span id="long-load">—</span></div>
<div style="margin-top:5px">Each contribution: τ = m · g · r</div>
</div>
<div id="difference">Torque mismatch: 0.00 N·m</div>
</aside>
<footer id="footer">
<div class="instructions">
<strong>DRAG</strong> a colored mass to a marked arm position.
<strong>DRAG EMPTY SPACE</strong> to orbit.
<strong>SCROLL</strong> to zoom.
<div id="hint">Hint: moving the 250 g mass from 2.5 m to 2.0 m balances the mechanism.</div>
</div>
<button id="reset" type="button">RESET</button>
</footer>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Constants and DOM
// -------------------------------------------------------------------------
const G = 9.81;
const TOLERANCE = 0.05;
const SHORT_COLOR = 0x38bdf8;
const LONG_COLOR = 0xfbbf24;
const sceneHost = document.querySelector("#scene");
const labelHost = document.querySelector("#labels");
const statusChip = document.querySelector("#status-chip");
const shortTorqueEl = document.querySelector("#short-torque");
const longTorqueEl = document.querySelector("#long-torque");
const shortLoadEl = document.querySelector("#short-load");
const longLoadEl = document.querySelector("#long-load");
const differenceEl = document.querySelector("#difference");
const shortBar = document.querySelector("#short-bar");
const longBar = document.querySelector("#long-bar");
const resetButton = document.querySelector("#reset");
// -------------------------------------------------------------------------
// Renderer, scene, camera, lights
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 14, 25);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraState = {
target: new THREE.Vector3(0.65, 1.32, 0),
radius: 12.4,
theta: 0.265,
phi: 1.205
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xa8ddff, 0x07101c, 1.65);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xd7f1ff, 2.3);
key.position.set(4.5, 9, 7.5);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 7;
key.shadow.camera.bottom = -6;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 25;
scene.add(key);
const cyanLight = new THREE.PointLight(0x22d3ee, 12, 8, 2);
cyanLight.position.set(-3.5, 1.8, 3.3);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xf59e0b, 9, 7, 2);
amberLight.position.set(4.2, 3.5, 2.7);
scene.add(amberLight);
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.48,
metalness: 0.48,
...options
});
}
function addEdges(mesh, color = 0x8fb8d0, opacity = 0.48) {
const edges = new THREE.EdgesGeometry(mesh.geometry, 25);
const lines = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(lines);
return lines;
}
function makeBox(
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0,
parent = scene,
edgeColor = 0x8fb8d0,
edgeOpacity = 0.44
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (edgeOpacity > 0) {
addEdges(mesh, edgeColor, edgeOpacity);
}
return mesh;
}
function makeCylinder(
radiusTop,
radiusBottom,
height,
material,
x = 0,
y = 0,
z = 0,
parent = scene,
radialSegments = 24
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
radialSegments
),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeWorldLabel(text, className) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.textContent = text;
labelHost.appendChild(element);
return element;
}
// -------------------------------------------------------------------------
// Technical base with an open front section
// -------------------------------------------------------------------------
const baseMaterial = standardMaterial(0x15263a, {
roughness: 0.62,
metalness: 0.52
});
const innerMaterial = standardMaterial(0x0b1725, {
roughness: 0.72,
metalness: 0.3
});
const railMaterial = standardMaterial(0x27445d, {
roughness: 0.38,
metalness: 0.72
});
makeBox(9.4, 0.15, 3.05, innerMaterial, 0.35, -0.66, 0);
makeBox(2.9, 0.24, 3.05, baseMaterial, -3.05, -0.44, 0);
makeBox(2.05, 0.24, 3.05, baseMaterial, 3.48, -0.44, 0);
makeBox(3.45, 0.19, 0.48, baseMaterial, 0.28, -0.46, -1.26);
makeBox(1.05, 0.19, 0.42, baseMaterial, -2.0, -0.46, 1.27);
makeBox(2.05, 0.19, 0.42, baseMaterial, 3.48, -0.46, 1.27);
// Exposed internal rails and cross braces.
makeBox(3.1, 0.09, 0.12, railMaterial, 0.3, -0.48, 0.85);
makeBox(0.12, 0.09, 2.1, railMaterial, -1.25, -0.48, 0);
makeBox(0.12, 0.09, 2.1, railMaterial, 1.85, -0.48, 0);
const braceMaterial = standardMaterial(0x986a24, {
roughness: 0.48,
metalness: 0.66,
emissive: 0x3a2100,
emissiveIntensity: 0.25
});
const brace = makeBox(3.0, 0.08, 0.10, braceMaterial, 0.3, -0.48, 0.55);
brace.rotation.y = 0.21;
const grid = new THREE.GridHelper(18, 36, 0x2b5673, 0x163047);
grid.position.y = -0.755;
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
// -------------------------------------------------------------------------
// Gate frame and hinged gate
// -------------------------------------------------------------------------
const frameMaterial = standardMaterial(0x31526c, {
roughness: 0.36,
metalness: 0.78
});
const sectionMaterial = standardMaterial(0x38bdf8, {
roughness: 0.35,
metalness: 0.55,
transparent: true,
opacity: 0.30,
depthWrite: false
});
makeBox(0.18, 2.48, 0.30, frameMaterial, -1.47, 1.24, 0);
makeBox(0.18, 2.48, 0.30, sectionMaterial, 1.47, 1.24, 0);
makeBox(3.12, 0.18, 0.32, frameMaterial, 0, 2.48, 0);
makeBox(0.48, 0.18, 0.58, railMaterial, -1.47, 0.05, 0);
makeBox(0.48, 0.18, 0.58, railMaterial, 1.47, 0.05, 0);
// Section hatching on the exposed right post.
const hatchPoints = [];
for (let i = 0; i < 7; i++) {
const y = 0.25 + i * 0.32;
hatchPoints.push(
new THREE.Vector3(1.38, y, 0.165),
new THREE.Vector3(1.56, y + 0.19, 0.165)
);
}
const hatchGeometry = new THREE.BufferGeometry().setFromPoints(hatchPoints);
const hatch = new THREE.LineSegments(
hatchGeometry,
new THREE.LineBasicMaterial({
color: 0xfbbf24,
transparent: true,
opacity: 0.75
})
);
scene.add(hatch);
const internalRod = makeCylinder(
0.055,
0.055,
2.05,
braceMaterial,
1.47,
1.25,
0.18,
scene,
14
);
internalRod.castShadow = false;
const gateHinge = new THREE.Group();
gateHinge.position.set(0, 0.04, 0.13);
scene.add(gateHinge);
const gateGlassMaterial = standardMaterial(0x0ea5e9, {
roughness: 0.22,
metalness: 0.18,
transparent: true,
opacity: 0.22,
depthWrite: false
});
const gatePanel = makeBox(
2.58,
1.70,
0.11,
gateGlassMaterial,
0,
0.85,
0,
gateHinge,
0x67e8f9,
0.34
);
gatePanel.castShadow = false;
makeBox(0.10, 1.78, 0.16, frameMaterial, -1.30, 0.87, 0.04, gateHinge);
makeBox(0.10, 1.78, 0.16, frameMaterial, 1.30, 0.87, 0.04, gateHinge);
makeBox(2.68, 0.10, 0.16, frameMaterial, 0, 0.03, 0.04, gateHinge);
makeBox(2.68, 0.10, 0.16, frameMaterial, 0, 1.70, 0.04, gateHinge);
function makePanelBrace(x1, y1, x2, y2) {
const dx = x2 - x1;
const dy = y2 - y1;
const length = Math.hypot(dx, dy);
const braceMesh = makeBox(
length,
0.055,
0.075,
railMaterial,
(x1 + x2) / 2,
(y1 + y2) / 2,
0.07,
gateHinge,
0x8fb8d0,
0.25
);
braceMesh.rotation.z = Math.atan2(dy, dx);
}
makePanelBrace(-1.24, 0.12, 1.24, 1.62);
makePanelBrace(1.24, 0.12, -1.24, 1.62);
for (const x of [-1.1, 1.1]) {
makeCylinder(
0.10,
0.10,
0.34,
frameMaterial,
x,
0.02,
0.12,
scene,
18
);
}
// Sliding lock bar and status lamp.
const lockMaterial = standardMaterial(0xff5266, {
roughness: 0.30,
metalness: 0.65,
emissive: 0x5b0913,
emissiveIntensity: 0.55
});
const lockBar = makeBox(
0.95,
0.075,
0.13,
lockMaterial,
0,
1.76,
0.28,
scene,
0xffa0ad,
0.42
);
const ledMaterial = standardMaterial(0xff5266, {
roughness: 0.22,
metalness: 0.25,
emissive: 0xff1744,
emissiveIntensity: 2.2
});
const led = new THREE.Mesh(
new THREE.SphereGeometry(0.10, 20, 12),
ledMaterial
);
led.position.set(1.47, 2.25, 0.24);
scene.add(led);
// -------------------------------------------------------------------------
// Unequal-arm balance
// -------------------------------------------------------------------------
const beamGroup = new THREE.Group();
beamGroup.position.set(0, 3.26, 0.08);
scene.add(beamGroup);
const shortBeamMaterial = standardMaterial(SHORT_COLOR, {
roughness: 0.30,
metalness: 0.68,
emissive: 0x06364b,
emissiveIntensity: 0.22
});
const longBeamMaterial = standardMaterial(LONG_COLOR, {
roughness: 0.30,
metalness: 0.68,
emissive: 0x3a2500,
emissiveIntensity: 0.22
});
makeBox(
1.70,
0.16,
0.19,
shortBeamMaterial,
-0.85,
0,
0,
beamGroup,
0xa5e8ff,
0.70
);
makeBox(
4.35,
0.16,
0.19,
longBeamMaterial,
2.175,
0,
0,
beamGroup,
0xffe39a,
0.70
);
const axle = makeCylinder(
0.17,
0.17,
0.66,
frameMaterial,
0,
0,
0.03,
beamGroup,
30
);
axle.rotation.x = Math.PI / 2;
const fulcrum = new THREE.Mesh(
new THREE.ConeGeometry(0.52, 1.0, 3, 1),
standardMaterial(0x54728a, {
roughness: 0.35,
metalness: 0.78
})
);
fulcrum.position.set(0, 2.76, 0);
fulcrum.rotation.y = Math.PI / 6;
fulcrum.castShadow = true;
fulcrum.receiveShadow = true;
scene.add(fulcrum);
addEdges(fulcrum, 0xb9d4e6, 0.65);
makeBox(0.92, 0.13, 0.90, frameMaterial, 0, 2.23, 0);
// -------------------------------------------------------------------------
// Marked positions
// -------------------------------------------------------------------------
const slots = [];
function addSlot(id, side, radius) {
const localX = side === "short" ? -radius : radius;
const color = side === "short" ? SHORT_COLOR : LONG_COLOR;
const marker = new THREE.Group();
marker.position.x = localX;
beamGroup.add(marker);
const ringMaterial = standardMaterial(color, {
roughness: 0.28,
metalness: 0.45,
emissive: color,
emissiveIntensity: 0.30
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.105, 0.023, 8, 24),
ringMaterial
);
ring.position.set(0, 0.015, 0.125);
marker.add(ring);
makeBox(
0.045,
0.24,
0.05,
ringMaterial,
0,
0.02,
0.14,
marker,
color,
0.50
);
const slot = {
id,
side,
radius,
localX,
marker,
ringMaterial,
label: null
};
slot.label = makeWorldLabel(
`${side === "short" ? "S" : "L"} · ${radius.toFixed(1)} m`,
`slot-label ${side}`
);
slots.push(slot);
return slot;
}
addSlot("s0", "short", 0.5);
addSlot("s1", "short", 1.0);
addSlot("s2", "short", 1.5);
addSlot("l0", "long", 2.0);
addSlot("l1", "long", 2.5);
addSlot("l2", "long", 3.0);
addSlot("l3", "long", 3.5);
addSlot("l4", "long", 4.0);
const slotsById = new Map(slots.map((slot) => [slot.id, slot]));
const shortArmLabel = makeWorldLabel(
"SHORT ARM · 0.5–1.5 m",
"arm-label"
);
shortArmLabel.style.color = "#7dd3fc";
const longArmLabel = makeWorldLabel(
"LONG ARM · 2.0–4.0 m",
"arm-label"
);
longArmLabel.style.color = "#fde047";
const pivotLabel = makeWorldLabel("PIVOT", "arm-label");
pivotLabel.style.color = "#d9edff";
const gateLabel = makeWorldLabel("GATE · LOCKED", "gate-label");
gateLabel.style.color = "#ff8a9b";
// -------------------------------------------------------------------------
// Hanging masses
// -------------------------------------------------------------------------
const weightColors = [0x67e8f9, 0xa78bfa, 0xfbbf24, 0xfb7185];
const weights = [
{ mass: 0.100, slot: "s0", color: weightColors[0] },
{ mass: 0.150, slot: "s1", color: weightColors[1] },
{ mass: 0.200, slot: "s2", color: weightColors[2] },
{ mass: 0.250, slot: "l1", color: weightColors[3] }
];
const pickMeshes = [];
const ropeLines = [];
weights.forEach((weight, index) => {
const group = new THREE.Group();
scene.add(group);
const material = standardMaterial(weight.color, {
roughness: 0.27,
metalness: 0.58,
emissive: weight.color,
emissiveIntensity: 0.12
});
const body = makeCylinder(
0.21,
0.23,
0.30,
material,
0,
0,
0,
group,
28
);
addEdges(body, 0xffffff, 0.34);
body.userData.weightIndex = index;
pickMeshes.push(body);
const cap = makeCylinder(
0.14,
0.16,
0.065,
material,
0,
0.17,
0,
group,
24
);
cap.userData.weightIndex = index;
pickMeshes.push(cap);
const hook = new THREE.Mesh(
new THREE.TorusGeometry(0.075, 0.018, 8, 20, Math.PI * 1.45),
frameMaterial
);
hook.position.y = 0.28;
hook.rotation.z = Math.PI * 0.75;
group.add(hook);
const label = makeWorldLabel(
`${Math.round(weight.mass * 1000)} g`,
"weight-label"
);
label.style.color = `#${weight.color.toString(16).padStart(6, "0")}`;
const ropeGeometry = new THREE.BufferGeometry();
ropeGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([0, 0, 0, 0, 0, 0], 3)
);
const rope = new THREE.Line(
ropeGeometry,
new THREE.LineBasicMaterial({
color: 0xb5c9da,
transparent: true,
opacity: 0.88
})
);
scene.add(rope);
weight.group = group;
weight.label = label;
weight.rope = rope;
ropeLines.push(rope);
});
// -------------------------------------------------------------------------
// Physics / state
// -------------------------------------------------------------------------
let unlocked = false;
let beamTargetAngle = 0;
let shortTorque = 0;
let longTorque = 0;
let drag = null;
function slotIsFree(slotId, excludingIndex = -1) {
return !weights.some(
(weight, index) =>
index !== excludingIndex && weight.slot === slotId
);
}
function effectiveSlot(index) {
if (
drag &&
drag.index === index &&
drag.candidate &&
slotIsFree(drag.candidate.id, index)
) {
return drag.candidate;
}
return slotsById.get(weights[index].slot);
}
function calculateTorques() {
shortTorque = 0;
longTorque = 0;
const shortTerms = [];
const longTerms = [];
weights.forEach((weight, index) => {
const slot = effectiveSlot(index);
if (!slot) return;
const contribution = weight.mass * G * slot.radius;
if (slot.side === "short") {
shortTorque += contribution;
shortTerms.push(
`${Math.round(weight.mass * 1000)} g × ${slot.radius.toFixed(1)} m`
);
} else {
longTorque += contribution;
longTerms.push(
`${Math.round(weight.mass * 1000)} g × ${slot.radius.toFixed(1)} m`
);
}
});
const difference = shortTorque - longTorque;
unlocked = Math.abs(difference) <= TOLERANCE;
// Positive angle lifts the short arm and lowers the long arm.
// Therefore the larger-torque side visibly moves downward.
beamTargetAngle = THREE.MathUtils.clamp(
difference * 0.08,
-0.24,
0.24
);
updateReadout(shortTerms, longTerms);
updateIndicators();
}
function updateReadout(shortTerms, longTerms) {
shortTorqueEl.textContent = `${shortTorque.toFixed(2)} N·m`;
longTorqueEl.textContent = `${longTorque.toFixed(2)} N·m`;
shortLoadEl.textContent = shortTerms.length
? shortTerms.join(" + ")
: "none";
longLoadEl.textContent = longTerms.length
? longTerms.join(" + ")
: "none";
const difference = Math.abs(shortTorque - longTorque);
differenceEl.textContent =
`Torque mismatch: ${difference.toFixed(3)} N·m ` +
`/ tolerance ${TOLERANCE.toFixed(2)} N·m`;
const maxTorque = Math.max(shortTorque, longTorque, 1);
shortBar.style.width = `${(shortTorque / maxTorque) * 100}%`;
longBar.style.width = `${(longTorque / maxTorque) * 100}%`;
if (unlocked) {
statusChip.textContent = "UNLOCKED";
statusChip.style.color = "#52f2a2";
differenceEl.style.color = "#72f6b2";
} else {
statusChip.textContent = "LOCKED";
statusChip.style.color = "#ff647c";
differenceEl.style.color = "#dbe8f2";
}
}
function updateIndicators() {
const activeColor = unlocked ? 0x52f2a2 : 0xff5266;
const activeEmissive = unlocked ? 0x0b8f54 : 0x8c1026;
lockMaterial.color.setHex(activeColor);
lockMaterial.emissive.setHex(activeEmissive);
ledMaterial.color.setHex(activeColor);
ledMaterial.emissive.setHex(activeColor);
gateLabel.textContent = unlocked ? "GATE · UNLOCKED" : "GATE · LOCKED";
gateLabel.style.color = unlocked ? "#52f2a2" : "#ff8a9b";
for (const slot of slots) {
const occupied = weights.some((weight) => weight.slot === slot.id);
const highlighted =
drag &&
drag.candidate &&
drag.candidate.id === slot.id &&
slotIsFree(slot.id, drag.index);
slot.ringMaterial.emissiveIntensity = highlighted
? 1.45
: occupied
? 0.72
: 0.28;
}
}
// -------------------------------------------------------------------------
// Weight positioning and dragging
// -------------------------------------------------------------------------
function slotWorldPosition(slot) {
return beamGroup.localToWorld(
new THREE.Vector3(slot.localX, -0.08, 0.18)
);
}
function updateWeightVisuals() {
beamGroup.updateMatrixWorld(true);
weights.forEach((weight, index) => {
const isDragged = drag && drag.index === index;
if (isDragged) {
weight.group.position.copy(drag.bodyPosition);
const anchor = slotWorldPosition(
slotsById.get(weight.slot)
);
const positions = weight.rope.geometry.attributes.position;
positions.setXYZ(0, anchor.x, anchor.y, anchor.z);
positions.setXYZ(
1,
weight.group.position.x,
weight.group.position.y + 0.20,
weight.group.position.z
);
positions.needsUpdate = true;
weight.rope.geometry.computeBoundingSphere();
} else {
const slot = slotsById.get(weight.slot);
const anchor = slotWorldPosition(slot);
// Weights remain vertical under gravity while the beam rotates.
weight.group.position.set(
anchor.x,
anchor.y - 0.48,
anchor.z
);
const positions = weight.rope.geometry.attributes.position;
positions.setXYZ(0, anchor.x, anchor.y, anchor.z);
positions.setXYZ(
1,
weight.group.position.x,
weight.group.position.y + 0.20,
weight.group.position.z
);
positions.needsUpdate = true;
weight.rope.geometry.computeBoundingSphere();
}
weight.group.rotation.z = 0;
});
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function setPointer(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;
raycaster.setFromCamera(pointer, camera);
}
function nearestSlotForBody(bodyPosition, excludingIndex) {
let nearest = null;
let nearestDistance = Infinity;
for (const slot of slots) {
if (!slotIsFree(slot.id, excludingIndex)) continue;
const anchor = slotWorldPosition(slot);
const distance = anchor.distanceTo(bodyPosition);
if (distance < nearestDistance) {
nearestDistance = distance;
nearest = slot;
}
}
return nearestDistance < 0.64 ? nearest : null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
setPointer(event);
const hits = raycaster.intersectObjects(pickMeshes, false);
if (hits.length) {
const index = hits[0].object.userData.weightIndex;
const weight = weights[index];
const hitPoint = hits[0].point;
const planeNormal = camera.getWorldDirection(
new THREE.Vector3()
);
const dragPlane = new THREE.Plane().setFromNormalAndCoplanarPoint(
planeNormal,
hitPoint
);
drag = {
index,
pointerId: event.pointerId,
plane: dragPlane,
offset: hitPoint.clone().sub(weight.group.position),
bodyPosition: weight.group.position.clone(),
candidate: null
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
calculateTorques();
} else {
drag = {
orbiting: true,
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!drag) {
setPointer(event);
const hoverHits = raycaster.intersectObjects(pickMeshes, false);
renderer.domElement.style.cursor = hoverHits.length ? "grab" : "default";
return;
}
if (drag.orbiting) {
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.62,
1.46
);
drag.x = event.clientX;
drag.y = event.clientY;
updateCamera();
return;
}
if (event.pointerId !== drag.pointerId) return;
setPointer(event);
const point = new THREE.Vector3();
if (!raycaster.ray.intersectPlane(drag.plane, point)) return;
drag.bodyPosition.copy(point).add(drag.offset);
weights[drag.index].group.position.copy(drag.bodyPosition);
drag.candidate = nearestSlotForBody(
drag.bodyPosition,
drag.index
);
calculateTorques();
});
function endPointer(event) {
if (!drag) return;
if (!drag.orbiting && drag.candidate) {
const candidate = drag.candidate;
if (slotIsFree(candidate.id, drag.index)) {
weights[drag.index].slot = candidate.id;
}
}
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
drag = null;
renderer.domElement.style.cursor = "default";
calculateTorques();
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8.2,
19
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Label projection
// -------------------------------------------------------------------------
function projectLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
const projected = worldPosition.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x + offsetX}px`;
element.style.top = `${y + offsetY}px`;
element.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
}
function updateLabels() {
slots.forEach((slot, index) => {
const y = 0.30 + (index % 2) * 0.24;
const position = beamGroup.localToWorld(
new THREE.Vector3(slot.localX, y, 0.2)
);
projectLabel(slot.label, position);
});
const shortArmPosition = beamGroup.localToWorld(
new THREE.Vector3(-1.08, 0.82, 0.2)
);
const longArmPosition = beamGroup.localToWorld(
new THREE.Vector3(2.68, 0.84, 0.2)
);
const pivotPosition = beamGroup.localToWorld(
new THREE.Vector3(0, 0.43, 0.3)
);
projectLabel(shortArmLabel, shortArmPosition);
projectLabel(longArmLabel, longArmPosition);
projectLabel(pivotLabel, pivotPosition);
weights.forEach((weight, index) => {
const slot = effectiveSlot(index);
const offsetX = slot && slot.side === "short" ? -23 : 23;
projectLabel(
weight.label,
weight.group.position,
offsetX,
-2
);
});
projectLabel(
gateLabel,
new THREE.Vector3(1.67, 1.92, 0.35),
45,
-4
);
}
// -------------------------------------------------------------------------
// Reset and resize
// -------------------------------------------------------------------------
function resetExperiment() {
drag = null;
weights[0].slot = "s0";
weights[1].slot = "s1";
weights[2].slot = "s2";
weights[3].slot = "l1";
beamGroup.rotation.z = 0;
gateHinge.rotation.x = 0;
lockBar.position.x = 0;
calculateTorques();
updateWeightVisuals();
}
resetButton.addEventListener("click", resetExperiment);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
beamGroup.rotation.z = THREE.MathUtils.damp(
beamGroup.rotation.z,
beamTargetAngle,
6.0,
dt
);
const targetGateAngle = unlocked ? 1.03 : 0;
gateHinge.rotation.x = THREE.MathUtils.damp(
gateHinge.rotation.x,
targetGateAngle,
5.2,
dt
);
lockBar.position.x = THREE.MathUtils.damp(
lockBar.position.x,
unlocked ? 0.56 : 0,
7.0,
dt
);
updateWeightVisuals();
scene.updateMatrixWorld(true);
updateLabels();
renderer.render(scene, camera);
}
calculateTorques();
updateWeightVisuals();
animate();
</script>
</body>
</html>
```