Prompt specification
Build or customize the Three.js 3D scene "Stone wall across microenvironments" 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: Stone wall across microenvironments
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/stone-wall-across-microenvironments-x1199
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts rock weathering. Compose shaded, wet, and exposed sections of a small wall with different illustrative surface changes; label environmental contrasts without inventing real deterioration rates. Voxel style.
Working Three.js Starter (stone-wall-across-microenvironments-x1199):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rock Weathering — Voxel Field Study</title>
<style>
:root {
--ink: #18302f;
--muted: #5d706d;
--panel: rgba(246, 249, 246, 0.82);
--line: rgba(35, 65, 62, 0.16);
--shade: #75857d;
--wet: #477f83;
--exposed: #b58b58;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background:
radial-gradient(circle at 72% 18%, rgba(255, 246, 216, 0.92), transparent 30%),
radial-gradient(circle at 25% 70%, rgba(176, 211, 205, 0.55), transparent 42%),
linear-gradient(145deg, #e8efeb 0%, #d7e4e1 48%, #c9d9d6 100%);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.title-panel {
position: absolute;
top: 26px;
left: 28px;
width: min(350px, calc(100vw - 56px));
padding: 20px 22px 18px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(35, 66, 61, 0.12);
backdrop-filter: blur(14px);
}
.eyebrow {
margin: 0 0 8px;
color: #54716c;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(32px, 4vw, 48px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
margin: 12px 0 0;
max-width: 30ch;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.zone-strip {
display: flex;
gap: 7px;
margin-top: 16px;
}
.zone-chip {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 8px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.38);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.zone-chip::before {
content: "";
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--chip);
}
#reset {
position: absolute;
top: 28px;
right: 28px;
pointer-events: auto;
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 12px;
color: var(--ink);
background: rgba(248, 250, 247, 0.78);
box-shadow: 0 8px 25px rgba(35, 66, 61, 0.1);
backdrop-filter: blur(12px);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: transform 160ms ease, background 160ms ease;
}
#reset:hover {
transform: translateY(-2px);
background: rgba(255, 255, 255, 0.94);
}
#reset:active {
transform: translateY(0);
}
#labels {
position: absolute;
inset: 0;
pointer-events: none;
}
.world-label {
position: absolute;
width: 154px;
padding: 10px 12px 11px;
border: 1px solid rgba(32, 62, 59, 0.15);
border-radius: 12px;
background: rgba(247, 250, 247, 0.9);
box-shadow: 0 10px 28px rgba(34, 64, 60, 0.12);
opacity: 0;
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
backdrop-filter: blur(10px);
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 27px;
background: linear-gradient(rgba(32, 62, 59, 0.32), transparent);
}
.world-label.visible {
opacity: 1;
}
.world-label .label-kicker {
display: flex;
align-items: center;
gap: 6px;
margin-bottom: 5px;
color: var(--label-color);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.world-label .label-kicker::before {
content: "";
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--label-color);
}
.world-label strong {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 17px;
font-weight: 600;
letter-spacing: -0.02em;
}
.world-label small {
display: block;
margin-top: 3px;
color: #60736f;
font-size: 10px;
line-height: 1.35;
}
.shade-label {
--label-color: var(--shade);
}
.wet-label {
--label-color: var(--wet);
}
.exposed-label {
--label-color: var(--exposed);
}
.note-panel {
position: absolute;
right: 28px;
bottom: 26px;
width: min(335px, calc(100vw - 56px));
padding: 14px 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(247, 250, 247, 0.78);
box-shadow: 0 12px 30px rgba(35, 66, 61, 0.1);
backdrop-filter: blur(12px);
}
.note-panel strong {
display: block;
margin-bottom: 5px;
font-size: 10px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.note-panel p {
margin: 0;
color: #5f716e;
font-size: 11px;
line-height: 1.45;
}
.controls {
position: absolute;
left: 28px;
bottom: 28px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(247, 250, 247, 0.68);
color: #536966;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
backdrop-filter: blur(10px);
}
.controls span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.controls i {
width: 18px;
height: 18px;
display: inline-grid;
place-items: center;
border: 1px solid rgba(35, 65, 62, 0.18);
border-radius: 5px;
color: var(--ink);
font-style: normal;
font-size: 11px;
background: rgba(255, 255, 255, 0.45);
}
@media (max-width: 760px) {
.title-panel {
top: 14px;
left: 14px;
width: 250px;
padding: 15px 16px;
}
.subtitle {
display: none;
}
.zone-strip {
margin-top: 10px;
}
.zone-chip {
padding: 5px 6px;
font-size: 8px;
}
#reset {
top: 16px;
right: 14px;
padding: 9px 11px;
}
.world-label {
width: 108px;
padding: 8px 9px;
}
.world-label small {
display: none;
}
.world-label strong {
font-size: 14px;
}
.note-panel {
right: 14px;
bottom: 14px;
width: 225px;
}
.controls {
left: 14px;
bottom: 14px;
}
.controls .mouse-hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" tabindex="0" aria-label="Interactive voxel scene of a weathered stone wall"></canvas>
<div class="hud">
<section class="title-panel">
<p class="eyebrow">Voxel field study / 01</p>
<h1>Rock<br />weathering</h1>
<p class="subtitle">
Three contrasting exposures of the same small wall, translated into a tactile
voxel landscape.
</p>
<div class="zone-strip" aria-label="Environmental zones">
<span class="zone-chip" style="--chip: var(--shade)">Shaded</span>
<span class="zone-chip" style="--chip: var(--wet)">Wet</span>
<span class="zone-chip" style="--chip: var(--exposed)">Exposed</span>
</div>
</section>
<button id="reset" type="button">Reset view</button>
<div id="labels">
<div class="world-label shade-label" id="label-shade">
<div class="label-kicker">01 / Shaded</div>
<strong>Diffuse light</strong>
<small>Cool stone tones and lichen-like surface patches.</small>
</div>
<div class="world-label wet-label" id="label-wet">
<div class="label-kicker">02 / Wet</div>
<strong>Visible moisture</strong>
<small>Darkened stone, reflective sheen and mossy joints.</small>
</div>
<div class="world-label exposed-label" id="label-exposed">
<div class="label-kicker">03 / Exposed</div>
<strong>Direct exposure</strong>
<small>Pale wash tones, runoff marks and chipped corners.</small>
</div>
</div>
<div class="controls">
<span><i>↔</i> Drag to orbit</span>
<span class="mouse-hint"><i>⌕</i> Scroll to zoom</span>
<span class="mouse-hint"><i>↺</i> Double-click to reset</span>
</div>
<aside class="note-panel">
<strong>Illustrative comparison</strong>
<p>
Color, moisture sheen, lichen and chipping are schematic visual cues only.
No measured deterioration rate or forecast is implied.
</p>
</aside>
</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.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true
});
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.12;
renderer.setClearColor(0x000000, 0);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xd4e1dd, 17, 36);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
target: new THREE.Vector3(0, 1.18, 0),
theta: 0.08,
phi: 1.23,
radius: window.innerWidth < 760 ? 25 : 15.7,
desiredTheta: 0.08,
desiredPhi: 1.23,
desiredRadius: window.innerWidth < 760 ? 25 : 15.7
};
function resetCamera() {
cameraState.desiredTheta = 0.08;
cameraState.desiredPhi = 1.23;
cameraState.desiredRadius = window.innerWidth < 760 ? 25 : 15.7;
}
// Lighting: a warm direct source distinguishes the exposed face.
const hemisphere = new THREE.HemisphereLight(0xf5fbff, 0x69766a, 1.8);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedc7, 3.4);
sun.position.set(8, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0xb9d9dc, 0.7);
coolFill.position.set(-7, 5, 6);
scene.add(coolFill);
const wetHighlight = new THREE.PointLight(0x89c7c8, 7, 8, 2);
wetHighlight.position.set(0.5, 2.1, 3.1);
scene.add(wetHighlight);
// Seeded randomness keeps the voxel details stable between reloads.
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(41729);
const dummy = new THREE.Object3D();
function colorVariation(hex, amount = 0.12) {
const color = new THREE.Color(hex);
color.offsetHSL(
(random() - 0.5) * 0.025,
(random() - 0.5) * 0.07,
(random() - 0.5) * amount
);
return color;
}
function createInstancedMesh(entries, material, castShadow = true) {
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
material,
entries.length
);
entries.forEach((entry, index) => {
dummy.position.set(entry.p[0], entry.p[1], entry.p[2]);
dummy.scale.set(entry.s[0], entry.s[1], entry.s[2]);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, entry.c instanceof THREE.Color
? entry.c
: new THREE.Color(entry.c)
);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
return mesh;
}
// Ground made from shallow square tiles.
const groundEntries = [];
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.97
});
for (let x = -9; x <= 9; x++) {
for (let z = -5; z <= 5; z++) {
const color = colorVariation(x % 2 === 0 ? 0xb8b59f : 0xc1bda6, 0.08);
groundEntries.push({
p: [x * 0.78, -0.045 + random() * 0.012, z * 0.78],
s: [0.755, 0.09, 0.755],
c: color
});
}
}
scene.add(createInstancedMesh(groundEntries, groundMaterial, false));
const foundationMaterial = new THREE.MeshStandardMaterial({
color: 0x626c63,
roughness: 0.94
});
for (let i = 0; i < 11; i++) {
const block = new THREE.Mesh(
new THREE.BoxGeometry(1.08, 0.25, 1.02),
foundationMaterial
);
block.position.set(-5.4 + i * 1.08, 0.13, 0);
block.castShadow = true;
block.receiveShadow = true;
scene.add(block);
}
const shadeEntries = [];
const wetEntries = [];
const exposedEntries = [];
const zoneColors = {
shade: 0x737f78,
wet: 0x30464a,
exposed: 0xb5a58a
};
// The wall is assembled from small stone voxels. Missing outer voxels create
// deliberately illustrative chips and cavities rather than measured decay.
const voxelPitch = 0.18;
const blockStep = 1.08;
const startX = -5.4;
for (let row = 0; row < 4; row++) {
const rowOffset = row % 2 === 0 ? 0 : 0.27;
for (let column = 0; column < 11; column++) {
const zone = column < 4 ? "shade" : column < 8 ? "wet" : "exposed";
const blockX = startX + column * blockStep + rowOffset;
const blockY = 0.43 + row * 0.62;
const targetEntries =
zone === "shade" ? shadeEntries : zone === "wet" ? wetEntries : exposedEntries;
for (let ix = 0; ix < 6; ix++) {
for (let iy = 0; iy < 3; iy++) {
for (let iz = 0; iz < 4; iz++) {
const isFront = iz === 3;
const isEdge = ix === 0 || ix === 5 || iy === 0 || iy === 2;
let missingChance = 0.002;
if (zone === "shade") {
missingChance = isFront ? 0.008 : isEdge ? 0.004 : 0;
} else if (zone === "wet") {
missingChance = isFront ? 0.028 : isEdge ? 0.016 : 0.003;
} else {
missingChance = isFront ? 0.07 : isEdge ? 0.055 : 0.012;
}
if (random() < missingChance) continue;
let color = colorVariation(zoneColors[zone], zone === "wet" ? 0.17 : 0.13);
if (zone === "shade" && isFront && random() < 0.14) {
color = colorVariation(0x758665, 0.13);
}
if (zone === "wet") {
if (isFront && random() < 0.07) {
color = colorVariation(0x526c50, 0.13);
} else {
color.multiplyScalar(0.88 + random() * 0.18);
}
}
if (zone === "exposed" && isFront && random() < 0.09) {
color = colorVariation(0x94715c, 0.1);
}
targetEntries.push({
p: [
blockX + (ix - 2.5) * voxelPitch,
blockY + (iy - 1) * voxelPitch,
(iz - 1.5) * voxelPitch
],
s: [0.176, 0.176, 0.176],
c: color
});
}
}
}
}
}
const shadeMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94
});
const wetMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.23,
metalness: 0.08
});
const exposedMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.86
});
scene.add(createInstancedMesh(shadeEntries, shadeMaterial));
scene.add(createInstancedMesh(wetEntries, wetMaterial));
scene.add(createInstancedMesh(exposedEntries, exposedMaterial));
// A stepped stone overhang reinforces the shaded section.
const lintelEntries = [];
for (let i = 0; i < 7; i++) {
lintelEntries.push({
p: [-5.12 + i * 0.72, 2.68 + (i % 2) * 0.025, 0.16],
s: [0.69, 0.24, 1.02],
c: colorVariation(0x4f5b55, 0.09)
});
}
const lintelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96
});
scene.add(createInstancedMesh(lintelEntries, lintelMaterial));
// Thin runoff and moisture marks on the front plane.
const wetFilmEntries = [];
const exposedRunoffEntries = [];
for (let i = 0; i < 24; i++) {
wetFilmEntries.push({
p: [
-1.05 + random() * 3.25,
0.52 + random() * 1.75,
0.365
],
s: [
0.025 + random() * 0.045,
0.18 + random() * 0.48,
0.012
],
c: new THREE.Color(random() > 0.45 ? 0x24474b : 0x3a6263)
});
}
for (let i = 0; i < 13; i++) {
exposedRunoffEntries.push({
p: [
3.08 + random() * 2.55,
0.48 + random() * 1.58,
0.365
],
s: [
0.02 + random() * 0.035,
0.12 + random() * 0.32,
0.009
],
c: new THREE.Color(random() > 0.5 ? 0x826653 : 0xb59a7c)
});
}
const wetFilmMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.08,
metalness: 0.06,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const runoffMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88,
transparent: true,
opacity: 0.5,
depthWrite: false
});
scene.add(createInstancedMesh(wetFilmEntries, wetFilmMaterial, false));
scene.add(createInstancedMesh(exposedRunoffEntries, runoffMaterial, false));
// Small protruding lichen and moss voxels.
const growthEntries = [];
for (let i = 0; i < 42; i++) {
const shaded = i < 24;
const x = shaded
? -5.45 + random() * 3.3
: -1.15 + random() * 3.2;
const y = shaded
? 0.55 + random() * 1.8
: 0.35 + random() * 0.78;
growthEntries.push({
p: [x, y, 0.375],
s: [
0.055 + random() * 0.075,
0.045 + random() * 0.065,
0.025 + random() * 0.045
],
c: colorVariation(shaded ? 0x718461 : 0x587353, 0.14)
});
}
const growthMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9
});
scene.add(createInstancedMesh(growthEntries, growthMaterial));
// Stepped, translucent puddle voxels below the wet section.
const puddleEntries = [
{ p: [-0.45, 0.025, 0.75], s: [0.46, 0.025, 0.42], c: 0x75a9aa },
{ p: [0.02, 0.025, 0.73], s: [0.48, 0.025, 0.46], c: 0x6ba3a6 },
{ p: [0.49, 0.025, 0.76], s: [0.43, 0.025, 0.41], c: 0x79b0ae },
{ p: [-0.18, 0.025, 1.12], s: [0.40, 0.025, 0.34], c: 0x83b5b2 },
{ p: [0.32, 0.025, 1.10], s: [0.46, 0.025, 0.37], c: 0x6ea6a8 },
{ p: [0.74, 0.025, 1.02], s: [0.31, 0.025, 0.31], c: 0x80b4b1 }
];
const puddleMaterial = new THREE.MeshPhysicalMaterial({
color: 0xffffff,
roughness: 0.08,
metalness: 0,
transparent: true,
opacity: 0.58,
transmission: 0.12,
depthWrite: false
});
scene.add(createInstancedMesh(puddleEntries, puddleMaterial, false));
// Detached pale chips below the exposed face.
const chipEntries = [];
for (let i = 0; i < 19; i++) {
const size = 0.055 + random() * 0.105;
chipEntries.push({
p: [
3.1 + random() * 2.85,
0.06 + size * 0.45,
0.62 + random() * 1.35
],
s: [size, size * (0.55 + random() * 0.6), size],
c: colorVariation(0xb7a58a, 0.13)
});
}
scene.add(createInstancedMesh(chipEntries, exposedMaterial));
// A few foreground stones establish scale without distracting from the wall.
const pebbleEntries = [];
for (let i = 0; i < 13; i++) {
const size = 0.06 + random() * 0.13;
pebbleEntries.push({
p: [
-6.3 + random() * 12.6,
0.045 + size * 0.4,
1.65 + random() * 1.45
],
s: [size, size * 0.65, size],
c: colorVariation(random() > 0.5 ? 0x8d9386 : 0xa29a84, 0.1)
});
}
const pebbleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95
});
scene.add(createInstancedMesh(pebbleEntries, pebbleMaterial));
// HTML labels are projected from fixed world-space anchors.
const labels = [
{
element: document.querySelector("#label-shade"),
anchor: new THREE.Vector3(-3.85, 3.15, 0.1)
},
{
element: document.querySelector("#label-wet"),
anchor: new THREE.Vector3(0.52, 3.15, 0.1)
},
{
element: document.querySelector("#label-exposed"),
anchor: new THREE.Vector3(4.35, 3.15, 0.1)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
labels.forEach(({ element, anchor }) => {
projected.copy(anchor).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.classList.toggle("visible", visible);
if (visible) {
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
});
}
// Camera controls are implemented directly, without external controls modules.
let dragging = false;
let previousX = 0;
let previousY = 0;
let userHasMoved = false;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
canvas.focus();
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.desiredTheta -= deltaX * 0.006;
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi - deltaY * 0.005,
0.63,
1.48
);
userHasMoved = true;
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
canvas.classList.remove("dragging");
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius + event.deltaY * 0.012,
9,
32
);
userHasMoved = true;
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#reset").addEventListener("click", resetCamera);
canvas.addEventListener("keydown", (event) => {
const key = event.key;
if (key === "ArrowLeft") cameraState.desiredTheta += 0.08;
else if (key === "ArrowRight") cameraState.desiredTheta -= 0.08;
else if (key === "ArrowUp") cameraState.desiredPhi -= 0.06;
else if (key === "ArrowDown") cameraState.desiredPhi += 0.06;
else if (key === "+" || key === "=") cameraState.desiredRadius -= 0.6;
else if (key === "-") cameraState.desiredRadius += 0.6;
else return;
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi,
0.63,
1.48
);
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius,
9,
32
);
event.preventDefault();
});
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 (!userHasMoved) {
cameraState.desiredRadius = window.innerWidth < 760 ? 25 : 15.7;
}
});
function animate() {
requestAnimationFrame(animate);
cameraState.theta +=
(cameraState.desiredTheta - cameraState.theta) * 0.075;
cameraState.phi +=
(cameraState.desiredPhi - cameraState.phi) * 0.075;
cameraState.radius +=
(cameraState.desiredRadius - cameraState.radius) * 0.075;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
renderer.render(scene, camera);
updateLabels();
}
animate();
</script>
</body>
</html>
```