Prompt specification
Build or customize the Three.js 3D scene "Branch dam cross-section" 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: Branch dam cross-section
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/branch-dam-cross-section-x0649
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a beaver dam. Model interwoven branches across a narrow stream with a visible lower opening; a side cutaway distinguishes dam material, upstream water, downstream flow, and bank supports. Voxel style.
Working Three.js Starter (branch-dam-cross-section-x0649):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Beaver Dam</title>
<style>
:root {
--ink: #173c3a;
--muted: #54706b;
--paper: rgba(255, 252, 239, 0.94);
--dam: #a86636;
--upstream: #42a9d4;
--downstream: #35bca9;
--support: #e39a43;
--shadow: 0 14px 38px rgba(24, 61, 55, 0.17);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cbe7df;
color: var(--ink);
font-family:
Inter, ui-rounded, "Avenir Next", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.title-card {
position: absolute;
top: 22px;
left: 22px;
min-width: 270px;
padding: 17px 19px 16px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 18px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: #568075;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--downstream);
content: "";
box-shadow: 0 0 0 3px rgba(53, 188, 169, 0.16);
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 34px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.controls {
position: absolute;
top: 22px;
right: 22px;
display: flex;
gap: 8px;
padding: 7px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 15px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
pointer-events: auto;
}
button {
border: 0;
border-radius: 10px;
padding: 10px 13px;
background: transparent;
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
background 160ms ease,
color 160ms ease,
transform 160ms ease;
}
button:hover {
background: rgba(51, 117, 106, 0.09);
}
button:active {
transform: translateY(1px);
}
button.active {
background: var(--ink);
color: #fffdf2;
}
#cutaway-card {
position: absolute;
top: 91px;
right: 22px;
width: 342px;
padding: 16px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 19px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(13px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
#cutaway-card.hidden {
opacity: 0;
transform: translateY(-8px) scale(0.98);
pointer-events: none;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
}
.section-heading strong {
font-size: 12px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.section-tag {
padding: 4px 7px;
border-radius: 6px;
background: #e8eee7;
color: #63766f;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
}
.section-copy {
margin: 0 0 11px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.cutaway-svg {
display: block;
width: 100%;
height: auto;
overflow: visible;
}
.cutaway-legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px 10px;
margin-top: 10px;
}
.cutaway-legend span {
display: flex;
align-items: center;
gap: 7px;
color: #49645f;
font-size: 10px;
font-weight: 700;
}
.swatch {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 2px;
}
.quick-legend {
position: absolute;
bottom: 22px;
left: 22px;
display: flex;
flex-wrap: wrap;
gap: 7px;
max-width: min(580px, calc(100vw - 44px));
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 15px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.quick-legend span {
display: flex;
align-items: center;
gap: 6px;
padding: 5px 7px;
border-radius: 8px;
color: #49645f;
font-size: 10px;
font-weight: 750;
}
.hint {
position: absolute;
right: 22px;
bottom: 22px;
padding: 9px 12px;
border-radius: 11px;
background: rgba(23, 60, 58, 0.78);
color: #effff9;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.02em;
backdrop-filter: blur(8px);
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 8px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 8px;
background: rgba(255, 252, 239, 0.92);
color: #244b47;
box-shadow: 0 5px 15px rgba(29, 65, 59, 0.12);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.075em;
white-space: nowrap;
opacity: 0;
transition: opacity 120ms ease;
}
.world-label::after {
position: absolute;
top: 50%;
left: 50%;
width: 1px;
height: 15px;
background: currentColor;
opacity: 0.35;
content: "";
}
.world-label i {
display: block;
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--label-color);
}
.world-label.dam-label {
--label-color: var(--dam);
}
.world-label.up-label {
--label-color: var(--upstream);
}
.world-label.down-label {
--label-color: var(--downstream);
}
.world-label.support-label {
--label-color: var(--support);
}
.world-label.opening-label {
--label-color: #eafcff;
}
@media (max-width: 800px) {
.title-card {
top: 12px;
left: 12px;
min-width: 0;
max-width: calc(100vw - 24px);
padding: 13px 15px;
}
.subtitle {
display: none;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
}
#cutaway-card {
top: 88px;
right: 12px;
width: min(342px, calc(100vw - 24px));
}
.quick-legend {
display: none;
}
.hint {
right: 12px;
bottom: 68px;
}
}
</style>
</head>
<body>
<div id="labels" aria-hidden="true">
<div class="world-label dam-label" data-label="dam"><i></i>DAM MATERIAL</div>
<div class="world-label up-label" data-label="upstream"><i></i>UPSTREAM WATER</div>
<div class="world-label down-label" data-label="downstream"><i></i>DOWNSTREAM FLOW</div>
<div class="world-label support-label" data-label="support"><i></i>BANK SUPPORT</div>
<div class="world-label opening-label" data-label="opening"><i></i>LOWER OPENING</div>
</div>
<div class="ui">
<section class="title-card">
<div class="eyebrow">Voxel river study</div>
<h1>Beaver Dam</h1>
<p class="subtitle">
Interwoven branches, a low water opening, and a longitudinal cutaway.
</p>
</section>
<nav class="controls" aria-label="Camera views">
<button id="overview-button" class="active" type="button">Overview</button>
<button id="cutaway-button" type="button">Side cutaway</button>
</nav>
<aside id="cutaway-card" aria-label="Side cutaway diagram">
<div class="section-heading">
<strong>Side Cutaway</strong>
<span class="section-tag">SECTION A—A</span>
</div>
<p class="section-copy">
Longitudinal view through the dam, showing distinct water levels,
branch material, lower opening, and orange bank abutments.
</p>
<svg
class="cutaway-svg"
viewBox="0 0 340 188"
role="img"
aria-label="Side cutaway of a beaver dam with upstream water, downstream flow, branch material, bank supports, and a lower opening"
>
<defs>
<marker
id="flow-arrow"
markerWidth="7"
markerHeight="7"
refX="5.5"
refY="3.5"
orient="auto"
>
<path d="M0,0 L7,3.5 L0,7 Z" fill="#dffff8"></path>
</marker>
</defs>
<!-- Stream bed -->
<rect x="12" y="138" width="316" height="22" rx="5" fill="#b78a60"></rect>
<rect x="12" y="138" width="316" height="7" rx="3" fill="#8f674a"></rect>
<!-- Upstream and downstream water volumes -->
<rect x="17" y="70" width="133" height="68" rx="5" fill="#42a9d4"></rect>
<rect x="188" y="96" width="135" height="42" rx="5" fill="#35bca9"></rect>
<!-- Water surface highlights -->
<rect x="24" y="74" width="117" height="5" rx="2.5" fill="#8fd9ec"></rect>
<rect x="195" y="100" width="119" height="5" rx="2.5" fill="#91e2d3"></rect>
<!-- Dam branch section -->
<g stroke="#684129" stroke-width="2">
<rect x="147" y="24" width="46" height="15" rx="3" fill="#b8783f"></rect>
<rect x="143" y="40" width="53" height="15" rx="3" fill="#985b32"></rect>
<rect x="149" y="56" width="42" height="16" rx="3" fill="#c18348"></rect>
<rect x="145" y="73" width="49" height="17" rx="3" fill="#9c6135"></rect>
<rect x="142" y="91" width="19" height="47" rx="3" fill="#a66537"></rect>
<rect x="179" y="91" width="20" height="47" rx="3" fill="#8d542f"></rect>
<rect x="147" y="87" width="48" height="10" rx="3" fill="#b77740"></rect>
</g>
<!-- Lower opening and water passing through it -->
<path
d="M161 104 H179 V138 H161 Z"
fill="#58c7d7"
stroke="#376f72"
stroke-width="2"
></path>
<path
d="M168 111 H172 V136"
fill="none"
stroke="#d7ffff"
stroke-width="3"
opacity="0.9"
></path>
<!-- Orange bank supports -->
<g stroke="#8c552d" stroke-width="2">
<rect x="134" y="100" width="13" height="40" rx="3" fill="#e39a43"></rect>
<rect x="194" y="100" width="13" height="40" rx="3" fill="#e39a43"></rect>
<rect x="130" y="128" width="20" height="12" rx="3" fill="#ca7a34"></rect>
<rect x="191" y="128" width="20" height="12" rx="3" fill="#ca7a34"></rect>
</g>
<!-- Flow arrows -->
<path
d="M50 112 H115"
fill="none"
stroke="#dffff8"
stroke-width="5"
marker-end="url(#flow-arrow)"
></path>
<path
d="M224 118 H298"
fill="none"
stroke="#dffff8"
stroke-width="5"
marker-end="url(#flow-arrow)"
></path>
<!-- Small section annotations -->
<g fill="#41605a" font-family="system-ui, sans-serif" font-size="9" font-weight="800">
<text x="28" y="57">UPSTREAM</text>
<text x="224" y="86">DOWNSTREAM</text>
<text x="153" y="16">DAM</text>
<text x="151" y="157" text-anchor="middle">OPENING</text>
<text x="120" y="174" text-anchor="middle">SUPPORT</text>
<text x="218" y="174" text-anchor="middle">SUPPORT</text>
</g>
</svg>
<div class="cutaway-legend">
<span><i class="swatch" style="background:var(--dam)"></i>Dam material</span>
<span><i class="swatch" style="background:var(--upstream)"></i>Upstream water</span>
<span><i class="swatch" style="background:var(--downstream)"></i>Downstream flow</span>
<span><i class="swatch" style="background:var(--support)"></i>Bank supports</span>
</div>
</aside>
<div class="quick-legend">
<span><i class="swatch" style="background:var(--dam)"></i>Dam material</span>
<span><i class="swatch" style="background:var(--upstream)"></i>Upstream water</span>
<span><i class="swatch" style="background:var(--downstream)"></i>Downstream flow</span>
<span><i class="swatch" style="background:var(--support)"></i>Bank supports</span>
</div>
<div class="hint">Drag to orbit · Right-drag to pan · Scroll to zoom</div>
</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, lighting
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcbe7df);
scene.fog = new THREE.Fog(0xcbe7df, 24, 48);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
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;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel scene of a beaver dam across a stream"
);
document.body.prepend(renderer.domElement);
const hemiLight = new THREE.HemisphereLight(0xe8ffff, 0x856846, 2.0);
scene.add(hemiLight);
const sun = new THREE.DirectionalLight(0xfff2cf, 3.4);
sun.position.set(-9, 16, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 17;
sun.shadow.camera.bottom = -17;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 42;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x85d9ed, 1.0);
fill.position.set(10, 7, -12);
scene.add(fill);
// ------------------------------------------------------------
// Materials and voxel helpers
// ------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
const mats = {
soil: material(0xa57b55),
bed: material(0xc49a6b),
bank: material(0x78984e),
bankTop: material(0x86a95b),
grass: material(0x5f873e),
grassLight: material(0x8bad58),
reed: material(0x719844),
rock: material(0x8c9188),
rockLight: material(0xa7aaa0),
trunk: material(0x765039),
leaves: material(0x4f7e43),
leavesLight: material(0x67954c),
support: material(0xc98138),
supportLight: material(0xe39a43),
supportDark: material(0x80502e),
openingDark: material(0x4d3426),
beaver: material(0x6e4932),
beaverDark: material(0x513426),
beaverLight: material(0x8a6040),
eye: material(0x151b1a),
tooth: material(0xfff4cf),
foam: material(0xe8ffff, { roughness: 0.45 })
};
const branchMaterials = [
material(0x92572f),
material(0xa96637),
material(0xb87842),
material(0x87502d),
material(0xc0844d)
];
const upstreamMaterial = material(0x42a9d4, {
roughness: 0.28,
transparent: true,
opacity: 0.88,
depthWrite: false
});
const downstreamMaterial = material(0x35bca9, {
roughness: 0.3,
transparent: true,
opacity: 0.86,
depthWrite: false
});
const waveUpMaterial = material(0x8bd9ed, { roughness: 0.45 });
const waveDownMaterial = material(0x92e1d1, { roughness: 0.45 });
function box(
parent,
mat,
size,
position,
{ cast = true, receive = true } = {}
) {
const mesh = new THREE.Mesh(unitBox, mat);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
function branchBeam(
parent,
start,
end,
z,
mat,
width = 0.34,
depth = 0.48
) {
const dx = end[0] - start[0];
const dy = end[1] - start[1];
const length = Math.hypot(dx, dy);
const angle = Math.atan2(dy, dx);
const midpoint = [
(start[0] + end[0]) / 2,
(start[1] + end[1]) / 2,
z
];
const beam = box(
parent,
mat,
[length, width, depth],
midpoint
);
beam.rotation.z = angle;
// Small bark bands make each branch read as a segmented voxel object.
const jointCount = Math.max(1, Math.floor(length / 1.1));
for (let i = 1; i < jointCount; i++) {
const t = i / jointCount;
const joint = box(
parent,
i % 2 ? branchMaterials[0] : branchMaterials[3],
[0.13, width * 1.08, depth * 1.05],
[
start[0] + dx * t,
start[1] + dy * t,
z + (i % 2 ? 0.025 : -0.025)
]
);
joint.rotation.z = angle;
}
return beam;
}
// ------------------------------------------------------------
// Terrain and stream
// ------------------------------------------------------------
box(scene, mats.soil, [18, 0.8, 24], [0, -0.78, 0]);
box(scene, mats.bed, [8, 0.34, 24], [0, -0.28, 0]);
box(scene, mats.bank, [4, 1.2, 24], [-6, 0.02, 0]);
box(scene, mats.bank, [4, 1.2, 24], [6, 0.02, 0]);
box(scene, mats.bankTop, [3.7, 0.18, 23.7], [-6.1, 0.65, 0]);
box(scene, mats.bankTop, [3.7, 0.18, 23.7], [6.1, 0.65, 0]);
// Upstream pool is deeper than the downstream run.
box(
scene,
upstreamMaterial,
[8, 1.1, 8.3],
[0, 0.2, -4.55],
{ cast: false }
);
box(
scene,
downstreamMaterial,
[8, 0.62, 9.3],
[0, 0.08, 5.25],
{ cast: false }
);
// Thin water surface visible through the lower opening.
box(
scene,
downstreamMaterial,
[2.75, 0.13, 1.55],
[0, 0.35, 0.05],
{ cast: false }
);
const waterTiles = [];
for (let i = 0; i < 28; i++) {
const x = -3.5 + Math.random() * 7;
const z = -8.1 + Math.random() * 7.15;
const tile = box(
scene,
waveUpMaterial,
[0.55 + Math.random() * 0.75, 0.045, 0.24 + Math.random() * 0.28],
[x, 0.77, z],
{ cast: false }
);
waterTiles.push({
mesh: tile,
baseY: 0.77,
phase: Math.random() * Math.PI * 2,
amount: 0.012
});
}
for (let i = 0; i < 22; i++) {
const x = -3.5 + Math.random() * 7;
const z = 1.05 + Math.random() * 8.25;
const tile = box(
scene,
waveDownMaterial,
[0.45 + Math.random() * 0.9, 0.04, 0.2 + Math.random() * 0.3],
[x, 0.415, z],
{ cast: false }
);
waterTiles.push({
mesh: tile,
baseY: 0.415,
phase: Math.random() * Math.PI * 2,
amount: 0.018
});
}
// ------------------------------------------------------------
// Beaver dam: interwoven branch layers with a clear lower opening
// ------------------------------------------------------------
const damGroup = new THREE.Group();
scene.add(damGroup);
// Lower side branches leave the center open.
branchBeam(damGroup, [-4.65, 0.78], [-1.55, 0.78], -0.42, branchMaterials[1]);
branchBeam(damGroup, [-4.5, 1.22], [-1.62, 1.22], 0.32, branchMaterials[0]);
branchBeam(damGroup, [1.62, 0.78], [4.65, 0.78], 0.34, branchMaterials[0]);
branchBeam(damGroup, [1.55, 1.22], [4.5, 1.22], -0.38, branchMaterials[2]);
// Upper lintel and layered woven branches.
branchBeam(damGroup, [-4.55, 1.72], [4.55, 1.72], -0.04, branchMaterials[3], 0.42, 0.62);
branchBeam(damGroup, [-4.5, 2.08], [4.5, 2.08], 0.24, branchMaterials[1]);
branchBeam(damGroup, [-4.6, 2.48], [4.6, 2.48], -0.37, branchMaterials[2]);
branchBeam(damGroup, [-4.35, 2.88], [4.35, 2.88], 0.28, branchMaterials[0]);
branchBeam(damGroup, [-4.5, 3.28], [4.5, 3.28], -0.2, branchMaterials[4]);
branchBeam(damGroup, [-4.3, 3.68], [4.3, 3.68], 0.3, branchMaterials[1]);
branchBeam(damGroup, [-4.0, 4.08], [4.0, 4.08], -0.26, branchMaterials[2]);
// Diagonal branches cross the upper pile, creating a woven structure.
branchBeam(damGroup, [-4.45, 1.15], [-1.85, 4.35], 0.55, branchMaterials[4]);
branchBeam(damGroup, [4.45, 1.15], [1.85, 4.35], 0.55, branchMaterials[0]);
branchBeam(damGroup, [-3.9, 2.05], [3.95, 4.45], -0.58, branchMaterials[2], 0.3, 0.42);
branchBeam(damGroup, [3.9, 1.85], [-3.85, 4.1], 0.02, branchMaterials[3], 0.29, 0.42);
// Upright stakes remain outside the opening.
const stakeXs = [-4.0, -3.35, -2.6, -1.78, 1.78, 2.55, 3.3, 4.0];
stakeXs.forEach((x, index) => {
branchBeam(
damGroup,
[x, 0.48],
[x + (index % 2 ? 0.08 : -0.08), 4.5],
index % 2 ? -0.55 : 0.52,
branchMaterials[index % branchMaterials.length],
0.27,
0.39
);
});
// Dark frame emphasizes the recessed lower opening.
branchBeam(damGroup, [-1.54, 0.42], [-1.54, 1.7], -0.56, mats.openingDark, 0.27, 0.35);
branchBeam(damGroup, [1.54, 0.42], [1.54, 1.7], -0.56, mats.openingDark, 0.27, 0.35);
branchBeam(damGroup, [-1.54, 1.7], [1.54, 1.7], -0.56, mats.openingDark, 0.28, 0.35);
// Small branch ends and knots.
for (let i = 0; i < 24; i++) {
const x = -4.2 + Math.random() * 8.4;
const y = 1.9 + Math.random() * 2.5;
const z = 0.55 + Math.random() * 0.15;
const knot = box(
damGroup,
branchMaterials[(i + 1) % branchMaterials.length],
[0.3, 0.3, 0.24],
[x, y, z]
);
knot.rotation.z = Math.random() * Math.PI;
}
// ------------------------------------------------------------
// Distinct orange bank supports / abutments
// ------------------------------------------------------------
const supportGroup = new THREE.Group();
scene.add(supportGroup);
for (const side of [-1, 1]) {
branchBeam(
supportGroup,
[side * 4.45, 0.25],
[side * 4.2, 3.1],
0,
mats.support,
0.58,
0.72
);
branchBeam(
supportGroup,
[side * 4.65, 0.35],
[side * 3.15, 2.55],
0.25,
mats.supportLight,
0.46,
0.62
);
branchBeam(
supportGroup,
[side * 4.7, 2.55],
[side * 3.25, 2.45],
-0.18,
mats.supportDark,
0.38,
0.54
);
box(
supportGroup,
mats.supportLight,
[1.25, 0.35, 1.25],
[side * 4.35, 0.52, 0.05]
);
}
// ------------------------------------------------------------
// Voxel banks, reeds, rocks, and trees
// ------------------------------------------------------------
let seed = 37;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
for (let i = 0; i < 64; i++) {
const side = i % 2 === 0 ? -1 : 1;
const x = side * (4.45 + random() * 2.8);
const z = -11 + random() * 22;
const h = 0.16 + random() * 0.38;
const grass = box(
scene,
random() > 0.5 ? mats.grass : mats.grassLight,
[0.22 + random() * 0.26, h, 0.22 + random() * 0.26],
[x, 0.73 + h / 2, z]
);
grass.rotation.y = Math.random() * 0.4;
}
for (let i = 0; i < 18; i++) {
const side = i % 2 === 0 ? -1 : 1;
const x = side * (4.0 + random() * 0.45);
const z = -9 + random() * 18;
const baseY = 0.73;
for (let j = 0; j < 3; j++) {
const reed = box(
scene,
mats.reed,
[0.075, 0.65 + random() * 0.5, 0.075],
[
x + (random() - 0.5) * 0.35,
baseY + 0.35,
z + (random() - 0.5) * 0.35
]
);
reed.rotation.z = (random() - 0.5) * 0.2;
}
}
for (let i = 0; i < 22; i++) {
const side = i % 2 === 0 ? -1 : 1;
const x = side * (4.25 + random() * 2.5);
const z = -10.5 + random() * 21;
const size = 0.25 + random() * 0.42;
box(
scene,
random() > 0.5 ? mats.rock : mats.rockLight,
[size, size * 0.65, size],
[x, 0.72 + size * 0.325, z]
);
}
function voxelTree(x, z, scale = 1) {
box(
scene,
mats.trunk,
[0.46 * scale, 3.6 * scale, 0.46 * scale],
[x, 0.7 + 1.8 * scale, z]
);
const foliage = [
[-0.55, 3.65, 0.1, 1.45],
[0.5, 3.7, -0.05, 1.5],
[0, 4.35, 0.05, 1.7],
[-0.55, 4.35, -0.1, 1.25],
[0.55, 4.3, 0.1, 1.25]
];
foliage.forEach((f, index) => {
box(
scene,
index % 2 ? mats.leaves : mats.leavesLight,
[f[3] * scale, f[3] * 0.72 * scale, f[3] * 0.82 * scale],
[x + f[0] * scale, f[1] * scale, z + f[2] * scale]
);
});
}
voxelTree(-6.35, -5.6, 1.0);
voxelTree(6.6, 4.8, 0.9);
voxelTree(-6.8, 7.4, 0.72);
// ------------------------------------------------------------
// Small voxel beaver on the right bank
// ------------------------------------------------------------
const beaver = new THREE.Group();
beaver.position.set(5.25, 0, 2.65);
scene.add(beaver);
box(beaver, mats.beaver, [1.0, 0.72, 1.55], [0, 1.12, 0]);
box(beaver, mats.beaverDark, [0.84, 0.72, 0.8], [0, 1.42, 0.82]);
box(beaver, mats.beaverLight, [0.55, 0.34, 0.38], [0, 1.31, 1.31]);
box(beaver, mats.beaverDark, [1.18, 0.18, 1.2], [0, 0.79, -1.12]);
box(beaver, mats.beaver, [0.22, 0.48, 0.3], [-0.32, 0.94, 0.42]);
box(beaver, mats.beaver, [0.22, 0.48, 0.3], [0.32, 0.94, 0.42]);
box(beaver, mats.eye, [0.13, 0.13, 0.12], [-0.27, 1.57, 1.16]);
box(beaver, mats.eye, [0.13, 0.13, 0.12], [0.27, 1.57, 1.16]);
box(beaver, mats.tooth, [0.16, 0.2, 0.12], [0, 1.08, 1.51]);
// ------------------------------------------------------------
// Downstream arrows and animated flow blocks
// ------------------------------------------------------------
function addFlowArrow(x, z) {
box(
scene,
mats.foam,
[0.22, 0.08, 1.35],
[x, 0.49, z - 0.15],
{ cast: false }
);
box(
scene,
mats.foam,
[0.76, 0.08, 0.28],
[x, 0.49, z + 0.43],
{ cast: false }
);
box(
scene,
mats.foam,
[0.48, 0.08, 0.28],
[x, 0.49, z + 0.63],
{ cast: false }
);
box(
scene,
mats.foam,
[0.2, 0.08, 0.28],
[x, 0.49, z + 0.83],
{ cast: false }
);
}
addFlowArrow(-2.4, 4.4);
addFlowArrow(0, 6.6);
addFlowArrow(2.45, 8.2);
const flowParticles = [];
for (let i = 0; i < 24; i++) {
const particle = box(
scene,
i % 3 === 0 ? mats.foam : waveDownMaterial,
[0.14, 0.1, 0.26 + Math.random() * 0.16],
[
-3.7 + Math.random() * 7.4,
0.49,
0.85 + Math.random() * 8.7
],
{ cast: false }
);
flowParticles.push({
mesh: particle,
speed: 0.65 + Math.random() * 0.75,
baseX: particle.position.x,
phase: Math.random() * Math.PI * 2
});
}
// ------------------------------------------------------------
// Camera controls implemented directly in this HTML
// ------------------------------------------------------------
const overviewView = {
theta: 0.72,
phi: 1.08,
radius: 21.5,
target: new THREE.Vector3(0, 1.35, 0)
};
const cutawayView = {
theta: -Math.PI / 2,
phi: 1.27,
radius: 18.5,
target: new THREE.Vector3(0, 1.45, 0)
};
const spherical = new THREE.Spherical(
overviewView.radius,
overviewView.phi,
overviewView.theta
);
const cameraTarget = overviewView.target.clone();
let cameraTween = null;
function applyCamera() {
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
function transitionCamera(view) {
cameraTween = {
start: performance.now(),
duration: 900,
from: {
theta: spherical.theta,
phi: spherical.phi,
radius: spherical.radius,
target: cameraTarget.clone()
},
to: {
theta: view.theta,
phi: view.phi,
radius: view.radius,
target: view.target.clone()
}
};
}
function updateCameraTween(now) {
if (!cameraTween) return;
const t = Math.min(1, (now - cameraTween.start) / cameraTween.duration);
const eased = 1 - Math.pow(1 - t, 3);
spherical.theta = THREE.MathUtils.lerp(
cameraTween.from.theta,
cameraTween.to.theta,
eased
);
spherical.phi = THREE.MathUtils.lerp(
cameraTween.from.phi,
cameraTween.to.phi,
eased
);
spherical.radius = THREE.MathUtils.lerp(
cameraTween.from.radius,
cameraTween.to.radius,
eased
);
cameraTarget.lerpVectors(
cameraTween.from.target,
cameraTween.to.target,
eased
);
if (t >= 1) cameraTween = null;
}
applyCamera();
const canvas = renderer.domElement;
const pointers = new Map();
let lastPinchDistance = 0;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
if (pointers.size === 2) {
const points = [...pointers.values()];
lastPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: previous.button
});
cameraTween = null;
if (pointers.size >= 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (lastPinchDistance > 0) {
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * (lastPinchDistance / distance),
11,
34
);
}
lastPinchDistance = distance;
applyCamera();
return;
}
if (previous.button === 2 || event.shiftKey) {
const panScale = spherical.radius * 0.0011;
cameraTarget.x = THREE.MathUtils.clamp(
cameraTarget.x - dx * panScale,
-5,
5
);
cameraTarget.z = THREE.MathUtils.clamp(
cameraTarget.z + dy * panScale,
-7,
7
);
} else {
spherical.theta -= dx * 0.006;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - dy * 0.005,
0.42,
1.46
);
}
applyCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) lastPinchDistance = 0;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraTween = null;
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * Math.exp(event.deltaY * 0.001),
11,
34
);
applyCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
transitionCamera(overviewView);
setActiveButton("overview-button");
});
const overviewButton = document.getElementById("overview-button");
const cutawayButton = document.getElementById("cutaway-button");
const cutawayCard = document.getElementById("cutaway-card");
function setActiveButton(id) {
overviewButton.classList.toggle("active", id === "overview-button");
cutawayButton.classList.toggle("active", id === "cutaway-button");
}
overviewButton.addEventListener("click", () => {
transitionCamera(overviewView);
setActiveButton("overview-button");
if (window.innerWidth <= 800) cutawayCard.classList.add("hidden");
});
cutawayButton.addEventListener("click", () => {
transitionCamera(cutawayView);
setActiveButton("cutaway-button");
cutawayCard.classList.remove("hidden");
});
if (window.innerWidth <= 800) {
cutawayCard.classList.add("hidden");
}
// ------------------------------------------------------------
// Screen-space labels
// ------------------------------------------------------------
const labelDefinitions = {
dam: {
position: new THREE.Vector3(-0.5, 4.65, 0.15),
offset: [-25, -13]
},
upstream: {
position: new THREE.Vector3(-2.4, 0.95, -4.6),
offset: [-8, -7]
},
downstream: {
position: new THREE.Vector3(2.5, 0.58, 5.2),
offset: [12, -5]
},
support: {
position: new THREE.Vector3(4.35, 3.0, 0.05),
offset: [24, -5]
},
opening: {
position: new THREE.Vector3(0, 1.13, 0.7),
offset: [-5, 20]
}
};
const labelElements = {};
document.querySelectorAll("[data-label]").forEach(element => {
labelElements[element.dataset.label] = element;
});
const projected = new THREE.Vector3();
function updateLabels() {
Object.entries(labelDefinitions).forEach(([key, definition]) => {
const element = labelElements[key];
projected.copy(definition.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.12 &&
Math.abs(projected.y) < 1.12;
if (!visible) {
element.style.opacity = "0";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.opacity = "1";
element.style.transform = `
translate(
${x + definition.offset[0]}px,
${y + definition.offset[1]}px
)
translate(-50%, -100%)
`;
});
}
// ------------------------------------------------------------
// Resize and animation loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate(now) {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const delta = Math.min(clock.getDelta(), 0.05);
updateCameraTween(now);
waterTiles.forEach(tile => {
tile.mesh.position.y =
tile.baseY + Math.sin(elapsed * 1.7 + tile.phase) * tile.amount;
});
flowParticles.forEach(particle => {
particle.mesh.position.z += particle.speed * delta;
particle.mesh.position.x =
particle.baseX +
Math.sin(elapsed * 1.4 + particle.phase) * 0.14;
if (particle.mesh.position.z > 9.8) {
particle.mesh.position.z = 0.85;
particle.baseX = -3.65 + Math.random() * 7.3;
particle.mesh.position.x = particle.baseX;
}
});
applyCamera();
updateLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```