Prompt specification
Build or customize the Three.js 3D scene "Road crossing a fault-model landscape" 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: Road crossing a fault-model landscape
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/road-crossing-a-fault-model-landscape-x1143
Scene Description Prompt:
Build a schematic road and stream across a movable fault boundary; lateral movement offsets both markers while a translucent plane exposes the underlying displacement. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (road-crossing-a-fault-model-landscape-x1143):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Fault Offset Lab</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(12, 24, 31, 0.82);
--panel-border: rgba(255, 255, 255, 0.13);
--text: #eef8f8;
--muted: #a9bec3;
--accent: #ffb547;
--cyan: #55d9e6;
--road: #39444d;
--stream: #2aaec2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e8;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
color: var(--text);
}
.glass {
background: var(--panel);
border: 1px solid var(--panel-border);
box-shadow: 0 18px 60px rgba(8, 22, 27, 0.22);
backdrop-filter: blur(16px) saturate(125%);
-webkit-backdrop-filter: blur(16px) saturate(125%);
}
.title-card {
position: absolute;
top: 22px;
left: 22px;
max-width: 430px;
padding: 17px 20px 16px;
border-radius: 16px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.live-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(21px, 3vw, 30px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.title-card p {
max-width: 390px;
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
#controls {
position: absolute;
left: 22px;
bottom: 22px;
width: min(370px, calc(100vw - 44px));
padding: 18px;
border-radius: 18px;
pointer-events: auto;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 17px;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status {
padding: 5px 9px;
border: 1px solid rgba(85, 217, 230, 0.3);
border-radius: 999px;
background: rgba(85, 217, 230, 0.09);
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.1em;
}
.control {
margin-top: 16px;
}
.control-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
}
.control label {
font-size: 13px;
font-weight: 650;
}
output {
color: var(--accent);
font-variant-numeric: tabular-nums;
font-size: 13px;
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
appearance: none;
-webkit-appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(
90deg,
var(--accent) 0%,
var(--accent) var(--fill, 50%),
rgba(255, 255, 255, 0.13) var(--fill, 50%),
rgba(255, 255, 255, 0.13) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
-webkit-appearance: none;
border: 3px solid #14262d;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px rgba(255, 181, 71, 0.45);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #14262d;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px rgba(255, 181, 71, 0.45);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 19px;
}
button {
min-height: 39px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 10px;
background: rgba(255, 255, 255, 0.075);
color: var(--text);
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(255, 255, 255, 0.28);
background: rgba(255, 255, 255, 0.13);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: rgba(255, 181, 71, 0.4);
background: rgba(255, 181, 71, 0.15);
color: #ffd58e;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 14px;
}
.metric {
padding: 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.metric span {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric strong {
font-size: 17px;
font-variant-numeric: tabular-nums;
}
#legend {
position: absolute;
top: 22px;
right: 22px;
width: 205px;
padding: 15px 16px;
border-radius: 15px;
}
#legend .section-title {
margin-bottom: 12px;
}
.legend-item {
display: flex;
align-items: center;
gap: 10px;
margin-top: 9px;
color: var(--muted);
font-size: 12px;
}
.swatch {
width: 17px;
height: 17px;
flex: 0 0 auto;
border-radius: 4px;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16);
}
.swatch.fixed {
background: #788d73;
}
.swatch.moving {
background: #647e72;
border: 1px dashed #ffb547;
}
.swatch.road {
background: var(--road);
}
.swatch.stream {
background: var(--stream);
}
.swatch.fault {
border: 1px solid var(--cyan);
background: rgba(85, 217, 230, 0.2);
}
#help {
position: absolute;
right: 22px;
bottom: 22px;
max-width: 330px;
padding: 11px 14px;
border-radius: 12px;
color: #d8e7e9;
font-size: 11px;
line-height: 1.55;
text-align: right;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #dce8e8;
color: #193038;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.title-card {
top: 12px;
left: 12px;
right: 12px;
max-width: none;
padding: 13px 15px;
}
.title-card p {
display: none;
}
#controls {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
#legend,
#help {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="hud">
<header class="title-card glass">
<div class="eyebrow">
<span class="live-dot"></span>
Interactive tectonic model
</div>
<h1>Voxel Fault Offset Lab</h1>
<p>
Move the translucent fault boundary and apply lateral slip. The road and
stream markers offset together while the transparent plane exposes the
displacement between the fixed and moving blocks.
</p>
</header>
<section id="controls" class="glass">
<div class="section-title">
Fault controls
<span id="status" class="status">LOCKED</span>
</div>
<div class="control">
<div class="control-row">
<label for="faultSlider">Fault boundary</label>
<output id="faultValue">−1.25 m</output>
</div>
<input
id="faultSlider"
type="range"
min="-6"
max="6"
step="0.25"
value="-1.25"
aria-label="Fault boundary position"
/>
</div>
<div class="control">
<div class="control-row">
<label for="slipSlider">Lateral slip</label>
<output id="slipValue">+0.0 m</output>
</div>
<input
id="slipSlider"
type="range"
min="0"
max="6"
step="0.1"
value="0"
aria-label="Lateral slip"
/>
</div>
<div class="buttons">
<button id="playButton" class="primary" type="button">Run motion</button>
<button id="resetButton" type="button">Reset model</button>
</div>
<div class="metrics">
<div class="metric">
<span>Road marker</span>
<strong id="roadOffset">+0.0 m</strong>
</div>
<div class="metric">
<span>Stream marker</span>
<strong id="streamOffset">+0.0 m</strong>
</div>
</div>
</section>
<aside id="legend" class="glass">
<div class="section-title">Scene legend</div>
<div class="legend-item">
<span class="swatch fixed"></span>
Fixed block
</div>
<div class="legend-item">
<span class="swatch moving"></span>
Moving block
</div>
<div class="legend-item">
<span class="swatch road"></span>
Road
</div>
<div class="legend-item">
<span class="swatch stream"></span>
Stream
</div>
<div class="legend-item">
<span class="swatch fault"></span>
Translucent fault plane
</div>
</aside>
<div id="help" class="glass">
Drag the scene to orbit · wheel/pinch to zoom · right-drag to pan ·
drag the orange fault handle to move the boundary
</div>
</div>
<div id="loading">Building voxel terrain…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and scene setup
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const loading = document.getElementById("loading");
const faultSlider = document.getElementById("faultSlider");
const slipSlider = document.getElementById("slipSlider");
const faultValue = document.getElementById("faultValue");
const slipValue = document.getElementById("slipValue");
const roadOffset = document.getElementById("roadOffset");
const streamOffset = document.getElementById("streamOffset");
const playButton = document.getElementById("playButton");
const resetButton = document.getElementById("resetButton");
const status = document.getElementById("status");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8e8);
scene.fog = new THREE.Fog(0xdce8e8, 48, 95);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
150
);
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
} catch (error) {
loading.textContent = "WebGL could not be initialized.";
throw error;
}
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(
0xe9fbff,
0x657368,
2.15
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0d3, 3.4);
sun.position.set(19, 31, 16);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -32;
sun.shadow.camera.right = 32;
sun.shadow.camera.top = 30;
sun.shadow.camera.bottom = -30;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 80;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x91dce7, 1.1);
fill.position.set(-20, 13, -18);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared geometry and materials
// -------------------------------------------------------------------------
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9,
metalness: 0,
vertexColors: true
});
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x74523a,
roughness: 1
});
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0x315f4b,
roughness: 0.95
});
const leafLightMaterial = new THREE.MeshStandardMaterial({
color: 0x4e8057,
roughness: 0.95
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x263b43,
roughness: 0.84,
metalness: 0.08
});
const roadMarkerMaterial = new THREE.MeshStandardMaterial({
color: 0xffb547,
emissive: 0x6b3100,
emissiveIntensity: 0.38,
roughness: 0.48
});
const streamMarkerMaterial = new THREE.MeshStandardMaterial({
color: 0x55d9e6,
emissive: 0x063c46,
emissiveIntensity: 0.5,
roughness: 0.4
});
// -------------------------------------------------------------------------
// Model constants and state
// -------------------------------------------------------------------------
const CELL = 1.25;
const GRID_X_MIN = -12;
const GRID_X_MAX = 12;
const GRID_Z_MIN = -9;
const GRID_Z_MAX = 9;
const ROAD_Z = -3 * CELL;
const STREAM_Z = 4 * CELL;
const MARKER_X = 7.5;
const MAX_SLIP = 6;
const state = {
fault: -1.25,
slip: 0,
playing: false,
phase: 0,
faultDirty: false
};
const leftPlate = new THREE.Group();
const rightPlate = new THREE.Group();
const movingMarkers = new THREE.Group();
const faultGroup = new THREE.Group();
scene.add(leftPlate, rightPlate, movingMarkers, faultGroup);
// -------------------------------------------------------------------------
// Platform
// -------------------------------------------------------------------------
const base = new THREE.Mesh(
new THREE.BoxGeometry(48, 0.8, 32),
baseMaterial
);
base.position.y = -0.42;
base.receiveShadow = true;
scene.add(base);
const baseEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(base.geometry),
new THREE.LineBasicMaterial({
color: 0x78949b,
transparent: true,
opacity: 0.38
})
);
baseEdges.position.copy(base.position);
scene.add(baseEdges);
// -------------------------------------------------------------------------
// Voxel terrain generation
// -------------------------------------------------------------------------
function hash2(x, z) {
const value = Math.sin(x * 127.1 + z * 311.7) * 43758.5453123;
return value - Math.floor(value);
}
function makeCell(ix, iz) {
const x = ix * CELL;
const z = iz * CELL;
const random = hash2(ix, iz);
const random2 = hash2(ix + 31, iz - 17);
const isRoad = iz >= -4 && iz <= -2;
const isStream = iz >= 3 && iz <= 5;
const isBank = iz === 2 || iz === 6;
let height = 0.62 + random * 0.55;
let color;
if (isRoad) {
height = 0.52;
color = new THREE.Color(0x354149).offsetHSL(
0,
0,
(random2 - 0.5) * 0.045
);
} else if (isStream) {
height = 0.4;
color = new THREE.Color(0x20a8bc).offsetHSL(
0,
(random2 - 0.5) * 0.05,
(random2 - 0.5) * 0.06
);
} else if (isBank) {
height = 0.55 + random * 0.12;
color = new THREE.Color(0x8a8068).offsetHSL(
0,
0,
(random2 - 0.5) * 0.07
);
} else {
color = new THREE.Color(0x72866e).offsetHSL(
(random2 - 0.5) * 0.015,
(random - 0.5) * 0.08,
(random2 - 0.5) * 0.09
);
}
return {
ix,
iz,
x,
z,
h: height,
color,
isRoad,
isStream,
roadStripe:
isRoad &&
iz === -3 &&
((ix + 30) % 3 !== 0),
streamGlint:
isStream &&
iz === 4 &&
ix % 2 === 0,
hasTree:
!isRoad &&
!isStream &&
!isBank &&
random > 0.875
};
}
function clearGroup(group) {
for (const child of [...group.children]) {
group.remove(child);
if (child.isInstancedMesh && typeof child.dispose === "function") {
child.dispose();
}
}
}
function buildVoxelPlate(group, cells) {
clearGroup(group);
const terrain = new THREE.InstancedMesh(
cubeGeometry,
terrainMaterial,
cells.length
);
terrain.castShadow = true;
terrain.receiveShadow = true;
const dummy = new THREE.Object3D();
cells.forEach((cell, index) => {
dummy.position.set(cell.x, cell.h / 2, cell.z);
dummy.scale.set(CELL * 0.94, cell.h, CELL * 0.94);
dummy.updateMatrix();
terrain.setMatrixAt(index, dummy.matrix);
terrain.setColorAt(index, cell.color);
});
terrain.instanceMatrix.needsUpdate = true;
if (terrain.instanceColor) {
terrain.instanceColor.needsUpdate = true;
}
group.add(terrain);
// Dashed yellow road centerline.
const roadStripeCells = cells.filter((cell) => cell.roadStripe);
if (roadStripeCells.length) {
const stripes = new THREE.InstancedMesh(
cubeGeometry,
terrainMaterial,
roadStripeCells.length
);
roadStripeCells.forEach((cell, index) => {
dummy.position.set(cell.x, cell.h + 0.055, cell.z);
dummy.scale.set(0.78, 0.075, 0.13);
dummy.updateMatrix();
stripes.setMatrixAt(index, dummy.matrix);
stripes.setColorAt(index, new THREE.Color(0xf4c85b));
});
stripes.instanceMatrix.needsUpdate = true;
if (stripes.instanceColor) {
stripes.instanceColor.needsUpdate = true;
}
group.add(stripes);
}
// Small pale-blue stream glints.
const glintCells = cells.filter((cell) => cell.streamGlint);
if (glintCells.length) {
const glints = new THREE.InstancedMesh(
cubeGeometry,
terrainMaterial,
glintCells.length
);
glintCells.forEach((cell, index) => {
dummy.position.set(cell.x, cell.h + 0.045, cell.z);
dummy.scale.set(0.63, 0.06, 0.09);
dummy.updateMatrix();
glints.setMatrixAt(index, dummy.matrix);
glints.setColorAt(index, new THREE.Color(0xb2f3f2));
});
glints.instanceMatrix.needsUpdate = true;
if (glints.instanceColor) {
glints.instanceColor.needsUpdate = true;
}
group.add(glints);
}
// Voxel trees.
for (const cell of cells) {
if (!cell.hasTree) continue;
const trunk = new THREE.Mesh(cubeGeometry, trunkMaterial);
trunk.position.set(cell.x, cell.h + 0.62, cell.z);
trunk.scale.set(0.31, 1.24, 0.31);
trunk.castShadow = true;
group.add(trunk);
const canopy = new THREE.Mesh(cubeGeometry, leafMaterial);
canopy.position.set(cell.x, cell.h + 1.55, cell.z);
canopy.scale.set(1.18, 1.28, 1.18);
canopy.castShadow = true;
group.add(canopy);
const crown = new THREE.Mesh(cubeGeometry, leafLightMaterial);
crown.position.set(cell.x + 0.12, cell.h + 2.22, cell.z - 0.08);
crown.scale.set(0.78, 0.7, 0.78);
crown.castShadow = true;
group.add(crown);
}
}
function rebuildPlates() {
const leftCells = [];
const rightCells = [];
for (let ix = GRID_X_MIN; ix <= GRID_X_MAX; ix++) {
for (let iz = GRID_Z_MIN; iz <= GRID_Z_MAX; iz++) {
const cell = makeCell(ix, iz);
if (cell.x >= state.fault) {
rightCells.push(cell);
} else {
leftCells.push(cell);
}
}
}
buildVoxelPlate(leftPlate, leftCells);
buildVoxelPlate(rightPlate, rightCells);
state.faultDirty = false;
}
// -------------------------------------------------------------------------
// Text sprites
// -------------------------------------------------------------------------
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeTextSprite(initialText, accent = "#ffb547", width = 5.2) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width / 4, 1);
sprite.renderOrder = 30;
function update(text) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRectPath(ctx, 5, 12, 502, 104, 22);
ctx.fillStyle = "rgba(11, 25, 32, 0.88)";
ctx.fill();
ctx.lineWidth = 3;
ctx.strokeStyle = accent;
ctx.stroke();
ctx.fillStyle = "#f4ffff";
ctx.font = "700 35px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 65, 470);
texture.needsUpdate = true;
}
update(initialText);
return { sprite, update };
}
// -------------------------------------------------------------------------
// Translucent fault boundary
// -------------------------------------------------------------------------
const faultPlane = new THREE.Mesh(
new THREE.PlaneGeometry(27, 7),
new THREE.MeshBasicMaterial({
color: 0x48d9e2,
transparent: true,
opacity: 0.19,
side: THREE.DoubleSide,
depthWrite: false
})
);
faultPlane.rotation.y = Math.PI / 2;
faultPlane.position.y = 3.5;
faultPlane.renderOrder = 3;
faultGroup.add(faultPlane);
const faultEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(faultPlane.geometry),
new THREE.LineBasicMaterial({
color: 0xb8ffff,
transparent: true,
opacity: 0.58
})
);
faultEdges.position.copy(faultPlane.position);
faultEdges.rotation.copy(faultPlane.rotation);
faultEdges.renderOrder = 4;
faultGroup.add(faultEdges);
const hatchPositions = [];
for (let z = -13; z <= 12; z++) {
hatchPositions.push(
0, 0.2, z,
0, 6.8, Math.min(z + 1.15, 13.4)
);
}
const hatchGeometry = new THREE.BufferGeometry();
hatchGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(hatchPositions, 3)
);
const hatchLines = new THREE.LineSegments(
hatchGeometry,
new THREE.LineBasicMaterial({
color: 0xb9ffff,
transparent: true,
opacity: 0.16
})
);
faultGroup.add(hatchLines);
const faultGroundLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0.63, -13.4),
new THREE.Vector3(0, 0.63, 13.4)
]),
new THREE.LineBasicMaterial({
color: 0xffc261,
transparent: true,
opacity: 0.92
})
);
faultGroup.add(faultGroundLine);
const faultHandle = new THREE.Mesh(
cubeGeometry,
new THREE.MeshStandardMaterial({
color: 0xffb547,
emissive: 0x7a3500,
emissiveIntensity: 0.45,
roughness: 0.4
})
);
faultHandle.position.set(0, 7.35, 0);
faultHandle.scale.set(0.9, 0.62, 0.9);
faultHandle.castShadow = true;
faultHandle.userData.faultHandle = true;
faultGroup.add(faultHandle);
const handleRing = new THREE.Mesh(
new THREE.TorusGeometry(0.73, 0.075, 8, 28),
new THREE.MeshBasicMaterial({ color: 0xffd183 })
);
handleRing.rotation.x = Math.PI / 2;
handleRing.position.set(0, 7.35, 0);
faultGroup.add(handleRing);
const faultLabel = makeTextSprite("FAULT BOUNDARY", "#55d9e6", 4.8);
faultLabel.sprite.position.set(0, 8.45, 0);
faultGroup.add(faultLabel.sprite);
// Orange outline showing the displaced position of the fault seam.
const displacedEdge = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0.08, -13.4),
new THREE.Vector3(0, 7, -13.4),
new THREE.Vector3(0, 7, 13.4),
new THREE.Vector3(0, 0.08, 13.4)
]),
new THREE.LineBasicMaterial({
color: 0xffb547,
transparent: true,
opacity: 0.85
})
);
scene.add(displacedEdge);
// -------------------------------------------------------------------------
// Markers, reference positions, and displacement arrows
// -------------------------------------------------------------------------
function createMarker(z, material, label, accent) {
const group = new THREE.Group();
group.position.z = z;
const pole = new THREE.Mesh(cubeGeometry, material);
pole.position.y = 1.35;
pole.scale.set(0.15, 1.7, 0.15);
pole.castShadow = true;
group.add(pole);
const head = new THREE.Mesh(cubeGeometry, material);
head.position.y = 2.36;
head.scale.set(0.62, 0.62, 0.62);
head.castShadow = true;
group.add(head);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.72, 0.09, 8, 28),
material
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.67;
group.add(ring);
const text = makeTextSprite(label, accent, 5.2);
text.sprite.position.set(0, 3.5, 0);
group.add(text.sprite);
movingMarkers.add(group);
return { group, text };
}
const roadMarker = createMarker(
ROAD_Z,
roadMarkerMaterial,
"ROAD MARKER +0.0 m",
"#ffb547"
);
const streamMarker = createMarker(
STREAM_Z,
streamMarkerMaterial,
"STREAM MARKER +0.0 m",
"#55d9e6"
);
function createGhostMarker(z, color) {
const group = new THREE.Group();
group.position.set(MARKER_X, 0, z);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.48,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.72, 0.045, 6, 24),
material
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.66;
group.add(ring);
const crossGeometry = new THREE.BoxGeometry(0.75, 0.035, 0.07);
const crossA = new THREE.Mesh(crossGeometry, material);
const crossB = new THREE.Mesh(crossGeometry, material);
crossB.rotation.y = Math.PI / 2;
crossA.position.y = 0.66;
crossB.position.y = 0.66;
group.add(crossA, crossB);
scene.add(group);
return group;
}
createGhostMarker(ROAD_Z, 0xffd183);
createGhostMarker(STREAM_Z, 0xb8ffff);
function createArrow(z, color) {
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(MARKER_X, 1.02, z),
0.001,
color,
0.62,
0.3
);
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.94;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.94;
scene.add(arrow);
return arrow;
}
const roadArrow = createArrow(ROAD_Z, 0xffb547);
const streamArrow = createArrow(STREAM_Z, 0x55d9e6);
const roadName = makeTextSprite("ROAD", "#aeb8bd", 3.1);
roadName.sprite.position.set(-10.5, 2.65, ROAD_Z);
scene.add(roadName.sprite);
const streamName = makeTextSprite("STREAM", "#55d9e6", 3.1);
streamName.sprite.position.set(-10.5, 2.55, STREAM_Z);
scene.add(streamName.sprite);
// -------------------------------------------------------------------------
// Custom camera controls: orbit, zoom, pan, and fault-handle dragging
// -------------------------------------------------------------------------
const cameraState = {
theta: 0.72,
phi: 1.03,
radius: 47,
target: new THREE.Vector3(1, 0, 0),
desiredTheta: 0.72,
desiredPhi: 1.03,
desiredRadius: 47,
desiredTarget: new THREE.Vector3(1, 0, 0)
};
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
const dragPlane = new THREE.Plane(
new THREE.Vector3(0, 1, 0),
-7.35
);
const dragPoint = new THREE.Vector3();
let pointerMode = null;
let lastPointerX = 0;
let lastPointerY = 0;
let faultDragOffset = 0;
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pointerHitsFaultHandle() {
scene.updateMatrixWorld(true);
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObject(faultHandle, false).length > 0;
}
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 9);
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
cameraState.desiredTheta,
smoothing
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
cameraState.desiredPhi,
smoothing
);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraState.desiredRadius,
smoothing
);
cameraState.target.lerp(
cameraState.desiredTarget,
smoothing
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
event.preventDefault();
updatePointer(event);
renderer.domElement.setPointerCapture(event.pointerId);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (event.button === 0 && pointerHitsFaultHandle()) {
pointerMode = "fault";
renderer.domElement.style.cursor = "grabbing";
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
faultDragOffset = state.fault - dragPoint.x;
} else {
faultDragOffset = 0;
}
} else if (event.button === 2 || event.button === 1) {
pointerMode = "pan";
renderer.domElement.style.cursor = "grabbing";
} else {
pointerMode = "orbit";
renderer.domElement.style.cursor = "grabbing";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
updatePointer(event);
if (pointerMode === "fault") {
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
state.fault = THREE.MathUtils.clamp(
dragPoint.x + faultDragOffset,
-6,
6
);
state.faultDirty = true;
}
return;
}
if (pointerMode === "orbit") {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraState.desiredTheta -= dx * 0.006;
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi - dy * 0.005,
0.25,
1.43
);
} else if (pointerMode === "pan") {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0);
right.y = 0;
right.normalize();
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const panScale = cameraState.radius * 0.0017;
cameraState.desiredTarget.addScaledVector(
right,
-dx * panScale
);
cameraState.desiredTarget.addScaledVector(
forward,
dy * panScale
);
} else {
renderer.domElement.style.cursor = pointerHitsFaultHandle()
? "grab"
: "default";
}
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
function releasePointer(event) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
pointerMode = null;
renderer.domElement.style.cursor = pointerHitsFaultHandle()
? "grab"
: "default";
}
renderer.domElement.addEventListener(
"pointerup",
releasePointer
);
renderer.domElement.addEventListener(
"pointercancel",
releasePointer
);
renderer.domElement.addEventListener(
"contextmenu",
(event) => event.preventDefault()
);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius *
Math.exp(event.deltaY * 0.001),
25,
72
);
},
{ passive: false }
);
// Pinch zoom for touch devices.
let pinchDistance = 0;
renderer.domElement.addEventListener(
"touchstart",
(event) => {
if (event.touches.length === 2) {
const a = event.touches[0];
const b = event.touches[1];
pinchDistance = Math.hypot(
a.clientX - b.clientX,
a.clientY - b.clientY
);
}
},
{ passive: true }
);
renderer.domElement.addEventListener(
"touchmove",
(event) => {
if (event.touches.length === 2 && pinchDistance > 0) {
event.preventDefault();
const a = event.touches[0];
const b = event.touches[1];
const distance = Math.hypot(
a.clientX - b.clientX,
a.clientY - b.clientY
);
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius *
(pinchDistance / Math.max(distance, 1)),
25,
72
);
pinchDistance = distance;
}
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Controls
// -------------------------------------------------------------------------
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percentage = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percentage}%`);
}
faultSlider.addEventListener("input", () => {
state.fault = Number(faultSlider.value);
state.faultDirty = true;
updateRangeFill(faultSlider);
});
slipSlider.addEventListener("input", () => {
state.slip = Number(slipSlider.value);
state.playing = false;
updateRangeFill(slipSlider);
});
function toggleMotion() {
if (state.playing) {
state.playing = false;
} else {
const normalized = THREE.MathUtils.clamp(
state.slip / MAX_SLIP,
0,
1
);
state.phase = Math.acos(1 - 2 * normalized);
state.playing = true;
}
}
function resetModel() {
state.fault = -1.25;
state.slip = 0;
state.playing = false;
state.phase = 0;
state.faultDirty = true;
faultSlider.value = String(state.fault);
slipSlider.value = "0";
updateRangeFill(faultSlider);
updateRangeFill(slipSlider);
cameraState.desiredTheta = 0.72;
cameraState.desiredPhi = 1.03;
cameraState.desiredRadius = 47;
cameraState.desiredTarget.set(1, 0, 0);
}
playButton.addEventListener("click", toggleMotion);
resetButton.addEventListener("click", resetModel);
window.addEventListener("keydown", (event) => {
const activeTag = document.activeElement?.tagName;
if (activeTag === "INPUT" || activeTag === "BUTTON") return;
if (event.code === "Space") {
event.preventDefault();
toggleMotion();
} else if (event.key.toLowerCase() === "r") {
resetModel();
} else if (event.key === "ArrowLeft") {
cameraState.desiredTheta += 0.08;
} else if (event.key === "ArrowRight") {
cameraState.desiredTheta -= 0.08;
} else if (event.key === "+" || event.key === "=") {
cameraState.desiredRadius = Math.max(
25,
cameraState.desiredRadius - 2
);
} else if (event.key === "-") {
cameraState.desiredRadius = Math.min(
72,
cameraState.desiredRadius + 2
);
}
});
// -------------------------------------------------------------------------
// Scene synchronization and animation
// -------------------------------------------------------------------------
let lastLabelSlip = -999;
function applySceneState() {
faultGroup.position.x = state.fault;
rightPlate.position.x = state.slip;
movingMarkers.position.x = state.slip;
displacedEdge.position.x = state.fault + state.slip;
const arrowLength = Math.max(0.001, state.slip);
roadArrow.setLength(arrowLength, 0.62, 0.3);
streamArrow.setLength(arrowLength, 0.62, 0.3);
roadArrow.visible = state.slip > 0.025;
streamArrow.visible = state.slip > 0.025;
if (Math.abs(state.slip - lastLabelSlip) > 0.005) {
const sign = state.slip >= 0 ? "+" : "−";
roadMarker.text.update(
`ROAD MARKER ${sign}${state.slip.toFixed(1)} m`
);
streamMarker.text.update(
`STREAM MARKER ${sign}${state.slip.toFixed(1)} m`
);
lastLabelSlip = state.slip;
}
}
function updateHUD() {
faultSlider.value = String(state.fault);
slipSlider.value = String(state.slip);
updateRangeFill(faultSlider);
updateRangeFill(slipSlider);
const faultSign = state.fault >= 0 ? "+" : "−";
const slipSign = state.slip >= 0 ? "+" : "−";
faultValue.textContent = `${faultSign}${Math.abs(
state.fault
).toFixed(2)} m`;
slipValue.textContent = `${slipSign}${Math.abs(
state.slip
).toFixed(1)} m`;
roadOffset.textContent = `${slipSign}${Math.abs(
state.slip
).toFixed(1)} m`;
streamOffset.textContent = `${slipSign}${Math.abs(
state.slip
).toFixed(1)} m`;
playButton.textContent = state.playing
? "Pause motion"
: "Run motion";
status.textContent = state.playing ? "MOVING" : "LOCKED";
}
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(0.05, (now - previousTime) / 1000);
previousTime = now;
if (state.playing) {
state.phase += dt * 0.56;
state.slip =
MAX_SLIP * (0.5 - 0.5 * Math.cos(state.phase));
}
if (state.faultDirty) {
rebuildPlates();
}
updateCamera(dt);
applySceneState();
updateHUD();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
rebuildPlates();
applySceneState();
updateCamera(1);
updateHUD();
renderer.render(scene, camera);
loading.classList.add("hidden");
requestAnimationFrame(animate);
</script>
</body>
</html>
```