Prompt specification
Build or customize the Three.js 3D scene "Dry-stone wall with overlapping blocks" 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: Dry-stone wall with overlapping blocks
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/dry-stone-wall-with-overlapping-blocks-x2349
Scene Description Prompt:
Please make a stylized Three.js scene in one standalone HTML file using cdnjs. Build a short referenced Great Zimbabwe-style dry-stone wall section. Show stacked irregular stone units, visible joints, and wall thickness without applying mortar textures.
Working Three.js Starter (dry-stone-wall-with-overlapping-blocks-x2349):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Great Zimbabwe–Inspired Dry-Stone Wall</title>
<style>
:root {
color-scheme: light;
--ink: #252721;
--paper: rgba(246, 241, 225, 0.84);
--line: rgba(54, 55, 46, 0.2);
--accent: #9f633d;
}
* {
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, 247, 216, 0.72), transparent 29%),
linear-gradient(180deg, #aebec1 0%, #ddd9c9 53%, #b9ae91 100%);
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.info-panel {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 19px 20px 18px;
border: 1px solid rgba(255, 255, 255, 0.48);
border-radius: 14px;
background: var(--paper);
box-shadow: 0 16px 44px rgba(43, 43, 34, 0.15);
backdrop-filter: blur(14px) saturate(0.9);
-webkit-backdrop-filter: blur(14px) saturate(0.9);
pointer-events: none;
}
.eyebrow {
margin: 0 0 8px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.23em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 3vw, 36px);
font-weight: 500;
line-height: 1.05;
letter-spacing: -0.025em;
}
.description {
margin: 12px 0 15px;
color: rgba(37, 39, 33, 0.74);
font-size: 13px;
line-height: 1.55;
}
.facts {
display: flex;
flex-wrap: wrap;
gap: 7px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.fact {
padding: 5px 8px;
border: 1px solid rgba(54, 55, 46, 0.14);
border-radius: 999px;
background: rgba(255, 255, 255, 0.3);
color: rgba(37, 39, 33, 0.72);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.controls {
position: fixed;
z-index: 12;
top: 22px;
right: 22px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: calc(100vw - 44px);
}
.controls button {
appearance: none;
border: 1px solid rgba(255, 255, 255, 0.52);
border-radius: 999px;
padding: 10px 14px;
color: #303129;
background: rgba(246, 241, 225, 0.86);
box-shadow: 0 8px 24px rgba(43, 43, 34, 0.12);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.045em;
cursor: pointer;
transition:
transform 150ms ease,
background 150ms ease;
}
.controls button:hover {
transform: translateY(-1px);
background: rgba(255, 249, 232, 0.96);
}
.controls button.active {
color: #fff9e9;
background: rgba(72, 70, 59, 0.9);
}
.hint {
position: fixed;
z-index: 10;
left: 22px;
bottom: 20px;
display: flex;
align-items: center;
gap: 9px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.36);
border-radius: 999px;
background: rgba(43, 45, 37, 0.58);
color: rgba(255, 255, 255, 0.88);
backdrop-filter: blur(9px);
-webkit-backdrop-filter: blur(9px);
font-size: 10px;
letter-spacing: 0.035em;
pointer-events: none;
}
.hint span {
opacity: 0.62;
}
#annotations {
position: fixed;
inset: 0;
z-index: 8;
pointer-events: none;
opacity: 0;
transition: opacity 180ms ease;
}
#annotations.visible {
opacity: 1;
}
.annotation {
position: absolute;
transform: translate(-50%, -100%);
padding: 7px 9px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 7px;
background: rgba(40, 42, 35, 0.78);
color: #fffaf0;
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.16);
backdrop-filter: blur(7px);
-webkit-backdrop-filter: blur(7px);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.045em;
white-space: nowrap;
}
.annotation::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 25px;
background: rgba(255, 250, 235, 0.72);
}
.annotation.depth {
transform: translate(12px, -50%);
}
.annotation.depth::after {
top: 50%;
left: 0;
width: 25px;
height: 1px;
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #d9d3c0;
color: #565448;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.info-panel {
top: 14px;
left: 14px;
width: min(290px, calc(100vw - 28px));
padding: 15px 16px;
}
.description {
margin: 9px 0 11px;
font-size: 12px;
}
.facts {
padding-top: 10px;
}
.controls {
top: auto;
right: 14px;
bottom: 54px;
justify-content: flex-end;
}
.controls button {
padding: 9px 11px;
font-size: 10px;
}
.hint {
left: 14px;
bottom: 14px;
}
#annotations {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<section class="info-panel" aria-label="Scene information">
<p class="eyebrow">Great Zimbabwe · Construction Study</p>
<h1>Dry-stone<br>wall section</h1>
<p class="description">
A Great Zimbabwe–inspired study of coursed granite masonry. The joints are
physical gaps between individually modeled stones—no mortar or joint texture.
The broken right end exposes the wall’s thickness.
</p>
<div class="facts">
<div class="fact">Open joints</div>
<div class="fact">Through stones</div>
<div class="fact">Slight batter</div>
<div class="fact" id="stoneCount">— stones</div>
</div>
</section>
<nav class="controls" aria-label="Camera views">
<button id="threeQuarter" class="active">Three-quarter</button>
<button id="endView">End section</button>
<button id="topView">Top</button>
<button id="labelsButton" aria-pressed="false">Labels</button>
<button id="resetButton">Reset</button>
</nav>
<div id="annotations" aria-hidden="true">
<div id="jointLabel" class="annotation">Open vertical joint</div>
<div id="depthLabel" class="annotation depth">Exposed wall thickness</div>
</div>
<div class="hint">
Drag to orbit <span>•</span> Scroll / pinch to zoom
</div>
<div id="loading">Assembling stone courses</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene and renderer
// ------------------------------------------------------------
const stage = document.querySelector("#stage");
const loading = document.querySelector("#loading");
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xd8d3c1, 9, 24);
const camera = new THREE.PerspectiveCamera(
39,
innerWidth / innerHeight,
0.08,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
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 three-dimensional model of a dry-stone wall"
);
stage.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xf4ead1, 0x535b43, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffdfad, 3.1);
sun.position.set(-4.8, 8.5, 5.6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 24;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0xb9d1dc, 0.72);
coolFill.position.set(6, 3.5, -5);
scene.add(coolFill);
// Deterministic random values keep the masonry composition stable.
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(84731);
const range = (min, max) => min + (max - min) * random();
// ------------------------------------------------------------
// Irregular bevelled stone geometry
// ------------------------------------------------------------
function createStoneGeometry(width, height, depth, seed) {
const hx = width * 0.5;
const hy = height * 0.5;
const hz = depth * 0.5;
const bevel = Math.min(
Math.min(width, height, depth) * 0.16,
range(0.045, 0.085)
);
const xi = hx - bevel;
const yi = hy - bevel;
const zi = hz - bevel;
const positions = [];
const addTriangle = (a, b, c) => {
positions.push(...a, ...b, ...c);
};
const addQuad = (a, b, c, d) => {
addTriangle(a, b, c);
addTriangle(a, c, d);
};
const fx = (sx, sy, sz) => [
sx * hx,
sy * yi,
sz * zi
];
const fy = (sy, sx, sz) => [
sx * xi,
sy * hy,
sz * zi
];
const fz = (sz, sx, sy) => [
sx * xi,
sy * yi,
sz * hz
];
// Six principal faces.
addQuad(
fx(1, -1, 1), fx(1, -1, -1),
fx(1, 1, -1), fx(1, 1, 1)
);
addQuad(
fx(-1, -1, -1), fx(-1, -1, 1),
fx(-1, 1, 1), fx(-1, 1, -1)
);
addQuad(
fy(1, -1, 1), fy(1, 1, 1),
fy(1, 1, -1), fy(1, -1, -1)
);
addQuad(
fy(-1, -1, -1), fy(-1, 1, -1),
fy(-1, 1, 1), fy(-1, -1, 1)
);
addQuad(
fz(1, -1, -1), fz(1, 1, -1),
fz(1, 1, 1), fz(1, -1, 1)
);
addQuad(
fz(-1, 1, -1), fz(-1, -1, -1),
fz(-1, -1, 1), fz(-1, 1, 1)
);
// Twelve beveled edge strips.
for (const sy of [-1, 1]) {
for (const sz of [-1, 1]) {
const a = fy(sy, -1, sz);
const d = fy(sy, 1, sz);
const c = fz(sz, 1, sy);
const b = fz(sz, -1, sy);
addQuad(a, d, c, b);
}
}
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
const a = fx(sx, -1, sz);
const d = fx(sx, 1, sz);
const c = fz(sz, sx, 1);
const b = fz(sz, sx, -1);
addQuad(a, d, c, b);
}
}
for (const sx of [-1, 1]) {
for (const sy of [-1, 1]) {
const a = fx(sx, sy, -1);
const d = fx(sx, sy, 1);
const c = fy(sy, sx, 1);
const b = fy(sy, sx, -1);
addQuad(a, d, c, b);
}
}
// Eight corner facets.
for (const sx of [-1, 1]) {
for (const sy of [-1, 1]) {
for (const sz of [-1, 1]) {
addTriangle(
fx(sx, sy, sz),
fy(sy, sx, sz),
fz(sz, sx, sy)
);
}
}
}
// Tiny deterministic surface irregularity. Matching coordinates receive
// identical displacement so the separate facets remain joined.
for (let i = 0; i < positions.length; i += 3) {
const x = positions[i];
const y = positions[i + 1];
const z = positions[i + 2];
const n =
Math.sin(x * 31.7 + y * 47.3 + z * 23.1 + seed * 1.91) * 0.0045 +
Math.sin(x * 71.1 - y * 18.9 + z * 53.7 + seed * 0.73) * 0.002;
positions[i] = x + n;
positions[i + 1] = y + n * 0.72;
positions[i + 2] = z + n * 0.91;
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
const graniteColors = [
0x858176,
0x77776f,
0x928a7b,
0x6f716b,
0x817c70,
0x9a9180,
0x74736b,
0x898477
];
const graniteMaterials = graniteColors.map(
(color) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.94,
metalness: 0.015,
flatShading: true,
side: THREE.DoubleSide
})
);
const coreMaterial = new THREE.MeshStandardMaterial({
color: 0x56574f,
roughness: 1,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
});
let stoneCount = 0;
let geometrySeed = 1;
function addStone(parent, width, height, depth, material, position) {
const mesh = new THREE.Mesh(
createStoneGeometry(width, height, depth, geometrySeed++),
material
);
mesh.position.copy(position);
mesh.rotation.set(
range(-0.012, 0.012),
range(-0.022, 0.022),
range(-0.009, 0.009)
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
stoneCount++;
return mesh;
}
// ------------------------------------------------------------
// Terrain
// ------------------------------------------------------------
function groundHeight(x, z) {
return (
0.015 +
Math.sin(x * 0.17) * Math.sin(z * 0.21) * 0.045 +
Math.cos(z * 0.11 + 0.8) * 0.012
);
}
const groundGeometry = new THREE.PlaneGeometry(26, 26, 44, 44);
groundGeometry.rotateX(-Math.PI / 2);
const groundPositions = groundGeometry.attributes.position;
for (let i = 0; i < groundPositions.count; i++) {
const x = groundPositions.getX(i);
const z = groundPositions.getZ(i);
groundPositions.setY(i, groundHeight(x, z));
}
groundPositions.needsUpdate = true;
groundGeometry.computeVertexNormals();
const ground = new THREE.Mesh(
groundGeometry,
new THREE.MeshStandardMaterial({
color: 0x8e8b62,
roughness: 1,
metalness: 0
})
);
ground.receiveShadow = true;
scene.add(ground);
// A compact crossed-blade tuft, instanced across the terrain.
function makeGrassGeometry() {
const positions = [];
for (let i = 0; i < 3; i++) {
const angle = (i / 3) * Math.PI * 2 + 0.3;
const dx = Math.cos(angle);
const dz = Math.sin(angle);
const px = -dz;
const pz = dx;
const width = 0.025;
positions.push(
-px * width, 0, -pz * width,
px * width, 0, pz * width,
dx * 0.055, 0.22, dz * 0.055
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
const grass = new THREE.InstancedMesh(
makeGrassGeometry(),
new THREE.MeshStandardMaterial({
color: 0x626b43,
roughness: 1,
side: THREE.DoubleSide
}),
180
);
const grassDummy = new THREE.Object3D();
let grassIndex = 0;
while (grassIndex < 180) {
const x = range(-8, 8);
const z = range(-6, 6);
// Keep vegetation from obscuring the wall.
if (Math.abs(x) < 3.1 && Math.abs(z) < 0.9) continue;
const scale = range(0.55, 1.35);
grassDummy.position.set(x, groundHeight(x, z) + 0.005, z);
grassDummy.rotation.set(0, range(0, Math.PI * 2), 0);
grassDummy.scale.set(scale, scale * range(0.8, 1.3), scale);
grassDummy.updateMatrix();
grass.setMatrixAt(grassIndex, grassDummy.matrix);
grassIndex++;
}
grass.receiveShadow = true;
scene.add(grass);
// ------------------------------------------------------------
// Dry-stone wall
// ------------------------------------------------------------
const wall = new THREE.Group();
scene.add(wall);
const wallLength = 5.45;
const courseCount = 9;
let courseBase = 0.035;
for (let row = 0; row < courseCount; row++) {
const rowHeight = range(0.275, 0.335);
const rowCenterY = courseBase + rowHeight * 0.5;
// The wall narrows and leans slightly inward toward the top.
const thickness = THREE.MathUtils.lerp(1.04, 0.78, row / (courseCount - 1));
const frontPlane = thickness * 0.5;
const backPlane = -thickness * 0.5;
const startX = -wallLength * 0.5 + range(0.01, 0.06);
const endX =
wallLength * 0.5 -
range(0.04, 0.14) -
(courseCount - 1 - row) * 0.012;
let x = startX;
while (x < endX - 0.08) {
let cellWidth = range(0.56, 0.96);
const remaining = endX - x;
if (cellWidth > remaining - 0.04) {
cellWidth = Math.max(0.22, remaining);
}
const gap = cellWidth > 0.32 ? range(0.018, 0.032) : 0.012;
const width = Math.max(0.16, cellWidth - gap);
const height = Math.max(
0.16,
rowHeight - 0.018 - range(0, 0.018)
);
const isThroughStone = random() < 0.26;
const material = graniteMaterials[
Math.floor(random() * graniteMaterials.length)
];
if (isThroughStone) {
// Full-depth stones bind the two faces together.
const depth = thickness * range(0.91, 0.99);
addStone(
wall,
width,
height,
depth,
material,
new THREE.Vector3(
x + width * 0.5,
rowCenterY + range(-0.005, 0.005),
range(-0.022, 0.022)
)
);
} else {
// Front face stone.
const frontDepth = Math.min(0.39, thickness * range(0.37, 0.44));
const frontRecess = range(-0.026, 0.012);
addStone(
wall,
width,
height,
frontDepth,
material,
new THREE.Vector3(
x + width * 0.5,
rowCenterY + range(-0.004, 0.004),
frontPlane - frontDepth * 0.5 + frontRecess
)
);
// Rear face stone, slightly recessed at the exposed end.
const backDepth = Math.min(0.34, thickness * range(0.29, 0.36));
const backWidth = Math.max(0.15, width - range(0.025, 0.055));
addStone(
wall,
backWidth,
height * range(0.92, 1),
backDepth,
graniteMaterials[
Math.floor(random() * graniteMaterials.length)
],
new THREE.Vector3(
x + width * 0.5 + range(0.01, 0.035),
rowCenterY + range(-0.004, 0.004),
backPlane + backDepth * 0.5 - range(0.005, 0.025)
)
);
// Smaller hearting/core stone fills the space between the faces.
const coreDepth = Math.max(
0.23,
thickness - frontDepth - backDepth + 0.025
);
addStone(
wall,
width * range(0.48, 0.68),
height * range(0.48, 0.67),
coreDepth,
coreMaterial,
new THREE.Vector3(
x + width * range(0.42, 0.58),
rowCenterY + range(-0.018, 0.018),
range(-0.035, 0.035)
)
);
}
x += cellWidth;
}
courseBase += rowHeight;
}
// Uneven coping stones bridge part of the wall's thickness.
const topThickness = 0.78;
let copingX = -wallLength * 0.5 + 0.12;
while (copingX < wallLength * 0.5 - 0.4) {
const width = Math.min(
range(0.72, 1.13),
wallLength * 0.5 - 0.18 - copingX
);
if (width < 0.25) break;
addStone(
wall,
width - range(0.02, 0.045),
range(0.16, 0.22),
range(0.56, 0.73),
graniteMaterials[
Math.floor(random() * graniteMaterials.length)
],
new THREE.Vector3(
copingX + width * 0.5,
courseBase + range(0.055, 0.085),
range(-0.075, 0.075)
)
);
copingX += width + range(0.025, 0.05);
}
document.querySelector("#stoneCount").textContent =
`${stoneCount} stones`;
// Loose stones around the base.
for (let i = 0; i < 18; i++) {
let x = range(-6.5, 6.5);
let z = range(-3.5, 3.5);
if (Math.abs(x) < wallLength * 0.5 + 0.25 && Math.abs(z) < 0.85) {
x += Math.sign(x || 1) * 1.2;
}
const size = range(0.09, 0.25);
addStone(
scene,
size * range(1.0, 1.8),
size * range(0.55, 0.9),
size * range(0.8, 1.3),
graniteMaterials[
Math.floor(random() * graniteMaterials.length)
],
new THREE.Vector3(
x,
groundHeight(x, z) + size * 0.3,
z
)
);
}
// ------------------------------------------------------------
// Direct camera controls: orbit, zoom, touch pinch, and keyboard
// ------------------------------------------------------------
const defaultView = {
theta: 0.69,
phi: 1.12,
radius: 7.55,
target: new THREE.Vector3(0, 1.18, 0)
};
let desiredTheta = defaultView.theta;
let desiredPhi = defaultView.phi;
let desiredRadius = defaultView.radius;
const desiredTarget = defaultView.target.clone();
let currentTheta = desiredTheta;
let currentPhi = desiredPhi;
let currentRadius = desiredRadius;
const currentTarget = desiredTarget.clone();
const canvas = renderer.domElement;
const pointers = new Map();
let previousPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
previousPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
desiredTheta -= dx * 0.006;
desiredPhi = THREE.MathUtils.clamp(
desiredPhi - dy * 0.005,
0.38,
1.46
);
}
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
desiredRadius = THREE.MathUtils.clamp(
desiredRadius * (previousPinchDistance / distance),
3.8,
13.5
);
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointers.size === 2 ? pointerDistance() : 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius * Math.exp(event.deltaY * 0.001),
3.8,
13.5
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
setView(defaultView);
});
addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") desiredTheta += 0.08;
if (event.key === "ArrowRight") desiredTheta -= 0.08;
if (event.key === "ArrowUp") {
desiredPhi = THREE.MathUtils.clamp(desiredPhi - 0.06, 0.38, 1.46);
}
if (event.key === "ArrowDown") {
desiredPhi = THREE.MathUtils.clamp(desiredPhi + 0.06, 0.38, 1.46);
}
if (event.key === "+" || event.key === "=") {
desiredRadius = Math.max(3.8, desiredRadius - 0.35);
}
if (event.key === "-" || event.key === "_") {
desiredRadius = Math.min(13.5, desiredRadius + 0.35);
}
});
function setView(view) {
desiredTheta = view.theta;
desiredPhi = view.phi;
desiredRadius = view.radius;
desiredTarget.copy(view.target);
}
function updateCamera() {
const sinPhi = Math.sin(currentPhi);
camera.position.set(
currentTarget.x + currentRadius * sinPhi * Math.sin(currentTheta),
currentTarget.y + currentRadius * Math.cos(currentPhi),
currentTarget.z + currentRadius * sinPhi * Math.cos(currentTheta)
);
camera.lookAt(currentTarget);
}
updateCamera();
// ------------------------------------------------------------
// UI and projected annotations
// ------------------------------------------------------------
const threeQuarterButton = document.querySelector("#threeQuarter");
const endViewButton = document.querySelector("#endView");
const topViewButton = document.querySelector("#topView");
const resetButton = document.querySelector("#resetButton");
const labelsButton = document.querySelector("#labelsButton");
const annotations = document.querySelector("#annotations");
function setActiveButton(button) {
[threeQuarterButton, endViewButton, topViewButton].forEach((item) => {
item.classList.toggle("active", item === button);
});
}
threeQuarterButton.addEventListener("click", () => {
setView(defaultView);
setActiveButton(threeQuarterButton);
});
endViewButton.addEventListener("click", () => {
setView({
theta: 1.34,
phi: 1.16,
radius: 6.7,
target: new THREE.Vector3(0.75, 1.22, 0)
});
setActiveButton(endViewButton);
});
topViewButton.addEventListener("click", () => {
setView({
theta: 0.62,
phi: 0.53,
radius: 7.6,
target: new THREE.Vector3(0, 1.05, 0)
});
setActiveButton(topViewButton);
});
resetButton.addEventListener("click", () => {
setView(defaultView);
setActiveButton(threeQuarterButton);
});
labelsButton.addEventListener("click", () => {
const visible = annotations.classList.toggle("visible");
labelsButton.classList.toggle("active", visible);
labelsButton.setAttribute("aria-pressed", String(visible));
});
const jointAnchor = new THREE.Vector3(0.33, 1.02, 0.49);
const depthAnchor = new THREE.Vector3(2.70, 1.26, 0.03);
const projected = new THREE.Vector3();
function positionAnnotation(element, anchor, offsetX = 0, offsetY = 0) {
projected.copy(anchor).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.visibility = visible ? "visible" : "hidden";
const x = (projected.x * 0.5 + 0.5) * innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * innerHeight + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// ------------------------------------------------------------
// Animation and resize
// ------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
function animate() {
requestAnimationFrame(animate);
const damping = 0.085;
currentTheta += (desiredTheta - currentTheta) * damping;
currentPhi += (desiredPhi - currentPhi) * damping;
currentRadius += (desiredRadius - currentRadius) * damping;
currentTarget.lerp(desiredTarget, damping);
updateCamera();
positionAnnotation(
document.querySelector("#jointLabel"),
jointAnchor,
0,
-31
);
positionAnnotation(
document.querySelector("#depthLabel"),
depthAnchor,
18,
-13
);
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 450);
});
</script>
</body>
</html>
```