Prompt specification
Build or customize the Three.js 3D scene "Malecón seawall and seated-person scale study" 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: Malecón seawall and seated-person scale study
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/malec-n-seawall-and-seated-person-scale-study-x2741
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows Havana's Malecón. Build a referenced seawall fragment with pavement and one simple seated figure. Show the wall's height relative to the promenade and the water beyond. Voxel style.
Working Three.js Starter (malec-n-seawall-and-seated-person-scale-study-x2741):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Havana Malecón — Voxel Seawall</title>
<style>
:root {
--ink: #132a31;
--paper: #f4ead8;
--accent: #efb84f;
--sea: #75d1cf;
--panel: rgba(19, 42, 49, 0.86);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #99cbd4;
color: var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
canvas {
width: 100%;
height: 100%;
display: block;
}
canvas {
touch-action: none;
cursor: grab;
outline: none;
}
canvas:active {
cursor: grabbing;
}
.title {
position: fixed;
top: 26px;
left: 28px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 18px rgba(16, 41, 48, 0.35);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 3px;
content: "";
background: var(--accent);
box-shadow: 0 0 12px rgba(239, 184, 79, 0.45);
}
h1 {
margin: 0;
font-size: clamp(32px, 5vw, 64px);
font-weight: 850;
line-height: 0.92;
letter-spacing: -0.055em;
text-transform: uppercase;
}
.subtitle {
max-width: 410px;
margin-top: 12px;
color: rgba(244, 234, 216, 0.82);
font-size: 12px;
line-height: 1.55;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.study-tag {
position: fixed;
top: 27px;
right: 28px;
z-index: 6;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 4px;
background: rgba(19, 42, 49, 0.48);
backdrop-filter: blur(10px);
color: rgba(255, 255, 255, 0.85);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.metrics {
position: fixed;
left: 28px;
bottom: 27px;
z-index: 5;
display: flex;
gap: 7px;
pointer-events: none;
}
.metric {
min-width: 132px;
padding: 10px 12px 9px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 5px;
background: var(--panel);
backdrop-filter: blur(12px);
}
.metric span {
display: block;
margin-bottom: 4px;
color: rgba(244, 234, 216, 0.62);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.metric strong {
color: var(--paper);
font-size: 14px;
font-weight: 750;
letter-spacing: 0.01em;
}
.metric.wall strong {
color: var(--accent);
}
.metric.water strong {
color: var(--sea);
}
.help {
position: fixed;
right: 28px;
bottom: 28px;
z-index: 6;
display: flex;
align-items: center;
gap: 12px;
color: rgba(255, 255, 255, 0.82);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
text-shadow: 0 2px 8px rgba(19, 42, 49, 0.6);
}
button {
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 4px;
padding: 10px 13px;
background: rgba(19, 42, 49, 0.8);
color: var(--paper);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
cursor: pointer;
transition:
background 150ms ease,
transform 150ms ease;
}
button:hover {
background: rgba(19, 42, 49, 0.95);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 3px;
}
@media (max-width: 760px) {
.title {
top: 18px;
left: 18px;
}
h1 {
font-size: 37px;
}
.subtitle {
max-width: 260px;
font-size: 10px;
}
.study-tag {
top: auto;
right: 16px;
bottom: 76px;
}
.metrics {
right: 16px;
bottom: 16px;
left: 16px;
gap: 5px;
}
.metric {
flex: 1;
min-width: 0;
padding: 8px;
}
.metric strong {
font-size: 12px;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="title">
<div class="eyebrow">Havana · Cuba</div>
<h1>Malecón</h1>
<div class="subtitle">
A voxel seawall fragment with promenade, coastal water and a seated observer.
</div>
</header>
<div class="study-tag">Voxel study · 01</div>
<section class="metrics" aria-label="Elevation reference">
<div class="metric">
<span>Promenade</span>
<strong>±0.00 m</strong>
</div>
<div class="metric wall">
<span>Wall crest</span>
<strong>+1.27 m</strong>
</div>
<div class="metric water">
<span>Water</span>
<strong>−0.78 m</strong>
</div>
</section>
<div class="help">
<span>Drag to orbit · Scroll to zoom</span>
<button id="reset" type="button">Reset view</button>
</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 app = document.querySelector("#app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 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.setAttribute("aria-label", "Interactive voxel view of Havana's Malecón");
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9acbd4);
scene.fog = new THREE.Fog(0x9acbd4, 31, 78);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
140
);
const target = new THREE.Vector3(0, 0.05, 0.35);
const orbit = {
yaw: 2.48,
pitch: 0.43,
radius: 24,
zoom: 1
};
function updateCamera() {
const cp = Math.cos(orbit.pitch);
camera.position.set(
target.x + orbit.radius * cp * Math.sin(orbit.yaw),
target.y + orbit.radius * Math.sin(orbit.pitch),
target.z + orbit.radius * cp * Math.cos(orbit.yaw)
);
camera.lookAt(target);
camera.updateProjectionMatrix();
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xd9f4ff, 0x806c50, 1.8);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffd8a1, 3.2);
sun.position.set(-14, 20, -10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -26;
sun.shadow.camera.right = 26;
sun.shadow.camera.top = 23;
sun.shadow.camera.bottom = -23;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.0004;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0x8bdce3, 0.7);
coolFill.position.set(12, 8, 18);
scene.add(coolFill);
// -------------------------------------------------------------------------
// Shared voxel helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true,
...options
});
}
function cube(parent, sx, sy, sz, mat, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(unitBox, mat);
mesh.scale.set(sx, sy, sz);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const promenadeBaseMat = material(0x766e61);
const curbMat = material(0xb4aa98);
const darkMetalMat = material(0x203238, { roughness: 0.68, metalness: 0.18 });
const woodMat = material(0x73523b);
const woodLightMat = material(0x8f6748);
const goldMat = material(0xf0b84b, {
emissive: 0x4a2e00,
emissiveIntensity: 0.18
});
// -------------------------------------------------------------------------
// Promenade and paving
// -------------------------------------------------------------------------
cube(scene, 43, 0.62, 10.7, promenadeBaseMat, 0, -0.36, -5.55);
cube(scene, 43, 0.14, 0.36, curbMat, 0, -0.02, -0.52);
const paverGeometry = new THREE.BoxGeometry(1.44, 0.1, 1.44);
const paverMaterial = material(0xffffff);
const paverCountX = 29;
const paverCountZ = 7;
const pavers = new THREE.InstancedMesh(
paverGeometry,
paverMaterial,
paverCountX * paverCountZ
);
const paverDummy = new THREE.Object3D();
const paverPalette = [
0xc9bea9,
0xd5c9b6,
0xbdb4a4,
0xd9cdb8,
0xc3b6a2,
0xe0d4be
];
let paverIndex = 0;
for (let ix = 0; ix < paverCountX; ix++) {
for (let iz = 0; iz < paverCountZ; iz++) {
const x = -21 + ix * 1.5;
const z = -10.5 + iz * 1.5;
paverDummy.position.set(x, -0.05, z);
paverDummy.rotation.set(0, 0, 0);
paverDummy.scale.set(1, 1, 1);
paverDummy.updateMatrix();
pavers.setMatrixAt(paverIndex, paverDummy.matrix);
const color = new THREE.Color(
paverPalette[Math.floor(Math.random() * paverPalette.length)]
);
color.offsetHSL(
(Math.random() - 0.5) * 0.015,
(Math.random() - 0.5) * 0.05,
(Math.random() - 0.5) * 0.055
);
pavers.setColorAt(paverIndex, color);
paverIndex++;
}
}
pavers.instanceMatrix.needsUpdate = true;
pavers.instanceColor.needsUpdate = true;
pavers.receiveShadow = true;
scene.add(pavers);
// -------------------------------------------------------------------------
// Curved Malecón seawall fragment
//
// Elevations are measured from the promenade surface:
// promenade: 0.00 m
// crest: +1.27 m
// water: -0.78 m
// -------------------------------------------------------------------------
const WALL_CREST = 1.27;
const WATER_TOP = -0.78;
const wallMaterials = [
material(0x3d4949),
material(0x455150),
material(0x354344),
material(0x4d5651),
material(0x3a4546)
];
const capMaterials = [
material(0x5b605a),
material(0x525b57),
material(0x62645c)
];
const rustMaterial = material(0x865f43);
const saltMaterial = material(0xb5b3a1);
const algaeMaterial = material(0x50664d);
function wallZ(x) {
return 0.08 + 0.0036 * x * x;
}
function wallRotation(x) {
return -Math.atan(0.0072 * x);
}
const wallSegmentCount = 29;
const wallSegmentWidth = 1.5;
for (let i = 0; i < wallSegmentCount; i++) {
const x = (i - (wallSegmentCount - 1) / 2) * wallSegmentWidth;
const group = new THREE.Group();
group.position.set(x, 0, wallZ(x));
group.rotation.y = wallRotation(x);
scene.add(group);
const rowData = [
{ y: -0.70, h: 0.70 },
{ y: 0.00, h: 0.70 },
{ y: 0.70, h: 0.70 }
];
rowData.forEach((row, rowIndex) => {
const mat = wallMaterials[
Math.floor(Math.random() * wallMaterials.length)
];
cube(
group,
1.43,
row.h,
0.78,
mat,
(Math.random() - 0.5) * 0.018,
row.y,
(Math.random() - 0.5) * 0.025
);
});
cube(
group,
1.54,
0.22,
0.98,
capMaterials[Math.floor(Math.random() * capMaterials.length)],
0,
1.16,
0
);
// Weathering and salt marks on the promenade-facing wall.
if (i % 2 === 0) {
const patchMat = Math.random() > 0.5 ? rustMaterial : saltMaterial;
cube(
group,
0.18 + Math.random() * 0.32,
0.10 + Math.random() * 0.20,
0.025,
patchMat,
(Math.random() - 0.5) * 0.85,
-0.80 + Math.random() * 1.55,
-0.405
);
}
// A subtle waterline stain.
if (i % 3 === 0) {
cube(
group,
1.15,
0.15,
0.025,
algaeMaterial,
0,
-0.92,
-0.407
);
}
}
// -------------------------------------------------------------------------
// Water: a voxel field plus small moving wave streaks
// -------------------------------------------------------------------------
const waterGeometry = new THREE.BoxGeometry(1.92, 0.18, 1.92);
const waterMaterial = material(0xffffff, {
roughness: 0.48,
metalness: 0.08
});
const waterCountX = 24;
const waterCountZ = 18;
const waterTiles = new THREE.InstancedMesh(
waterGeometry,
waterMaterial,
waterCountX * waterCountZ
);
const waterData = [];
const waterDummy = new THREE.Object3D();
const waterPalette = [
0x287e85,
0x2f8990,
0x23767d,
0x38939a,
0x2b858c
];
let waterIndex = 0;
for (let ix = 0; ix < waterCountX; ix++) {
for (let iz = 0; iz < waterCountZ; iz++) {
const x = -23 + ix * 2;
const z = 1.55 + iz * 2;
const phase = Math.random() * Math.PI * 2;
waterData.push({ x, z, phase });
waterDummy.position.set(x, WATER_TOP - 0.09, z);
waterDummy.rotation.set(0, 0, 0);
waterDummy.scale.set(1, 1, 1);
waterDummy.updateMatrix();
waterTiles.setMatrixAt(waterIndex, waterDummy.matrix);
const color = new THREE.Color(
waterPalette[Math.floor(Math.random() * waterPalette.length)]
);
color.offsetHSL(0, 0, (Math.random() - 0.5) * 0.035);
waterTiles.setColorAt(waterIndex, color);
waterIndex++;
}
}
waterTiles.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waterTiles.instanceMatrix.needsUpdate = true;
waterTiles.instanceColor.needsUpdate = true;
waterTiles.receiveShadow = true;
scene.add(waterTiles);
const waveMaterial = material(0xa7e4df, {
roughness: 0.35,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const waveCount = 54;
const waveMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 0.025, 0.11),
waveMaterial,
waveCount
);
waveMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waveMesh.renderOrder = 2;
scene.add(waveMesh);
const waveData = Array.from({ length: waveCount }, () => ({
x: -23 + Math.random() * 46,
z: 1.0 + Math.random() * 32,
length: 0.45 + Math.random() * 1.3,
speed: 0.12 + Math.random() * 0.22,
phase: Math.random() * Math.PI * 2
}));
const waveDummy = new THREE.Object3D();
// Pale foam fragments close to the wall.
const foamMaterial = material(0xbce8dc, {
roughness: 0.6,
transparent: true,
opacity: 0.72
});
for (let i = 0; i < 13; i++) {
const x = -10 + i * 1.65;
cube(
scene,
0.55 + Math.random() * 0.55,
0.025,
0.13 + Math.random() * 0.12,
foamMaterial,
x,
WATER_TOP + 0.012,
wallZ(x) + 0.57
);
}
// -------------------------------------------------------------------------
// Bench and one simple seated voxel figure
// -------------------------------------------------------------------------
const benchGroup = new THREE.Group();
benchGroup.position.set(1.15, 0, -2.35);
benchGroup.rotation.y = -0.12;
scene.add(benchGroup);
cube(benchGroup, 1.75, 0.14, 0.66, woodMat, 0, 0.46, 0);
cube(benchGroup, 1.75, 0.52, 0.12, woodLightMat, 0, 0.82, -0.30);
[-0.62, 0.62].forEach((x) => {
cube(benchGroup, 0.13, 0.42, 0.13, darkMetalMat, x, 0.21, -0.22);
cube(benchGroup, 0.13, 0.42, 0.13, darkMetalMat, x, 0.21, 0.22);
});
const figure = new THREE.Group();
benchGroup.add(figure);
const shirtMat = material(0x3d9295);
const shirtShadowMat = material(0x327e82);
const shortsMat = material(0xe5d4b4);
const skinMat = material(0x9a6848);
const hairMat = material(0x252729);
const shoeMat = material(0x283034);
// Hips, torso and head.
cube(figure, 0.53, 0.24, 0.37, shortsMat, 0, 0.61, 0.02);
cube(figure, 0.56, 0.62, 0.36, shirtMat, 0, 0.98, -0.01);
cube(figure, 0.38, 0.39, 0.38, skinMat, 0, 1.47, -0.005);
cube(figure, 0.41, 0.16, 0.41, hairMat, 0, 1.66, -0.01);
// Subtle shirt block division.
cube(figure, 0.575, 0.07, 0.375, shirtShadowMat, 0, 0.72, -0.01);
// Bent legs facing the water.
[-0.15, 0.15].forEach((x) => {
cube(figure, 0.19, 0.18, 0.68, shortsMat, x, 0.59, 0.38);
cube(figure, 0.17, 0.49, 0.18, skinMat, x, 0.32, 0.70);
cube(figure, 0.21, 0.12, 0.36, shoeMat, x, 0.08, 0.83);
});
// Arms resting beside the legs.
[-0.35, 0.35].forEach((x) => {
cube(figure, 0.15, 0.42, 0.17, shirtMat, x, 0.95, 0.02);
cube(figure, 0.14, 0.14, 0.43, skinMat, x, 0.66, 0.24);
});
// -------------------------------------------------------------------------
// Scale gauge on the seawall
// -------------------------------------------------------------------------
const gaugeX = 4.2;
const gauge = new THREE.Group();
gauge.position.set(gaugeX, 0, wallZ(gaugeX));
gauge.rotation.y = wallRotation(gaugeX);
scene.add(gauge);
cube(gauge, 0.045, WALL_CREST, 0.055, goldMat, 0, WALL_CREST / 2, -0.535);
cube(gauge, 0.36, 0.04, 0.06, goldMat, 0, 0, -0.535);
cube(gauge, 0.36, 0.04, 0.06, goldMat, 0, WALL_CREST, -0.535);
// -------------------------------------------------------------------------
// Small Malecón details: lamps and mooring posts
// -------------------------------------------------------------------------
const lampGlassMat = material(0xf5d997, {
emissive: 0xe4ad4d,
emissiveIntensity: 0.55,
roughness: 0.5
});
function makeLamp(x, z) {
const lamp = new THREE.Group();
lamp.position.set(x, 0, z);
scene.add(lamp);
cube(lamp, 0.46, 0.22, 0.46, darkMetalMat, 0, 0.11, 0);
cube(lamp, 0.13, 3.05, 0.13, darkMetalMat, 0, 1.63, 0);
cube(lamp, 0.48, 0.52, 0.48, lampGlassMat, 0, 3.36, 0);
cube(lamp, 0.64, 0.12, 0.64, darkMetalMat, 0, 3.68, 0);
cube(lamp, 0.18, 0.20, 0.18, darkMetalMat, 0, 3.82, 0);
}
makeLamp(-7.2, -6.25);
makeLamp(8.8, -5.75);
[-11, -5.8, 7.1, 12].forEach((x) => {
const z = wallZ(x) - 0.88;
cube(scene, 0.28, 0.52, 0.28, darkMetalMat, x, 0.26, z);
cube(scene, 0.38, 0.10, 0.38, darkMetalMat, x, 0.55, z);
});
// -------------------------------------------------------------------------
// Canvas sprite labels
// -------------------------------------------------------------------------
function makeLabel(title, value, accent = "#efb84f") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(19, 42, 49, 0.91)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 12, canvas.height);
ctx.font = "700 25px Arial, sans-serif";
ctx.fillStyle = "rgba(244, 234, 216, 0.72)";
ctx.fillText(title, 35, 52);
ctx.font = "800 55px Arial, sans-serif";
ctx.fillStyle = "#f4ead8";
ctx.fillText(value, 35, 119);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.renderOrder = 10;
return sprite;
}
const crestLabel = makeLabel("WALL CREST", "+1.27 m");
crestLabel.position.set(gaugeX + 0.1, 1.88, wallZ(gaugeX) - 0.72);
crestLabel.scale.set(2.75, 0.86, 1);
scene.add(crestLabel);
const seaLabel = makeLabel("SEA LEVEL", "−0.78 m", "#75d1cf");
seaLabel.position.set(-5.2, -0.05, 2.1);
seaLabel.scale.set(2.25, 0.70, 1);
scene.add(seaLabel);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pinch zoom, wheel zoom and keyboard
// -------------------------------------------------------------------------
const pointers = new Map();
const canvas = renderer.domElement;
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function resetView() {
orbit.yaw = 2.48;
orbit.pitch = 0.43;
orbit.radius = 24;
orbit.zoom = 1;
updateCamera();
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const current = {
x: event.clientX,
y: event.clientY
};
if (pointers.size === 1) {
const dx = current.x - previous.x;
const dy = current.y - previous.y;
orbit.yaw -= dx * 0.005;
orbit.pitch = clamp(orbit.pitch + dy * 0.004, 0.16, 1.08);
updateCamera();
} else if (pointers.size === 2) {
pointers.set(event.pointerId, current);
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (previous.pinchDistance) {
orbit.zoom = clamp(
orbit.zoom * (distance / previous.pinchDistance),
0.62,
1.8
);
updateCamera();
}
previous.pinchDistance = distance;
return;
}
pointers.set(event.pointerId, current);
});
function releasePointer(event) {
pointers.delete(event.pointerId);
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.zoom = clamp(
orbit.zoom * Math.exp(-event.deltaY * 0.001),
0.62,
1.8
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
window.addEventListener("keydown", (event) => {
let changed = true;
switch (event.key) {
case "ArrowLeft":
orbit.yaw += 0.08;
break;
case "ArrowRight":
orbit.yaw -= 0.08;
break;
case "ArrowUp":
orbit.pitch = clamp(orbit.pitch + 0.06, 0.16, 1.08);
break;
case "ArrowDown":
orbit.pitch = clamp(orbit.pitch - 0.06, 0.16, 1.08);
break;
case "+":
case "=":
orbit.zoom = clamp(orbit.zoom * 1.08, 0.62, 1.8);
break;
case "-":
orbit.zoom = clamp(orbit.zoom / 1.08, 0.62, 1.8);
break;
default:
changed = false;
}
if (changed) {
event.preventDefault();
updateCamera();
}
});
document.querySelector("#reset").addEventListener("click", resetView);
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const reduceMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
const delta = Math.min(clock.getDelta(), 0.05);
if (!reduceMotion) {
// Gentle voxel-water motion.
waterData.forEach((tile, index) => {
waterDummy.position.set(
tile.x,
WATER_TOP - 0.09 + Math.sin(elapsed * 0.75 + tile.phase) * 0.022,
tile.z
);
waterDummy.rotation.set(0, 0, 0);
waterDummy.scale.set(1, 1, 1);
waterDummy.updateMatrix();
waterTiles.setMatrixAt(index, waterDummy.matrix);
});
waterTiles.instanceMatrix.needsUpdate = true;
// Sliding highlights.
waveData.forEach((wave, index) => {
wave.x += wave.speed * delta;
if (wave.x > 24) wave.x = -24;
waveDummy.position.set(
wave.x,
WATER_TOP + 0.025,
wave.z + Math.sin(elapsed * 0.45 + wave.phase) * 0.08
);
waveDummy.rotation.set(0, 0, 0);
waveDummy.scale.set(wave.length, 1, 1);
waveDummy.updateMatrix();
waveMesh.setMatrixAt(index, waveDummy.matrix);
});
waveMesh.instanceMatrix.needsUpdate = true;
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```