Prompt specification
Build or customize the Three.js 3D scene "Road Repair Module and Detour Sign" 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: Road Repair Module and Detour Sign
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/road-repair-module-and-detour-sign-x2109
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring maintenance closures and transport detours. Model a short closed road segment, work-zone barrier, and an alternate path marker. Keep the construction area separate from the traversable route. Stylized solid forms.
Working Three.js Starter (road-repair-module-and-detour-sign-x2109):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Road Closure & Transport Detour</title>
<style>
:root {
--ink: #17343b;
--muted: #587078;
--panel: rgba(255, 255, 255, 0.88);
--red: #d94b45;
--orange: #ee8b38;
--green: #168d72;
--green-light: #62c6a8;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe8e8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
canvas {
width: 100%;
height: 100%;
display: block;
}
canvas {
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
top: 22px;
left: 24px;
z-index: 10;
max-width: min(440px, calc(100vw - 48px));
pointer-events: none;
}
.eyebrow {
margin: 0 0 7px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 38px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.subtitle {
margin: 10px 0 0;
max-width: 390px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.legend {
position: fixed;
right: 24px;
top: 24px;
z-index: 10;
display: grid;
gap: 9px;
padding: 14px 16px;
border: 1px solid rgba(23, 52, 59, 0.1);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 12px 30px rgba(35, 64, 68, 0.09);
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #466068;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
}
.dot.red {
background: var(--red);
}
.dot.orange {
background: var(--orange);
}
.dot.green {
background: var(--green);
}
.controls {
position: fixed;
left: 50%;
bottom: 20px;
z-index: 10;
padding: 9px 14px;
border: 1px solid rgba(23, 52, 59, 0.1);
border-radius: 999px;
background: var(--panel);
color: #587078;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
transform: translateX(-50%);
box-shadow: 0 10px 25px rgba(35, 64, 68, 0.09);
pointer-events: none;
white-space: nowrap;
}
.world-label {
position: fixed;
z-index: 8;
display: flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 999px;
background: rgba(255, 255, 255, 0.88);
box-shadow: 0 8px 20px rgba(35, 64, 68, 0.12);
color: #35535b;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
transform: translate(-50%, -50%);
pointer-events: none;
white-space: nowrap;
backdrop-filter: blur(6px);
}
.world-label::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
}
#construction-label::before {
background: var(--orange);
}
#closure-label::before {
background: var(--red);
}
@media (max-width: 700px) {
.header {
top: 16px;
left: 16px;
}
.subtitle {
max-width: 280px;
}
.legend {
top: auto;
right: 12px;
bottom: 60px;
padding: 10px 12px;
}
.legend-row {
font-size: 9px;
}
.controls {
bottom: 12px;
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="header">
<p class="eyebrow">Temporary traffic management</p>
<h1>Road Closed.<br />Follow the Detour.</h1>
<p class="subtitle">
A fenced work zone blocks the original road. The green alternate route remains
physically separate from all construction activity.
</p>
</header>
<aside class="legend" aria-label="Scene legend">
<div class="legend-row"><span class="dot red"></span>Closed segment</div>
<div class="legend-row"><span class="dot orange"></span>Construction zone</div>
<div class="legend-row"><span class="dot green"></span>Alternate route</div>
</aside>
<div class="controls">Drag to orbit · Scroll to zoom · Arrow keys to pan · Double-click to reset</div>
<div id="closure-label" class="world-label">Road closure</div>
<div id="construction-label" class="world-label">Fenced work zone</div>
<div id="detour-label" class="world-label">Transport detour</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbe8e8);
scene.fog = new THREE.Fog(0xdbe8e8, 25, 48);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xf7fcff, 0x82906e, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff5df, 3.2);
sun.position.set(-9, 16, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc7e5ff, 0.8);
fill.position.set(10, 7, -12);
scene.add(fill);
const material = (color, roughness = 0.85, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const mats = {
grass: material(0xa8bd8c),
grassEdge: material(0x91a879),
shoulder: material(0xb7b4a6),
asphalt: material(0x435157),
asphaltDark: material(0x303d42),
white: material(0xf5f2e8),
line: material(0xf5d979),
orange: material(0xef8438),
red: material(0xd94b45),
steel: material(0x4d6066, 0.58, 0.22),
dark: material(0x26363b),
green: material(0x258f73),
greenLight: material(0x65c9a9),
pathBorder: material(0xe5d9bd),
dirt: material(0xb9845d),
yellow: material(0xe9ad3f),
glass: material(0x496d78, 0.25, 0.1),
tree: material(0x5f8e68),
treeLight: material(0x76a875),
trunk: material(0x8b684b)
};
function addMesh(geometry, mat, parent = scene) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function box(size, position, mat, parent = scene) {
const mesh = addMesh(new THREE.BoxGeometry(...size), mat, parent);
mesh.position.set(...position);
return mesh;
}
function cylinder(radiusTop, radiusBottom, height, position, mat, parent = scene, segments = 20) {
const mesh = addMesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
mat,
parent
);
mesh.position.set(...position);
return mesh;
}
function beamBetween(start, end, width, mat, parent) {
const a = new THREE.Vector3(...start);
const b = new THREE.Vector3(...end);
const direction = b.clone().sub(a);
const mesh = addMesh(
new THREE.BoxGeometry(width, width, direction.length()),
mat,
parent
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
direction.clone().normalize()
);
return mesh;
}
// Rounded landscape platform.
function roundedRectangleShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
const landShape = roundedRectangleShape(22, 15, 1.25);
const landGeometry = new THREE.ExtrudeGeometry(landShape, {
depth: 0.6,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.12,
bevelThickness: 0.12,
curveSegments: 12
});
landGeometry.rotateX(-Math.PI / 2);
const land = addMesh(landGeometry, mats.grass);
land.position.y = -0.6;
// Original road, shoulder, and center markings.
box([17.0, 0.10, 4.1], [0, 0.05, 0], mats.shoulder);
box([16.4, 0.12, 3.45], [0, 0.15, 0], mats.asphalt);
for (let x = -7.4; x < 7.3; x += 1.55) {
if (Math.abs(x) > 1.15) {
box([0.72, 0.025, 0.10], [x, 0.225, 0], mats.line);
}
}
// The blocked section is visibly darker beneath the barrier.
box([2.5, 0.025, 3.42], [0, 0.225, 0], mats.asphaltDark);
// Alternate route: flat ribbon geometry following a smooth curve.
const detourCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-7.6, 0.255, 0),
new THREE.Vector3(-5.6, 0.255, -1.25),
new THREE.Vector3(-5.0, 0.255, -4.35),
new THREE.Vector3(-2.2, 0.255, -5.2),
new THREE.Vector3(2.5, 0.255, -5.25),
new THREE.Vector3(5.6, 0.255, -4.5),
new THREE.Vector3(6.7, 0.255, -1.8),
new THREE.Vector3(7.6, 0.255, 0)
],
false,
"centripetal"
);
function createRibbon(curve, width, y, mat, samples = 120) {
const positions = [];
const uvs = [];
const indices = [];
for (let i = 0; i <= samples; i++) {
const t = i / samples;
const point = curve.getPoint(t);
const tangent = curve.getTangent(t).normalize();
const side = new THREE.Vector3(-tangent.z, 0, tangent.x).normalize();
const left = point.clone().addScaledVector(side, width / 2);
const right = point.clone().addScaledVector(side, -width / 2);
positions.push(left.x, y, left.z, right.x, y, right.z);
uvs.push(0, t, 1, t);
if (i < samples) {
const a = i * 2;
indices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = addMesh(geometry, mat);
mesh.castShadow = false;
return mesh;
}
createRibbon(detourCurve, 2.12, 0.245, mats.pathBorder);
createRibbon(detourCurve, 1.62, 0.265, mats.green);
// White direction chevrons on the alternate route.
function addChevron(t) {
const point = detourCurve.getPoint(t);
const tangent = detourCurve.getTangent(t).normalize();
const group = new THREE.Group();
group.position.set(point.x, 0.285, point.z);
group.rotation.y = Math.atan2(tangent.x, tangent.z);
const left = box([0.62, 0.025, 0.13], [-0.16, 0, 0], mats.white, group);
const right = box([0.62, 0.025, 0.13], [0.16, 0, 0], mats.white, group);
left.rotation.y = -0.58;
right.rotation.y = 0.58;
scene.add(group);
}
[0.22, 0.39, 0.58, 0.76].forEach(addChevron);
// Canvas-based sign textures.
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function makeSignTexture(kind) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 320;
const ctx = canvas.getContext("2d");
if (kind === "closed") {
ctx.fillStyle = "#f7f3e8";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#d94b45";
ctx.fillRect(0, 0, canvas.width, 54);
ctx.fillStyle = "#26363b";
ctx.font = "900 94px Arial, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("ROAD CLOSED", 384, 180);
ctx.strokeStyle = "#d94b45";
ctx.lineWidth = 20;
ctx.beginPath();
ctx.moveTo(95, 270);
ctx.lineTo(673, 92);
ctx.stroke();
} else {
ctx.fillStyle = "#168d72";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "#f5f2e8";
ctx.lineWidth = 12;
roundedRect(ctx, 16, 16, 736, 288, 24);
ctx.stroke();
ctx.fillStyle = "#ffffff";
ctx.font = "900 84px Arial, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("DETOUR", 330, 154);
// Strong right-pointing route arrow.
ctx.beginPath();
ctx.moveTo(500, 80);
ctx.lineTo(650, 160);
ctx.lineTo(500, 240);
ctx.lineTo(530, 160);
ctx.closePath();
ctx.fill();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function makeSign(texture, width, height, position, rotationY = 0) {
const group = new THREE.Group();
const board = addMesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshStandardMaterial({
map: texture,
roughness: 0.8,
side: THREE.DoubleSide
}),
group
);
group.position.set(...position);
group.rotation.y = rotationY;
scene.add(group);
return group;
}
// Work-zone barrier across the original road.
const barrier = new THREE.Group();
barrier.position.x = 0;
scene.add(barrier);
box([0.22, 0.72, 3.45], [0, 1.05, 0], mats.white, barrier);
for (const z of [-1.28, -0.42, 0.44, 1.3]) {
const stripe = box([0.235, 0.70, 0.27], [0.012, 1.05, z], mats.orange, barrier);
stripe.rotation.x = -0.56;
}
for (const z of [-1.25, 1.25]) {
box([0.75, 0.14, 0.34], [0, 0.29, z], mats.dark, barrier);
box([0.13, 0.85, 0.13], [0, 0.72, z], mats.steel, barrier);
}
const closedSign = makeSign(
makeSignTexture("closed"),
2.75,
0.78,
[0.12, 1.86, 0],
Math.PI / 2
);
closedSign.children[0].position.z = 0.02;
function addCone(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.24, z);
group.scale.setScalar(scale);
box([0.62, 0.08, 0.62], [0, 0.04, 0], mats.dark, group);
cylinder(0.13, 0.25, 0.72, [0, 0.43, 0], mats.orange, group, 16);
cylinder(0.145, 0.18, 0.17, [0, 0.48, 0], mats.white, group, 16);
scene.add(group);
}
addCone(-1.15, -1.5);
addCone(-0.25, -1.65);
addCone(0.8, -1.55);
addCone(1.25, 1.48, 0.9);
addCone(-1.3, 1.55, 0.9);
// Construction area on the opposite side of the road from the detour.
const zone = { minX: 1.55, maxX: 6.75, minZ: 2.55, maxZ: 6.0 };
box(
[zone.maxX - zone.minX, 0.12, zone.maxZ - zone.minZ],
[(zone.minX + zone.maxX) / 2, 0.07, (zone.minZ + zone.maxZ) / 2],
mats.dirt
);
// Low separator blocks make the separation from the traversable road explicit.
for (let x = 1.75; x < 6.6; x += 1.2) {
box([0.88, 0.38, 0.38], [x, 0.28, 2.42], mats.orange);
box([0.33, 0.39, 0.39], [x - 0.24, 0.28, 2.42], mats.white);
}
// Fence posts and rails.
const fenceXs = [1.55, 2.72, 3.9, 5.08, 6.26, 6.75];
const fenceZs = [2.55, 3.7, 4.85, 6.0];
fenceXs.forEach((x) => {
fenceZs.forEach((z) => {
cylinder(0.055, 0.065, 1.55, [x, 0.8, z], mats.steel);
});
});
fenceZs.forEach((z) => {
box([5.2, 0.09, 0.09], [4.15, 1.55, z], mats.orange);
box([5.2, 0.06, 0.06], [4.15, 0.62, z], mats.steel);
});
[1.55, 6.75].forEach((x) => {
box([0.09, 0.09, 3.45], [x, 1.55, 4.275], mats.orange);
box([0.06, 0.06, 3.45], [x, 0.62, 4.275], mats.steel);
});
// Simplified translucent safety panels.
const netMaterial = new THREE.MeshStandardMaterial({
color: 0xef8438,
roughness: 0.8,
transparent: true,
opacity: 0.18,
depthWrite: false
});
box([5.15, 1.25, 0.025], [4.15, 0.98, 6.0], netMaterial);
box([0.025, 1.25, 3.4], [1.55, 0.98, 4.275], netMaterial);
box([0.025, 1.25, 3.4], [6.75, 0.98, 4.275], netMaterial);
// Stylized excavator inside the fenced zone.
const excavator = new THREE.Group();
excavator.position.set(4.15, 0.15, 4.25);
excavator.rotation.y = -0.18;
scene.add(excavator);
box([2.3, 0.38, 0.34], [0, 0.23, -0.58], mats.dark, excavator);
box([2.3, 0.38, 0.34], [0, 0.23, 0.58], mats.dark, excavator);
for (const z of [-0.58, 0.58]) {
for (let x = -0.85; x <= 0.86; x += 0.42) {
cylinder(0.16, 0.16, 0.37, [x, 0.23, z], mats.steel, excavator, 16);
}
}
box([1.8, 0.66, 1.18], [0, 0.73, 0], mats.yellow, excavator);
box([0.78, 1.02, 0.96], [-0.43, 1.48, 0], mats.yellow, excavator);
box([0.60, 0.54, 1.00], [-0.43, 1.58, 0], mats.glass, excavator);
box([1.05, 0.13, 1.25], [-0.05, 2.02, 0], mats.yellow, excavator);
beamBetween([0.55, 1.08, 0.12], [1.4, 2.25, 0.12], 0.28, mats.yellow, excavator);
beamBetween([1.4, 2.25, 0.12], [2.35, 1.28, 0.12], 0.24, mats.yellow, excavator);
beamBetween([2.28, 1.35, 0.12], [2.55, 0.72, 0.12], 0.16, mats.steel, excavator);
box([0.65, 0.48, 0.82], [2.55, 0.64, 0.12], mats.dark, excavator);
addCone(2.2, 3.25, 0.85);
addCone(6.1, 3.2, 0.85);
addCone(6.05, 5.35, 0.85);
// Stacked solid pipes in the work area.
for (let row = 0; row < 2; row++) {
for (let i = 0; i < 3 - row; i++) {
const pipe = cylinder(
0.23,
0.23,
1.25,
[2.45 + i * 0.48 + row * 0.24, 0.52 + row * 0.42, 5.38],
mats.steel
);
pipe.rotation.z = Math.PI / 2;
}
}
// Alternate path marker sign and posts.
const detourSign = makeSign(
makeSignTexture("detour"),
3.0,
1.1,
[-5.45, 2.0, -2.15],
0.18
);
cylinder(0.075, 0.09, 1.55, [-5.45, 1.05, -2.15], mats.steel);
box([0.75, 0.16, 0.18], [-5.45, 0.27, -2.15], mats.green);
// Route entry and exit bollards.
function addRoutePost(x, z) {
cylinder(0.11, 0.14, 1.0, [x, 0.72, z], mats.green);
cylinder(0.115, 0.115, 0.17, [x, 1.02, z], mats.white);
box([0.5, 0.12, 0.5], [x, 0.25, z], mats.green);
}
addRoutePost(-7.05, -0.65);
addRoutePost(-6.95, 0.65);
addRoutePost(7.05, -0.65);
addRoutePost(7.05, 0.65);
// Small stylized trees frame the scene without obstructing the route.
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
cylinder(0.13, 0.18, 1.25, [0, 0.63, 0], mats.trunk, group, 10);
const crown = addMesh(new THREE.IcosahedronGeometry(0.9, 1), mats.tree, group);
crown.position.y = 1.75;
crown.scale.set(1, 1.18, 1);
scene.add(group);
}
addTree(-9.1, 5.45, 0.95);
addTree(-8.1, 6.15, 0.72);
addTree(9.1, -5.6, 0.9);
addTree(8.4, 5.9, 0.7);
// Projected HTML labels.
const labels = [
{ element: document.querySelector("#closure-label"), position: new THREE.Vector3(0, 2.65, 0) },
{ element: document.querySelector("#construction-label"), position: new THREE.Vector3(4.2, 3.0, 4.6) },
{ element: document.querySelector("#detour-label"), position: new THREE.Vector3(-0.7, 1.2, -5.2) }
];
function updateLabels() {
for (const label of labels) {
const projected = label.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.display = visible ? "flex" : "none";
label.element.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
}
// Custom orbit controls, implemented without Three.js addons.
const controls = {
target: new THREE.Vector3(0, 0.25, 0),
radius: 20,
theta: 0.82,
phi: 0.92,
dragging: false,
lastX: 0,
lastY: 0
};
function resetCamera() {
controls.target.set(0, 0.25, 0);
controls.radius = 20;
controls.theta = 0.82;
controls.phi = 0.92;
}
function updateCamera() {
controls.phi = THREE.MathUtils.clamp(controls.phi, 0.42, 1.35);
controls.radius = THREE.MathUtils.clamp(controls.radius, 9, 31);
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const dx = event.clientX - controls.lastX;
const dy = event.clientY - controls.lastY;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
controls.theta -= dx * 0.006;
controls.phi -= dy * 0.005;
updateCamera();
});
function stopDragging(event) {
controls.dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
addEventListener("keydown", (event) => {
const pan = 0.35;
if (event.key === "ArrowLeft") {
controls.target.x -= pan;
} else if (event.key === "ArrowRight") {
controls.target.x += pan;
} else if (event.key === "ArrowUp") {
controls.target.z -= pan;
} else if (event.key === "ArrowDown") {
controls.target.z += pan;
} else if (event.key === "+" || event.key === "=") {
controls.radius -= 1;
} else if (event.key === "-" || event.key === "_") {
controls.radius += 1;
}
updateCamera();
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateCamera();
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
updateLabels();
}
animate();
</script>
</body>
</html>
```