Prompt specification
Build or customize the Three.js 3D scene "Relative-motion geology sandbox" 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: Relative-motion geology sandbox
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/relative-motion-geology-sandbox-x1144
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts geological fault blocks. Provide independent controls for vertical and horizontal block displacement in a bounded terrain model; show shifted reference layers and label the result conceptual rather than earthquake prediction. Low-poly.
Working Three.js Starter (relative-motion-geology-sandbox-x1144):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fault Block Lab — Conceptual Geology</title>
<style>
:root {
color-scheme: dark;
--bg: #10191d;
--panel: rgba(18, 28, 32, 0.94);
--panel-border: rgba(205, 225, 218, 0.14);
--text: #edf2ed;
--muted: #9eada7;
--accent: #d7c17f;
--accent-2: #7fa99b;
--danger: #dc806d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 24% 18%, #26363a 0, transparent 38%),
linear-gradient(145deg, #111b1f, #0b1215);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 6px;
background: rgba(12, 19, 22, 0.82);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(8px);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
white-space: nowrap;
}
.world-label.block-a {
color: #b8d6c8;
border-color: rgba(127, 169, 155, 0.5);
}
.world-label.block-b {
color: #eadb9d;
border-color: rgba(215, 193, 127, 0.5);
}
.world-label.datum {
color: #f0d98a;
border-color: rgba(240, 217, 138, 0.55);
font-size: 9px;
}
.world-label.fault {
color: #e9a393;
border-color: rgba(220, 128, 109, 0.42);
font-size: 9px;
}
.brand {
position: fixed;
top: 22px;
left: 24px;
pointer-events: none;
text-shadow: 0 2px 12px rgba(0, 0, 0, 0.55);
}
.brand .eyebrow,
.panel .eyebrow {
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.brand h1 {
margin: 7px 0 0;
font-size: clamp(22px, 3vw, 38px);
font-weight: 700;
letter-spacing: -0.04em;
}
.brand p {
margin: 7px 0 0;
color: #b7c1bc;
font-size: 12px;
}
.concept-badge {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 14px;
padding: 8px 11px;
border: 1px solid rgba(220, 128, 109, 0.38);
border-radius: 999px;
background: rgba(54, 28, 25, 0.52);
color: #f0b8ab;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.concept-badge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--danger);
box-shadow: 0 0 12px rgba(220, 128, 109, 0.75);
}
.panel {
position: fixed;
top: 20px;
right: 20px;
width: min(330px, calc(100vw - 40px));
max-height: calc(100vh - 40px);
overflow-y: auto;
padding: 20px;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(18px);
}
.panel h2 {
margin: 7px 0 8px;
font-size: 20px;
letter-spacing: -0.025em;
}
.panel > p {
margin: 0 0 17px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.section-label {
margin: 18px 0 9px;
color: #c8d2cd;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.block-tabs {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.block-tab {
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
color: var(--muted);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: 160ms ease;
}
.block-tab:hover {
background: rgba(255, 255, 255, 0.07);
color: var(--text);
}
.block-tab.active[data-block="A"] {
border-color: rgba(127, 169, 155, 0.65);
background: rgba(127, 169, 155, 0.13);
color: #c8e0d5;
}
.block-tab.active[data-block="B"] {
border-color: rgba(215, 193, 127, 0.65);
background: rgba(215, 193, 127, 0.13);
color: #f0dfa8;
}
.control {
margin-top: 16px;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
font-size: 12px;
}
.control-heading output {
min-width: 55px;
padding: 3px 7px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.06);
color: var(--accent);
font-variant-numeric: tabular-nums;
text-align: center;
font-size: 11px;
}
input[type="range"] {
--fill: 50%;
width: 100%;
height: 5px;
margin: 7px 0;
border-radius: 99px;
outline: none;
appearance: none;
background: linear-gradient(
to right,
var(--accent) 0,
var(--accent) var(--fill),
rgba(255, 255, 255, 0.12) var(--fill),
rgba(255, 255, 255, 0.12) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid #152025;
border-radius: 50%;
appearance: none;
background: #f0dfa8;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #152025;
border-radius: 50%;
background: #f0dfa8;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
}
.range-ends {
display: flex;
justify-content: space-between;
color: #71817b;
font-size: 9px;
}
.presets {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.preset {
padding: 9px 8px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
color: #b9c5bf;
font: inherit;
font-size: 10px;
font-weight: 700;
cursor: pointer;
transition: 150ms ease;
}
.preset:hover {
border-color: rgba(215, 193, 127, 0.4);
background: rgba(215, 193, 127, 0.09);
color: #eee0ac;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 18px;
}
.metric {
padding: 10px 8px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 8px;
background: rgba(0, 0, 0, 0.12);
text-align: center;
}
.metric span {
display: block;
margin-bottom: 4px;
color: #71817b;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric strong {
color: #dce5df;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 11px;
margin-top: 15px;
color: #9eada7;
font-size: 9px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
background: #d7c17f;
}
.swatch.terrain {
background: #71806d;
}
.disclaimer {
margin-top: 18px;
padding: 11px 12px;
border-left: 2px solid var(--danger);
border-radius: 0 7px 7px 0;
background: rgba(220, 128, 109, 0.08);
color: #d6aaa0;
font-size: 10px;
line-height: 1.5;
}
.hint {
position: fixed;
left: 24px;
bottom: 20px;
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 7px;
background: rgba(11, 18, 21, 0.68);
color: #9caaa4;
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(8px);
}
.hint strong {
color: #d6dfda;
}
@media (max-width: 720px) {
.brand {
top: 14px;
left: 16px;
}
.brand h1 {
font-size: 23px;
}
.brand p {
display: none;
}
.concept-badge {
margin-top: 9px;
font-size: 8px;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 46vh;
padding: 15px;
border-radius: 13px;
}
.panel > p,
.hint {
display: none;
}
.section-label {
margin-top: 13px;
}
.control {
margin-top: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly geological fault block model"></canvas>
<div id="labels">
<div id="label-a" class="world-label block-a">BLOCK A · FOOTWALL</div>
<div id="label-b" class="world-label block-b">BLOCK B · HANGING WALL</div>
<div id="label-datum" class="world-label datum">SHARED DATUM LAYER</div>
<div id="label-fault" class="world-label fault">FAULT ZONE</div>
</div>
<header class="brand">
<div class="eyebrow">Structural geology / interactive study</div>
<h1>Fault Block Lab</h1>
<p>Independent vertical and horizontal displacement of bounded terrain blocks.</p>
<div class="concept-badge">Conceptual model · not earthquake prediction</div>
</header>
<aside class="panel">
<div class="eyebrow">Displacement controls</div>
<h2>Compare two fault blocks</h2>
<p>
Select a block, then adjust its vertical and horizontal movement independently.
The cream stratum is a shared reference layer.
</p>
<div class="section-label">Selected block</div>
<div class="block-tabs">
<button class="block-tab" data-block="A" type="button">A · Footwall</button>
<button class="block-tab active" data-block="B" type="button">B · Hanging wall</button>
</div>
<div class="control">
<div class="control-heading">
<label for="vertical">Vertical displacement</label>
<output id="vertical-value" for="vertical">+0.00 u</output>
</div>
<input id="vertical" type="range" min="-1" max="1" step="0.01" value="0" />
<div class="range-ends"><span>−1.0 down</span><span>+1.0 up</span></div>
</div>
<div class="control">
<div class="control-heading">
<label for="horizontal">Horizontal displacement</label>
<output id="horizontal-value" for="horizontal">+0.00 u</output>
</div>
<input id="horizontal" type="range" min="-0.45" max="0.45" step="0.01" value="0" />
<div class="range-ends"><span>left</span><span>right</span></div>
</div>
<div class="section-label">Illustrative presets</div>
<div class="presets">
<button class="preset" data-preset="normal" type="button">Normal fault</button>
<button class="preset" data-preset="reverse" type="button">Reverse fault</button>
<button class="preset" data-preset="strike" type="button">Strike-slip</button>
<button class="preset" data-preset="combined" type="button">Combined offset</button>
<button class="preset" data-preset="reset" type="button">Reset</button>
<button class="preset" data-preset="camera" type="button">Reset camera</button>
</div>
<div class="section-label">Relative result · B minus A</div>
<div class="metrics">
<div class="metric"><span>Vertical</span><strong id="relative-v">+0.00 u</strong></div>
<div class="metric"><span>Horizontal</span><strong id="relative-h">+0.00 u</strong></div>
<div class="metric"><span>Selected</span><strong id="selected-name">Block B</strong></div>
</div>
<div class="legend">
<span><i class="swatch"></i> Shared datum stratum</span>
<span><i class="swatch terrain"></i> Terrain cover</span>
</div>
<div class="disclaimer">
<strong>Conceptual visualization only.</strong>
Values are illustrative model units, not measurements, forecasts, seismic estimates,
hazard assessments, or engineering design guidance.
</div>
</aside>
<div class="hint"><strong>Drag</strong> to orbit · <strong>Scroll</strong> to zoom · <strong>Double-click</strong> to reset view</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x10191d);
scene.fog = new THREE.Fog(0x10191d, 20, 38);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
radius: 17.2,
theta: 0.72,
phi: 1.02,
targetRadius: 17.2,
targetTheta: 0.72,
targetPhi: 1.02
};
const cameraTarget = new THREE.Vector3(0, -0.35, 0);
function updateCamera() {
const s = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + cameraState.radius * s * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z + cameraState.radius * s * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
function resetCamera() {
cameraState.targetRadius = 17.2;
cameraState.targetTheta = 0.72;
cameraState.targetPhi = 1.02;
}
// Custom pointer orbit controls; no examples/addons are used.
let dragging = false;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - dy * 0.005,
0.38,
1.43
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
10.5,
25
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// Lighting.
scene.add(new THREE.HemisphereLight(0xcde2da, 0x30251f, 1.75));
const keyLight = new THREE.DirectionalLight(0xfff0cf, 3.2);
keyLight.position.set(7, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x89b7c7, 1.25);
rimLight.position.set(-9, 5, -8);
scene.add(rimLight);
// Shared materials.
const topMaterialA = new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 0.96,
metalness: 0
});
const topMaterialB = topMaterialA.clone();
const strataMaterials = [
new THREE.MeshStandardMaterial({
color: 0x29373b,
flatShading: true,
roughness: 1,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x8d5143,
flatShading: true,
roughness: 1,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0xb17b50,
flatShading: true,
roughness: 1,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0xd7c17f,
flatShading: true,
roughness: 0.9,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x70806e,
flatShading: true,
roughness: 1,
side: THREE.DoubleSide
})
];
const outlineMaterial = new THREE.LineBasicMaterial({
color: 0x162226,
transparent: true,
opacity: 0.48
});
const datumMaterial = new THREE.LineBasicMaterial({
color: 0xffe28b,
transparent: true,
opacity: 0.95
});
// Bounded tray/base.
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x182327,
roughness: 0.72,
metalness: 0.14
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x425154,
roughness: 0.55,
metalness: 0.35
});
const base = new THREE.Mesh(
new THREE.BoxGeometry(12.9, 0.34, 7.8),
baseMaterial
);
base.position.y = -2.03;
base.receiveShadow = true;
scene.add(base);
function addRim(sizeX, sizeZ, x, z) {
const rim = new THREE.Mesh(
new THREE.BoxGeometry(sizeX, 0.24, sizeZ),
rimMaterial
);
rim.position.set(x, -1.76, z);
rim.castShadow = true;
rim.receiveShadow = true;
scene.add(rim);
}
addRim(12.9, 0.13, 0, 3.84);
addRim(12.9, 0.13, 0, -3.84);
addRim(0.13, 7.68, 6.38, 0);
addRim(0.13, 7.68, -6.38, 0);
const grid = new THREE.GridHelper(12, 12, 0x617073, 0x354448);
grid.position.y = -1.845;
grid.material.transparent = true;
grid.material.opacity = 0.16;
scene.add(grid);
// Boundary corner posts.
const postGeometry = new THREE.CylinderGeometry(0.065, 0.08, 0.65, 6);
const postMaterial = new THREE.MeshStandardMaterial({
color: 0x718083,
roughness: 0.45,
metalness: 0.35
});
for (const x of [-6.32, 6.32]) {
for (const z of [-3.78, 3.78]) {
const post = new THREE.Mesh(postGeometry, postMaterial);
post.position.set(x, -1.48, z);
post.castShadow = true;
scene.add(post);
}
}
// Semi-transparent fault plane.
const faultMaterial = new THREE.MeshBasicMaterial({
color: 0x3b2020,
transparent: true,
opacity: 0.42,
side: THREE.DoubleSide,
depthWrite: false
});
const faultPlane = new THREE.Mesh(
new THREE.PlaneGeometry(6.7, 3.2),
faultMaterial
);
faultPlane.rotation.y = Math.PI / 2;
faultPlane.position.set(0, -0.22, 0);
scene.add(faultPlane);
const faultTraceMaterial = new THREE.LineDashedMaterial({
color: 0xce7666,
dashSize: 0.18,
gapSize: 0.12,
transparent: true,
opacity: 0.72
});
const faultTrace = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -1.82, -3.55),
new THREE.Vector3(0, -1.82, 3.55)
]),
faultTraceMaterial
);
faultTrace.computeLineDistances();
scene.add(faultTrace);
// Deterministic pseudo-random value.
function hash(i, j, seed) {
const n = Math.sin(i * 127.1 + j * 311.7 + seed * 74.7) * 43758.5453;
return n - Math.floor(n);
}
function baseTerrainHeight(u, v, seed) {
const ridge = 0.12 * Math.sin(u * 7.2 + seed * 0.8);
const cross = 0.1 * Math.cos(v * 6.1 - seed * 0.5);
const grain = (hash(Math.round(u * 20), Math.round(v * 20), seed) - 0.5) * 0.18;
const peak = 0.13 * Math.exp(-((u - 0.62) ** 2 + (v - 0.58) ** 2) * 9);
return 0.35 + ridge + cross + grain + peak;
}
function makeGrid(x0, x1, z0, z1, seed) {
const nx = 7;
const nz = 6;
const heights = [];
for (let j = 0; j <= nz; j++) {
const row = [];
const v = j / nz;
for (let i = 0; i <= nx; i++) {
const u = i / nx;
row.push(baseTerrainHeight(u, v, seed));
}
heights.push(row);
}
return { nx, nz, heights, x0, x1, z0, z1 };
}
function makeTopGeometry(grid, verticalOffset, seed, baseColor) {
const positions = [];
const colors = [];
const { nx, nz, heights, x0, x1, z0, z1 } = grid;
const point = (i, j) => {
const x = THREE.MathUtils.lerp(x0, x1, i / nx);
const z = THREE.MathUtils.lerp(z0, z1, j / nz);
const y = heights[j][i] + verticalOffset;
return new THREE.Vector3(x, y, z);
};
const color = new THREE.Color(baseColor);
let triangleIndex = 0;
function addTriangle(a, b, c) {
const averageHeight = (a.y + b.y + c.y) / 3;
const facetColor = color.clone();
facetColor.offsetHSL(
(hash(triangleIndex, seed, 2) - 0.5) * 0.025,
(hash(triangleIndex, seed, 4) - 0.5) * 0.08,
(averageHeight - 0.35 - verticalOffset) * 0.12 +
(hash(triangleIndex, seed, 7) - 0.5) * 0.055
);
for (const p of [a, b, c]) {
positions.push(p.x, p.y, p.z);
colors.push(facetColor.r, facetColor.g, facetColor.b);
}
triangleIndex++;
}
for (let j = 0; j < nz; j++) {
for (let i = 0; i < nx; i++) {
const p00 = point(i, j);
const p10 = point(i + 1, j);
const p01 = point(i, j + 1);
const p11 = point(i + 1, j + 1);
// Upward-facing triangles.
addTriangle(p00, p11, p10);
addTriangle(p00, p01, p11);
}
}
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 makePerimeterSegments(grid, verticalOffset) {
const { nx, nz, heights, x0, x1, z0, z1 } = grid;
const segments = [];
function segment(ax, ay, az, bx, by, bz) {
segments.push([
new THREE.Vector3(ax, ay, az),
new THREE.Vector3(bx, by, bz)
]);
}
// Far edge z = z0.
for (let i = 0; i < nx; i++) {
segment(
THREE.MathUtils.lerp(x0, x1, i / nx), heights[0][i] + verticalOffset, z0,
THREE.MathUtils.lerp(x0, x1, (i + 1) / nx), heights[0][i + 1] + verticalOffset, z0
);
}
// Right edge.
for (let j = 0; j < nz; j++) {
segment(
x1, heights[j][nx] + verticalOffset, THREE.MathUtils.lerp(z0, z1, j / nz),
x1, heights[j + 1][nx] + verticalOffset, THREE.MathUtils.lerp(z0, z1, (j + 1) / nz)
);
}
// Near edge z = z1.
for (let i = nx - 1; i >= 0; i--) {
segment(
THREE.MathUtils.lerp(x0, x1, (i + 1) / nx), heights[nz][i + 1] + verticalOffset, z1,
THREE.MathUtils.lerp(x0, x1, i / nx), heights[nz][i] + verticalOffset, z1
);
}
// Left edge.
for (let j = nz - 1; j >= 0; j--) {
segment(
x0, heights[j + 1][0] + verticalOffset, THREE.MathUtils.lerp(z0, z1, (j + 1) / nz),
x0, heights[j][0] + verticalOffset, THREE.MathUtils.lerp(z0, z1, j / nz)
);
}
return segments;
}
function clipPolygonY(polygon, y, keepAbove) {
const result = [];
for (let i = 0; i < polygon.length; i++) {
const a = polygon[i];
const b = polygon[(i + 1) % polygon.length];
const aInside = keepAbove ? a.y >= y : a.y <= y;
const bInside = keepAbove ? b.y >= y : b.y <= y;
if (aInside && bInside) {
result.push(b.clone());
} else if (aInside && !bInside) {
const t = (y - a.y) / (b.y - a.y);
result.push(
new THREE.Vector3(
THREE.MathUtils.lerp(a.x, b.x, t),
y,
THREE.MathUtils.lerp(a.z, b.z, t)
)
);
} else if (!aInside && bInside) {
const t = (y - a.y) / (b.y - a.y);
result.push(
new THREE.Vector3(
THREE.MathUtils.lerp(a.x, b.x, t),
y,
THREE.MathUtils.lerp(a.z, b.z, t)
)
);
result.push(b.clone());
}
}
return result;
}
function makeStratumGeometry(segments, low, high) {
const positions = [];
for (const [p0, p1] of segments) {
let polygon = [
new THREE.Vector3(p0.x, -1.85, p0.z),
new THREE.Vector3(p1.x, -1.85, p1.z),
p1.clone(),
p0.clone()
];
polygon = clipPolygonY(polygon, low, true);
polygon = clipPolygonY(polygon, high, false);
if (polygon.length < 3) continue;
for (let i = 1; i < polygon.length - 1; i++) {
const a = polygon[0];
const b = polygon[i];
const c = polygon[i + 1];
positions.push(
a.x, a.y, a.z,
b.x, b.y, b.z,
c.x, c.y, c.z
);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
function makeOutlineGeometry(grid, verticalOffset) {
const { nx, nz, heights, x0, x1, z0, z1 } = grid;
const points = [];
for (let i = 0; i <= nx; i++) {
points.push(
new THREE.Vector3(
THREE.MathUtils.lerp(x0, x1, i / nx),
heights[0][i] + verticalOffset,
z0
)
);
}
for (let j = 1; j <= nz; j++) {
points.push(
new THREE.Vector3(
x1,
heights[j][nx] + verticalOffset,
THREE.MathUtils.lerp(z0, z1, j / nz)
)
);
}
for (let i = nx - 1; i >= 0; i--) {
points.push(
new THREE.Vector3(
THREE.MathUtils.lerp(x0, x1, i / nx),
heights[nz][i] + verticalOffset,
z1
)
);
}
for (let j = nz - 1; j >= 0; j--) {
points.push(
new THREE.Vector3(
x0,
heights[j][0] + verticalOffset,
THREE.MathUtils.lerp(z0, z1, j / nz)
)
);
}
return new THREE.BufferGeometry().setFromPoints(points);
}
function createBlock({
id,
x0,
x1,
z0 = -3.35,
z1 = 3.35,
seed,
terrainColor,
topMaterial
}) {
const group = new THREE.Group();
scene.add(group);
const grid = makeGrid(x0, x1, z0, z1, seed);
const surface = new THREE.Mesh(
makeTopGeometry(grid, 0, seed, terrainColor),
topMaterial
);
surface.castShadow = true;
surface.receiveShadow = true;
group.add(surface);
const strata = strataMaterials.map((material) => {
const mesh = new THREE.Mesh(new THREE.BufferGeometry(), material);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
return mesh;
});
const outline = new THREE.Line(
makeOutlineGeometry(grid, 0),
outlineMaterial
);
group.add(outline);
const datumLine = new THREE.LineSegments(
new THREE.BufferGeometry(),
datumMaterial
);
group.add(datumLine);
const markerGeometry = new THREE.OctahedronGeometry(0.065, 0);
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xffdf80,
emissive: 0x6b4a0b,
emissiveIntensity: 0.45,
roughness: 0.7
});
const marker = new THREE.Mesh(markerGeometry, markerMaterial);
group.add(marker);
const labelAnchor = new THREE.Object3D();
group.add(labelAnchor);
const datumAnchor = new THREE.Object3D();
group.add(datumAnchor);
function rebuild(verticalOffset) {
const segments = makePerimeterSegments(grid, verticalOffset);
surface.geometry.dispose();
surface.geometry = makeTopGeometry(
grid,
verticalOffset,
seed,
terrainColor
);
// The deepest boundary stays fixed while upper reference layers shift.
// This keeps the block attached to the bounded model.
const levels = [
-1.85,
-1.48 + verticalOffset * 0.08,
-1.08 + verticalOffset * 0.32,
-0.50 + verticalOffset * 0.72,
-0.30 + verticalOffset * 0.86
];
for (let i = 0; i < strata.length; i++) {
strata[i].geometry.dispose();
strata[i].geometry = makeStratumGeometry(
segments,
levels[i],
levels[i + 1]
);
}
outline.geometry.dispose();
outline.geometry = makeOutlineGeometry(grid, verticalOffset);
const datumY = (-0.50 + verticalOffset * 0.72) / 2 +
(-0.30 + verticalOffset * 0.86) / 2;
const innerX = id === "A" ? x1 + 0.012 : x0 - 0.012;
datumLine.geometry.dispose();
datumLine.geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x0 + 0.2, datumY, z1 + 0.012),
new THREE.Vector3(x1 - 0.2, datumY, z1 + 0.012),
new THREE.Vector3(innerX, datumY, z0 + 0.2),
new THREE.Vector3(innerX, datumY, z1 - 0.2)
]);
marker.position.set((x0 + x1) / 2, datumY, z1 + 0.055);
labelAnchor.position.set((x0 + x1) / 2, 1.0 + verticalOffset, z1 - 0.35);
datumAnchor.position.set((x0 + x1) / 2, datumY, z1 + 0.12);
}
rebuild(0);
return {
id,
group,
rebuild,
x0,
x1,
labelAnchor,
datumAnchor
};
}
const blockA = createBlock({
id: "A",
x0: -5.65,
x1: -0.45,
seed: 2.4,
terrainColor: 0x71806d,
topMaterial: topMaterialA
});
const blockB = createBlock({
id: "B",
x0: 0.45,
x1: 5.65,
seed: 5.8,
terrainColor: 0x7d826a,
topMaterial: topMaterialB
});
const state = {
selected: "B",
A: { vertical: 0, horizontal: 0 },
B: { vertical: 0, horizontal: 0 }
};
const verticalInput = document.querySelector("#vertical");
const horizontalInput = document.querySelector("#horizontal");
const verticalValue = document.querySelector("#vertical-value");
const horizontalValue = document.querySelector("#horizontal-value");
const relativeVertical = document.querySelector("#relative-v");
const relativeHorizontal = document.querySelector("#relative-h");
const selectedName = document.querySelector("#selected-name");
const blockTabs = [...document.querySelectorAll(".block-tab")];
function signed(value) {
return `${value >= 0 ? "+" : ""}${value.toFixed(2)} u`;
}
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
function applyBlockTransform(block, values) {
block.group.position.x = values.horizontal;
block.rebuild(values.vertical);
}
function updateUI() {
const selected = state[state.selected];
verticalInput.value = selected.vertical;
horizontalInput.value = selected.horizontal;
verticalValue.textContent = signed(selected.vertical);
horizontalValue.textContent = signed(selected.horizontal);
relativeVertical.textContent = signed(state.B.vertical - state.A.vertical);
relativeHorizontal.textContent = signed(state.B.horizontal - state.A.horizontal);
selectedName.textContent = `Block ${state.selected}`;
blockTabs.forEach((tab) => {
tab.classList.toggle("active", tab.dataset.block === state.selected);
});
updateRangeFill(verticalInput);
updateRangeFill(horizontalInput);
}
function updateBlock(blockName) {
applyBlockTransform(blockA, state.A);
applyBlockTransform(blockB, state.B);
}
blockTabs.forEach((tab) => {
tab.addEventListener("click", () => {
state.selected = tab.dataset.block;
updateUI();
});
});
verticalInput.addEventListener("input", () => {
state[state.selected].vertical = Number(verticalInput.value);
updateBlock(state.selected);
updateUI();
});
horizontalInput.addEventListener("input", () => {
state[state.selected].horizontal = Number(horizontalInput.value);
updateBlock(state.selected);
updateUI();
});
document.querySelectorAll("[data-preset]").forEach((button) => {
button.addEventListener("click", () => {
const preset = button.dataset.preset;
if (preset === "camera") {
resetCamera();
return;
}
state.A.vertical = 0;
state.A.horizontal = 0;
state.B.vertical = 0;
state.B.horizontal = 0;
state.selected = "B";
if (preset === "normal") {
state.B.vertical = -0.75;
} else if (preset === "reverse") {
state.B.vertical = 0.75;
} else if (preset === "strike") {
state.B.horizontal = 0.45;
} else if (preset === "combined") {
state.B.vertical = 0.55;
state.B.horizontal = 0.38;
}
updateBlock("A");
updateBlock("B");
updateUI();
});
});
updateBlock("A");
updateBlock("B");
updateUI();
// Project DOM labels from world-space anchors.
const labels = {
a: document.querySelector("#label-a"),
b: document.querySelector("#label-b"),
datum: document.querySelector("#label-datum"),
fault: document.querySelector("#label-fault")
};
const faultAnchor = new THREE.Vector3(0, 0.9, 3.35);
const projected = new THREE.Vector3();
function placeLabel(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.display = visible ? "block" : "none";
if (!visible) return;
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`;
}
const worldPosition = new THREE.Vector3();
function updateLabels() {
blockA.labelAnchor.getWorldPosition(worldPosition);
placeLabel(labels.a, worldPosition);
blockB.labelAnchor.getWorldPosition(worldPosition);
placeLabel(labels.b, worldPosition);
blockB.datumAnchor.getWorldPosition(worldPosition);
placeLabel(labels.datum, worldPosition);
placeLabel(labels.fault, faultAnchor);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
const damping = 0.085;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * damping;
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * damping;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * damping;
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```