Prompt specification
Build or customize the Three.js 3D scene "Garden water-transfer teaching bench" 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: Garden water-transfer teaching bench
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/garden-water-transfer-teaching-bench-x1499
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Compose a raised barrel, lower collection tank, and transparent siphon route; animate a prescribed flow while clearly showing both free-surface elevations.
Working Three.js Starter (garden-water-transfer-teaching-bench-x1499):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Siphon Works</title>
<style>
:root {
--ink: #173238;
--muted: #5d7376;
--cyan: #19c8d4;
--panel: rgba(250, 252, 248, 0.88);
--line: rgba(23, 50, 56, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e7ede8;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
canvas {
width: 100%;
height: 100%;
display: block;
}
canvas {
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: absolute;
z-index: 10;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 12px 40px rgba(35, 59, 58, 0.11);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#intro {
top: 22px;
left: 22px;
width: 330px;
padding: 20px 21px 18px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
color: #098d98;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 7px;
font-size: clamp(25px, 3vw, 36px);
line-height: 1;
letter-spacing: -0.045em;
}
#intro p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#readout {
top: 22px;
right: 22px;
width: 270px;
padding: 17px;
border-radius: 18px;
user-select: none;
}
.readout-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.readout-title {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#status {
padding: 5px 8px;
border-radius: 999px;
color: #087d86;
background: rgba(25, 200, 212, 0.12);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#status[data-state="paused"] {
color: #8b6812;
background: rgba(232, 180, 48, 0.16);
}
#status[data-state="stopped"] {
color: #a1463d;
background: rgba(200, 77, 63, 0.14);
}
.flow-rate {
display: flex;
align-items: baseline;
justify-content: space-between;
padding: 11px 12px;
margin-bottom: 8px;
border-radius: 12px;
color: white;
background: #183b40;
}
.flow-rate span:first-child {
color: #b8d5d6;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.flow-rate strong {
font-size: 20px;
letter-spacing: -0.04em;
}
.flow-rate small {
color: #9dc1c3;
font-size: 10px;
}
.stat {
display: flex;
justify-content: space-between;
gap: 12px;
padding: 8px 2px;
border-bottom: 1px solid rgba(23, 50, 56, 0.08);
font-size: 11px;
}
.stat span {
color: var(--muted);
}
.stat strong {
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
button {
appearance: none;
border: 1px solid rgba(23, 50, 56, 0.16);
border-radius: 10px;
padding: 9px 8px;
color: var(--ink);
background: rgba(255, 255, 255, 0.75);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease;
}
button:hover {
background: white;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#labels {
position: absolute;
inset: 0;
z-index: 8;
overflow: hidden;
pointer-events: none;
}
.surface-label {
position: absolute;
min-width: 160px;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.5);
border-left: 4px solid var(--cyan);
border-radius: 10px;
color: white;
background: rgba(20, 47, 52, 0.9);
box-shadow: 0 8px 24px rgba(20, 47, 52, 0.2);
transform: translate(-50%, calc(-100% - 12px));
white-space: nowrap;
}
.surface-label::after {
content: "";
position: absolute;
left: 18px;
top: 100%;
width: 1px;
height: 12px;
background: rgba(25, 200, 212, 0.9);
}
.surface-label small {
display: block;
margin-bottom: 2px;
color: #a8d9dc;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.surface-label strong {
font-size: 15px;
font-variant-numeric: tabular-nums;
letter-spacing: -0.02em;
}
.surface-label span {
margin-left: 4px;
color: #a8d9dc;
font-size: 9px;
}
#help {
position: absolute;
z-index: 9;
left: 22px;
bottom: 20px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
color: #587074;
background: rgba(250, 252, 248, 0.82);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.03em;
pointer-events: none;
}
#legend {
position: absolute;
right: 22px;
bottom: 20px;
z-index: 9;
display: flex;
gap: 15px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
color: #587074;
background: rgba(250, 252, 248, 0.82);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.04em;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
}
.swatch.fluid {
background: #16c6d1;
box-shadow: 0 0 7px rgba(22, 198, 209, 0.8);
}
.swatch.glass {
border: 2px solid rgba(35, 152, 165, 0.55);
background: rgba(174, 235, 239, 0.3);
}
@media (max-width: 720px) {
#intro {
top: 12px;
left: 12px;
width: calc(100% - 24px);
padding: 14px 16px;
}
#intro p {
display: none;
}
#readout {
top: auto;
right: 12px;
bottom: 12px;
width: 245px;
padding: 13px;
}
.stat {
padding: 5px 2px;
}
.flow-rate {
padding: 8px 10px;
}
#help,
#legend {
display: none;
}
.surface-label {
min-width: 132px;
padding: 7px 8px;
}
.surface-label strong {
font-size: 12px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<section id="intro" class="panel">
<div class="eyebrow">Hydraulic study 01</div>
<h1>Voxel Siphon Works</h1>
<p>
A primed inverted-U line moves water from a raised voxel barrel into a
lower collection tank. Both free surfaces remain explicitly measured.
</p>
</section>
<aside id="readout" class="panel">
<div class="readout-head">
<div class="readout-title">Live readout</div>
<div id="status" data-state="flowing">Flowing</div>
</div>
<div class="flow-rate">
<span>Prescribed Q</span>
<div>
<strong>0.45</strong>
<small>m³/s</small>
</div>
</div>
<div class="stat">
<span>Upper free surface</span>
<strong id="stat-upper">—</strong>
</div>
<div class="stat">
<span>Lower free surface</span>
<strong id="stat-lower">—</strong>
</div>
<div class="stat">
<span>Elevation difference</span>
<strong id="stat-head">—</strong>
</div>
<div class="stat">
<span>Transferred volume</span>
<strong id="stat-volume">0.00 m³</strong>
</div>
<div class="buttons">
<button id="pause" type="button">Pause</button>
<button id="reset" type="button">Restart</button>
</div>
</aside>
<div id="labels">
<div id="upper-label" class="surface-label">
<small>Upper free surface</small>
<strong id="upper-label-value">—</strong><span>above datum</span>
</div>
<div id="lower-label" class="surface-label">
<small>Lower free surface</small>
<strong id="lower-label-value">—</strong><span>above datum</span>
</div>
</div>
<div id="help">Drag to orbit · Shift-drag to pan · Scroll to zoom · Space to pause</div>
<div id="legend">
<span class="legend-item">
<span class="swatch fluid"></span>
Moving fluid packets
</span>
<span class="legend-item">
<span class="swatch glass"></span>
Transparent siphon
</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe7ede8);
scene.fog = new THREE.Fog(0xe7ede8, 24, 48);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemi = new THREE.HemisphereLight(0xf7fbff, 0x83918b, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4d7, 3.0);
sun.position.set(8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8eddea, 0.8);
fill.position.set(-9, 7, -8);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared helpers
// -------------------------------------------------------------------------
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
function makeBox(
width,
height,
depth,
material,
x,
y,
z,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeInstancedBoxes(entries, material, colored = false) {
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
material,
entries.length
);
entries.forEach((entry, index) => {
dummy.position.set(...entry.position);
dummy.scale.set(...entry.scale);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (colored && entry.color) {
tempColor.set(entry.color);
mesh.setColorAt(index, tempColor);
}
});
if (colored && mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel ground
// -------------------------------------------------------------------------
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
vertexColors: true
});
const groundEntries = [];
const groundColumns = 26;
const groundRows = 20;
for (let x = 0; x < groundColumns; x++) {
for (let z = 0; z < groundRows; z++) {
const alternating = (x + z) % 2;
const color = new THREE.Color()
.setHSL(0.28, 0.10, alternating ? 0.79 : 0.83)
.getStyle();
groundEntries.push({
position: [
(x - (groundColumns - 1) / 2) * 1.0,
-0.09,
(z - (groundRows - 1) / 2) * 1.0
],
scale: [0.975, 0.14, 0.975],
color
});
}
}
makeInstancedBoxes(groundEntries, groundMaterial, true);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const stoneMaterial = new THREE.MeshStandardMaterial({
color: 0x82908b,
roughness: 0.88,
vertexColors: true
});
const barrelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.7,
vertexColors: true
});
const barrelBandMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.62,
vertexColors: true
});
const barrelCutawayMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb88358,
transparent: true,
opacity: 0.26,
roughness: 0.25,
depthWrite: false,
side: THREE.DoubleSide
});
const tankMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.82,
vertexColors: true
});
const tankGlassMaterial = new THREE.MeshPhysicalMaterial({
color: 0x8fbfc0,
transparent: true,
opacity: 0.29,
roughness: 0.3,
depthWrite: false,
side: THREE.DoubleSide
});
const waterMaterial = new THREE.MeshPhysicalMaterial({
color: 0x24b9cc,
transparent: true,
opacity: 0.48,
roughness: 0.16,
metalness: 0.02,
depthWrite: false,
side: THREE.DoubleSide
});
const upperSurfaceMaterial = new THREE.MeshBasicMaterial({
color: 0x37e2e8,
transparent: true,
opacity: 0.68,
depthWrite: false,
side: THREE.DoubleSide
});
const lowerSurfaceMaterial = new THREE.MeshBasicMaterial({
color: 0x37e2e8,
transparent: true,
opacity: 0.72,
depthWrite: false,
side: THREE.DoubleSide
});
const rimMaterial = new THREE.MeshBasicMaterial({
color: 0xb8ffff,
transparent: true,
opacity: 0.9
});
// -------------------------------------------------------------------------
// Raised barrel support
// -------------------------------------------------------------------------
const BARREL_X = -3.1;
const BARREL_BOTTOM = 4.12;
const supportEntries = [];
for (const dx of [-1.18, 1.18]) {
for (const dz of [-1.18, 1.18]) {
for (let row = 0; row < 8; row++) {
const shade = 0.49 + ((row + Math.round(dx)) % 3) * 0.025;
supportEntries.push({
position: [
BARREL_X + dx,
0.27 + row * 0.5,
dz
],
scale: [0.52, 0.48, 0.52],
color: new THREE.Color().setHSL(0.39, 0.08, shade).getStyle()
});
}
}
}
makeInstancedBoxes(supportEntries, stoneMaterial, true);
makeBox(
4.15,
0.22,
4.15,
stoneMaterial,
BARREL_X,
4.0,
0
);
makeBox(
3.2,
0.22,
0.38,
stoneMaterial,
BARREL_X,
2.15,
-1.18
);
makeBox(
3.2,
0.22,
0.38,
stoneMaterial,
BARREL_X,
2.15,
1.18
);
// -------------------------------------------------------------------------
// Voxel barrel shell with a transparent front cutaway
// -------------------------------------------------------------------------
const barrelBlocks = [];
const barrelBands = [];
const barrelCutaway = [];
const barrelCell = 0.42;
for (let layer = 0; layer < 8; layer++) {
for (let ix = -5; ix <= 5; ix++) {
for (let iz = -5; iz <= 5; iz++) {
const px = ix * barrelCell;
const pz = iz * barrelCell;
const radius = Math.hypot(px, pz);
if (radius < 1.55 || radius > 2.12) continue;
const entry = {
position: [
BARREL_X + px,
BARREL_BOTTOM + layer * barrelCell + barrelCell / 2,
pz
],
scale: [barrelCell * 0.95, barrelCell * 0.95, barrelCell * 0.95]
};
// Front-facing quadrant is transparent so the water level stays visible.
if (px > 0.15 && pz > 0.15) {
barrelCutaway.push(entry);
} else {
const lightness = 0.34 + ((ix + iz + layer + 20) % 5) * 0.018;
entry.color = new THREE.Color()
.setHSL(0.075, 0.47, lightness)
.getStyle();
if (layer === 1 || layer === 6) {
barrelBands.push(entry);
} else {
barrelBlocks.push(entry);
}
}
}
}
}
makeInstancedBoxes(barrelBlocks, barrelMaterial, true);
makeInstancedBoxes(barrelBands, barrelBandMaterial, true);
makeInstancedBoxes(barrelCutaway, barrelCutawayMaterial, false);
const barrelFloor = new THREE.Mesh(
new THREE.CylinderGeometry(1.6, 1.6, 0.18, 24),
new THREE.MeshStandardMaterial({
color: 0x5d4636,
roughness: 0.85
})
);
barrelFloor.position.set(BARREL_X, BARREL_BOTTOM + 0.09, 0);
barrelFloor.receiveShadow = true;
scene.add(barrelFloor);
// -------------------------------------------------------------------------
// Lower collection tank
// -------------------------------------------------------------------------
const TANK_X = 3.3;
const TANK_BOTTOM = 0.32;
const TANK_CELL = 0.46;
const TANK_NX = 12;
const TANK_NZ = 7;
const TANK_LAYERS = 5;
const INNER_W = (TANK_NX - 2) * TANK_CELL;
const INNER_D = (TANK_NZ - 2) * TANK_CELL;
const TANK_TOP = 0.25 + TANK_LAYERS * TANK_CELL;
const tankBlocks = [];
const tankGlass = [];
for (let layer = 0; layer < TANK_LAYERS; layer++) {
for (let ix = 0; ix < TANK_NX; ix++) {
for (let iz = 0; iz < TANK_NZ; iz++) {
const border =
ix === 0 ||
ix === TANK_NX - 1 ||
iz === 0 ||
iz === TANK_NZ - 1;
if (!border) continue;
const px = (ix - (TANK_NX - 1) / 2) * TANK_CELL;
const pz = (iz - (TANK_NZ - 1) / 2) * TANK_CELL;
const entry = {
position: [
TANK_X + px,
0.25 + layer * TANK_CELL + TANK_CELL / 2,
pz
],
scale: [
TANK_CELL * 0.96,
TANK_CELL * 0.96,
TANK_CELL * 0.96
]
};
// Front and right walls are cut away visually.
if (iz === TANK_NZ - 1 || ix === TANK_NX - 1) {
tankGlass.push(entry);
} else {
const lightness = 0.43 + ((ix + iz + layer) % 4) * 0.025;
entry.color = new THREE.Color()
.setHSL(0.48, 0.08, lightness)
.getStyle();
tankBlocks.push(entry);
}
}
}
}
makeInstancedBoxes(tankBlocks, tankMaterial, true);
makeInstancedBoxes(tankGlass, tankGlassMaterial, false);
const tankSlab = makeBox(
TANK_NX * TANK_CELL,
0.22,
TANK_NZ * TANK_CELL,
new THREE.MeshStandardMaterial({
color: 0x64736f,
roughness: 0.9
}),
TANK_X,
0.19,
0
);
tankSlab.receiveShadow = true;
// -------------------------------------------------------------------------
// Water bodies and explicit free-surface planes
// -------------------------------------------------------------------------
const UPPER_RADIUS = 1.48;
const UPPER_AREA = Math.PI * UPPER_RADIUS * UPPER_RADIUS;
const LOWER_AREA = INNER_W * INNER_D;
const upperWater = new THREE.Mesh(
new THREE.CylinderGeometry(UPPER_RADIUS, UPPER_RADIUS, 1, 32),
waterMaterial
);
upperWater.renderOrder = 1;
scene.add(upperWater);
const upperSurface = new THREE.Mesh(
new THREE.CircleGeometry(UPPER_RADIUS, 32),
upperSurfaceMaterial
);
upperSurface.rotation.x = -Math.PI / 2;
upperSurface.renderOrder = 3;
scene.add(upperSurface);
const upperRim = new THREE.Mesh(
new THREE.TorusGeometry(UPPER_RADIUS, 0.025, 6, 48),
rimMaterial
);
upperRim.rotation.x = Math.PI / 2;
upperRim.renderOrder = 4;
scene.add(upperRim);
const lowerWater = new THREE.Mesh(
new THREE.BoxGeometry(INNER_W, 1, INNER_D),
waterMaterial
);
lowerWater.renderOrder = 1;
scene.add(lowerWater);
const lowerSurface = new THREE.Mesh(
new THREE.PlaneGeometry(INNER_W, INNER_D),
lowerSurfaceMaterial
);
lowerSurface.rotation.x = -Math.PI / 2;
lowerSurface.renderOrder = 3;
scene.add(lowerSurface);
// -------------------------------------------------------------------------
// Transparent siphon route
// -------------------------------------------------------------------------
const INLET_DEPTH = 0.62;
const INLET_Y = BARREL_BOTTOM + 0.06 + INLET_DEPTH;
const OUTLET_Y = 0.64;
const pipeCurve = new THREE.CurvePath();
pipeCurve.add(
new THREE.LineCurve3(
new THREE.Vector3(BARREL_X, INLET_Y, 0),
new THREE.Vector3(BARREL_X, 7.75, 0)
)
);
pipeCurve.add(
new THREE.QuadraticBezierCurve3(
new THREE.Vector3(BARREL_X, 7.75, 0),
new THREE.Vector3(BARREL_X, 8.25, 0),
new THREE.Vector3(BARREL_X + 0.4, 8.25, 0)
)
);
pipeCurve.add(
new THREE.LineCurve3(
new THREE.Vector3(BARREL_X + 0.4, 8.25, 0),
new THREE.Vector3(TANK_X - 0.45, 8.25, 0)
)
);
pipeCurve.add(
new THREE.QuadraticBezierCurve3(
new THREE.Vector3(TANK_X - 0.45, 8.25, 0),
new THREE.Vector3(TANK_X + 0.05, 8.25, 0),
new THREE.Vector3(TANK_X + 0.05, 7.85, 0)
)
);
pipeCurve.add(
new THREE.LineCurve3(
new THREE.Vector3(TANK_X + 0.05, 7.85, 0),
new THREE.Vector3(TANK_X + 0.05, OUTLET_Y, 0)
)
);
const pipeMaterial = new THREE.MeshPhysicalMaterial({
color: 0x9cecf2,
transparent: true,
opacity: 0.24,
roughness: 0.16,
metalness: 0,
depthWrite: false,
side: THREE.DoubleSide
});
const pipe = new THREE.Mesh(
new THREE.TubeGeometry(pipeCurve, 160, 0.145, 8, false),
pipeMaterial
);
pipe.renderOrder = 2;
scene.add(pipe);
const pipeCoreMaterial = new THREE.MeshBasicMaterial({
color: 0x27cbd4,
transparent: true,
opacity: 0.28,
depthWrite: false
});
const pipeCore = new THREE.Mesh(
new THREE.TubeGeometry(pipeCurve, 160, 0.055, 6, false),
pipeCoreMaterial
);
pipeCore.renderOrder = 3;
scene.add(pipeCore);
// Coupling blocks emphasize the voxel construction language.
const couplingMaterial = new THREE.MeshPhysicalMaterial({
color: 0xc9f6f7,
transparent: true,
opacity: 0.52,
roughness: 0.2,
depthWrite: false
});
makeBox(0.36, 0.36, 0.36, couplingMaterial, BARREL_X, INLET_Y, 0);
makeBox(
0.36,
0.36,
0.36,
couplingMaterial,
TANK_X + 0.05,
OUTLET_Y,
0
);
// -------------------------------------------------------------------------
// Moving voxel fluid packets
// -------------------------------------------------------------------------
const PARTICLE_COUNT = 38;
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0x16d3dc,
emissive: 0x087e87,
emissiveIntensity: 0.85,
roughness: 0.28,
metalness: 0.04
});
const particles = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.19, 0.19, 0.19),
particleMaterial,
PARTICLE_COUNT
);
particles.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particles.frustumCulled = false;
particles.renderOrder = 5;
scene.add(particles);
const particlePosition = new THREE.Vector3();
// -------------------------------------------------------------------------
// Elevation gauges
// -------------------------------------------------------------------------
function createGauge(x, z, maxHeight, color) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const material = new THREE.LineDashedMaterial({
color,
dashSize: 0.13,
gapSize: 0.09,
transparent: true,
opacity: 0.82
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
scene.add(line);
const marker = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x - 0.28, 0, z),
new THREE.Vector3(x + 0.28, 0, z)
]),
new THREE.LineBasicMaterial({ color })
);
scene.add(marker);
const tickMaterial = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.45
});
for (let height = 1; height <= maxHeight; height++) {
const tick = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x - 0.11, height, z),
new THREE.Vector3(x + 0.11, height, z)
]),
tickMaterial
);
scene.add(tick);
}
return {
x,
z,
set(level) {
const positions = geometry.attributes.position.array;
positions[0] = x;
positions[1] = 0.02;
positions[2] = z;
positions[3] = x;
positions[4] = level;
positions[5] = z;
geometry.attributes.position.needsUpdate = true;
line.computeLineDistances();
marker.position.y = level;
}
};
}
const upperGauge = createGauge(-5.45, 0.85, 8, 0x12bec9);
const lowerGauge = createGauge(6.55, 0.85, 3, 0x12bec9);
// -------------------------------------------------------------------------
// Hydraulic state
// -------------------------------------------------------------------------
const FLOW_Q = 0.45;
const INITIAL_UPPER_DEPTH = 2.82;
const INITIAL_LOWER_DEPTH = 0.35;
const state = {
sourceDepth: INITIAL_UPPER_DEPTH,
lowerDepth: INITIAL_LOWER_DEPTH,
flowing: true,
paused: false,
simulationTime: 0
};
const upperLabel = document.getElementById("upper-label");
const lowerLabel = document.getElementById("lower-label");
const upperLabelValue = document.getElementById("upper-label-value");
const lowerLabelValue = document.getElementById("lower-label-value");
const statUpper = document.getElementById("stat-upper");
const statLower = document.getElementById("stat-lower");
const statHead = document.getElementById("stat-head");
const statVolume = document.getElementById("stat-volume");
const status = document.getElementById("status");
const pauseButton = document.getElementById("pause");
const resetButton = document.getElementById("reset");
function updateWaterGeometry() {
const upperLevel = BARREL_BOTTOM + 0.06 + state.sourceDepth;
const lowerLevel = TANK_BOTTOM + state.lowerDepth;
upperWater.scale.y = state.sourceDepth;
upperWater.position.set(
BARREL_X,
BARREL_BOTTOM + 0.06 + state.sourceDepth / 2,
0
);
upperSurface.position.set(BARREL_X, upperLevel + 0.007, 0);
upperRim.position.set(BARREL_X, upperLevel + 0.015, 0);
lowerWater.scale.y = state.lowerDepth;
lowerWater.position.set(
TANK_X,
TANK_BOTTOM + state.lowerDepth / 2,
0
);
lowerSurface.position.set(TANK_X, lowerLevel + 0.007, 0);
upperGauge.set(upperLevel);
lowerGauge.set(lowerLevel);
const transferred =
(INITIAL_UPPER_DEPTH - state.sourceDepth) * UPPER_AREA;
upperLabelValue.textContent = `${upperLevel.toFixed(2)} m`;
lowerLabelValue.textContent = `${lowerLevel.toFixed(2)} m`;
statUpper.textContent = `${upperLevel.toFixed(2)} m`;
statLower.textContent = `${lowerLevel.toFixed(2)} m`;
statHead.textContent = `${(upperLevel - lowerLevel).toFixed(2)} m`;
statVolume.textContent = `${transferred.toFixed(2)} m³`;
if (state.paused) {
status.dataset.state = "paused";
status.textContent = "Paused";
} else if (state.flowing) {
status.dataset.state = "flowing";
status.textContent = "Flowing";
} else {
status.dataset.state = "stopped";
status.textContent = "Inlet exposed";
}
}
function resetSimulation() {
state.sourceDepth = INITIAL_UPPER_DEPTH;
state.lowerDepth = INITIAL_LOWER_DEPTH;
state.flowing = true;
state.paused = false;
state.simulationTime = 0;
pauseButton.textContent = "Pause";
updateWaterGeometry();
}
function togglePause() {
if (!state.flowing) return;
state.paused = !state.paused;
pauseButton.textContent = state.paused ? "Resume" : "Pause";
updateWaterGeometry();
}
pauseButton.addEventListener("click", togglePause);
resetButton.addEventListener("click", resetSimulation);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "r") {
resetSimulation();
}
});
// -------------------------------------------------------------------------
// Custom camera controls — no external controls addon
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.73,
pitch: 0.48,
radius: 19.5,
target: new THREE.Vector3(0.15, 3.9, 0)
};
function updateCamera() {
const { yaw, pitch, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.cos(pitch) * Math.sin(yaw),
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
let dragging = false;
let dragMode = "orbit";
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
dragging = true;
dragMode = event.shiftKey || event.button === 2 ? "pan" : "orbit";
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (dragMode === "orbit") {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.22,
1.28
);
} else {
const scale = cameraState.radius * 0.0012;
const right = new THREE.Vector3(
Math.cos(cameraState.yaw),
0,
-Math.sin(cameraState.yaw)
);
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.y += dy * scale;
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-5,
5
);
cameraState.target.y = THREE.MathUtils.clamp(
cameraState.target.y,
2,
6
);
}
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
11,
31
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.yaw = 0.73;
cameraState.pitch = 0.48;
cameraState.radius = 19.5;
cameraState.target.set(0.15, 3.9, 0);
});
// -------------------------------------------------------------------------
// Projected labels
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, x, y, z, offsetX = 0, offsetY = 0) {
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 + offsetX}px`;
element.style.top = `${screenY + offsetY}px`;
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (!state.paused && state.flowing) {
state.simulationTime += dt;
const availableDepth = state.sourceDepth - INLET_DEPTH;
if (availableDepth <= 0.00001) {
state.sourceDepth = INLET_DEPTH;
state.flowing = false;
} else {
const transferredVolume = Math.min(
FLOW_Q * dt,
availableDepth * UPPER_AREA
);
state.sourceDepth -= transferredVolume / UPPER_AREA;
state.lowerDepth += transferredVolume / LOWER_AREA;
if (state.sourceDepth <= INLET_DEPTH + 0.00001) {
state.sourceDepth = INLET_DEPTH;
state.flowing = false;
}
}
}
updateWaterGeometry();
// Flow packets follow the exact prescribed siphon route.
const visualSpeed = 0.105 * (FLOW_Q / 0.45);
for (let index = 0; index < PARTICLE_COUNT; index++) {
const u =
(index / PARTICLE_COUNT + state.simulationTime * visualSpeed) % 1;
pipeCurve.getPointAt(u, particlePosition);
dummy.position.copy(particlePosition);
dummy.rotation.set(0, 0, 0);
const pulse = 0.8 + 0.2 * Math.sin(state.simulationTime * 5 + index);
dummy.scale.setScalar(pulse);
dummy.updateMatrix();
particles.setMatrixAt(index, dummy.matrix);
}
particles.instanceMatrix.needsUpdate = true;
pipeCoreMaterial.opacity =
0.25 + 0.055 * Math.sin(state.simulationTime * 4.0);
updateCamera();
const upperLevel = BARREL_BOTTOM + 0.06 + state.sourceDepth;
const lowerLevel = TANK_BOTTOM + state.lowerDepth;
positionLabel(upperLabel, BARREL_X, upperLevel, 1.38, 0, -4);
positionLabel(lowerLabel, TANK_X, lowerLevel, 1.38, 0, -4);
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
resetSimulation();
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```