Prompt specification
Build or customize the Three.js 3D scene "Ancient Egyptian basin irrigation gate" 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: Ancient Egyptian basin irrigation gate
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/ancient-egyptian-basin-irrigation-gate-x2302
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing ancient Egyptian agriculture on the Nile floodplain. Create a stylized earth embankment with a controllable opening between river channel and field basin. Show water entering only when the level difference permits flow. Low-poly.
Working Three.js Starter (ancient-egyptian-basin-irrigation-gate-x2302):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nile Floodplain — Ancient Egyptian Sluice</title>
<style>
:root {
--ink: #33271d;
--muted: #725e47;
--paper: rgba(255, 248, 228, 0.93);
--line: rgba(91, 64, 36, 0.22);
--blue: #207d8e;
--green: #657b35;
--gold: #c58b2b;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e7cfa0;
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.title {
position: fixed;
top: 22px;
left: 24px;
max-width: 430px;
padding: 15px 18px 14px;
background: var(--paper);
border: 1px solid var(--line);
border-left: 5px solid var(--gold);
box-shadow: 0 9px 28px rgba(62, 40, 20, 0.16);
pointer-events: none;
z-index: 5;
}
.title h1 {
margin: 0;
font-size: clamp(23px, 3vw, 34px);
line-height: 1;
letter-spacing: 0.02em;
font-weight: 600;
}
.title p {
margin: 8px 0 0;
color: var(--muted);
font: 12px/1.45 system-ui, sans-serif;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.panel {
position: fixed;
top: 22px;
right: 22px;
width: min(320px, calc(100vw - 44px));
padding: 17px;
background: var(--paper);
border: 1px solid var(--line);
box-shadow: 0 10px 32px rgba(62, 40, 20, 0.18);
z-index: 10;
}
.panel h2 {
margin: 0 0 12px;
font-size: 19px;
font-weight: 600;
}
.status {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 15px;
padding: 10px 11px;
border: 1px solid rgba(116, 91, 56, 0.2);
background: rgba(233, 211, 164, 0.42);
font: 700 12px/1.2 system-ui, sans-serif;
letter-spacing: 0.055em;
text-transform: uppercase;
}
.status-dot {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 50%;
background: #9b8060;
box-shadow: 0 0 0 3px rgba(155, 128, 96, 0.15);
}
.status.flowing {
color: #315b35;
background: rgba(108, 146, 69, 0.14);
border-color: rgba(78, 113, 50, 0.28);
}
.status.flowing .status-dot {
background: #5f8e38;
box-shadow: 0 0 0 3px rgba(95, 142, 56, 0.17);
}
.control { margin: 15px 0; }
.control label {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 7px;
font: 600 13px/1 system-ui, sans-serif;
}
output {
color: var(--blue);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 22px;
margin: 0;
accent-color: var(--blue);
cursor: pointer;
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 17px 0 13px;
}
.readout {
padding: 9px 10px;
background: rgba(255, 255, 255, 0.35);
border: 1px solid rgba(91, 64, 36, 0.13);
}
.readout span {
display: block;
color: var(--muted);
font: 10px/1.2 system-ui, sans-serif;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readout strong {
display: block;
margin-top: 4px;
font: 600 16px/1 system-ui, sans-serif;
font-variant-numeric: tabular-nums;
}
.note {
margin: 0;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font: 12px/1.48 system-ui, sans-serif;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 7px;
margin-top: 13px;
}
button {
min-height: 35px;
padding: 7px 8px;
border: 1px solid rgba(91, 64, 36, 0.28);
background: #efe0bd;
color: var(--ink);
font: 600 11px/1.1 system-ui, sans-serif;
cursor: pointer;
}
button:hover { background: #e5d1a5; }
button:active { transform: translateY(1px); }
.hint {
position: fixed;
left: 24px;
bottom: 20px;
padding: 8px 11px;
background: rgba(255, 248, 228, 0.8);
border: 1px solid rgba(91, 64, 36, 0.16);
color: var(--muted);
font: 11px/1.3 system-ui, sans-serif;
pointer-events: none;
z-index: 5;
}
.legend {
position: fixed;
right: 22px;
bottom: 20px;
display: flex;
gap: 14px;
padding: 8px 11px;
background: rgba(255, 248, 228, 0.8);
border: 1px solid rgba(91, 64, 36, 0.16);
color: var(--muted);
font: 11px/1 system-ui, sans-serif;
pointer-events: none;
z-index: 5;
}
.legend span::before {
content: "";
display: inline-block;
width: 9px;
height: 9px;
margin-right: 5px;
border-radius: 2px;
vertical-align: -1px;
}
.legend .water::before { background: #2e91a3; }
.legend .earth::before { background: #bd8242; }
.legend .crop::before { background: #c79c3c; }
@media (max-width: 720px) {
.title {
top: 12px;
left: 12px;
padding: 11px 13px;
max-width: calc(100vw - 24px);
}
.title p { display: none; }
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: min(310px, calc(100vw - 24px));
padding: 13px;
}
.panel h2 { font-size: 16px; }
.hint, .legend { display: none; }
.readouts { margin: 12px 0 10px; }
.note { font-size: 11px; }
}
</style>
</head>
<body>
<div class="title">
<h1>Nile Floodplain</h1>
<p>Ancient Egyptian basin irrigation · interactive sluice reconstruction</p>
</div>
<section class="panel" aria-label="Sluice controls">
<h2>Sluice gate controls</h2>
<div id="status" class="status" aria-live="polite">
<span class="status-dot"></span>
<span id="statusText">Preparing water system…</span>
</div>
<div class="control">
<label for="gate">
Gate opening
<output id="gateOutput" for="gate">35%</output>
</label>
<input id="gate" type="range" min="0" max="100" step="1" value="35">
</div>
<div class="control">
<label for="river">
Nile river stage
<output id="riverOutput" for="river">1.25 m</output>
</label>
<input id="river" type="range" min="0.05" max="2.20" step="0.01" value="1.25">
</div>
<div class="readouts">
<div class="readout">
<span>Basin level</span>
<strong id="basinValue">0.08 m</strong>
</div>
<div class="readout">
<span>Level difference</span>
<strong id="headValue">1.17 m</strong>
</div>
<div class="readout">
<span>Gate lift</span>
<strong id="liftValue">0.65 m</strong>
</div>
<div class="readout">
<span>Model flow</span>
<strong id="flowValue">0.00 m³/s</strong>
</div>
</div>
<p class="note">
Water enters only when the gate is raised, the river reaches the sill,
and the river level is higher than the basin level.
</p>
<div class="buttons">
<button id="drain" type="button">Drain basin</button>
<button id="pause" type="button">Pause</button>
<button id="reset" type="button">Reset</button>
</div>
</section>
<div class="hint">Drag to orbit · right-drag to pan · scroll to zoom</div>
<div class="legend" aria-hidden="true">
<span class="water">River / basin</span>
<span class="earth">Earth embankment</span>
<span class="crop">Crops</span>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9d5a9);
scene.fog = new THREE.Fog(0xe9d5a9, 30, 62);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional Nile floodplain. Drag to orbit and scroll to zoom."
);
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xfff4d5, 0x87613d, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe5a8, 3.0);
sun.position.set(-12, 22, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 22;
sun.shadow.camera.bottom = -22;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.00035;
scene.add(sun);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true,
...options
});
const mats = {
sand: material(0xc99554),
paleSand: material(0xd9b779),
darkSand: material(0x9d6537),
basinSoil: material(0xa97443),
stone: material(0xb69b72),
stoneDark: material(0x826849),
wood: material(0x68412a),
woodLight: material(0x9b6838),
rope: material(0x6e5839),
leaf: material(0x557239, { side: THREE.DoubleSide }),
leafLight: material(0x769146, { side: THREE.DoubleSide }),
wheat: material(0xd2a63d),
wheatLight: material(0xe2bc55),
skin: material(0x9b5e37),
linen: material(0xe8d8ad),
clay: material(0xa85f39),
water: material(0x258ca0, {
transparent: true,
opacity: 0.82,
roughness: 0.28,
metalness: 0.08,
depthWrite: false,
side: THREE.DoubleSide
}),
deepWater: material(0x176f82, {
transparent: true,
opacity: 0.86,
roughness: 0.35,
depthWrite: false
}),
flowWater: material(0x43a8b3, {
transparent: true,
opacity: 0.72,
roughness: 0.25,
depthWrite: false,
side: THREE.DoubleSide
})
};
function box(w, h, d, mat, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(a, b, radius, mat, parent = scene, radialSegments = 5) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 0.88, radius, length, radialSegments),
mat
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Deterministic pseudo-random values keep the scene stable between reloads.
let randomSeed = 1947;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
// -------------------------------------------------------------------------
// Terrain and floodplain
// -------------------------------------------------------------------------
box(27, 1.0, 22, mats.sand, 0, -0.67, 0);
box(8.25, 0.34, 20.2, mats.darkSand, -6.55, -0.28, 0);
box(3.0, 0.58, 20.4, mats.paleSand, -11.25, 0.08, 0);
box(2.7, 0.60, 20.4, mats.paleSand, 11.75, 0.08, 0);
// Raised agricultural basin floor.
box(9.0, 0.24, 14.5, mats.basinSoil, 6.0, -0.14, 0);
// Basin retaining walls.
box(0.38, 2.25, 14.7, mats.paleSand, 10.48, 0.98, 0);
box(9.2, 2.25, 0.38, mats.paleSand, 6.0, 0.98, -7.28);
box(9.2, 2.25, 0.38, mats.paleSand, 6.0, 0.98, 7.28);
function makeEmbankment(z0, z1) {
const ring = [
[-1.85, -0.18],
[0.95, -0.18],
[0.45, 2.82],
[-1.25, 2.82]
];
const positions = [];
for (const z of [z0, z1]) {
for (const [x, y] of ring) positions.push(x, y, z);
}
const indices = [
0, 1, 5, 0, 5, 4,
1, 2, 6, 1, 6, 5,
2, 3, 7, 2, 7, 6,
3, 0, 4, 3, 4, 7,
0, 3, 2, 0, 2, 1,
4, 5, 6, 4, 6, 7
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(
geometry,
material(0xb7773e, { side: THREE.DoubleSide })
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
makeEmbankment(-9.7, -1.55);
makeEmbankment(1.55, 9.7);
// Stone-lined sluice opening.
const SILL_Y = 0.22;
const MAX_GATE_LIFT = 1.85;
const BASIN_AREA = 9.0 * 14.5;
box(2.62, 0.22, 3.18, mats.stone, -0.43, SILL_Y - 0.11, 0);
box(2.65, 2.45, 0.30, mats.stone, -0.43, 1.02, -1.62);
box(2.65, 2.45, 0.30, mats.stone, -0.43, 1.02, 1.62);
// Timber guide frame.
box(0.48, 0.36, 3.72, mats.wood, -0.67, 4.28, 0);
box(0.25, 4.15, 0.25, mats.woodLight, -0.68, 2.15, -1.49);
box(0.25, 4.15, 0.25, mats.woodLight, -0.68, 2.15, 1.49);
const gateGroup = new THREE.Group();
scene.add(gateGroup);
box(0.24, 1.80, 2.76, mats.wood, 0, 0, 0, gateGroup);
for (let z = -1.15; z <= 1.15; z += 0.46) {
box(0.29, 1.83, 0.075, mats.woodLight, -0.015, 0, z, gateGroup);
}
box(0.30, 0.13, 2.80, mats.stoneDark, -0.015, -0.73, 0, gateGroup);
box(0.30, 0.13, 2.80, mats.stoneDark, -0.015, 0.73, 0, gateGroup);
// -------------------------------------------------------------------------
// Water surfaces
// -------------------------------------------------------------------------
const riverSurface = new THREE.Mesh(
new THREE.PlaneGeometry(7.95, 20.05),
mats.water
);
riverSurface.rotation.x = -Math.PI / 2;
riverSurface.position.set(-6.55, 1.25, 0);
riverSurface.receiveShadow = true;
riverSurface.renderOrder = 1;
scene.add(riverSurface);
const basinSurface = new THREE.Mesh(
new THREE.PlaneGeometry(8.8, 14.2),
mats.deepWater
);
basinSurface.rotation.x = -Math.PI / 2;
basinSurface.position.set(6.0, 0.08, 0);
basinSurface.receiveShadow = true;
basinSurface.renderOrder = 2;
scene.add(basinSurface);
// Moving highlights on the river.
const riverHighlights = [];
const highlightMat = new THREE.MeshBasicMaterial({
color: 0x91d2cf,
transparent: true,
opacity: 0.42,
depthWrite: false
});
for (let i = 0; i < 13; i++) {
const strip = new THREE.Mesh(
new THREE.BoxGeometry(0.65 + random() * 0.7, 0.018, 0.045),
highlightMat
);
strip.position.set(
-10.0 + random() * 6.8,
1.27,
-9.7 + random() * 19.4
);
strip.userData.speed = 0.22 + random() * 0.32;
scene.add(strip);
riverHighlights.push(strip);
}
// Expanding field ripples.
const ripples = [];
for (let i = 0; i < 12; i++) {
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.24, 0.28, 16),
new THREE.MeshBasicMaterial({
color: 0xb5e0d2,
transparent: true,
opacity: 0.25,
side: THREE.DoubleSide,
depthWrite: false
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(1.8 + random() * 8.3, 0.1, -6.5 + random() * 13);
ring.userData.phase = random();
ring.userData.speed = 0.22 + random() * 0.18;
ring.renderOrder = 3;
scene.add(ring);
ripples.push(ring);
}
// Flow pieces are visible only while a valid head difference exists.
const streamRamp = new THREE.Mesh(
new THREE.BoxGeometry(1.15, 0.055, 2.08),
mats.flowWater
);
streamRamp.renderOrder = 4;
scene.add(streamRamp);
const streamSheet = new THREE.Mesh(
new THREE.BoxGeometry(3.05, 0.055, 2.08),
mats.flowWater
);
streamSheet.renderOrder = 4;
scene.add(streamSheet);
const waterfall = new THREE.Mesh(
new THREE.PlaneGeometry(2.08, 1),
mats.flowWater
);
waterfall.rotation.y = Math.PI / 2;
waterfall.renderOrder = 4;
scene.add(waterfall);
const dropletCount = 38;
const droplets = new THREE.InstancedMesh(
new THREE.TetrahedronGeometry(0.085),
material(0x8bd7d4, {
transparent: true,
opacity: 0.88,
depthWrite: false
}),
dropletCount
);
droplets.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
droplets.frustumCulled = false;
droplets.renderOrder = 5;
scene.add(droplets);
const dummy = new THREE.Object3D();
const dropletData = Array.from({ length: dropletCount }, () => ({
phase: random(),
z: (random() - 0.5) * 1.9,
speed: 0.20 + random() * 0.18
}));
// -------------------------------------------------------------------------
// Crops and agricultural details
// -------------------------------------------------------------------------
const bedXs = [2.55, 4.35, 6.15, 7.95];
for (const x of bedXs) {
box(1.13, 0.34, 12.7, mats.darkSand, x, 0.15, 0);
}
const wheatGeometry = new THREE.ConeGeometry(0.12, 0.68, 5);
wheatGeometry.translate(0, 0.34, 0);
const wheat = new THREE.InstancedMesh(wheatGeometry, mats.wheat, 88);
let wheatIndex = 0;
for (const x of bedXs) {
for (let i = 0; i < 22; i++) {
dummy.position.set(
x + (random() - 0.5) * 0.55,
0.32,
-5.9 + i * 0.56 + (random() - 0.5) * 0.13
);
dummy.rotation.set(
(random() - 0.5) * 0.12,
random() * Math.PI,
(random() - 0.5) * 0.13
);
const scale = 0.78 + random() * 0.5;
dummy.scale.set(scale, scale, scale);
dummy.updateMatrix();
wheat.setMatrixAt(wheatIndex, dummy.matrix);
wheat.setColorAt(
wheatIndex,
new THREE.Color().setHSL(0.105 + random() * 0.025, 0.58, 0.48 + random() * 0.12)
);
wheatIndex++;
}
}
wheat.castShadow = true;
wheat.receiveShadow = true;
scene.add(wheat);
// Palm leaves are low-poly diamond shapes with a drooping tip.
function palmLeafGeometry(length) {
const positions = [
0, 0, 0,
length * 0.52, -0.12, -0.22,
length, -0.68, 0,
length * 0.52, -0.12, 0.22
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
geometry.setIndex([0, 1, 2, 0, 2, 3]);
geometry.computeVertexNormals();
return geometry;
}
function addPalm(x, z, scale = 1, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
group.scale.setScalar(scale);
scene.add(group);
const trunkPoints = [];
for (let i = 0; i < 6; i++) {
const t = i / 5;
trunkPoints.push(
new THREE.Vector3(Math.sin(t * 1.8) * 0.24, t * 4.25, Math.cos(t * 1.5) * 0.12)
);
}
for (let i = 0; i < trunkPoints.length - 1; i++) {
const r1 = 0.25 - i * 0.026;
const r2 = 0.25 - (i + 1) * 0.026;
const a = trunkPoints[i];
const b = trunkPoints[i + 1];
const direction = new THREE.Vector3().subVectors(b, a);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(r2, r1, direction.length(), 6),
i % 2 ? mats.wood : mats.woodLight
);
trunk.position.copy(a).add(b).multiplyScalar(0.5);
trunk.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
trunk.castShadow = true;
group.add(trunk);
}
const crown = trunkPoints[trunkPoints.length - 1];
for (let i = 0; i < 8; i++) {
const leaf = new THREE.Mesh(
palmLeafGeometry(2.35 + random() * 0.65),
i % 2 ? mats.leaf : mats.leafLight
);
leaf.position.copy(crown);
leaf.rotation.y = i * Math.PI / 4 + random() * 0.18;
leaf.rotation.z = -0.05 + random() * 0.14;
leaf.castShadow = true;
group.add(leaf);
}
const dates = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.32, 0),
material(0x9b6332)
);
dates.position.copy(crown).add(new THREE.Vector3(0, -0.25, 0));
dates.scale.set(1, 0.75, 1);
dates.castShadow = true;
group.add(dates);
}
addPalm(-10.8, -7.5, 1.02, 0.4);
addPalm(-10.5, 5.9, 0.82, -0.3);
addPalm(11.0, -8.9, 1.0, 0.8);
addPalm(11.0, 8.7, 0.86, -0.6);
addPalm(3.0, -8.8, 0.72, 0.1);
// Mud-brick farm building.
const house = new THREE.Group();
house.position.set(9.2, 0.2, -9.1);
house.rotation.y = -0.12;
scene.add(house);
box(2.25, 1.55, 1.65, mats.clay, 0, 0.78, 0, house);
box(2.42, 0.18, 1.82, mats.paleSand, 0, 1.62, 0, house);
box(0.58, 0.98, 0.08, mats.wood, 0.48, 0.49, 0.84, house);
const roof = new THREE.Mesh(
new THREE.ConeGeometry(1.72, 0.85, 4),
mats.paleSand
);
roof.position.y = 2.12;
roof.rotation.y = Math.PI / 4;
roof.castShadow = true;
house.add(roof);
// A shaduf beside the irrigated field.
const shaduf = new THREE.Group();
shaduf.position.set(9.0, 0.32, 6.0);
shaduf.rotation.y = 0.35;
scene.add(shaduf);
cylinderBetween(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 2.35, 0),
0.11,
mats.wood,
shaduf
);
cylinderBetween(
new THREE.Vector3(-0.95, 2.2, 0),
new THREE.Vector3(0.85, 2.2, 0),
0.085,
mats.woodLight,
shaduf
);
cylinderBetween(
new THREE.Vector3(0.62, 2.2, 0),
new THREE.Vector3(0.62, 1.15, 0),
0.025,
mats.rope,
shaduf
);
const bucket = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.19, 0.38, 7),
mats.woodLight
);
bucket.position.set(0.62, 0.98, 0);
bucket.castShadow = true;
shaduf.add(bucket);
// -------------------------------------------------------------------------
// Low-poly boat
// -------------------------------------------------------------------------
function makeBoat() {
const group = new THREE.Group();
const top = [
[-0.68, 0.25, -1.7],
[0.68, 0.25, -1.7],
[0.92, 0.25, 0.65],
[0.58, 0.25, 1.85],
[-0.58, 0.25, 1.85],
[-0.92, 0.25, 0.65]
];
const bottom = [
[-0.43, -0.24, -1.45],
[0.43, -0.24, -1.45],
[0.62, -0.24, 0.62],
[0.39, -0.24, 1.56],
[-0.39, -0.24, 1.56],
[-0.62, -0.24, 0.62]
];
const positions = [];
for (const p of top) positions.push(...p);
for (const p of bottom) positions.push(...p);
const indices = [];
for (let i = 0; i < 6; i++) {
const j = (i + 1) % 6;
indices.push(i, j, j + 6, i, j + 6, i + 6);
}
indices.push(6, 7, 8, 6, 8, 9, 6, 9, 10, 6, 10, 11);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const hull = new THREE.Mesh(geometry, mats.wood);
hull.castShadow = true;
hull.receiveShadow = true;
group.add(hull);
box(1.15, 0.09, 2.75, mats.woodLight, 0, 0.26, 0, group);
const mast = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.055, 2.75, 6),
mats.wood
);
mast.position.set(0, 1.6, -0.15);
mast.castShadow = true;
group.add(mast);
const sail = new THREE.Mesh(
new THREE.PlaneGeometry(1.9, 1.65),
material(0xe6d3a0, { side: THREE.DoubleSide })
);
sail.position.set(0, 1.72, -0.13);
sail.castShadow = true;
group.add(sail);
for (const x of [-0.36, 0.36]) {
const jar = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.21, 0.43, 7),
mats.clay
);
jar.position.set(x, 0.52, 1.02);
jar.castShadow = true;
group.add(jar);
}
return group;
}
const boat = makeBoat();
boat.position.set(-7.4, 1.1, 2.4);
boat.rotation.y = -0.24;
scene.add(boat);
// -------------------------------------------------------------------------
// Stylized people
// -------------------------------------------------------------------------
function addPerson(x, y, z, rotation = 0, scale = 1) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.rotation.y = rotation;
group.scale.setScalar(scale);
scene.add(group);
const leftLeg = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.11, 0.66, 5),
mats.skin
);
leftLeg.position.set(-0.13, 0.35, 0);
leftLeg.castShadow = true;
group.add(leftLeg);
const rightLeg = leftLeg.clone();
rightLeg.position.x = 0.13;
group.add(rightLeg);
const torso = new THREE.Mesh(
new THREE.CylinderGeometry(0.21, 0.28, 0.66, 7),
mats.linen
);
torso.position.y = 0.98;
torso.castShadow = true;
group.add(torso);
const head = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.22, 0),
mats.skin
);
head.position.y = 1.53;
head.castShadow = true;
group.add(head);
const hair = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.225, 0),
material(0x3c2a1e)
);
hair.position.set(0, 1.61, -0.02);
hair.scale.set(1, 0.58, 1);
group.add(hair);
cylinderBetween(
new THREE.Vector3(-0.2, 1.18, 0),
new THREE.Vector3(-0.39, 0.78, 0.08),
0.065,
mats.skin,
group
);
cylinderBetween(
new THREE.Vector3(0.2, 1.18, 0),
new THREE.Vector3(0.42, 1.42, 0.16),
0.065,
mats.skin,
group
);
return group;
}
const gateWorker = addPerson(1.35, 0.18, 2.65, -0.4, 0.9);
const fieldWorker = addPerson(8.7, 0.33, -3.7, 1.2, 0.78);
// Distant low-poly pyramids establish the Egyptian setting.
const pyramidMaterial = material(0xc99c5c);
function addPyramid(x, z, radius, height, rotation) {
const pyramid = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, 4),
pyramidMaterial
);
pyramid.position.set(x, height / 2 - 0.15, z);
pyramid.rotation.y = rotation;
pyramid.castShadow = true;
scene.add(pyramid);
}
addPyramid(-7.5, -16.0, 4.4, 4.2, Math.PI / 4);
addPyramid(-1.0, -18.0, 3.4, 3.1, Math.PI / 4);
addPyramid(6.5, -17.2, 3.0, 2.6, Math.PI / 4);
// -------------------------------------------------------------------------
// Simulation state and controls
// -------------------------------------------------------------------------
const state = {
gateOpen: 0.35,
riverLevel: 1.25,
basinLevel: 0.08,
paused: false,
flowRate: 0,
canFlow: false,
effectiveOpening: 0,
head: 0,
apertureY: SILL_Y
};
const gateInput = document.querySelector("#gate");
const riverInput = document.querySelector("#river");
const gateOutput = document.querySelector("#gateOutput");
const riverOutput = document.querySelector("#riverOutput");
const basinValue = document.querySelector("#basinValue");
const headValue = document.querySelector("#headValue");
const liftValue = document.querySelector("#liftValue");
const flowValue = document.querySelector("#flowValue");
const status = document.querySelector("#status");
const statusText = document.querySelector("#statusText");
const pauseButton = document.querySelector("#pause");
gateInput.addEventListener("input", () => {
state.gateOpen = Number(gateInput.value) / 100;
gateOutput.textContent = `${gateInput.value}%`;
});
riverInput.addEventListener("input", () => {
state.riverLevel = Number(riverInput.value);
riverOutput.textContent = `${state.riverLevel.toFixed(2)} m`;
});
document.querySelector("#drain").addEventListener("click", () => {
state.basinLevel = Math.max(-0.015, state.basinLevel - 0.32);
});
pauseButton.addEventListener("click", () => {
state.paused = !state.paused;
pauseButton.textContent = state.paused ? "Resume" : "Pause";
});
document.querySelector("#reset").addEventListener("click", () => {
state.gateOpen = 0.35;
state.riverLevel = 1.25;
state.basinLevel = 0.08;
state.paused = false;
gateInput.value = "35";
riverInput.value = "1.25";
gateOutput.textContent = "35%";
riverOutput.textContent = "1.25 m";
pauseButton.textContent = "Pause";
});
// -------------------------------------------------------------------------
// Custom camera controls — no examples or addon modules are used.
// -------------------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(-0.25, 0.75, 0),
radius: 28,
theta: -0.66,
phi: 1.03
};
function updateCamera() {
const spherical = new THREE.Spherical(
orbit.radius,
orbit.phi,
orbit.theta
);
camera.position.setFromSpherical(spherical).add(orbit.target);
camera.lookAt(orbit.target);
}
updateCamera();
const activePointers = new Map();
let dragMode = "orbit";
let previousX = 0;
let previousY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.focus();
canvas.setPointerCapture(event.pointerId);
activePointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (activePointers.size === 1) {
dragMode = event.button === 2 ? "pan" : "orbit";
previousX = event.clientX;
previousY = event.clientY;
}
});
canvas.addEventListener("pointermove", event => {
if (!activePointers.has(event.pointerId)) return;
const old = activePointers.get(event.pointerId);
const dx = event.clientX - old.x;
const dy = event.clientY - old.y;
old.x = event.clientX;
old.y = event.clientY;
if (activePointers.size === 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (canvas._pinchDistance) {
orbit.radius *= canvas._pinchDistance / distance;
orbit.radius = THREE.MathUtils.clamp(orbit.radius, 13, 48);
}
canvas._pinchDistance = distance;
return;
}
if (dragMode === "orbit") {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.38, 1.42);
} else {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3().crossVectors(forward, camera.up).normalize();
const scale = orbit.radius * 0.00135;
orbit.target.addScaledVector(right, -dx * scale);
orbit.target.addScaledVector(camera.up, dy * scale);
}
updateCamera();
});
function endPointer(event) {
activePointers.delete(event.pointerId);
if (activePointers.size < 2) canvas._pinchDistance = 0;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
orbit.radius *= Math.exp(event.deltaY * 0.001);
orbit.radius = THREE.MathUtils.clamp(orbit.radius, 13, 48);
updateCamera();
}, { passive: false });
canvas.addEventListener("keydown", event => {
const step = 0.08;
if (event.key === "ArrowLeft") orbit.theta += step;
else if (event.key === "ArrowRight") orbit.theta -= step;
else if (event.key === "ArrowUp") orbit.phi = Math.max(0.38, orbit.phi - step);
else if (event.key === "ArrowDown") orbit.phi = Math.min(1.42, orbit.phi + step);
else return;
event.preventDefault();
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Hydraulic model
// -------------------------------------------------------------------------
function simulate(dt) {
const gateLift = state.gateOpen * MAX_GATE_LIFT;
const submergence = Math.max(0, state.riverLevel - SILL_Y);
const effectiveOpening = Math.min(gateLift, submergence);
const head = state.riverLevel - state.basinLevel;
const valid =
state.gateOpen > 0.005 &&
effectiveOpening > 0.01 &&
head > 0.015 &&
state.riverLevel > SILL_Y + 0.005;
// A square-root head relationship makes the flow diminish as the basin fills.
const flowRate = valid
? 8.8 * effectiveOpening * Math.sqrt(Math.max(0, head))
: 0;
if (valid) {
state.basinLevel += flowRate * dt / BASIN_AREA;
}
state.flowRate = flowRate;
state.canFlow = valid;
state.effectiveOpening = effectiveOpening;
state.head = head;
state.apertureY = SILL_Y + effectiveOpening * 0.52;
}
function updateStatus() {
const lift = state.gateOpen * MAX_GATE_LIFT;
basinValue.textContent = `${state.basinLevel.toFixed(2)} m`;
headValue.textContent = `${Math.max(0, state.head).toFixed(2)} m`;
liftValue.textContent = `${lift.toFixed(2)} m`;
flowValue.textContent = `${state.flowRate.toFixed(2)} m³/s`;
status.classList.toggle("flowing", state.canFlow);
if (state.gateOpen <= 0.005) {
statusText.textContent = "Gate closed";
} else if (state.riverLevel <= SILL_Y + 0.01) {
statusText.textContent = "River below gate sill";
} else if (state.head <= 0.015) {
statusText.textContent = "No level difference";
} else if (state.effectiveOpening <= 0.01) {
statusText.textContent = "No opening at waterline";
} else {
statusText.textContent = "Water entering basin";
}
}
// -------------------------------------------------------------------------
// Animation and visual updates
// -------------------------------------------------------------------------
function updateVisuals(elapsed) {
const lift = state.gateOpen * MAX_GATE_LIFT;
gateGroup.position.set(-0.68, SILL_Y + lift + 0.90, 0);
riverSurface.position.y = state.riverLevel;
basinSurface.position.y = state.basinLevel;
basinSurface.visible = state.basinLevel > -0.01;
for (const strip of riverHighlights) {
strip.position.y = state.riverLevel + 0.018;
strip.position.z += strip.userData.speed * 0.016;
if (strip.position.z > 10.2) strip.position.z = -10.2;
}
for (const ring of ripples) {
ring.visible = state.basinLevel > 0.015;
ring.position.y = state.basinLevel + 0.018;
const phase = (elapsed * ring.userData.speed + ring.userData.phase) % 1;
const scale = 0.5 + phase * 2.5;
ring.scale.setScalar(scale);
ring.material.opacity = (1 - phase) * 0.24;
}
boat.position.y = state.riverLevel - 0.16;
boat.rotation.z = Math.sin(elapsed * 0.85) * 0.018;
boat.rotation.x = Math.cos(elapsed * 0.62) * 0.012;
gateWorker.rotation.z = Math.sin(elapsed * 1.2) * 0.015;
fieldWorker.rotation.z = Math.sin(elapsed * 1.1 + 2) * 0.018;
const visible = state.canFlow && state.flowRate > 0.001;
streamRamp.visible = visible;
streamSheet.visible = visible;
waterfall.visible = visible;
droplets.visible = visible;
if (visible) {
const apertureY = state.apertureY;
const riverY = state.riverLevel;
streamRamp.position.set(
-1.94,
(riverY + apertureY) * 0.5,
0
);
streamRamp.rotation.z = Math.atan2(apertureY - riverY, 1.15);
streamSheet.position.set(0.02, apertureY, 0);
const fallHeight = Math.max(0.03, apertureY - state.basinLevel);
waterfall.position.set(1.50, (apertureY + state.basinLevel) * 0.5, 0);
waterfall.scale.set(1, fallHeight, 1);
const flowStrength = THREE.MathUtils.clamp(
state.flowRate / 10,
0.15,
1.4
);
for (let i = 0; i < dropletCount; i++) {
const data = dropletData[i];
const t = (data.phase + elapsed * data.speed * flowStrength) % 1;
const x = -2.45 + t * 4.02;
let y;
if (t < 0.24) {
y = THREE.MathUtils.lerp(riverY - 0.03, apertureY, t / 0.24);
} else if (t < 0.76) {
y = apertureY;
} else {
const fall = (t - 0.76) / 0.24;
y = THREE.MathUtils.lerp(apertureY, state.basinLevel + 0.025, fall);
}
dummy.position.set(x, y, data.z);
dummy.rotation.set(elapsed * 3 + i, elapsed * 2, 0);
const s = 0.65 + flowStrength * 0.35;
dummy.scale.set(s, s, s);
dummy.updateMatrix();
droplets.setMatrixAt(i, dummy.matrix);
}
droplets.instanceMatrix.needsUpdate = true;
}
updateStatus();
}
let previousTime = performance.now();
let elapsed = 0;
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(0.05, (now - previousTime) / 1000);
previousTime = now;
if (!state.paused) {
elapsed += dt;
simulate(dt);
}
updateVisuals(elapsed);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```