Prompt specification
Build or customize the Three.js 3D scene "Fog approaching a Namib coastal dune" 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: Fog approaching a Namib coastal dune
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/fog-approaching-a-namib-coastal-dune-x3094
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts the Namib Desert's coastal dunes. Create a compact landscape vignette with cool coastal fog layer meeting a dune face. Show the atmospheric layer separately from sand movement and ground vegetation. A 3D cutaway view.
Working Three.js Starter (fog-approaching-a-namib-coastal-dune-x3094):
```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 Cutaway</title>
<style>
:root {
--fog: #b8dce2;
--sand: #f2bd6d;
--flora: #70866a;
--ink: #edf4ef;
--muted: #9eb3b2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
}
body {
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
background:
radial-gradient(circle at 30% 30%, rgba(38, 72, 81, 0.72), transparent 43%),
radial-gradient(circle at 72% 68%, rgba(17, 45, 53, 0.85), transparent 48%),
linear-gradient(155deg, #102b35 0%, #071820 52%, #101b20 100%);
}
body::after {
content: "";
position: fixed;
inset: 0;
z-index: 1;
pointer-events: none;
background: radial-gradient(
ellipse at 50% 48%,
transparent 42%,
rgba(2, 8, 11, 0.18) 76%,
rgba(2, 7, 10, 0.58) 100%
);
}
#scene {
position: fixed;
inset: 0;
z-index: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hero {
position: fixed;
top: clamp(22px, 4vw, 48px);
left: clamp(22px, 4vw, 52px);
z-index: 3;
width: min(460px, calc(100vw - 44px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: #b6cbc8;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.24em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 1px;
background: #9ab5b4;
}
h1 {
margin: 0;
max-width: 440px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(38px, 5.8vw, 72px);
font-weight: 400;
line-height: 0.91;
letter-spacing: -0.045em;
text-wrap: balance;
}
h1 em {
display: block;
color: #d8b57b;
font-weight: 400;
}
.intro {
max-width: 365px;
margin: 20px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.65;
}
.layers {
position: fixed;
top: clamp(20px, 3vw, 34px);
right: clamp(20px, 3vw, 34px);
z-index: 4;
width: 228px;
padding: 17px 18px 15px;
border: 1px solid rgba(182, 211, 211, 0.16);
border-radius: 15px;
background: rgba(6, 19, 24, 0.67);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(16px);
}
.panel-title {
margin-bottom: 13px;
color: #d6e2df;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.layer-row {
display: grid;
grid-template-columns: 16px 10px 1fr;
align-items: center;
gap: 9px;
min-height: 35px;
color: #c6d4d1;
font-size: 11px;
cursor: pointer;
user-select: none;
}
.layer-row input {
width: 14px;
height: 14px;
margin: 0;
accent-color: #d7c18c;
cursor: pointer;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.swatch.fog {
color: var(--fog);
background: currentColor;
}
.swatch.sand {
color: var(--sand);
background: currentColor;
}
.swatch.flora {
color: var(--flora);
background: currentColor;
}
.layer-sub {
display: block;
margin-top: 2px;
color: #708989;
font-size: 9px;
}
.panel-note {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid rgba(183, 210, 210, 0.1);
color: #718989;
font-size: 9px;
line-height: 1.5;
}
.world-label {
position: fixed;
z-index: 3;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 8px;
border: 1px solid rgba(217, 235, 232, 0.15);
border-radius: 5px;
color: #dce8e5;
background: rgba(5, 16, 20, 0.63);
box-shadow: 0 7px 22px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(8px);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.15em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -100%);
}
.world-label::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 21px;
background: linear-gradient(rgba(221, 237, 233, 0.55), transparent);
}
.world-label i {
width: 6px;
height: 6px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
#label-fog i {
color: var(--fog);
background: currentColor;
}
#label-sand i {
color: var(--sand);
background: currentColor;
}
#label-flora i {
color: var(--flora);
background: currentColor;
}
.help {
position: fixed;
left: 50%;
bottom: 20px;
z-index: 3;
padding: 8px 12px;
border: 1px solid rgba(190, 213, 211, 0.12);
border-radius: 999px;
color: #8da3a2;
background: rgba(5, 16, 20, 0.54);
backdrop-filter: blur(10px);
font-size: 9px;
letter-spacing: 0.08em;
white-space: nowrap;
pointer-events: none;
transform: translateX(-50%);
}
.orientation {
position: fixed;
right: clamp(20px, 3vw, 34px);
bottom: 24px;
z-index: 3;
color: #718789;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.17em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 760px) {
.intro {
display: none;
}
.hero {
top: 20px;
}
.layers {
top: auto;
right: 14px;
bottom: 58px;
width: 194px;
padding: 13px 14px;
}
.panel-note {
display: none;
}
.help {
bottom: 14px;
font-size: 8px;
}
.orientation {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive three-dimensional cutaway of the Namib coastal dunes"></main>
<header class="hero">
<div class="eyebrow">23.5° S · Atlantic Edge</div>
<h1>Where cold air<em>meets warm sand.</em></h1>
<p class="intro">
A compact cutaway of the Namib's coastal dunes: Atlantic fog advances over
the beach, wind lifts grains from the crest, and sparse vegetation holds
close to the ground.
</p>
</header>
<aside class="layers" aria-label="Scene layer controls">
<div class="panel-title">Independent scene layers</div>
<label class="layer-row">
<input type="checkbox" checked data-layer="atmosphere">
<span class="swatch fog"></span>
<span>
Coastal atmosphere
<span class="layer-sub">Cool fog / advection</span>
</span>
</label>
<label class="layer-row">
<input type="checkbox" checked data-layer="sand">
<span class="swatch sand"></span>
<span>
Sand movement
<span class="layer-sub">Crest transport / saltation</span>
</span>
</label>
<label class="layer-row">
<input type="checkbox" checked data-layer="vegetation">
<span class="swatch flora"></span>
<span>
Ground vegetation
<span class="layer-sub">Rooted / dune-foot flora</span>
</span>
</label>
<div class="panel-note">
Toggle a layer to inspect its behavior independently. The cutaway retains
the ocean, terrain, and geological section.
</div>
</aside>
<div id="label-fog" class="world-label">
<i></i><span>COASTAL FOG</span>
</div>
<div id="label-sand" class="world-label">
<i></i><span>SAND IN FLIGHT</span>
</div>
<div id="label-flora" class="world-label">
<i></i><span>ROOTED VEGETATION</span>
</div>
<div class="help">DRAG TO ORBIT · SHIFT/RIGHT-DRAG TO PAN · SCROLL TO ZOOM · DOUBLE-CLICK TO RESET</div>
<div class="orientation">Atlantic coast ← → Inland</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 camera
// ------------------------------------------------------------
const container = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: 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;
renderer.domElement.setAttribute(
"aria-label",
"Orbitable cutaway of coastal dunes, fog, moving sand, and vegetation"
);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
0.1,
120
);
const currentView = {
radius: 24,
phi: 1.02,
theta: -0.7
};
const desiredView = { ...currentView };
const target = new THREE.Vector3(0, 0.35, 0);
const desiredTarget = target.clone();
camera.position.setFromSpherical(currentView).add(target);
camera.lookAt(target);
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0x9bcbd6, 0x4a3022, 1.65);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffd39a, 3.2);
sun.position.set(-10, 14, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 38;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0x8cc9da, 1.25);
coolFill.position.set(8, 6, -10);
scene.add(coolFill);
const rimLight = new THREE.DirectionalLight(0xffc77d, 0.55);
rimLight.position.set(-4, 3, -11);
scene.add(rimLight);
// ------------------------------------------------------------
// Shared helpers
// ------------------------------------------------------------
const clamp = THREE.MathUtils.clamp;
function smoothstep(a, b, value) {
const t = clamp((value - a) / (b - a), 0, 1);
return t * t * (3 - 2 * t);
}
function mulberry32(seed) {
return function random() {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const WIDTH = 18;
const DEPTH = 11.5;
const BOTTOM = -2.8;
function crestAt(z) {
return 1.65 + 0.55 * Math.sin(z * 0.38) + 0.18 * Math.sin(z * 0.83);
}
function heightAt(x, z) {
const crest = crestAt(z);
const shoulder = crest - 2.65;
const ridgeHeight =
3.12 + 0.16 * Math.sin(z * 0.63) + 0.08 * Math.sin(z * 1.7);
const grain =
0.035 * Math.sin(x * 3.1 + z * 2.7) +
0.018 * Math.sin(x * 7.2 - z * 4.3);
// Submerged seabed and beach.
if (x < -5) {
return -1.25 + grain * 0.2;
}
if (x < -3) {
const beach = smoothstep(-5, -3, x);
return THREE.MathUtils.lerp(-1.25, 0.05, beach) + grain * 0.3;
}
// Steep ocean-facing dune face.
if (x <= shoulder) {
return 0.05 + grain * 0.35;
}
if (x <= crest) {
const face = smoothstep(shoulder, crest, x);
return 0.05 + ridgeHeight * face + grain * face;
}
// Long, descending inland slope.
const inland = clamp((x - crest) / (WIDTH / 2 - crest), 0, 1);
return 0.05 + ridgeHeight * Math.pow(1 - inland, 1.45) + grain;
}
// ------------------------------------------------------------
// Terrain surface
// ------------------------------------------------------------
const terrainGroup = new THREE.Group();
scene.add(terrainGroup);
const segmentsX = 100;
const segmentsZ = 68;
const terrainPositions = [];
const terrainColors = [];
const terrainIndices = [];
const seabedColor = new THREE.Color(0x596a68);
const wetSandColor = new THREE.Color(0xa9956f);
const duneBaseColor = new THREE.Color(0xc98243);
const duneHighColor = new THREE.Color(0xefc87d);
const color = new THREE.Color();
for (let iz = 0; iz <= segmentsZ; iz++) {
const z = -DEPTH / 2 + (iz / segmentsZ) * DEPTH;
for (let ix = 0; ix <= segmentsX; ix++) {
const x = -WIDTH / 2 + (ix / segmentsX) * WIDTH;
const y = heightAt(x, z);
terrainPositions.push(x, y, z);
if (x < -4.8) {
color.copy(seabedColor).lerp(wetSandColor, smoothstep(-5.2, -3.5, x));
} else {
const heightMix = clamp((y - 0.05) / 3.15, 0, 1);
color.copy(duneBaseColor).lerp(duneHighColor, heightMix);
const moist = 1 - smoothstep(-3.6, -1.9, x);
color.lerp(wetSandColor, moist * 0.28);
}
const variation =
Math.sin(x * 4.7 + z * 2.1) * 0.012 +
Math.sin(z * 8.2 - x * 2.4) * 0.006;
color.offsetHSL(0, 0, variation);
terrainColors.push(color.r, color.g, color.b);
}
}
for (let iz = 0; iz < segmentsZ; iz++) {
for (let ix = 0; ix < segmentsX; ix++) {
const a = iz * (segmentsX + 1) + ix;
const b = a + 1;
const c = a + segmentsX + 1;
const d = c + 1;
terrainIndices.push(a, c, b, b, c, d);
}
}
const terrainGeometry = new THREE.BufferGeometry();
terrainGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(terrainPositions, 3)
);
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.setIndex(terrainIndices);
terrainGeometry.computeVertexNormals();
const terrainMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.96,
metalness: 0
});
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.castShadow = true;
terrain.receiveShadow = true;
terrainGroup.add(terrain);
// ------------------------------------------------------------
// Geological cutaway around the perimeter
// ------------------------------------------------------------
const perimeter = [];
for (let i = 0; i <= 72; i++) {
perimeter.push({
x: -WIDTH / 2 + (i / 72) * WIDTH,
z: DEPTH / 2
});
}
for (let i = 1; i <= 56; i++) {
perimeter.push({
x: WIDTH / 2,
z: DEPTH / 2 - (i / 56) * DEPTH
});
}
for (let i = 1; i <= 72; i++) {
perimeter.push({
x: WIDTH / 2 - (i / 72) * WIDTH,
z: -DEPTH / 2
});
}
for (let i = 1; i <= 56; i++) {
perimeter.push({
x: -WIDTH / 2,
z: -DEPTH / 2 + (i / 56) * DEPTH
});
}
const strataFractions = [0, 0.22, 0.48, 0.72, 1];
const strataColors = [
new THREE.Color(0x533a31),
new THREE.Color(0x875038),
new THREE.Color(0xb86d3e),
new THREE.Color(0xd79a58),
new THREE.Color(0xe7b66e)
];
const sidePositions = [];
const sideColors = [];
const sideIndices = [];
const boundaryPositions = [];
const rimPositions = [];
for (let i = 0; i < perimeter.length; i++) {
const p1 = perimeter[i];
const p2 = perimeter[(i + 1) % perimeter.length];
const h1 = heightAt(p1.x, p1.z);
const h2 = heightAt(p2.x, p2.z);
rimPositions.push(p1.x, h1 + 0.025, p1.z);
rimPositions.push(p2.x, h2 + 0.025, p2.z);
for (let band = 0; band < strataFractions.length - 1; band++) {
const base = sidePositions.length / 3;
const low1 = BOTTOM + (h1 - BOTTOM) * strataFractions[band];
const low2 = BOTTOM + (h2 - BOTTOM) * strataFractions[band];
const high1 = BOTTOM + (h1 - BOTTOM) * strataFractions[band + 1];
const high2 = BOTTOM + (h2 - BOTTOM) * strataFractions[band + 1];
sidePositions.push(
p1.x, low1, p1.z,
p2.x, low2, p2.z,
p2.x, high2, p2.z,
p1.x, high1, p1.z
);
const c = strataColors[band];
for (let v = 0; v < 4; v++) {
sideColors.push(c.r, c.g, c.b);
}
sideIndices.push(
base, base + 1, base + 2,
base, base + 2, base + 3
);
}
for (let boundary = 0; boundary < strataFractions.length - 1; boundary++) {
const fraction = strataFractions[boundary];
const y1 = BOTTOM + (h1 - BOTTOM) * fraction;
const y2 = BOTTOM + (h2 - BOTTOM) * fraction;
boundaryPositions.push(
p1.x, y1, p1.z,
p2.x, y2, p2.z
);
}
}
const sideGeometry = new THREE.BufferGeometry();
sideGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(sidePositions, 3)
);
sideGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(sideColors, 3)
);
sideGeometry.setIndex(sideIndices);
sideGeometry.computeVertexNormals();
const sideMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
side: THREE.DoubleSide
});
const sides = new THREE.Mesh(sideGeometry, sideMaterial);
sides.castShadow = true;
sides.receiveShadow = true;
terrainGroup.add(sides);
const boundaryGeometry = new THREE.BufferGeometry();
boundaryGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(boundaryPositions, 3)
);
const boundaries = new THREE.LineSegments(
boundaryGeometry,
new THREE.LineBasicMaterial({
color: 0x5c392d,
transparent: true,
opacity: 0.55
})
);
terrainGroup.add(boundaries);
const rimGeometry = new THREE.BufferGeometry();
rimGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(rimPositions, 3)
);
const rim = new THREE.Line(
rimGeometry,
new THREE.LineBasicMaterial({
color: 0xf2ca87,
transparent: true,
opacity: 0.62
})
);
terrainGroup.add(rim);
// Dark plinth beneath the geological section.
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(WIDTH + 0.2, 0.36, DEPTH + 0.2),
new THREE.MeshStandardMaterial({
color: 0x132a30,
roughness: 0.82,
metalness: 0.08
})
);
plinth.position.y = BOTTOM - 0.17;
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
const plinthEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(plinth.geometry),
new THREE.LineBasicMaterial({
color: 0x698083,
transparent: true,
opacity: 0.36
})
);
plinthEdges.position.copy(plinth.position);
scene.add(plinthEdges);
// Soft contact shadow beneath the floating cutaway.
const shadowPlane = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.ShadowMaterial({
color: 0x02090c,
opacity: 0.38
})
);
shadowPlane.rotation.x = -Math.PI / 2;
shadowPlane.position.y = BOTTOM - 0.37;
shadowPlane.receiveShadow = true;
scene.add(shadowPlane);
// A restrained crest accent.
const crestPoints = [];
for (let i = 0; i <= 90; i++) {
const z = -DEPTH / 2 + (i / 90) * DEPTH;
const x = crestAt(z);
crestPoints.push(new THREE.Vector3(x, heightAt(x, z) + 0.035, z));
}
const crestLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(crestPoints),
new THREE.LineBasicMaterial({
color: 0xffd28a,
transparent: true,
opacity: 0.25
})
);
terrainGroup.add(crestLine);
// ------------------------------------------------------------
// Atlantic water
// ------------------------------------------------------------
const oceanGeometry = new THREE.PlaneGeometry(5.4, DEPTH - 0.08, 30, 72);
oceanGeometry.rotateX(-Math.PI / 2);
oceanGeometry.attributes.position.setUsage(THREE.DynamicDrawUsage);
const oceanBase = Float32Array.from(oceanGeometry.attributes.position.array);
const oceanMaterial = new THREE.MeshPhysicalMaterial({
color: 0x2f7180,
roughness: 0.3,
metalness: 0.13,
clearcoat: 0.72,
clearcoatRoughness: 0.28,
transparent: true,
opacity: 0.94
});
const ocean = new THREE.Mesh(oceanGeometry, oceanMaterial);
ocean.position.set(-6.25, -0.22, 0);
ocean.receiveShadow = true;
scene.add(ocean);
const foamPositions = [];
for (let i = 0; i <= 80; i++) {
const z = -DEPTH / 2 + (i / 80) * DEPTH;
foamPositions.push(-3.55, -0.185, z);
}
const foamGeometry = new THREE.BufferGeometry();
foamGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(foamPositions, 3)
);
const foam = new THREE.Line(
foamGeometry,
new THREE.LineBasicMaterial({
color: 0xc4e4e4,
transparent: true,
opacity: 0.54
})
);
scene.add(foam);
// ------------------------------------------------------------
// Layer 1: atmospheric fog
// ------------------------------------------------------------
const atmosphereGroup = new THREE.Group();
scene.add(atmosphereGroup);
const fogSheetMaterial = new THREE.ShaderMaterial({
uniforms: {
uTime: { value: 0 },
uColor: { value: new THREE.Color(0xb9dce2) }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float uTime;
uniform vec3 uColor;
varying vec2 vUv;
float hash(vec2 p) {
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
}
float noise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(
mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x),
mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x),
u.y
);
}
void main() {
vec2 drift = vec2(uTime * 0.035, uTime * 0.012);
float broad = noise(vUv * vec2(6.0, 8.0) + drift);
float detail = noise(vUv * vec2(15.0, 18.0) - drift * 1.7);
float density = broad * 0.72 + detail * 0.28;
float edgeX = smoothstep(0.0, 0.12, vUv.x) *
(1.0 - smoothstep(0.79, 1.0, vUv.x));
float edgeZ = smoothstep(0.0, 0.11, vUv.y) *
(1.0 - smoothstep(0.88, 1.0, vUv.y));
float alpha = (0.028 + density * 0.075) * edgeX * edgeZ;
gl_FragColor = vec4(uColor, alpha);
}
`,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const fogSheet = new THREE.Mesh(
new THREE.PlaneGeometry(10.3, DEPTH, 1, 1),
fogSheetMaterial
);
fogSheet.rotation.x = -Math.PI / 2;
fogSheet.position.set(-4.35, 0.34, 0);
fogSheet.renderOrder = 3;
atmosphereGroup.add(fogSheet);
function makeFogTexture() {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
const gradient = context.createRadialGradient(64, 64, 2, 64, 64, 64);
gradient.addColorStop(0.0, "rgba(255,255,255,0.92)");
gradient.addColorStop(0.30, "rgba(255,255,255,0.48)");
gradient.addColorStop(0.68, "rgba(255,255,255,0.12)");
gradient.addColorStop(1.0, "rgba(255,255,255,0)");
context.fillStyle = gradient;
context.fillRect(0, 0, 128, 128);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const fogTexture = makeFogTexture();
const fogRandom = mulberry32(91);
const fogSprites = [];
for (let i = 0; i < 34; i++) {
const contact = i > 24;
const y = contact
? 0.35 + fogRandom() * 1.8
: 0.15 + fogRandom() * 0.72;
const x = contact
? -0.1 + y * 0.18 + fogRandom() * 0.7
: -7.8 + fogRandom() * 8.1;
const z = -5.2 + fogRandom() * 10.4;
const material = new THREE.SpriteMaterial({
map: fogTexture,
color: 0xb8dbe1,
transparent: true,
opacity: 0.055 + fogRandom() * 0.105,
depthWrite: false,
depthTest: true
});
const sprite = new THREE.Sprite(material);
sprite.position.set(x, y, z);
const width = contact
? 1.8 + fogRandom() * 1.6
: 2.1 + fogRandom() * 2.6;
const height = contact
? 0.8 + fogRandom() * 1.25
: 0.42 + fogRandom() * 0.82;
sprite.scale.set(width, height, 1);
sprite.renderOrder = 4;
atmosphereGroup.add(sprite);
fogSprites.push({
sprite,
baseX: x,
baseY: y,
phase: fogRandom() * Math.PI * 2
});
}
// ------------------------------------------------------------
// Layer 2: wind-driven sand
// ------------------------------------------------------------
const sandGroup = new THREE.Group();
scene.add(sandGroup);
const particleCount = 150;
const particlePositions = new Float32Array(particleCount * 3);
const particleSizes = new Float32Array(particleCount);
const particleData = [];
const sandRandom = mulberry32(317);
for (let i = 0; i < particleCount; i++) {
particleData.push({
z: -5.2 + sandRandom() * 10.4,
phase: sandRandom(),
speed: 0.65 + sandRandom() * 0.8,
lift: 0.12 + sandRandom() * 0.35,
drift: sandRandom() * 2 - 1
});
particleSizes[i] = 0.75 + sandRandom() * 1.15;
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3).setUsage(THREE.DynamicDrawUsage)
);
particleGeometry.setAttribute(
"aSize",
new THREE.BufferAttribute(particleSizes, 1)
);
const particleMaterial = new THREE.ShaderMaterial({
uniforms: {
uPixelRatio: { value: Math.min(window.devicePixelRatio, 2) },
uColor: { value: new THREE.Color(0xffd58a) }
},
vertexShader: `
uniform float uPixelRatio;
attribute float aSize;
varying float vAlpha;
void main() {
vec4 viewPosition = modelViewMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * viewPosition;
gl_PointSize = clamp(
aSize * uPixelRatio * (34.0 / max(1.0, -viewPosition.z)),
1.0,
7.0
);
vAlpha = 0.72;
}
`,
fragmentShader: `
uniform vec3 uColor;
varying float vAlpha;
void main() {
vec2 p = gl_PointCoord - 0.5;
float d = length(p);
float alpha = smoothstep(0.5, 0.12, d) * vAlpha;
if (alpha < 0.01) discard;
gl_FragColor = vec4(uColor, alpha);
}
`,
transparent: true,
depthWrite: false
});
const sandParticles = new THREE.Points(particleGeometry, particleMaterial);
sandParticles.frustumCulled = false;
sandParticles.renderOrder = 5;
sandGroup.add(sandParticles);
// Small wind-aligned streaks make the transport direction legible.
const dashCount = 30;
const dashPositions = new Float32Array(dashCount * 6);
const dashData = [];
for (let i = 0; i < dashCount; i++) {
dashData.push({
z: -5 + sandRandom() * 10,
phase: sandRandom(),
speed: 0.55 + sandRandom() * 0.8,
length: 0.11 + sandRandom() * 0.25
});
}
const dashGeometry = new THREE.BufferGeometry();
dashGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dashPositions, 3).setUsage(THREE.DynamicDrawUsage)
);
const dashes = new THREE.LineSegments(
dashGeometry,
new THREE.LineBasicMaterial({
color: 0xffcf7d,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
dashes.frustumCulled = false;
dashes.renderOrder = 5;
sandGroup.add(dashes);
// ------------------------------------------------------------
// Layer 3: rooted ground vegetation
// ------------------------------------------------------------
const vegetationGroup = new THREE.Group();
scene.add(vegetationGroup);
const grassMaterials = [
new THREE.MeshStandardMaterial({
color: 0x526849,
roughness: 0.98,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x746c45,
roughness: 1,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x3f5b47,
roughness: 1,
side: THREE.DoubleSide
})
];
function createTuftGeometry(seed) {
const random = mulberry32(seed);
const positions = [];
const indices = [];
const bladeCount = 8;
for (let blade = 0; blade < bladeCount; blade++) {
const angle = (blade / bladeCount) * Math.PI * 2 + random() * 0.45;
const height = 0.25 + random() * 0.42;
const lean = 0.11 + random() * 0.2;
const width = 0.025 + random() * 0.026;
const originX = (random() - 0.5) * 0.12;
const originZ = (random() - 0.5) * 0.12;
const segments = 4;
const base = positions.length / 3;
for (let segment = 0; segment <= segments; segment++) {
const t = segment / segments;
const centerX = originX + Math.cos(angle) * lean * t * t;
const centerZ = originZ + Math.sin(angle) * lean * t * t;
const y = height * t - 0.055 * t * t;
const halfWidth = width * (1 - t * 0.88);
positions.push(
centerX - Math.sin(angle) * halfWidth,
y,
centerZ + Math.cos(angle) * halfWidth,
centerX + Math.sin(angle) * halfWidth,
y,
centerZ - Math.cos(angle) * halfWidth
);
}
for (let segment = 0; segment < segments; segment++) {
const a = base + segment * 2;
indices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const plantPositions = [
[-2.75, 4.55, 1.1],
[-2.25, 3.55, 0.8],
[-2.85, 2.65, 0.75],
[-3.0, -2.55, 0.68],
[7.15, 3.55, 0.95],
[7.85, 4.15, 0.72],
[8.15, 3.05, 0.85],
[7.4, 2.25, 0.68]
];
const swayingPlants = [];
plantPositions.forEach((position, index) => {
const group = new THREE.Group();
const [x, z, scale] = position;
group.position.set(x, heightAt(x, z) + 0.015, z);
group.scale.setScalar(scale);
group.rotation.y = index * 0.71;
const tuft = new THREE.Mesh(
createTuftGeometry(500 + index * 17),
grassMaterials[index % grassMaterials.length]
);
tuft.castShadow = true;
tuft.receiveShadow = true;
group.add(tuft);
vegetationGroup.add(group);
swayingPlants.push({
group,
phase: index * 0.83,
amount: 0.012 + (index % 3) * 0.007
});
});
function createWelwitschia(x, z, seed) {
const random = mulberry32(seed);
const baseY = heightAt(x, z);
const group = new THREE.Group();
group.position.set(x, baseY + 0.02, z);
const material = new THREE.MeshStandardMaterial({
color: 0x53684f,
roughness: 0.9,
side: THREE.DoubleSide
});
for (let leaf = 0; leaf < 7; leaf++) {
const angle = (leaf / 7) * Math.PI * 2 + random() * 0.5;
const length = 0.65 + random() * 0.65;
const width = 0.065 + random() * 0.04;
const segments = 6;
const positions = [];
const indices = [];
for (let segment = 0; segment <= segments; segment++) {
const t = segment / segments;
const dx = Math.cos(angle) * length * t;
const dz = Math.sin(angle) * length * t;
const worldY = heightAt(x + dx, z + dz);
const y = worldY - baseY + 0.035 + Math.sin(t * Math.PI) * 0.045;
const halfWidth = 0.008 + Math.pow(Math.sin(t * Math.PI), 0.65) * width;
positions.push(
dx - Math.sin(angle) * halfWidth,
y,
dz + Math.cos(angle) * halfWidth,
dx + Math.sin(angle) * halfWidth,
y,
dz - Math.cos(angle) * halfWidth
);
}
for (let segment = 0; segment < segments; segment++) {
const a = segment * 2;
indices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
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.castShadow = true;
group.add(mesh);
}
const crown = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 10, 7),
new THREE.MeshStandardMaterial({
color: 0x6d7652,
roughness: 0.95
})
);
crown.scale.set(1.25, 0.55, 1.1);
crown.position.y = 0.04;
crown.castShadow = true;
group.add(crown);
vegetationGroup.add(group);
swayingPlants.push({
group,
phase: random() * Math.PI * 2,
amount: 0.008
});
}
createWelwitschia(-2.15, 4.25, 782);
createWelwitschia(7.75, 3.7, 941);
// ------------------------------------------------------------
// Layer visibility controls
// ------------------------------------------------------------
const layerGroups = {
atmosphere: atmosphereGroup,
sand: sandGroup,
vegetation: vegetationGroup
};
document.querySelectorAll("[data-layer]").forEach((input) => {
input.addEventListener("change", () => {
layerGroups[input.dataset.layer].visible = input.checked;
});
});
// ------------------------------------------------------------
// Custom camera controls — no addon imports
// ------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let pointerId = null;
let lastX = 0;
let lastY = 0;
let panning = false;
function resetCamera() {
desiredView.radius = 24;
desiredView.phi = 1.02;
desiredView.theta = -0.7;
desiredTarget.set(0, 0.35, 0);
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerId = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
panning = event.button === 2 || event.shiftKey;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== pointerId) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (panning) {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0)
.setY(0)
.normalize();
const forward = new THREE.Vector3(
-Math.sin(desiredView.theta),
0,
-Math.cos(desiredView.theta)
);
const scale = desiredView.radius * 0.0012;
desiredTarget.addScaledVector(right, -dx * scale);
desiredTarget.addScaledVector(forward, dy * scale);
desiredTarget.x = clamp(desiredTarget.x, -8, 8);
desiredTarget.y = clamp(desiredTarget.y, -1.5, 5);
desiredTarget.z = clamp(desiredTarget.z, -7, 7);
} else {
desiredView.theta -= dx * 0.006;
desiredView.phi = clamp(desiredView.phi - dy * 0.006, 0.32, 1.43);
}
});
function endDrag(event) {
if (event.pointerId !== pointerId) return;
dragging = false;
pointerId = null;
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredView.radius = clamp(
desiredView.radius * Math.exp(event.deltaY * 0.001),
13,
38
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// World-space annotations
// ------------------------------------------------------------
const annotations = [
{
element: document.querySelector("#label-fog"),
position: new THREE.Vector3(-4.8, 1.05, -0.7),
group: atmosphereGroup
},
{
element: document.querySelector("#label-sand"),
position: new THREE.Vector3(
crestAt(0.4),
heightAt(crestAt(0.4), 0.4) + 0.55,
0.4
),
group: sandGroup
},
{
element: document.querySelector("#label-flora"),
position: new THREE.Vector3(-2.2, 0.9, 4.25),
group: vegetationGroup
}
];
const projected = new THREE.Vector3();
function updateAnnotations() {
const width = renderer.domElement.clientWidth;
const height = renderer.domElement.clientHeight;
annotations.forEach((annotation) => {
projected.copy(annotation.position).project(camera);
const visible =
annotation.group.visible &&
projected.z > -1 &&
projected.z < 1;
annotation.element.hidden = !visible;
if (!visible) return;
annotation.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
annotation.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height - 18}px`;
});
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
let frame = 0;
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
frame++;
// Damped orbit camera.
currentView.radius +=
(desiredView.radius - currentView.radius) * 0.085;
currentView.phi +=
(desiredView.phi - currentView.phi) * 0.085;
currentView.theta +=
(desiredView.theta - currentView.theta) * 0.085;
target.lerp(desiredTarget, 0.085);
camera.position.setFromSpherical(currentView).add(target);
camera.lookAt(target);
// Small ocean swell.
const oceanPositions = oceanGeometry.attributes.position.array;
for (let i = 0; i < oceanPositions.length; i += 3) {
const x = oceanBase[i];
const z = oceanBase[i + 2];
oceanPositions[i + 1] =
0.026 * Math.sin(x * 2.7 + elapsed * 0.82 + z * 0.31) +
0.012 * Math.sin(z * 2.15 - elapsed * 0.61);
}
oceanGeometry.attributes.position.needsUpdate = true;
if (frame % 3 === 0) {
oceanGeometry.computeVertexNormals();
}
foam.material.opacity = 0.46 + Math.sin(elapsed * 0.85) * 0.08;
// Fog sheet and soft fog bank drift.
fogSheetMaterial.uniforms.uTime.value = elapsed;
fogSprites.forEach((fog) => {
fog.sprite.position.x =
fog.baseX + Math.sin(elapsed * 0.13 + fog.phase) * 0.26;
fog.sprite.position.y =
fog.baseY + Math.sin(elapsed * 0.22 + fog.phase) * 0.055;
});
// Saltating particles travel inland from the dune crest.
for (let i = 0; i < particleCount; i++) {
const data = particleData[i];
const progress = (data.phase + elapsed * 0.105 * data.speed) % 1;
const x = crestAt(data.z) + 0.08 + progress * 4.4;
const z = data.z + data.drift * 0.16;
const y =
heightAt(x, z) +
0.1 +
Math.sin(progress * Math.PI) * data.lift;
particlePositions[i * 3] = x;
particlePositions[i * 3 + 1] = y;
particlePositions[i * 3 + 2] = z;
}
particleGeometry.attributes.position.needsUpdate = true;
// Low, wind-aligned sand streaks.
for (let i = 0; i < dashCount; i++) {
const data = dashData[i];
const progress = (data.phase + elapsed * 0.16 * data.speed) % 1;
const x = crestAt(data.z) + 0.12 + progress * 4.35;
const z = data.z + Math.sin(progress * 8 + data.phase * 9) * 0.08;
const y =
heightAt(x, z) +
0.09 +
Math.sin(progress * Math.PI) * 0.14;
const offset = i * 6;
dashPositions[offset] = x - data.length;
dashPositions[offset + 1] = y;
dashPositions[offset + 2] = z;
dashPositions[offset + 3] = x + data.length;
dashPositions[offset + 4] = y + 0.012;
dashPositions[offset + 5] = z;
}
dashGeometry.attributes.position.needsUpdate = true;
// Subtle vegetation sway.
swayingPlants.forEach((plant) => {
plant.group.rotation.z =
Math.sin(elapsed * 0.75 + plant.phase) * plant.amount;
plant.group.rotation.x =
Math.cos(elapsed * 0.61 + plant.phase) * plant.amount * 0.55;
});
updateAnnotations();
renderer.render(scene, camera);
}
animate();
// ------------------------------------------------------------
// Resize handling
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
particleMaterial.uniforms.uPixelRatio.value =
Math.min(window.devicePixelRatio, 2);
});
</script>
</body>
</html>
```