Prompt specification
Build or customize the Three.js 3D scene "Plaza Mayor arcade pier and shop threshold" 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: Plaza Mayor arcade pier and shop threshold
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/plaza-mayor-arcade-pier-and-shop-threshold-x2845
Scene Description Prompt:
Plaza Mayor arcade pier and shop threshold. Build one referenced Madrid arcade bay with pier, shop doorway, and sheltered paving. Distinguish the covered perimeter path from the square's open central surface. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (plaza-mayor-arcade-pier-and-shop-threshold-x2845):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Plaza Mayor Arcade Bay — Madrid</title>
<style>
:root {
--ink: #29231f;
--paper: rgba(247, 239, 222, 0.92);
--stone: #d8c7a6;
--plaza: #918a80;
--brick: #984735;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b8c8cc;
font-family: Inter, "Segoe UI", Arial, sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
color: var(--ink);
}
.title-card {
position: absolute;
top: 22px;
left: 24px;
max-width: 340px;
padding: 15px 17px 14px;
background: var(--paper);
border: 1px solid rgba(70, 52, 39, 0.25);
border-left: 5px solid var(--brick);
box-shadow: 0 8px 30px rgba(38, 30, 23, 0.16);
backdrop-filter: blur(8px);
}
.eyebrow {
margin-bottom: 5px;
color: #8e4434;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(21px, 3vw, 30px);
font-weight: 500;
line-height: 1.05;
}
.subtitle {
margin-top: 7px;
color: #665d54;
font-size: 12px;
line-height: 1.45;
}
.legend {
position: absolute;
right: 22px;
top: 22px;
width: 220px;
padding: 13px 14px;
background: var(--paper);
border: 1px solid rgba(70, 52, 39, 0.22);
box-shadow: 0 8px 30px rgba(38, 30, 23, 0.13);
backdrop-filter: blur(8px);
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 7px 0;
color: #554c43;
font-size: 11px;
}
.swatch {
width: 22px;
height: 11px;
flex: 0 0 auto;
border: 1px solid rgba(43, 36, 30, 0.18);
}
.swatch.covered {
background:
linear-gradient(90deg, transparent 47%, rgba(64, 54, 43, 0.13) 48% 52%, transparent 53%),
var(--stone);
}
.swatch.open {
background:
repeating-linear-gradient(
90deg,
#9b9388 0 9px,
#827b72 9px 11px
);
}
.help {
position: absolute;
left: 24px;
bottom: 20px;
padding: 9px 12px;
background: rgba(247, 239, 222, 0.82);
border: 1px solid rgba(70, 52, 39, 0.2);
color: #5c5248;
font-size: 11px;
letter-spacing: 0.02em;
backdrop-filter: blur(6px);
}
#reset {
position: absolute;
right: 22px;
bottom: 20px;
padding: 9px 13px;
border: 1px solid rgba(70, 52, 39, 0.28);
background: var(--paper);
color: var(--ink);
font: inherit;
font-size: 11px;
cursor: pointer;
pointer-events: auto;
box-shadow: 0 5px 18px rgba(38, 30, 23, 0.12);
}
#reset:hover {
background: #fff8e8;
}
@media (max-width: 680px) {
.title-card {
top: 12px;
left: 12px;
max-width: 250px;
}
.legend {
top: auto;
right: auto;
left: 12px;
bottom: 48px;
width: 210px;
}
.help {
left: 12px;
bottom: 12px;
}
#reset {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly Plaza Mayor arcade bay"></canvas>
<div class="hud">
<section class="title-card">
<div class="eyebrow">Architectural study · Madrid</div>
<h1>Plaza Mayor<br />Arcade Bay</h1>
<div class="subtitle">
A stylized brick-and-granite perimeter bay with a recessed shop threshold.
</div>
</section>
<aside class="legend">
<div class="legend-row">
<span class="swatch covered"></span>
Covered perimeter path
</div>
<div class="legend-row">
<span class="swatch open"></span>
Open central plaza
</div>
</aside>
<div class="help">Drag to orbit · Wheel/pinch to zoom · R to reset</div>
<button id="reset" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
// -------------------------------------------------------------------------
// Renderer, scene, and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9c9cc);
scene.fog = new THREE.Fog(0xb9c9cc, 24, 48);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
90
);
const hemisphere = new THREE.HemisphereLight(0xd9e9ed, 0x665244, 1.75);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe4b5, 3.2);
sun.position.set(-9, 15, 10);
sun.target.position.set(0, 1.8, -7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 48;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun, sun.target);
const arcadeLight = new THREE.PointLight(0xffd19a, 24, 8, 2);
arcadeLight.position.set(0.35, 3.7, -8.05);
scene.add(arcadeLight);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const standard = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.93,
metalness: 0,
flatShading: true,
...extra
});
const materials = {
earth: standard(0x82796e),
plazaBase: standard(0x918a80),
borderStone: standard(0xb8aa91),
coveredStone: standard(0xc9b99b),
coveredStoneAlt: standard(0xb7a68a),
coveredStoneLight: standard(0xd7c6a5),
facade: standard(0x914331),
facadeDark: standard(0x713328),
mortar: standard(0x4c332c),
stone: standard(0xcbbda1),
stoneLight: standard(0xdfd0b4),
stoneDark: standard(0xa9977c),
wood: standard(0x50372b),
door: standard(0x24483d),
doorPanel: standard(0x18382f),
brass: standard(0xb18a43, { metalness: 0.35, roughness: 0.5 }),
glass: standard(0x203438, { roughness: 0.3 }),
shutter: standard(0x52645e),
mat: standard(0x594d3f)
};
const brickMaterials = [
standard(0x984735),
standard(0xa34f3a),
standard(0x8d4032),
standard(0xb1583e),
standard(0x7f382e)
];
const cobbleMaterials = [
new THREE.Color(0x9e978d),
new THREE.Color(0x918a81),
new THREE.Color(0xa8a095),
new THREE.Color(0x89837b),
new THREE.Color(0xb0a89b)
];
// -------------------------------------------------------------------------
// Small helpers
// -------------------------------------------------------------------------
let randomSeed = 92731;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function addBox(
parent,
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function makeTextTexture(title, subtitle, background = "#243b4a") {
const textCanvas = document.createElement("canvas");
textCanvas.width = 768;
textCanvas.height = 320;
const ctx = textCanvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, textCanvas.width, textCanvas.height);
ctx.strokeStyle = "#dfcba4";
ctx.lineWidth = 12;
ctx.strokeRect(16, 16, textCanvas.width - 32, textCanvas.height - 32);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#f0dfbd";
ctx.font = "600 68px Georgia";
ctx.fillText(title, 384, 128);
ctx.fillStyle = "#d6c39d";
ctx.font = "500 31px Arial";
ctx.fillText(subtitle, 384, 219);
const texture = new THREE.CanvasTexture(textCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return texture;
}
function makeSign(title, subtitle, width, height, x, y, z) {
const texture = makeTextTexture(title, subtitle);
const material = new THREE.MeshBasicMaterial({
map: texture,
side: THREE.DoubleSide
});
const sign = new THREE.Mesh(new THREE.PlaneGeometry(width, height), material);
sign.position.set(x, y, z);
scene.add(sign);
return sign;
}
// -------------------------------------------------------------------------
// Ground and open plaza surface
// -------------------------------------------------------------------------
addBox(
scene,
29,
0.14,
31,
materials.earth,
0,
-0.19,
1.0,
false,
true
);
addBox(
scene,
24,
0.06,
18,
materials.plazaBase,
0,
-0.09,
3.0,
false,
true
);
// Small, irregular cobbles form the uncovered central square.
const cobbleGeometry = new THREE.BoxGeometry(0.57, 0.09, 0.43);
const cobbleMaterial = standard(0xffffff);
const cobbleCount = 34 * 33;
const cobbles = new THREE.InstancedMesh(
cobbleGeometry,
cobbleMaterial,
cobbleCount
);
cobbles.receiveShadow = true;
cobbles.castShadow = false;
const dummy = new THREE.Object3D();
let cobbleIndex = 0;
for (let row = 0; row < 33; row++) {
for (let column = 0; column < 34; column++) {
const x = -10.17 + column * 0.62 + (random() - 0.5) * 0.035;
const z = -4.95 + row * 0.48 + (random() - 0.5) * 0.03;
dummy.position.set(
x,
-0.006 + random() * 0.012,
z
);
dummy.rotation.set(
0,
(random() - 0.5) * 0.055,
0
);
dummy.scale.set(
0.93 + random() * 0.09,
0.85 + random() * 0.2,
0.94 + random() * 0.07
);
dummy.updateMatrix();
cobbles.setMatrixAt(cobbleIndex, dummy.matrix);
cobbles.setColorAt(
cobbleIndex,
cobbleMaterials[Math.floor(random() * cobbleMaterials.length)]
);
cobbleIndex++;
}
}
cobbles.instanceMatrix.needsUpdate = true;
if (cobbles.instanceColor) cobbles.instanceColor.needsUpdate = true;
scene.add(cobbles);
// A line of larger granite blocks clearly marks the edge of the arcade.
for (let i = 0; i < 18; i++) {
addBox(
scene,
0.54,
0.075,
0.48,
i % 3 === 0 ? materials.stoneLight : materials.borderStone,
-4.585 + i * 0.54,
0.008,
-5.39,
false,
true
);
}
// -------------------------------------------------------------------------
// Sheltered perimeter paving
// -------------------------------------------------------------------------
const pathStoneMaterials = [
materials.coveredStone,
materials.coveredStoneAlt,
materials.coveredStoneLight
];
for (let row = 0; row < 4; row++) {
for (let column = 0; column < 8; column++) {
const slab = addBox(
scene,
0.97,
0.075,
0.67,
pathStoneMaterials[(row + column) % pathStoneMaterials.length],
(column - 3.5) * 1.005,
0.008,
-6.55 - row * 0.72,
false,
true
);
slab.rotation.y = (random() - 0.5) * 0.012;
}
}
// -------------------------------------------------------------------------
// Arcade architecture
// -------------------------------------------------------------------------
const facadeZ = -6.05;
const springY = 2.68;
const innerRadius = 2.25;
const outerRadius = 2.72;
// Back wall of the perimeter building.
addBox(
scene,
8.8,
8.15,
0.38,
materials.facadeDark,
0,
4.075,
-9.57,
true,
true
);
// Upper wall and cornice.
addBox(
scene,
8.55,
2.78,
0.86,
materials.facade,
0,
6.77,
facadeZ,
true,
true
);
addBox(
scene,
8.9,
0.19,
1.04,
materials.stoneLight,
0,
5.43,
facadeZ,
true,
true
);
addBox(
scene,
8.9,
0.22,
1.02,
materials.stone,
0,
8.13,
facadeZ,
true,
true
);
// Roof/ceiling over the sheltered path.
addBox(
scene,
8.45,
0.19,
3.35,
materials.wood,
0,
5.32,
-7.83,
true,
true
);
// Exposed low-poly ceiling beams.
for (const z of [-6.62, -7.32, -8.02, -8.72, -9.26]) {
addBox(
scene,
8.18,
0.12,
0.16,
materials.facadeDark,
0,
5.18,
z,
true,
true
);
}
function makeBrickPier(centerX) {
const shaftBottom = 0.72;
const shaftTop = 5.39;
const rows = 18;
const rowHeight = (shaftTop - shaftBottom) / rows;
// Recessed dark backing gives the brick joints depth.
addBox(
scene,
1.39,
shaftTop - shaftBottom,
0.55,
materials.mortar,
centerX,
(shaftBottom + shaftTop) / 2,
-6.10,
false,
true
);
for (let row = 0; row < rows; row++) {
const y = shaftBottom + row * rowHeight + rowHeight / 2;
const brickHeight = rowHeight - 0.024;
if (row % 2 === 0) {
const widths = [0.435, 0.435, 0.435];
const centers = [-0.46, 0, 0.46];
widths.forEach((width, i) => {
addBox(
scene,
width,
brickHeight,
0.68,
brickMaterials[Math.floor(random() * brickMaterials.length)],
centerX + centers[i] + (random() - 0.5) * 0.012,
y,
facadeZ,
true,
true
);
});
} else {
[-0.35, 0.35].forEach((offset) => {
addBox(
scene,
0.665,
brickHeight,
0.68,
brickMaterials[Math.floor(random() * brickMaterials.length)],
centerX + offset + (random() - 0.5) * 0.012,
y,
facadeZ,
true,
true
);
});
}
}
// Granite base.
addBox(
scene,
1.62,
0.72,
0.96,
materials.stoneDark,
centerX,
0.36,
facadeZ,
true,
true
);
addBox(
scene,
1.67,
0.10,
1.00,
materials.stone,
centerX,
0.73,
facadeZ,
true,
true
);
// Capital at the springing line of the arch.
addBox(
scene,
1.53,
0.19,
0.95,
materials.stone,
centerX,
5.34,
facadeZ,
true,
true
);
// Impost blocks project toward the opening.
const inward = centerX < 0 ? 1 : -1;
addBox(
scene,
0.47,
0.18,
0.87,
materials.stoneLight,
centerX + inward * 0.73,
springY,
facadeZ,
true,
true
);
}
makeBrickPier(-2.95);
makeBrickPier(2.95);
// -------------------------------------------------------------------------
// Semicircular brick arch
// -------------------------------------------------------------------------
function createArchGeometry(radiusInner, radiusOuter, angleStart, angleEnd) {
const shape = new THREE.Shape();
shape.moveTo(
radiusInner * Math.cos(angleStart),
radiusInner * Math.sin(angleStart)
);
shape.lineTo(
radiusOuter * Math.cos(angleStart),
radiusOuter * Math.sin(angleStart)
);
shape.absarc(0, 0, radiusOuter, angleStart, angleEnd, false);
shape.lineTo(
radiusInner * Math.cos(angleEnd),
radiusInner * Math.sin(angleEnd)
);
shape.absarc(0, 0, radiusInner, angleEnd, angleStart, true);
shape.closePath();
return new THREE.ExtrudeGeometry(shape, {
depth: 0.75,
bevelEnabled: false,
curveSegments: 2,
steps: 1
});
}
// Dark continuous ring behind the individual voussoirs.
const archBackingGeometry = new THREE.Shape();
archBackingGeometry.moveTo(innerRadius - 0.015, 0);
archBackingGeometry.lineTo(outerRadius + 0.015, 0);
archBackingGeometry.absarc(
0,
0,
outerRadius + 0.015,
0,
Math.PI,
false
);
archBackingGeometry.lineTo(-innerRadius + 0.015, 0);
archBackingGeometry.absarc(
0,
0,
innerRadius - 0.015,
Math.PI,
0,
true
);
archBackingGeometry.closePath();
const archBacking = new THREE.Mesh(
new THREE.ExtrudeGeometry(archBackingGeometry, {
depth: 0.04,
bevelEnabled: false,
curveSegments: 2
}),
materials.mortar
);
archBacking.position.set(0, springY, -6.48);
archBacking.castShadow = true;
archBacking.receiveShadow = true;
scene.add(archBacking);
const voussoirCount = 13;
const angularGap = 0.011;
for (let i = 0; i < voussoirCount; i++) {
const a0 = (i / voussoirCount) * Math.PI + angularGap / 2;
const a1 = ((i + 1) / voussoirCount) * Math.PI - angularGap / 2;
const material = i === 6
? materials.stoneLight
: brickMaterials[(i + 1) % brickMaterials.length];
const voussoir = new THREE.Mesh(
createArchGeometry(innerRadius, outerRadius, a0, a1),
material
);
voussoir.position.set(0, springY, -6.43);
voussoir.castShadow = true;
voussoir.receiveShadow = true;
scene.add(voussoir);
}
// -------------------------------------------------------------------------
// Upper window
// -------------------------------------------------------------------------
addBox(
scene,
1.56,
1.64,
0.10,
materials.glass,
0,
6.83,
-5.55,
false,
true
);
addBox(
scene,
0.16,
1.80,
0.16,
materials.stoneLight,
-0.86,
6.83,
-5.47,
true,
true
);
addBox(
scene,
0.16,
1.80,
0.16,
materials.stoneLight,
0.86,
6.83,
-5.47,
true,
true
);
addBox(
scene,
1.88,
0.18,
0.18,
materials.stoneLight,
0,
7.76,
-5.46,
true,
true
);
addBox(
scene,
1.90,
0.12,
0.45,
materials.stone,
0,
5.95,
-5.37,
true,
true
);
addBox(
scene,
0.075,
1.53,
0.10,
materials.stoneDark,
0,
6.83,
-5.46,
false,
true
);
addBox(
scene,
1.48,
0.075,
0.10,
materials.stoneDark,
0,
6.83,
-5.46,
false,
true
);
// -------------------------------------------------------------------------
// Recessed shop doorway and raised threshold
// -------------------------------------------------------------------------
// Dark recess behind the doorway.
addBox(
scene,
2.05,
3.30,
0.13,
materials.mortar,
0,
1.65,
-9.30,
false,
true
);
// Stone jambs and lintel.
addBox(
scene,
0.20,
3.18,
0.25,
materials.stoneLight,
-0.96,
1.59,
-9.12,
true,
true
);
addBox(
scene,
0.20,
3.18,
0.25,
materials.stoneLight,
0.96,
1.59,
-9.12,
true,
true
);
addBox(
scene,
2.13,
0.21,
0.27,
materials.stoneLight,
0,
3.20,
-9.11,
true,
true
);
// Green wooden door.
addBox(
scene,
1.54,
2.65,
0.10,
materials.door,
0,
1.38,
-9.17,
true,
true
);
// Recessed door panels.
for (const x of [-0.39, 0.39]) {
addBox(
scene,
0.58,
1.95,
0.025,
materials.doorPanel,
x,
1.37,
-9.105,
false,
true
);
}
addBox(
scene,
1.47,
0.07,
0.035,
materials.stoneDark,
0,
2.57,
-9.09,
false,
true
);
addBox(
scene,
0.07,
2.46,
0.035,
materials.stoneDark,
0,
1.38,
-9.09,
false,
true
);
// Transom.
addBox(
scene,
1.54,
0.34,
0.07,
materials.glass,
0,
2.91,
-9.15,
false,
true
);
// Brass handle and escutcheon.
addBox(
scene,
0.055,
0.24,
0.055,
materials.brass,
0.50,
1.43,
-9.04,
true,
true
);
addBox(
scene,
0.18,
0.055,
0.065,
materials.brass,
0.50,
1.43,
-9.025,
true,
true
);
// Raised shop threshold and lower step.
addBox(
scene,
2.02,
0.13,
0.72,
materials.stoneLight,
0,
0.065,
-8.96,
true,
true
);
addBox(
scene,
2.23,
0.07,
0.46,
materials.stone,
0,
0.035,
-8.58,
true,
true
);
// Dark entrance mat.
addBox(
scene,
0.91,
0.025,
0.44,
materials.mat,
0,
0.079,
-8.60,
false,
true
);
makeSign("PANADERÍA", "PLAZA MAYOR", 2.35, 0.47, 0, 3.57, -9.035);
// Ceramic-style location plaque on the left pier.
makeSign("PLAZA MAYOR", "MADRID", 1.17, 0.49, -2.95, 1.69, -5.565);
// Small wall lamp above the shop.
const lampArm = addBox(
scene,
0.065,
0.065,
0.52,
materials.brass,
0,
3.99,
-8.91,
true,
true
);
lampArm.rotation.x = Math.PI / 2;
const lampShade = new THREE.Mesh(
new THREE.ConeGeometry(0.24, 0.28, 6),
materials.brass
);
lampShade.position.set(0, 3.96, -8.62);
lampShade.rotation.x = Math.PI;
lampShade.castShadow = true;
scene.add(lampShade);
const lampBulb = new THREE.Mesh(
new THREE.SphereGeometry(0.10, 8, 5),
new THREE.MeshBasicMaterial({ color: 0xffd493 })
);
lampBulb.position.set(0, 3.79, -8.62);
scene.add(lampBulb);
// -------------------------------------------------------------------------
// Custom camera controls: orbit and zoom, implemented without addons
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 2.30, -7.05);
const defaultView = {
radius: 17.4,
phi: 1.235,
theta: 0.385
};
let radius = defaultView.radius;
let phi = defaultView.phi;
let theta = defaultView.theta;
function updateCamera() {
const spherical = new THREE.Spherical(radius, phi, theta);
camera.position.setFromSpherical(spherical).add(target);
camera.lookAt(target);
}
function resetCamera() {
radius = defaultView.radius;
phi = defaultView.phi;
theta = defaultView.theta;
updateCamera();
}
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
theta -= deltaX * 0.005;
phi = THREE.MathUtils.clamp(phi - deltaY * 0.0045, 0.38, 1.46);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius * Math.exp(event.deltaY * 0.001),
10,
27
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateCamera();
function render() {
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();
</script>
</body>
</html>
```