Prompt specification
Build or customize the Three.js 3D scene "Atacama coast fog-oasis landscape slice" 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: Atacama coast fog-oasis landscape slice
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/atacama-coast-fog-oasis-landscape-slice-x3051
Scene Description Prompt:
Create a low-poly Atacama coastal fog oasis, placing vegetation on exposed slopes between ocean and arid interior. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (atacama-coast-fog-oasis-landscape-slice-x3051):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Atacama Coastal Fog Oasis</title>
<style>
:root {
color-scheme: dark;
--ink: #f5eee0;
--muted: rgba(245, 238, 224, .68);
--line: rgba(245, 238, 224, .2);
--glass: rgba(20, 28, 28, .36);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b8aa96;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(10, 18, 20, .2), transparent 30%),
radial-gradient(circle at 50% 45%, transparent 43%, rgba(12, 17, 18, .25));
z-index: 2;
}
#panel {
position: fixed;
z-index: 4;
top: 28px;
left: 30px;
width: min(390px, calc(100vw - 60px));
color: var(--ink);
pointer-events: none;
text-shadow: 0 1px 20px rgba(0, 0, 0, .3);
}
.eyebrow {
margin-bottom: 10px;
font-size: 10px;
font-weight: 700;
letter-spacing: .24em;
text-transform: uppercase;
color: rgba(255, 248, 232, .78);
}
h1 {
margin: 0;
max-width: 360px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 5vw, 58px);
font-weight: 400;
line-height: .96;
letter-spacing: -.035em;
}
#panel p {
margin: 15px 0 0;
max-width: 340px;
color: var(--muted);
font-size: 12px;
line-height: 1.65;
}
#tools {
position: fixed;
z-index: 5;
top: 26px;
right: 28px;
display: flex;
gap: 8px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 999px;
padding: 9px 13px;
color: var(--ink);
background: var(--glass);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
font: inherit;
font-size: 10px;
font-weight: 650;
letter-spacing: .08em;
text-transform: uppercase;
cursor: pointer;
transition: background .2s, border-color .2s;
}
button:hover {
background: rgba(28, 39, 38, .62);
border-color: rgba(245, 238, 224, .45);
}
button[aria-pressed="false"] {
color: rgba(245, 238, 224, .42);
}
#legend {
position: fixed;
z-index: 4;
left: 30px;
bottom: 27px;
display: flex;
flex-wrap: wrap;
gap: 17px;
color: rgba(255, 250, 239, .82);
font-size: 9px;
font-weight: 650;
letter-spacing: .12em;
text-transform: uppercase;
pointer-events: none;
text-shadow: 0 1px 12px rgba(0, 0, 0, .55);
}
.key {
display: inline-flex;
align-items: center;
gap: 7px;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
box-shadow: 0 0 0 2px rgba(255, 255, 255, .08);
}
.ocean-dot {
background: #5e9794;
}
.plant-dot {
background: #829365;
}
.desert-dot {
background: #be9468;
}
#hint {
position: fixed;
z-index: 4;
right: 29px;
bottom: 27px;
color: rgba(255, 250, 239, .64);
font-size: 9px;
font-weight: 650;
letter-spacing: .11em;
text-transform: uppercase;
pointer-events: none;
text-shadow: 0 1px 12px rgba(0, 0, 0, .55);
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: #eee6d7;
background: #27302f;
font-size: 10px;
font-weight: 700;
letter-spacing: .22em;
text-transform: uppercase;
transition: opacity .7s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
#panel {
top: 20px;
left: 20px;
}
h1 {
max-width: 250px;
font-size: 38px;
}
#panel p {
display: none;
}
#tools {
top: auto;
right: 18px;
bottom: 58px;
flex-direction: column;
align-items: flex-end;
}
#legend {
left: 20px;
bottom: 20px;
gap: 10px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="vignette"></div>
<header id="panel">
<div class="eyebrow">Atacama · 24° South · Pacific margin</div>
<h1>Coastal fog oasis</h1>
<p>
Pacific moisture threads up the exposed coastal slopes. Hardy vegetation
clusters around the spring, then gives way to the hyper-arid interior.
</p>
</header>
<nav id="tools" aria-label="Scene controls">
<button id="fog-toggle" type="button" aria-pressed="true">Toggle fog</button>
<button id="reset-view" type="button">Reset view</button>
</nav>
<div id="legend">
<span class="key"><i class="dot ocean-dot"></i>Pacific</span>
<span class="key"><i class="dot plant-dot"></i>Fog-fed scrub</span>
<span class="key"><i class="dot desert-dot"></i>Arid interior</span>
</div>
<div id="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<div id="loading">Forming the coastal fog oasis</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 scene = new THREE.Scene();
scene.background = new THREE.Color(0xc6b89f);
scene.fog = new THREE.FogExp2(0xc8bba3, 0.0085);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
260
);
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
} catch (error) {
document.getElementById("loading").textContent =
"This scene requires a browser with WebGL enabled.";
throw error;
}
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.style.touchAction = "none";
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(
0xc9d8d5,
0x76563c,
1.7
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe0ae, 2.45);
sun.position.set(-34, 42, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -42;
sun.shadow.camera.right = 42;
sun.shadow.camera.top = 33;
sun.shadow.camera.bottom = -33;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 115;
sun.shadow.bias = -0.0007;
sun.shadow.normalBias = 0.035;
scene.add(sun);
scene.add(sun.target);
sun.target.position.set(-3, 0, 0);
const fill = new THREE.DirectionalLight(0x8eb5bd, 0.35);
fill.position.set(24, 12, -24);
scene.add(fill);
// -------------------------------------------------------------------------
// Deterministic helpers and terrain functions
// -------------------------------------------------------------------------
let seed = 197823;
function random() {
seed |= 0;
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
const range = (a, b) => a + (b - a) * random();
const clamp = THREE.MathUtils.clamp;
const UP = new THREE.Vector3(0, 1, 0);
function smoothstep(a, b, x) {
const t = clamp((x - a) / (b - a), 0, 1);
return t * t * (3 - 2 * t);
}
function coastX(z) {
return -12.4
+ 1.15 * Math.sin(z * 0.14)
+ 0.45 * Math.sin(z * 0.37 + 1.1);
}
function terrainHeight(x, z) {
const d = x - coastX(z);
if (d < 0) {
return d * 0.16
+ 0.08 * Math.sin(z * 0.45) * clamp(-d / 5, 0, 1);
}
let h = 0.04 + 2.1 * (1 - Math.exp(-d / 4.8));
h += smoothstep(12, 34, d) * 2.25;
const terrainNoise = smoothstep(0, 2.5, d) * (
0.35 * Math.sin(x * 0.37 + z * 0.16)
+ 0.20 * Math.sin(z * 0.63 - x * 0.11)
+ 0.11 * Math.sin(x * 0.91 + z * 0.42)
);
h += terrainNoise;
h += 1.15 * Math.exp(
-((x - 16) ** 2 / 90 + (z + 10) ** 2 / 45)
);
h += 0.85 * Math.exp(
-((x - 24) ** 2 / 55 + (z - 11) ** 2 / 38)
);
// A shallow, fog-fed hollow containing the small spring.
h -= 1.05 * Math.exp(
-((x + 4) ** 2 / 22 + (z - 0.4) ** 2 / 14)
);
return h;
}
function terrainNormal(x, z) {
const e = 0.18;
const hL = terrainHeight(x - e, z);
const hR = terrainHeight(x + e, z);
const hD = terrainHeight(x, z - e);
const hU = terrainHeight(x, z + e);
return new THREE.Vector3(
hL - hR,
2 * e,
hD - hU
).normalize();
}
function moisture(x, z) {
const d = x - coastX(z);
const coastalBand = Math.exp(-(((d - 5.1) / 4.6) ** 2));
const oasis = Math.exp(
-(((x + 4) ** 2) / 24 + (z - 0.4) ** 2) / 15
);
const fogShear = 0.5 + 0.5 * Math.sin(z * 0.31 + x * 0.12);
return clamp(
coastalBand * (0.55 + 0.25 * fogShear) + oasis * 0.78,
0,
1
);
}
// -------------------------------------------------------------------------
// Procedural sky dome
// -------------------------------------------------------------------------
const sky = new THREE.Mesh(
new THREE.SphereGeometry(130, 32, 18),
new THREE.ShaderMaterial({
side: THREE.BackSide,
depthWrite: false,
fog: false,
toneMapped: false,
uniforms: {
topColor: { value: new THREE.Color(0x72969c) },
horizonColor: { value: new THREE.Color(0xd8c4a6) },
sunColor: { value: new THREE.Color(0xffc981) },
sunDirection: {
value: new THREE.Vector3(-0.65, 0.31, -0.69).normalize()
}
},
vertexShader: `
varying vec3 vDirection;
void main() {
vDirection = position;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform vec3 topColor;
uniform vec3 horizonColor;
uniform vec3 sunColor;
uniform vec3 sunDirection;
varying vec3 vDirection;
void main() {
vec3 direction = normalize(vDirection);
float horizonMix = smoothstep(-0.12, 0.72, direction.y);
vec3 color = mix(horizonColor, topColor, horizonMix);
float sunDot = max(dot(direction, sunDirection), 0.0);
float sunDisc = pow(sunDot, 650.0);
float sunHalo = pow(sunDot, 22.0);
color += sunColor * sunDisc * 1.7;
color += sunColor * sunHalo * 0.17;
gl_FragColor = vec4(color, 1.0);
}
`
})
);
sky.renderOrder = -100;
scene.add(sky);
// -------------------------------------------------------------------------
// Faceted terrain
// -------------------------------------------------------------------------
let terrainGeometry = new THREE.PlaneGeometry(
76,
50,
54,
38
);
terrainGeometry.rotateX(-Math.PI / 2);
terrainGeometry.translate(8, 0, 0);
const terrainPositions = terrainGeometry.attributes.position;
for (let i = 0; i < terrainPositions.count; i++) {
const x = terrainPositions.getX(i);
const z = terrainPositions.getZ(i);
terrainPositions.setY(i, terrainHeight(x, z));
}
terrainPositions.needsUpdate = true;
terrainGeometry = terrainGeometry.toNonIndexed();
const terrainColors = new Float32Array(
terrainGeometry.attributes.position.count * 3
);
const terrainColor = new THREE.Color();
const sand = new THREE.Color(0xb39d78);
const coastalEarth = new THREE.Color(0x8b7655);
const scrub = new THREE.Color(0x6d7953);
const oasisGreen = new THREE.Color(0x536d49);
const desert = new THREE.Color(0xb28b62);
const highDesert = new THREE.Color(0x9b7756);
const faceVertices = terrainGeometry.attributes.position.count / 3;
for (let face = 0; face < faceVertices; face++) {
const index = face * 3;
const x = (
terrainGeometry.attributes.position.getX(index)
+ terrainGeometry.attributes.position.getX(index + 1)
+ terrainGeometry.attributes.position.getX(index + 2)
) / 3;
const z = (
terrainGeometry.attributes.position.getZ(index)
+ terrainGeometry.attributes.position.getZ(index + 1)
+ terrainGeometry.attributes.position.getZ(index + 2)
) / 3;
const y = (
terrainGeometry.attributes.position.getY(index)
+ terrainGeometry.attributes.position.getY(index + 1)
+ terrainGeometry.attributes.position.getY(index + 2)
) / 3;
const d = x - coastX(z);
const wet = moisture(x, z);
if (d < -0.45) {
terrainColor.copy(sand).multiplyScalar(0.65);
} else if (d < 1.25) {
terrainColor.copy(sand);
} else if (d < 15) {
terrainColor.copy(coastalEarth).lerp(scrub, wet * 0.8);
const oasis = Math.exp(
-(((x + 4) ** 2) / 28 + (z - 0.4) ** 2) / 18
);
terrainColor.lerp(oasisGreen, oasis * 0.72);
} else {
terrainColor.copy(desert).lerp(
highDesert,
smoothstep(15, 32, d) * 0.62
);
}
if (y > 3.5) {
terrainColor.lerp(highDesert, 0.18);
}
terrainColor.offsetHSL(
range(-0.018, 0.018),
range(-0.035, 0.035),
range(-0.055, 0.055)
);
for (let vertex = 0; vertex < 3; vertex++) {
const p = index + vertex;
terrainColors[p * 3] = terrainColor.r;
terrainColors[p * 3 + 1] = terrainColor.g;
terrainColors[p * 3 + 2] = terrainColor.b;
}
}
terrainGeometry.setAttribute(
"color",
new THREE.BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrain = new THREE.Mesh(
terrainGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
metalness: 0,
flatShading: true
})
);
terrain.receiveShadow = true;
terrain.castShadow = true;
scene.add(terrain);
// -------------------------------------------------------------------------
// Ocean with animated vertices
// -------------------------------------------------------------------------
const oceanGeometry = new THREE.PlaneGeometry(
180,
110,
72,
44
);
oceanGeometry.rotateX(-Math.PI / 2);
oceanGeometry.translate(-20, 0, 0);
const oceanPositions = oceanGeometry.attributes.position;
const oceanColors = new Float32Array(oceanPositions.count * 3);
const deepOcean = new THREE.Color(0x173f50);
const shallowOcean = new THREE.Color(0x568e89);
const oceanColor = new THREE.Color();
for (let i = 0; i < oceanPositions.count; i++) {
const x = oceanPositions.getX(i);
const z = oceanPositions.getZ(i);
const d = x - coastX(z);
const shoreFactor = clamp((d + 18) / 18, 0, 1);
oceanColor.copy(deepOcean).lerp(shallowOcean, shoreFactor);
oceanColor.offsetHSL(0, 0, range(-0.025, 0.025));
oceanColors[i * 3] = oceanColor.r;
oceanColors[i * 3 + 1] = oceanColor.g;
oceanColors[i * 3 + 2] = oceanColor.b;
}
oceanGeometry.setAttribute(
"color",
new THREE.BufferAttribute(oceanColors, 3)
);
const ocean = new THREE.Mesh(
oceanGeometry,
new THREE.MeshPhysicalMaterial({
vertexColors: true,
roughness: 0.4,
metalness: 0.08,
clearcoat: 0.3,
clearcoatRoughness: 0.5,
side: THREE.DoubleSide
})
);
ocean.receiveShadow = true;
scene.add(ocean);
// Fine foam traces along the changing shoreline.
const foamLines = [];
for (let lineIndex = 0; lineIndex < 2; lineIndex++) {
const points = [];
for (let z = -25; z <= 25; z += 0.3) {
points.push(
new THREE.Vector3(
coastX(z) - 0.07 - lineIndex * 0.24,
0.065 + lineIndex * 0.012,
z + Math.sin(z * 0.8 + lineIndex) * 0.07
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color: 0xe5eee4,
transparent: true,
opacity: lineIndex === 0 ? 0.43 : 0.18,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
scene.add(line);
foamLines.push(line);
}
// -------------------------------------------------------------------------
// Spring pond and wet margin
// -------------------------------------------------------------------------
const pondX = -4;
const pondZ = 0.4;
const pondY = terrainHeight(pondX, pondZ) + 0.035;
const pondGeometry = new THREE.CircleGeometry(1, 48);
pondGeometry.rotateX(-Math.PI / 2);
const pond = new THREE.Mesh(
pondGeometry,
new THREE.MeshPhysicalMaterial({
color: 0x4b7f75,
roughness: 0.24,
metalness: 0.05,
transparent: true,
opacity: 0.92,
clearcoat: 0.55,
clearcoatRoughness: 0.24,
side: THREE.DoubleSide
})
);
pond.position.set(pondX, pondY, pondZ);
pond.scale.set(2.55, 1, 1.48);
pond.receiveShadow = true;
scene.add(pond);
// -------------------------------------------------------------------------
// Low-poly plant geometry
// -------------------------------------------------------------------------
function createTuftGeometry(leafCount, length, width) {
const positions = [];
for (let i = 0; i < leafCount; i++) {
const angle = (i / leafCount) * Math.PI * 2 + range(-0.18, 0.18);
const radial = new THREE.Vector3(
Math.cos(angle),
0,
Math.sin(angle)
);
const side = new THREE.Vector3(
-Math.sin(angle),
0,
Math.cos(angle)
);
const leafLength = length * range(0.72, 1.12);
const tip = radial.clone().multiplyScalar(leafLength * 0.58);
tip.y = leafLength;
const left = radial.clone().multiplyScalar(leafLength * 0.28)
.addScaledVector(side, width);
left.y = leafLength * 0.48;
const right = radial.clone().multiplyScalar(leafLength * 0.28)
.addScaledVector(side, -width);
right.y = leafLength * 0.48;
positions.push(
0, 0, 0,
left.x, left.y, left.z,
tip.x, tip.y, tip.z,
0, 0, 0,
tip.x, tip.y, tip.z,
right.x, right.y, right.z
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
const shrubGeometry = new THREE.IcosahedronGeometry(0.48, 0);
const grassGeometry = createTuftGeometry(7, 0.72, 0.105);
const agaveGeometry = createTuftGeometry(10, 1.02, 0.15);
const shrubMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
flatShading: true
});
const grassMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95,
side: THREE.DoubleSide,
flatShading: true
});
const agaveMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92,
side: THREE.DoubleSide,
flatShading: true
});
const shrubData = [];
const grassData = [];
const agaveData = [];
const cactusData = [];
const shrubPalette = [
new THREE.Color(0x66734d),
new THREE.Color(0x74734a),
new THREE.Color(0x7d8055),
new THREE.Color(0x596b48)
];
const grassPalette = [
new THREE.Color(0x7b7b4f),
new THREE.Color(0x8a8053),
new THREE.Color(0x68724d)
];
function alignedQuaternion(normal) {
const align = new THREE.Quaternion().setFromUnitVectors(UP, normal);
const yaw = new THREE.Quaternion().setFromAxisAngle(
UP,
range(0, Math.PI * 2)
);
return align.multiply(yaw);
}
for (let attempt = 0; attempt < 9000; attempt++) {
const x = range(-10.7, 4.8);
const z = range(-23.5, 23.5);
const d = x - coastX(z);
if (d < 1.05 || d > 15.2) continue;
const pondDistance = (
((x - pondX) / 3.15) ** 2
+ ((z - pondZ) / 2.05) ** 2
);
if (pondDistance < 1) continue;
const wet = moisture(x, z);
const probability = 0.07 + wet * 0.86;
if (random() > probability) continue;
const normal = terrainNormal(x, z);
if (normal.y < 0.72) continue;
const p = new THREE.Vector3(x, terrainHeight(x, z), z);
const q = alignedQuaternion(normal);
if (
d > 4.8 &&
d < 18.5 &&
wet < 0.64 &&
random() < 0.025 &&
cactusData.length < 44
) {
cactusData.push({
p,
scale: range(0.65, 1.3),
rotation: range(0, Math.PI * 2),
phase: range(0, Math.PI * 2)
});
continue;
}
const plantChoice = random();
const size = range(0.68, 1.38);
if (plantChoice < 0.27) {
agaveData.push({
p,
q,
s: new THREE.Vector3(size, size, size),
color: new THREE.Color(0x536b52).offsetHSL(
range(-0.025, 0.025),
range(-0.04, 0.04),
range(-0.05, 0.05)
)
});
} else if (plantChoice < 0.68) {
const color = shrubPalette[
Math.floor(random() * shrubPalette.length)
].clone().offsetHSL(
range(-0.025, 0.025),
range(-0.04, 0.04),
range(-0.055, 0.055)
);
shrubData.push({
p,
q,
n: normal,
s: new THREE.Vector3(
size * range(0.72, 1.15),
size * range(0.62, 1.03),
size * range(0.72, 1.15)
),
color
});
} else {
const color = grassPalette[
Math.floor(random() * grassPalette.length)
].clone().offsetHSL(
range(-0.02, 0.02),
range(-0.03, 0.03),
range(-0.045, 0.045)
);
grassData.push({
p,
q,
s: new THREE.Vector3(
size * range(0.7, 1.15),
size,
size * range(0.7, 1.15)
),
color
});
}
}
// Reeds around the wet margin of the spring.
for (let i = 0; i < 42; i++) {
const angle = range(0, Math.PI * 2);
const x = pondX + Math.cos(angle) * range(2.5, 3.15);
const z = pondZ + Math.sin(angle) * range(1.45, 1.82);
const normal = terrainNormal(x, z);
const p = new THREE.Vector3(x, terrainHeight(x, z), z);
const size = range(0.75, 1.35);
grassData.push({
p,
q: alignedQuaternion(normal),
s: new THREE.Vector3(size * 0.75, size * 1.35, size * 0.75),
color: new THREE.Color(0x496a48).offsetHSL(
0,
range(-0.04, 0.04),
range(-0.04, 0.04)
)
});
}
function createInstancedPlants(
geometry,
material,
data,
centerOnSurface = false
) {
const mesh = new THREE.InstancedMesh(
geometry,
material,
data.length
);
const dummy = new THREE.Object3D();
data.forEach((item, index) => {
dummy.position.copy(item.p);
if (centerOnSurface && item.n) {
dummy.position.addScaledVector(item.n, item.s.y * 0.34);
}
dummy.quaternion.copy(item.q);
dummy.scale.copy(item.s);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, item.color);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
createInstancedPlants(shrubGeometry, shrubMaterial, shrubData, true);
createInstancedPlants(grassGeometry, grassMaterial, grassData);
createInstancedPlants(agaveGeometry, agaveMaterial, agaveData);
// -------------------------------------------------------------------------
// Sparse columnar cacti
// -------------------------------------------------------------------------
const cactusMaterial = new THREE.MeshStandardMaterial({
color: 0x4d6746,
roughness: 0.9,
flatShading: true
});
const cactusBodyGeometry = new THREE.CylinderGeometry(
0.14,
0.2,
1.7,
6
);
const cactusArmGeometry = new THREE.CylinderGeometry(
0.085,
0.105,
1,
6
);
const cactusCapGeometry = new THREE.SphereGeometry(0.145, 6, 4);
const swayingCacti = [];
function addCactusSegment(parent, a, b, radius) {
const direction = b.clone().sub(a);
const length = direction.length();
const mesh = new THREE.Mesh(cactusArmGeometry, cactusMaterial);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(UP, direction.normalize());
mesh.scale.set(radius / 0.085, length, radius / 0.085);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
}
cactusData.forEach((item) => {
const group = new THREE.Group();
group.position.copy(item.p);
group.rotation.y = item.rotation;
group.scale.setScalar(item.scale);
const body = new THREE.Mesh(cactusBodyGeometry, cactusMaterial);
body.position.y = 0.85;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const cap = new THREE.Mesh(cactusCapGeometry, cactusMaterial);
cap.position.y = 1.68;
cap.scale.y = 0.72;
cap.castShadow = true;
group.add(cap);
const side = random() > 0.5 ? 1 : -1;
addCactusSegment(
group,
new THREE.Vector3(side * 0.12, 0.62, 0),
new THREE.Vector3(side * 0.43, 0.72, 0),
0.95
);
addCactusSegment(
group,
new THREE.Vector3(side * 0.43, 0.72, 0),
new THREE.Vector3(side * 0.43, 1.08, 0),
0.82
);
if (random() > 0.35) {
const zSide = random() > 0.5 ? 1 : -1;
addCactusSegment(
group,
new THREE.Vector3(0, 0.88, zSide * 0.1),
new THREE.Vector3(0, 0.98, zSide * 0.36),
0.75
);
addCactusSegment(
group,
new THREE.Vector3(0, 0.98, zSide * 0.36),
new THREE.Vector3(0, 1.25, zSide * 0.36),
0.66
);
}
group.userData.phase = item.phase;
group.userData.baseRotation = item.rotation;
swayingCacti.push(group);
scene.add(group);
});
// -------------------------------------------------------------------------
// Rocks, pond stones, and distant mesas
// -------------------------------------------------------------------------
const rockGeometry = new THREE.DodecahedronGeometry(1, 0);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const rockData = [];
for (let i = 0; i < 210; i++) {
const x = range(-12.2, 43);
const z = range(-24, 24);
if (x - coastX(z) < 0.2) continue;
const normal = terrainNormal(x, z);
const p = new THREE.Vector3(x, terrainHeight(x, z), z);
const size = range(0.18, 0.85);
rockData.push({
p,
q: alignedQuaternion(normal),
s: new THREE.Vector3(
size * range(0.8, 1.55),
size * range(0.45, 0.95),
size * range(0.8, 1.55)
),
color: new THREE.Color(
random() > 0.5 ? 0x786c5d : 0x8b7962
).offsetHSL(
range(-0.02, 0.02),
range(-0.04, 0.04),
range(-0.08, 0.08)
)
});
}
// A deliberate ring of larger stones around the spring.
for (let i = 0; i < 20; i++) {
const angle = (i / 20) * Math.PI * 2;
const x = pondX + Math.cos(angle) * 2.75;
const z = pondZ + Math.sin(angle) * 1.68;
const normal = terrainNormal(x, z);
const size = range(0.2, 0.43);
rockData.push({
p: new THREE.Vector3(x, terrainHeight(x, z), z),
q: alignedQuaternion(normal),
s: new THREE.Vector3(size * 1.4, size, size),
color: new THREE.Color(0x8b806c)
});
}
createInstancedPlants(rockGeometry, rockMaterial, rockData, true);
const mesaMaterials = [
new THREE.MeshStandardMaterial({
color: 0xa87555,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x93664f,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb17c5a,
roughness: 1,
flatShading: true
})
];
for (let i = 0; i < 7; i++) {
const x = range(23, 43);
const z = range(-22, 22);
const radius = range(2.3, 4.6);
const height = range(2.5, 5.8);
const sides = Math.floor(range(6, 9));
const geometry = new THREE.CylinderGeometry(
radius * range(0.58, 0.78),
radius,
height,
sides,
1
);
const mesa = new THREE.Mesh(
geometry,
mesaMaterials[i % mesaMaterials.length]
);
mesa.position.set(
x,
terrainHeight(x, z) + height * 0.38,
z
);
mesa.rotation.y = range(0, Math.PI * 2);
mesa.castShadow = true;
mesa.receiveShadow = true;
scene.add(mesa);
}
// -------------------------------------------------------------------------
// Fog banks
// -------------------------------------------------------------------------
function createFogTexture() {
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, 62
);
gradient.addColorStop(0, "rgba(255,255,255,0.8)");
gradient.addColorStop(0.28, "rgba(255,255,255,0.38)");
gradient.addColorStop(0.68, "rgba(255,255,255,0.11)");
gradient.addColorStop(1, "rgba(255,255,255,0)");
context.fillStyle = gradient;
context.fillRect(0, 0, 128, 128);
const texture = new THREE.CanvasTexture(canvas);
texture.needsUpdate = true;
return texture;
}
const fogTexture = createFogTexture();
const fogGroup = new THREE.Group();
const fogSprites = [];
scene.add(fogGroup);
for (let i = 0; i < 72; i++) {
const material = new THREE.SpriteMaterial({
map: fogTexture,
color: i % 4 === 0 ? 0xe8ddd0 : 0xf0eee7,
transparent: true,
opacity: range(0.055, 0.14),
depthWrite: false,
depthTest: true,
fog: true
});
const sprite = new THREE.Sprite(material);
const x = range(-20, 6);
const z = range(-25, 25);
const y = range(0.35, 4.2);
sprite.position.set(x, y, z);
sprite.scale.set(range(8, 17), range(1.7, 4.8), 1);
sprite.material.rotation = range(-0.15, 0.15);
fogGroup.add(sprite);
fogSprites.push({
sprite,
speed: range(0.08, 0.22),
phase: range(0, Math.PI * 2),
baseY: y
});
}
// Fine mineral dust suspended over the desert interior.
const dustGeometry = new THREE.BufferGeometry();
const dustPositions = new Float32Array(360 * 3);
for (let i = 0; i < 360; i++) {
dustPositions[i * 3] = range(5, 45);
dustPositions[i * 3 + 1] = range(0.4, 8);
dustPositions[i * 3 + 2] = range(-28, 28);
}
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0xd1b58f,
size: 0.075,
transparent: true,
opacity: 0.27,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(dust);
// -------------------------------------------------------------------------
// A few seabirds moving through the coastal wind
// -------------------------------------------------------------------------
const birdGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.52, 0.05, 0),
new THREE.Vector3(0, -0.13, 0),
new THREE.Vector3(0.52, 0.05, 0)
]);
const birdMaterial = new THREE.LineBasicMaterial({
color: 0x394441,
transparent: true,
opacity: 0.75
});
const birds = [];
for (let i = 0; i < 8; i++) {
const bird = new THREE.Line(birdGeometry, birdMaterial);
bird.userData.phase = range(0, Math.PI * 2);
bird.userData.radius = range(7, 15);
bird.userData.speed = range(0.055, 0.095);
bird.userData.height = range(5.5, 10);
bird.scale.setScalar(range(0.6, 1.05));
scene.add(bird);
birds.push(bird);
}
// -------------------------------------------------------------------------
// Custom orbit / pan / zoom controls; no external controls addon
// -------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(-4, 1.0, 0),
desiredTarget: new THREE.Vector3(-4, 1.0, 0),
theta: -0.72,
desiredTheta: -0.72,
phi: 1.14,
desiredPhi: 1.14,
radius: 47,
desiredRadius: 47
};
const pointers = new Map();
let dragged = false;
let interacted = false;
let previousPinchDistance = 0;
const canvas = renderer.domElement;
function resetView() {
controls.desiredTarget.set(-4, 1.0, 0);
controls.desiredTheta = -0.72;
controls.desiredPhi = 1.14;
controls.desiredRadius = 47;
}
function updatePointerMode(event) {
return event.shiftKey || event.button === 2
? "pan"
: "orbit";
}
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
mode: updatePointerMode(event)
});
interacted = true;
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinchDistance = Math.hypot(
a.x - b.x,
a.y - b.y
);
}
});
const cameraRight = new THREE.Vector3();
const cameraUp = new THREE.Vector3();
canvas.addEventListener("pointermove", (event) => {
const pointer = pointers.get(event.pointerId);
if (!pointer) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance > 0 && distance > 0) {
controls.desiredRadius = clamp(
controls.desiredRadius
* previousPinchDistance / distance,
15,
86
);
}
previousPinchDistance = distance;
return;
}
if (pointer.mode === "pan") {
camera.updateMatrixWorld();
cameraRight.setFromMatrixColumn(camera.matrixWorld, 0);
cameraUp.setFromMatrixColumn(camera.matrixWorld, 1);
const panScale = controls.desiredRadius * 0.00145;
controls.desiredTarget
.addScaledVector(cameraRight, -dx * panScale)
.addScaledVector(cameraUp, dy * panScale);
} else {
controls.desiredTheta -= dx * 0.005;
controls.desiredPhi = clamp(
controls.desiredPhi - dy * 0.0045,
0.24,
1.47
);
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) previousPinchDistance = 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
interacted = true;
controls.desiredRadius = clamp(
controls.desiredRadius * Math.exp(event.deltaY * 0.001),
15,
86
);
}, { passive: false });
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key.toLowerCase()) {
case "arrowleft":
controls.desiredTheta -= 0.09;
break;
case "arrowright":
controls.desiredTheta += 0.09;
break;
case "arrowup":
controls.desiredPhi = clamp(
controls.desiredPhi - 0.07,
0.24,
1.47
);
break;
case "arrowdown":
controls.desiredPhi = clamp(
controls.desiredPhi + 0.07,
0.24,
1.47
);
break;
case "+":
case "=":
controls.desiredRadius = clamp(
controls.desiredRadius * 0.9,
15,
86
);
break;
case "-":
case "_":
controls.desiredRadius = clamp(
controls.desiredRadius * 1.1,
15,
86
);
break;
case "r":
resetView();
break;
default:
handled = false;
}
if (handled) {
interacted = true;
event.preventDefault();
}
});
document.getElementById("reset-view").addEventListener(
"click",
resetView
);
const fogToggle = document.getElementById("fog-toggle");
fogToggle.addEventListener("click", () => {
const visible = fogGroup.visible;
fogGroup.visible = !visible;
fogToggle.setAttribute("aria-pressed", String(!visible));
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const cameraOffset = new THREE.Vector3();
let frame = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
frame++;
if (!interacted && !matchMedia("(prefers-reduced-motion: reduce)").matches) {
controls.desiredTheta += delta * 0.022;
}
const damping = 1 - Math.exp(-delta * 7.5);
controls.theta = THREE.MathUtils.lerp(
controls.theta,
controls.desiredTheta,
damping
);
controls.phi = THREE.MathUtils.lerp(
controls.phi,
controls.desiredPhi,
damping
);
controls.radius = THREE.MathUtils.lerp(
controls.radius,
controls.desiredRadius,
damping
);
controls.target.lerp(
controls.desiredTarget,
damping
);
const sinPhi = Math.sin(controls.phi);
cameraOffset.set(
controls.radius * sinPhi * Math.sin(controls.theta),
controls.radius * Math.cos(controls.phi),
controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.position.copy(controls.target).add(cameraOffset);
camera.lookAt(controls.target);
sky.position.copy(camera.position);
// Ocean surface.
const positions = oceanGeometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const z = positions.getZ(i);
const wave =
Math.sin(x * 0.19 + time * 0.72) * 0.058
+ Math.sin(z * 0.27 - time * 0.46) * 0.034
+ Math.sin((x + z) * 0.11 + time * 0.27) * 0.024;
positions.setY(i, wave);
}
positions.needsUpdate = true;
if (frame % 3 === 0) oceanGeometry.computeVertexNormals();
// Fog moves inland from the Pacific.
for (const fog of fogSprites) {
fog.sprite.position.x += delta * fog.speed;
fog.sprite.position.y =
fog.baseY + Math.sin(time * 0.23 + fog.phase) * 0.13;
fog.sprite.material.rotation += delta * 0.008;
if (fog.sprite.position.x > 8) {
fog.sprite.position.x = -20;
}
}
dust.rotation.y = Math.sin(time * 0.025) * 0.025;
dust.position.y = Math.sin(time * 0.16) * 0.08;
for (const cactus of swayingCacti) {
cactus.rotation.z =
Math.sin(time * 0.8 + cactus.userData.phase) * 0.012;
}
for (const bird of birds) {
const phase = bird.userData.phase;
const radius = bird.userData.radius;
const angle = time * bird.userData.speed + phase;
bird.position.set(
-12 + Math.cos(angle) * radius,
bird.userData.height + Math.sin(time * 0.45 + phase) * 0.6,
Math.sin(angle) * radius * 0.7
);
bird.rotation.y = -angle;
bird.rotation.z = Math.sin(time * 1.6 + phase) * 0.09;
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
});
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```