Prompt specification
Build or customize the Three.js 3D scene "Medieval miller opening a bypass channel" 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: Medieval miller opening a bypass channel
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/medieval-miller-opening-a-bypass-channel-x2378
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a technical cutaway of a referenced medieval mill entrance. Show the upstream channel dividing between a wheel race and a bypass, with both downstream outlets visible in the same model. Include a simple gate and a small miller figure beside its operating lever. As the gate changes position, redirect symbolic water tracers toward the selected branch while keeping the channel geometry continuous. The wheel turns only when its race receives the illustrative flow. Use stone, timber, and muted metal colors, and keep the hydraulic motion schematic rather than presenting a precise reconstruction of historical discharge.
Working Three.js Starter (medieval-miller-opening-a-bypass-channel-x2378):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Medieval Mill Headrace — Technical Cutaway</title>
<style>
:root {
color-scheme: light;
--ink: #26302f;
--muted: #63706d;
--paper: rgba(245, 244, 237, 0.92);
--line: rgba(48, 64, 62, 0.22);
--accent: #2f7d83;
--accent-soft: #d8e8e5;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9ddd7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--paper);
box-shadow: 0 14px 40px rgba(42, 54, 52, 0.12);
backdrop-filter: blur(12px);
}
#title-card {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px 20px 16px;
border-radius: 4px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.2vw, 30px);
font-weight: 650;
letter-spacing: -0.035em;
line-height: 1.08;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
#controls {
left: 22px;
bottom: 22px;
width: min(390px, calc(100vw - 44px));
padding: 16px 18px 15px;
border-radius: 4px;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.control-heading strong {
font-size: 12px;
letter-spacing: 0.02em;
}
#state {
color: var(--accent);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
margin: 0 0 12px;
accent-color: var(--accent);
cursor: ew-resize;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 34px;
border: 1px solid var(--line);
border-radius: 3px;
background: rgba(255, 255, 255, 0.46);
color: var(--ink);
font: 650 11px/1 ui-sans-serif, system-ui, sans-serif;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease, color 150ms ease;
}
button:hover,
button:focus-visible {
border-color: rgba(47, 125, 131, 0.6);
outline: none;
}
button.active {
border-color: var(--accent);
background: var(--accent-soft);
color: #1f666b;
}
.note {
display: flex;
align-items: flex-start;
gap: 8px;
margin-top: 12px;
color: var(--muted);
font-size: 10.5px;
line-height: 1.45;
}
.note::before {
content: "";
flex: 0 0 5px;
width: 5px;
height: 5px;
margin-top: 4px;
border-radius: 50%;
background: var(--accent);
}
#legend {
top: 22px;
right: 22px;
padding: 13px 15px;
border-radius: 4px;
font-size: 10.5px;
line-height: 1.8;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
white-space: nowrap;
}
.swatch {
display: inline-block;
width: 16px;
height: 7px;
border-radius: 1px;
}
.swatch.water {
background: #5d9fa1;
}
.swatch.stone {
background: #aaa99e;
}
.swatch.timber {
background: #80664d;
}
.swatch.metal {
background: #6d7776;
}
#hint {
position: fixed;
right: 22px;
bottom: 22px;
z-index: 10;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 3px;
background: rgba(245, 244, 237, 0.82);
color: var(--muted);
font: 10px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.025em;
text-align: right;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 50;
display: grid;
place-items: center;
background: #d9ddd7;
color: #53625f;
font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
#controls {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
#legend,
#hint {
display: none;
}
.subtitle {
max-width: 320px;
}
}
</style>
</head>
<body>
<div id="loading">Constructing schematic mill cutaway…</div>
<section id="title-card" class="panel">
<div class="eyebrow">Hydraulic routing study / c. medieval mill</div>
<h1>Entrance Headrace & Wheel Race</h1>
<p class="subtitle">
A roofless technical cutaway showing a shared upstream channel, a movable
distribution gate, the wheel race, and both downstream outlets.
</p>
</section>
<section id="controls" class="panel">
<div class="control-heading">
<strong>Gate position</strong>
<span id="state">Flow: wheel race</span>
</div>
<input
id="gate-slider"
type="range"
min="0"
max="100"
step="1"
value="0"
aria-label="Move the distribution gate between wheel race and bypass"
>
<div class="buttons">
<button id="wheel-button" class="active" type="button">Wheel race</button>
<button id="bypass-button" type="button">Bypass</button>
<button id="reset-view" type="button">Reset view</button>
<button id="toggle-gate" type="button">Move gate</button>
</div>
<div class="note">
Water cones are illustrative tracers, not a calculated discharge model.
The wheel responds only while its race is supplied.
</div>
</section>
<aside id="legend" class="panel">
<div class="legend-row"><span class="swatch water"></span>Schematic water</div>
<div class="legend-row"><span class="swatch stone"></span>Masonry / cutaway base</div>
<div class="legend-row"><span class="swatch timber"></span>Gate / mill carpentry</div>
<div class="legend-row"><span class="swatch metal"></span>Axle and fittings</div>
</aside>
<div id="hint">
Drag: orbit · Wheel: zoom<br>
Click the gate or miller’s lever
</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 canvas = document.querySelector("canvas") || document.createElement("canvas");
canvas.setAttribute("aria-label", "Interactive technical cutaway of a medieval mill headrace");
document.body.prepend(canvas);
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(0xd9ddd7);
scene.fog = new THREE.Fog(0xd9ddd7, 24, 43);
const camera = new THREE.PerspectiveCamera(
41,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
target: new THREE.Vector3(-1.45, 0.45, 0.15),
azimuth: 0.72,
polar: 0.70,
radius: 22
};
const initialOrbit = {
target: orbit.target.clone(),
azimuth: orbit.azimuth,
polar: orbit.polar,
radius: orbit.radius
};
function updateCamera() {
const s = Math.sin(orbit.polar);
camera.position.set(
orbit.target.x + orbit.radius * s * Math.cos(orbit.azimuth),
orbit.target.y + orbit.radius * Math.cos(orbit.polar),
orbit.target.z + orbit.radius * s * Math.sin(orbit.azimuth)
);
camera.lookAt(orbit.target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xf4f3e9, 0x747b72, 2.05);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff6de, 3.0);
sun.position.set(-7, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb6d4d7, 0.75);
fill.position.set(9, 7, -10);
scene.add(fill);
// ---------------------------------------------------------------------
// Procedural, self-contained surface textures
// ---------------------------------------------------------------------
let randomSeed = 73129;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
function makeStoneTexture(repeatX = 3, repeatY = 2) {
const c = document.createElement("canvas");
c.width = c.height = 256;
const ctx = c.getContext("2d");
ctx.fillStyle = "#77786f";
ctx.fillRect(0, 0, 256, 256);
const colors = ["#a9a89d", "#b5b2a5", "#9d9e95", "#b9b5a8", "#a3a59d"];
for (let row = 0; row < 8; row++) {
const y = row * 32;
const offset = row % 2 ? -32 : 0;
for (let x = offset; x < 256; x += 64) {
const shade = colors[Math.floor(random() * colors.length)];
ctx.fillStyle = shade;
ctx.fillRect(x + 2, y + 2, 60, 28);
ctx.fillStyle = `rgba(255,255,255,${0.025 + random() * 0.055})`;
ctx.fillRect(x + 3, y + 3, 58, 3);
ctx.fillStyle = `rgba(55,59,55,${0.03 + random() * 0.06})`;
ctx.fillRect(x + 3, y + 26, 58, 3);
}
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(repeatX, repeatY);
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return texture;
}
function makeTimberTexture() {
const c = document.createElement("canvas");
c.width = 128;
c.height = 256;
const ctx = c.getContext("2d");
ctx.fillStyle = "#80664c";
ctx.fillRect(0, 0, 128, 256);
for (let i = 0; i < 75; i++) {
const x = random() * 128;
ctx.strokeStyle = `rgba(42,30,20,${0.04 + random() * 0.18})`;
ctx.lineWidth = 0.5 + random() * 2;
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.bezierCurveTo(
x + random() * 8 - 4,
80,
x + random() * 9 - 4.5,
170,
x + random() * 8 - 4,
256
);
ctx.stroke();
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1, 2);
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return texture;
}
const groundTexture = makeStoneTexture(5, 4);
const channelTexture = makeStoneTexture(3, 1);
const timberTexture = makeTimberTexture();
const materials = {
ground: new THREE.MeshStandardMaterial({
color: 0xb9b8ad,
map: groundTexture,
bumpMap: groundTexture,
bumpScale: 0.045,
roughness: 0.96
}),
foundation: new THREE.MeshStandardMaterial({
color: 0x777b75,
roughness: 1
}),
stone: new THREE.MeshStandardMaterial({
color: 0xb4b2a7,
map: channelTexture,
bumpMap: channelTexture,
bumpScale: 0.035,
roughness: 0.93,
side: THREE.DoubleSide
}),
darkStone: new THREE.MeshStandardMaterial({
color: 0x727a72,
roughness: 0.98
}),
timber: new THREE.MeshStandardMaterial({
color: 0x8c7051,
map: timberTexture,
roughness: 0.82
}),
darkTimber: new THREE.MeshStandardMaterial({
color: 0x574632,
roughness: 0.9
}),
metal: new THREE.MeshStandardMaterial({
color: 0x6f7977,
metalness: 0.58,
roughness: 0.57
}),
darkMetal: new THREE.MeshStandardMaterial({
color: 0x46504f,
metalness: 0.52,
roughness: 0.65
}),
cloth: new THREE.MeshStandardMaterial({
color: 0x827a63,
roughness: 0.94
}),
skin: new THREE.MeshStandardMaterial({
color: 0xc5a383,
roughness: 0.88
}),
interior: new THREE.MeshStandardMaterial({
color: 0x353b38,
roughness: 1
})
};
function box(parent, size, position, material, cast = true, receive = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), material);
mesh.position.set(...position);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
function cylinderBetween(parent, a, b, radius, material, radialSegments = 10) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
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;
}
// ---------------------------------------------------------------------
// Cutaway base and technical grid
// ---------------------------------------------------------------------
const base = box(
scene,
[17.2, 0.58, 10.8],
[-1.7, -0.34, 0.2],
materials.ground
);
box(
scene,
[17.5, 0.20, 11.1],
[-1.7, -0.73, 0.2],
materials.foundation
);
const baseEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(base.geometry),
new THREE.LineBasicMaterial({
color: 0x596562,
transparent: true,
opacity: 0.52
})
);
baseEdges.position.copy(base.position);
scene.add(baseEdges);
const grid = new THREE.GridHelper(40, 80, 0x7c8d88, 0x9caaa5);
grid.position.y = -0.84;
grid.material.transparent = true;
grid.material.opacity = 0.16;
scene.add(grid);
// ---------------------------------------------------------------------
// Continuous channel geometry
// ---------------------------------------------------------------------
function makeCurve(points) {
return new THREE.CatmullRomCurve3(
points.map(([x, z]) => new THREE.Vector3(x, 0.06, z)),
false,
"catmullrom",
0.5
);
}
const upstreamCurve = makeCurve([
[-9.45, 2.50],
[-7.45, 2.50],
[-6.10, 2.12],
[-4.72, 1.54],
[-4.00, 1.00]
]);
const wheelCurve = makeCurve([
[-4.00, 1.00],
[-3.42, 0.34],
[-2.28, -0.54],
[-0.86, -1.04],
[0.78, -1.10],
[3.30, -1.10],
[6.02, -1.10]
]);
const bypassCurve = makeCurve([
[-4.00, 1.00],
[-3.42, 1.88],
[-2.42, 3.08],
[-0.55, 3.50],
[2.15, 3.54],
[4.35, 3.50],
[6.02, 3.50]
]);
function smoothstep(a, b, x) {
const t = THREE.MathUtils.clamp((x - a) / (b - a), 0, 1);
return t * t * (3 - 2 * t);
}
function ribbonGeometry(curve, width, y, uvRepeat = 4) {
const count = 100;
const positions = [];
const uvs = [];
const indices = [];
for (let i = 0; i < count; i++) {
const t = i / (count - 1);
const p = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
const normal = new THREE.Vector3(-tangent.z, 0, tangent.x);
const left = p.clone().addScaledVector(normal, width * 0.5);
const right = p.clone().addScaledVector(normal, -width * 0.5);
positions.push(left.x, y, left.z);
positions.push(right.x, y, right.z);
uvs.push(t * uvRepeat, 0);
uvs.push(t * uvRepeat, 1);
if (i < count - 1) {
const a = i * 2;
indices.push(a, a + 2, a + 1, a + 1, a + 2, a + 3);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function wallGeometry(curve, width) {
const count = 90;
const positions = [];
const uvs = [];
const indices = [];
for (const side of [-1, 1]) {
const baseIndex = positions.length / 3;
for (let i = 0; i < count; i++) {
const t = i / (count - 1);
const p = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
const normal = new THREE.Vector3(-tangent.z, 0, tangent.x);
const edge = p.clone().addScaledVector(normal, side * width * 0.5);
const taper =
smoothstep(0, 0.13, t) * smoothstep(0, 0.13, 1 - t);
const height = 0.39 * taper;
positions.push(edge.x, 0.025, edge.z);
positions.push(edge.x, 0.025 + height, edge.z);
uvs.push(t * 4, 0);
uvs.push(t * 4, 1);
if (i < count - 1) {
const a = baseIndex + i * 2;
indices.push(a, a + 2, a + 1, a + 1, a + 2, a + 3);
}
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function addWallCaps(curve, width) {
const length = curve.getLength();
const count = Math.max(4, Math.floor(length / 0.55));
for (let i = 0; i < count; i++) {
const t = 0.09 + (i / Math.max(1, count - 1)) * 0.82;
const p = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
const normal = new THREE.Vector3(-tangent.z, 0, tangent.x);
const segmentLength = length / count * 0.94;
for (const side of [-1, 1]) {
const edge = p.clone().addScaledVector(normal, side * (width * 0.5 + 0.065));
const cap = box(
scene,
[0.18, 0.105, segmentLength],
[edge.x, 0.42, edge.z],
materials.stone
);
cap.rotation.y = Math.atan2(tangent.x, tangent.z);
}
}
}
function waterMaterial(opacity = 0.48) {
return new THREE.MeshStandardMaterial({
color: 0x5c9da0,
roughness: 0.28,
metalness: 0.04,
transparent: true,
opacity,
depthWrite: false,
side: THREE.DoubleSide
});
}
const upstreamWaterMaterial = waterMaterial(0.47);
const wheelWaterMaterial = waterMaterial(0.62);
const bypassWaterMaterial = waterMaterial(0.30);
function addChannel(curve, waterMat, label) {
const bed = new THREE.Mesh(
ribbonGeometry(curve, 1.36, 0.025, 4),
materials.darkStone
);
bed.receiveShadow = true;
scene.add(bed);
const wall = new THREE.Mesh(
wallGeometry(curve, 1.36),
materials.stone
);
wall.castShadow = true;
wall.receiveShadow = true;
scene.add(wall);
addWallCaps(curve, 1.36);
const water = new THREE.Mesh(
ribbonGeometry(curve, 1.03, 0.115, 5),
waterMat
);
water.renderOrder = 2;
scene.add(water);
return { bed, wall, water, label };
}
addChannel(upstreamCurve, upstreamWaterMaterial, "upstream");
addChannel(wheelCurve, wheelWaterMaterial, "wheel");
addChannel(bypassCurve, bypassWaterMaterial, "bypass");
// ---------------------------------------------------------------------
// Symbolic flow tracers
// ---------------------------------------------------------------------
const tracerGeometry = new THREE.ConeGeometry(0.075, 0.30, 5);
const upAxis = new THREE.Vector3(0, 1, 0);
function makeStream(curve, count, color, opacity = 0.9) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
const group = new THREE.Group();
const tracers = [];
for (let i = 0; i < count; i++) {
const mesh = new THREE.Mesh(tracerGeometry, material);
mesh.renderOrder = 5;
group.add(mesh);
tracers.push({
mesh,
u: i / count,
speed: 0.052 + (i % 3) * 0.004
});
}
scene.add(group);
return {
group,
material,
tracers,
curve
};
}
const upstreamStream = makeStream(upstreamCurve, 13, 0x9fd7d2, 0.88);
const wheelStream = makeStream(wheelCurve, 19, 0xb4ece1, 0.96);
const bypassStream = makeStream(bypassCurve, 18, 0xb4ece1, 0.18);
function updateStream(stream, delta, speedMultiplier = 1) {
for (const tracer of stream.tracers) {
tracer.u = (tracer.u + delta * tracer.speed * speedMultiplier) % 1;
const p = stream.curve.getPointAt(tracer.u);
const tangent = stream.curve.getTangentAt(tracer.u).normalize();
tracer.mesh.position.set(p.x, 0.205, p.z);
tracer.mesh.quaternion.setFromUnitVectors(upAxis, tangent);
}
}
// ---------------------------------------------------------------------
// Water wheel and timber support frame
// ---------------------------------------------------------------------
const wheelAssembly = new THREE.Group();
wheelAssembly.position.set(0.15, 1.48, -1.10);
scene.add(wheelAssembly);
const rotatingWheel = new THREE.Group();
wheelAssembly.add(rotatingWheel);
const rimGeometry = new THREE.TorusGeometry(1.28, 0.10, 9, 48);
for (const z of [-0.36, 0.36]) {
const rim = new THREE.Mesh(rimGeometry, materials.darkTimber);
rim.position.z = z;
rim.castShadow = true;
rotatingWheel.add(rim);
}
for (const z of [-0.36, 0.36]) {
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2;
const a = new THREE.Vector3(0, 0, z);
const b = new THREE.Vector3(
Math.cos(angle) * 1.22,
Math.sin(angle) * 1.22,
z
);
cylinderBetween(rotatingWheel, a, b, 0.043, materials.timber, 7);
}
}
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.18, 0.94, 16),
materials.darkMetal
);
hub.rotation.x = Math.PI * 0.5;
hub.castShadow = true;
rotatingWheel.add(hub);
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const paddle = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.17, 0.82),
materials.timber
);
paddle.position.set(
Math.cos(angle) * 1.18,
Math.sin(angle) * 1.18,
0
);
paddle.rotation.z = angle;
paddle.castShadow = true;
paddle.receiveShadow = true;
rotatingWheel.add(paddle);
}
for (const z of [-0.86, 0.86]) {
box(
scene,
[0.18, 2.45, 0.19],
[0.15, 1.22, -1.10 + z],
materials.darkTimber
);
}
box(
scene,
[0.22, 0.20, 1.92],
[0.15, 2.53, -1.10],
materials.darkTimber
);
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 1.92, 16),
materials.metal
);
axle.position.set(0.15, 1.48, -1.10);
axle.rotation.x = Math.PI * 0.5;
axle.castShadow = true;
scene.add(axle);
// ---------------------------------------------------------------------
// Roofless mill entrance / cutaway structure
// ---------------------------------------------------------------------
const mill = new THREE.Group();
scene.add(mill);
box(mill, [4.55, 0.18, 2.42], [2.40, 0.04, -3.27], materials.stone);
box(mill, [4.55, 2.78, 0.24], [2.40, 1.39, -4.38], materials.stone);
box(mill, [0.24, 2.78, 2.30], [0.20, 1.39, -3.26], materials.stone);
// Front wall is interrupted by the mill entrance.
box(mill, [1.96, 2.62, 0.30], [1.13, 1.31, -2.15], materials.stone);
box(mill, [1.28, 2.62, 0.30], [3.99, 1.31, -2.15], materials.stone);
box(mill, [1.26, 0.66, 0.34], [2.76, 2.24, -2.14], materials.stone);
// Dark interior gives the open portal readable depth.
box(
mill,
[1.22, 2.05, 0.08],
[2.76, 1.04, -4.20],
materials.interior,
false,
false
);
// Exposed roof frame identifies the building as a cutaway.
for (const x of [0.85, 2.76, 4.40]) {
box(
mill,
[0.16, 0.16, 2.37],
[x, 2.91, -3.26],
materials.darkTimber
);
}
box(
mill,
[4.38, 0.17, 0.18],
[2.40, 2.93, -2.13],
materials.darkTimber
);
// A partially open timber door.
const doorHinge = new THREE.Group();
doorHinge.position.set(3.37, 1.05, -2.00);
doorHinge.rotation.y = -0.82;
mill.add(doorHinge);
box(
doorHinge,
[1.12, 1.85, 0.10],
[-0.56, 0, 0],
materials.darkTimber
);
for (const y of [-0.58, 0.58]) {
box(
doorHinge,
[1.08, 0.09, 0.13],
[-0.56, y, 0.01],
materials.metal
);
}
// ---------------------------------------------------------------------
// Distribution gate at the fork
// ---------------------------------------------------------------------
const gatePivot = new THREE.Group();
gatePivot.position.set(-4.00, 0.04, 1.00);
gatePivot.userData.action = "gate";
scene.add(gatePivot);
const gateBoard = box(
gatePivot,
[1.48, 0.68, 0.14],
[0.74, 0.34, 0],
materials.timber
);
gateBoard.userData.action = "gate";
for (const x of [0.24, 0.74, 1.24]) {
box(
gatePivot,
[0.035, 0.66, 0.16],
[x, 0.34, 0],
materials.darkTimber
);
}
box(
gatePivot,
[1.40, 0.075, 0.18],
[0.74, 0.57, 0],
materials.metal
);
const gatePin = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.065, 0.92, 12),
materials.darkMetal
);
gatePin.position.set(-4.00, 0.43, 1.00);
gatePin.castShadow = true;
scene.add(gatePin);
// ---------------------------------------------------------------------
// Miller and operating lever
// ---------------------------------------------------------------------
box(
scene,
[1.65, 0.12, 1.25],
[-3.04, 0.01, 0.02],
materials.stone
);
const leverPivot = new THREE.Group();
leverPivot.position.set(-3.35, 0.77, 0.13);
scene.add(leverPivot);
const leverHandle = cylinderBetween(
leverPivot,
new THREE.Vector3(0, -0.03, 0),
new THREE.Vector3(0, 0.78, 0),
0.055,
materials.darkTimber,
10
);
leverHandle.userData.action = "lever";
const leverGrip = new THREE.Mesh(
new THREE.SphereGeometry(0.09, 12, 8),
materials.timber
);
leverGrip.position.y = 0.79;
leverGrip.userData.action = "lever";
leverPivot.add(leverGrip);
const leverStand = new THREE.Group();
leverStand.userData.action = "lever";
scene.add(leverStand);
box(
leverStand,
[0.10, 0.74, 0.10],
[-3.49, 0.37, 0.13],
materials.metal
);
box(
leverStand,
[0.10, 0.74, 0.10],
[-3.21, 0.37, 0.13],
materials.metal
);
box(
leverStand,
[0.44, 0.09, 0.12],
[-3.35, 0.75, 0.13],
materials.metal
);
const miller = new THREE.Group();
miller.position.set(-2.48, 0.07, 0.13);
scene.add(miller);
// Feet and legs.
for (const x of [-0.13, 0.13]) {
box(miller, [0.18, 0.10, 0.31], [x, 0.06, 0.03], materials.darkTimber);
cylinderBetween(
miller,
new THREE.Vector3(x, 0.10, 0),
new THREE.Vector3(x, 0.53, 0),
0.085,
materials.darkTimber,
8
);
}
const tunic = new THREE.Mesh(
new THREE.CylinderGeometry(0.23, 0.30, 0.62, 10),
materials.cloth
);
tunic.position.y = 0.78;
tunic.castShadow = true;
miller.add(tunic);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 14, 10),
materials.skin
);
head.position.y = 1.22;
head.castShadow = true;
miller.add(head);
const hood = new THREE.Mesh(
new THREE.ConeGeometry(0.23, 0.29, 10),
materials.cloth
);
hood.position.y = 1.36;
hood.castShadow = true;
miller.add(hood);
const nose = new THREE.Mesh(
new THREE.SphereGeometry(0.045, 8, 6),
materials.skin
);
nose.position.set(0, 1.21, 0.17);
miller.add(nose);
// One arm reaches toward the operating lever.
cylinderBetween(
miller,
new THREE.Vector3(-0.18, 0.96, 0),
new THREE.Vector3(-0.42, 0.85, 0.14),
0.065,
materials.cloth,
8
);
cylinderBetween(
miller,
new THREE.Vector3(0.18, 0.96, 0),
new THREE.Vector3(0.38, 0.68, 0.04),
0.065,
materials.cloth,
8
);
// ---------------------------------------------------------------------
// Outlet spill faces and symbolic falling markers
// ---------------------------------------------------------------------
const wheelOutletMaterial = waterMaterial(0.55);
const bypassOutletMaterial = waterMaterial(0.22);
function addOutlet(x, z, material) {
const sheet = new THREE.Mesh(
new THREE.PlaneGeometry(0.96, 0.56),
material
);
sheet.rotation.y = Math.PI * 0.5;
sheet.position.set(x + 0.06, -0.16, z);
sheet.renderOrder = 3;
scene.add(sheet);
const drops = [];
const dropMaterial = new THREE.MeshBasicMaterial({
color: 0xb4ece1,
transparent: true,
opacity: 0.8,
depthWrite: false
});
for (let i = 0; i < 4; i++) {
const drop = new THREE.Mesh(
new THREE.BoxGeometry(0.025, 0.11, 0.055),
dropMaterial
);
drop.position.set(x + 0.10, 0, z - 0.33 + i * 0.22);
drop.renderOrder = 5;
scene.add(drop);
drops.push({ mesh: drop, offset: i * 0.23 });
}
return { material, dropMaterial, drops };
}
const wheelOutlet = addOutlet(6.02, -1.10, wheelOutletMaterial);
const bypassOutlet = addOutlet(6.02, 3.50, bypassOutletMaterial);
// ---------------------------------------------------------------------
// Technical labels drawn as Three.js sprites
// ---------------------------------------------------------------------
function makeLabel(text, accent = "#438c90", scale = 2.7) {
const c = document.createElement("canvas");
c.width = 512;
c.height = 128;
const ctx = c.getContext("2d");
ctx.fillStyle = "rgba(244,243,235,0.92)";
ctx.fillRect(0, 0, 512, 128);
ctx.strokeStyle = "rgba(54,70,67,0.48)";
ctx.lineWidth = 3;
ctx.strokeRect(2, 2, 508, 124);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 12, 128);
ctx.fillStyle = "#26302f";
ctx.font = "700 31px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 263, 64);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(scale, scale * 0.25, 1);
sprite.renderOrder = 20;
scene.add(sprite);
return sprite;
}
const labels = [
[makeLabel("UPSTREAM INLET", "#4e8585", 2.55), [-8.15, 1.15, 3.50]],
[makeLabel("DISTRIBUTION GATE", "#b36e47", 2.85), [-4.10, 1.45, 0.38]],
[makeLabel("WHEEL RACE", "#438c90", 2.35), [-0.30, 3.18, -1.10]],
[makeLabel("BYPASS RACE", "#438c90", 2.45), [1.00, 1.08, 4.45]],
[makeLabel("MILL ENTRANCE", "#8d704d", 2.75), [2.72, 3.52, -3.05]],
[makeLabel("OUTLET A", "#438c90", 2.10), [6.30, 0.78, -1.12]],
[makeLabel("OUTLET B", "#438c90", 2.10), [6.30, 0.78, 3.50]]
];
labels.forEach(([label, position]) => label.position.set(...position));
// ---------------------------------------------------------------------
// Gate state and UI
// ---------------------------------------------------------------------
let gateTarget = 0; // 0 = wheel race, 1 = bypass
let gatePosition = 0; // smoothly interpolated visual state
let wheelAngularVelocity = 0;
const slider = document.querySelector("#gate-slider");
const stateLabel = document.querySelector("#state");
const wheelButton = document.querySelector("#wheel-button");
const bypassButton = document.querySelector("#bypass-button");
const resetViewButton = document.querySelector("#reset-view");
const toggleButton = document.querySelector("#toggle-gate");
function updateUI() {
const wheelSelected = gateTarget < 0.5;
stateLabel.textContent = wheelSelected
? "Flow: wheel race"
: "Flow: bypass";
wheelButton.classList.toggle("active", wheelSelected);
bypassButton.classList.toggle("active", !wheelSelected);
}
function setGate(value) {
gateTarget = THREE.MathUtils.clamp(value, 0, 1);
slider.value = Math.round(gateTarget * 100);
updateUI();
}
slider.addEventListener("input", () => {
setGate(Number(slider.value) / 100);
});
wheelButton.addEventListener("click", () => setGate(0));
bypassButton.addEventListener("click", () => setGate(1));
toggleButton.addEventListener("click", () => {
setGate(gateTarget < 0.5 ? 1 : 0);
});
resetViewButton.addEventListener("click", () => {
orbit.target.copy(initialOrbit.target);
orbit.azimuth = initialOrbit.azimuth;
orbit.polar = initialOrbit.polar;
orbit.radius = initialOrbit.radius;
updateCamera();
});
updateUI();
// ---------------------------------------------------------------------
// Custom camera interaction; no addons are used
// ---------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const drag = {
active: false,
pointerId: null,
lastX: 0,
lastY: 0,
distance: 0
};
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
drag.active = true;
drag.pointerId = event.pointerId;
drag.lastX = event.clientX;
drag.lastY = event.clientY;
drag.distance = 0;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!drag.active || event.pointerId !== drag.pointerId) return;
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
drag.lastX = event.clientX;
drag.lastY = event.clientY;
drag.distance += Math.hypot(dx, dy);
orbit.azimuth -= dx * 0.006;
orbit.polar = THREE.MathUtils.clamp(
orbit.polar - dy * 0.005,
0.28,
1.28
);
updateCamera();
});
function findAction(object) {
while (object) {
if (object.userData.action) return object.userData.action;
object = object.parent;
}
return null;
}
canvas.addEventListener("pointerup", (event) => {
if (!drag.active || event.pointerId !== drag.pointerId) return;
drag.active = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (drag.distance < 6) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(
[gatePivot, leverHandle, leverGrip, leverStand],
true
);
if (hits.length) {
const action = findAction(hits[0].object);
if (action === "lever" || action === "gate") {
setGate(gateTarget < 0.5 ? 1 : 0);
}
}
}
});
canvas.addEventListener("pointercancel", () => {
drag.active = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
12,
34
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
gatePosition +=
(gateTarget - gatePosition) * (1 - Math.exp(-delta * 5.5));
let wheelWeight;
let bypassWeight;
if (gatePosition < 0.5) {
wheelWeight = 1 - gatePosition * 2;
bypassWeight = gatePosition * 2;
} else {
wheelWeight = (1 - gatePosition) * 2;
bypassWeight = (gatePosition - 0.5) * 2;
}
// At wheel selection the gate lies across the bypass mouth.
gatePivot.rotation.y = THREE.MathUtils.lerp(-0.49, 0.49, gatePosition);
leverPivot.rotation.z = THREE.MathUtils.lerp(0.48, -0.48, gatePosition);
// The water surface remains continuous; only emphasis changes.
wheelWaterMaterial.opacity = 0.28 + wheelWeight * 0.35;
bypassWaterMaterial.opacity = 0.28 + bypassWeight * 0.35;
wheelStream.material.opacity = 0.08 + wheelWeight * 0.88;
bypassStream.material.opacity = 0.08 + bypassWeight * 0.88;
updateStream(upstreamStream, delta, 1);
updateStream(wheelStream, delta, 0.10 + wheelWeight * 0.90);
updateStream(bypassStream, delta, 0.10 + bypassWeight * 0.90);
// The wheel receives torque only from the active wheel-race tracers.
wheelAngularVelocity +=
(wheelWeight * 1.38 - wheelAngularVelocity) *
(1 - Math.exp(-delta * (wheelWeight > 0.001 ? 3.5 : 8.0)));
rotatingWheel.rotation.z -= delta * wheelAngularVelocity;
wheelOutletMaterial.opacity = 0.12 + wheelWeight * 0.47;
bypassOutletMaterial.opacity = 0.12 + bypassWeight * 0.47;
wheelOutlet.dropMaterial.opacity = 0.08 + wheelWeight * 0.72;
bypassOutlet.dropMaterial.opacity = 0.08 + bypassWeight * 0.72;
for (const drop of wheelOutlet.drops) {
const phase = (elapsed * 0.72 + drop.offset) % 1;
drop.mesh.position.y = 0.07 - phase * 0.49;
drop.mesh.scale.y = 0.75 + Math.sin(phase * Math.PI) * 0.6;
}
for (const drop of bypassOutlet.drops) {
const phase = (elapsed * 0.72 + drop.offset) % 1;
drop.mesh.position.y = 0.07 - phase * 0.49;
drop.mesh.scale.y = 0.75 + Math.sin(phase * Math.PI) * 0.6;
}
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
document.querySelector("#loading").classList.add("hidden");
</script>
</body>
</html>
```