Prompt specification
Build or customize the Three.js 3D scene "Unit conversion scale 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: Unit conversion scale comparison
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/unit-conversion-scale-comparison-x1750
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring measurement units and dimensional consistency. Place centimeter and meter rulers beside the same object, plus corresponding area tiles; demonstrate squared conversion factors for area instead of reusing the linear factor. Stylized solid forms.
Working Three.js Starter (unit-conversion-scale-comparison-x1750):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>True-Scale Measurement Gallery</title>
<style>
:root {
color-scheme: dark;
--bg: #09111c;
--panel: rgba(13, 24, 38, 0.84);
--border: rgba(255, 255, 255, 0.12);
--text: #f4f8fb;
--muted: #9eb0c0;
--cyan: #55e6d3;
--amber: #ffb454;
--violet: #a78bfa;
--yellow: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 70% 20%, #182c40 0%, transparent 42%),
linear-gradient(150deg, #0b1724 0%, #07101a 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.glass {
background: var(--panel);
border: 1px solid var(--border);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.title-card {
position: fixed;
top: 18px;
left: 18px;
width: min(430px, calc(100vw - 36px));
padding: 20px;
border-radius: 20px;
pointer-events: auto;
z-index: 10;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 16px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 34px);
line-height: 1.04;
letter-spacing: -0.035em;
}
.intro {
margin: 10px 0 15px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.conversion-pills {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.pill {
min-width: 0;
padding: 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 12px;
background: rgba(255, 255, 255, 0.035);
}
.pill span {
display: block;
margin-bottom: 4px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
}
.pill.linear span {
color: var(--cyan);
}
.pill.area span {
color: var(--violet);
}
.pill b {
display: block;
white-space: nowrap;
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.controls-card {
position: fixed;
top: 18px;
right: 18px;
width: 286px;
padding: 16px;
border-radius: 18px;
z-index: 12;
pointer-events: auto;
}
.section-label {
margin-bottom: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.view-buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-bottom: 14px;
}
.view-button {
padding: 9px 8px;
color: #dce7ef;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 10px;
background: rgba(255, 255, 255, 0.045);
cursor: pointer;
font-size: 12px;
transition: 160ms ease;
}
.view-button:hover {
border-color: rgba(85, 230, 211, 0.55);
background: rgba(85, 230, 211, 0.08);
}
.view-button.active {
color: #071510;
border-color: var(--cyan);
background: var(--cyan);
font-weight: 750;
}
.switch-row {
display: flex;
align-items: center;
gap: 9px;
margin: 9px 0;
color: #d8e3eb;
font-size: 12px;
cursor: pointer;
user-select: none;
}
.switch-row input {
width: 16px;
height: 16px;
accent-color: var(--cyan);
cursor: pointer;
}
.object-note {
margin-top: 13px;
padding: 11px;
border: 1px solid rgba(255, 180, 84, 0.22);
border-radius: 12px;
background: rgba(255, 180, 84, 0.055);
color: #cbd5dc;
font-size: 11px;
line-height: 1.6;
}
.object-note strong {
color: var(--amber);
}
.tile-check {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 10px;
}
.tile-check div {
padding: 9px;
border: 1px solid rgba(167, 139, 250, 0.2);
border-radius: 10px;
background: rgba(167, 139, 250, 0.055);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.tile-check b {
display: block;
color: #d9ceff;
font-size: 12px;
}
.hint {
margin-top: 12px;
color: #718596;
font-size: 10px;
line-height: 1.5;
}
.formula-dock {
position: fixed;
left: 50%;
bottom: 18px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 18px;
max-width: calc(100vw - 36px);
padding: 13px 18px;
border-radius: 17px;
z-index: 11;
pointer-events: auto;
}
.formula-part {
white-space: nowrap;
}
.formula-part small {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
}
.formula-part strong {
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.formula-part.linear strong {
color: var(--cyan);
}
.formula-part.area strong {
color: var(--violet);
}
.square-arrow {
color: var(--yellow);
text-align: center;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.square-arrow::before {
content: "↗";
display: block;
margin-bottom: 2px;
font-size: 23px;
line-height: 1;
}
#tag-layer {
position: fixed;
inset: 0;
overflow: hidden;
pointer-events: none;
z-index: 7;
}
.scene-tag {
position: absolute;
min-width: 112px;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 9px;
background: rgba(8, 16, 26, 0.86);
box-shadow: 0 8px 22px rgba(0, 0, 0, 0.25);
text-align: center;
transform: translate(-50%, -100%);
white-space: nowrap;
font-size: 11px;
line-height: 1.35;
backdrop-filter: blur(8px);
}
.scene-tag::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 15px;
background: rgba(255, 255, 255, 0.35);
}
.scene-tag b {
display: block;
font-size: 12px;
}
.scene-tag small {
color: var(--muted);
font-size: 9px;
}
.scene-tag.object {
border-color: rgba(255, 180, 84, 0.45);
}
.scene-tag.object b {
color: var(--amber);
}
.scene-tag.cm {
border-color: rgba(85, 230, 211, 0.45);
}
.scene-tag.cm b {
color: var(--cyan);
}
.scene-tag.area {
border-color: rgba(167, 139, 250, 0.45);
}
.scene-tag.area b {
color: var(--violet);
}
.scene-tag.dimension {
min-width: auto;
padding: 4px 7px;
color: #ffe8a5;
border-color: rgba(255, 209, 102, 0.5);
font-size: 10px;
font-weight: 800;
}
.scene-tag.dimension::after {
display: none;
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: #08121d;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
z-index: 30;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
.controls-card {
top: auto;
right: 12px;
bottom: 126px;
width: 250px;
}
.title-card {
top: 12px;
left: 12px;
width: min(410px, calc(100vw - 24px));
padding: 16px;
}
.formula-dock {
bottom: 12px;
}
}
@media (max-width: 600px) {
.title-card {
padding: 14px;
}
.intro,
.conversion-pills,
.tile-check,
.hint {
display: none;
}
.controls-card {
right: 10px;
bottom: 112px;
width: 220px;
padding: 12px;
}
.object-note {
margin-top: 8px;
padding: 8px;
font-size: 10px;
}
.formula-dock {
width: calc(100vw - 20px);
gap: 8px;
padding: 10px;
justify-content: center;
}
.formula-part strong {
font-size: 12px;
}
.formula-part small {
font-size: 8px;
}
.square-arrow {
font-size: 8px;
}
.scene-tag {
min-width: 92px;
padding: 5px 7px;
font-size: 9px;
}
.scene-tag b {
font-size: 10px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive true-scale measurement scene"></div>
<div id="tag-layer"></div>
<header class="title-card glass">
<div class="eyebrow">True-scale dimension lab</div>
<h1>One object.<br />Two unit systems.</h1>
<p class="intro">
Every mesh is built using meters. The rulers and square tiles are shown
at their actual relative sizes, so the area relationship stays honest.
</p>
<div class="conversion-pills">
<div class="pill linear">
<span>LINEAR CONVERSION</span>
<b>1 m = 100 cm</b>
</div>
<div class="pill area">
<span>AREA CONVERSION</span>
<b>1 m² = 10,000 cm²</b>
</div>
</div>
</header>
<aside class="controls-card glass">
<div class="section-label">Inspect the scene</div>
<div class="view-buttons">
<button class="view-button active" data-view="overview">Overview</button>
<button class="view-button" data-view="object">Object</button>
<button class="view-button" data-view="cm">Cm ruler</button>
<button class="view-button" data-view="meter">Meter ruler</button>
<button class="view-button" data-view="tiles">Area tiles</button>
</div>
<label class="switch-row">
<input id="grid-toggle" type="checkbox" checked />
Show 1 cm² cell grid
</label>
<label class="switch-row">
<input id="dimension-toggle" type="checkbox" checked />
Show object dimensions
</label>
<div class="object-note">
<strong>Same solid body</strong><br />
20 × 10 × 20 cm<br />
= 0.20 × 0.10 × 0.20 m<br />
<strong>Front: 200 cm² = 0.020 m²</strong>
</div>
<div class="tile-check">
<div>
10 cm tile
<b>100 cm²</b>
</div>
<div>
1 m tile
<b>10,000 cm²</b>
</div>
</div>
<div class="hint">
Drag to orbit · wheel/pinch to zoom · right-drag to pan.<br />
The floor grid is 10 cm × 10 cm.
</div>
</aside>
<div class="formula-dock glass" aria-live="polite">
<div class="formula-part linear">
<small>LENGTH</small>
<strong>1 m = 100 cm</strong>
</div>
<div class="square-arrow">square the factor</div>
<div class="formula-part area">
<small>AREA</small>
<strong>1 m² = (100 cm)² = 10,000 cm²</strong>
</div>
</div>
<div id="loading">Loading measurement gallery…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/*
All Three.js coordinates are in meters.
Centimeter measurements are converted before geometry is created.
*/
const cm = value => value / 100;
const m = value => value;
const sceneHost = document.getElementById("scene");
const tagLayer = document.getElementById("tag-layer");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x09121d);
scene.fog = new THREE.Fog(0x09121d, 4.4, 9.5);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.01,
50
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const controls = {
target: new THREE.Vector3(0.28, 0.12, 0.06),
radius: 3.35,
theta: 0.56,
phi: 1.04
};
const views = {
overview: {
target: [0.28, 0.12, 0.06],
radius: 3.35,
theta: 0.56,
phi: 1.04
},
object: {
target: [0, 0.12, 0],
radius: 0.66,
theta: 0.67,
phi: 1.10
},
cm: {
target: [-0.34, 0.05, 0.15],
radius: 0.62,
theta: 0.73,
phi: 1.03
},
meter: {
target: [1.05, 0.05, 0.43],
radius: 1.30,
theta: 0.58,
phi: 1.03
},
tiles: {
target: [0.34, 0.05, 0.03],
radius: 2.35,
theta: 0.58,
phi: 1.02
}
};
const hemisphere = new THREE.HemisphereLight(0xc9ecff, 0x162131, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(2.2, 4.0, 2.4);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -3;
keyLight.shadow.camera.right = 3;
keyLight.shadow.camera.top = 3;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.camera.near = 0.1;
keyLight.shadow.camera.far = 10;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x45e6d2, 12, 4, 2);
cyanLight.position.set(-1.2, 1.0, 1.2);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x8b74ff, 14, 4, 2);
violetLight.position.set(1.7, 1.2, -0.5);
scene.add(violetLight);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x101d2a,
roughness: 0.94,
metalness: 0.02
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(4.1, 0.06, 3.1),
floorMaterial
);
floor.position.set(0.3, -0.03, 0.25);
floor.receiveShadow = true;
scene.add(floor);
// 4 m / 40 divisions = a physical 10 cm floor grid.
const floorGrid = new THREE.GridHelper(4, 40, 0x426176, 0x294052);
floorGrid.position.set(0.3, 0.002, 0.25);
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.34;
floorGrid.material.depthWrite = false;
scene.add(floorGrid);
const tagEntries = [];
function addTag(anchor, html, className = "", isDimension = false) {
const element = document.createElement("div");
element.className = `scene-tag ${className}`;
element.innerHTML = html;
tagLayer.appendChild(element);
tagEntries.push({
element,
anchor: anchor.clone(),
isDimension
});
return element;
}
function createBox(width, height, depth, material, position) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function roundedRectPath(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 createRulerTexture(lengthCm, widthMeters, mode) {
const pixelsPerCm = 32;
const width = Math.round(lengthCm * pixelsPerCm);
const height = Math.round(
width * (widthMeters / cm(lengthCm))
);
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
const accent = mode === "meter" ? "#6ea8ff" : "#39cdbb";
ctx.fillStyle = "#f5efdf";
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, width, Math.max(9, height * 0.07));
const margin = width * 0.012;
const usableWidth = width - margin * 2;
const titleSize = Math.max(11, height * 0.09);
const labelSize = Math.max(13, height * 0.115);
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillStyle = "#17334a";
ctx.font = `800 ${titleSize}px system-ui, sans-serif`;
ctx.fillText(
mode === "meter"
? "METERS · 1 CM DIVISIONS"
: "CENTIMETERS · 1 CM DIVISIONS",
margin,
height * 0.15
);
const tickTop = height * 0.27;
const baseY = height - height * 0.12;
for (let i = 0; i <= lengthCm; i++) {
const x = margin + (i / lengthCm) * usableWidth;
const isTen = i % 10 === 0;
const isFive = i % 5 === 0;
const isLabelTick =
mode === "meter" ? i % 20 === 0 : i % 5 === 0;
let tickLength = height * 0.13;
if (isFive) tickLength = height * 0.23;
if (isTen) tickLength = height * 0.34;
if (isLabelTick) tickLength = height * 0.41;
ctx.strokeStyle = isTen ? "#17334a" : "#526979";
ctx.lineWidth = isTen ? 2.2 : 1.2;
ctx.beginPath();
ctx.moveTo(x, tickTop);
ctx.lineTo(x, tickTop + tickLength);
ctx.stroke();
if (isLabelTick) {
let label;
if (mode === "meter") {
if (i === 0) label = "0 m";
else if (i === lengthCm) label = `${(i / 100).toFixed(1)} m`;
else label = (i / 100).toFixed(1);
} else {
if (i === lengthCm) label = `${i} cm`;
else label = `${i}`;
}
ctx.fillStyle = "#17334a";
ctx.font = `700 ${labelSize}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "alphabetic";
ctx.fillText(label, x, baseY);
}
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function createRuler({
lengthCm,
widthMeters,
thicknessMeters,
x,
z,
mode
}) {
const lengthMeters = cm(lengthCm);
const group = new THREE.Group();
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xe8dfc9,
roughness: 0.55,
metalness: 0.03
});
const body = new THREE.Mesh(
new THREE.BoxGeometry(lengthMeters, thicknessMeters, widthMeters),
bodyMaterial
);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const texture = createRulerTexture(lengthCm, widthMeters, mode);
const face = new THREE.Mesh(
new THREE.PlaneGeometry(lengthMeters, widthMeters),
new THREE.MeshStandardMaterial({
map: texture,
roughness: 0.72,
metalness: 0
})
);
face.rotation.x = -Math.PI / 2;
face.position.y = thicknessMeters / 2 + 0.0008;
face.receiveShadow = true;
group.add(face);
const endMaterial = new THREE.MeshStandardMaterial({
color: mode === "meter" ? 0x3978d8 : 0x159f91,
roughness: 0.45
});
const endSize = 0.012;
const end = new THREE.Mesh(
new THREE.BoxGeometry(endSize, thicknessMeters + 0.001, widthMeters),
endMaterial
);
end.position.x = -lengthMeters / 2 + endSize / 2;
end.castShadow = true;
group.add(end);
group.position.set(x, thicknessMeters / 2, z);
scene.add(group);
return group;
}
// A 30 cm ruler, with physically spaced 1 cm ticks.
createRuler({
lengthCm: 30,
widthMeters: m(0.04),
thicknessMeters: m(0.024),
x: -0.34,
z: 0.29,
mode: "cm"
});
// A true 1 m ruler, also with physically spaced 1 cm ticks.
createRuler({
lengthCm: 100,
widthMeters: m(0.06),
thicknessMeters: m(0.026),
x: 1.05,
z: 0.67,
mode: "meter"
});
function createSquareTile(sideMeters, x, z, color) {
const thickness = m(0.018);
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.62,
metalness: 0.04
});
const tile = new THREE.Mesh(
new THREE.BoxGeometry(sideMeters, thickness, sideMeters),
material
);
tile.position.set(x, thickness / 2, z);
tile.castShadow = true;
tile.receiveShadow = true;
scene.add(tile);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(tile.geometry),
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.42
})
);
edges.position.copy(tile.position);
scene.add(edges);
return tile;
}
// Corresponding square-unit tiles at true relative scale.
const smallTile = createSquareTile(
cm(10),
-0.36,
-0.02,
0x2dc8b6
);
const largeTile = createSquareTile(
m(1),
1.05,
0.05,
0x7057dc
);
const areaGridGroup = new THREE.Group();
scene.add(areaGridGroup);
function createAreaGrid(sideMeters, divisions, x, z, y) {
const half = sideMeters / 2;
const positions = [];
for (let i = 0; i <= divisions; i++) {
const p = -half + (i / divisions) * sideMeters;
positions.push(
p, 0, -half,
p, 0, half,
-half, 0, p,
half, 0, p
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const material = new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.26,
depthWrite: false
});
const grid = new THREE.LineSegments(geometry, material);
grid.position.set(x, y, z);
areaGridGroup.add(grid);
}
// 10 x 10 cells: each cell is 1 cm x 1 cm = 1 cm².
createAreaGrid(cm(10), 10, -0.36, -0.02, 0.020);
// 100 x 100 cells: each cell is still 1 cm x 1 cm = 1 cm².
createAreaGrid(m(1), 100, 1.05, 0.05, 0.020);
// Display stand; dimensions shown in the interface refer to the orange body.
const standMaterial = new THREE.MeshStandardMaterial({
color: 0x173247,
roughness: 0.48,
metalness: 0.18
});
createBox(
m(0.28),
m(0.025),
m(0.23),
standMaterial,
new THREE.Vector3(0, m(0.0125), 0)
);
// The measured solid: 20 cm wide, 20 cm high, and 10 cm deep.
const objectMaterial = new THREE.MeshStandardMaterial({
color: 0xffa34a,
roughness: 0.4,
metalness: 0.08
});
const objectBody = createBox(
cm(20),
cm(20),
cm(10),
objectMaterial,
new THREE.Vector3(0, cm(22.5), 0)
);
const insetMaterial = new THREE.MeshStandardMaterial({
color: 0xffd19a,
roughness: 0.55,
metalness: 0.03
});
// A thin surface detail that stays inside the measured bounds.
createBox(
cm(14),
cm(2.2),
m(0.001),
insetMaterial,
new THREE.Vector3(0, cm(15), cm(5.05))
);
createBox(
cm(7),
m(0.001),
cm(1.5),
insetMaterial,
new THREE.Vector3(0, cm(20.05), 0)
);
const dimensionMaterial = new THREE.LineBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.95
});
function addDimensionSegments(points) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map(point => new THREE.Vector3(...point))
);
const lines = new THREE.LineSegments(geometry, dimensionMaterial);
scene.add(lines);
return lines;
}
// Width bracket: 20 cm.
addDimensionSegments([
[-0.10, 0.045, 0.05], [-0.10, 0.045, 0.12],
[ 0.10, 0.045, 0.05], [ 0.10, 0.045, 0.12],
[-0.10, 0.045, 0.12], [0.10, 0.045, 0.12]
]);
// Height bracket: 20 cm.
addDimensionSegments([
[-0.10, 0.025, 0.075], [-0.145, 0.025, 0.075],
[-0.10, 0.225, 0.075], [-0.145, 0.225, 0.075],
[-0.145, 0.025, 0.075], [-0.145, 0.225, 0.075]
]);
// Depth bracket: 10 cm.
addDimensionSegments([
[0.10, 0.045, -0.05], [0.14, 0.045, -0.05],
[0.10, 0.045, 0.05], [0.14, 0.045, 0.05],
[0.14, 0.045, -0.05], [0.14, 0.045, 0.05]
]);
addTag(
new THREE.Vector3(0, 0.27, 0),
"<b>SAME OBJECT</b><small>20 × 10 × 20 cm</small>",
"object"
);
addTag(
new THREE.Vector3(-0.34, 0.06, 0.29),
"<b>30 CM RULER</b><small>1 cm tick spacing</small>",
"cm"
);
addTag(
new THREE.Vector3(-0.36, 0.065, -0.02),
"<b>10 × 10 CM</b><small>100 cm²</small>",
"area"
);
addTag(
new THREE.Vector3(1.05, 0.065, 0.67),
"<b>1 M RULER</b><small>100 one-centimeter ticks</small>",
"cm"
);
addTag(
new THREE.Vector3(1.05, 0.08, -0.17),
"<b>1 M × 1 M</b><small>1 m² = 10,000 cm²</small>",
"area"
);
addTag(
new THREE.Vector3(0, 0.055, 0.13),
"20 cm",
"dimension",
true
);
addTag(
new THREE.Vector3(-0.15, 0.145, 0.075),
"20 cm",
"dimension",
true
);
addTag(
new THREE.Vector3(0.15, 0.055, 0),
"10 cm",
"dimension",
true
);
let dimensionsVisible = true;
document.getElementById("grid-toggle").addEventListener("change", event => {
areaGridGroup.visible = event.target.checked;
});
document.getElementById("dimension-toggle").addEventListener("change", event => {
dimensionsVisible = event.target.checked;
});
const tempDirection = new THREE.Vector3();
const tempRight = new THREE.Vector3();
const tempUp = new THREE.Vector3();
function updateCamera() {
camera.position.set(
controls.target.x +
controls.radius * Math.sin(controls.phi) * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z +
controls.radius * Math.sin(controls.phi) * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
updateCamera();
// Camera controls are implemented directly, without Three.js addons.
let tween = null;
function shortestAngle(from, to) {
let delta = (to - from + Math.PI) % (Math.PI * 2);
if (delta < 0) delta += Math.PI * 2;
return from + delta - Math.PI;
}
function setView(name, instant = false) {
const view = views[name];
if (!view) return;
document.querySelectorAll(".view-button").forEach(button => {
button.classList.toggle("active", button.dataset.view === name);
});
if (instant) {
controls.target.set(...view.target);
controls.radius = view.radius;
controls.theta = view.theta;
controls.phi = view.phi;
tween = null;
updateCamera();
return;
}
tween = {
start: performance.now(),
duration: 850,
from: {
target: controls.target.clone(),
radius: controls.radius,
theta: controls.theta,
phi: controls.phi
},
to: {
target: new THREE.Vector3(...view.target),
radius: view.radius,
theta: shortestAngle(controls.theta, view.theta),
phi: view.phi
}
};
}
document.querySelectorAll(".view-button").forEach(button => {
button.addEventListener("click", () => setView(button.dataset.view));
});
const pointers = new Map();
let lastPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
previousX: event.clientX,
previousY: event.clientY,
button: event.button
});
tween = null;
if (pointers.size === 2) {
lastPinchDistance = pointerDistance();
}
});
renderer.domElement.addEventListener("pointermove", event => {
const pointer = pointers.get(event.pointerId);
if (!pointer) return;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (pointers.size >= 2) {
const distance = pointerDistance();
if (lastPinchDistance > 0 && distance > 0) {
controls.radius = THREE.MathUtils.clamp(
controls.radius * (lastPinchDistance / distance),
0.34,
6
);
}
lastPinchDistance = distance;
} else {
const dx = pointer.x - pointer.previousX;
const dy = pointer.y - pointer.previousY;
if (pointer.button === 2 || event.shiftKey) {
camera.getWorldDirection(tempDirection);
tempRight.crossVectors(tempDirection, camera.up).normalize();
tempUp.crossVectors(tempRight, tempDirection).normalize();
const panScale = controls.radius * 0.0015;
controls.target
.addScaledVector(tempRight, -dx * panScale)
.addScaledVector(tempUp, dy * panScale);
} else {
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.005,
0.28,
1.48
);
}
}
pointer.previousX = pointer.x;
pointer.previousY = pointer.y;
});
function releasePointer(event) {
pointers.delete(event.pointerId);
lastPinchDistance = pointers.size >= 2 ? pointerDistance() : 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
tween = null;
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
0.34,
6
);
},
{ passive: false }
);
window.addEventListener("keydown", event => {
if (event.target instanceof HTMLInputElement) return;
tween = null;
const step = 0.08;
if (event.key === "ArrowLeft") controls.theta -= step;
if (event.key === "ArrowRight") controls.theta += step;
if (event.key === "ArrowUp") {
controls.phi = THREE.MathUtils.clamp(controls.phi - 0.06, 0.28, 1.48);
}
if (event.key === "ArrowDown") {
controls.phi = THREE.MathUtils.clamp(controls.phi + 0.06, 0.28, 1.48);
}
if (event.key === "+" || event.key === "=") {
controls.radius = Math.max(0.34, controls.radius * 0.9);
}
if (event.key === "-" || event.key === "_") {
controls.radius = Math.min(6, controls.radius * 1.1);
}
});
renderer.domElement.addEventListener("dblclick", () => {
setView("overview");
});
const projected = new THREE.Vector3();
function updateTags() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
for (const entry of tagEntries) {
projected.copy(entry.anchor).project(camera);
const inside =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
const dimensionAllowed = !entry.isDimension || dimensionsVisible;
entry.element.style.display =
inside && dimensionAllowed ? "block" : "none";
if (!inside || !dimensionAllowed) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
entry.element.style.left = `${x}px`;
entry.element.style.top = `${y}px`;
}
}
function animate(now) {
requestAnimationFrame(animate);
if (tween) {
const raw = Math.min(1, (now - tween.start) / tween.duration);
const eased =
raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
controls.target.lerpVectors(
tween.from.target,
tween.to.target,
eased
);
controls.radius = THREE.MathUtils.lerp(
tween.from.radius,
tween.to.radius,
eased
);
controls.theta = THREE.MathUtils.lerp(
tween.from.theta,
tween.to.theta,
eased
);
controls.phi = THREE.MathUtils.lerp(
tween.from.phi,
tween.to.phi,
eased
);
if (raw >= 1) tween = null;
}
updateCamera();
camera.updateMatrixWorld();
renderer.render(scene, camera);
updateTags();
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", onResize);
setView("overview", true);
requestAnimationFrame(animate);
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```