Prompt specification
Build or customize the Three.js 3D scene "Balanced and unbalanced tug displays" 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: Balanced and unbalanced tug displays
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/balanced-and-unbalanced-tug-displays-x1438
Scene Description Prompt:
Please use Three.js from cdnjs for a stylized scene based on this idea: balanced and unbalanced tug displays. Arrange two identical rings with opposing pulls, one balanced and one unequal; show zero resultant in the balanced case and acceleration only in the unbalanced model. Return one self-contained HTML file.
Working Three.js Starter (balanced-and-unbalanced-tug-displays-x1438):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tug Lab — Balanced vs. Unbalanced Forces</title>
<style>
:root {
--bg: #07111f;
--panel: rgba(11, 25, 43, 0.78);
--border: rgba(148, 184, 214, 0.18);
--text: #edf7ff;
--muted: #8fa9c1;
--left: #ff6f7d;
--right: #49d9ff;
--accent: #fbbf24;
--balanced: #5eead4;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 22% 20%, rgba(54, 116, 150, 0.2), transparent 36%),
radial-gradient(circle at 78% 28%, rgba(84, 65, 140, 0.18), transparent 34%),
linear-gradient(180deg, #091626 0%, #050c16 100%);
color: var(--text);
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;
cursor: grab;
touch-action: none;
outline: none;
}
#scene:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(2, 8, 16, 0.3), transparent 25%, transparent 72%, rgba(2, 8, 16, 0.5)),
radial-gradient(circle at center, transparent 48%, rgba(2, 7, 14, 0.5) 100%);
}
header {
position: fixed;
z-index: 5;
top: 24px;
left: 28px;
right: 28px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
width: 46px;
height: 46px;
border: 1px solid rgba(94, 234, 212, 0.32);
border-radius: 15px;
display: grid;
place-items: center;
background: linear-gradient(145deg, rgba(94, 234, 212, 0.13), rgba(73, 217, 255, 0.04));
box-shadow: inset 0 0 24px rgba(94, 234, 212, 0.06);
font-size: 23px;
color: var(--balanced);
}
h1 {
margin: 0;
font-size: clamp(20px, 2.3vw, 30px);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin-top: 7px;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.header-right {
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.legend {
display: flex;
align-items: center;
gap: 16px;
padding: 10px 14px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(6, 15, 27, 0.55);
backdrop-filter: blur(12px);
color: #b8cadb;
font-size: 11px;
letter-spacing: 0.03em;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.dot.left {
color: var(--left);
background: currentColor;
}
.dot.right {
color: var(--right);
background: currentColor;
}
.dot.accel {
color: var(--accent);
background: currentColor;
}
button {
appearance: none;
border: 1px solid rgba(148, 184, 214, 0.22);
border-radius: 999px;
padding: 10px 13px;
background: rgba(8, 20, 35, 0.72);
color: #d8e9f7;
font: 700 10px/1 Inter, ui-sans-serif, system-ui, sans-serif;
letter-spacing: 0.08em;
cursor: pointer;
backdrop-filter: blur(12px);
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
button:hover {
border-color: rgba(94, 234, 212, 0.55);
background: rgba(20, 43, 60, 0.85);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--balanced);
outline-offset: 3px;
}
.cases {
position: fixed;
z-index: 5;
left: 28px;
right: 28px;
bottom: 28px;
display: flex;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.case-card {
width: min(350px, 31vw);
padding: 16px 18px 15px;
border: 1px solid var(--border);
border-top-color: rgba(94, 234, 212, 0.6);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(17, 37, 58, 0.82), rgba(7, 18, 31, 0.82));
box-shadow:
0 20px 55px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px);
}
.case-card.unbalanced {
border-top-color: rgba(251, 191, 36, 0.72);
}
.card-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
.model-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.same-badge {
padding: 5px 7px;
border: 1px solid rgba(148, 184, 214, 0.18);
border-radius: 999px;
color: #a9bed0;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
white-space: nowrap;
}
.case-card h2 {
margin: 0 0 10px;
font-size: 18px;
letter-spacing: -0.025em;
}
.forces {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin: 10px 0 12px;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.02em;
}
.force-left {
color: var(--left);
}
.force-right {
color: var(--right);
}
.equals {
color: #5f7890;
font-weight: 500;
}
.result {
display: flex;
align-items: end;
justify-content: space-between;
gap: 12px;
padding-top: 11px;
border-top: 1px solid rgba(148, 184, 214, 0.12);
}
.result strong {
display: block;
font-size: 22px;
line-height: 1;
letter-spacing: -0.035em;
}
.result small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
}
.result .acceleration {
color: var(--balanced);
text-align: right;
}
.unbalanced .result .acceleration {
color: var(--accent);
}
.motion-state {
display: inline-flex;
align-items: center;
gap: 6px;
margin-top: 10px;
color: var(--accent);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.1em;
}
.motion-state::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.progress {
height: 2px;
margin-top: 8px;
overflow: hidden;
border-radius: 999px;
background: rgba(251, 191, 36, 0.12);
}
.progress > div {
width: 100%;
height: 100%;
background: var(--accent);
transform: scaleX(0);
transform-origin: left;
box-shadow: 0 0 10px rgba(251, 191, 36, 0.7);
}
.camera-hint {
position: fixed;
z-index: 4;
bottom: 9px;
left: 50%;
transform: translateX(-50%);
color: rgba(164, 188, 208, 0.55);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.15em;
white-space: nowrap;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #07111f;
color: #91a9bf;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
.legend {
display: none;
}
}
@media (max-width: 700px) {
header {
top: 14px;
left: 14px;
right: 14px;
}
.brand-mark {
width: 38px;
height: 38px;
border-radius: 12px;
}
.subtitle {
display: none;
}
.header-right {
gap: 6px;
}
button {
padding: 9px 10px;
font-size: 8px;
}
.cases {
left: 12px;
right: 12px;
bottom: 12px;
gap: 8px;
}
.case-card {
width: 50%;
padding: 12px;
border-radius: 14px;
}
.case-card h2 {
font-size: 14px;
}
.same-badge {
display: none;
}
.forces {
font-size: 10px;
}
.result strong {
font-size: 17px;
}
.result small {
font-size: 8px;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive 3D comparison of balanced and unbalanced tug-of-force models"
></canvas>
<div class="vignette"></div>
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">⇄</div>
<div>
<h1>Tug / Force Lab</h1>
<div class="subtitle">Identical rings · opposing pulls · different outcomes</div>
</div>
</div>
<div class="header-right">
<div class="legend" aria-label="Force color legend">
<span><i class="dot left"></i>Left pull</span>
<span><i class="dot right"></i>Right pull</span>
<span><i class="dot accel"></i>Acceleration</span>
</div>
<button id="resetView" type="button">RESET VIEW</button>
<button id="motionToggle" type="button" aria-pressed="false">PAUSE</button>
</div>
</header>
<div class="cases">
<section class="case-card balanced" aria-label="Balanced model results">
<div class="card-top">
<span class="model-label">Model A</span>
<span class="same-badge">SAME 2 KG RING</span>
</div>
<h2>Balanced pull</h2>
<div class="forces">
<span class="force-left">← 10 N</span>
<span class="equals">=</span>
<span class="force-right">10 N →</span>
</div>
<div class="result">
<div>
<strong>ΣF = 0 N</strong>
<small>ZERO RESULTANT</small>
</div>
<div class="acceleration">
<strong>a = 0</strong>
<small>NO ACCELERATION</small>
</div>
</div>
</section>
<section class="case-card unbalanced" aria-label="Unbalanced model results">
<div class="card-top">
<span class="model-label">Model B</span>
<span class="same-badge">SAME 2 KG RING</span>
</div>
<h2>Unbalanced pull</h2>
<div class="forces">
<span class="force-left">← 6 N</span>
<span class="equals">≠</span>
<span class="force-right">14 N →</span>
</div>
<div class="result">
<div>
<strong>ΣF = 8 N →</strong>
<small>NONZERO RESULTANT</small>
</div>
<div class="acceleration">
<strong>a = 4 m/s²</strong>
<small>TO THE RIGHT</small>
</div>
</div>
<div class="motion-state" id="motionState">ACCELERATING →</div>
<div class="progress"><div id="progressBar"></div></div>
</section>
</div>
<div class="camera-hint">DRAG TO ORBIT · SCROLL TO ZOOM · DOUBLE-CLICK TO RESET</div>
<div id="loading">BUILDING FORCE LAB…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const motionToggle = document.querySelector("#motionToggle");
const resetViewButton = document.querySelector("#resetView");
const motionState = document.querySelector("#motionState");
const progressBar = document.querySelector("#progressBar");
const COLORS = {
background: 0x07111f,
left: 0xff6f7d,
right: 0x49d9ff,
acceleration: 0xfbbf24,
balanced: 0x5eead4,
ring: 0x49e0c1,
white: 0xe8f7ff
};
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(COLORS.background, 13, 31);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
theta: -0.055,
phi: 1.28,
radius: 13.4,
target: new THREE.Vector3(0, 1.65, 0)
};
let cameraWasAdjusted = false;
function defaultCameraRadius() {
const aspect = window.innerWidth / window.innerHeight;
if (aspect < 0.75) return 28;
if (aspect < 1.2) return 20;
return 13.4;
}
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * cosPhi,
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
function resize() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
if (!cameraWasAdjusted) {
orbit.radius = defaultCameraRadius();
}
updateCamera();
}
window.addEventListener("resize", resize);
// Lighting and atmosphere.
scene.add(new THREE.HemisphereLight(0x9ddcff, 0x07101b, 1.75));
const keyLight = new THREE.DirectionalLight(0xd8f3ff, 3.2);
keyLight.position.set(4, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.00015;
scene.add(keyLight);
const warmLight = new THREE.DirectionalLight(0xffb66c, 1.25);
warmLight.position.set(-7, 5, 4);
scene.add(warmLight);
const rimLight = new THREE.PointLight(0x426dff, 16, 18, 2);
rimLight.position.set(0, 5, -5);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
new THREE.MeshStandardMaterial({
color: 0x07121f,
roughness: 0.95,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.035;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(46, 46, 0x24445f, 0x14283c);
grid.position.y = -0.02;
grid.material.transparent = true;
grid.material.opacity = 0.25;
scene.add(grid);
// Deterministic background motes for depth.
let randomSeed = 8173;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
const moteMaterial = new THREE.MeshBasicMaterial({
color: 0x7fc9e8,
transparent: true,
opacity: 0.22
});
for (let i = 0; i < 34; i++) {
const mote = new THREE.Mesh(
new THREE.SphereGeometry(0.025 + random() * 0.045, 8, 8),
moteMaterial
);
mote.position.set(
(random() - 0.5) * 21,
0.8 + random() * 7,
-3 - random() * 8
);
scene.add(mote);
}
function cssColor(color) {
return typeof color === "number"
? `#${color.toString(16).padStart(6, "0")}`
: color;
}
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 makeBadge(title, subtitle, accent, worldWidth = 3) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 900;
labelCanvas.height = 250;
const ctx = labelCanvas.getContext("2d");
roundedRect(ctx, 7, 7, 886, 236, 40);
ctx.fillStyle = "rgba(5, 14, 26, 0.88)";
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = cssColor(accent);
ctx.stroke();
ctx.fillStyle = cssColor(accent);
roundedRect(ctx, 34, 48, 8, 154, 4);
ctx.fill();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#f3fbff";
ctx.font = "800 58px Inter, system-ui, sans-serif";
ctx.fillText(title, 460, 93);
ctx.fillStyle = "#9cb5ca";
ctx.font = "700 27px Inter, system-ui, sans-serif";
ctx.fillText(subtitle, 460, 166);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldWidth * 250 / 900, 1);
sprite.renderOrder = 20;
return sprite;
}
function makePill(text, accent, worldWidth = 0.82) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 600;
labelCanvas.height = 180;
const ctx = labelCanvas.getContext("2d");
roundedRect(ctx, 8, 8, 584, 164, 42);
ctx.fillStyle = "rgba(5, 14, 26, 0.9)";
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = cssColor(accent);
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = cssColor(accent);
ctx.font = "800 55px Inter, system-ui, sans-serif";
ctx.fillText(text, 300, 91);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldWidth * 180 / 600, 1);
sprite.renderOrder = 20;
return sprite;
}
function createArrow(start, direction, length, color, headLength = 0.28) {
const group = new THREE.Group();
const shaftLength = Math.max(0.05, length - headLength);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
toneMapped: false
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, shaftLength, 16),
material
);
shaft.rotation.z = -Math.PI / 2;
shaft.position.x = shaftLength / 2;
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.15, headLength, 20),
material
);
head.rotation.z = -Math.PI / 2;
head.position.x = shaftLength + headLength / 2;
group.add(shaft, head);
group.position.copy(start);
group.rotation.y = -Math.atan2(direction.z, direction.x);
return group;
}
function createStation(root, accent, modelNumber) {
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(2.45, 2.58, 0.18, 64),
new THREE.MeshStandardMaterial({
color: 0x0d1d30,
roughness: 0.7,
metalness: 0.25
})
);
platform.position.y = 0.09;
platform.receiveShadow = true;
platform.castShadow = true;
root.add(platform);
const topDisc = new THREE.Mesh(
new THREE.CylinderGeometry(2.4, 2.4, 0.035, 64),
new THREE.MeshStandardMaterial({
color: 0x11253a,
roughness: 0.72,
metalness: 0.18
})
);
topDisc.position.y = 0.195;
topDisc.receiveShadow = true;
root.add(topDisc);
const accentWash = new THREE.Mesh(
new THREE.CircleGeometry(2.2, 64),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.025,
depthWrite: false
})
);
accentWash.rotation.x = -Math.PI / 2;
accentWash.position.y = 0.218;
root.add(accentWash);
const track = new THREE.Mesh(
new THREE.BoxGeometry(3.75, 0.05, 0.34),
new THREE.MeshStandardMaterial({
color: 0x07111d,
roughness: 0.58,
metalness: 0.35
})
);
track.position.set(0, 0.245, 0);
track.receiveShadow = true;
root.add(track);
for (let i = -3; i <= 3; i++) {
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.025, 0.014, i % 2 === 0 ? 0.52 : 0.35),
new THREE.MeshBasicMaterial({
color: i === 0 ? accent : 0x52708a,
transparent: true,
opacity: i === 0 ? 0.9 : 0.45
})
);
tick.position.set(i * 0.55, 0.279, 0);
root.add(tick);
}
const numberBadge = makePill(modelNumber, accent, 0.43);
numberBadge.position.set(-2.02, 0.75, 0.45);
root.add(numberBadge);
}
function createRing() {
const group = new THREE.Group();
const ringMaterial = new THREE.MeshPhysicalMaterial({
color: COLORS.ring,
emissive: 0x0b655e,
emissiveIntensity: 0.38,
roughness: 0.22,
metalness: 0.28,
clearcoat: 0.8,
clearcoatRoughness: 0.18
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.67, 0.13, 22, 80),
ringMaterial
);
ring.castShadow = true;
ring.receiveShadow = true;
group.add(ring);
const centerMaterial = new THREE.MeshStandardMaterial({
color: 0xffcf62,
emissive: 0x6b4300,
emissiveIntensity: 0.55,
roughness: 0.3,
metalness: 0.45
});
const center = new THREE.Mesh(
new THREE.SphereGeometry(0.17, 24, 18),
centerMaterial
);
center.position.z = 0.045;
center.castShadow = true;
group.add(center);
const dotMaterial = new THREE.MeshBasicMaterial({
color: 0xdffff8,
transparent: true,
opacity: 0.9,
toneMapped: false
});
for (let i = 0; i < 4; i++) {
const angle = i * Math.PI / 2;
const dot = new THREE.Mesh(
new THREE.SphereGeometry(0.038, 12, 10),
dotMaterial
);
dot.position.set(
Math.cos(angle) * 0.67,
Math.sin(angle) * 0.67,
0.08
);
group.add(dot);
}
const massLabel = makePill("2 KG", 0xffffff, 0.5);
massLabel.position.set(0, 0, 0.2);
massLabel.scale.multiplyScalar(0.65);
group.add(massLabel);
const glow = new THREE.PointLight(0x4fe0c5, 1.1, 3.5, 2);
glow.position.set(0, 0.15, 1.1);
group.add(glow);
return group;
}
const balancedRoot = new THREE.Group();
balancedRoot.position.x = -3.3;
scene.add(balancedRoot);
createStation(balancedRoot, COLORS.balanced, "A");
const unbalancedRoot = new THREE.Group();
unbalancedRoot.position.x = 3.3;
scene.add(unbalancedRoot);
createStation(unbalancedRoot, COLORS.acceleration, "B");
function buildModel(root, balanced) {
const moving = new THREE.Group();
root.add(moving);
const ring = createRing();
ring.position.set(0, 1.5, 0);
moving.add(ring);
const leftLength = balanced ? 1.45 : 0.9;
const rightLength = balanced ? 1.45 : 2.1;
const leftArrow = createArrow(
new THREE.Vector3(-0.72, 1.5, 0.05),
new THREE.Vector3(-1, 0, 0),
leftLength,
COLORS.left
);
moving.add(leftArrow);
const rightArrow = createArrow(
new THREE.Vector3(0.72, 1.5, 0.05),
new THREE.Vector3(1, 0, 0),
rightLength,
COLORS.right
);
moving.add(rightArrow);
const connectorGeometry = new THREE.SphereGeometry(0.085, 16, 12);
const leftConnector = new THREE.Mesh(
connectorGeometry,
new THREE.MeshBasicMaterial({ color: COLORS.left, toneMapped: false })
);
leftConnector.position.set(-0.72, 1.5, 0.05);
moving.add(leftConnector);
const rightConnector = new THREE.Mesh(
connectorGeometry,
new THREE.MeshBasicMaterial({ color: COLORS.right, toneMapped: false })
);
rightConnector.position.set(0.72, 1.5, 0.05);
moving.add(rightConnector);
const leftTag = makePill(balanced ? "10 N" : "6 N", COLORS.left, 0.76);
leftTag.position.set(-0.72 - leftLength, 2.03, 0.12);
moving.add(leftTag);
const rightTag = makePill(balanced ? "10 N" : "14 N", COLORS.right, 0.82);
rightTag.position.set(0.72 + rightLength, 2.03, 0.12);
moving.add(rightTag);
if (balanced) {
const resultBadge = makeBadge(
"ΣF = 0 N",
"a = 0 m/s² · NO ACCELERATION",
COLORS.balanced,
3.0
);
resultBadge.position.set(0, 3.25, 0.12);
moving.add(resultBadge);
} else {
const resultantArrow = createArrow(
new THREE.Vector3(0.12, 2.62, 0.1),
new THREE.Vector3(1, 0, 0),
1.48,
COLORS.acceleration,
0.3
);
moving.add(resultantArrow);
const resultBadge = makeBadge(
"ΣF = 8 N →",
"a = 4 m/s² · RIGHTWARD",
COLORS.acceleration,
3.0
);
resultBadge.position.set(0.18, 3.28, 0.12);
moving.add(resultBadge);
}
return moving;
}
const balancedMoving = buildModel(balancedRoot, true);
const unbalancedMoving = buildModel(unbalancedRoot, false);
const balancedTitle = makeBadge(
"BALANCED",
"EQUAL + OPPOSITE PULLS",
COLORS.balanced,
3.05
);
balancedTitle.position.set(0, 4.16, 0);
balancedRoot.add(balancedTitle);
const unbalancedTitle = makeBadge(
"UNBALANCED",
"THE STRONGER PULL WINS",
COLORS.acceleration,
3.05
);
unbalancedTitle.position.set(0, 4.16, 0);
unbalancedRoot.add(unbalancedTitle);
// The acceleration vector appears only in the unbalanced model.
const accelerationArrow = createArrow(
new THREE.Vector3(-1.42, 0.42, 0.58),
new THREE.Vector3(1, 0, 0),
2.05,
COLORS.acceleration,
0.3
);
unbalancedRoot.add(accelerationArrow);
const accelerationLabel = makePill("a = 4 m/s²", COLORS.acceleration, 1.05);
accelerationLabel.position.set(0.42, 0.68, 0.62);
unbalancedRoot.add(accelerationLabel);
const startLabel = makePill("START", 0x7892a8, 0.58);
startLabel.position.set(-0.65, 0.37, 0.55);
unbalancedRoot.add(startLabel);
// Prepare the moving unbalanced model for a seamless acceleration/reset loop.
function prepareFade(group) {
group.traverse((object) => {
if (!object.material) return;
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach((material) => {
if (!material.userData.fadePrepared) {
material.transparent = true;
material.userData.fadePrepared = true;
material.userData.baseOpacity = material.opacity;
}
});
});
}
function setGroupOpacity(group, alpha) {
group.traverse((object) => {
if (!object.material) return;
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach((material) => {
material.opacity = material.userData.baseOpacity * alpha;
});
});
}
prepareFade(unbalancedMoving);
// Direct camera controls: pointer orbit, wheel zoom, and keyboard controls.
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.focus({ preventScroll: true });
});
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.004, 0.72, 1.48);
cameraWasAdjusted = true;
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8,
30
);
cameraWasAdjusted = true;
updateCamera();
}, { passive: false });
function resetCamera() {
orbit.theta = -0.055;
orbit.phi = 1.28;
orbit.radius = defaultCameraRadius();
cameraWasAdjusted = false;
updateCamera();
}
canvas.addEventListener("dblclick", resetCamera);
resetViewButton.addEventListener("click", resetCamera);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
orbit.theta += 0.08;
break;
case "ArrowRight":
orbit.theta -= 0.08;
break;
case "ArrowUp":
orbit.phi = Math.max(0.72, orbit.phi - 0.06);
break;
case "ArrowDown":
orbit.phi = Math.min(1.48, orbit.phi + 0.06);
break;
case "+":
case "=":
orbit.radius = Math.max(8, orbit.radius - 0.7);
break;
case "-":
orbit.radius = Math.min(30, orbit.radius + 0.7);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
cameraWasAdjusted = true;
updateCamera();
}
});
let paused = false;
motionToggle.addEventListener("click", () => {
paused = !paused;
motionToggle.textContent = paused ? "RESUME" : "PAUSE";
motionToggle.setAttribute("aria-pressed", String(paused));
});
resize();
updateCamera();
let motionTime = 0;
let previousTime = performance.now();
let previousState = "";
const cycleDuration = 5.4;
const accelerationPhase = 3.6;
const visualAcceleration = (2 * 1.3) / (accelerationPhase * accelerationPhase);
const startX = -0.65;
const endX = 0.65;
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
motionTime += delta;
}
const phase = motionTime % cycleDuration;
let ringX = startX;
let opacity = 1;
let progress = 0;
if (phase < accelerationPhase) {
// Constant acceleration: displacement grows with t².
ringX = startX + 0.5 * visualAcceleration * phase * phase;
progress = phase / accelerationPhase;
} else if (phase < 4.3) {
// Fade at the end before resetting, so the ring never accelerates backward.
ringX = endX;
opacity = 1 - (phase - accelerationPhase) / (4.3 - accelerationPhase);
progress = 1;
} else {
// Fade in at the original position and begin the next run.
ringX = startX;
opacity = THREE.MathUtils.clamp((phase - 4.3) / 0.9, 0, 1);
progress = 0;
}
unbalancedMoving.position.x = ringX;
setGroupOpacity(unbalancedMoving, opacity);
const currentState = phase < accelerationPhase ? "ACCELERATING →" : "DEMO RESET";
if (currentState !== previousState) {
motionState.textContent = currentState;
previousState = currentState;
}
progressBar.style.transform = `scaleX(${progress})`;
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
loading.classList.add("hidden");
</script>
</body>
</html>
```