Prompt specification
Build or customize the Three.js 3D scene "Heavy-low storage comparison" 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: Heavy-low storage comparison
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/heavy-low-storage-comparison-x0226
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing two identical low-poly shelving units on matched tilting platforms. Give both shelves the same set of equal-sized blocks with explicit masses, but arrange the heavier blocks low on one shelf and high on the other. Treat each loaded shelf as a rigidly joined assembly for this simplified balance comparison. As both platforms tilt together, show each combined center of mass and its vertical projection relative to the supporting footprint. Mark the first angle at which that projection leaves the footprint.
Working Three.js Starter (heavy-low-storage-comparison-x0226):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rigid Shelf Tipping Comparison</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 20, 35, 0.91);
--border: rgba(155, 195, 225, 0.19);
--text: #edf6ff;
--muted: #9eb2c8;
--cyan: #55d9ff;
--green: #52e3ad;
--red: #ff647c;
--gold: #ffd166;
}
* {
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 {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
top: 16px;
left: 16px;
width: min(390px, calc(100vw - 32px));
max-height: calc(100vh - 32px);
overflow: auto;
padding: 18px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(15px);
}
h1 {
margin: 0;
font-size: 21px;
line-height: 1.2;
letter-spacing: -0.02em;
}
.subtitle {
margin: 7px 0 16px;
color: var(--muted);
font-size: 12.5px;
line-height: 1.5;
}
.control-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
font-size: 13px;
}
.angle-readout {
color: var(--gold);
font-variant-numeric: tabular-nums;
font-weight: 750;
font-size: 17px;
}
input[type="range"] {
width: 100%;
margin: 0;
accent-color: var(--cyan);
cursor: pointer;
}
.timeline {
position: relative;
height: 25px;
margin: 8px 3px 14px;
border-top: 1px solid rgba(255, 255, 255, 0.13);
}
.timeline-fill {
position: absolute;
top: -2px;
left: 0;
height: 3px;
width: 0%;
background: linear-gradient(90deg, var(--cyan), var(--gold));
box-shadow: 0 0 10px rgba(85, 217, 255, 0.4);
}
.timeline-current {
position: absolute;
top: -6px;
left: 0%;
width: 2px;
height: 15px;
background: white;
transform: translateX(-1px);
}
.tick {
position: absolute;
top: -6px;
width: 2px;
height: 14px;
background: var(--gold);
}
.tick b {
position: absolute;
top: -14px;
left: 50%;
transform: translateX(-50%);
font-size: 9px;
}
.tick.b {
background: var(--red);
}
.buttons {
display: flex;
gap: 8px;
margin-bottom: 15px;
}
button {
appearance: none;
border: 1px solid rgba(85, 217, 255, 0.26);
border-radius: 9px;
padding: 8px 12px;
background: rgba(85, 217, 255, 0.1);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
}
button:hover {
background: rgba(85, 217, 255, 0.18);
}
button.primary {
flex: 1;
border-color: rgba(85, 217, 255, 0.5);
background: rgba(85, 217, 255, 0.17);
}
.section-title {
margin: 15px 0 8px;
color: #c8d8e8;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.result-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.result-card {
min-width: 0;
padding: 11px;
border: 1px solid rgba(82, 227, 173, 0.26);
border-radius: 11px;
background: rgba(82, 227, 173, 0.065);
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.result-card:hover {
transform: translateY(-1px);
border-color: rgba(255, 209, 102, 0.65);
}
.result-card.unsafe {
border-color: rgba(255, 100, 124, 0.62);
background: rgba(255, 100, 124, 0.09);
}
.result-card h2 {
margin: 0 0 6px;
font-size: 13px;
}
.big-number {
margin: 3px 0 7px;
color: var(--gold);
font-size: 24px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.unsafe .big-number {
color: var(--red);
}
.result-line {
display: flex;
justify-content: space-between;
gap: 8px;
margin-top: 4px;
color: var(--muted);
font-size: 10.5px;
}
.result-line strong {
color: var(--text);
font-variant-numeric: tabular-nums;
}
.status {
display: inline-block;
margin-top: 8px;
padding: 3px 6px;
border-radius: 5px;
background: rgba(82, 227, 173, 0.14);
color: var(--green);
font-size: 9.5px;
font-weight: 850;
letter-spacing: 0.07em;
}
.status.edge {
background: rgba(255, 209, 102, 0.15);
color: var(--gold);
}
.status.outside {
background: rgba(255, 100, 124, 0.16);
color: var(--red);
}
.mass-table {
width: 100%;
border-collapse: collapse;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.mass-table th,
.mass-table td {
padding: 7px 5px;
border-bottom: 1px solid rgba(255, 255, 255, 0.07);
text-align: center;
}
.mass-table th:first-child,
.mass-table td:first-child {
text-align: left;
}
.mass-table th {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.note {
margin: 12px 0 0;
color: var(--muted);
font-size: 10.5px;
line-height: 1.55;
}
.note strong {
color: #d9e7f4;
}
.help {
position: fixed;
right: 15px;
bottom: 13px;
z-index: 5;
padding: 7px 10px;
border: 1px solid var(--border);
border-radius: 8px;
background: rgba(7, 17, 31, 0.72);
color: var(--muted);
font-size: 10.5px;
pointer-events: none;
}
@media (max-width: 700px) {
.panel {
top: auto;
bottom: 10px;
left: 10px;
width: calc(100vw - 20px);
max-height: 48vh;
padding: 13px;
}
h1 {
font-size: 17px;
}
.subtitle {
margin-bottom: 10px;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<aside class="panel">
<h1>Rigid Shelf Tipping Comparison</h1>
<p class="subtitle">
Two identical loaded shelves on matched platforms. The block set and
horizontal positions are identical; only the vertical order changes.
</p>
<div class="control-header">
<label for="angle">Common platform tilt</label>
<span class="angle-readout" id="angleValue">0.0°</span>
</div>
<input
id="angle"
type="range"
min="0"
max="75"
step="0.1"
value="0"
aria-label="Common platform tilt angle"
/>
<div class="timeline" aria-label="First-exit angle timeline">
<div class="timeline-fill" id="timelineFill"></div>
<div class="timeline-current" id="timelineCurrent"></div>
<span class="tick" id="tickA"><b>A</b></span>
<span class="tick b" id="tickB"><b>B</b></span>
</div>
<div class="buttons">
<button class="primary" id="playButton">▶ Animate tilt</button>
<button id="resetButton">Reset</button>
</div>
<div class="section-title">First projection exit</div>
<div class="result-grid">
<div class="result-card" id="cardA" title="Click to show the first exit angle">
<h2>A · Heavy blocks low</h2>
<div class="big-number" id="criticalA">—</div>
<div class="result-line">
<span>COM height</span>
<strong id="heightA">—</strong>
</div>
<div class="result-line">
<span>Projection x</span>
<strong id="projectionA">—</strong>
</div>
<span class="status" id="statusA">INSIDE</span>
</div>
<div class="result-card" id="cardB" title="Click to show the first exit angle">
<h2>B · Heavy blocks high</h2>
<div class="big-number" id="criticalB">—</div>
<div class="result-line">
<span>COM height</span>
<strong id="heightB">—</strong>
</div>
<div class="result-line">
<span>Projection x</span>
<strong id="projectionB">—</strong>
</div>
<span class="status" id="statusB">INSIDE</span>
</div>
</div>
<div class="section-title">Identical block set</div>
<table class="mass-table">
<thead>
<tr>
<th>Tier</th>
<th>A bottom→top</th>
<th>B bottom→top</th>
</tr>
</thead>
<tbody>
<tr>
<td>Top</td>
<td>6, 4 kg</td>
<td>30, 26 kg</td>
</tr>
<tr>
<td>3</td>
<td>14, 10 kg</td>
<td>22, 18 kg</td>
</tr>
<tr>
<td>2</td>
<td>22, 18 kg</td>
<td>14, 10 kg</td>
</tr>
<tr>
<td>Bottom</td>
<td>30, 26 kg</td>
<td>6, 4 kg</td>
</tr>
</tbody>
</table>
<p class="note">
Each shelf also includes a <strong>12 kg frame</strong>, giving
<strong>142 kg</strong> per loaded shelf. The gold octahedron is the
combined center of mass of the frame and blocks. Its vertical plumb
projection is compared with the platform’s
<strong>4.80 m × 1.70 m</strong> supporting footprint.
Platform mass is excluded. Click either result card to jump to its
marked first-exit angle.
</p>
</aside>
<div class="help">Drag to orbit · Scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Physical comparison data
// ---------------------------------------------------------------------
const DEG = Math.PI / 180;
const MAX_ANGLE = 75;
const PLATFORM_LENGTH = 4.80;
const PLATFORM_DEPTH = 1.70;
const HALF_LENGTH = PLATFORM_LENGTH / 2;
const HALF_DEPTH = PLATFORM_DEPTH / 2;
const TOP_Y = 0.12; // Local height of the supporting top surface.
const AXLE_HEIGHT = 2.45;
const FRAME_MASS = 12;
const FRAME_COM_HEIGHT = 1.55; // Above platform top.
const BLOCK_SIZE = 0.42;
const TIER_HEIGHTS = [0.50, 1.30, 2.10, 2.90];
// Distinct masses: 30, 26, 22, 18, 14, 10, 6, 4 kg.
const MASS_SET = [30, 26, 22, 18, 14, 10, 6, 4];
// Base horizontal slots. A common offset below makes the weighted
// horizontal center of mass exactly zero for both assemblies.
const BASE_SLOT = {
30: -0.71,
26: -0.24,
22: 0.24,
18: 0.71,
14: -1.18,
10: -1.65,
6: 1.18,
4: 1.65
};
const BASE_WEIGHTED_SUM = MASS_SET.reduce(
(sum, mass) => sum + mass * BASE_SLOT[mass],
0
);
const MASS_TOTAL = MASS_SET.reduce((sum, mass) => sum + mass, 0);
const HORIZONTAL_OFFSET = -BASE_WEIGHTED_SUM / MASS_TOTAL;
const MASS_X = Object.fromEntries(
MASS_SET.map((mass) => [
mass,
BASE_SLOT[mass] + HORIZONTAL_OFFSET
])
);
const shelfConfigs = [
{
id: "A",
title: "A · HEAVY LOW",
tiers: [
[30, 26],
[22, 18],
[14, 10],
[6, 4]
]
},
{
id: "B",
title: "B · HEAVY HIGH",
tiers: [
[6, 4],
[14, 10],
[22, 18],
[30, 26]
]
}
];
function calculateCOM(config) {
let totalMass = FRAME_MASS;
let mx = 0;
let my = 0;
let mz = 0;
config.tiers.forEach((tier, tierIndex) => {
for (const mass of tier) {
const x = MASS_X[mass];
const y = TIER_HEIGHTS[tierIndex];
totalMass += mass;
mx += mass * x;
my += mass * y;
mz += mass * 0;
}
});
// Add the shelf frame.
mx += FRAME_MASS * 0;
my += FRAME_MASS * FRAME_COM_HEIGHT;
mz += FRAME_MASS * 0;
return {
x: mx / totalMass,
h: my / totalMass,
z: mz / totalMass,
totalMass
};
}
shelfConfigs.forEach((config) => {
config.com = calculateCOM(config);
// Projection moves toward local -X as the platform rises toward +X:
// xProjection = xCOM - hCOM * tan(angle).
// First exit occurs at xProjection = -HALF_LENGTH.
config.criticalAngle =
Math.atan2(HALF_LENGTH + config.com.x, config.com.h) / DEG;
});
// ---------------------------------------------------------------------
// Renderer, camera, lighting
// ---------------------------------------------------------------------
const container = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 18, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
// Camera orbit is implemented directly, without Three.js addons.
const cameraTarget = new THREE.Vector3(0, 2.35, 0);
const orbit = {
azimuth: 0.34,
polar: 1.16,
radius: 15.2
};
function updateCamera() {
const sinPolar = Math.sin(orbit.polar);
camera.position.set(
cameraTarget.x +
orbit.radius * sinPolar * Math.sin(orbit.azimuth),
cameraTarget.y + orbit.radius * Math.cos(orbit.polar),
cameraTarget.z +
orbit.radius * sinPolar * Math.cos(orbit.azimuth)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0x9ecbff, 0x172033, 1.8);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2dc, 3.4);
sun.position.set(-5, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x59bfff, 1.5);
rimLight.position.set(8, 5, -7);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(34, 24),
new THREE.MeshStandardMaterial({
color: 0x0a1626,
roughness: 0.95,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(
28,
28,
0x24445e,
0x152a3e
);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.72;
scene.add(grid);
// ---------------------------------------------------------------------
// Geometry and texture helpers
// ---------------------------------------------------------------------
function makeCanvasTexture(text, options = {}) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
const radius = 18;
const x = 4;
const y = 4;
const width = canvas.width - 8;
const height = canvas.height - 8;
context.beginPath();
context.moveTo(x + radius, y);
context.arcTo(x + width, y, x + width, y + height, radius);
context.arcTo(x + width, y + height, x, y + height, radius);
context.arcTo(x, y + height, x, y, radius);
context.arcTo(x, y, x + width, y, radius);
context.closePath();
context.fillStyle = options.background ?? "rgba(6, 15, 27, 0.90)";
context.fill();
context.strokeStyle = options.border ?? "rgba(255,255,255,0.22)";
context.lineWidth = 3;
context.stroke();
context.fillStyle = options.color ?? "#ffffff";
context.font =
options.bold === false
? "600 48px system-ui, sans-serif"
: "800 48px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
return texture;
}
function makeSprite(
text,
width = 1.5,
height = 0.34,
options = {}
) {
const texture = makeCanvasTexture(text, options);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: options.depthTest ?? true
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
return sprite;
}
function addEdges(mesh, color = 0x07111f, opacity = 0.72) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 24),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(edges);
return mesh;
}
function boxMesh(width, height, depth, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function lineFrom(points, material, parent) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, material);
parent.add(line);
return line;
}
const massColors = {
30: 0xff6874,
20: 0xffa653,
14: 0x4fd1b5,
10: 0x54c7ec,
6: 0x9d8cff,
4: 0xe8b85c,
26: 0xff8068,
22: 0xffb45e,
18: 0x70d9b9,
8: 0x69ccef,
7: 0xa898ff,
5: 0xe6b960
};
// ---------------------------------------------------------------------
// One comparison unit
// ---------------------------------------------------------------------
function buildComparisonUnit(config, worldX) {
const group = new THREE.Group();
group.position.set(worldX, AXLE_HEIGHT, 0);
scene.add(group);
// Fixed test-rig pedestal and axle.
const rigMaterial = new THREE.MeshStandardMaterial({
color: 0x223247,
roughness: 0.7,
metalness: 0.55,
flatShading: true
});
const base = boxMesh(1.35, 0.16, 1.75, rigMaterial);
base.position.set(worldX, 0.08, 0);
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
const column = boxMesh(0.52, 2.28, 0.52, rigMaterial);
column.position.set(worldX, 1.22, 0);
column.castShadow = true;
column.receiveShadow = true;
scene.add(column);
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 2.02, 10),
new THREE.MeshStandardMaterial({
color: 0x8094a9,
roughness: 0.35,
metalness: 0.8,
flatShading: true
})
);
axle.rotation.x = Math.PI / 2;
axle.position.set(worldX, AXLE_HEIGHT, 0);
axle.castShadow = true;
scene.add(axle);
// Tilting platform. Its local top surface is y = TOP_Y.
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x233a54,
roughness: 0.62,
metalness: 0.42,
flatShading: true
});
const platform = boxMesh(
PLATFORM_LENGTH,
0.24,
PLATFORM_DEPTH,
platformMaterial
);
addEdges(platform, 0x07111f, 0.78);
group.add(platform);
// Supporting footprint outline.
const footprintMaterial = new THREE.LineBasicMaterial({
color: 0x55d9ff,
transparent: true,
opacity: 0.95
});
const footprint = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-HALF_LENGTH, TOP_Y + 0.008, -HALF_DEPTH),
new THREE.Vector3(HALF_LENGTH, TOP_Y + 0.008, -HALF_DEPTH),
new THREE.Vector3(HALF_LENGTH, TOP_Y + 0.008, HALF_DEPTH),
new THREE.Vector3(-HALF_LENGTH, TOP_Y + 0.008, HALF_DEPTH)
]),
footprintMaterial
);
group.add(footprint);
// Mark the local edge where the vertical projection first exits.
const exitMaterial = new THREE.LineBasicMaterial({
color: config.id === "A" ? 0xffd166 : 0xff647c,
transparent: true,
opacity: 0.95
});
const exitEdge = lineFrom(
[
new THREE.Vector3(-HALF_LENGTH, TOP_Y + 0.016, -HALF_DEPTH + 0.08),
new THREE.Vector3(-HALF_LENGTH, TOP_Y + 0.016, HALF_DEPTH - 0.08)
],
exitMaterial,
group
);
const exitLabel = makeSprite(
`${config.id} · FIRST EXIT ${config.criticalAngle.toFixed(1)}°`,
1.72,
0.29,
{
color: config.id === "A" ? "#ffd166" : "#ff8ba0",
border:
config.id === "A"
? "rgba(255,209,102,0.72)"
: "rgba(255,100,124,0.72)",
depthTest: false
}
);
exitLabel.position.set(
-HALF_LENGTH - 0.12,
TOP_Y + 0.24,
HALF_DEPTH - 0.05
);
exitLabel.renderOrder = 20;
group.add(exitLabel);
// Shelf frame.
const frameMaterial = new THREE.MeshStandardMaterial({
color: config.id === "A" ? 0x506b7f : 0x536b80,
roughness: 0.72,
metalness: 0.28,
flatShading: true
});
const shelfDepth = 1.25;
const shelfWidth = 4.15;
const postHeight = 3.43;
const postY = TOP_Y + postHeight / 2;
for (const x of [-2.0, 2.0]) {
for (const z of [-0.55, 0.55]) {
const post = boxMesh(0.10, postHeight, 0.10, frameMaterial);
post.position.set(x, postY, z);
addEdges(post, 0x07111f, 0.78);
group.add(post);
}
}
const boardRelativeHeights = [0.17, 0.97, 1.77, 2.57];
for (const relativeY of boardRelativeHeights) {
const board = boxMesh(
shelfWidth,
0.08,
shelfDepth,
frameMaterial
);
board.position.set(0, TOP_Y + relativeY, 0);
addEdges(board, 0x07111f, 0.78);
group.add(board);
}
// Top rail.
const topRail = boxMesh(shelfWidth, 0.10, 0.10, frameMaterial);
topRail.position.set(0, 3.55, -0.55);
addEdges(topRail, 0x07111f, 0.78);
group.add(topRail);
// Shelf feet.
for (const x of [-1.82, 1.82]) {
for (const z of [-0.48, 0.48]) {
const foot = boxMesh(0.24, 0.10, 0.22, frameMaterial);
foot.position.set(x, TOP_Y + 0.05, z);
addEdges(foot, 0x07111f, 0.75);
group.add(foot);
}
}
// Equal-sized blocks, each with an explicit mass label.
config.tiers.forEach((tier, tierIndex) => {
tier.forEach((mass, blockIndex) => {
const x = MASS_X[mass];
const y = TOP_Y + TIER_HEIGHTS[tierIndex];
const blockMaterial = new THREE.MeshStandardMaterial({
color: massColors[mass] ?? 0x70c7ef,
roughness: 0.55,
metalness: 0.08,
flatShading: true
});
const block = boxMesh(
BLOCK_SIZE,
BLOCK_SIZE,
BLOCK_SIZE,
blockMaterial
);
block.position.set(x, y, 0);
addEdges(block, 0x07111f, 0.85);
group.add(block);
const label = makeSprite(
`${mass} kg`,
0.53,
0.15,
{
background: "rgba(4, 12, 23, 0.88)",
border: "rgba(255,255,255,0.18)",
color: "#ffffff",
depthTest: true
}
);
label.position.set(
x,
y + 0.025,
0.34 + blockIndex * 0.025
);
group.add(label);
});
});
const titleLabel = makeSprite(
config.title,
2.12,
0.38,
{
color: config.id === "A" ? "#bfffe8" : "#ffd5dc",
border:
config.id === "A"
? "rgba(82,227,173,0.62)"
: "rgba(255,100,124,0.62)",
depthTest: false
}
);
titleLabel.position.set(0, 3.88, 0.10);
titleLabel.renderOrder = 20;
group.add(titleLabel);
// Combined center of mass of the rigid loaded shelf.
const comMesh = new THREE.Mesh(
new THREE.OctahedronGeometry(0.135, 0),
new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x7a4b00,
emissiveIntensity: 0.65,
roughness: 0.34,
metalness: 0.2,
flatShading: true
})
);
comMesh.castShadow = true;
group.add(comMesh);
const comLabel = makeSprite(
`${config.id} COM`,
0.83,
0.23,
{
color: "#ffe49a",
border: "rgba(255,209,102,0.65)",
depthTest: false
}
);
comLabel.renderOrder = 21;
group.add(comLabel);
// Projection marker lies in the platform's supporting plane.
const projectionMarker = new THREE.Mesh(
new THREE.RingGeometry(0.11, 0.18, 32),
new THREE.MeshBasicMaterial({
color: 0x52e3ad,
transparent: true,
opacity: 0.98,
side: THREE.DoubleSide,
depthTest: false
})
);
projectionMarker.rotation.x = -Math.PI / 2;
projectionMarker.renderOrder = 22;
group.add(projectionMarker);
// Vertical plumb line from COM to its vertical projection.
const plumbGeometry = new THREE.BufferGeometry();
plumbGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const plumbLine = new THREE.Line(
plumbGeometry,
new THREE.LineDashedMaterial({
color: 0xffd166,
dashSize: 0.11,
gapSize: 0.075,
transparent: true,
opacity: 0.92,
depthTest: false
})
);
plumbLine.frustumCulled = false;
plumbLine.renderOrder = 19;
scene.add(plumbLine);
return {
config,
group,
comMesh,
comLabel,
projectionMarker,
plumbLine,
exitEdge
};
}
const units = [
buildComparisonUnit(shelfConfigs[0], -3.35),
buildComparisonUnit(shelfConfigs[1], 3.35)
];
// ---------------------------------------------------------------------
// UI and dynamic projection calculation
// ---------------------------------------------------------------------
const angleInput = document.getElementById("angle");
const angleValue = document.getElementById("angleValue");
const playButton = document.getElementById("playButton");
const resetButton = document.getElementById("resetButton");
const timelineFill = document.getElementById("timelineFill");
const timelineCurrent = document.getElementById("timelineCurrent");
const tickA = document.getElementById("tickA");
const tickB = document.getElementById("tickB");
const cardA = document.getElementById("cardA");
const cardB = document.getElementById("cardB");
tickA.style.left = `${shelfConfigs[0].criticalAngle / MAX_ANGLE * 100}%`;
tickB.style.left = `${shelfConfigs[1].criticalAngle / MAX_ANGLE * 100}%`;
document.getElementById("criticalA").textContent =
`${shelfConfigs[0].criticalAngle.toFixed(1)}°`;
document.getElementById("criticalB").textContent =
`${shelfConfigs[1].criticalAngle.toFixed(1)}°`;
document.getElementById("heightA").textContent =
`${shelfConfigs[0].com.h.toFixed(3)} m`;
document.getElementById("heightB").textContent =
`${shelfConfigs[1].com.h.toFixed(3)} m`;
let angle = 0;
let playing = false;
let animationDirection = 1;
let previousTime = performance.now();
function setAngle(value) {
angle = THREE.MathUtils.clamp(value, 0, MAX_ANGLE);
angleInput.value = angle.toFixed(1);
updateComparison();
}
function updateUnit(unit) {
const { config, group } = unit;
const radians = angle * DEG;
group.rotation.z = radians;
const comLocal = new THREE.Vector3(
config.com.x,
TOP_Y + config.com.h,
config.com.z
);
// Intersect the global vertical line through the COM with the
// tilted platform plane. In platform-local coordinates:
// xProjection = xCOM - hCOM * tan(angle).
const projectionX =
config.com.x - config.com.h * Math.tan(radians);
const projectionLocal = new THREE.Vector3(
projectionX,
TOP_Y,
config.com.z
);
unit.comMesh.position.copy(comLocal);
unit.comLabel.position.set(
comLocal.x + 0.18,
comLocal.y + 0.25,
comLocal.z + 0.08
);
unit.projectionMarker.position.set(
projectionX,
TOP_Y + 0.025,
config.com.z
);
group.updateMatrixWorld(true);
const comWorld = group.localToWorld(comLocal.clone());
const projectionWorld = group.localToWorld(projectionLocal.clone());
const positions = unit.plumbLine.geometry.attributes.position;
positions.setXYZ(0, comWorld.x, comWorld.y, comWorld.z);
positions.setXYZ(
1,
projectionWorld.x,
projectionWorld.y,
projectionWorld.z
);
positions.needsUpdate = true;
unit.plumbLine.computeLineDistances();
const outside = angle > config.criticalAngle + 0.001;
const atEdge =
!outside &&
Math.abs(angle - config.criticalAngle) < 0.06;
const markerColor = outside ? 0xff647c : atEdge ? 0xffd166 : 0x52e3ad;
unit.projectionMarker.material.color.setHex(markerColor);
const suffix = config.id;
const card = suffix === "A" ? cardA : cardB;
const status = document.getElementById(`status${suffix}`);
const projectionText = document.getElementById(`projection${suffix}`);
card.classList.toggle("unsafe", outside);
projectionText.textContent = `${projectionX.toFixed(3)} m`;
if (outside) {
status.textContent = "OUTSIDE";
status.className = "status outside";
} else if (atEdge) {
status.textContent = "AT EDGE";
status.className = "status edge";
} else {
status.textContent = "INSIDE";
status.className = "status";
}
}
function updateComparison() {
angleValue.textContent = `${angle.toFixed(1)}°`;
const fraction = angle / MAX_ANGLE;
timelineFill.style.width = `${fraction * 100}%`;
timelineCurrent.style.left = `${fraction * 100}%`;
units.forEach(updateUnit);
}
angleInput.addEventListener("input", () => {
playing = false;
playButton.textContent = "▶ Animate tilt";
setAngle(Number(angleInput.value));
});
playButton.addEventListener("click", () => {
playing = !playing;
if (playing && angle >= MAX_ANGLE - 0.01) {
angle = 0;
animationDirection = 1;
}
playButton.textContent = playing ? "❚❚ Pause" : "▶ Animate tilt";
});
resetButton.addEventListener("click", () => {
playing = false;
animationDirection = 1;
playButton.textContent = "▶ Animate tilt";
setAngle(0);
});
cardA.addEventListener("click", () => {
playing = false;
playButton.textContent = "▶ Animate tilt";
setAngle(shelfConfigs[0].criticalAngle);
});
cardB.addEventListener("click", () => {
playing = false;
playButton.textContent = "▶ Animate tilt";
setAngle(shelfConfigs[1].criticalAngle);
});
// ---------------------------------------------------------------------
// Direct camera controls
// ---------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.azimuth -= dx * 0.006;
orbit.polar = THREE.MathUtils.clamp(
orbit.polar - dy * 0.005,
0.42,
1.46
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.012,
8.5,
24
);
updateCamera();
},
{ passive: false }
);
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", onResize);
function animate(now) {
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (playing) {
angle += animationDirection * dt * 8.0;
if (angle >= MAX_ANGLE) {
angle = MAX_ANGLE;
animationDirection = -1;
} else if (angle <= 0) {
angle = 0;
animationDirection = 1;
}
angleInput.value = angle.toFixed(1);
updateComparison();
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateComparison();
requestAnimationFrame(animate);
</script>
</body>
</html>
```