Prompt specification
Build or customize the Three.js 3D scene "Grand Canyon tributary and main-channel 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: Grand Canyon tributary and main-channel landscape
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/grand-canyon-tributary-and-main-channel-landscape-x3071
Scene Description Prompt:
Grand Canyon tributary and main-channel landscape. Build a bounded referenced canyon scene with layered plateaus, branching side canyon, and main river. Preserve the drainage relationships while simplifying the vast scale. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (grand-canyon-tributary-and-main-channel-landscape-x3071):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Grand Canyon — Tributary Confluence</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b8d0d5;
}
#app,
#app canvas {
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 18px;
left: 18px;
z-index: 20;
width: min(350px, calc(100vw - 36px));
padding: 18px 18px 16px;
color: #f3eadc;
background: linear-gradient(145deg, rgba(27, 34, 36, 0.94), rgba(39, 43, 42, 0.82));
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 14px;
box-shadow: 0 16px 45px rgba(18, 23, 25, 0.26);
backdrop-filter: blur(12px);
user-select: none;
}
.eyebrow {
margin: 0 0 5px;
color: #e5b471;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 24px;
line-height: 1.12;
letter-spacing: -0.025em;
}
.subtitle {
margin: 9px 0 15px;
color: rgba(243, 234, 220, 0.7);
font-size: 12px;
line-height: 1.55;
}
.legend {
display: grid;
gap: 8px;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.12);
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: rgba(243, 234, 220, 0.84);
font-size: 12px;
}
.swatch {
width: 25px;
height: 4px;
flex: 0 0 auto;
border-radius: 999px;
}
.swatch.main {
width: 31px;
background: #3b9caf;
box-shadow: 0 0 9px rgba(59, 156, 175, 0.35);
}
.swatch.tributary {
width: 20px;
background: #60b6c3;
}
.swatch.rock {
height: 10px;
background: linear-gradient(#d3aa78, #985341, #55303a);
}
.drainage-note {
margin-top: 13px;
padding: 10px 11px;
color: rgba(243, 234, 220, 0.72);
font-size: 11px;
line-height: 1.5;
background: rgba(255, 255, 255, 0.055);
border-radius: 9px;
}
.panel-actions {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 13px;
}
button {
appearance: none;
padding: 8px 11px;
color: #f8eee0;
background: rgba(229, 180, 113, 0.15);
border: 1px solid rgba(229, 180, 113, 0.38);
border-radius: 8px;
font: inherit;
font-size: 11px;
cursor: pointer;
}
button:hover {
background: rgba(229, 180, 113, 0.25);
}
.scale-note {
color: rgba(243, 234, 220, 0.47);
font-size: 10px;
letter-spacing: 0.04em;
}
#labels {
position: fixed;
inset: 0;
z-index: 8;
pointer-events: none;
overflow: hidden;
}
.map-label {
position: absolute;
white-space: nowrap;
transform: translate(-50%, -100%);
padding: 5px 8px;
border-radius: 6px;
color: #fff8eb;
background: rgba(25, 34, 37, 0.76);
border: 1px solid rgba(255, 255, 255, 0.2);
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
backdrop-filter: blur(5px);
}
.map-label::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 17px;
background: rgba(255, 255, 255, 0.68);
}
.map-label.river {
color: #d9f5f5;
border-color: rgba(113, 204, 218, 0.5);
background: rgba(22, 57, 64, 0.84);
}
.map-label.region {
color: #f0d4a6;
background: rgba(50, 38, 30, 0.76);
border-color: rgba(240, 212, 166, 0.32);
letter-spacing: 0.16em;
}
.controls-hint {
position: fixed;
right: 16px;
bottom: 15px;
z-index: 20;
padding: 8px 11px;
color: rgba(255, 255, 255, 0.78);
background: rgba(28, 37, 39, 0.68);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 8px;
font-size: 10px;
letter-spacing: 0.025em;
pointer-events: none;
backdrop-filter: blur(8px);
}
@media (max-width: 620px) {
.panel {
top: 10px;
left: 10px;
padding: 13px;
}
h1 {
font-size: 20px;
}
.subtitle,
.drainage-note,
.scale-note {
display: none;
}
.controls-hint {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div id="labels">
<div id="labelColorado" class="map-label river">Colorado River</div>
<div id="labelTributary" class="map-label river">Little Colorado</div>
<div id="labelCanyon" class="map-label region">Grand Canyon</div>
</div>
<section class="panel">
<p class="eyebrow">Schematic canyon geomorphology</p>
<h1>Grand Canyon<br />Tributary Confluence</h1>
<p class="subtitle">
A bounded low-poly relief model reducing the canyon’s immense scale while
retaining its plateau-and-channel hierarchy.
</p>
<div class="legend">
<div class="legend-row">
<span class="swatch main"></span>
<span>Colorado River — main channel</span>
</div>
<div class="legend-row">
<span class="swatch tributary"></span>
<span>Little Colorado — tributary</span>
</div>
<div class="legend-row">
<span class="swatch rock"></span>
<span>Layered sandstone and plateau benches</span>
</div>
</div>
<div class="drainage-note">
The tributary descends from the higher rim and joins the main river.
The combined channel continues downstream at a lower elevation.
</div>
<div class="panel-actions">
<button id="resetView" type="button">Reset view</button>
<span class="scale-note">SCHEMATIC · NOT TO SCALE</span>
</div>
</section>
<div class="controls-hint">
Drag: orbit · Shift/right-drag: pan · Wheel/pinch: zoom · R: 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 and scene
// -------------------------------------------------------------------------
const app = document.getElementById("app");
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;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9d0d4);
scene.fog = new THREE.Fog(0xb9d0d4, 42, 82);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
150
);
const hemisphere = new THREE.HemisphereLight(0xe5f3ff, 0x6b3e32, 1.75);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe1b0, 3.25);
sun.position.set(-14, 27, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -21;
sun.shadow.camera.right = 21;
sun.shadow.camera.top = 21;
sun.shadow.camera.bottom = -21;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
scene.add(sun.target);
const fill = new THREE.DirectionalLight(0x9bc9e0, 0.8);
fill.position.set(15, 12, -18);
scene.add(fill);
// -------------------------------------------------------------------------
// Constants and deterministic random helpers
// -------------------------------------------------------------------------
const WIDTH = 28;
const DEPTH = 24;
const GRID_X = 80;
const GRID_Z = 68;
const BASE_Y = -5.2;
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(74193);
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smoothstep(t) {
return t * t * (3 - 2 * t);
}
function hash2(x, z) {
let h =
Math.imul(x, 374761393) +
Math.imul(z, 668265263);
h = Math.imul(h ^ (h >>> 13), 1274126177);
return ((h ^ (h >>> 16)) >>> 0) / 4294967295;
}
function valueNoise(x, z) {
const ix = Math.floor(x);
const iz = Math.floor(z);
const fx = x - ix;
const fz = z - iz;
const ux = smoothstep(fx);
const uz = smoothstep(fz);
const a = hash2(ix, iz);
const b = hash2(ix + 1, iz);
const c = hash2(ix, iz + 1);
const d = hash2(ix + 1, iz + 1);
return THREE.MathUtils.lerp(
THREE.MathUtils.lerp(a, b, ux),
THREE.MathUtils.lerp(c, d, ux),
uz
);
}
function fractalNoise(x, z) {
return (
valueNoise(x * 0.22, z * 0.22) * 0.64 +
valueNoise(x * 0.53 + 9.1, z * 0.53 - 3.7) * 0.25 +
valueNoise(x * 1.08 - 4.3, z * 1.08 + 8.2) * 0.11
);
}
// -------------------------------------------------------------------------
// Drainage network
//
// Controls are [x, y, z]. Y is the water elevation.
// The tributary begins high on the northern/eastern rim and meets the
// Colorado at the same elevation. The main river continues lower.
// -------------------------------------------------------------------------
const mainControls = [
new THREE.Vector3(-12.4, -1.25, -12.0),
new THREE.Vector3(-10.2, -1.42, -7.3),
new THREE.Vector3(-7.2, -1.61, -4.7),
new THREE.Vector3(-3.2, -1.76, -2.3),
new THREE.Vector3( 0.0, -1.92, 0.0),
new THREE.Vector3( 4.2, -2.15, 2.5),
new THREE.Vector3( 8.2, -2.48, 5.2),
new THREE.Vector3(12.6, -2.90, 12.0)
];
const tributaryControls = [
new THREE.Vector3(8.4, 0.55, -12.0),
new THREE.Vector3(6.6, 0.12, -8.5),
new THREE.Vector3(4.9, -0.38, -6.0),
new THREE.Vector3(3.1, -1.03, -3.2),
new THREE.Vector3(0.0, -1.915, 0.0)
];
function createPath(controls, subdivisionsPerSpan = 18) {
const curve = new THREE.CatmullRomCurve3(
controls,
false,
"centripetal",
0.5
);
const points = curve.getPoints(controls.length * subdivisionsPerSpan);
const segments = [];
for (let i = 0; i < points.length - 1; i++) {
const p = points[i];
const q = points[i + 1];
const dx = q.x - p.x;
const dz = q.z - p.z;
segments.push({
x: p.x,
y: p.y,
z: p.z,
dx,
dz,
dy: q.y - p.y,
lengthSquared: dx * dx + dz * dz
});
}
return { points, segments };
}
const mainPath = createPath(mainControls);
const tributaryPath = createPath(tributaryControls);
function nearestOnPath(x, z, path) {
let bestDistanceSquared = Infinity;
let nearestY = 0;
for (let i = 0; i < path.segments.length; i++) {
const s = path.segments[i];
const t = clamp(
((x - s.x) * s.dx + (z - s.z) * s.dz) /
Math.max(s.lengthSquared, 0.000001),
0,
1
);
const px = s.x + s.dx * t;
const pz = s.z + s.dz * t;
const distanceSquared = (x - px) ** 2 + (z - pz) ** 2;
if (distanceSquared < bestDistanceSquared) {
bestDistanceSquared = distanceSquared;
nearestY = s.y + s.dy * t;
}
}
return {
distance: Math.sqrt(bestDistanceSquared),
y: nearestY
};
}
// Stepped cross-sections form terraces and broad benches.
function canyonProfile(distance, channelHalfWidth) {
const x = Math.max(0, distance - channelHalfWidth);
if (x < 0.18) return -0.12;
if (x < 0.68) return 0.38;
if (x < 1.15) return 0.98;
if (x < 1.70) return 1.66;
if (x < 2.35) return 2.46;
if (x < 3.10) return 3.42;
if (x < 4.00) return 4.58;
if (x < 4.55) return 6.20;
return 8.00;
}
function plateauHeight(x, z) {
const n = fractalNoise(x, z);
const mesaA =
0.34 *
Math.exp(
-(
((x + 7.0) ** 2) / 20 +
((z - 6.4) ** 2) / 24
)
);
const mesaB =
0.40 *
Math.exp(
-(
((x - 7.0) ** 2) / 19 +
((z + 6.5) ** 2) / 15
)
);
const broadTilt = 0.19 * Math.sin(x * 0.18 + z * 0.08);
const height =
3.72 +
n * 0.90 +
mesaA +
mesaB +
broadTilt;
// Quantized broad plateau levels.
return Math.round(height / 0.34) * 0.34;
}
function terrainHeight(x, z) {
const base = plateauHeight(x, z);
const main = nearestOnPath(x, z, mainPath);
const tributary = nearestOnPath(x, z, tributaryPath);
const mainTarget = main.y + canyonProfile(main.distance, 1.08);
const tributaryTarget =
tributary.y + canyonProfile(tributary.distance, 0.60);
return Math.min(base, mainTarget, tributaryTarget);
}
// -------------------------------------------------------------------------
// Rock palette
// -------------------------------------------------------------------------
const strata = [
{ top: -4.8, color: new THREE.Color(0x38272f) },
{ top: -3.1, color: new THREE.Color(0x4b3038) },
{ top: -1.7, color: new THREE.Color(0x713c3e) },
{ top: -0.5, color: new THREE.Color(0x925044) },
{ top: 0.7, color: new THREE.Color(0xae6848) },
{ top: 1.8, color: new THREE.Color(0xc48655) },
{ top: 3.0, color: new THREE.Color(0xd1a16c) },
{ top: 4.2, color: new THREE.Color(0xd0b181) },
{ top: 99.0, color: new THREE.Color(0xc3b88d) }
];
function colorForHeight(height, jitter = 0) {
let color = strata[strata.length - 1].color;
for (const band of strata) {
if (height <= band.top) {
color = band.color;
break;
}
}
const result = color.clone();
result.offsetHSL(
(random() - 0.5) * 0.012,
(random() - 0.5) * 0.025,
jitter * (random() - 0.5)
);
return result;
}
// -------------------------------------------------------------------------
// Terrain mesh
// -------------------------------------------------------------------------
const heights = new Float32Array((GRID_X + 1) * (GRID_Z + 1));
function gridIndex(ix, iz) {
return iz * (GRID_X + 1) + ix;
}
for (let iz = 0; iz <= GRID_Z; iz++) {
const z = -DEPTH / 2 + (iz / GRID_Z) * DEPTH;
for (let ix = 0; ix <= GRID_X; ix++) {
const x = -WIDTH / 2 + (ix / GRID_X) * WIDTH;
heights[gridIndex(ix, iz)] = terrainHeight(x, z);
}
}
const terrainPositions = [];
const terrainColors = [];
function pushTriangle(array, a, b, c) {
array.push(a[0], a[1], a[2]);
array.push(b[0], b[1], b[2]);
array.push(c[0], c[1], c[2]);
}
function pushColoredTriangle(a, b, c, color) {
pushTriangle(terrainPositions, a, b, c);
for (let i = 0; i < 3; i++) {
terrainColors.push(color.r, color.g, color.b);
}
}
for (let iz = 0; iz < GRID_Z; iz++) {
const z0 = -DEPTH / 2 + (iz / GRID_Z) * DEPTH;
const z1 = -DEPTH / 2 + ((iz + 1) / GRID_Z) * DEPTH;
for (let ix = 0; ix < GRID_X; ix++) {
const x0 = -WIDTH / 2 + (ix / GRID_X) * WIDTH;
const x1 = -WIDTH / 2 + ((ix + 1) / GRID_X) * WIDTH;
const a = [x0, heights[gridIndex(ix, iz)], z0];
const b = [x1, heights[gridIndex(ix + 1, iz)], z0];
const c = [x1, heights[gridIndex(ix + 1, iz + 1)], z1];
const d = [x0, heights[gridIndex(ix, iz + 1)], z1];
const colorA = colorForHeight((a[1] + c[1] + b[1]) / 3, 0.055);
const colorB = colorForHeight((a[1] + d[1] + c[1]) / 3, 0.055);
pushColoredTriangle(a, c, b, colorA);
pushColoredTriangle(a, d, c, colorB);
}
}
const terrainGeometry = new THREE.BufferGeometry();
terrainGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(terrainPositions, 3)
);
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrainMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
metalness: 0,
flatShading: true
});
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.castShadow = true;
terrain.receiveShadow = true;
scene.add(terrain);
// -------------------------------------------------------------------------
// Stratified vertical cutaway around the bounded model
// -------------------------------------------------------------------------
const wallPositions = [];
const wallColors = [];
function pushWallTriangle(a, b, c, color) {
pushTriangle(wallPositions, a, b, c);
for (let i = 0; i < 3; i++) {
wallColors.push(color.r, color.g, color.b);
}
}
function pushWallQuad(a, b, c, d, color) {
pushWallTriangle(a, b, c, color);
pushWallTriangle(a, c, d, color);
}
function addWallSegment(x1, z1, h1, x2, z2, h2) {
const levels = [
BASE_Y,
-3.8,
-2.5,
-1.4,
-0.4,
0.5,
1.4,
2.4,
3.4,
4.5,
6.0
];
for (let i = 0; i < levels.length - 1; i++) {
const lo = levels[i];
const hi = levels[i + 1];
const l1 = Math.min(lo, h1);
const u1 = Math.min(hi, h1);
const l2 = Math.min(lo, h2);
const u2 = Math.min(hi, h2);
const side1 = u1 - l1;
const side2 = u2 - l2;
if (side1 < 0.001 && side2 < 0.001) continue;
const color = colorForHeight((lo + hi) * 0.5, 0.025);
const p1l = [x1, l1, z1];
const p1u = [x1, u1, z1];
const p2l = [x2, l2, z2];
const p2u = [x2, u2, z2];
if (side1 > 0.001 && side2 > 0.001) {
pushWallQuad(p1l, p1u, p2u, p2l, color);
} else if (side1 > 0.001) {
pushWallTriangle(p1l, p1u, p2l, color);
} else {
pushWallTriangle(p1l, p2l, p2u, color);
}
}
}
const perimeter = [];
for (let ix = 0; ix <= GRID_X; ix++) {
perimeter.push({
x: -WIDTH / 2 + (ix / GRID_X) * WIDTH,
z: -DEPTH / 2,
h: heights[gridIndex(ix, 0)]
});
}
for (let iz = 1; iz <= GRID_Z; iz++) {
perimeter.push({
x: WIDTH / 2,
z: -DEPTH / 2 + (iz / GRID_Z) * DEPTH,
h: heights[gridIndex(GRID_X, iz)]
});
}
for (let ix = GRID_X - 1; ix >= 0; ix--) {
perimeter.push({
x: -WIDTH / 2 + (ix / GRID_X) * WIDTH,
z: DEPTH / 2,
h: heights[gridIndex(ix, GRID_Z)]
});
}
for (let iz = GRID_Z - 1; iz > 0; iz--) {
perimeter.push({
x: -WIDTH / 2,
z: -DEPTH / 2 + (iz / GRID_Z) * DEPTH,
h: heights[gridIndex(0, iz)]
});
}
for (let i = 0; i < perimeter.length; i++) {
const a = perimeter[i];
const b = perimeter[(i + 1) % perimeter.length];
addWallSegment(a.x, a.z, a.h, b.x, b.z, b.h);
}
const wallGeometry = new THREE.BufferGeometry();
wallGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(wallPositions, 3)
);
wallGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(wallColors, 3)
);
wallGeometry.computeVertexNormals();
const wallMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
});
const walls = new THREE.Mesh(wallGeometry, wallMaterial);
walls.castShadow = true;
walls.receiveShadow = true;
scene.add(walls);
// Dark plinth and receiving ground.
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(WIDTH + 0.42, 0.36, DEPTH + 0.42),
new THREE.MeshStandardMaterial({
color: 0x35393a,
roughness: 0.92
})
);
plinth.position.y = BASE_Y - 0.18;
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(140, 140),
new THREE.MeshStandardMaterial({
color: 0xa8b8b7,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = BASE_Y - 0.39;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// River ribbons
// -------------------------------------------------------------------------
function createRiver(path, widthStart, widthEnd, color) {
const positions = [];
const points = path.points;
for (let i = 0; i < points.length - 1; i++) {
const p = points[i];
const q = points[i + 1];
const previous = points[Math.max(0, i - 1)];
const next = points[Math.min(points.length - 1, i + 2)];
const tx = next.x - previous.x;
const tz = next.z - previous.z;
const length = Math.hypot(tx, tz) || 1;
const nx = -tz / length;
const nz = tx / length;
const t0 = i / (points.length - 1);
const t1 = (i + 1) / (points.length - 1);
const w0 = THREE.MathUtils.lerp(widthStart, widthEnd, t0);
const w1 = THREE.MathUtils.lerp(widthStart, widthEnd, t1);
const pLeft = [p.x + nx * w0 * 0.5, p.y + 0.055, p.z + nz * w0 * 0.5];
const pRight = [p.x - nx * w0 * 0.5, p.y + 0.055, p.z - nz * w0 * 0.5];
const qLeft = [q.x + nx * w1 * 0.5, q.y + 0.055, q.z + nz * w1 * 0.5];
const qRight = [q.x - nx * w1 * 0.5, q.y + 0.055, q.z - nz * w1 * 0.5];
pushTriangle(positions, pLeft, qRight, pRight);
pushTriangle(positions, pLeft, qLeft, qRight);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color,
emissive: new THREE.Color(color).multiplyScalar(0.18),
emissiveIntensity: 0.55,
roughness: 0.28,
metalness: 0.08,
transparent: true,
opacity: 0.93,
side: THREE.DoubleSide,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
mesh.renderOrder = 2;
scene.add(mesh);
return { mesh, material };
}
const mainRiver = createRiver(mainPath, 0.95, 1.48, 0x287f94);
const tributaryRiver = createRiver(tributaryPath, 0.36, 0.72, 0x4ca8b4);
// -------------------------------------------------------------------------
// Sparse low-poly juniper / pinyon vegetation
// -------------------------------------------------------------------------
function sampleTerrainHeight(x, z) {
const gx = clamp((x + WIDTH / 2) / WIDTH * GRID_X, 0, GRID_X - 0.0001);
const gz = clamp((z + DEPTH / 2) / DEPTH * GRID_Z, 0, GRID_Z - 0.0001);
const x0 = Math.floor(gx);
const z0 = Math.floor(gz);
const x1 = Math.min(x0 + 1, GRID_X);
const z1 = Math.min(z0 + 1, GRID_Z);
const fx = gx - x0;
const fz = gz - z0;
const h00 = heights[gridIndex(x0, z0)];
const h10 = heights[gridIndex(x1, z0)];
const h01 = heights[gridIndex(x0, z1)];
const h11 = heights[gridIndex(x1, z1)];
return THREE.MathUtils.lerp(
THREE.MathUtils.lerp(h00, h10, fx),
THREE.MathUtils.lerp(h01, h11, fx),
fz
);
}
const treeData = [];
for (let attempt = 0; attempt < 1100 && treeData.length < 78; attempt++) {
const x = (random() - 0.5) * (WIDTH - 1.2);
const z = (random() - 0.5) * (DEPTH - 1.2);
const h = sampleTerrainHeight(x, z);
const dm = nearestOnPath(x, z, mainPath).distance;
const dt = nearestOnPath(x, z, tributaryPath).distance;
if (h > 3.55 && dm > 3.4 && dt > 3.0) {
treeData.push({
x,
y: h,
z,
scale: 0.72 + random() * 0.68,
rotation: random() * Math.PI * 2,
shade: random()
});
}
}
const foliageGeometry = new THREE.ConeGeometry(0.24, 0.8, 5, 1);
const foliageMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const trunkGeometry = new THREE.CylinderGeometry(0.045, 0.065, 0.3, 5);
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x654532,
roughness: 1,
flatShading: true
});
const foliage = new THREE.InstancedMesh(
foliageGeometry,
foliageMaterial,
treeData.length
);
const trunks = new THREE.InstancedMesh(
trunkGeometry,
trunkMaterial,
treeData.length
);
const dummy = new THREE.Object3D();
treeData.forEach((tree, i) => {
dummy.position.set(tree.x, tree.y + 0.56 * tree.scale, tree.z);
dummy.rotation.set(0, tree.rotation, 0);
dummy.scale.setScalar(tree.scale);
dummy.updateMatrix();
foliage.setMatrixAt(i, dummy.matrix);
const color = new THREE.Color(0x405c3b);
color.offsetHSL(
(random() - 0.5) * 0.04,
(random() - 0.5) * 0.08,
(tree.shade - 0.5) * 0.08
);
foliage.setColorAt(i, color);
dummy.position.set(tree.x, tree.y + 0.15 * tree.scale, tree.z);
dummy.rotation.set(0, tree.rotation, 0);
dummy.scale.setScalar(tree.scale);
dummy.updateMatrix();
trunks.setMatrixAt(i, dummy.matrix);
});
foliage.castShadow = true;
foliage.receiveShadow = true;
trunks.castShadow = true;
scene.add(foliage, trunks);
// -------------------------------------------------------------------------
// Low-poly canyon boulders
// -------------------------------------------------------------------------
const rockData = [];
for (let attempt = 0; attempt < 1700 && rockData.length < 44; attempt++) {
const x = (random() - 0.5) * (WIDTH - 1);
const z = (random() - 0.5) * (DEPTH - 1);
const h = sampleTerrainHeight(x, z);
const dm = nearestOnPath(x, z, mainPath).distance;
const dt = nearestOnPath(x, z, tributaryPath).distance;
const distance = Math.min(dm, dt);
if (h > -1.7 && h < 3.0 && distance > 0.45 && distance < 3.8) {
rockData.push({
x,
y: h,
z,
scale: 0.55 + random() * 1.4,
rotation: random() * Math.PI * 2
});
}
}
const rockGeometry = new THREE.DodecahedronGeometry(0.24, 0);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const rocks = new THREE.InstancedMesh(
rockGeometry,
rockMaterial,
rockData.length
);
rockData.forEach((rock, i) => {
dummy.position.set(rock.x, rock.y + 0.12 * rock.scale, rock.z);
dummy.rotation.set(random() * 0.35, rock.rotation, random() * 0.25);
dummy.scale.set(
rock.scale,
rock.scale * (0.62 + random() * 0.28),
rock.scale * (0.82 + random() * 0.45)
);
dummy.updateMatrix();
rocks.setMatrixAt(i, dummy.matrix);
const color = new THREE.Color(0x8a5d48);
color.offsetHSL(0, 0, (random() - 0.5) * 0.08);
rocks.setColorAt(i, color);
});
rocks.castShadow = true;
rocks.receiveShadow = true;
scene.add(rocks);
// -------------------------------------------------------------------------
// Moving water glints, indicating downstream direction
// -------------------------------------------------------------------------
const flowGroups = [];
function createFlowGlints(path, count, speed, color, size) {
const geometry = new THREE.BoxGeometry(size * 0.24, 0.018, size);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.53,
depthWrite: false,
toneMapped: false
});
const mesh = new THREE.InstancedMesh(geometry, material, count);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.renderOrder = 3;
scene.add(mesh);
const offsets = [];
for (let i = 0; i < count; i++) {
offsets.push((i / count + random() * 0.035) % 1);
}
flowGroups.push({ path, count, speed, mesh, offsets });
}
createFlowGlints(mainPath, 15, 0.025, 0xbfeef0, 0.34);
createFlowGlints(tributaryPath, 8, 0.018, 0xc7f0ef, 0.24);
const forwardAxis = new THREE.Vector3(0, 0, 1);
const flowQuaternion = new THREE.Quaternion();
const flowPosition = new THREE.Vector3();
const flowScale = new THREE.Vector3(1, 1, 1);
const flowMatrix = new THREE.Matrix4();
function updateFlowGlints(timeSeconds) {
for (const group of flowGroups) {
const points = group.path.points;
for (let i = 0; i < group.count; i++) {
const progress = (group.offsets[i] + timeSeconds * group.speed) % 1;
const position = progress * (points.length - 1);
const index = Math.floor(position);
const fraction = position - index;
const p0 = points[Math.max(0, index - 1)];
const p1 = points[index];
const p2 = points[Math.min(points.length - 1, index + 1)];
const p3 = points[Math.min(points.length - 1, index + 2)];
flowPosition.lerpVectors(p1, p2, fraction);
const tangent = new THREE.Vector3()
.addScaledVector(p2, 0.5)
.addScaledVector(p3, 0.5)
.addScaledVector(p1, -0.5)
.addScaledVector(p0, -0.5)
.normalize();
flowQuaternion.setFromUnitVectors(forwardAxis, tangent);
flowPosition.y += 0.075;
flowMatrix.compose(
flowPosition,
flowQuaternion,
flowScale
);
group.mesh.setMatrixAt(i, flowMatrix);
}
group.mesh.instanceMatrix.needsUpdate = true;
}
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom, touch pinch
// -------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, -0.35, 0),
radius: 34,
theta: 0.66,
phi: 0.95
};
const initialControls = {
radius: controls.radius,
theta: controls.theta,
phi: controls.phi,
target: controls.target.clone()
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
}
function resetView() {
controls.radius = initialControls.radius;
controls.theta = initialControls.theta;
controls.phi = initialControls.phi;
controls.target.copy(initialControls.target);
updateCamera();
}
function panCamera(dx, dy) {
const scale = controls.radius * 0.00145;
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0);
right.y = 0;
right.normalize();
const cameraUp = new THREE.Vector3(0, 1, 0)
.applyQuaternion(camera.quaternion);
cameraUp.y = 0;
cameraUp.normalize();
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(cameraUp, dy * scale);
controls.target.x = clamp(controls.target.x, -10, 10);
controls.target.z = clamp(controls.target.z, -9, 9);
controls.target.y = clamp(controls.target.y, -3, 4);
}
const pointers = new Map();
let dragMode = "orbit";
let previousX = 0;
let previousY = 0;
let previousPinchDistance = 0;
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragMode =
event.button === 2 || event.button === 1 || event.shiftKey
? "pan"
: "orbit";
previousX = event.clientX;
previousY = event.clientY;
} else if (pointers.size === 2) {
dragMode = "pinch";
const [a, b] = Array.from(pointers.values());
previousPinchDistance = Math.hypot(
a.x - b.x,
a.y - b.y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size >= 2) {
const [a, b] = Array.from(pointers.values());
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance > 0) {
controls.radius = clamp(
controls.radius * (previousPinchDistance / distance),
16,
54
);
}
previousPinchDistance = distance;
updateCamera();
return;
}
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (dragMode === "pan") {
panCamera(dx, dy);
} else {
controls.theta -= dx * 0.006;
controls.phi = clamp(controls.phi - dy * 0.005, 0.34, 1.43);
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
dragMode = "orbit";
previousPinchDistance = 0;
} else if (pointers.size === 1) {
const remaining = Array.from(pointers.values())[0];
previousX = remaining.x;
previousY = remaining.y;
dragMode = "orbit";
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
16,
54
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetView();
return;
}
if (event.key === "+" || event.key === "=") {
controls.radius = clamp(controls.radius - 2, 16, 54);
}
if (event.key === "-" || event.key === "_") {
controls.radius = clamp(controls.radius + 2, 16, 54);
}
if (event.key === "ArrowLeft") {
controls.theta -= 0.08;
}
if (event.key === "ArrowRight") {
controls.theta += 0.08;
}
if (event.key === "ArrowUp") {
controls.phi = clamp(controls.phi - 0.06, 0.34, 1.43);
}
if (event.key === "ArrowDown") {
controls.phi = clamp(controls.phi + 0.06, 0.34, 1.43);
}
updateCamera();
});
document.getElementById("resetView").addEventListener("click", resetView);
// -------------------------------------------------------------------------
// Projected map labels
// -------------------------------------------------------------------------
const labelColorado = document.getElementById("labelColorado");
const labelTributary = document.getElementById("labelTributary");
const labelCanyon = document.getElementById("labelCanyon");
const coloradoPoint = mainPath.points[
Math.floor(mainPath.points.length * 0.66)
];
const tributaryPoint = tributaryPath.points[
Math.floor(tributaryPath.points.length * 0.43)
];
const labelAnchors = [
{
element: labelColorado,
position: new THREE.Vector3(
coloradoPoint.x,
coloradoPoint.y + 0.9,
coloradoPoint.z
)
},
{
element: labelTributary,
position: new THREE.Vector3(
tributaryPoint.x,
tributaryPoint.y + 0.85,
tributaryPoint.z
)
},
{
element: labelCanyon,
position: new THREE.Vector3(-7.4, 5.5, 5.4)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of labelAnchors) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.12 &&
Math.abs(projected.y) < 1.12;
label.element.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
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();
renderer.setAnimationLoop((timeMilliseconds) => {
const time = timeMilliseconds * 0.001;
updateFlowGlints(time);
mainRiver.material.opacity = 0.91 + Math.sin(time * 0.8) * 0.018;
tributaryRiver.material.opacity = 0.91 + Math.sin(time * 0.9 + 1.3) * 0.018;
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```