Prompt specification
Build or customize the Three.js 3D scene "Two fields sharing an irrigation branch" 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: Two fields sharing an irrigation branch
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-fields-sharing-an-irrigation-branch-x2366
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts irrigation in a medieval Japanese farming village. Create a historically informed Japanese farming vignette with two paddy plots and one shared channel. Animate water entering through separate controllable openings. Low-poly.
Working Three.js Starter (two-fields-sharing-an-irrigation-branch-x2366):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Medieval Japanese Irrigation Village</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(24, 30, 27, 0.88);
--line: rgba(235, 225, 185, 0.22);
--gold: #d7bd78;
--text: #eee6cc;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b8c8bf;
font-family: Georgia, "Times New Roman", serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#ui {
position: fixed;
top: 18px;
left: 18px;
width: min(330px, calc(100vw - 36px));
padding: 18px 18px 16px;
color: var(--text);
background: var(--panel);
border: 1px solid var(--line);
border-radius: 12px;
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(10px);
user-select: none;
}
h1 {
margin: 0 0 5px;
font-size: 21px;
line-height: 1.15;
letter-spacing: 0.02em;
color: var(--gold);
}
.subtitle {
margin: 0 0 14px;
color: #c7c7af;
font-size: 12px;
line-height: 1.45;
}
.hint {
margin: 0 0 15px;
color: #aeb8ac;
font-size: 11px;
line-height: 1.4;
}
.control {
padding: 10px 0 12px;
border-top: 1px solid var(--line);
}
.control-header {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 10px;
margin-bottom: 7px;
font-size: 13px;
}
.control-header strong {
font-weight: normal;
color: #f0e5bd;
}
output {
min-width: 42px;
text-align: right;
color: #9dd2cb;
font-family: Arial, sans-serif;
font-size: 12px;
}
input[type="range"] {
width: 100%;
accent-color: #78b9b1;
cursor: pointer;
}
.buttons {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 5px;
}
button {
flex: 1;
min-width: 92px;
padding: 8px 9px;
border: 1px solid rgba(215, 189, 120, 0.45);
border-radius: 6px;
color: var(--text);
background: rgba(215, 189, 120, 0.12);
font: 12px Arial, sans-serif;
cursor: pointer;
transition: background 0.15s ease, transform 0.15s ease;
}
button:hover {
background: rgba(215, 189, 120, 0.25);
}
button:active {
transform: translateY(1px);
}
#status {
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid var(--line);
color: #b8d7cf;
font: 11px/1.45 Arial, sans-serif;
}
#legend {
position: fixed;
right: 18px;
bottom: 18px;
padding: 9px 12px;
color: #e8e2c9;
background: rgba(24, 30, 27, 0.72);
border: 1px solid var(--line);
border-radius: 8px;
font: 11px/1.5 Arial, sans-serif;
pointer-events: none;
}
.swatch {
display: inline-block;
width: 10px;
height: 10px;
margin-right: 6px;
border-radius: 2px;
vertical-align: -1px;
}
.water-swatch {
background: #5da8b4;
}
.wood-swatch {
background: #70452d;
}
@media (max-width: 600px) {
#ui {
top: 10px;
left: 10px;
padding: 14px;
}
#legend {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="ui">
<h1>Irrigation in a Medieval Japanese Village</h1>
<p class="subtitle">
Two flooded rice paddies share a stone-lined channel fed by a mountain stream.
</p>
<p class="hint">
Drag the scene to orbit. Use the mouse wheel to zoom. Raise or lower each
wooden sluice gate independently.
</p>
<div class="control">
<div class="control-header">
<strong>West paddy sluice</strong>
<output id="leftValue">68%</output>
</div>
<input id="leftGate" type="range" min="0" max="100" value="68"
aria-label="West paddy sluice opening" />
</div>
<div class="control">
<div class="control-header">
<strong>East paddy sluice</strong>
<output id="rightValue">38%</output>
</div>
<input id="rightGate" type="range" min="0" max="100" value="38"
aria-label="East paddy sluice opening" />
</div>
<div class="buttons">
<button id="openBoth">Open both</button>
<button id="closeBoth">Close both</button>
<button id="resetView">Reset view</button>
</div>
<div id="status">
Stream flow: steady<br />
West gate opening 68% · East gate opening 38%
</div>
</div>
<div id="legend">
<span class="swatch water-swatch"></span>irrigation water
<span class="swatch wood-swatch"></span>timber sluices
</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 scene = new THREE.Scene();
scene.background = new THREE.Color(0xbccdc2);
scene.fog = new THREE.Fog(0xbccdc2, 18, 36);
const camera = new THREE.PerspectiveCamera(
46,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
document.body.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xe9f3e4, 0x5b5140, 1.65);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe7b5, 2.15);
sun.position.set(-8, 15, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -16;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8fb9c7, 0.45);
fill.position.set(10, 6, -10);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and helpers
// -------------------------------------------------------------------------
function material(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
flatShading: true,
roughness: 0.85,
...extra
});
}
const M = {
earth: material(0x756b50),
soil: material(0x65553d),
mud: material(0x4e4433),
stone: material(0x6e766e),
stoneLight: material(0x858b7e),
wood: material(0x70452d),
darkWood: material(0x3e2c23),
plaster: material(0xd4c49d),
shoji: material(0xe7d9b2),
roof: material(0x354b4b, { side: THREE.DoubleSide }),
rice: material(0x507d3e),
riceLeaf: material(0x6f9b48),
bamboo: material(0x668c4f),
leaf: material(0x3e6540),
mountain: material(0x728278),
mountainLight: material(0x87958a),
water: material(0x559eaa, {
transparent: true,
opacity: 0.74,
roughness: 0.22,
metalness: 0.05
})
};
function addMesh(geometry, mat, x = 0, y = 0, z = 0, parent = scene) {
const object = new THREE.Mesh(geometry, mat);
object.position.set(x, y, z);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function box(w, h, d, x, y, z, mat, parent = scene) {
return addMesh(
new THREE.BoxGeometry(w, h, d),
mat,
x,
y,
z,
parent
);
}
function cylinder(rTop, rBottom, h, x, y, z, mat, parent = scene, segments = 8) {
return addMesh(
new THREE.CylinderGeometry(rTop, rBottom, h, segments),
mat,
x,
y,
z,
parent
);
}
function cone(radius, height, x, y, z, mat, parent = scene, segments = 6) {
return addMesh(
new THREE.ConeGeometry(radius, height, segments),
mat,
x,
y,
z,
parent
);
}
const waterMeshes = [];
const centralFlow = [];
const branchFlow = [];
const ripples = [];
// -------------------------------------------------------------------------
// Low ground, paths, and distant terrain
// -------------------------------------------------------------------------
box(30, 0.6, 27, 0, -0.55, 0, M.earth);
// A rough stone path approaches the houses from the front.
for (let i = 0; i < 11; i++) {
const x = -8.0 + Math.sin(i * 0.55) * 0.42;
const z = 7.8 - i * 1.05;
box(1.15, 0.12, 0.72, x, -0.17, z, M.stoneLight);
}
// Distant low-poly mountains.
const mountainData = [
[-12, -15, 4.8, 8.5],
[-7.2, -16, 3.8, 6.8],
[-2.8, -15.2, 4.6, 9.2],
[2.8, -16.0, 4.0, 7.7],
[8.2, -15.2, 5.0, 9.0],
[13.0, -16.5, 4.2, 8.0]
];
mountainData.forEach(([x, z, radius, height], index) => {
const mountain = cone(
radius,
height,
x,
height / 2 - 0.35,
z,
index % 2 ? M.mountainLight : M.mountain,
scene,
5
);
mountain.rotation.y = index * 0.71;
mountain.scale.z = 0.72;
});
// A bamboo grove behind the eastern house.
for (let i = 0; i < 14; i++) {
const x = 8.3 + (i % 5) * 0.38;
const z = -9.2 - Math.floor(i / 5) * 0.52;
const height = 2.0 + (i % 4) * 0.32;
cylinder(
0.055,
0.075,
height,
x,
height / 2 - 0.1,
z,
M.bamboo,
scene,
5
);
if (i % 2 === 0) {
const leaf = cone(
0.22,
0.8,
x + 0.12,
height - 0.25,
z,
M.leaf,
scene,
4
);
leaf.rotation.z = -0.65;
}
}
// -------------------------------------------------------------------------
// Water surfaces with animated vertices
// -------------------------------------------------------------------------
function makeWater(width, depth, x, z, phase = 0, amplitude = 0.012) {
const segmentsX = Math.max(3, Math.round(width * 3));
const segmentsZ = Math.max(3, Math.round(depth * 3));
const geometry = new THREE.PlaneGeometry(
width,
depth,
segmentsX,
segmentsZ
);
geometry.rotateX(-Math.PI / 2);
const water = new THREE.Mesh(geometry, M.water.clone());
water.position.set(x, 0.045, z);
water.receiveShadow = false;
scene.add(water);
waterMeshes.push({ water, phase, amplitude });
return water;
}
// -------------------------------------------------------------------------
// Two flooded rice paddies
// -------------------------------------------------------------------------
const BRANCH_Z = -1.0;
function buildPaddy(centerX) {
const width = 6.0;
const depth = 7.4;
const centerZ = 1.0;
const outerX = centerX < 0 ? centerX - width / 2 : centerX + width / 2;
const innerX = centerX < 0 ? centerX + width / 2 : centerX - width / 2;
// Wet paddy floor.
box(width, 0.16, depth, centerX, -0.12, centerZ, M.soil);
// Water.
makeWater(width - 0.34, depth - 0.34, centerX, centerZ, centerX, 0.013);
// Earthen banks around the outer sides and front/back.
box(0.36, 0.32, depth, outerX, 0.04, centerZ, M.mud);
box(width + 0.35, 0.32, 0.36, centerX, 0.04, centerZ - depth / 2, M.mud);
box(width + 0.35, 0.32, 0.36, centerX, 0.04, centerZ + depth / 2, M.mud);
// Leave a gap in the inner bank for the sluice branch.
const backLimit = BRANCH_Z - 0.48;
const frontLimit = BRANCH_Z + 0.48;
const backEdge = centerZ - depth / 2;
const frontEdge = centerZ + depth / 2;
const backLength = backLimit - backEdge;
const frontLength = frontEdge - frontLimit;
box(
0.36,
0.32,
backLength,
innerX,
0.04,
backEdge + backLength / 2,
M.mud
);
box(
0.36,
0.32,
frontLength,
innerX,
0.04,
frontLimit + frontLength / 2,
M.mud
);
// Rice seedlings, arranged in rows and rendered with instancing.
const columns = 11;
const rows = 13;
const count = columns * rows;
const stemGeometry = new THREE.ConeGeometry(0.055, 0.44, 4);
const stemMesh = new THREE.InstancedMesh(stemGeometry, M.rice, count);
stemMesh.castShadow = true;
stemMesh.receiveShadow = true;
scene.add(stemMesh);
const leafGeometry = new THREE.ConeGeometry(0.105, 0.40, 3);
const leafMesh = new THREE.InstancedMesh(
leafGeometry,
M.riceLeaf,
count * 2
);
leafMesh.castShadow = true;
scene.add(leafMesh);
const dummy = new THREE.Object3D();
let stemIndex = 0;
let leafIndex = 0;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < columns; col++) {
const x = centerX - 2.45 + col * 0.49;
const z = -2.2 + row * 0.54;
const heightScale = 0.86 + ((row * 7 + col * 3) % 5) * 0.055;
dummy.position.set(x, 0.23, z);
dummy.rotation.set(0, (row + col) * 0.37, 0);
dummy.scale.set(1, heightScale, 1);
dummy.updateMatrix();
stemMesh.setMatrixAt(stemIndex++, dummy.matrix);
for (let side = -1; side <= 1; side += 2) {
dummy.position.set(x + side * 0.035, 0.22, z);
dummy.rotation.set(0.08, side * 0.3, side * 0.24);
dummy.scale.set(0.85, heightScale * 0.9, 0.85);
dummy.updateMatrix();
leafMesh.setMatrixAt(leafIndex++, dummy.matrix);
}
}
}
stemMesh.instanceMatrix.needsUpdate = true;
leafMesh.instanceMatrix.needsUpdate = true;
}
buildPaddy(-3.7);
buildPaddy(3.7);
// -------------------------------------------------------------------------
// One shared irrigation channel
// -------------------------------------------------------------------------
const CHANNEL_BACK = -5.5;
const CHANNEL_FRONT = 5.7;
const CHANNEL_LENGTH = CHANNEL_FRONT - CHANNEL_BACK;
const CHANNEL_CENTER = (CHANNEL_FRONT + CHANNEL_BACK) / 2;
box(1.38, 0.18, CHANNEL_LENGTH, 0, -0.13, CHANNEL_CENTER, M.stone);
makeWater(
0.92,
CHANNEL_LENGTH - 0.15,
0,
CHANNEL_CENTER,
1.7,
0.018
);
// Side walls are split to leave actual openings for the two branch sluices.
const wallRanges = [
[CHANNEL_BACK, BRANCH_Z - 0.52],
[BRANCH_Z + 0.52, CHANNEL_FRONT]
];
[-0.69, 0.69].forEach((x) => {
wallRanges.forEach(([start, end]) => {
const length = end - start;
box(
0.24,
0.45,
length,
x,
0.12,
start + length / 2,
M.stone
);
});
});
// The stream intake at the mountain end.
box(1.05, 0.18, 1.8, 0, 0.18, -6.25, M.stone);
makeWater(0.78, 1.9, 0, -6.15, 4.0, 0.018);
for (let i = 0; i < 8; i++) {
const side = i % 2 ? -1 : 1;
const rock = addMesh(
new THREE.DodecahedronGeometry(0.25 + (i % 3) * 0.07, 0),
M.stoneLight,
side * (0.7 + (i % 2) * 0.22),
0.02,
-6.9 + Math.floor(i / 2) * 0.48
);
rock.rotation.set(i * 0.2, i * 0.7, i * 0.3);
rock.scale.y = 0.65;
}
// -------------------------------------------------------------------------
// Independent wooden sluice gates
// -------------------------------------------------------------------------
const gates = {};
function buildBranch(sign, sideName) {
const branchLength = 1.4;
const branchCenter = sign * 0.7;
// Stone branch trough.
box(
branchLength,
0.16,
0.84,
branchCenter,
-0.12,
BRANCH_Z,
M.stone
);
box(
1.18,
0.24,
0.13,
sign * 0.78,
0.04,
BRANCH_Z - 0.43,
M.stone
);
box(
1.18,
0.24,
0.13,
sign * 0.78,
0.04,
BRANCH_Z + 0.43,
M.stone
);
const branchWater = makeWater(
1.28,
0.56,
branchCenter,
BRANCH_Z,
sign > 0 ? 2.8 : 3.5,
0.01
);
branchWater.material.opacity = 0.5;
// Gate frame.
const gateX = sign * 0.35;
box(0.18, 1.24, 0.16, gateX, 0.56, BRANCH_Z - 0.5, M.darkWood);
box(0.18, 1.24, 0.16, gateX, 0.56, BRANCH_Z + 0.5, M.darkWood);
box(0.22, 0.15, 1.18, gateX, 1.18, BRANCH_Z, M.wood);
// The board is lifted vertically as the opening increases.
const board = box(
0.13,
0.78,
0.88,
gateX,
0.39,
BRANCH_Z,
M.wood
);
// Small handle on the gate.
box(0.34, 0.08, 0.10, gateX, 0.08, BRANCH_Z, M.darkWood);
gates[sideName] = {
board,
water: branchWater,
sign,
current: 0,
target: 0
};
// Flowing highlights in the branch.
const flowMaterial = new THREE.MeshBasicMaterial({
color: 0xc5eee8,
transparent: true,
opacity: 0.62
});
for (let i = 0; i < 8; i++) {
const flow = new THREE.Mesh(
new THREE.BoxGeometry(0.25, 0.016, 0.055),
flowMaterial
);
flow.position.set(
sign * (0.48 + (i / 8) * 0.82),
0.085,
BRANCH_Z + (Math.random() - 0.5) * 0.25
);
flow.userData.offset = i / 8;
flow.userData.speed = 0.25 + Math.random() * 0.14;
flow.castShadow = false;
scene.add(flow);
branchFlow.push(flow);
}
}
buildBranch(-1, "left");
buildBranch(1, "right");
// Moving highlights in the shared channel.
const centralFlowMaterial = new THREE.MeshBasicMaterial({
color: 0xb9e8e4,
transparent: true,
opacity: 0.5
});
for (let i = 0; i < 26; i++) {
const flow = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 0.016, 0.28),
centralFlowMaterial
);
flow.position.set(
(Math.random() - 0.5) * 0.55,
0.085,
CHANNEL_BACK + Math.random() * CHANNEL_LENGTH
);
flow.userData.offset = Math.random();
flow.userData.speed = 0.55 + Math.random() * 0.42;
flow.castShadow = false;
scene.add(flow);
centralFlow.push(flow);
}
// Small ripples on the paddy surfaces.
const rippleGeometry = new THREE.RingGeometry(0.11, 0.135, 12);
rippleGeometry.rotateX(-Math.PI / 2);
for (let i = 0; i < 16; i++) {
const rippleMaterial = new THREE.MeshBasicMaterial({
color: 0xd2f2e9,
transparent: true,
opacity: 0.2,
side: THREE.DoubleSide,
depthWrite: false
});
const ripple = new THREE.Mesh(rippleGeometry, rippleMaterial);
const west = i < 8;
ripple.position.set(
(west ? -3.7 : 3.7) + (Math.random() - 0.5) * 4.8,
0.071,
-1.8 + Math.random() * 5.9
);
ripple.userData.offset = Math.random();
scene.add(ripple);
ripples.push(ripple);
}
// -------------------------------------------------------------------------
// Traditional Japanese-style farm buildings
// -------------------------------------------------------------------------
function gableRoofGeometry(width, depth, eaveY, ridgeY) {
const x0 = -width / 2;
const x1 = width / 2;
const z0 = -depth / 2;
const z1 = depth / 2;
const positions = [
x0, eaveY, z0,
0, ridgeY, z0,
0, ridgeY, z1,
x0, eaveY, z1,
0, ridgeY, z0,
x1, eaveY, z0,
x1, eaveY, z1,
0, ridgeY, z1
];
const indices = [
0, 1, 2,
0, 2, 3,
4, 5, 6,
4, 6, 7
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function buildHouse(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
// Stone foundation and wooden veranda.
box(4.35, 0.25, 3.65, 0, -0.1, 0, M.stone, group);
box(4.1, 0.18, 0.55, 0, 0.05, 1.67, M.wood, group);
// Plastered walls.
box(4.0, 2.0, 3.0, 0, 1.05, 0, M.plaster, group);
// Timber frame.
[-1.85, 1.85].forEach((postX) => {
[-1.38, 1.38].forEach((postZ) => {
box(0.16, 2.22, 0.16, postX, 1.13, postZ, M.darkWood, group);
});
});
box(4.08, 0.15, 0.16, 0, 1.98, 1.43, M.darkWood, group);
box(4.08, 0.15, 0.16, 0, 1.98, -1.43, M.darkWood, group);
box(0.15, 0.15, 3.0, -1.87, 1.98, 0, M.darkWood, group);
box(0.15, 0.15, 3.0, 1.87, 1.98, 0, M.darkWood, group);
// Shoji-like sliding panels.
[-1.03, 0.0, 1.03].forEach((panelX, index) => {
box(
0.86,
0.94,
0.055,
panelX,
1.18,
1.525,
index === 1 ? M.wood : M.shoji,
group
);
});
// Dark entrance.
box(0.78, 1.35, 0.075, 0, 0.78, 1.56, M.darkWood, group);
// Broad, steep roof.
const roof = addMesh(
gableRoofGeometry(4.9, 3.85, 2.08, 3.15),
M.roof,
0,
0,
0,
group
);
roof.castShadow = true;
// Ridge cap.
box(0.18, 0.16, 4.05, 0, 3.16, 0, M.darkWood, group);
// Eave boards.
box(4.9, 0.11, 0.15, 0, 2.07, 1.86, M.darkWood, group);
box(4.9, 0.11, 0.15, 0, 2.07, -1.86, M.darkWood, group);
}
buildHouse(-7.1, -6.2, 0.92);
buildHouse(7.0, -6.3, 0.9);
// A small field storehouse.
const storehouse = new THREE.Group();
storehouse.position.set(-7.6, 0, 4.9);
storehouse.rotation.y = 0.18;
scene.add(storehouse);
box(2.5, 1.55, 2.0, 0, 0.78, 0, M.plaster, storehouse);
box(0.16, 1.72, 0.16, -1.13, 0.86, 0.88, M.darkWood, storehouse);
box(0.16, 1.72, 0.16, 1.13, 0.86, 0.88, M.darkWood, storehouse);
box(2.7, 0.12, 2.45, 0, 1.56, 0, M.roof, storehouse);
box(0.75, 1.15, 0.08, 0, 0.61, 1.02, M.darkWood, storehouse);
// -------------------------------------------------------------------------
// Stone lantern and field details
// -------------------------------------------------------------------------
function buildLantern(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
cylinder(0.27, 0.32, 0.18, 0, 0.08, 0, M.stone, group);
cylinder(0.12, 0.16, 0.62, 0, 0.43, 0, M.stone, group);
box(0.42, 0.34, 0.42, 0, 0.9, 0, M.stoneLight, group);
cone(0.39, 0.32, 0, 1.22, 0, M.stone, group, 4);
cone(0.08, 0.18, 0, 1.47, 0, M.stone, group, 4);
}
buildLantern(-7.0, 6.2);
buildLantern(7.2, 5.9);
// Bundles and field markers near the paddies.
for (let i = 0; i < 7; i++) {
const side = i < 4 ? -1 : 1;
const x = side * (7.0 + (i % 3) * 0.35);
const z = -3.0 + (i % 4) * 1.6;
box(0.08, 0.75, 0.08, x, 0.34, z, M.wood);
}
// -------------------------------------------------------------------------
// UI and sluice controls
// -------------------------------------------------------------------------
const leftInput = document.getElementById("leftGate");
const rightInput = document.getElementById("rightGate");
const leftValue = document.getElementById("leftValue");
const rightValue = document.getElementById("rightValue");
const status = document.getElementById("status");
function updateReadouts() {
const left = Math.round(gates.left.target * 100);
const right = Math.round(gates.right.target * 100);
leftValue.value = `${left}%`;
rightValue.value = `${right}%`;
status.innerHTML =
`Stream flow: steady<br />` +
`West gate opening ${left}% · East gate opening ${right}%`;
}
leftInput.addEventListener("input", () => {
gates.left.target = Number(leftInput.value) / 100;
updateReadouts();
});
rightInput.addEventListener("input", () => {
gates.right.target = Number(rightInput.value) / 100;
updateReadouts();
});
document.getElementById("openBoth").addEventListener("click", () => {
gates.left.target = 1;
gates.right.target = 1;
leftInput.value = "100";
rightInput.value = "100";
updateReadouts();
});
document.getElementById("closeBoth").addEventListener("click", () => {
gates.left.target = 0;
gates.right.target = 0;
leftInput.value = "0";
rightInput.value = "0";
updateReadouts();
});
gates.left.target = Number(leftInput.value) / 100;
gates.right.target = Number(rightInput.value) / 100;
gates.left.current = gates.left.target;
gates.right.current = gates.right.target;
updateReadouts();
// -------------------------------------------------------------------------
// Custom orbit and zoom controls, implemented without addons
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 0, 0.1);
const spherical = new THREE.Spherical(19, 1.0, 0.55);
const cameraOffset = new THREE.Vector3();
function updateCamera() {
cameraOffset.setFromSpherical(spherical);
camera.position.copy(target).add(cameraOffset);
camera.lookAt(target);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
spherical.theta -= deltaX * 0.006;
spherical.phi -= deltaY * 0.005;
spherical.phi = THREE.MathUtils.clamp(spherical.phi, 0.38, 1.35);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
try {
renderer.domElement.releasePointerCapture(event.pointerId);
} catch (_) {}
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius + event.deltaY * 0.012,
10,
30
);
updateCamera();
},
{ passive: false }
);
document.getElementById("resetView").addEventListener("click", () => {
target.set(0, 0, 0.1);
spherical.set(19, 1.0, 0.55);
updateCamera();
});
updateCamera();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
// Ease the gates toward their target positions.
Object.values(gates).forEach((gate) => {
const smoothing = 1 - Math.exp(-delta * 6);
gate.current += (gate.target - gate.current) * smoothing;
// Closed: board bottom at the channel floor.
// Open: board is lifted beneath the top crossbeam.
gate.board.position.y = 0.39 + gate.current * 0.58;
gate.water.material.opacity = 0.08 + gate.current * 0.67;
gate.water.visible = gate.current > 0.015;
});
// Animate the water surfaces.
waterMeshes.forEach(({ water, phase, amplitude }) => {
const positions = water.geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const z = positions.getZ(i);
const wave =
Math.sin(x * 1.7 + z * 1.2 + time * 1.8 + phase) * amplitude +
Math.sin(z * 2.8 - time * 1.15 + phase) * amplitude * 0.45;
positions.setY(i, wave);
}
positions.needsUpdate = true;
water.geometry.computeVertexNormals();
});
// Shared-channel flow.
centralFlow.forEach((flow) => {
flow.position.z =
CHANNEL_BACK +
((flow.userData.offset + time * flow.userData.speed) %
(CHANNEL_LENGTH - 0.35));
flow.position.x += Math.sin(time * 2.1 + flow.userData.offset * 9) * 0.0008;
});
// Branch flow. The visual flow rate follows the gate opening.
branchFlow.forEach((flow) => {
const side = flow.position.x < 0 ? "left" : "right";
const gate = gates[side];
flow.visible = gate.current > 0.045;
flow.position.x =
gate.sign *
(0.48 +
((flow.userData.offset + time * flow.userData.speed * gate.current) %
0.83));
flow.material.opacity = 0.25 + gate.current * 0.45;
});
// Gentle expanding ripples.
ripples.forEach((ripple) => {
const phase = (time * 0.35 + ripple.userData.offset) % 1;
const scale = 0.65 + phase * 2.8;
ripple.scale.setScalar(scale);
ripple.material.opacity = 0.2 * (1 - phase);
});
renderer.render(scene, camera);
}
animate();
// -------------------------------------------------------------------------
// Resize handling
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
</script>
</body>
</html>
```