Prompt specification
Build or customize the Three.js 3D scene "Mill-channel splitter with unequal outlet levels" 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: Mill-channel splitter with unequal outlet levels
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/mill-channel-splitter-with-unequal-outlet-levels-x2377
Scene Description Prompt:
A voxel medieval mill channel splits into gated branches with different sill heights; compare flow against the shared waterline. Three.js from cdnjs, one HTML.
Working Three.js Starter (mill-channel-splitter-with-unequal-outlet-levels-x2377):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Voxel Millrace — Sill Flow Study</title>
<style>
:root {
--ink: #24332d;
--muted: #66746b;
--paper: rgba(246, 240, 220, 0.94);
--edge: rgba(55, 72, 61, 0.24);
--blue: #238ca3;
--gold: #d89835;
--green: #637a48;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9e4dc;
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
background: var(--paper);
border: 1px solid var(--edge);
box-shadow: 0 12px 35px rgba(35, 51, 43, 0.16);
backdrop-filter: blur(8px);
}
#title {
top: 20px;
left: 20px;
padding: 16px 19px 14px;
max-width: 390px;
}
#title .eyebrow {
color: var(--muted);
font: 700 10px/1.2 Arial, sans-serif;
letter-spacing: 0.19em;
text-transform: uppercase;
}
#title h1 {
margin: 7px 0 5px;
font-size: clamp(24px, 3vw, 36px);
line-height: 0.98;
font-weight: 700;
}
#title p {
margin: 0;
color: var(--muted);
font: 13px/1.45 Arial, sans-serif;
}
#stats {
top: 20px;
right: 20px;
width: 292px;
padding: 16px;
}
.stat-heading {
display: flex;
justify-content: space-between;
align-items: baseline;
padding-bottom: 10px;
border-bottom: 1px solid var(--edge);
}
.stat-heading strong {
font-size: 16px;
}
.stat-heading span {
color: var(--muted);
font: 10px Arial, sans-serif;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.waterline-row {
display: flex;
align-items: center;
gap: 9px;
margin: 12px 0 14px;
font: 12px Arial, sans-serif;
}
.waterline-swatch {
width: 28px;
height: 3px;
background: #e9c761;
box-shadow: 0 0 0 1px rgba(88, 70, 20, 0.12);
}
.flow-stat {
margin: 12px 0;
}
.flow-top {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 10px;
font: 12px Arial, sans-serif;
}
.flow-top strong {
font-size: 14px;
}
.flow-top span {
color: var(--muted);
}
.meter {
height: 9px;
margin-top: 6px;
overflow: hidden;
border-radius: 1px;
background: rgba(49, 69, 57, 0.12);
}
.meter > div {
width: 0;
height: 100%;
transition: width 180ms ease;
}
#lowerBar { background: var(--blue); }
#higherBar { background: var(--gold); }
#comparison {
margin-top: 14px;
padding-top: 11px;
border-top: 1px solid var(--edge);
color: var(--muted);
font: 11px/1.45 Arial, sans-serif;
}
#comparison b { color: var(--ink); }
#controls {
left: 50%;
bottom: 20px;
transform: translateX(-50%);
width: min(670px, calc(100% - 40px));
padding: 13px 15px;
display: grid;
grid-template-columns: 1fr 1fr auto;
gap: 13px 18px;
align-items: center;
}
.slider-row label {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
font: 700 11px Arial, sans-serif;
letter-spacing: 0.04em;
}
.slider-row output {
color: var(--muted);
font-weight: 500;
}
input[type="range"] {
width: 100%;
margin: 0;
accent-color: var(--green);
cursor: pointer;
}
.button-row {
display: flex;
gap: 6px;
grid-column: 3;
grid-row: 1 / span 2;
}
button {
border: 1px solid rgba(55, 72, 61, 0.32);
background: rgba(255, 255, 255, 0.35);
color: var(--ink);
padding: 8px 10px;
font: 700 10px Arial, sans-serif;
cursor: pointer;
transition: background 140ms ease, transform 140ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.8);
transform: translateY(-1px);
}
button:active { transform: translateY(0); }
#hint {
position: fixed;
z-index: 8;
left: 20px;
bottom: 20px;
padding: 8px 11px;
color: rgba(36, 51, 45, 0.78);
background: rgba(246, 240, 220, 0.78);
border: 1px solid rgba(55, 72, 61, 0.14);
font: 10px/1.35 Arial, sans-serif;
pointer-events: none;
}
.world-label {
position: fixed;
z-index: 6;
transform: translate(-50%, -100%);
padding: 6px 9px;
border: 1px solid rgba(47, 65, 55, 0.28);
background: rgba(250, 246, 229, 0.92);
box-shadow: 0 5px 14px rgba(35, 51, 43, 0.13);
pointer-events: none;
white-space: nowrap;
text-align: center;
}
.world-label::after {
content: "";
position: absolute;
width: 1px;
height: 18px;
left: 50%;
top: 100%;
background: rgba(47, 65, 55, 0.55);
}
.world-label small {
display: block;
color: var(--muted);
font: 9px/1.25 Arial, sans-serif;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#sharedLabel {
border-color: rgba(179, 139, 32, 0.45);
}
#lowerLabel { border-left: 4px solid var(--blue); }
#higherLabel { border-left: 4px solid var(--gold); }
#pauseButton {
position: fixed;
z-index: 12;
right: 20px;
bottom: 20px;
background: var(--paper);
}
@media (max-width: 820px) {
#title {
top: 12px;
left: 12px;
max-width: calc(100% - 24px);
padding: 12px 14px;
}
#title p { display: none; }
#stats {
top: 103px;
right: 12px;
width: 245px;
padding: 12px;
}
#controls {
bottom: 12px;
width: calc(100% - 24px);
grid-template-columns: 1fr 1fr;
gap: 10px 14px;
padding: 11px;
}
.button-row {
grid-column: 1 / -1;
grid-row: auto;
justify-content: flex-end;
}
#hint { display: none; }
#pauseButton { right: 12px; bottom: 112px; }
}
</style>
</head>
<body>
<div id="title" class="panel">
<div class="eyebrow">Hydraulic field study · c. 1380</div>
<h1>The Divided Millrace</h1>
<p>Two timber gates share one upstream waterline. Raise the downstream sills and watch the branch discharge separate.</p>
</div>
<div id="stats" class="panel">
<div class="stat-heading">
<strong>Flow comparison</strong>
<span>Shared head</span>
</div>
<div class="waterline-row">
<div class="waterline-swatch"></div>
<span>Shared waterline <b>1.70 m</b></span>
</div>
<div class="flow-stat">
<div class="flow-top">
<strong>Lower sill · 1.15 m</strong>
<span id="lowerValue">0 L/s</span>
</div>
<div class="meter"><div id="lowerBar"></div></div>
</div>
<div class="flow-stat">
<div class="flow-top">
<strong>Higher sill · 1.45 m</strong>
<span id="higherValue">0 L/s</span>
</div>
<div class="meter"><div id="higherBar"></div></div>
</div>
<div id="comparison"></div>
</div>
<div id="controls" class="panel">
<div class="slider-row">
<label for="lowerGate">LOWER-SILL GATE <output id="lowerOut">78%</output></label>
<input id="lowerGate" type="range" min="0" max="100" value="78">
</div>
<div class="slider-row">
<label for="higherGate">HIGHER-SILL GATE <output id="higherOut">78%</output></label>
<input id="higherGate" type="range" min="0" max="100" value="78">
</div>
<div class="button-row">
<button id="equalButton" type="button">EQUAL GATES</button>
<button id="openButton" type="button">OPEN ALL</button>
<button id="resetButton" type="button">RESET VIEW</button>
</div>
</div>
<div id="hint">Drag to orbit · Shift/right-drag to pan · Wheel to zoom · R resets</div>
<button id="pauseButton" type="button">PAUSE</button>
<div id="sharedLabel" class="world-label">SHARED WATERLINE<small>1.70 m</small></div>
<div id="lowerLabel" class="world-label">LOWER SILLOW<small>1.15 m · deeper head</small></div>
<div id="higherLabel" class="world-label">HIGHER SILLOW<small>1.45 m · reduced head</small></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* ------------------------------------------------------------------
Scene
------------------------------------------------------------------ */
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9e4dc);
scene.fog = new THREE.Fog(0xd9e4dc, 22, 39);
const camera = new THREE.PerspectiveCamera(
39,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xf4f3df, 0x56654c, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe3ad, 3.2);
sun.position.set(9, 15, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 38;
sun.shadow.bias = -0.0007;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9dc9d2, 0.65);
fill.position.set(-10, 8, -8);
scene.add(fill);
const WATER_Y = 1.70;
const LOWER_SILL = 1.15;
const HIGHER_SILL = 1.45;
const LOWER_TAIL = 1.04;
const HIGHER_TAIL = 1.18;
const materials = {
earth: new THREE.MeshStandardMaterial({ color: 0x5b5945, roughness: 1 }),
grass: new THREE.MeshStandardMaterial({ color: 0x73835a, roughness: 1 }),
grassDark: new THREE.MeshStandardMaterial({ color: 0x526642, roughness: 1 }),
stone: new THREE.MeshStandardMaterial({ color: 0x8b8b78, roughness: 0.95 }),
stoneDark: new THREE.MeshStandardMaterial({ color: 0x666b5d, roughness: 1 }),
stoneLight: new THREE.MeshStandardMaterial({ color: 0xa5a38e, roughness: 0.95 }),
wood: new THREE.MeshStandardMaterial({ color: 0x65452d, roughness: 0.94 }),
woodLight: new THREE.MeshStandardMaterial({ color: 0x8b6339, roughness: 0.92 }),
roof: new THREE.MeshStandardMaterial({ color: 0x414a45, roughness: 1 }),
roofEdge: new THREE.MeshStandardMaterial({ color: 0x303834, roughness: 1 }),
window: new THREE.MeshStandardMaterial({ color: 0x202a28, roughness: 0.9 }),
metal: new THREE.MeshStandardMaterial({
color: 0x454a43,
roughness: 0.55,
metalness: 0.55
}),
blueSill: new THREE.MeshStandardMaterial({ color: 0x2c9bb0, roughness: 0.8 }),
goldSill: new THREE.MeshStandardMaterial({ color: 0xd39b3b, roughness: 0.8 }),
water: new THREE.MeshStandardMaterial({
color: 0x43a9bd,
roughness: 0.22,
metalness: 0.03,
transparent: true,
opacity: 0.76,
depthWrite: false
}),
waterDark: new THREE.MeshStandardMaterial({
color: 0x287e94,
roughness: 0.3,
transparent: true,
opacity: 0.64,
depthWrite: false
}),
foam: new THREE.MeshStandardMaterial({
color: 0xd8f4ed,
roughness: 0.5,
transparent: true,
opacity: 0.92
})
};
function box(parent, w, h, d, x, y, z, material, cast = true, receive = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
/* Deterministic pseudo-random detail. */
let seed = 81723;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
/* ------------------------------------------------------------------
Diorama ground
------------------------------------------------------------------ */
const diorama = new THREE.Group();
scene.add(diorama);
box(diorama, 15.8, 0.55, 15.2, 0, -0.42, 0.2, materials.earth);
box(diorama, 15.45, 0.17, 14.85, 0, -0.055, 0.2, materials.grass);
for (let i = 0; i < 105; i++) {
const x = (random() - 0.5) * 14.5;
const z = (random() - 0.5) * 13.8;
// Keep large cobbles away from the main race.
if (Math.abs(x) < 1.8 && z > 1.7) continue;
const size = 0.18 + random() * 0.38;
const tileMat = random() > 0.55 ? materials.stoneDark : materials.stone;
box(
diorama,
size,
0.055 + random() * 0.055,
size * (0.65 + random() * 0.7),
x,
0.08,
z,
tileMat,
false,
true
);
}
for (let i = 0; i < 70; i++) {
const x = (random() - 0.5) * 14;
const z = (random() - 0.5) * 13.5;
if (Math.abs(x) < 1.8 && z > 1.7) continue;
const h = 0.12 + random() * 0.23;
box(diorama, 0.07, h, 0.07, x, 0.12 + h / 2, z,
random() > 0.5 ? materials.grassDark : materials.grass, false, false);
}
/* ------------------------------------------------------------------
Masonry helpers
------------------------------------------------------------------ */
function masonryWall(parent, x, startZ, length, side, axis = "z") {
const blockLength = 0.68;
const count = Math.ceil(length / blockLength);
for (let row = 0; row < 2; row++) {
for (let i = 0; i < count; i++) {
const along = -length / 2 + blockLength / 2 + i * blockLength;
const stagger = row % 2 === 0 ? 0 : blockLength * 0.28;
const z = startZ + along + stagger;
const mat = (i + row) % 4 === 0 ? materials.stoneLight : materials.stone;
if (axis === "z") {
box(parent, 0.32, 0.48, blockLength - 0.035,
x + side * 0.015, 0.82 + row * 0.49, z, mat);
} else {
box(parent, blockLength - 0.035, 0.48, 0.32,
x + along + stagger, 0.82 + row * 0.49, startZ + side * 0.015, mat);
}
}
}
}
/* ------------------------------------------------------------------
Shared upstream race
------------------------------------------------------------------ */
const mainRace = new THREE.Group();
diorama.add(mainRace);
box(mainRace, 2.45, 0.36, 6.25, 0, 0.36, 5.42, materials.stoneDark);
box(mainRace, 2.12, 0.16, 6.0, 0, 1.62, 5.42, materials.water, false, false);
masonryWall(mainRace, 0, 5.42, 6.25, -1, "z");
masonryWall(mainRace, 0, 5.42, 6.25, 1, "z");
for (const side of [-1, 1]) {
for (let i = 0; i < 9; i++) {
box(mainRace, 0.39, 0.12, 0.64,
side * 1.34, 1.68, 2.55 + i * 0.69,
i % 3 === 0 ? materials.stoneLight : materials.stone);
}
}
// Source portal.
box(mainRace, 0.48, 2.35, 0.52, -1.37, 1.55, 8.53, materials.stoneLight);
box(mainRace, 0.48, 2.35, 0.52, 1.37, 1.55, 8.53, materials.stoneLight);
box(mainRace, 3.2, 0.42, 0.55, 0, 2.55, 8.53, materials.stoneLight);
// Split island.
box(mainRace, 0.46, 0.78, 1.0, 0, 0.77, 2.33, materials.stoneLight);
box(mainRace, 0.74, 0.38, 0.55, 0, 0.52, 2.82, materials.stone);
// Shared waterline marker.
const lineMat = new THREE.LineBasicMaterial({ color: 0xffdf76 });
const sharedLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-1.04, WATER_Y + 0.025, 3.55),
new THREE.Vector3( 1.04, WATER_Y + 0.025, 3.55)
]),
lineMat
);
mainRace.add(sharedLine);
/* ------------------------------------------------------------------
Branch construction
------------------------------------------------------------------ */
function makeWheel(parent) {
const wheel = new THREE.Group();
wheel.position.set(0, 1.30, -8.25);
parent.add(wheel);
box(wheel, 2.45, 0.11, 0.13, 0, 0, 0, materials.woodLight);
box(wheel, 0.11, 2.45, 0.13, 0, 0, 0, materials.woodLight);
for (let i = 0; i < 8; i++) {
const spoke = box(wheel, 2.42, 0.10, 0.12, 0, 0, 0, materials.wood);
spoke.rotation.z = i * Math.PI / 4;
}
for (let i = 0; i < 12; i++) {
const a = i * Math.PI / 6;
const paddle = box(
wheel,
0.24,
0.48,
0.42,
Math.cos(a) * 1.18,
Math.sin(a) * 1.18,
0,
i % 2 ? materials.woodLight : materials.wood
);
paddle.rotation.z = a - Math.PI / 2;
}
box(wheel, 0.42, 0.42, 0.58, 0, 0, 0, materials.metal);
// Axle and timber supports.
box(parent, 0.18, 0.18, 1.1, 0, 1.3, -8.25, materials.metal);
box(parent, 0.24, 2.35, 0.24, -1.18, 1.0, -8.25, materials.wood);
box(parent, 0.24, 2.35, 0.24, 1.18, 1.0, -8.25, materials.wood);
return wheel;
}
function createBranch(angle, sill, tailLevel, sillMaterial, key) {
const group = new THREE.Group();
group.position.set(0, 0, 2.48);
group.rotation.y = angle;
diorama.add(group);
const length = 9.0;
box(group, 2.05, 0.36, length, 0, 0.36, -3.75, materials.stoneDark);
masonryWall(group, 0, -3.75, length, -1, "z");
masonryWall(group, 0, -3.75, length, 1, "z");
for (let i = 0; i < 13; i++) {
for (const side of [-1, 1]) {
box(group, 0.39, 0.12, 0.64,
side * 1.02, 1.68, 0.58 - i * 0.69,
i % 3 === 0 ? materials.stoneLight : materials.stone);
}
}
// Upstream pool remains at the shared waterline.
box(group, 1.73, 0.16, 1.95, 0, 1.62, -0.20, materials.water, false, false);
// Lower tailwater after the sill.
box(
group,
1.73,
0.14,
6.3,
0,
tailLevel - 0.07,
-4.78,
materials.waterDark,
false,
false
);
// Raised sill.
const sillHeight = sill - 0.54;
box(
group,
1.94,
sillHeight,
0.48,
0,
0.54 + sillHeight / 2,
-1.43,
materials.stoneDark
);
box(group, 2.0, 0.075, 0.53, 0, sill - 0.038, -1.43, sillMaterial);
// Timber gate frame.
box(group, 0.18, 2.2, 0.22, -0.93, 1.35, 0.27, materials.wood);
box(group, 0.18, 2.2, 0.22, 0.93, 1.35, 0.27, materials.wood);
box(group, 2.12, 0.20, 0.26, 0, 2.42, 0.27, materials.woodLight);
// Raised sluice boards.
const gatePanel = new THREE.Group();
gatePanel.position.set(0, 0.60, 0.27);
group.add(gatePanel);
for (let i = 0; i < 5; i++) {
box(gatePanel, 1.60, 0.145, 0.13,
0, 0.075 + i * 0.17, 0, materials.woodLight);
}
box(gatePanel, 0.13, 0.82, 0.18, 0, 0.41, 0, materials.wood);
box(group, 0.13, 1.6, 0.15, 0, 1.88, 0.27, materials.metal);
box(group, 0.75, 0.11, 0.11, 0, 2.66, 0.27, materials.metal);
// Branch reference line.
const reference = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.84, WATER_Y + 0.025, -0.73),
new THREE.Vector3( 0.84, WATER_Y + 0.025, -0.73)
]),
lineMat
);
group.add(reference);
const particles = [];
const particleGeo = new THREE.BoxGeometry(0.12, 0.075, 0.18);
for (let i = 0; i < 64; i++) {
const mesh = new THREE.Mesh(
particleGeo,
i % 4 === 0 ? materials.foam : materials.waterDark
);
mesh.castShadow = false;
group.add(mesh);
particles.push({
mesh,
phase: random(),
x: (random() - 0.5) * 1.35,
wobble: random() * Math.PI * 2
});
}
const foam = [];
const foamGeo = new THREE.BoxGeometry(0.10, 0.10, 0.10);
for (let i = 0; i < 18; i++) {
const mesh = new THREE.Mesh(foamGeo, materials.foam);
mesh.castShadow = false;
group.add(mesh);
foam.push({
mesh,
phase: random(),
x: (random() - 0.5) * 1.2
});
}
const wheel = makeWheel(group);
return {
key,
group,
sill,
tailLevel,
gatePanel,
particles,
foam,
wheel,
q: 0,
opening: 0
};
}
const branches = [
createBranch( 0.42, LOWER_SILL, LOWER_TAIL, materials.blueSill, "lower"),
createBranch(-0.42, HIGHER_SILL, HIGHER_TAIL, materials.goldSill, "higher")
];
/* ------------------------------------------------------------------
Central medieval mill
------------------------------------------------------------------ */
const mill = new THREE.Group();
mill.position.set(0, 0, -7.0);
diorama.add(mill);
box(mill, 4.8, 2.65, 3.45, 0, 1.58, 0, materials.stone);
box(mill, 5.0, 0.18, 3.65, 0, 0.31, 0, materials.stoneDark);
// Stepped voxel gable roof.
for (let i = 0; i < 6; i++) {
box(
mill,
5.35 - i * 0.59,
0.28,
4.05,
0,
2.98 + i * 0.26,
-0.08,
i === 0 ? materials.roofEdge : materials.roof
);
}
// Front facade.
box(mill, 0.90, 1.75, 0.10, 0, 1.18, 1.77, materials.wood);
box(mill, 0.10, 1.75, 0.13, 0, 1.18, 1.84, materials.woodLight);
for (const x of [-1.45, 1.45]) {
box(mill, 0.63, 0.78, 0.12, x, 1.90, 1.78, materials.window);
box(mill, 0.09, 0.82, 0.15, x, 1.90, 1.84, materials.wood);
box(mill, 0.67, 0.09, 0.15, x, 1.90, 1.84, materials.wood);
}
box(mill, 0.42, 1.8, 0.48, 1.65, 3.66, -0.8, materials.stoneDark);
// Timber framing.
for (const x of [-2.25, 2.25]) {
box(mill, 0.13, 2.4, 0.14, x, 1.50, 1.79, materials.wood);
}
box(mill, 4.55, 0.12, 0.14, 0, 2.74, 1.79, materials.wood);
// Small sacks and barrels.
for (let i = 0; i < 5; i++) {
const sack = box(
mill,
0.38,
0.50 + random() * 0.16,
0.36,
2.75 + random() * 0.45,
0.60,
-2.6 + random() * 1.0,
i % 2 ? materials.woodLight : materials.stoneLight
);
sack.rotation.y = random() * 0.5;
}
/* ------------------------------------------------------------------
Hydraulic model and interface
------------------------------------------------------------------ */
const lowerGateInput = document.getElementById("lowerGate");
const higherGateInput = document.getElementById("higherGate");
const lowerOut = document.getElementById("lowerOut");
const higherOut = document.getElementById("higherOut");
const lowerValue = document.getElementById("lowerValue");
const higherValue = document.getElementById("higherValue");
const lowerBar = document.getElementById("lowerBar");
const higherBar = document.getElementById("higherBar");
const comparison = document.getElementById("comparison");
let paused = false;
let simulationTime = 0;
function branchDischarge(opening, sill) {
const head = Math.max(0, WATER_Y - sill);
return 70 * opening * Math.sqrt(head / (WATER_Y - LOWER_SILL));
}
function updateHydraulics() {
const lowerOpening = Number(lowerGateInput.value) / 100;
const higherOpening = Number(higherGateInput.value) / 100;
lowerOut.textContent = `${lowerGateInput.value}%`;
higherOut.textContent = `${higherGateInput.value}%`;
const lowerQ = branchDischarge(lowerOpening, LOWER_SILL);
const higherQ = branchDischarge(higherOpening, HIGHER_SILL);
branches[0].q = lowerQ;
branches[0].opening = lowerOpening;
branches[1].q = higherQ;
branches[1].opening = higherOpening;
// Lift the timber gate panel.
branches[0].gatePanel.position.y = 0.58 + lowerOpening * 1.31;
branches[1].gatePanel.position.y = 0.58 + higherOpening * 1.31;
lowerValue.textContent = `${Math.round(lowerQ)} L/s`;
higherValue.textContent = `${Math.round(higherQ)} L/s`;
lowerBar.style.width = `${Math.min(100, lowerQ / 70 * 100)}%`;
higherBar.style.width = `${Math.min(100, higherQ / 70 * 100)}%`;
if (lowerQ + higherQ < 0.5) {
comparison.innerHTML = "Both branches are closed. No water reaches the mill wheels.";
} else {
const ratio = higherQ > 0.1 ? lowerQ / higherQ : Infinity;
const difference = Math.round(lowerQ - higherQ);
comparison.innerHTML = ratio === Infinity
? "The higher-sill branch is closed while the lower branch continues to discharge."
: `At the current gates, the lower sill passes <b>${ratio.toFixed(2)}×</b> the higher-sill flow — a difference of <b>${difference} L/s</b>.`;
}
}
lowerGateInput.addEventListener("input", updateHydraulics);
higherGateInput.addEventListener("input", updateHydraulics);
document.getElementById("equalButton").addEventListener("click", () => {
lowerGateInput.value = 78;
higherGateInput.value = 78;
updateHydraulics();
});
document.getElementById("openButton").addEventListener("click", () => {
lowerGateInput.value = 100;
higherGateInput.value = 100;
updateHydraulics();
});
const pauseButton = document.getElementById("pauseButton");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "RESUME" : "PAUSE";
});
updateHydraulics();
/* ------------------------------------------------------------------
Direct camera controls
------------------------------------------------------------------ */
const controls = {
target: new THREE.Vector3(0, 1.15, -0.3),
radius: 20.0,
theta: 0.67,
phi: 0.79,
dragging: false,
panning: false,
lastX: 0,
lastY: 0
};
function applyCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
function resetCamera() {
controls.target.set(0, 1.15, -0.3);
controls.radius = 20;
controls.theta = 0.67;
controls.phi = 0.79;
applyCamera();
}
resetCamera();
renderer.domElement.addEventListener("contextmenu", e => e.preventDefault());
renderer.domElement.addEventListener("pointerdown", e => {
controls.dragging = true;
controls.panning = e.shiftKey || e.button === 2;
controls.lastX = e.clientX;
controls.lastY = e.clientY;
renderer.domElement.setPointerCapture(e.pointerId);
});
renderer.domElement.addEventListener("pointermove", e => {
if (!controls.dragging) return;
const dx = e.clientX - controls.lastX;
const dy = e.clientY - controls.lastY;
controls.lastX = e.clientX;
controls.lastY = e.clientY;
if (controls.panning) {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const scale = controls.radius * 0.0015;
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(up, dy * scale);
} else {
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.005,
0.28,
1.35
);
}
applyCamera();
});
renderer.domElement.addEventListener("pointerup", e => {
controls.dragging = false;
if (renderer.domElement.hasPointerCapture(e.pointerId)) {
renderer.domElement.releasePointerCapture(e.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
controls.dragging = false;
});
renderer.domElement.addEventListener("wheel", e => {
e.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(e.deltaY * 0.001),
10,
34
);
applyCamera();
}, { passive: false });
document.getElementById("resetButton").addEventListener("click", resetCamera);
addEventListener("keydown", e => {
if (e.key.toLowerCase() === "r") resetCamera();
});
/* ------------------------------------------------------------------
World labels
------------------------------------------------------------------ */
const sharedLabel = document.getElementById("sharedLabel");
const lowerLabel = document.getElementById("lowerLabel");
const higherLabel = document.getElementById("higherLabel");
const sharedAnchor = new THREE.Vector3(0, 2.12, 4.20);
const lowerAnchor = branches[0].group.localToWorld(
new THREE.Vector3(-0.25, LOWER_SILL + 0.47, -1.43)
);
const higherAnchor = branches[1].group.localToWorld(
new THREE.Vector3(0.25, HIGHER_SILL + 0.47, -1.43)
);
function placeLabel(element, worldPosition) {
const p = worldPosition.clone().project(camera);
const visible = p.z > -1 && p.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (p.x * 0.5 + 0.5) * innerWidth;
const y = (-p.y * 0.5 + 0.5) * innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
/* ------------------------------------------------------------------
Animation
------------------------------------------------------------------ */
const clock = new THREE.Clock();
function updateBranch(branch, dt) {
const normalized = THREE.MathUtils.clamp(branch.q / 70, 0, 1);
const activeCount = branch.q < 0.5
? 0
: Math.round(7 + normalized * 57);
const speed = 0.055 + normalized * 0.075;
for (let i = 0; i < branch.particles.length; i++) {
const p = branch.particles[i];
p.mesh.visible = i < activeCount;
if (!p.mesh.visible) continue;
const u = (simulationTime * speed + p.phase) % 1;
const z = 0.67 - u * 9.15;
let y;
if (z > -1.05) {
y = WATER_Y + 0.015;
} else if (z > -1.82) {
const t = THREE.MathUtils.clamp((-1.05 - z) / 0.77, 0, 1);
y = THREE.MathUtils.lerp(WATER_Y, branch.tailLevel, t) + 0.025;
} else {
y = branch.tailLevel + 0.025;
}
p.mesh.position.set(
p.x + Math.sin(simulationTime * 2.1 + p.wobble) * 0.035,
y + Math.sin(simulationTime * 3.4 + p.wobble) * 0.018,
z
);
}
const foamCount = branch.q < 0.5
? 0
: Math.round(2 + normalized * 16);
for (let i = 0; i < branch.foam.length; i++) {
const f = branch.foam[i];
f.mesh.visible = i < foamCount;
if (!f.mesh.visible) continue;
const t = (simulationTime * 1.15 + f.phase) % 1;
f.mesh.position.set(
f.x,
branch.sill + 0.07 + Math.sin(t * Math.PI) * (0.15 + normalized * 0.22),
-1.64 - t * 0.55
);
f.mesh.rotation.set(t * 2, t * 3, t * 4);
f.mesh.scale.setScalar(0.55 + normalized * 0.65);
}
if (branch.q > 0.5) {
branch.wheel.rotation.z -= dt * (0.35 + normalized * 1.55);
}
}
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationTime += dt;
updateBranch(branches[0], dt);
updateBranch(branches[1], dt);
}
scene.updateMatrixWorld();
placeLabel(sharedLabel, sharedAnchor);
placeLabel(lowerLabel, lowerAnchor);
placeLabel(higherLabel, higherAnchor);
renderer.render(scene, camera);
}
animate();
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
</script>
</body>
</html>
```