Prompt specification
Build or customize the Three.js 3D scene "Namib dune crest and slip-face fragment" 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: Namib dune crest and slip-face fragment
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/namib-dune-crest-and-slip-face-fragment-x3093
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing the Namib Desert's coastal dunes. Build a referenced dune segment with windward slope, crest, and steeper slip face. Use a directional wind marker and keep the landform recognizable from several camera angles. Stylized solid forms.
Working Three.js Starter (namib-dune-crest-and-slip-face-fragment-x3093):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Namib — Coastal Dune Segment</title>
<style>
:root {
--ink: #18333a;
--paper: rgba(255, 248, 229, 0.9);
--line: rgba(24, 51, 58, 0.16);
--windward: #d98a3d;
--crest: #f1b85b;
--slip: #a94e2d;
--ocean: #2d7881;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eadbbd;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--paper);
box-shadow: 0 14px 40px rgba(58, 38, 20, 0.12);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.title-panel {
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 19px 21px 18px;
border-radius: 4px;
}
.eyebrow {
margin: 0 0 7px;
color: #93612e;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 4vw, 45px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
max-width: 330px;
margin: 13px 0 0;
color: rgba(24, 51, 58, 0.72);
font-size: 12px;
line-height: 1.55;
}
.wind-panel {
top: 24px;
right: 24px;
width: 218px;
padding: 14px 16px;
border-radius: 4px;
}
.wind-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.wind-heading::after {
width: 7px;
height: 7px;
border-radius: 50%;
background: #dc9a43;
content: "";
box-shadow: 0 0 0 4px rgba(220, 154, 67, 0.16);
}
.wind-route {
display: grid;
grid-template-columns: 1fr auto 1fr;
align-items: center;
gap: 9px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
}
.wind-route span:last-child {
text-align: right;
}
.wind-arrow {
color: #d88735;
font-size: 25px;
line-height: 1;
}
.wind-note {
margin: 9px 0 0;
color: rgba(24, 51, 58, 0.58);
font-size: 10px;
line-height: 1.4;
}
.view-panel {
bottom: 24px;
left: 24px;
padding: 12px;
border-radius: 4px;
}
.view-label {
margin: 0 0 9px 3px;
color: rgba(24, 51, 58, 0.58);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.view-buttons {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
button {
appearance: none;
border: 1px solid rgba(24, 51, 58, 0.14);
border-radius: 3px;
padding: 9px 11px;
background: rgba(255, 255, 255, 0.48);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.035em;
cursor: pointer;
transition:
background 160ms ease,
color 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(24, 51, 58, 0.3);
background: rgba(255, 255, 255, 0.8);
}
button.active {
border-color: var(--ink);
background: var(--ink);
color: #fff8e8;
}
.legend-panel {
right: 24px;
bottom: 24px;
width: 218px;
padding: 15px 16px;
border-radius: 4px;
}
.legend-title {
margin: 0 0 11px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 12px 1fr;
align-items: center;
gap: 9px;
margin: 8px 0;
font-size: 11px;
}
.swatch {
width: 12px;
height: 12px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(24, 51, 58, 0.08);
}
.swatch.windward {
background: var(--windward);
}
.swatch.crest {
background: var(--crest);
}
.swatch.slip {
background: var(--slip);
}
.legend-detail {
margin: 12px 0 0;
padding-top: 11px;
border-top: 1px solid var(--line);
color: rgba(24, 51, 58, 0.58);
font-size: 10px;
line-height: 1.5;
}
.hint {
position: fixed;
z-index: 9;
bottom: 28px;
left: 50%;
transform: translateX(-50%);
padding: 8px 11px;
border-radius: 99px;
background: rgba(24, 51, 58, 0.76);
color: rgba(255, 248, 230, 0.88);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.06em;
pointer-events: none;
backdrop-filter: blur(8px);
}
.annotation {
position: fixed;
z-index: 6;
transform: translate(-50%, -125%);
padding: 6px 9px;
border: 1px solid rgba(24, 51, 58, 0.17);
border-radius: 3px;
background: rgba(255, 249, 235, 0.9);
box-shadow: 0 5px 18px rgba(48, 29, 12, 0.12);
color: var(--ink);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
opacity: 0;
transition: opacity 150ms ease;
}
.annotation::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 14px;
background: rgba(24, 51, 58, 0.5);
content: "";
}
@media (max-width: 800px) {
.title-panel {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 14px 16px;
}
.title-panel .subtitle {
display: none;
}
h1 {
font-size: 29px;
}
.wind-panel {
top: 108px;
right: 14px;
width: 178px;
}
.wind-note {
display: none;
}
.view-panel {
right: 14px;
bottom: 14px;
left: 14px;
width: auto;
}
.view-buttons {
flex-wrap: nowrap;
overflow-x: auto;
}
button {
flex: 0 0 auto;
}
.legend-panel,
.hint {
display: none;
}
.annotation {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive three-dimensional model of the Namib coastal dunes"></div>
<header class="panel title-panel">
<p class="eyebrow">Namib Desert · Atlantic Coast</p>
<h1>Coastal Dune<br />Segment</h1>
<p class="subtitle">
A stylized transverse dune formed by persistent onshore wind:
a long windward rise, a sharp crest, and a steep lee slip face.
</p>
</header>
<aside class="panel wind-panel">
<div class="wind-heading">Prevailing wind</div>
<div class="wind-route">
<span>Atlantic</span>
<span class="wind-arrow">→</span>
<span>Inland</span>
</div>
<p class="wind-note">The world-space arrow shows the same onshore-to-inland direction.</p>
</aside>
<nav class="panel view-panel" aria-label="Camera views">
<p class="view-label">Inspect the landform</p>
<div class="view-buttons">
<button type="button" data-view="overview" class="active">Overview</button>
<button type="button" data-view="windward">Windward</button>
<button type="button" data-view="crest">Along crest</button>
<button type="button" data-view="slip">Slip face</button>
</div>
</nav>
<aside class="panel legend-panel">
<p class="legend-title">Landform key</p>
<div class="legend-row">
<span class="swatch windward"></span>
<span>Windward slope · gradual</span>
</div>
<div class="legend-row">
<span class="swatch crest"></span>
<span>Crest · narrow and rounded</span>
</div>
<div class="legend-row">
<span class="swatch slip"></span>
<span>Slip face · steep</span>
</div>
<p class="legend-detail">
Schematic solid forms, not a surveyed site. Use the camera presets or drag to orbit.
</p>
</aside>
<div class="hint">Drag to orbit · Scroll or pinch to zoom</div>
<div id="label-windward" class="annotation">Windward slope</div>
<div id="label-crest" class="annotation">Crest</div>
<div id="label-slip" class="annotation">Slip face</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.12;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eadbbd");
scene.fog = new THREE.Fog("#eadbbd", 48, 125);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
180
);
const hemisphere = new THREE.HemisphereLight("#fff3cf", "#9f5d37", 2.15);
scene.add(hemisphere);
const sunLight = new THREE.DirectionalLight("#fff0ca", 3.2);
sunLight.position.set(-18, 29, 15);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -40;
sunLight.shadow.camera.right = 40;
sunLight.shadow.camera.top = 40;
sunLight.shadow.camera.bottom = -40;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 90;
sunLight.shadow.bias = -0.00025;
sunLight.shadow.normalBias = 0.035;
scene.add(sunLight);
// ---------------------------------------------------------------------
// Ground and stylized Atlantic coastline
// ---------------------------------------------------------------------
const groundMaterial = new THREE.MeshStandardMaterial({
color: "#d99a50",
roughness: 0.98,
metalness: 0
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(180, 180),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
function shoreline(z) {
return (
17 +
Math.sin(z * 0.12) * 0.62 +
Math.sin(z * 0.31 + 1.1) * 0.24
);
}
function createCoastalStrip(innerOffset, outerOffset, y, material) {
const positions = [];
const indices = [];
const segments = 100;
for (let i = 0; i <= segments; i++) {
const z = -70 + (140 * i) / segments;
const edge = shoreline(z);
positions.push(edge + innerOffset, y, z);
positions.push(edge + outerOffset, y, z);
if (i < segments) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
createCoastalStrip(
-2.7,
0.12,
0.012,
new THREE.MeshStandardMaterial({
color: "#e5ad61",
roughness: 0.95
})
);
const oceanMaterial = new THREE.MeshStandardMaterial({
color: "#2d7881",
roughness: 0.34,
metalness: 0.08
});
const oceanPositions = [];
const oceanIndices = [];
const oceanSegments = 100;
for (let i = 0; i <= oceanSegments; i++) {
const z = -70 + (140 * i) / oceanSegments;
const edge = shoreline(z);
oceanPositions.push(edge + 0.08, 0.035, z);
oceanPositions.push(105, 0.035, z);
if (i < oceanSegments) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
oceanIndices.push(a, c, b, b, c, d);
}
}
const oceanGeometry = new THREE.BufferGeometry();
oceanGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(oceanPositions, 3)
);
oceanGeometry.setIndex(oceanIndices);
oceanGeometry.computeVertexNormals();
const ocean = new THREE.Mesh(oceanGeometry, oceanMaterial);
ocean.receiveShadow = true;
scene.add(ocean);
createCoastalStrip(
0.12,
0.76,
0.055,
new THREE.MeshBasicMaterial({
color: "#d9eee5",
transparent: true,
opacity: 0.82,
side: THREE.DoubleSide
})
);
const waveMaterial = new THREE.MeshBasicMaterial({
color: "#c7e5df",
transparent: true,
opacity: 0.42,
side: THREE.DoubleSide
});
let randomSeed = 4321;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
const waveDashes = [];
for (let i = 0; i < 34; i++) {
const z = -54 + random() * 108;
const x = 21 + random() * 62;
const dash = new THREE.Mesh(
new THREE.PlaneGeometry(1.2 + random() * 2.7, 0.08 + random() * 0.09),
waveMaterial
);
dash.rotation.x = -Math.PI / 2;
dash.rotation.z = (random() - 0.5) * 0.12;
dash.position.set(x, 0.075, z);
scene.add(dash);
waveDashes.push(dash);
}
// ---------------------------------------------------------------------
// Asymmetric dune geometry
//
// Each ridge follows +Z. Wind arrives from the ocean at +X.
// The windward side is long and gradual; the lee side is short and steep.
// ---------------------------------------------------------------------
const duneColors = {
windward: new THREE.Color("#d98a3d"),
crest: new THREE.Color("#f1b85b"),
slip: new THREE.Color("#a94e2d")
};
function createDuneSegment({
x,
height,
length,
windwardLength,
slipLength,
phase = 0,
rows = 34
}) {
const positions = [];
const colors = [];
const windwardSteps = 10;
const slipSteps = 10;
const profileRows = [];
for (let row = 0; row <= rows; row++) {
const u = row / rows;
const z = -length / 2 + length * u;
const crestX =
x +
Math.sin(z * 0.15 + phase) * 0.75 +
Math.sin(z * 0.43 + phase * 1.7) * 0.24;
const taper =
0.1 +
0.9 * Math.pow(Math.max(0, Math.sin(Math.PI * u)), 0.42);
const localHeight =
height *
taper *
(1 + Math.sin(z * 0.53 + phase) * 0.055);
const profile = [];
// Long, gently rising windward slope.
for (let k = 0; k <= windwardSteps; k++) {
const q = k / windwardSteps;
const px =
crestX +
0.28 +
(windwardLength - 0.28) * (1 - q);
const py = 0.025 + (localHeight - 0.025) * Math.pow(q, 1.55);
profile.push(new THREE.Vector3(px, py, z));
}
// Rounded crest.
profile.push(new THREE.Vector3(crestX, localHeight, z));
// Short, steep slip face.
for (let k = 1; k <= slipSteps; k++) {
const q = k / slipSteps;
const px =
crestX -
0.22 -
(slipLength - 0.22) * q;
const py =
0.025 +
localHeight * 0.985 * Math.pow(1 - q, 1.04);
profile.push(new THREE.Vector3(px, py, z));
}
profileRows.push({ z, crestX, profile });
}
function addTriangle(a, b, c, baseColor, variation) {
positions.push(
a.x, a.y, a.z,
b.x, b.y, b.z,
c.x, c.y, c.z
);
const color = baseColor.clone();
color.offsetHSL(0, 0, variation);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
for (let row = 0; row < rows; row++) {
const current = profileRows[row];
const next = profileRows[row + 1];
const profileCount = current.profile.length;
for (let j = 0; j < profileCount - 1; j++) {
const a = current.profile[j];
const b = current.profile[j + 1];
const c = next.profile[j];
const d = next.profile[j + 1];
const midpointX = (a.x + b.x) * 0.5;
const offset = midpointX - current.crestX;
let color;
if (offset > 0.34) {
color = duneColors.windward;
} else if (offset > -0.28) {
color = duneColors.crest;
} else {
color = duneColors.slip;
}
const variation =
Math.sin(current.z * 0.31 + j * 0.47 + phase) * 0.018;
addTriangle(a, b, c, color, variation);
addTriangle(b, d, c, color, variation * 0.8);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.91,
metalness: 0,
flatShading: true
});
const dune = new THREE.Mesh(geometry, material);
dune.castShadow = true;
dune.receiveShadow = true;
scene.add(dune);
return dune;
}
// The foreground segment is the principal, fully readable landform.
createDuneSegment({
x: 1.2,
height: 5.3,
length: 27,
windwardLength: 10,
slipLength: 4.2,
phase: 0.4
});
// Receding dune segments establish the coastal dune field.
createDuneSegment({
x: -12,
height: 4.15,
length: 34,
windwardLength: 10.5,
slipLength: 4.7,
phase: 1.35,
rows: 38
});
createDuneSegment({
x: -25,
height: 3.05,
length: 42,
windwardLength: 11,
slipLength: 5.1,
phase: 2.15,
rows: 42
});
// ---------------------------------------------------------------------
// World-space wind marker
// ---------------------------------------------------------------------
const windGroup = new THREE.Group();
windGroup.position.set(12.5, 7.2, -12);
scene.add(windGroup);
const arrowMaterial = new THREE.MeshStandardMaterial({
color: "#fff0b0",
emissive: "#a45d21",
emissiveIntensity: 0.18,
roughness: 0.55
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.095, 0.095, 7.2, 10),
arrowMaterial
);
shaft.rotation.z = Math.PI / 2;
shaft.position.x = 1.15;
windGroup.add(shaft);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.38, 1.35, 12),
arrowMaterial
);
arrowHead.rotation.z = Math.PI / 2;
arrowHead.position.x = -3.25;
windGroup.add(arrowHead);
function makeTextSprite(text) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
const left = 8;
const top = 18;
const width = 496;
const height = 92;
const radius = 16;
context.beginPath();
context.moveTo(left + radius, top);
context.lineTo(left + width - radius, top);
context.quadraticCurveTo(
left + width,
top,
left + width,
top + radius
);
context.lineTo(left + width, top + height - radius);
context.quadraticCurveTo(
left + width,
top + height,
left + width - radius,
top + height
);
context.lineTo(left + radius, top + height);
context.quadraticCurveTo(
left,
top + height,
left,
top + height - radius
);
context.lineTo(left, top + radius);
context.quadraticCurveTo(left, top, left + radius, top);
context.closePath();
context.fillStyle = "rgba(24, 51, 58, 0.88)";
context.fill();
context.fillStyle = "#fff4d5";
context.font = "700 31px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 65);
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.scale.set(6.3, 1.575, 1);
sprite.renderOrder = 10;
return sprite;
}
const windLabel = makeTextSprite("PREVAILING WIND");
windLabel.position.set(0, 1.35, 0);
windGroup.add(windLabel);
// A simple, distant sun disc.
const sunDisc = new THREE.Mesh(
new THREE.SphereGeometry(3.1, 24, 16),
new THREE.MeshBasicMaterial({ color: "#ffe4a2", fog: true })
);
sunDisc.position.set(-42, 25, -53);
scene.add(sunDisc);
// ---------------------------------------------------------------------
// Custom camera controls
// ---------------------------------------------------------------------
const desired = {
theta: 0.76,
phi: 1.02,
radius: 45,
target: new THREE.Vector3(-3, 1.7, 0)
};
const current = {
theta: desired.theta,
phi: desired.phi,
radius: desired.radius,
target: desired.target.clone()
};
const cameraTarget = new THREE.Vector3();
let dragging = false;
let pointerId = null;
let previousX = 0;
let previousY = 0;
const viewButtons = [...document.querySelectorAll("[data-view]")];
function setActiveButton(name) {
viewButtons.forEach((button) => {
button.classList.toggle("active", button.dataset.view === name);
});
}
function selectView(name) {
if (name === "overview") {
desired.theta = 0.76;
desired.phi = 1.02;
desired.radius = 45;
desired.target.set(-3, 1.7, 0);
} else if (name === "windward") {
desired.theta = 1.43;
desired.phi = 1.05;
desired.radius = 24;
desired.target.set(2.8, 2.1, 0);
} else if (name === "crest") {
desired.theta = 0.03;
desired.phi = 1.16;
desired.radius = 24;
desired.target.set(0.2, 2.3, 0);
} else if (name === "slip") {
desired.theta = -1.48;
desired.phi = 1.04;
desired.radius = 23;
desired.target.set(-1.5, 2.1, 0);
}
setActiveButton(name);
}
viewButtons.forEach((button) => {
button.addEventListener("click", () => {
selectView(button.dataset.view);
});
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
setActiveButton(null);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== pointerId) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
desired.theta -= deltaX * 0.005;
desired.phi = THREE.MathUtils.clamp(
desired.phi + deltaY * 0.0045,
0.24,
1.48
);
previousX = event.clientX;
previousY = event.clientY;
});
function stopDragging(event) {
if (event && pointerId !== null && event.pointerId !== pointerId) return;
dragging = false;
renderer.domElement.classList.remove("dragging");
if (pointerId !== null && renderer.domElement.hasPointerCapture(pointerId)) {
renderer.domElement.releasePointerCapture(pointerId);
}
pointerId = null;
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
16,
76
);
setActiveButton(null);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
const views = ["overview", "windward", "crest", "slip"];
const index = Number(event.key) - 1;
if (index >= 0 && index < views.length) {
selectView(views[index]);
}
});
// ---------------------------------------------------------------------
// Projected annotations
// ---------------------------------------------------------------------
const annotationData = [
{
element: document.getElementById("label-windward"),
point: new THREE.Vector3(6.4, 2.35, 0.8),
offsetX: 23,
offsetY: 0
},
{
element: document.getElementById("label-crest"),
point: new THREE.Vector3(1.5, 5.65, 0),
offsetX: 0,
offsetY: -5
},
{
element: document.getElementById("label-slip"),
point: new THREE.Vector3(-1.45, 3.8, 1.1),
offsetX: -25,
offsetY: 3
}
];
const projected = new THREE.Vector3();
const occupied = [];
function updateAnnotations() {
occupied.length = 0;
annotationData.forEach((annotation) => {
projected.copy(annotation.point).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.1 &&
Math.abs(projected.y) < 1.1;
if (!visible) {
annotation.element.style.opacity = "0";
return;
}
let x =
(projected.x * 0.5 + 0.5) * window.innerWidth +
annotation.offsetX;
let y =
(-projected.y * 0.5 + 0.5) * window.innerHeight +
annotation.offsetY;
const width = annotation.element.offsetWidth;
const height = annotation.element.offsetHeight;
for (let attempt = 0; attempt < 5; attempt++) {
const overlaps = occupied.some((rect) => {
return (
x - width / 2 < rect.right &&
x + width / 2 > rect.left &&
y - height < rect.bottom &&
y > rect.top
);
});
if (!overlaps) break;
y -= height + 7;
}
occupied.push({
left: x - width / 2,
right: x + width / 2,
top: y - height,
bottom: y
});
annotation.element.style.left = `${x}px`;
annotation.element.style.top = `${y}px`;
annotation.element.style.opacity = "1";
});
}
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const damping = 1 - Math.exp(-delta * 7);
current.theta += (desired.theta - current.theta) * damping;
current.phi += (desired.phi - current.phi) * damping;
current.radius += (desired.radius - current.radius) * damping;
current.target.lerp(desired.target, damping);
const sinPhi = Math.sin(current.phi);
const cosPhi = Math.cos(current.phi);
cameraTarget.set(
current.target.x +
current.radius * sinPhi * Math.sin(current.theta),
current.target.y + current.radius * cosPhi,
current.target.z +
current.radius * sinPhi * Math.cos(current.theta)
);
camera.position.copy(cameraTarget);
camera.lookAt(current.target);
camera.updateMatrixWorld();
// Subtle life in the water without changing the solid-landform style.
waveDashes.forEach((dash, index) => {
dash.position.x += Math.sin(clock.elapsedTime * 0.28 + index) * 0.0008;
});
windGroup.position.y =
7.2 + Math.sin(clock.elapsedTime * 0.8) * 0.08;
updateAnnotations();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```