Prompt specification
Build or customize the Three.js 3D scene "Great Lakes bay and dune landscape slice" 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: Great Lakes bay and dune landscape slice
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/great-lakes-bay-and-dune-landscape-slice-x3079
Scene Description Prompt:
A voxel Great Lakes shoreline slice with bay, exposed shore, dunes, and tributary; wave exposure follows shore orientation. Three.js from cdnjs, one HTML file.
Working Three.js Starter (great-lakes-bay-and-dune-landscape-slice-x3079):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Great Lakes — Wave-Exposed Shoreline</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf6f2;
--muted: #abc4c4;
--glass: rgba(10, 31, 36, 0.78);
--line: rgba(215, 244, 238, 0.22);
--accent: #9de0cf;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: #b9dbe2;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
top: 20px;
left: 20px;
z-index: 5;
width: min(340px, calc(100vw - 40px));
padding: 17px 18px 16px;
color: var(--ink);
background: var(--glass);
border: 1px solid var(--line);
border-radius: 14px;
box-shadow: 0 18px 48px rgba(12, 39, 43, 0.2);
backdrop-filter: blur(12px);
pointer-events: auto;
user-select: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 3vw, 29px);
line-height: 1.02;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.control {
display: grid;
grid-template-columns: 46px 1fr 48px;
align-items: center;
gap: 10px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.control label {
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.bearing {
text-align: right;
color: var(--accent);
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
width: 100%;
accent-color: var(--accent);
cursor: ew-resize;
}
.bearing-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 11px;
color: var(--muted);
font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
}
.compass {
position: relative;
width: 29px;
height: 29px;
flex: 0 0 auto;
border: 1px solid rgba(220, 245, 239, 0.35);
border-radius: 50%;
}
.compass::before {
content: "N";
position: absolute;
top: -10px;
left: 50%;
transform: translateX(-50%);
color: var(--muted);
font-size: 7px;
}
.wave-arrow {
position: absolute;
inset: 4px;
display: grid;
place-items: center;
color: var(--accent);
font-size: 16px;
transition: transform 80ms linear;
transform-origin: 50% 50%;
}
.hint {
position: fixed;
right: 20px;
bottom: 18px;
z-index: 4;
padding: 9px 12px;
color: rgba(238, 251, 247, 0.9);
background: rgba(13, 39, 43, 0.62);
border: 1px solid rgba(225, 248, 243, 0.18);
border-radius: 10px;
backdrop-filter: blur(10px);
font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.07em;
pointer-events: none;
}
.annotation {
position: fixed;
z-index: 3;
padding: 5px 8px;
color: #f1fbf7;
background: rgba(15, 42, 45, 0.68);
border: 1px solid rgba(229, 249, 244, 0.24);
border-radius: 5px;
box-shadow: 0 4px 14px rgba(0, 25, 28, 0.16);
backdrop-filter: blur(5px);
transform: translate(-50%, -100%);
white-space: nowrap;
pointer-events: none;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.annotation::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 14px;
background: rgba(236, 251, 247, 0.55);
}
@media (max-width: 650px) {
.panel { top: 12px; left: 12px; width: 285px; }
.subtitle { margin-bottom: 10px; }
.hint { right: 12px; bottom: 12px; }
.annotation { font-size: 8px; }
}
</style>
</head>
<body>
<div class="panel">
<div class="eyebrow">Great Lakes / Shoreline Study 07</div>
<h1>Wave-Exposed<br>Coastline</h1>
<p class="subtitle">
A voxel shoreline slice with a sheltered bay, open rocky shore,
dune ridge, and inland tributary. Foam follows each local shore tangent.
</p>
<div class="control">
<label for="bearing">Wave</label>
<input id="bearing" type="range" min="0" max="359" value="35">
<output id="bearingValue" class="bearing">035°</output>
</div>
<div class="bearing-row">
<div class="compass">
<div id="waveArrow" class="wave-arrow">↑</div>
</div>
<span>PREVAILING WAVES FROM</span>
</div>
</div>
<div class="hint">DRAG TO ORBIT · SCROLL TO ZOOM · DOUBLE-CLICK TO RESET</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ------------------------------------------------------------------ */
/* Renderer, scene, and camera */
/* ------------------------------------------------------------------ */
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9dbe2);
scene.fog = new THREE.Fog(0xb9dbe2, 92, 175);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
260
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xe8fbf8, 0x5f6650, 2.1);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0cc, 3.1);
sun.position.set(-32, 52, -26);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -55;
sun.shadow.camera.right = 55;
sun.shadow.camera.top = 45;
sun.shadow.camera.bottom = -45;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 140;
sun.shadow.bias = -0.0005;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x8ed9e1, 0.75);
rim.position.set(35, 18, 30);
scene.add(rim);
/* ------------------------------------------------------------------ */
/* Direct camera controls; no addons */
/* ------------------------------------------------------------------ */
const cameraState = {
target: new THREE.Vector3(0, 0, 1.5),
radius: 83,
theta: 2.38,
phi: 0.94
};
function resetCamera() {
cameraState.radius = 83;
cameraState.theta = 2.38;
cameraState.phi = 0.94;
}
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.0055;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0048,
0.34,
1.43
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
42,
135
);
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") cameraState.theta += 0.12;
if (event.key === "ArrowRight") cameraState.theta -= 0.12;
if (event.key === "ArrowUp") cameraState.phi = Math.max(0.34, cameraState.phi - 0.08);
if (event.key === "ArrowDown") cameraState.phi = Math.min(1.43, cameraState.phi + 0.08);
if (event.key === "+" || event.key === "=") cameraState.radius = Math.max(42, cameraState.radius - 5);
if (event.key === "-") cameraState.radius = Math.min(135, cameraState.radius + 5);
});
/* ------------------------------------------------------------------ */
/* Procedural shoreline and terrain */
/* ------------------------------------------------------------------ */
const NX = 72;
const NZ = 54;
const BASE_Y = -2.88;
const VOXEL_H = 0.72;
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smoothstep(min, max, value) {
const x = clamp((value - min) / (max - min), 0, 1);
return x * x * (3 - 2 * x);
}
function hash2(x, z) {
const value = Math.sin(x * 127.1 + z * 311.7) * 43758.5453;
return value - Math.floor(value);
}
/* Main coast: broad open shore + concave bay + protruding rocky point. */
function coastZ(x) {
const broad = 0.85 * Math.sin(x * 0.13)
+ 1.05 * Math.sin(x * 0.28 + 0.7);
const bay = 11.5 * Math.exp(-Math.pow((x + 15) / 8.8, 2));
const point = 8.4 * Math.exp(-Math.pow((x - 22) / 6.8, 2));
return broad + bay - point;
}
function coastDerivative(x) {
const epsilon = 0.08;
return (coastZ(x + epsilon) - coastZ(x - epsilon)) / (2 * epsilon);
}
/* Tributary centerline and surface elevation. */
function riverX(z) {
return 8.0 + 2.4 * Math.sin((z - 1) * 0.21) + 0.012 * (z - 1);
}
function riverDX(z) {
const epsilon = 0.05;
return (riverX(z + epsilon) - riverX(z - epsilon)) / (2 * epsilon);
}
function riverLevel(z) {
return 0.08 + Math.max(0, z + 1) * 0.045;
}
function riverHalfWidth(z) {
return 0.82 + Math.max(0, z + 1) * 0.018;
}
function riverDistance(x, z) {
return Math.abs(x - riverX(z)) / Math.sqrt(1 + riverDX(z) * riverDX(z));
}
function duneMask(x, z) {
const d = z - coastZ(x);
const ridgeA = Math.exp(-Math.pow((x + 3) / 9.5, 2));
const ridgeB = 0.72 * Math.exp(-Math.pow((x - 11) / 6.0, 2));
return (ridgeA + ridgeB) * Math.exp(-Math.pow((d - 5.0) / 3.1, 2));
}
function bluffMask(x, z) {
const d = z - coastZ(x);
return Math.exp(-Math.pow((x - 22) / 4.7, 2))
* Math.exp(-Math.pow((d - 2.4) / 3.0, 2));
}
function terrainHeight(x, z) {
const coast = coastZ(x);
const d = z - coast;
let height = d < 0
? -0.62 + d * 0.073
: 0.11 * d + 0.0045 * d * d;
height += duneMask(x, z) * 1.85;
height += bluffMask(x, z) * 1.45;
if (z > -1) {
const distance = riverDistance(x, z);
const halfWidth = riverHalfWidth(z);
const level = riverLevel(z);
if (distance < halfWidth) {
const incision = level - 0.38 - 0.15 * (1 - distance / halfWidth);
height = Math.min(height, incision);
} else if (distance < halfWidth + 1.8) {
const bank = level - 0.18
+ smoothstep(halfWidth, halfWidth + 1.8, distance) * 0.68;
height = Math.min(height, bank);
}
}
return height;
}
function surfaceColor(x, z, height) {
const coast = coastZ(x);
const d = z - coast;
const noise = hash2(Math.floor(x), Math.floor(z));
const color = new THREE.Color();
if (height < 0.05) {
color.set(0x315f62);
} else if (height < 0.72) {
color.set(0xb7a37b);
} else if (duneMask(x, z) > 0.28 && d > 1.0 && d < 9.0) {
color.set(0xaeb979).lerp(new THREE.Color(0xd0c58c), noise * 0.36);
} else if (bluffMask(x, z) > 0.25 && height > 0.2) {
color.set(0x88877b).lerp(new THREE.Color(0xb0a58b), noise * 0.5);
} else if (z > 0 && riverDistance(x, z) < 2.5) {
color.set(0x80785b);
} else {
color.set(0x456d4b).lerp(new THREE.Color(0x77905d), noise * 0.65);
}
return color;
}
function strataColor(y, top, x, z) {
if (top) return surfaceColor(x, z, y);
if (y < -0.2) return new THREE.Color(0x4e6157);
if (y < 0.7) return new THREE.Color(0x988665);
return new THREE.Color(0x66704f).lerp(
new THREE.Color(0x526449),
hash2(Math.floor(x), Math.floor(z))
);
}
/* Build all terrain voxels as a single instanced mesh. */
const voxelMatrices = [];
const voxelColors = [];
const dummy = new THREE.Object3D();
for (let iz = 0; iz < NZ; iz++) {
const z = -NZ * 0.5 + iz + 0.5;
for (let ix = 0; ix < NX; ix++) {
const x = -NX * 0.5 + ix + 0.5;
const height = terrainHeight(x, z);
const layers = Math.max(0, Math.ceil((height - BASE_Y) / VOXEL_H));
for (let layer = 0; layer < layers; layer++) {
const y = BASE_Y + (layer + 0.5) * VOXEL_H;
dummy.position.set(x, y, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
voxelMatrices.push(dummy.matrix.clone());
voxelColors.push(strataColor(y, layer === layers - 1, x, z));
}
}
}
const terrainGeometry = new THREE.BoxGeometry(0.90, VOXEL_H - 0.025, 0.90);
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
metalness: 0,
flatShading: true
});
const terrain = new THREE.InstancedMesh(
terrainGeometry,
terrainMaterial,
voxelMatrices.length
);
voxelMatrices.forEach((matrix, index) => {
terrain.setMatrixAt(index, matrix);
terrain.setColorAt(index, voxelColors[index]);
});
terrain.instanceMatrix.needsUpdate = true;
terrain.instanceColor.needsUpdate = true;
terrain.castShadow = true;
terrain.receiveShadow = true;
terrain.frustumCulled = false;
scene.add(terrain);
/* Dark cutaway pedestal under the terrain slice. */
const pedestal = new THREE.Mesh(
new THREE.BoxGeometry(NX + 0.8, 0.85, NZ + 0.8),
new THREE.MeshStandardMaterial({
color: 0x24464a,
roughness: 0.82,
metalness: 0.05
})
);
pedestal.position.set(0, BASE_Y - 0.43, 0);
pedestal.receiveShadow = true;
scene.add(pedestal);
/* ------------------------------------------------------------------ */
/* Animated lake water */
/* ------------------------------------------------------------------ */
const waterUniforms = {
uTime: { value: 0 },
uDirection: { value: new THREE.Vector2(-0.57, 0.82) }
};
const waterMaterial = new THREE.ShaderMaterial({
transparent: true,
depthWrite: false,
side: THREE.DoubleSide,
toneMapped: false,
uniforms: waterUniforms,
vertexShader: `
uniform float uTime;
uniform vec2 uDirection;
varying vec3 vWorld;
void main() {
vec3 transformed = position;
vec2 worldish = vec2(position.x, -position.y);
float phase = dot(worldish, normalize(uDirection)) * 1.12 - uTime * 1.65;
float ripple = sin(phase) * 0.055 + sin(phase * 0.47 + 1.2) * 0.025;
transformed.z += ripple;
vec4 worldPosition = modelMatrix * vec4(transformed, 1.0);
vWorld = worldPosition.xyz;
gl_Position = projectionMatrix * viewMatrix * worldPosition;
}
`,
fragmentShader: `
uniform float uTime;
uniform vec2 uDirection;
varying vec3 vWorld;
void main() {
vec2 p = vWorld.xz;
float phase = dot(p, normalize(uDirection)) * 0.88 - uTime * 1.52;
float longWave = sin(phase);
float crossWave = sin(p.x * 0.34 + p.y * 0.51 + uTime * 0.83);
float crest = smoothstep(0.83, 0.99, longWave * 0.5 + 0.5);
float shimmer = smoothstep(
0.94,
1.0,
sin(p.x * 1.75 - p.y * 0.93 + uTime * 2.1) * 0.5 + 0.5
);
vec3 deep = vec3(0.075, 0.36, 0.43);
vec3 shallow = vec3(0.17, 0.54, 0.58);
vec3 color = mix(deep, shallow, 0.38 + crossWave * 0.12);
color += crest * vec3(0.11, 0.18, 0.17);
color += shimmer * vec3(0.14, 0.19, 0.18);
gl_FragColor = vec4(color, 0.88);
}
`
});
const water = new THREE.Mesh(
new THREE.PlaneGeometry(180, 150, 100, 80),
waterMaterial
);
water.rotation.x = -Math.PI / 2;
water.position.y = -0.035;
water.renderOrder = 1;
scene.add(water);
/* ------------------------------------------------------------------ */
/* Tributary ribbon */
/* ------------------------------------------------------------------ */
const riverPositions = [];
const riverIndices = [];
const riverSegments = 64;
const riverStartZ = -1.2;
const riverEndZ = 26.3;
for (let i = 0; i <= riverSegments; i++) {
const z = THREE.MathUtils.lerp(riverStartZ, riverEndZ, i / riverSegments);
const x = riverX(z);
const slope = riverDX(z);
const perpendicular = new THREE.Vector3(1, 0, -slope).normalize();
const halfWidth = riverHalfWidth(z);
const y = riverLevel(z) + 0.055;
const left = new THREE.Vector3(x, y, z).addScaledVector(perpendicular, halfWidth);
const right = new THREE.Vector3(x, y, z).addScaledVector(perpendicular, -halfWidth);
riverPositions.push(left.x, left.y, left.z, right.x, right.y, right.z);
if (i < riverSegments) {
const a = i * 2;
riverIndices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
}
const riverGeometry = new THREE.BufferGeometry();
riverGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(riverPositions, 3)
);
riverGeometry.setIndex(riverIndices);
riverGeometry.computeVertexNormals();
const river = new THREE.Mesh(
riverGeometry,
new THREE.MeshPhysicalMaterial({
color: 0x3d91a0,
roughness: 0.24,
metalness: 0,
transparent: true,
opacity: 0.91,
clearcoat: 0.75,
clearcoatRoughness: 0.2,
depthWrite: false,
side: THREE.DoubleSide
})
);
river.renderOrder = 2;
scene.add(river);
/* ------------------------------------------------------------------ */
/* Shore-oriented foam and exposure */
/* ------------------------------------------------------------------ */
const foamRecords = [];
const foamMatrices = [];
const foamPhases = [];
const rowOffsets = [0.42, 1.35, 2.65, 6.2, 9.1];
const rowFactors = [1.0, 0.82, 0.58, 0.30, 0.20];
for (let x = -35.5; x <= 35.5; x += 1.12) {
const z = coastZ(x);
const derivative = coastDerivative(x);
const tangent = new THREE.Vector3(1, 0, derivative).normalize();
const outward = new THREE.Vector3(derivative, 0, -1).normalize();
const bayShelter = Math.exp(-Math.pow((x + 15) / 9.8, 2));
const openness = 1 - 0.82 * bayShelter;
rowOffsets.forEach((offset, rowIndex) => {
const position = new THREE.Vector3(x, 0.075, z)
.addScaledVector(outward, offset)
.addScaledVector(tangent, rowIndex % 2 === 0 ? 0.25 : -0.28);
const length = rowIndex < 3
? 0.95 + openness * 2.0
: 2.2 + openness * 4.0;
dummy.position.copy(position);
dummy.quaternion.setFromAxisAngle(
new THREE.Vector3(0, 1, 0),
-Math.atan2(tangent.z, tangent.x)
);
dummy.scale.set(length, 0.035, rowIndex < 3 ? 0.14 : 0.095);
dummy.updateMatrix();
foamMatrices.push(dummy.matrix.clone());
foamPhases.push(Math.random() * Math.PI * 2 + rowIndex * 0.9);
foamRecords.push({
outwardX: outward.x,
outwardZ: outward.z,
openness,
rowFactor: rowFactors[rowIndex]
});
});
}
const foamGeometry = new THREE.BoxGeometry(1, 1, 1);
const foamStrengths = new Float32Array(foamRecords.length);
foamGeometry.setAttribute(
"aPhase",
new THREE.InstancedBufferAttribute(new Float32Array(foamPhases), 1)
);
foamGeometry.setAttribute(
"aStrength",
new THREE.InstancedBufferAttribute(foamStrengths, 1)
);
const foamMaterial = new THREE.ShaderMaterial({
transparent: true,
depthWrite: false,
side: THREE.DoubleSide,
toneMapped: false,
uniforms: {
uTime: { value: 0 },
uColor: { value: new THREE.Color(0xdff7ee) }
},
vertexShader: `
attribute float aPhase;
attribute float aStrength;
varying float vPhase;
varying float vStrength;
void main() {
vec4 transformedPosition = vec4(position, 1.0);
#ifdef USE_INSTANCING
transformedPosition = instanceMatrix * transformedPosition;
#endif
vec4 worldPosition = modelMatrix * transformedPosition;
vPhase = aPhase;
vStrength = aStrength;
gl_Position = projectionMatrix * viewMatrix * worldPosition;
}
`,
fragmentShader: `
uniform float uTime;
uniform vec3 uColor;
varying float vPhase;
varying float vStrength;
void main() {
float pulse = 0.5 + 0.5 * sin(vPhase + uTime * 1.85);
float flicker = smoothstep(0.12, 0.9, pulse);
float alpha = vStrength * (0.24 + flicker * 0.72);
if (alpha < 0.015) discard;
gl_FragColor = vec4(uColor, alpha);
}
`
});
const foam = new THREE.InstancedMesh(
foamGeometry,
foamMaterial,
foamMatrices.length
);
foamMatrices.forEach((matrix, index) => foam.setMatrixAt(index, matrix));
foam.instanceMatrix.needsUpdate = true;
foam.frustumCulled = false;
foam.renderOrder = 4;
scene.add(foam);
function updateExposure(degrees) {
const radians = THREE.MathUtils.degToRad(degrees);
// Bearing denotes where waves originate, measured clockwise from north.
const sourceX = Math.sin(radians);
const sourceZ = -Math.cos(radians);
foamRecords.forEach((record, index) => {
const alignment = Math.max(
0,
record.outwardX * sourceX + record.outwardZ * sourceZ
);
const exposure = record.openness * (0.16 + 0.84 * alignment);
foamStrengths[index] = exposure * record.rowFactor;
});
foamGeometry.getAttribute("aStrength").needsUpdate = true;
waterUniforms.uDirection.value.set(-sourceX, -sourceZ).normalize();
}
const bearingInput = document.getElementById("bearing");
const bearingValue = document.getElementById("bearingValue");
const waveArrow = document.getElementById("waveArrow");
function setBearing(value) {
const bearing = Number(value);
bearingValue.value = `${String(bearing).padStart(3, "0")}°`;
bearingValue.textContent = `${String(bearing).padStart(3, "0")}°`;
waveArrow.style.transform = `rotate(${bearing}deg)`;
updateExposure(bearing);
}
bearingInput.addEventListener("input", (event) => setBearing(event.target.value));
setBearing(bearingInput.value);
/* ------------------------------------------------------------------ */
/* Inland voxel vegetation and exposed shore rocks */
/* ------------------------------------------------------------------ */
let randomSeed = 92741;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const treePositions = [];
let attempts = 0;
while (treePositions.length < 145 && attempts < 5000) {
attempts++;
const x = -34 + random() * 68;
const z = 5 + random() * 20;
const height = terrainHeight(x, z);
const d = z - coastZ(x);
if (
height > 1.6 &&
d > 7 &&
riverDistance(x, z) > 3.0 &&
duneMask(x, z) < 0.32
) {
treePositions.push({ x, z, height, size: 0.75 + random() * 0.55 });
}
}
const trunkGeometry = new THREE.BoxGeometry(0.24, 1.25, 0.24);
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const trunks = new THREE.InstancedMesh(
trunkGeometry,
trunkMaterial,
treePositions.length
);
const crownGeometry = new THREE.BoxGeometry(1, 1, 1);
const crownMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95,
flatShading: true
});
const crowns = new THREE.InstancedMesh(
crownGeometry,
crownMaterial,
treePositions.length * 3
);
treePositions.forEach((tree, index) => {
dummy.position.set(tree.x, tree.height + 0.62 * tree.size, tree.z);
dummy.rotation.set(0, random() * Math.PI, 0);
dummy.scale.set(tree.size, tree.size, tree.size);
dummy.updateMatrix();
trunks.setMatrixAt(index, dummy.matrix);
trunks.setColorAt(index, new THREE.Color(0x66503a));
const crownBase = new THREE.Color().setHSL(
0.27 + random() * 0.035,
0.30 + random() * 0.12,
0.31 + random() * 0.09
);
const crownParts = [
{ x: 0, y: 1.35, z: 0, sx: 1.05, sy: 0.83, sz: 1.02 },
{ x: 0.48, y: 1.22, z: 0.08, sx: 0.72, sy: 0.68, sz: 0.74 },
{ x: -0.42, y: 1.38, z: -0.12, sx: 0.77, sy: 0.73, sz: 0.72 }
];
crownParts.forEach((part, partIndex) => {
dummy.position.set(
tree.x + part.x * tree.size,
tree.height + part.y * tree.size,
tree.z + part.z * tree.size
);
dummy.rotation.set(0, random() * 0.5, 0);
dummy.scale.set(
part.sx * tree.size,
part.sy * tree.size,
part.sz * tree.size
);
dummy.updateMatrix();
const crownIndex = index * 3 + partIndex;
crowns.setMatrixAt(crownIndex, dummy.matrix);
crowns.setColorAt(crownIndex, crownBase.clone().offsetHSL(0, 0, random() * 0.045));
});
});
trunks.instanceMatrix.needsUpdate = true;
trunks.instanceColor.needsUpdate = true;
crowns.instanceMatrix.needsUpdate = true;
crowns.instanceColor.needsUpdate = true;
trunks.castShadow = true;
crowns.castShadow = true;
trunks.frustumCulled = false;
crowns.frustumCulled = false;
scene.add(trunks, crowns);
/* Marram-grass voxel blades along the dune ridge. */
const grassPositions = [];
attempts = 0;
while (grassPositions.length < 235 && attempts < 5000) {
attempts++;
const x = -34 + random() * 68;
const z = -2 + random() * 17;
const height = terrainHeight(x, z);
const d = z - coastZ(x);
if (
d > 1.2 &&
d < 9.0 &&
height > 0.55 &&
height < 3.2 &&
duneMask(x, z) > 0.28 &&
riverDistance(x, z) > 2.2
) {
grassPositions.push({ x, z, height });
}
}
const grassGeometry = new THREE.BoxGeometry(0.065, 0.42, 0.065);
const grassMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const grass = new THREE.InstancedMesh(
grassGeometry,
grassMaterial,
grassPositions.length
);
grassPositions.forEach((blade, index) => {
dummy.position.set(blade.x, blade.height + 0.22, blade.z);
dummy.rotation.set(0, random() * Math.PI, (random() - 0.5) * 0.16);
dummy.scale.set(
0.75 + random() * 0.6,
0.7 + random() * 1.0,
0.75 + random() * 0.6
);
dummy.updateMatrix();
grass.setMatrixAt(index, dummy.matrix);
grass.setColorAt(
index,
new THREE.Color().setHSL(
0.16 + random() * 0.035,
0.30 + random() * 0.14,
0.43 + random() * 0.13
)
);
});
grass.instanceMatrix.needsUpdate = true;
grass.instanceColor.needsUpdate = true;
grass.castShadow = true;
grass.frustumCulled = false;
scene.add(grass);
/* Small blocky boulders on the exposed point. */
const rockCount = 72;
const rockGeometry = new THREE.BoxGeometry(1, 1, 1);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const rocks = new THREE.InstancedMesh(
rockGeometry,
rockMaterial,
rockCount
);
for (let index = 0; index < rockCount; index++) {
const x = 16 + random() * 18;
const z = coastZ(x) - 0.6 + random() * 7.0;
const height = terrainHeight(x, z);
const size = 0.24 + random() * 0.55;
dummy.position.set(x, height + size * 0.40, z);
dummy.rotation.set(random() * 0.45, random() * Math.PI, random() * 0.45);
dummy.scale.set(
size * (0.8 + random() * 0.6),
size * (0.55 + random() * 0.65),
size * (0.8 + random() * 0.6)
);
dummy.updateMatrix();
rocks.setMatrixAt(index, dummy.matrix);
rocks.setColorAt(
index,
new THREE.Color().setHSL(
0.12,
0.07 + random() * 0.07,
0.39 + random() * 0.16
)
);
}
rocks.instanceMatrix.needsUpdate = true;
rocks.instanceColor.needsUpdate = true;
rocks.castShadow = true;
rocks.receiveShadow = true;
rocks.frustumCulled = false;
scene.add(rocks);
/* ------------------------------------------------------------------ */
/* Screen-space annotations */
/* ------------------------------------------------------------------ */
const annotations = [];
function addAnnotation(text, position) {
const element = document.createElement("div");
element.className = "annotation";
element.textContent = text;
document.body.appendChild(element);
annotations.push({
element,
position: new THREE.Vector3(...position)
});
}
addAnnotation("Sheltered bay", [-15, 1.4, 9.5]);
addAnnotation("Dune ridge", [-2.5, 2.2, 5.2]);
addAnnotation("Exposed rocky shore", [22, 2.0, -1.4]);
addAnnotation("Tributary", [riverX(17), 1.8, 17]);
function updateAnnotations() {
const width = innerWidth;
const height = innerHeight;
annotations.forEach((annotation) => {
const projected = annotation.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1
&& Math.abs(projected.x) < 1.08
&& Math.abs(projected.y) < 1.08;
annotation.element.style.display = visible ? "block" : "none";
if (visible) {
annotation.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
annotation.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
});
}
/* ------------------------------------------------------------------ */
/* Resize and animation */
/* ------------------------------------------------------------------ */
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
});
const startTime = performance.now();
function animate(now) {
const time = (now - startTime) * 0.001;
waterUniforms.uTime.value = time;
foamMaterial.uniforms.uTime.value = time;
updateCamera();
updateAnnotations();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateCamera();
requestAnimationFrame(animate);
</script>
</body>
</html>
```