Prompt specification
Build or customize the Three.js 3D scene "Two footing sizes under equal loads" 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: Two footing sizes under equal loads
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/two-footing-sizes-under-equal-loads-x2198
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows building foundations and soil contact. Place equal loaded columns on differently sized schematic footings. Display average contact pressure calculated from load divided by area, without simulating real soil settlement. Voxel style.
Working Three.js Starter (two-footing-sizes-under-equal-loads-x2198):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Foundation Contact Study</title>
<style>
:root {
color-scheme: dark;
--bg: #07111c;
--panel: rgba(13, 25, 38, 0.82);
--border: rgba(160, 196, 218, 0.2);
--text: #eef7fb;
--muted: #9db2c2;
--cyan: #29d3c3;
--yellow: #f6c85f;
--red: #f05b6c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 32%, rgba(34, 69, 91, 0.42), transparent 45%),
linear-gradient(145deg, #0a1723 0%, var(--bg) 70%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene,
#labels {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.panel {
position: absolute;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 14px 40px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.brand {
top: 20px;
left: 20px;
width: min(390px, calc(100vw - 40px));
padding: 18px 19px;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 8px;
font-size: clamp(20px, 2.3vw, 31px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 0;
max-width: 330px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 13px;
padding: 7px 9px;
border: 1px solid rgba(240, 91, 108, 0.28);
border-radius: 7px;
background: rgba(240, 91, 108, 0.08);
color: #ffacb5;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.badge::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--red);
box-shadow: 0 0 10px rgba(240, 91, 108, 0.7);
content: "";
}
.controls {
top: 20px;
right: 20px;
display: flex;
gap: 7px;
padding: 7px;
pointer-events: auto;
}
button {
border: 1px solid rgba(173, 207, 225, 0.18);
border-radius: 8px;
padding: 9px 11px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(41, 211, 195, 0.55);
background: rgba(41, 211, 195, 0.1);
}
button:active {
transform: translateY(1px);
}
.legend {
bottom: 20px;
left: 20px;
width: 278px;
padding: 15px 16px;
}
.legend-title,
.method-title {
margin-bottom: 10px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.gradient {
height: 9px;
border-radius: 999px;
background: linear-gradient(90deg, #29d3c3 0%, #f6c85f 50%, #f05b6c 100%);
box-shadow: 0 0 18px rgba(246, 200, 95, 0.12);
}
.scale {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.legend-note {
margin-top: 9px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.method {
right: 20px;
bottom: 20px;
width: 316px;
padding: 16px;
}
.formula {
margin: 2px 0 10px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
letter-spacing: 0.01em;
}
.formula strong {
color: var(--cyan);
}
.method p {
margin: 7px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.hint {
position: absolute;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
padding: 8px 12px;
border: 1px solid rgba(166, 199, 218, 0.13);
border-radius: 999px;
background: rgba(7, 17, 28, 0.66);
color: #90a6b7;
font-size: 10px;
white-space: nowrap;
}
#labels {
z-index: 4;
pointer-events: none;
}
.callout {
position: absolute;
width: 190px;
padding: 12px 13px;
transform: translate(-50%, -100%);
border: 1px solid rgba(175, 206, 224, 0.2);
border-top: 3px solid var(--accent);
border-radius: 10px;
background: rgba(10, 22, 34, 0.91);
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(10px);
will-change: left, top;
}
.callout::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 13px;
background: linear-gradient(var(--accent), transparent);
content: "";
}
.case-row {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 10px;
}
.case-name {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
line-height: 1.4;
text-transform: uppercase;
}
.pressure {
margin-top: 3px;
font-size: 22px;
font-weight: 800;
letter-spacing: -0.045em;
line-height: 1;
white-space: nowrap;
}
.pressure small {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0;
}
.calculation {
display: flex;
justify-content: space-between;
gap: 8px;
margin-top: 10px;
padding-top: 8px;
border-top: 1px solid rgba(170, 202, 220, 0.13);
color: var(--muted);
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.calculation b {
color: var(--text);
}
.dimension {
position: absolute;
transform: translate(-50%, -50%);
padding: 4px 7px;
border: 1px solid rgba(176, 205, 221, 0.17);
border-radius: 5px;
background: rgba(7, 17, 28, 0.78);
color: #c8d8e2;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
white-space: nowrap;
will-change: left, top;
}
.mobile-summary {
display: none;
}
@media (max-width: 760px) {
.brand {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 15px;
}
h1 {
font-size: 21px;
}
.subtitle {
max-width: none;
font-size: 11px;
}
.badge {
margin-top: 9px;
}
.controls {
top: auto;
right: 12px;
bottom: 76px;
}
.controls #toggleLabels {
display: none;
}
.legend,
.method,
.hint,
#labels {
display: none;
}
.mobile-summary {
position: absolute;
right: 12px;
bottom: 12px;
left: 12px;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
pointer-events: none;
}
.mobile-item {
padding: 10px 8px;
border: 1px solid var(--border);
border-top: 3px solid var(--accent);
border-radius: 10px;
background: rgba(10, 22, 34, 0.9);
text-align: center;
box-shadow: 0 10px 24px rgba(0, 0, 0, 0.25);
}
.mobile-item span {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 700;
}
.mobile-item strong {
display: block;
margin-top: 4px;
font-size: 14px;
}
}
</style>
</head>
<body>
<main id="app">
<div id="scene"></div>
<div id="labels"></div>
<div class="hud">
<header class="panel brand">
<div class="eyebrow">Voxel geotechnical study</div>
<h1>Foundation / Soil Contact</h1>
<p class="subtitle">
Equal 120 kN column loads on three schematic footing sizes.
Contact pressure is calculated directly as load divided by bearing area.
</p>
<div class="badge">Schematic · No settlement simulation</div>
</header>
<div class="panel controls">
<button id="resetView" type="button">Reset view</button>
<button id="toggleLabels" type="button" aria-pressed="true">Labels on</button>
</div>
<section class="panel legend">
<div class="legend-title">Average contact pressure</div>
<div class="gradient"></div>
<div class="scale">
<span>0 kPa</span>
<span>15</span>
<span>30 kPa</span>
</div>
<div class="legend-note">
Colored tiles indicate the pressure under each footing footprint.
</div>
</section>
<aside class="panel method">
<div class="method-title">Calculation basis</div>
<div class="formula"><strong>q<sub>avg</sub></strong> = P / A</div>
<p>
P = 120 kN for every column. A is the square plan area of each
schematic footing. Smaller contact area produces higher average pressure.
</p>
<p>
Footing elevations and column lengths are fixed. No soil stiffness,
bearing-capacity failure, or settlement model is applied.
</p>
</aside>
<div class="hint">Drag to orbit · Scroll to zoom · Shift-drag or right-drag to pan</div>
<div id="mobileSummary" class="mobile-summary"></div>
</div>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const LOAD_KN = 120;
const PRESSURE_MAX = 30;
const VOXEL = 0.5;
const foundations = [
{ id: "A", name: "Compact", x: -7.2, size: 2.0 },
{ id: "B", name: "Medium", x: 0.0, size: 3.0 },
{ id: "C", name: "Wide", x: 7.2, size: 4.0 }
].map((item) => {
const area = item.size * item.size;
const pressure = LOAD_KN / area;
return { ...item, area, pressure };
});
const sceneHost = document.getElementById("scene");
const labelsHost = document.getElementById("labels");
const mobileSummary = document.getElementById("mobileSummary");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel model of three loaded foundations on soil"
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111c);
scene.fog = new THREE.Fog(0x07111c, 32, 62);
const camera = new THREE.OrthographicCamera(-15, 15, 8, -8, 0.1, 100);
const target = new THREE.Vector3(0, 1.35, 0);
const radius = 30;
let theta = 0.69;
let phi = 0.86;
function updateCamera() {
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(14.8, 24 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc6e8ff, 0x4a3021, 2.15);
scene.add(hemisphere);
const sunlight = new THREE.DirectionalLight(0xfff0d8, 3.2);
sunlight.position.set(-10, 17, 12);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -18;
sunlight.shadow.camera.right = 18;
sunlight.shadow.camera.top = 15;
sunlight.shadow.camera.bottom = -15;
sunlight.shadow.camera.near = 1;
sunlight.shadow.camera.far = 50;
sunlight.shadow.normalBias = 0.035;
sunlight.shadow.bias = -0.00025;
scene.add(sunlight);
const rimLight = new THREE.DirectionalLight(0x65bfff, 1.45);
rimLight.position.set(11, 8, -12);
scene.add(rimLight);
const basePlane = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0x081521,
roughness: 1,
metalness: 0
})
);
basePlane.rotation.x = -Math.PI / 2;
basePlane.position.y = -1.62;
basePlane.receiveShadow = true;
scene.add(basePlane);
let randomSeed = 202503;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function pressureColor(pressure) {
const t = THREE.MathUtils.clamp(pressure / PRESSURE_MAX, 0, 1);
const low = new THREE.Color(0x29d3c3);
const mid = new THREE.Color(0xf6c85f);
const high = new THREE.Color(0xf05b6c);
if (t < 0.5) {
return low.clone().lerp(mid, t / 0.5);
}
return mid.clone().lerp(high, (t - 0.5) / 0.5);
}
// Two layers of individually colored cubic soil blocks form the cutaway ground.
const soilNX = 58;
const soilNZ = 26;
const soilCount = soilNX * soilNZ * 2;
const soilGeometry = new THREE.BoxGeometry(0.47, 0.72, 0.47);
const soilMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0
});
const soil = new THREE.InstancedMesh(soilGeometry, soilMaterial, soilCount);
soil.receiveShadow = true;
soil.instanceMatrix.setUsage(THREE.StaticDrawUsage);
const dummy = new THREE.Object3D();
const upperSoil = new THREE.Color(0x81583d);
const lowerSoil = new THREE.Color(0x563a2c);
const soilHighlight = new THREE.Color(0xa07751);
let soilIndex = 0;
for (let layer = 0; layer < 2; layer++) {
for (let iz = 0; iz < soilNZ; iz++) {
for (let ix = 0; ix < soilNX; ix++) {
const x = (ix - (soilNX - 1) / 2) * VOXEL;
const z = (iz - (soilNZ - 1) / 2) * VOXEL;
const y = -0.52 - layer * 0.72;
dummy.position.set(x, y, z);
dummy.updateMatrix();
soil.setMatrixAt(soilIndex, dummy.matrix);
const n = random();
const color = (layer === 0 ? upperSoil : lowerSoil)
.clone()
.lerp(soilHighlight, n * 0.3)
.multiplyScalar(0.78 + random() * 0.25);
soil.setColorAt(soilIndex, color);
soilIndex++;
}
}
}
soil.instanceColor.needsUpdate = true;
scene.add(soil);
const callouts = [];
function makeVoxelSlab(data, y, height, geometry, material, colorFunction) {
const count = data.size * data.size;
const mesh = new THREE.InstancedMesh(geometry, material, count);
let index = 0;
for (let iz = 0; iz < data.size; iz++) {
for (let ix = 0; ix < data.size; ix++) {
const x = data.x - data.size / 2 + (ix + 0.5) * VOXEL;
const z = -data.size / 2 + (iz + 0.5) * VOXEL;
dummy.position.set(x, y, z);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, colorFunction(ix, iz, index));
index++;
}
}
mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function makeColumn(x) {
const layers = 7;
const across = 2;
const count = layers * across * across;
const geometry = new THREE.BoxGeometry(0.475, 0.475, 0.475);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.63,
metalness: 0.08
});
const mesh = new THREE.InstancedMesh(geometry, material, count);
const steel = new THREE.Color(0x7895a8);
const steelLight = new THREE.Color(0xa4bdca);
let index = 0;
for (let layer = 0; layer < layers; layer++) {
for (let iz = 0; iz < across; iz++) {
for (let ix = 0; ix < across; ix++) {
const px = x + (ix - 0.5) * VOXEL;
const pz = (iz - 0.5) * VOXEL;
const py = 0.675 + 0.25 + layer * VOXEL;
dummy.position.set(px, py, pz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
const color = steel.clone().lerp(
steelLight,
layer === layers - 1 ? 0.75 : random() * 0.28
);
mesh.setColorAt(index, color);
index++;
}
}
}
mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
function makeLoadArrow(x) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color: 0xff715e,
emissive: 0x57110b,
emissiveIntensity: 0.45,
roughness: 0.42
});
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 1.12, 0.14),
material
);
shaft.position.y = 5.75;
shaft.castShadow = true;
group.add(shaft);
const headGeometry = new THREE.BoxGeometry(1, 0.16, 0.16);
const widths = [1.0, 0.72, 0.44, 0.16];
widths.forEach((width, i) => {
const pixel = new THREE.Mesh(headGeometry, material);
pixel.scale.x = width;
pixel.position.y = 5.12 - i * 0.16;
pixel.castShadow = true;
group.add(pixel);
});
group.position.x = x;
scene.add(group);
}
function makeRuler(data) {
const z = data.size / 2 + 0.95;
const count = Math.max(4, Math.round(data.size * 4));
const geometry = new THREE.BoxGeometry(0.09, 0.08, 0.09);
const material = new THREE.MeshStandardMaterial({
color: 0x7894a8,
roughness: 0.8
});
const mesh = new THREE.InstancedMesh(geometry, material, count + 2);
for (let i = 0; i < count; i++) {
const x = data.x - data.size / 2 + (i / (count - 1)) * data.size;
dummy.position.set(x, -0.105, z);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
}
[-1, 1].forEach((side, i) => {
dummy.position.set(data.x + side * data.size / 2, -0.105, z);
dummy.updateMatrix();
mesh.setMatrixAt(count + i, dummy.matrix);
});
mesh.castShadow = true;
scene.add(mesh);
}
foundations.forEach((data) => {
const color = pressureColor(data.pressure);
const colorHex = `#${color.getHexString()}`;
// Concrete footing: one voxel-sized block per half-meter plan cell.
const footingGeometry = new THREE.BoxGeometry(0.475, 0.68, 0.475);
const footingMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.78,
metalness: 0.02
});
makeVoxelSlab(
data,
0.34,
0.68,
footingGeometry,
footingMaterial,
(ix, iz, index) => {
const concrete = new THREE.Color(0x879ba8);
const concreteLight = new THREE.Color(0xb0c0c9);
return concrete.clone().lerp(concreteLight, 0.18 + random() * 0.28);
}
);
// Exact bearing-area footprint. Color encodes q = P/A.
const contactGeometry = new THREE.BoxGeometry(0.515, 0.16, 0.515);
const contactMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.28,
roughness: 0.52
});
makeVoxelSlab(
data,
-0.08,
0.16,
contactGeometry,
contactMaterial,
() => color.clone()
);
makeColumn(data.x);
makeLoadArrow(data.x);
makeRuler(data);
const callout = document.createElement("div");
callout.className = "callout";
callout.style.setProperty("--accent", colorHex);
callout.innerHTML = `
<div class="case-row">
<div>
<div class="case-name">Case ${data.id} · ${data.size.toFixed(1)} × ${data.size.toFixed(1)} m</div>
<div class="pressure">${data.pressure.toFixed(1)} <small>kPa</small></div>
</div>
</div>
<div class="calculation">
<span>q<sub>avg</sub> = ${LOAD_KN} / ${data.area.toFixed(2)}</span>
<b>${LOAD_KN} kN</b>
</div>
`;
labelsHost.appendChild(callout);
const dimension = document.createElement("div");
dimension.className = "dimension";
dimension.textContent = `${data.size.toFixed(1)} × ${data.size.toFixed(1)} m`;
labelsHost.appendChild(dimension);
callouts.push({
element: callout,
anchor: new THREE.Vector3(data.x, 6.85, 0)
});
callouts.push({
element: dimension,
anchor: new THREE.Vector3(data.x, -0.04, data.size / 2 + 1.65),
dimension: true
});
const mobileItem = document.createElement("div");
mobileItem.className = "mobile-item";
mobileItem.style.setProperty("--accent", colorHex);
mobileItem.innerHTML = `
<span>${data.id} · ${data.size.toFixed(1)} × ${data.size.toFixed(1)} m</span>
<strong>${data.pressure.toFixed(1)} kPa</strong>
`;
mobileSummary.appendChild(mobileItem);
});
// Direct camera controls implemented without importing any Three.js addons.
const pointers = new Map();
let gesture = null;
const tempForward = new THREE.Vector3();
const tempRight = new THREE.Vector3();
const tempUp = new THREE.Vector3();
const worldUp = new THREE.Vector3(0, 1, 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 });
if (pointers.size === 1) {
gesture = {
type: event.button === 2 || event.shiftKey ? "pan" : "orbit",
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
gesture = {
type: "pinch",
distance: pointerDistance(),
zoom: camera.zoom
};
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1 && gesture) {
const dx = event.clientX - gesture.x;
const dy = event.clientY - gesture.y;
if (gesture.type === "orbit") {
theta -= dx * 0.006;
phi = THREE.MathUtils.clamp(phi + dy * 0.005, 0.28, 1.38);
updateCamera();
} else if (gesture.type === "pan") {
camera.getWorldDirection(tempForward);
tempForward.y = 0;
tempForward.normalize();
tempRight.crossVectors(tempForward, worldUp).normalize();
tempUp.crossVectors(tempRight, tempForward).normalize();
const scale =
(camera.top - camera.bottom) /
camera.zoom /
renderer.domElement.clientHeight;
target.addScaledVector(tempRight, -dx * scale);
target.addScaledVector(tempUp, dy * scale);
updateCamera();
}
gesture.x = event.clientX;
gesture.y = event.clientY;
} else if (pointers.size === 2 && gesture?.type === "pinch") {
const distance = pointerDistance();
if (distance > 0) {
camera.zoom = THREE.MathUtils.clamp(
gesture.zoom * (distance / gesture.distance),
0.65,
2.8
);
camera.updateProjectionMatrix();
}
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
gesture = {
type: "orbit",
x: remaining.x,
y: remaining.y
};
} else {
gesture = null;
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.8
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
let labelsVisible = true;
document.getElementById("resetView").addEventListener("click", () => {
target.set(0, 1.35, 0);
theta = 0.69;
phi = 0.86;
camera.zoom = 1;
camera.updateProjectionMatrix();
updateCamera();
});
document.getElementById("toggleLabels").addEventListener("click", (event) => {
labelsVisible = !labelsVisible;
labelsHost.style.opacity = labelsVisible ? "1" : "0";
event.currentTarget.textContent = labelsVisible ? "Labels on" : "Labels off";
event.currentTarget.setAttribute("aria-pressed", String(labelsVisible));
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
target.set(0, 1.35, 0);
theta = 0.69;
phi = 0.86;
camera.zoom = 1;
camera.updateProjectionMatrix();
updateCamera();
}
});
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
callouts.forEach(({ element, anchor }) => {
const projected = anchor.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const visible = projected.z >= -1 && projected.z <= 1;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = visible ? "1" : "0";
});
}
function animate() {
requestAnimationFrame(animate);
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```