Prompt specification
Build or customize the Three.js 3D scene "Ouro Preto stone stair and balustrade detail" 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: Ouro Preto stone stair and balustrade detail
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/ouro-preto-stone-stair-and-balustrade-detail-x2681
Scene Description Prompt:
A voxel Ouro Preto stone stair and balustrade, with level treads beside a sloping street. Three.js from cdnjs, one HTML file.
Working Three.js Starter (ouro-preto-stone-stair-and-balustrade-detail-x2681):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ouro Preto — Voxel Stone Stair</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 25% 12%, #f4dfbd 0%, transparent 38%),
linear-gradient(180deg, #cdbb9d 0%, #9c8a72 100%);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.title {
position: fixed;
top: 28px;
left: 30px;
z-index: 2;
color: #fff9eb;
text-shadow: 0 2px 14px rgba(42, 29, 18, 0.48);
pointer-events: none;
user-select: none;
}
.eyebrow {
margin-bottom: 7px;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.24em;
opacity: 0.78;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 4vw, 48px);
font-weight: 500;
line-height: 0.95;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 10px;
font-size: 11px;
letter-spacing: 0.08em;
opacity: 0.72;
}
.help {
position: fixed;
left: 30px;
bottom: 25px;
z-index: 2;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 8px;
color: rgba(255, 249, 235, 0.85);
background: rgba(38, 31, 25, 0.28);
backdrop-filter: blur(8px);
font-size: 10px;
line-height: 1.5;
letter-spacing: 0.04em;
pointer-events: none;
user-select: none;
}
.help strong {
color: #fff9eb;
font-weight: 700;
}
#reset {
position: fixed;
top: 28px;
right: 28px;
z-index: 3;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.28);
border-radius: 7px;
color: #fff9eb;
background: rgba(49, 39, 29, 0.3);
backdrop-filter: blur(8px);
font: 600 10px/1 inherit;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
transition: background 160ms ease, transform 160ms ease;
}
#reset:hover {
background: rgba(49, 39, 29, 0.55);
transform: translateY(-1px);
}
#loading {
position: fixed;
inset: 0;
z-index: 5;
display: grid;
place-items: center;
color: #fff9eb;
background: #9c8a72;
font-size: 11px;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 500ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 650px) {
.title {
top: 20px;
left: 20px;
}
.help {
left: 20px;
right: 20px;
bottom: 18px;
}
#reset {
top: 20px;
right: 20px;
}
}
</style>
</head>
<body>
<div class="title">
<div class="eyebrow">OURO PRETO · MINING TOWN</div>
<h1>Escadaria<br>de Pedra</h1>
<div class="subtitle">Voxel stone stair and balustrade</div>
</div>
<button id="reset" type="button">Reset view</button>
<div class="help">
<strong>Drag</strong> orbit ·
<strong>Wheel / pinch</strong> zoom ·
<strong>Right-drag / Shift-drag</strong> pan
</div>
<div id="loading">Carregando a cena…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.body;
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: 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;
container.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xc8b596, 18, 48);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const clock = new THREE.Clock();
// Deterministic pseudo-random values keep the composition stable.
let randomSeed = 73129;
function random() {
randomSeed |= 0;
randomSeed = (randomSeed + 0x6D2B79F5) | 0;
let t = Math.imul(randomSeed ^ (randomSeed >>> 15), 1 | randomSeed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
const randomBetween = (min, max) => min + (max - min) * random();
const hemisphere = new THREE.HemisphereLight(0xffe8bd, 0x50483f, 2.05);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffdda4, 3.25);
sun.position.set(-7, 13, -9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 38;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0xb8c9d6, 0.72);
coolFill.position.set(10, 7, 8);
scene.add(coolFill);
const stoneColors = [
0x77766d,
0x858278,
0x696b65,
0x918b7e,
0x777166,
0x9a9283
];
const stoneMaterials = stoneColors.map(color =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.96,
metalness: 0.02
})
);
const treadMaterial = new THREE.MeshStandardMaterial({
color: 0x999184,
roughness: 0.97
});
const darkStoneMaterial = new THREE.MeshStandardMaterial({
color: 0x555852,
roughness: 1
});
const roadMaterial = new THREE.MeshStandardMaterial({
color: 0x55524b,
roughness: 1
});
const plasterMaterial = new THREE.MeshStandardMaterial({
color: 0xc98254,
roughness: 0.92
});
const plasterLightMaterial = new THREE.MeshStandardMaterial({
color: 0xe0b57c,
roughness: 0.94
});
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0x3f241b,
roughness: 0.88
});
const shutterMaterial = new THREE.MeshStandardMaterial({
color: 0x48625a,
roughness: 0.9
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: 0x944b35,
roughness: 0.96
});
const glassMaterial = new THREE.MeshStandardMaterial({
color: 0x202728,
roughness: 0.45,
metalness: 0.08
});
const warmLightMaterial = new THREE.MeshStandardMaterial({
color: 0xffcf73,
emissive: 0xffa52e,
emissiveIntensity: 1.35,
roughness: 0.65
});
function addBox(
width,
height,
depth,
material,
x,
y,
z,
parent = scene,
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 addBeam(start, end, width, height, material) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.BoxGeometry(width, height, length);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Ground below the street and stair.
addBox(
50,
0.18,
50,
new THREE.MeshStandardMaterial({
color: 0x8f7c61,
roughness: 1
}),
0,
-0.17,
0,
scene,
false,
true
);
// Sloping street.
const slope = 0.34 / 0.72;
const roadAngle = Math.atan(slope);
function roadHeight(z) {
return slope * (z + 4.32) - 0.05;
}
const roadGeometry = new THREE.PlaneGeometry(7.4, 18, 1, 48);
roadGeometry.rotateX(-Math.PI / 2);
const roadPositions = roadGeometry.attributes.position;
for (let i = 0; i < roadPositions.count; i++) {
const z = roadPositions.getZ(i);
roadPositions.setY(i, roadHeight(z) - 0.025);
}
roadGeometry.computeVertexNormals();
const road = new THREE.Mesh(roadGeometry, roadMaterial);
road.position.x = 5.12;
road.receiveShadow = true;
scene.add(road);
// Individual raised cobblestones, keeping the street visibly voxel-based.
const cobbleGeometry = new THREE.BoxGeometry(0.47, 0.085, 0.405);
const cobbleMaterial = new THREE.MeshStandardMaterial({
color: 0x68635a,
roughness: 1
});
const columns = 14;
const rows = 39;
const cobbles = new THREE.InstancedMesh(
cobbleGeometry,
cobbleMaterial,
columns * rows
);
const dummy = new THREE.Object3D();
const cobbleColor = new THREE.Color();
let cobbleIndex = 0;
for (let row = 0; row < rows; row++) {
const z = -8.55 + row * 0.445;
for (let column = 0; column < columns; column++) {
const x = 1.76 + column * 0.505 + (row % 2) * 0.045;
dummy.position.set(
x,
roadHeight(z) + randomBetween(0.015, 0.04),
z
);
dummy.rotation.set(-roadAngle, 0, 0);
dummy.scale.set(
randomBetween(0.91, 1.06),
randomBetween(0.82, 1.12),
randomBetween(0.91, 1.06)
);
dummy.updateMatrix();
cobbles.setMatrixAt(cobbleIndex, dummy.matrix);
const shade = randomBetween(0.73, 1.12);
cobbleColor.setRGB(
0.38 * shade,
0.36 * shade,
0.33 * shade
);
cobbles.setColorAt(cobbleIndex, cobbleColor);
cobbleIndex++;
}
}
cobbles.instanceMatrix.needsUpdate = true;
if (cobbles.instanceColor) cobbles.instanceColor.needsUpdate = true;
cobbles.receiveShadow = true;
cobbles.castShadow = false;
scene.add(cobbles);
// Narrow raised curb between the staircase walkway and street.
for (let i = 0; i < 36; i++) {
const z = -8.5 + i * 0.48;
const block = addBox(
0.43,
0.23,
0.455,
stoneMaterials[i % stoneMaterials.length],
1.43,
roadHeight(z) + 0.11,
z
);
block.rotation.x = -roadAngle;
}
// Sloped stone walkway immediately beside the stair.
for (let i = 0; i < 20; i++) {
const z = -4.48 + i * 0.445;
const slab = addBox(
0.78,
0.16,
0.425,
stoneMaterials[(i + 2) % stoneMaterials.length],
1.0,
roadHeight(z) + 0.075,
z
);
slab.rotation.x = -roadAngle;
}
// Main voxel staircase.
const stepCount = 12;
const rise = 0.34;
const run = 0.72;
const stairCenterX = -1.8;
const stairWidth = 4.2;
const stairStartZ = -4.32;
const finalBodyTop = stepCount * rise;
const finalTreadTop = finalBodyTop + 0.12;
for (let i = 0; i < stepCount; i++) {
const bodyTop = (i + 1) * rise;
const centerZ = stairStartZ + i * run + run / 2;
// Solid stair mass.
addBox(
stairWidth,
bodyTop,
run + 0.015,
stoneMaterials[i % stoneMaterials.length],
stairCenterX,
bodyTop / 2,
centerZ
);
// Riser divided into large masonry blocks.
const riserZ = stairStartZ + i * run - 0.018;
const riserBase = i * rise;
for (let block = 0; block < 3; block++) {
addBox(
stairWidth / 3 - 0.045,
rise - 0.045,
0.075,
stoneMaterials[(i + block + 1) % stoneMaterials.length],
stairCenterX - stairWidth / 2 + stairWidth / 6 + block * stairWidth / 3,
riserBase + rise / 2,
riserZ
);
}
// Thick worn tread.
addBox(
stairWidth + 0.06,
0.12,
run + 0.035,
treadMaterial,
stairCenterX,
bodyTop + 0.06,
centerZ
);
// Small voxel chips and tonal variations on the tread surface.
for (let chip = 0; chip < 3; chip++) {
const chipWidth = randomBetween(0.09, 0.22);
const chipDepth = randomBetween(0.07, 0.16);
addBox(
chipWidth,
randomBetween(0.018, 0.035),
chipDepth,
stoneMaterials[(i + chip) % stoneMaterials.length],
randomBetween(stairCenterX - 1.9, stairCenterX + 1.9),
bodyTop + 0.13,
centerZ + randomBetween(-run * 0.36, run * 0.36),
scene,
false,
true
);
}
// Horizontal mortar joints visible on the camera-facing side.
for (let level = 1; level <= i; level++) {
addBox(
0.025,
0.025,
run - 0.055,
darkStoneMaterial,
stairCenterX + stairWidth / 2 + 0.012,
level * rise,
centerZ,
scene,
false,
true
);
}
}
// Upper landing.
addBox(
stairWidth + 0.12,
finalBodyTop,
1.5,
stoneMaterials[1],
stairCenterX,
finalBodyTop / 2,
5.06
);
addBox(
stairWidth + 0.18,
0.12,
1.55,
treadMaterial,
stairCenterX,
finalBodyTop + 0.06,
5.06
);
// Low solid parapet on the outer side of the stair.
for (let i = 0; i < stepCount; i++) {
const bodyTop = (i + 1) * rise;
const z = stairStartZ + i * run + run / 2;
addBox(
0.25,
bodyTop + 0.23,
run + 0.01,
stoneMaterials[(i + 2) % stoneMaterials.length],
stairCenterX - stairWidth / 2 - 0.12,
(bodyTop + 0.23) / 2,
z
);
addBox(
0.34,
0.12,
run + 0.025,
treadMaterial,
stairCenterX - stairWidth / 2 - 0.12,
bodyTop + 0.29,
z
);
}
// Balustrade separating the stair from the sloping street.
const balustradeX = 0.52;
const balusterPoints = [];
function addBaluster(x, z, baseY, scale = 1) {
const s = scale;
addBox(
0.46 * s,
0.16 * s,
0.44 * s,
stoneMaterials[0],
x,
baseY + 0.08 * s,
z
);
addBox(
0.25 * s,
0.51 * s,
0.25 * s,
stoneMaterials[1],
x,
baseY + 0.415 * s,
z
);
addBox(
0.34 * s,
0.18 * s,
0.32 * s,
stoneMaterials[3],
x,
baseY + 0.755 * s,
z
);
addBox(
0.23 * s,
0.15 * s,
0.23 * s,
stoneMaterials[1],
x,
baseY + 0.92 * s,
z
);
addBox(
0.44 * s,
0.14 * s,
0.42 * s,
stoneMaterials[0],
x,
baseY + 1.065 * s,
z
);
addBox(
0.24 * s,
0.22 * s,
0.24 * s,
stoneMaterials[3],
x,
baseY + 1.245 * s,
z
);
}
for (let i = 0; i < stepCount; i++) {
const z = stairStartZ + i * run + run / 2;
const top = (i + 1) * rise + 0.12;
addBaluster(balustradeX, z, top);
balusterPoints.push(
new THREE.Vector3(balustradeX, top + 1.08, z)
);
}
// Lower and upper diagonal rails.
for (let i = 0; i < balusterPoints.length - 1; i++) {
const a = balusterPoints[i];
const b = balusterPoints[i + 1];
const upperA = a.clone();
const upperB = b.clone();
const lowerA = new THREE.Vector3(
balustradeX,
a.y - 0.61,
a.z
);
const lowerB = new THREE.Vector3(
balustradeX,
b.y - 0.61,
b.z
);
addBeam(upperA, upperB, 0.27, 0.18, stoneMaterials[0]);
addBeam(lowerA, lowerB, 0.16, 0.16, stoneMaterials[2]);
}
// Larger bottom and top newel posts.
addBaluster(
balustradeX,
stairStartZ - 0.26,
0.48,
1.28
);
addBaluster(
balustradeX,
4.55,
finalTreadTop,
1.28
);
// Colonial facade at the head of the stairs.
const facade = new THREE.Group();
facade.position.set(stairCenterX, finalTreadTop, 8.0);
scene.add(facade);
addBox(
7.7,
6.15,
2.75,
plasterMaterial,
0,
3.075,
0,
facade
);
addBox(
7.95,
0.42,
2.95,
stoneMaterials[1],
0,
0.21,
0,
facade
);
addBox(
8.0,
0.34,
3.05,
roofMaterial,
0,
6.27,
0,
facade
);
addBox(
8.2,
0.16,
3.15,
stoneMaterials[2],
0,
6.49,
0,
facade
);
// Door and surround.
addBox(
1.7,
3.25,
0.17,
stoneMaterials[3],
0,
1.62,
-1.43,
facade
);
addBox(
1.37,
2.91,
0.15,
woodMaterial,
0,
1.46,
-1.54,
facade
);
addBox(
0.08,
2.78,
0.05,
roofMaterial,
0,
1.46,
-1.64,
facade
);
addBox(
1.44,
0.08,
0.05,
roofMaterial,
0,
1.42,
-1.65,
facade
);
// Windows with dark frames and green shutters.
for (const x of [-2.45, 2.45]) {
addBox(
1.43,
2.0,
0.17,
stoneMaterials[3],
x,
4.45,
-1.43,
facade
);
addBox(
1.12,
1.72,
0.12,
glassMaterial,
x,
4.45,
-1.55,
facade
);
addBox(
0.1,
1.72,
0.06,
shutterMaterial,
x,
4.45,
-1.64,
facade
);
addBox(
0.46,
1.72,
0.08,
shutterMaterial,
x - 0.76,
4.45,
-1.59,
facade
);
addBox(
0.46,
1.72,
0.08,
shutterMaterial,
x + 0.76,
4.45,
-1.59,
facade
);
}
// Small upper window.
addBox(
1.13,
1.15,
0.14,
stoneMaterials[3],
0,
5.18,
-1.45,
facade
);
addBox(
0.86,
0.9,
0.1,
glassMaterial,
0,
5.18,
-1.56,
facade
);
// Voxel chimney.
addBox(
0.65,
1.05,
0.65,
plasterLightMaterial,
1.65,
6.9,
0.3,
facade
);
// Square colonial street lantern.
const lampZ = 5.95;
const lampBaseY = roadHeight(lampZ);
addBox(
0.5,
0.2,
0.5,
stoneMaterials[0],
1.18,
lampBaseY + 0.1,
lampZ
);
addBox(
0.13,
2.7,
0.13,
darkStoneMaterial,
1.18,
lampBaseY + 1.45,
lampZ
);
addBox(
0.62,
0.1,
0.62,
darkStoneMaterial,
1.18,
lampBaseY + 2.82,
lampZ
);
addBox(
0.42,
0.48,
0.42,
warmLightMaterial,
1.18,
lampBaseY + 3.1,
lampZ
);
addBox(
0.55,
0.12,
0.55,
darkStoneMaterial,
1.18,
lampBaseY + 3.4,
lampZ
);
const lanternLight = new THREE.PointLight(0xffad4c, 1.3, 4.2, 2);
lanternLight.position.set(1.18, lampBaseY + 3.1, lampZ);
scene.add(lanternLight);
// A few square terracotta pots and angular plants.
for (const potX of [-3.15, -0.45]) {
addBox(
0.58,
0.62,
0.58,
roofMaterial,
potX,
finalTreadTop + 0.31,
6.18
);
addBox(
0.7,
0.12,
0.7,
stoneMaterials[1],
potX,
finalTreadTop + 0.66,
6.18
);
const plantMaterial = new THREE.MeshStandardMaterial({
color: potX < -1 ? 0x526d42 : 0x647948,
roughness: 1
});
addBox(
0.7,
0.72,
0.66,
plantMaterial,
potX,
finalTreadTop + 1.03,
6.18
);
addBox(
0.4,
0.45,
0.42,
plantMaterial,
potX + 0.22,
finalTreadTop + 1.5,
6.13
);
}
// Subtle airborne dust in the warm light.
const dustCount = 90;
const dustPositions = new Float32Array(dustCount * 3);
for (let i = 0; i < dustCount; i++) {
dustPositions[i * 3] = randomBetween(-5.2, 3.3);
dustPositions[i * 3 + 1] = randomBetween(0.4, 7.0);
dustPositions[i * 3 + 2] = randomBetween(-5.0, 7.0);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0xffe8bd,
size: 0.035,
transparent: true,
opacity: 0.32,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(dust);
// Custom camera controls: orbit, zoom, and pan without addons.
const defaultView = {
yaw: 2.43,
pitch: 0.48,
distance: 15.8,
target: new THREE.Vector3(-0.85, 2.05, 0.35)
};
const desired = {
yaw: defaultView.yaw,
pitch: defaultView.pitch,
distance: defaultView.distance,
target: defaultView.target.clone()
};
const current = {
yaw: defaultView.yaw,
pitch: defaultView.pitch,
distance: defaultView.distance,
target: defaultView.target.clone()
};
function updateCamera() {
const cosPitch = Math.cos(current.pitch);
camera.position.set(
current.target.x + Math.sin(current.yaw) * cosPitch * current.distance,
current.target.y + Math.sin(current.pitch) * current.distance,
current.target.z + Math.cos(current.yaw) * cosPitch * current.distance
);
camera.lookAt(current.target);
}
updateCamera();
const pointers = new Map();
let lastPinchDistance = 0;
let activePointer = null;
let lastX = 0;
let lastY = 0;
let panning = false;
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
activePointer = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
panning = event.button === 2 || event.shiftKey;
} else if (pointers.size === 2) {
const values = [...pointers.values()];
lastPinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
});
canvas.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size >= 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (lastPinchDistance > 0 && distance > 0) {
desired.distance = THREE.MathUtils.clamp(
desired.distance * (lastPinchDistance / distance),
6,
30
);
}
lastPinchDistance = distance;
return;
}
if (event.pointerId !== activePointer) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (panning) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const panScale = desired.distance * 0.0017;
desired.target.addScaledVector(right, -dx * panScale);
desired.target.y = THREE.MathUtils.clamp(
desired.target.y + dy * panScale * 0.72,
-1,
8
);
} else {
desired.yaw -= dx * 0.0052;
desired.pitch = THREE.MathUtils.clamp(
desired.pitch + dy * 0.0045,
0.12,
1.28
);
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const [pointerId, point] = pointers.entries().next().value;
activePointer = pointerId;
lastX = point.x;
lastY = point.y;
} else {
activePointer = null;
lastPinchDistance = 0;
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
desired.distance = THREE.MathUtils.clamp(
desired.distance * Math.exp(event.deltaY * 0.001),
6,
30
);
},
{ passive: false }
);
window.addEventListener("keydown", event => {
const step = 0.09;
if (event.key === "ArrowLeft") desired.yaw += step;
if (event.key === "ArrowRight") desired.yaw -= step;
if (event.key === "ArrowUp") {
desired.pitch = Math.min(1.28, desired.pitch + step * 0.65);
}
if (event.key === "ArrowDown") {
desired.pitch = Math.max(0.12, desired.pitch - step * 0.65);
}
if (event.key === "+" || event.key === "=") {
desired.distance = Math.max(6, desired.distance - 0.8);
}
if (event.key === "-" || event.key === "_") {
desired.distance = Math.min(30, desired.distance + 0.8);
}
});
document.querySelector("#reset").addEventListener("click", () => {
desired.yaw = defaultView.yaw;
desired.pitch = defaultView.pitch;
desired.distance = defaultView.distance;
desired.target.copy(defaultView.target);
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const smoothing = 1 - Math.exp(-8 * delta);
current.yaw = THREE.MathUtils.lerp(current.yaw, desired.yaw, smoothing);
current.pitch = THREE.MathUtils.lerp(current.pitch, desired.pitch, smoothing);
current.distance = THREE.MathUtils.lerp(
current.distance,
desired.distance,
smoothing
);
current.target.lerp(desired.target, smoothing);
updateCamera();
dust.rotation.y = elapsed * 0.012;
dust.position.y = Math.sin(elapsed * 0.22) * 0.08;
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 550);
});
</script>
</body>
</html>
```