Prompt specification
Build or customize the Three.js 3D scene "Tsingy limestone pinnacle and crevice 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: Tsingy limestone pinnacle and crevice fragment
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/tsingy-limestone-pinnacle-and-crevice-fragment-x3085
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring Madagascar's Tsingy limestone karst. Build a referenced limestone pinnacle fragment with sharp ridges and a deep crevice. Keep the geometry coherent and distinguish surface weathering from arbitrary spike decoration. Stylized solid forms.
Working Three.js Starter (tsingy-limestone-pinnacle-and-crevice-fragment-x3085):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#9baead">
<title>Tsingy — Limestone Pinnacle Fragment</title>
<style>
:root {
color-scheme: light;
--ink: #292b28;
--muted: #5f625c;
--paper: rgba(242, 235, 215, 0.78);
--line: rgba(50, 52, 47, 0.22);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #a8b9b8;
}
body {
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 52% 43%, transparent 42%,
rgba(38, 38, 31, 0.13) 100%);
mix-blend-mode: multiply;
}
.title-card {
position: fixed;
top: clamp(20px, 4vw, 48px);
left: clamp(20px, 4vw, 52px);
width: min(320px, calc(100vw - 40px));
padding: 17px 19px 18px;
pointer-events: none;
background: linear-gradient(
135deg,
rgba(244, 238, 220, 0.82),
rgba(229, 225, 207, 0.58)
);
border: 1px solid rgba(255, 255, 255, 0.28);
border-left: 2px solid rgba(54, 55, 49, 0.55);
box-shadow: 0 16px 42px rgba(52, 47, 35, 0.09);
backdrop-filter: blur(9px);
}
.eyebrow {
margin: 0 0 8px;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
color: var(--muted);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(27px, 4vw, 43px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.description {
margin: 11px 0 0;
max-width: 270px;
font-size: 12px;
line-height: 1.55;
color: var(--muted);
}
.controls {
position: fixed;
right: clamp(16px, 3vw, 36px);
bottom: clamp(16px, 3vw, 30px);
display: flex;
align-items: center;
gap: 12px;
padding: 8px 9px 8px 13px;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.26);
box-shadow: 0 10px 30px rgba(45, 43, 34, 0.1);
backdrop-filter: blur(10px);
font-size: 10px;
letter-spacing: 0.035em;
color: var(--muted);
}
.controls button {
appearance: none;
border: 1px solid var(--line);
border-radius: 0;
padding: 7px 10px;
background: rgba(255, 255, 255, 0.34);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
}
.controls button:hover,
.controls button:focus-visible {
background: rgba(255, 255, 255, 0.75);
outline: none;
}
.loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: #a8b9b8;
z-index: 20;
transition: opacity 500ms ease;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 640px) {
.title-card {
padding: 13px 15px 14px;
}
.description {
display: none;
}
.controls span {
display: none;
}
}
</style>
</head>
<body>
<div class="loading" id="loading">Forming limestone</div>
<div class="vignette"></div>
<header class="title-card">
<p class="eyebrow">Madagascar · Tsingy</p>
<h1>Limestone<br>Pinnacle</h1>
<p class="description">
A solid karst fragment shaped by vertical solution fluting,
stratified weathering, and a deep axial fracture.
</p>
</header>
<div class="controls" aria-label="Scene controls">
<span>Drag to orbit · Scroll to zoom · Right-drag to pan</span>
<button id="reset" type="button">Reset</button>
</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 loading = document.querySelector("#loading");
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive stylized scene of Madagascar's Tsingy limestone pinnacles"
);
renderer.domElement.tabIndex = 0;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xc8c6b7, 19, 61);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.08,
180
);
const clock = new THREE.Clock();
// ---------------------------------------------------------------------
// Small deterministic random generator
// ---------------------------------------------------------------------
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(83017);
// ---------------------------------------------------------------------
// Gradient sky dome
// ---------------------------------------------------------------------
const skyGeometry = new THREE.SphereGeometry(110, 40, 20);
const skyColors = [];
const skyPosition = skyGeometry.attributes.position;
const horizonColor = new THREE.Color(0xd9cfb5);
const middleColor = new THREE.Color(0xb5c7c7);
const zenithColor = new THREE.Color(0x769aa9);
const groundHaze = new THREE.Color(0xb9b39e);
for (let i = 0; i < skyPosition.count; i++) {
const y = skyPosition.getY(i) / 110;
let color;
if (y >= 0) {
const lowerMix = THREE.MathUtils.smoothstep(y, 0, 0.35);
const upperMix = THREE.MathUtils.smoothstep(y, 0.25, 0.95);
color = horizonColor.clone().lerp(middleColor, lowerMix);
color.lerp(zenithColor, upperMix);
} else {
color = horizonColor.clone().lerp(groundHaze, Math.min(1, -y * 3));
}
skyColors.push(color.r, color.g, color.b);
}
skyGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(skyColors, 3)
);
const sky = new THREE.Mesh(
skyGeometry,
new THREE.MeshBasicMaterial({
vertexColors: true,
side: THREE.BackSide,
fog: false,
depthWrite: false
})
);
sky.renderOrder = -10;
scene.add(sky);
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xd5e3e2,
0x6e6048,
1.65
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe1a5, 3.25);
sun.position.set(-8, 16, 11);
sun.target.position.set(0, 2.2, 0);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun, sun.target);
const fill = new THREE.DirectionalLight(0xb7d0d2, 0.55);
fill.position.set(9, 7, -10);
scene.add(fill);
// ---------------------------------------------------------------------
// Coherent karst geometry
//
// The pinnacle is one closed, solid mesh. Its shape comes from:
// 1. a broad tapered mass,
// 2. repeated vertical solution flutes,
// 3. restrained low-frequency surface weathering,
// 4. an actual inward fold forming a deep crevice.
//
// No separate cone or spike decorations are used.
// ---------------------------------------------------------------------
const limestone = new THREE.Color(0xc5b48e);
const paleLimestone = new THREE.Color(0xe0d0aa);
const weatheredLimestone = new THREE.Color(0x9d9073);
const fissureColor = new THREE.Color(0x514d43);
const earthColor = new THREE.Color(0x756c55);
const limestoneMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.94,
metalness: 0,
flatShading: true
});
function triangleWave(value) {
return Math.asin(Math.cos(value)) / (Math.PI * 0.5);
}
function angleDistance(a, b) {
let difference = Math.abs(a - b) % (Math.PI * 2);
if (difference > Math.PI) difference = Math.PI * 2 - difference;
return difference;
}
function createKarstGeometry(options) {
const {
height = 7,
radius = 2,
lobes = 7,
phase = 0,
notchDepth = 0,
notchWidth = 0.22,
notchAngle = 0,
leanX = 0,
leanZ = 0,
segments = 64
} = options;
const positions = [];
const colors = [];
const levels = [
0,
0.075,
0.17,
0.30,
0.44,
0.58,
0.71,
0.83,
0.92
];
function centerAt(t) {
return {
x: leanX * t * t + 0.055 * Math.sin(Math.PI * t),
z: leanZ * t * t - 0.035 * Math.sin(Math.PI * t)
};
}
function crestHeight(theta) {
return (
height +
0.12 * triangleWave(lobes * theta + phase) +
0.065 * Math.sin(3 * theta + phase * 0.7) +
0.035 * Math.cos(7 * theta - phase)
);
}
function sample(theta, t, top = false) {
const center = centerAt(t);
const fluting = triangleWave(lobes * theta + phase);
let radial =
radius *
(1.025 - 0.50 * t + 0.09 * Math.sin(Math.PI * t));
// Repeated ribs and valleys describe the primary karst fluting.
radial *= 1 + 0.135 * fluting;
// Restrained, coherent weathering rather than random spikes.
radial *=
1 +
0.025 * Math.sin(3 * theta + phase + 1.7 * t) +
0.017 * Math.sin(7 * theta - 1.2 * t);
let crevice = 0;
if (notchDepth > 0) {
const distance = angleDistance(theta, notchAngle);
if (distance < notchWidth) {
crevice = 1 - distance / notchWidth;
radial -=
notchDepth *
(0.42 + 0.58 * t) *
crevice;
}
}
radial = Math.max(0.12, radial);
const y = top ? crestHeight(theta) : height * t;
return {
position: new THREE.Vector3(
center.x + Math.sin(theta) * radial,
y,
center.z + Math.cos(theta) * radial
),
theta,
t,
crevice
};
}
function faceColor(a, b, c) {
const x = (a.position.x + b.position.x + c.position.x) / 3;
const y = (a.position.y + b.position.y + c.position.y) / 3;
const z = (a.position.z + b.position.z + c.position.z) / 3;
const theta = (a.theta + b.theta + c.theta) / 3;
const t = THREE.MathUtils.clamp(y / height, 0, 1);
const grain =
Math.sin(x * 1.73 + y * 0.91) *
Math.sin(z * 1.37 - y * 0.67);
const color = limestone.clone();
color.lerp(paleLimestone, 0.12 + 0.24 * t);
color.offsetHSL(0, -grain * 0.025, grain * 0.038);
// Fine limestone bedding remains subordinate to the solid form.
const bedding = Math.sin(y * 6.8 + grain * 0.55);
color.offsetHSL(0, 0, bedding * 0.015);
// Darker vertical solution valleys reinforce the modeled flutes.
const flute = triangleWave(lobes * theta + phase);
if (flute < -0.45) {
color.lerp(
weatheredLimestone,
(-flute - 0.45) * 0.18
);
}
// The interior fracture is visibly darker, not a black surface decal.
const crevice = Math.max(a.crevice, b.crevice, c.crevice);
if (crevice > 0) {
color.lerp(
fissureColor,
Math.min(0.84, 0.28 + crevice * 0.57)
);
}
if (y < height * 0.09) {
color.lerp(earthColor, 0.3);
}
return color;
}
function addTriangle(a, b, c) {
positions.push(
a.position.x, a.position.y, a.position.z,
b.position.x, b.position.y, b.position.z,
c.position.x, c.position.y, c.position.z
);
const color = faceColor(a, b, c);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
const rings = levels.map((t) => {
const ring = [];
for (let j = 0; j < segments; j++) {
const theta = -Math.PI + (j / segments) * Math.PI * 2;
ring.push(sample(theta, t));
}
return ring;
});
const topRing = [];
for (let j = 0; j < segments; j++) {
const theta = -Math.PI + (j / segments) * Math.PI * 2;
topRing.push(sample(theta, 1, true));
}
// Solid tapered sides with coherent vertical ribs.
for (let i = 0; i + 1 < rings.length; i++) {
for (let j = 0; j < segments; j++) {
const next = (j + 1) % segments;
const a = rings[i][j];
const b = rings[i][next];
const c = rings[i + 1][next];
const d = rings[i + 1][j];
if ((i + j) % 2 === 0) {
addTriangle(a, b, c);
addTriangle(a, c, d);
} else {
addTriangle(a, b, d);
addTriangle(b, c, d);
}
}
}
for (let j = 0; j < segments; j++) {
const next = (j + 1) % segments;
const a = rings[rings.length - 1][j];
const b = rings[rings.length - 1][next];
const c = topRing[next];
const d = topRing[j];
if ((j + segments) % 2 === 0) {
addTriangle(a, b, c);
addTriangle(a, c, d);
} else {
addTriangle(a, b, d);
addTriangle(b, c, d);
}
}
// The summit is a low, faceted crown rather than an attached spike.
const middleRing = topRing.map((vertex) => {
const center = centerAt(1);
const radial = Math.hypot(
vertex.position.x - center.x,
vertex.position.z - center.z
) * 0.43;
const theta = vertex.theta;
const y =
height +
0.12 +
0.095 * triangleWave(lobes * theta + phase) +
0.04 * Math.sin(3 * theta + phase);
return {
position: new THREE.Vector3(
center.x + Math.sin(theta) * radial,
y,
center.z + Math.cos(theta) * radial
),
theta,
t: 1,
crevice: vertex.crevice
};
});
for (let j = 0; j < segments; j++) {
const next = (j + 1) % segments;
addTriangle(topRing[j], topRing[next], middleRing[next]);
addTriangle(topRing[j], middleRing[next], middleRing[j]);
}
const summitCenter = centerAt(1);
const summit = {
position: new THREE.Vector3(
summitCenter.x,
height + 0.19,
summitCenter.z
),
theta: 0,
t: 1,
crevice: 0
};
for (let j = 0; j < segments; j++) {
const next = (j + 1) % segments;
addTriangle(middleRing[j], middleRing[next], summit);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
function addKarst(options) {
const mesh = new THREE.Mesh(
createKarstGeometry(options),
limestoneMaterial
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Hero fragment. The deep crevice points toward the initial camera.
const hero = addKarst({
height: 7.35,
radius: 2.18,
lobes: 7,
phase: 0.42,
notchDepth: 1.02,
notchWidth: 0.235,
notchAngle: 0.62,
leanX: -0.12,
leanZ: 0.08,
segments: 64
});
hero.position.set(0, -0.08, 0);
// Supporting fragments, all generated from the same geological logic.
const formations = [
{
position: [-5.5, -0.08, -4.5],
options: {
height: 5.2, radius: 1.48, lobes: 6, phase: 1.1,
notchDepth: 0.55, notchWidth: 0.2, notchAngle: 0.4,
leanX: 0.18, leanZ: -0.12, segments: 48
}
},
{
position: [5.4, -0.08, -6.2],
options: {
height: 6.1, radius: 1.55, lobes: 8, phase: 2.2,
notchDepth: 0.64, notchWidth: 0.18, notchAngle: 0.9,
leanX: -0.16, leanZ: 0.06, segments: 48
}
},
{
position: [-7.0, -0.08, 2.4],
options: {
height: 3.8, radius: 1.23, lobes: 6, phase: 0.1,
notchDepth: 0.35, notchWidth: 0.2, notchAngle: 0.2,
leanX: 0.12, leanZ: 0.15, segments: 48
}
},
{
position: [7.1, -0.08, 2.1],
options: {
height: 4.3, radius: 1.18, lobes: 7, phase: 1.8,
notchDepth: 0, notchWidth: 0.2, notchAngle: 0,
leanX: -0.14, leanZ: -0.08, segments: 48
}
},
{
position: [-1.0, -0.08, -12.0],
options: {
height: 5.7, radius: 1.52, lobes: 7, phase: 3.0,
notchDepth: 0, notchWidth: 0.2, notchAngle: 0,
leanX: 0.08, leanZ: 0, segments: 48
}
},
{
position: [9.8, -0.08, -11.5],
options: {
height: 3.6, radius: 1.3, lobes: 6, phase: 0.7,
notchDepth: 0, notchWidth: 0.2, notchAngle: 0,
leanX: 0, leanZ: 0, segments: 48
}
}
];
for (const formation of formations) {
const mesh = addKarst(formation.options);
mesh.position.fromArray(formation.position);
}
// ---------------------------------------------------------------------
// Low, dry limestone ground
// ---------------------------------------------------------------------
function terrainHeight(x, z) {
const broad =
0.055 * Math.sin(x * 0.21) * Math.cos(z * 0.17) +
0.025 * Math.sin(x * 0.73 + z * 0.41);
const heroMound =
0.12 * Math.exp(-(x * x + z * z) / 34);
return -0.22 + broad + heroMound;
}
function createGround() {
const size = 76;
const divisions = 38;
const step = size / divisions;
const positions = [];
const colors = [];
const indices = [];
const dark = new THREE.Color(0x8c836b);
const light = new THREE.Color(0xb7aa88);
for (let zIndex = 0; zIndex <= divisions; zIndex++) {
const z = -size * 0.5 + zIndex * step;
for (let xIndex = 0; xIndex <= divisions; xIndex++) {
const x = -size * 0.5 + xIndex * step;
const y = terrainHeight(x, z);
positions.push(x, y, z);
const variation =
0.5 +
0.5 * Math.sin(x * 0.37 + z * 0.19) *
Math.cos(z * 0.28 - x * 0.11);
const color = dark.clone().lerp(light, variation * 0.62);
color.offsetHSL(
0,
0,
0.025 * Math.sin(x * 1.7 + z * 1.3)
);
colors.push(color.r, color.g, color.b);
}
}
for (let zIndex = 0; zIndex < divisions; zIndex++) {
for (let xIndex = 0; xIndex < divisions; xIndex++) {
const a = zIndex * (divisions + 1) + xIndex;
const b = a + 1;
const c = a + divisions + 1;
const d = c + 1;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const ground = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true
})
);
ground.receiveShadow = true;
scene.add(ground);
}
createGround();
// ---------------------------------------------------------------------
// Limestone rubble: low, blocky fragments rather than ornamental spikes
// ---------------------------------------------------------------------
const rubbleGeometry = new THREE.DodecahedronGeometry(1, 0);
const rubbleMaterials = [
new THREE.MeshStandardMaterial({
color: 0x9f9478, roughness: 1, flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb4a685, roughness: 1, flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x877d67, roughness: 1, flatShading: true
})
];
const reservedAreas = [
{ x: 0, z: 0, r: 2.2 },
{ x: -5.5, z: -4.5, r: 1.5 },
{ x: 5.4, z: -6.2, r: 1.55 },
{ x: -7, z: 2.4, r: 1.25 },
{ x: 7.1, z: 2.1, r: 1.2 }
];
for (let i = 0; i < 58; i++) {
const angle = random() * Math.PI * 2;
const distance = 2.4 + Math.pow(random(), 0.72) * 17;
const x = Math.cos(angle) * distance;
const z = Math.sin(angle) * distance;
const overlapsFormation = reservedAreas.some((area) => {
const dx = x - area.x;
const dz = z - area.z;
return Math.hypot(dx, dz) < area.r * 0.8;
});
if (overlapsFormation && random() > 0.3) continue;
const size = 0.13 + Math.pow(random(), 2) * 0.55;
const rubble = new THREE.Mesh(
rubbleGeometry,
rubbleMaterials[Math.floor(random() * rubbleMaterials.length)]
);
rubble.position.set(
x,
terrainHeight(x, z) + size * 0.28,
z
);
rubble.scale.set(
size * (0.8 + random() * 0.8),
size * (0.35 + random() * 0.4),
size * (0.75 + random() * 0.75)
);
rubble.rotation.set(
random() * 0.45,
random() * Math.PI * 2,
random() * 0.35
);
rubble.castShadow = true;
rubble.receiveShadow = true;
scene.add(rubble);
}
// ---------------------------------------------------------------------
// Direct camera controls — no examples or addons
// ---------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 2.9, 0),
azimuth: 0.62,
elevation: 0.28,
distance: 14.8
};
const defaultControls = {
target: new THREE.Vector3(0, 2.9, 0),
azimuth: 0.62,
elevation: 0.28,
distance: 14.8
};
function updateCamera() {
const horizontalDistance =
controls.distance * Math.cos(controls.elevation);
camera.position.set(
controls.target.x +
Math.sin(controls.azimuth) * horizontalDistance,
controls.target.y +
Math.sin(controls.elevation) * controls.distance,
controls.target.z +
Math.cos(controls.azimuth) * horizontalDistance
);
camera.lookAt(controls.target);
}
function resetCamera() {
controls.target.copy(defaultControls.target);
controls.azimuth = defaultControls.azimuth;
controls.elevation = defaultControls.elevation;
controls.distance = defaultControls.distance;
updateCamera();
}
function panCamera(deltaX, deltaY) {
const scale =
2 *
controls.distance *
Math.tan(THREE.MathUtils.degToRad(camera.fov * 0.5)) /
window.innerHeight;
const right = new THREE.Vector3(
Math.cos(controls.azimuth),
0,
-Math.sin(controls.azimuth)
);
const forward = new THREE.Vector3(
-Math.sin(controls.azimuth),
0,
-Math.cos(controls.azimuth)
);
controls.target.addScaledVector(right, -deltaX * scale);
controls.target.addScaledVector(forward, deltaY * scale);
controls.target.x = THREE.MathUtils.clamp(
controls.target.x, -9, 9
);
controls.target.z = THREE.MathUtils.clamp(
controls.target.z, -9, 9
);
controls.target.y = THREE.MathUtils.clamp(
controls.target.y, 0.6, 6.5
);
updateCamera();
}
const pointers = new Map();
let lastPointer = null;
let pinchDistance = 0;
let gestureButton = 0;
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
canvas.focus();
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
gestureButton = event.button;
if (pointers.size === 1) {
lastPointer = {
x: event.clientX,
y: event.clientY,
button: event.button
};
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && lastPointer) {
const deltaX = event.clientX - lastPointer.x;
const deltaY = event.clientY - lastPointer.y;
if (gestureButton === 2 || gestureButton === 1) {
panCamera(deltaX, deltaY);
} else {
controls.azimuth -= deltaX * 0.0052;
controls.elevation = THREE.MathUtils.clamp(
controls.elevation + deltaY * 0.0042,
0.08,
1.18
);
updateCamera();
}
lastPointer = {
x: event.clientX,
y: event.clientY,
button: gestureButton
};
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const newDistance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0) {
controls.distance = THREE.MathUtils.clamp(
controls.distance * (pinchDistance / newDistance),
7,
29
);
updateCamera();
}
pinchDistance = newDistance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
lastPointer = {
x: remaining.x,
y: remaining.y,
button: 0
};
} else {
lastPointer = null;
pinchDistance = 0;
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.distance = THREE.MathUtils.clamp(
controls.distance * Math.exp(event.deltaY * 0.001),
7,
29
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#reset").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "r") {
resetCamera();
} else if (key === "arrowleft") {
controls.azimuth += 0.09;
updateCamera();
} else if (key === "arrowright") {
controls.azimuth -= 0.09;
updateCamera();
} else if (key === "arrowup") {
controls.elevation = THREE.MathUtils.clamp(
controls.elevation + 0.06, 0.08, 1.18
);
updateCamera();
} else if (key === "arrowdown") {
controls.elevation = THREE.MathUtils.clamp(
controls.elevation - 0.06, 0.08, 1.18
);
updateCamera();
} else if (key === "+" || key === "=") {
controls.distance = Math.max(7, controls.distance - 0.7);
updateCamera();
} else if (key === "-" || key === "_") {
controls.distance = Math.min(29, controls.distance + 0.7);
updateCamera();
}
});
// ---------------------------------------------------------------------
// Resize and render
// ---------------------------------------------------------------------
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);
updateCamera();
resize();
renderer.render(scene, camera);
loading.classList.add("hidden");
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```