Prompt specification
Build or customize the Three.js 3D scene "Explore resource changes in a fictional herd-route model" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Explore resource changes in a fictional herd-route model
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/explore-resource-changes-in-a-fictional-herd-route-mod-x3120
Scene Description Prompt:
Move rainfall patches in a fictional Serengeti-inspired voxel landscape to explore simplified herd routes. Build standalone HTML with Three.js from cdnjs.
Working Three.js Starter (explore-resource-changes-in-a-fictional-herd-route-mod-x3120):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Serengeti Rain Routes</title>
<style>
:root {
--ink: #17211d;
--muted: #65716a;
--paper: rgba(251, 248, 235, 0.94);
--paper-solid: #fbf8eb;
--line: rgba(28, 44, 35, 0.13);
--blue: #48b9e6;
--green: #708e4c;
--gold: #d3a548;
--shadow: 0 18px 50px rgba(30, 39, 28, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d7d3b5;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(255, 250, 225, 0.08), transparent 30%),
radial-gradient(circle at center, transparent 52%, rgba(48, 50, 29, 0.14));
z-index: 2;
}
.topbar {
position: fixed;
top: 20px;
left: 22px;
right: 22px;
z-index: 10;
display: flex;
align-items: flex-start;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
padding: 12px 16px 12px 12px;
border: 1px solid rgba(255, 255, 255, 0.45);
border-radius: 17px;
background: rgba(251, 248, 235, 0.9);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.brand-mark {
position: relative;
width: 42px;
height: 42px;
overflow: hidden;
border-radius: 12px;
background: #20362b;
}
.brand-mark::before {
content: "";
position: absolute;
left: -4px;
right: -4px;
bottom: -7px;
height: 23px;
border-radius: 50%;
background: #c5a65b;
transform: rotate(-5deg);
}
.brand-mark::after {
content: "☂";
position: absolute;
z-index: 1;
top: 2px;
left: 11px;
color: #77d5f1;
font-size: 24px;
line-height: 1;
}
.eyebrow {
margin: 0 0 3px;
color: #778078;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 18px;
line-height: 1.1;
letter-spacing: -0.025em;
}
.top-status {
display: flex;
gap: 8px;
align-items: center;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.44);
border-radius: 14px;
background: rgba(251, 248, 235, 0.88);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
color: #506058;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #6da84e;
box-shadow: 0 0 0 4px rgba(109, 168, 78, 0.13);
}
#panel {
position: fixed;
z-index: 20;
top: 100px;
left: 22px;
width: 304px;
max-height: calc(100vh - 180px);
overflow: auto;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.48);
border-radius: 20px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: rgba(48, 67, 54, 0.25) transparent;
transition: transform 180ms ease, opacity 180ms ease;
}
.panel-title {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 5px;
}
.panel-title h2 {
margin: 0;
font-size: 15px;
letter-spacing: -0.015em;
}
.mini-badge {
padding: 4px 7px;
border-radius: 999px;
background: #e8eddc;
color: #5b704f;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel-copy {
margin: 0 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
margin: 17px 0 9px;
color: #7a837c;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-label::after {
content: "";
flex: 1;
height: 1px;
margin-left: 10px;
background: var(--line);
}
.patch-list {
display: grid;
gap: 8px;
}
.patch-card {
padding: 10px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.48);
transition: border-color 150ms ease, background 150ms ease,
transform 150ms ease;
}
.patch-card.active {
border-color: rgba(72, 185, 230, 0.75);
background: rgba(226, 247, 252, 0.76);
transform: translateX(2px);
}
.patch-focus {
width: 100%;
padding: 0;
border: 0;
background: none;
text-align: left;
cursor: pointer;
}
.patch-row {
display: flex;
align-items: center;
gap: 9px;
}
.rain-dot {
flex: 0 0 auto;
width: 12px;
height: 12px;
border: 3px solid #d9f3fb;
border-radius: 50%;
background: var(--blue);
box-shadow: 0 0 0 1px rgba(35, 102, 130, 0.18);
}
.patch-name {
flex: 1;
font-size: 12px;
font-weight: 780;
}
.patch-coords {
color: #7b877f;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
white-space: nowrap;
}
.intensity {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 8px;
margin-top: 9px;
color: #778179;
font-size: 9px;
font-weight: 700;
}
.intensity input {
width: 100%;
height: 4px;
margin: 0;
accent-color: var(--blue);
cursor: pointer;
}
.toggle-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.toggle {
display: flex;
align-items: center;
gap: 8px;
padding: 9px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.38);
color: #4d5b52;
font-size: 11px;
font-weight: 700;
cursor: pointer;
}
.toggle input {
width: 14px;
height: 14px;
margin: 0;
accent-color: #537d54;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.action {
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.56);
color: #405047;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.025em;
cursor: pointer;
transition: background 140ms ease, transform 140ms ease;
}
.action:hover {
background: white;
transform: translateY(-1px);
}
.action.primary {
border-color: #273c30;
background: #273c30;
color: #f6f3df;
}
.legend {
display: grid;
gap: 8px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #68756d;
font-size: 10px;
}
.legend-swatch {
width: 23px;
height: 8px;
border-radius: 99px;
}
.legend-swatch.rain {
height: 12px;
background: repeating-linear-gradient(
90deg,
#54c2e9 0,
#54c2e9 2px,
transparent 2px,
transparent 6px
);
}
.legend-swatch.route {
background: repeating-linear-gradient(
90deg,
#d6a543 0,
#d6a543 6px,
transparent 6px,
transparent 9px
);
}
.legend-swatch.herd {
background: #65513e;
}
.panel-note {
margin: 12px 0 0;
padding-top: 11px;
border-top: 1px solid var(--line);
color: #879088;
font-size: 9px;
line-height: 1.5;
}
#hint {
position: fixed;
z-index: 10;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 12px;
padding: 10px 14px;
border: 1px solid rgba(255, 255, 255, 0.45);
border-radius: 999px;
background: rgba(251, 248, 235, 0.9);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
color: #536158;
font-size: 10px;
font-weight: 700;
white-space: nowrap;
pointer-events: none;
}
.hint-key {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 22px;
height: 20px;
padding: 0 5px;
border: 1px solid rgba(37, 57, 45, 0.17);
border-radius: 6px;
background: rgba(255, 255, 255, 0.54);
color: #32453a;
font-size: 9px;
}
#labels {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
display: flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 8px;
background: rgba(24, 50, 66, 0.78);
box-shadow: 0 5px 18px rgba(17, 36, 47, 0.2);
color: white;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
transform: translate(-50%, -100%);
backdrop-filter: blur(6px);
white-space: nowrap;
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -6px;
width: 1px;
height: 6px;
background: rgba(255, 255, 255, 0.65);
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #e8e4ca;
color: #304235;
transition: opacity 350ms ease, visibility 350ms ease;
}
#loading.hidden {
opacity: 0;
visibility: hidden;
}
.loader {
display: grid;
justify-items: center;
gap: 12px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.loader-cubes {
display: flex;
gap: 5px;
}
.loader-cubes i {
width: 10px;
height: 10px;
background: #4d9360;
animation: cube 900ms ease-in-out infinite alternate;
}
.loader-cubes i:nth-child(2) {
animation-delay: 150ms;
}
.loader-cubes i:nth-child(3) {
animation-delay: 300ms;
}
@keyframes cube {
to {
transform: translateY(-9px);
background: #57b9df;
}
}
#panel-toggle {
display: none;
position: fixed;
z-index: 30;
top: 20px;
right: 20px;
width: 40px;
height: 40px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 12px;
background: var(--paper);
box-shadow: var(--shadow);
cursor: pointer;
}
@media (max-width: 760px) {
.topbar {
top: 14px;
left: 14px;
right: 14px;
}
.brand {
padding: 10px;
}
.brand-copy .eyebrow {
display: none;
}
h1 {
font-size: 14px;
}
.top-status {
display: none;
}
#panel {
top: 74px;
left: 12px;
width: min(304px, calc(100vw - 24px));
max-height: calc(100vh - 148px);
transform: translateX(calc(-100% - 20px));
opacity: 0;
pointer-events: none;
}
#panel.open {
transform: translateX(0);
opacity: 1;
pointer-events: auto;
}
#panel-toggle {
display: block;
}
#hint {
bottom: 14px;
max-width: calc(100vw - 24px);
gap: 7px;
overflow: hidden;
text-overflow: ellipsis;
}
.hint-secondary {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<div id="labels"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div class="brand-copy">
<p class="eyebrow">Fictional savanna laboratory</p>
<h1>Rainfall & Herd Routes</h1>
</div>
</div>
<div class="top-status">
<span class="live-dot"></span>
<span id="simulation-time">Day 01 · 06:30</span>
<span>·</span>
<span>24 animals</span>
</div>
</header>
<aside id="panel">
<div class="panel-title">
<h2>Rain cells</h2>
<span class="mini-badge">Drag in world</span>
</div>
<p class="panel-copy">
Reposition rainfall to pull each herd onto a new grazing circuit.
Moisture around each cell briefly greens the terrain.
</p>
<div class="section-label">Active cells</div>
<div id="patch-list" class="patch-list"></div>
<div class="section-label">Map layers</div>
<div class="toggle-grid">
<label class="toggle">
<input id="rain-toggle" type="checkbox" checked />
Rainfall
</label>
<label class="toggle">
<input id="route-toggle" type="checkbox" checked />
Routes
</label>
<label class="toggle">
<input id="herd-toggle" type="checkbox" checked />
Herds
</label>
<label class="toggle">
<input id="label-toggle" type="checkbox" checked />
Labels
</label>
</div>
<div class="section-label">Experiment</div>
<div class="button-row">
<button id="pause-button" class="action primary">Pause herds</button>
<button id="reset-button" class="action">Reset map</button>
<button id="shuffle-button" class="action">Shuffle rain</button>
<button id="focus-button" class="action">Focus herd</button>
</div>
<div class="section-label">Legend</div>
<div class="legend">
<div class="legend-row">
<span class="legend-swatch rain"></span>
Movable voxel rain cell
</div>
<div class="legend-row">
<span class="legend-swatch route"></span>
Simplified grazing circuit
</div>
<div class="legend-row">
<span class="legend-swatch herd"></span>
Voxel wildebeest herd
</div>
</div>
<p class="panel-note">
Drag empty ground to orbit · Right-drag to pan · Scroll to zoom<br />
Keyboard: Space to pause · R to reset · 1–3 to focus a cell
</p>
</aside>
<button id="panel-toggle" aria-label="Toggle controls">☰</button>
<div id="hint">
<span class="hint-key">DRAG</span>
<span>Move a blue rain patch</span>
<span class="hint-secondary">·</span>
<span class="hint-secondary">Right-drag to pan · Scroll to zoom</span>
</div>
<div id="loading">
<div class="loader">
<div class="loader-cubes"><i></i><i></i><i></i></div>
Building the savanna
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.querySelector("#scene");
const labelHost = document.querySelector("#labels");
const panel = document.querySelector("#panel");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance",
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
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;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9d7b9);
scene.fog = new THREE.Fog(0xd9d7b9, 38, 78);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
140
);
const hemisphere = new THREE.HemisphereLight(0xeaf4ed, 0x725b36, 2.05);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe8b2, 3.25);
sun.position.set(-15, 26, 14);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -31;
sun.shadow.camera.right = 31;
sun.shadow.camera.top = 27;
sun.shadow.camera.bottom = -27;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.bias = -0.00035;
scene.add(sun);
const warmFill = new THREE.DirectionalLight(0xffd7a0, 0.65);
warmFill.position.set(18, 10, -20);
scene.add(warmFill);
const WORLD = {
cols: 38,
rows: 30,
cell: 1.25,
width: 38 * 1.25,
depth: 30 * 1.25,
bottom: -1.35,
};
function clamp(value, min, max) {
return Math.min(max, Math.max(min, value));
}
function terrainHeight(x, z) {
const broad =
Math.sin(x * 0.22) * 0.48 +
Math.cos(z * 0.19) * 0.36 +
Math.sin((x + z) * 0.105) * 0.32 +
Math.cos((x - z) * 0.15) * 0.17;
const detail = Math.sin(x * 0.56 + z * 0.23) * 0.09;
const h = 0.62 + broad + detail;
return Math.round(Math.max(-0.15, h) * 4) / 4;
}
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 random = mulberry32(73421);
const dummy = new THREE.Object3D();
const terrainCells = [];
const moistureColor = new THREE.Color(0x567f45);
const terrain = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0,
}),
WORLD.cols * WORLD.rows
);
terrain.receiveShadow = true;
let terrainIndex = 0;
for (let row = 0; row < WORLD.rows; row++) {
for (let col = 0; col < WORLD.cols; col++) {
const x = (col - (WORLD.cols - 1) / 2) * WORLD.cell;
const z = (row - (WORLD.rows - 1) / 2) * WORLD.cell;
const top = terrainHeight(x, z);
const height = top - WORLD.bottom;
const centerY = (top + WORLD.bottom) / 2;
dummy.position.set(x, centerY, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(WORLD.cell * 0.965, height, WORLD.cell * 0.965);
dummy.updateMatrix();
terrain.setMatrixAt(terrainIndex, dummy.matrix);
const grain = random() * 0.1 - 0.05;
const dry = new THREE.Color(0xc6a35a);
const green = new THREE.Color(0x829650);
const high = new THREE.Color(0xb28b4f);
const color = dry.clone().lerp(green, clamp((top + 0.1) * 0.68, 0, 0.62));
color.lerp(high, clamp((top - 0.65) * 0.34, 0, 0.2));
color.offsetHSL(grain * 0.1, grain * 0.18, grain);
terrain.setColorAt(terrainIndex, color);
terrainCells.push({ x, z, color: color.clone() });
terrainIndex++;
}
}
terrain.instanceMatrix.needsUpdate = true;
terrain.instanceColor.needsUpdate = true;
scene.add(terrain);
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(WORLD.width + 1.2, 1.1, WORLD.depth + 1.2),
new THREE.MeshStandardMaterial({
color: 0x70553a,
roughness: 1,
})
);
plinth.position.y = WORLD.bottom - 0.52;
plinth.receiveShadow = true;
scene.add(plinth);
// Small voxel grass strokes.
const grassCount = 440;
const grass = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.055, 0.38, 0.055),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
}),
grassCount
);
for (let i = 0; i < grassCount; i++) {
const x = (random() - 0.5) * (WORLD.width - 1.4);
const z = (random() - 0.5) * (WORLD.depth - 1.4);
const y = terrainHeight(x, z);
dummy.position.set(x, y + 0.16, z);
dummy.rotation.set(
(random() - 0.5) * 0.15,
random() * Math.PI,
(random() - 0.5) * 0.15
);
dummy.scale.set(1, 0.65 + random() * 0.8, 1);
dummy.updateMatrix();
grass.setMatrixAt(i, dummy.matrix);
const color = new THREE.Color(0x738647).lerp(
new THREE.Color(0xa59b51),
random() * 0.6
);
grass.setColorAt(i, color);
}
grass.instanceMatrix.needsUpdate = true;
grass.instanceColor.needsUpdate = true;
grass.castShadow = true;
scene.add(grass);
function addBox(parent, size, position, material, rotation = null) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
if (rotation) mesh.rotation.set(rotation.x, rotation.y, rotation.z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x65442d,
roughness: 1,
});
const canopyMaterial = new THREE.MeshStandardMaterial({
color: 0x5d743e,
roughness: 1,
});
const canopyLightMaterial = new THREE.MeshStandardMaterial({
color: 0x768b49,
roughness: 1,
});
function createTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, terrainHeight(x, z), z);
tree.scale.setScalar(scale);
addBox(
tree,
new THREE.Vector3(0.28, 1.8, 0.28),
new THREE.Vector3(0, 0.9, 0),
trunkMaterial
);
addBox(
tree,
new THREE.Vector3(1.55, 0.44, 1.1),
new THREE.Vector3(0, 1.87, 0),
canopyMaterial
);
addBox(
tree,
new THREE.Vector3(1.05, 0.48, 1.45),
new THREE.Vector3(0.28, 2.12, -0.04),
canopyLightMaterial
);
addBox(
tree,
new THREE.Vector3(0.7, 0.38, 0.72),
new THREE.Vector3(-0.4, 2.08, 0.08),
canopyMaterial
);
scene.add(tree);
}
[
[-16, -9, 1.12],
[-13, 11, 0.9],
[-2, -12, 1.0],
[5, 12, 1.18],
[15, 7, 0.92],
[17, -10, 1.08],
[11, -3, 0.72],
[-7, 0, 0.68],
[-18, 2, 0.76],
].forEach((tree) => createTree(...tree));
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x817965,
roughness: 1,
});
[
[-5.5, -9.5, 0.9],
[12.5, 3.5, 0.7],
[-17.5, 5.8, 0.65],
[3.5, -8.2, 0.55],
[18, 13, 0.75],
].forEach(([x, z, scale]) => {
const rock = new THREE.Group();
rock.position.set(x, terrainHeight(x, z), z);
rock.rotation.y = random() * Math.PI;
addBox(
rock,
new THREE.Vector3(1.1, 0.55, 0.8),
new THREE.Vector3(0, 0.23, 0),
rockMaterial
);
addBox(
rock,
new THREE.Vector3(0.65, 0.38, 0.55),
new THREE.Vector3(0.45, 0.5, 0.1),
rockMaterial
);
rock.scale.setScalar(scale);
scene.add(rock);
});
const patchDefinitions = [
{
id: "R1",
name: "Long-grass shower",
color: 0x55c4ec,
routeColor: 0xd5a43f,
x: -9.8,
z: 4.1,
intensity: 0.82,
phase: 0.4,
routePhase: 0.18,
routeX: 1.15,
routeZ: 0.9,
},
{
id: "R2",
name: "River-bend cells",
color: 0x4aaee0,
routeColor: 0xd8794f,
x: 9.4,
z: -5.6,
intensity: 0.68,
phase: 2.1,
routePhase: 1.05,
routeX: 0.9,
routeZ: 1.16,
},
{
id: "R3",
name: "Acacia drizzle",
color: 0x78d1ef,
routeColor: 0x87ad4d,
x: 1.3,
z: 10.2,
intensity: 0.55,
phase: 4.3,
routePhase: 2.2,
routeX: 1.08,
routeZ: 0.86,
},
];
const patches = [];
const patchHitTargets = [];
const MAX_RAIN_STREAKS = 76;
function createPatch(definition, index) {
const patch = { ...definition, index };
patch.group = new THREE.Group();
patch.group.position.set(
patch.x,
terrainHeight(patch.x, patch.z) + 0.03,
patch.z
);
scene.add(patch.group);
patch.cloudGroup = new THREE.Group();
patch.group.add(patch.cloudGroup);
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0x9bb9c7,
roughness: 1,
transparent: true,
opacity: 0.58,
depthWrite: false,
});
const cloudMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
cloudMaterial,
15
);
const cloudRandom = mulberry32(900 + index * 53);
for (let i = 0; i < 15; i++) {
const x = (cloudRandom() - 0.5) * 4.3;
const z = (cloudRandom() - 0.5) * 3.1;
const y = 4.35 + cloudRandom() * 0.72;
dummy.position.set(x, y, z);
dummy.rotation.set(0, cloudRandom() * 0.18, 0);
dummy.scale.set(
0.9 + cloudRandom() * 1.25,
0.46 + cloudRandom() * 0.28,
0.75 + cloudRandom() * 0.9
);
dummy.updateMatrix();
cloudMesh.setMatrixAt(i, dummy.matrix);
}
cloudMesh.instanceMatrix.needsUpdate = true;
cloudMesh.computeBoundingSphere();
cloudMesh.userData.patch = patch;
patch.cloudGroup.add(cloudMesh);
patch.rainMaterial = new THREE.MeshBasicMaterial({
color: patch.color,
transparent: true,
opacity: 0.68,
depthWrite: false,
});
patch.rainMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
patch.rainMaterial,
MAX_RAIN_STREAKS
);
patch.rainMesh.userData.patch = patch;
patch.rainMesh.frustumCulled = false;
patch.group.add(patch.rainMesh);
patch.rainSeeds = [];
const rainRandom = mulberry32(1400 + index * 91);
for (let i = 0; i < MAX_RAIN_STREAKS; i++) {
const angle = rainRandom() * Math.PI * 2;
const radius = Math.sqrt(rainRandom()) * 2.75;
patch.rainSeeds.push({
x: Math.cos(angle) * radius,
z: Math.sin(angle) * radius,
phase: rainRandom(),
speed: 0.34 + rainRandom() * 0.33,
length: 0.48 + rainRandom() * 0.78,
});
}
patch.rainMesh.count = Math.max(
8,
Math.floor(MAX_RAIN_STREAKS * patch.intensity)
);
patch.footprintMaterial = new THREE.MeshBasicMaterial({
color: patch.color,
transparent: true,
opacity: 0.34,
depthWrite: false,
});
patch.footprintMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.22, 0.035, 0.22),
patch.footprintMaterial,
92
);
patch.footprintMesh.userData.patch = patch;
const footRandom = mulberry32(2200 + index * 77);
for (let i = 0; i < 92; i++) {
const angle = footRandom() * Math.PI * 2;
const radius = Math.sqrt(footRandom()) * 2.9;
dummy.position.set(
Math.cos(angle) * radius,
0.025,
Math.sin(angle) * radius
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
patch.footprintMesh.setMatrixAt(i, dummy.matrix);
}
patch.footprintMesh.instanceMatrix.needsUpdate = true;
patch.footprintMesh.computeBoundingSphere();
patch.group.add(patch.footprintMesh);
patch.routeLine = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineDashedMaterial({
color: patch.routeColor,
dashSize: 0.42,
gapSize: 0.28,
transparent: true,
opacity: 0.9,
})
);
patch.routeLine.renderOrder = 2;
scene.add(patch.routeLine);
const label = document.createElement("div");
label.className = "world-label";
label.innerHTML = `<span>☂</span><span>${patch.id} · ${Math.round(
patch.intensity * 100
)}%</span>`;
labelHost.appendChild(label);
patch.label = label;
patchHitTargets.push(
cloudMesh,
patch.rainMesh,
patch.footprintMesh
);
patches.push(patch);
return patch;
}
patchDefinitions.forEach(createPatch);
function rebuildRoute(patch) {
const controls = [];
for (let i = 0; i < 8; i++) {
const angle =
(i / 8) * Math.PI * 2 + patch.routePhase + patch.phase * 0.03;
const radius =
3.65 + Math.sin(i * 1.72 + patch.phase) * 0.75;
const x = patch.x + Math.cos(angle) * radius * patch.routeX;
const z = patch.z + Math.sin(angle) * radius * patch.routeZ;
controls.push(
new THREE.Vector3(x, terrainHeight(x, z) + 0.13, z)
);
}
patch.curve = new THREE.CatmullRomCurve3(
controls,
true,
"catmullrom",
0.45
);
const points = [];
for (let i = 0; i < 140; i++) {
const point = patch.curve.getPoint(i / 140);
point.y = Math.max(
point.y,
terrainHeight(point.x, point.z) + 0.12
);
points.push(point);
}
patch.routeLine.geometry.dispose();
patch.routeLine.geometry = new THREE.BufferGeometry().setFromPoints(
points
);
patch.routeLine.computeLineDistances();
}
patches.forEach(rebuildRoute);
function createAnimal(material, accentMaterial, scale = 1) {
const animal = new THREE.Group();
addBox(
animal,
new THREE.Vector3(0.5, 0.3, 0.78),
new THREE.Vector3(0, 0.56, 0),
material
);
addBox(
animal,
new THREE.Vector3(0.32, 0.35, 0.28),
new THREE.Vector3(0, 0.67, 0.43),
material
);
addBox(
animal,
new THREE.Vector3(0.24, 0.2, 0.2),
new THREE.Vector3(0, 0.62, 0.61),
accentMaterial
);
const legPositions = [
[-0.17, 0.25, -0.25],
[0.17, 0.25, -0.25],
[-0.17, 0.25, 0.25],
[0.17, 0.25, 0.25],
];
legPositions.forEach((position) => {
addBox(
animal,
new THREE.Vector3(0.075, 0.36, 0.075),
new THREE.Vector3(...position),
accentMaterial
);
});
addBox(
animal,
new THREE.Vector3(0.045, 0.22, 0.045),
new THREE.Vector3(-0.1, 0.87, 0.53),
accentMaterial,
new THREE.Euler(0, 0, -0.22)
);
addBox(
animal,
new THREE.Vector3(0.045, 0.22, 0.045),
new THREE.Vector3(0.1, 0.87, 0.53),
accentMaterial,
new THREE.Euler(0, 0, 0.22)
);
addBox(
animal,
new THREE.Vector3(0.055, 0.3, 0.055),
new THREE.Vector3(0, 0.5, -0.45),
accentMaterial,
new THREE.Euler(-0.3, 0, 0)
);
animal.scale.setScalar(scale);
animal.userData.bobOffset = Math.random() * Math.PI * 2;
return animal;
}
function createHerd(patch) {
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0x654b36,
roughness: 0.95,
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: patch.routeColor,
roughness: 0.9,
});
patch.herdGroup = new THREE.Group();
scene.add(patch.herdGroup);
patch.herdMembers = [];
for (let i = 0; i < 8; i++) {
const animal = createAnimal(
bodyMaterial,
accentMaterial,
i === 0 ? 0.92 : 0.72 + Math.random() * 0.1
);
patch.herdGroup.add(animal);
patch.herdMembers.push({
mesh: animal,
offset: i / 8,
});
}
}
patches.forEach(createHerd);
const selectionGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-3.18, 0, -3.18),
new THREE.Vector3(3.18, 0, -3.18),
new THREE.Vector3(3.18, 0, 3.18),
new THREE.Vector3(-3.18, 0, 3.18),
]);
const selectionMaterial = new THREE.LineBasicMaterial({
color: 0xf8f1bd,
transparent: true,
opacity: 0.95,
});
const selectionRing = new THREE.LineLoop(
selectionGeometry,
selectionMaterial
);
scene.add(selectionRing);
let selectedPatch = patches[0];
selectionRing.visible = true;
function selectPatch(patch, focus = true) {
selectedPatch = patch;
patches.forEach((item) => {
item.card?.classList.toggle("active", item === patch);
});
if (focus) {
desiredTarget.set(patch.x, 0.8, patch.z);
desiredRadius = 20;
}
}
function updateTerrainMoisture() {
const color = new THREE.Color();
terrainCells.forEach((cell, index) => {
color.copy(cell.color);
let moisture = 0;
patches.forEach((patch) => {
const distance = Math.hypot(
cell.x - patch.x,
cell.z - patch.z
);
const localMoisture = clamp(1 - distance / 4.3, 0, 1);
moisture = Math.max(moisture, localMoisture);
});
color.lerp(moistureColor, moisture * 0.58);
terrain.setColorAt(index, color);
});
terrain.instanceColor.needsUpdate = true;
}
const patchList = document.querySelector("#patch-list");
patches.forEach((patch) => {
const card = document.createElement("div");
card.className = "patch-card";
card.innerHTML = `
<button class="patch-focus" type="button">
<span class="patch-row">
<span class="rain-dot"></span>
<span class="patch-name">${patch.name}</span>
<span class="patch-coords"></span>
</span>
</button>
<label class="intensity">
<span>RAIN</span>
<input type="range" min="20" max="100" step="1" />
<span class="intensity-value">${Math.round(
patch.intensity * 100
)}%</span>
</label>
`;
const range = card.querySelector("input");
const coords = card.querySelector(".patch-coords");
const value = card.querySelector(".intensity-value");
range.value = Math.round(patch.intensity * 100);
card.querySelector(".patch-focus").addEventListener("click", () => {
selectPatch(patch, true);
});
range.addEventListener("input", () => {
patch.intensity = Number(range.value) / 100;
patch.rainMesh.count = Math.max(
8,
Math.floor(MAX_RAIN_STREAKS * patch.intensity)
);
value.textContent = `${range.value}%`;
patch.label.innerHTML = `<span>☂</span><span>${patch.id} · ${range.value}%</span>`;
});
range.addEventListener("click", (event) => {
event.stopPropagation();
});
patch.card = card;
patch.coordElement = coords;
patch.intensityValueElement = value;
patchList.appendChild(card);
updatePatchCard(patch);
});
function updatePatchCard(patch) {
patch.coordElement.textContent = `${patch.x.toFixed(1)}, ${patch.z.toFixed(1)}`;
}
selectPatch(patches[0], false);
// Custom orbit, pan, zoom, and patch dragging.
const cameraState = {
target: new THREE.Vector3(0, 0.7, 0),
desiredTarget: new THREE.Vector3(0, 0.7, 0),
radius: 34,
desiredRadius: 34,
theta: 0.64,
desiredTheta: 0.64,
phi: 0.82,
desiredPhi: 0.82,
};
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragState = null;
let moistureDirty = false;
function setPointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pickPatch(event) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(
patchHitTargets,
false
);
return intersections.length ? intersections[0].object.userData.patch : null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
if (event.button === 0) {
const patch = pickPatch(event);
if (patch) {
selectPatch(patch, false);
const normal = new THREE.Vector3();
camera.getWorldDirection(normal);
const plane = new THREE.Plane().setFromNormalAndCoplanarPoint(
normal,
patch.group.position
);
const hit = new THREE.Vector3();
raycaster.setFromCamera(pointer, camera);
raycaster.ray.intersectPlane(plane, hit);
dragState = {
type: "patch",
patch,
plane,
offset: new THREE.Vector3(
patch.group.position.x - hit.x,
0,
patch.group.position.z - hit.z
),
};
renderer.domElement.style.cursor = "move";
return;
}
dragState = {
type: "orbit",
x: event.clientX,
y: event.clientY,
};
renderer.domElement.style.cursor = "grabbing";
return;
}
if (event.button === 2) {
dragState = {
type: "pan",
x: event.clientX,
y: event.clientY,
};
renderer.domElement.style.cursor = "grabbing";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragState) {
const patch = pickPatch(event);
renderer.domElement.style.cursor = patch ? "move" : "grab";
return;
}
if (dragState.type === "orbit") {
const dx = event.clientX - dragState.x;
const dy = event.clientY - dragState.y;
cameraState.desiredTheta -= dx * 0.006;
cameraState.desiredPhi = clamp(
cameraState.desiredPhi - dy * 0.005,
0.35,
1.32
);
dragState.x = event.clientX;
dragState.y = event.clientY;
}
if (dragState.type === "pan") {
const dx = event.clientX - dragState.x;
const dy = event.clientY - dragState.y;
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(camera.up, forward)
.normalize();
const scale = cameraState.desiredRadius * 0.00155;
cameraState.desiredTarget.addScaledVector(
right,
-dx * scale
);
cameraState.desiredTarget.addScaledVector(
forward,
dy * scale
);
cameraState.desiredTarget.x = clamp(
cameraState.desiredTarget.x,
-WORLD.width / 2,
WORLD.width / 2
);
cameraState.desiredTarget.z = clamp(
cameraState.desiredTarget.z,
-WORLD.depth / 2,
WORLD.depth / 2
);
dragState.x = event.clientX;
dragState.y = event.clientY;
}
if (dragState.type === "patch") {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hit = new THREE.Vector3();
if (raycaster.ray.intersectPlane(dragState.plane, hit)) {
const patch = dragState.patch;
patch.x = clamp(
hit.x + dragState.offset.x,
-WORLD.width / 2 + 3.2,
WORLD.width / 2 - 3.2
);
patch.z = clamp(
hit.z + dragState.offset.z,
-WORLD.depth / 2 + 3.2,
WORLD.depth / 2 - 3.2
);
patch.group.position.set(
patch.x,
terrainHeight(patch.x, patch.z) + 0.03,
patch.z
);
rebuildRoute(patch);
updatePatchCard(patch);
moistureDirty = true;
}
}
});
function endDrag(event) {
dragState = null;
renderer.domElement.style.cursor = "grab";
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = clamp(
cameraState.desiredRadius + event.deltaY * 0.025,
12,
53
);
},
{ passive: false }
);
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-7 * dt);
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
cameraState.desiredTheta,
smoothing
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
cameraState.desiredPhi,
smoothing
);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraState.desiredRadius,
smoothing
);
cameraState.target.lerp(
cameraState.desiredTarget,
smoothing
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
const rainToggle = document.querySelector("#rain-toggle");
const routeToggle = document.querySelector("#route-toggle");
const herdToggle = document.querySelector("#herd-toggle");
const labelToggle = document.querySelector("#label-toggle");
rainToggle.addEventListener("change", () => {
patches.forEach((patch) => {
patch.group.visible = rainToggle.checked;
});
});
routeToggle.addEventListener("change", () => {
patches.forEach((patch) => {
patch.routeLine.visible = routeToggle.checked;
});
});
herdToggle.addEventListener("change", () => {
patches.forEach((patch) => {
patch.herdGroup.visible = herdToggle.checked;
});
});
let herdsRunning = true;
const pauseButton = document.querySelector("#pause-button");
function togglePause() {
herdsRunning = !herdsRunning;
pauseButton.textContent = herdsRunning ? "Pause herds" : "Resume herds";
}
pauseButton.addEventListener("click", togglePause);
function resetPatches() {
patchDefinitions.forEach((definition, index) => {
const patch = patches[index];
patch.x = definition.x;
patch.z = definition.z;
patch.intensity = definition.intensity;
patch.group.position.set(
patch.x,
terrainHeight(patch.x, patch.z) + 0.03,
patch.z
);
patch.rainMesh.count = Math.max(
8,
Math.floor(MAX_RAIN_STREAKS * patch.intensity)
);
const range = patch.card.querySelector("input");
range.value = Math.round(patch.intensity * 100);
patch.intensityValueElement.textContent = `${range.value}%`;
patch.label.innerHTML = `<span>☂</span><span>${patch.id} · ${range.value}%</span>`;
rebuildRoute(patch);
updatePatchCard(patch);
});
moistureDirty = true;
}
document.querySelector("#reset-button").addEventListener("click", resetPatches);
document.querySelector("#shuffle-button").addEventListener("click", () => {
patches.forEach((patch, index) => {
const angle = random() * Math.PI * 2;
const radius = 7 + random() * 8;
patch.x = clamp(
Math.cos(angle) * radius,
-WORLD.width / 2 + 4,
WORLD.width / 2 - 4
);
patch.z = clamp(
Math.sin(angle) * radius,
-WORLD.depth / 2 + 4,
WORLD.depth / 2 - 4
);
patch.group.position.set(
patch.x,
terrainHeight(patch.x, patch.z) + 0.03,
patch.z
);
rebuildRoute(patch);
updatePatchCard(patch);
});
moistureDirty = true;
});
document.querySelector("#focus-button").addEventListener("click", () => {
selectPatch(selectedPatch, true);
});
document.querySelector("#panel-toggle").addEventListener("click", () => {
panel.classList.toggle("open");
});
window.addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "INPUT" || tag === "TEXTAREA") return;
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "r") {
resetPatches();
}
if (["1", "2", "3"].includes(event.key)) {
const patch = patches[Number(event.key) - 1];
if (patch) selectPatch(patch, true);
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
});
const projected = new THREE.Vector3();
let elapsed = 0;
let lastMoistureUpdate = -1;
const clock = new THREE.Clock();
function updateRain(time) {
patches.forEach((patch) => {
patch.cloudGroup.position.y =
Math.sin(time * 0.7 + patch.phase) * 0.08;
patch.rainSeeds.forEach((seed, index) => {
const cycle = (time * seed.speed + seed.phase) % 1;
const y = 4.18 - cycle * 3.95;
const shimmer = 0.82 + Math.sin(time * 5 + seed.phase * 9) * 0.18;
dummy.position.set(seed.x, y, seed.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(
0.052,
seed.length * shimmer,
0.052
);
dummy.updateMatrix();
patch.rainMesh.setMatrixAt(index, dummy.matrix);
});
patch.rainMesh.instanceMatrix.needsUpdate = true;
});
}
function updateHerds(time) {
if (!herdsRunning) return;
patches.forEach((patch) => {
patch.herdMembers.forEach((member, index) => {
const speed = index === 0 ? 0.021 : 0.017;
const t = (time * speed + member.offset) % 1;
const nextT = (t + 0.006) % 1;
const point = patch.curve.getPointAt(t);
const nextPoint = patch.curve.getPointAt(nextT);
const bob =
Math.sin(time * 5.5 + member.mesh.userData.bobOffset) *
0.025;
member.mesh.position.set(point.x, point.y + bob, point.z);
const dx = nextPoint.x - point.x;
const dz = nextPoint.z - point.z;
member.mesh.rotation.y = Math.atan2(dx, dz);
});
});
}
function updateLabels() {
patches.forEach((patch) => {
if (!labelToggle.checked || !rainToggle.checked) {
patch.label.style.display = "none";
return;
}
projected.set(patch.x, patch.group.position.y + 5.65, patch.z);
projected.project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
patch.label.style.display = visible ? "flex" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
patch.label.style.left = `${x}px`;
patch.label.style.top = `${y}px`;
}
});
}
function updateSelection(time) {
if (!selectedPatch) return;
selectionRing.position.set(
selectedPatch.x,
terrainHeight(selectedPatch.x, selectedPatch.z) + 0.09,
selectedPatch.z
);
selectionMaterial.opacity =
0.65 + Math.sin(time * 2.4) * 0.22;
}
function updateClock() {
const totalMinutes = 6 * 60 + 30 + Math.floor(elapsed * 3);
const day = Math.floor(totalMinutes / 1440) + 1;
const minuteOfDay = totalMinutes % 1440;
const hours = Math.floor(minuteOfDay / 60);
const minutes = minuteOfDay % 60;
document.querySelector("#simulation-time").textContent =
`Day ${String(day).padStart(2, "0")} · ` +
`${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`;
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
updateCamera(dt);
updateRain(elapsed);
updateHerds(elapsed);
updateSelection(elapsed);
updateLabels();
updateClock();
if (moistureDirty && elapsed - lastMoistureUpdate > 0.12) {
updateTerrainMoisture();
moistureDirty = false;
lastMoistureUpdate = elapsed;
}
renderer.render(scene, camera);
}
updateTerrainMoisture();
updateCamera(1);
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```