Prompt specification
Build or customize the Three.js 3D scene "Limestone cave passage cutaway" 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: Limestone cave passage cutaway
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/limestone-cave-passage-cutaway-x1153
Scene Description Prompt:
Limestone cave passage cutaway. Make a limestone block with an accessible side cut exposing a connected cave passage, stalactites, and floor deposits; distinguish ceiling growth from upward floor formations. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (limestone-cave-passage-cutaway-x1153):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Limestone Cave Passage — Low-Poly Cutaway</title>
<style>
:root {
--ink: #edf2ec;
--muted: #aeb9b3;
--panel: rgba(17, 24, 27, 0.86);
--line: rgba(225, 235, 225, 0.16);
--stalactite: #e1ad62;
--stalagmite: #73c9c0;
--deposit: #d4b37d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 42%, #26343a 0%, #151d22 43%, #0b1115 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 22px;
left: 22px;
z-index: 10;
width: min(330px, calc(100vw - 44px));
padding: 18px 18px 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: auto;
}
.eyebrow {
margin-bottom: 8px;
color: #9aa8a3;
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(25px, 3vw, 34px);
font-weight: 500;
line-height: 1.03;
letter-spacing: -0.025em;
}
.intro {
margin: 10px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.legend {
display: grid;
gap: 9px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.legend-item {
display: grid;
grid-template-columns: 29px 1fr;
gap: 9px;
align-items: center;
}
.key {
position: relative;
width: 27px;
height: 27px;
border-radius: 7px;
background: rgba(255, 255, 255, 0.045);
color: var(--ink);
font-size: 17px;
font-weight: 800;
text-align: center;
line-height: 27px;
}
.key.down {
color: var(--stalactite);
}
.key.up {
color: var(--stalagmite);
}
.key.layers {
color: var(--deposit);
font-size: 14px;
}
.legend-title {
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.legend-detail {
margin-top: 2px;
color: #9eaaa5;
font-size: 10.5px;
line-height: 1.35;
}
.note {
margin: 13px 0 0;
padding-top: 12px;
border-top: 1px solid var(--line);
color: #8f9c97;
font-size: 10.5px;
line-height: 1.45;
}
.controls {
position: fixed;
right: 20px;
top: 20px;
z-index: 12;
display: flex;
gap: 8px;
}
button {
border: 1px solid var(--line);
border-radius: 9px;
padding: 9px 12px;
color: var(--ink);
background: rgba(17, 24, 27, 0.82);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
backdrop-filter: blur(8px);
}
button:hover {
border-color: rgba(225, 235, 225, 0.38);
background: rgba(33, 44, 48, 0.94);
}
button.off {
color: #78837f;
}
.hint {
position: fixed;
right: 21px;
bottom: 18px;
z-index: 8;
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 8px;
color: #95a09b;
background: rgba(12, 17, 20, 0.58);
font-size: 10px;
letter-spacing: 0.025em;
pointer-events: none;
}
.world-label {
position: fixed;
z-index: 7;
max-width: 220px;
padding: 6px 9px;
border: 1px solid currentColor;
border-radius: 7px;
color: var(--stalactite);
background: rgba(12, 17, 20, 0.83);
box-shadow: 0 8px 22px rgba(0, 0, 0, 0.24);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.075em;
line-height: 1.3;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -115%);
transition: opacity 120ms ease;
}
.world-label::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 16px;
background: currentColor;
opacity: 0.75;
}
#label-stalagmite {
color: var(--stalagmite);
}
#label-deposit {
color: var(--deposit);
}
.hidden {
opacity: 0 !important;
}
.section-mark {
position: fixed;
left: 22px;
bottom: 18px;
z-index: 8;
display: flex;
align-items: center;
gap: 10px;
color: #7f8d87;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
pointer-events: none;
}
.section-line {
width: 55px;
height: 1px;
background: #61716a;
}
@media (max-width: 680px) {
.panel {
top: 12px;
left: 12px;
width: min(285px, calc(100vw - 24px));
padding: 14px;
}
.intro,
.note {
display: none;
}
.legend {
gap: 6px;
padding-top: 10px;
}
.controls {
top: auto;
right: 12px;
bottom: 47px;
}
.hint {
right: 12px;
bottom: 10px;
}
.section-mark {
display: none;
}
.world-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<aside class="panel">
<div class="eyebrow">Field Note 04 · Limestone System</div>
<h1>Cave passage<br>cutaway</h1>
<p class="intro">
A single connected cavity runs through the limestone block. The open
side section exposes its roof, floor, and both continuation points.
</p>
<div class="legend">
<div class="legend-item">
<div class="key down">↓</div>
<div>
<div class="legend-title">Ceiling growth · Stalactites</div>
<div class="legend-detail">Hang downward from the cave roof.</div>
</div>
</div>
<div class="legend-item">
<div class="key up">↑</div>
<div>
<div class="legend-title">Floor growth · Stalagmites</div>
<div class="legend-detail">Rise upward from the cave floor.</div>
</div>
</div>
<div class="legend-item">
<div class="key layers">▰</div>
<div>
<div class="legend-title">Floor deposits</div>
<div class="legend-detail">Layered clay, silt, and pale calcite.</div>
</div>
</div>
</div>
<p class="note">
The warm formations descend from above; the cool formations grow from
below. Direction and color distinguish the two growth types.
</p>
</aside>
<div class="controls">
<button id="reset-view" type="button">Reset view</button>
<button id="toggle-labels" type="button">Labels on</button>
</div>
<div id="label-stalactite" class="world-label">↓ Ceiling growth · Stalactite</div>
<div id="label-stalagmite" class="world-label">↑ Floor growth · Stalagmite</div>
<div id="label-deposit" class="world-label">▰ Layered floor deposits</div>
<div class="section-mark">
<span class="section-line"></span>
Open side section · Schematic, not to scale
</div>
<div class="hint">Drag to orbit · Scroll / pinch to zoom · R resets</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, and camera
// ------------------------------------------------------------
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x10171b, 0.014);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 3.7, -0.55);
const defaultView = {
theta: 0.62,
phi: 1.16,
radius: 23.5
};
const view = {
theta: defaultView.theta,
phi: defaultView.phi,
radius: defaultView.radius,
targetTheta: defaultView.theta,
targetPhi: defaultView.phi,
targetRadius: defaultView.radius
};
function updateCamera() {
const sinPhi = Math.sin(view.phi);
camera.position.set(
cameraTarget.x + view.radius * sinPhi * Math.sin(view.theta),
cameraTarget.y + view.radius * Math.cos(view.phi),
cameraTarget.z + view.radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// ------------------------------------------------------------
// Deterministic helpers
// ------------------------------------------------------------
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(481516);
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smoothstep(edge0, edge1, value) {
const t = clamp((value - edge0) / (edge1 - edge0), 0, 1);
return t * t * (3 - 2 * t);
}
// ------------------------------------------------------------
// Continuous cave centerline
// ------------------------------------------------------------
const path = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-10.0, 3.40, -0.45),
new THREE.Vector3(-7.6, 4.05, -0.85),
new THREE.Vector3(-4.8, 3.45, -0.35),
new THREE.Vector3(-1.8, 4.15, -0.85),
new THREE.Vector3( 1.4, 3.35, -0.45),
new THREE.Vector3( 4.6, 4.05, -0.95),
new THREE.Vector3( 7.5, 3.55, -0.55),
new THREE.Vector3(10.0, 3.90, -0.75)
],
false,
"catmullrom",
0.35
);
path.arcLengthDivisions = 400;
const pathSamples = path.getSpacedPoints(150).map((point, index, array) => {
const t = index / (array.length - 1);
const radius =
2.34 +
0.21 * Math.sin(t * Math.PI * 6.0 + 0.8) +
0.11 * Math.sin(t * Math.PI * 14.0);
return {
x: point.x,
y: point.y,
z: point.z,
radius
};
});
function caveDistanceSquared(x, y, z) {
let best = Infinity;
let bestRadius = 2.3;
for (const sample of pathSamples) {
const dx = x - sample.x;
const dy = y - sample.y;
const dz = z - sample.z;
const distance = dx * dx + dy * dy + dz * dz;
if (distance < best) {
best = distance;
bestRadius = sample.radius;
}
}
return {
distanceSquared: best,
radius: bestRadius
};
}
function isCavePoint(x, y, z) {
const result = caveDistanceSquared(x, y, z);
const roughness =
0.09 * Math.sin(x * 2.4 + y * 1.7 + z * 2.1) +
0.05 * Math.cos(x * 5.1 - z * 3.3);
const radius = result.radius + roughness;
return result.distanceSquared < radius * radius;
}
function pathInfoAtX(x) {
for (let i = 0; i < pathSamples.length - 1; i++) {
const a = pathSamples[i];
const b = pathSamples[i + 1];
if (x >= a.x && x <= b.x) {
const span = b.x - a.x;
const t = span === 0 ? 0 : (x - a.x) / span;
return {
x,
y: THREE.MathUtils.lerp(a.y, b.y, t),
z: THREE.MathUtils.lerp(a.z, b.z, t),
radius: THREE.MathUtils.lerp(a.radius, b.radius, t)
};
}
}
return x < pathSamples[0].x
? { ...pathSamples[0], x }
: { ...pathSamples[pathSamples.length - 1], x };
}
function floorAt(x, z) {
const info = pathInfoAtX(x);
const dz = z - info.z;
const radial = Math.max(
0.08,
info.radius * info.radius - dz * dz
);
return info.y - Math.sqrt(radial);
}
function roofAt(x, z) {
const info = pathInfoAtX(x);
const dz = z - info.z;
const radial = Math.max(
0.08,
info.radius * info.radius - dz * dz
);
return info.y + Math.sqrt(radial);
}
// ------------------------------------------------------------
// Voxel-like low-poly limestone block with a carved passage
// ------------------------------------------------------------
const CELL = 0.6;
const NX = 33;
const NY = 15;
const NZ = 11;
const MIN_X = -9.6;
const MIN_Y = 0.0;
const MIN_Z = -6.0;
const caveMask = new Uint8Array(NX * NY * NZ);
const occupancy = new Uint8Array(NX * NY * NZ);
function gridIndex(x, y, z) {
return x + NX * (y + NY * z);
}
for (let z = 0; z < NZ; z++) {
for (let y = 0; y < NY; y++) {
for (let x = 0; x < NX; x++) {
const index = gridIndex(x, y, z);
const worldX = MIN_X + CELL * (x + 0.5);
const worldY = MIN_Y + CELL * (y + 0.5);
const worldZ = MIN_Z + CELL * (z + 0.5);
const cave = isCavePoint(worldX, worldY, worldZ);
caveMask[index] = cave ? 1 : 0;
occupancy[index] = cave ? 0 : 1;
}
}
}
const exteriorPalette = [
new THREE.Color(0xc7b58f),
new THREE.Color(0xd5c39d),
new THREE.Color(0xdfcda8),
new THREE.Color(0xcdb994)
];
const cutPalette = [
new THREE.Color(0xe1cda4),
new THREE.Color(0xe9d8b3),
new THREE.Color(0xd8c198),
new THREE.Color(0xead9b4)
];
const cavePalette = [
new THREE.Color(0x746a5b),
new THREE.Color(0x665e53),
new THREE.Color(0x817565),
new THREE.Color(0x71685d)
];
function faceColor(type, y) {
const palette =
type === "cave" ? cavePalette :
type === "cut" ? cutPalette :
exteriorPalette;
const color = palette[Math.abs(Math.floor(y / 0.72)) % palette.length].clone();
color.multiplyScalar(0.92 + random() * 0.14);
return color;
}
const positions = [];
const colors = [];
function pushTriangle(a, b, c, color) {
positions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
function pushQuad(a, b, c, d, color) {
pushTriangle(a, b, c, color);
pushTriangle(a, c, d, color);
}
function addFace(x, y, z, direction) {
const x0 = MIN_X + CELL * x;
const x1 = x0 + CELL;
const y0 = MIN_Y + CELL * y;
const y1 = y0 + CELL;
const z0 = MIN_Z + CELL * z;
const z1 = z0 + CELL;
let type = "exterior";
if (direction === 1 && z === NZ - 1) {
type = "cut";
} else {
const neighborIndex = gridIndex(
x + direction[0],
y + direction[1],
z + direction[2]
);
if (caveMask[neighborIndex]) {
type = "cave";
}
}
const color = faceColor(type, y0);
if (direction[2] === 1) {
pushQuad(
[x0, y0, z1], [x1, y0, z1],
[x1, y1, z1], [x0, y1, z1], color
);
} else if (direction[2] === -1) {
pushQuad(
[x1, y0, z0], [x0, y0, z0],
[x0, y1, z0], [x1, y1, z0], color
);
} else if (direction[0] === 1) {
pushQuad(
[x1, y0, z1], [x1, y0, z0],
[x1, y1, z0], [x1, y1, z1], color
);
} else if (direction[0] === -1) {
pushQuad(
[x0, y0, z0], [x0, y0, z1],
[x0, y1, z1], [x0, y1, z0], color
);
} else if (direction[1] === 1) {
pushQuad(
[x0, y1, z1], [x1, y1, z1],
[x1, y1, z0], [x0, y1, z0], color
);
} else if (direction[1] === -1) {
pushQuad(
[x0, y0, z0], [x1, y0, z0],
[x1, y0, z1], [x0, y0, z1], color
);
}
}
const directions = [
[ 1, 0, 0],
[-1, 0, 0],
[ 0, 1, 0],
[ 0, -1, 0],
[ 0, 0, 1],
[ 0, 0, -1]
];
for (let z = 0; z < NZ; z++) {
for (let y = 0; y < NY; y++) {
for (let x = 0; x < NX; x++) {
const index = gridIndex(x, y, z);
if (!occupancy[index]) continue;
for (const direction of directions) {
const nx = x + direction[0];
const ny = y + direction[1];
const nz = z + direction[2];
const inside =
nx >= 0 && nx < NX &&
ny >= 0 && ny < NY &&
nz >= 0 && nz < NZ;
if (inside && occupancy[gridIndex(nx, ny, nz)]) {
continue;
}
addFace(x, y, z, direction);
}
}
}
}
const blockGeometry = new THREE.BufferGeometry();
blockGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
blockGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
blockGeometry.computeVertexNormals();
const limestoneMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.94,
metalness: 0.0,
flatShading: true
});
const block = new THREE.Mesh(blockGeometry, limestoneMaterial);
block.castShadow = true;
block.receiveShadow = true;
scene.add(block);
// ------------------------------------------------------------
// Horizontal limestone strata on the exposed section face
// ------------------------------------------------------------
const strataVertices = [];
for (let row = 0; row < 11; row++) {
const y = 0.65 + row * 0.71;
let runStart = null;
for (let step = 0; step <= 160; step++) {
const x = -9.45 + step * (18.9 / 160);
const valid = !isCavePoint(x, y, 0.607);
if (valid && runStart === null) {
runStart = x;
}
if ((!valid || step === 160) && runStart !== null) {
const end = valid ? x : x - 18.9 / 160;
if (end - runStart > 0.28) {
strataVertices.push(
runStart, y, 0.609,
end, y, 0.609
);
}
runStart = null;
}
}
}
const strataGeometry = new THREE.BufferGeometry();
strataGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(strataVertices, 3)
);
const strataLines = new THREE.LineSegments(
strataGeometry,
new THREE.LineBasicMaterial({
color: 0x786a55,
transparent: true,
opacity: 0.32,
depthWrite: false
})
);
scene.add(strataLines);
// ------------------------------------------------------------
// Exterior low-poly rock fragments
// ------------------------------------------------------------
const rockGeometry = new THREE.DodecahedronGeometry(1, 0);
const rockMaterials = [
new THREE.MeshStandardMaterial({
color: 0xc9b68e,
roughness: 0.97,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb7a47f,
roughness: 0.98,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xd8c69e,
roughness: 0.95,
flatShading: true
})
];
function addRock(position, scale, materialIndex = 0) {
const rock = new THREE.Mesh(
rockGeometry,
rockMaterials[materialIndex % rockMaterials.length]
);
rock.position.copy(position);
rock.scale.set(...scale);
rock.rotation.set(
random() * Math.PI,
random() * Math.PI,
random() * Math.PI
);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
for (let i = 0; i < 11; i++) {
const x = -8.8 + i * 1.72;
addRock(
new THREE.Vector3(
x + random() * 0.5,
8.78 + random() * 0.16,
-2.6 - random() * 2.5
),
[
0.55 + random() * 0.6,
0.28 + random() * 0.38,
0.48 + random() * 0.55
],
i % 3
);
}
for (let i = 0; i < 8; i++) {
addRock(
new THREE.Vector3(
-8.5 + i * 2.35 + random() * 0.5,
0.4 + random() * 1.1,
-5.92 - random() * 0.15
),
[
0.7 + random() * 0.7,
0.45 + random() * 0.75,
0.28 + random() * 0.34
],
(i + 1) % 3
);
}
// Small patches on the rear cave wall provide depth cues.
const wallPatchMaterial = new THREE.MeshStandardMaterial({
color: 0xa89b83,
roughness: 0.88,
flatShading: true
});
for (let i = 0; i < 17; i++) {
const x = -8.8 + i * 1.08;
const info = pathInfoAtX(x);
const patch = new THREE.Mesh(rockGeometry, wallPatchMaterial);
patch.position.set(
x,
info.y + (random() - 0.5) * 0.75,
info.z - info.radius + 0.06
);
patch.scale.set(
0.35 + random() * 0.55,
0.16 + random() * 0.28,
0.055 + random() * 0.035
);
patch.rotation.set(
random() * 0.4,
random() * 0.3,
random() * 0.4
);
patch.receiveShadow = true;
scene.add(patch);
}
// ------------------------------------------------------------
// Low-poly formation geometry
// ------------------------------------------------------------
function makeStalactiteGeometry(length, radius, seed) {
const localRandom = mulberry32(seed);
const segments = 7;
const rings = [
{ y: 0.00, r: radius * 0.58 },
{ y: -0.13 * length, r: radius * 0.96 },
{ y: -0.48 * length, r: radius * 0.76 },
{ y: -0.80 * length, r: radius * 0.39 },
{ y: -length, r: radius * 0.035 }
];
const positions = [];
const indices = [];
for (let ringIndex = 0; ringIndex < rings.length; ringIndex++) {
const ring = rings[ringIndex];
const bend = (localRandom() - 0.5) * length * 0.09;
for (let segment = 0; segment < segments; segment++) {
const angle =
segment / segments * Math.PI * 2 +
ringIndex * 0.19 +
(localRandom() - 0.5) * 0.12;
const radialJitter = 0.84 + localRandom() * 0.28;
positions.push(
Math.cos(angle) * ring.r * radialJitter + bend * 0.22,
ring.y + (localRandom() - 0.5) * length * 0.035,
Math.sin(angle) * ring.r * radialJitter + bend * 0.22
);
}
}
for (let ring = 0; ring < rings.length - 1; ring++) {
for (let segment = 0; segment < segments; segment++) {
const next = (segment + 1) % segments;
const a = ring * segments + segment;
const b = ring * segments + next;
const c = (ring + 1) * segments + next;
const d = (ring + 1) * segments + segment;
indices.push(a, b, d, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function makeStalagmiteGeometry(length, radius, seed) {
const localRandom = mulberry32(seed);
const segments = 7;
const rings = [
{ y: 0.00, r: radius * 0.78 },
{ y: 0.19 * length, r: radius * 1.04 },
{ y: 0.54 * length, r: radius * 0.67 },
{ y: 0.83 * length, r: radius * 0.34 },
{ y: length, r: radius * 0.035 }
];
const positions = [];
const indices = [];
for (let ringIndex = 0; ringIndex < rings.length; ringIndex++) {
const ring = rings[ringIndex];
const bend = (localRandom() - 0.5) * length * 0.09;
for (let segment = 0; segment < segments; segment++) {
const angle =
segment / segments * Math.PI * 2 +
ringIndex * 0.17 +
(localRandom() - 0.5) * 0.12;
const radialJitter = 0.84 + localRandom() * 0.27;
positions.push(
Math.cos(angle) * ring.r * radialJitter + bend * 0.2,
ring.y + (localRandom() - 0.5) * length * 0.03,
Math.sin(angle) * ring.r * radialJitter + bend * 0.2
);
}
}
for (let ring = 0; ring < rings.length - 1; ring++) {
for (let segment = 0; segment < segments; segment++) {
const next = (segment + 1) % segments;
const a = ring * segments + segment;
const b = ring * segments + next;
const c = (ring + 1) * segments + next;
const d = (ring + 1) * segments + segment;
indices.push(a, d, b, b, d, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const stalactiteMaterial = new THREE.MeshStandardMaterial({
color: 0xe0ad61,
roughness: 0.68,
metalness: 0.0,
emissive: 0x2d1705,
emissiveIntensity: 0.18,
flatShading: true
});
const stalagmiteMaterial = new THREE.MeshStandardMaterial({
color: 0x70c8bf,
roughness: 0.72,
metalness: 0.0,
emissive: 0x062925,
emissiveIntensity: 0.18,
flatShading: true
});
const stalactitePositions = [
-8.6, -7.1, -5.7, -3.9, -2.0,
0.5, 2.2, 4.5, 6.4, 8.2
];
const stalagmitePositions = [
-8.0, -6.4, -4.8, -3.0, -1.1,
0.8, 2.9, 4.7, 6.5, 8.4
];
let stalactiteAnchor = new THREE.Vector3();
let stalagmiteAnchor = new THREE.Vector3();
stalactitePositions.forEach((x, index) => {
const info = pathInfoAtX(x);
const z = info.z + 0.78 + random() * 0.38;
const roof = roofAt(x, z);
const length = 0.56 + random() * 0.85;
const radius = 0.17 + random() * 0.18;
const mesh = new THREE.Mesh(
makeStalactiteGeometry(length, radius, 1100 + index * 71),
stalactiteMaterial
);
mesh.position.set(x, roof - 0.035, z);
mesh.rotation.y = random() * Math.PI;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
if (index === 2) {
stalactiteAnchor.set(x, roof - length * 0.44, z);
}
});
stalagmitePositions.forEach((x, index) => {
const info = pathInfoAtX(x);
const z = info.z + 0.78 + random() * 0.42;
const floor = floorAt(x, z);
const length = 0.48 + random() * 0.84;
const radius = 0.18 + random() * 0.17;
const mesh = new THREE.Mesh(
makeStalagmiteGeometry(length, radius, 3200 + index * 83),
stalagmiteMaterial
);
mesh.position.set(x, floor + 0.19, z);
mesh.rotation.y = random() * Math.PI;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
if (index === 5) {
stalagmiteAnchor.set(x, floor + length * 0.52 + 0.12, z);
}
});
// ------------------------------------------------------------
// Layered floor deposits
// ------------------------------------------------------------
function makeDepositRibbon(width, lift, bulge, colorValue, seed) {
const localRandom = mulberry32(seed);
const segments = 72;
const columns = 5;
const positions = [];
const colors = [];
const indices = [];
const baseColor = new THREE.Color(colorValue);
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const x = -9.25 + t * 18.5;
const info = pathInfoAtX(x);
const endTaper =
smoothstep(0, 0.07, t) * smoothstep(0, 0.07, 1 - t);
for (let j = 0; j < columns; j++) {
const across = j / (columns - 1) - 0.5;
const z =
info.z + 1.02 +
across * width * endTaper +
Math.sin(i * 0.83 + seed) * 0.055;
const profile = 1 - Math.pow(Math.abs(across) * 2, 1.7);
const floor = floorAt(x, z);
const jitter = (localRandom() - 0.5) * 0.025;
positions.push(
x,
floor + lift + bulge * profile + jitter,
z
);
const shade = 0.88 + localRandom() * 0.18;
colors.push(
baseColor.r * shade,
baseColor.g * shade,
baseColor.b * shade
);
}
}
for (let i = 0; i < segments; i++) {
for (let j = 0; j < columns - 1; j++) {
const a = i * columns + j;
const b = (i + 1) * columns + j;
const c = b + 1;
const d = a + 1;
indices.push(a, d, b, b, d, c);
}
}
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();
return geometry;
}
const depositMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.96,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
});
const depositLayers = [
{
geometry: makeDepositRibbon(2.12, 0.035, 0.05, 0x9b7256, 71),
label: "Clay"
},
{
geometry: makeDepositRibbon(1.38, 0.12, 0.05, 0xc7a56f, 72),
label: "Silt"
},
{
geometry: makeDepositRibbon(0.72, 0.205, 0.045, 0xe1d4b6, 73),
label: "Calcite"
}
];
depositLayers.forEach((layer) => {
const mesh = new THREE.Mesh(layer.geometry, depositMaterial);
mesh.receiveShadow = true;
scene.add(mesh);
});
// Rounded low-poly pebbles embedded in the sediment.
const pebbleGeometry = new THREE.DodecahedronGeometry(0.11, 0);
const pebbleMaterials = [
new THREE.MeshStandardMaterial({
color: 0x8c725d,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb49a72,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xd7c6a5,
roughness: 0.95,
flatShading: true
})
];
for (let i = 0; i < 48; i++) {
const x = -9.0 + random() * 18.0;
const info = pathInfoAtX(x);
const z = info.z + 0.35 + random() * 1.35;
const y = floorAt(x, z) + 0.20 + random() * 0.05;
const pebble = new THREE.Mesh(
pebbleGeometry,
pebbleMaterials[Math.floor(random() * pebbleMaterials.length)]
);
pebble.position.set(x, y, z);
pebble.scale.set(
0.55 + random() * 1.2,
0.35 + random() * 0.6,
0.55 + random() * 1.1
);
pebble.rotation.set(
random() * Math.PI,
random() * Math.PI,
random() * Math.PI
);
pebble.castShadow = true;
pebble.receiveShadow = true;
scene.add(pebble);
}
const depositAnchor = new THREE.Vector3(4.65, 0, 0);
const depositInfo = pathInfoAtX(depositAnchor.x);
depositAnchor.z = depositInfo.z + 1.0;
depositAnchor.y = floorAt(depositAnchor.x, depositAnchor.z) + 0.38;
// ------------------------------------------------------------
// Lighting and ground
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc8deea, 0x5b4937, 1.8));
const sun = new THREE.DirectionalLight(0xffedc9, 3.1);
sun.position.set(7, 15, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.00045;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9fc9dc, 1.0);
fill.position.set(-10, 7, 8);
scene.add(fill);
const passageLightA = new THREE.PointLight(0xffb866, 24, 8.5, 2);
passageLightA.position.set(-5.2, 4.0, -1.0);
scene.add(passageLightA);
const passageLightB = new THREE.PointLight(0x8edbd1, 18, 8.0, 2);
passageLightB.position.set(4.9, 3.0, -0.6);
scene.add(passageLightB);
const passageLightC = new THREE.PointLight(0xffd19a, 16, 7.0, 2);
passageLightC.position.set(0.0, 2.2, -2.0);
scene.add(passageLightC);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x11191d,
roughness: 0.95,
metalness: 0
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.08;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------
// Direct camera controls, without add-ons
// ------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
view.targetTheta -= deltaX * 0.006;
view.targetPhi = clamp(
view.targetPhi - deltaY * 0.005,
0.55,
1.47
);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
view.targetRadius = clamp(
view.targetRadius * Math.exp(event.deltaY * 0.001),
13.5,
34
);
}, { passive: false });
// Basic pinch zoom.
const activePointers = new Map();
let previousPinchDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
previousPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!activePointers.has(event.pointerId)) return;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (previousPinchDistance > 0) {
const factor = previousPinchDistance / Math.max(distance, 1);
view.targetRadius = clamp(
view.targetRadius * factor,
13.5,
34
);
}
previousPinchDistance = distance;
}
});
function removePointer(event) {
activePointers.delete(event.pointerId);
previousPinchDistance = 0;
}
canvas.addEventListener("pointerup", removePointer);
canvas.addEventListener("pointercancel", removePointer);
function resetView() {
view.targetTheta = defaultView.theta;
view.targetPhi = defaultView.phi;
view.targetRadius = defaultView.radius;
}
document.getElementById("reset-view").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetView();
}
});
let labelsVisible = true;
const labels = [
document.getElementById("label-stalactite"),
document.getElementById("label-stalagmite"),
document.getElementById("label-deposit")
];
document.getElementById("toggle-labels").addEventListener("click", () => {
labelsVisible = !labelsVisible;
const button = document.getElementById("toggle-labels");
button.textContent = labelsVisible ? "Labels on" : "Labels off";
button.classList.toggle("off", !labelsVisible);
});
// ------------------------------------------------------------
// Screen-space annotation placement
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.12 &&
Math.abs(projected.y) < 1.12;
element.style.opacity = visible && labelsVisible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
renderer.setAnimationLoop(() => {
view.theta += (view.targetTheta - view.theta) * 0.1;
view.phi += (view.targetPhi - view.phi) * 0.1;
view.radius += (view.targetRadius - view.radius) * 0.1;
updateCamera();
positionLabel(labels[0], stalactiteAnchor);
positionLabel(labels[1], stalagmiteAnchor);
positionLabel(labels[2], depositAnchor);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```