Prompt specification
Build or customize the Three.js 3D scene "Recipe and workshop measurement gallery" 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: Recipe and workshop measurement gallery
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/recipe-and-workshop-measurement-gallery-x1751
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring measurement units and dimensional consistency. Compose measuring cups, length rulers, and mass proxies with fictional values; distinguish volume, length, and mass so similar-looking numbers are not treated as interchangeable. Voxel style.
Working Three.js Starter (recipe-and-workshop-measurement-gallery-x1751):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Unit Workshop — Voxel Measurement Scene</title>
<style>
:root {
--bg: #08111d;
--panel: rgba(10, 20, 33, 0.88);
--border: rgba(255, 255, 255, 0.14);
--text: #edf7ff;
--muted: #9db0c4;
--volume: #52e5f5;
--length: #ffc45c;
--mass: #f277ae;
}
* {
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;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
}
.panel {
position: fixed;
z-index: 10;
background: var(--panel);
border: 1px solid var(--border);
border-radius: 16px;
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#brand {
top: 20px;
left: 20px;
width: min(420px, calc(100vw - 40px));
padding: 18px 20px;
pointer-events: none;
}
.eyebrow {
color: #89a1b8;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 7px;
font-size: clamp(22px, 3vw, 34px);
line-height: 1;
letter-spacing: -0.035em;
}
#brand p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#legend {
top: 20px;
right: 20px;
width: 300px;
padding: 15px;
pointer-events: none;
}
.legend-title {
margin-bottom: 10px;
color: #8fa5bb;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 12px 82px 1fr;
align-items: center;
gap: 8px;
margin: 8px 0;
font-size: 12px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
box-shadow: 0 0 12px currentColor;
}
.legend-row strong {
font-size: 11px;
letter-spacing: 0.08em;
}
.legend-row span:last-child {
color: #aec0d1;
}
.legend-rule {
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--border);
color: #b9c9d8;
font-size: 11px;
line-height: 1.5;
}
.legend-rule b {
color: white;
}
#readout {
right: 20px;
bottom: 20px;
width: min(360px, calc(100vw - 40px));
padding: 18px;
pointer-events: none;
}
.detail-kicker {
margin-bottom: 6px;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.18em;
}
.detail-title {
margin-bottom: 10px;
font-size: 20px;
font-weight: 800;
letter-spacing: -0.025em;
}
.detail-body {
color: #afc0d1;
font-size: 12px;
line-height: 1.55;
}
.equation {
margin: 12px 0;
padding: 11px 12px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(255, 255, 255, 0.045);
color: white;
font: 700 12px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.scope-note {
margin-top: 10px;
color: #8095aa;
font-size: 10px;
line-height: 1.45;
}
#help {
position: fixed;
z-index: 10;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
padding: 10px 15px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(8, 17, 29, 0.8);
color: #9eb0c3;
font-size: 11px;
white-space: nowrap;
pointer-events: none;
}
#fictional {
position: fixed;
z-index: 10;
left: 20px;
bottom: 20px;
padding: 8px 11px;
border: 1px solid rgba(255, 196, 92, 0.35);
border-radius: 8px;
background: rgba(31, 24, 12, 0.78);
color: #ffd58a;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.14em;
pointer-events: none;
}
@media (max-width: 850px) {
#legend {
display: none;
}
#brand {
width: calc(100vw - 40px);
}
#readout {
right: 12px;
bottom: 54px;
width: min(330px, calc(100vw - 24px));
}
#help {
bottom: 12px;
font-size: 9px;
}
#fictional {
left: 12px;
bottom: 54px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header id="brand" class="panel">
<div class="eyebrow">Fictional calibration laboratory</div>
<h1>Unit Workshop</h1>
<p>
One scene unit equals one centimetre. Volume, length, and mass remain
dimension-scoped—even when their numbers look similar.
</p>
</header>
<aside id="legend" class="panel">
<div class="legend-title">Measurement families</div>
<div class="legend-row">
<i class="swatch" style="color:var(--volume);background:var(--volume)"></i>
<strong style="color:var(--volume)">VOLUME</strong>
<span>mL / cm³</span>
</div>
<div class="legend-row">
<i class="swatch" style="color:var(--length);background:var(--length)"></i>
<strong style="color:var(--length)">LENGTH</strong>
<span>cm</span>
</div>
<div class="legend-row">
<i class="swatch" style="color:var(--mass);background:var(--mass)"></i>
<strong style="color:var(--mass)">MASS</strong>
<span>g</span>
</div>
<div class="legend-rule">
<b>1 mL = 1 cm³.</b><br>
250 mL is not 250 cm, and 250 g is not either one.
</div>
</aside>
<aside id="readout" class="panel"></aside>
<div id="fictional">ALL VALUES FICTIONAL</div>
<div id="help">
Drag: rotate · Shift/right-drag: pan · Scroll: zoom · Click: inspect
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const stage = document.querySelector("#stage");
const readout = document.querySelector("#readout");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08111d);
scene.fog = new THREE.Fog(0x08111d, 52, 95);
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-20, 20, 15, -15, 0.1, 200);
const hemi = new THREE.HemisphereLight(0xbfeaff, 0x172035, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0d7, 3.1);
sun.position.set(-12, 25, 16);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -30;
sun.shadow.camera.right = 30;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x48dfff, 1.1);
rim.position.set(18, 12, -18);
scene.add(rim);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const pickables = [];
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.03,
flatShading: true,
...options
});
}
const materials = {
floor: standard(0x111c2a),
platform: standard(0x172538),
dark: standard(0x15202d),
edgeDark: new THREE.LineBasicMaterial({
color: 0x4c6175,
transparent: true,
opacity: 0.55
}),
lengthA: standard(0xf2ae3f),
lengthB: standard(0xffca65),
lengthDark: standard(0x9c621f),
volumeA: standard(0x20b8ca),
volumeB: standard(0x47dbe8),
volumeDark: standard(0x117583),
volumeGlass: standard(0x7eefff, {
transparent: true,
opacity: 0.13,
depthWrite: false,
side: THREE.DoubleSide
}),
fluid: standard(0x54e8fb, {
transparent: true,
opacity: 0.5,
depthWrite: false
}),
massA: standard(0xe85b98),
massB: standard(0xf58ab8),
massDark: standard(0x8f315f)
};
// ------------------------------------------------------------
// Small geometry helpers
// ------------------------------------------------------------
function addBox(
width,
height,
depth,
x,
y,
z,
material,
{
parent = scene,
edges = false,
pick = null,
shadow = true
} = {}
) {
const mesh = new THREE.Mesh(cubeGeometry, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = shadow;
mesh.receiveShadow = shadow;
parent.add(mesh);
if (edges) {
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(cubeGeometry),
materials.edgeDark
);
outline.scale.setScalar(1.008);
mesh.add(outline);
}
if (pick) {
mesh.userData.detailKey = pick;
pickables.push(mesh);
}
return mesh;
}
function addLabel(
text,
accent,
worldWidth = 5,
{
parent = scene,
x = 0,
y = 0,
z = 0,
multiline = false,
small = false
} = {}
) {
const canvas = document.createElement("canvas");
canvas.width = multiline ? 1024 : 768;
canvas.height = multiline ? 320 : 160;
const ctx = canvas.getContext("2d");
const W = canvas.width;
const H = canvas.height;
ctx.clearRect(0, 0, W, H);
const radius = 24;
ctx.beginPath();
ctx.moveTo(radius, 8);
ctx.arcTo(W - 8, 8, W - 8, H - 8, radius);
ctx.arcTo(W - 8, H - 8, 8, H - 8, radius);
ctx.arcTo(8, H - 8, 8, 8, radius);
ctx.arcTo(8, 8, W - 8, 8, radius);
ctx.closePath();
ctx.fillStyle = "rgba(7, 15, 26, 0.92)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 5;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(23, 25, 12, H - 50);
const lines = text.split("\n");
let fontSize = multiline ? 58 : small ? 66 : 70;
do {
ctx.font = `800 ${fontSize}px ui-sans-serif, system-ui, sans-serif`;
const widest = Math.max(
...lines.map(line => ctx.measureText(line).width)
);
if (widest <= W - 95) break;
fontSize -= 2;
} while (fontSize > 26);
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillStyle = "#f2f9ff";
if (lines.length === 1) {
ctx.fillText(lines[0], 62, H / 2 + 2);
} else {
lines.forEach((line, index) => {
ctx.fillText(
line,
62,
H / 2 + (index - (lines.length - 1) / 2) * 76
);
});
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.position.set(x, y, z);
sprite.scale.set(
worldWidth,
worldWidth * (canvas.height / canvas.width),
1
);
sprite.renderOrder = 100;
sprite.frustumCulled = false;
parent.add(sprite);
return sprite;
}
function addZone(x, z, width, depth, color) {
const zoneMaterial = standard(color, {
transparent: true,
opacity: 0.09,
depthWrite: false
});
addBox(width, 0.06, depth, x, 0.04, z, zoneMaterial, {
edges: true,
shadow: false
});
}
// ------------------------------------------------------------
// Floor and dimensional work zones
// ------------------------------------------------------------
addBox(46, 0.4, 38, 0, -0.24, 0, materials.floor, {
edges: true
});
const grid = new THREE.GridHelper(44, 44, 0x52687d, 0x304358);
grid.position.y = 0.015;
grid.material.transparent = true;
grid.material.opacity = 0.27;
scene.add(grid);
addZone(-10, -4.5, 22, 13, 0xffb63f);
addZone(1.5, -0.3, 16, 13, 0x35d9ef);
addZone(12, 1.2, 14, 13, 0xef6aa6);
addLabel("LENGTH / cm", "#ffc45c", 5.1, {
x: -10,
y: 1.25,
z: -10.2
});
addLabel("VOLUME / mL", "#52e5f5", 5.5, {
x: 1.5,
y: 1.25,
z: -8.1
});
addLabel("MASS / g", "#f277ae", 4.7, {
x: 12,
y: 1.25,
z: -7.2
});
// ------------------------------------------------------------
// Volume: a 250 mL measuring cup
//
// Internal dimensions are exactly 10 cm × 5 cm × 5 cm.
// Therefore 10 × 5 × 5 = 250 cm³ = 250 mL.
// The fill consists of exactly 250 nominal 1 cm cubic cells.
// ------------------------------------------------------------
const cup = new THREE.Group();
cup.position.set(1.4, 0, 0);
scene.add(cup);
// Base: bottom of the internal cavity is y = 0.7.
addBox(11.2, 0.7, 6.2, 0, 0.35, 0, materials.volumeDark, {
parent: cup,
edges: true,
pick: "cup"
});
// Rear and side walls.
addBox(11.2, 5, 0.6, 0, 3.2, -2.6, materials.volumeA, {
parent: cup,
edges: true,
pick: "cup"
});
addBox(0.6, 5, 5.2, -5.3, 3.2, 0, materials.volumeB, {
parent: cup,
edges: true,
pick: "cup"
});
addBox(0.6, 5, 5.2, 5.3, 3.2, 0, materials.volumeB, {
parent: cup,
edges: true,
pick: "cup"
});
// Transparent front wall for a cutaway laboratory view.
addBox(11.2, 5, 0.18, 0, 3.2, 2.72, materials.volumeGlass, {
parent: cup,
edges: true,
pick: "cup",
shadow: false
});
// Voxel handle.
addBox(0.7, 4.4, 0.7, 6.25, 3.05, 0, materials.volumeDark, {
parent: cup,
edges: true,
pick: "cup"
});
addBox(1.7, 0.7, 0.7, 5.85, 5.05, 0, materials.volumeA, {
parent: cup,
edges: true,
pick: "cup"
});
addBox(1.7, 0.7, 0.7, 5.85, 1.05, 0, materials.volumeA, {
parent: cup,
edges: true,
pick: "cup"
});
// 250 fluid voxels: 10 along X, 5 along Z, 5 levels high.
const fluid = new THREE.InstancedMesh(
cubeGeometry,
materials.fluid,
250
);
const matrix = new THREE.Matrix4();
let fluidIndex = 0;
for (let level = 0; level < 5; level++) {
for (let z = 0; z < 5; z++) {
for (let x = 0; x < 10; x++) {
matrix.makeTranslation(
-4.5 + x,
1.2 + level,
-2 + z
);
fluid.setMatrixAt(fluidIndex, matrix);
fluidIndex++;
}
}
}
fluid.instanceMatrix.needsUpdate = true;
fluid.userData.detailKey = "cup";
cup.add(fluid);
pickables.push(fluid);
// Graduations: every 50 mL equals one centimetre of height.
for (let i = 1; i <= 5; i++) {
const value = i * 50;
const y = 0.7 + i;
const major = value % 100 === 0;
addBox(
major ? 1.45 : 0.9,
0.12,
0.2,
4.62,
y,
2.93,
materials.volumeB,
{
parent: cup,
pick: "cup"
}
);
addLabel(`${value} mL`, "#52e5f5", 1.62, {
parent: cup,
x: -6.35,
y,
z: 3.0,
small: true
});
}
addLabel("250 mL · VOLUME", "#52e5f5", 7.2, {
parent: cup,
x: 0,
y: 7.1,
z: 0
});
// ------------------------------------------------------------
// Length rulers
//
// Every coloured division is exactly 1 cm.
// ------------------------------------------------------------
function makeRuler(length, x, z, detailKey) {
const ruler = new THREE.Group();
ruler.position.set(x, 0, z);
scene.add(ruler);
addBox(
length + 0.35,
0.28,
1.95,
0,
0.25,
0,
materials.lengthDark,
{
parent: ruler,
edges: true,
pick: detailKey
}
);
for (let i = 0; i < length; i++) {
const segmentX = -(length - 1) / 2 + i;
addBox(
0.98,
0.34,
1.6,
segmentX,
0.54,
0,
i % 2 === 0 ? materials.lengthA : materials.lengthB,
{
parent: ruler,
pick: detailKey
}
);
const isMajor = (i + 1) % 5 === 0;
addBox(
0.08,
0.06,
isMajor ? 1.05 : 0.58,
segmentX + 0.5,
0.75,
isMajor ? 0.12 : 0.38,
materials.lengthDark,
{
parent: ruler,
pick: detailKey,
shadow: false
}
);
}
const marks = [];
for (let mark = 0; mark <= length; mark += 5) {
marks.push(mark);
}
marks.forEach(mark => {
const markX = -length / 2 + mark;
addLabel(`${mark}`, "#ffc45c", 1.25, {
parent: ruler,
x: markX,
y: 1.3,
z: 1.0,
small: true
});
});
addLabel(`${length} cm · LENGTH`, "#ffc45c", 6.2, {
parent: ruler,
x: 0,
y: 3.0,
z: 0
});
return ruler;
}
makeRuler(20, -10, -7.1, "ruler20");
makeRuler(10, -13, -1.8, "ruler10");
// ------------------------------------------------------------
// Mass proxies
//
// Each visible token is a symbolic 50 g unit. The standardized
// trays are deliberately not treated as physical volume scales.
// ------------------------------------------------------------
function makeMassProxy(mass, x, z, detailKey) {
const proxy = new THREE.Group();
proxy.position.set(x, 0, z);
scene.add(proxy);
// Identical carrier tray for every proxy.
addBox(4.8, 0.38, 3.9, 0, 0.27, 0, materials.massDark, {
parent: proxy,
edges: true,
pick: detailKey
});
const tokenCount = mass / 50;
const tokenSize = 0.72;
// Fixed 5 × 2 token grid, so carriers remain the same size.
for (let index = 0; index < tokenCount; index++) {
const column = index % 5;
const row = Math.floor(index / 5);
addBox(
tokenSize,
tokenSize,
tokenSize,
-1.55 + column * 0.78,
0.48 + tokenSize / 2,
-0.78 + row * 0.78,
index % 2 === 0 ? materials.massA : materials.massB,
{
parent: proxy,
edges: true,
pick: detailKey
}
);
}
addLabel(`${mass} g · MASS`, "#f277ae", 4.9, {
parent: proxy,
x: 0,
y: 2.35,
z: 0
});
return proxy;
}
makeMassProxy(100, 7.3, 3.2, "mass100");
makeMassProxy(250, 11.6, 3.2, "mass250");
makeMassProxy(500, 15.9, 3.2, "mass500");
// ------------------------------------------------------------
// Inspection content
// ------------------------------------------------------------
const details = {
cup: `
<div class="detail-kicker" style="color:var(--volume)">VOLUME · CUBIC CAPACITY</div>
<div class="detail-title">250 mL measuring cup</div>
<div class="detail-body">
Internal cavity: <b>10 cm × 5 cm × 5 cm</b>.
The cyan fill contains 250 nominal one-centimetre cubic cells.
</div>
<div class="equation">
10 cm × 5 cm × 5 cm<br>
= 250 cm³ = 250 mL
</div>
<div class="scope-note">
The cubic centimetre conversion is dimensional. The number 250
does not imply a length of 250 cm or a mass of 250 g.
</div>
`,
ruler20: `
<div class="detail-kicker" style="color:var(--length)">LENGTH · LINEAR SCALE</div>
<div class="detail-title">20 cm ruler</div>
<div class="detail-body">
Twenty coloured divisions span exactly <b>20 cm</b>.
Each division is one centimetre long.
</div>
<div class="equation">20 divisions × 1 cm = 20 cm</div>
<div class="scope-note">
This is a length measurement only. It is not a volume reading
and it is not a mass reading.
</div>
`,
ruler10: `
<div class="detail-kicker" style="color:var(--length)">LENGTH · LINEAR SCALE</div>
<div class="detail-title">10 cm ruler</div>
<div class="detail-body">
Ten coloured divisions span exactly <b>10 cm</b>.
Each division is one centimetre long.
</div>
<div class="equation">10 divisions × 1 cm = 10 cm</div>
<div class="scope-note">
A number written beside a ruler always describes a span in
centimetres unless another unit is explicitly stated.
</div>
`,
mass100: `
<div class="detail-kicker" style="color:var(--mass)">MASS · CALIBRATION PROXY</div>
<div class="detail-title">100 g mass proxy</div>
<div class="detail-body">
Two symbolic 50 g tokens make a total of <b>100 g</b>.
</div>
<div class="equation">2 tokens × 50 g = 100 g</div>
<div class="scope-note">
Token geometry is an icon for counting mass units. It is not a
physical volume scale and does not specify density.
</div>
`,
mass250: `
<div class="detail-kicker" style="color:var(--mass)">MASS · CALIBRATION PROXY</div>
<div class="detail-title">250 g mass proxy</div>
<div class="detail-body">
Five symbolic 50 g tokens make a total of <b>250 g</b>.
</div>
<div class="equation">5 tokens × 50 g = 250 g</div>
<div class="scope-note">
The same numeral can occur in another measurement family, but
250 g does not become 250 mL or 250 cm.
</div>
`,
mass500: `
<div class="detail-kicker" style="color:var(--mass)">MASS · CALIBRATION PROXY</div>
<div class="detail-title">500 g mass proxy</div>
<div class="detail-body">
Ten symbolic 50 g tokens make a total of <b>500 g</b>.
</div>
<div class="equation">10 tokens × 50 g = 500 g</div>
<div class="scope-note">
Mass, length, and volume are separate dimensions. Identical-looking
numbers remain attached to their own units.
</div>
`
};
function showDetail(key) {
if (details[key]) {
readout.innerHTML = details[key];
}
}
showDetail("cup");
// ------------------------------------------------------------
// Custom camera controls — no external controls addon
// ------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 1.2, 0),
theta: 0.76,
phi: 1.02,
zoom: 1,
radius: 55
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
const cosPhi = Math.cos(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * cosPhi,
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Preserve a broad view on narrow displays.
const viewHeight = Math.max(28, 46 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
}
window.addEventListener("resize", resize);
resize();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const selectionBox = new THREE.BoxHelper(
new THREE.Object3D(),
0xffffff
);
selectionBox.visible = false;
selectionBox.material.transparent = true;
selectionBox.material.opacity = 0.85;
scene.add(selectionBox);
let pointerDown = null;
let dragged = false;
function setRayFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function pick(event) {
setRayFromEvent(event);
return raycaster.intersectObjects(pickables, false)[0] || null;
}
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0 && event.button !== 2) return;
pointerDown = {
x: event.clientX,
y: event.clientY,
button: event.button,
shift: event.shiftKey
};
dragged = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointerDown) {
const hit = pick(event);
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
return;
}
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 4) {
dragged = true;
}
const panMode = pointerDown.button === 2 || pointerDown.shift;
if (panMode) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
1
);
const worldPerPixel =
(camera.top - camera.bottom) /
camera.zoom /
renderer.domElement.clientHeight;
controls.target.addScaledVector(right, -dx * worldPerPixel);
controls.target.addScaledVector(up, dy * worldPerPixel);
controls.target.x = THREE.MathUtils.clamp(
controls.target.x,
-15,
15
);
controls.target.y = THREE.MathUtils.clamp(
controls.target.y,
-3,
10
);
controls.target.z = THREE.MathUtils.clamp(
controls.target.z,
-13,
13
);
} else {
controls.theta -= dx * 0.007;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.006,
0.48,
1.42
);
}
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
if (!pointerDown) return;
if (!dragged && event.button === 0) {
const hit = pick(event);
if (hit) {
const key = hit.object.userData.detailKey;
showDetail(key);
selectionBox.setFromObject(hit.object);
selectionBox.visible = true;
}
}
pointerDown = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
dragged = false;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * Math.exp(-event.deltaY * 0.001),
0.62,
2.2
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
renderer.setAnimationLoop(() => {
updateCamera();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```