Prompt specification
Build or customize the Three.js 3D scene "Beach profile with two waterlines" 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: Beach profile with two waterlines
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/beach-profile-with-two-waterlines-x1285
Scene Description Prompt:
Beach profile with two waterlines. Model a sloping beach cross-section with low and high waterline markers. Show that a small height change can move the shoreline farther across a gentle slope. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (beach-profile-with-two-waterlines-x1285):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Beach Profile — Two Waterlines</title>
<style>
:root {
color-scheme: light;
--ink: #17323a;
--muted: #64757a;
--paper: rgba(255, 252, 246, 0.93);
--line: rgba(23, 50, 58, 0.13);
--ocean: #238ead;
--low: #65d8ee;
--high: #ef845e;
--accent: #c96f2d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f4efe7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.scene-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
padding: 6px 10px;
border-radius: 999px;
border: 1px solid rgba(255, 255, 255, 0.75);
background: rgba(255, 252, 246, 0.92);
box-shadow: 0 5px 18px rgba(27, 51, 57, 0.13);
font-size: 12px;
font-weight: 750;
letter-spacing: 0.025em;
transition: opacity 120ms ease;
}
.scene-label.low {
color: #087d9c;
border-color: rgba(101, 216, 238, 0.9);
}
.scene-label.high {
color: #a94327;
border-color: rgba(239, 132, 94, 0.9);
}
.scene-label.current {
color: white;
background: rgba(23, 50, 58, 0.9);
border-color: rgba(255, 255, 255, 0.35);
}
.scene-label.dimension {
color: #9b4d1e;
background: rgba(255, 246, 235, 0.95);
border-color: rgba(201, 111, 45, 0.38);
}
.scene-label.ghost {
padding: 4px 8px;
color: rgba(23, 50, 58, 0.68);
background: rgba(255, 255, 255, 0.45);
box-shadow: none;
border-color: rgba(23, 50, 58, 0.08);
letter-spacing: 0.13em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid rgba(255, 255, 255, 0.82);
background: var(--paper);
box-shadow: 0 16px 50px rgba(38, 57, 60, 0.14);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.info {
top: 18px;
left: 18px;
width: min(365px, calc(100vw - 36px));
padding: 20px;
border-radius: 20px;
}
.eyebrow {
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h1 {
margin: 8px 0 10px;
max-width: 310px;
font-size: 29px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.description {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 17px;
}
.metric {
min-width: 0;
padding: 10px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.48);
}
.metric span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 4px;
font-size: 17px;
white-space: nowrap;
}
.formula {
margin-top: 13px;
padding: 11px 12px;
border-left: 3px solid var(--accent);
border-radius: 0 10px 10px 0;
background: rgba(201, 111, 45, 0.08);
color: #774522;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
line-height: 1.5;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 14px;
color: var(--muted);
font-size: 11px;
font-weight: 650;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.9);
}
.dot.low {
background: var(--low);
}
.dot.high {
background: var(--high);
}
.dot.current {
background: white;
border: 1px solid var(--ink);
}
.controls {
left: 50%;
bottom: 18px;
transform: translateX(-50%);
width: min(720px, calc(100vw - 36px));
padding: 14px 16px;
border-radius: 18px;
}
.control-row {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 14px;
}
.control-label {
min-width: 118px;
}
.control-label strong {
display: block;
font-size: 12px;
}
.control-label output {
display: block;
margin-top: 2px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
}
input[type="range"] {
width: 100%;
accent-color: var(--ocean);
cursor: pointer;
}
.buttons {
display: flex;
gap: 7px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 10px;
padding: 9px 12px;
background: rgba(255, 255, 255, 0.78);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(23, 50, 58, 0.3);
background: white;
}
button:focus-visible,
input:focus-visible {
outline: 3px solid rgba(35, 142, 173, 0.28);
outline-offset: 2px;
}
#play {
min-width: 88px;
color: white;
border-color: transparent;
background: var(--ink);
}
#reset-view {
font-size: 11px;
color: var(--muted);
}
.help {
position: fixed;
right: 18px;
bottom: 18px;
z-index: 4;
padding: 8px 11px;
border-radius: 999px;
color: rgba(23, 50, 58, 0.62);
background: rgba(255, 255, 255, 0.58);
font-size: 11px;
pointer-events: none;
}
@media (max-width: 760px) {
.info {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px;
}
h1 {
max-width: none;
margin: 6px 0 8px;
font-size: 22px;
}
.description {
font-size: 12px;
}
.metrics {
margin-top: 12px;
}
.metric {
padding: 8px;
}
.metric strong {
font-size: 15px;
}
.formula {
margin-top: 10px;
padding: 8px 10px;
}
.legend {
margin-top: 10px;
}
.controls {
left: 12px;
right: 12px;
bottom: 12px;
transform: none;
width: auto;
padding: 12px;
}
.control-row {
grid-template-columns: 1fr;
gap: 9px;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.control-label output {
margin: 0;
}
.buttons {
justify-content: space-between;
}
.help {
display: none;
}
.scene-label {
padding: 5px 7px;
font-size: 10px;
}
}
@media (max-height: 690px) {
.info .description,
.legend,
.formula {
display: none;
}
.metrics {
margin-top: 10px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D beach cross-section with low and high waterlines"></canvas>
<div id="labels" aria-hidden="true">
<div id="low-label" class="scene-label low">LOW · −0.55 m</div>
<div id="high-label" class="scene-label high">HIGH · +0.10 m</div>
<div id="current-label" class="scene-label current">CURRENT SHORELINE</div>
<div id="height-label" class="scene-label dimension">Δh = 0.65 m</div>
<div id="run-label" class="scene-label dimension">Δx = 2.32 m</div>
<div id="sea-label" class="scene-label ghost">Sea</div>
<div id="beach-label" class="scene-label ghost">Beach</div>
</div>
<section class="panel info">
<div class="eyebrow">Coastal geometry / interactive model</div>
<h1>A small rise, a longer retreat.</h1>
<p id="selected-narrative" class="description">
The low and high waterlines show how a gentle beach amplifies a small vertical
sea-level change into a much larger horizontal shoreline shift.
</p>
<div class="metrics">
<div class="metric">
<span>Full tide rise</span>
<strong>0.65 m</strong>
</div>
<div class="metric">
<span>Horizontal run</span>
<strong>2.32 m</strong>
</div>
<div class="metric">
<span>Beach slope</span>
<strong>15.6°</strong>
</div>
</div>
<div class="formula">
Δx = Δh / tan θ<br />
0.65 / tan(15.6°) = 2.32 m
</div>
<div class="legend">
<span><i class="dot low"></i>Low waterline</span>
<span><i class="dot current"></i>Current shoreline</span>
<span><i class="dot high"></i>High waterline</span>
</div>
</section>
<section class="panel controls" aria-label="Water level controls">
<div class="control-row">
<div class="control-label">
<strong>Relative sea level</strong>
<output id="level-readout" for="level">−0.55 m</output>
</div>
<input
id="level"
type="range"
min="-0.55"
max="0.10"
step="0.01"
value="-0.55"
aria-label="Relative sea level"
/>
<div class="buttons">
<button id="low" type="button">Low</button>
<button id="play" type="button">Run tide</button>
<button id="high" type="button">High</button>
<button id="reset-view" type="button">Reset view</button>
</div>
</div>
</section>
<div class="help">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";
// ------------------------------------------------------------
// Model dimensions, in metres
// ------------------------------------------------------------
const X0 = -5.0;
const Y0 = -1.2;
const X1 = 8.0;
const Y1 = 1.8;
const SLOPE = (Y1 - Y0) / (X1 - X0); // 0.28
const BEACH_ANGLE = Math.atan(SLOPE);
const DEPTH = 8.4;
const BASE_Y = -2.55;
const OCEAN_LEFT = -9.2;
const OCEAN_BOTTOM = -2.48;
const LOW_LEVEL = -0.55;
const HIGH_LEVEL = 0.10;
const FULL_RISE = HIGH_LEVEL - LOW_LEVEL;
const FULL_RUN = FULL_RISE / SLOPE;
const xAtLevel = (level) => X0 + (level - Y0) / SLOPE;
const yAtX = (x) => Y0 + SLOPE * (x - X0);
const LOW_X = xAtLevel(LOW_LEVEL);
const HIGH_X = xAtLevel(HIGH_LEVEL);
const FRONT_Z = DEPTH / 2;
// ------------------------------------------------------------
// DOM references
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const levelInput = document.querySelector("#level");
const levelReadout = document.querySelector("#level-readout");
const narrative = document.querySelector("#selected-narrative");
const playButton = document.querySelector("#play");
const lowButton = document.querySelector("#low");
const highButton = document.querySelector("#high");
const resetButton = document.querySelector("#reset-view");
const labelElements = {
low: document.querySelector("#low-label"),
high: document.querySelector("#high-label"),
current: document.querySelector("#current-label"),
height: document.querySelector("#height-label"),
run: document.querySelector("#run-label"),
sea: document.querySelector("#sea-label"),
beach: document.querySelector("#beach-label")
};
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4efe7);
scene.fog = new THREE.Fog(0xf4efe7, 25, 55);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xfff9ec, 0x78909a, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3d6, 3.0);
sun.position.set(-7, 14, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9e9ff, 0.75);
fill.position.set(10, 5, -8);
scene.add(fill);
// Ground
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xf8f4ec,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = BASE_Y - 0.08;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------
// Beach solid: extruded cross-section
// ------------------------------------------------------------
const beachShape = new THREE.Shape();
beachShape.moveTo(X0, BASE_Y);
beachShape.lineTo(X1, BASE_Y);
beachShape.lineTo(X1, Y1);
beachShape.lineTo(X0, Y0);
beachShape.closePath();
const beachGeometry = new THREE.ExtrudeGeometry(beachShape, {
depth: DEPTH,
steps: 1,
bevelEnabled: false
});
beachGeometry.translate(0, 0, -DEPTH / 2);
const beach = new THREE.Mesh(
beachGeometry,
new THREE.MeshStandardMaterial({
color: 0xd9b477,
roughness: 0.93,
metalness: 0
})
);
beach.castShadow = true;
beach.receiveShadow = true;
scene.add(beach);
// Clean outline around the cutaway solid.
const beachEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(beachGeometry, 22),
new THREE.LineBasicMaterial({
color: 0x987446,
transparent: true,
opacity: 0.58
})
);
beachEdges.renderOrder = 1;
scene.add(beachEdges);
// A distinct top surface following the sloping profile.
function makeTopStrip(xA, xB, offset = 0.018) {
const yA = yAtX(xA) + offset;
const yB = yAtX(xB) + offset;
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
xA, yA, -DEPTH / 2,
xB, yB, -DEPTH / 2,
xB, yB, DEPTH / 2,
xA, yA, DEPTH / 2
],
3
)
);
geometry.setIndex([0, 3, 1, 1, 3, 2]);
geometry.computeVertexNormals();
return geometry;
}
const beachTop = new THREE.Mesh(
makeTopStrip(X0, X1, 0.018),
new THREE.MeshStandardMaterial({
color: 0xefd398,
roughness: 0.96,
metalness: 0,
side: THREE.DoubleSide,
polygonOffset: true,
polygonOffsetFactor: -1,
polygonOffsetUnits: -1
})
);
beachTop.receiveShadow = true;
scene.add(beachTop);
// Highlight the horizontal beach zone crossed between the two waterlines.
const tideZone = new THREE.Mesh(
makeTopStrip(LOW_X, HIGH_X, 0.033),
new THREE.MeshStandardMaterial({
color: 0xf39a50,
roughness: 0.9,
transparent: true,
opacity: 0.27,
depthWrite: false,
side: THREE.DoubleSide
})
);
tideZone.renderOrder = 2;
scene.add(tideZone);
// A stylized sediment band on the front cut face.
const sedimentBand = new THREE.Mesh(
new THREE.BoxGeometry(X1 - X0, 0.42, 0.07),
new THREE.MeshStandardMaterial({
color: 0xa9794d,
roughness: 1
})
);
sedimentBand.position.set((X0 + X1) / 2, -2.17, FRONT_Z + 0.035);
scene.add(sedimentBand);
const sedimentLine = new THREE.Mesh(
new THREE.BoxGeometry(X1 - X0, 0.055, 0.08),
new THREE.MeshStandardMaterial({
color: 0xf0d7a2,
roughness: 1
})
);
sedimentLine.position.set((X0 + X1) / 2, -1.93, FRONT_Z + 0.045);
scene.add(sedimentLine);
// ------------------------------------------------------------
// Ocean solid and stylized wave marks
// ------------------------------------------------------------
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x269fc4,
roughness: 0.28,
metalness: 0.04,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const waterBlock = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
waterMaterial
);
waterBlock.renderOrder = 1;
scene.add(waterBlock);
const waterTop = new THREE.Mesh(
new THREE.PlaneGeometry(1, 1),
new THREE.MeshStandardMaterial({
color: 0x55b9dc,
roughness: 0.25,
metalness: 0.02,
transparent: true,
opacity: 0.62,
depthWrite: false,
side: THREE.DoubleSide
})
);
waterTop.rotation.x = -Math.PI / 2;
waterTop.renderOrder = 3;
scene.add(waterTop);
const waveMaterial = new THREE.MeshBasicMaterial({
color: 0xd9f7ff,
transparent: true,
opacity: 0.65
});
const waves = [];
const waveLayout = [
[-8.0, -3.0, 1.1],
[-6.7, -1.6, 0.9],
[-5.4, 0.0, 1.25],
[-7.2, 1.6, 1.0],
[-4.7, 3.0, 0.85]
];
for (const [x, z, length] of waveLayout) {
const wave = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.025, 0.055),
waveMaterial
);
wave.userData.baseX = x;
wave.userData.z = z;
wave.renderOrder = 4;
scene.add(wave);
waves.push(wave);
}
// ------------------------------------------------------------
// Waterline markers
// ------------------------------------------------------------
function makeWaterlineMarker(color) {
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.32,
roughness: 0.35,
depthTest: false
});
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.105, 0.075, DEPTH + 0.14),
material
);
marker.renderOrder = 10;
scene.add(marker);
return marker;
}
const lowMarker = makeWaterlineMarker(0x65d8ee);
const highMarker = makeWaterlineMarker(0xef845e);
const currentMarker = makeWaterlineMarker(0xffffff);
lowMarker.position.set(LOW_X, LOW_LEVEL + 0.035, 0);
highMarker.position.set(HIGH_X, HIGH_LEVEL + 0.035, 0);
// ------------------------------------------------------------
// Dimension arrows
// ------------------------------------------------------------
function cylinderBetween(start, end, radius, material) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const cylinder = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 1, 10),
material
);
cylinder.position.copy(start).add(end).multiplyScalar(0.5);
cylinder.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
cylinder.scale.y = length;
cylinder.renderOrder = 9;
scene.add(cylinder);
return cylinder;
}
function addArrowHead(position, direction, material) {
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.095, 0.25, 12),
material
);
head.position.copy(position);
head.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
head.renderOrder = 9;
scene.add(head);
}
const dimensionMaterial = new THREE.MeshBasicMaterial({
color: 0xc96f2d,
depthTest: false
});
// Vertical rise bracket.
const heightStart = new THREE.Vector3(
LOW_X - 0.62,
LOW_LEVEL,
FRONT_Z + 0.34
);
const heightEnd = new THREE.Vector3(
LOW_X - 0.62,
HIGH_LEVEL,
FRONT_Z + 0.34
);
cylinderBetween(heightStart, heightEnd, 0.025, dimensionMaterial);
addArrowHead(heightStart, new THREE.Vector3(0, -1, 0), dimensionMaterial);
addArrowHead(heightEnd, new THREE.Vector3(0, 1, 0), dimensionMaterial);
// Horizontal shoreline-shift bracket.
const runStart = new THREE.Vector3(
LOW_X,
HIGH_LEVEL + 0.48,
FRONT_Z + 0.34
);
const runEnd = new THREE.Vector3(
HIGH_X,
HIGH_LEVEL + 0.48,
FRONT_Z + 0.34
);
cylinderBetween(runStart, runEnd, 0.025, dimensionMaterial);
addArrowHead(runStart, new THREE.Vector3(-1, 0, 0), dimensionMaterial);
addArrowHead(runEnd, new THREE.Vector3(1, 0, 0), dimensionMaterial);
// ------------------------------------------------------------
// Camera controls implemented directly, without add-ons
// ------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0.1, -0.15, 0),
radius: window.innerWidth < 760 ? 21 : 17.5,
azimuth: -0.48,
elevation: 0.43
};
function updateCamera() {
const { target, radius, azimuth, elevation } = cameraState;
camera.position.set(
target.x + radius * Math.cos(elevation) * Math.sin(azimuth),
target.y + radius * Math.sin(elevation),
target.z + radius * Math.cos(elevation) * Math.cos(azimuth)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.radius = window.innerWidth < 760 ? 21 : 17.5;
cameraState.azimuth = -0.48;
cameraState.elevation = 0.43;
updateCamera();
}
updateCamera();
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.azimuth -= dx * 0.006;
cameraState.elevation = THREE.MathUtils.clamp(
cameraState.elevation + dy * 0.0045,
0.16,
1.15
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
11,
28
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
resetButton.addEventListener("click", resetCamera);
// ------------------------------------------------------------
// Tide state and UI
// ------------------------------------------------------------
let currentLevel = LOW_LEVEL;
let targetLevel = LOW_LEVEL;
let playing = false;
let direction = 1;
function setTargetLevel(value) {
playing = false;
targetLevel = THREE.MathUtils.clamp(value, LOW_LEVEL, HIGH_LEVEL);
playButton.textContent = "Run tide";
}
levelInput.addEventListener("input", () => {
setTargetLevel(Number(levelInput.value));
});
lowButton.addEventListener("click", () => {
setTargetLevel(LOW_LEVEL);
});
highButton.addEventListener("click", () => {
setTargetLevel(HIGH_LEVEL);
});
playButton.addEventListener("click", () => {
if (playing) {
playing = false;
targetLevel = currentLevel;
playButton.textContent = "Run tide";
} else {
playing = true;
direction = currentLevel >= HIGH_LEVEL - 0.001 ? -1 : 1;
playButton.textContent = "Pause tide";
}
});
function updateTideGeometry() {
const shorelineX = xAtLevel(currentLevel);
const waterWidth = shorelineX - OCEAN_LEFT;
const waterHeight = currentLevel - OCEAN_BOTTOM;
waterBlock.scale.set(waterWidth, waterHeight, DEPTH + 0.03);
waterBlock.position.set(
OCEAN_LEFT + waterWidth / 2,
OCEAN_BOTTOM + waterHeight / 2,
0
);
waterTop.scale.set(Math.max(0.1, waterWidth - 0.03), DEPTH - 0.03, 1);
waterTop.position.set(
OCEAN_LEFT + waterWidth / 2,
currentLevel + 0.025,
0
);
currentMarker.position.set(
shorelineX,
currentLevel + 0.042,
0
);
for (const wave of waves) {
wave.position.set(
wave.userData.baseX,
currentLevel + 0.044,
wave.userData.z
);
}
}
function updateInterface() {
const selectedRise = Math.max(0, currentLevel - LOW_LEVEL);
const selectedRun = selectedRise / SLOPE;
const signedLevel = `${currentLevel >= 0 ? "+" : "−"}${Math.abs(currentLevel).toFixed(2)} m`;
levelReadout.textContent = signedLevel;
if (!playing) {
levelInput.value = currentLevel.toFixed(2);
}
if (selectedRise < 0.015) {
narrative.textContent =
"The water is at the low waterline. Raising it will move the shoreline " +
"much farther across the gently sloping beach than the water rises vertically.";
} else {
narrative.textContent =
`A ${selectedRise.toFixed(2)} m rise moves the shoreline ` +
`${selectedRun.toFixed(2)} m farther inland. ` +
"The gentle slope converts a small vertical change into a long horizontal run.";
}
labelElements.height.textContent = `Δh = ${FULL_RISE.toFixed(2)} m`;
labelElements.run.textContent = `Δx = ${FULL_RUN.toFixed(2)} m`;
}
// ------------------------------------------------------------
// HTML labels projected from world positions
// ------------------------------------------------------------
function placeLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
placeLabel(
labelElements.low,
new THREE.Vector3(LOW_X, LOW_LEVEL, FRONT_Z + 0.12),
-5,
22
);
placeLabel(
labelElements.high,
new THREE.Vector3(HIGH_X, HIGH_LEVEL, FRONT_Z + 0.12),
8,
-22
);
const currentIsDistinct =
Math.abs(currentLevel - LOW_LEVEL) > 0.045 &&
Math.abs(currentLevel - HIGH_LEVEL) > 0.045;
labelElements.current.style.display = currentIsDistinct ? "block" : "none";
if (currentIsDistinct) {
placeLabel(
labelElements.current,
new THREE.Vector3(
xAtLevel(currentLevel),
currentLevel,
FRONT_Z + 0.13
),
36,
10
);
}
placeLabel(
labelElements.height,
new THREE.Vector3(
LOW_X - 0.69,
(LOW_LEVEL + HIGH_LEVEL) / 2,
FRONT_Z + 0.42
),
-25,
0
);
placeLabel(
labelElements.run,
new THREE.Vector3(
(LOW_X + HIGH_X) / 2,
HIGH_LEVEL + 0.55,
FRONT_Z + 0.42
),
0,
-16
);
placeLabel(
labelElements.sea,
new THREE.Vector3(-6.7, currentLevel + 0.12, 0),
0,
0
);
placeLabel(
labelElements.beach,
new THREE.Vector3(4.8, yAtX(4.8) + 0.12, 0),
0,
0
);
}
// ------------------------------------------------------------
// Resize and animation loop
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = camera.aspect < 0.8 ? 55 : 42;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
currentLevel +=
direction * dt * ((HIGH_LEVEL - LOW_LEVEL) / 2.6);
if (currentLevel >= HIGH_LEVEL) {
currentLevel = HIGH_LEVEL;
direction = -1;
} else if (currentLevel <= LOW_LEVEL) {
currentLevel = LOW_LEVEL;
direction = 1;
}
targetLevel = currentLevel;
levelInput.value = currentLevel.toFixed(2);
} else {
currentLevel = THREE.MathUtils.lerp(
currentLevel,
targetLevel,
1 - Math.exp(-dt * 8)
);
if (Math.abs(currentLevel - targetLevel) < 0.0005) {
currentLevel = targetLevel;
}
}
updateTideGeometry();
updateInterface();
updateLabels();
renderer.render(scene, camera);
}
updateTideGeometry();
updateInterface();
animate();
</script>
</body>
</html>
```