Prompt specification
Build or customize the Three.js 3D scene "Balance Platform With Mass Slots" 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: Balance Platform With Mass Slots
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/balance-platform-with-mass-slots-x3445
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts counterweights, lever arms, and balanced loads. Build a lever platform with labeled masses and equal-distance slots on either side. Show the pivot and use torque rather than object count to determine tilt. Low-poly.
Working Three.js Starter (balance-platform-with-mass-slots-x3445):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Counterweight Lever Lab</title>
<style>
:root {
--ink: #17313d;
--muted: #617781;
--panel: rgba(247, 251, 250, 0.88);
--border: rgba(30, 67, 79, 0.14);
--left: #21a9b6;
--right: #ec765e;
--pivot: #e7ad38;
--balanced: #259a72;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 20px;
border: 1px solid var(--border);
border-radius: 20px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(35, 68, 78, 0.14),
inset 0 1px 0 rgba(255, 255, 255, 0.8);
backdrop-filter: blur(18px) saturate(125%);
-webkit-backdrop-filter: blur(18px) saturate(125%);
user-select: none;
}
.eyebrow {
margin-bottom: 7px;
color: #62808a;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.intro {
margin: 10px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
padding: 5px;
border: 1px solid var(--border);
border-radius: 13px;
background: rgba(28, 64, 76, 0.055);
}
button {
border: 0;
color: inherit;
font: inherit;
cursor: pointer;
}
.preset {
padding: 9px 5px;
border-radius: 9px;
background: transparent;
color: #607780;
font-size: 11px;
font-weight: 750;
transition: 160ms ease;
}
.preset:hover {
color: var(--ink);
background: rgba(255, 255, 255, 0.55);
}
.preset.active {
color: #17313d;
background: #fff;
box-shadow: 0 2px 8px rgba(38, 69, 79, 0.12);
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
margin: 15px 0 11px;
padding: 6px 10px;
border-radius: 999px;
background: rgba(37, 154, 114, 0.12);
color: var(--balanced);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.status::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
content: "";
}
.status.left {
color: var(--left);
background: rgba(33, 169, 182, 0.12);
}
.status.right {
color: var(--right);
background: rgba(236, 118, 94, 0.12);
}
.equation {
display: flex;
align-items: baseline;
justify-content: space-between;
padding: 11px 13px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(255, 255, 255, 0.58);
}
.equation strong {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 17px;
letter-spacing: 0.03em;
}
.equation small {
color: #71878f;
font-size: 10px;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 9px;
}
.metric {
min-width: 0;
padding: 11px 12px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(255, 255, 255, 0.56);
}
.metric-label {
display: block;
margin-bottom: 5px;
color: #6c828a;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.metric strong {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 18px;
font-weight: 760;
}
.metric small {
margin-left: 3px;
color: #748990;
font-size: 10px;
}
.metric.left strong {
color: #168e9a;
}
.metric.right strong {
color: #ce604c;
}
.net-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 9px;
padding: 11px 13px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(23, 49, 61, 0.055);
}
.net-row span {
color: #617981;
font-size: 10px;
font-weight: 750;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.net-row strong {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 14px;
}
.tilt-row {
display: grid;
grid-template-columns: 1fr 66px;
gap: 10px;
align-items: center;
margin-top: 9px;
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(255, 255, 255, 0.56);
}
.tilt-copy span {
display: block;
margin-bottom: 3px;
color: #6c828a;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.tilt-copy strong {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 18px;
}
.tilt-copy small {
display: block;
margin-top: 2px;
color: #71878f;
font-size: 9px;
}
.tilt-meter {
position: relative;
width: 58px;
height: 32px;
overflow: hidden;
}
.tilt-meter::before {
position: absolute;
right: 4px;
bottom: 5px;
left: 4px;
height: 18px;
border-bottom: 2px solid #9bb0b6;
border-radius: 50%;
content: "";
}
#tiltNeedle {
position: absolute;
z-index: 1;
bottom: 13px;
left: 28px;
width: 25px;
height: 2px;
border-radius: 2px;
background: var(--pivot);
transform-origin: 0 50%;
transition: transform 80ms linear;
}
#tiltNeedle::after {
position: absolute;
top: -3px;
right: -1px;
width: 0;
height: 0;
border-top: 4px solid transparent;
border-bottom: 4px solid transparent;
border-left: 7px solid var(--pivot);
content: "";
}
.count-note {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid var(--border);
color: #6b8189;
font-size: 10px;
}
.count-note strong {
color: var(--ink);
}
#explanation {
min-height: 34px;
margin: 7px 0 0;
color: #6c828a;
font-size: 10px;
line-height: 1.5;
}
.legend {
position: fixed;
z-index: 9;
top: 22px;
right: 22px;
width: 205px;
padding: 14px 15px;
border: 1px solid var(--border);
border-radius: 16px;
background: rgba(247, 251, 250, 0.77);
box-shadow: 0 12px 30px rgba(35, 68, 78, 0.10);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
pointer-events: none;
}
.legend-title {
margin-bottom: 9px;
color: #6b8189;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
margin: 7px 0;
color: #4e6972;
font-size: 10px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
background: var(--left);
}
.swatch.right {
background: var(--right);
}
.swatch.pivot {
background: var(--pivot);
}
.slot-note {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid var(--border);
color: #6c828a;
font-size: 9px;
line-height: 1.5;
}
.hint {
position: fixed;
z-index: 8;
bottom: 19px;
left: 50%;
display: flex;
gap: 15px;
align-items: center;
padding: 9px 13px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(247, 251, 250, 0.72);
box-shadow: 0 8px 24px rgba(35, 68, 78, 0.08);
backdrop-filter: blur(12px);
color: #617881;
font-size: 9px;
letter-spacing: 0.04em;
transform: translateX(-50%);
pointer-events: none;
}
.hint b {
color: var(--ink);
font-size: 9px;
letter-spacing: 0.1em;
}
@media (max-width: 760px) {
.panel {
top: 10px;
left: 10px;
width: min(330px, calc(100vw - 20px));
padding: 15px;
border-radius: 16px;
}
h1 {
font-size: 21px;
}
.intro {
margin-bottom: 11px;
}
.legend {
top: auto;
right: 10px;
bottom: 10px;
width: 178px;
padding: 11px 12px;
}
.hint {
display: none;
}
}
@media (max-height: 720px) {
.panel {
padding: 14px 16px;
}
.intro {
display: none;
}
.status {
margin-top: 11px;
}
#explanation {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive three-dimensional counterweight lever"></canvas>
<section class="panel" aria-label="Lever controls and torque measurements">
<div class="eyebrow">Low-poly mechanics lab</div>
<h1>Counterweight Balance</h1>
<p class="intro">
Equal-distance slots surround the pivot. The lever tilts from the
difference in torque—not from how many objects are present.
</p>
<div class="presets" role="group" aria-label="Lever presets">
<button class="preset active" data-preset="balanced" aria-pressed="true">
Balanced
</button>
<button class="preset" data-preset="left" aria-pressed="false">
Left heavy
</button>
<button class="preset" data-preset="right" aria-pressed="false">
Right heavy
</button>
</div>
<div id="status" class="status">Balanced</div>
<div class="equation">
<strong>τ = m × r × g</strong>
<small>g = 9.81 m/s²</small>
</div>
<div class="metrics">
<div class="metric left">
<span class="metric-label">Left torque</span>
<strong id="leftTorque">372.8</strong><small>N·m</small>
</div>
<div class="metric right">
<span class="metric-label">Right torque</span>
<strong id="rightTorque">372.8</strong><small>N·m</small>
</div>
</div>
<div class="net-row">
<span>Net torque</span>
<strong id="netTorque">0.0 N·m</strong>
</div>
<div class="tilt-row">
<div class="tilt-copy">
<span>Lever tilt</span>
<strong id="tiltValue">0.0°</strong>
<small>θ responds to net torque</small>
</div>
<div class="tilt-meter" aria-hidden="true">
<span id="tiltNeedle"></span>
</div>
</div>
<div class="count-note">
<span>Objects: <strong>3 / 3</strong></span>
<span>Count not used</span>
</div>
<p id="explanation">
Equal weighted moments: 38.0 kg·m on each side of the pivot.
</p>
</section>
<aside class="legend" aria-label="Scene legend">
<div class="legend-title">Beam map</div>
<div class="legend-row"><span class="swatch"></span> Left arm / torque</div>
<div class="legend-row"><span class="swatch right"></span> Right arm / torque</div>
<div class="legend-row"><span class="swatch pivot"></span> Pivot and axle</div>
<div class="slot-note">
Six slots at 1 m spacing. The inner positions hold counterweights.
</div>
</aside>
<div class="hint">
<span><b>DRAG</b> orbit</span>
<span><b>SCROLL</b> zoom</span>
<span><b>DOUBLE CLICK</b> reset</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const canvas = $("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e8);
scene.fog = new THREE.Fog(0xdce9e8, 17, 31);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Orbit parameters are implemented locally; no examples/addons are used.
const cameraTarget = new THREE.Vector3(-0.25, 1.45, 0);
const defaultCamera = {
yaw: 0.72,
pitch: 1.12,
radius: 13.8
};
let cameraYaw = defaultCamera.yaw;
let cameraPitch = defaultCamera.pitch;
let cameraRadius = defaultCamera.radius;
function updateCamera() {
const sinPitch = Math.sin(cameraPitch);
camera.position.set(
cameraTarget.x + cameraRadius * sinPitch * Math.sin(cameraYaw),
cameraTarget.y + cameraRadius * Math.cos(cameraPitch),
cameraTarget.z + cameraRadius * sinPitch * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = defaultCamera.yaw;
cameraPitch = defaultCamera.pitch;
cameraRadius = defaultCamera.radius;
updateCamera();
}
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.005,
0.48,
1.43
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.009,
8,
20
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// Lighting.
scene.add(new THREE.HemisphereLight(0xe9ffff, 0x52626a, 1.75));
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.1);
keyLight.position.set(-5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0006;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x8ddde1, 1.0);
rimLight.position.set(7, 5, -7);
scene.add(rimLight);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.05,
flatShading: true,
...options
});
}
const materials = {
ground: standardMaterial(0xc9d9d7),
platform: standardMaterial(0xe4ebe7),
platformEdge: standardMaterial(0xaebfbd),
dark: standardMaterial(0x294753),
darkSoft: standardMaterial(0x3f5d66),
left: standardMaterial(0x35aeba),
right: standardMaterial(0xe17a63),
leftDark: standardMaterial(0x237f89),
rightDark: standardMaterial(0xb95847),
pivot: standardMaterial(0xe4aa36),
pivotDark: standardMaterial(0xa87122),
steel: standardMaterial(0x526873, { metalness: 0.35 }),
crateDark: standardMaterial(0x2e4650),
white: standardMaterial(0xf7f4df)
};
// Ground and circular demonstration platform.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.13;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(6.2, 6.38, 0.22, 48),
materials.platform
);
platform.position.y = -0.01;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const platformRim = new THREE.Mesh(
new THREE.CylinderGeometry(6.38, 6.38, 0.08, 48),
materials.platformEdge
);
platformRim.position.y = -0.09;
platformRim.receiveShadow = true;
scene.add(platformRim);
// Grid clipped to the circular platform.
const gridPositions = [];
const gridRadius = 5.0;
for (let i = -5; i <= 5; i++) {
const extent = Math.sqrt(gridRadius * gridRadius - i * i);
gridPositions.push(
i, 0.115, -extent,
i, 0.115, extent
);
gridPositions.push(
-extent, 0.115, i,
extent, 0.115, i
);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
const gridMaterial = new THREE.LineBasicMaterial({
color: 0x789397,
transparent: true,
opacity: 0.2
});
scene.add(new THREE.LineSegments(gridGeometry, gridMaterial));
// Platform bolts.
const boltGeometry = new THREE.CylinderGeometry(0.075, 0.075, 0.045, 8);
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2;
const bolt = new THREE.Mesh(boltGeometry, materials.darkSoft);
bolt.position.set(Math.cos(angle) * 5.55, 0.13, Math.sin(angle) * 5.55);
bolt.castShadow = true;
scene.add(bolt);
}
// Pivot height and rotating lever group.
const pivotHeight = 1.68;
const lever = new THREE.Group();
lever.position.y = pivotHeight;
scene.add(lever);
// Central hub.
const centerBeam = new THREE.Mesh(
new THREE.BoxGeometry(1.3, 0.32, 0.62),
materials.dark
);
centerBeam.castShadow = true;
centerBeam.receiveShadow = true;
lever.add(centerBeam);
// Separate left and right lever arms.
const armGeometry = new THREE.BoxGeometry(3.65, 0.26, 0.52);
const lowerArmGeometry = new THREE.BoxGeometry(3.65, 0.09, 0.58);
const leftArm = new THREE.Mesh(armGeometry, materials.left);
leftArm.position.x = -2.425;
leftArm.castShadow = true;
leftArm.receiveShadow = true;
lever.add(leftArm);
const rightArm = new THREE.Mesh(armGeometry, materials.right);
rightArm.position.x = 2.425;
rightArm.castShadow = true;
rightArm.receiveShadow = true;
lever.add(rightArm);
const leftLower = new THREE.Mesh(lowerArmGeometry, materials.leftDark);
leftLower.position.set(-2.425, -0.16, 0);
leftLower.castShadow = true;
lever.add(leftLower);
const rightLower = new THREE.Mesh(lowerArmGeometry, materials.rightDark);
rightLower.position.set(2.425, -0.16, 0);
rightLower.castShadow = true;
lever.add(rightLower);
// Slot strips.
const slotGeometry = new THREE.BoxGeometry(0.6, 0.035, 0.58);
for (const side of [-1, 1]) {
for (const distance of [1, 2, 3]) {
const slot = new THREE.Mesh(slotGeometry, materials.white);
slot.position.set(side * distance, 0.145, 0);
slot.castShadow = true;
lever.add(slot);
}
}
// Canvas-based labels, generated entirely in this HTML.
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function canvasTexture(canvasElement) {
const texture = new THREE.CanvasTexture(canvasElement);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
function makeDistanceTexture(distance) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 256;
labelCanvas.height = 96;
const ctx = labelCanvas.getContext("2d");
roundedRect(ctx, 18, 16, 220, 64, 20);
ctx.fillStyle = "rgba(20, 47, 57, 0.84)";
ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.font = "700 38px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(`${distance} m`, 128, 49);
return canvasTexture(labelCanvas);
}
function makeMassTexture(mass, kind, accent) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 320;
labelCanvas.height = 160;
const ctx = labelCanvas.getContext("2d");
roundedRect(ctx, 10, 10, 300, 140, 28);
ctx.fillStyle = "rgba(20, 47, 57, 0.92)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 5;
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "rgba(255,255,255,0.68)";
ctx.font = "800 21px system-ui, sans-serif";
ctx.fillText(kind, 160, 42);
ctx.fillStyle = "#ffffff";
ctx.font = "800 61px system-ui, sans-serif";
ctx.fillText(`${mass} kg`, 160, 91);
return canvasTexture(labelCanvas);
}
function makeBadgeTexture(text, accent) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 256;
labelCanvas.height = 96;
const ctx = labelCanvas.getContext("2d");
roundedRect(ctx, 14, 16, 228, 64, 20);
ctx.fillStyle = "rgba(25, 49, 58, 0.9)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 4;
ctx.stroke();
ctx.fillStyle = "#ffffff";
ctx.font = "800 30px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 128, 49);
return canvasTexture(labelCanvas);
}
function makeSprite(texture, width, height) {
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
for (const side of [-1, 1]) {
for (const distance of [1, 2, 3]) {
const label = makeSprite(
makeDistanceTexture(distance),
0.58,
0.22
);
label.position.set(side * distance, 0.19, 0.69);
label.renderOrder = 8;
lever.add(label);
}
}
// Triangular fulcrum.
const fulcrumShape = new THREE.Shape();
fulcrumShape.moveTo(-0.74, 0);
fulcrumShape.lineTo(0.74, 0);
fulcrumShape.lineTo(0, 1.42);
fulcrumShape.closePath();
const fulcrumGeometry = new THREE.ExtrudeGeometry(fulcrumShape, {
depth: 0.78,
bevelEnabled: false,
curveSegments: 1
});
fulcrumGeometry.translate(0, 0, -0.39);
const fulcrum = new THREE.Mesh(fulcrumGeometry, materials.pivot);
fulcrum.position.y = 0.12;
fulcrum.castShadow = true;
fulcrum.receiveShadow = true;
scene.add(fulcrum);
// Axle and visible front pivot cap.
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.205, 0.205, 1.24, 12),
materials.steel
);
axle.rotation.x = Math.PI / 2;
axle.position.set(0, pivotHeight, 0);
axle.castShadow = true;
scene.add(axle);
const pivotRing = new THREE.Mesh(
new THREE.TorusGeometry(0.25, 0.065, 8, 20),
materials.pivotDark
);
pivotRing.position.set(0, pivotHeight, 0.64);
pivotRing.castShadow = true;
scene.add(pivotRing);
const pivotCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.12, 10),
materials.pivot
);
pivotCap.rotation.x = Math.PI / 2;
pivotCap.position.set(0, pivotHeight, 0.72);
pivotCap.castShadow = true;
scene.add(pivotCap);
const pivotLabel = makeSprite(
makeBadgeTexture("PIVOT", "#e4aa36"),
0.92,
0.34
);
pivotLabel.position.set(0, pivotHeight + 0.83, 0.34);
scene.add(pivotLabel);
// Curved torque arrows around the pivot.
function createTorqueArrow(color, startAngle, endAngle) {
const points = [];
const segments = 42;
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const angle = THREE.MathUtils.lerp(startAngle, endAngle, t);
points.push(
new THREE.Vector3(
Math.cos(angle) * 0.96,
Math.sin(angle) * 0.96,
0
)
);
}
const group = new THREE.Group();
group.position.set(0, pivotHeight, 0.79);
const lineMaterial = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.7,
depthTest: false
});
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
lineMaterial
);
line.renderOrder = 15;
group.add(line);
const directionSign = Math.sign(endAngle - startAngle);
const tangent = new THREE.Vector3(
-Math.sin(endAngle) * directionSign,
Math.cos(endAngle) * directionSign,
0
).normalize();
const arrowMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.7,
depthTest: false
});
const arrowhead = new THREE.Mesh(
new THREE.ConeGeometry(0.10, 0.25, 6),
arrowMaterial
);
const endpoint = points[points.length - 1];
arrowhead.position.copy(endpoint).addScaledVector(tangent, -0.09);
arrowhead.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
arrowhead.renderOrder = 15;
group.add(arrowhead);
scene.add(group);
return {
group,
lineMaterial,
arrowMaterial
};
}
const leftTorqueArrow = createTorqueArrow(
0x21a9b6,
Math.PI * 0.64,
Math.PI * 1.37
);
const rightTorqueArrow = createTorqueArrow(
0xec765e,
Math.PI * 0.36,
-Math.PI * 0.36
);
// Shared geometries for the six loads.
const crateGeometry = new THREE.BoxGeometry(1, 1, 1);
const crateEdgesGeometry = new THREE.EdgesGeometry(crateGeometry);
const strapGeometry = new THREE.BoxGeometry(1, 1, 1);
const counterPlateGeometry = new THREE.CylinderGeometry(
0.34,
0.34,
0.13,
8
);
const rodGeometry = new THREE.CylinderGeometry(
0.025,
0.025,
0.82,
6
);
const loads = [];
function createLoad(side, distance, kind) {
const group = new THREE.Group();
group.position.set(side * distance, 0.15, 0);
lever.add(group);
const sideMaterial = side < 0 ? materials.left : materials.right;
const sideName = side < 0 ? "left" : "right";
const accent = side < 0 ? "#21a9b6" : "#ec765e";
const load = {
side: sideName,
distance,
kind,
group,
massLabel: null,
crate: null,
crateEdges: null,
straps: [],
plates: []
};
if (kind === "crate") {
load.crate = new THREE.Mesh(crateGeometry, sideMaterial);
load.crate.castShadow = true;
load.crate.receiveShadow = true;
group.add(load.crate);
load.crateEdges = new THREE.LineSegments(
crateEdgesGeometry,
new THREE.LineBasicMaterial({
color: 0x28434d,
transparent: true,
opacity: 0.68
})
);
group.add(load.crateEdges);
for (let i = 0; i < 2; i++) {
const strap = new THREE.Mesh(strapGeometry, materials.crateDark);
strap.castShadow = true;
group.add(strap);
load.straps.push(strap);
}
const topStrap = new THREE.Mesh(strapGeometry, materials.crateDark);
topStrap.castShadow = true;
group.add(topStrap);
load.topStrap = topStrap;
} else {
const clamp = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 0.16, 0.55),
sideMaterial
);
clamp.position.y = 0.05;
clamp.castShadow = true;
group.add(clamp);
load.clamp = clamp;
for (const z of [-0.18, 0.18]) {
const rod = new THREE.Mesh(rodGeometry, materials.steel);
rod.position.set(0, -0.34, z);
rod.castShadow = true;
group.add(rod);
}
for (let i = 0; i < 4; i++) {
const plate = new THREE.Mesh(
counterPlateGeometry,
[materials.steel, sideMaterial, materials.steel]
);
plate.position.y = -0.58 + i * 0.14;
plate.castShadow = true;
plate.receiveShadow = true;
group.add(plate);
load.plates.push(plate);
}
}
load.massLabel = makeSprite(
makeMassTexture(1, kind === "crate" ? "LOAD" : "COUNTERWEIGHT", accent),
1.08,
0.54
);
group.add(load.massLabel);
loads.push(load);
return load;
}
createLoad(-1, 1, "counter");
createLoad(-1, 2, "crate");
createLoad(-1, 3, "crate");
createLoad(1, 1, "counter");
createLoad(1, 2, "crate");
createLoad(1, 3, "crate");
function counterPlateCount(mass) {
if (mass <= 4) return 2;
if (mass <= 6) return 3;
if (mass <= 9) return 3;
return 4;
}
function updateMassLabel(load, mass) {
const accent = load.side === "left" ? "#21a9b6" : "#ec765e";
const kind = load.kind === "crate" ? "LOAD" : "COUNTERWEIGHT";
const oldTexture = load.massLabel.material.map;
load.massLabel.material.map = makeMassTexture(mass, kind, accent);
load.massLabel.material.needsUpdate = true;
oldTexture.dispose();
}
function updateLoad(load, mass) {
load.mass = mass;
if (load.kind === "crate") {
const width = 0.58 + mass * 0.015;
const height = 0.50 + mass * 0.020;
const depth = 0.58;
load.crate.scale.set(width, height, depth);
load.crate.position.y = height / 2;
load.crateEdges.scale.copy(load.crate.scale);
load.crateEdges.position.copy(load.crate.position);
load.straps[0].scale.set(0.055, height + 0.035, depth + 0.045);
load.straps[0].position.set(-width * 0.28, height / 2, 0);
load.straps[1].scale.set(0.055, height + 0.035, depth + 0.045);
load.straps[1].position.set(width * 0.28, height / 2, 0);
load.topStrap.scale.set(width + 0.035, 0.05, depth + 0.045);
load.topStrap.position.set(0, height + 0.01, 0);
load.massLabel.position.set(0, height + 0.42, 0.04);
} else {
const count = counterPlateCount(mass);
load.plates.forEach((plate, index) => {
plate.visible = index < count;
plate.position.y = -0.58 + index * 0.14;
});
load.massLabel.position.set(0, 0.78, 0.03);
}
updateMassLabel(load, mass);
}
// Every preset contains three objects on each side. Only mass changes.
const presets = {
balanced: {
left: { 1: 6, 2: 4, 3: 8 },
right: { 1: 4, 2: 5, 3: 8 }
},
left: {
left: { 1: 12, 2: 6, 3: 10 },
right: { 1: 4, 2: 5, 3: 8 }
},
right: {
left: { 1: 6, 2: 4, 3: 8 },
right: { 1: 12, 2: 6, 3: 10 }
}
};
const gravity = 9.81;
const maximumTilt = THREE.MathUtils.degToRad(16);
let targetAngle = 0;
let currentAngle = 0;
let netTorqueNm = 0;
function formatSigned(value) {
if (Math.abs(value) < 0.05) return "0.0";
return `${value > 0 ? "+" : ""}${value.toFixed(1)}`;
}
function updateTorqueReadout() {
let leftKgM = 0;
let rightKgM = 0;
for (const load of loads) {
const moment = load.mass * load.distance;
if (load.side === "left") leftKgM += moment;
else rightKgM += moment;
}
const leftNm = leftKgM * gravity;
const rightNm = rightKgM * gravity;
netTorqueNm = leftNm - rightNm;
$("leftTorque").textContent = leftNm.toFixed(1);
$("rightTorque").textContent = rightNm.toFixed(1);
$("netTorque").textContent = `${formatSigned(netTorqueNm)} N·m`;
const differenceKgM = leftKgM - rightKgM;
if (Math.abs(differenceKgM) < 0.001) {
$("status").textContent = "Balanced";
$("status").className = "status";
$("explanation").textContent =
`Equal weighted moments: ${leftKgM.toFixed(1)} kg·m on each side of the pivot.`;
} else if (differenceKgM > 0) {
$("status").textContent = "Left heavy";
$("status").className = "status left";
$("explanation").textContent =
`Left torque exceeds right by ${differenceKgM.toFixed(1)} kg·m, so the left arm settles lower.`;
} else {
$("status").textContent = "Right heavy";
$("status").className = "status right";
$("explanation").textContent =
`Right torque exceeds left by ${Math.abs(differenceKgM).toFixed(1)} kg·m, so the right arm settles lower.`;
}
// IMPORTANT: Tilt is derived only from net torque.
// Object count is deliberately not part of this calculation.
const normalizedTorque = netTorqueNm / 190;
targetAngle = Math.tanh(normalizedTorque) * maximumTilt;
const isBalanced = Math.abs(netTorqueNm) < 0.05;
leftTorqueArrow.lineMaterial.opacity = isBalanced
? 0.55
: netTorqueNm > 0
? 0.95
: 0.18;
leftTorqueArrow.arrowMaterial.opacity =
leftTorqueArrow.lineMaterial.opacity;
rightTorqueArrow.lineMaterial.opacity = isBalanced
? 0.55
: netTorqueNm < 0
? 0.95
: 0.18;
rightTorqueArrow.arrowMaterial.opacity =
rightTorqueArrow.lineMaterial.opacity;
}
function applyPreset(name) {
const preset = presets[name];
for (const load of loads) {
updateLoad(load, preset[load.side][load.distance]);
}
updateTorqueReadout();
document.querySelectorAll(".preset").forEach((button) => {
const active = button.dataset.preset === name;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
}
document.querySelectorAll(".preset").forEach((button) => {
button.addEventListener("click", () => {
applyPreset(button.dataset.preset);
});
});
applyPreset("balanced");
// Animation loop.
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-delta * 4.8);
currentAngle = THREE.MathUtils.lerp(
currentAngle,
targetAngle,
smoothing
);
lever.rotation.z = currentAngle;
// Keep crates and hanging counterweights upright.
for (const load of loads) {
load.group.rotation.z = -currentAngle;
}
const tiltDegrees = THREE.MathUtils.radToDeg(currentAngle);
$("tiltValue").textContent = `${tiltDegrees.toFixed(1)}°`;
$("tiltNeedle").style.transform = `rotate(${-tiltDegrees}deg)`;
renderer.render(scene, camera);
});
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", onResize);
updateCamera();
</script>
</body>
</html>
```