Prompt specification
Build or customize the Three.js 3D scene "The Rocks sandstone wall and stair fragment" 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: The Rocks sandstone wall and stair fragment
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/the-rocks-sandstone-wall-and-stair-fragment-x3029
Scene Description Prompt:
The Rocks sandstone wall and stair fragment. Build a referenced Sydney sandstone wall and stair segment with a small doorway. Make the material blocks and elevation change legible at a compact pedestrian scale. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (the-rocks-sandstone-wall-and-stair-fragment-x3029):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Rocks — Sandstone Wall and Stair Fragment</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: #9eb6c4;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 2;
top: 24px;
left: 24px;
max-width: min(390px, calc(100vw - 48px));
padding: 17px 19px 16px;
color: #f6f0e5;
background: rgba(35, 39, 39, 0.76);
border: 1px solid rgba(255, 255, 255, 0.17);
border-radius: 4px;
box-shadow: 0 12px 40px rgba(25, 28, 28, 0.18);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: #d8b77d;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.5vw, 28px);
font-weight: 620;
line-height: 1.08;
letter-spacing: -0.025em;
}
.description {
margin-top: 9px;
color: rgba(246, 240, 229, 0.73);
font-size: 12px;
line-height: 1.5;
}
.facts {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 13px;
}
.fact {
padding: 5px 7px;
color: rgba(246, 240, 229, 0.85);
background: rgba(255, 255, 255, 0.07);
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 2px;
font-size: 9px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.hint {
position: fixed;
z-index: 2;
right: 22px;
bottom: 20px;
padding: 9px 12px;
color: rgba(255, 255, 255, 0.88);
background: rgba(35, 39, 39, 0.66);
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 3px;
backdrop-filter: blur(10px);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
transition: opacity 0.6s ease;
}
.hint.faded {
opacity: 0;
}
#loading {
position: fixed;
z-index: 5;
inset: 0;
display: grid;
place-items: center;
color: #f4ead8;
background: #858d8d;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 0.45s ease;
pointer-events: none;
}
#loading.hidden {
opacity: 0;
}
@media (max-width: 700px) {
.panel {
top: 14px;
left: 14px;
padding: 14px 15px;
}
.description {
display: none;
}
.facts {
margin-top: 10px;
}
.hint {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="panel">
<div class="eyebrow">The Rocks · Sydney sandstone study</div>
<h1>Wall & Stair Fragment</h1>
<div class="description">
A compact pedestrian-scale study of coursed sandstone, a recessed doorway,
and five shallow stone steps.
</div>
<div class="facts">
<span class="fact">5 × 200 mm rise</span>
<span class="fact">1.5 m stair width</span>
<span class="fact">200 mm courses</span>
</div>
</div>
<div class="hint" id="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="loading">Assembling sandstone courses</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 host = document.getElementById("scene");
const loading = document.getElementById("loading");
const hint = document.getElementById("hint");
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.03;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional sandstone wall and stair fragment"
);
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9fb8c6);
scene.fog = new THREE.Fog(0x9fb8c6, 16, 34);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.08,
80
);
const hemi = new THREE.HemisphereLight(0xe5f0f4, 0x755b3d, 1.45);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe1ad, 2.75);
sun.position.set(-6.5, 11, 8.5);
sun.target.position.set(0, 1.3, 0.6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun, sun.target);
const fill = new THREE.DirectionalLight(0x9dbbd0, 0.48);
fill.position.set(7, 5, -7);
scene.add(fill);
// -------------------------------------------------------------------------
// Procedural sandstone and paving textures
// -------------------------------------------------------------------------
function makeSandstoneTextures() {
const size = 256;
const colorCanvas = document.createElement("canvas");
colorCanvas.width = colorCanvas.height = size;
const colorContext = colorCanvas.getContext("2d");
const colorImage = colorContext.createImageData(size, size);
const bumpCanvas = document.createElement("canvas");
bumpCanvas.width = bumpCanvas.height = size;
const bumpContext = bumpCanvas.getContext("2d");
const bumpImage = bumpContext.createImageData(size, size);
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
const i = (y * size + x) * 4;
const fine = (Math.random() - 0.5) * 24;
const broad = Math.sin(x * 0.075) * 4 + Math.sin(y * 0.11) * 3;
const pore = Math.random() > 0.992 ? -42 : 0;
const value = Math.max(
0,
Math.min(255, 224 + fine + broad + pore)
);
colorImage.data[i] = value;
colorImage.data[i + 1] = value * 0.985;
colorImage.data[i + 2] = value * 0.95;
colorImage.data[i + 3] = 255;
const bumpValue = Math.max(
0,
Math.min(255, 155 + fine * 1.7 + broad + pore * 1.8)
);
bumpImage.data[i] = bumpValue;
bumpImage.data[i + 1] = bumpValue;
bumpImage.data[i + 2] = bumpValue;
bumpImage.data[i + 3] = 255;
}
}
colorContext.putImageData(colorImage, 0, 0);
bumpContext.putImageData(bumpImage, 0, 0);
const colorMap = new THREE.CanvasTexture(colorCanvas);
colorMap.colorSpace = THREE.SRGBColorSpace;
colorMap.wrapS = colorMap.wrapT = THREE.RepeatWrapping;
colorMap.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const bumpMap = new THREE.CanvasTexture(bumpCanvas);
bumpMap.wrapS = bumpMap.wrapT = THREE.RepeatWrapping;
return { colorMap, bumpMap };
}
const { colorMap, bumpMap } = makeSandstoneTextures();
const sandstoneMaterial = new THREE.MeshStandardMaterial({
map: colorMap,
bumpMap,
bumpScale: 0.028,
roughness: 0.93,
metalness: 0,
color: 0xffffff
});
const trimmedStoneMaterial = new THREE.MeshStandardMaterial({
map: colorMap,
bumpMap,
bumpScale: 0.018,
roughness: 0.86,
metalness: 0,
color: 0xffe0ae
});
const mortarMaterial = new THREE.MeshStandardMaterial({
color: 0x51483c,
roughness: 1,
metalness: 0
});
const pavingMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.97,
metalness: 0
});
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
const rand = (min, max) => min + Math.random() * (max - min);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
function stoneColor(lightness = 0.76, saturation = 0.31, hue = 0.105) {
return new THREE.Color().setHSL(
hue + rand(-0.012, 0.012),
saturation + rand(-0.045, 0.045),
lightness + rand(-0.055, 0.055)
);
}
function buildInstances(items, material, name) {
if (!items.length) return null;
const mesh = new THREE.InstancedMesh(unitBox, material, items.length);
mesh.name = name;
mesh.castShadow = true;
mesh.receiveShadow = true;
items.forEach((item, index) => {
dummy.position.set(
item.x + (item.jx || 0),
item.y + (item.jy || 0),
item.z + (item.jz || 0)
);
dummy.rotation.set(
item.rx || 0,
item.ry || 0,
item.rz || 0
);
dummy.scale.set(item.w, item.h, item.d);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, item.color || new THREE.Color(0xffffff));
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
scene.add(mesh);
return mesh;
}
function addSolid(w, h, d, x, y, z, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Ground and paving
// -------------------------------------------------------------------------
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x77766d,
roughness: 1
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.035;
ground.receiveShadow = true;
scene.add(ground);
const paving = [];
for (let ix = -7; ix <= 7; ix++) {
for (let iz = 0; iz <= 7; iz++) {
const x = ix * 0.78 + rand(-0.012, 0.012);
const z = 0.69 + iz * 0.78 + rand(-0.012, 0.012);
paving.push({
x,
y: -0.012,
z,
w: 0.755,
h: 0.045,
d: 0.755,
ry: rand(-0.006, 0.006),
color: new THREE.Color().setHSL(
0.095,
rand(0.055, 0.10),
rand(0.43, 0.53)
)
});
}
}
buildInstances(paving, pavingMaterial, "Pedestrian paving");
// -------------------------------------------------------------------------
// Main sandstone wall
// -------------------------------------------------------------------------
const wallBlocks = [];
const copingBlocks = [];
const wallLeft = -5.2;
const wallRight = 5.2;
const wallTop = 4.2;
const courseHeight = 0.2;
const nominalBlockWidth = 0.5;
const joint = 0.022;
const doorHalfWidth = 0.75;
const stairLevel = 1.0;
const doorTop = 3.0;
function addWallRegion(xMin, xMax, row, materialItems = wallBlocks) {
const y = row * courseHeight + courseHeight * 0.5;
const stagger = row % 2 === 0 ? 0 : nominalBlockWidth * 0.5;
let x = xMin - stagger;
while (x < xMax) {
const left = Math.max(x + joint * 0.5, xMin);
const right = Math.min(
x + nominalBlockWidth - joint * 0.5,
xMax
);
if (right - left > 0.08) {
const baseDarkening = row < 3 ? 0.1 : row < 6 ? 0.035 : 0;
const hue = rand(-0.006, 0.012);
materialItems.push({
x: (left + right) * 0.5,
y,
z: rand(-0.008, 0.008),
w: right - left,
h: courseHeight - joint,
d: 0.54,
ry: rand(-0.0025, 0.0025),
color: stoneColor(
0.77 - baseDarkening,
0.34,
0.105 + hue
)
});
}
x += nominalBlockWidth;
}
}
const totalCourses = Math.round(wallTop / courseHeight);
for (let row = 0; row < totalCourses; row++) {
const rowBottom = row * courseHeight;
const rowTop = rowBottom + courseHeight;
if (rowTop > doorTop) {
// Continuous bond above the lintel.
addWallRegion(wallLeft, wallRight, row);
} else {
addWallRegion(wallLeft, -doorHalfWidth, row);
addWallRegion(doorHalfWidth, wallRight, row);
if (rowBottom < stairLevel) {
addWallRegion(-doorHalfWidth, doorHalfWidth, row);
}
}
}
// Dark backing makes the small mortar joints legible.
addSolid(
wallRight - wallLeft,
wallTop,
0.52,
0,
wallTop * 0.5,
-0.025,
mortarMaterial
);
// The backing above is replaced by region-specific pieces so the doorway
// remains a genuine recess rather than a dark rectangle on a solid slab.
scene.remove(scene.children[scene.children.length - 1]);
addSolid(
wallRight - wallLeft,
stairLevel,
0.52,
0,
stairLevel * 0.5,
-0.025,
mortarMaterial
);
addSolid(
wallRight - wallLeft,
wallTop - doorTop,
0.52,
0,
(wallTop + doorTop) * 0.5,
-0.025,
mortarMaterial
);
// Projecting plinth.
let plinthX = wallLeft;
while (plinthX < wallRight) {
const right = Math.min(plinthX + 0.52, wallRight);
wallBlocks.push({
x: (plinthX + right) * 0.5,
y: 0.1,
z: 0.04,
w: right - plinthX - 0.012,
h: 0.19,
d: 0.66,
color: stoneColor(0.67, 0.32)
});
plinthX = right;
}
// Irregular top coping.
let copingX = wallLeft;
while (copingX < wallRight) {
const right = Math.min(copingX + 0.55, wallRight);
copingBlocks.push({
x: (copingX + right) * 0.5,
y: wallTop + 0.075 + rand(-0.012, 0.012),
z: rand(-0.018, 0.018),
w: right - copingX - 0.016,
h: 0.16,
d: 0.72,
ry: rand(-0.003, 0.003),
color: stoneColor(0.82, 0.27)
});
copingX = right;
}
buildInstances(wallBlocks, sandstoneMaterial, "Coursed wall blocks");
buildInstances(copingBlocks, trimmedStoneMaterial, "Wall coping");
// -------------------------------------------------------------------------
// Dressed doorway surround and threshold
// -------------------------------------------------------------------------
const frameBlocks = [];
for (let side = -1; side <= 1; side += 2) {
for (let row = 0; row < 10; row++) {
frameBlocks.push({
x: side * 0.855,
y: stairLevel + row * courseHeight + courseHeight * 0.5,
z: 0.355,
w: 0.21,
h: 0.178,
d: 0.19,
color: stoneColor(0.8, 0.25)
});
}
}
for (let i = 0; i < 3; i++) {
frameBlocks.push({
x: (i - 1) * 0.64,
y: doorTop + 0.1,
z: 0.36,
w: 0.625,
h: 0.2,
d: 0.22,
color: stoneColor(0.82, 0.24)
});
}
frameBlocks.push({
x: 0,
y: stairLevel - 0.055,
z: 0.25,
w: 1.5,
h: 0.11,
d: 0.78,
color: stoneColor(0.7, 0.3)
});
buildInstances(
frameBlocks,
trimmedStoneMaterial,
"Dressed doorway surround"
);
// Dark interior at the back of the opening.
const interiorMaterial = new THREE.MeshStandardMaterial({
color: 0x151819,
roughness: 1
});
addSolid(
1.48,
2.0,
0.08,
0,
2.0,
-0.48,
interiorMaterial
);
addSolid(
1.48,
0.08,
0.55,
0,
0.96,
-0.19,
mortarMaterial
);
// A slightly ajar, dark timber door adds depth without hiding the recess.
const doorGroup = new THREE.Group();
doorGroup.position.set(0.63, 1.98, -0.28);
doorGroup.rotation.y = -0.24;
const timberColors = [0x292b28, 0x35332d, 0x252824, 0x3a352d];
for (let i = 0; i < 4; i++) {
const plank = new THREE.Mesh(
new THREE.BoxGeometry(0.285, 1.88, 0.065),
new THREE.MeshStandardMaterial({
color: timberColors[i],
roughness: 0.93
})
);
plank.position.x = -(i + 0.5) * 0.3;
plank.castShadow = true;
plank.receiveShadow = true;
doorGroup.add(plank);
}
const brace = new THREE.Mesh(
new THREE.BoxGeometry(1.14, 0.065, 0.035),
new THREE.MeshStandardMaterial({
color: 0x171916,
roughness: 0.9
})
);
brace.position.set(-0.6, 0, 0.05);
doorGroup.add(brace);
const handle = new THREE.Mesh(
new THREE.SphereGeometry(0.038, 12, 8),
new THREE.MeshStandardMaterial({
color: 0x8f7044,
roughness: 0.46,
metalness: 0.55
})
);
handle.position.set(-1.04, 0.04, 0.09);
doorGroup.add(handle);
scene.add(doorGroup);
// -------------------------------------------------------------------------
// Five sandstone steps with a low stepped cheek wall
// -------------------------------------------------------------------------
const stairBlocks = [];
const stairWidth = 1.5;
const stepDepth = 0.56;
const stairBlockWidth = 0.375;
for (let step = 0; step < 5; step++) {
const height = 1.0 - step * 0.2;
const zMin = 0.23 + step * stepDepth;
const zCenter = zMin + stepDepth * 0.5;
const layers = Math.round(height / 0.2);
// Dark core behind the joints.
addSolid(
stairWidth - 0.03,
height - 0.035,
stepDepth - 0.035,
0,
height * 0.5,
zCenter,
mortarMaterial
);
for (let layer = 0; layer < layers; layer++) {
for (let column = 0; column < 4; column++) {
const isTop = layer === layers - 1;
const x = -stairWidth * 0.5 +
stairBlockWidth * 0.5 +
column * stairBlockWidth;
stairBlocks.push({
x,
y: layer * 0.2 + 0.1,
z: zCenter + (isTop ? 0.018 : 0),
w: stairBlockWidth - 0.014,
h: 0.184,
d: isTop ? 0.57 : 0.525,
color: stoneColor(
step === 0 ? 0.72 : 0.75,
0.32,
0.103
)
});
}
}
// Low left cheek wall follows the changing stair elevation.
const cheekHeight = height + 0.2;
const cheekLayers = Math.round(cheekHeight / 0.2);
for (let layer = 0; layer < cheekLayers; layer++) {
stairBlocks.push({
x: -stairWidth * 0.5 - 0.14,
y: layer * 0.2 + 0.1,
z: zCenter,
w: 0.28,
h: 0.182,
d: stepDepth - 0.02,
color: stoneColor(0.71, 0.34)
});
}
}
buildInstances(stairBlocks, sandstoneMaterial, "Sandstone stair blocks");
// -------------------------------------------------------------------------
// Small pedestrian for scale
// -------------------------------------------------------------------------
const pedestrian = new THREE.Group();
pedestrian.position.set(-2.18, 0.035, 2.45);
pedestrian.rotation.y = 0.48;
const clothingMaterial = new THREE.MeshStandardMaterial({
color: 0x29434a,
roughness: 0.95
});
const trouserMaterial = new THREE.MeshStandardMaterial({
color: 0x272d30,
roughness: 0.98
});
const skinMaterial = new THREE.MeshStandardMaterial({
color: 0xb98368,
roughness: 0.85
});
function pedestrianLimb(radius, length, material) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius * 1.05, length, 10),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
const leftLeg = pedestrianLimb(0.07, 0.78, trouserMaterial);
leftLeg.position.set(-0.105, 0.43, 0);
leftLeg.rotation.z = -0.035;
pedestrian.add(leftLeg);
const rightLeg = pedestrianLimb(0.07, 0.78, trouserMaterial);
rightLeg.position.set(0.105, 0.43, 0);
rightLeg.rotation.z = 0.035;
pedestrian.add(rightLeg);
const torso = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.215, 0.56, 12),
clothingMaterial
);
torso.position.y = 1.11;
torso.castShadow = true;
torso.receiveShadow = true;
pedestrian.add(torso);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 16, 12),
skinMaterial
);
head.position.y = 1.57;
head.castShadow = true;
pedestrian.add(head);
const neck = pedestrianLimb(0.055, 0.12, skinMaterial);
neck.position.y = 1.4;
pedestrian.add(neck);
const leftArm = pedestrianLimb(0.048, 0.54, clothingMaterial);
leftArm.position.set(-0.25, 1.1, 0.015);
leftArm.rotation.z = -0.1;
pedestrian.add(leftArm);
const rightArm = pedestrianLimb(0.048, 0.54, clothingMaterial);
rightArm.position.set(0.25, 1.1, 0.015);
rightArm.rotation.z = 0.1;
pedestrian.add(rightArm);
const leftShoe = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.075, 0.25),
new THREE.MeshStandardMaterial({ color: 0x181b1b, roughness: 1 })
);
leftShoe.position.set(-0.11, 0.045, 0.04);
leftShoe.castShadow = true;
pedestrian.add(leftShoe);
const rightShoe = leftShoe.clone();
rightShoe.position.x = 0.11;
pedestrian.add(rightShoe);
scene.add(pedestrian);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, pan-free reset, keyboard support
// -------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 1.55, 0.55),
theta: 0.53,
phi: 1.25,
radius: 11.4,
desiredTheta: 0.53,
desiredPhi: 1.25,
desiredRadius: 11.4,
dragging: false,
pointerX: 0,
pointerY: 0,
interacted: false
};
function defaultRadius() {
const aspect = window.innerWidth / window.innerHeight;
if (aspect < 0.72) return 22;
if (aspect < 1.05) return 17;
if (aspect < 1.35) return 13.5;
return 11.4;
}
function updateCamera() {
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);
}
function resetView() {
controls.desiredTheta = 0.53;
controls.desiredPhi = 1.25;
controls.desiredRadius = defaultRadius();
controls.interacted = false;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
controls.interacted = true;
renderer.domElement.setPointerCapture(event.pointerId);
hint.classList.add("faded");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const dx = event.clientX - controls.pointerX;
const dy = event.clientY - controls.pointerY;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
controls.desiredTheta -= dx * 0.005;
controls.desiredPhi = THREE.MathUtils.clamp(
controls.desiredPhi - dy * 0.0045,
0.48,
1.46
);
});
function endDrag(event) {
controls.dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.interacted = true;
controls.desiredRadius = THREE.MathUtils.clamp(
controls.desiredRadius * Math.exp(event.deltaY * 0.001),
6.2,
27
);
hint.classList.add("faded");
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "r") {
resetView();
return;
}
let handled = true;
switch (key) {
case "arrowleft":
controls.desiredTheta += 0.08;
break;
case "arrowright":
controls.desiredTheta -= 0.08;
break;
case "arrowup":
controls.desiredPhi = Math.max(0.48, controls.desiredPhi - 0.06);
break;
case "arrowdown":
controls.desiredPhi = Math.min(1.46, controls.desiredPhi + 0.06);
break;
case "+":
case "=":
controls.desiredRadius = Math.max(6.2, controls.desiredRadius - 0.7);
break;
case "-":
case "_":
controls.desiredRadius = Math.min(27, controls.desiredRadius + 0.7);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
controls.interacted = true;
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
if (!controls.interacted) {
controls.desiredRadius = defaultRadius();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
controls.desiredRadius = defaultRadius();
controls.radius = controls.desiredRadius;
updateCamera();
function animate() {
requestAnimationFrame(animate);
const damping = 0.11;
controls.theta +=
(controls.desiredTheta - controls.theta) * damping;
controls.phi +=
(controls.desiredPhi - controls.phi) * damping;
controls.radius +=
(controls.desiredRadius - controls.radius) * damping;
updateCamera();
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 500);
});
setTimeout(() => hint.classList.add("faded"), 6500);
</script>
</body>
</html>
```