Prompt specification
Build or customize the Three.js 3D scene "Dam raises an upstream pool" 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: Dam raises an upstream pool
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/dam-raises-an-upstream-pool-x0650
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a branch barrier raising an upstream pool in a low-poly stream. Place the barrier between two uneven banks, using crossed branches with small gaps rather than a solid concrete wall. Begin with a shallow water surface, then gradually raise the upstream section as a modest flow continues through a visible outlet. Keep the downstream channel connected and show the difference between the two surface levels from a side view. Use broad transparent water shapes instead of a particle torrent. Label the water response as a qualitative illustration of an animal-built structure changing local flow.
Working Three.js Starter (dam-raises-an-upstream-pool-x0650):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Animal-built Branch Barrier — Local Flow Illustration</title>
<style>
:root {
color-scheme: light;
--ink: #173b3e;
--muted: #557174;
--paper: rgba(249, 245, 229, 0.92);
--line: rgba(23, 59, 62, 0.18);
--accent: #167d86;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e1ca;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#app {
position: relative;
width: 100%;
height: 100%;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.title-block {
position: absolute;
top: 24px;
left: 28px;
z-index: 3;
max-width: min(580px, calc(100vw - 56px));
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(27px, 3.3vw, 48px);
font-weight: 600;
line-height: 1.02;
letter-spacing: -0.025em;
}
.subtitle {
margin-top: 10px;
color: var(--muted);
font-size: 14px;
line-height: 1.45;
}
.controls {
position: absolute;
z-index: 5;
top: 24px;
right: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 999px;
padding: 9px 13px;
background: var(--paper);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
box-shadow: 0 5px 18px rgba(36, 56, 49, 0.08);
backdrop-filter: blur(8px);
}
button:hover {
border-color: rgba(22, 125, 134, 0.5);
color: #0c6269;
}
button:focus-visible {
outline: 3px solid rgba(22, 125, 134, 0.28);
outline-offset: 2px;
}
.caption {
position: absolute;
z-index: 4;
left: 28px;
bottom: 25px;
width: min(590px, calc(100vw - 56px));
padding: 15px 17px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--paper);
box-shadow: 0 10px 30px rgba(46, 62, 53, 0.09);
backdrop-filter: blur(10px);
pointer-events: none;
}
.caption strong {
display: block;
margin-bottom: 4px;
font-size: 13px;
}
.caption span {
display: block;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.hint {
position: absolute;
z-index: 3;
right: 26px;
bottom: 27px;
color: rgba(23, 59, 62, 0.68);
font-size: 11px;
text-align: right;
pointer-events: none;
}
.scene-tag {
position: absolute;
z-index: 2;
transform: translate(-50%, -100%);
padding: 6px 9px;
border: 1px solid rgba(23, 59, 62, 0.16);
border-radius: 8px;
background: rgba(249, 245, 229, 0.86);
color: var(--ink);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
white-space: nowrap;
pointer-events: none;
box-shadow: 0 4px 12px rgba(30, 55, 50, 0.07);
}
.scene-tag::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 13px;
background: rgba(23, 59, 62, 0.48);
}
.scene-tag.water {
border-color: rgba(22, 125, 134, 0.28);
background: rgba(232, 246, 242, 0.9);
color: #0b6269;
}
.scene-tag.outlet {
background: rgba(23, 59, 62, 0.9);
color: #f7f2df;
border-color: transparent;
}
.status {
position: absolute;
z-index: 3;
left: 50%;
bottom: 28px;
transform: translateX(-50%);
color: rgba(23, 59, 62, 0.72);
font-size: 11px;
letter-spacing: 0.04em;
pointer-events: none;
}
@media (max-width: 760px) {
.title-block {
top: 17px;
left: 18px;
}
.controls {
top: auto;
right: 16px;
bottom: 111px;
}
.caption {
left: 16px;
bottom: 16px;
width: calc(100vw - 32px);
}
.hint,
.status {
display: none;
}
.subtitle {
max-width: 72vw;
}
}
</style>
</head>
<body>
<main id="app">
<header class="title-block">
<div class="eyebrow">Local flow study</div>
<h1>Animal-built branch barrier</h1>
<div class="subtitle">
A crossed-branch structure holds back a shallow stream, creating an
upstream pool while the connected downstream channel continues to flow.
</div>
</header>
<nav class="controls" aria-label="Animation controls">
<button id="viewButton" type="button">Side view</button>
<button id="pauseButton" type="button">Pause</button>
<button id="resetButton" type="button">Reset pool</button>
</nav>
<div id="upstreamTag" class="scene-tag water">UPSTREAM POOL</div>
<div id="downstreamTag" class="scene-tag water">DOWNSTREAM CHANNEL →</div>
<div id="outletTag" class="scene-tag outlet">VISIBLE OUTLET</div>
<div id="gapTag" class="scene-tag">SMALL INTERSTICES</div>
<div id="levelTag" class="scene-tag water">LEVEL DIFFERENCE</div>
<div class="caption">
<strong>Water response — qualitative illustration</strong>
<span>
An animal-built structure changes local flow: the upstream pool rises,
water passes through small branch gaps, and a larger opening feeds the
connected downstream channel.
</span>
</div>
<div class="status" id="status">Pool rising gradually…</div>
<div class="hint">Drag to orbit · Scroll to zoom</div>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly stream with an animal-built branch barrier"
);
app.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e1ca);
scene.fog = new THREE.Fog(0xe9e1ca, 21, 38);
const camera = new THREE.OrthographicCamera(-9, 9, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.55, 0);
let azimuth = -0.22;
let elevation = 0.48;
let cameraDistance = 20;
let cameraZoom = 1;
function updateCamera() {
const horizontalDistance = Math.cos(elevation) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(azimuth) * horizontalDistance,
cameraTarget.y + Math.sin(elevation) * cameraDistance,
cameraTarget.z + Math.cos(azimuth) * horizontalDistance
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff5d6, 0x657b67, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0ca, 3.0);
sun.position.set(-7, 12, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0006;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9fd9d5, 0.65);
fill.position.set(7, 5, -9);
scene.add(fill);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x6e5941,
roughness: 1,
flatShading: true
});
const base = new THREE.Mesh(
new THREE.BoxGeometry(14.5, 1.15, 11.3),
groundMaterial
);
base.position.set(0, -1.19, 0);
base.receiveShadow = true;
scene.add(base);
function bankHeight(x, side = 1) {
const longSlope = 1.72 - 0.06 * (x + 7);
const unevenness =
0.065 * Math.sin(x * 1.55) +
0.035 * Math.cos(x * 0.72 + 0.6);
const nearBankAdjustment = side > 0 ? -0.12 : 0.08;
return longSlope + unevenness + nearBankAdjustment;
}
function bedHeight(x) {
return 0.12 - 0.043 * (x + 7) + 0.018 * Math.sin(x * 1.3);
}
function innerBankZ(x, side) {
const variation =
0.11 * Math.sin(x * 1.28) +
0.055 * Math.cos(x * 0.63 + 0.4);
return side * (2.52 + variation);
}
function makeBank(side) {
const positions = [];
const colors = [];
const segments = 16;
const x0 = -7.2;
const x1 = 7.2;
const outerZ = side * 5.55;
const earth = new THREE.Color(0x897451);
const grass = new THREE.Color(0x829264);
const edge = new THREE.Color(0x69563d);
function pushTriangle(a, b, c, color) {
positions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) colors.push(color.r, color.g, color.b);
}
for (let i = 0; i < segments; i++) {
const xa = THREE.MathUtils.lerp(x0, x1, i / segments);
const xb = THREE.MathUtils.lerp(x0, x1, (i + 1) / segments);
const ia = innerBankZ(xa, side);
const ib = innerBankZ(xb, side);
const ya = bankHeight(xa, side);
const yb = bankHeight(xb, side);
const oya = ya + 0.12 + 0.07 * Math.sin(xa * 0.9);
const oyb = yb + 0.12 + 0.07 * Math.sin(xb * 0.9);
const a = [xa, ya, ia];
const b = [xa, oya, outerZ];
const c = [xb, yb, ib];
const d = [xb, oyb, outerZ];
const topColor = grass.clone().lerp(earth, 0.18 + 0.08 * (i % 3));
if (side > 0) {
pushTriangle(a, b, d, topColor);
pushTriangle(a, d, c, topColor);
} else {
pushTriangle(a, d, b, topColor);
pushTriangle(a, c, d, topColor);
}
const bottomA = [xa, -0.68, ia];
const bottomB = [xb, -0.68, ib];
if (side > 0) {
pushTriangle(a, c, bottomB, edge);
pushTriangle(a, bottomB, bottomA, edge);
} else {
pushTriangle(a, bottomB, c, edge);
pushTriangle(a, bottomA, bottomB, edge);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
});
const bank = new THREE.Mesh(geometry, material);
bank.receiveShadow = true;
bank.castShadow = true;
scene.add(bank);
}
makeBank(1);
makeBank(-1);
function makeChannelBed() {
const positions = [];
const colors = [];
const nx = 18;
const nz = 5;
function point(x, z) {
return [x, bedHeight(x) + 0.025 * Math.sin(x * 2 + z), z];
}
function pushTriangle(a, b, c, color) {
positions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) colors.push(color.r, color.g, color.b);
}
for (let ix = 0; ix < nx; ix++) {
const xa = THREE.MathUtils.lerp(-7.2, 7.2, ix / nx);
const xb = THREE.MathUtils.lerp(-7.2, 7.2, (ix + 1) / nx);
for (let iz = 0; iz < nz; iz++) {
const za = THREE.MathUtils.lerp(-2.65, 2.65, iz / nz);
const zb = THREE.MathUtils.lerp(-2.65, 2.65, (iz + 1) / nz);
const a = point(xa, za);
const b = point(xb, za);
const c = point(xa, zb);
const d = point(xb, zb);
const color = new THREE.Color(0x776d4d).lerp(
new THREE.Color(0x948361),
0.25 + 0.12 * ((ix + iz) % 3)
);
pushTriangle(a, c, b, color);
pushTriangle(b, c, d, color);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
const bed = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
})
);
bed.receiveShadow = true;
scene.add(bed);
}
makeChannelBed();
function makeSurface(nx, nz, x0, x1, z0, z1) {
const positions = [];
const uvs = [];
const indices = [];
for (let iz = 0; iz <= nz; iz++) {
const z = THREE.MathUtils.lerp(z0, z1, iz / nz);
for (let ix = 0; ix <= nx; ix++) {
const x = THREE.MathUtils.lerp(x0, x1, ix / nx);
positions.push(x, 0, z);
uvs.push(ix / nx, iz / nz);
}
}
const row = nx + 1;
for (let iz = 0; iz < nz; iz++) {
for (let ix = 0; ix < nx; ix++) {
const a = iz * row + ix;
const b = a + 1;
const c = a + row;
const d = c + 1;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const upstreamMaterial = new THREE.MeshPhongMaterial({
color: 0x43a5aa,
transparent: true,
opacity: 0.57,
shininess: 85,
specular: 0xa9eeee,
side: THREE.DoubleSide,
depthWrite: false
});
const downstreamMaterial = new THREE.MeshPhongMaterial({
color: 0x3b9ca6,
transparent: true,
opacity: 0.54,
shininess: 75,
specular: 0x9ee3df,
side: THREE.DoubleSide,
depthWrite: false
});
const outletMaterial = new THREE.MeshPhongMaterial({
color: 0x79d0cf,
transparent: true,
opacity: 0.72,
shininess: 95,
specular: 0xffffff,
side: THREE.DoubleSide,
depthWrite: false
});
const seepMaterial = new THREE.MeshPhongMaterial({
color: 0x8adbd5,
transparent: true,
opacity: 0.42,
shininess: 70,
side: THREE.DoubleSide,
depthWrite: false
});
const upstreamGeometry = makeSurface(
24, 9, -6.65, -0.18, -2.43, 2.43
);
const downstreamGeometry = makeSurface(
27, 9, 0.18, 6.65, -2.43, 2.43
);
const outletGeometry = makeSurface(10, 6, -0.25, 0.4, 0.67, 1.7);
const seepGeometryA = makeSurface(7, 3, -0.2, 0.32, -1.78, -1.5);
const seepGeometryB = makeSurface(7, 3, -0.2, 0.32, -0.86, -0.62);
const upstreamWater = new THREE.Mesh(upstreamGeometry, upstreamMaterial);
const downstreamWater = new THREE.Mesh(
downstreamGeometry,
downstreamMaterial
);
const outletWater = new THREE.Mesh(outletGeometry, outletMaterial);
const seepWaterA = new THREE.Mesh(seepGeometryA, seepMaterial);
const seepWaterB = new THREE.Mesh(seepGeometryB, seepMaterial);
upstreamWater.renderOrder = 2;
downstreamWater.renderOrder = 2;
outletWater.renderOrder = 3;
seepWaterA.renderOrder = 3;
seepWaterB.renderOrder = 3;
scene.add(
upstreamWater,
downstreamWater,
outletWater,
seepWaterA,
seepWaterB
);
const barkMaterials = [
new THREE.MeshStandardMaterial({
color: 0x69442d,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xc49a68,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb98b5e,
roughness: 1,
flatShading: true
})
];
const yAxis = new THREE.Vector3(0, 1, 0);
function addBranch(a, b, radius = 0.11, bark = 0x69442d) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const materials = barkMaterials.map((material) => material.clone());
materials[0].color.setHex(bark);
const geometry = new THREE.CylinderGeometry(
radius * 0.86,
radius,
length,
6,
1,
false
);
const mesh = new THREE.Mesh(geometry, materials);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Upright stakes are driven into the uneven banks.
[
[-0.05, -2.72],
[-0.03, -1.35],
[0.02, 0.48],
[-0.04, 1.78],
[0.04, 2.72]
].forEach(([x, z], index) => {
const lean = index % 2 === 0 ? -0.13 : 0.12;
addBranch(
[x, -0.2, z],
[x + lean, 1.38, z],
0.085,
index % 2 ? 0x775035 : 0x5e3d29
);
});
// Far-side crossed branches stop before the larger outlet.
addBranch([-0.56, 0.14, -2.87], [0.48, 0.43, 0.52], 0.13);
addBranch([0.53, 0.27, -2.84], [-0.48, 0.66, 0.57], 0.12);
addBranch([-0.52, 0.52, -2.84], [0.47, 0.91, 0.50], 0.115);
addBranch([0.50, 0.78, -2.82], [-0.42, 1.18, 0.48], 0.105);
addBranch([-0.45, 1.03, -2.62], [0.36, 1.30, 0.40], 0.085);
// Near-side branches leave a visible opening from z ≈ 0.58 to 1.78.
addBranch([0.48, 0.23, 1.82], [-0.52, 0.52, 2.96], 0.12);
addBranch([-0.50, 0.40, 1.80], [0.49, 0.73, 2.94], 0.11);
addBranch([0.48, 0.65, 1.82], [-0.44, 0.98, 2.93], 0.105);
addBranch([-0.46, 0.91, 1.79], [0.40, 1.23, 2.93], 0.09);
// A thin upper twig crosses above the outlet rather than sealing it.
addBranch([-0.38, 1.29, 0.43], [0.34, 1.36, 1.78], 0.055, 0x876044);
// Short branch stubs reinforce the hand-built appearance.
addBranch([0.13, 0.58, -1.85], [0.48, 0.78, -1.58], 0.055);
addBranch([-0.12, 0.85, 2.35], [-0.43, 1.02, 2.55], 0.05);
addBranch([0.22, 0.47, -0.74], [0.52, 0.63, -0.52], 0.045);
function makeRock(x, z, side, scale) {
const material = new THREE.MeshStandardMaterial({
color: new THREE.Color().setHSL(0.11, 0.11, 0.48 + 0.05 * scale),
roughness: 1,
flatShading: true
});
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.36 * scale, 0),
material
);
rock.position.set(x, bankHeight(x, side) + 0.16 * scale, z);
rock.scale.set(1.25, 0.78, 1);
rock.rotation.set(0.2 * scale, x * 0.8, 0.12);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
[
[-5.6, 3.35, 1, 1.1],
[-4.5, 4.15, 1, 0.65],
[-2.9, 3.65, 1, 0.85],
[2.4, 3.7, 1, 0.8],
[4.9, 4.25, 1, 1.2],
[-3.7, -3.8, -1, 0.8],
[1.2, -3.7, -1, 0.65],
[4.7, -3.35, -1, 1.05]
].forEach(([x, z, side, scale]) => makeRock(x, z, side, scale));
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x68452f,
roughness: 1,
flatShading: true
});
const leafMaterials = [
new THREE.MeshStandardMaterial({
color: 0x58785b,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x6c875d,
roughness: 1,
flatShading: true
})
];
function makeTree(x, z, side, height) {
const group = new THREE.Group();
group.position.set(x, bankHeight(x, side), z);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.1, 0.15, height * 0.66, 6),
trunkMaterial
);
trunk.position.y = height * 0.33;
trunk.castShadow = true;
group.add(trunk);
const lower = new THREE.Mesh(
new THREE.ConeGeometry(height * 0.31, height * 0.72, 7),
leafMaterials[0]
);
lower.position.y = height * 0.67;
lower.castShadow = true;
group.add(lower);
const upper = new THREE.Mesh(
new THREE.ConeGeometry(height * 0.22, height * 0.55, 7),
leafMaterials[1]
);
upper.position.y = height * 0.98;
upper.castShadow = true;
group.add(upper);
scene.add(group);
}
makeTree(-6.0, 4.6, 1, 1.7);
makeTree(-3.8, -4.4, -1, 1.45);
makeTree(0.7, 4.7, 1, 1.25);
makeTree(4.2, -4.6, -1, 1.6);
makeTree(6.3, 4.3, 1, 1.4);
function makeShrub(x, z, side, scale) {
const shrub = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.35 * scale, 0),
leafMaterials[1]
);
shrub.position.set(x, bankHeight(x, side) + 0.2 * scale, z);
shrub.scale.y = 0.72;
shrub.rotation.set(0.2, x, 0.1);
shrub.castShadow = true;
scene.add(shrub);
}
makeShrub(-5.1, 3.65, 1, 0.9);
makeShrub(-1.7, 4.1, 1, 0.7);
makeShrub(2.9, -3.5, -1, 0.9);
makeShrub(5.6, 3.65, 1, 0.75);
// A small, stylized beaver marks the structure as animal-built.
function makeBeaver() {
const beaver = new THREE.Group();
const fur = new THREE.MeshStandardMaterial({
color: 0x684735,
roughness: 1,
flatShading: true
});
const darkFur = new THREE.MeshStandardMaterial({
color: 0x3f3028,
roughness: 1,
flatShading: true
});
const lightFur = new THREE.MeshStandardMaterial({
color: 0xa98560,
roughness: 1,
flatShading: true
});
const body = new THREE.Mesh(new THREE.SphereGeometry(0.3, 8, 5), fur);
body.scale.set(1.45, 0.82, 0.76);
body.castShadow = true;
beaver.add(body);
const head = new THREE.Mesh(new THREE.SphereGeometry(0.19, 8, 5), fur);
head.position.set(0.38, 0.07, 0);
head.scale.set(1.08, 0.9, 0.9);
head.castShadow = true;
beaver.add(head);
const snout = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.12, 0.19),
lightFur
);
snout.position.set(0.54, 0.035, 0);
snout.castShadow = true;
beaver.add(snout);
const tail = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.08, 0.33),
darkFur
);
tail.position.set(-0.52, -0.14, 0);
tail.rotation.z = -0.13;
tail.castShadow = true;
beaver.add(tail);
for (const z of [-0.13, 0.13]) {
const ear = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 6, 4),
darkFur
);
ear.position.set(0.33, 0.22, z);
beaver.add(ear);
}
const x = -1.75;
const z = 3.48;
beaver.position.set(x, bankHeight(x, 1) + 0.25, z);
beaver.rotation.y = -0.28;
scene.add(beaver);
}
makeBeaver();
// Side-view level guide.
const guideMaterial = new THREE.LineDashedMaterial({
color: 0x0e747c,
dashSize: 0.12,
gapSize: 0.07,
transparent: true,
opacity: 0.88,
depthTest: false
});
const solidGuideMaterial = new THREE.LineBasicMaterial({
color: 0x0e747c,
transparent: true,
opacity: 0.88,
depthTest: false
});
function makeGuideLine(material, count = 2) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(new Float32Array(count * 3), 3)
);
const line = new THREE.Line(geometry, material);
line.renderOrder = 10;
scene.add(line);
return line;
}
const upstreamGuide = makeGuideLine(guideMaterial);
const downstreamGuide = makeGuideLine(guideMaterial);
const verticalGuide = makeGuideLine(guideMaterial);
const upperTick = makeGuideLine(solidGuideMaterial);
const lowerTick = makeGuideLine(solidGuideMaterial);
function setLine(line, points) {
const attribute = line.geometry.attributes.position;
points.forEach((point, index) => {
attribute.setXYZ(index, point[0], point[1], point[2]);
});
attribute.needsUpdate = true;
line.geometry.computeBoundingSphere();
if (line.material.isLineDashedMaterial) line.computeLineDistances();
}
const guideZ = 2.28;
const guideX = 0.92;
const downstreamLevel = 0.32;
function updateLevelGuide(poolLevel) {
setLine(upstreamGuide, [
[-0.85, poolLevel, guideZ],
[guideX, poolLevel, guideZ]
]);
setLine(downstreamGuide, [
[guideX, downstreamLevel, guideZ],
[2.25, downstreamLevel, guideZ]
]);
setLine(verticalGuide, [
[guideX, downstreamLevel, guideZ],
[guideX, poolLevel, guideZ]
]);
setLine(upperTick, [
[guideX - 0.16, poolLevel, guideZ],
[guideX + 0.16, poolLevel, guideZ]
]);
setLine(lowerTick, [
[guideX - 0.16, downstreamLevel, guideZ],
[guideX + 0.16, downstreamLevel, guideZ]
]);
}
const labels = {
upstream: document.querySelector("#upstreamTag"),
downstream: document.querySelector("#downstreamTag"),
outlet: document.querySelector("#outletTag"),
gap: document.querySelector("#gapTag"),
level: document.querySelector("#levelTag")
};
const projected = new THREE.Vector3();
function placeLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
const screenX = (projected.x * 0.5 + 0.5) * window.innerWidth;
const screenY = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${screenX}px`;
element.style.top = `${screenY}px`;
element.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
}
function updateLabels(poolLevel) {
placeLabel(labels.upstream, -3.65, poolLevel + 0.31, -0.45);
placeLabel(labels.downstream, 3.65, 0.51, -0.45);
placeLabel(labels.outlet, 0.02, 1.55, 1.15);
placeLabel(labels.gap, -0.72, 0.95, -1.55);
placeLabel(
labels.level,
1.55,
(poolLevel + downstreamLevel) * 0.5,
guideZ
);
}
function updateWater(geometry, heightFunction, time) {
const position = geometry.attributes.position;
for (let i = 0; i < position.count; i++) {
const x = position.getX(i);
const z = position.getZ(i);
position.setY(i, heightFunction(x, z, time));
}
position.needsUpdate = true;
geometry.computeVertexNormals();
}
function downstreamHeight(x) {
return downstreamLevel - 0.043 * Math.max(0, x - 0.18);
}
function smoothStep(t) {
t = THREE.MathUtils.clamp(t, 0, 1);
return t * t * (3 - 2 * t);
}
let elapsed = 0;
let simulationTime = 0;
let paused = false;
let poolLevel = 0.38;
function updatePool() {
const riseDuration = 24;
const progress = smoothStep(elapsed / riseDuration);
poolLevel = THREE.MathUtils.lerp(0.38, 1.08, progress);
upstreamMaterial.opacity = 0.51 + progress * 0.09;
outletMaterial.opacity = 0.58 + progress * 0.17;
updateLevelGuide(poolLevel);
const status = document.querySelector("#status");
if (progress < 0.995) {
status.textContent = "Pool rising gradually…";
} else {
status.textContent = "Upstream pool established · downstream flow remains connected";
}
}
function updateSimulation(dt) {
if (!paused) {
elapsed += dt;
simulationTime += dt;
}
updatePool();
const waveStrength = 0.012 + 0.012 * smoothStep(elapsed / 24);
updateWater(
upstreamGeometry,
(x, z, time) =>
poolLevel +
waveStrength *
(Math.sin(x * 1.7 + time * 0.9) +
0.45 * Math.sin(z * 2.6 - time * 0.7)),
simulationTime
);
updateWater(
downstreamGeometry,
(x, z, time) =>
downstreamHeight(x) +
waveStrength *
(Math.sin(x * 1.85 - time * 1.25) +
0.35 * Math.sin(z * 2.9 + time * 0.6)),
simulationTime
);
updateWater(
outletGeometry,
(x, z, time) => {
const u = THREE.MathUtils.clamp((x + 0.25) / 0.65, 0, 1);
const fall = smoothStep(u);
return (
THREE.MathUtils.lerp(poolLevel, downstreamLevel, fall) +
0.014 * Math.sin(time * 3.1 + z * 5)
);
},
simulationTime
);
function updateSeep(geometry, centerZ) {
updateWater(
geometry,
(x, z, time) => {
const u = THREE.MathUtils.clamp((x + 0.2) / 0.52, 0, 1);
return (
THREE.MathUtils.lerp(poolLevel, downstreamLevel, smoothStep(u)) +
0.008 * Math.sin(time * 2.7 + centerZ * 4)
);
},
simulationTime
);
}
updateSeep(seepGeometryA, -1.65);
updateSeep(seepGeometryB, -0.74);
}
// Direct, dependency-free camera controls.
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
azimuth -= deltaX * 0.005;
elevation = THREE.MathUtils.clamp(
elevation + deltaY * 0.004,
0.18,
1.05
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.75
);
updateCamera();
},
{ passive: false }
);
document.querySelector("#viewButton").addEventListener("click", () => {
azimuth = -0.22;
elevation = 0.48;
cameraZoom = 1;
updateCamera();
});
const pauseButton = document.querySelector("#pauseButton");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
document.querySelector("#resetButton").addEventListener("click", () => {
elapsed = 0;
paused = false;
pauseButton.textContent = "Pause";
updatePool();
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
const viewHeight = Math.max(10.8, 16 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
updateSimulation(dt);
updateLabels(poolLevel);
renderer.render(scene, camera);
}
updatePool();
animate();
</script>
</body>
</html>
```