Prompt specification
Build or customize the Three.js 3D scene "Construction water-level teaching vignette" 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: Construction water-level teaching vignette
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/construction-water-level-teaching-vignette-x1451
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows liquid levels in communicating vessels. Compose two wall markers connected by a transparent hose containing water; demonstrate matching elevations without presenting the scene as a calibrated surveying instrument. Voxel style.
Working Three.js Starter (construction-water-level-teaching-vignette-x1451):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Communicating Vessels — Voxel Demonstration</title>
<style>
:root {
color-scheme: dark;
--ink: #eef7f7;
--muted: #9bb0b7;
--panel: rgba(12, 28, 36, 0.82);
--border: rgba(174, 224, 230, 0.18);
--cyan: #65e6e0;
--amber: #ffb36b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0b1720;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
#app {
position: fixed;
inset: 0;
}
#app canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
z-index: 0;
}
#app canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 3;
pointer-events: none;
}
.panel {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: auto;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: var(--ink);
font-size: clamp(19px, 3vw, 26px);
line-height: 1.08;
letter-spacing: -0.04em;
}
.description {
margin: 11px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
}
.status {
display: inline-flex;
align-items: center;
gap: 8px;
color: var(--ink);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 14px rgba(101, 230, 224, 0.8);
transition: background 160ms, box-shadow 160ms;
}
.status-dot.active {
background: var(--amber);
box-shadow: 0 0 14px rgba(255, 179, 107, 0.9);
}
.qualifier {
color: #748c94;
font-size: 9px;
text-align: right;
line-height: 1.35;
}
.controls {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
button {
appearance: none;
border: 1px solid rgba(174, 224, 230, 0.2);
border-radius: 8px;
padding: 10px 12px;
background: rgba(91, 155, 166, 0.1);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(101, 230, 224, 0.55);
background: rgba(101, 230, 224, 0.13);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: rgba(101, 230, 224, 0.5);
background: rgba(101, 230, 224, 0.17);
color: #d9ffff;
}
.hint {
left: 22px;
bottom: 20px;
color: #7e979f;
font-size: 10px;
letter-spacing: 0.04em;
text-shadow: 0 2px 8px #000;
}
.concept-note {
right: 22px;
bottom: 20px;
max-width: 330px;
padding: 10px 12px;
border: 1px solid rgba(255, 179, 107, 0.2);
border-radius: 8px;
background: rgba(12, 28, 36, 0.65);
color: #b7c6ca;
font-size: 9px;
line-height: 1.45;
text-align: right;
}
.scene-label {
position: fixed;
z-index: 2;
left: 0;
top: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
pointer-events: none;
text-shadow: 0 2px 12px rgba(0, 0, 0, 0.9);
}
.vessel-label {
padding: 6px 8px;
border: 1px solid rgba(174, 224, 230, 0.22);
border-radius: 6px;
background: rgba(9, 23, 30, 0.76);
color: #e8f8f8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
}
.equal-label {
padding: 6px 9px;
border: 1px solid rgba(255, 179, 107, 0.42);
border-radius: 6px;
background: rgba(35, 28, 23, 0.82);
color: #ffd3a7;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.description {
margin: 8px 0 12px;
}
.hint {
left: 12px;
bottom: 10px;
}
.concept-note {
right: 12px;
bottom: 10px;
max-width: 220px;
}
}
@media (max-width: 480px) {
.concept-note {
display: none;
}
.qualifier {
display: none;
}
}
</style>
</head>
<body>
<main id="app">
<section class="hud panel" aria-label="Demonstration controls">
<div class="eyebrow">Qualitative physics model</div>
<h1>Communicating Vessels</h1>
<p class="description">
Add water to either open vessel. The connected columns settle to the
same liquid elevation.
</p>
<div class="status-row">
<div class="status">
<span class="status-dot" id="statusDot"></span>
<span id="statusText" aria-live="polite">Balanced</span>
</div>
<div class="qualifier">Unnumbered guides<br />No calibrated scale</div>
</div>
<div class="controls">
<button class="primary" id="pourButton">Pour into A</button>
<button id="drainButton">Lower level</button>
<button id="resetButton">Reset</button>
</div>
</section>
<div class="hud hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<div class="hud concept-note">
Concept illustration only. The marks show relative elevation and are not
surveying or measurement graduations.
</div>
<div id="labelA" class="scene-label vessel-label">VESSEL A</div>
<div id="labelB" class="scene-label vessel-label">VESSEL B</div>
<div id="equalLabel" class="scene-label equal-label">Equal liquid elevation</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, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive 3D communicating vessels");
app.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1720);
scene.fog = new THREE.Fog(0x0b1720, 16, 27);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0, 3.1, 0);
const cameraState = {
radius: 12.8,
theta: 0.13,
phi: 1.24
};
function updateCamera() {
const mobileBoost = window.innerWidth < 700 ? 1.28 : 1;
const radius = cameraState.radius * mobileBoost;
camera.position.set(
cameraTarget.x +
radius *
Math.sin(cameraState.phi) *
Math.sin(cameraState.theta),
cameraTarget.y + radius * Math.cos(cameraState.phi),
cameraTarget.z +
radius *
Math.sin(cameraState.phi) *
Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xccefff, 0x25323a, 1.45);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d8, 3.0);
keyLight.position.set(5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5ad9ff, 1.4);
rimLight.position.set(-7, 5, 4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9d62, 12, 15, 2);
warmLight.position.set(0, 2.2, 4);
scene.add(warmLight);
const materials = {
wallBack: new THREE.MeshStandardMaterial({
color: 0x243640,
roughness: 0.95
}),
brick: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9
}),
floor: new THREE.MeshStandardMaterial({
color: 0x162832,
roughness: 0.86
}),
floorEdge: new THREE.MeshStandardMaterial({
color: 0x29434d,
roughness: 0.75
}),
frame: new THREE.MeshStandardMaterial({
color: 0xd6b581,
roughness: 0.48,
metalness: 0.08
}),
darkFrame: new THREE.MeshStandardMaterial({
color: 0x18313b,
roughness: 0.56,
metalness: 0.18
}),
plaque: new THREE.MeshStandardMaterial({
color: 0x112731,
roughness: 0.72
}),
tick: new THREE.MeshStandardMaterial({
color: 0xe3dfc8,
roughness: 0.6
}),
indicator: new THREE.MeshStandardMaterial({
color: 0xff9b5f,
emissive: 0x8b3517,
emissiveIntensity: 0.5,
roughness: 0.38
}),
glass: new THREE.MeshStandardMaterial({
color: 0xc7f3ff,
transparent: true,
opacity: 0.13,
roughness: 0.12,
metalness: 0,
depthWrite: false,
side: THREE.DoubleSide
}),
hoseGlass: new THREE.MeshStandardMaterial({
color: 0xa9e9f3,
transparent: true,
opacity: 0.2,
roughness: 0.1,
metalness: 0,
depthWrite: false,
side: THREE.DoubleSide
}),
hoseWater: new THREE.MeshStandardMaterial({
color: 0x1bb9cf,
emissive: 0x063c4c,
emissiveIntensity: 0.5,
roughness: 0.2,
metalness: 0.03
}),
collar: new THREE.MeshStandardMaterial({
color: 0x31545e,
roughness: 0.4,
metalness: 0.22
}),
flowParticle: new THREE.MeshBasicMaterial({
color: 0xb9ffff,
transparent: true,
opacity: 0,
depthWrite: false
}),
equalLine: new THREE.MeshBasicMaterial({
color: 0xffc28d,
transparent: true,
opacity: 0.9,
depthWrite: false
})
};
function box(width, height, depth, material, x, y, z, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Voxel wall.
box(12.7, 6.35, 0.28, materials.wallBack, 0, 3.15, -1.48, false);
const brickGeometry = new THREE.BoxGeometry(0.98, 0.43, 0.25);
const brickData = [];
for (let row = 0; row < 13; row++) {
const offset = row % 2 === 0 ? 0 : 0.5;
for (let column = -6; column <= 5; column++) {
const x = column * 1.02 + offset;
if (x > -6.25 && x < 6.25) {
brickData.push({ x, y: 0.22 + row * 0.485 });
}
}
}
const bricks = new THREE.InstancedMesh(
brickGeometry,
materials.brick,
brickData.length
);
const transform = new THREE.Object3D();
const brickPalette = [
0x354954,
0x3b505a,
0x30444e,
0x40545d,
0x354b55
];
brickData.forEach((brick, index) => {
transform.position.set(brick.x, brick.y, -1.29);
transform.rotation.set(0, 0, 0);
transform.scale.set(1, 1, 1);
transform.updateMatrix();
bricks.setMatrixAt(index, transform.matrix);
const color = new THREE.Color(
brickPalette[(index * 7 + Math.floor(index / 6)) % brickPalette.length]
);
color.offsetHSL(0, 0, ((index % 5) - 2) * 0.012);
bricks.setColorAt(index, color);
});
bricks.instanceMatrix.needsUpdate = true;
if (bricks.instanceColor) bricks.instanceColor.needsUpdate = true;
bricks.receiveShadow = true;
scene.add(bricks);
// Chunky display platform.
box(13.1, 0.28, 4.7, materials.floor, 0, -0.25, 0.12, false);
box(13.1, 0.13, 4.55, materials.floorEdge, 0, -0.045, 0.12, false);
for (let i = -5; i <= 5; i++) {
box(
0.16,
0.1 + (Math.abs(i) % 3) * 0.035,
0.42,
materials.floorEdge,
i * 1.05,
0.045,
2.12,
false
);
}
const TANK_BOTTOM = 1.72;
const TANK_TOP = 6.06;
const INITIAL_LEVEL = 3.56;
const MIN_LEVEL = 2.05;
const MAX_LEVEL = 5.88;
const ROW_STEP = 0.27;
const ROW_COUNT = 16;
let currentLevel = INITIAL_LEVEL;
let desiredLevel = INITIAL_LEVEL;
let flowEnergy = 0;
let flowDirection = 1;
function createVoxelLetter(letter, x, topY, z) {
const patterns = {
A: [
"01110",
"10001",
"10001",
"11111",
"10001",
"10001",
"10001"
],
B: [
"11110",
"10001",
"10001",
"11110",
"10001",
"10001",
"11110"
]
};
const cells = [];
const unit = 0.067;
patterns[letter].forEach((row, rowIndex) => {
[...row].forEach((value, columnIndex) => {
if (value === "1") {
cells.push({
x:
x +
(columnIndex - 2) * unit -
unit * 0.5,
y: topY - rowIndex * unit
});
}
});
});
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(unit * 0.84, unit * 0.84, unit * 0.5),
materials.tick,
cells.length
);
cells.forEach((cell, index) => {
transform.position.set(cell.x, cell.y, z);
transform.scale.set(1, 1, 1);
transform.updateMatrix();
mesh.setMatrixAt(index, transform.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
scene.add(mesh);
}
function createVoxelWater(x) {
const columns = 3;
const depthCells = 3;
const count = ROW_COUNT * columns * depthCells;
const water = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.282, 0.258, 0.282),
new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0x063847,
emissiveIntensity: 0.48,
roughness: 0.2,
metalness: 0.04,
transparent: true,
opacity: 0.82,
depthWrite: false
}),
count
);
water.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
water.renderOrder = 2;
water.frustumCulled = false;
const palette = [
0x20b8cf,
0x28c5d4,
0x1ca9c4,
0x35ced8
];
let instance = 0;
for (let row = 0; row < ROW_COUNT; row++) {
for (let column = 0; column < columns; column++) {
for (let depth = 0; depth < depthCells; depth++) {
water.setColorAt(
instance++,
new THREE.Color(
palette[(row + column + depth) % palette.length]
)
);
}
}
}
if (water.instanceColor) water.instanceColor.needsUpdate = true;
scene.add(water);
const surface = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.292, 0.035, 0.292),
new THREE.MeshBasicMaterial({
color: 0x83f4ed,
transparent: true,
opacity: 0.88,
depthWrite: false
}),
9
);
let surfaceIndex = 0;
for (let column = 0; column < 3; column++) {
for (let depth = 0; depth < 3; depth++) {
transform.position.set(
x + (column - 1) * 0.3,
0,
(depth - 1) * 0.3
);
transform.scale.set(1, 1, 1);
transform.updateMatrix();
surface.setMatrixAt(surfaceIndex++, transform.matrix);
}
}
surface.instanceMatrix.needsUpdate = true;
surface.renderOrder = 3;
scene.add(surface);
const bubbles = [];
const bubbleGeometry = new THREE.BoxGeometry(0.055, 0.055, 0.055);
const bubbleMaterial = new THREE.MeshBasicMaterial({
color: 0xd5ffff,
transparent: true,
opacity: 0.72,
depthWrite: false
});
for (let i = 0; i < 7; i++) {
const bubble = new THREE.Mesh(bubbleGeometry, bubbleMaterial);
bubble.renderOrder = 4;
scene.add(bubble);
bubbles.push({
mesh: bubble,
offset: i / 7 + 0.07 * Math.sin(i * 2.3),
speed: 0.13 + (i % 3) * 0.025,
x: x + Math.sin(i * 2.7) * 0.31,
z: Math.cos(i * 1.9) * 0.31,
wobble: i * 1.73
});
}
return { water, surface, bubbles };
}
function createVessel(x, letter) {
const centerY = (TANK_BOTTOM + TANK_TOP) / 2;
const height = TANK_TOP - TANK_BOTTOM + 0.28;
// Transparent square vessel panels.
const front = box(
1.03,
height,
0.035,
materials.glass,
x,
centerY,
0.5,
false
);
const back = box(
1.03,
height,
0.035,
materials.glass,
x,
centerY,
-0.5,
false
);
const left = box(
0.035,
height,
0.96,
materials.glass,
x - 0.5,
centerY,
0,
false
);
const right = box(
0.035,
height,
0.96,
materials.glass,
x + 0.5,
centerY,
0,
false
);
[front, back, left, right].forEach((panel) => {
panel.renderOrder = 5;
});
// Voxel frame.
for (const dx of [-0.56, 0.56]) {
for (const dz of [-0.48, 0.48]) {
box(
0.11,
height,
0.11,
materials.frame,
x + dx,
centerY,
dz
);
}
}
for (const y of [TANK_BOTTOM - 0.15, TANK_TOP + 0.15]) {
box(1.23, 0.12, 0.11, materials.frame, x, y, 0.48);
box(1.23, 0.12, 0.11, materials.frame, x, y, -0.48);
box(0.11, 0.12, 0.96, materials.frame, x - 0.56, y, 0);
box(0.11, 0.12, 0.96, materials.frame, x + 0.56, y, 0);
}
// Wall plaque behind each transparent vessel.
box(
1.16,
4.76,
0.12,
materials.plaque,
x,
centerY,
-0.72,
false
);
// Mounting blocks connect the assembly to the wall.
for (const y of [2.15, 5.35]) {
box(0.24, 0.28, 0.58, materials.darkFrame, x, y, -0.9);
}
const markerX = x < 0 ? x + 0.68 : x - 0.68;
// Deliberately unnumbered visual guide marks.
for (let y = 1.93; y <= 5.96; y += 0.43) {
const major = Math.round((y - 1.93) / 0.43) % 2 === 0;
box(
major ? 0.19 : 0.115,
0.045,
0.07,
materials.tick,
markerX,
y,
-0.62,
false
);
}
const indicator = box(
0.17,
0.17,
0.16,
materials.indicator,
markerX,
currentLevel,
-0.57,
false
);
createVoxelLetter(letter, x, 6.68, -0.55);
const liquid = createVoxelWater(x);
return {
x,
markerX,
indicator,
...liquid
};
}
const vesselA = createVessel(-2.65, "A");
const vesselB = createVessel(2.65, "B");
const HOSE_Y = 0.63;
const HOSE_TOP = 1.73;
const HOSE_WIDTH = 0.42;
const HOSE_THICKNESS = 0.035;
function makeHoseSegment(axis, center, length) {
const panels = [];
if (axis === "y") {
const x = center.x;
const y = center.y;
const z = center.z;
panels.push(
box(
HOSE_THICKNESS,
length,
HOSE_WIDTH,
materials.hoseGlass,
x - HOSE_WIDTH / 2,
y,
z,
false
),
box(
HOSE_THICKNESS,
length,
HOSE_WIDTH,
materials.hoseGlass,
x + HOSE_WIDTH / 2,
y,
z,
false
),
box(
HOSE_WIDTH,
length,
HOSE_THICKNESS,
materials.hoseGlass,
x,
y,
z - HOSE_WIDTH / 2,
false
),
box(
HOSE_WIDTH,
length,
HOSE_THICKNESS,
materials.hoseGlass,
x,
y,
z + HOSE_WIDTH / 2,
false
)
);
} else {
const x = center.x;
const y = center.y;
const z = center.z;
panels.push(
box(
length,
HOSE_THICKNESS,
HOSE_WIDTH,
materials.hoseGlass,
x,
y - HOSE_WIDTH / 2,
z,
false
),
box(
length,
HOSE_THICKNESS,
HOSE_WIDTH,
materials.hoseGlass,
x,
y + HOSE_WIDTH / 2,
z,
false
),
box(
length,
HOSE_WIDTH,
HOSE_THICKNESS,
materials.hoseGlass,
x,
y,
z - HOSE_WIDTH / 2,
false
),
box(
length,
HOSE_WIDTH,
HOSE_THICKNESS,
materials.hoseGlass,
x,
y,
z + HOSE_WIDTH / 2,
false
)
);
}
panels.forEach((panel) => {
panel.renderOrder = 6;
});
}
function makeCollar(axis, center) {
const outer = 0.49;
const bar = 0.055;
const thickness = 0.075;
if (axis === "y") {
const { x, y, z } = center;
box(outer, thickness, bar, materials.collar, x, y, z + outer / 2);
box(outer, thickness, bar, materials.collar, x, y, z - outer / 2);
box(bar, thickness, outer, materials.collar, x + outer / 2, y, z);
box(bar, thickness, outer, materials.collar, x - outer / 2, y, z);
} else {
const { x, y, z } = center;
box(thickness, outer, bar, materials.collar, x, y + outer / 2, z);
box(thickness, outer, bar, materials.collar, x, y - outer / 2, z);
box(thickness, bar, outer, materials.collar, x, y, z + outer / 2);
box(thickness, bar, outer, materials.collar, x, y, z - outer / 2);
}
}
const verticalLength = HOSE_TOP - HOSE_Y;
const verticalCenterY = (HOSE_TOP + HOSE_Y) / 2;
const horizontalLength = 5.3;
makeHoseSegment(
"y",
{ x: -2.65, y: verticalCenterY, z: 0.02 },
verticalLength
);
makeHoseSegment(
"y",
{ x: 2.65, y: verticalCenterY, z: 0.02 },
verticalLength
);
makeHoseSegment(
"x",
{ x: 0, y: HOSE_Y, z: 0.02 },
horizontalLength
);
// Blocky transparent elbow chambers.
for (const x of [-2.65, 2.65]) {
const elbow = box(
0.43,
0.43,
0.43,
materials.hoseGlass,
x,
HOSE_Y,
0.02,
false
);
elbow.renderOrder = 6;
}
// Ribbed collars give the square hose a readable segmented form.
for (const x of [-2.65, 2.65]) {
makeCollar("y", { x, y: 0.92, z: 0.02 });
makeCollar("y", { x, y: 1.48, z: 0.02 });
}
for (const x of [-1.75, 0, 1.75]) {
makeCollar("x", { x, y: HOSE_Y, z: 0.02 });
}
// Water inside the hose.
for (const x of [-2.65, 2.65]) {
box(
0.235,
verticalLength + 0.06,
0.235,
materials.hoseWater,
x,
verticalCenterY,
0.02,
false
);
box(
0.25,
0.25,
0.25,
materials.hoseWater,
x,
HOSE_Y,
0.02,
false
);
}
const hoseCubeCount = 19;
const hoseCubeLength = horizontalLength / hoseCubeCount;
for (let i = 0; i < hoseCubeCount; i++) {
const x =
-2.65 +
hoseCubeLength * (i + 0.5);
box(
hoseCubeLength * 0.96,
0.235,
0.235,
materials.hoseWater,
x,
HOSE_Y,
0.02,
false
);
}
// Bright voxel particles reveal transient flow through the hose.
const flowParticles = [];
const flowGeometry = new THREE.BoxGeometry(0.075, 0.075, 0.075);
for (let i = 0; i < 14; i++) {
const particle = new THREE.Mesh(flowGeometry, materials.flowParticle);
particle.renderOrder = 7;
scene.add(particle);
flowParticles.push(particle);
}
const pathLeftLength = HOSE_TOP - HOSE_Y;
const pathTotalLength = pathLeftLength * 2 + horizontalLength;
function pointOnHosePath(progress) {
const distance = progress * pathTotalLength;
if (distance <= pathLeftLength) {
return new THREE.Vector3(
-2.65,
HOSE_TOP - distance,
0.02
);
}
if (distance <= pathLeftLength + horizontalLength) {
return new THREE.Vector3(
-2.65 + distance - pathLeftLength,
HOSE_Y,
0.02
);
}
return new THREE.Vector3(
2.65,
HOSE_Y + distance - pathLeftLength - horizontalLength,
0.02
);
}
// Dotted equal-elevation guide.
const equalLine = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.068, 0.045, 0.068),
materials.equalLine,
27
);
for (let i = 0; i < 27; i++) {
transform.position.set(
-2.03 + (4.06 * i) / 26,
0,
0
);
transform.scale.set(1, 1, 1);
transform.updateMatrix();
equalLine.setMatrixAt(i, transform.matrix);
}
equalLine.instanceMatrix.needsUpdate = true;
equalLine.position.z = 0.77;
equalLine.renderOrder = 8;
scene.add(equalLine);
function updateVoxelWater(liquid, level, time) {
const usableHeight = Math.max(0, level - TANK_BOTTOM);
const fullRows = Math.min(
ROW_COUNT,
Math.floor(usableHeight / ROW_STEP)
);
const remainder = usableHeight - fullRows * ROW_STEP;
let instance = 0;
for (let row = 0; row < ROW_COUNT; row++) {
let height = 0;
if (row < fullRows) {
height = ROW_STEP - 0.012;
} else if (row === fullRows) {
height = remainder;
}
for (let column = 0; column < 3; column++) {
for (let depth = 0; depth < 3; depth++) {
transform.position.set(
liquid.x + (column - 1) * 0.3,
TANK_BOTTOM + row * ROW_STEP + height / 2,
(depth - 1) * 0.3
);
transform.scale.set(
1,
Math.max(0.00001, height / 0.258),
1
);
transform.updateMatrix();
liquid.water.setMatrixAt(instance++, transform.matrix);
}
}
}
liquid.water.instanceMatrix.needsUpdate = true;
liquid.surface.position.y =
level + Math.sin(time * 3.2) * 0.008 * flowEnergy;
liquid.bubbles.forEach((bubble) => {
const progress =
(time * bubble.speed + bubble.offset) % 1;
const span = Math.max(0.1, level - TANK_BOTTOM - 0.16);
const y = TANK_BOTTOM + 0.08 + progress * span;
bubble.mesh.visible = y < level - 0.04;
bubble.mesh.position.set(
bubble.x + Math.sin(time * 1.8 + bubble.wobble) * 0.025,
y,
bubble.z + Math.cos(time * 1.4 + bubble.wobble) * 0.018
);
bubble.mesh.rotation.set(
time * 0.5 + bubble.wobble,
time * 0.7,
0
);
});
}
// Direct camera controls; no addon modules are used.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.62,
1.47
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
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();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
8.7,
17.5
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.radius = 12.8;
cameraState.theta = 0.13;
cameraState.phi = 1.24;
updateCamera();
});
const statusText = document.querySelector("#statusText");
const statusDot = document.querySelector("#statusDot");
document.querySelector("#pourButton").addEventListener("click", () => {
desiredLevel = Math.min(MAX_LEVEL, desiredLevel + 0.72);
statusText.textContent = "Sharing";
});
document.querySelector("#drainButton").addEventListener("click", () => {
desiredLevel = Math.max(MIN_LEVEL, desiredLevel - 0.72);
statusText.textContent = "Settling";
});
document.querySelector("#resetButton").addEventListener("click", () => {
desiredLevel = INITIAL_LEVEL;
statusText.textContent = "Balanced";
});
const labelA = document.querySelector("#labelA");
const labelB = document.querySelector("#labelB");
const equalLabel = document.querySelector("#equalLabel");
const labelPointA = new THREE.Vector3(-2.65, 7.05, 0.04);
const labelPointB = new THREE.Vector3(2.65, 7.05, 0.04);
const equalPoint = new THREE.Vector3(0, currentLevel + 0.48, 0.77);
function projectLabel(element, point) {
const projected = point.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
const difference = desiredLevel - currentLevel;
const changing = Math.abs(difference) > 0.003;
if (changing) {
currentLevel +=
difference * (1 - Math.exp(-delta * 2.7));
flowDirection = Math.sign(difference) || 1;
} else {
currentLevel = desiredLevel;
}
const targetFlow = changing
? Math.min(1, Math.abs(difference) * 6)
: 0;
flowEnergy +=
(targetFlow - flowEnergy) *
(1 - Math.exp(-delta * 7));
if (changing || flowEnergy > 0.025) {
statusText.textContent = changing ? "Sharing" : "Settling";
statusDot.classList.add("active");
} else {
statusText.textContent = "Balanced";
statusDot.classList.remove("active");
}
updateVoxelWater(vesselA, currentLevel, elapsed);
updateVoxelWater(vesselB, currentLevel, elapsed);
vesselA.indicator.position.y = currentLevel;
vesselB.indicator.position.y = currentLevel;
equalLine.position.y = currentLevel + 0.065;
equalPoint.y = currentLevel + 0.48;
materials.flowParticle.opacity = flowEnergy * 0.92;
flowParticles.forEach((particle, index) => {
let progress =
(index / flowParticles.length + elapsed * 0.18 * flowDirection) % 1;
if (progress < 0) progress += 1;
particle.position.copy(pointOnHosePath(progress));
particle.rotation.set(
elapsed * 1.4 + index,
elapsed * 1.9,
elapsed
);
particle.visible = flowEnergy > 0.015;
});
projectLabel(labelA, labelPointA);
projectLabel(labelB, labelPointB);
projectLabel(equalLabel, equalPoint);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```