Prompt specification
Build or customize the Three.js 3D scene "Island and narrow strait model" 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: Island and narrow strait model
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/island-and-narrow-strait-model-x1305
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this voxel scene: Create two large land blocks separated by a narrow water channel and one small island. Label strait and island without implying that every channel has identical depth.
Working Three.js Starter (island-and-narrow-strait-model-x1305):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Strait</title>
<style>
:root {
--ink: #17323a;
--paper: rgba(247, 251, 249, 0.91);
--water: #1e92b3;
--land: #527c50;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe7e5;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
outline: none;
}
.card {
position: fixed;
z-index: 10;
background: var(--paper);
border: 1px solid rgba(23, 50, 58, 0.12);
border-radius: 14px;
box-shadow: 0 12px 35px rgba(28, 59, 64, 0.12);
backdrop-filter: blur(12px);
}
.title-card {
top: 22px;
left: 22px;
padding: 16px 19px;
pointer-events: none;
}
.eyebrow {
display: block;
margin-bottom: 5px;
color: #567078;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.4vw, 31px);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin-top: 7px;
color: #567078;
font-size: 12px;
}
.depth-card {
right: 22px;
bottom: 22px;
width: min(330px, calc(100vw - 44px));
padding: 15px 17px;
pointer-events: none;
}
.depth-card strong {
display: block;
margin-bottom: 5px;
font-size: 12px;
letter-spacing: 0.04em;
}
.depth-card p {
margin: 0;
color: #536b72;
font-size: 12px;
line-height: 1.45;
}
.controls {
left: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 12px;
padding: 10px 12px;
color: #526a71;
font-size: 11px;
}
.controls span {
white-space: nowrap;
}
button {
appearance: none;
border: 0;
border-radius: 8px;
padding: 7px 10px;
background: #204e5b;
color: white;
cursor: pointer;
font: inherit;
font-weight: 700;
}
button:hover {
background: #153f4a;
}
.map-label {
position: fixed;
z-index: 8;
min-width: 132px;
padding: 9px 12px;
border-radius: 10px;
border-left: 4px solid var(--water);
background: rgba(19, 48, 57, 0.91);
color: white;
box-shadow: 0 8px 24px rgba(19, 48, 57, 0.24);
pointer-events: none;
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
}
.map-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -11px;
width: 2px;
height: 10px;
background: rgba(19, 48, 57, 0.72);
}
.map-label small {
display: block;
margin-bottom: 3px;
color: #9ec2ca;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.map-label strong {
display: block;
font-size: 14px;
letter-spacing: 0.01em;
}
.map-label em {
display: block;
margin-top: 3px;
color: #c8dcdf;
font-size: 10px;
font-style: normal;
}
.island-label {
border-left-color: #85a95f;
}
.island-label small {
color: #bad29f;
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #dbe7e5;
color: #4b686f;
font-size: 13px;
font-weight: 700;
letter-spacing: 0.05em;
}
@media (max-width: 720px) {
.title-card {
top: 12px;
left: 12px;
padding: 13px 15px;
}
.depth-card {
right: 12px;
bottom: 64px;
width: min(300px, calc(100vw - 24px));
}
.controls {
left: 12px;
bottom: 12px;
}
.controls .desktop-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div id="loading">Building the voxel scene…</div>
<header class="card title-card">
<span class="eyebrow">Bathymetric study</span>
<h1>Voxel Strait</h1>
<div class="subtitle">Two land blocks, a narrow waterway, and one offshore isle</div>
</header>
<div id="label-strait" class="map-label">
<small>Waterway</small>
<strong>The Strait</strong>
<em>Depth varies along this reach</em>
</div>
<div id="label-island" class="map-label island-label">
<small>Landform</small>
<strong>Tiny Isle</strong>
<em>Small offshore island</em>
</div>
<aside class="card depth-card">
<strong>Depth note</strong>
<p>
This strait includes shoals and deeper pockets. No uniform depth is implied
for this reach or for any other channel.
</p>
</aside>
<div class="card controls">
<span class="desktop-hint">Drag to orbit · wheel or pinch to zoom · right-drag to pan</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";
const viewport = document.getElementById("viewport");
const loading = document.getElementById("loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbe7e5);
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;
viewport.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-20, 20, 16, -16, 0.1, 150);
const hemi = new THREE.HemisphereLight(0xf7ffff, 0x6b776c, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.1);
sun.position.set(-18, 31, 19);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -28;
sun.shadow.camera.right = 28;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 80;
sun.shadow.bias = -0.0006;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa9d8e5, 1.25);
fill.position.set(20, 14, -20);
scene.add(fill);
const world = new THREE.Group();
scene.add(world);
const dummy = new THREE.Object3D();
function hash2(x, z) {
const value = Math.sin(x * 127.1 + z * 311.7 + 19.73) * 43758.5453123;
return value - Math.floor(value);
}
function setInstance(mesh, index, x, y, z, sx, sy, sz, color, rotationY = 0) {
dummy.position.set(x, y, z);
dummy.rotation.set(0, rotationY, 0);
dummy.scale.set(sx, sy, sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (color) {
mesh.setColorAt(index, color);
}
}
function colorWithVariation(base, variation = 0.025) {
const color = new THREE.Color(base);
color.offsetHSL(
(Math.random() - 0.5) * variation * 0.35,
(Math.random() - 0.5) * variation,
(Math.random() - 0.5) * variation
);
return color;
}
// Diorama base and seabed.
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x294f59,
roughness: 0.88
});
const base = new THREE.Mesh(
new THREE.BoxGeometry(38, 1.2, 34),
baseMaterial
);
base.position.y = -5.95;
base.receiveShadow = true;
base.castShadow = true;
world.add(base);
const seabedMaterial = new THREE.MeshStandardMaterial({
color: 0x547b78,
roughness: 1
});
const seabed = new THREE.Mesh(
new THREE.BoxGeometry(37, 0.12, 33),
seabedMaterial
);
seabed.position.y = -5.29;
seabed.receiveShadow = true;
world.add(seabed);
const shadowMaterial = new THREE.MeshStandardMaterial({
color: 0xd5e0de,
roughness: 1
});
const shadowFloor = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
shadowMaterial
);
shadowFloor.rotation.x = -Math.PI / 2;
shadowFloor.position.y = -6.57;
shadowFloor.receiveShadow = true;
scene.add(shadowFloor);
// Stepped, uneven bed inside the strait.
const bedCells = [];
for (let x = -1; x <= 1; x++) {
for (let z = -10; z <= 10; z++) {
const n = hash2(x * 2.1 + 8, z * 0.73 - 4);
let top = -1.55 - n * 1.7;
// A deeper pocket and a visible shoal demonstrate local variation.
if (x === 0 && z >= -2 && z <= 2) {
top -= 0.45;
}
if (x === -1 && z === -4) {
top = -1.15;
}
bedCells.push({ x, z, top });
}
}
const bedGeometry = new THREE.BoxGeometry(1, 1, 1);
const bedMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95
});
const bedMesh = new THREE.InstancedMesh(
bedGeometry,
bedMaterial,
bedCells.length
);
bedCells.forEach((cell, index) => {
const bottom = -5.25;
const height = cell.top - bottom;
const deepColor = new THREE.Color(0x365f63);
const shoalColor = new THREE.Color(0x8a9375);
const color = deepColor.clone().lerp(shoalColor, THREE.MathUtils.clamp((cell.top + 3.9) / 2.8, 0, 1));
setInstance(
bedMesh,
index,
cell.x,
bottom + height / 2,
cell.z,
0.98,
height,
0.98,
color
);
});
bedMesh.instanceMatrix.needsUpdate = true;
bedMesh.instanceColor.needsUpdate = true;
bedMesh.receiveShadow = true;
world.add(bedMesh);
// Build the two large land blocks.
const terrain = [];
const heightMap = new Map();
const treeCells = [];
const rockCells = [];
const columns = 14;
const rows = 20;
for (let side = 0; side < 2; side++) {
for (let ix = 0; ix < columns; ix++) {
for (let iz = 0; iz < rows; iz++) {
const x = side === 0 ? -15 + ix : 2 + ix;
const z = -9.5 + iz;
const n = hash2(x + side * 17, z - 11);
const n2 = hash2(x * 0.47 - 3, z * 0.61 + 9);
const edge = Math.min(ix, columns - 1 - ix, iz, rows - 1 - iz);
const westPeak = 2.25 * Math.exp(
-((x + 8.5) ** 2 + (z + 2.8) ** 2) / 34
);
const eastPeak = 2.55 * Math.exp(
-((x - 8.2) ** 2 + (z - 2.2) ** 2) / 42
);
const ridge = Math.pow(Math.max(0, n2 - 0.54), 1.4) * 4.2;
let height =
1.18 +
Math.min(edge, 3) * 0.14 +
(n - 0.5) * 0.74 +
(side === 0 ? westPeak : eastPeak) +
ridge;
height = THREE.MathUtils.clamp(height, 1.12, 4.65);
terrain.push({ x, z, height, side, edge });
heightMap.set(`${x},${z}`, height);
if (edge > 1 && height < 3.15 && hash2(x + 31, z - 7) > 0.935) {
treeCells.push({ x, z, height });
}
if (height > 2.65 && hash2(x - 41, z + 13) > 0.965) {
rockCells.push({ x, z, height });
}
}
}
}
const substrateGeometry = new THREE.BoxGeometry(1, 1, 1);
const substrateMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96
});
const capMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92
});
const substrateMesh = new THREE.InstancedMesh(
substrateGeometry,
substrateMaterial,
terrain.length
);
const capMesh = new THREE.InstancedMesh(
substrateGeometry,
capMaterial,
terrain.length
);
function terrainCapColor(height, side, x, z) {
const n = hash2(x - 14, z + 23);
if (height > 4.05) {
return colorWithVariation(0xd6dcd5, 0.025);
}
if (height > 3.35) {
return colorWithVariation(0x7c8a79, 0.035);
}
const base = side === 0 ? 0x628557 : 0x78925d;
const color = new THREE.Color(base);
color.offsetHSL(0, 0, (n - 0.5) * 0.085);
return color;
}
terrain.forEach((cell, index) => {
const capHeight = 0.28;
const substrateHeight = cell.height - capHeight;
const sideColor = cell.height > 3.35
? new THREE.Color(0x77766c)
: new THREE.Color(0x80694f);
sideColor.offsetHSL(
0,
0,
(hash2(cell.x + 5, cell.z) - 0.5) * 0.08
);
setInstance(
substrateMesh,
index,
cell.x,
0.04 + substrateHeight / 2,
cell.z,
0.98,
substrateHeight,
0.98,
sideColor
);
setInstance(
capMesh,
index,
cell.x,
0.04 + cell.height - capHeight / 2,
cell.z,
0.98,
capHeight,
0.98,
terrainCapColor(cell.height, cell.side, cell.x, cell.z)
);
});
substrateMesh.instanceMatrix.needsUpdate = true;
substrateMesh.instanceColor.needsUpdate = true;
substrateMesh.castShadow = true;
substrateMesh.receiveShadow = true;
world.add(substrateMesh);
capMesh.instanceMatrix.needsUpdate = true;
capMesh.instanceColor.needsUpdate = true;
capMesh.castShadow = true;
capMesh.receiveShadow = true;
world.add(capMesh);
// Voxel trees.
const trunkMesh = new THREE.InstancedMesh(
substrateGeometry,
new THREE.MeshStandardMaterial({ color: 0x5e4938, roughness: 1 }),
treeCells.length
);
const foliageMesh = new THREE.InstancedMesh(
substrateGeometry,
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.88 }),
treeCells.length * 2
);
treeCells.forEach((cell, index) => {
setInstance(
trunkMesh,
index,
cell.x,
cell.height + 0.36,
cell.z,
0.18,
0.64,
0.18,
new THREE.Color(0x5e4938)
);
const green = new THREE.Color(
hash2(cell.x * 2, cell.z) > 0.5 ? 0x315c45 : 0x3d704d
);
setInstance(
foliageMesh,
index * 2,
cell.x,
cell.height + 0.91,
cell.z,
0.76,
0.72,
0.76,
green
);
setInstance(
foliageMesh,
index * 2 + 1,
cell.x,
cell.height + 1.33,
cell.z,
0.48,
0.48,
0.48,
colorWithVariation(0x3f7550, 0.03)
);
});
trunkMesh.instanceMatrix.needsUpdate = true;
trunkMesh.instanceColor.needsUpdate = true;
trunkMesh.castShadow = true;
world.add(trunkMesh);
foliageMesh.instanceMatrix.needsUpdate = true;
foliageMesh.instanceColor.needsUpdate = true;
foliageMesh.castShadow = true;
world.add(foliageMesh);
// Small exposed rocks.
const rockMesh = new THREE.InstancedMesh(
substrateGeometry,
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1 }),
rockCells.length
);
rockCells.forEach((cell, index) => {
const size = 0.35 + hash2(cell.x, cell.z) * 0.38;
setInstance(
rockMesh,
index,
cell.x + 0.12,
cell.height + size * 0.42,
cell.z - 0.08,
size,
size * 0.72,
size * 0.85,
colorWithVariation(0x8b9188, 0.04),
hash2(cell.z, cell.x) * 0.5
);
});
rockMesh.instanceMatrix.needsUpdate = true;
rockMesh.instanceColor.needsUpdate = true;
rockMesh.castShadow = true;
world.add(rockMesh);
// The one small island, in the water just beyond the land blocks.
const islandColumns = [];
for (let dx = -1; dx <= 1; dx++) {
for (let dz = -1; dz <= 1; dz++) {
const edge = Math.max(Math.abs(dx), Math.abs(dz));
const height =
1.78 - edge * 0.29 + (hash2(dx + 9, dz + 4) - 0.5) * 0.16;
islandColumns.push({
x: dx,
z: 11 + dz,
height,
edge
});
}
}
const islandSubstrate = new THREE.InstancedMesh(
substrateGeometry,
substrateMaterial,
islandColumns.length
);
const islandCaps = new THREE.InstancedMesh(
substrateGeometry,
capMaterial,
islandColumns.length
);
islandColumns.forEach((cell, index) => {
const bottom = -4.95;
const capHeight = 0.2;
const rockHeight = cell.height - bottom - capHeight;
setInstance(
islandSubstrate,
index,
cell.x,
bottom + rockHeight / 2,
cell.z,
0.98,
rockHeight,
0.98,
colorWithVariation(0x756b55, 0.025)
);
const capColor = cell.edge === 1
? colorWithVariation(0xc7b88b, 0.025)
: colorWithVariation(0x70915b, 0.03);
setInstance(
islandCaps,
index,
cell.x,
cell.height - capHeight / 2,
cell.z,
0.98,
capHeight,
0.98,
capColor
);
});
islandSubstrate.instanceMatrix.needsUpdate = true;
islandSubstrate.instanceColor.needsUpdate = true;
islandSubstrate.castShadow = true;
islandSubstrate.receiveShadow = true;
world.add(islandSubstrate);
islandCaps.instanceMatrix.needsUpdate = true;
islandCaps.instanceColor.needsUpdate = true;
islandCaps.castShadow = true;
islandCaps.receiveShadow = true;
world.add(islandCaps);
// A single voxel tree on the island.
const islandTree = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.BoxGeometry(0.2, 0.7, 0.2),
new THREE.MeshStandardMaterial({ color: 0x644a37, roughness: 1 })
);
trunk.position.y = 2.12;
const leaves = new THREE.Mesh(
new THREE.BoxGeometry(0.86, 0.82, 0.86),
new THREE.MeshStandardMaterial({ color: 0x3e704b, roughness: 0.9 })
);
leaves.position.y = 2.82;
islandTree.add(trunk, leaves);
islandTree.traverse(object => {
if (object.isMesh) {
object.castShadow = true;
object.receiveShadow = true;
}
});
world.add(islandTree);
// Transparent water surface.
const waterMaterial = new THREE.MeshPhysicalMaterial({
color: 0x35a4bd,
transparent: true,
opacity: 0.58,
roughness: 0.3,
metalness: 0.04,
depthWrite: false,
side: THREE.DoubleSide
});
const water = new THREE.Mesh(
new THREE.PlaneGeometry(37, 33),
waterMaterial
);
water.rotation.x = -Math.PI / 2;
water.position.y = 0.14;
water.receiveShadow = true;
water.renderOrder = 2;
world.add(water);
// Fine square-wave highlights.
const wavePositions = [];
for (let i = 0; i < 150; i++) {
const x = -18 + hash2(i, 91) * 36;
const z = -16 + hash2(i * 1.7, 22) * 32;
const overLand =
z > -10 &&
z < 10 &&
((x > -15.5 && x < -1.5) || (x > 1.5 && x < 15.5));
if (!overLand) {
const length = 0.35 + hash2(i + 8, z) * 1.15;
wavePositions.push(x, 0.19, z, x + length, 0.19, z);
}
}
const waveGeometry = new THREE.BufferGeometry();
waveGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(wavePositions, 3)
);
const waveMaterial = new THREE.LineBasicMaterial({
color: 0xd9f8fb,
transparent: true,
opacity: 0.28,
depthWrite: false
});
const waves = new THREE.LineSegments(waveGeometry, waveMaterial);
waves.renderOrder = 3;
world.add(waves);
// A local sounding marker emphasizes that a reading is not a universal depth.
const marker = new THREE.Group();
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xf1b84b,
roughness: 0.58,
metalness: 0.08
});
const markerFloat = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.24, 0.34),
markerMaterial
);
markerFloat.position.y = 0.31;
const markerStem = new THREE.Mesh(
new THREE.BoxGeometry(0.07, 2.1, 0.07),
markerMaterial
);
markerStem.position.y = -0.82;
marker.add(markerFloat, markerStem);
marker.position.set(0.78, 0, -2.1);
marker.traverse(object => {
if (object.isMesh) {
object.castShadow = true;
}
});
world.add(marker);
// Camera controls, implemented directly without external controls modules.
const controls = {
theta: 0.72,
phi: 0.88,
target: new THREE.Vector3(0, 0, 1.2),
zoom: 1
};
function updateCamera() {
const radius = 52;
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + radius * sinPhi * Math.sin(controls.theta),
controls.target.y + radius * Math.cos(controls.phi),
controls.target.z + radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const halfHeight = Math.max(16.5, 18.5 / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const pointers = new Map();
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
event.preventDefault();
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
});
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 1;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointermove", event => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointers.size === 1) {
if (previous.button === 2 || event.shiftKey) {
const scale = (camera.top - camera.bottom) / camera.zoom / window.innerHeight;
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0);
right.y = 0;
right.normalize();
const screenUp = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 1);
screenUp.y = 0;
if (screenUp.lengthSq() < 0.001) {
screenUp.set(0, 0, -1);
} else {
screenUp.normalize();
}
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(screenUp, dy * scale);
controls.target.x = THREE.MathUtils.clamp(controls.target.x, -12, 12);
controls.target.z = THREE.MathUtils.clamp(controls.target.z, -10, 12);
} else {
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.005,
0.38,
1.32
);
}
} else if (pointers.size === 2) {
const oldDistance = pointerDistance();
const newDistance = pointerDistance();
// Recalculate using the live positions for a stable pinch response.
const values = [...pointers.values()];
const liveDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (liveDistance > 0 && oldDistance > 0) {
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * (liveDistance / Math.max(oldDistance, 1)),
0.65,
2.4
);
}
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.4
);
updateCamera();
},
{ passive: false }
);
document.getElementById("reset").addEventListener("click", () => {
controls.theta = 0.72;
controls.phi = 0.88;
controls.zoom = 1;
controls.target.set(0, 0, 1.2);
updateCamera();
});
// HTML labels are projected from 3D anchors every frame.
const straitLabel = document.getElementById("label-strait");
const islandLabel = document.getElementById("label-island");
const straitAnchor = new THREE.Vector3(0, 0.8, -2.1);
const islandAnchor = new THREE.Vector3(0, 3.7, 12);
function placeLabel(element, anchor, verticalOffset) {
const projected = anchor.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
element.style.transform =
`translate(-50%, calc(-100% - ${verticalOffset}px))`;
}
const clock = new THREE.Clock();
function animate() {
const time = clock.getElapsedTime();
waveMaterial.opacity = 0.23 + Math.sin(time * 0.8) * 0.045;
markerFloat.position.y = 0.31 + Math.sin(time * 1.8) * 0.035;
camera.updateMatrixWorld();
placeLabel(straitLabel, straitAnchor, 12);
placeLabel(islandLabel, islandAnchor, 14);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
loading.style.display = "none";
</script>
</body>
</html>
```