Prompt specification
Build or customize the Three.js 3D scene "Southern Alps valley with braided river and road" 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: Southern Alps valley with braided river and road
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/southern-alps-valley-with-braided-river-and-road-x3099
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts braided rivers in New Zealand's Southern Alps. Build a bounded regional landscape with steep mountains, braided river floor, vegetation bands, and a road on higher ground. Use explicit scale compression and coherent drainage. Low-poly.
Working Three.js Starter (southern-alps-valley-with-braided-river-and-road-x3099):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Braided Rivers — Southern Alps</title>
<style>
:root {
color-scheme: light;
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: #cbdde2;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(340px, calc(100vw - 36px));
padding: 16px 18px;
border: 1px solid rgba(255, 255, 255, 0.52);
border-radius: 12px;
color: #f5f4ec;
background: rgba(24, 39, 40, 0.79);
box-shadow: 0 12px 36px rgba(18, 31, 34, 0.18);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 5px;
color: #b6d2ce;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 4vw, 31px);
font-weight: 650;
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 7px;
color: #d1ddda;
font-size: 13px;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 15px;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.14);
}
.stat small {
display: block;
color: #9eb9b7;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.stat strong {
display: block;
margin-top: 3px;
color: #f6f3e8;
font-size: 12px;
font-weight: 550;
}
.controls {
position: fixed;
z-index: 10;
right: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 10px;
color: rgba(30, 47, 49, 0.8);
font-size: 11px;
pointer-events: none;
}
.controls span {
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 8px;
background: rgba(235, 242, 240, 0.76);
box-shadow: 0 5px 18px rgba(18, 31, 34, 0.1);
backdrop-filter: blur(8px);
}
button {
position: fixed;
z-index: 11;
right: 18px;
top: 18px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.62);
border-radius: 8px;
color: #243a3b;
background: rgba(239, 244, 241, 0.82);
box-shadow: 0 7px 20px rgba(18, 31, 34, 0.12);
font: inherit;
font-size: 11px;
cursor: pointer;
backdrop-filter: blur(8px);
}
button:hover {
background: rgba(255, 255, 255, 0.94);
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
color: #294244;
background: #cbdde2;
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 0.5s ease;
pointer-events: none;
}
#loading.hidden {
opacity: 0;
}
@media (max-width: 650px) {
.panel {
top: 12px;
left: 12px;
width: min(300px, calc(100vw - 24px));
padding: 13px 14px;
}
.stats {
margin-top: 10px;
padding-top: 9px;
}
.controls {
right: 12px;
bottom: 12px;
}
.controls span:nth-child(2),
.controls span:nth-child(3) {
display: none;
}
button {
top: 12px;
right: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly Southern Alps braided river landscape"></canvas>
<section class="panel">
<div class="eyebrow">Te Waipounamu · Regional Model</div>
<h1>Braided Rivers</h1>
<div class="subtitle">Steep greywacke ridges, beech forest bands, and a glacial outwash floor.</div>
<div class="stats">
<div class="stat">
<small>Drainage</small>
<strong>South → North</strong>
</div>
<div class="stat">
<small>Horizontal</small>
<strong>0.64× compressed</strong>
</div>
<div class="stat">
<small>Relief</small>
<strong>0.72× compressed</strong>
</div>
<div class="stat">
<small>Model Style</small>
<strong>Faceted low-poly</strong>
</div>
</div>
</section>
<div class="controls">
<span>Drag: orbit</span>
<span>Shift-drag: pan</span>
<span>Wheel: zoom</span>
</div>
<button id="reset" type="button">Reset view</button>
<div id="loading">Building the regional landscape…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Explicit display compression. Logical valley and mountain dimensions are
// compressed into the bounded regional scene.
// -------------------------------------------------------------------------
const COMPRESSION = Object.freeze({
horizontal: 0.64,
relief: 0.72
});
const REGION = Object.freeze({
halfWidth: 30,
halfLength: 36,
bottom: -3.1
});
const GRID = Object.freeze({
x: 112,
z: 168
});
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcbdde2);
scene.fog = new THREE.Fog(0xcbdde2, 92, 175);
const camera = new THREE.PerspectiveCamera(
44,
window.innerWidth / window.innerHeight,
0.1,
240
);
const hemisphere = new THREE.HemisphereLight(0xd8edf2, 0x615d48, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d2, 2.75);
sun.position.set(-34, 55, -27);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -48;
sun.shadow.camera.right = 48;
sun.shadow.camera.top = 52;
sun.shadow.camera.bottom = -52;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 140;
sun.shadow.bias = -0.00045;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const controls = {
target: new THREE.Vector3(0, 3.5, 0),
radius: 78,
theta: 0.72,
phi: 0.83
};
const initialRadius = () =>
window.innerWidth / window.innerHeight < 0.9 ? 104 : 78;
controls.radius = initialRadius();
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
function smoothstep(min, max, value) {
const t = clamp((value - min) / (max - min), 0, 1);
return t * t * (3 - 2 * t);
}
function fract(value) {
return value - Math.floor(value);
}
function hash2(x, z) {
return fract(Math.sin(x * 127.1 + z * 311.7) * 43758.5453123);
}
function valueNoise(x, z) {
const ix = Math.floor(x);
const iz = Math.floor(z);
const fx = x - ix;
const fz = z - iz;
const ux = fx * fx * (3 - 2 * fx);
const uz = fz * fz * (3 - 2 * 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 lerp(
lerp(a, b, ux),
lerp(c, d, ux),
uz
);
}
function fbm(x, z) {
let value = 0;
let amplitude = 0.55;
let frequency = 1;
for (let octave = 0; octave < 4; octave++) {
value += amplitude * valueNoise(x * frequency, z * frequency);
frequency *= 2.03;
amplitude *= 0.48;
}
return value;
}
function seededRandom(seed) {
return function () {
seed |= 0;
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(0x51a1f0);
// z increases downstream and northward. The channel elevation decreases
// monotonically along z, so every tributary can drain into the same trunk.
function valleyCenter(z) {
return (
1.35 * Math.sin(z * 0.105) +
0.62 * Math.sin(z * 0.29 + 0.4)
);
}
function riverLevel(z) {
return (0.6 + (REGION.halfLength - z) * 0.07) * COMPRESSION.relief;
}
function valleyHalfWidth(z) {
return (
7.8 +
1.15 * Math.sin(z * 0.10 + 1.1) +
0.45 * Math.sin(z * 0.31)
) * COMPRESSION.horizontal;
}
function braidOffset(z, start, end, amplitude) {
if (z <= start || z >= end) return 0;
const t = (z - start) / (end - start);
return amplitude * Math.pow(Math.sin(Math.PI * t), 0.88);
}
function baseTerrainHeight(x, z) {
const center = valleyCenter(z);
const distance = Math.abs(x - center);
const edge = valleyHalfWidth(z);
const floor =
riverLevel(z) +
0.28 +
0.13 * Math.sin(x * 0.83 + z * 0.21) +
0.09 * Math.sin(z * 0.69);
if (distance <= edge) return floor;
const steepWidth = 10 * COMPRESSION.horizontal;
const broadWidth = 25 * COMPRESSION.horizontal;
const steepT = clamp((distance - edge) / steepWidth, 0, 1);
const broadT = clamp((distance - edge) / broadWidth, 0, 1);
const steep = smoothstep(0, 1, steepT);
const broad = smoothstep(0, 1, broadT);
const ridgeNoise = fbm(x * 0.13 + 4, z * 0.13 - 7);
const foldedRidge =
0.6 +
0.26 * Math.sin(z * 0.18 + x * 0.07) +
0.14 * Math.sin(x * 0.34 - z * 0.09);
const steepRelief =
steep *
(15.5 + 4.2 * ridgeNoise + 2.1 * foldedRidge) *
COMPRESSION.relief;
const broadRelief =
broad *
(5.2 + 2.5 * valueNoise(x * 0.08, z * 0.08)) *
COMPRESSION.relief;
const eastPeak =
5.8 *
COMPRESSION.relief *
Math.exp(
-(
Math.pow((x - 16.5 - 1.7 * Math.sin(z * 0.08)) / 7.5, 2) +
Math.pow((z + 7) / 19, 2)
)
);
const westPeak =
6.3 *
COMPRESSION.relief *
Math.exp(
-(
Math.pow((x + 17 + 1.4 * Math.sin(z * 0.09)) / 8.0, 2) +
Math.pow((z - 3) / 21, 2)
)
);
const sideAsymmetry = x > center ? 1.08 : 0.96;
return (
riverLevel(z) +
steepRelief * sideAsymmetry +
broadRelief +
eastPeak +
westPeak
);
}
// -------------------------------------------------------------------------
// Coherent channel network.
//
// The central trunk continues through the entire region. Braided branches
// split from and rejoin that trunk. Mountain tributaries are interpolated
// from elevated source points to lower, monotonically draining junctions.
// -------------------------------------------------------------------------
const channelPaths = [];
function addLongitudinalPath(name, z0, z1, offsetFunction, width) {
const samples = [];
const count = Math.max(4, Math.ceil((z1 - z0) / 0.48));
for (let i = 0; i <= count; i++) {
const z = lerp(z0, z1, i / count);
const x = valleyCenter(z) + offsetFunction(z);
const waterY = riverLevel(z);
samples.push({
x,
z,
waterY,
bedY: waterY - 0.26,
width,
bedWidth: width * 1.32
});
}
const path = { name, samples };
channelPaths.push(path);
return path;
}
const trunkPath = addLongitudinalPath(
"Trunk",
-REGION.halfLength + 0.08,
REGION.halfLength - 0.08,
() => 0,
0.88
);
const westBranch = addLongitudinalPath(
"West braid",
-20,
16,
z => -braidOffset(z, -20, 16, 2.3),
0.63
);
const eastBranch = addLongitudinalPath(
"East braid",
-16,
20,
z => braidOffset(z, -16, 20, 2.25),
0.63
);
const innerWestBranch = addLongitudinalPath(
"Inner west braid",
-4,
30,
z => -braidOffset(z, -4, 30, 1.02),
0.42
);
const innerEastBranch = addLongitudinalPath(
"Inner east braid",
-28,
8,
z => braidOffset(z, -28, 8, 1.0),
0.42
);
function addTributary(name, side, sourceX, sourceZ, joinZ, sourceRelief) {
const samples = [];
const count = 54;
const joinX = valleyCenter(joinZ);
const joinBedY = riverLevel(joinZ) - 0.25;
const sourceBedY =
riverLevel(sourceZ) + sourceRelief * COMPRESSION.relief;
for (let i = 0; i <= count; i++) {
const t = i / count;
const curve = Math.sin(Math.PI * t);
const x =
lerp(sourceX, joinX, t) +
curve * 0.72 * Math.sin(t * Math.PI * 2.0);
const z =
lerp(sourceZ, joinZ, t) +
curve * (joinZ > sourceZ ? 1.25 : -1.05);
const bedY = lerp(sourceBedY, joinBedY, t);
samples.push({
x,
z,
bedY,
waterY: bedY + 0.25,
width: 0.34,
bedWidth: 0.54
});
}
const path = { name, samples };
channelPaths.push(path);
return path;
}
const tributaries = [
addTributary("Upper east tributary", 1, 15.5, -25, 6, 11),
addTributary("Upper west tributary", -1, -16.5, -4, -11, 12),
addTributary("Lower east tributary", 1, 18.5, 18, 27, 10),
addTributary("Lower west tributary", -1, -17.5, 30, 20, 9)
];
const channelSegments = [];
for (const path of channelPaths) {
for (let i = 0; i < path.samples.length - 1; i++) {
const a = path.samples[i];
const b = path.samples[i + 1];
channelSegments.push({
x1: a.x,
z1: a.z,
y1: a.bedY,
x2: b.x,
z2: b.z,
y2: b.bedY,
width: (a.bedWidth + b.bedWidth) * 0.5
});
}
}
function nearestChannel(x, z) {
let bestRatio = Infinity;
let bestY = 0;
for (const segment of channelSegments) {
const dx = segment.x2 - segment.x1;
const dz = segment.z2 - segment.z1;
const lengthSquared = dx * dx + dz * dz;
const t = lengthSquared > 0
? clamp(
((x - segment.x1) * dx + (z - segment.z1) * dz) /
lengthSquared,
0,
1
)
: 0;
const px = segment.x1 + dx * t;
const pz = segment.z1 + dz * t;
const distance = Math.hypot(x - px, z - pz);
const ratio = distance / segment.width;
if (ratio < bestRatio) {
bestRatio = ratio;
bestY = lerp(segment.y1, segment.y2, t);
}
}
return { ratio: bestRatio, bedY: bestY };
}
// The road occupies a cut bench on the eastern, higher ground.
function roadX(z) {
return (
valleyCenter(z) +
(12.8 + 1.15 * Math.sin(z * 0.17 + 0.8)) *
COMPRESSION.horizontal
);
}
function roadY(z) {
return (
riverLevel(z) +
(8.4 + 0.55 * Math.sin(z * 0.13)) * COMPRESSION.relief
);
}
function computeTerrainHeight(x, z) {
let height = baseTerrainHeight(x, z);
const channel = nearestChannel(x, z);
if (channel.ratio < 1.65) {
const cut = 1 - smoothstep(0.45, 1.65, channel.ratio);
const channelTarget =
channel.bedY + Math.max(0, channel.ratio - 0.45) * 0.18;
height = lerp(
height,
Math.min(height, channelTarget),
cut * 0.94
);
}
const roadDistance = Math.abs(x - roadX(z));
const roadBlend = 1 - smoothstep(0.52, 1.45, roadDistance);
height = lerp(height, roadY(z) - 0.07, roadBlend * 0.93);
return height;
}
// -------------------------------------------------------------------------
// Faceted terrain with face-level ecological color bands.
// -------------------------------------------------------------------------
const heights = new Float32Array((GRID.x + 1) * (GRID.z + 1));
const dx = (REGION.halfWidth * 2) / GRID.x;
const dz = (REGION.halfLength * 2) / GRID.z;
for (let iz = 0; iz <= GRID.z; iz++) {
const z = -REGION.halfLength + iz * dz;
for (let ix = 0; ix <= GRID.x; ix++) {
const x = -REGION.halfWidth + ix * dx;
heights[iz * (GRID.x + 1) + ix] = computeTerrainHeight(x, z);
}
}
const gravelBed = new THREE.Color(0x646d67);
const gravel = new THREE.Color(0x969487);
const tussock = new THREE.Color(0x858b50);
const forest = new THREE.Color(0x35533f);
const forestDark = new THREE.Color(0x2c4637);
const scrub = new THREE.Color(0x776a46);
const rock = new THREE.Color(0x747875);
const rockWarm = new THREE.Color(0x756d60);
const snow = new THREE.Color(0xd7dedc);
function faceColor(relativeHeight, slope, variation) {
let color;
if (relativeHeight < 0.18) {
color = gravelBed.clone();
} else if (relativeHeight < 1.25) {
color = gravel.clone();
} else if (relativeHeight < 3.1) {
color = tussock.clone();
} else if (relativeHeight < 8.3) {
color = forest.clone().lerp(forestDark, variation * 0.65);
} else if (relativeHeight < 11.5) {
color = scrub.clone();
} else if (relativeHeight < 16.1) {
color = rock.clone().lerp(rockWarm, variation * 0.55);
} else {
color = snow.clone();
}
if (
relativeHeight > 2.2 &&
relativeHeight < 9.4 &&
slope > 1.18
) {
color.lerp(rockWarm, 0.62);
}
color.offsetHSL(0, 0, (variation - 0.5) * 0.055);
return color;
}
const terrainPositions = [];
const terrainColors = [];
function pushTerrainTriangle(a, b, c, color) {
terrainPositions.push(
a[0], a[1], a[2],
b[0], b[1], b[2],
c[0], c[1], c[2]
);
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 = -REGION.halfLength + iz * dz;
const z1 = z0 + dz;
for (let ix = 0; ix < GRID.x; ix++) {
const x0 = -REGION.halfWidth + ix * dx;
const x1 = x0 + dx;
const i00 = iz * (GRID.x + 1) + ix;
const i10 = i00 + 1;
const i01 = i00 + (GRID.x + 1);
const i11 = i01 + 1;
const p00 = [x0, heights[i00], z0];
const p10 = [x1, heights[i10], z0];
const p01 = [x0, heights[i01], z1];
const p11 = [x1, heights[i11], z1];
const averageHeight =
(heights[i00] + heights[i10] + heights[i01] + heights[i11]) * 0.25;
const slopeX =
(heights[i10] + heights[i11] - heights[i00] - heights[i01]) /
(2 * dx);
const slopeZ =
(heights[i01] + heights[i11] - heights[i00] - heights[i10]) /
(2 * dz);
const slope = Math.hypot(slopeX, slopeZ);
const relativeHeight = averageHeight - riverLevel((z0 + z1) * 0.5);
const variation = hash2(ix * 17.13, iz * 29.71);
const color = faceColor(relativeHeight, slope, variation);
pushTerrainTriangle(p00, p01, p10, color);
pushTerrainTriangle(p10, p01, p11, color);
}
}
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: 0.98,
metalness: 0,
flatShading: true
});
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.receiveShadow = true;
terrain.castShadow = true;
scene.add(terrain);
// -------------------------------------------------------------------------
// Bounded diorama walls and base.
// -------------------------------------------------------------------------
const wallPositions = [];
const wallColors = [];
const wallTop = new THREE.Color(0x555851);
const wallBottom = new THREE.Color(0x282e2e);
function pushWallQuad(a, b) {
const ba = [a[0], REGION.bottom, a[2]];
const bb = [b[0], REGION.bottom, b[2]];
wallPositions.push(
...a, ...ba, ...b,
...b, ...ba, ...bb
);
for (let i = 0; i < 3; i++) {
wallColors.push(wallTop.r, wallTop.g, wallTop.b);
}
for (let i = 0; i < 3; i++) {
wallColors.push(wallBottom.r, wallBottom.g, wallBottom.b);
}
}
for (let ix = 0; ix < GRID.x; ix++) {
const x0 = -REGION.halfWidth + ix * dx;
const x1 = x0 + dx;
const southIndex = ix;
const northIndex = GRID.z * (GRID.x + 1) + ix;
pushWallQuad(
[x0, heights[southIndex], -REGION.halfLength],
[x1, heights[southIndex + 1], -REGION.halfLength]
);
pushWallQuad(
[x1, heights[northIndex + 1], REGION.halfLength],
[x0, heights[northIndex], REGION.halfLength]
);
}
for (let iz = 0; iz < GRID.z; iz++) {
const z0 = -REGION.halfLength + iz * dz;
const z1 = z0 + dz;
const westIndex = iz * (GRID.x + 1);
const eastIndex = westIndex + GRID.x;
pushWallQuad(
[-REGION.halfWidth, heights[westIndex], z1],
[-REGION.halfWidth, heights[westIndex], z0]
);
pushWallQuad(
[REGION.halfWidth, heights[eastIndex], z0],
[REGION.halfWidth, heights[eastIndex + 1], z1]
);
}
const wallGeometry = new THREE.BufferGeometry();
wallGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(wallPositions, 3)
);
wallGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(wallColors, 3)
);
wallGeometry.computeVertexNormals();
const walls = new THREE.Mesh(
wallGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
})
);
walls.castShadow = true;
walls.receiveShadow = true;
scene.add(walls);
const base = new THREE.Mesh(
new THREE.BoxGeometry(
REGION.halfWidth * 2,
0.7,
REGION.halfLength * 2
),
new THREE.MeshStandardMaterial({
color: 0x252c2d,
roughness: 1
})
);
base.position.y = REGION.bottom - 0.35;
base.receiveShadow = true;
scene.add(base);
// -------------------------------------------------------------------------
// Water ribbons.
// -------------------------------------------------------------------------
function makeRibbon(path, widthScale, material, yOffset = 0) {
const samples = path.samples;
const positions = [];
const indices = [];
for (let i = 0; i < samples.length; i++) {
const previous = samples[Math.max(0, i - 1)];
const next = samples[Math.min(samples.length - 1, i + 1)];
const sample = samples[i];
const tangentX = next.x - previous.x;
const tangentZ = next.z - previous.z;
const length = Math.hypot(tangentX, tangentZ) || 1;
const normalX = -tangentZ / length;
const normalZ = tangentX / length;
const halfWidth = sample.width * widthScale * 0.5;
const y = sample.waterY + yOffset;
positions.push(
sample.x + normalX * halfWidth,
y,
sample.z + normalZ * halfWidth,
sample.x - normalX * halfWidth,
y,
sample.z - normalZ * halfWidth
);
if (i < samples.length - 1) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x4d9eae,
roughness: 0.24,
metalness: 0.08,
side: THREE.DoubleSide
});
const tributaryMaterial = new THREE.MeshStandardMaterial({
color: 0x6caeb3,
roughness: 0.3,
metalness: 0.04,
side: THREE.DoubleSide
});
makeRibbon(trunkPath, 0.88, waterMaterial);
makeRibbon(westBranch, 1, waterMaterial);
makeRibbon(eastBranch, 1, waterMaterial);
makeRibbon(innerWestBranch, 1, waterMaterial);
makeRibbon(innerEastBranch, 1, waterMaterial);
for (const tributary of tributaries) {
makeRibbon(tributary, 1, tributaryMaterial);
}
// -------------------------------------------------------------------------
// Higher-ground road with shoulder, asphalt, center dashes, and posts.
// -------------------------------------------------------------------------
const roadSamples = [];
const roadCount = 150;
for (let i = 0; i <= roadCount; i++) {
const z = lerp(
-REGION.halfLength + 0.45,
REGION.halfLength - 0.45,
i / roadCount
);
roadSamples.push({
x: roadX(z),
z,
waterY: roadY(z),
width: 0.64
});
}
const roadPath = { samples: roadSamples };
const shoulderMaterial = new THREE.MeshStandardMaterial({
color: 0x918a77,
roughness: 1,
side: THREE.DoubleSide
});
const asphaltMaterial = new THREE.MeshStandardMaterial({
color: 0x353c3d,
roughness: 0.95,
side: THREE.DoubleSide
});
makeRibbon(roadPath, 1.5, shoulderMaterial, -0.025);
makeRibbon(roadPath, 1, asphaltMaterial, 0.025);
const dashPositions = [];
function addDash(a, b, width, y) {
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz) || 1;
const nx = -dz / length;
const nz = dx / length;
const p0 = [a.x + nx * width, y, a.z + nz * width];
const p1 = [a.x - nx * width, y, a.z - nz * width];
const p2 = [b.x + nx * width, y, b.z + nz * width];
const p3 = [b.x - nx * width, y, b.z - nz * width];
dashPositions.push(...p0, ...p2, ...p1, ...p1, ...p2, ...p3);
}
for (let i = 2; i < roadSamples.length - 3; i += 4) {
const a = roadSamples[i];
const b = roadSamples[i + 1];
const middleA = {
x: lerp(a.x, b.x, 0.08),
z: lerp(a.z, b.z, 0.08)
};
const middleB = {
x: lerp(a.x, b.x, 0.82),
z: lerp(a.z, b.z, 0.82)
};
addDash(middleA, middleB, 0.045, roadY(middleA.z) + 0.075);
}
const dashGeometry = new THREE.BufferGeometry();
dashGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dashPositions, 3)
);
dashGeometry.computeVertexNormals();
const dashes = new THREE.Mesh(
dashGeometry,
new THREE.MeshBasicMaterial({
color: 0xd8d0ae,
side: THREE.DoubleSide
})
);
scene.add(dashes);
const postPositions = [];
for (let i = 2; i < roadSamples.length - 2; i += 5) {
const previous = roadSamples[i - 1];
const next = roadSamples[i + 1];
const sample = roadSamples[i];
const tangentX = next.x - previous.x;
const tangentZ = next.z - previous.z;
const length = Math.hypot(tangentX, tangentZ) || 1;
const normalX = -tangentZ / length;
const normalZ = tangentX / length;
// The uphill side is east of the road.
postPositions.push(
sample.x - normalX * 0.53,
roadY(sample.z) + 0.23,
sample.z - normalZ * 0.53
);
}
const postGeometry = new THREE.CylinderGeometry(0.035, 0.045, 0.46, 5);
const postMaterial = new THREE.MeshStandardMaterial({
color: 0xc4c2b5,
roughness: 0.8
});
const posts = new THREE.InstancedMesh(
postGeometry,
postMaterial,
postPositions.length / 3
);
const dummy = new THREE.Object3D();
for (let i = 0; i < postPositions.length / 3; i++) {
dummy.position.set(
postPositions[i * 3],
postPositions[i * 3 + 1],
postPositions[i * 3 + 2]
);
dummy.updateMatrix();
posts.setMatrixAt(i, dummy.matrix);
}
posts.instanceMatrix.needsUpdate = true;
posts.castShadow = true;
scene.add(posts);
// -------------------------------------------------------------------------
// Vegetation bands: low-poly forest, tussock clumps, and alpine rocks.
// -------------------------------------------------------------------------
function terrainSlope(x, z) {
const e = 0.65;
const hx = computeTerrainHeight(x + e, z) - computeTerrainHeight(x - e, z);
const hz = computeTerrainHeight(x, z + e) - computeTerrainHeight(x, z - e);
return Math.hypot(hx, hz) / (2 * e);
}
const treeData = [];
for (let attempt = 0; attempt < 6000 && treeData.length < 430; attempt++) {
const x = lerp(-27, 27, random());
const z = lerp(-33.8, 33.8, random());
const height = computeTerrainHeight(x, z);
const relativeHeight = height - riverLevel(z);
if (relativeHeight < 2.5 || relativeHeight > 8.3) continue;
if (Math.abs(x - valleyCenter(z)) < 6.6) continue;
if (Math.abs(x - roadX(z)) < 1.7) continue;
if (nearestChannel(x, z).ratio < 1.55) continue;
if (terrainSlope(x, z) > 1.28) continue;
treeData.push({
x,
z,
y: height,
scale: lerp(0.66, 1.28, random()),
rotation: random() * Math.PI * 2,
tint: random()
});
}
const canopyGeometry = new THREE.ConeGeometry(0.28, 1.35, 5, 1);
canopyGeometry.translate(0, 0.675, 0);
const canopyMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const canopies = new THREE.InstancedMesh(
canopyGeometry,
canopyMaterial,
treeData.length
);
const trunkGeometry = new THREE.CylinderGeometry(0.035, 0.055, 0.5, 5);
trunkGeometry.translate(0, 0.25, 0);
const trunks = new THREE.InstancedMesh(
trunkGeometry,
new THREE.MeshStandardMaterial({
color: 0x514538,
roughness: 1,
flatShading: true
}),
treeData.length
);
for (let i = 0; i < treeData.length; i++) {
const tree = treeData[i];
dummy.position.set(tree.x, tree.y, tree.z);
dummy.rotation.set(0, tree.rotation, 0);
dummy.scale.set(
tree.scale,
tree.scale * lerp(0.88, 1.17, tree.tint),
tree.scale
);
dummy.updateMatrix();
canopies.setMatrixAt(i, dummy.matrix);
const green = new THREE.Color().setHSL(
lerp(0.30, 0.36, tree.tint),
lerp(0.25, 0.42, tree.tint),
lerp(0.18, 0.28, tree.tint)
);
canopies.setColorAt(i, green);
dummy.scale.set(tree.scale, tree.scale, tree.scale);
dummy.updateMatrix();
trunks.setMatrixAt(i, dummy.matrix);
}
canopies.instanceMatrix.needsUpdate = true;
if (canopies.instanceColor) canopies.instanceColor.needsUpdate = true;
trunks.instanceMatrix.needsUpdate = true;
canopies.castShadow = true;
canopies.receiveShadow = true;
trunks.castShadow = true;
scene.add(canopies);
scene.add(trunks);
const tussockData = [];
for (let attempt = 0; attempt < 2200 && tussockData.length < 190; attempt++) {
const z = lerp(-33.5, 33.5, random());
const center = valleyCenter(z);
const x = center + (random() < 0.5 ? -1 : 1) * lerp(4.4, 8.0, random());
const height = computeTerrainHeight(x, z);
const relativeHeight = height - riverLevel(z);
if (relativeHeight < 1.0 || relativeHeight > 3.0) continue;
if (nearestChannel(x, z).ratio < 1.4) continue;
tussockData.push({
x,
z,
y: height,
scale: lerp(0.65, 1.35, random()),
rotation: random() * Math.PI
});
}
const tussockGeometry = new THREE.IcosahedronGeometry(0.2, 0);
const tussocks = new THREE.InstancedMesh(
tussockGeometry,
new THREE.MeshStandardMaterial({
color: 0x727c43,
roughness: 1,
flatShading: true
}),
tussockData.length
);
for (let i = 0; i < tussockData.length; i++) {
const shrub = tussockData[i];
dummy.position.set(shrub.x, shrub.y + 0.1, shrub.z);
dummy.rotation.set(0, shrub.rotation, 0);
dummy.scale.set(
shrub.scale * 1.25,
shrub.scale * 0.55,
shrub.scale * 1.25
);
dummy.updateMatrix();
tussocks.setMatrixAt(i, dummy.matrix);
}
tussocks.instanceMatrix.needsUpdate = true;
tussocks.castShadow = true;
scene.add(tussocks);
const rockData = [];
for (let attempt = 0; attempt < 1400 && rockData.length < 75; attempt++) {
const x = lerp(-28, 28, random());
const z = lerp(-34, 34, random());
const height = computeTerrainHeight(x, z);
const relativeHeight = height - riverLevel(z);
if (relativeHeight < 9.5) continue;
if (nearestChannel(x, z).ratio < 1.7) continue;
rockData.push({
x,
z,
y: height,
scale: lerp(0.45, 1.15, random()),
rotation: random() * Math.PI
});
}
const alpineRocks = new THREE.InstancedMesh(
new THREE.DodecahedronGeometry(0.48, 0),
new THREE.MeshStandardMaterial({
color: 0x777a75,
roughness: 1,
flatShading: true
}),
rockData.length
);
for (let i = 0; i < rockData.length; i++) {
const rock = rockData[i];
dummy.position.set(rock.x, rock.y + 0.12, rock.z);
dummy.rotation.set(
random() * 0.5,
rock.rotation,
random() * 0.5
);
dummy.scale.set(
rock.scale,
rock.scale * lerp(0.55, 0.9, random()),
rock.scale
);
dummy.updateMatrix();
alpineRocks.setMatrixAt(i, dummy.matrix);
}
alpineRocks.instanceMatrix.needsUpdate = true;
alpineRocks.castShadow = true;
alpineRocks.receiveShadow = true;
scene.add(alpineRocks);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, wheel zoom, touch pinch, and keyboard.
// No camera-control addons are imported.
// -------------------------------------------------------------------------
const pointers = new Map();
let mousePan = false;
let pinchDistance = 0;
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);
}
function orbit(deltaX, deltaY) {
controls.theta -= deltaX * 0.006;
controls.phi = clamp(controls.phi - deltaY * 0.005, 0.28, 1.43);
}
function pan(deltaX, deltaY) {
const forward = new THREE.Vector3()
.subVectors(controls.target, camera.position);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3(
-forward.z,
0,
forward.x
);
const scale = controls.radius * 0.00135;
controls.target.addScaledVector(right, -deltaX * scale);
controls.target.addScaledVector(forward, deltaY * scale);
controls.target.x = clamp(controls.target.x, -26, 26);
controls.target.z = clamp(controls.target.z, -32, 32);
}
function pairDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
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 (event.pointerType === "mouse") {
mousePan = event.shiftKey || event.button === 2;
} else {
mousePan = false;
}
if (pointers.size === 2) {
pinchDistance = pairDistance();
}
});
canvas.addEventListener("pointermove", event => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const deltaX = event.clientX - previous.x;
const deltaY = event.clientY - previous.y;
if (pointers.size === 1) {
if (mousePan) {
pan(deltaX, deltaY);
} else {
orbit(deltaX, deltaY);
}
} else if (pointers.size === 2) {
const oldDistance = pinchDistance;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
const newDistance = pairDistance();
if (oldDistance > 0 && newDistance > 0) {
controls.radius = clamp(
controls.radius * (oldDistance / newDistance),
28,
150
);
}
pinchDistance = newDistance;
return;
}
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
});
function releasePointer(event) {
pointers.delete(event.pointerId);
pinchDistance = pointers.size === 2 ? pairDistance() : 0;
}
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),
28,
150
);
},
{ passive: false }
);
window.addEventListener("keydown", event => {
if (["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", "+", "-", "="].includes(event.key)) {
event.preventDefault();
}
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.28, 1.43);
if (event.key === "ArrowDown") controls.phi = clamp(controls.phi + 0.06, 0.28, 1.43);
if (event.key === "+" || event.key === "=") controls.radius = clamp(controls.radius * 0.9, 28, 150);
if (event.key === "-") controls.radius = clamp(controls.radius * 1.1, 28, 150);
});
document.querySelector("#reset").addEventListener("click", () => {
controls.target.set(0, 3.5, 0);
controls.radius = initialRadius();
controls.theta = 0.72;
controls.phi = 0.83;
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
});
updateCamera();
renderer.setAnimationLoop(() => {
updateCamera();
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 600);
});
</script>
</body>
</html>
```