Prompt specification
Build or customize the Three.js 3D scene "Canal Lock Puzzle Garden" 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: Canal Lock Puzzle Garden
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/canal-lock-puzzle-garden-x3519
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts water levels, connected tanks, and lock gates. Build a fictional garden lock with upper and lower pools, doors, and a floating platform. Enforce water-level conditions before doors can open. Voxel style.
Working Three.js Starter (canal-lock-puzzle-garden-x3519):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Garden Lock</title>
<style>
:root {
--ink: #17252b;
--panel: rgba(19, 36, 42, 0.91);
--panel-soft: rgba(27, 48, 55, 0.84);
--line: rgba(190, 226, 226, 0.22);
--text: #e9f5ef;
--muted: #9fb5b3;
--green: #8ee3a6;
--yellow: #f2cf70;
--red: #f08a78;
--cyan: #6dd9ed;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #9ecbd2;
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}
canvas {
position: fixed;
inset: 0;
display: block;
z-index: 0;
outline: none;
image-rendering: pixelated;
touch-action: none;
}
#hud {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.panel {
pointer-events: auto;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 12px 35px rgba(10, 24, 29, 0.24);
backdrop-filter: blur(9px);
}
#brand {
position: absolute;
top: 16px;
left: 16px;
width: min(360px, calc(100vw - 32px));
padding: 16px 18px;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 6px 0 8px;
font-size: clamp(23px, 3vw, 34px);
line-height: 1;
letter-spacing: -0.06em;
text-transform: uppercase;
}
#brand p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.hint {
margin-top: 11px !important;
color: #c5d7d2 !important;
}
#telemetry {
position: absolute;
top: 16px;
right: 16px;
width: min(370px, calc(100vw - 32px));
padding: 13px;
}
.telemetry-heading {
display: flex;
justify-content: space-between;
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.readouts {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.readout {
min-width: 0;
padding: 9px;
border: 1px solid rgba(190, 226, 226, 0.12);
background: rgba(255, 255, 255, 0.035);
}
.readout span {
display: block;
overflow: hidden;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.08em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.readout strong {
display: block;
margin-top: 6px;
font-size: 16px;
white-space: nowrap;
}
.meter {
height: 6px;
margin-top: 10px;
overflow: hidden;
background: rgba(255, 255, 255, 0.09);
}
#chamberFill {
width: 0%;
height: 100%;
background: var(--cyan);
transition: width 100ms linear;
}
#console {
position: absolute;
bottom: 16px;
left: 50%;
width: min(1120px, calc(100vw - 32px));
transform: translateX(-50%);
padding: 12px;
}
#status {
min-height: 32px;
margin-bottom: 10px;
padding: 8px 10px;
border-left: 3px solid var(--cyan);
background: rgba(255, 255, 255, 0.04);
color: #dcebe6;
font-size: 11px;
line-height: 1.4;
}
#status.good {
border-left-color: var(--green);
}
#status.warn {
border-left-color: var(--yellow);
}
#status.bad {
border-left-color: var(--red);
}
.console-grid {
display: grid;
grid-template-columns: 1fr 1fr 1.65fr;
gap: 10px;
}
.group {
padding: 10px;
border: 1px solid rgba(190, 226, 226, 0.13);
background: var(--panel-soft);
}
.group-title {
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
button {
min-height: 35px;
flex: 1 1 auto;
border: 1px solid rgba(190, 226, 226, 0.25);
border-radius: 0;
background: #29454e;
color: var(--text);
cursor: pointer;
font: 700 10px/1.15 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
letter-spacing: 0.05em;
padding: 9px 10px;
text-transform: uppercase;
transition:
background 120ms ease,
border-color 120ms ease,
transform 80ms ease;
}
button:hover {
border-color: var(--cyan);
background: #345a64;
}
button:active {
transform: translateY(1px);
}
button.active {
border-color: var(--yellow);
background: #5c5232;
color: #fff2bd;
}
button.open {
border-color: var(--green);
background: #315f49;
color: #d9ffe4;
}
button.ready:not(.open) {
border-color: var(--green);
}
.small-note {
margin-top: 8px;
color: #90aaa7;
font-size: 9px;
line-height: 1.45;
}
.gate-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.gate-card {
min-width: 0;
padding: 8px;
border: 1px solid rgba(190, 226, 226, 0.12);
background: rgba(0, 0, 0, 0.1);
}
.gate-card button {
width: 100%;
display: flex;
justify-content: space-between;
gap: 8px;
align-items: center;
}
.gate-card button strong {
color: var(--yellow);
font-size: 9px;
}
.gate-card button.open strong {
color: var(--green);
}
.conditions {
display: grid;
gap: 4px;
margin: 8px 0 0;
padding: 0;
list-style: none;
}
.conditions li {
position: relative;
padding-left: 14px;
color: #8fa5a3;
font-size: 8.5px;
line-height: 1.35;
}
.conditions li::before {
position: absolute;
left: 0;
content: "×";
color: var(--red);
font-weight: 900;
}
.conditions li.ok {
color: #bce4c8;
}
.conditions li.ok::before {
content: "✓";
color: var(--green);
}
@media (max-width: 850px) {
#brand {
max-width: 290px;
}
#brand p {
display: none;
}
#telemetry {
top: auto;
bottom: 316px;
right: 12px;
width: 250px;
}
#console {
bottom: 12px;
width: calc(100vw - 24px);
max-height: 295px;
overflow-y: auto;
}
.console-grid {
grid-template-columns: 1fr;
}
}
@media (max-width: 520px) {
#brand {
top: 10px;
left: 10px;
}
#telemetry {
right: 10px;
bottom: 310px;
width: 220px;
}
.readouts {
gap: 4px;
}
.readout {
padding: 7px;
}
.readout strong {
font-size: 13px;
}
#console {
max-height: 290px;
}
}
</style>
</head>
<body>
<div id="hud">
<section id="brand" class="panel">
<div class="eyebrow">Hydraulic Basin System 04</div>
<h1>Garden Lock</h1>
<p>
Three connected voxel tanks share water through two lock doors.
A door cannot move unless hydraulic levels match and the floating
platform has cleared its approach.
</p>
<p class="hint">
Drag: orbit · Wheel: zoom · Click a gate: operate · Keys:
F fill / D drain / ← → platform / U upper gate / L lower gate
</p>
</section>
<section id="telemetry" class="panel">
<div class="telemetry-heading">
<span>Live Water Telemetry</span>
<span id="headRead">Head 2.20 m</span>
</div>
<div class="readouts">
<div class="readout">
<span>Upper pool</span>
<strong id="upperRead">3.35 m</strong>
</div>
<div class="readout">
<span>Lock chamber</span>
<strong id="chamberRead">1.15 m</strong>
</div>
<div class="readout">
<span>Lower pool</span>
<strong id="lowerRead">1.15 m</strong>
</div>
</div>
<div class="meter"><div id="chamberFill"></div></div>
</section>
<section id="console" class="panel">
<div id="status" class="good">
System ready. The lower gate is ready to open: chamber level matches
and the platform is parked on the upper side.
</div>
<div class="console-grid">
<div class="group">
<div class="group-title">Connected Tank Valves</div>
<div class="button-row">
<button id="fillButton" type="button">Fill from upper</button>
<button id="drainButton" type="button">Drain to lower</button>
</div>
<div class="small-note">
Close both doors before moving water. Flow stops automatically
when the chamber reaches its target level.
</div>
</div>
<div class="group">
<div class="group-title">Floating Platform</div>
<div class="button-row">
<button id="platformLeft" type="button">← Upper side</button>
<button id="platformRight" type="button">Lower side →</button>
</div>
<div id="platformPosition" class="small-note">
Platform parked on the upper side.
</div>
</div>
<div class="group">
<div class="group-title">Lock Door Interlocks</div>
<div class="gate-grid">
<div class="gate-card">
<button id="upperGateButton" type="button">
<span>Upper door</span>
<strong id="upperGateState">CLOSED</strong>
</button>
<ul class="conditions">
<li id="upperWaterCheck">Water levels must match</li>
<li id="upperPlatformCheck">Platform must clear approach</li>
</ul>
</div>
<div class="gate-card">
<button id="lowerGateButton" type="button">
<span>Lower door</span>
<strong id="lowerGateState">CLOSED</strong>
</button>
<ul class="conditions">
<li id="lowerWaterCheck">Water levels must match</li>
<li id="lowerPlatformCheck">Platform must clear approach</li>
</ul>
</div>
</div>
</div>
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM helpers
// -------------------------------------------------------------------------
const $ = (id) => document.getElementById(id);
// -------------------------------------------------------------------------
// Renderer, scene, camera
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: false,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.5));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setClearColor(0x9ecbd2);
renderer.domElement.setAttribute("aria-label", "Interactive voxel garden lock");
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9ecbd2);
scene.fog = new THREE.Fog(0x9ecbd2, 31, 68);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraTarget = new THREE.Vector3(0, 1.65, 0);
const orbit = {
theta: 0.69,
phi: 0.98,
radius: 27,
dragging: false,
moved: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemi = new THREE.HemisphereLight(0xdff7ff, 0x48523d, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0c6, 3.1);
sun.position.set(-11, 20, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x8bd7ff, 0.7);
fillLight.position.set(14, 9, -10);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Materials and primitive helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.0,
...options
});
}
const materials = {
soil: standardMaterial(0x51483b),
grass: standardMaterial(0x647d45),
grassLight: standardMaterial(0x789453),
grassDark: standardMaterial(0x506a3e),
stone: standardMaterial(0x68757a),
stoneLight: standardMaterial(0x89969a),
stoneDark: standardMaterial(0x4c595f),
stoneWarm: standardMaterial(0x7d7c70),
floor: standardMaterial(0x677477),
wood: standardMaterial(0x65442d),
woodLight: standardMaterial(0x936844),
woodDark: standardMaterial(0x3e2c21),
metal: standardMaterial(0x33454c, { metalness: 0.45, roughness: 0.55 }),
brass: standardMaterial(0xb99645, { metalness: 0.52, roughness: 0.45 }),
leaf: standardMaterial(0x446c3d),
leafLight: standardMaterial(0x5d8548),
flower: standardMaterial(0xe8a0a0),
cloud: standardMaterial(0xf2f3dc),
yellow: standardMaterial(0xe1b94b),
red: standardMaterial(0x9f5145),
white: standardMaterial(0xe6eee1),
dark: standardMaterial(0x243338)
};
function box(
parent,
width,
height,
depth,
x,
y,
z,
material,
castShadow = true
) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function groupAt(x, y, z, parent = scene) {
const group = new THREE.Group();
group.position.set(x, y, z);
parent.add(group);
return group;
}
// -------------------------------------------------------------------------
// World and decorative voxel terrain
// -------------------------------------------------------------------------
const world = groupAt(0, 0, 0);
box(world, 39, 0.8, 22, 0, -0.65, 0, materials.soil);
box(world, 39, 0.18, 22, 0, -0.18, 0, materials.grass);
// Grass variation.
for (let i = 0; i < 44; i++) {
const x = -18 + (i * 7.13) % 36;
const z = i % 2 === 0 ? -7.8 - (i % 3) * 0.5 : 6.8 + (i % 2) * 0.35;
box(
world,
1.4 + (i % 3) * 0.45,
0.12,
1.0 + (i % 2) * 0.3,
x,
-0.02,
z,
i % 2 ? materials.grassLight : materials.grassDark,
false
);
}
// Foreground cobble path.
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 18; col++) {
const x = -16 + col * 1.85 + (row % 2) * 0.45;
const z = 5.2 + row * 0.8;
box(
world,
1.55,
0.16,
0.62,
x,
0.02,
z,
(row + col) % 3 === 0 ? materials.stoneLight : materials.stone,
false
);
}
}
function addTree(x, z, scale = 1) {
const tree = groupAt(x, 0, z, world);
tree.scale.setScalar(scale);
box(tree, 0.55, 2.7, 0.55, 0, 1.25, 0, materials.wood);
box(tree, 2.7, 1.25, 2.0, 0, 3.0, 0, materials.leaf);
box(tree, 1.8, 1.0, 2.7, -0.35, 3.6, 0.1, materials.leafLight);
box(tree, 2.1, 0.9, 1.8, 0.5, 4.2, -0.15, materials.leaf);
box(tree, 1.0, 0.85, 1.0, -0.7, 4.1, 0.3, materials.leafLight);
}
addTree(-16.5, -6.8, 1.2);
addTree(-8.8, -7.4, 0.92);
addTree(8.8, -7.3, 1.05);
addTree(16.2, -6.5, 1.15);
function addFlower(x, z, color = materials.flower) {
box(world, 0.08, 0.42, 0.08, x, 0.2, z, materials.leafDark, false);
box(world, 0.23, 0.2, 0.23, x, 0.48, z, color, false);
}
for (let i = 0; i < 20; i++) {
const x = -17 + i * 1.75;
addFlower(x, -5.5 - (i % 3) * 0.28, i % 2 ? materials.flower : materials.yellow);
}
function addCloud(x, y, z, scale) {
const cloud = groupAt(x, y, z, world);
cloud.scale.setScalar(scale);
box(cloud, 3.0, 0.85, 1.0, 0, 0, 0, materials.cloud, false);
box(cloud, 1.8, 1.2, 1.1, -0.65, 0.5, 0, materials.cloud, false);
box(cloud, 1.6, 0.75, 1.0, 0.95, 0.25, 0, materials.cloud, false);
}
addCloud(-10, 10.5, -14, 1.2);
addCloud(8, 12, -18, 1.0);
addCloud(18, 9.5, -11, 0.85);
// Voxel sun.
const sunBlock = box(world, 2.0, 2.0, 2.0, -15, 13, -18,
standardMaterial(0xffdd78, {
emissive: 0xffb83d,
emissiveIntensity: 1.5,
roughness: 1
}),
false
);
sunBlock.rotation.z = Math.PI * 0.08;
// -------------------------------------------------------------------------
// Masonry basin construction
// -------------------------------------------------------------------------
const masonryColors = [
materials.stone,
materials.stoneLight,
materials.stoneDark,
materials.stoneWarm
];
function buildLongWall(x0, x1, z, height, depth = 0.55) {
let row = 0;
for (let y = 0.28; y < height; y += 0.62) {
let x = x0 - (row % 2) * 0.52;
while (x < x1) {
const width = Math.min(1.08, x1 - x);
const start = Math.max(x, x0);
if (width > 0.12) {
const material = masonryColors[(row + Math.floor(start * 2)) % masonryColors.length];
box(
world,
width - 0.035,
0.56,
depth,
start + width * 0.5,
y + 0.28,
z,
material
);
}
x += 1.08;
}
row++;
}
}
function buildEndWall(x, z0, z1, height, depth = 0.55) {
let row = 0;
for (let y = 0.28; y < height; y += 0.62) {
let z = z0 - (row % 2) * 0.5;
while (z < z1) {
const length = Math.min(1.05, z1 - z);
const start = Math.max(z, z0);
if (length > 0.12) {
box(
world,
depth,
0.56,
length - 0.035,
x,
y + 0.28,
start + length * 0.5,
masonryColors[(row + Math.floor(start)) % masonryColors.length]
);
}
z += 1.05;
}
row++;
}
}
// Pool floors.
box(world, 10.2, 0.38, 6.3, -10, 0.03, 0, materials.floor);
box(world, 10.2, 0.38, 6.3, 0, 0.03, 0, materials.floor);
box(world, 10.2, 0.38, 6.3, 10, 0.03, 0, materials.floor);
// Upper pool retaining walls.
buildLongWall(-15, -5, -3.05, 4.55);
buildLongWall(-15, -5, 3.05, 1.05, 0.48);
buildEndWall(-15, -3.05, 3.05, 4.55);
// Lock chamber cutaway walls.
buildLongWall(-5, 5, -3.05, 2.0, 0.48);
buildLongWall(-5, 5, 3.05, 0.8, 0.48);
// Lower pool retaining walls.
buildLongWall(5, 15, -3.05, 2.7);
buildLongWall(5, 15, 3.05, 0.95, 0.48);
buildEndWall(15, -3.05, 3.05, 2.7);
// Coping stones.
for (let x = -15; x <= 15; x += 1.1) {
box(world, 0.92, 0.16, 0.72, x, 4.63, -3.05, materials.stoneLight);
box(world, 0.92, 0.16, 0.62, x, 1.08, 3.05, materials.stoneLight);
}
// -------------------------------------------------------------------------
// Water volumes
// -------------------------------------------------------------------------
const UPPER_LEVEL = 3.35;
const LOWER_LEVEL = 1.15;
const WATER_BOTTOM = 0.34;
const upperWaterMaterial = standardMaterial(0x237fa4, {
transparent: true,
opacity: 0.57,
depthWrite: false,
roughness: 0.28,
metalness: 0.08
});
const chamberWaterMaterial = standardMaterial(0x259eb0, {
transparent: true,
opacity: 0.53,
depthWrite: false,
roughness: 0.25,
metalness: 0.05
});
const lowerWaterMaterial = standardMaterial(0x287e91, {
transparent: true,
opacity: 0.58,
depthWrite: false,
roughness: 0.3,
metalness: 0.04
});
const surfaceMaterials = {
upper: standardMaterial(0x54c5e1, {
transparent: true,
opacity: 0.75,
depthWrite: false,
roughness: 0.18
}),
chamber: standardMaterial(0x62d5d1, {
transparent: true,
opacity: 0.74,
depthWrite: false,
roughness: 0.18
}),
lower: standardMaterial(0x49b4c4, {
transparent: true,
opacity: 0.75,
depthWrite: false,
roughness: 0.2
})
};
const foamMaterial = standardMaterial(0xd7fbf3, {
transparent: true,
opacity: 0.7,
depthWrite: false,
roughness: 0.4
});
function createWater(x, width, level, bodyMaterial, surfaceMaterial, kind) {
const group = groupAt(x, 0, 0, world);
const body = box(
group,
width,
level - WATER_BOTTOM,
5.35,
0,
WATER_BOTTOM + (level - WATER_BOTTOM) * 0.5,
0,
bodyMaterial,
false
);
body.renderOrder = 1;
const surface = box(
group,
width - 0.04,
0.075,
5.31,
0,
level,
0,
surfaceMaterial,
false
);
surface.renderOrder = 2;
const foam = [];
for (let i = 0; i < 11; i++) {
const fx = -width * 0.42 + (i * 1.73) % (width * 0.84);
const fz = -2.25 + ((i * 1.37) % 4.5);
const patch = box(
group,
0.55 + (i % 3) * 0.18,
0.035,
0.18 + (i % 2) * 0.12,
fx,
level + 0.055,
fz,
foamMaterial,
false
);
patch.renderOrder = 3;
foam.push({ mesh: patch, baseX: fx, phase: i * 1.37 });
}
return { group, body, surface, foam, level, kind };
}
const upperWater = createWater(
-10,
9.55,
UPPER_LEVEL,
upperWaterMaterial,
surfaceMaterials.upper,
"upper"
);
const chamberWater = createWater(
0,
9.55,
LOWER_LEVEL,
chamberWaterMaterial,
surfaceMaterials.chamber,
"chamber"
);
const lowerWater = createWater(
10,
9.55,
LOWER_LEVEL,
lowerWaterMaterial,
surfaceMaterials.lower,
"lower"
);
function updateWater(water, level, time) {
const height = Math.max(0.05, level - WATER_BOTTOM);
water.body.scale.y = height;
water.body.position.y = WATER_BOTTOM + height * 0.5;
water.surface.position.y = level;
water.level = level;
for (const foam of water.foam) {
foam.mesh.position.y = level + 0.052 + Math.sin(time * 1.7 + foam.phase) * 0.018;
foam.mesh.position.x = foam.baseX + Math.sin(time * 0.72 + foam.phase) * 0.18;
}
}
// -------------------------------------------------------------------------
// 3D labels made with local canvas textures
// -------------------------------------------------------------------------
function createLabel(text, color = "#e9f5ef") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.NearestFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(4.4, 1.1, 1);
sprite.renderOrder = 20;
const label = { sprite, canvas, context, texture, color };
function draw(value) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(20, 37, 43, 0.88)";
context.fillRect(8, 18, 496, 92);
context.fillStyle = color;
context.fillRect(8, 18, 9, 92);
context.font = "bold 38px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(value, 260, 64);
texture.needsUpdate = true;
}
draw(text);
label.draw = draw;
return label;
}
const upperLabel = createLabel("UPPER POOL // 3.35 m", "#79d8f0");
upperLabel.sprite.position.set(-10, 5.25, 3.35);
scene.add(upperLabel.sprite);
const chamberLabel = createLabel("LOCK // 1.15 m", "#8ee3a6");
chamberLabel.sprite.position.set(0, 5.75, 3.35);
scene.add(chamberLabel.sprite);
const lowerLabel = createLabel("LOWER POOL // 1.15 m", "#79d8f0");
lowerLabel.sprite.position.set(10, 3.55, 3.35);
scene.add(lowerLabel.sprite);
// -------------------------------------------------------------------------
// Lock gates
// -------------------------------------------------------------------------
const gateHitMeshes = [];
function createGate(x, key) {
const frame = groupAt(x, 0, 0, world);
// Frame piers.
box(frame, 0.82, 5.15, 0.82, 0, 2.48, -3.0, materials.stoneDark);
box(frame, 0.82, 5.15, 0.82, 0, 2.48, 3.0, materials.stoneDark);
box(frame, 1.16, 0.52, 7.05, 0, 5.02, 0, materials.stoneLight);
// Decorative capstones.
box(frame, 1.2, 0.28, 1.1, 0, 5.42, -3.0, materials.stoneLight);
box(frame, 1.2, 0.28, 1.1, 0, 5.42, 3.0, materials.stoneLight);
// Hydraulic-looking side mechanism.
box(frame, 0.55, 1.0, 0.65, 0, 4.35, 3.55, materials.metal);
box(frame, 0.22, 1.65, 0.22, 0, 4.5, 3.88, materials.brass);
box(frame, 0.72, 0.24, 0.72, 0, 3.78, 3.55, materials.yellow);
const leaf = groupAt(x, 2.35, 0, world);
leaf.userData.gateKey = key;
// Gate backing and vertical planks.
const backing = box(
leaf,
0.34,
4.02,
5.5,
0,
0,
0,
materials.woodDark
);
gateHitMeshes.push(backing);
for (let i = 0; i < 9; i++) {
const plank = box(
leaf,
0.48,
3.96,
0.53,
0,
0,
-2.45 + i * 0.61,
i % 2 ? materials.wood : materials.woodLight
);
plank.userData.gateKey = key;
gateHitMeshes.push(plank);
}
// Reinforcing bands.
for (const y of [-1.25, 0, 1.25]) {
const band = box(
leaf,
0.62,
0.2,
5.75,
0,
y,
0,
materials.metal
);
band.userData.gateKey = key;
gateHitMeshes.push(band);
}
// Diagonal braces, constructed from voxel-sized blocks.
const braceMaterial = materials.metal;
for (let i = 0; i < 6; i++) {
const brace = box(
leaf,
0.67,
0.19,
0.63,
0,
-1.35 + i * 0.52,
-0.42,
braceMaterial
);
brace.rotation.x = -0.69;
brace.userData.gateKey = key;
gateHitMeshes.push(brace);
}
// Red / green state lamp.
const indicatorMaterial = standardMaterial(0xd05d4e, {
emissive: 0x6d130d,
emissiveIntensity: 0.6
});
const indicator = box(
frame,
0.34,
0.34,
0.34,
0,
4.76,
3.5,
indicatorMaterial
);
return {
key,
frame,
leaf,
indicator,
baseY: 2.35,
amount: 0,
open: false
};
}
const upperGate = createGate(-5, "upper");
const lowerGate = createGate(5, "lower");
// -------------------------------------------------------------------------
// Floating platform
// -------------------------------------------------------------------------
const platform = groupAt(-2.7, LOWER_LEVEL + 0.17, 0, world);
// Four block pontoons.
for (const x of [-0.78, 0.78]) {
for (const z of [-0.62, 0.62]) {
box(
platform,
1.12,
0.34,
0.42,
x,
-0.19,
z,
materials.metal
);
}
}
// Deck slats.
for (let i = 0; i < 7; i++) {
box(
platform,
0.29,
0.17,
1.62,
-1.05 + i * 0.35,
0.02,
0,
i % 2 ? materials.wood : materials.woodLight
);
}
// Small voxel cabin.
box(platform, 0.72, 0.65, 0.78, 0.63, 0.42, -0.3, materials.cream || materials.white);
box(platform, 0.82, 0.16, 0.9, 0.63, 0.81, -0.3, materials.red);
box(platform, 0.42, 0.25, 0.03, 0.63, 0.48, 0.1, materials.metal);
// Bollards and a little flag.
box(platform, 0.16, 0.45, 0.16, -0.85, 0.31, 0.65, materials.brass);
box(platform, 0.16, 0.45, 0.16, 0.82, 0.31, 0.65, materials.brass);
box(platform, 0.09, 1.35, 0.09, -0.92, 0.82, -0.48, materials.woodDark);
box(platform, 0.58, 0.36, 0.05, -0.62, 1.25, -0.48, materials.yellow);
// Mooring markers.
function createMooringMarker(x) {
const marker = groupAt(x, 0, 2.45, world);
box(marker, 0.7, 0.35, 0.7, 0, 0.17, 0, materials.stoneDark);
box(marker, 0.22, 1.35, 0.22, 0, 0.95, 0, materials.wood);
const capMaterial = standardMaterial(0x6f8d72, {
emissive: 0x183b26,
emissiveIntensity: 0.35
});
box(marker, 0.38, 0.22, 0.38, 0, 1.65, 0, capMaterial);
return capMaterial;
}
const leftMooringMaterial = createMooringMarker(-3.8);
const rightMooringMaterial = createMooringMarker(3.8);
// -------------------------------------------------------------------------
// Visible square hydraulic conduits
// -------------------------------------------------------------------------
function createConduit(x0, x1) {
const center = (x0 + x1) * 0.5;
const width = x1 - x0;
box(world, width, 0.16, 0.68, center, 0.27, 3.72, materials.stoneDark);
box(world, width, 0.34, 0.12, center, 0.44, 3.42, materials.metal);
box(world, width, 0.34, 0.12, center, 0.44, 4.02, materials.metal);
}
createConduit(-5, 0);
createConduit(0, 5);
const flowMaterial = standardMaterial(0x78e4f0, {
emissive: 0x1b8090,
emissiveIntensity: 0.7,
transparent: true,
opacity: 0.92
});
const fillFlow = [];
const drainFlow = [];
for (let i = 0; i < 9; i++) {
const fillParticle = box(
world,
0.24,
0.18,
0.24,
0,
0.49,
3.72,
flowMaterial,
false
);
fillFlow.push(fillParticle);
const drainParticle = box(
world,
0.24,
0.18,
0.24,
0,
0.49,
3.72,
flowMaterial,
false
);
drainFlow.push(drainParticle);
}
const fillLabel = createLabel("FILL", "#78e4f0");
fillLabel.sprite.scale.set(1.65, 0.42, 1);
fillLabel.sprite.position.set(-2.5, 1.05, 4.15);
scene.add(fillLabel.sprite);
const drainLabel = createLabel("DRAIN", "#78e4f0");
drainLabel.sprite.scale.set(1.65, 0.42, 1);
drainLabel.sprite.position.set(2.5, 1.05, 4.15);
scene.add(drainLabel.sprite);
// -------------------------------------------------------------------------
// State and operating rules
// -------------------------------------------------------------------------
const state = {
chamberLevel: LOWER_LEVEL,
valve: null,
upperGateOpen: false,
lowerGateOpen: false,
platformTarget: -2.7,
platformX: -2.7,
message:
"System ready. The lower gate is ready to open: chamber level matches and the platform is parked on the upper side.",
messageType: "good"
};
const LEVEL_TOLERANCE = 0.065;
function setMessage(message, type = "info") {
state.message = message;
state.messageType = type;
$("status").textContent = message;
$("status").className = type;
}
function isLevelMatch(a, b) {
return Math.abs(a - b) <= LEVEL_TOLERANCE;
}
function upperWaterReady() {
return isLevelMatch(state.chamberLevel, UPPER_LEVEL);
}
function lowerWaterReady() {
return isLevelMatch(state.chamberLevel, LOWER_LEVEL);
}
function upperPlatformReady() {
return state.platformX > 1.35;
}
function lowerPlatformReady() {
return state.platformX < -1.35;
}
function toggleGate(gateName) {
const isUpper = gateName === "upper";
const gate = isUpper ? upperGate : lowerGate;
if (gate.open) {
gate.open = false;
state[isUpper ? "upperGateOpen" : "lowerGateOpen"] = false;
setMessage(
`${isUpper ? "Upper" : "Lower"} gate closed. The chamber is sealed.`,
"good"
);
return;
}
if (state.valve) {
setMessage("Stop the water valve before operating a gate.", "warn");
return;
}
const waterReady = isUpper ? upperWaterReady() : lowerWaterReady();
const platformReady = isUpper ? upperPlatformReady() : lowerPlatformReady();
const targetLevel = isUpper ? UPPER_LEVEL : LOWER_LEVEL;
if (!waterReady) {
setMessage(
`${isUpper ? "Upper" : "Lower"} gate interlock: chamber is ${state.chamberLevel.toFixed(2)} m; required level is ${targetLevel.toFixed(2)} m.`,
"bad"
);
return;
}
if (!platformReady) {
setMessage(
`${isUpper ? "Upper" : "Lower"} gate interlock: move the floating platform to the ${isUpper ? "lower" : "upper"} side to clear the gate approach.`,
"warn"
);
return;
}
gate.open = true;
state[isUpper ? "upperGateOpen" : "lowerGateOpen"] = true;
setMessage(
`${isUpper ? "Upper" : "Lower"} gate unlocked. Water levels match and the platform has cleared the approach.`,
"good"
);
}
function toggleValve(mode) {
if (state.upperGateOpen || state.lowerGateOpen) {
setMessage(
"Both lock doors must be closed before water can be transferred.",
"warn"
);
return;
}
if (state.valve === mode) {
state.valve = null;
setMessage(`${mode === "fill" ? "Fill" : "Drain"} valve closed.`, "info");
return;
}
if (mode === "fill" && state.chamberLevel >= UPPER_LEVEL - 0.025) {
setMessage("The chamber is already at upper-pool level.", "info");
return;
}
if (mode === "drain" && state.chamberLevel <= LOWER_LEVEL + 0.025) {
setMessage("The chamber is already at lower-pool level.", "info");
return;
}
state.valve = mode;
setMessage(
mode === "fill"
? "Upper feed valve open. The lock chamber is filling toward 3.35 m."
: "Lower outlet valve open. The lock chamber is draining toward 1.15 m.",
"warn"
);
}
function movePlatform(direction) {
state.platformTarget = direction === "left" ? -2.7 : 2.7;
setMessage(
direction === "left"
? "Platform moving toward the upper side of the lock chamber."
: "Platform moving toward the lower side of the lock chamber.",
"info"
);
}
// -------------------------------------------------------------------------
// UI wiring
// -------------------------------------------------------------------------
$("fillButton").addEventListener("click", () => toggleValve("fill"));
$("drainButton").addEventListener("click", () => toggleValve("drain"));
$("platformLeft").addEventListener("click", () => movePlatform("left"));
$("platformRight").addEventListener("click", () => movePlatform("right"));
$("upperGateButton").addEventListener("click", () => toggleGate("upper"));
$("lowerGateButton").addEventListener("click", () => toggleGate("lower"));
document.addEventListener("keydown", (event) => {
if (event.repeat) return;
switch (event.key.toLowerCase()) {
case "f":
toggleValve("fill");
break;
case "d":
toggleValve("drain");
break;
case "arrowleft":
event.preventDefault();
movePlatform("left");
break;
case "arrowright":
event.preventDefault();
movePlatform("right");
break;
case "u":
toggleGate("upper");
break;
case "l":
toggleGate("lower");
break;
}
});
function setCondition(element, ok, goodText, badText) {
element.textContent = ok ? goodText : badText;
element.classList.toggle("ok", ok);
}
function refreshUI() {
$("upperRead").textContent = `${UPPER_LEVEL.toFixed(2)} m`;
$("chamberRead").textContent = `${state.chamberLevel.toFixed(2)} m`;
$("lowerRead").textContent = `${LOWER_LEVEL.toFixed(2)} m`;
$("headRead").textContent = `Head ${(UPPER_LEVEL - state.chamberLevel).toFixed(2)} m`;
const fillPercent =
((state.chamberLevel - LOWER_LEVEL) / (UPPER_LEVEL - LOWER_LEVEL)) * 100;
$("chamberFill").style.width = `${Math.max(0, Math.min(100, fillPercent))}%`;
$("fillButton").classList.toggle("active", state.valve === "fill");
$("drainButton").classList.toggle("active", state.valve === "drain");
const upperReady = upperWaterReady() && upperPlatformReady();
const lowerReady = lowerWaterReady() && lowerPlatformReady();
$("upperGateButton").classList.toggle("ready", upperReady);
$("upperGateButton").classList.toggle("open", state.upperGateOpen);
$("lowerGateButton").classList.toggle("ready", lowerReady);
$("lowerGateButton").classList.toggle("open", state.lowerGateOpen);
$("upperGateState").textContent = state.upperGateOpen ? "OPEN" : "CLOSED";
$("lowerGateState").textContent = state.lowerGateOpen ? "OPEN" : "CLOSED";
setCondition(
$("upperWaterCheck"),
upperWaterReady(),
`Water match: ${state.chamberLevel.toFixed(2)} / 3.35 m`,
`Water mismatch: ${state.chamberLevel.toFixed(2)} / 3.35 m`
);
setCondition(
$("upperPlatformCheck"),
upperPlatformReady(),
"Platform cleared: lower side",
"Platform must move to lower side"
);
setCondition(
$("lowerWaterCheck"),
lowerWaterReady(),
`Water match: ${state.chamberLevel.toFixed(2)} / 1.15 m`,
`Water mismatch: ${state.chamberLevel.toFixed(2)} / 1.15 m`
);
setCondition(
$("lowerPlatformCheck"),
lowerPlatformReady(),
"Platform cleared: upper side",
"Platform must move to upper side"
);
if (Math.abs(state.platformX - state.platformTarget) > 0.08) {
$("platformPosition").textContent = "Platform moving to its new mooring position…";
} else if (state.platformX < -1.35) {
$("platformPosition").textContent = "Platform parked on the upper side.";
} else if (state.platformX > 1.35) {
$("platformPosition").textContent = "Platform parked on the lower side.";
} else {
$("platformPosition").textContent = "Platform between moorings — gate approach is blocked.";
}
}
// -------------------------------------------------------------------------
// Direct camera controls, implemented without Three.js addons
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
orbit.dragging = true;
orbit.moved = false;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.lastX;
const dy = event.clientY - orbit.lastY;
if (Math.abs(dx) + Math.abs(dy) > 2) orbit.moved = true;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.38, 1.35);
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
const wasClick = orbit.dragging && !orbit.moved;
orbit.dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (!wasClick) return;
const rect = renderer.domElement.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(gateHitMeshes, false);
if (hits.length > 0) {
const gateKey = hits[0].object.userData.gateKey;
if (gateKey) toggleGate(gateKey);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
orbit.dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.015,
15,
42
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
let uiAccumulator = 0;
let labelAccumulator = 0;
function damp(current, target, lambda, dt) {
return THREE.MathUtils.lerp(current, target, 1 - Math.exp(-lambda * dt));
}
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
// Water transfer between connected tanks.
if (state.valve === "fill" && !state.upperGateOpen && !state.lowerGateOpen) {
state.chamberLevel = Math.min(
UPPER_LEVEL,
state.chamberLevel + dt * 0.34
);
if (state.chamberLevel >= UPPER_LEVEL - 0.001) {
state.chamberLevel = UPPER_LEVEL;
state.valve = null;
setMessage(
"Fill complete. The chamber matches the upper pool. Move the platform to the lower side before opening the upper gate.",
"good"
);
}
}
if (state.valve === "drain" && !state.upperGateOpen && !state.lowerGateOpen) {
state.chamberLevel = Math.max(
LOWER_LEVEL,
state.chamberLevel - dt * 0.31
);
if (state.chamberLevel <= LOWER_LEVEL + 0.001) {
state.chamberLevel = LOWER_LEVEL;
state.valve = null;
setMessage(
"Drain complete. The chamber matches the lower pool. Move the platform to the upper side before opening the lower gate.",
"good"
);
}
}
// Smooth platform motion and vertical bobbing.
state.platformX = damp(state.platformX, state.platformTarget, 4.8, dt);
platform.position.x = state.platformX;
platform.position.y =
state.chamberLevel + 0.17 + Math.sin(elapsed * 1.8) * 0.025;
platform.rotation.z = Math.sin(elapsed * 1.25) * 0.012;
// Mooring indicator states.
const leftActive = state.platformX < -1.35;
const rightActive = state.platformX > 1.35;
leftMooringMaterial.color.setHex(leftActive ? 0x8ee3a6 : 0x6f8d72);
leftMooringMaterial.emissive.setHex(leftActive ? 0x245b32 : 0x183b26);
rightMooringMaterial.color.setHex(rightActive ? 0x8ee3a6 : 0x6f8d72);
rightMooringMaterial.emissive.setHex(rightActive ? 0x245b32 : 0x183b26);
// Gate movement.
upperGate.amount = damp(upperGate.amount, upperGate.open ? 1 : 0, 4.2, dt);
lowerGate.amount = damp(lowerGate.amount, lowerGate.open ? 1 : 0, 4.2, dt);
upperGate.leaf.position.y = upperGate.baseY + upperGate.amount * 3.82;
lowerGate.leaf.position.y = lowerGate.baseY + lowerGate.amount * 3.82;
const upperIndicatorColor = upperGate.open ? 0x72e89a : 0xd05d4e;
const lowerIndicatorColor = lowerGate.open ? 0x72e89a : 0xd05d4e;
upperGate.indicator.material.color.setHex(upperIndicatorColor);
lowerGate.indicator.material.color.setHex(lowerIndicatorColor);
upperGate.indicator.material.emissive.setHex(
upperGate.open ? 0x1b7338 : 0x6d130d
);
lowerGate.indicator.material.emissive.setHex(
lowerGate.open ? 0x1b7338 : 0x6d130d
);
// Water surfaces.
updateWater(upperWater, UPPER_LEVEL, elapsed);
updateWater(chamberWater, state.chamberLevel, elapsed);
updateWater(lowerWater, LOWER_LEVEL, elapsed);
// Animated flow particles in the square conduits.
for (let i = 0; i < fillFlow.length; i++) {
const active = state.valve === "fill";
const particle = fillFlow[i];
particle.visible = active;
if (active) {
const progress = (elapsed * 0.55 + i / fillFlow.length) % 1;
particle.position.x = THREE.MathUtils.lerp(-4.8, -0.25, progress);
particle.position.y = 0.48 + Math.sin(elapsed * 8 + i) * 0.025;
}
}
for (let i = 0; i < drainFlow.length; i++) {
const active = state.valve === "drain";
const particle = drainFlow[i];
particle.visible = active;
if (active) {
const progress = (elapsed * 0.55 + i / drainFlow.length) % 1;
particle.position.x = THREE.MathUtils.lerp(0.25, 4.8, progress);
particle.position.y = 0.48 + Math.sin(elapsed * 8 + i) * 0.025;
}
}
// Periodic UI and dynamic label updates.
uiAccumulator += dt;
labelAccumulator += dt;
if (uiAccumulator > 0.08) {
refreshUI();
uiAccumulator = 0;
}
if (labelAccumulator > 0.2) {
chamberLabel.draw(`LOCK // ${state.chamberLevel.toFixed(2)} m`);
labelAccumulator = 0;
}
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.5));
});
refreshUI();
animate();
</script>
</body>
</html>
```