Prompt specification
Build or customize the Three.js 3D scene "Scale bar and measured footbridge" 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 bar and measured footbridge
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/scale-bar-and-measured-footbridge-x1321
Scene Description Prompt:
Place a segmented scale bar next to a simple footbridge plan. Mark both endpoints and distinguish the model's display units from represented ground meters. Build this with Three.js from cdnjs as a stylized scene in one self-contained HTML file.
Working Three.js Starter (scale-bar-and-measured-footbridge-x1321):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Footbridge Plan — Segmented Scale Bar</title>
<style>
:root {
--ink: #18384d;
--muted: #62727c;
--paper: #f5f0e6;
--model: #245477;
--ground: #c9573f;
--teal: #278b80;
--panel: rgba(250, 247, 239, 0.92);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #ddd6c7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#app {
position: fixed;
inset: 0;
}
#app canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(24, 56, 77, 0.14);
background: var(--panel);
box-shadow: 0 12px 36px rgba(37, 48, 53, 0.12);
backdrop-filter: blur(12px);
}
.title-card {
top: 22px;
left: 22px;
width: min(355px, calc(100vw - 44px));
padding: 18px 20px 17px;
border-radius: 16px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 36px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.legend-card {
left: 22px;
bottom: 22px;
width: min(330px, calc(100vw - 44px));
padding: 15px 17px;
border-radius: 14px;
pointer-events: none;
}
.legend-row {
display: flex;
align-items: flex-start;
gap: 10px;
margin: 7px 0;
}
.swatch {
width: 22px;
height: 8px;
flex: 0 0 auto;
margin-top: 5px;
border-radius: 999px;
}
.swatch.model {
background: var(--model);
}
.swatch.ground {
background: var(--ground);
}
.legend-row b {
display: block;
font-size: 12px;
line-height: 1.25;
}
.legend-row small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 11px;
line-height: 1.3;
}
.conversion {
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid rgba(24, 56, 77, 0.12);
color: var(--ink);
font-size: 12px;
font-weight: 800;
}
.conversion span {
color: var(--muted);
font-size: 10px;
font-weight: 600;
}
.toolbar {
top: 22px;
right: 22px;
display: flex;
gap: 8px;
padding: 7px;
border-radius: 12px;
}
button {
border: 0;
border-radius: 8px;
padding: 9px 12px;
background: transparent;
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
}
button:hover,
button.active {
background: rgba(36, 84, 119, 0.11);
color: var(--model);
}
.help {
right: 22px;
bottom: 22px;
padding: 10px 13px;
border-radius: 10px;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #e8e1d4;
color: var(--ink);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
transition: opacity 0.35s ease;
}
@media (max-width: 700px) {
.title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.subtitle,
.legend-row small {
display: none;
}
.legend-card {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 11px 13px;
}
.legend-row {
margin: 4px 0;
}
.toolbar {
top: auto;
right: 12px;
bottom: 101px;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<main id="app" aria-label="Stylized three-dimensional footbridge plan with a segmented scale bar"></main>
<section class="panel title-card">
<div class="eyebrow">Spatial study 01</div>
<h1>Footbridge plan</h1>
<p class="subtitle">
A stylized plan with a segmented dual-unit scale. Model units and
represented ground meters are labeled independently.
</p>
</section>
<section class="panel legend-card" aria-label="Scale unit legend">
<div class="legend-row">
<span class="swatch model"></span>
<div>
<b>Model display units</b>
<small>MU · scene-coordinate units</small>
</div>
</div>
<div class="legend-row">
<span class="swatch ground"></span>
<div>
<b>Represented ground distance</b>
<small>m · declared model-to-ground scale</small>
</div>
</div>
<div class="conversion">
1 MU = 0.75 represented m
<br>
<span>One 5 MU segment represents 3.75 ground meters.</span>
</div>
</section>
<nav class="panel toolbar" aria-label="Scene controls">
<button id="reset-view" type="button">Reset view</button>
<button id="top-view" type="button">Top view</button>
<button id="toggle-grid" class="active" type="button">Grid</button>
</nav>
<aside class="panel help">
Drag to orbit · Shift-drag to pan · Scroll to zoom<br>
Double-click to reset
</aside>
<div id="loading">Building footbridge plan…</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 app = document.querySelector("#app");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional footbridge and segmented scale bar"
);
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xded7c9);
scene.fog = new THREE.Fog(0xded7c9, 58, 100);
const camera = new THREE.OrthographicCamera(-20, 20, 15, -15, 0.1, 150);
const hemisphere = new THREE.HemisphereLight(0xfff8e8, 0x6e7b79, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d6, 3.4);
sun.position.set(-13, 28, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -34;
sun.shadow.camera.right = 34;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 75;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9ecbd1, 1.1);
fill.position.set(20, 14, -18);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const materials = {
board: new THREE.MeshStandardMaterial({
color: 0xf4efe4,
roughness: 0.96,
metalness: 0
}),
water: new THREE.MeshStandardMaterial({
color: 0x88c7d2,
roughness: 0.28,
metalness: 0.05,
transparent: true,
opacity: 0.86
}),
woodA: new THREE.MeshStandardMaterial({
color: 0xc9905b,
roughness: 0.82
}),
woodB: new THREE.MeshStandardMaterial({
color: 0xd9a36d,
roughness: 0.86
}),
rail: new THREE.MeshStandardMaterial({
color: 0x243d49,
roughness: 0.58,
metalness: 0.18
}),
beam: new THREE.MeshStandardMaterial({
color: 0x47636d,
roughness: 0.7,
metalness: 0.12
}),
navy: new THREE.MeshStandardMaterial({
color: 0x245477,
roughness: 0.7
}),
paleBlue: new THREE.MeshStandardMaterial({
color: 0xb8d8e8,
roughness: 0.74
}),
coral: new THREE.MeshStandardMaterial({
color: 0xc9573f,
roughness: 0.7
}),
teal: new THREE.MeshStandardMaterial({
color: 0x278b80,
roughness: 0.68
}),
cream: new THREE.MeshStandardMaterial({
color: 0xf7f1e4,
roughness: 0.85
})
};
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function addBox(width, height, depth, x, y, z, material, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(radius, height, x, y, z, material, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, 16),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
// Text is rendered into canvas textures so no external font or label addon
// is required.
function makeLabel(text, options = {}) {
const {
color = "#18384d",
background = null,
borderColor = null,
worldHeight = 0.8,
fontSize = 48,
weight = 700
} = options;
const lines = text.split("\n");
const measuringCanvas = document.createElement("canvas");
const measuringContext = measuringCanvas.getContext("2d");
const font = `${weight} ${fontSize}px Inter, ui-sans-serif, system-ui, sans-serif`;
measuringContext.font = font;
const textWidth = Math.max(
...lines.map((line) => measuringContext.measureText(line).width)
);
const lineHeight = fontSize * 1.18;
const paddingX = background ? 28 : 5;
const paddingY = background ? 18 : 4;
const canvas = document.createElement("canvas");
canvas.width = Math.ceil(textWidth + paddingX * 2);
canvas.height = Math.ceil(lines.length * lineHeight + paddingY * 2);
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
if (background) {
roundedRect(
context,
2,
2,
canvas.width - 4,
canvas.height - 4,
16
);
context.fillStyle = background;
context.fill();
if (borderColor) {
context.lineWidth = 4;
context.strokeStyle = borderColor;
context.stroke();
}
}
context.font = font;
context.textAlign = "left";
context.textBaseline = "top";
context.fillStyle = color;
lines.forEach((line, index) => {
context.fillText(
line,
paddingX,
paddingY + index * lineHeight
);
});
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearMipmapLinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
texture.needsUpdate = true;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(
worldHeight * (canvas.width / canvas.height),
worldHeight,
1
);
sprite.renderOrder = 20;
scene.add(sprite);
return sprite;
}
function placeLabel(text, x, y, z, options = {}) {
const label = makeLabel(text, options);
label.position.set(x, y, z);
return label;
}
// -------------------------------------------------------------------------
// Drawing board and model-unit grid
// -------------------------------------------------------------------------
const board = addBox(50, 0.6, 28, 9, -0.35, 0, materials.board);
board.castShadow = false;
const boardEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(board.geometry),
new THREE.LineBasicMaterial({
color: 0x8e9b9d,
transparent: true,
opacity: 0.38
})
);
boardEdges.position.copy(board.position);
scene.add(boardEdges);
const gridGroup = new THREE.Group();
scene.add(gridGroup);
const minorPositions = [];
const majorPositions = [];
for (let x = -16; x <= 34; x += 2) {
const target = (x + 16) % 10 === 0 ? majorPositions : minorPositions;
target.push(x, 0.012, -14, x, 0.012, 14);
}
for (let z = -14; z <= 14; z += 2) {
const target = (z + 14) % 10 === 0 ? majorPositions : minorPositions;
target.push(-16, 0.012, z, 34, 0.012, z);
}
function addGrid(positions, color, opacity) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const lines = new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
})
);
gridGroup.add(lines);
}
addGrid(minorPositions, 0x78909a, 0.13);
addGrid(majorPositions, 0x5d7883, 0.24);
// -------------------------------------------------------------------------
// Canal and ripples
// -------------------------------------------------------------------------
const water = addBox(
7.2,
0.1,
27.2,
-1,
0.04,
0,
materials.water
);
water.castShadow = false;
const ripplePositions = [];
for (let i = 0; i < 10; i++) {
const z = -11.5 + i * 2.55;
const width = 2.8 + (i % 3) * 0.65;
ripplePositions.push(
-1 - width / 2,
0.105,
z,
-1 + width / 2,
0.105,
z
);
}
const rippleGeometry = new THREE.BufferGeometry();
rippleGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(ripplePositions, 3)
);
scene.add(
new THREE.LineSegments(
rippleGeometry,
new THREE.LineBasicMaterial({
color: 0xe8fbff,
transparent: true,
opacity: 0.46
})
)
);
// -------------------------------------------------------------------------
// Footbridge: 24 model units from endpoint A to endpoint B
// -------------------------------------------------------------------------
const bridgeStart = -13;
const bridgeEnd = 11;
const bridgeCenter = (bridgeStart + bridgeEnd) / 2;
const bridgeLength = bridgeEnd - bridgeStart;
// Approach slabs.
addBox(3, 0.34, 5.2, -14.5, 0.28, 0, materials.woodB);
addBox(3, 0.34, 5.2, 12.5, 0.28, 0, materials.woodB);
// Longitudinal structural beams.
addBox(bridgeLength, 0.34, 0.28, bridgeCenter, 0.43, -2.15, materials.beam);
addBox(bridgeLength, 0.34, 0.28, bridgeCenter, 0.43, 2.15, materials.beam);
// Individual deck planks.
const plankCount = 32;
const plankPitch = bridgeLength / plankCount;
for (let i = 0; i < plankCount; i++) {
const x = bridgeStart + (i + 0.5) * plankPitch;
addBox(
plankPitch * 0.87,
0.22,
4.9,
x,
0.7,
0,
i % 2 === 0 ? materials.woodA : materials.woodB
);
}
// Railings on both sides.
for (const z of [-2.58, 2.58]) {
addBox(bridgeLength, 0.16, 0.16, bridgeCenter, 2.44, z, materials.rail);
addBox(bridgeLength, 0.11, 0.11, bridgeCenter, 1.76, z, materials.rail);
for (let i = 0; i <= 12; i++) {
const x = bridgeStart + (bridgeLength / 12) * i;
addCylinder(0.085, 1.75, x, 1.58, z, materials.rail);
}
}
// -------------------------------------------------------------------------
// Bridge endpoints and span dimension
// -------------------------------------------------------------------------
const endpointRings = [];
function addEndpoint(x, letter, meterText, colorMaterial, borderColor) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.48, 0.075, 10, 32),
new THREE.MeshBasicMaterial({
color: borderColor,
transparent: true,
opacity: 0.82
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(x, 0.13, -3.25);
scene.add(ring);
endpointRings.push(ring);
addCylinder(0.18, 0.24, x, 0.16, -3.25, colorMaterial);
placeLabel(
`ENDPOINT ${letter}\n${meterText}`,
x,
1.48,
-3.55,
{
worldHeight: 1.28,
fontSize: 46,
color: "#18384d",
background: "rgba(250,247,239,0.96)",
borderColor
}
);
}
addEndpoint(
bridgeStart,
"A",
"0.00 MU • 0.00 m",
materials.coral,
"#c9573f"
);
addEndpoint(
bridgeEnd,
"B",
"24.00 MU • 18.00 m",
materials.teal,
"#278b80"
);
// Dimension extension lines.
const dimensionPositions = [
bridgeStart, 0.15, -2.7,
bridgeStart, 0.15, -5.8,
bridgeEnd, 0.15, -2.7,
bridgeEnd, 0.15, -5.8,
bridgeStart, 0.15, -5.35,
bridgeEnd, 0.15, -5.35
];
const dimensionGeometry = new THREE.BufferGeometry();
dimensionGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dimensionPositions, 3)
);
scene.add(
new THREE.LineSegments(
dimensionGeometry,
new THREE.LineBasicMaterial({ color: 0xc9573f })
)
);
const dimensionMaterial = new THREE.MeshBasicMaterial({
color: 0xc9573f
});
function addDimensionArrow(x, direction) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.22, 0.75, 12),
dimensionMaterial
);
arrow.rotation.z = direction > 0 ? -Math.PI / 2 : Math.PI / 2;
arrow.position.set(
x + (direction > 0 ? -0.36 : 0.36),
0.18,
-5.35
);
scene.add(arrow);
}
addDimensionArrow(bridgeStart, -1);
addDimensionArrow(bridgeEnd, 1);
placeLabel(
"BRIDGE SPAN • 24 MU = 18.00 m",
bridgeCenter,
0.9,
-6.35,
{
worldHeight: 0.72,
fontSize: 43,
color: "#a94331"
}
);
// -------------------------------------------------------------------------
// Segmented scale bar
//
// Scene geometry uses model display units directly:
// 1 scene unit = 1 MU
//
// Declared represented ground scale:
// 1 MU = 0.75 m
//
// The main segmented bar is 20 MU long, split into four 5 MU segments.
// -------------------------------------------------------------------------
const scaleStart = 12.5;
const scaleEnd = 32.5;
const scaleLength = scaleEnd - scaleStart;
const scaleCenter = (scaleStart + scaleEnd) / 2;
const scaleZ = 7.45;
const segmentLength = 5;
// Dark backing and outline.
addBox(
scaleLength + 0.22,
0.2,
1.52,
scaleCenter,
0.16,
scaleZ,
materials.navy
);
// Four alternating 5 MU segments.
for (let i = 0; i < 4; i++) {
const material = i % 2 === 0 ? materials.navy : materials.paleBlue;
addBox(
segmentLength - 0.035,
0.25,
1.28,
scaleStart + segmentLength * (i + 0.5),
0.36,
scaleZ,
material
);
}
// Strong end caps make both endpoints explicit.
addBox(0.2, 0.5, 1.75, scaleStart, 0.38, scaleZ, materials.navy);
addBox(0.2, 0.5, 1.75, scaleEnd, 0.38, scaleZ, materials.navy);
// Separate coral ruler showing represented ground meters.
const groundRulerZ = 6.08;
addBox(
scaleLength,
0.08,
0.1,
scaleCenter,
0.14,
groundRulerZ,
materials.coral
);
for (let i = 0; i <= 4; i++) {
const x = scaleStart + i * segmentLength;
addBox(
0.09,
0.16,
i % 4 === 0 ? 0.72 : 0.48,
x,
0.18,
groundRulerZ,
materials.coral
);
}
// Model-unit labels above the segmented bar.
placeLabel(
"MODEL DISPLAY UNITS • MU",
scaleCenter,
1.05,
10.55,
{
worldHeight: 0.76,
fontSize: 45,
color: "#245477"
}
);
for (let i = 0; i <= 4; i++) {
const x = scaleStart + i * segmentLength;
const modelUnits = i * segmentLength;
placeLabel(
String(modelUnits),
x,
0.78,
9.08,
{
worldHeight: 0.64,
fontSize: 47,
color: "#245477"
}
);
}
// Represented ground-meter labels below the coral ruler.
placeLabel(
"REPRESENTED GROUND METERS • m",
scaleCenter,
1.05,
3.05,
{
worldHeight: 0.76,
fontSize: 43,
color: "#c9573f"
}
);
for (let i = 0; i <= 4; i++) {
const x = scaleStart + i * segmentLength;
const modelUnits = i * segmentLength;
const groundMeters = modelUnits * 0.75;
placeLabel(
groundMeters.toFixed(2),
x,
0.78,
4.65,
{
worldHeight: 0.58,
fontSize: 41,
color: "#c9573f"
}
);
}
// Conversion card placed inside the scene.
placeLabel(
"DECLARED GROUND SCALE\n1 MU = 0.75 m\n5 MU segment = 3.75 m",
scaleCenter,
1.0,
-2.05,
{
worldHeight: 1.42,
fontSize: 44,
color: "#ffffff",
background: "rgba(24,56,77,0.96)",
borderColor: "rgba(255,255,255,0.18)"
}
);
// Plan heading on the board.
placeLabel(
"FOOTBRIDGE • PLAN VIEW",
-8.4,
0.9,
11.25,
{
worldHeight: 0.82,
fontSize: 43,
color: "#48626f"
}
);
placeLabel(
"GRID • 2 MU MINOR / 10 MU MAJOR",
-8.4,
0.72,
10.05,
{
worldHeight: 0.47,
fontSize: 34,
color: "#7a8b91"
}
);
// -------------------------------------------------------------------------
// Custom orthographic camera controls
// -------------------------------------------------------------------------
const target = new THREE.Vector3(7, 0, 0);
const defaultTarget = target.clone();
let radius = 55;
let theta = 0;
let phi = 0.82;
let viewSize = 31;
function updateCamera() {
const horizontal = radius * Math.sin(phi);
camera.position.set(
target.x + horizontal * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + horizontal * Math.cos(theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
applyProjection();
}
function applyProjection() {
const aspect = window.innerWidth / window.innerHeight;
const fitFactor = Math.max(1, 1.28 / aspect);
const verticalSize = viewSize * fitFactor;
camera.left = (-verticalSize * aspect) / 2;
camera.right = (verticalSize * aspect) / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
camera.updateProjectionMatrix();
}
function resetView() {
target.copy(defaultTarget);
radius = 55;
theta = 0;
phi = 0.82;
viewSize = 31;
updateCamera();
}
const canvas = renderer.domElement;
let dragging = false;
let panning = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.button === 2 || event.shiftKey;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (panning) {
const worldPerPixel =
(camera.top - camera.bottom) / canvas.clientHeight;
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0)
.normalize();
const up = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 1)
.normalize();
target
.addScaledVector(right, -dx * worldPerPixel)
.addScaledVector(up, dy * worldPerPixel);
} else {
theta -= dx * 0.005;
phi = THREE.MathUtils.clamp(phi + dy * 0.005, 0.12, 1.42);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
viewSize = THREE.MathUtils.clamp(
viewSize * Math.exp(event.deltaY * 0.001),
15,
50
);
applyProjection();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
document.querySelector("#reset-view").addEventListener("click", resetView);
document.querySelector("#top-view").addEventListener("click", () => {
target.copy(defaultTarget);
theta = 0;
phi = 0.13;
viewSize = 31;
updateCamera();
});
const gridButton = document.querySelector("#toggle-grid");
gridButton.addEventListener("click", () => {
gridGroup.visible = !gridGroup.visible;
gridButton.classList.toggle("active", gridGroup.visible);
});
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight);
applyProjection();
});
updateCamera();
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
function animate(time) {
requestAnimationFrame(animate);
const t = time * 0.001;
endpointRings.forEach((ring, index) => {
const pulse = 1 + Math.sin(t * 2.1 + index * Math.PI) * 0.075;
ring.scale.set(pulse, pulse, pulse);
});
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
requestAnimationFrame(() => loading.classList.add("hidden"));
</script>
</body>
</html>
```