Prompt specification
Build or customize the Three.js 3D scene "Loess gully wall and soil-layer fragment" 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: Loess gully wall and soil-layer fragment
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/loess-gully-wall-and-soil-layer-fragment-x3057
Scene Description Prompt:
Loess gully wall and soil-layer fragment. Build a referenced loess cutaway with fine sediment layers and a small eroded channel. Distinguish unconsolidated material from solid bedrock using geometry and surface treatment. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (loess-gully-wall-and-soil-layer-fragment-x3057):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Loess Gully Cutaway</title>
<style>
:root {
--ink: #302d27;
--muted: #71695d;
--paper: rgba(247, 242, 231, 0.9);
--line: rgba(74, 65, 52, 0.26);
--loess: #c99350;
--rock: #59615f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e1d2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#view {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#view.dragging {
cursor: grabbing;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 52% 44%, transparent 45%,
rgba(65, 52, 36, 0.09) 100%);
z-index: 1;
}
header {
position: fixed;
z-index: 4;
top: 24px;
left: 28px;
max-width: 390px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 4vw, 48px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
max-width: 330px;
margin: 13px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#legend {
position: fixed;
z-index: 4;
right: 24px;
bottom: 24px;
width: 222px;
padding: 15px 16px 14px;
border: 1px solid var(--line);
border-radius: 11px;
background: var(--paper);
box-shadow: 0 12px 30px rgba(58, 45, 28, 0.08);
backdrop-filter: blur(10px);
pointer-events: none;
}
#legend .legend-title {
margin-bottom: 10px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 20px 1fr;
gap: 9px;
align-items: center;
margin-top: 8px;
font-size: 11px;
}
.swatch {
width: 18px;
height: 11px;
border: 1px solid rgba(37, 32, 25, 0.15);
border-radius: 2px;
}
.swatch.loess {
background:
linear-gradient(
to bottom,
#d7a45d 0 25%,
#e3b66e 25% 50%,
#c58b4d 50% 75%,
#9a653d 75%
);
}
.swatch.rock {
background: #59615f;
clip-path: polygon(0 16%, 22% 0, 48% 13%, 73% 4%, 100% 18%,
100% 84%, 78% 100%, 51% 88%, 25% 100%, 0 82%);
}
#controls-hint {
position: fixed;
z-index: 4;
left: 50%;
bottom: 24px;
transform: translateX(-50%);
padding: 8px 12px;
border: 1px solid rgba(74, 65, 52, 0.18);
border-radius: 999px;
background: rgba(247, 242, 231, 0.72);
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
backdrop-filter: blur(8px);
}
#leaders {
position: fixed;
inset: 0;
z-index: 2;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#leaders line {
stroke: rgba(55, 48, 38, 0.58);
stroke-width: 1;
stroke-dasharray: 3 4;
}
#leaders circle {
fill: #f7f2e7;
stroke: rgba(55, 48, 38, 0.7);
stroke-width: 1;
}
.callout {
position: fixed;
z-index: 3;
top: 0;
left: 0;
max-width: 190px;
padding: 7px 9px;
border: 1px solid rgba(66, 57, 45, 0.25);
border-radius: 7px;
background: rgba(248, 244, 234, 0.88);
box-shadow: 0 7px 20px rgba(53, 40, 25, 0.08);
color: #39342c;
font-size: 10px;
font-weight: 650;
line-height: 1.3;
letter-spacing: 0.03em;
pointer-events: none;
backdrop-filter: blur(8px);
white-space: nowrap;
}
@media (max-width: 720px) {
header {
top: 17px;
left: 18px;
}
.subtitle,
#controls-hint,
#leaders,
.callout {
display: none;
}
#legend {
right: 14px;
bottom: 14px;
width: 185px;
}
}
</style>
</head>
<body>
<div id="vignette"></div>
<header>
<div class="eyebrow">Geomorphic specimen · schematic section</div>
<h1>Loess gully<br>cutaway</h1>
<p class="subtitle">
Fine, unconsolidated wind-deposited silt overlies a darker,
fractured bedrock foundation. A shallow sinuous channel has
incised the exposed soil layers.
</p>
</header>
<svg id="leaders" aria-hidden="true">
<line id="line-channel"></line>
<circle id="dot-channel" r="3.5"></circle>
<line id="line-strata"></line>
<circle id="dot-strata" r="3.5"></circle>
<line id="line-bedrock"></line>
<circle id="dot-bedrock" r="3.5"></circle>
</svg>
<div class="callout" id="label-channel">Small eroded channel</div>
<div class="callout" id="label-strata">Unconsolidated fine silt</div>
<div class="callout" id="label-bedrock">Solid, fractured bedrock</div>
<aside id="legend">
<div class="legend-title">Material key</div>
<div class="legend-row">
<span class="swatch loess"></span>
<span>Unconsolidated loess strata</span>
</div>
<div class="legend-row">
<span class="swatch rock"></span>
<span>Compact bedrock</span>
</div>
</aside>
<div id="controls-hint">
Drag to orbit · Scroll to zoom · Double-click to reset
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.domElement.id = "view";
document.body.prepend(renderer.domElement);
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e1d2);
scene.fog = new THREE.Fog(0xe9e1d2, 18, 34);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const hemi = new THREE.HemisphereLight(
0xfff3dc,
0x71675b,
2.15
);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff0d2, 3.25);
key.position.set(7, 11, 8);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -10;
key.shadow.camera.right = 10;
key.shadow.camera.top = 10;
key.shadow.camera.bottom = -10;
key.shadow.camera.near = 1;
key.shadow.camera.far = 32;
key.shadow.bias = -0.00025;
key.shadow.normalBias = 0.025;
scene.add(key);
const fill = new THREE.DirectionalLight(0xc6d7df, 0.9);
fill.position.set(-7, 4, -5);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0xd8cebd,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.12;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------
// Shared dimensions and terrain functions
// ------------------------------------------------------------
const SOIL_X_MIN = -4.85;
const SOIL_X_MAX = 4.85;
const SOIL_Z_MIN = -3.05;
const SOIL_Z_MAX = 3.05;
const GRID_X = 46;
const GRID_Z = 32;
function clamp01(v) {
return Math.max(0, Math.min(1, v));
}
function smoothstep(edge0, edge1, value) {
const t = clamp01((value - edge0) / (edge1 - edge0));
return t * t * (3 - 2 * t);
}
function hash(x, z, seed = 0) {
const n = Math.sin(
x * 127.1 + z * 311.7 + seed * 74.7
) * 43758.5453123;
return n - Math.floor(n);
}
function channelCenter(z) {
return 0.55 * Math.sin((z + 1.5) * 0.75);
}
function terrainHeight(x, z) {
const center = channelCenter(z);
// One bank is steeper and the other more gently eroded.
const distance = Math.abs(x - center);
const innerWidth = 0.22;
const outerWidth = x < center ? 0.78 : 0.96;
const incision =
1 - smoothstep(innerWidth, outerWidth, distance);
// The small channel fades out toward the rear of the block.
const longitudinalFade = smoothstep(-2.95, -1.75, z);
const depth = 1.7 * incision * longitudinalFade;
const surfaceVariation =
0.032 * Math.sin(x * 1.65 + z * 0.42) +
0.018 * Math.cos(z * 2.15 - x * 0.31);
return 3.22 + surfaceVariation - depth;
}
function stratumLevel(index, x, z) {
const base = LEVELS[index];
const amplitude = index % 4 === 0 ? 0.043 : 0.018;
return base +
amplitude * Math.sin(x * 0.84 + z * 0.31 + index * 0.42) +
amplitude * 0.38 * Math.cos(z * 1.08 - x * 0.23);
}
// ------------------------------------------------------------
// Layered loess volume
//
// Each stratum is a closed cell-based solid. Its upper surface
// is clipped by the terrain, so the channel physically removes
// the upper layers and exposes the lower ones.
// ------------------------------------------------------------
const LEVELS = [
-0.69, -0.49, -0.31, -0.12, 0.04, 0.17, 0.31, 0.44,
0.58, 0.71, 0.86, 1.00, 1.16, 1.31, 1.47, 1.63,
1.81, 1.98, 2.17, 2.34, 2.52, 2.69, 2.84, 2.99,
3.12, 3.24
];
const LAYER_COLORS = [
0x98633c, 0xa97040, 0xb87c45, 0xc78b4d, 0xd6a45d,
0xe1b874, 0xbb7b43, 0xc98f4f, 0xd8a960, 0xe7c47e,
0xcd9453, 0xb97841, 0xbf8247, 0xd19a56, 0xe3b66d,
0xc58a4c, 0xb6753f, 0xc88b4b, 0xdcac60, 0xe9c27b,
0xbd7c41, 0xce9451, 0xdeac61, 0xe9c37a, 0xc38847,
0xd7a45b
];
const loessMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.98,
metalness: 0,
flatShading: true
});
function createLoessLayer(layerIndex) {
const positions = [];
const colors = [];
const indices = [];
const active = new Uint8Array(GRID_X * GRID_Z);
const dx = (SOIL_X_MAX - SOIL_X_MIN) / GRID_X;
const dz = (SOIL_Z_MAX - SOIL_Z_MIN) / GRID_Z;
function cornersFor(ix, iz) {
const x0 = SOIL_X_MIN + ix * dx;
const x1 = x0 + dx;
const z0 = SOIL_Z_MIN + iz * dz;
const z1 = z0 + dz;
return [
[x0, z0],
[x1, z0],
[x1, z1],
[x0, z1]
];
}
// Determine which cells remain after incision.
for (let iz = 0; iz < GRID_Z; iz++) {
for (let ix = 0; ix < GRID_X; ix++) {
const corners = cornersFor(ix, iz);
let remains = true;
for (const [x, z] of corners) {
const bottom = stratumLevel(layerIndex, x, z);
if (terrainHeight(x, z) <= bottom + 0.008) {
remains = false;
break;
}
}
active[iz * GRID_X + ix] = remains ? 1 : 0;
}
}
const baseColor = new THREE.Color(LAYER_COLORS[layerIndex]);
const channelColor = new THREE.Color(0x745039);
for (let iz = 0; iz < GRID_Z; iz++) {
for (let ix = 0; ix < GRID_X; ix++) {
const cellIndex = iz * GRID_X + ix;
if (!active[cellIndex]) continue;
const corners = cornersFor(ix, iz);
const vertexOffset = positions.length / 3;
const surfaceHeights = corners.map(
([x, z]) => terrainHeight(x, z)
);
const nextLevel = corners.map(
([x, z]) => stratumLevel(layerIndex + 1, x, z)
);
const averageHeight =
surfaceHeights.reduce((a, b) => a + b, 0) / 4;
const averageNext =
nextLevel.reduce((a, b) => a + b, 0) / 4;
const channelExposure = clamp01(
(averageNext - averageHeight) / 0.72
);
const cellColor = baseColor.clone();
const variation = hash(ix, iz, layerIndex) - 0.5;
cellColor.offsetHSL(
variation * 0.012,
variation * 0.035,
variation * 0.065
);
cellColor.lerp(channelColor, channelExposure * 0.38);
const undersideColor = cellColor.clone().multiplyScalar(0.76);
// Four top vertices.
for (let c = 0; c < 4; c++) {
const [x, z] = corners[c];
const top = Math.min(
nextLevel[c],
surfaceHeights[c]
) - 0.002;
positions.push(x, top, z);
colors.push(
cellColor.r,
cellColor.g,
cellColor.b
);
}
// Four bottom vertices.
for (let c = 0; c < 4; c++) {
const [x, z] = corners[c];
const bottom =
stratumLevel(layerIndex, x, z) + 0.002;
positions.push(x, bottom, z);
colors.push(
undersideColor.r,
undersideColor.g,
undersideColor.b
);
}
// Top and bottom faces.
indices.push(
vertexOffset + 0, vertexOffset + 2, vertexOffset + 1,
vertexOffset + 0, vertexOffset + 3, vertexOffset + 2,
vertexOffset + 5, vertexOffset + 6, vertexOffset + 4,
vertexOffset + 6, vertexOffset + 7, vertexOffset + 4
);
function addWall(a, b) {
indices.push(
vertexOffset + a,
vertexOffset + b,
vertexOffset + a + 4,
vertexOffset + b,
vertexOffset + b + 4,
vertexOffset + a + 4
);
}
// Exposed perimeter and walls created by channel incision.
const backExists =
iz > 0 &&
active[(iz - 1) * GRID_X + ix];
const rightExists =
ix < GRID_X - 1 &&
active[iz * GRID_X + ix + 1];
const frontExists =
iz < GRID_Z - 1 &&
active[(iz + 1) * GRID_X + ix];
const leftExists =
ix > 0 &&
active[iz * GRID_X + ix - 1];
if (!backExists) addWall(0, 1);
if (!rightExists) addWall(1, 2);
if (!frontExists) addWall(2, 3);
if (!leftExists) addWall(3, 0);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
const mesh = new THREE.Mesh(geometry, loessMaterial);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.name = `Loess stratum ${layerIndex + 1}`;
return mesh;
}
for (let i = 0; i < LEVELS.length - 1; i++) {
scene.add(createLoessLayer(i));
}
// ------------------------------------------------------------
// Angular bedrock foundation
// ------------------------------------------------------------
const bedrockGeometry = new THREE.BoxGeometry(
10.3,
1.42,
6.72,
8,
2,
5
).toNonIndexed();
const bedrockPosition = bedrockGeometry.attributes.position;
for (let i = 0; i < bedrockPosition.count; i++) {
let x = bedrockPosition.getX(i);
let y = bedrockPosition.getY(i);
let z = bedrockPosition.getZ(i);
const fractureNoise =
Math.sin((x + 2.0) * 1.72 + (z - 1.0) * 0.83) * 0.55 +
Math.cos(z * 2.1 - x * 0.46) * 0.45;
if (y > 0.60) {
y += fractureNoise * 0.065;
} else if (y < -0.60) {
y += fractureNoise * 0.028;
} else {
y += Math.sin(x * 2.4 + z * 1.1) * 0.025;
}
x += Math.sin(z * 1.9 + y * 2.4) * 0.025;
z += Math.cos(x * 1.3 + y * 1.7) * 0.018;
bedrockPosition.setXYZ(i, x, y, z);
}
bedrockGeometry.computeVertexNormals();
const bedrock = new THREE.Mesh(
bedrockGeometry,
new THREE.MeshStandardMaterial({
color: 0x59615f,
roughness: 0.96,
metalness: 0.02,
flatShading: true
})
);
bedrock.position.y = -1.34;
bedrock.castShadow = true;
bedrock.receiveShadow = true;
scene.add(bedrock);
// Dark fracture seams on the exposed bedrock face.
const fractureMaterial = new THREE.LineBasicMaterial({
color: 0x2d3332,
transparent: true,
opacity: 0.72
});
const fracturePaths = [
[
[-4.75, -0.88, 3.39],
[-3.82, -1.08, 3.39],
[-3.05, -1.02, 3.39],
[-2.18, -1.30, 3.39],
[-1.52, -1.24, 3.39]
],
[
[1.15, -0.82, 3.39],
[1.62, -1.11, 3.39],
[2.38, -1.06, 3.39],
[2.95, -1.38, 3.39],
[3.52, -1.31, 3.39]
],
[
[4.26, -1.84, 3.39],
[3.86, -1.57, 3.39],
[3.58, -1.59, 3.39],
[3.19, -1.32, 3.39]
]
];
for (const path of fracturePaths) {
const geometry = new THREE.BufferGeometry().setFromPoints(
path.map((p) => new THREE.Vector3(...p))
);
scene.add(new THREE.Line(geometry, fractureMaterial));
}
const veinMaterial = new THREE.LineBasicMaterial({
color: 0x969c94,
transparent: true,
opacity: 0.55
});
const veinGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-4.48, -1.77, 3.395),
new THREE.Vector3(-3.65, -1.63, 3.395),
new THREE.Vector3(-2.95, -1.72, 3.395),
new THREE.Vector3(-2.28, -1.55, 3.395)
]);
scene.add(new THREE.Line(veinGeometry, veinMaterial));
// ------------------------------------------------------------
// Channel bed, erosion striations, and sediment clasts
// ------------------------------------------------------------
const ribbonPositions = [];
const ribbonIndices = [];
const ribbonSegments = 90;
for (let i = 0; i <= ribbonSegments; i++) {
const z = -1.86 + (3.04 + 1.86) * i / ribbonSegments;
const center = channelCenter(z);
for (const side of [-1, 1]) {
const x = center + side * 0.115;
const y = terrainHeight(x, z) + 0.014;
ribbonPositions.push(x, y, z);
}
if (i < ribbonSegments) {
const a = i * 2;
ribbonIndices.push(
a, a + 2, a + 1,
a + 1, a + 2, a + 3
);
}
}
const ribbonGeometry = new THREE.BufferGeometry();
ribbonGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(ribbonPositions, 3)
);
ribbonGeometry.setIndex(ribbonIndices);
ribbonGeometry.computeVertexNormals();
const channelBed = new THREE.Mesh(
ribbonGeometry,
new THREE.MeshStandardMaterial({
color: 0x65452f,
roughness: 1,
side: THREE.DoubleSide
})
);
channelBed.receiveShadow = true;
scene.add(channelBed);
const striationMaterial = new THREE.LineBasicMaterial({
color: 0x765037,
transparent: true,
opacity: 0.30
});
for (let i = 0; i < 24; i++) {
const z = -1.55 + i * 0.19;
const center = channelCenter(z);
const side = i % 2 === 0 ? -1 : 1;
const points = [];
for (let j = 0; j < 5; j++) {
const distance = 0.24 + (0.52 * j) / 4;
const x = center + side * distance;
const y = terrainHeight(x, z) + 0.019;
points.push(new THREE.Vector3(x, y, z + 0.008));
}
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
striationMaterial
)
);
}
let randomSeed = 84721;
function random() {
randomSeed =
(1664525 * randomSeed + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const clastGeometry = new THREE.DodecahedronGeometry(1, 0);
const clastMaterials = [
new THREE.MeshStandardMaterial({
color: 0xc8a06b,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x8c6746,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb8a07b,
roughness: 1,
flatShading: true
})
];
// Embedded aggregates in the exposed cut face.
for (let i = 0; i < 34; i++) {
const x = -4.55 + random() * 9.1;
// Keep aggregates out of the open channel notch.
if (Math.abs(x - channelCenter(SOIL_Z_MAX)) < 1.12) {
continue;
}
const y = -0.48 + random() * 3.25;
const clast = new THREE.Mesh(
clastGeometry,
clastMaterials[Math.floor(random() * clastMaterials.length)]
);
clast.position.set(x, y, SOIL_Z_MAX + 0.025);
clast.scale.set(
0.045 + random() * 0.065,
0.025 + random() * 0.045,
0.022 + random() * 0.03
);
clast.rotation.set(
random() * Math.PI,
random() * Math.PI,
random() * Math.PI
);
clast.castShadow = true;
scene.add(clast);
}
// Small grains resting in the channel.
for (let i = 0; i < 28; i++) {
const z = -1.55 + random() * 4.45;
const center = channelCenter(z);
const x = center + (random() - 0.5) * 0.22;
const y = terrainHeight(x, z) + 0.035;
const grain = new THREE.Mesh(
clastGeometry,
clastMaterials[Math.floor(random() * clastMaterials.length)]
);
grain.position.set(x, y, z);
grain.scale.set(
0.025 + random() * 0.045,
0.018 + random() * 0.032,
0.025 + random() * 0.045
);
grain.rotation.set(
random() * Math.PI,
random() * Math.PI,
random() * Math.PI
);
grain.castShadow = true;
scene.add(grain);
}
// ------------------------------------------------------------
// A few exposed roots emphasize the unconsolidated material
// ------------------------------------------------------------
const rootMaterial = new THREE.MeshStandardMaterial({
color: 0x5b3c26,
roughness: 1
});
const rootPaths = [
[
[-3.72, 2.90, 3.075],
[-3.62, 2.56, 3.078],
[-3.75, 2.18, 3.075],
[-3.55, 1.72, 3.075]
],
[
[-3.65, 2.56, 3.078],
[-3.30, 2.36, 3.078],
[-3.06, 2.12, 3.075]
],
[
[-3.73, 2.22, 3.075],
[-4.05, 1.98, 3.075],
[-4.22, 1.62, 3.075]
],
[
[3.75, 2.82, 3.075],
[3.58, 2.43, 3.075],
[3.76, 2.04, 3.075]
]
];
for (const path of rootPaths) {
const curve = new THREE.CatmullRomCurve3(
path.map((p) => new THREE.Vector3(...p))
);
const root = new THREE.Mesh(
new THREE.TubeGeometry(curve, 24, 0.014, 5, false),
rootMaterial
);
root.castShadow = true;
scene.add(root);
}
// ------------------------------------------------------------
// Direct camera controls — no addon imports
// ------------------------------------------------------------
const cameraState = {
theta: 0.62,
phi: 1.03,
radius: 13.6,
target: new THREE.Vector3(0, 0.43, 0)
};
const desiredCamera = {
theta: cameraState.theta,
phi: cameraState.phi,
radius: cameraState.radius
};
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desiredCamera.theta -= dx * 0.006;
desiredCamera.phi = THREE.MathUtils.clamp(
desiredCamera.phi - dy * 0.005,
0.43,
1.42
);
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener(
"pointerup",
endDrag
);
renderer.domElement.addEventListener(
"pointercancel",
endDrag
);
renderer.domElement.addEventListener(
"pointerleave",
(event) => {
if (dragging && event.buttons === 0) endDrag(event);
}
);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.radius = THREE.MathUtils.clamp(
desiredCamera.radius * Math.exp(event.deltaY * 0.001),
7.2,
22
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
desiredCamera.theta = 0.62;
desiredCamera.phi = 1.03;
desiredCamera.radius = 13.6;
});
renderer.domElement.addEventListener(
"contextmenu",
(event) => event.preventDefault()
);
window.addEventListener("keydown", (event) => {
const keyName = event.key.toLowerCase();
if (keyName === "arrowleft") {
desiredCamera.theta += 0.08;
} else if (keyName === "arrowright") {
desiredCamera.theta -= 0.08;
} else if (keyName === "arrowup") {
desiredCamera.phi = Math.max(
0.43,
desiredCamera.phi - 0.05
);
} else if (keyName === "arrowdown") {
desiredCamera.phi = Math.min(
1.42,
desiredCamera.phi + 0.05
);
} else if (keyName === "+" || keyName === "=") {
desiredCamera.radius = Math.max(
7.2,
desiredCamera.radius - 0.6
);
} else if (keyName === "-") {
desiredCamera.radius = Math.min(
22,
desiredCamera.radius + 0.6
);
} else if (keyName === "r") {
desiredCamera.theta = 0.62;
desiredCamera.phi = 1.03;
desiredCamera.radius = 13.6;
}
});
// ------------------------------------------------------------
// Projected annotations
// ------------------------------------------------------------
const channelAnchor = new THREE.Vector3(
channelCenter(1.0) + 0.36,
terrainHeight(channelCenter(1.0) + 0.36, 1.0) + 0.05,
1.0
);
const strataAnchor = new THREE.Vector3(
-2.75,
1.30,
SOIL_Z_MAX + 0.05
);
const bedrockAnchor = new THREE.Vector3(
2.72,
-1.30,
3.40
);
const annotations = [
{
label: document.getElementById("label-channel"),
line: document.getElementById("line-channel"),
dot: document.getElementById("dot-channel"),
anchor: channelAnchor,
offsetX: -145,
offsetY: -60
},
{
label: document.getElementById("label-strata"),
line: document.getElementById("line-strata"),
dot: document.getElementById("dot-strata"),
anchor: strataAnchor,
offsetX: -115,
offsetY: -30
},
{
label: document.getElementById("label-bedrock"),
line: document.getElementById("line-bedrock"),
dot: document.getElementById("dot-bedrock"),
anchor: bedrockAnchor,
offsetX: 110,
offsetY: 45
}
];
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const annotation of annotations) {
const projected = annotation.anchor.clone().project(camera);
const anchorX = (projected.x * 0.5 + 0.5) * width;
const anchorY = (-projected.y * 0.5 + 0.5) * height;
const labelX = THREE.MathUtils.clamp(
anchorX + annotation.offsetX,
95,
width - 95
);
const labelY = THREE.MathUtils.clamp(
anchorY + annotation.offsetY,
80,
height - 80
);
annotation.label.style.transform =
`translate(${labelX}px, ${labelY}px) translate(-50%, -50%)`;
annotation.line.setAttribute("x1", anchorX);
annotation.line.setAttribute("y1", anchorY);
annotation.line.setAttribute("x2", labelX);
annotation.line.setAttribute("y2", labelY);
annotation.dot.setAttribute("cx", anchorX);
annotation.dot.setAttribute("cy", anchorY);
const visible =
projected.z > -1 &&
projected.z < 1 &&
anchorX > -50 &&
anchorX < width + 50 &&
anchorY > -50 &&
anchorY < height + 50;
annotation.label.style.display = visible ? "block" : "none";
annotation.line.style.display = visible ? "block" : "none";
annotation.dot.style.display = visible ? "block" : "none";
}
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
const easing = 0.085;
cameraState.theta +=
(desiredCamera.theta - cameraState.theta) * easing;
cameraState.phi +=
(desiredCamera.phi - cameraState.phi) * easing;
cameraState.radius +=
(desiredCamera.radius - cameraState.radius) * easing;
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical);
camera.lookAt(cameraState.target);
renderer.render(scene, camera);
updateAnnotations();
}
animate();
</script>
</body>
</html>
```