Prompt specification
Build or customize the Three.js 3D scene "Carved false-door stela 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: Carved false-door stela detail
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/carved-false-door-stela-detail-x2265
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows an Aksumite stela detail. Model a small referenced Aksumite stela section with a carved false-door motif. Keep the relief shallow and clearly identify the piece as a stylized detail. Stylized solid forms.
Working Three.js Starter (carved-false-door-stela-detail-x2265):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Aksumite Stela — False-Door Detail</title>
<style>
:root {
--ink: #272521;
--muted: #6e675d;
--paper: #eee8dd;
--line: rgba(55, 48, 39, 0.18);
--accent: #7d3f2f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.title-card {
position: absolute;
top: 28px;
left: 30px;
width: min(370px, calc(100vw - 60px));
padding: 21px 23px 20px;
background: rgba(246, 242, 234, 0.82);
border: 1px solid rgba(69, 60, 49, 0.16);
border-radius: 3px;
box-shadow: 0 16px 50px rgba(72, 60, 45, 0.08);
backdrop-filter: blur(14px);
}
.eyebrow {
margin: 0 0 14px;
color: var(--accent);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(31px, 3.2vw, 46px);
font-weight: 400;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-family: Georgia, "Times New Roman", serif;
font-size: 18px;
font-style: italic;
}
.description {
max-width: 300px;
margin: 18px 0 0;
color: #5f5950;
font-size: 12px;
line-height: 1.65;
}
.badge-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 17px;
}
.badge {
padding: 6px 8px 5px;
border: 1px solid rgba(91, 71, 53, 0.2);
border-radius: 2px;
color: #665d51;
background: rgba(255, 255, 255, 0.25);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.badge.accent {
color: #f1e8dc;
background: var(--accent);
border-color: var(--accent);
}
.notes {
position: absolute;
top: 30px;
right: 30px;
width: 242px;
padding: 18px 19px;
background: rgba(246, 242, 234, 0.7);
border-top: 1px solid rgba(61, 54, 46, 0.28);
border-bottom: 1px solid rgba(61, 54, 46, 0.14);
backdrop-filter: blur(10px);
}
.notes h2 {
margin: 0 0 13px;
color: #514b43;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.note {
display: grid;
grid-template-columns: 66px 1fr;
gap: 9px;
padding: 9px 0;
border-top: 1px solid var(--line);
color: #6b645a;
font-size: 10px;
line-height: 1.45;
}
.note:first-of-type {
border-top: 0;
}
.note strong {
color: #3f3a34;
font-size: 9px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.controls {
position: absolute;
right: 30px;
bottom: 27px;
display: flex;
gap: 7px;
pointer-events: auto;
}
.controls button {
min-width: 72px;
height: 34px;
padding: 0 12px;
border: 1px solid rgba(61, 54, 46, 0.22);
border-radius: 2px;
color: #514a42;
background: rgba(246, 242, 234, 0.86);
font: inherit;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(8px);
transition:
color 160ms ease,
background 160ms ease,
border-color 160ms ease;
}
.controls button:hover,
.controls button.active {
color: #f7f1e7;
background: #3f3932;
border-color: #3f3932;
}
.hint {
position: absolute;
left: 30px;
bottom: 29px;
display: flex;
align-items: center;
gap: 10px;
color: rgba(57, 52, 45, 0.62);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.mouse {
position: relative;
width: 17px;
height: 24px;
border: 1px solid currentColor;
border-radius: 9px;
}
.mouse::before {
content: "";
position: absolute;
top: 4px;
left: 7px;
width: 2px;
height: 5px;
border-radius: 2px;
background: currentColor;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: var(--paper);
transition: opacity 500ms ease;
}
#loading div {
color: #6c6459;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.18em;
text-transform: uppercase;
}
body.ready #loading {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.title-card {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 16px 18px;
}
.eyebrow {
margin-bottom: 9px;
}
h1 {
font-size: 31px;
}
.subtitle {
font-size: 16px;
}
.description {
margin-top: 11px;
font-size: 11px;
}
.badge-row {
margin-top: 12px;
}
.notes {
display: none;
}
.controls {
right: 14px;
bottom: 14px;
}
.controls button {
min-width: 60px;
height: 32px;
padding: 0 9px;
font-size: 8px;
}
.hint {
left: 16px;
bottom: 58px;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive three-dimensional Aksumite stela detail"></main>
<div class="ui">
<section class="title-card">
<p class="eyebrow">Aksumite tradition · Ethiopian Highlands</p>
<h1>False-door<br />stela detail</h1>
<p class="subtitle">An interpretive carved-stone fragment</p>
<p class="description">
A simplified study of the characteristic geometric false door:
paired leaves, a recessed field, and a stepped lintel.
</p>
<div class="badge-row">
<span class="badge accent">Stylized detail</span>
<span class="badge">Not a site replica</span>
<span class="badge">Shallow relief</span>
</div>
</section>
<aside class="notes" aria-label="Model interpretation notes">
<h2>Interpretive features</h2>
<div class="note">
<strong>Motif</strong>
<span>Paired recessed door leaves with a central seam.</span>
</div>
<div class="note">
<strong>Frame</strong>
<span>Abstract jambs and a stepped, projecting lintel.</span>
</div>
<div class="note">
<strong>Material</strong>
<span>Warm gray stone rendered as simplified solid masses.</span>
</div>
<div class="note">
<strong>Treatment</strong>
<span>Proportions and carving depth are intentionally stylized.</span>
</div>
</aside>
<div class="hint">
<span class="mouse" aria-hidden="true"></span>
<span>Drag to orbit · Scroll to zoom</span>
</div>
<nav class="controls" aria-label="Camera views">
<button type="button" data-view="angle" class="active">3/4 View</button>
<button type="button" data-view="front">Front</button>
<button type="button" data-view="reset">Reset</button>
</nav>
</div>
<div id="loading" aria-live="polite">
<div>Preparing stone study</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e2d7);
scene.fog = new THREE.Fog(0xe9e2d7, 15, 30);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
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.06;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.id = "webgl";
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional stylized Aksumite stela with a carved false-door motif"
);
stage.appendChild(renderer.domElement);
// ---------------------------------------------------------------------
// Procedural stone textures: no external image assets.
// ---------------------------------------------------------------------
function seededRandom(seed) {
let value = seed >>> 0;
return function () {
value = (1664525 * value + 1013904223) >>> 0;
return value / 4294967296;
};
}
function createStoneTextures() {
const size = 512;
const random = seededRandom(715204);
const colorCanvas = document.createElement("canvas");
colorCanvas.width = colorCanvas.height = size;
const colorContext = colorCanvas.getContext("2d");
colorContext.fillStyle = "#aaa49a";
colorContext.fillRect(0, 0, size, size);
for (let i = 0; i < 55; i++) {
const x = random() * size;
const y = random() * size;
const radius = 25 + random() * 110;
const gradient = colorContext.createRadialGradient(
x,
y,
0,
x,
y,
radius
);
const light = random() > 0.5;
gradient.addColorStop(
0,
light ? "rgba(225,216,201,0.13)" : "rgba(55,51,45,0.10)"
);
gradient.addColorStop(1, "rgba(0,0,0,0)");
colorContext.fillStyle = gradient;
colorContext.fillRect(x - radius, y - radius, radius * 2, radius * 2);
}
for (let i = 0; i < 4200; i++) {
const x = random() * size;
const y = random() * size;
const radius = 0.25 + random() * 1.25;
const tone = Math.floor(45 + random() * 120);
colorContext.fillStyle = `rgba(${tone},${tone - 3},${
tone - 7
},${0.08 + random() * 0.18})`;
colorContext.beginPath();
colorContext.arc(x, y, radius, 0, Math.PI * 2);
colorContext.fill();
}
for (let i = 0; i < 85; i++) {
const x = random() * size;
const y = random() * size;
const length = 8 + random() * 35;
colorContext.strokeStyle =
random() > 0.5
? "rgba(240,232,217,0.07)"
: "rgba(52,48,43,0.06)";
colorContext.lineWidth = 0.5 + random();
colorContext.beginPath();
colorContext.moveTo(x, y);
colorContext.lineTo(x + length, y + (random() - 0.5) * 4);
colorContext.stroke();
}
const bumpCanvas = document.createElement("canvas");
bumpCanvas.width = bumpCanvas.height = size;
const bumpContext = bumpCanvas.getContext("2d");
bumpContext.drawImage(colorCanvas, 0, 0);
const colorTexture = new THREE.CanvasTexture(colorCanvas);
colorTexture.colorSpace = THREE.SRGBColorSpace;
colorTexture.wrapS = colorTexture.wrapT = THREE.RepeatWrapping;
colorTexture.repeat.set(0.46, 0.46);
colorTexture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const bumpTexture = new THREE.CanvasTexture(bumpCanvas);
bumpTexture.wrapS = bumpTexture.wrapT = THREE.RepeatWrapping;
bumpTexture.repeat.copy(colorTexture.repeat);
bumpTexture.anisotropy = colorTexture.anisotropy;
return { colorTexture, bumpTexture };
}
const { colorTexture, bumpTexture } = createStoneTextures();
const slabMaterial = new THREE.MeshStandardMaterial({
color: 0x9b8d79,
map: colorTexture,
bumpMap: bumpTexture,
bumpScale: 0.035,
roughness: 0.91,
metalness: 0.015
});
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0xb1a18a,
map: colorTexture,
bumpMap: bumpTexture,
bumpScale: 0.028,
roughness: 0.87,
metalness: 0.01
});
const recessedMaterial = new THREE.MeshStandardMaterial({
color: 0x6f665c,
map: colorTexture,
bumpMap: bumpTexture,
bumpScale: 0.025,
roughness: 0.96
});
const doorMaterial = new THREE.MeshStandardMaterial({
color: 0x887968,
map: colorTexture,
bumpMap: bumpTexture,
bumpScale: 0.023,
roughness: 0.92
});
const detailMaterial = new THREE.MeshStandardMaterial({
color: 0xb5a58e,
map: colorTexture,
bumpMap: bumpTexture,
bumpScale: 0.018,
roughness: 0.86
});
const plinthMaterial = new THREE.MeshStandardMaterial({
color: 0x48443e,
roughness: 0.78,
metalness: 0.03
});
// ---------------------------------------------------------------------
// Geometry helpers.
// ---------------------------------------------------------------------
function makeBlockGeometry(width, height, depth, radius = 0.025) {
const r = Math.min(
radius,
width * 0.2,
height * 0.2,
depth * 0.2
);
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
const bevel = Math.min(0.018, depth * 0.12);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: bevel,
bevelThickness: bevel,
curveSegments: 3
});
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
return geometry;
}
function addBlock(
width,
height,
depth,
x,
y,
z,
material,
radius = 0.025
) {
const mesh = new THREE.Mesh(
makeBlockGeometry(width, height, depth, radius),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Stylized fragment: irregular broken silhouette with a real opening.
// The backing panel closes the opening and creates the shallow recess.
// ---------------------------------------------------------------------
const artifact = new THREE.Group();
scene.add(artifact);
function createSlabGeometry() {
const outline = [
[-2.63, -3.16],
[-2.12, -3.30],
[-1.47, -3.23],
[-0.72, -3.31],
[0.03, -3.20],
[0.78, -3.29],
[1.49, -3.18],
[2.16, -3.27],
[2.58, -3.10],
[2.67, -2.87],
[2.64, 2.91],
[2.48, 3.15],
[2.01, 3.24],
[1.58, 3.43],
[0.93, 3.34],
[0.16, 3.49],
[-0.64, 3.37],
[-1.23, 3.45],
[-1.74, 3.29],
[-2.20, 3.34],
[-2.64, 3.14]
];
const shape = new THREE.Shape();
shape.moveTo(outline[0][0], outline[0][1]);
for (let i = 1; i < outline.length; i++) {
shape.lineTo(outline[i][0], outline[i][1]);
}
shape.closePath();
// Clockwise path for the carved false-door opening.
const opening = new THREE.Path();
opening.moveTo(-1.56, -1.72);
opening.lineTo(-1.56, 1.48);
opening.lineTo(1.56, 1.48);
opening.lineTo(1.56, -1.72);
opening.closePath();
shape.holes.push(opening);
const depth = 0.72;
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 2,
bevelSize: 0.045,
bevelThickness: 0.045,
curveSegments: 1
});
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
return geometry;
}
const slabGeometry = createSlabGeometry();
const slab = new THREE.Mesh(slabGeometry, slabMaterial);
slab.castShadow = true;
slab.receiveShadow = true;
artifact.add(slab);
const edgeLines = new THREE.LineSegments(
new THREE.EdgesGeometry(slabGeometry, 34),
new THREE.LineBasicMaterial({
color: 0x4a4035,
transparent: true,
opacity: 0.18
})
);
artifact.add(edgeLines);
// Recessed backing closes the carved opening.
addBlock(
3.20,
3.26,
0.10,
0,
-0.11,
0.185,
recessedMaterial,
0.015
);
// Paired door leaves, set slightly behind the slab face.
const leafWidth = 1.34;
const leafHeight = 2.96;
const leafY = -0.10;
for (const side of [-1, 1]) {
const leafX = side * 0.735;
addBlock(
leafWidth,
leafHeight,
0.08,
leafX,
leafY,
0.27,
doorMaterial,
0.016
);
// Fine, shallow panel borders.
addBlock(
0.045,
2.62,
0.035,
leafX - 0.57,
leafY,
0.326,
detailMaterial,
0.008
);
addBlock(
0.045,
2.62,
0.035,
leafX + 0.57,
leafY,
0.326,
detailMaterial,
0.008
);
addBlock(
1.17,
0.045,
0.035,
leafX,
leafY + 1.31,
0.326,
detailMaterial,
0.008
);
addBlock(
1.17,
0.045,
0.035,
leafX,
leafY - 1.31,
0.326,
detailMaterial,
0.008
);
// Abstract architectural bands on each leaf.
addBlock(
0.82,
0.055,
0.038,
leafX,
0.88,
0.335,
detailMaterial,
0.008
);
addBlock(
0.92,
0.065,
0.04,
leafX,
-0.91,
0.337,
detailMaterial,
0.008
);
// Small boss beside the central meeting line.
const boss = new THREE.Mesh(
new THREE.CylinderGeometry(0.068, 0.075, 0.05, 18),
detailMaterial
);
boss.rotation.x = Math.PI / 2;
boss.position.set(side * 0.13, 0.12, 0.355);
boss.castShadow = true;
artifact.add(boss);
}
// Projecting jambs and threshold.
addBlock(0.28, 3.18, 0.20, -1.68, -0.10, 0.46, frameMaterial);
addBlock(0.28, 3.18, 0.20, 1.68, -0.10, 0.46, frameMaterial);
addBlock(3.64, 0.25, 0.22, 0, -1.81, 0.47, frameMaterial);
addBlock(3.64, 0.25, 0.22, 0, 1.59, 0.47, frameMaterial);
// Stepped lintel, characteristic of the stylized false-door motif.
addBlock(3.16, 0.20, 0.18, 0, 1.79, 0.45, frameMaterial);
addBlock(2.62, 0.20, 0.16, 0, 1.99, 0.44, frameMaterial);
addBlock(2.08, 0.18, 0.14, 0, 2.18, 0.43, frameMaterial);
// Small lateral shoulders balance the lintel.
addBlock(0.43, 0.20, 0.17, -2.02, 1.67, 0.445, frameMaterial);
addBlock(0.43, 0.20, 0.17, 2.02, 1.67, 0.445, frameMaterial);
// Museum-style low support.
addBlock(
6.05,
0.25,
1.48,
0,
-3.46,
-0.02,
plinthMaterial,
0.045
);
// A narrow inset plaque on the plinth, rendered without text.
addBlock(
1.55,
0.13,
0.025,
0,
-3.43,
0.73,
new THREE.MeshStandardMaterial({
color: 0x8d704c,
roughness: 0.58,
metalness: 0.32
}),
0.012
);
// ---------------------------------------------------------------------
// Ground and lighting.
// ---------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xd8d0c3,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -3.60;
ground.receiveShadow = true;
scene.add(ground);
const hemisphere = new THREE.HemisphereLight(
0xfff4df,
0x514a42,
1.65
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffe7c3, 3.25);
keyLight.position.set(4.8, 7.8, 6.2);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc9d6df, 1.15);
fillLight.position.set(-5.5, 3.2, 5.5);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffd8aa, 1.7);
rimLight.position.set(1.5, 5.0, -6.0);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Direct camera controls. No addons or OrbitControls are used.
// ---------------------------------------------------------------------
const VIEWS = {
angle: {
theta: 0.48,
phi: 1.23,
radius: 10.8,
target: new THREE.Vector3(0, -0.05, 0)
},
front: {
theta: 0.0,
phi: 1.43,
radius: 10.4,
target: new THREE.Vector3(0, -0.05, 0)
},
reset: {
theta: 0.48,
phi: 1.23,
radius: 10.8,
target: new THREE.Vector3(0, -0.05, 0)
}
};
const desired = {
theta: VIEWS.angle.theta,
phi: VIEWS.angle.phi,
radius: VIEWS.angle.radius,
target: VIEWS.angle.target.clone()
};
const current = {
theta: desired.theta,
phi: desired.phi,
radius: desired.radius,
target: desired.target.clone()
};
const pointerMap = new Map();
let lastPinchDistance = 0;
let activeView = "angle";
const viewButtons = Array.from(
document.querySelectorAll("[data-view]")
);
function setView(name) {
const view = VIEWS[name];
if (!view) return;
desired.theta = view.theta;
desired.phi = view.phi;
desired.radius = view.radius;
desired.target.copy(view.target);
activeView = name;
viewButtons.forEach((button) => {
button.classList.toggle(
"active",
button.dataset.view === name
);
});
}
viewButtons.forEach((button) => {
button.addEventListener("click", () => {
setView(button.dataset.view);
});
});
function clampCamera() {
desired.phi = THREE.MathUtils.clamp(
desired.phi,
0.42,
Math.PI - 0.42
);
desired.radius = THREE.MathUtils.clamp(
desired.radius,
6.4,
16.0
);
desired.target.x = THREE.MathUtils.clamp(
desired.target.x,
-2.2,
2.2
);
desired.target.y = THREE.MathUtils.clamp(
desired.target.y,
-1.8,
1.8
);
}
function panCamera(deltaX, deltaY) {
camera.updateMatrixWorld();
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const scale = desired.radius * 0.00125;
desired.target.addScaledVector(right, -deltaX * scale);
desired.target.addScaledVector(up, deltaY * scale);
clampCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointerMap.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
if (pointerMap.size === 1) {
lastPinchDistance = 0;
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
const previous = pointerMap.get(event.pointerId);
if (!previous) return;
const deltaX = event.clientX - previous.x;
const deltaY = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointerMap.size === 1) {
if (event.shiftKey || previous.button === 2) {
panCamera(deltaX, deltaY);
} else {
desired.theta -= deltaX * 0.006;
desired.phi -= deltaY * 0.006;
clampCamera();
}
} else if (pointerMap.size === 2) {
const points = Array.from(pointerMap.values());
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (lastPinchDistance > 0 && distance > 0) {
desired.radius *= lastPinchDistance / distance;
clampCamera();
}
lastPinchDistance = distance;
}
});
function releasePointer(event) {
pointerMap.delete(event.pointerId);
if (pointerMap.size < 2) {
lastPinchDistance = 0;
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius *= Math.exp(event.deltaY * 0.001);
clampCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("dblclick", () => {
setView("reset");
});
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "r") {
setView("reset");
} else if (key === "f") {
setView("front");
} else if (event.key === "ArrowLeft") {
desired.theta -= 0.12;
event.preventDefault();
} else if (event.key === "ArrowRight") {
desired.theta += 0.12;
event.preventDefault();
} else if (event.key === "ArrowUp") {
desired.phi -= 0.08;
event.preventDefault();
} else if (event.key === "ArrowDown") {
desired.phi += 0.08;
event.preventDefault();
} else if (key === "+" || key === "=") {
desired.radius *= 0.9;
} else if (key === "-") {
desired.radius *= 1.1;
} else {
return;
}
clampCamera();
});
// ---------------------------------------------------------------------
// Resize and render loop.
// ---------------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
function updateCamera(deltaTime) {
const damping = reducedMotion ? 1 : 1 - Math.exp(-deltaTime * 9);
current.theta = THREE.MathUtils.lerp(
current.theta,
desired.theta,
damping
);
current.phi = THREE.MathUtils.lerp(
current.phi,
desired.phi,
damping
);
current.radius = THREE.MathUtils.lerp(
current.radius,
desired.radius,
damping
);
current.target.lerp(desired.target, damping);
// Keep the full fragment visible on narrow screens.
const aspect = window.innerWidth / window.innerHeight;
const fitMultiplier =
aspect < 0.78 ? Math.min(1.8, 0.78 / aspect) : 1;
const radius = current.radius * fitMultiplier;
const sinPhi = Math.sin(current.phi);
camera.position.set(
current.target.x + radius * sinPhi * Math.sin(current.theta),
current.target.y + radius * Math.cos(current.phi),
current.target.z + radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(current.target);
}
let previousTime = performance.now();
let firstFrame = true;
function animate(time) {
const deltaTime = Math.min((time - previousTime) / 1000, 0.05);
previousTime = time;
updateCamera(deltaTime);
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
requestAnimationFrame(() => {
document.body.classList.add("ready");
});
}
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```