Prompt specification
Build or customize the Three.js 3D scene "Cerrado plateau and gallery-forest corridor" 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: Cerrado plateau and gallery-forest corridor
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cerrado-plateau-and-gallery-forest-corridor-x3055
Scene Description Prompt:
Make a low-poly Three.js scene in a self-contained HTML file using cdnjs: cerrado plateau and gallery-forest corridor. Build a bounded landscape with open upland, drainage line, gallery forest, and small rock outcrop. Keep vegetation patterns related to the modeled watercourse and terrain.
Working Three.js Starter (cerrado-plateau-and-gallery-forest-corridor-x3055):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cerrado Plateau — Gallery Forest Corridor</title>
<style>
:root {
--ink: #243229;
--paper: rgba(248, 244, 229, 0.92);
--line: rgba(48, 62, 48, 0.18);
--forest: #355e39;
--water: #4b8c91;
--upland: #a38d4f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #c9d8d0;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 22px;
left: 22px;
width: min(370px, calc(100vw - 44px));
padding: 20px 22px;
border: 1px solid rgba(255, 255, 255, 0.42);
border-radius: 15px;
background: var(--paper);
box-shadow: 0 18px 50px rgba(44, 54, 43, 0.16);
backdrop-filter: blur(12px);
pointer-events: none;
user-select: none;
}
.eyebrow {
margin-bottom: 7px;
color: #687365;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 3vw, 34px);
font-weight: 500;
letter-spacing: -0.025em;
line-height: 1.05;
}
.subtitle {
margin: 10px 0 17px;
color: #596457;
font-size: 12px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.035em;
text-transform: uppercase;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.controls {
position: fixed;
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border: 1px solid rgba(255, 255, 255, 0.42);
border-radius: 13px;
background: var(--paper);
box-shadow: 0 14px 40px rgba(44, 54, 43, 0.16);
backdrop-filter: blur(12px);
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 8px;
padding: 9px 12px;
background: rgba(255, 255, 255, 0.56);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
background 160ms ease,
transform 160ms ease;
}
button:hover {
background: #fffdf5;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.hint {
position: fixed;
left: 22px;
bottom: 24px;
padding: 8px 11px;
border-radius: 9px;
background: rgba(248, 244, 229, 0.78);
color: #657061;
font-size: 10px;
letter-spacing: 0.03em;
pointer-events: none;
user-select: none;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px 17px;
}
.subtitle {
margin-bottom: 12px;
}
.controls {
right: 12px;
bottom: 12px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly cerrado landscape"></canvas>
<section class="panel">
<div class="eyebrow">Terrain study · 01</div>
<h1>Cerrado Plateau</h1>
<p class="subtitle">
A bounded upland crossed by a winding drainage line. Moist gallery forest
follows the watercourse, while sparse trees and grasses occupy the drier plateau.
</p>
<div class="legend">
<span><i class="swatch" style="background:#355e39"></i>Gallery forest</span>
<span><i class="swatch" style="background:#4b8c91"></i>Drainage</span>
<span><i class="swatch" style="background:#a38d4f"></i>Open cerrado</span>
</div>
</section>
<div class="hint">Drag to orbit · Right-drag to pan · Scroll to zoom</div>
<div class="controls">
<button id="top-view" type="button">Top view</button>
<button id="reset-view" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc9d8d0);
scene.fog = new THREE.Fog(0xc9d8d0, 52, 108);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
180
);
const world = new THREE.Group();
scene.add(world);
const hemisphere = new THREE.HemisphereLight(0xe7f0df, 0x73583a, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe1a6, 3.25);
sun.position.set(-13, 23, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -27;
sun.shadow.camera.right = 27;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
scene.add(sun.target);
// ---------------------------------------------------------------------
// Deterministic random utilities
// ---------------------------------------------------------------------
function mulberry32(seed) {
return function () {
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 terrainRandom = mulberry32(1947);
const vegetationRandom = mulberry32(73129);
const rand = () => vegetationRandom();
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
const smoothstep = THREE.MathUtils.smoothstep;
// ---------------------------------------------------------------------
// Landscape model
//
// The valley center is a modeled function of Z. Terrain height and plant
// placement both derive from distance to this centerline.
// ---------------------------------------------------------------------
const LAND_WIDTH = 34;
const LAND_DEPTH = 28;
const GRID_X = 66;
const GRID_Z = 56;
const OUTCROP = { x: 8.3, z: 3.0, radius: 2.6 };
function valleyX(z) {
return (
-1.35 +
2.4 * Math.sin(z * 0.115) +
0.88 * Math.sin(z * 0.31)
);
}
function valleyFloor(z) {
// A gentle downstream slope from the southern to northern boundary.
return 0.14 + (z + LAND_DEPTH / 2) * 0.0105;
}
function waterLevel(z) {
return valleyFloor(z) + 0.16;
}
function terrainHeight(x, z) {
const center = valleyX(z);
const distance = Math.abs(x - center);
const plateau =
2.55 +
0.28 * Math.sin(z * 0.12) +
0.18 * Math.sin(x * 0.31 + z * 0.08) +
0.11 * Math.sin(x * 0.77 - z * 0.17);
const valleyMix = smoothstep(distance, 0.48, 3.1);
let height = lerp(valleyFloor(z), plateau, valleyMix);
height +=
0.09 *
Math.sin(x * 1.23 + z * 0.91) *
valleyMix;
const outcropDistance =
((x - OUTCROP.x) ** 2) / 3.2 +
((z - OUTCROP.z) ** 2) / 5.5;
height += 0.83 * Math.exp(-outcropDistance);
return height;
}
function localSlope(x, z) {
const dx = terrainHeight(x + 0.3, z) - terrainHeight(x - 0.3, z);
const dz = terrainHeight(x, z + 0.3) - terrainHeight(x, z - 0.3);
return Math.hypot(dx, dz);
}
function isNearOutcrop(x, z, extra = 0) {
return (
Math.hypot(x - OUTCROP.x, z - OUTCROP.z) <
OUTCROP.radius + extra
);
}
const dryColor = new THREE.Color(0x9b8746);
const wetColor = new THREE.Color(0x496637);
const bankColor = new THREE.Color(0x8b7245);
const highColor = new THREE.Color(0xb3a06a);
function terrainColor(x, z) {
const distance = Math.abs(x - valleyX(z));
const wetness = 1 - smoothstep(distance, 0.65, 3.7);
const bankness = 1 - smoothstep(distance, 0.45, 1.5);
const height = terrainHeight(x, z);
const color = dryColor.clone().lerp(wetColor, wetness * 0.8);
color.lerp(bankColor, bankness * 0.52);
const highAmount = clamp((height - 2.55) * 0.12, 0, 0.18);
color.lerp(highColor, highAmount);
if (isNearOutcrop(x, z, -0.5)) {
color.lerp(new THREE.Color(0x91836b), 0.26);
}
return color;
}
// ---------------------------------------------------------------------
// Faceted terrain mesh
// ---------------------------------------------------------------------
const terrainPositions = [];
const terrainColors = [];
const dx = LAND_WIDTH / GRID_X;
const dz = LAND_DEPTH / GRID_Z;
function terrainPoint(i, j) {
const x = -LAND_WIDTH / 2 + i * dx;
const z = -LAND_DEPTH / 2 + j * dz;
return { x, y: terrainHeight(x, z), z };
}
function addTerrainTriangle(a, b, c) {
const centerX = (a.x + b.x + c.x) / 3;
const centerZ = (a.z + b.z + c.z) / 3;
const color = terrainColor(centerX, centerZ);
color.offsetHSL(
(terrainRandom() - 0.5) * 0.018,
(terrainRandom() - 0.5) * 0.045,
(terrainRandom() - 0.5) * 0.065
);
for (const point of [a, b, c]) {
terrainPositions.push(point.x, point.y, point.z);
terrainColors.push(color.r, color.g, color.b);
}
}
for (let j = 0; j < GRID_Z; j++) {
for (let i = 0; i < GRID_X; i++) {
const p00 = terrainPoint(i, j);
const p10 = terrainPoint(i + 1, j);
const p01 = terrainPoint(i, j + 1);
const p11 = terrainPoint(i + 1, j + 1);
if ((i + j) % 2 === 0) {
addTerrainTriangle(p00, p10, p11);
addTerrainTriangle(p00, p11, p01);
} else {
addTerrainTriangle(p00, p10, p01);
addTerrainTriangle(p10, p11, p01);
}
}
}
const terrainGeometry = new THREE.BufferGeometry();
terrainGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(terrainPositions, 3)
);
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrainMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true
});
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.castShadow = true;
terrain.receiveShadow = true;
world.add(terrain);
// ---------------------------------------------------------------------
// Exposed soil sides and bottom: the landscape reads as a bounded model.
// ---------------------------------------------------------------------
const perimeter = [];
for (let i = 0; i <= GRID_X; i++) {
perimeter.push({
x: -LAND_WIDTH / 2 + i * dx,
z: -LAND_DEPTH / 2
});
}
for (let j = 1; j <= GRID_Z; j++) {
perimeter.push({
x: LAND_WIDTH / 2,
z: -LAND_DEPTH / 2 + j * dz
});
}
for (let i = GRID_X - 1; i >= 0; i--) {
perimeter.push({
x: -LAND_WIDTH / 2 + i * dx,
z: LAND_DEPTH / 2
});
}
for (let j = GRID_Z - 1; j > 0; j--) {
perimeter.push({
x: -LAND_WIDTH / 2,
z: -LAND_DEPTH / 2 + j * dz
});
}
const sidePositions = [];
const sideColors = [];
const strata = [
{ y: -0.35, color: new THREE.Color(0x654a30) },
{ y: -1.42, color: new THREE.Color(0x403329) }
];
function addSideQuad(a, b, colorA, colorB) {
const points = [
[a.x, a.y, a.z],
[b.x, b.y, b.z],
[a.x, strata[0].y, a.z],
[b.x, strata[0].y, b.z]
];
const colors = [colorA, colorB, colorA, colorB];
for (const index of [0, 2, 1, 1, 2, 3]) {
sidePositions.push(...points[index]);
const color = colors[index];
sideColors.push(color.r, color.g, color.b);
}
const lowerPoints = [
[a.x, strata[0].y, a.z],
[b.x, strata[0].y, b.z],
[a.x, strata[1].y, a.z],
[b.x, strata[1].y, b.z]
];
const lowerColors = [
strata[0].color,
strata[0].color,
strata[1].color,
strata[1].color
];
for (const index of [0, 2, 1, 1, 2, 3]) {
sidePositions.push(...lowerPoints[index]);
const color = lowerColors[index];
sideColors.push(color.r, color.g, color.b);
}
}
const topSoil = new THREE.Color(0x715438);
for (let i = 0; i < perimeter.length; i++) {
const a = perimeter[i];
const b = perimeter[(i + 1) % perimeter.length];
a.y = terrainHeight(a.x, a.z);
b.y = terrainHeight(b.x, b.z);
const variation = 0.9 + terrainRandom() * 0.16;
const colorA = topSoil.clone().multiplyScalar(variation);
const colorB = topSoil.clone().multiplyScalar(0.93 + terrainRandom() * 0.16);
addSideQuad(a, b, colorA, colorB);
}
const sideGeometry = new THREE.BufferGeometry();
sideGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(sidePositions, 3)
);
sideGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(sideColors, 3)
);
sideGeometry.computeVertexNormals();
const sides = new THREE.Mesh(
sideGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
})
);
sides.castShadow = true;
sides.receiveShadow = true;
world.add(sides);
const base = new THREE.Mesh(
new THREE.BoxGeometry(LAND_WIDTH, 0.24, LAND_DEPTH),
new THREE.MeshStandardMaterial({
color: 0x3c3026,
roughness: 1
})
);
base.position.y = -1.54;
base.receiveShadow = true;
world.add(base);
const studioGround = new THREE.Mesh(
new THREE.PlaneGeometry(180, 180),
new THREE.MeshStandardMaterial({
color: 0xc5b792,
roughness: 1
})
);
studioGround.rotation.x = -Math.PI / 2;
studioGround.position.y = -1.68;
studioGround.receiveShadow = true;
scene.add(studioGround);
// ---------------------------------------------------------------------
// Drainage corridor: muddy bank ribbon and low-poly stream surface
// ---------------------------------------------------------------------
function createTerrainRibbon(width, lift) {
const positions = [];
const indices = [];
const samples = 110;
for (let i = 0; i < samples; i++) {
const z = lerp(-LAND_DEPTH / 2, LAND_DEPTH / 2, i / (samples - 1));
const center = valleyX(z);
const localWidth = width(z);
for (const side of [-1, 1]) {
const x = center + side * localWidth * 0.5;
positions.push(x, terrainHeight(x, z) + lift, z);
}
}
for (let i = 0; i < samples - 1; i++) {
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();
return geometry;
}
const bank = new THREE.Mesh(
createTerrainRibbon(
(z) => 1.28 + 0.12 * Math.sin(z * 0.43),
0.055
),
new THREE.MeshStandardMaterial({
color: 0x7f7044,
roughness: 0.98,
flatShading: true,
side: THREE.DoubleSide
})
);
bank.receiveShadow = true;
world.add(bank);
const streamPositions = [];
const streamIndices = [];
const streamSamples = 120;
for (let i = 0; i < streamSamples; i++) {
const z = lerp(-LAND_DEPTH / 2, LAND_DEPTH / 2, i / (streamSamples - 1));
const center = valleyX(z);
const width = 0.56 + 0.055 * Math.sin(z * 0.71);
const y = waterLevel(z);
streamPositions.push(center - width / 2, y, z);
streamPositions.push(center + width / 2, y, z);
}
for (let i = 0; i < streamSamples - 1; i++) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
streamIndices.push(a, c, b, b, c, d);
}
const streamGeometry = new THREE.BufferGeometry();
streamGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(streamPositions, 3)
);
streamGeometry.setIndex(streamIndices);
streamGeometry.computeVertexNormals();
const stream = new THREE.Mesh(
streamGeometry,
new THREE.MeshStandardMaterial({
color: 0x4c8e91,
emissive: 0x143f42,
emissiveIntensity: 0.16,
roughness: 0.28,
metalness: 0.08,
transparent: true,
opacity: 0.88,
flatShading: true,
side: THREE.DoubleSide,
depthWrite: false
})
);
world.add(stream);
// ---------------------------------------------------------------------
// Vegetation sampling
// ---------------------------------------------------------------------
const galleryTrees = [];
let attempts = 0;
while (galleryTrees.length < 126 && attempts < 1200) {
attempts++;
const z = lerp(-13.2, 13.2, rand());
const center = valleyX(z);
const side = rand() < 0.5 ? -1 : 1;
const offset = 0.76 + Math.pow(rand(), 1.65) * 3.05;
const x = center + side * offset;
if (isNearOutcrop(x, z, 0.5)) continue;
if (terrainHeight(x, z) < waterLevel(z) + 0.1) continue;
if (localSlope(x, z) > 1.15) continue;
if (rand() < 0.08) continue;
galleryTrees.push({
x,
z,
base: terrainHeight(x, z),
rotation: rand() * Math.PI * 2,
trunkLength: 1.25 + rand() * 1.45,
radius: 0.53 + rand() * 0.46,
size: 0.78 + rand() * 0.38,
leanX: (rand() - 0.5) * 0.1,
leanZ: (rand() - 0.5) * 0.1,
shade: rand()
});
}
const cerradoTrees = [];
attempts = 0;
while (cerradoTrees.length < 43 && attempts < 1600) {
attempts++;
const x = lerp(-15.2, 15.2, rand());
const z = lerp(-12.8, 12.8, rand());
const distance = Math.abs(x - valleyX(z));
if (distance < 4.25) continue;
if (isNearOutcrop(x, z, -0.35)) continue;
if (localSlope(x, z) > 0.55) continue;
if (rand() < 0.1) continue;
cerradoTrees.push({
x,
z,
base: terrainHeight(x, z),
rotation: rand() * Math.PI * 2,
trunkLength: 0.95 + rand() * 0.95,
radius: 0.72 + rand() * 0.58,
size: 0.82 + rand() * 0.35,
leanX: (rand() - 0.5) * 0.08,
leanZ: (rand() - 0.5) * 0.08,
shade: rand()
});
}
const galleryBushes = [];
attempts = 0;
while (galleryBushes.length < 165 && attempts < 1000) {
attempts++;
const z = lerp(-13.1, 13.1, rand());
const center = valleyX(z);
const side = rand() < 0.5 ? -1 : 1;
const offset = 0.38 + Math.pow(rand(), 1.55) * 4.0;
const x = center + side * offset;
if (terrainHeight(x, z) < waterLevel(z) + 0.07) continue;
if (isNearOutcrop(x, z, 0)) continue;
galleryBushes.push({
x,
z,
base: terrainHeight(x, z),
scale: 0.17 + rand() * 0.3,
rotation: rand() * Math.PI * 2,
shade: rand()
});
}
const cerradoBushes = [];
attempts = 0;
while (cerradoBushes.length < 115 && attempts < 1000) {
attempts++;
const x = lerp(-15.5, 15.5, rand());
const z = lerp(-13.0, 13.0, rand());
if (Math.abs(x - valleyX(z)) < 3.5) continue;
if (isNearOutcrop(x, z, 0)) continue;
cerradoBushes.push({
x,
z,
base: terrainHeight(x, z),
scale: 0.13 + rand() * 0.24,
rotation: rand() * Math.PI * 2,
shade: rand()
});
}
const grass = [];
for (let i = 0; i < 330; i++) {
let x;
let z;
const wetGrass = rand() < 0.36;
if (wetGrass) {
z = lerp(-13.1, 13.1, rand());
const center = valleyX(z);
const side = rand() < 0.5 ? -1 : 1;
const offset = 0.32 + rand() * 3.8;
x = center + side * offset;
} else {
x = lerp(-15.6, 15.6, rand());
z = lerp(-13.1, 13.1, rand());
}
if (terrainHeight(x, z) < waterLevel(z) + 0.05) continue;
if (isNearOutcrop(x, z, 0)) continue;
grass.push({
x,
z,
base: terrainHeight(x, z),
scale: 0.55 + rand() * 0.85,
rotation: rand() * Math.PI * 2,
wet: wetGrass,
shade: rand()
});
}
// ---------------------------------------------------------------------
// Instanced low-poly trees and shrubs
// ---------------------------------------------------------------------
const dummy = new THREE.Object3D();
function createInstancedMesh(geometry, material, count) {
const mesh = new THREE.InstancedMesh(geometry, material, count);
mesh.castShadow = true;
mesh.receiveShadow = true;
world.add(mesh);
return mesh;
}
function setInstance(mesh, index, position, scale, rotation, leanX = 0, leanZ = 0) {
dummy.position.copy(position);
dummy.rotation.set(leanX, rotation, leanZ);
dummy.scale.copy(scale);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
}
const trunkGeometry = new THREE.CylinderGeometry(0.11, 0.17, 1, 5);
const canopyGeometry = new THREE.IcosahedronGeometry(1, 0);
const bushGeometry = new THREE.DodecahedronGeometry(1, 0);
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x684a31,
roughness: 1,
flatShading: true
});
const galleryTrunks = createInstancedMesh(
trunkGeometry,
trunkMaterial,
galleryTrees.length
);
const galleryCanopies = createInstancedMesh(
canopyGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94,
flatShading: true
}),
galleryTrees.length * 2
);
const galleryPalette = [
new THREE.Color(0x2f5634),
new THREE.Color(0x3b6539),
new THREE.Color(0x426c3d),
new THREE.Color(0x294d31)
];
galleryTrees.forEach((tree, index) => {
const trunkScale = new THREE.Vector3(
tree.size,
tree.trunkLength,
tree.size
);
setInstance(
galleryTrunks,
index,
new THREE.Vector3(
tree.x,
tree.base + tree.trunkLength / 2,
tree.z
),
trunkScale,
tree.rotation,
tree.leanX,
tree.leanZ
);
const color = galleryPalette[
Math.floor(tree.shade * galleryPalette.length)
];
const canopyY = tree.base + tree.trunkLength + tree.radius * 0.48;
setInstance(
galleryCanopies,
index * 2,
new THREE.Vector3(tree.x, canopyY, tree.z),
new THREE.Vector3(
tree.radius,
tree.radius * 0.88,
tree.radius * 0.9
),
tree.rotation,
tree.leanX,
tree.leanZ
);
const lobeAngle = tree.rotation + tree.shade * Math.PI;
const lobeX = tree.x + Math.cos(lobeAngle) * tree.radius * 0.55;
const lobeZ = tree.z + Math.sin(lobeAngle) * tree.radius * 0.55;
setInstance(
galleryCanopies,
index * 2 + 1,
new THREE.Vector3(
lobeX,
canopyY + tree.radius * 0.2,
lobeZ
),
new THREE.Vector3(
tree.radius * 0.72,
tree.radius * 0.65,
tree.radius * 0.7
),
tree.rotation + 0.7,
tree.leanX,
tree.leanZ
);
galleryCanopies.setColorAt(index * 2, color);
galleryCanopies.setColorAt(index * 2 + 1, color.clone().offsetHSL(0, 0, 0.025));
});
galleryCanopies.instanceColor.needsUpdate = true;
const cerradoTrunks = createInstancedMesh(
trunkGeometry,
trunkMaterial,
cerradoTrees.length
);
const cerradoCanopies = createInstancedMesh(
canopyGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
flatShading: true
}),
cerradoTrees.length * 2
);
const cerradoPalette = [
new THREE.Color(0x59613a),
new THREE.Color(0x68683d),
new THREE.Color(0x766b3b),
new THREE.Color(0x4e5935)
];
cerradoTrees.forEach((tree, index) => {
setInstance(
cerradoTrunks,
index,
new THREE.Vector3(
tree.x,
tree.base + tree.trunkLength / 2,
tree.z
),
new THREE.Vector3(
tree.size * 1.15,
tree.trunkLength,
tree.size * 1.15
),
tree.rotation,
tree.leanX,
tree.leanZ
);
const color = cerradoPalette[
Math.floor(tree.shade * cerradoPalette.length)
];
const umbrellaY = tree.base + tree.trunkLength;
setInstance(
cerradoCanopies,
index * 2,
new THREE.Vector3(tree.x, umbrellaY + 0.13, tree.z),
new THREE.Vector3(
tree.radius * 1.12,
tree.radius * 0.43,
tree.radius * 0.92
),
tree.rotation,
tree.leanX,
tree.leanZ
);
setInstance(
cerradoCanopies,
index * 2 + 1,
new THREE.Vector3(
tree.x + Math.cos(tree.rotation) * tree.radius * 0.32,
umbrellaY + tree.radius * 0.15,
tree.z + Math.sin(tree.rotation) * tree.radius * 0.32
),
new THREE.Vector3(
tree.radius * 0.62,
tree.radius * 0.34,
tree.radius * 0.58
),
tree.rotation + 0.5,
tree.leanX,
tree.leanZ
);
cerradoCanopies.setColorAt(index * 2, color);
cerradoCanopies.setColorAt(index * 2 + 1, color.clone().offsetHSL(0, 0, 0.025));
});
cerradoCanopies.instanceColor.needsUpdate = true;
const bushMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const galleryBushMesh = createInstancedMesh(
bushGeometry,
bushMaterial,
galleryBushes.length
);
const cerradoBushMesh = createInstancedMesh(
bushGeometry,
bushMaterial,
cerradoBushes.length
);
galleryBushes.forEach((bush, index) => {
setInstance(
galleryBushMesh,
index,
new THREE.Vector3(bush.x, bush.base + bush.scale * 0.55, bush.z),
new THREE.Vector3(
bush.scale * 1.2,
bush.scale * 0.8,
bush.scale
),
bush.rotation
);
galleryBushMesh.setColorAt(
index,
new THREE.Color(0x496b3b).offsetHSL(0, 0, (bush.shade - 0.5) * 0.12)
);
});
cerradoBushes.forEach((bush, index) => {
setInstance(
cerradoBushMesh,
index,
new THREE.Vector3(bush.x, bush.base + bush.scale * 0.48, bush.z),
new THREE.Vector3(
bush.scale * 1.15,
bush.scale * 0.72,
bush.scale * 0.95
),
bush.rotation
);
cerradoBushMesh.setColorAt(
index,
new THREE.Color(0x7b7744).offsetHSL(0, 0, (bush.shade - 0.5) * 0.13)
);
});
galleryBushMesh.instanceColor.needsUpdate = true;
cerradoBushMesh.instanceColor.needsUpdate = true;
// ---------------------------------------------------------------------
// Faceted grass clusters
// ---------------------------------------------------------------------
const bladePositions = [];
for (let blade = 0; blade < 3; blade++) {
const angle = (blade / 3) * Math.PI;
const dx = Math.cos(angle) * 0.12;
const dz = Math.sin(angle) * 0.12;
bladePositions.push(
-dx, 0, -dz,
dx, 0, dz,
0, 0.62, 0
);
}
const grassGeometry = new THREE.BufferGeometry();
grassGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(bladePositions, 3)
);
grassGeometry.computeVertexNormals();
const grassMesh = createInstancedMesh(
grassGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
side: THREE.DoubleSide
}),
grass.length
);
grass.forEach((tuft, index) => {
setInstance(
grassMesh,
index,
new THREE.Vector3(tuft.x, tuft.base + 0.02, tuft.z),
new THREE.Vector3(tuft.scale, tuft.scale, tuft.scale),
tuft.rotation
);
const color = tuft.wet
? new THREE.Color(0x557646)
: new THREE.Color(0x9b944f);
color.offsetHSL(0, 0, (tuft.shade - 0.5) * 0.11);
grassMesh.setColorAt(index, color);
});
grassMesh.instanceColor.needsUpdate = true;
// ---------------------------------------------------------------------
// Rock outcrop and stream stones
// ---------------------------------------------------------------------
const rockGeometry = new THREE.DodecahedronGeometry(1, 0);
const rockMaterials = [
new THREE.MeshStandardMaterial({
color: 0x766d5d,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x8c806c,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x625e54,
roughness: 1,
flatShading: true
})
];
for (let i = 0; i < 13; i++) {
const angle = rand() * Math.PI * 2;
const radius = i === 0 ? 0.15 : Math.sqrt(rand()) * 1.75;
const x = OUTCROP.x + Math.cos(angle) * radius;
const z = OUTCROP.z + Math.sin(angle) * radius * 0.72;
const sx = i === 0 ? 1.65 : 0.48 + rand() * 0.82;
const sy = i === 0 ? 1.35 : 0.35 + rand() * 0.8;
const sz = i === 0 ? 1.35 : 0.46 + rand() * 0.78;
const rock = new THREE.Mesh(
rockGeometry,
rockMaterials[Math.floor(rand() * rockMaterials.length)]
);
rock.scale.set(sx, sy, sz);
rock.position.set(
x,
terrainHeight(x, z) + sy * 0.58,
z
);
rock.rotation.set(rand() * 0.5, rand() * Math.PI, rand() * 0.4);
rock.castShadow = true;
rock.receiveShadow = true;
world.add(rock);
}
const creekStoneCount = 38;
const creekStones = createInstancedMesh(
rockGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
}),
creekStoneCount
);
for (let i = 0; i < creekStoneCount; i++) {
const z = lerp(-13.1, 13.1, rand());
const side = rand() < 0.5 ? -1 : 1;
const center = valleyX(z);
const x = center + side * (0.35 + rand() * 0.42);
const scale = 0.09 + rand() * 0.19;
setInstance(
creekStones,
i,
new THREE.Vector3(
x,
terrainHeight(x, z) + scale * 0.42,
z
),
new THREE.Vector3(
scale * (1 + rand() * 0.5),
scale * 0.72,
scale
),
rand() * Math.PI
);
creekStones.setColorAt(
i,
new THREE.Color(0x8b8068).offsetHSL(0, 0, (rand() - 0.5) * 0.12)
);
}
creekStones.instanceColor.needsUpdate = true;
// ---------------------------------------------------------------------
// Moving stream glints
// ---------------------------------------------------------------------
const glintCount = 20;
const glints = new THREE.InstancedMesh(
new THREE.PlaneGeometry(1, 0.075),
new THREE.MeshBasicMaterial({
color: 0xe3f1dc,
transparent: true,
opacity: 0.42,
depthWrite: false,
side: THREE.DoubleSide
}),
glintCount
);
glints.frustumCulled = false;
world.add(glints);
function updateGlints(time) {
for (let i = 0; i < glintCount; i++) {
const phase = (i / glintCount + time * 0.025) % 1;
const z = lerp(-13.7, 13.7, phase);
const center = valleyX(z);
const x = center + Math.sin(i * 4.17 + time * 0.7) * 0.12;
dummy.position.set(x, waterLevel(z) + 0.025, z);
dummy.rotation.set(-Math.PI / 2, 0, 0);
dummy.scale.set(0.35 + (i % 4) * 0.12, 1, 1);
dummy.updateMatrix();
glints.setMatrixAt(i, dummy.matrix);
}
glints.instanceMatrix.needsUpdate = true;
}
// ---------------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom, and touch pinch
// ---------------------------------------------------------------------
const initialView = {
theta: 0.73,
phi: 1.03,
radius: 43,
target: new THREE.Vector3(0, 1.15, 0)
};
const goal = {
theta: initialView.theta,
phi: initialView.phi,
radius: initialView.radius,
target: initialView.target.clone()
};
const current = {
theta: initialView.theta,
phi: initialView.phi,
radius: initialView.radius,
target: initialView.target.clone()
};
const pointers = new Map();
let gesture = null;
let interactionMode = null;
function clampGoal() {
goal.phi = clamp(goal.phi, 0.1, 1.42);
goal.radius = clamp(goal.radius, 16, 68);
goal.target.x = clamp(goal.target.x, -11, 11);
goal.target.z = clamp(goal.target.z, -10, 10);
}
function panPixels(dx, dy) {
const height = canvas.clientHeight || 1;
const scale =
(2 * goal.radius *
Math.tan(THREE.MathUtils.degToRad(camera.fov * 0.5))) /
height;
const right = new THREE.Vector3(
Math.cos(goal.theta),
0,
-Math.sin(goal.theta)
);
const forward = new THREE.Vector3(
-Math.sin(goal.theta),
0,
-Math.cos(goal.theta)
);
goal.target.addScaledVector(right, -dx * scale);
goal.target.addScaledVector(forward, dy * scale);
clampGoal();
}
function pointerDistance() {
const points = [...pointers.values()];
return Math.hypot(points[0].x - points[1].x, points[0].y - points[1].y);
}
function pointerMidpoint() {
const points = [...pointers.values()];
return {
x: (points[0].x + points[1].x) / 2,
y: (points[0].y + points[1].y) / 2
};
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
interactionMode = "pinch";
gesture = {
distance: pointerDistance(),
midpoint: pointerMidpoint()
};
} else {
interactionMode =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (interactionMode === "pinch" && pointers.size >= 2) {
const distance = pointerDistance();
const midpoint = pointerMidpoint();
if (gesture && distance > 0) {
goal.radius = clamp(
goal.radius * (gesture.distance / distance),
16,
68
);
panPixels(
midpoint.x - gesture.midpoint.x,
midpoint.y - gesture.midpoint.y
);
}
gesture = {
distance,
midpoint
};
return;
}
if (interactionMode === "orbit") {
goal.theta -= dx * 0.006;
goal.phi -= dy * 0.005;
clampGoal();
} else if (interactionMode === "pan") {
panPixels(dx, dy);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
interactionMode = null;
gesture = null;
} else if (pointers.size === 1) {
interactionMode = "orbit";
gesture = null;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
goal.radius = clamp(
goal.radius * Math.exp(event.deltaY * 0.001),
16,
68
);
},
{ passive: false }
);
function resetView() {
goal.theta = initialView.theta;
goal.phi = initialView.phi;
goal.radius = initialView.radius;
goal.target.copy(initialView.target);
}
document.querySelector("#reset-view").addEventListener("click", resetView);
document.querySelector("#top-view").addEventListener("click", () => {
goal.theta = 0;
goal.phi = 0.12;
goal.radius = 41;
goal.target.set(0, 1.0, 0);
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
const spherical = new THREE.Spherical();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
const smoothing = 1 - Math.pow(0.0015, delta);
current.theta = lerp(current.theta, goal.theta, smoothing);
current.phi = lerp(current.phi, goal.phi, smoothing);
current.radius = lerp(current.radius, goal.radius, smoothing);
current.target.lerp(goal.target, smoothing);
spherical.set(
current.radius,
current.phi,
current.theta
);
camera.position.setFromSpherical(spherical).add(current.target);
camera.lookAt(current.target);
updateGlints(time);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```