Prompt specification
Build or customize the Three.js 3D scene "Two Concentric Curved Lane Segments" 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 Concentric Curved Lane Segments
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-concentric-curved-lane-segments-x3669
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts curved running-track lanes and arc lengths. Model two parallel curved lanes with marked radii and endpoints. Show that equal central angles produce different arc lengths when the radii differ. Stylized solid forms.
Working Three.js Starter (two-concentric-curved-lane-segments-x3669):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Arc Length Lab — Equal Angles, Different Lengths</title>
<style>
:root {
--ink: #26364a;
--paper: #f8f4eb;
--coral: #ef705f;
--teal: #269d98;
--gold: #e9b343;
--muted: #687383;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e4da;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 20;
top: 24px;
left: 24px;
width: min(330px, calc(100vw - 48px));
padding: 22px;
border: 1px solid rgba(38, 54, 74, 0.13);
border-radius: 22px;
background: rgba(248, 244, 235, 0.9);
box-shadow:
0 18px 50px rgba(38, 54, 74, 0.13),
inset 0 1px 0 rgba(255, 255, 255, 0.8);
backdrop-filter: blur(16px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 10px;
color: #7c8490;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 3vw, 37px);
font-weight: 780;
letter-spacing: -0.045em;
line-height: 1.02;
}
.intro {
margin: 13px 0 17px;
color: #596575;
font-size: 13px;
line-height: 1.55;
}
.shared-angle {
display: flex;
align-items: center;
gap: 9px;
width: fit-content;
margin-bottom: 17px;
padding: 8px 11px;
border: 1px solid rgba(184, 126, 28, 0.25);
border-radius: 999px;
background: rgba(233, 179, 67, 0.17);
color: #6f5119;
font-size: 12px;
font-weight: 750;
}
.shared-angle::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 0 4px rgba(233, 179, 67, 0.16);
content: "";
}
.equation {
margin: 0 0 15px;
padding: 12px 14px;
border-radius: 13px;
background: #26364a;
color: #fffaf0;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 16px;
text-align: center;
}
.equation small {
display: block;
margin-top: 4px;
color: #bfc8d2;
font-family: inherit;
font-size: 10px;
}
.lane-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 3px 12px;
margin-top: 9px;
padding: 11px 13px;
border: 1px solid rgba(38, 54, 74, 0.09);
border-radius: 13px;
background: rgba(255, 255, 255, 0.55);
}
.lane-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 12px;
font-weight: 800;
}
.lane-name::before {
width: 10px;
height: 10px;
border-radius: 3px;
background: var(--coral);
content: "";
}
.lane-row.teal .lane-name::before {
background: var(--teal);
}
.lane-length {
align-self: center;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 13px;
font-weight: 800;
}
.lane-radius {
grid-column: 1 / -1;
color: var(--muted);
font-size: 10px;
}
.result {
margin-top: 13px;
color: #536171;
font-size: 11px;
line-height: 1.5;
text-align: center;
}
.result strong {
color: var(--ink);
}
#labels {
position: fixed;
inset: 0;
z-index: 10;
overflow: hidden;
pointer-events: none;
}
.tag {
position: absolute;
top: 0;
left: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
user-select: none;
}
.length-tag {
padding: 8px 11px;
border: 1px solid rgba(38, 54, 74, 0.12);
border-radius: 10px;
background: rgba(255, 250, 240, 0.94);
box-shadow: 0 7px 20px rgba(38, 54, 74, 0.13);
color: var(--ink);
font-size: 12px;
font-weight: 820;
letter-spacing: -0.01em;
}
.length-tag.coral {
border-left: 4px solid var(--coral);
}
.length-tag.teal {
border-left: 4px solid var(--teal);
}
.radius-tag {
padding: 5px 8px;
border: 1px solid rgba(38, 54, 74, 0.12);
border-radius: 8px;
background: rgba(248, 244, 235, 0.92);
color: #3f4c5b;
font-size: 10px;
font-weight: 800;
box-shadow: 0 4px 12px rgba(38, 54, 74, 0.08);
}
.angle-tag {
padding: 7px 10px;
border: 1px solid rgba(119, 82, 19, 0.25);
border-radius: 9px;
background: rgba(246, 201, 105, 0.95);
box-shadow: 0 6px 18px rgba(119, 82, 19, 0.15);
color: #44330f;
font-size: 13px;
font-weight: 900;
}
.endpoint-tag {
display: flex;
align-items: center;
justify-content: center;
width: 27px;
height: 27px;
border: 2px solid rgba(255, 255, 255, 0.9);
border-radius: 50%;
background: var(--ink);
box-shadow: 0 5px 14px rgba(38, 54, 74, 0.25);
color: white;
font-size: 10px;
font-weight: 850;
}
#reset {
position: fixed;
z-index: 25;
top: 24px;
right: 24px;
width: 42px;
height: 42px;
border: 1px solid rgba(38, 54, 74, 0.14);
border-radius: 50%;
background: rgba(248, 244, 235, 0.9);
box-shadow: 0 8px 24px rgba(38, 54, 74, 0.12);
color: var(--ink);
font-size: 20px;
line-height: 1;
cursor: pointer;
backdrop-filter: blur(12px);
transition: transform 160ms ease, background 160ms ease;
}
#reset:hover {
transform: rotate(-18deg) scale(1.05);
background: #fffaf0;
}
.hint {
position: fixed;
z-index: 20;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
padding: 10px 15px;
border: 1px solid rgba(38, 54, 74, 0.11);
border-radius: 999px;
background: rgba(248, 244, 235, 0.83);
box-shadow: 0 7px 22px rgba(38, 54, 74, 0.09);
color: #66717e;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
white-space: nowrap;
pointer-events: none;
backdrop-filter: blur(12px);
}
.hint span {
margin: 0 6px;
color: #a3a9af;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: 250px;
padding: 16px;
border-radius: 17px;
}
h1 {
font-size: 25px;
}
.intro {
margin: 9px 0 12px;
font-size: 11px;
}
.shared-angle {
margin-bottom: 11px;
padding: 6px 9px;
font-size: 10px;
}
.equation {
margin-bottom: 10px;
padding: 9px;
font-size: 14px;
}
.lane-row {
padding: 8px 10px;
}
.result {
display: none;
}
#reset {
top: 12px;
right: 12px;
}
.hint {
bottom: 12px;
font-size: 9px;
}
.radius-tag {
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels"></div>
<section class="panel" aria-label="Arc length explanation">
<div class="eyebrow">Geometry Lab / Arc Length</div>
<h1>Same angle.<br />Different distance.</h1>
<p class="intro">
These concentric lanes remain parallel, but their centerline radii differ.
Both subtend the same central angle.
</p>
<div class="shared-angle">Shared angle Δθ = 100°</div>
<div class="equation">
s = rθ
<small>θ measured in radians</small>
</div>
<div class="lane-row">
<div class="lane-name">Lane 1</div>
<div class="lane-length">s₁ = 10.47 m</div>
<div class="lane-radius">centerline radius r₁ = 6.0 m</div>
</div>
<div class="lane-row teal">
<div class="lane-name">Lane 2</div>
<div class="lane-length">s₂ = 15.18 m</div>
<div class="lane-radius">centerline radius r₂ = 8.7 m</div>
</div>
<div class="result">
The outer arc is <strong>4.71 m longer</strong><br />
and <strong>1.45×</strong> the inner arc.
</div>
</section>
<button id="reset" title="Reset camera" aria-label="Reset camera">↻</button>
<div class="hint">
Drag to orbit <span>·</span> Scroll to zoom <span>·</span> Double-click to reset
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const COLORS = {
background: 0xe9e4da,
floor: 0xdfd9cd,
base: 0xd9d2c5,
cream: 0xfffaf0,
ink: 0x26364a,
coral: 0xef705f,
coralDark: 0xa7433c,
teal: 0x269d98,
tealDark: 0x116966,
gold: 0xe9b343,
goldDark: 0x9b691a
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.background);
scene.fog = new THREE.Fog(COLORS.background, 35, 65);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D comparison of two curved track lanes"
);
document.getElementById("scene").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff7e8, 0x7e8996, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d9, 3.3);
keyLight.position.set(-9, 18, 11);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 16;
keyLight.shadow.camera.bottom = -16;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 45;
keyLight.shadow.bias = -0.00015;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xbdd8ff, 1.1);
fillLight.position.set(12, 8, -10);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Geometry constants
// -------------------------------------------------------------------------
const thetaDegrees = 100;
const theta = THREE.MathUtils.degToRad(thetaDegrees);
const startAngle = THREE.MathUtils.degToRad(-50);
const endAngle = THREE.MathUtils.degToRad(50);
const laneWidth = 1.45;
const trackThickness = 0.32;
const laneDefinitions = [
{
id: 1,
radius: 6.0,
color: COLORS.coral,
darkColor: COLORS.coralDark
},
{
id: 2,
radius: 8.7,
color: COLORS.teal,
darkColor: COLORS.tealDark
}
];
function polar(radius, angle, y = 0) {
return new THREE.Vector3(
radius * Math.cos(angle),
y,
radius * Math.sin(angle)
);
}
function makeTrack(lane) {
const innerRadius = lane.radius - laneWidth / 2;
const outerRadius = lane.radius + laneWidth / 2;
const shape = new THREE.Shape();
shape.moveTo(
innerRadius * Math.cos(startAngle),
innerRadius * Math.sin(startAngle)
);
shape.absarc(0, 0, innerRadius, startAngle, endAngle, false);
shape.lineTo(
outerRadius * Math.cos(endAngle),
outerRadius * Math.sin(endAngle)
);
shape.absarc(0, 0, outerRadius, endAngle, startAngle, true);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: trackThickness,
steps: 1,
curveSegments: 112,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.055,
bevelThickness: 0.035
});
const material = new THREE.MeshStandardMaterial({
color: lane.color,
roughness: 0.72,
metalness: 0.02
});
const mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = Math.PI / 2;
mesh.position.y = 0.46;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
// Pale edge stripes emphasize the solid lane surfaces.
for (const radius of [innerRadius, outerRadius]) {
const points = [];
for (let i = 0; i <= 120; i++) {
const angle = THREE.MathUtils.lerp(startAngle, endAngle, i / 120);
points.push(polar(radius, angle, 0.505));
}
const edgeGeometry = new THREE.BufferGeometry().setFromPoints(points);
const edgeMaterial = new THREE.LineBasicMaterial({
color: COLORS.cream,
transparent: true,
opacity: 0.78
});
scene.add(new THREE.Line(edgeGeometry, edgeMaterial));
}
// The dark curved line is the measured centerline.
const centerPoints = [];
for (let i = 0; i <= 140; i++) {
const angle = THREE.MathUtils.lerp(startAngle, endAngle, i / 140);
centerPoints.push(polar(lane.radius, angle, 0.535));
}
const centerGeometry = new THREE.BufferGeometry().setFromPoints(centerPoints);
const centerMaterial = new THREE.LineBasicMaterial({
color: lane.darkColor,
transparent: true,
opacity: 0.95
});
scene.add(new THREE.Line(centerGeometry, centerMaterial));
// Radial start/end bars across each lane.
for (const angle of [startAngle, endAngle]) {
const bar = new THREE.Mesh(
new THREE.BoxGeometry(laneWidth * 0.92, 0.035, 0.13),
new THREE.MeshStandardMaterial({
color: COLORS.cream,
roughness: 0.8
})
);
bar.position.copy(polar(lane.radius, angle, 0.525));
bar.rotation.y = -angle;
bar.castShadow = true;
scene.add(bar);
}
}
function makeEndpoint(lane, angle, name) {
const group = new THREE.Group();
group.position.copy(polar(lane.radius, angle, 0));
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.24, 0.065, 24),
new THREE.MeshStandardMaterial({
color: COLORS.ink,
roughness: 0.65
})
);
base.position.y = 0.535;
base.castShadow = true;
group.add(base);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.28, 12),
new THREE.MeshStandardMaterial({
color: COLORS.ink,
roughness: 0.6
})
);
stem.position.y = 0.69;
stem.castShadow = true;
group.add(stem);
const cap = new THREE.Mesh(
new THREE.SphereGeometry(0.17, 20, 14),
new THREE.MeshStandardMaterial({
color: lane.color,
roughness: 0.48
})
);
cap.position.y = 0.87;
cap.castShadow = true;
group.add(cap);
scene.add(group);
return group;
}
function makeArcLine(radius, height, color, opacity = 1, segments = 120) {
const points = [];
for (let i = 0; i <= segments; i++) {
const angle = THREE.MathUtils.lerp(startAngle, endAngle, i / segments);
points.push(polar(radius, angle, height));
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
transparent: opacity < 1,
opacity
});
return new THREE.Line(geometry, material);
}
// -------------------------------------------------------------------------
// Pedestal, floor, and reference geometry
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.MeshStandardMaterial({
color: COLORS.floor,
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.32;
floor.receiveShadow = true;
scene.add(floor);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(11.35, 11.35, 0.28, 128),
new THREE.MeshStandardMaterial({
color: COLORS.base,
roughness: 0.88,
metalness: 0.01
})
);
pedestal.position.y = -0.16;
pedestal.receiveShadow = true;
pedestal.castShadow = true;
scene.add(pedestal);
// Subtle concentric construction circles.
for (const radius of [2.8, 4.2]) {
const points = [];
for (let i = 0; i <= 160; i++) {
const angle = (i / 160) * Math.PI * 2;
points.push(polar(radius, angle, 0.012));
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color: 0x89919a,
transparent: true,
opacity: 0.18
});
scene.add(new THREE.LineLoop(geometry, material));
}
// Central-angle wedge.
const wedgeShape = new THREE.Shape();
wedgeShape.moveTo(0, 0);
wedgeShape.lineTo(
2.25 * Math.cos(startAngle),
2.25 * Math.sin(startAngle)
);
wedgeShape.absarc(0, 0, 2.25, startAngle, endAngle, false);
wedgeShape.closePath();
const wedge = new THREE.Mesh(
new THREE.ShapeGeometry(wedgeShape, 96),
new THREE.MeshBasicMaterial({
color: COLORS.gold,
transparent: true,
opacity: 0.2,
side: THREE.DoubleSide,
depthWrite: false
})
);
wedge.rotation.x = -Math.PI / 2;
wedge.position.y = 0.005;
scene.add(wedge);
scene.add(makeArcLine(2.25, 0.04, COLORS.goldDark, 0.95));
// Dashed rays from the common center to both angular boundaries.
for (const angle of [startAngle, endAngle]) {
const geometry = new THREE.BufferGeometry().setFromPoints([
polar(0, angle, 0.55),
polar(10.15, angle, 0.55)
]);
const material = new THREE.LineDashedMaterial({
color: COLORS.ink,
dashSize: 0.23,
gapSize: 0.15,
transparent: true,
opacity: 0.56
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
scene.add(line);
}
// Central marker.
const centerBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.32, 0.12, 28),
new THREE.MeshStandardMaterial({
color: COLORS.gold,
roughness: 0.6
})
);
centerBase.position.y = 0.08;
centerBase.castShadow = true;
scene.add(centerBase);
const centerPin = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 20, 14),
new THREE.MeshStandardMaterial({
color: COLORS.ink,
roughness: 0.5
})
);
centerPin.position.y = 0.25;
centerPin.castShadow = true;
scene.add(centerPin);
// Directional arrowheads on the central-angle arc.
function addAngleArrow(angle, direction) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.105, 0.29, 3),
new THREE.MeshStandardMaterial({
color: COLORS.goldDark,
roughness: 0.65
})
);
arrow.position.copy(polar(2.25, angle, 0.12));
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
scene.add(arrow);
}
addAngleArrow(
startAngle,
new THREE.Vector3(-Math.sin(startAngle), 0, Math.cos(startAngle))
);
addAngleArrow(
endAngle,
new THREE.Vector3(Math.sin(endAngle), 0, -Math.cos(endAngle))
);
// Build the two lanes and their endpoint markers.
laneDefinitions.forEach((lane) => {
makeTrack(lane);
makeEndpoint(lane, startAngle, `A${lane.id}`);
makeEndpoint(lane, endAngle, `B${lane.id}`);
});
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const labelRoot = document.getElementById("labels");
const labels = [];
function addLabel(text, position, className = "tag", offset = [0, 0]) {
const element = document.createElement("div");
element.className = className;
element.textContent = text;
labelRoot.appendChild(element);
labels.push({
element,
position: position.clone(),
offset
});
}
addLabel(
"100°",
new THREE.Vector3(0, 0.72, 1.12),
"tag angle-tag",
[0, 0]
);
laneDefinitions.forEach((lane) => {
const labelAngle = THREE.MathUtils.degToRad(16);
const arcLength =
lane.radius * theta;
addLabel(
`s${lane.id} = ${arcLength.toFixed(2)} m`,
polar(lane.radius, labelAngle, 0.88),
`tag length-tag ${lane.id === 1 ? "coral" : "teal"}`,
[0, 0]
);
addLabel(
`r${lane.id} = ${lane.radius.toFixed(1)} m`,
polar(lane.id === 1 ? 5.15 : 7.7, startAngle, 0.92),
"tag radius-tag",
lane.id === 1 ? [-52, -18] : [-62, -18]
);
addLabel(
`A${lane.id}`,
polar(lane.radius, startAngle, 1.08),
"tag endpoint-tag",
[16, 10]
);
addLabel(
`B${lane.id}`,
polar(lane.radius, endAngle, 1.08),
"tag endpoint-tag",
[16, 10]
);
});
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
projected.copy(label.position).project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width + label.offset[0];
const y = (-projected.y * 0.5 + 0.5) * height + label.offset[1];
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, reset
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 0, 1.0);
const initialCamera = {
radius: 30,
polar: 0.94,
azimuth: 0.47
};
const cameraState = { ...initialCamera };
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const sinPolar = Math.sin(cameraState.polar);
camera.position.set(
target.x +
cameraState.radius * sinPolar * Math.sin(cameraState.azimuth),
target.y + cameraState.radius * Math.cos(cameraState.polar),
target.z +
cameraState.radius * sinPolar * Math.cos(cameraState.azimuth)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.radius = initialCamera.radius;
cameraState.polar = initialCamera.polar;
cameraState.azimuth = initialCamera.azimuth;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
cameraState.azimuth -= deltaX * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + deltaY * 0.005,
0.34,
1.38
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
event &&
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("lostpointercapture", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
15,
46
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetCamera();
} else if (event.key === "ArrowLeft") {
cameraState.azimuth += 0.08;
updateCamera();
} else if (event.key === "ArrowRight") {
cameraState.azimuth -= 0.08;
updateCamera();
} else if (event.key === "ArrowUp") {
cameraState.radius = Math.max(15, cameraState.radius - 1);
updateCamera();
} else if (event.key === "ArrowDown") {
cameraState.radius = Math.min(46, cameraState.radius + 1);
updateCamera();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
updateCamera();
renderer.setAnimationLoop(() => {
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```