Prompt specification
Build or customize the Three.js 3D scene "Offset fault block pair" 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: Offset fault block pair
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/offset-fault-block-pair-x1141
Scene Description Prompt:
Slide two low-poly geological blocks along a fault, visibly offsetting matching colored strata. Make one HTML file using Three.js from cdnjs.
Working Three.js Starter (offset-fault-block-pair-x1141):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fault Lab — Geological Block Offset</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(10, 21, 30, 0.82);
--border: rgba(174, 207, 222, 0.16);
--text: #eef6f7;
--muted: #8ea6b1;
--accent: #e7ad55;
--marker: #42c7b0;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 40%, #142735 0%, #08131d 48%, #050b11 100%);
color: var(--text);
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;
outline: none;
}
.hud {
position: fixed;
z-index: 3;
pointer-events: none;
}
#intro {
top: 28px;
left: 30px;
max-width: 430px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 2px;
background: var(--accent);
box-shadow: 0 0 12px rgba(231, 173, 85, 0.65);
}
h1 {
margin: 0;
font-size: clamp(28px, 4vw, 52px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 750;
text-shadow: 0 8px 35px rgba(0, 0, 0, 0.4);
}
#intro p {
max-width: 390px;
margin: 14px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
#legend {
top: 28px;
right: 28px;
width: 218px;
padding: 16px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(14px);
}
.panel-title {
margin-bottom: 12px;
color: #d8e4e8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#legendItems {
display: grid;
gap: 7px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #aebfc6;
font-size: 11px;
}
.swatch {
width: 24px;
height: 9px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.12);
}
.legend-row.marker {
color: #bff7ee;
font-weight: 700;
}
.legend-note {
margin: 13px 0 0;
padding-top: 11px;
border-top: 1px solid rgba(255,255,255,0.08);
color: #6f8995;
font-size: 10px;
line-height: 1.45;
}
#controls {
left: 50%;
bottom: 26px;
width: min(690px, calc(100vw - 32px));
transform: translateX(-50%);
padding: 14px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.control-row {
display: flex;
align-items: center;
gap: 10px;
}
button {
min-height: 39px;
padding: 0 15px;
border: 1px solid rgba(255,255,255,0.12);
border-radius: 9px;
background: rgba(255,255,255,0.055);
color: #dbe8eb;
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.035em;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease, transform 160ms ease;
}
button:hover {
background: rgba(255,255,255,0.1);
border-color: rgba(255,255,255,0.22);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: rgba(231, 173, 85, 0.5);
background: linear-gradient(135deg, #dca14e, #b97835);
color: #111820;
box-shadow: 0 7px 22px rgba(213, 145, 61, 0.18);
}
button.primary:hover {
background: linear-gradient(135deg, #efb963, #c98742);
}
.slider-wrap {
display: flex;
flex: 1;
min-width: 160px;
align-items: center;
gap: 12px;
margin-left: 6px;
}
.slider-wrap label {
color: #8fa6b1;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
height: 4px;
accent-color: var(--accent);
cursor: pointer;
}
#offsetValue {
min-width: 98px;
color: var(--marker);
font-size: 11px;
font-variant-numeric: tabular-nums;
font-weight: 800;
text-align: right;
white-space: nowrap;
}
.help {
margin: 10px 3px 0;
color: #69838f;
font-size: 10px;
letter-spacing: 0.03em;
text-align: center;
}
.pin {
position: fixed;
z-index: 2;
top: 0;
left: 0;
padding: 6px 9px;
border: 1px solid rgba(255,255,255,0.13);
border-radius: 7px;
background: rgba(7, 16, 25, 0.78);
color: #cfdee3;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.13em;
white-space: nowrap;
pointer-events: none;
box-shadow: 0 8px 25px rgba(0,0,0,0.24);
backdrop-filter: blur(8px);
}
.pin::after {
content: "";
position: absolute;
left: 50%;
bottom: -12px;
width: 1px;
height: 12px;
background: rgba(255,255,255,0.35);
}
#leftPin {
border-left: 2px solid #7593a0;
}
#rightPin {
border-left: 2px solid var(--accent);
color: #f3c47e;
}
#loader {
position: fixed;
z-index: 10;
inset: 0;
display: grid;
place-items: center;
background: #071019;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
transition: opacity 350ms ease, visibility 350ms ease;
}
#loader.hidden {
opacity: 0;
visibility: hidden;
}
@media (max-width: 760px) {
#legend {
display: none;
}
#intro {
top: 20px;
left: 20px;
right: 20px;
}
#intro p {
max-width: 330px;
font-size: 11px;
}
#controls {
bottom: 14px;
padding: 10px;
}
.control-row {
flex-wrap: wrap;
}
.slider-wrap {
order: 3;
flex-basis: 100%;
margin: 4px 0 0;
}
button {
flex: 1;
padding: 0 10px;
font-size: 10px;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<div id="loader">Building geological model…</div>
<section id="intro" class="hud">
<div class="eyebrow">Structural geology / interactive model</div>
<h1>Fault Lab</h1>
<p>
Matching strata on either side of a dipping fault. Slide the hanging wall
to reveal the vertical displacement of the marker bed.
</p>
</section>
<aside id="legend" class="hud">
<div class="panel-title">Correlative strata</div>
<div id="legendItems"></div>
<p class="legend-note">
The turquoise bed occurs at the same level in both blocks before slip.
</p>
</aside>
<div id="controls" class="hud">
<div class="control-row">
<button id="slideButton" class="primary">Slide Along Fault</button>
<button id="autoButton">Auto Cycle</button>
<button id="resetButton">Reset</button>
<div class="slider-wrap">
<label for="slipRange">Fault slip</label>
<input
id="slipRange"
type="range"
min="0"
max="100"
value="0"
step="1"
aria-label="Fault slip amount"
/>
<div id="offsetValue" aria-live="polite">+0.00 m throw</div>
</div>
</div>
<div class="help">
Drag to orbit · Scroll to zoom · Double-click to reset the camera · Space to slide
</div>
</div>
<div id="leftPin" class="pin">FOOTWALL / FIXED</div>
<div id="rightPin" class="pin">HANGING WALL / SLIDING</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene setup
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 10, 24);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
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;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc8e9ff, 0x172028, 1.75);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedcf, 3.3);
sun.position.set(5, 9, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 24;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x62b9d7, 1.45);
fill.position.set(-5, 4, -5);
scene.add(fill);
const rim = new THREE.PointLight(0xe5a552, 22, 11, 2);
rim.position.set(0, 3.2, 4.5);
scene.add(rim);
// ------------------------------------------------------------
// Geological definition
// ------------------------------------------------------------
const layers = [
{ name: "Sandstone", color: "#d5aa58" },
{ name: "Siltstone", color: "#b76446" },
{ name: "Blue shale", color: "#52778b" },
{ name: "Marker bed", color: "#42c7b0" },
{ name: "Limestone", color: "#d2c9a8" },
{ name: "Basalt", color: "#3d4b56" },
{ name: "Greenstone", color: "#4f8875" },
{ name: "Breccia", color: "#965b6a" },
{ name: "Sandstone", color: "#aa7046" }
];
document.getElementById("legendItems").innerHTML = layers
.map(
(layer, index) => `
<div class="legend-row ${index === 3 ? "marker" : ""}">
<span class="swatch" style="background:${layer.color}"></span>
<span>${layer.name}</span>
</div>
`
)
.join("");
const BLOCK_BOTTOM = -0.78;
const LAYER_HEIGHT = 0.30;
const MARKER_LAYER = 3;
// Irregular, low-poly footprints. The inner edges form the fault gap.
const leftFootprint = [
[-1.56, -1.45],
[-1.34, -1.68],
[-0.16, -1.72],
[-0.16, 1.54],
[-0.46, 1.80],
[-1.42, 1.63],
[-1.67, 0.42]
];
const rightFootprint = leftFootprint
.map(([x, z]) => [-x, z])
.reverse();
// Shared, subtly folded interfaces. Mirrored x values keep the
// corresponding beds perfectly aligned across the fault at zero slip.
function surfaceY(x, z, boundary) {
if (boundary === 0) return BLOCK_BOTTOM;
const ax = Math.abs(x);
const wave =
0.026 * Math.sin(ax * 1.60 + boundary * 0.63) +
0.018 * Math.cos(z * 1.25 - boundary * 0.47) +
0.009 * Math.sin((ax + z) * 3.10 + boundary * 0.90);
return BLOCK_BOTTOM + boundary * LAYER_HEIGHT + wave;
}
function signedArea(points) {
let area = 0;
for (let i = 0; i < points.length; i++) {
const a = points[i];
const b = points[(i + 1) % points.length];
area += a[0] * b[1] - b[0] * a[1];
}
return area * 0.5;
}
function makePrismGeometry(points, layer, boundary, sideSign) {
if (signedArea(points) < 0) points = points.slice().reverse();
const contour = points.map(([x, z]) => new THREE.Vector2(x, z));
const triangles = THREE.ShapeUtils.triangulateShape(contour, []);
const positions = [];
const colors = [];
const baseColor = new THREE.Color(layer.color);
const white = new THREE.Color(0xffffff);
const black = new THREE.Color(0x000000);
let seed = 9377 + boundary * 131 + sideSign * 37;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function shade(amount) {
const color = baseColor.clone();
if (amount >= 0) color.lerp(white, amount);
else color.lerp(black, -amount);
return color;
}
function pushTriangle(a, b, c, color) {
positions.push(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
function lower(x, z) {
return new THREE.Vector3(x, surfaceY(x, z, boundary), z);
}
function upper(x, z) {
return new THREE.Vector3(x, surfaceY(x, z, boundary + 1), z);
}
// Top and bottom surfaces.
for (const triangle of triangles) {
const [a, b, c] = triangle;
pushTriangle(
upper(points[c][0], points[c][1]),
upper(points[b][0], points[b][1]),
upper(points[a][0], points[a][1]),
shade(0.10 + random() * 0.08)
);
pushTriangle(
lower(points[a][0], points[a][1]),
lower(points[b][0], points[b][1]),
lower(points[c][0], points[c][1]),
shade(-0.28)
);
}
// Faceted side walls.
for (let i = 0; i < points.length; i++) {
const j = (i + 1) % points.length;
const ax = points[i][0];
const az = points[i][1];
const bx = points[j][0];
const bz = points[j][1];
const colorA = shade(-0.055 + random() * 0.13);
const colorB = shade(-0.055 + random() * 0.13);
pushTriangle(lower(ax, az), upper(ax, az), upper(bx, bz), colorA);
pushTriangle(lower(ax, az), upper(bx, bz), lower(bx, bz), colorB);
}
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 createGeologicalBlock(points, sideSign) {
const group = new THREE.Group();
layers.forEach((layer, index) => {
const geometry = makePrismGeometry(
points,
layer,
index,
sideSign
);
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.88,
metalness: 0.015,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
const edgeColor = new THREE.Color(layer.color).multiplyScalar(0.55);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 18),
new THREE.LineBasicMaterial({
color: edgeColor,
transparent: true,
opacity: 0.38
})
);
group.add(edges);
});
return group;
}
const footwall = createGeologicalBlock(leftFootprint, -1);
const hangingWall = createGeologicalBlock(rightFootprint, 1);
scene.add(footwall, hangingWall);
// ------------------------------------------------------------
// Matching marker bed
// ------------------------------------------------------------
const markerMaterial = new THREE.MeshBasicMaterial({
color: 0xc9fff5
});
function markerY(x, z) {
return (
surfaceY(x, z, MARKER_LAYER) +
surfaceY(x, z, MARKER_LAYER + 1)
) * 0.5;
}
const leftMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 10, 8),
markerMaterial
);
leftMarker.position.set(-0.16, markerY(-0.16, 1.72), 1.735);
footwall.add(leftMarker);
const rightMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 10, 8),
markerMaterial
);
rightMarker.position.set(0.16, markerY(0.16, 1.72), 1.735);
hangingWall.add(rightMarker);
const offsetGeometry = new THREE.BufferGeometry();
offsetGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(new Float32Array(6), 3)
);
const offsetLine = new THREE.Line(
offsetGeometry,
new THREE.LineBasicMaterial({
color: 0xa8fff0,
transparent: true,
opacity: 0.85
})
);
offsetLine.frustumCulled = false;
scene.add(offsetLine);
// ------------------------------------------------------------
// Fault surface and slip-direction arrow
// ------------------------------------------------------------
const faultPlaneGeometry = new THREE.BufferGeometry();
faultPlaneGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
-0.12, -1.02, -1.80,
0.42, 2.55, -1.80,
0.42, 2.55, 1.69,
-0.12, -1.02, 1.69
], 3)
);
faultPlaneGeometry.setIndex([0, 2, 1, 0, 3, 2]);
faultPlaneGeometry.computeVertexNormals();
const faultPlane = new THREE.Mesh(
faultPlaneGeometry,
new THREE.MeshStandardMaterial({
color: 0x17212a,
roughness: 0.96,
metalness: 0.02,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.94
})
);
scene.add(faultPlane);
const faultTracePoints = [];
for (let i = 0; i <= 30; i++) {
const t = i / 30;
const y = THREE.MathUtils.lerp(-1.02, 2.55, t);
const x = THREE.MathUtils.lerp(-0.12, 0.42, t);
faultTracePoints.push(new THREE.Vector3(x, y, 1.76));
}
const faultTrace = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(faultTracePoints),
new THREE.LineDashedMaterial({
color: 0xe7ad55,
dashSize: 0.11,
gapSize: 0.07,
transparent: true,
opacity: 0.95
})
);
faultTrace.computeLineDistances();
scene.add(faultTrace);
// Tangent to the dipping fault plane.
const faultVector = new THREE.Vector3(0.15, 1.0, 0).normalize();
const slipArrow = new THREE.ArrowHelper(
faultVector,
new THREE.Vector3(-0.37, -0.64, 1.94),
1.34,
0xf0b65a,
0.18,
0.10
);
scene.add(slipArrow);
// ------------------------------------------------------------
// Display platform and scattered low-poly rocks
// ------------------------------------------------------------
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(5.35, 5.55, 0.42, 48),
new THREE.MeshStandardMaterial({
color: 0x101b23,
roughness: 0.92,
metalness: 0.05
})
);
platform.position.y = -1.04;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const platformTop = new THREE.Mesh(
new THREE.CylinderGeometry(5.28, 5.28, 0.06, 48),
new THREE.MeshStandardMaterial({
color: 0x172630,
roughness: 0.88,
metalness: 0.08
})
);
platformTop.position.y = -0.80;
platformTop.receiveShadow = true;
scene.add(platformTop);
const grid = new THREE.GridHelper(7.6, 16, 0x43606b, 0x304650);
grid.position.y = -0.765;
grid.material.transparent = true;
grid.material.opacity = 0.12;
grid.material.depthWrite = false;
scene.add(grid);
let rockSeed = 421;
function randomRock() {
rockSeed = (rockSeed * 1664525 + 1013904223) >>> 0;
return rockSeed / 4294967296;
}
const rockGeometry = new THREE.DodecahedronGeometry(0.28, 0);
for (let i = 0; i < 15; i++) {
const angle = randomRock() * Math.PI * 2;
const radius = 3.55 + randomRock() * 1.1;
const size = 0.5 + randomRock() * 1.25;
const rock = new THREE.Mesh(
rockGeometry,
new THREE.MeshStandardMaterial({
color: new THREE.Color().setHSL(
0.53 + randomRock() * 0.07,
0.08 + randomRock() * 0.08,
0.17 + randomRock() * 0.10
),
roughness: 0.98,
flatShading: true
})
);
rock.position.set(
Math.cos(angle) * radius,
-0.55 + size * 0.18,
Math.sin(angle) * radius
);
rock.scale.set(
size * (0.8 + randomRock() * 0.5),
size * (0.45 + randomRock() * 0.4),
size * (0.75 + randomRock() * 0.5)
);
rock.rotation.set(
randomRock() * Math.PI,
randomRock() * Math.PI,
randomRock() * Math.PI
);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
// ------------------------------------------------------------
// Labels anchored in world space
// ------------------------------------------------------------
const leftAnchor = new THREE.Object3D();
leftAnchor.position.set(-1.05, 2.25, 0.30);
footwall.add(leftAnchor);
const rightAnchor = new THREE.Object3D();
rightAnchor.position.set(1.05, 2.25, 0.30);
hangingWall.add(rightAnchor);
const leftPinElement = document.getElementById("leftPin");
const rightPinElement = document.getElementById("rightPin");
function positionPin(element, object) {
const world = object.getWorldPosition(new THREE.Vector3());
const projected = world.clone().project(camera);
if (projected.z > 1) {
element.style.opacity = "0";
return;
}
element.style.opacity = "1";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.transform =
`translate(${x}px, ${y}px) translate(-50%, -100%)`;
}
// ------------------------------------------------------------
// Direct camera controls: orbit, pan-free zoom, and reset
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.48, 0);
const cameraState = {
radius: 10.4,
theta: 0.57,
phi: 1.08
};
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position
.copy(cameraTarget)
.add(new THREE.Vector3().setFromSpherical(spherical));
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.radius = 10.4;
cameraState.theta = 0.57;
cameraState.phi = 1.08;
updateCamera();
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.34,
1.40
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
5.3,
17
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateCamera();
// ------------------------------------------------------------
// Fault-slip animation and UI
// ------------------------------------------------------------
const slipRange = document.getElementById("slipRange");
const offsetValue = document.getElementById("offsetValue");
const slideButton = document.getElementById("slideButton");
const autoButton = document.getElementById("autoButton");
const resetButton = document.getElementById("resetButton");
let currentSlip = 0;
let motion = null;
let autoEnabled = false;
let autoDirection = 1;
let autoHold = 0;
function applySlip() {
const displacement = faultVector
.clone()
.multiplyScalar(currentSlip * 1.06);
hangingWall.position.copy(displacement);
const verticalThrow = displacement.y;
offsetValue.textContent = `+${verticalThrow.toFixed(2)} m throw`;
slipRange.value = String(Math.round(currentSlip * 100));
}
function animateTo(value, duration = 1900) {
motion = {
from: currentSlip,
to: THREE.MathUtils.clamp(value, 0, 1),
start: performance.now(),
duration
};
}
function easeInOutCubic(t) {
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
}
slipRange.addEventListener("input", () => {
motion = null;
currentSlip = Number(slipRange.value) / 100;
applySlip();
});
slideButton.addEventListener("click", () => {
animateTo(currentSlip > 0.96 ? 0 : 1, 2200);
});
resetButton.addEventListener("click", () => {
autoEnabled = false;
autoHold = 0;
autoButton.textContent = "Auto Cycle";
animateTo(0, 1100);
});
autoButton.addEventListener("click", () => {
autoEnabled = !autoEnabled;
autoHold = 0;
if (autoEnabled) {
autoDirection = currentSlip > 0.5 ? 0 : 1;
animateTo(autoDirection, 1800);
autoButton.textContent = "Pause Cycle";
} else {
autoButton.textContent = "Auto Cycle";
}
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && !event.repeat) {
event.preventDefault();
animateTo(currentSlip > 0.96 ? 0 : 1, 2200);
}
});
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
const leftMarkerWorld = new THREE.Vector3();
const rightMarkerWorld = new THREE.Vector3();
function updateOffsetGuide() {
leftMarker.getWorldPosition(leftMarkerWorld);
rightMarker.getWorldPosition(rightMarkerWorld);
const attribute = offsetGeometry.getAttribute("position");
attribute.setXYZ(
0,
leftMarkerWorld.x,
leftMarkerWorld.y,
leftMarkerWorld.z + 0.025
);
attribute.setXYZ(
1,
rightMarkerWorld.x,
rightMarkerWorld.y,
rightMarkerWorld.z + 0.025
);
attribute.needsUpdate = true;
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (motion) {
const elapsed = performance.now() - motion.start;
const progress = THREE.MathUtils.clamp(
elapsed / motion.duration,
0,
1
);
currentSlip = THREE.MathUtils.lerp(
motion.from,
motion.to,
easeInOutCubic(progress)
);
if (progress >= 1) {
currentSlip = motion.to;
motion = null;
}
} else if (autoEnabled) {
autoHold += delta;
if (autoHold > 1.25) {
autoHold = 0;
autoDirection = autoDirection === 1 ? 0 : 1;
animateTo(autoDirection, 1900);
}
}
applySlip();
updateOffsetGuide();
positionPin(leftPinElement, leftAnchor);
positionPin(rightPinElement, rightAnchor);
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
document.getElementById("loader").classList.add("hidden");
});
</script>
</body>
</html>
```