Prompt specification
Build or customize the Three.js 3D scene "Scale-factor volume explorer" 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: Scale-factor volume explorer
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/scale-factor-volume-explorer-x1760
Scene Description Prompt:
Please make a low-poly Three.js scene in one standalone HTML file using cdnjs. Let users uniformly scale a marked solid; show linear dimensions, surface area, and volume changing with the first, second, and third powers of scale.
Working Three.js Starter (scale-factor-volume-explorer-x1760):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Uniform Scale Lab — Three.js</title>
<style>
:root {
color-scheme: dark;
--bg: #08111f;
--panel: #101a2a;
--panel-2: #152235;
--border: #26364d;
--text: #eef5ff;
--muted: #96a8c0;
--accent: #ffbd59;
--accent-2: #65e6a6;
--x: #ff7d82;
--y: #65e6a6;
--z: #69a9ff;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
height: 100vh;
}
#viewport {
position: relative;
min-width: 0;
overflow: hidden;
background:
radial-gradient(circle at 45% 35%, #16263b 0%, #08111f 68%);
}
#viewport canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.scene-heading {
position: absolute;
top: 24px;
left: 26px;
z-index: 3;
pointer-events: none;
}
.eyebrow {
margin: 0 0 7px;
color: var(--accent);
font-size: 0.7rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.scene-heading h1 {
margin: 0;
font-size: clamp(1.45rem, 2.4vw, 2.25rem);
line-height: 1;
letter-spacing: -0.035em;
}
.scene-heading p:last-child {
margin: 10px 0 0;
color: var(--muted);
font-size: 0.83rem;
}
.view-tools {
position: absolute;
right: 18px;
bottom: 18px;
z-index: 5;
display: flex;
gap: 7px;
}
.icon-button {
width: 38px;
height: 38px;
border: 1px solid #34465f;
border-radius: 10px;
background: rgba(14, 25, 41, 0.88);
cursor: pointer;
font-size: 1.05rem;
backdrop-filter: blur(10px);
}
.icon-button:hover {
border-color: var(--accent);
color: var(--accent);
}
.scene-hint {
position: absolute;
left: 22px;
bottom: 20px;
z-index: 3;
max-width: calc(100% - 190px);
padding: 9px 12px;
border: 1px solid rgba(76, 99, 130, 0.36);
border-radius: 9px;
background: rgba(9, 17, 30, 0.72);
color: #a9bad0;
font-size: 0.74rem;
pointer-events: none;
backdrop-filter: blur(10px);
}
#labels {
position: absolute;
inset: 0;
z-index: 2;
overflow: hidden;
pointer-events: none;
}
.dimension {
position: absolute;
transform: translate(-50%, -50%);
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 7px;
background: rgba(7, 15, 27, 0.9);
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
font-size: 0.72rem;
font-weight: 800;
white-space: nowrap;
backdrop-filter: blur(6px);
}
.dimension.x {
color: var(--x);
}
.dimension.y {
color: var(--y);
}
.dimension.z {
color: var(--z);
}
.scene-label {
position: absolute;
transform: translate(-50%, -50%);
padding: 5px 8px;
border-radius: 7px;
background: rgba(7, 15, 27, 0.9);
color: var(--accent);
border: 1px solid rgba(255, 189, 89, 0.55);
font-size: 0.68rem;
font-weight: 750;
white-space: nowrap;
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.22);
}
.scene-label.reference {
color: #9eb0c8;
border-color: #53657c;
}
#panel {
z-index: 10;
overflow-y: auto;
padding: 27px 25px 30px;
background:
linear-gradient(180deg, rgba(255, 255, 255, 0.025), transparent 170px),
var(--panel);
border-left: 1px solid var(--border);
box-shadow: -18px 0 50px rgba(0, 0, 0, 0.15);
}
.panel-kicker {
color: var(--accent);
font-size: 0.69rem;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#panel h2 {
margin: 8px 0 9px;
font-size: 1.7rem;
line-height: 1.12;
letter-spacing: -0.035em;
}
.intro {
margin: 0 0 22px;
color: var(--muted);
font-size: 0.88rem;
line-height: 1.55;
}
.scale-control {
padding: 17px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel-2);
}
.scale-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
}
.scale-title label {
color: #cbd8e9;
font-size: 0.8rem;
font-weight: 700;
}
.scale-number {
display: flex;
align-items: baseline;
gap: 3px;
color: var(--accent);
}
.scale-number strong {
font-size: 1.55rem;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.scale-number span {
font-size: 0.9rem;
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 6px;
margin: 5px 0 14px;
border-radius: 999px;
appearance: none;
background:
linear-gradient(
90deg,
var(--accent) 0%,
var(--accent) var(--range-position, 33.33%),
#34445c var(--range-position, 33.33%),
#34445c 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 19px;
height: 19px;
border: 3px solid #172235;
border-radius: 50%;
appearance: none;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #172235;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent);
}
.quick-values {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
}
.quick-values button,
.text-button {
border: 1px solid #34445a;
border-radius: 8px;
background: #111d2e;
cursor: pointer;
font-size: 0.73rem;
font-weight: 750;
}
.quick-values button {
padding: 7px 3px;
}
.quick-values button:hover,
.quick-values button.active {
border-color: var(--accent);
color: var(--accent);
background: rgba(255, 189, 89, 0.08);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 24px 0 10px;
color: #c8d5e5;
font-size: 0.74rem;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.section-title span:last-child {
color: var(--muted);
font-size: 0.68rem;
font-weight: 650;
letter-spacing: 0;
text-transform: none;
}
.metric-card {
margin-bottom: 10px;
padding: 15px;
border: 1px solid var(--border);
border-radius: 13px;
background: rgba(21, 34, 53, 0.68);
}
.metric-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.metric-name {
color: #d8e3f1;
font-size: 0.78rem;
font-weight: 780;
}
.power-badge {
padding: 4px 7px;
border-radius: 999px;
color: var(--accent-2);
background: rgba(101, 230, 166, 0.1);
font-size: 0.66rem;
font-weight: 850;
white-space: nowrap;
}
.power-badge.area {
color: #ffbd59;
background: rgba(255, 189, 89, 0.1);
}
.power-badge.volume {
color: #83b7ff;
background: rgba(105, 169, 255, 0.11);
}
.dimension-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 12px;
}
.dimension-item {
padding: 9px 8px;
border: 1px solid #2d3d54;
border-radius: 9px;
background: #111d2e;
}
.dimension-item span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 0.64rem;
font-weight: 800;
}
.dimension-item strong {
font-size: 0.88rem;
font-variant-numeric: tabular-nums;
}
.dimension-item.x strong {
color: var(--x);
}
.dimension-item.y strong {
color: var(--y);
}
.dimension-item.z strong {
color: var(--z);
}
.metric-value {
margin-top: 11px;
font-size: 1.65rem;
font-weight: 800;
letter-spacing: -0.035em;
font-variant-numeric: tabular-nums;
}
.metric-value small {
margin-left: 4px;
color: var(--muted);
font-size: 0.78rem;
font-weight: 650;
letter-spacing: 0;
}
.metric-formula {
margin-top: 4px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.72rem;
}
.metric-change {
margin-top: 8px;
color: #b6c6da;
font-size: 0.7rem;
}
.power-chart {
padding: 15px;
border: 1px solid var(--border);
border-radius: 13px;
background: rgba(21, 34, 53, 0.58);
}
.power-row + .power-row {
margin-top: 14px;
}
.power-row-head {
display: flex;
justify-content: space-between;
gap: 10px;
margin-bottom: 6px;
color: #bfcee0;
font-size: 0.72rem;
}
.power-row-head b {
color: var(--text);
font-variant-numeric: tabular-nums;
}
.track {
position: relative;
height: 8px;
overflow: hidden;
border-radius: 999px;
background: #26364c;
}
.track::after {
content: "";
position: absolute;
top: -2px;
bottom: -2px;
left: 12.5%;
width: 1px;
background: rgba(255, 255, 255, 0.62);
}
.fill {
width: 12.5%;
height: 100%;
border-radius: inherit;
transition: width 120ms ease-out;
}
#fillLinear {
background: var(--accent-2);
}
#fillArea {
background: var(--accent);
}
#fillVolume {
background: var(--z);
}
.chart-note {
margin: 12px 0 0;
color: var(--muted);
font-size: 0.68rem;
line-height: 1.45;
}
.insight {
margin-top: 12px;
padding: 12px 13px;
border-left: 3px solid var(--accent);
border-radius: 4px 9px 9px 4px;
background: rgba(255, 189, 89, 0.07);
color: #cfdae8;
font-size: 0.76rem;
line-height: 1.5;
}
.reference-toggle {
display: flex;
align-items: center;
gap: 9px;
margin-top: 19px;
color: var(--muted);
font-size: 0.76rem;
cursor: pointer;
}
.reference-toggle input {
width: 16px;
height: 16px;
accent-color: var(--accent);
}
.footer-row {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 18px;
padding-top: 17px;
border-top: 1px solid var(--border);
}
.text-button {
padding: 8px 11px;
}
.text-button:hover {
border-color: var(--accent);
color: var(--accent);
}
.base-size {
color: var(--muted);
font-size: 0.69rem;
}
@media (max-width: 860px) {
body {
overflow: auto;
}
#app {
display: flex;
flex-direction: column;
height: auto;
min-height: 100vh;
}
#viewport {
height: 58vh;
min-height: 420px;
}
#panel {
overflow: visible;
border-top: 1px solid var(--border);
border-left: 0;
}
.scene-heading {
top: 18px;
left: 18px;
}
.scene-heading p:last-child {
display: none;
}
.scene-hint {
left: 12px;
bottom: 12px;
max-width: calc(100% - 125px);
font-size: 0.66rem;
}
.view-tools {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<main id="app">
<section id="viewport" aria-label="Interactive 3D scaled rectangular solid">
<div class="scene-heading">
<p class="eyebrow">Geometry Lab</p>
<h1>Uniform Scale Lab</h1>
<p>One scale factor transforms every edge.</p>
</div>
<div id="labels" aria-hidden="true">
<div id="labelX" class="dimension x"></div>
<div id="labelY" class="dimension y"></div>
<div id="labelZ" class="dimension z"></div>
<div id="labelOrigin" class="scene-label">marked vertex · scale center</div>
<div id="labelReference" class="scene-label reference">original reference</div>
</div>
<div class="scene-hint">
Drag to orbit · Shift/right-drag to pan · Scroll or pinch to zoom
</div>
<div class="view-tools">
<button id="zoomOut" class="icon-button" title="Zoom out" aria-label="Zoom out">−</button>
<button id="resetView" class="icon-button" title="Reset camera" aria-label="Reset camera">↺</button>
<button id="zoomIn" class="icon-button" title="Zoom in" aria-label="Zoom in">+</button>
</div>
</section>
<aside id="panel">
<p class="panel-kicker">Interactive Mathematics</p>
<h2>Scale the marked solid</h2>
<p class="intro">
The rectangular solid starts at
<strong>2.00 m × 1.40 m × 1.00 m</strong>.
Every edge is multiplied by the same factor.
</p>
<div class="scale-control">
<div class="scale-title">
<label for="scale">Uniform scale factor</label>
<div class="scale-number">
<strong id="scaleValue">1.00</strong><span>×</span>
</div>
</div>
<input
id="scale"
type="range"
min="0.5"
max="2"
step="0.01"
value="1"
aria-label="Uniform scale factor"
>
<div class="quick-values">
<button type="button" data-scale="0.5">0.50×</button>
<button type="button" data-scale="1">1.00×</button>
<button type="button" data-scale="1.5">1.50×</button>
<button type="button" data-scale="2">2.00×</button>
</div>
</div>
<div class="section-title">
<span>Live Measurements</span>
<span>relative to the original</span>
</div>
<div class="metric-card">
<div class="metric-head">
<div class="metric-name">Linear dimensions</div>
<div class="power-badge">1st power · k¹</div>
</div>
<div class="dimension-grid">
<div class="dimension-item x">
<span>X EDGE</span>
<strong id="xValue">2.00 m</strong>
</div>
<div class="dimension-item y">
<span>Y EDGE</span>
<strong id="yValue">1.40 m</strong>
</div>
<div class="dimension-item z">
<span>Z EDGE</span>
<strong id="zValue">1.00 m</strong>
</div>
</div>
<div class="metric-formula" id="lengthFormula">
(2.00k, 1.40k, 1.00k) m
</div>
</div>
<div class="metric-card">
<div class="metric-head">
<div class="metric-name">Surface area</div>
<div class="power-badge area">2nd power · k²</div>
</div>
<div class="metric-value">
<span id="areaValue">12.40</span><small>m²</small>
</div>
<div class="metric-formula" id="areaFormula">
12.40 k² m²
</div>
<div class="metric-change" id="areaChange">
1.00× the original area
</div>
</div>
<div class="metric-card">
<div class="metric-head">
<div class="metric-name">Volume</div>
<div class="power-badge volume">3rd power · k³</div>
</div>
<div class="metric-value">
<span id="volumeValue">2.800</span><small>m³</small>
</div>
<div class="metric-formula" id="volumeFormula">
2.800 k³ m³
</div>
<div class="metric-change" id="volumeChange">
1.00× the original volume
</div>
</div>
<div class="section-title">
<span>Power Comparison</span>
<span>bar range: 0 to 8×</span>
</div>
<div class="power-chart">
<div class="power-row">
<div class="power-row-head">
<span>Linear · k¹</span>
<b id="linearRatio">1.00×</b>
</div>
<div class="track">
<div id="fillLinear" class="fill"></div>
</div>
</div>
<div class="power-row">
<div class="power-row-head">
<span>Area · k²</span>
<b id="areaRatio">1.00×</b>
</div>
<div class="track">
<div id="fillArea" class="fill"></div>
</div>
</div>
<div class="power-row">
<div class="power-row-head">
<span>Volume · k³</span>
<b id="volumeRatio">1.00×</b>
</div>
<div class="track">
<div id="fillVolume" class="fill"></div>
</div>
</div>
<p class="chart-note">
The thin marker on each track represents the original measurement, 1×.
</p>
</div>
<div id="insight" class="insight">
At 1.00×, the solid is unchanged. Increasing the scale factor affects area and volume much faster than length.
</div>
<label class="reference-toggle">
<input id="showReference" type="checkbox" checked>
Show the original-size wireframe
</label>
<div class="footer-row">
<span class="base-size">Base solid: 2.00 × 1.40 × 1.00 m</span>
<button id="resetScale" class="text-button" type="button">Reset scale</button>
</div>
</aside>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const $ = (id) => document.getElementById(id);
const BASE = Object.freeze({
x: 2.0,
y: 1.4,
z: 1.0
});
const BASE_AREA =
2 * (BASE.x * BASE.y + BASE.x * BASE.z + BASE.y * BASE.z);
const BASE_VOLUME = BASE.x * BASE.y * BASE.z;
// -------------------------------------------------------------------------
// Renderer, camera, lighting, and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08111f);
scene.fog = new THREE.Fog(0x08111f, 10, 23);
const camera = new THREE.PerspectiveCamera(
42,
viewport.clientWidth / viewport.clientHeight,
0.05,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(viewport.clientWidth, viewport.clientHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
viewport.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc5ddff, 0x09111f, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xffb65c, 1.5);
rimLight.position.set(-5, 3, -4);
scene.add(rimLight);
const fillLight = new THREE.PointLight(0x4b8dff, 15, 14, 2);
fillLight.position.set(4, 2.5, -3);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x0b1524,
roughness: 0.96,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.035;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(16, 32, 0x38506d, 0x213149);
grid.position.set(2, -0.025, 0);
grid.material.transparent = true;
grid.material.opacity = 0.58;
grid.material.depthWrite = false;
scene.add(grid);
// -------------------------------------------------------------------------
// Marked rectangular solid
// -------------------------------------------------------------------------
const faceMaterials = [
new THREE.MeshStandardMaterial({
color: 0xe95f66,
roughness: 0.66,
metalness: 0.08,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb94362,
roughness: 0.7,
metalness: 0.06,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xffbd59,
roughness: 0.58,
metalness: 0.08,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x386fae,
roughness: 0.72,
metalness: 0.05,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x35a9a1,
roughness: 0.64,
metalness: 0.08,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x7463bd,
roughness: 0.68,
metalness: 0.06,
flatShading: true
})
];
const solid = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
faceMaterials
);
solid.castShadow = true;
solid.receiveShadow = true;
scene.add(solid);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0xf4f8ff,
transparent: true,
opacity: 0.86
});
const solidEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(1, 1, 1)),
edgeMaterial
);
scene.add(solidEdges);
const cornerGeometry = new THREE.SphereGeometry(0.052, 8, 6);
const cornerMaterial = new THREE.MeshStandardMaterial({
color: 0xffd27d,
emissive: 0x7a4300,
emissiveIntensity: 0.35,
roughness: 0.45,
metalness: 0.2,
flatShading: true
});
const corners = [];
for (let xi = 0; xi < 2; xi++) {
for (let yi = 0; yi < 2; yi++) {
for (let zi = 0; zi < 2; zi++) {
const marker = new THREE.Mesh(cornerGeometry, cornerMaterial);
marker.castShadow = true;
scene.add(marker);
corners.push({ marker, xi, yi, zi });
}
}
}
const originMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.095, 10, 7),
new THREE.MeshStandardMaterial({
color: 0xffbd59,
emissive: 0xa65b00,
emissiveIntensity: 0.65,
roughness: 0.35,
metalness: 0.2,
flatShading: true
})
);
originMarker.castShadow = true;
scene.add(originMarker);
const originRing = new THREE.Mesh(
new THREE.TorusGeometry(0.17, 0.018, 6, 24),
new THREE.MeshBasicMaterial({ color: 0xffbd59 })
);
originRing.rotation.x = Math.PI / 2;
originRing.position.y = 0.006;
scene.add(originRing);
// Original reference wireframe.
const referenceBox = new THREE.BoxGeometry(BASE.x, BASE.y, BASE.z);
const reference = new THREE.LineSegments(
new THREE.EdgesGeometry(referenceBox),
new THREE.LineDashedMaterial({
color: 0x91a5bd,
dashSize: 0.12,
gapSize: 0.08,
transparent: true,
opacity: 0.43
})
);
reference.position.set(BASE.x / 2, BASE.y / 2, BASE.z / 2);
reference.computeLineDistances();
scene.add(reference);
// -------------------------------------------------------------------------
// Dimension lines
// -------------------------------------------------------------------------
const dimensionGroup = new THREE.Group();
scene.add(dimensionGroup);
const dimensionMaterials = {
x: new THREE.LineBasicMaterial({ color: 0xff7d82 }),
y: new THREE.LineBasicMaterial({ color: 0x65e6a6 }),
z: new THREE.LineBasicMaterial({ color: 0x69a9ff })
};
const labelPositions = {
x: new THREE.Vector3(),
y: new THREE.Vector3(),
z: new THREE.Vector3(),
origin: new THREE.Vector3(-0.08, 0.13, -0.08),
reference: new THREE.Vector3(
BASE.x + 0.12,
0.05,
BASE.z
)
};
function clearDimensionGeometry() {
while (dimensionGroup.children.length) {
const child = dimensionGroup.children.pop();
child.geometry?.dispose();
}
}
function addDimensionSegments(points, material) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map((point) => new THREE.Vector3(...point))
);
const lines = new THREE.LineSegments(geometry, material);
lines.renderOrder = 5;
dimensionGroup.add(lines);
}
function rebuildDimensions(dx, dy, dz) {
clearDimensionGeometry();
// X dimension, below the front edge.
addDimensionSegments([
[0, -0.22, -0.28], [dx, -0.22, -0.28],
[0, -0.32, -0.28], [0, -0.12, -0.28],
[dx, -0.32, -0.28], [dx, -0.12, -0.28],
[0, 0, 0], [0, -0.22, -0.28],
[dx, 0, 0], [dx, -0.22, -0.28]
], dimensionMaterials.x);
// Y dimension, in front of the left edge.
addDimensionSegments([
[-0.28, 0, -0.28], [-0.28, dy, -0.28],
[-0.38, 0, -0.28], [-0.18, 0, -0.28],
[-0.38, dy, -0.28], [-0.18, dy, -0.28],
[0, 0, 0], [-0.28, 0, -0.28],
[0, dy, 0], [-0.28, dy, -0.28]
], dimensionMaterials.y);
// Z dimension, above the left edge.
addDimensionSegments([
[-0.28, dy + 0.28, 0], [-0.28, dy + 0.28, dz],
[-0.38, dy + 0.28, 0], [-0.18, dy + 0.28, 0],
[-0.38, dy + 0.28, dz], [-0.18, dy + 0.28, dz],
[0, dy, 0], [-0.28, dy + 0.28, 0],
[0, dy, dz], [-0.28, dy + 0.28, dz]
], dimensionMaterials.z);
labelPositions.x.set(dx / 2, -0.22, -0.28);
labelPositions.y.set(-0.28, dy / 2, -0.28);
labelPositions.z.set(-0.28, dy + 0.28, dz / 2);
}
// -------------------------------------------------------------------------
// Formatting and UI updates
// -------------------------------------------------------------------------
function formatNumber(value, decimals = 2) {
return value.toLocaleString(undefined, {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals
});
}
function setScale(value) {
const k = Number(value);
const dx = BASE.x * k;
const dy = BASE.y * k;
const dz = BASE.z * k;
const area = BASE_AREA * k * k;
const volume = BASE_VOLUME * k * k * k;
// 3D geometry.
solid.scale.set(dx, dy, dz);
solid.position.set(dx / 2, dy / 2, dz / 2);
solidEdges.scale.set(dx, dy, dz);
solidEdges.position.set(dx / 2, dy / 2, dz / 2);
for (const { marker, xi, yi, zi } of corners) {
marker.position.set(
xi * dx,
yi * dy,
zi * dz
);
}
originMarker.position.set(0, 0, 0);
originRing.position.set(0, 0.006, 0);
rebuildDimensions(dx, dy, dz);
// Main values.
$("scaleValue").textContent = formatNumber(k, 2);
$("xValue").textContent = `${formatNumber(dx, 2)} m`;
$("yValue").textContent = `${formatNumber(dy, 2)} m`;
$("zValue").textContent = `${formatNumber(dz, 2)} m`;
$("areaValue").textContent = formatNumber(area, 2);
$("volumeValue").textContent = formatNumber(volume, 3);
// Formula display.
$("lengthFormula").textContent =
`(${formatNumber(BASE.x, 2)}k, ${formatNumber(BASE.y, 2)}k, ` +
`${formatNumber(BASE.z, 2)}k) m`;
$("areaFormula").textContent =
`${formatNumber(BASE_AREA, 2)} k² m²`;
$("volumeFormula").textContent =
`${formatNumber(BASE_VOLUME, 3)} k³ m³`;
const areaRatio = k * k;
const volumeRatio = k * k * k;
const areaPercent = (areaRatio - 1) * 100;
const volumePercent = (volumeRatio - 1) * 100;
$("areaChange").textContent =
`${formatNumber(areaRatio, 2)}× the original area ` +
`(${areaPercent >= 0 ? "+" : ""}${formatNumber(areaPercent, 1)}%)`;
$("volumeChange").textContent =
`${formatNumber(volumeRatio, 3)}× the original volume ` +
`(${volumePercent >= 0 ? "+" : ""}${formatNumber(volumePercent, 1)}%)`;
$("linearRatio").textContent = `${formatNumber(k, 2)}×`;
$("areaRatio").textContent = `${formatNumber(areaRatio, 2)}×`;
$("volumeRatio").textContent = `${formatNumber(volumeRatio, 3)}×`;
// The chart has a fixed 0–8× range.
$("fillLinear").style.width = `${(k / 8) * 100}%`;
$("fillArea").style.width = `${(areaRatio / 8) * 100}%`;
$("fillVolume").style.width = `${(volumeRatio / 8) * 100}%`;
// Slider fill.
const rangePosition = ((k - 0.5) / 1.5) * 100;
$("scale").style.setProperty("--range-position", `${rangePosition}%`);
// 3D labels.
$("labelX").textContent = `X ${formatNumber(dx, 2)} m`;
$("labelY").textContent = `Y ${formatNumber(dy, 2)} m`;
$("labelZ").textContent = `Z ${formatNumber(dz, 2)} m`;
// Quick button state.
document.querySelectorAll("[data-scale]").forEach((button) => {
button.classList.toggle(
"active",
Math.abs(Number(button.dataset.scale) - k) < 0.0001
);
});
// Educational summary.
if (Math.abs(k - 1) < 0.0001) {
$("insight").textContent =
"At 1.00×, the solid is unchanged. Increasing the scale factor affects area and volume much faster than length.";
} else {
const direction = k > 1 ? "increases" : "decreases";
$("insight").textContent =
`Scaling to ${formatNumber(k, 2)}× ${direction} length by ` +
`${formatNumber(k, 2)}×, area by ${formatNumber(areaRatio, 2)}×, ` +
`and volume by ${formatNumber(volumeRatio, 3)}×.`;
}
}
// -------------------------------------------------------------------------
// Custom camera controls: orbit, pan, wheel/pinch zoom, and reset
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(1.0, 0.68, 0.48);
const spherical = new THREE.Spherical(6.15, 1.02, 0.87);
const initialTarget = cameraTarget.clone();
const initialSpherical = spherical.clone();
function updateCamera() {
spherical.phi = THREE.MathUtils.clamp(spherical.phi, 0.25, 1.47);
spherical.radius = THREE.MathUtils.clamp(spherical.radius, 3.2, 12.5);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraTarget.copy(initialTarget);
spherical.copy(initialSpherical);
updateCamera();
}
function zoomBy(multiplier) {
spherical.radius *= multiplier;
updateCamera();
}
function panCamera(deltaX, deltaY) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
1
);
const panScale = spherical.radius * 0.0014;
cameraTarget.addScaledVector(right, -deltaX * panScale);
cameraTarget.addScaledVector(up, deltaY * panScale);
cameraTarget.x = THREE.MathUtils.clamp(cameraTarget.x, -3, 6);
cameraTarget.y = THREE.MathUtils.clamp(cameraTarget.y, -1, 4);
cameraTarget.z = THREE.MathUtils.clamp(cameraTarget.z, -3, 5);
updateCamera();
}
const pointers = new Map();
let previousPinchDistance = null;
function getPinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return null;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
if (pointers.size === 2) {
previousPinchDistance = getPinchDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const deltaX = event.clientX - previous.x;
const deltaY = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: previous.button
});
if (pointers.size === 1) {
const panning =
event.shiftKey ||
previous.button === 2 ||
(event.buttons & 2) !== 0;
if (panning) {
panCamera(deltaX, deltaY);
} else {
spherical.theta -= deltaX * 0.006;
spherical.phi -= deltaY * 0.006;
updateCamera();
}
} else if (pointers.size === 2) {
const distance = getPinchDistance();
if (distance && previousPinchDistance) {
spherical.radius *= previousPinchDistance / distance;
updateCamera();
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance =
pointers.size >= 2 ? getPinchDistance() : null;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
$("zoomIn").addEventListener("click", () => zoomBy(0.86));
$("zoomOut").addEventListener("click", () => zoomBy(1.16));
$("resetView").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// HTML labels projected from world positions
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition, enabled = true) {
if (!enabled) {
element.style.display = "none";
return;
}
projected.copy(worldPosition).project(camera);
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
projected.x >= -1.08 &&
projected.x <= 1.08 &&
projected.y >= -1.08 &&
projected.y <= 1.08;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * viewport.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * viewport.clientHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
placeLabel($("labelX"), labelPositions.x);
placeLabel($("labelY"), labelPositions.y);
placeLabel($("labelZ"), labelPositions.z);
placeLabel($("labelOrigin"), labelPositions.origin);
placeLabel(
$("labelReference"),
labelPositions.reference,
reference.visible
);
}
// -------------------------------------------------------------------------
// Events and resize
// -------------------------------------------------------------------------
$("scale").addEventListener("input", (event) => {
setScale(event.target.value);
});
document.querySelectorAll("[data-scale]").forEach((button) => {
button.addEventListener("click", () => {
$("scale").value = button.dataset.scale;
setScale(button.dataset.scale);
});
});
$("resetScale").addEventListener("click", () => {
$("scale").value = "1";
setScale(1);
});
$("showReference").addEventListener("change", (event) => {
reference.visible = event.target.checked;
});
const resizeObserver = new ResizeObserver(() => {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
});
resizeObserver.observe(viewport);
// -------------------------------------------------------------------------
// Start
// -------------------------------------------------------------------------
setScale(1);
updateCamera();
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
updateLabels();
}
animate();
</script>
</body>
</html>
```