Prompt specification
Build or customize the Three.js 3D scene "Unequal-mass balance sculpture" 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-mass balance sculpture
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/unequal-mass-balance-sculpture-x1365
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Join two differently weighted model blocks with a rigid rod and mark their calculated center of mass; distinguish physical size from explicitly assigned mass.
Working Three.js Starter (unequal-mass-balance-sculpture-x1365):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Three.js — Size vs. Mass Center-of-Mass Lab</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(13, 19, 30, 0.92);
--border: rgba(255, 255, 255, 0.12);
--text: #f2f6fb;
--muted: #9baabd;
--a: #28c7aa;
--b: #ff8a4c;
--com: #ffd447;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101722;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
overflow: hidden;
pointer-events: none;
}
.model-label,
.com-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -100%);
white-space: nowrap;
border: 1px solid var(--border);
background: rgba(9, 14, 23, 0.88);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(10px);
}
.model-label {
display: flex;
align-items: center;
gap: 9px;
padding: 8px 11px;
border-radius: 9px;
}
.model-label .dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--block-color);
box-shadow: 0 0 12px var(--block-color);
}
.model-label b,
.model-label small {
display: block;
}
.model-label b {
font-size: 12px;
letter-spacing: 0.02em;
}
.model-label small {
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.com-label {
padding: 8px 11px;
border-color: rgba(255, 212, 71, 0.45);
border-radius: 9px;
color: var(--com);
}
.com-label span {
display: block;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
}
.com-label strong {
display: block;
margin-top: 2px;
font-size: 12px;
color: #fff4c7;
}
#panel {
position: fixed;
z-index: 10;
top: 16px;
left: 16px;
width: 340px;
max-height: calc(100vh - 32px);
overflow: auto;
padding: 18px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.36);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #41506a transparent;
}
.eyebrow {
color: #8ba4c7;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
}
h1 {
margin: 5px 0 7px;
font-size: 23px;
line-height: 1.1;
}
.intro {
margin: 0 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.block-panel {
margin-top: 12px;
padding: 13px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.block-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 9px;
}
.block-heading strong {
font-size: 13px;
}
.block-heading strong::before {
content: "";
display: inline-block;
width: 8px;
height: 8px;
margin-right: 7px;
border-radius: 50%;
background: var(--block-color);
box-shadow: 0 0 9px var(--block-color);
}
.block-summary {
color: var(--muted);
font-size: 10px;
text-align: right;
}
.control-section-title {
margin: 12px 0 8px;
color: #8fa2ba;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.control + .control {
margin-top: 9px;
}
.control-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 4px;
font-size: 11px;
}
output {
color: #dce6f4;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 99px;
accent-color: var(--block-color);
cursor: pointer;
}
.mass-control {
padding: 8px 9px 10px;
border: 1px solid color-mix(in srgb, var(--block-color) 30%, transparent);
border-radius: 9px;
background: color-mix(in srgb, var(--block-color) 7%, transparent);
}
.mass-control .control-title {
font-weight: 700;
}
.note {
margin-top: 12px;
padding: 10px;
border-left: 2px solid #607895;
border-radius: 0 8px 8px 0;
background: rgba(255, 255, 255, 0.035);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
button {
padding: 9px 10px;
border: 1px solid var(--border);
border-radius: 9px;
background: rgba(255, 255, 255, 0.06);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
}
button:hover {
background: rgba(255, 255, 255, 0.11);
}
#readout {
position: fixed;
z-index: 9;
right: 16px;
top: 16px;
width: 285px;
padding: 15px;
border: 1px solid rgba(255, 212, 71, 0.26);
border-radius: 14px;
background: rgba(13, 19, 30, 0.88);
box-shadow: 0 18px 45px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(16px);
}
#readout .caption {
color: #9baabd;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#comValue {
display: block;
margin: 7px 0 3px;
color: var(--com);
font-size: 19px;
font-variant-numeric: tabular-nums;
}
#massTotal {
color: var(--muted);
font-size: 11px;
}
.formula {
margin-top: 12px;
padding: 9px 10px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.04);
color: #c7d3e2;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 11px;
margin-top: 11px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.legend i {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
}
.legend .system-dot {
background: var(--com);
box-shadow: 0 0 8px var(--com);
}
.legend .center-dot {
border: 1px solid #dceeff;
background: transparent;
}
#hint {
position: fixed;
z-index: 8;
left: 50%;
bottom: 14px;
transform: translateX(-50%);
padding: 8px 12px;
border: 1px solid var(--border);
border-radius: 99px;
background: rgba(10, 15, 24, 0.75);
color: #aebbd0;
font-size: 10px;
white-space: nowrap;
pointer-events: none;
backdrop-filter: blur(10px);
}
@media (max-width: 760px) {
#panel {
top: auto;
right: 8px;
bottom: 8px;
left: 8px;
width: auto;
max-height: 46vh;
padding: 14px;
}
#readout {
top: 8px;
right: 8px;
width: 225px;
padding: 12px;
}
#comValue {
font-size: 15px;
}
.formula,
#hint {
display: none;
}
.model-label small {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels" aria-hidden="true">
<div id="labelA" class="model-label"></div>
<div id="labelB" class="model-label"></div>
<div id="labelCom" class="com-label"></div>
</div>
<aside id="panel">
<div class="eyebrow">THREE.JS / RIGID-BODY VISUALIZATION</div>
<h1>Size ≠ Mass</h1>
<p class="intro">
Geometry controls each block’s physical dimensions. The mass sliders are
independent, explicit assignments and determine the combined center of mass.
</p>
<section class="block-panel" style="--block-color: var(--a)">
<div class="block-heading">
<strong>Block A</strong>
<span id="aSummary" class="block-summary"></span>
</div>
<div class="control-section-title">Physical size · meters</div>
<div class="control">
<div class="control-title"><span>Width</span><output id="aWOut"></output></div>
<input id="aW" type="range" min="1.2" max="4" step="0.1" value="2.8"
data-block="A" data-field="w">
</div>
<div class="control">
<div class="control-title"><span>Height</span><output id="aHOut"></output></div>
<input id="aH" type="range" min="0.6" max="2.6" step="0.1" value="1.8"
data-block="A" data-field="h">
</div>
<div class="control">
<div class="control-title"><span>Depth</span><output id="aDOut"></output></div>
<input id="aD" type="range" min="0.8" max="2.4" step="0.1" value="1.6"
data-block="A" data-field="d">
</div>
<div class="control-section-title">Explicitly assigned mass</div>
<div class="control mass-control">
<div class="control-title"><span>Mass</span><output id="aMassOut"></output></div>
<input id="aMass" type="range" min="1" max="30" step="0.5" value="6"
data-block="A" data-field="mass">
</div>
</section>
<section class="block-panel" style="--block-color: var(--b)">
<div class="block-heading">
<strong>Block B</strong>
<span id="bSummary" class="block-summary"></span>
</div>
<div class="control-section-title">Physical size · meters</div>
<div class="control">
<div class="control-title"><span>Width</span><output id="bWOut"></output></div>
<input id="bW" type="range" min="1.2" max="4" step="0.1" value="1.8"
data-block="B" data-field="w">
</div>
<div class="control">
<div class="control-title"><span>Height</span><output id="bHOut"></output></div>
<input id="bH" type="range" min="0.6" max="2.6" step="0.1" value="1.2"
data-block="B" data-field="h">
</div>
<div class="control">
<div class="control-title"><span>Depth</span><output id="bDOut"></output></div>
<input id="bD" type="range" min="0.8" max="2.4" step="0.1" value="1.4"
data-block="B" data-field="d">
</div>
<div class="control-section-title">Explicitly assigned mass</div>
<div class="control mass-control">
<div class="control-title"><span>Mass</span><output id="bMassOut"></output></div>
<input id="bMass" type="range" min="1" max="30" step="0.5" value="14"
data-block="B" data-field="mass">
</div>
</section>
<div class="note">
The connecting rod has a fixed 1.40 m length and is treated as massless.
Each uniform block’s own center of mass is at its geometric center.
</div>
<div class="button-row">
<button id="resetCamera" type="button">Reset camera</button>
<button id="resetModel" type="button">Reset model</button>
</div>
</aside>
<section id="readout">
<div class="caption">Calculated system center of mass</div>
<strong id="comValue">—</strong>
<div id="massTotal">—</div>
<div class="formula">
r<sub>COM</sub> = (m<sub>A</sub>r<sub>A</sub> + m<sub>B</sub>r<sub>B</sub>)<br>
/ (m<sub>A</sub> + m<sub>B</sub>)
</div>
<div class="legend">
<span><i class="system-dot"></i> system COM</span>
<span><i class="center-dot"></i> block center</span>
</div>
</section>
<div id="hint">Drag to orbit · Scroll to zoom · Double-click to reset camera</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101722);
scene.fog = new THREE.Fog(0x101722, 13, 30);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb9d8ff, 0x18202c, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4, 9, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5fa8ff, 1.25);
rimLight.position.set(-6, 4, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa85c, 1.0, 15);
warmLight.position.set(4, 3, -3);
scene.add(warmLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x17212e,
roughness: 0.94,
metalness: 0.03
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 30, 0x38506b, 0x243247);
grid.position.y = 0.003;
grid.material.transparent = true;
grid.material.opacity = 0.5;
scene.add(grid);
// -------------------------------------------------------------------------
// Model state
// -------------------------------------------------------------------------
const ROD_LENGTH = 1.4;
const defaults = {
A: { w: 2.8, h: 1.8, d: 1.6, mass: 6 },
B: { w: 1.8, h: 1.2, d: 1.4, mass: 14 }
};
const state = {
A: { ...defaults.A },
B: { ...defaults.B }
};
const colorA = 0x28c7aa;
const colorB = 0xff8a4c;
const comColor = 0xffd447;
const materialA = new THREE.MeshStandardMaterial({
color: colorA,
roughness: 0.62,
metalness: 0.08,
flatShading: true
});
const materialB = new THREE.MeshStandardMaterial({
color: colorB,
roughness: 0.62,
metalness: 0.08,
flatShading: true
});
const edgeMaterialA = new THREE.LineBasicMaterial({
color: 0xbffbed,
transparent: true,
opacity: 0.42
});
const edgeMaterialB = new THREE.LineBasicMaterial({
color: 0xffd2b7,
transparent: true,
opacity: 0.42
});
const blockA = new THREE.Group();
const blockB = new THREE.Group();
scene.add(blockA, blockB);
function rebuildBlock(group, config, material, edgeMaterial) {
const signature = `${config.w}|${config.h}|${config.d}`;
if (group.userData.signature === signature) return;
for (const child of [...group.children]) {
child.geometry?.dispose();
group.remove(child);
}
const geometry = new THREE.BoxGeometry(
config.w,
config.h,
config.d
);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
);
group.add(edges);
group.userData.signature = signature;
}
// Massless, fixed-length rod.
const rodMaterial = new THREE.MeshStandardMaterial({
color: 0xc6d2df,
roughness: 0.25,
metalness: 0.8,
flatShading: true
});
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, ROD_LENGTH + 0.22, 10),
rodMaterial
);
rod.rotation.z = Math.PI / 2;
rod.castShadow = true;
scene.add(rod);
const jointGeometry = new THREE.IcosahedronGeometry(0.13, 1);
const jointMaterial = new THREE.MeshStandardMaterial({
color: 0x60748c,
roughness: 0.3,
metalness: 0.72,
flatShading: true
});
const jointLeft = new THREE.Mesh(jointGeometry, jointMaterial);
const jointRight = new THREE.Mesh(jointGeometry, jointMaterial);
jointLeft.castShadow = true;
jointRight.castShadow = true;
scene.add(jointLeft, jointRight);
// Individual geometric centers of the uniform blocks.
const individualMaterial = new THREE.MeshBasicMaterial({
color: 0xe6f4ff,
depthTest: false,
transparent: true,
opacity: 0.9
});
const individualA = new THREE.Mesh(
new THREE.OctahedronGeometry(0.085, 0),
individualMaterial
);
const individualB = new THREE.Mesh(
new THREE.OctahedronGeometry(0.085, 0),
individualMaterial
);
individualA.renderOrder = 20;
individualB.renderOrder = 20;
scene.add(individualA, individualB);
// Combined system center of mass.
const comMarker = new THREE.Group();
const comSphere = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.15, 1),
new THREE.MeshBasicMaterial({
color: comColor,
depthTest: false
})
);
comSphere.renderOrder = 30;
const comRing = new THREE.Mesh(
new THREE.TorusGeometry(0.21, 0.018, 6, 28),
new THREE.MeshBasicMaterial({
color: 0xfff1a0,
depthTest: false,
transparent: true,
opacity: 0.9
})
);
comRing.renderOrder = 30;
comMarker.add(comSphere, comRing);
scene.add(comMarker);
const comStem = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineDashedMaterial({
color: comColor,
dashSize: 0.09,
gapSize: 0.06,
transparent: true,
opacity: 0.8
})
);
scene.add(comStem);
const groundRing = new THREE.Mesh(
new THREE.TorusGeometry(0.18, 0.024, 6, 28),
new THREE.MeshBasicMaterial({
color: comColor,
transparent: true,
opacity: 0.85
})
);
groundRing.rotation.x = -Math.PI / 2;
groundRing.position.y = 0.025;
scene.add(groundRing);
const labelA = document.getElementById("labelA");
const labelB = document.getElementById("labelB");
const labelCom = document.getElementById("labelCom");
labelA.style.setProperty("--block-color", "#28c7aa");
labelB.style.setProperty("--block-color", "#ff8a4c");
let centerA = new THREE.Vector3();
let centerB = new THREE.Vector3();
let systemCOM = new THREE.Vector3();
function formatNumber(value) {
return Number(value).toFixed(2);
}
function updateScene() {
rebuildBlock(blockA, state.A, materialA, edgeMaterialA);
rebuildBlock(blockB, state.B, materialB, edgeMaterialB);
// The rod's inner endpoints stay fixed. Changing block dimensions makes
// the blocks move outward while preserving the rigid rod length.
centerA.set(
-ROD_LENGTH / 2 - state.A.w / 2,
state.A.h / 2,
0
);
centerB.set(
ROD_LENGTH / 2 + state.B.w / 2,
state.B.h / 2,
0
);
blockA.position.copy(centerA);
blockB.position.copy(centerB);
individualA.position.copy(centerA);
individualB.position.copy(centerB);
const totalMass = state.A.mass + state.B.mass;
// Weighted center of mass of the two uniform blocks.
// The connecting rod is explicitly treated as massless.
systemCOM.copy(centerA).multiplyScalar(state.A.mass / totalMass);
systemCOM.addScaledVector(centerB, state.B.mass / totalMass);
comMarker.position.copy(systemCOM);
groundRing.position.set(systemCOM.x, 0.025, systemCOM.z);
const stemGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(systemCOM.x, 0.02, systemCOM.z),
systemCOM.clone()
]);
comStem.geometry.dispose();
comStem.geometry = stemGeometry;
comStem.computeLineDistances();
// Keep the rod within the vertical extent of both blocks.
const attachmentHeight = Math.min(state.A.h, state.B.h) * 0.58;
rod.position.set(0, attachmentHeight, 0);
jointLeft.position.set(-ROD_LENGTH / 2, attachmentHeight, 0);
jointRight.position.set(ROD_LENGTH / 2, attachmentHeight, 0);
// Update interface values.
document.getElementById("aWOut").textContent =
`${formatNumber(state.A.w)} m`;
document.getElementById("aHOut").textContent =
`${formatNumber(state.A.h)} m`;
document.getElementById("aDOut").textContent =
`${formatNumber(state.A.d)} m`;
document.getElementById("aMassOut").textContent =
`${formatNumber(state.A.mass)} kg`;
document.getElementById("bWOut").textContent =
`${formatNumber(state.B.w)} m`;
document.getElementById("bHOut").textContent =
`${formatNumber(state.B.h)} m`;
document.getElementById("bDOut").textContent =
`${formatNumber(state.B.d)} m`;
document.getElementById("bMassOut").textContent =
`${formatNumber(state.B.mass)} kg`;
const volumeA = state.A.w * state.A.h * state.A.d;
const volumeB = state.B.w * state.B.h * state.B.d;
document.getElementById("aSummary").textContent =
`${formatNumber(volumeA)} m³ · ${formatNumber(state.A.mass)} kg assigned`;
document.getElementById("bSummary").textContent =
`${formatNumber(volumeB)} m³ · ${formatNumber(state.B.mass)} kg assigned`;
document.getElementById("comValue").textContent =
`(${formatNumber(systemCOM.x)}, ${formatNumber(systemCOM.y)}, ` +
`${formatNumber(systemCOM.z)}) m`;
document.getElementById("massTotal").textContent =
`${formatNumber(totalMass)} kg total · 1.40 m massless rod`;
labelA.innerHTML = `
<span class="dot"></span>
<div>
<b>Block A</b>
<small>${formatNumber(state.A.mass)} kg assigned ·
${formatNumber(state.A.w)} × ${formatNumber(state.A.h)} × ${formatNumber(state.A.d)} m</small>
</div>
`;
labelB.innerHTML = `
<span class="dot"></span>
<div>
<b>Block B</b>
<small>${formatNumber(state.B.mass)} kg assigned ·
${formatNumber(state.B.w)} × ${formatNumber(state.B.h)} × ${formatNumber(state.B.d)} m</small>
</div>
`;
labelCom.innerHTML = `
<span>SYSTEM COM</span>
<strong>
x ${formatNumber(systemCOM.x)} ·
y ${formatNumber(systemCOM.y)} ·
z ${formatNumber(systemCOM.z)} m
</strong>
`;
}
// -------------------------------------------------------------------------
// Controls
// -------------------------------------------------------------------------
const rangeInputs = [...document.querySelectorAll("input[type='range']")];
for (const input of rangeInputs) {
input.addEventListener("input", () => {
const { block, field } = input.dataset;
state[block][field] = Number.parseFloat(input.value);
updateScene();
});
}
document.getElementById("resetModel").addEventListener("click", () => {
for (const key of ["A", "B"]) {
Object.assign(state[key], defaults[key]);
}
for (const input of rangeInputs) {
const { block, field } = input.dataset;
input.value = state[block][field];
}
updateScene();
});
// -------------------------------------------------------------------------
// Custom camera controls: no Three.js addons are used.
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.75, 0);
let cameraYaw = 0.58;
let cameraPitch = 0.31;
let cameraDistance = 11.2;
function updateCamera() {
const horizontal = Math.cos(cameraPitch) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraYaw = 0.58;
cameraPitch = 0.31;
cameraDistance = 11.2;
cameraTarget.set(0, 0.75, 0);
updateCamera();
}
document.getElementById("resetCamera").addEventListener("click", resetCamera);
const canvas = renderer.domElement;
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.005,
-0.12,
1.15
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance + event.deltaY * 0.008,
5.5,
18
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// HTML label projection
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
placeLabel(
labelA,
new THREE.Vector3(
centerA.x,
centerA.y + state.A.h / 2 + 0.17,
centerA.z
)
);
placeLabel(
labelB,
new THREE.Vector3(
centerB.x,
centerB.y + state.B.h / 2 + 0.17,
centerB.z
)
);
placeLabel(
labelCom,
systemCOM.clone().add(new THREE.Vector3(0, 0.34, 0))
);
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateScene();
updateCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
const pulse = 1 + Math.sin(time * 3.2) * 0.07;
comMarker.scale.setScalar(pulse);
comRing.rotation.z = time * 0.45;
renderer.render(scene, camera);
updateLabels();
}
animate();
</script>
</body>
</html>
```