Prompt specification
Build or customize the Three.js 3D scene "Water-level comparison at a historic coastal threshold" 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: Water-level comparison at a historic coastal threshold
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/water-level-comparison-at-a-historic-coastal-threshold-x2688
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring Paraty's historic waterfront. Let users vary a schematic water level around the modeled street edge. Show which surfaces become wet while clearly separating the demonstration from a forecast of actual flooding. Voxel style.
Working Three.js Starter (water-level-comparison-at-a-historic-coastal-threshold-x2688):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Paraty Historic Waterfront — Water-Level Demonstration</title>
<style>
:root {
--ink: #18353a;
--muted: #668086;
--paper: rgba(249, 247, 238, 0.94);
--paper-solid: #f8f5e9;
--line: rgba(24, 53, 58, 0.14);
--water: #178ca8;
--water-bright: #50c4dc;
--dry: #b7a47e;
--warning: #a45f18;
--warning-bg: #fff2d9;
--shadow: 0 14px 42px rgba(27, 59, 64, 0.17);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9e8e6;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button:focus-visible,
input:focus-visible,
canvas:focus-visible {
outline: 3px solid rgba(23, 140, 168, 0.38);
outline-offset: 3px;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.glass {
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(14px) saturate(115%);
-webkit-backdrop-filter: blur(14px) saturate(115%);
}
.brand {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
width: 310px;
padding: 16px 18px 15px;
border-radius: 15px;
pointer-events: none;
}
.brand-top {
display: flex;
align-items: center;
gap: 11px;
}
.brand-mark {
position: relative;
width: 36px;
height: 36px;
flex: 0 0 36px;
border-radius: 8px;
background: var(--water);
box-shadow: inset 0 -9px 0 rgba(0, 0, 0, 0.08);
overflow: hidden;
}
.brand-mark::before {
content: "";
position: absolute;
left: 5px;
right: 5px;
bottom: 6px;
height: 10px;
background: #f4e5b7;
box-shadow: 0 -8px 0 #e86f55;
}
.brand-mark::after {
content: "";
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 5px;
background: #73d1df;
}
.eyebrow {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 25px;
font-weight: 700;
line-height: 1;
letter-spacing: -0.02em;
}
.brand p {
margin: 9px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.mode-strip {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 12px;
}
.chip {
display: inline-flex;
align-items: center;
min-height: 23px;
padding: 4px 8px;
border-radius: 6px;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.chip.demo {
color: #086d86;
background: #d9f0f0;
}
.chip.no-data {
color: #8a4e10;
background: #ffe8be;
}
.status-panel {
position: fixed;
z-index: 10;
top: 22px;
right: 22px;
width: 285px;
max-height: calc(100vh - 44px);
padding: 15px;
border-radius: 15px;
overflow: hidden;
display: flex;
flex-direction: column;
}
.status-heading {
display: flex;
justify-content: space-between;
gap: 12px;
align-items: flex-start;
padding-bottom: 11px;
border-bottom: 1px solid var(--line);
}
.status-heading h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.status-heading p {
margin: 4px 0 0;
color: var(--muted);
font-size: 10px;
}
.wet-count {
flex: 0 0 auto;
min-width: 47px;
padding: 7px 8px;
border-radius: 8px;
color: #fff;
background: var(--water);
text-align: center;
font-size: 10px;
font-weight: 850;
line-height: 1.15;
}
.wet-count strong {
display: block;
font-size: 16px;
}
.zone-list {
margin-top: 8px;
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: rgba(23, 140, 168, 0.35) transparent;
}
.zone-row {
display: grid;
grid-template-columns: 9px 1fr auto;
align-items: center;
gap: 9px;
padding: 8px 2px;
border-bottom: 1px solid rgba(24, 53, 58, 0.08);
}
.zone-dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--dry);
box-shadow: 0 0 0 3px rgba(183, 164, 126, 0.12);
transition: 160ms ease;
}
.zone-name {
min-width: 0;
font-size: 11px;
font-weight: 750;
line-height: 1.2;
}
.zone-name small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 550;
}
.zone-state {
padding: 4px 6px;
border-radius: 5px;
color: #806f50;
background: rgba(183, 164, 126, 0.13);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
}
.zone-row.is-wet .zone-dot {
background: var(--water-bright);
box-shadow: 0 0 0 3px rgba(80, 196, 220, 0.18);
}
.zone-row.is-wet .zone-state {
color: #086d86;
background: rgba(80, 196, 220, 0.17);
}
.status-note {
margin-top: 11px;
padding: 10px 11px;
border-left: 3px solid #dc913c;
border-radius: 3px 8px 8px 3px;
color: #76502a;
background: var(--warning-bg);
font-size: 9px;
line-height: 1.45;
}
.status-note strong {
display: block;
margin-bottom: 2px;
color: #8a4e10;
font-size: 9px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.controls {
position: fixed;
z-index: 12;
left: 50%;
bottom: 22px;
width: min(650px, calc(100vw - 44px));
padding: 17px 19px 15px;
border-radius: 17px;
transform: translateX(-50%);
}
.control-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
}
.control-header h2 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
line-height: 1.1;
}
.control-header p {
margin: 5px 0 0;
color: var(--muted);
font-size: 10px;
}
.readout {
flex: 0 0 auto;
text-align: right;
}
.readout strong {
display: block;
color: var(--water);
font-size: 28px;
line-height: 0.95;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.readout small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.range-wrap {
margin-top: 15px;
}
#level {
width: 100%;
height: 20px;
margin: 0;
appearance: none;
-webkit-appearance: none;
background: transparent;
cursor: pointer;
}
#level::-webkit-slider-runnable-track {
height: 8px;
border-radius: 3px;
background:
linear-gradient(
90deg,
#b9c4bb 0%,
#d5cfad 16%,
#64c3d3 52%,
#188eaa 100%
);
box-shadow: inset 0 0 0 1px rgba(24, 53, 58, 0.13);
}
#level::-moz-range-track {
height: 8px;
border-radius: 3px;
background:
linear-gradient(
90deg,
#b9c4bb 0%,
#d5cfad 16%,
#64c3d3 52%,
#188eaa 100%
);
box-shadow: inset 0 0 0 1px rgba(24, 53, 58, 0.13);
}
#level::-webkit-slider-thumb {
width: 20px;
height: 20px;
margin-top: -6px;
appearance: none;
-webkit-appearance: none;
border: 3px solid var(--paper-solid);
border-radius: 5px;
background: var(--water);
box-shadow: 0 2px 7px rgba(16, 69, 80, 0.35);
}
#level::-moz-range-thumb {
width: 15px;
height: 15px;
border: 3px solid var(--paper-solid);
border-radius: 5px;
background: var(--water);
box-shadow: 0 2px 7px rgba(16, 69, 80, 0.35);
}
.range-meta {
display: flex;
justify-content: space-between;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.04em;
}
.range-meta .datum {
color: var(--water);
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 12px;
}
.button-row button {
min-height: 31px;
padding: 6px 10px;
border: 1px solid var(--line);
border-radius: 7px;
color: var(--ink);
background: rgba(255, 255, 255, 0.62);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
cursor: pointer;
transition: 130ms ease;
}
.button-row button:hover {
transform: translateY(-1px);
border-color: rgba(23, 140, 168, 0.38);
background: #fff;
}
.button-row button.active {
color: #fff;
border-color: var(--water);
background: var(--water);
}
.button-row .spacer {
flex: 1;
}
.disclaimer {
display: flex;
gap: 10px;
margin-top: 12px;
padding: 10px 11px;
border-radius: 8px;
color: #76502a;
background: var(--warning-bg);
font-size: 9px;
line-height: 1.45;
}
.disclaimer-icon {
display: grid;
width: 20px;
height: 20px;
flex: 0 0 20px;
place-items: center;
border-radius: 5px;
color: #fff;
background: #c27a28;
font-weight: 900;
}
.disclaimer p {
margin: 0;
}
.disclaimer strong {
color: #8a4e10;
}
.world-label {
position: fixed;
z-index: 4;
left: 0;
top: 0;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 6px;
color: #24464b;
background: rgba(249, 247, 238, 0.82);
box-shadow: 0 5px 15px rgba(28, 67, 73, 0.12);
pointer-events: none;
white-space: nowrap;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
will-change: transform;
}
.world-label.water {
color: #086d86;
background: rgba(224, 247, 246, 0.82);
}
#tooltip {
position: fixed;
z-index: 30;
display: none;
max-width: 230px;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 8px;
color: var(--ink);
background: rgba(249, 247, 238, 0.96);
box-shadow: 0 9px 25px rgba(24, 53, 58, 0.18);
pointer-events: none;
font-size: 10px;
line-height: 1.4;
}
#tooltip strong {
display: block;
margin-bottom: 2px;
}
#tooltip span {
color: var(--muted);
}
.camera-hint {
position: fixed;
z-index: 8;
left: 22px;
bottom: 25px;
padding: 8px 10px;
border-radius: 8px;
color: rgba(24, 53, 58, 0.72);
background: rgba(249, 247, 238, 0.68);
font-size: 9px;
font-weight: 700;
pointer-events: none;
}
@media (max-width: 900px) {
.camera-hint {
display: none;
}
}
@media (max-width: 720px) {
.brand {
top: 12px;
left: 12px;
width: 240px;
padding: 12px 13px;
}
.brand h1 {
font-size: 20px;
}
.brand p {
display: none;
}
.mode-strip {
margin-top: 9px;
}
.status-panel {
top: 12px;
right: 12px;
width: 185px;
max-height: 42vh;
padding: 11px;
}
.status-heading {
display: block;
}
.status-heading p {
display: none;
}
.wet-count {
display: inline-block;
margin-top: 8px;
}
.zone-row {
padding: 6px 1px;
}
.zone-name {
font-size: 9px;
}
.zone-name small {
font-size: 8px;
}
.zone-state {
font-size: 7px;
}
.status-note {
display: none;
}
.controls {
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.control-header h2 {
font-size: 17px;
}
.readout strong {
font-size: 24px;
}
.disclaimer {
font-size: 8px;
}
.world-label.landmark {
display: none;
}
}
@media (max-height: 680px) and (min-width: 721px) {
.brand p,
.mode-strip,
.status-note {
display: none;
}
.controls {
bottom: 12px;
padding-top: 13px;
}
.disclaimer {
padding: 7px 10px;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive voxel model of Paraty's historic waterfront"></main>
<header class="brand glass">
<div class="brand-top">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<span class="eyebrow">Paraty · Brazil</span>
<h1>Historic Waterfront</h1>
</div>
</div>
<p>
An interpretive voxel reconstruction of the river edge and
Rua do Comércio.
</p>
<div class="mode-strip">
<span class="chip demo">Demonstration model</span>
<span class="chip no-data">No live data</span>
</div>
</header>
<aside class="status-panel glass" aria-label="Wet surface status">
<div class="status-heading">
<div>
<span class="eyebrow">Surface crossings</span>
<h2>Which surfaces are wet?</h2>
<p>Based only on the level set below.</p>
</div>
<div class="wet-count" id="wetCount" aria-live="polite">
<strong id="wetNumber">0</strong>
of <span id="surfaceTotal">8</span>
</div>
</div>
<div class="zone-list" id="zoneList"></div>
<div class="status-note">
<strong>Geometry demonstration</strong>
A “wet” surface means the manually imposed water plane is above that
surface in this simplified model.
</div>
</aside>
<section class="controls glass" aria-label="Water level controls">
<div class="control-header">
<div>
<span class="eyebrow">Water-level lab</span>
<h2>Illustrative surface wetting</h2>
<p>Move the level to inspect the modeled waterfront.</p>
</div>
<div class="readout" aria-live="polite">
<strong id="levelValue">+0.08</strong>
<small>m · model datum</small>
</div>
</div>
<div class="range-wrap">
<input
id="level"
type="range"
min="-0.30"
max="1.80"
step="0.01"
value="0.08"
aria-label="Illustrative water level relative to model datum"
>
<div class="range-meta">
<span>−0.30</span>
<span class="datum">0 = MODEL DATUM</span>
<span>+1.80</span>
</div>
</div>
<div class="button-row">
<button type="button" data-level="-0.20">Low</button>
<button type="button" data-level="0">Datum</button>
<button type="button" data-level="0.60">Street edge</button>
<button type="button" data-level="1.40">Plaza</button>
<span class="spacer"></span>
<button type="button" id="animateButton">Replay rise</button>
<button type="button" id="resetView">Reset view</button>
</div>
<div class="disclaimer" role="note">
<div class="disclaimer-icon" aria-hidden="true">!</div>
<p>
<strong>Demonstration, not a flood forecast.</strong>
This interpretive voxel model is not a hydraulic simulation and is not
connected to tide, rainfall, river discharge, drainage, sea-level, or
official alert data. Do not use it to infer real-world flooding.
</p>
</div>
</section>
<div class="world-label landmark" id="churchLabel">
Igreja de Nossa Senhora dos Remédios
</div>
<div class="world-label" id="streetLabel">Rua do Comércio</div>
<div class="world-label water" id="riverLabel">Rio Paraty</div>
<div id="tooltip"></div>
<div class="camera-hint">
Drag to orbit · Shift/right-drag to pan · Scroll to zoom · R resets view
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Voxel waterfront scene. Drag to orbit, shift-drag to pan, and scroll to zoom."
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e6);
scene.fog = new THREE.Fog(0xdce9e6, 35, 76);
const camera = new THREE.OrthographicCamera(-10, 10, 10, -10, 0.1, 150);
const hemisphere = new THREE.HemisphereLight(0xf5fbf3, 0x718f8b, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.1);
sun.position.set(-15, 25, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -27;
sun.shadow.camera.right = 27;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x8bd3e1, 0.55);
fillLight.position.set(18, 10, -12);
scene.add(fillLight);
const materialCache = new Map();
function materialFor(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true
})
);
}
return materialCache.get(color);
}
function makeBox(width, height, depth, x, y, z, color, options = {}) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const material = options.material || materialFor(color);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
scene.add(mesh);
return mesh;
}
const COLORS = {
land: 0xb9aa8f,
stone: 0xb7b3a7,
stoneDark: 0x8d9189,
cream: 0xf0e5c6,
white: 0xf7f1df,
roof: 0xb95843,
roofLight: 0xd47852,
wood: 0x735548,
dark: 0x294a52,
green: 0x5f8968,
greenLight: 0x789c6d,
trunk: 0x725543,
gold: 0xd9a94b,
water: 0x49abc0
};
// ---------------------------------------------------------------------
// Terrain and waterfront geometry
// ---------------------------------------------------------------------
// Urban ground: top at y = 0.60.
makeBox(38, 2.2, 18, 0, -0.50, -7.20, COLORS.land);
// Lower stone quay: top at y = 0.
makeBox(38, 1.4, 2.0, 0, -0.70, 4.40, COLORS.stoneDark);
// River plane, moved by the slider.
const water = new THREE.Mesh(
new THREE.PlaneGeometry(90, 80),
new THREE.MeshPhongMaterial({
color: COLORS.water,
transparent: true,
opacity: 0.79,
shininess: 85,
depthWrite: false,
side: THREE.DoubleSide
})
);
water.rotation.x = -Math.PI / 2;
water.position.set(0, 0.08, 16);
water.renderOrder = 1;
scene.add(water);
// Cobbled Rua do Comércio.
const cobbleGeometry = new THREE.BoxGeometry(0.51, 0.06, 0.36);
const cobbleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.97,
flatShading: true
});
const cobbleCount = 61 * 9;
const cobbles = new THREE.InstancedMesh(
cobbleGeometry,
cobbleMaterial,
cobbleCount
);
const dummy = new THREE.Object3D();
const cobbleColors = [0xb9b0a0, 0xc4baaa, 0xaaa596, 0xc9beaa, 0xb2aa9b];
let cobbleIndex = 0;
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 61; col++) {
const x = -16.5 + col * 0.545 + (row % 2) * 0.18;
const z = -1.58 + row * 0.39;
dummy.position.set(x, 0.615 + Math.random() * 0.008, z);
dummy.rotation.set(0, (Math.random() - 0.5) * 0.035, 0);
dummy.updateMatrix();
cobbles.setMatrixAt(cobbleIndex, dummy.matrix);
const color = new THREE.Color(
cobbleColors[Math.floor(Math.random() * cobbleColors.length)]
);
color.offsetHSL(0, 0, (Math.random() - 0.5) * 0.07);
cobbles.setColorAt(cobbleIndex, color);
cobbleIndex++;
}
}
cobbles.instanceMatrix.needsUpdate = true;
cobbles.instanceColor.needsUpdate = true;
cobbles.receiveShadow = true;
scene.add(cobbles);
// Stone paving on the lower quay.
const quayTileGeometry = new THREE.BoxGeometry(0.82, 0.07, 0.54);
const quayTiles = new THREE.InstancedMesh(
quayTileGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95,
flatShading: true
}),
47 * 4
);
let quayIndex = 0;
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 47; col++) {
dummy.position.set(
-18.4 + col * 0.81 + (row % 2) * 0.18,
0.035,
3.62 + row * 0.51
);
dummy.rotation.set(0, (Math.random() - 0.5) * 0.02, 0);
dummy.updateMatrix();
quayTiles.setMatrixAt(quayIndex, dummy.matrix);
const color = new THREE.Color(0xb8b6aa);
color.offsetHSL(0, 0, (Math.random() - 0.5) * 0.08);
quayTiles.setColorAt(quayIndex, color);
quayIndex++;
}
}
quayTiles.instanceMatrix.needsUpdate = true;
quayTiles.instanceColor.needsUpdate = true;
quayTiles.receiveShadow = true;
scene.add(quayTiles);
// Three voxel steps from the lower quay to the street.
makeBox(3.8, 1.04, 0.53, 9.0, -0.18, 2.065, COLORS.stone);
makeBox(3.8, 0.92, 0.54, 9.0, -0.24, 2.595, COLORS.stone);
makeBox(3.8, 0.80, 0.53, 9.0, -0.30, 3.125, COLORS.stone);
// Raised shop threshold strip.
makeBox(28.8, 0.14, 1.10, -1.3, 0.65, -2.44, 0xc9bda7);
// Church forecourt.
makeBox(8.2, 0.30, 2.8, 3.5, 0.75, -5.95, 0xd0c3aa);
// Raised inner courtyard.
makeBox(5.2, 0.55, 4.8, -11.1, 0.875, -6.1, 0xb8aa91);
makeBox(4.6, 0.18, 0.28, -11.1, 1.23, -8.2, COLORS.stone);
makeBox(0.28, 0.18, 4.2, -13.2, 1.23, -6.1, COLORS.stone);
makeBox(0.28, 0.18, 4.2, -9.0, 1.23, -6.1, COLORS.stone);
// ---------------------------------------------------------------------
// Buildings
// ---------------------------------------------------------------------
function addFrontWindow(x, y, z, width = 0.62, height = 0.88) {
makeBox(width + 0.18, height + 0.18, 0.10, x, y, z, COLORS.white);
makeBox(width, height, 0.13, x, y, z + 0.075, COLORS.dark);
makeBox(width + 0.28, 0.09, 0.18, x, y - height / 2 - 0.11, z + 0.03, COLORS.white);
}
function addFrontDoor(x, bottom, z, width = 0.78, height = 1.62) {
makeBox(width + 0.18, height + 0.12, 0.10, x, bottom + height / 2, z, COLORS.white);
makeBox(width, height, 0.14, x, bottom + height / 2, z + 0.08, COLORS.dark);
makeBox(width * 0.56, 0.34, 0.14, x, bottom + height + 0.12, z + 0.08, COLORS.dark);
makeBox(width * 0.28, 0.22, 0.14, x, bottom + height + 0.36, z + 0.08, COLORS.dark);
makeBox(width * 0.12, 0.13, 0.14, x, bottom + height + 0.52, z + 0.08, COLORS.dark);
}
function addSign(text, x, y, z, width = 1.9, background = "#f5e7bc", foreground = "#25494e") {
const canvas = document.createElement("canvas");
canvas.width = 384;
canvas.height = 96;
const context = canvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = "rgba(30,60,65,.25)";
context.lineWidth = 7;
context.strokeRect(4, 4, canvas.width - 8, canvas.height - 8);
context.fillStyle = foreground;
context.font = "800 42px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(width, width * 0.25),
new THREE.MeshBasicMaterial({ map: texture, side: THREE.DoubleSide })
);
sign.position.set(x, y, z);
scene.add(sign);
}
function addBuilding({
x,
z,
width,
depth,
height,
color,
sign = null,
balcony = false
}) {
const bottom = 0.72;
const front = z + depth / 2;
// Stone plinth and colored facade.
makeBox(width + 0.12, 0.18, depth + 0.12, x, 0.79, z, COLORS.stone);
makeBox(width, height, depth, x, bottom + height / 2, z, color);
// Horizontal trim and a simple cornice.
makeBox(width + 0.10, 0.10, depth + 0.10, x, bottom + 0.12, z, COLORS.white);
makeBox(width + 0.14, 0.16, depth + 0.14, x, bottom + height - 0.05, z, COLORS.white);
// Stepped terracotta roof.
const roofBase = bottom + height + 0.06;
makeBox(width + 0.34, 0.23, depth + 0.34, x, roofBase, z, COLORS.roof);
for (let step = 0; step < 3; step++) {
const shrink = 1 - step * 0.19;
makeBox(
width * shrink,
0.27,
depth * shrink,
x,
roofBase + 0.22 + step * 0.255,
z,
step % 2 ? COLORS.roof : COLORS.roofLight
);
}
// Ground-floor doorway and windows.
addFrontDoor(x, bottom, front + 0.025);
addFrontWindow(x - width * 0.27, bottom + 1.06, front + 0.025);
addFrontWindow(x + width * 0.27, bottom + 1.06, front + 0.025);
// Upper windows.
const upperLevels = height > 5.0 ? 2 : 1;
for (let level = 0; level < upperLevels; level++) {
const y = bottom + 3.25 + level * 2.05;
if (y + 0.45 > bottom + height - 0.25) continue;
addFrontWindow(x - width * 0.27, y, front + 0.025);
addFrontWindow(x, y, front + 0.025);
addFrontWindow(x + width * 0.27, y, front + 0.025);
}
// Shutters beside selected windows.
const shutterColor = new THREE.Color(color).multiplyScalar(0.78).getHex();
makeBox(0.16, 0.88, 0.08, x - width * 0.27 - 0.48, bottom + 1.06, front + 0.10, shutterColor);
makeBox(0.16, 0.88, 0.08, x + width * 0.27 + 0.48, bottom + 1.06, front + 0.10, shutterColor);
if (balcony) {
makeBox(width * 0.58, 0.12, 0.78, x, bottom + 2.38, front + 0.32, COLORS.white);
makeBox(width * 0.58, 0.08, 0.08, x, bottom + 2.82, front + 0.67, COLORS.white);
for (let rail = -3; rail <= 3; rail++) {
makeBox(
0.055,
0.42,
0.055,
x + rail * width * 0.075,
bottom + 2.61,
front + 0.67,
COLORS.white
);
}
}
if (sign) {
addSign(sign, x, bottom + 2.24, front + 0.12, Math.min(width * 0.56, 2.2));
}
}
addBuilding({
x: -13.2,
z: -5.15,
width: 4.2,
depth: 4.0,
height: 5.0,
color: 0xd7755d,
sign: "CAFÉ"
});
addBuilding({
x: -8.5,
z: -5.10,
width: 4.2,
depth: 4.0,
height: 4.45,
color: 0xe4bd69,
sign: "ARTE",
balcony: true
});
addBuilding({
x: -3.85,
z: -5.20,
width: 4.0,
depth: 4.0,
height: 5.55,
color: 0x86afa0,
sign: "MERCADO"
});
addBuilding({
x: 8.0,
z: -5.10,
width: 4.4,
depth: 4.0,
height: 4.80,
color: 0xdca15f,
sign: "PESCADOS"
});
addBuilding({
x: 12.9,
z: -5.15,
width: 4.2,
depth: 4.0,
height: 5.75,
color: 0x7199a8
});
// Rear row.
addBuilding({
x: -14.5,
z: -10.5,
width: 5.0,
depth: 4.4,
height: 5.6,
color: 0xcf9b72
});
addBuilding({
x: -9.2,
z: -10.8,
width: 4.6,
depth: 4.2,
height: 6.4,
color: 0xd16e59
});
addBuilding({
x: 11.8,
z: -10.6,
width: 5.2,
depth: 4.4,
height: 5.7,
color: 0xe2c87b
});
addBuilding({
x: 16.3,
z: -10.8,
width: 3.3,
depth: 4.0,
height: 4.7,
color: 0x8daf9a
});
// Igreja de Nossa Senhora dos Remédios.
makeBox(6.6, 0.18, 4.7, 3.5, 0.99, -10.8, COLORS.stone);
makeBox(6.6, 5.2, 4.6, 3.5, 3.68, -10.8, COLORS.cream);
makeBox(6.8, 0.18, 4.8, 3.5, 6.38, -10.8, COLORS.white);
for (const towerX of [0.65, 6.35]) {
makeBox(1.55, 7.05, 1.75, towerX, 4.50, -8.95, COLORS.cream);
makeBox(1.68, 0.14, 1.88, towerX, 8.08, -8.95, COLORS.white);
makeBox(1.82, 0.28, 2.00, towerX, 8.27, -8.95, COLORS.roof);
makeBox(1.40, 0.28, 1.60, towerX, 8.53, -8.95, COLORS.roofLight);
makeBox(0.98, 0.28, 1.20, towerX, 8.79, -8.95, COLORS.roof);
makeBox(0.55, 0.28, 0.80, towerX, 9.05, -8.95, COLORS.roofLight);
makeBox(0.52, 0.86, 0.12, towerX, 6.75, -8.02, COLORS.dark);
makeBox(0.12, 0.80, 0.14, towerX, 6.75, -7.93, COLORS.white);
makeBox(0.66, 0.12, 0.14, towerX, 6.75, -7.93, COLORS.white);
}
addFrontDoor(3.5, 1.08, -8.44, 1.0, 2.05);
makeBox(1.42, 1.42, 0.12, 3.5, 5.13, -8.43, COLORS.white);
makeBox(1.08, 1.08, 0.15, 3.5, 5.13, -8.34, COLORS.gold);
makeBox(0.08, 0.82, 0.17, 3.5, 5.13, -8.23, COLORS.dark);
makeBox(0.62, 0.08, 0.17, 3.5, 5.13, -8.23, COLORS.dark);
makeBox(0.20, 0.62, 0.15, 3.5, 7.10, -8.42, COLORS.dark);
// Voxel cross.
makeBox(0.12, 0.74, 0.12, 3.5, 9.45, -8.95, COLORS.gold);
makeBox(0.48, 0.12, 0.12, 3.5, 9.57, -8.95, COLORS.gold);
// ---------------------------------------------------------------------
// Trees, people, boats, and background
// ---------------------------------------------------------------------
function addTree(x, z, scale = 1) {
makeBox(0.42 * scale, 2.0 * scale, 0.42 * scale, x, 1.45 * scale, z, COLORS.trunk);
makeBox(2.0 * scale, 1.35 * scale, 1.75 * scale, x, 2.75 * scale, z, COLORS.green);
makeBox(1.45 * scale, 1.05 * scale, 1.45 * scale, x - 0.45 * scale, 3.65 * scale, z, COLORS.greenLight);
makeBox(1.15 * scale, 0.8 * scale, 1.1 * scale, x + 0.55 * scale, 3.75 * scale, z - 0.15, COLORS.green);
}
addTree(-16.5, -4.4, 0.95);
addTree(15.8, -5.6, 0.86);
addTree(-12.6, -7.6, 0.78);
addTree(-9.7, -7.5, 0.66);
function addPerson(x, z, shirtColor) {
makeBox(0.36, 0.62, 0.28, x, 1.18, z, shirtColor);
makeBox(0.30, 0.30, 0.28, x, 1.68, z, 0x8c5f48);
makeBox(0.12, 0.44, 0.13, x - 0.11, 0.48, z, 0x334d56);
makeBox(0.12, 0.44, 0.13, x + 0.11, 0.48, z, 0x334d56);
}
addPerson(-4.6, -0.1, 0xcc5f4f);
addPerson(-3.9, -0.4, 0xe0b450);
addPerson(5.5, 0.7, 0x437f94);
addPerson(6.2, 0.3, 0x697e56);
addPerson(-9.7, -6.0, 0x875e6e);
const boats = [];
function addBoat(x, z, hullColor, scale = 1) {
const group = new THREE.Group();
const hullGeometry = new THREE.BoxGeometry(2.9, 0.48, 1.12);
const hull = new THREE.Mesh(hullGeometry, materialFor(hullColor));
hull.position.y = 0.25;
hull.castShadow = true;
group.add(hull);
const bow = new THREE.Mesh(
new THREE.BoxGeometry(0.55, 0.48, 0.68),
materialFor(hullColor)
);
bow.position.set(1.63, 0.25, 0);
bow.castShadow = true;
group.add(bow);
const cabin = new THREE.Mesh(
new THREE.BoxGeometry(0.88, 0.68, 0.78),
materialFor(COLORS.white)
);
cabin.position.set(-0.35, 0.78, 0);
cabin.castShadow = true;
group.add(cabin);
const mast = new THREE.Mesh(
new THREE.BoxGeometry(0.09, 2.3, 0.09),
materialFor(COLORS.wood)
);
mast.position.set(0.42, 1.42, 0);
mast.castShadow = true;
group.add(mast);
const sail = new THREE.Mesh(
new THREE.BoxGeometry(0.10, 1.25, 0.78),
materialFor(0xf1e4bd)
);
sail.position.set(0.39, 1.48, 0);
sail.castShadow = true;
group.add(sail);
group.position.set(x, 0.08, z);
group.scale.setScalar(scale);
scene.add(group);
boats.push(group);
}
addBoat(-7.7, 8.3, 0xdd7753, 1.05);
addBoat(10.4, 11.5, 0xf0d783, 0.82);
// Voxel hills behind town.
makeBox(10, 4.5, 5.5, -13, 1.0, -19.2, 0x77957c);
makeBox(8, 5.6, 5.0, -5.5, 1.45, -20.0, 0x688b76);
makeBox(11, 4.2, 6.0, 4.0, 0.75, -20.3, 0x7c9a7b);
makeBox(8.5, 5.0, 5.0, 13.5, 1.10, -19.8, 0x6d9076);
// Blocky clouds.
makeBox(4.8, 0.7, 1.0, -10, 12.5, -18, 0xf5f3e7, { castShadow: false });
makeBox(2.0, 1.0, 1.1, -8.8, 13.0, -18, 0xf5f3e7, { castShadow: false });
makeBox(3.8, 0.65, 1.0, 8, 11.8, -19, 0xf5f3e7, { castShadow: false });
makeBox(1.7, 0.9, 1.0, 9.1, 12.2, -19, 0xf5f3e7, { castShadow: false });
// ---------------------------------------------------------------------
// Wet-surface zones
// ---------------------------------------------------------------------
const MIN_LEVEL = -0.30;
const MAX_LEVEL = 1.80;
const zones = [];
const zoneMeshes = [];
function addZone(id, label, elevation, x, z, width, depth) {
const overlay = new THREE.Mesh(
new THREE.PlaneGeometry(width, depth),
new THREE.MeshBasicMaterial({
color: 0x39c4df,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide,
polygonOffset: true,
polygonOffsetFactor: -1,
polygonOffsetUnits: -1
})
);
overlay.rotation.x = -Math.PI / 2;
overlay.position.set(x, elevation + 0.012, z);
overlay.renderOrder = 5;
overlay.userData.zoneId = id;
scene.add(overlay);
const zone = {
id,
label,
elevation,
mesh: overlay,
material: overlay.material,
row: null,
stateElement: null,
wet: false
};
zones.push(zone);
zoneMeshes.push(overlay);
return zone;
}
addZone("quay", "Cais de Pedra", 0.05, 0, 4.40, 37.3, 1.90);
addZone("step3", "Lower quay step", 0.10, 9.0, 3.125, 3.8, 0.50);
addZone("step2", "Middle quay step", 0.22, 9.0, 2.595, 3.8, 0.50);
addZone("step1", "Upper quay step", 0.34, 9.0, 2.065, 3.8, 0.50);
addZone("street", "Rua do Comércio", 0.60, -0.2, -0.02, 34.5, 3.45);
addZone("thresholds", "Shop thresholds", 0.72, -1.3, -2.44, 28.6, 1.02);
addZone("plaza", "Church forecourt", 0.90, 3.5, -5.95, 8.0, 2.65);
addZone("courtyard", "Raised inner courtyard", 1.15, -11.1, -6.1, 5.0, 4.65);
const zoneList = document.querySelector("#zoneList");
zoneList.innerHTML = zones.map((zone) => `
<div class="zone-row" data-zone="${zone.id}">
<div class="zone-dot"></div>
<div class="zone-name">
${zone.label}
<small>threshold ${zone.elevation >= 0 ? "+" : "−"}${Math.abs(zone.elevation).toFixed(2)} m</small>
</div>
<div class="zone-state">DRY</div>
</div>
`).join("");
for (const zone of zones) {
const row = zoneList.querySelector(`[data-zone="${zone.id}"]`);
zone.row = row;
zone.stateElement = row.querySelector(".zone-state");
}
// ---------------------------------------------------------------------
// Model-level gauge
// ---------------------------------------------------------------------
makeBox(0.16, 2.8, 0.16, 16.7, 0.83, 2.35, 0x405c61);
for (let i = 0; i < 8; i++) {
makeBox(
0.34,
0.045,
0.08,
16.7,
-0.25 + i * 0.30,
2.46,
i % 2 ? 0xd9d2bb : 0x4b686c
);
}
const gaugeMarker = makeBox(
0.68,
0.055,
0.10,
16.7,
0.08,
2.50,
0x50d2e5,
{ castShadow: false }
);
// ---------------------------------------------------------------------
// Water sparkles
// ---------------------------------------------------------------------
const sparkleCount = 95;
const sparkles = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.30, 0.025, 0.075),
new THREE.MeshBasicMaterial({
color: 0xd8fbf5,
transparent: true,
opacity: 0.60,
depthWrite: false
}),
sparkleCount
);
sparkles.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
sparkles.frustumCulled = false;
sparkles.renderOrder = 3;
scene.add(sparkles);
const sparkleData = Array.from({ length: sparkleCount }, () => ({
x: THREE.MathUtils.randFloatSpread(45),
z: THREE.MathUtils.randFloat(5.5, 17),
speed: THREE.MathUtils.randFloat(0.18, 0.55),
phase: THREE.MathUtils.randFloat(0, Math.PI * 2),
scale: THREE.MathUtils.randFloat(0.55, 1.8)
}));
// ---------------------------------------------------------------------
// Level control and wet-state logic
// ---------------------------------------------------------------------
const slider = document.querySelector("#level");
const levelValue = document.querySelector("#levelValue");
const wetNumber = document.querySelector("#wetNumber");
const surfaceTotal = document.querySelector("#surfaceTotal");
const presetButtons = [...document.querySelectorAll("[data-level]")];
const tooltip = document.querySelector("#tooltip");
surfaceTotal.textContent = String(zones.length);
let currentLevel = Number(slider.value);
let hoveredZone = null;
let levelAnimation = null;
function formatLevel(value) {
const sign = value >= 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(2)}`;
}
function setLevel(value, fromUser = false) {
currentLevel = THREE.MathUtils.clamp(value, MIN_LEVEL, MAX_LEVEL);
slider.value = currentLevel.toFixed(2);
levelValue.textContent = formatLevel(currentLevel);
water.position.y = currentLevel;
gaugeMarker.position.y = currentLevel;
let wetTotal = 0;
for (const zone of zones) {
zone.wet = currentLevel >= zone.elevation;
if (zone.wet) wetTotal++;
zone.row.classList.toggle("is-wet", zone.wet);
zone.stateElement.textContent = zone.wet ? "WET" : "DRY";
const depth = Math.max(0, currentLevel - zone.elevation);
const baseOpacity = zone.wet
? THREE.MathUtils.clamp(0.28 + depth * 0.22, 0.28, 0.62)
: 0;
const hoverBoost = hoveredZone === zone ? 0.12 : 0;
zone.material.opacity = baseOpacity + hoverBoost;
}
wetNumber.textContent = String(wetTotal);
for (const button of presetButtons) {
const buttonLevel = Number(button.dataset.level);
button.classList.toggle("active", Math.abs(buttonLevel - currentLevel) < 0.025);
}
if (fromUser) levelAnimation = null;
}
slider.addEventListener("input", () => {
setLevel(Number(slider.value), true);
});
for (const button of presetButtons) {
button.addEventListener("click", () => {
setLevel(Number(button.dataset.level), true);
});
}
document.querySelector("#animateButton").addEventListener("click", () => {
levelAnimation = {
from: currentLevel,
to: 1.55,
start: performance.now(),
duration: 7200
};
});
// ---------------------------------------------------------------------
// Direct camera controls: orbit, pan, wheel zoom, pinch zoom, keyboard
// ---------------------------------------------------------------------
const controls = {
theta: 0.55,
phi: 0.82,
zoom: 1,
target: new THREE.Vector3(0, 1.0, -2.0),
radius: 38
};
const frustumSize = 22;
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
camera.left = (-frustumSize * aspect) / 2;
camera.right = (frustumSize * aspect) / 2;
camera.top = frustumSize / 2;
camera.bottom = -frustumSize / 2;
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
const offset = new THREE.Vector3(
controls.radius * Math.sin(controls.phi) * Math.sin(controls.theta),
controls.radius * Math.cos(controls.phi),
controls.radius * Math.sin(controls.phi) * Math.cos(controls.theta)
);
camera.position.copy(controls.target).add(offset);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
}
function resetCamera() {
controls.theta = 0.55;
controls.phi = 0.82;
controls.zoom = 1;
controls.target.set(0, 1.0, -2.0);
updateCamera();
}
document.querySelector("#resetView").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
const pointers = new Map();
let dragMode = null;
let lastPointerX = 0;
let lastPointerY = 0;
let pinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
dragMode = event.shiftKey || event.button === 2 ? "pan" : "orbit";
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
dragMode = "pinch";
pinchDistance = pointerDistance();
}
tooltip.style.display = "none";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointers.has(event.pointerId)) {
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
}
if (dragMode === "pinch" && pointers.size >= 2) {
const distance = pointerDistance();
if (pinchDistance > 0 && distance > 0) {
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * (distance / pinchDistance),
0.72,
2.45
);
pinchDistance = distance;
updateCamera();
}
return;
}
if (pointers.size === 1 && (dragMode === "orbit" || dragMode === "pan")) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (dragMode === "orbit") {
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.005,
0.34,
1.34
);
} else {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrixWorld, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrixWorld, 1);
const scale = frustumSize / window.innerHeight / controls.zoom;
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(up, dy * scale);
controls.target.x = THREE.MathUtils.clamp(controls.target.x, -12, 12);
controls.target.z = THREE.MathUtils.clamp(controls.target.z, -12, 9);
controls.target.y = THREE.MathUtils.clamp(controls.target.y, -1, 6);
}
updateCamera();
return;
}
if (event.pointerType === "mouse") {
updateTooltip(event);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
dragMode = null;
} else if (pointers.size === 1) {
dragMode = "orbit";
const remaining = [...pointers.values()][0];
lastPointerX = remaining.x;
lastPointerY = remaining.y;
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("pointerleave", () => {
if (pointers.size === 0) {
hoveredZone = null;
tooltip.style.display = "none";
setLevel(currentLevel);
}
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
2.45
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "INPUT" || tag === "BUTTON") return;
let handled = true;
switch (event.key.toLowerCase()) {
case "arrowleft":
controls.theta -= 0.08;
break;
case "arrowright":
controls.theta += 0.08;
break;
case "arrowup":
controls.phi = Math.max(0.34, controls.phi - 0.05);
break;
case "arrowdown":
controls.phi = Math.min(1.34, controls.phi + 0.05);
break;
case "+":
case "=":
controls.zoom = Math.min(2.45, controls.zoom * 1.08);
break;
case "-":
controls.zoom = Math.max(0.72, controls.zoom / 1.08);
break;
case "r":
resetCamera();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
// ---------------------------------------------------------------------
// Hover identification
// ---------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
function updateTooltip(event) {
pointerNDC.x = (event.clientX / window.innerWidth) * 2 - 1;
pointerNDC.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObjects(zoneMeshes, false);
const zone = hits.length ? zones.find((item) => item.id === hits[0].object.userData.zoneId) : null;
if (zone !== hoveredZone) {
hoveredZone = zone;
setLevel(currentLevel);
}
if (zone) {
tooltip.innerHTML = `
<strong>${zone.label}</strong>
<span>${
zone.wet
? "Wet in this demonstration"
: "Dry in this demonstration"
} · surface threshold ${formatLevel(zone.elevation)} m</span>
`;
tooltip.style.display = "block";
const width = 230;
tooltip.style.left = `${Math.min(event.clientX + 15, window.innerWidth - width - 12)}px`;
tooltip.style.top = `${Math.min(event.clientY + 15, window.innerHeight - 70)}px`;
} else {
tooltip.style.display = "none";
}
}
// ---------------------------------------------------------------------
// World-space labels
// ---------------------------------------------------------------------
const worldLabels = [
{
element: document.querySelector("#churchLabel"),
position: new THREE.Vector3(3.5, 9.8, -9.0)
},
{
element: document.querySelector("#streetLabel"),
position: new THREE.Vector3(-3.5, 0.9, -0.1)
},
{
element: document.querySelector("#riverLabel"),
position: new THREE.Vector3(7.0, 0.3, 8.0)
}
];
function updateWorldLabels() {
for (const label of worldLabels) {
const projected = label.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -50%)`;
}
}
// ---------------------------------------------------------------------
// Resize and animation loop
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
});
updateCamera();
setLevel(currentLevel);
const clock = new THREE.Clock();
function animate(now) {
requestAnimationFrame(animate);
if (levelAnimation) {
const elapsed = now - levelAnimation.start;
const progress = THREE.MathUtils.clamp(elapsed / levelAnimation.duration, 0, 1);
const eased = progress * progress * (3 - 2 * progress);
setLevel(
THREE.MathUtils.lerp(levelAnimation.from, levelAnimation.to, eased)
);
if (progress >= 1) levelAnimation = null;
}
const time = clock.getElapsedTime();
for (let i = 0; i < sparkleCount; i++) {
const item = sparkleData[i];
dummy.position.set(
item.x + Math.sin(time * 0.55 + item.phase) * 0.28,
currentLevel + 0.035,
item.z + ((time * item.speed) % 9)
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(item.scale, 1, item.scale);
dummy.updateMatrix();
sparkles.setMatrixAt(i, dummy.matrix);
}
sparkles.instanceMatrix.needsUpdate = true;
for (const boat of boats) {
boat.position.y = currentLevel + 0.04;
boat.rotation.z = Math.sin(time * 0.8 + boat.position.x) * 0.018;
}
worldLabels[2].position.y = currentLevel + 0.22;
updateWorldLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```