Prompt specification
Build or customize the Three.js 3D scene "Sinkhole and aquifer exploration model" 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: Sinkhole and aquifer exploration model
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/sinkhole-and-aquifer-exploration-model-x1156
Scene Description Prompt:
Allow toggling surface transparency on a bounded karst landscape; trace a dye marker through a simplified underground route and label the route illustrative rather than survey data. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (sinkhole-and-aquifer-exploration-model-x1156):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Karst block model — illustrative dye trace</title>
<style>
:root{--accent:#39d7ff;--accent2:#ff8a3c;--ink:#dceaf2;}
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:#070b10;overflow:hidden;
font-family:ui-sans-serif,system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;color:var(--ink)}
#c{position:fixed;inset:0;width:100%;height:100%;display:block;touch-action:none;cursor:grab}
#c:active{cursor:grabbing}
#panel{position:fixed;left:16px;top:16px;width:272px;padding:14px 15px;border-radius:13px;
background:rgba(11,17,24,.82);border:1px solid rgba(57,215,255,.20);
box-shadow:0 14px 40px rgba(0,0,0,.5);backdrop-filter:blur(9px);z-index:5}
#panel h1{margin:0 0 2px;font-size:15px;letter-spacing:.02em;font-weight:650}
#panel h1 span{color:var(--accent);font-weight:500}
#panel p.sub{margin:0 0 11px;font-size:11.5px;line-height:1.45;color:#8fa6b5}
.row{display:flex;align-items:center;gap:8px;font-size:12px;margin:7px 0;cursor:pointer;
padding:5px 7px;border-radius:7px;background:rgba(255,255,255,.03);border:1px solid rgba(255,255,255,.05)}
.row:hover{background:rgba(57,215,255,.09)}
.row input{accent-color:var(--accent);width:14px;height:14px;margin:0;cursor:pointer}
.btns{display:flex;gap:6px;margin:11px 0 9px;flex-wrap:wrap}
button{flex:1 1 auto;min-width:74px;padding:6px 8px;font-size:11.5px;color:var(--ink);cursor:pointer;
background:rgba(57,215,255,.10);border:1px solid rgba(57,215,255,.32);border-radius:7px}
button:hover{background:rgba(57,215,255,.2)}
#barOuter{height:5px;border-radius:4px;background:rgba(255,255,255,.08);overflow:hidden;margin:4px 0 7px}
#bar{height:100%;width:0%;background:linear-gradient(90deg,var(--accent),var(--accent2));
transition:width .08s linear}
#stat{font-size:11px;color:#8fa6b5;margin-bottom:10px;font-variant-numeric:tabular-nums}
.legend{font-size:11px;color:#9fb3c1;border-top:1px solid rgba(255,255,255,.08);padding-top:9px;line-height:1.85}
.legend i{display:inline-block;width:9px;height:9px;border-radius:50%;margin-right:7px;vertical-align:1px}
.sw.route{background:var(--accent);box-shadow:0 0 8px var(--accent)}
.sw.dye{background:var(--accent2);box-shadow:0 0 8px var(--accent2)}
.sw.water{background:#2f8fd0}
#labels{position:fixed;inset:0;pointer-events:none;z-index:4}
.lab{position:absolute;transform:translate(-50%,-50%);white-space:nowrap;font-size:11px;letter-spacing:.02em;
padding:3px 9px;border-radius:999px;color:#d7eefb;background:rgba(7,13,19,.78);
border:1px solid rgba(57,215,255,.34);box-shadow:0 4px 14px rgba(0,0,0,.45)}
.lab b{color:var(--accent);font-weight:600}
.lab.amber{border-color:rgba(255,138,60,.42);color:#ffe0c9}
.lab.amber b{color:var(--accent2)}
.lab.dim{border-color:rgba(160,180,195,.25);color:#a9bcc9;background:rgba(7,13,19,.62)}
#disclaimer{position:fixed;left:16px;bottom:14px;max-width:430px;padding:9px 13px;border-radius:9px;
font-size:11.5px;line-height:1.5;color:#b9ccd8;background:rgba(11,17,24,.8);
border:1px solid rgba(255,138,60,.28);z-index:5}
#disclaimer b{color:var(--accent2)}
#hint{position:fixed;right:16px;bottom:14px;font-size:10.5px;color:#6d8494;text-align:right;line-height:1.6;z-index:5}
@media(max-width:640px){#panel{width:calc(100% - 32px)}#disclaimer{max-width:calc(100% - 32px)}#hint{display:none}}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="labels"></div>
<div id="panel">
<h1>Karst block model <span>— dye tracer</span></h1>
<p class="sub">Bounded voxel landscape with a simplified underground route and a travelling dye marker.</p>
<label class="row"><input type="checkbox" id="ghost"><span>Surface transparency (x-ray)</span></label>
<label class="row"><input type="checkbox" id="follow"><span>Camera follows the dye</span></label>
<label class="row"><input type="checkbox" id="showroute" checked><span>Show route path</span></label>
<div class="btns">
<button id="play">Pause trace</button>
<button id="reset">Restart</button>
<button id="view">Reset view</button>
</div>
<div id="barOuter"><div id="bar"></div></div>
<div id="stat">Trace position: 0 %</div>
<div class="legend">
<div><i class="sw route"></i>Illustrative conduit path (schematic)</div>
<div><i class="sw dye"></i>Dye marker / tracer front</div>
<div><i class="sw water"></i>Sink hole (swallow hole) & spring pool</div>
</div>
</div>
<div id="disclaimer">
<b>Illustrative only.</b> The conduit geometry, depths and travel times in this scene are a
<b>simplified schematic — not survey data</b> and not derived from a monitored site.
</div>
<div id="hint">drag = orbit · shift/right-drag = pan · wheel = zoom</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ============================================================
0. Constants & helpers
============================================================ */
const NX = 48, NY = 20, NZ = 26; // voxel grid
const X0 = -NX / 2 + 0.5; // world x of voxel column ix
const Y0 = 0.5, Z0 = -0.5; // world y / z of voxel layer 0
const ixOf = wx => wx - X0;
const izOf = wz => -wz - 0.5; // block is cut at z = 0 (section face)
const iyOf = wy => wy - 0.5;
const SINK = { x: -15, z: -19 }; // swallow hole (inlet)
const SPRING = { x: 12, z: -9 }; // spring outlet
const clamp = (v, a, b) => v < a ? a : v > b ? b : v;
const lerp = (a, b, t) => a + (b - a) * t;
function hash2i(x, y){
let h = Math.imul(x | 0, 374761393) ^ Math.imul(y | 0, 668265263);
h = Math.imul(h ^ (h >>> 13), 1274126177);
return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
}
function vnoise(x, y){
const xi = Math.floor(x), yi = Math.floor(y);
const xf = x - xi, yf = y - yi;
const u = xf * xf * (3 - 2 * xf), v = yf * yf * (3 - 2 * yf);
const a = hash2i(xi, yi), b = hash2i(xi + 1, yi);
const c = hash2i(xi, yi + 1), d = hash2i(xi + 1, yi + 1);
return lerp(lerp(a, b, u), lerp(c, d, u), v);
}
function fbm(x, y){
return 0.66 * vnoise(x, y) + 0.34 * vnoise(x * 2.13 + 13.7, y * 2.13 + 7.1);
}
/* ============================================================
1. Renderer / scene / lights
============================================================ */
const canvas = document.getElementById('c');
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070b10);
scene.fog = new THREE.Fog(0x070b10, 70, 190);
const camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 0.5, 500);
scene.add(new THREE.HemisphereLight(0xbcd8ff, 0x3b3226, 0.75));
scene.add(new THREE.AmbientLight(0xffffff, 0.16));
const sun = new THREE.DirectionalLight(0xfff1dd, 1.05);
sun.position.set(28, 44, 26);
scene.add(sun);
const rim = new THREE.DirectionalLight(0x6fd8ff, 0.35);
rim.position.set(-30, 18, -30);
scene.add(rim);
/* ============================================================
2. Terrain height field (bounded block, half cut away)
============================================================ */
function heightAt(wx, wz){
let h = 7.0
+ fbm(wx * 0.075 + 3.1, wz * 0.075 + 9.4) * 8.2
+ fbm(wx * 0.230 + 31.0, wz * 0.230 + 17.0) * 1.9;
// swallow-hole funnel
const d1 = Math.hypot(wx - SINK.x, wz - SINK.z);
h -= 4.6 * Math.exp(-(d1 * d1) / (2 * 2.7 * 2.7));
// spring basin
const d2 = Math.hypot(wx - SPRING.x, wz - SPRING.z);
h -= 1.5 * Math.exp(-(d2 * d2) / (2 * 3.0 * 3.0));
return clamp(h, 3, 17);
}
const countAt = (wx, wz) => Math.max(2, Math.round(heightAt(wx, wz)));
const topY = (wx, wz) => countAt(wx, wz) - 0.5;
/* ============================================================
3. Voxel grid + route carving
============================================================ */
const grid = new Uint8Array(NX * NY * NZ); // 1 = rock, 0 = void
const gidx = (ix, iy, iz) => (ix * NY + iy) * NZ + iz;
for (let ix = 0; ix < NX; ix++){
const wx = ix + X0;
for (let iz = 0; iz < NZ; iz++){
const wz = -(iz + 0.5);
const c = countAt(wx, wz);
for (let iy = 0; iy < c; iy++) grid[gidx(ix, iy, iz)] = 1;
}
}
function carveSphere(cx, cy, cz, r){
const i0 = Math.max(0, Math.floor(ixOf(cx - r))), i1 = Math.min(NX - 1, Math.ceil(ixOf(cx + r)));
const j0 = Math.max(0, Math.floor(izOf(cz + r))), j1 = Math.min(NZ - 1, Math.ceil(izOf(cz - r)));
const k0 = Math.max(0, Math.floor(iyOf(cy - r))), k1 = Math.min(NY - 1, Math.ceil(iyOf(cy + r)));
const r2 = r * r;
for (let ix = i0; ix <= i1; ix++){
const dx = ix + X0 - cx;
for (let iy = k0; iy <= k1; iy++){
const dy = iy + 0.5 - cy;
for (let iz = j0; iz <= j1; iz++){
const dz = -(iz + 0.5) - cz;
if (dx * dx + dy * dy + dz * dz <= r2) grid[gidx(ix, iy, iz)] = 0;
}
}
}
}
function carveEllipsoid(cx, cy, cz, rx, ry, rz){
const i0 = Math.max(0, Math.floor(ixOf(cx - rx))), i1 = Math.min(NX - 1, Math.ceil(ixOf(cx + rx)));
const j0 = Math.max(0, Math.floor(izOf(cz + rz))), j1 = Math.min(NZ - 1, Math.ceil(izOf(cz - rz)));
const k0 = Math.max(0, Math.floor(iyOf(cy - ry))), k1 = Math.min(NY - 1, Math.ceil(iyOf(cy + ry)));
for (let ix = i0; ix <= i1; ix++){
const dx = (ix + X0 - cx) / rx;
for (let iy = k0; iy <= k1; iy++){
const dy = (iy + 0.5 - cy) / ry;
for (let iz = j0; iz <= j1; iz++){
const dz = (-(iz + 0.5) - cz) / rz;
if (dx * dx + dy * dy + dz * dz <= 1) grid[gidx(ix, iy, iz)] = 0;
}
}
}
}
/* --- the illustrative route (way points only, NOT survey data) --- */
const sinkTop = topY(SINK.x, SINK.z);
const springTop = topY(SPRING.x, SPRING.z);
const routePts = [
new THREE.Vector3(SINK.x, sinkTop + 2.2, SINK.z), // dye released above the pit
new THREE.Vector3(SINK.x, sinkTop - 0.6, SINK.z), // entering the funnel
new THREE.Vector3(SINK.x, 6.6, SINK.z), // shaft bottom
new THREE.Vector3(-12.0, 5.2, -16.0),
new THREE.Vector3(-8.0, 4.4, -10.0), // small chamber
new THREE.Vector3(-3.0, 4.0, -5.0),
new THREE.Vector3( 2.0, 4.2, -2.0), // chamber opening at the section face
new THREE.Vector3( 7.0, 5.4, -4.5),
new THREE.Vector3(11.0, 7.0, -8.0),
new THREE.Vector3(SPRING.x, springTop + 0.9, SPRING.z)
];
const curve = new THREE.CatmullRomCurve3(routePts, false, 'catmullrom', 0.5);
// carve the conduit corridor + shaft + chambers
const RS = 480;
for (let i = 0; i < RS; i++){
const p = curve.getPoint(i / (RS - 1));
carveSphere(p.x, p.y, p.z, 1.85);
}
for (let i = 0; i <= 13; i++) carveSphere(SINK.x, i + 0.5, SINK.z, 1.55); // vertical shaft
carveEllipsoid( 2.0, 4.2, -2.0, 3.7, 2.7, 3.3); // chamber at the cut face
carveEllipsoid(-8.0, 4.4, -10.0, 2.9, 2.3, 2.9); // mid chamber
carveEllipsoid( 9.5, 6.0, -6.0, 2.6, 2.2, 2.6); // rising chamber
/* ============================================================
4. Voxel instancing (surface shell + bedrock core)
============================================================ */
const shellData = [], coreData = [];
const grass = new THREE.Color(), soil = new THREE.Color(), rock = new THREE.Color();
const BANDS = [0x9aa3aa, 0xc3b9a3, 0x8e9691, 0xb0a894].map(h => new THREE.Color(h));
for (let ix = 0; ix < NX; ix++){
const wx = ix + X0;
for (let iz = 0; iz < NZ; iz++){
const wz = -(iz + 0.5);
const c = countAt(wx, wz);
for (let iy = 0; iy < c; iy++){
if (!grid[gidx(ix, iy, iz)]) continue;
const r = hash2i(ix * 13 + iz, iy * 7919 + ix);
if (iy === c - 1){ // grass / soil surface
grass.setHSL(0.255 + r * 0.035, 0.40 + r * 0.08, 0.26 + r * 0.07);
rock.copy(grass);
} else if (iy >= c - 3){ // subsoil
soil.setHSL(0.075 + r * 0.02, 0.34, 0.27 + r * 0.08);
rock.copy(soil);
} else { // limestone with banding
rock.copy(BANDS[Math.floor(iy / 3) % BANDS.length]);
rock.offsetHSL(0, (r - 0.5) * 0.04, (r - 0.5) * 0.055);
}
const m = new THREE.Matrix4().makeTranslation(wx, iy + 0.5, wz);
(iy >= c - 4 ? shellData : coreData).push({ m, c: rock.clone() });
}
}
}
const boxGeo = new THREE.BoxGeometry(0.985, 0.985, 0.985);
const shellMat = new THREE.MeshLambertMaterial({ color: 0xffffff });
const coreMat = new THREE.MeshLambertMaterial({ color: 0xffffff });
function buildInstanced(data, mat, order){
const mesh = new THREE.InstancedMesh(boxGeo, mat, data.length);
data.forEach((d, i) => { mesh.setMatrixAt(i, d.m); mesh.setColorAt(i, d.c); });
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.renderOrder = order;
scene.add(mesh);
return mesh;
}
const coreMesh = buildInstanced(coreData, coreMat, 1);
const shellMesh = buildInstanced(shellData, shellMat, 2);
/* --- base plate + bounding wireframe (bounded model) --- */
const base = new THREE.Mesh(
new THREE.BoxGeometry(NX + 1.2, 0.9, NZ + 1.2),
new THREE.MeshLambertMaterial({ color: 0x121a22 })
);
base.position.set(0, -0.45, -(NZ / 2));
scene.add(base);
const gridHelp = new THREE.GridHelper(90, 60, 0x1f2f3a, 0x121b22);
gridHelp.position.y = -0.92;
scene.add(gridHelp);
const bbox = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(NX + 0.6, NY + 1.2, NZ + 0.6)),
new THREE.LineBasicMaterial({ color: 0x39d7ff, transparent: true, opacity: 0.22 })
);
bbox.position.set(0, (NY - 1) / 2, -(NZ / 2));
scene.add(bbox);
/* ============================================================
5. Route visuals: tube, dye marker, trail, pulses, water
============================================================ */
const routeMat = new THREE.MeshBasicMaterial({ color: 0x38e0ff, transparent: true, opacity: 0.9 });
const routeMesh = new THREE.Mesh(new THREE.TubeGeometry(curve, 260, 0.20, 10, false), routeMat);
routeMesh.renderOrder = 5;
scene.add(routeMesh);
const markerMat = new THREE.MeshBasicMaterial({ color: 0xff7a3c });
const marker = new THREE.Mesh(new THREE.SphereGeometry(0.5, 24, 18), markerMat);
marker.renderOrder = 6;
scene.add(marker);
function glowTexture(){
const c = document.createElement('canvas'); c.width = c.height = 64;
const g = c.getContext('2d');
const grd = g.createRadialGradient(32, 32, 0, 32, 32, 32);
grd.addColorStop(0, 'rgba(255,255,255,1)');
grd.addColorStop(0.35, 'rgba(255,190,130,0.55)');
grd.addColorStop(1, 'rgba(255,140,60,0)');
g.fillStyle = grd; g.fillRect(0, 0, 64, 64);
const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace; return t;
}
const haloMat = new THREE.SpriteMaterial({
map: glowTexture(), color: 0xffa257, transparent: true, opacity: 0.85,
blending: THREE.AdditiveBlending, depthWrite: false
});
const halo = new THREE.Sprite(haloMat);
halo.scale.set(4.2, 4.2, 1);
halo.renderOrder = 7;
scene.add(halo);
const markerLight = new THREE.PointLight(0xff9a55, 2.4, 14);
scene.add(markerLight);
// dye trail (additive spheres fading to black behind the front)
const TRAIL = 30;
const trailMat = new THREE.MeshBasicMaterial({
blending: THREE.AdditiveBlending, transparent: true, depthWrite: false, color: 0xffffff
});
const trail = new THREE.InstancedMesh(new THREE.SphereGeometry(0.32, 12, 9), trailMat, TRAIL);
trail.renderOrder = 7;
scene.add(trail);
const trailColor = new THREE.Color();
// spring pool + outlet
const pool = new THREE.Mesh(
new THREE.BoxGeometry(3.6, 0.45, 3.6),
new THREE.MeshBasicMaterial({ color: 0x2f8fd0, transparent: true, opacity: 0.45, depthWrite: false })
);
pool.position.set(SPRING.x, springTop - 0.05, SPRING.z);
pool.renderOrder = 3;
scene.add(pool);
const outlet = new THREE.Mesh(new THREE.SphereGeometry(0.55, 16, 12), new THREE.MeshBasicMaterial({ color: 0x7fd6ff }));
outlet.position.set(SPRING.x, springTop + 0.35, SPRING.z);
outlet.renderOrder = 4;
scene.add(outlet);
// sink-hole rim ring
const rimRing = new THREE.Mesh(
new THREE.TorusGeometry(2.5, 0.10, 8, 44),
new THREE.MeshBasicMaterial({ color: 0xffb066, transparent: true, opacity: 0.9 })
);
rimRing.rotation.x = -Math.PI / 2;
rimRing.position.set(SINK.x, sinkTop + 0.55, SINK.z);
scene.add(rimRing);
// pulse rings (release at the sink, arrival at the spring)
const pulses = [];
const ringGeo = new THREE.TorusGeometry(1, 0.09, 6, 40);
function spawnPulse(pos, color){
const m = new THREE.Mesh(ringGeo, new THREE.MeshBasicMaterial({
color, transparent: true, opacity: 0.9, depthWrite: false, side: THREE.DoubleSide
}));
m.rotation.x = -Math.PI / 2;
m.position.set(pos.x, pos.y + 0.25, pos.z);
m.renderOrder = 8;
scene.add(m); pulses.push({ m, age: 0 });
}
/* ============================================================
6. Labels (projected HTML chips)
============================================================ */
const labelHost = document.getElementById('labels');
const labels = [];
function addLabel(html, anchor, cls){
const el = document.createElement('div');
el.className = 'lab ' + (cls || '');
el.innerHTML = html;
labelHost.appendChild(el);
labels.push({ el, anchor: anchor.clone() });
}
const midPoint = curve.getPointAt(0.46);
addLabel('<b>Swallow hole</b> · shaft inlet', new THREE.Vector3(SINK.x, sinkTop + 1.2, SINK.z), 'amber');
addLabel('<b>Conduit</b> · illustrative path', midPoint);
addLabel('<b>Spring outlet</b>', new THREE.Vector3(SPRING.x, springTop + 1.0, SPRING.z), 'amber');
addLabel('Section face (model boundary)', new THREE.Vector3(-6, 1.4, 0), 'dim');
const _v = new THREE.Vector3();
function updateLabels(){
const w = innerWidth, h = innerHeight;
for (const L of labels){
_v.copy(L.anchor).project(camera);
if (_v.z > 1 || _v.z < -1){ L.el.style.display = 'none'; continue; }
L.el.style.display = 'block';
const x = (_v.x * 0.5 + 0.5) * w, y = (-_v.y * 0.5 + 0.5) * h;
L.el.style.transform = `translate(-50%,-50%) translate(${x.toFixed(1)}px,${y.toFixed(1)}px)`;
}
}
/* ============================================================
7. Hand-written camera controls (orbit / pan / zoom / pinch)
============================================================ */
const cam = { theta: 0.92, phi: 1.02, radius: 62, target: new THREE.Vector3(0, 5, -11) };
const camT = { theta: 0.92, phi: 1.02, radius: 62, target: new THREE.Vector3(0, 5, -11) };
const HOME = { theta: 0.92, phi: 1.02, radius: 62, target: new THREE.Vector3(0, 5, -11) };
const pointers = new Map();
let mode = 'rotate', lastX = 0, lastY = 0, pinchDist = 0;
const _dir = new THREE.Vector3(), _right = new THREE.Vector3(), _up = new THREE.Vector3();
function panBy(dx, dy){
const s = camT.radius * 0.0017;
camera.getWorldDirection(_dir);
_right.crossVectors(_dir, camera.up).normalize();
_up.crossVectors(_right, _dir).normalize();
camT.target.addScaledVector(_right, -dx * s).addScaledVector(_up, dy * s);
camT.target.x = clamp(camT.target.x, -30, 30);
camT.target.y = clamp(camT.target.y, -4, 22);
camT.target.z = clamp(camT.target.z, -30, 12);
}
function pinch(){
const p = [...pointers.values()];
if (p.length < 2) return;
const d = Math.hypot(p[0].x - p[1].x, p[0].y - p[1].y);
if (pinchDist > 0) camT.radius = clamp(camT.radius * (pinchDist / Math.max(d, 1)), 16, 170);
pinchDist = d;
camT.target.x += (p[0].x + p[1].x) / 2 - lastX;
camT.target.y -= (p[0].y + p[1].y) / 2 - lastY;
lastX = (p[0].x + p[1].x) / 2; lastY = (p[0].y + p[1].y) / 2;
}
canvas.addEventListener('pointerdown', e => {
canvas.setPointerCapture(e.pointerId);
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size === 1){
mode = (e.button === 2 || e.shiftKey) ? 'pan' : 'rotate';
lastX = e.clientX; lastY = e.clientY;
} else { mode = 'pinch'; pinchDist = 0; pinch(); }
});
canvas.addEventListener('pointermove', e => {
if (!pointers.has(e.pointerId)) return;
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size >= 2){ pinch(); return; }
const dx = e.clientX - lastX, dy = e.clientY - lastY;
if (mode === 'rotate'){
camT.theta -= dx * 0.005;
camT.phi = clamp(camT.phi - dy * 0.005, 0.14, 1.46);
} else if (mode === 'pan'){
panBy(dx, dy);
}
lastX = e.clientX; lastY = e.clientY;
});
function endPointer(e){
pointers.delete(e.pointerId);
if (pointers.size === 0){ mode = 'rotate'; pinchDist = 0; }
}
canvas.addEventListener('pointerup', endPointer);
canvas.addEventListener('pointercancel', endPointer);
canvas.addEventListener('contextmenu', e => e.preventDefault());
canvas.addEventListener('wheel', e => {
e.preventDefault();
camT.radius = clamp(camT.radius * Math.exp(e.deltaY * 0.0011), 16, 170);
}, { passive: false });
/* ============================================================
8. UI wiring
============================================================ */
const ui = {
ghost: document.getElementById('ghost'),
follow: document.getElementById('follow'),
showroute: document.getElementById('showroute'),
play: document.getElementById('play'),
reset: document.getElementById('reset'),
view: document.getElementById('view'),
bar: document.getElementById('bar'),
stat: document.getElementById('stat')
};
let ghostOn = false, shellTarget = 1, coreTarget = 1, playing = true, traceT = 0;
const SPEED = 0.040; // fraction of route per second (~25 s loop)
const MODEL_MINUTES = 26; // purely illustrative "model time"
function setGhost(on){
ghostOn = on;
shellTarget = on ? 0.22 : 1.0; // land skin fades to glass
coreTarget = on ? 0.55 : 1.0; // bedrock becomes a translucent mass
for (const m of [shellMat, coreMat]){
m.transparent = true; m.depthWrite = true; m.needsUpdate = true;
}
routeMat.depthTest = !on;
markerMat.depthTest = !on;
trailMat.depthTest = !on;
haloMat.depthTest = !on;
}
ui.ghost.addEventListener('change', () => setGhost(ui.ghost.checked));
ui.showroute.addEventListener('change', () => {
routeMesh.visible = ui.showroute.checked;
});
ui.play.addEventListener('click', () => {
playing = !playing;
ui.play.textContent = playing ? 'Pause trace' : 'Resume trace';
});
ui.reset.addEventListener('click', () => {
traceT = 0;
spawnPulse(new THREE.Vector3(SINK.x, sinkTop, SINK.z), 0xffb066);
});
ui.view.addEventListener('click', () => {
camT.theta = HOME.theta; camT.phi = HOME.phi; camT.radius = HOME.radius;
camT.target.copy(HOME.target);
});
/* ============================================================
9. Animation loop
============================================================ */
const clock = new THREE.Clock();
const _p = new THREE.Vector3(), _s = new THREE.Vector3(), _mtx = new THREE.Matrix4();
function animate(){
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
/* ---- trace ---- */
if (playing){
const prev = traceT;
traceT += dt * SPEED;
if (traceT >= 1){
traceT -= 1;
spawnPulse(new THREE.Vector3(SPRING.x, springTop, SPRING.z), 0x7fd6ff);
spawnPulse(new THREE.Vector3(SINK.x, sinkTop, SINK.z), 0xffb066);
}
if (prev < 0.02 && traceT >= 0.02) spawnPulse(new THREE.Vector3(SINK.x, sinkTop, SINK.z), 0xffb066);
}
curve.getPointAt(traceT, _p);
marker.position.copy(_p);
halo.position.copy(_p);
markerLight.position.copy(_p);
const pulse = 1 + Math.sin(performance.now() * 0.006) * 0.12;
marker.scale.setScalar(pulse);
halo.scale.set(4.2 * pulse, 4.2 * pulse, 1);
/* ---- dye trail behind the front ---- */
for (let i = 0; i < TRAIL; i++){
const tt = traceT - i * 0.0075;
if (tt < 0){ _mtx.makeScale(0, 0, 0); trail.setMatrixAt(i, _mtx); continue; }
curve.getPointAt(tt, _s);
const k = Math.pow(1 - i / TRAIL, 1.6);
_mtx.makeScale(k, k, k);
_mtx.setPosition(_s);
trail.setMatrixAt(i, _mtx);
trailColor.setRGB(1.0 * k, 0.45 * k, 0.16 * k);
trail.setColorAt(i, trailColor);
}
trail.instanceMatrix.needsUpdate = true;
if (trail.instanceColor) trail.instanceColor.needsUpdate = true;
/* ---- pulses ---- */
for (let i = pulses.length - 1; i >= 0; i--){
const P = pulses[i]; P.age += dt;
const s = 1 + P.age * 6.5;
P.m.scale.set(s, s, s);
P.m.material.opacity = Math.max(0, 0.9 * (1 - P.age / 1.7));
if (P.age > 1.7){ scene.remove(P.m); P.m.material.dispose(); pulses.splice(i, 1); }
}
/* ---- ghost / transparency animation ---- */
const kk = 1 - Math.exp(-dt * 7);
shellMat.opacity = lerp(shellMat.opacity, shellTarget, kk);
coreMat.opacity = lerp(coreMat.opacity, coreTarget, kk);
if (!ghostOn && shellMat.opacity > 0.999 && coreMat.opacity > 0.999){
shellMat.opacity = coreMat.opacity = 1;
shellMat.transparent = coreMat.transparent = false;
shellMat.needsUpdate = coreMat.needsUpdate = true;
}
/* ---- camera ---- */
const k = 1 - Math.exp(-dt * 11);
cam.theta = lerp(cam.theta, camT.theta, k);
cam.phi = lerp(cam.phi, camT.phi, k);
cam.radius= lerp(cam.radius,camT.radius,k);
cam.target.lerp(camT.target, k);
if (ui.follow.checked) camT.target.lerp(_p, 0.02);
const sp = Math.sin(cam.phi), cp = Math.cos(cam.phi);
camera.position.set(
cam.target.x + cam.radius * sp * Math.cos(cam.theta),
cam.target.y + cam.radius * cp,
cam.target.z + cam.radius * sp * Math.sin(cam.theta)
);
camera.lookAt(cam.target);
/* ---- readouts ---- */
ui.bar.style.width = (traceT * 100).toFixed(1) + '%';
ui.stat.textContent =
`Trace position: ${(traceT * 100).toFixed(0)} % · model time ${(traceT * MODEL_MINUTES).toFixed(1)} min (illustrative)`;
updateLabels();
renderer.render(scene, camera);
}
animate();
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
```