Prompt specification
Build or customize the Three.js 3D scene "Harbor tide gauge with shoreline map" 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: Harbor tide gauge with shoreline map
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/harbor-tide-gauge-with-shoreline-map-x1286
Scene Description Prompt:
A voxel harbor tide gauge beside a relief map; rising water changes exposed shoreline while the chart datum stays fixed. Three.js from cdnjs, one HTML file.
Working Three.js Starter (harbor-tide-gauge-with-shoreline-map-x1286):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Harbor — Tide Gauge</title>
<style>
:root {
--ink: #eaf8ff;
--muted: #8daab8;
--cyan: #53e1ea;
--orange: #ffb45b;
--panel: rgba(7, 20, 31, 0.78);
--border: rgba(126, 196, 218, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07131e;
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%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
z-index: 10;
pointer-events: none;
}
.hero {
position: absolute;
top: 28px;
left: 30px;
width: min(390px, calc(100vw - 60px));
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.35);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 1px;
background: var(--cyan);
content: "";
box-shadow: 0 0 9px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(34px, 4.5vw, 64px);
font-weight: 650;
letter-spacing: -0.055em;
line-height: 0.94;
}
h1 span {
display: block;
color: #91b6c5;
font-weight: 350;
}
.hero p {
max-width: 340px;
margin: 16px 0 0;
color: #a7c0ca;
font-size: 12px;
line-height: 1.65;
}
.readout {
position: absolute;
top: 28px;
right: 30px;
width: 258px;
padding: 18px;
overflow: hidden;
border: 1px solid var(--border);
border-radius: 14px;
background:
linear-gradient(145deg, rgba(18, 43, 57, 0.84), rgba(5, 17, 27, 0.76)),
var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
}
.readout::after {
position: absolute;
top: -30px;
right: -25px;
width: 100px;
height: 100px;
border-radius: 50%;
background: rgba(83, 225, 234, 0.09);
filter: blur(8px);
content: "";
pointer-events: none;
}
.panel-label {
color: #82a4b3;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.level {
display: flex;
align-items: flex-end;
gap: 7px;
margin: 10px 0 16px;
font-variant-numeric: tabular-nums;
}
.level strong {
font-size: 42px;
font-weight: 520;
letter-spacing: -0.055em;
line-height: 1;
}
.level small {
padding-bottom: 5px;
color: #8fabb8;
font-size: 14px;
}
.datum-row,
.shore-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 11px 0;
border-top: 1px solid rgba(132, 194, 214, 0.12);
font-size: 11px;
}
.datum-row span:first-child {
display: flex;
align-items: center;
gap: 8px;
color: #c4d9e1;
}
.datum-swatch {
width: 16px;
height: 2px;
background: var(--orange);
box-shadow: 0 0 8px rgba(255, 180, 91, 0.75);
}
.fixed {
padding: 3px 6px;
border: 1px solid rgba(255, 180, 91, 0.35);
border-radius: 4px;
color: var(--orange);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.12em;
}
.shore-row {
color: #8eabb8;
}
.shore-row b {
color: var(--ink);
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.legend {
position: absolute;
bottom: 29px;
left: 30px;
display: grid;
gap: 8px;
color: #9bb5c1;
font-size: 10px;
}
.legend div {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
}
.swatch.water {
background: #35b9c7;
box-shadow: 0 0 8px rgba(53, 185, 199, 0.45);
}
.swatch.land {
background: #78906b;
}
.swatch.shore {
border: 1px solid #c9fbff;
background: transparent;
}
.swatch.datum {
height: 2px;
background: var(--orange);
}
.controls {
position: absolute;
bottom: 28px;
left: 50%;
width: min(390px, calc(100vw - 48px));
padding: 14px 16px 13px;
transform: translateX(-50%);
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(15px);
pointer-events: auto;
}
.button-row {
display: flex;
gap: 8px;
margin-bottom: 13px;
}
button {
min-height: 31px;
padding: 0 12px;
border: 1px solid rgba(130, 190, 211, 0.2);
border-radius: 7px;
background: rgba(100, 151, 171, 0.08);
color: #bcd3dc;
font: inherit;
font-size: 10px;
font-weight: 650;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
color 160ms ease;
}
button:hover {
border-color: rgba(83, 225, 234, 0.45);
background: rgba(83, 225, 234, 0.09);
color: white;
}
button.primary {
display: flex;
flex: 1;
align-items: center;
justify-content: center;
gap: 8px;
border-color: rgba(83, 225, 234, 0.25);
color: #d8faff;
}
.cycle-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 9px var(--cyan);
animation: pulse 1.4s ease-in-out infinite;
}
.primary.paused .cycle-dot {
background: #78909a;
box-shadow: none;
animation: none;
}
@keyframes pulse {
50% {
opacity: 0.35;
transform: scale(0.75);
}
}
.slider-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
color: #94afba;
font-size: 10px;
}
output {
color: #e4f7fb;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 7px 0 4px;
border-radius: 4px;
outline: none;
appearance: none;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--fill, 0%),
rgba(117, 166, 183, 0.2) var(--fill, 0%),
rgba(117, 166, 183, 0.2) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
border: 2px solid #d8fcff;
border-radius: 50%;
appearance: none;
background: #27b9c6;
box-shadow: 0 0 12px rgba(83, 225, 234, 0.6);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 2px solid #d8fcff;
border-radius: 50%;
background: #27b9c6;
box-shadow: 0 0 12px rgba(83, 225, 234, 0.6);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #587783;
font-size: 8px;
}
.hint {
position: absolute;
right: 30px;
bottom: 31px;
color: #557581;
font-size: 9px;
letter-spacing: 0.03em;
}
@media (max-width: 760px) {
.hero {
top: 17px;
left: 17px;
}
.hero p {
display: none;
}
.readout {
top: 112px;
right: 16px;
width: 190px;
padding: 13px;
}
.level strong {
font-size: 33px;
}
.shore-row {
font-size: 9px;
}
.controls {
right: 16px;
bottom: 16px;
left: 16px;
width: auto;
transform: none;
}
.legend,
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<header class="hero">
<div class="eyebrow">Coastal observation / Harbor 04</div>
<h1>Voxel harbor<span>Tide & shoreline</span></h1>
<p>
The water level rises over a fixed voxel relief chart. The shoreline
redraws as terrain is covered; the orange chart datum remains anchored.
</p>
</header>
<aside class="readout">
<div class="panel-label">Gauge 04 / water level</div>
<div class="level">
<strong id="levelValue">+0.15</strong>
<small>m</small>
</div>
<div class="datum-row">
<span>
<i class="datum-swatch"></i>
Chart datum 0.00 m
</span>
<b class="fixed">FIXED</b>
</div>
<div class="shore-row">
<span>Exposed shoreline cells</span>
<b id="exposedValue">—</b>
</div>
</aside>
<div class="legend">
<div><i class="swatch water"></i> Water surface</div>
<div><i class="swatch land"></i> Exposed terrain</div>
<div><i class="swatch shore"></i> Moving shoreline</div>
<div><i class="swatch datum"></i> Fixed chart datum</div>
</div>
<section class="controls">
<div class="button-row">
<button id="cycleButton" class="primary" type="button">
<i class="cycle-dot"></i>
<span>Pause cycle</span>
</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<div class="slider-heading">
<label for="tideSlider">Manual tide height</label>
<output id="targetValue" for="tideSlider">+0.15 m</output>
</div>
<input
id="tideSlider"
type="range"
min="0.15"
max="1.25"
value="0.15"
step="0.01"
aria-label="Tide height"
/>
<div class="range-labels">
<span>+0.15 m / LOW</span>
<span>+1.25 m / HIGH</span>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const levelValue = document.getElementById("levelValue");
const exposedValue = document.getElementById("exposedValue");
const targetValue = document.getElementById("targetValue");
const tideSlider = document.getElementById("tideSlider");
const cycleButton = document.getElementById("cycleButton");
const resetButton = document.getElementById("resetButton");
const MIN_TIDE = 0.15;
const MAX_TIDE = 1.25;
const CHART_DATUM = 0.0;
let currentTide = MIN_TIDE;
let targetTide = MIN_TIDE;
let autoCycle = true;
let cycleTime = 0;
// ---------------------------------------------------------------------
// Renderer, scene and camera
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
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;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131e);
scene.fog = new THREE.Fog(0x07131e, 24, 43);
const camera = new THREE.OrthographicCamera(-7, 7, 4.4, -4.4, 0.1, 80);
const cameraTarget = new THREE.Vector3(-0.65, 0.35, 0);
let cameraAzimuth = 0.72;
let cameraPolar = 1.01;
let cameraZoom = 1;
function updateCamera() {
const radius = 19;
camera.position.set(
cameraTarget.x +
radius *
Math.sin(cameraPolar) *
Math.sin(cameraAzimuth),
cameraTarget.y + radius * Math.cos(cameraPolar),
cameraTarget.z +
radius *
Math.sin(cameraPolar) *
Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
const halfHeight = aspect < 1 ? 6.4 / aspect : 4.35;
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// ---------------------------------------------------------------------
// Lighting and presentation ground
// ---------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xa5d8e8, 0x263125, 1.65);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe1b3, 3.2);
sun.position.set(-4, 10, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -9;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x4fc7df, 1.25);
rim.position.set(8, 5, -7);
scene.add(rim);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(32, 32),
new THREE.MeshStandardMaterial({
color: 0x08151e,
roughness: 0.95,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.83;
ground.receiveShadow = true;
scene.add(ground);
const floorGrid = new THREE.GridHelper(
24,
48,
0x173746,
0x102b37
);
floorGrid.position.y = -0.82;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.45;
scene.add(floorGrid);
// ---------------------------------------------------------------------
// Voxel relief generation
// ---------------------------------------------------------------------
const NX = 46;
const NZ = 32;
const CELL = 0.2;
const MAP_W = NX * CELL;
const MAP_D = NZ * CELL;
const MAP_MIN_X = -6.2;
const MAP_MIN_Z = -3.2;
const MAP_CENTER_X = MAP_MIN_X + MAP_W * 0.5;
const TERRAIN_BOTTOM = -0.58;
const heights = new Float32Array(NX * NZ);
function terrainFormula(x, z) {
const nx = (x - MAP_CENTER_X) / (MAP_W * 0.5);
const nz = z / (MAP_D * 0.5);
const radial = Math.sqrt(
Math.pow(nx * 0.9, 2) + Math.pow(nz * 0.82, 2)
);
let height =
-0.08 +
1.28 * Math.max(0, 1 - radial) +
0.052 * Math.sin(x * 2.45 + z * 0.9) +
0.032 * Math.sin(x * 0.85 - z * 3.1);
// Broad harbor basin.
const bay =
Math.exp(
-1.45 *
(Math.pow((x - 1.25) / 1.25, 2) +
Math.pow((z - 0.1) / 1.35, 2))
);
height -= 0.72 * bay;
// Navigable channel running out through the front of the chart.
if (z > 0.25 && z < 3.2) {
const distance = Math.abs(x - 1.55);
const channel =
0.025 +
0.24 * Math.pow(Math.min(distance / 0.55, 1), 1.7);
height = Math.min(height, channel);
}
// A smaller eastern inlet.
const inlet =
Math.exp(
-1.8 *
(Math.pow((x - 2.15) / 0.8, 2) +
Math.pow((z + 0.85) / 0.85, 2))
);
height -= 0.46 * inlet;
return THREE.MathUtils.clamp(height, -0.12, 1.48);
}
for (let zIndex = 0; zIndex < NZ; zIndex++) {
for (let xIndex = 0; xIndex < NX; xIndex++) {
const x = MAP_MIN_X + (xIndex + 0.5) * CELL;
const z = MAP_MIN_Z + (zIndex + 0.5) * CELL;
heights[zIndex * NX + xIndex] = terrainFormula(x, z);
}
}
function sampleHeight(x, z) {
const fx = THREE.MathUtils.clamp(
(x - MAP_MIN_X) / CELL,
0,
NX - 1.001
);
const fz = THREE.MathUtils.clamp(
(z - MAP_MIN_Z) / CELL,
0,
NZ - 1.001
);
const ix = Math.floor(fx);
const iz = Math.floor(fz);
const tx = fx - ix;
const tz = fz - iz;
const h00 = heights[iz * NX + ix];
const h10 = heights[iz * NX + ix + 1];
const h01 = heights[(iz + 1) * NX + ix];
const h11 = heights[(iz + 1) * NX + ix + 1];
const a = THREE.MathUtils.lerp(h00, h10, tx);
const b = THREE.MathUtils.lerp(h01, h11, tx);
return THREE.MathUtils.lerp(a, b, tz);
}
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
const columns = new THREE.InstancedMesh(
unitBox,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0,
vertexColors: true
}),
NX * NZ
);
const caps = new THREE.InstancedMesh(
unitBox,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88,
metalness: 0,
vertexColors: true
}),
NX * NZ
);
columns.castShadow = true;
columns.receiveShadow = true;
caps.receiveShadow = true;
columns.frustumCulled = false;
caps.frustumCulled = false;
const sideColor = new THREE.Color();
const capColor = new THREE.Color();
const sand = new THREE.Color(0xa9946d);
const lowGreen = new THREE.Color(0x597b62);
const highGreen = new THREE.Color(0x829374);
let instanceIndex = 0;
for (let zIndex = 0; zIndex < NZ; zIndex++) {
for (let xIndex = 0; xIndex < NX; xIndex++) {
const height = heights[instanceIndex];
const x = MAP_MIN_X + (xIndex + 0.5) * CELL;
const z = MAP_MIN_Z + (zIndex + 0.5) * CELL;
const columnHeight = Math.max(0.03, height - TERRAIN_BOTTOM);
dummy.position.set(
x,
TERRAIN_BOTTOM + columnHeight * 0.5,
z
);
dummy.scale.set(CELL * 0.965, columnHeight, CELL * 0.965);
dummy.updateMatrix();
columns.setMatrixAt(instanceIndex, dummy.matrix);
sideColor.set(0x625c49);
sideColor.offsetHSL(
0,
0,
0.035 * Math.sin(xIndex * 1.7 + zIndex * 0.8)
);
columns.setColorAt(instanceIndex, sideColor);
dummy.position.set(x, height + 0.012, z);
dummy.scale.set(CELL * 0.985, 0.035, CELL * 0.985);
dummy.updateMatrix();
caps.setMatrixAt(instanceIndex, dummy.matrix);
if (height < 0.28) {
capColor.copy(sand);
} else if (height < 0.68) {
capColor.copy(lowGreen).lerp(sand, 0.16);
} else {
capColor.copy(highGreen);
}
capColor.offsetHSL(
0,
0,
0.026 * Math.sin(xIndex * 2.1 - zIndex * 1.3)
);
caps.setColorAt(instanceIndex, capColor);
instanceIndex++;
}
}
columns.instanceMatrix.needsUpdate = true;
caps.instanceMatrix.needsUpdate = true;
columns.instanceColor.needsUpdate = true;
caps.instanceColor.needsUpdate = true;
scene.add(columns, caps);
const mapBase = new THREE.Mesh(
unitBox,
new THREE.MeshStandardMaterial({
color: 0x283743,
roughness: 0.86,
metalness: 0.05
})
);
mapBase.position.set(
MAP_CENTER_X,
TERRAIN_BOTTOM - 0.13,
0
);
mapBase.scale.set(MAP_W + 0.25, 0.26, MAP_D + 0.25);
mapBase.castShadow = true;
mapBase.receiveShadow = true;
scene.add(mapBase);
const baseEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(mapBase.geometry),
new THREE.LineBasicMaterial({
color: 0x46606b,
transparent: true,
opacity: 0.42
})
);
baseEdges.position.copy(mapBase.position);
baseEdges.scale.copy(mapBase.scale);
scene.add(baseEdges);
// ---------------------------------------------------------------------
// Water surface, chart grid and moving shoreline
// ---------------------------------------------------------------------
const water = new THREE.Mesh(
new THREE.PlaneGeometry(MAP_W + 0.12, MAP_D + 0.12),
new THREE.MeshStandardMaterial({
color: 0x28b9c8,
transparent: true,
opacity: 0.68,
roughness: 0.22,
metalness: 0.08,
depthWrite: false,
side: THREE.DoubleSide
})
);
water.rotation.x = -Math.PI / 2;
water.position.set(MAP_CENTER_X, currentTide, 0);
water.renderOrder = 3;
scene.add(water);
const waterGridPositions = [];
for (let x = MAP_MIN_X; x <= MAP_MIN_X + MAP_W + 0.001; x += 0.4) {
waterGridPositions.push(
x,
0,
MAP_MIN_Z,
x,
0,
MAP_MIN_Z + MAP_D
);
}
for (let z = MAP_MIN_Z; z <= MAP_MIN_Z + MAP_D + 0.001; z += 0.4) {
waterGridPositions.push(
MAP_MIN_X,
0,
z,
MAP_MIN_X + MAP_W,
0,
z
);
}
const waterGridGeometry = new THREE.BufferGeometry();
waterGridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(waterGridPositions, 3)
);
const waterGrid = new THREE.LineSegments(
waterGridGeometry,
new THREE.LineBasicMaterial({
color: 0xb8fbff,
transparent: true,
opacity: 0.13,
depthWrite: false
})
);
waterGrid.position.set(MAP_CENTER_X, currentTide + 0.009, 0);
waterGrid.renderOrder = 4;
scene.add(waterGrid);
const shorePositions = new Float32Array(NX * NZ * 12);
const shoreGeometry = new THREE.BufferGeometry();
const shoreAttribute = new THREE.BufferAttribute(shorePositions, 3);
shoreAttribute.setUsage(THREE.DynamicDrawUsage);
shoreGeometry.setAttribute("position", shoreAttribute);
shoreGeometry.setDrawRange(0, 0);
const shoreLine = new THREE.LineSegments(
shoreGeometry,
new THREE.LineBasicMaterial({
color: 0xd2fbff,
transparent: true,
opacity: 0.88,
depthWrite: false
})
);
shoreLine.frustumCulled = false;
shoreLine.renderOrder = 5;
scene.add(shoreLine);
function updateShoreline(level) {
let pointer = 0;
function edgeIntersection(ax, az, bx, bz, ha, hb) {
const denominator = hb - ha;
if (Math.abs(denominator) < 0.000001) return 0.5;
const fraction = THREE.MathUtils.clamp(
(level - ha) / denominator,
0,
1
);
return [
THREE.MathUtils.lerp(ax, bx, fraction),
THREE.MathUtils.lerp(az, bz, fraction)
];
}
for (let iz = 0; iz < NZ - 1; iz++) {
for (let ix = 0; ix < NX - 1; ix++) {
const x0 = MAP_MIN_X + ix * CELL;
const x1 = x0 + CELL;
const z0 = MAP_MIN_Z + iz * CELL;
const z1 = z0 + CELL;
const h00 = heights[iz * NX + ix];
const h10 = heights[iz * NX + ix + 1];
const h11 = heights[(iz + 1) * NX + ix + 1];
const h01 = heights[(iz + 1) * NX + ix];
const crossings = [];
if ((h00 > level) !== (h10 > level)) {
crossings.push(
edgeIntersection(x0, z0, x1, z0, h00, h10)
);
}
if ((h10 > level) !== (h11 > level)) {
crossings.push(
edgeIntersection(x1, z0, x1, z1, h10, h11)
);
}
if ((h11 > level) !== (h01 > level)) {
crossings.push(
edgeIntersection(x1, z1, x0, z1, h11, h01)
);
}
if ((h01 > level) !== (h00 > level)) {
crossings.push(
edgeIntersection(x0, z1, x0, z0, h01, h00)
);
}
for (let p = 0; p + 1 < crossings.length; p += 2) {
shorePositions[pointer++] = crossings[p][0];
shorePositions[pointer++] = level + 0.028;
shorePositions[pointer++] = crossings[p][1];
shorePositions[pointer++] = crossings[p + 1][0];
shorePositions[pointer++] = level + 0.028;
shorePositions[pointer++] = crossings[p + 1][1];
}
}
}
shoreGeometry.setDrawRange(0, pointer / 3);
shoreAttribute.needsUpdate = true;
}
// ---------------------------------------------------------------------
// Harbor props
// ---------------------------------------------------------------------
const materialCache = new Map();
function matte(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function solidBox(
width,
height,
depth,
x,
y,
z,
colorOrMaterial,
parent = scene
) {
const material =
typeof colorOrMaterial === "object"
? colorOrMaterial
: matte(colorOrMaterial);
const mesh = new THREE.Mesh(unitBox, material);
mesh.position.set(x, y, z);
mesh.scale.set(width, height, depth);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Voxel lighthouse.
const lighthouse = new THREE.Group();
lighthouse.position.set(-0.8, sampleHeight(-0.8, 1.35), 1.35);
scene.add(lighthouse);
solidBox(0.58, 0.24, 0.58, 0, 0.12, 0, 0xb7b6a6, lighthouse);
solidBox(0.38, 0.56, 0.38, 0, 0.52, 0, 0xe1ddd0, lighthouse);
solidBox(0.405, 0.18, 0.405, 0, 0.68, 0, 0xb84d3f, lighthouse);
solidBox(0.32, 0.3, 0.32, 0, 0.92, 0, 0xe6d7ad, lighthouse);
const lanternMaterial = new THREE.MeshStandardMaterial({
color: 0xffca69,
emissive: 0xff9d2e,
emissiveIntensity: 1.8,
roughness: 0.35
});
solidBox(0.24, 0.22, 0.24, 0, 1.18, 0, lanternMaterial, lighthouse);
solidBox(0.48, 0.09, 0.48, 0, 1.34, 0, 0x36434a, lighthouse);
const lanternLight = new THREE.PointLight(0xffb44f, 1.1, 4);
lanternLight.position.set(0, 1.22, 0);
lighthouse.add(lanternLight);
function addWarehouse(x, z, wallColor) {
const warehouse = new THREE.Group();
warehouse.position.set(x, sampleHeight(x, z), z);
scene.add(warehouse);
solidBox(1.05, 0.55, 0.78, 0, 0.275, 0, wallColor, warehouse);
solidBox(1.16, 0.11, 0.88, 0, 0.58, 0, 0x394951, warehouse);
solidBox(0.72, 0.11, 0.62, 0, 0.69, 0, 0x465760, warehouse);
solidBox(0.24, 0.36, 0.025, 0.12, 0.18, 0.402, 0x25343b, warehouse);
solidBox(0.08, 0.3, 0.08, -0.31, 0.77, -0.19, 0x2e3c43, warehouse);
}
addWarehouse(-3.7, -0.9, 0xa6a696);
addWarehouse(-2.65, -1.25, 0x738b8c);
const containerColors = [0xd66b48, 0x397f95, 0x8b9652];
for (let i = 0; i < containerColors.length; i++) {
const x = -2.2 + i * 0.46;
const z = 0.7 + (i % 2) * 0.42;
const ground = sampleHeight(x, z);
solidBox(
0.39,
0.34,
0.7,
x,
ground + 0.17,
z,
containerColors[i]
);
}
// Fixed dock and pilings in the harbor channel.
solidBox(0.56, 0.09, 2.25, 1.55, 0.32, 1.35, 0x927453);
for (let i = 0; i < 6; i++) {
const z = 0.35 + i * 0.4;
solidBox(0.07, 0.76, 0.07, 1.29, -0.02, z, 0x554a39);
solidBox(0.07, 0.76, 0.07, 1.81, -0.02, z, 0x554a39);
}
// Low voxel breakwater, leaving the main channel open.
for (let i = 0; i < 8; i++) {
const x = 0.55 + i * 0.3;
if (Math.abs(x - 1.55) < 0.28) continue;
solidBox(
0.32,
0.32,
0.36,
x,
0.16,
2.72,
i % 2 ? 0x77786d : 0x666c67
);
}
// Small floating harbor boat.
const boat = new THREE.Group();
boat.position.set(1.55, currentTide + 0.04, 1.25);
boat.rotation.y = -0.14;
scene.add(boat);
solidBox(0.42, 0.15, 0.78, 0, 0.07, 0, 0xb54e3e, boat);
solidBox(0.32, 0.19, 0.34, 0, 0.23, -0.03, 0xe0d5bb, boat);
solidBox(0.36, 0.06, 0.38, 0, 0.35, -0.03, 0x345665, boat);
solidBox(0.045, 0.55, 0.045, 0, 0.42, -0.03, 0xded8c7, boat);
// ---------------------------------------------------------------------
// Tide gauge beside the relief map
// ---------------------------------------------------------------------
const gauge = new THREE.Group();
gauge.position.set(4.15, 0, 0.05);
scene.add(gauge);
solidBox(0.82, 0.2, 0.82, 0, -0.68, 0, 0x34444c, gauge);
solidBox(0.58, 0.08, 0.58, 0, -0.54, 0, 0x728086, gauge);
const gaugeBottom = -0.64;
const gaugeTop = 2.16;
const glassMaterial = new THREE.MeshStandardMaterial({
color: 0xb8f8ff,
transparent: true,
opacity: 0.16,
roughness: 0.1,
metalness: 0,
depthWrite: false,
side: THREE.DoubleSide
});
const glassTube = new THREE.Mesh(
new THREE.CylinderGeometry(
0.16,
0.16,
gaugeTop - gaugeBottom,
24,
1,
true
),
glassMaterial
);
glassTube.position.y = (gaugeTop + gaugeBottom) * 0.5;
glassTube.renderOrder = 6;
gauge.add(glassTube);
const gaugeWaterMaterial = new THREE.MeshStandardMaterial({
color: 0x33d5df,
transparent: true,
opacity: 0.82,
roughness: 0.2,
metalness: 0.03,
depthWrite: false
});
const gaugeWater = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 1, 20),
gaugeWaterMaterial
);
gaugeWater.renderOrder = 7;
gauge.add(gaugeWater);
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.22, 0.22, 0.12, 24),
matte(0x8a989d)
);
cap.position.y = gaugeTop + 0.02;
cap.castShadow = true;
gauge.add(cap);
// Gauge ruler.
for (let level = 0; level <= 1.5; level += 0.25) {
const major = Math.abs(level % 0.5) < 0.001;
solidBox(
major ? 0.2 : 0.11,
0.025,
0.035,
0.27,
level,
0.16,
major ? 0xe7f5f8 : 0x91a8b0,
gauge
);
}
// The orange chart datum is fixed to the gauge, never to the water.
const datumMaterial = new THREE.MeshStandardMaterial({
color: 0xffb45b,
emissive: 0xff8c2a,
emissiveIntensity: 0.45,
roughness: 0.45
});
const datumRing = new THREE.Mesh(
new THREE.TorusGeometry(0.185, 0.025, 8, 32),
datumMaterial
);
datumRing.rotation.x = Math.PI / 2;
datumRing.position.y = CHART_DATUM;
gauge.add(datumRing);
solidBox(
0.52,
0.035,
0.04,
0,
CHART_DATUM,
0.19,
datumMaterial,
gauge
);
const levelPointer = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.16, 3),
new THREE.MeshStandardMaterial({
color: 0xe3fdff,
emissive: 0x56dce5,
emissiveIntensity: 0.45
})
);
levelPointer.rotation.z = -Math.PI / 2;
levelPointer.position.set(0.36, currentTide, 0.16);
gauge.add(levelPointer);
// A visible chart-datum tick at the edge of the map.
solidBox(
0.72,
0.028,
0.035,
3.22,
CHART_DATUM,
3.05,
datumMaterial
);
// ---------------------------------------------------------------------
// Interaction and tide state
// ---------------------------------------------------------------------
function updateSliderFill(value) {
const amount =
((value - MIN_TIDE) / (MAX_TIDE - MIN_TIDE)) * 100;
tideSlider.style.setProperty("--fill", `${amount}%`);
}
function setCycleUI() {
cycleButton.classList.toggle("paused", !autoCycle);
cycleButton.querySelector("span").textContent = autoCycle
? "Pause cycle"
: "Resume cycle";
}
tideSlider.addEventListener("input", () => {
autoCycle = false;
targetTide = Number(tideSlider.value);
updateSliderFill(targetTide);
setCycleUI();
});
cycleButton.addEventListener("click", () => {
autoCycle = !autoCycle;
if (autoCycle) {
cycleTime = 0;
}
setCycleUI();
});
resetButton.addEventListener("click", () => {
cameraAzimuth = 0.72;
cameraPolar = 1.01;
cameraZoom = 1;
updateCamera();
});
// Direct camera controls, implemented without Three.js addons.
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraAzimuth -= deltaX * 0.005;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar - deltaY * 0.004,
0.48,
1.36
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.72,
2.5
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function updateScene(deltaTime) {
if (autoCycle) {
cycleTime += deltaTime * 1000;
const phase = (cycleTime % 18000) / 18000;
const wave = (1 - Math.cos(phase * Math.PI * 2)) * 0.5;
targetTide = THREE.MathUtils.lerp(MIN_TIDE, MAX_TIDE, wave);
tideSlider.value = targetTide.toFixed(2);
updateSliderFill(targetTide);
}
currentTide = THREE.MathUtils.damp(
currentTide,
targetTide,
4.5,
deltaTime
);
water.position.y = currentTide;
waterGrid.position.y = currentTide + 0.009;
updateShoreline(currentTide);
const gaugeFillHeight = Math.max(0.02, currentTide - gaugeBottom);
gaugeWater.scale.y = gaugeFillHeight;
gaugeWater.position.y =
gaugeBottom + gaugeFillHeight * 0.5;
levelPointer.position.y = currentTide;
boat.position.y = currentTide + 0.04;
boat.rotation.z =
Math.sin(performance.now() * 0.0016) * 0.018;
let exposed = 0;
for (let i = 0; i < heights.length; i++) {
if (heights[i] > currentTide) exposed++;
}
const exposedPercent = Math.round((exposed / heights.length) * 100);
levelValue.textContent = `${currentTide >= 0 ? "+" : ""}${currentTide.toFixed(2)}`;
targetValue.textContent = `${targetTide >= 0 ? "+" : ""}${targetTide.toFixed(2)} m`;
exposedValue.textContent = `${exposedPercent}% / ${exposed}`;
}
function animate() {
const deltaTime = Math.min(clock.getDelta(), 0.05);
updateScene(deltaTime);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateSliderFill(MIN_TIDE);
setCycleUI();
updateScene(0);
animate();
</script>
</body>
</html>
```