Prompt specification
Build or customize the Three.js 3D scene "Follow a boat and walker across a lock cycle" 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: Follow a boat and walker across a lock cycle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/follow-a-boat-and-walker-across-a-lock-cycle-x2852
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a voxel canal-lock scene beside a pedestrian quay. Include upper and lower water reaches, a lock chamber, working gates, a small boat, and a walkway along one side. Let the user advance a simplified lock cycle step by step while a pedestrian follows the quay toward a bridge. The boat must wait for suitable water levels and gate states; the walker follows the connected dry path independently. Keep both journeys visible in the same camera view. Use stepped stone walls, blocky railings, and clearly separated water surfaces, with a compact indication of the current chamber level and gate state.
Working Three.js Starter (follow-a-boat-and-walker-across-a-lock-cycle-x2852):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Canal Lock</title>
<style>
:root {
--ink: #17333a;
--muted: #60777c;
--panel: rgba(248, 250, 245, 0.94);
--line: rgba(23, 51, 58, 0.14);
--teal: #167d87;
--orange: #e76f38;
--green: #3d8b62;
--red: #bd4d45;
--shadow: 0 18px 50px rgba(24, 55, 61, 0.2);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cbdde0;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active { cursor: grabbing; }
.brand {
position: fixed;
z-index: 3;
top: 22px;
left: 24px;
max-width: 430px;
pointer-events: none;
text-shadow: 0 1px 0 rgba(255,255,255,.65);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 7px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: .16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 3px;
background: var(--orange);
border-radius: 4px;
}
h1 {
margin: 0;
font-size: clamp(29px, 4vw, 48px);
line-height: .98;
letter-spacing: -.055em;
}
.brand p {
margin: 10px 0 0;
max-width: 390px;
color: #46646b;
font-size: 13px;
line-height: 1.5;
}
.status {
position: fixed;
z-index: 4;
top: 20px;
right: 20px;
width: 302px;
padding: 17px;
border: 1px solid rgba(255,255,255,.7);
border-radius: 18px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(13px);
pointer-events: none;
}
.status-top {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 10px;
}
.status-kicker {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .15em;
text-transform: uppercase;
}
.step-count {
min-width: 48px;
padding: 4px 8px;
border-radius: 999px;
background: #e2efeb;
color: var(--teal);
text-align: center;
font-size: 11px;
font-weight: 800;
}
.status h2 {
margin: 0 0 5px;
font-size: 18px;
letter-spacing: -.025em;
}
.status-detail {
min-height: 42px;
margin: 0 0 14px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.divider {
height: 1px;
margin: 13px 0;
background: var(--line);
}
.meter-heading {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 10px;
margin-bottom: 7px;
font-size: 11px;
font-weight: 750;
}
#levelText {
color: var(--teal);
font-variant-numeric: tabular-nums;
}
.meter {
position: relative;
height: 10px;
overflow: hidden;
border-radius: 999px;
background: #d8e3e1;
box-shadow: inset 0 1px 2px rgba(0,0,0,.12);
}
.meter::after {
content: "";
position: absolute;
inset: 0;
background: repeating-linear-gradient(
90deg,
transparent 0,
transparent calc(25% - 1px),
rgba(255,255,255,.65) calc(25% - 1px),
rgba(255,255,255,.65) 25%
);
}
#meterFill {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #3f9db3, #1d7488);
transition: width .08s linear;
}
.gates {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
.gate-state {
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255,255,255,.58);
}
.gate-state span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .11em;
text-transform: uppercase;
}
.gate-state strong {
display: block;
font-size: 12px;
}
.gate-state.open strong { color: var(--green); }
.gate-state.moving strong { color: var(--orange); }
.gate-state.closed strong { color: var(--red); }
.travel-status {
display: grid;
grid-template-columns: 62px 1fr;
gap: 8px;
align-items: start;
margin-top: 9px;
font-size: 11px;
line-height: 1.35;
}
.travel-status b {
color: var(--muted);
font-size: 9px;
letter-spacing: .09em;
text-transform: uppercase;
}
.travel-status span {
font-weight: 700;
}
.travel-status .waiting span { color: var(--orange); }
.travel-status .moving span { color: var(--teal); }
.travel-status .safe span { color: var(--green); }
.dots {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: 5px;
margin-top: 14px;
}
.dot {
height: 4px;
border-radius: 99px;
background: #d7e0de;
transition: background .2s, transform .2s;
}
.dot.done { background: #80aaa3; }
.dot.active {
background: var(--orange);
transform: scaleY(1.55);
}
.controls {
position: fixed;
z-index: 5;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 9px;
transform: translateX(-50%);
padding: 9px;
border: 1px solid rgba(255,255,255,.72);
border-radius: 16px;
background: rgba(248,250,245,.93);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
button {
appearance: none;
border: 0;
border-radius: 11px;
padding: 12px 17px;
color: white;
background: var(--teal);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
transition: transform .15s, background .15s, opacity .15s;
white-space: nowrap;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
background: #0e6e77;
}
button:active:not(:disabled) { transform: translateY(0); }
button:disabled {
cursor: default;
opacity: .58;
}
button.secondary {
color: var(--ink);
background: #e6eeeb;
}
button.secondary:hover { background: #d9e5e1; }
.hint {
position: fixed;
z-index: 3;
left: 23px;
bottom: 24px;
color: #4f6b71;
font-size: 10px;
font-weight: 700;
letter-spacing: .035em;
pointer-events: none;
}
.hint span {
display: inline-block;
margin-right: 11px;
padding: 4px 7px;
border-radius: 6px;
background: rgba(248,250,245,.72);
}
@media (max-width: 760px) {
.brand {
top: 14px;
left: 14px;
max-width: 54vw;
}
.brand p { display: none; }
h1 { font-size: 29px; }
.status {
top: 12px;
right: 12px;
width: 235px;
padding: 13px;
border-radius: 14px;
}
.status-detail { min-height: 0; margin-bottom: 9px; }
.gates { gap: 5px; }
.gate-state { padding: 7px; }
.travel-status { font-size: 10px; }
.dots { margin-top: 10px; }
.controls { bottom: 12px; width: calc(100% - 24px); }
button { flex: 1; padding: 12px 10px; }
button.secondary { flex: 0 0 auto; }
.hint { display: none; }
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="brand">
<div class="eyebrow">Interactive voxel infrastructure</div>
<h1>Canal Lock</h1>
<p>Advance the lock cycle while the pedestrian independently follows the dry quay toward the bridge.</p>
</header>
<aside class="status" aria-live="polite">
<div class="status-top">
<div class="status-kicker">Lock status</div>
<div class="step-count" id="stepCount">0 / 7</div>
</div>
<h2 id="stageTitle">Boat waits at lower reach</h2>
<p class="status-detail" id="stageDetail"></p>
<div class="meter-heading">
<span>CHAMBER LEVEL</span>
<span id="levelText">0.00 m</span>
</div>
<div class="meter"><div id="meterFill"></div></div>
<div class="gates">
<div class="gate-state closed" id="lowerGateState">
<span>Lower gate</span>
<strong>Closed</strong>
</div>
<div class="gate-state closed" id="upperGateState">
<span>Upper gate</span>
<strong>Closed</strong>
</div>
</div>
<div class="divider"></div>
<div class="travel-status waiting" id="boatStatus">
<b>Boat</b><span>Waiting at lower reach</span>
</div>
<div class="travel-status moving" id="walkerStatus">
<b>Walker</b><span>Walking on the dry quay</span>
</div>
<div class="dots" id="dots">
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
<span class="dot"></span>
</div>
</aside>
<div class="controls">
<button id="advance" type="button">Open lower gate</button>
<button class="secondary" id="reset" type="button">Reset</button>
<button class="secondary" id="home" type="button" title="Reset camera">Home view</button>
</div>
<div class="hint">
<span>Drag to orbit</span>
<span>Scroll to zoom</span>
<span>Space: advance</span>
<span>R: reset</span>
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
// ---------------------------------------------------------------------------
// Renderer, camera, and scene
// ---------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcbdde0);
scene.fog = new THREE.Fog(0xcbdde0, 72, 135);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
220
);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.querySelector('#scene').appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xf2fbff, 0x6d745d, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d7, 2.7);
sun.position.set(24, 42, 28);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -35;
sun.shadow.camera.right = 35;
sun.shadow.camera.top = 42;
sun.shadow.camera.bottom = -42;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 110;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0xb8e3ef, 0.65);
fillLight.position.set(-25, 18, -30);
scene.add(fillLight);
const cameraState = {
theta: 0.73,
phi: 0.97,
radius: innerWidth < 700 ? 79 : 67,
target: new THREE.Vector3(0, 1.5, -1)
};
function updateCamera() {
const s = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * s * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * s * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
updateCamera();
// ---------------------------------------------------------------------------
// Materials and reusable geometry
// ---------------------------------------------------------------------------
const material = (color, roughness = 0.82, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness,
flatShading: true,
...extra
});
const mats = {
base: material(0x586d70),
earth: material(0x9b8a65),
grass: material(0x82975f),
grassDark: material(0x667e50),
stone: material(0x8d9288),
stoneLight: material(0xa5aa9e),
stoneDark: material(0x717970),
coping: material(0xc0c3b6),
wood: material(0x805331),
woodLight: material(0xa16a3d),
woodDark: material(0x513828),
iron: material(0x354448, 0.55),
hull: material(0xd65f35),
hullDark: material(0x783a2d),
cream: material(0xf1dfb7),
window: material(0x6bb8c9, 0.25, { metalness: 0.12 }),
coat: material(0x2f7f91),
trousers: material(0x34464d),
skin: material(0xd9a176),
orange: material(0xf08a3c),
ripple: new THREE.MeshBasicMaterial({
color: 0xb8e2e7,
transparent: true,
opacity: 0.28,
depthWrite: false
})
};
function box(w, h, d, mat, x, y, z, parent = scene, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function textSprite(text, x, y, z, width = 7, background = '#17333a') {
const canvas = document.createElement('canvas');
canvas.width = 640;
canvas.height = 144;
const ctx = canvas.getContext('2d');
ctx.fillStyle = background;
ctx.beginPath();
ctx.roundRect(4, 4, 632, 136, 24);
ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,.28)';
ctx.lineWidth = 4;
ctx.stroke();
ctx.fillStyle = '#f7f4e9';
ctx.font = '800 43px system-ui, sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, 320, 74);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.position.set(x, y, z);
sprite.scale.set(width, width * 144 / 640, 1);
sprite.renderOrder = 20;
scene.add(sprite);
return sprite;
}
// ---------------------------------------------------------------------------
// Diorama foundation, banks, and masonry
// ---------------------------------------------------------------------------
const LOW_LEVEL = 0.15;
const UPPER_LEVEL = 4.15;
box(30, 2.4, 56, mats.base, 0, -1.9, 0);
box(29.4, 0.24, 55.4, mats.earth, 0, -0.62, 0, scene, false);
function bank(side, z0, z1, top, grass = true) {
const length = z1 - z0;
const centerZ = (z0 + z1) / 2;
const height = top + 0.62;
box(8, height, length, mats.earth, side * 10.2, top - height / 2, centerZ, scene, false);
if (grass) {
box(8.02, 0.10, length, side < 0 ? mats.grass : mats.grassDark,
side * 10.2, top + 0.05, centerZ, scene, false);
}
}
bank(-1, 9, 26, 1.05);
bank(1, 9, 26, 1.05);
bank(-1, -5, 9, 5.2);
bank(1, -5, 9, 5.2);
bank(-1, -26, -5, 5.2);
bank(1, -26, -5, 5.2);
function masonryWall(x, z0, z1, bottom, top) {
const segmentLength = 2.15;
const count = Math.ceil((z1 - z0) / segmentLength);
const layerCount = Math.max(1, Math.ceil((top - bottom) / 1.12));
const layerHeight = (top - bottom) / layerCount;
for (let layer = 0; layer < layerCount; layer++) {
for (let segment = 0; segment < count; segment++) {
const length = Math.min(segmentLength, z1 - (z0 + segment * segmentLength));
const shade = (segment + layer) % 3 === 0 ? mats.stoneDark :
(segment + layer) % 2 === 0 ? mats.stoneLight : mats.stone;
box(
1.28,
Math.max(0.1, layerHeight - 0.055),
Math.max(0.1, length - 0.055),
shade,
x,
bottom + (layer + 0.5) * layerHeight,
z0 + segment * segmentLength + length / 2,
scene,
false
);
}
}
box(1.48, 0.22, z1 - z0, mats.coping, x, top + 0.08, (z0 + z1) / 2, scene, false);
}
for (const side of [-1, 1]) {
masonryWall(side * 5.65, 9, 26, -0.62, 1.16);
masonryWall(side * 5.65, -5, 9, -0.62, 5.48);
masonryWall(side * 5.65, -26, -5, -0.62, 5.48);
}
// Chamber floor.
box(10.1, 0.35, 14, mats.stoneDark, 0, -0.48, 2, scene, false);
// ---------------------------------------------------------------------------
// Water surfaces, kept visually separate
// ---------------------------------------------------------------------------
function makeWater(width, length, z, color, opacity = 0.88) {
const group = new THREE.Group();
group.position.set(0, 0, z);
scene.add(group);
const waterMat = material(color, 0.28, {
transparent: true,
opacity,
metalness: 0.05,
side: THREE.DoubleSide
});
const surface = new THREE.Mesh(new THREE.PlaneGeometry(width, length), waterMat);
surface.rotation.x = -Math.PI / 2;
surface.receiveShadow = true;
group.add(surface);
const ripples = [];
for (let i = 0; i < 13; i++) {
const strip = box(
0.65 + (i % 4) * 0.28,
0.018,
0.075,
mats.ripple,
(i % 2 ? 1 : -1) * (0.7 + (i * 0.83) % 4.1),
0.035,
-length / 2 + 0.6 + ((i * 2.17) % (length - 1.2)),
group,
false
);
strip.receiveShadow = false;
ripples.push(strip);
}
return { group, ripples, length };
}
const lowerWater = makeWater(10.15, 16.8, 17.6, 0x31869d);
lowerWater.group.position.y = LOW_LEVEL;
const upperWater = makeWater(10.15, 20.8, -15.6, 0x4ba5af, 0.91);
upperWater.group.position.y = UPPER_LEVEL;
const chamberWater = makeWater(10.05, 13.9, 2, 0x287a92, 0.93);
chamberWater.group.position.y = LOW_LEVEL;
// ---------------------------------------------------------------------------
// Quay, steps, railings, and bridge
// ---------------------------------------------------------------------------
// Lower quay.
box(3.1, 0.30, 15.0, mats.coping, -7.75, 1.16, 17.75, scene, false);
// Raised chamber-side and upper quay.
box(3.1, 0.30, 30.5, mats.coping, -7.75, 5.60, -9.75, scene, false);
// Voxel steps connecting the dry lower quay to the raised chamber quay.
const stepCount = 12;
const stepRun = 5.0;
const stepRise = 4.45;
for (let i = 0; i < stepCount; i++) {
const z = 10.5 - (i + 0.5) * (stepRun / stepCount);
const top = 1.30 + (i + 1) * (stepRise / stepCount);
const bottom = 0.88;
box(3.05, top - bottom, stepRun / stepCount - 0.025,
i % 2 ? mats.stoneLight : mats.stone,
-7.75, (top + bottom) / 2, z, scene, false);
}
function railingAlongZ(x, z0, z1, y) {
const length = z1 - z0;
const count = Math.max(2, Math.ceil(length / 2.5));
for (let i = 0; i <= count; i++) {
const z = z0 + (length * i / count);
box(0.18, 1.0, 0.18, mats.woodDark, x, y + 0.5, z);
}
box(0.16, 0.17, length, mats.wood, x, y + 0.91, (z0 + z1) / 2);
box(0.13, 0.13, length, mats.woodLight, x, y + 0.43, (z0 + z1) / 2);
}
railingAlongZ(-9.28, 10.6, 25.2, 1.31);
railingAlongZ(-9.28, -24.8, 5.4, 5.75);
// Step railings use short blocky sections.
for (let i = 0; i < stepCount; i += 2) {
const z = 10.5 - (i + 1) * (stepRun / stepCount);
const y = 1.3 + (i + 1) * (stepRise / stepCount);
box(0.18, 0.95, 0.18, mats.woodDark, -9.28, y + 0.48, z);
box(0.15, 0.14, stepRun / stepCount * 2.1, mats.wood, -9.28, y + 0.87, z - stepRun / stepCount);
}
// Bridge across the upper reach.
const bridgeZ = -19;
const bridgeStart = -9.1;
const bridgeEnd = 9.1;
const bridgePieces = 14;
const bridgeLength = bridgeEnd - bridgeStart;
for (let i = 0; i < bridgePieces; i++) {
const pieceLength = bridgeLength / bridgePieces;
const x = bridgeStart + (i + 0.5) * pieceLength;
box(pieceLength - 0.025, 0.32, 2.2, i % 2 ? mats.woodLight : mats.wood,
x, 5.73, bridgeZ, scene, false);
}
for (const x of [-2.8, 3.1]) {
box(0.62, 1.65, 1.65, mats.stoneDark, x, 4.73, bridgeZ, scene, false);
}
for (const z of [bridgeZ - 1.12, bridgeZ + 1.12]) {
for (let x = bridgeStart; x <= bridgeEnd + 0.1; x += 2.35) {
box(0.18, 1.05, 0.18, mats.woodDark, x, 6.38, z);
}
box(bridgeLength, 0.17, 0.17, mats.wood, (bridgeStart + bridgeEnd) / 2, 6.82, z);
box(bridgeLength, 0.12, 0.12, mats.woodLight, (bridgeStart + bridgeEnd) / 2, 6.34, z);
}
// Small blocky lock keeper's hut.
box(3.2, 2.55, 3.6, mats.stoneLight, 8.5, 6.48, -1.6, scene, false);
box(3.55, 0.32, 3.95, mats.hullDark, 8.5, 7.90, -1.6, scene, false);
box(0.78, 0.95, 0.08, mats.window, 7.75, 6.82, -3.43, scene, false);
box(0.08, 0.95, 0.78, mats.window, 6.88, 6.82, -1.25, scene, false);
box(0.72, 1.65, 0.10, mats.woodDark, 8.9, 6.05, -3.44, scene, false);
// Trees and shrubs.
function tree(x, y, z, scale = 1) {
box(0.45 * scale, 1.9 * scale, 0.45 * scale,
mats.woodDark, x, y + 0.95 * scale, z);
box(1.8 * scale, 1.35 * scale, 1.65 * scale,
mats.grassDark, x, y + 2.35 * scale, z);
box(1.25 * scale, 1.05 * scale, 1.25 * scale,
mats.grass, x + 0.25 * scale, y + 3.22 * scale, z - 0.12 * scale);
}
tree(11.4, 5.25, -11.5, 1.0);
tree(12.2, 5.25, -14.2, 0.78);
tree(11.0, 5.25, -22.8, 0.9);
tree(11.7, 1.1, 14.4, 0.82);
tree(12.1, 1.1, 22.3, 0.65);
tree(-11.7, 5.25, -23.3, 0.82);
// Bollards.
for (const z of [12.2, 17.0, 22.2]) {
box(0.36, 0.72, 0.36, mats.iron, -6.55, 1.66, z);
}
// ---------------------------------------------------------------------------
// Working lock gates
// ---------------------------------------------------------------------------
function createGate(z, outwardSide) {
const root = new THREE.Group();
root.position.z = z;
scene.add(root);
const leaves = [];
function leaf(hingeX, direction) {
const pivot = new THREE.Group();
pivot.position.x = hingeX;
root.add(pivot);
for (let i = 0; i < 5; i++) {
box(0.91, 5.25, 0.40, i % 2 ? mats.wood : mats.woodLight,
direction * (0.48 + i * 0.94), 2.35, 0, pivot);
}
box(4.6, 0.24, 0.54, mats.woodDark, direction * 2.4, 0.85, 0, pivot);
box(4.6, 0.24, 0.54, mats.woodDark, direction * 2.4, 3.80, 0, pivot);
box(0.16, 5.3, 0.54, mats.iron, direction * 0.08, 2.35, 0, pivot);
box(0.16, 5.3, 0.54, mats.iron, direction * 4.72, 2.35, 0, pivot);
leaves.push({ pivot, direction });
return pivot;
}
leaf(-4.85, 1);
leaf(4.85, -1);
// Stone gate frame.
box(1.05, 6.15, 1.1, mats.stoneLight, -5.15, 2.72, z);
box(1.05, 6.15, 1.1, mats.stoneLight, 5.15, 2.72, z);
box(11.35, 0.65, 1.1, mats.coping, 0, 5.92, z);
return {
z,
outwardSide,
leaves,
openness: 0,
target: 0
};
}
const lowerGate = createGate(9, 1);
const upperGate = createGate(-5, -1);
function updateGate(gate, dt) {
const speed = 1.25;
if (gate.openness < gate.target) {
gate.openness = Math.min(gate.target, gate.openness + dt * speed);
} else if (gate.openness > gate.target) {
gate.openness = Math.max(gate.target, gate.openness - dt * speed);
}
const angle = gate.openness * Math.PI / 2;
gate.leaves[0].pivot.rotation.y = -gate.outwardSide * angle;
gate.leaves[1].pivot.rotation.y = gate.outwardSide * angle;
}
// Small world-side level gauge.
box(0.20, 4.85, 0.98, mats.iron, 4.84, 2.05, 3.3, scene, false);
box(0.23, 4.25, 0.30, material(0x4c9fb0, 0.4), 4.70, 2.03, 3.3, scene, false);
const gaugeMarker = box(0.34, 0.15, 0.75, mats.orange, 4.60, LOW_LEVEL, 3.3, scene, false);
// ---------------------------------------------------------------------------
// Small voxel boat
// ---------------------------------------------------------------------------
const boat = new THREE.Group();
scene.add(boat);
box(2.55, 0.78, 3.85, mats.hull, 0, 0, 0.40, boat);
box(1.95, 0.72, 1.25, mats.hull, 0, 0.02, -2.00, boat);
box(1.05, 0.65, 0.60, mats.hull, 0, 0.05, -2.91, boat);
box(2.70, 0.18, 4.8, mats.woodDark, 0, 0.43, -0.05, boat);
box(2.15, 0.14, 3.9, mats.woodLight, 0, 0.54, 0.45, boat);
box(1.48, 1.15, 1.65, mats.cream, 0, 1.18, 0.78, boat);
box(1.70, 0.18, 1.88, mats.hullDark, 0, 1.85, 0.78, boat);
box(1.15, 0.50, 0.08, mats.window, 0, 1.40, 0.045, boat);
box(0.08, 0.50, 0.85, mats.window, 0.755, 1.40, 0.75, boat);
box(0.42, 0.62, 0.42, mats.iron, 0, 2.12, 1.14, boat);
box(0.12, 0.90, 0.12, mats.iron, 0, 2.67, 1.14, boat);
boat.position.set(0, LOW_LEVEL + 0.36, 18.2);
// ---------------------------------------------------------------------------
// Autonomous pedestrian and dry connected path
// ---------------------------------------------------------------------------
const walker = new THREE.Group();
scene.add(walker);
const walkerBody = new THREE.Group();
walker.add(walkerBody);
box(0.58, 0.82, 0.36, mats.coat, 0, 1.15, 0, walkerBody);
box(0.48, 0.30, 0.34, mats.skin, 0, 1.72, 0, walkerBody);
box(0.54, 0.16, 0.48, mats.orange, 0, 1.94, 0, walkerBody);
const legPivots = [];
for (const x of [-0.18, 0.18]) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.79, 0);
walker.add(pivot);
box(0.22, 0.65, 0.24, mats.trousers, 0, -0.31, 0, pivot);
box(0.24, 0.15, 0.38, mats.iron, 0, -0.66, 0.07, pivot);
legPivots.push(pivot);
}
const pathPoints = [
new THREE.Vector3(-7.75, 1.31, 24.1),
new THREE.Vector3(-7.75, 1.31, 10.5)
];
for (let i = 1; i <= stepCount; i++) {
pathPoints.push(new THREE.Vector3(
-7.75,
1.31 + i * (stepRise / stepCount),
10.5 - i * (stepRun / stepCount)
));
}
pathPoints.push(new THREE.Vector3(-7.75, 5.76, -19));
pathPoints.push(new THREE.Vector3(8.25, 5.90, -19));
const pathSegments = [];
let pathLength = 0;
for (let i = 0; i < pathPoints.length - 1; i++) {
const a = pathPoints[i];
const b = pathPoints[i + 1];
const length = a.distanceTo(b);
pathSegments.push({ a, b, length, start: pathLength });
pathLength += length;
}
function pathPoint(distance) {
const d = THREE.MathUtils.clamp(distance, 0, pathLength);
let segment = pathSegments[pathSegments.length - 1];
for (const candidate of pathSegments) {
if (d <= candidate.start + candidate.length) {
segment = candidate;
break;
}
}
const t = segment.length === 0 ? 0 :
THREE.MathUtils.clamp((d - segment.start) / segment.length, 0, 1);
const position = new THREE.Vector3().lerpVectors(segment.a, segment.b, t);
const direction = new THREE.Vector3().subVectors(segment.b, segment.a).normalize();
return { position, direction, segmentIndex: pathSegments.indexOf(segment) };
}
let walkerDistance = 0;
let walkerPhase = 0;
// ---------------------------------------------------------------------------
// Scene labels
// ---------------------------------------------------------------------------
textSprite('UPPER REACH +4.0 m', 0, 5.55, -13.2, 7.6);
textSprite('LOWER REACH 0.0 m', 0, 1.55, 18.4, 7.6);
textSprite('LOCK CHAMBER', 7.7, 7.35, 2.2, 6.4);
textSprite('QUAY → BRIDGE', -9.8, 7.15, -7.8, 7.2, '#2f6570');
textSprite('BRIDGE', 7.9, 7.55, -19, 5.4, '#805533');
// ---------------------------------------------------------------------------
// Lock cycle state machine
// ---------------------------------------------------------------------------
const stages = [
{
title: 'Boat waits at lower reach',
detail: 'The chamber is at the lower level and both gates are closed. The boat cannot enter yet.',
button: 'Open lower gate'
},
{
title: 'Open the lower gate',
detail: 'The lower gate may open only while the chamber and lower reach share the same water level.',
button: 'Admit the boat'
},
{
title: 'Bring the boat into the chamber',
detail: 'The boat waits for the lower gate to be fully open and for matching water levels.',
button: 'Close lower gate'
},
{
title: 'Secure the lower gate',
detail: 'The lower gate closes only after the entire boat has cleared the gate opening.',
button: 'Fill the chamber'
},
{
title: 'Fill the lock chamber',
detail: 'With the lower gate shut, the chamber level rises toward the upper reach.',
button: 'Open upper gate'
},
{
title: 'Open the upper gate',
detail: 'The upper gate waits for the chamber to reach the upper water level.',
button: 'Depart to upper reach'
},
{
title: 'Release the boat',
detail: 'The boat can leave only when the upper gate is open and the chamber remains at the upper level.',
button: 'Close upper gate'
},
{
title: 'Close the upper gate',
detail: 'The final gate waits for the boat to clear, then closes to secure the lock.',
button: 'Cycle complete'
}
];
let stage = 0;
let chamberLevel = LOW_LEVEL;
let chamberTarget = LOW_LEVEL;
let boatAction = 'waiting';
let lowerCloseRequested = false;
let fillRequested = false;
let upperOpenRequested = false;
let upperCloseRequested = false;
const advanceButton = document.querySelector('#advance');
const resetButton = document.querySelector('#reset');
const homeButton = document.querySelector('#home');
const stageTitle = document.querySelector('#stageTitle');
const stageDetail = document.querySelector('#stageDetail');
const stageCountElement = document.querySelector('#stepCount');
const levelText = document.querySelector('#levelText');
const meterFill = document.querySelector('#meterFill');
const lowerGateState = document.querySelector('#lowerGateState');
const upperGateState = document.querySelector('#upperGateState');
const boatStatus = document.querySelector('#boatStatus');
const walkerStatus = document.querySelector('#walkerStatus');
const dots = [...document.querySelectorAll('.dot')];
function applyStage(nextStage) {
stage = nextStage;
if (stage === 1) lowerGate.target = 1;
if (stage === 2) boatAction = 'enter';
if (stage === 3) lowerCloseRequested = true;
if (stage === 4) fillRequested = true;
if (stage === 5) upperOpenRequested = true;
if (stage === 6) boatAction = 'exit';
if (stage === 7) upperCloseRequested = true;
stageTitle.textContent = stages[stage].title;
stageDetail.textContent = stages[stage].detail;
stageCountElement.textContent = `${stage} / 7`;
advanceButton.textContent = stages[stage].button;
advanceButton.disabled = stage === 7;
dots.forEach((dot, i) => {
dot.classList.toggle('done', i < stage);
dot.classList.toggle('active', i === stage);
});
}
function resetCycle() {
stage = 0;
chamberLevel = LOW_LEVEL;
chamberTarget = LOW_LEVEL;
boatAction = 'waiting';
lowerCloseRequested = false;
fillRequested = false;
upperOpenRequested = false;
upperCloseRequested = false;
lowerGate.openness = 0;
lowerGate.target = 0;
upperGate.openness = 0;
upperGate.target = 0;
boat.position.set(0, LOW_LEVEL + 0.36, 18.2);
walkerDistance = 0;
walkerPhase = 0;
const point = pathPoint(0);
walker.position.copy(point.position);
walker.rotation.y = Math.atan2(point.direction.x, point.direction.z);
applyStage(0);
}
advanceButton.addEventListener('click', () => {
if (stage < stages.length - 1) applyStage(stage + 1);
});
resetButton.addEventListener('click', resetCycle);
homeButton.addEventListener('click', () => {
cameraState.theta = 0.73;
cameraState.phi = 0.97;
cameraState.radius = innerWidth < 700 ? 79 : 67;
cameraState.target.set(0, 1.5, -1);
});
window.addEventListener('keydown', (event) => {
if (event.code === 'Space' && stage < stages.length - 1) {
event.preventDefault();
applyStage(stage + 1);
}
if (event.key.toLowerCase() === 'r') resetCycle();
if (event.key.toLowerCase() === 'h') homeButton.click();
});
// ---------------------------------------------------------------------------
// Custom orbit controls, implemented without addons
// ---------------------------------------------------------------------------
const pointers = new Map();
let lastPointer = null;
let previousPinch = null;
renderer.domElement.addEventListener('pointerdown', (event) => {
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
renderer.domElement.setPointerCapture(event.pointerId);
lastPointer = { x: event.clientX, y: event.clientY };
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinch = Math.hypot(a.x - b.x, a.y - b.y);
}
});
renderer.domElement.addEventListener('pointermove', (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size >= 2) {
const [a, b] = [...pointers.values()];
const pinch = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinch) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (previousPinch / Math.max(1, pinch)),
42,
100
);
}
previousPinch = pinch;
return;
}
if (lastPointer) {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi - dy * 0.005, 0.43, 1.25);
}
lastPointer = { x: event.clientX, y: event.clientY };
});
function endPointer(event) {
pointers.delete(event.pointerId);
lastPointer = null;
previousPinch = null;
}
renderer.domElement.addEventListener('pointerup', endPointer);
renderer.domElement.addEventListener('pointercancel', endPointer);
renderer.domElement.addEventListener('pointerleave', (event) => {
if (event.buttons === 0) endPointer(event);
});
renderer.domElement.addEventListener('wheel', (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
42,
100
);
}, { passive: false });
renderer.domElement.addEventListener('dblclick', () => homeButton.click());
// ---------------------------------------------------------------------------
// Simulation and rendering
// ---------------------------------------------------------------------------
const clock = new THREE.Clock();
function moveToward(value, target, amount) {
if (value < target) return Math.min(target, value + amount);
if (value > target) return Math.max(target, value - amount);
return target;
}
function updateBoat(dt) {
const z = boat.position.z;
if (boatAction === 'enter') {
const gateReady = lowerGate.openness > 0.94;
const levelsReady = Math.abs(chamberLevel - LOW_LEVEL) < 0.05;
if (gateReady && levelsReady) {
boat.position.z = moveToward(z, 2.35, dt * 2.45);
if (Math.abs(boat.position.z - 2.35) < 0.025) {
boat.position.z = 2.35;
boatAction = 'inChamber';
}
}
} else if (boatAction === 'exit') {
const gateReady = upperGate.openness > 0.94;
const levelsReady = Math.abs(chamberLevel - UPPER_LEVEL) < 0.05;
if (gateReady && levelsReady) {
boat.position.z = moveToward(z, -16.0, dt * 2.65);
if (Math.abs(boat.position.z + 16.0) < 0.025) {
boat.position.z = -16.0;
boatAction = 'upperReach';
}
}
}
let localWaterLevel = LOW_LEVEL;
if (boat.position.z < -5) localWaterLevel = UPPER_LEVEL;
else if (boat.position.z <= 9) localWaterLevel = chamberLevel;
boat.position.y = localWaterLevel + 0.36 + Math.sin(clock.elapsedTime * 2.0) * 0.025;
boat.rotation.z = Math.sin(clock.elapsedTime * 1.3) * 0.006;
}
function updateWalker(dt) {
if (walkerDistance < pathLength) {
walkerDistance = Math.min(pathLength, walkerDistance + dt * 1.38);
walkerPhase += dt * 8.0;
}
const point = pathPoint(walkerDistance);
walker.position.copy(point.position);
const desiredYaw = Math.atan2(point.direction.x, point.direction.z);
const yawDelta = Math.atan2(
Math.sin(desiredYaw - walker.rotation.y),
Math.cos(desiredYaw - walker.rotation.y)
);
walker.rotation.y += yawDelta * Math.min(1, dt * 9);
const stride = walkerDistance < pathLength ? Math.sin(walkerPhase) * 0.48 : 0;
legPivots[0].rotation.x = stride;
legPivots[1].rotation.x = -stride;
walkerBody.position.y = Math.abs(Math.sin(walkerPhase)) * 0.035;
return point;
}
function gateDescription(gate) {
if (gate.openness > 0.96 && gate.target > 0.5) return ['OPEN', 'open'];
if (gate.openness < 0.04 && gate.target < 0.5) return ['CLOSED', 'closed'];
if (gate.target > gate.openness) return ['OPENING', 'moving'];
if (gate.target < gate.openness) return ['CLOSING', 'moving'];
return ['PART OPEN', 'moving'];
}
function setGateElement(element, gate) {
const [text, className] = gateDescription(gate);
element.className = `gate-state ${className}`;
element.querySelector('strong').textContent = text;
}
function setTravelElement(element, text, className) {
element.className = `travel-status ${className}`;
element.querySelector('span').textContent = text;
}
function updateHUD(walkerPoint) {
levelText.textContent = `${chamberLevel.toFixed(2)} m`;
const percent = THREE.MathUtils.clamp(
(chamberLevel - LOW_LEVEL) / (UPPER_LEVEL - LOW_LEVEL),
0,
1
) * 100;
meterFill.style.width = `${percent}%`;
setGateElement(lowerGateState, lowerGate);
setGateElement(upperGateState, upperGate);
let boatText = 'Waiting at lower reach';
let boatClass = 'waiting';
if (stage === 1) {
if (lowerGate.openness > 0.94) {
boatText = 'Ready to enter the chamber';
boatClass = 'safe';
} else {
boatText = 'Waiting for the lower gate to open';
}
} else if (stage === 2) {
if (boatAction === 'enter') {
if (lowerGate.openness < 0.94) {
boatText = 'Waiting: lower gate must be fully open';
} else if (Math.abs(chamberLevel - LOW_LEVEL) >= 0.05) {
boatText = 'Waiting: chamber must match lower level';
} else {
boatText = 'Entering the lock chamber';
boatClass = 'moving';
}
} else {
boatText = 'Safely inside the chamber';
boatClass = 'safe';
}
} else if (stage === 3) {
if (boatAction === 'enter') {
boatText = 'Waiting for the boat to clear the gate';
} else if (lowerGate.openness > 0.04) {
boatText = 'Lower gate closing';
boatClass = 'moving';
} else {
boatText = 'Lower gate secured';
boatClass = 'safe';
}
} else if (stage === 4) {
if (lowerGate.openness > 0.04) {
boatText = 'Waiting: lower gate must be closed';
} else if (chamberLevel < UPPER_LEVEL - 0.03) {
boatText = 'Chamber filling to upper level';
boatClass = 'moving';
} else {
boatText = 'Chamber at upper water level';
boatClass = 'safe';
}
} else if (stage === 5) {
if (chamberLevel < UPPER_LEVEL - 0.03) {
boatText = 'Waiting for upper water level';
} else if (upperGate.openness < 0.94) {
boatText = 'Upper gate opening';
boatClass = 'moving';
} else {
boatText = 'Ready to depart';
boatClass = 'safe';
}
} else if (stage === 6) {
if (upperGate.openness < 0.94) {
boatText = 'Waiting: upper gate must be open';
} else if (boatAction === 'exit') {
boatText = 'Departing into the upper reach';
boatClass = 'moving';
} else {
boatText = 'Boat clear of the upper gate';
boatClass = 'safe';
}
} else if (stage === 7) {
if (boat.position.z > -8.6) {
boatText = 'Waiting for the boat to clear';
} else if (upperGate.openness > 0.04) {
boatText = 'Upper gate closing';
boatClass = 'moving';
} else {
boatText = 'Lock cycle complete';
boatClass = 'safe';
}
}
setTravelElement(boatStatus, boatText, boatClass);
let walkerText = 'Walking on the dry quay';
if (walkerDistance >= pathLength - 0.05) {
walkerText = 'Arrived at the bridge';
} else if (walkerPoint.position.z < 10.5 && walkerPoint.position.z > 5.5) {
walkerText = 'Climbing the lock-side steps';
} else if (walkerPoint.position.z < -17 && walkerPoint.position.x < 7) {
walkerText = 'Crossing toward the bridge';
} else if (walkerPoint.position.z < -4) {
walkerText = 'Walking along the upper quay';
}
setTravelElement(walkerStatus, walkerText, 'moving');
}
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
// Interlocks: a gate request is only granted when the required state exists.
if (stage === 1 && !lowerCloseRequested &&
Math.abs(chamberLevel - LOW_LEVEL) < 0.05) {
lowerGate.target = 1;
}
if (lowerCloseRequested && boatAction !== 'enter' && boat.position.z < 5.8) {
lowerGate.target = 0;
}
if (fillRequested && lowerGate.openness < 0.04) {
chamberTarget = UPPER_LEVEL;
}
if (upperOpenRequested && chamberLevel > UPPER_LEVEL - 0.025) {
upperGate.target = 1;
}
if (upperCloseRequested && boat.position.z < -8.6) {
upperGate.target = 0;
}
updateGate(lowerGate, dt);
updateGate(upperGate, dt);
chamberLevel = moveToward(chamberLevel, chamberTarget, dt * 0.72);
if (Math.abs(chamberLevel - chamberTarget) < 0.006) chamberLevel = chamberTarget;
chamberWater.group.position.y = chamberLevel;
gaugeMarker.position.y = chamberLevel;
updateBoat(dt);
const walkerPoint = updateWalker(dt);
// Gentle movement in the water shimmer strips.
for (const water of [lowerWater, upperWater, chamberWater]) {
for (const ripple of water.ripples) {
ripple.position.z += dt * 0.10;
if (ripple.position.z > water.length / 2 - 0.4) {
ripple.position.z = -water.length / 2 + 0.4;
}
}
}
updateCamera();
updateHUD(walkerPoint);
renderer.render(scene, camera);
}
window.addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
resetCycle();
animate();
</script>
</body>
</html>
```