Prompt specification
Build or customize the Three.js 3D scene "Small stream entering a Great Lakes beach" 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: Small stream entering a Great Lakes beach
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/small-stream-entering-a-great-lakes-beach-x3078
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring Great Lakes shorelines and dunes. Create a compact shoreline vignette with tributary mouth, beach, and low dunes. Show freshwater moving into the lake and keep the river channel continuous across the sand. Low-poly.
Working Three.js Starter (small-stream-entering-a-great-lakes-beach-x3078):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Great Lakes Tributary Mouth</title>
<style>
:root {
color-scheme: light;
--ink: #17353c;
--glass: rgba(247, 250, 242, 0.78);
--line: rgba(23, 53, 60, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
background: #b9d8d9;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(
ellipse at 51% 44%,
transparent 45%,
rgba(25, 58, 63, 0.12) 78%,
rgba(20, 49, 55, 0.28) 115%
);
}
.panel {
position: fixed;
z-index: 5;
color: var(--ink);
}
.title-card {
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 17px 19px 16px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--glass);
box-shadow: 0 12px 36px rgba(25, 57, 62, 0.13);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 7px;
color: #477077;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 30px);
font-weight: 720;
line-height: 1.02;
letter-spacing: -0.045em;
}
.subtitle {
margin: 9px 0 0;
color: #4d6b6f;
font-size: 12px;
line-height: 1.45;
}
.controls {
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 13px;
background: var(--glass);
box-shadow: 0 10px 30px rgba(25, 57, 62, 0.12);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
button {
appearance: none;
border: 1px solid rgba(23, 53, 60, 0.17);
border-radius: 9px;
padding: 9px 12px;
color: #24464c;
background: rgba(255, 255, 255, 0.66);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
background 150ms ease,
transform 150ms ease;
}
button:hover {
background: #fff;
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid #2c8f9b;
outline-offset: 2px;
}
.hint {
position: fixed;
z-index: 5;
left: 22px;
bottom: 23px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
color: #42666b;
background: rgba(247, 250, 242, 0.68);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.03em;
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
pointer-events: none;
}
.legend {
display: flex;
gap: 15px;
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid rgba(23, 53, 60, 0.12);
color: #46666a;
font-size: 10px;
font-weight: 650;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 18px;
height: 4px;
border-radius: 5px;
}
.swatch.fresh {
background: #70d5cf;
}
.swatch.lake {
background: #246d82;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: #2f6067;
background: #c4dedd;
font-size: 12px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
@media (max-width: 620px) {
.title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle,
.legend {
display: none;
}
.controls {
right: 12px;
bottom: 12px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div class="vignette"></div>
<section class="panel title-card">
<div class="eyebrow">Great Lakes shoreline study</div>
<h1>Tributary Mouth</h1>
<p class="subtitle">
Freshwater threads through a low dune coast and opens into the lake.
</p>
<div class="legend">
<span><i class="swatch fresh"></i> Freshwater</span>
<span><i class="swatch lake"></i> Lake</span>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<nav class="panel controls" aria-label="Scene controls">
<button id="flowButton" type="button">Pause current</button>
<button id="resetButton" type="button">Reset view</button>
</nav>
<div id="loading">Loading shoreline</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, and lighting
// -------------------------------------------------------------------------
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xbad8d8);
scene.fog = new THREE.FogExp2(0xbad8d8, 0.0125);
const camera = new THREE.PerspectiveCamera(
41,
window.innerWidth / window.innerHeight,
0.1,
220
);
const cameraTarget = new THREE.Vector3(0, 0.25, 7);
const cameraState = {
theta: 0.27,
phi: 1.02,
radius: 51
};
const desiredCamera = { ...cameraState };
const hemisphere = new THREE.HemisphereLight(0xd9f0ed, 0x806d46, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.15);
sun.position.set(-22, 36, 24);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -48;
sun.shadow.camera.right = 48;
sun.shadow.camera.top = 48;
sun.shadow.camera.bottom = -48;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 110;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9fdde1, 0.65);
fill.position.set(24, 16, -24);
scene.add(fill);
// -------------------------------------------------------------------------
// Deterministic helpers
// -------------------------------------------------------------------------
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
function fract(value) {
return value - Math.floor(value);
}
function smoothstep(edge0, edge1, value) {
const t = clamp((value - edge0) / (edge1 - edge0), 0, 1);
return t * t * (3 - 2 * t);
}
function hash2(x, z) {
return fract(Math.sin(x * 127.1 + z * 311.7) * 43758.5453123);
}
function noise2(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 seededRandom(seed = 73421) {
let state = seed >>> 0;
return () => {
state = 1664525 * state + 1013904223;
return (state >>> 0) / 4294967296;
};
}
const random = seededRandom();
// -------------------------------------------------------------------------
// Lake shoreline and continuous river path
// -------------------------------------------------------------------------
function shorelineZ(x) {
return (
-1.2 +
1.05 * Math.sin(x * 0.135) +
0.52 * Math.sin(x * 0.31 + 1.4) +
2.8 * Math.exp(-Math.pow((x + 25) / 7.5, 2)) -
1.7 * Math.exp(-Math.pow((x - 19) / 6.2, 2))
);
}
const riverCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(12, 0, 54),
new THREE.Vector3(11, 0, 42),
new THREE.Vector3(7, 0, 31),
new THREE.Vector3(2, 0, 22),
new THREE.Vector3(-2, 0, 13),
new THREE.Vector3(1, 0, 5),
new THREE.Vector3(-1, 0, -5),
new THREE.Vector3(-4, 0, -15)
],
false,
"catmullrom",
0.35
);
function riverHeight(t) {
return 0.42 - 0.28 * t;
}
function riverHalfWidth(t) {
return 1.42 + 0.34 * Math.sin(Math.PI * t);
}
const riverSamples = [];
const riverSampleCount = 100;
for (let i = 0; i < riverSampleCount; i++) {
const t = i / (riverSampleCount - 1);
const point = riverCurve.getPointAt(t);
riverSamples.push({
x: point.x,
z: point.z,
y: riverHeight(t),
halfWidth: riverHalfWidth(t)
});
}
function nearestRiver(x, z) {
let bestDistance = Infinity;
let best = riverSamples[0];
for (const sample of riverSamples) {
const dx = x - sample.x;
const dz = z - sample.z;
const distance = Math.sqrt(dx * dx + dz * dz);
if (distance < bestDistance) {
bestDistance = distance;
best = sample;
}
}
return { distance: bestDistance, sample: best };
}
function terrainHeightAt(x, z) {
const shore = shorelineZ(x);
const inland = z - shore;
let height =
0.155 +
0.48 * smoothstep(0, 7.2, inland) +
0.07 * noise2(x * 0.24, z * 0.24);
const duneMask = smoothstep(3.5, 8, inland);
const ridgeA =
0.72 *
Math.exp(-Math.pow((inland - (8.4 + 1.6 * Math.sin(x * 0.13))) / 2.9, 2)) *
Math.exp(-Math.pow((x + 18) / 12, 2));
const ridgeB =
0.58 *
Math.exp(-Math.pow((inland - (6.6 + 1.2 * Math.sin(x * 0.18 + 2))) / 2.5, 2)) *
Math.exp(-Math.pow((x - 17) / 10, 2));
const longDune =
0.25 *
(0.5 + 0.5 * Math.sin(x * 0.16 + inland * 0.31)) *
Math.exp(-Math.pow((inland - 13) / 7, 2));
height += duneMask * (ridgeA + ridgeB + longDune);
const river = nearestRiver(x, z);
const { distance, sample } = river;
const bankWidth = 2.8;
if (distance < sample.halfWidth + bankWidth) {
const edgeRise = Math.max(0, distance - sample.halfWidth) * 0.78;
const centerDrop = 0.13 * (1 - clamp(distance / sample.halfWidth, 0, 1));
const channelHeight = sample.y + edgeRise - centerDrop;
const influence = 1 - smoothstep(
sample.halfWidth,
sample.halfWidth + bankWidth,
distance
);
height = lerp(height, Math.min(height, channelHeight), influence);
}
return height;
}
// -------------------------------------------------------------------------
// Low-poly faceted dune and beach terrain
// -------------------------------------------------------------------------
const terrainColumns = 92;
const terrainRows = 68;
const terrainXMin = -50;
const terrainXMax = 50;
const terrainZMax = 60;
const terrainPositions = [];
const terrainColors = [];
const terrainIndices = [];
const sandColor = new THREE.Color(0xd8bd82);
const paleSandColor = new THREE.Color(0xe4cf9a);
const duneColor = new THREE.Color(0x93985b);
const greenDuneColor = new THREE.Color(0x74814a);
const wetSandColor = new THREE.Color(0xa88d61);
for (let row = 0; row <= terrainRows; row++) {
const rowT = row / terrainRows;
const zT = Math.pow(rowT, 1.14);
for (let column = 0; column <= terrainColumns; column++) {
const x = lerp(terrainXMin, terrainXMax, column / terrainColumns);
const shore = shorelineZ(x);
const z = lerp(shore, terrainZMax, zT);
const inland = z - shore;
const y = terrainHeightAt(x, z);
terrainPositions.push(x, y, z);
const river = nearestRiver(x, z);
const colorNoise = noise2(x * 0.34, z * 0.28);
let color = paleSandColor.clone().lerp(sandColor, 0.32 + colorNoise * 0.4);
const vegetation = smoothstep(3.7, 10, inland) * (0.48 + colorNoise * 0.38);
color.lerp(duneColor.clone().lerp(greenDuneColor, colorNoise), vegetation);
if (river.distance < river.sample.halfWidth + 1.2) {
color.lerp(wetSandColor, 0.42);
}
const shade = 0.93 + hash2(x * 2.7, z * 1.9) * 0.12;
color.multiplyScalar(shade);
terrainColors.push(color.r, color.g, color.b);
}
}
const stride = terrainColumns + 1;
for (let row = 0; row < terrainRows; row++) {
for (let column = 0; column < terrainColumns; column++) {
const a = row * stride + column;
const b = a + 1;
const c = a + stride;
const d = c + 1;
terrainIndices.push(a, c, b, b, c, d);
}
}
const terrainGeometry = new THREE.BufferGeometry();
terrainGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(terrainPositions, 3)
);
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.setIndex(terrainIndices);
terrainGeometry.computeVertexNormals();
const terrainMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
metalness: 0,
flatShading: true
});
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.receiveShadow = true;
terrain.castShadow = true;
scene.add(terrain);
// -------------------------------------------------------------------------
// Animated lake surface
// -------------------------------------------------------------------------
const lakeUniforms = {
uTime: { value: 0 },
uDeepColor: { value: new THREE.Color(0x175c73) },
uShallowColor: { value: new THREE.Color(0x3b8e99) },
uSkyColor: { value: new THREE.Color(0x78b8bb) }
};
const lakeMaterial = new THREE.ShaderMaterial({
uniforms: lakeUniforms,
vertexShader: `
uniform float uTime;
varying vec3 vWorldPosition;
varying vec2 vUv;
void main() {
vec3 p = position;
float wave =
sin(p.x * 0.27 + p.y * 0.19 - uTime * 0.62) * 0.025 +
sin(p.x * 0.61 - p.y * 0.42 + uTime * 0.45) * 0.013;
p.z += wave;
vec4 world = modelMatrix * vec4(p, 1.0);
vWorldPosition = world.xyz;
vUv = uv;
gl_Position = projectionMatrix * viewMatrix * world;
}
`,
fragmentShader: `
uniform float uTime;
uniform vec3 uDeepColor;
uniform vec3 uShallowColor;
uniform vec3 uSkyColor;
varying vec3 vWorldPosition;
varying vec2 vUv;
void main() {
float broad = sin(
vWorldPosition.x * 0.28 +
vWorldPosition.z * 0.41 -
uTime * 0.48
);
float crossWave = sin(
vWorldPosition.x * 0.71 -
vWorldPosition.z * 0.23 +
uTime * 0.34
);
float crest = pow(
max(0.0, sin(vWorldPosition.x * 1.32 + vWorldPosition.z * 0.74 - uTime)),
18.0
);
vec3 color = mix(uDeepColor, uShallowColor, 0.34 + broad * 0.12);
color = mix(color, uSkyColor, 0.08 + crossWave * 0.035);
color += crest * vec3(0.13, 0.17, 0.16);
gl_FragColor = vec4(color, 1.0);
}
`,
side: THREE.DoubleSide
});
const lakeGeometry = new THREE.PlaneGeometry(180, 180, 90, 90);
const lake = new THREE.Mesh(lakeGeometry, lakeMaterial);
lake.rotation.x = -Math.PI / 2;
lake.position.set(0, 0.1, -35);
lake.receiveShadow = true;
scene.add(lake);
// -------------------------------------------------------------------------
// Continuous freshwater ribbon
// -------------------------------------------------------------------------
const riverUniforms = {
uTime: { value: 0 },
uFreshColor: { value: new THREE.Color(0x55c6c1) },
uDeepColor: { value: new THREE.Color(0x187989) },
uFoamColor: { value: new THREE.Color(0xd9fff0) }
};
const riverMaterial = new THREE.ShaderMaterial({
uniforms: riverUniforms,
vertexShader: `
uniform float uTime;
varying vec2 vUv;
varying vec3 vWorldPosition;
void main() {
vec3 p = position;
p.y +=
sin(p.x * 1.4 + p.z * 0.8 + uTime * 1.35) * 0.012 +
sin(p.z * 2.2 - uTime * 1.7) * 0.007;
vec4 world = modelMatrix * vec4(p, 1.0);
vWorldPosition = world.xyz;
vUv = uv;
gl_Position = projectionMatrix * viewMatrix * world;
}
`,
fragmentShader: `
uniform float uTime;
uniform vec3 uFreshColor;
uniform vec3 uDeepColor;
uniform vec3 uFoamColor;
varying vec2 vUv;
varying vec3 vWorldPosition;
void main() {
float movingPhase = vUv.y * 42.0 - uTime * 1.05;
float streak = pow(
max(0.0, sin(movingPhase + sin(vUv.x * 10.0) * 0.65)),
13.0
);
float finePhase = vUv.y * 91.0 - uTime * 1.5;
float fineStreak = pow(
max(0.0, sin(finePhase + vUv.x * 17.0)),
22.0
);
float center = 1.0 - abs(vUv.x - 0.5) * 0.25;
vec3 color = mix(uDeepColor, uFreshColor, 0.55 + center * 0.16);
color += streak * 0.20;
color += fineStreak * 0.12;
float bankFoam =
smoothstep(0.43, 0.5, abs(vUv.x - 0.5)) *
(0.45 + 0.55 * sin(vUv.y * 55.0 - uTime * 1.2));
color = mix(color, uFoamColor, bankFoam * 0.12);
gl_FragColor = vec4(color, 1.0);
}
`,
side: THREE.DoubleSide
});
const riverPositions = [];
const riverUvs = [];
const riverIndices = [];
const riverRows = 110;
const riverColumns = 6;
for (let row = 0; row <= riverRows; row++) {
const t = row / riverRows;
const point = riverCurve.getPointAt(t);
const tangent = riverCurve.getTangentAt(t).normalize();
const side = new THREE.Vector3(-tangent.z, 0, tangent.x).normalize();
const halfWidth = riverHalfWidth(t);
const y = riverHeight(t);
for (let column = 0; column <= riverColumns; column++) {
const u = column / riverColumns;
const offset = (u - 0.5) * 2 * halfWidth;
riverPositions.push(
point.x + side.x * offset,
y,
point.z + side.z * offset
);
riverUvs.push(u, t);
}
}
const riverStride = riverColumns + 1;
for (let row = 0; row < riverRows; row++) {
for (let column = 0; column < riverColumns; column++) {
const a = row * riverStride + column;
const b = a + 1;
const c = a + riverStride;
const d = c + 1;
riverIndices.push(a, b, c, b, d, c);
}
}
const riverGeometry = new THREE.BufferGeometry();
riverGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(riverPositions, 3)
);
riverGeometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(riverUvs, 2)
);
riverGeometry.setIndex(riverIndices);
riverGeometry.computeVertexNormals();
const riverMesh = new THREE.Mesh(riverGeometry, riverMaterial);
riverMesh.renderOrder = 2;
scene.add(riverMesh);
// -------------------------------------------------------------------------
// Clear shallow-water band and broken shoreline foam
// -------------------------------------------------------------------------
const shallowPositions = [];
const shallowUvs = [];
const shallowIndices = [];
const foamPositions = [];
const foamUvs = [];
const foamIndices = [];
const shorelineSegments = 170;
for (let i = 0; i <= shorelineSegments; i++) {
const u = i / shorelineSegments;
const x = lerp(-50, 50, u);
const shore = shorelineZ(x);
shallowPositions.push(x, 0.163, shore - 6);
shallowPositions.push(x, 0.163, shore + 0.05);
shallowUvs.push(u, 0, u, 1);
foamPositions.push(x, 0.182, shore - 0.16);
foamPositions.push(x, 0.182, shore + 0.16);
foamUvs.push(u, 0, u, 1);
}
for (let i = 0; i < shorelineSegments; i++) {
const a = i * 2;
shallowIndices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
foamIndices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
const shallowGeometry = new THREE.BufferGeometry();
shallowGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(shallowPositions, 3)
);
shallowGeometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(shallowUvs, 2)
);
shallowGeometry.setIndex(shallowIndices);
const shallowMaterial = new THREE.ShaderMaterial({
uniforms: {
uTime: { value: 0 },
uColor: { value: new THREE.Color(0x80d3c4) }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float uTime;
uniform vec3 uColor;
varying vec2 vUv;
void main() {
float edge = pow(vUv.y, 1.7);
float pulse = 0.82 + 0.18 * sin(vUv.x * 105.0 - uTime * 0.8);
gl_FragColor = vec4(uColor, edge * 0.23 * pulse);
}
`,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const shallowBand = new THREE.Mesh(shallowGeometry, shallowMaterial);
shallowBand.renderOrder = 1;
scene.add(shallowBand);
const foamGeometry = new THREE.BufferGeometry();
foamGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(foamPositions, 3)
);
foamGeometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(foamUvs, 2)
);
foamGeometry.setIndex(foamIndices);
const foamMaterial = new THREE.ShaderMaterial({
uniforms: {
uTime: { value: 0 },
uColor: { value: new THREE.Color(0xe9fff1) }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float uTime;
uniform vec3 uColor;
varying vec2 vUv;
void main() {
float across = 1.0 - smoothstep(0.08, 0.5, abs(vUv.y - 0.5));
float broken = smoothstep(
-0.25,
0.55,
sin(vUv.x * 155.0 + sin(vUv.x * 41.0) * 0.7 - uTime * 0.65)
);
float pulse = 0.68 + 0.32 * sin(vUv.x * 72.0 - uTime * 1.15);
float alpha = across * broken * pulse * 0.72;
gl_FragColor = vec4(uColor, alpha);
}
`,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const foam = new THREE.Mesh(foamGeometry, foamMaterial);
foam.renderOrder = 4;
scene.add(foam);
// -------------------------------------------------------------------------
// Small moving current streaks
// -------------------------------------------------------------------------
const currentStreaks = [];
const streakGeometry = new THREE.PlaneGeometry(0.055, 0.72);
streakGeometry.rotateX(-Math.PI / 2);
for (let i = 0; i < 23; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xe4fff5,
transparent: true,
opacity: 0.28 + random() * 0.3,
depthWrite: false,
toneMapped: false
});
const streak = new THREE.Mesh(streakGeometry, material);
streak.userData.start = random();
streak.userData.speed = 0.018 + random() * 0.012;
streak.userData.phase = random() * Math.PI * 2;
streak.renderOrder = 5;
scene.add(streak);
currentStreaks.push(streak);
}
function updateCurrentStreaks(flowTime) {
for (const streak of currentStreaks) {
const t = (streak.userData.start + flowTime * streak.userData.speed) % 1;
const point = riverCurve.getPointAt(t);
const tangent = riverCurve.getTangentAt(t);
streak.position.set(
point.x,
riverHeight(t) + 0.029,
point.z
);
streak.rotation.y = Math.atan2(tangent.x, tangent.z);
streak.rotation.z =
Math.sin(flowTime * 1.3 + streak.userData.phase) * 0.08;
streak.material.opacity =
0.22 +
0.22 *
(0.5 + 0.5 * Math.sin(flowTime * 1.8 + streak.userData.phase));
}
}
// -------------------------------------------------------------------------
// Instanced dune grass
// -------------------------------------------------------------------------
const grassGeometry = new THREE.ConeGeometry(0.18, 0.72, 4, 1, true);
grassGeometry.translate(0, 0.36, 0);
const grassMaterial = new THREE.MeshStandardMaterial({
color: 0x6e793e,
roughness: 1,
flatShading: true
});
const grassCount = 210;
const grass = new THREE.InstancedMesh(
grassGeometry,
grassMaterial,
grassCount
);
grass.castShadow = true;
grass.receiveShadow = true;
const dummy = new THREE.Object3D();
let grassPlaced = 0;
let attempts = 0;
while (grassPlaced < grassCount && attempts < 6000) {
attempts++;
const x = lerp(-43, 43, random());
const shore = shorelineZ(x);
const z = lerp(shore + 4.1, 37, random());
const y = terrainHeightAt(x, z);
const river = nearestRiver(x, z);
if (river.distance < river.sample.halfWidth + 2.6) continue;
if (y < 0.43) continue;
if (noise2(x * 0.21, z * 0.18) < 0.34) continue;
dummy.position.set(x, y - 0.025, z);
dummy.rotation.set(
(random() - 0.5) * 0.08,
random() * Math.PI * 2,
(random() - 0.5) * 0.08
);
const scale = 0.65 + random() * 0.95;
dummy.scale.set(
scale * (0.75 + random() * 0.35),
scale,
scale * (0.75 + random() * 0.35)
);
dummy.updateMatrix();
grass.setMatrixAt(grassPlaced, dummy.matrix);
grass.setColorAt(
grassPlaced,
new THREE.Color().setHSL(
0.18 + random() * 0.045,
0.28 + random() * 0.16,
0.31 + random() * 0.13
)
);
grassPlaced++;
}
grass.count = grassPlaced;
grass.instanceMatrix.needsUpdate = true;
if (grass.instanceColor) grass.instanceColor.needsUpdate = true;
scene.add(grass);
// -------------------------------------------------------------------------
// Low-poly shoreline stones
// -------------------------------------------------------------------------
const rockGeometry = new THREE.IcosahedronGeometry(0.34, 0);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x898577,
roughness: 0.95,
flatShading: true
});
const rockCount = 34;
const rocks = new THREE.InstancedMesh(
rockGeometry,
rockMaterial,
rockCount
);
rocks.castShadow = true;
rocks.receiveShadow = true;
for (let i = 0; i < rockCount; i++) {
let x;
let z;
let river;
do {
x = lerp(-43, 43, random());
const shore = shorelineZ(x);
z = shore + lerp(0.7, 5.6, random());
river = nearestRiver(x, z);
} while (river.distance < river.sample.halfWidth + 0.8);
const y = terrainHeightAt(x, z);
const scale = 0.45 + random() * 1.35;
dummy.position.set(x, y + 0.11 * scale, z);
dummy.rotation.set(random(), random() * Math.PI, random());
dummy.scale.set(
scale * (0.7 + random() * 0.6),
scale * (0.45 + random() * 0.4),
scale * (0.75 + random() * 0.55)
);
dummy.updateMatrix();
rocks.setMatrixAt(i, dummy.matrix);
rocks.setColorAt(
i,
new THREE.Color().setHSL(
0.1,
0.08 + random() * 0.07,
0.43 + random() * 0.15
)
);
}
rocks.instanceMatrix.needsUpdate = true;
if (rocks.instanceColor) rocks.instanceColor.needsUpdate = true;
scene.add(rocks);
// -------------------------------------------------------------------------
// A few weathered pieces of driftwood
// -------------------------------------------------------------------------
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0x77654f,
roughness: 1,
flatShading: true
});
function addDriftwood(x, z, length, angle) {
const group = new THREE.Group();
const wood = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.19, length, 7),
woodMaterial
);
wood.rotation.z = Math.PI / 2;
wood.position.y = 0.14;
wood.castShadow = true;
wood.receiveShadow = true;
const branch = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.08, length * 0.34, 5),
woodMaterial
);
branch.rotation.set(0, 0, Math.PI / 2.8);
branch.position.set(length * 0.18, 0.25, 0);
branch.castShadow = true;
group.add(wood, branch);
group.position.set(x, terrainHeightAt(x, z), z);
group.rotation.y = angle;
scene.add(group);
}
addDriftwood(-11.5, 4.6, 3.1, 0.72);
addDriftwood(24, 2.4, 2.5, -0.38);
addDriftwood(-29, 8.5, 2.7, 1.05);
// -------------------------------------------------------------------------
// Direct camera controls — no addons
// -------------------------------------------------------------------------
function resetCamera() {
desiredCamera.theta = 0.27;
desiredCamera.phi = 1.02;
desiredCamera.radius = 51;
}
let pointerActive = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerActive = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerActive) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desiredCamera.theta -= dx * 0.0055;
desiredCamera.phi = clamp(
desiredCamera.phi - dy * 0.0045,
0.56,
1.34
);
});
function endPointer(event) {
pointerActive = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.radius = clamp(
desiredCamera.radius * Math.exp(event.deltaY * 0.001),
32,
68
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
document.querySelector("#resetButton").addEventListener("click", resetCamera);
let flowRunning = true;
const flowButton = document.querySelector("#flowButton");
flowButton.addEventListener("click", () => {
flowRunning = !flowRunning;
flowButton.textContent = flowRunning ? "Pause current" : "Resume current";
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let sceneTime = 0;
let flowTime = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
sceneTime += delta;
if (flowRunning) flowTime += delta;
const damping = 1 - Math.exp(-delta * 7.5);
cameraState.theta = lerp(
cameraState.theta,
desiredCamera.theta,
damping
);
cameraState.phi = lerp(
cameraState.phi,
desiredCamera.phi,
damping
);
cameraState.radius = lerp(
cameraState.radius,
desiredCamera.radius,
damping
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
lakeUniforms.uTime.value = sceneTime;
riverUniforms.uTime.value = flowTime;
shallowMaterial.uniforms.uTime.value = sceneTime;
foamMaterial.uniforms.uTime.value = sceneTime;
updateCurrentStreaks(flowTime);
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
animate();
loading.style.opacity = "0";
setTimeout(() => loading.remove(), 400);
</script>
</body>
</html>
```