Prompt specification
Build or customize the Three.js 3D scene "Water flow starts wheel rotation" 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: Water flow starts wheel rotation
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/water-flow-starts-wheel-rotation-x0394
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a toy waterwheel. Activate a prescribed stream that fills upper buckets and empties lower ones; wheel speed follows a labeled simple rule while water always returns to the tray. Voxel style.
Working Three.js Starter (water-flow-starts-wheel-rotation-x0394):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Tidewheel</title>
<style>
:root {
--ink: #18343a;
--paper: rgba(246, 244, 225, 0.94);
--line: rgba(24, 52, 58, 0.2);
--blue: #168dc2;
--blue-dark: #08638d;
--orange: #ee8b3d;
--green: #4fa86d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d5e5df;
color: var(--ink);
font-family: "Courier New", Courier, monospace;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
max-width: 360px;
text-shadow: 0 2px 0 rgba(255, 255, 255, 0.45);
}
.brand h1 {
margin: 0;
font-size: clamp(25px, 3.2vw, 45px);
line-height: 0.92;
letter-spacing: -0.07em;
text-transform: uppercase;
}
.brand p {
margin: 9px 0 0;
font-size: 11px;
font-weight: bold;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.card {
pointer-events: auto;
border: 2px solid var(--ink);
background: var(--paper);
box-shadow: 7px 7px 0 rgba(24, 52, 58, 0.18);
backdrop-filter: blur(8px);
}
.controls {
position: absolute;
top: 24px;
right: 26px;
width: 290px;
padding: 16px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 11px;
font-size: 10px;
font-weight: bold;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status-dot {
width: 10px;
height: 10px;
border: 2px solid var(--ink);
background: #aaa;
}
.status-dot.on {
background: var(--green);
box-shadow: 0 0 0 3px rgba(79, 168, 109, 0.2);
}
.button-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
margin-bottom: 14px;
}
button {
min-height: 42px;
border: 2px solid var(--ink);
border-radius: 0;
background: var(--orange);
color: var(--ink);
font: 900 12px/1 "Courier New", Courier, monospace;
letter-spacing: 0.05em;
cursor: pointer;
box-shadow: 3px 3px 0 var(--ink);
transition: transform 0.08s, box-shadow 0.08s, background 0.15s;
}
button:hover {
transform: translate(-1px, -1px);
box-shadow: 4px 4px 0 var(--ink);
}
button:active {
transform: translate(3px, 3px);
box-shadow: 0 0 0 var(--ink);
}
button.on {
background: var(--green);
}
button.secondary {
width: 42px;
padding: 0;
background: #f7f3d8;
font-size: 16px;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
border-top: 2px solid var(--line);
border-bottom: 2px solid var(--line);
}
.metric {
padding: 11px 4px 10px 0;
}
.metric + .metric {
padding-left: 13px;
border-left: 2px solid var(--line);
}
.metric span {
display: block;
font-size: 9px;
font-weight: bold;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 5px;
font-size: 19px;
}
.route {
margin-top: 12px;
font-size: 10px;
line-height: 1.55;
}
.route b {
color: var(--blue-dark);
}
.rule-card {
position: absolute;
left: 26px;
bottom: 25px;
width: 320px;
padding: 14px 16px;
}
.formula {
margin: 3px 0 7px;
font-size: 22px;
font-weight: 900;
letter-spacing: -0.04em;
}
.rule-note {
font-size: 10px;
line-height: 1.5;
}
.loop-badge {
display: inline-block;
margin-top: 10px;
padding: 5px 7px;
border: 2px solid var(--ink);
background: var(--green);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
}
.hint {
position: absolute;
right: 27px;
bottom: 26px;
padding: 9px 11px;
border: 2px solid var(--line);
background: rgba(246, 244, 225, 0.82);
font-size: 9px;
font-weight: bold;
line-height: 1.6;
letter-spacing: 0.08em;
text-align: right;
text-transform: uppercase;
}
.annotation {
position: absolute;
padding: 7px 9px;
border-left: 4px solid var(--blue);
background: rgba(246, 244, 225, 0.85);
font-size: 9px;
font-weight: bold;
line-height: 1.45;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.annotation strong {
display: block;
color: var(--blue-dark);
font-size: 10px;
}
.annotation.intake {
top: 27%;
left: 14%;
}
.annotation.drain {
right: 13%;
bottom: 27%;
border-left-color: var(--orange);
}
.annotation.drain strong {
color: #a64d1c;
}
@media (max-width: 760px) {
.brand {
top: 16px;
left: 16px;
}
.brand p {
display: none;
}
.controls {
top: auto;
right: 14px;
bottom: 14px;
width: 260px;
padding: 12px;
}
.rule-card {
left: 14px;
bottom: 14px;
width: calc(100% - 294px);
min-width: 180px;
padding: 11px;
}
.formula {
font-size: 17px;
}
.route,
.loop-badge,
.annotation,
.hint {
display: none;
}
}
@media (max-width: 520px) {
.rule-card {
display: none;
}
.controls {
left: 14px;
right: 14px;
width: auto;
}
.brand h1 {
font-size: 27px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel waterwheel"></canvas>
<div class="ui">
<header class="brand">
<h1>Tidewheel</h1>
<p>A recirculating voxel water machine</p>
</header>
<section class="card controls" aria-label="Waterwheel controls">
<div class="eyebrow">
<span id="statusDot" class="status-dot on"></span>
<span id="flowState">Stream active</span>
</div>
<div class="button-row">
<button id="streamButton" class="on" type="button" aria-pressed="true">
DEACTIVATE STREAM
</button>
<button id="viewButton" class="secondary" type="button" title="Reset camera">
↺
</button>
</div>
<div class="metrics">
<div class="metric">
<span>Wheel speed</span>
<strong id="omegaValue">0.80 rad/s</strong>
</div>
<div class="metric">
<span>Load index F</span>
<strong id="fillValue">0.00</strong>
</div>
</div>
<div class="route">
<b>Upper intake:</b> 0.45 bucket/s<br />
<b>Lower drain:</b> 2.40 bucket/s<br />
<b>Path:</b> tray → pump → buckets → tray
</div>
</section>
<section class="card rule-card" aria-label="Wheel speed rule">
<div class="eyebrow">Labeled speed rule</div>
<div class="formula">ω = A(0.8 + 1.6F)</div>
<div class="rule-note">
A = 1 while streaming, otherwise 0.<br />
F = fullest bucket fill, from 0 to 1.
</div>
<div class="loop-badge">✓ CLOSED WATER LOOP</div>
</section>
<div class="annotation intake">
<strong>01 / Upper intake</strong>
Fixed stream · 0.45 bucket/s
</div>
<div class="annotation drain">
<strong>02 / Lower return</strong>
Buckets empty into the tray
</div>
<div class="hint">
Drag to orbit · Scroll to zoom<br />
Space: stream · R: reset simulation
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene and renderer
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd5e5df);
scene.fog = new THREE.Fog(0xd5e5df, 19, 34);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 3.65, 0);
let cameraYaw = 0.62;
let cameraPitch = 0.29;
let cameraRadius = 15.5;
function updateCamera() {
const horizontal = cameraRadius * Math.cos(cameraPitch);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraYaw),
cameraTarget.y + cameraRadius * Math.sin(cameraPitch),
cameraTarget.z + horizontal * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = 0.62;
cameraPitch = 0.29;
cameraRadius = 15.5;
updateCamera();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xf7fff2, 0x66837b, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1ce, 3.3);
sun.position.set(-7, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x9bd9ff, 1.1);
fillLight.position.set(8, 6, -7);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Shared voxel geometry and materials
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function makeMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
}
const materials = {
ground: makeMaterial(0xb8c9b6),
grass: makeMaterial(0x8eae84),
grassLight: makeMaterial(0xa9c394),
tray: makeMaterial(0xb98b57),
trayDark: makeMaterial(0x8d603d),
trayEdge: makeMaterial(0xd2a26c),
wood: makeMaterial(0xa96b38),
woodLight: makeMaterial(0xd2944f),
woodDark: makeMaterial(0x71472e),
bucket: makeMaterial(0xe1a657),
bucketDark: makeMaterial(0xb77735),
support: makeMaterial(0x526d68),
supportLight: makeMaterial(0x78918a),
pipe: makeMaterial(0x3f777c, { metalness: 0.18, roughness: 0.48 }),
pipeJoint: makeMaterial(0x28595f, { metalness: 0.2, roughness: 0.45 }),
orange: makeMaterial(0xe87e32),
dark: makeMaterial(0x294b50),
water: makeMaterial(0x168dc2, {
transparent: true,
opacity: 0.78,
roughness: 0.2,
metalness: 0.04,
depthWrite: false
}),
waterBright: makeMaterial(0x35b8e8, {
transparent: true,
opacity: 0.9,
roughness: 0.16,
metalness: 0.02,
depthWrite: false
}),
waterDeep: makeMaterial(0x0873a6, {
transparent: true,
opacity: 0.82,
roughness: 0.24,
depthWrite: false
}),
pump: makeMaterial(0x3f8f96, {
emissive: 0x164d52,
emissiveIntensity: 0.15,
metalness: 0.22,
roughness: 0.42
})
};
function addBox(parent, material, size, position, castShadow = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBeam(parent, material, from, to, width = 0.22, depth = 0.28) {
const start = new THREE.Vector3(...from);
const end = new THREE.Vector3(...to);
const direction = end.clone().sub(start);
const length = direction.length();
const beam = new THREE.Mesh(unitBox, material);
beam.scale.set(width, length, depth);
beam.position.copy(start).add(end).multiplyScalar(0.5);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
beam.castShadow = true;
beam.receiveShadow = true;
parent.add(beam);
return beam;
}
// -------------------------------------------------------------------------
// Tray, ground, and decorative voxel landscaping
// -------------------------------------------------------------------------
addBox(scene, materials.ground, [32, 0.3, 26], [0, -0.25, 0], false);
for (let i = 0; i < 34; i++) {
const angle = (i / 34) * Math.PI * 2;
const radiusX = 9.5 + Math.sin(i * 2.7) * 2.0;
const radiusZ = 8.5 + Math.cos(i * 1.9) * 1.8;
const size = 0.45 + (i % 3) * 0.11;
addBox(
scene,
i % 2 ? materials.grass : materials.grassLight,
[size, 0.24 + (i % 2) * 0.1, size],
[
Math.cos(angle) * radiusX,
0.01,
Math.sin(angle) * radiusZ
]
);
}
const tray = new THREE.Group();
scene.add(tray);
addBox(tray, materials.trayDark, [12.2, 0.42, 7.1], [0, 0.12, 0]);
addBox(tray, materials.tray, [11.8, 0.14, 6.7], [0, 0.38, 0]);
// Low front rim and taller rear/side rims keep the returning water visible.
addBox(tray, materials.trayEdge, [12.0, 0.46, 0.3], [0, 0.58, 3.38]);
addBox(tray, materials.trayEdge, [12.0, 0.9, 0.3], [0, 0.75, -3.38]);
addBox(tray, materials.trayEdge, [0.3, 0.9, 6.7], [-5.92, 0.75, 0]);
addBox(tray, materials.trayEdge, [0.3, 0.9, 6.7], [5.92, 0.75, 0]);
// Block construction joints around the tray.
for (let x = -5.4; x <= 5.4; x += 1.8) {
addBox(tray, materials.trayDark, [0.1, 0.1, 0.34], [x, 0.83, 3.38], false);
addBox(tray, materials.trayDark, [0.1, 0.94, 0.34], [x, 0.76, -3.38], false);
}
const trayWaterTiles = [];
const trayWater = new THREE.Group();
scene.add(trayWater);
for (let x = 0; x < 13; x++) {
for (let z = 0; z < 7; z++) {
const tile = addBox(
trayWater,
(x + z) % 3 === 0 ? materials.waterBright : materials.water,
[0.47, 0.09, 0.47],
[-5.4 + x * 0.9, 0, -2.7 + z * 0.9],
false
);
tile.receiveShadow = true;
trayWaterTiles.push(tile);
}
}
// Small voxel stones around the tray.
const stoneMaterials = [
makeMaterial(0x8c9990),
makeMaterial(0xa9afa1),
makeMaterial(0x76877f)
];
for (let i = 0; i < 14; i++) {
const side = i % 2 ? 1 : -1;
const x = side * (6.7 + (i % 3) * 0.7);
const z = -3 + ((i * 1.31) % 6);
addBox(
scene,
stoneMaterials[i % stoneMaterials.length],
[0.45 + (i % 2) * 0.15, 0.32, 0.5],
[x, 0.1, z]
);
}
// -------------------------------------------------------------------------
// Wheel supports and axle
// -------------------------------------------------------------------------
const wheelCenterY = 4.12;
const wheelRadius = 2.5;
const supportZ = 0.94;
for (const z of [-supportZ, supportZ]) {
addBeam(
scene,
materials.support,
[-3.65, 0.48, z],
[0, wheelCenterY, z],
0.31,
0.38
);
addBeam(
scene,
materials.support,
[3.65, 0.48, z],
[0, wheelCenterY, z],
0.31,
0.38
);
addBox(scene, materials.supportLight, [0.78, 0.24, 0.72], [-3.65, 0.47, z]);
addBox(scene, materials.supportLight, [0.78, 0.24, 0.72], [3.65, 0.47, z]);
}
addBox(
scene,
materials.dark,
[0.34, 0.34, 3.15],
[0, wheelCenterY, 0]
);
for (const z of [-1.63, 1.63]) {
addBox(scene, materials.orange, [0.66, 0.66, 0.18], [0, wheelCenterY, z]);
addBox(scene, materials.dark, [0.28, 0.28, 0.21], [0, wheelCenterY, z]);
}
// -------------------------------------------------------------------------
// Voxel waterwheel
// -------------------------------------------------------------------------
const wheel = new THREE.Group();
wheel.position.y = wheelCenterY;
scene.add(wheel);
const bucketCount = 12;
const bucketStep = (Math.PI * 2) / bucketCount;
const buckets = [];
// Block-built circular rim.
const rimBlocks = 32;
for (let i = 0; i < rimBlocks; i++) {
const angle = (i / rimBlocks) * Math.PI * 2;
addBox(
wheel,
i % 4 === 0 ? materials.woodDark : materials.woodLight,
[0.38, 0.38, 0.58],
[
Math.cos(angle) * wheelRadius,
Math.sin(angle) * wheelRadius,
0
]
);
}
// Eight radial spokes.
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const spoke = addBox(
wheel,
i % 2 ? materials.wood : materials.woodDark,
[wheelRadius - 0.25, 0.16, 0.27],
[
Math.cos(angle) * (wheelRadius - 0.25) * 0.5,
Math.sin(angle) * (wheelRadius - 0.25) * 0.5,
0
]
);
spoke.rotation.z = angle;
}
addBox(wheel, materials.woodDark, [0.9, 0.9, 1.25], [0, 0, 0]);
addBox(wheel, materials.orange, [0.52, 0.52, 1.36], [0, 0, 0]);
// Each bucket is an open outward-facing voxel scoop.
for (let i = 0; i < bucketCount; i++) {
const theta = i * bucketStep;
const group = new THREE.Group();
group.position.set(
Math.cos(theta) * wheelRadius,
Math.sin(theta) * wheelRadius,
0
);
// Local +Y points radially outward from the wheel.
group.rotation.z = theta - Math.PI / 2;
wheel.add(group);
addBox(group, materials.bucketDark, [1.0, 0.13, 1.02], [0, -0.29, 0]);
addBox(group, materials.bucket, [0.13, 0.68, 1.02], [-0.47, 0.02, 0]);
addBox(group, materials.bucket, [0.13, 0.68, 1.02], [0.47, 0.02, 0]);
addBox(group, materials.bucketDark, [0.88, 0.68, 0.13], [0, 0.02, -0.47]);
addBox(group, materials.bucketDark, [0.88, 0.68, 0.13], [0, 0.02, 0.47]);
// Four levels of 2×2 water voxels.
const cells = [];
for (let level = 0; level < 4; level++) {
for (const x of [-0.19, 0.19]) {
for (const z of [-0.19, 0.19]) {
const cell = addBox(
group,
materials.waterBright,
[0.29, 0.17, 0.29],
[x, -0.13 + level * 0.18, z],
false
);
cell.visible = false;
cells.push(cell);
}
}
}
buckets.push({
index: i,
theta,
group,
cells,
fill: 0,
worldPosition: new THREE.Vector3()
});
}
// -------------------------------------------------------------------------
// Recirculating pump and fixed stream pipe
// -------------------------------------------------------------------------
const pipe = new THREE.Group();
scene.add(pipe);
addBox(pipe, materials.pump, [1.12, 0.78, 1.0], [-4.35, 0.78, -0.85]);
addBox(pipe, materials.pipeJoint, [1.25, 0.18, 1.12], [-4.35, 1.15, -0.85]);
addBox(pipe, materials.dark, [0.55, 0.16, 0.55], [-4.35, 1.3, -0.85]);
for (let y = 1.45; y < 7.15; y += 0.88) {
addBox(pipe, materials.pipe, [0.36, 0.84, 0.36], [-4.35, y, -0.85]);
addBox(pipe, materials.pipeJoint, [0.46, 0.12, 0.46], [-4.35, y + 0.42, -0.85]);
}
addBox(pipe, materials.pipe, [2.82, 0.34, 0.34], [-2.95, 7.28, -0.85]);
addBox(pipe, materials.pipeJoint, [0.45, 0.46, 0.46], [-1.56, 7.22, -0.85]);
addBox(pipe, materials.pipe, [0.34, 0.38, 0.34], [-1.56, 7.0, -0.85]);
// A small pump indicator rotates while the stream is active.
const pumpRotor = new THREE.Group();
pumpRotor.position.set(-4.35, 0.86, -0.31);
pipe.add(pumpRotor);
addBox(pumpRotor, materials.orange, [0.12, 0.72, 0.12], [0, 0, 0]);
addBox(pumpRotor, materials.orange, [0.72, 0.12, 0.12], [0, 0, 0]);
// -------------------------------------------------------------------------
// Water simulation
// -------------------------------------------------------------------------
const TOTAL_WATER = 20;
const INTAKE_VOLUME = 0.025;
const INTAKE_INTERVAL = 0.055;
const DRAIN_RATE = 2.4;
const INTAKE_ANGLE = THREE.MathUtils.degToRad(130);
const DRAIN_ANGLE = THREE.MathUtils.degToRad(-35);
const streamStart = new THREE.Vector3(-1.56, 6.91, -0.85);
let streamActive = true;
let trayWaterUnits = TOTAL_WATER;
let wheelAngle = 0;
let currentOmega = 0;
let intakeTimer = 0;
let elapsed = 0;
const intakeParticles = [];
const spillParticles = [];
const splashParticles = [];
const dropGeometry = new THREE.BoxGeometry(1, 1, 1);
function angleDistance(a, b) {
return Math.abs(Math.atan2(Math.sin(a - b), Math.cos(a - b)));
}
function nearestBucketTo(angle) {
let nearest = null;
let nearestDistance = Infinity;
for (const bucket of buckets) {
const distance = angleDistance(bucket.theta + wheelAngle, angle);
if (distance < nearestDistance) {
nearestDistance = distance;
nearest = bucket;
}
}
return { bucket: nearest, distance: nearestDistance };
}
function createDrop(material, size) {
const mesh = new THREE.Mesh(dropGeometry, material);
mesh.scale.setScalar(size);
mesh.castShadow = false;
mesh.receiveShadow = false;
scene.add(mesh);
return mesh;
}
function spawnIntakeDrop() {
if (trayWaterUnits < INTAKE_VOLUME) {
return;
}
const target = nearestBucketTo(INTAKE_ANGLE);
if (!target.bucket || target.distance > 0.4) {
return;
}
// Water leaves the tray and is carried by the fixed pump stream.
trayWaterUnits -= INTAKE_VOLUME;
const mesh = createDrop(materials.waterBright, 0.16);
mesh.position.copy(streamStart);
intakeParticles.push({
mesh,
bucket: target.bucket,
volume: INTAKE_VOLUME,
age: 0,
duration: 0.43,
jitter: (Math.random() - 0.5) * 0.09
});
}
function spawnSpill(bucket, volume) {
bucket.group.getWorldPosition(bucket.worldPosition);
const worldAngle = bucket.theta + wheelAngle;
const outward = new THREE.Vector3(
Math.cos(worldAngle),
Math.sin(worldAngle),
0
);
const start = bucket.worldPosition.clone().addScaledVector(outward, 0.28);
const target = new THREE.Vector3(
-4.8 + Math.random() * 9.6,
0,
-2.65 + Math.random() * 5.3
);
const mesh = createDrop(materials.waterBright, 0.12 + volume * 0.45);
mesh.position.copy(start);
spillParticles.push({
mesh,
volume,
start,
target,
age: 0,
duration: 0.82 + Math.random() * 0.34
});
}
function spawnSplash(position) {
for (let i = 0; i < 2; i++) {
const mesh = createDrop(materials.water, 0.09);
mesh.position.copy(position);
mesh.position.x += (Math.random() - 0.5) * 0.12;
mesh.position.z += (Math.random() - 0.5) * 0.12;
splashParticles.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 1.4,
1.1 + Math.random() * 1.0,
(Math.random() - 0.5) * 1.4
),
age: 0,
life: 0.48 + Math.random() * 0.16
});
}
}
function updateBuckets(dt) {
let fullestBucket = 0;
for (const bucket of buckets) {
const exactCells = bucket.fill * bucket.cells.length;
for (let i = 0; i < bucket.cells.length; i++) {
const fraction = THREE.MathUtils.clamp(exactCells - i, 0, 1);
const cell = bucket.cells[i];
cell.visible = fraction > 0.005;
cell.scale.y = Math.max(0.025, fraction * 0.17);
}
fullestBucket = Math.max(fullestBucket, bucket.fill);
const worldAngle = bucket.theta + wheelAngle;
const drainDistance = angleDistance(worldAngle, DRAIN_ANGLE);
// Water leaves the lower-right scoop and follows a ballistic path
// back into the tray. Bucket volume and returned volume stay equal.
if (drainDistance < 0.39 && bucket.fill > 0.0001) {
const amount = Math.min(bucket.fill, dt * DRAIN_RATE);
bucket.fill -= amount;
spawnSpill(bucket, amount);
}
}
// Labeled rule: omega = A(0.8 + 1.6F).
const A = streamActive ? 1 : 0;
currentOmega = A * (0.8 + 1.6 * fullestBucket);
document.querySelector("#fillValue").textContent = fullestBucket.toFixed(2);
document.querySelector("#omegaValue").textContent = `${currentOmega.toFixed(2)} rad/s`;
}
function updateParticles(dt) {
// Fixed intake stream.
for (let i = intakeParticles.length - 1; i >= 0; i--) {
const particle = intakeParticles[i];
particle.age += dt;
const u = THREE.MathUtils.clamp(particle.age / particle.duration, 0, 1);
particle.bucket.group.getWorldPosition(particle.bucket.worldPosition);
const target = particle.bucket.worldPosition;
const targetY = target.y - 0.04;
particle.mesh.position.x = THREE.MathUtils.lerp(
streamStart.x,
target.x + particle.jitter,
u
);
particle.mesh.position.z = THREE.MathUtils.lerp(
streamStart.z,
target.z + particle.jitter * 0.5,
u
);
particle.mesh.position.y =
streamStart.y + (targetY - streamStart.y) * u * u;
particle.mesh.rotation.x += dt * 3;
particle.mesh.rotation.y += dt * 4;
if (u >= 1) {
const bucket = particle.bucket;
const accepted = Math.min(particle.volume, Math.max(0, 1 - bucket.fill));
const overflow = particle.volume - accepted;
bucket.fill = Math.min(1, bucket.fill + accepted);
trayWaterUnits += overflow;
scene.remove(particle.mesh);
intakeParticles.splice(i, 1);
}
}
// Return spills from lower buckets into the tray.
for (let i = spillParticles.length - 1; i >= 0; i--) {
const particle = spillParticles[i];
particle.age += dt;
const u = THREE.MathUtils.clamp(particle.age / particle.duration, 0, 1);
const easedFall = u * u;
particle.mesh.position.x = THREE.MathUtils.lerp(
particle.start.x,
particle.target.x,
u
);
particle.mesh.position.z = THREE.MathUtils.lerp(
particle.start.z,
particle.target.z,
u
);
particle.mesh.position.y =
particle.start.y + (particle.target.y - particle.start.y) * easedFall;
particle.mesh.rotation.x += dt * 4;
particle.mesh.rotation.z += dt * 3;
if (u >= 1) {
trayWaterUnits += particle.volume;
spawnSplash(particle.mesh.position);
scene.remove(particle.mesh);
spillParticles.splice(i, 1);
}
}
// Tiny celebratory voxel splashes, carrying no additional water.
for (let i = splashParticles.length - 1; i >= 0; i--) {
const particle = splashParticles[i];
particle.age += dt;
particle.velocity.y -= 5.8 * dt;
particle.mesh.position.addScaledVector(particle.velocity, dt);
particle.mesh.rotation.x += dt * 5;
particle.mesh.rotation.z += dt * 4;
if (particle.age >= particle.life) {
scene.remove(particle.mesh);
splashParticles.splice(i, 1);
}
}
}
function updateTray() {
const level = THREE.MathUtils.clamp(trayWaterUnits / TOTAL_WATER, 0, 1);
trayWater.position.y = 0.52 + level * 0.24;
// A subtle voxel ripple, with no change in water quantity.
trayWaterTiles.forEach((tile, index) => {
tile.position.y =
Math.sin(elapsed * 1.7 + index * 0.37) * 0.012;
});
}
function clearParticles() {
for (const particle of intakeParticles) {
scene.remove(particle.mesh);
}
for (const particle of spillParticles) {
scene.remove(particle.mesh);
}
for (const particle of splashParticles) {
scene.remove(particle.mesh);
}
intakeParticles.length = 0;
spillParticles.length = 0;
splashParticles.length = 0;
}
function resetSimulation() {
clearParticles();
for (const bucket of buckets) {
bucket.fill = 0;
}
trayWaterUnits = TOTAL_WATER;
wheelAngle = 0;
intakeTimer = 0;
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const streamButton = document.querySelector("#streamButton");
const statusDot = document.querySelector("#statusDot");
const flowState = document.querySelector("#flowState");
function updateStreamUI() {
streamButton.classList.toggle("on", streamActive);
streamButton.textContent = streamActive
? "DEACTIVATE STREAM"
: "ACTIVATE STREAM";
streamButton.setAttribute("aria-pressed", String(streamActive));
statusDot.classList.toggle("on", streamActive);
flowState.textContent = streamActive ? "Stream active" : "Stream paused";
}
function toggleStream() {
streamActive = !streamActive;
updateStreamUI();
}
streamButton.addEventListener("click", toggleStream);
document.querySelector("#viewButton").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
toggleStream();
}
if (event.key.toLowerCase() === "r") {
resetSimulation();
}
});
updateStreamUI();
// -------------------------------------------------------------------------
// Direct camera controls: drag orbit, wheel/pinch zoom
// -------------------------------------------------------------------------
const pointers = new Map();
let lastPinchDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
lastPinchDistance = Math.hypot(a.x - b.x, a.y - b.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
});
if (pointers.size === 1) {
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.0045,
0.08,
0.95
);
updateCamera();
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (lastPinchDistance > 0) {
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * (lastPinchDistance / Math.max(distance, 1)),
9,
24
);
updateCamera();
}
lastPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
lastPinchDistance = 0;
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
9,
24
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
let previousTime = performance.now();
function animate(time) {
const dt = Math.min((time - previousTime) / 1000, 0.04);
previousTime = time;
elapsed += dt;
// Clockwise rotation.
wheelAngle -= currentOmega * dt;
wheel.rotation.z = wheelAngle;
wheel.updateMatrixWorld(true);
updateBuckets(dt);
if (streamActive) {
intakeTimer += dt;
while (intakeTimer >= INTAKE_INTERVAL) {
intakeTimer -= INTAKE_INTERVAL;
spawnIntakeDrop();
}
} else {
intakeTimer = 0;
}
updateParticles(dt);
updateTray();
pumpRotor.rotation.z -= dt * (streamActive ? 6.5 : 0);
materials.pump.emissiveIntensity = streamActive
? 0.55 + Math.sin(elapsed * 7) * 0.13
: 0.08;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```