Prompt specification
Build or customize the Three.js 3D scene "Habitat corridor genetics diorama" 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: Habitat corridor genetics diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/habitat-corridor-genetics-diorama-x0911
Scene Description Prompt:
Habitat corridor genetics diorama. Compose two woodland patches joined by an optional corridor and fictional mobile animals; illustrate gene-flow opportunities without claiming real corridor effectiveness from geometry alone. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (habitat-corridor-genetics-diorama-x0911):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Habitat Corridor Genetics Diorama</title>
<style>
:root {
--ink: #21342c;
--muted: #5e7068;
--panel: rgba(255, 252, 240, 0.94);
--line: rgba(33, 52, 44, 0.16);
--blue: #36b9ef;
--coral: #ff6f61;
--purple: #8b68d9;
--shadow: 0 18px 50px rgba(36, 55, 43, 0.17);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe8dd;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#scene {
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.header {
position: fixed;
top: 22px;
left: 24px;
z-index: 5;
width: min(490px, calc(100vw - 390px));
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: #4e725f;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.02;
letter-spacing: -0.04em;
text-wrap: balance;
}
.subtitle {
max-width: 460px;
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.controls {
position: fixed;
top: 20px;
right: 20px;
z-index: 10;
width: 326px;
padding: 17px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
}
.control-title strong {
font-size: 14px;
}
.concept-pill {
padding: 4px 7px;
border-radius: 999px;
background: #e6efe5;
color: #52705d;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.switch-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
padding: 10px 0;
border-top: 1px solid var(--line);
}
.switch-row:first-of-type {
border-top: 0;
}
.switch-row label {
display: block;
font-size: 12px;
font-weight: 750;
}
.small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
input[type="checkbox"] {
width: 18px;
height: 18px;
flex: 0 0 auto;
accent-color: #4f8d68;
}
input[type="range"] {
width: 100%;
margin: 8px 0 2px;
accent-color: #4f8d68;
}
.range-labels {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
}
.buttons {
display: flex;
gap: 8px;
margin-top: 12px;
}
button {
flex: 1;
padding: 9px 10px;
border: 1px solid #cbd9cc;
border-radius: 10px;
background: #f8faf2;
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
}
button:hover {
background: #edf4e8;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 14px;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.status {
min-height: 54px;
margin-top: 12px;
padding: 10px 11px;
border-radius: 11px;
background: #eaf3e8;
color: #345a43;
font-size: 10px;
line-height: 1.45;
}
.status.exchange {
background: #f1eafa;
color: #594180;
}
.event-log {
margin-top: 10px;
color: var(--muted);
font-size: 9px;
line-height: 1.45;
}
.event-log div {
margin-top: 3px;
}
.note {
position: fixed;
left: 24px;
bottom: 22px;
z-index: 5;
width: min(410px, calc(100vw - 48px));
padding: 14px 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(255, 252, 240, 0.92);
box-shadow: var(--shadow);
pointer-events: none;
backdrop-filter: blur(12px);
}
.note strong {
display: block;
margin-bottom: 5px;
font-size: 11px;
letter-spacing: 0.02em;
}
.note p {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.help {
position: fixed;
right: 22px;
bottom: 20px;
z-index: 5;
padding: 8px 11px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.72);
color: #51635a;
font-size: 9px;
pointer-events: none;
}
.world-label {
position: fixed;
z-index: 4;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 9px;
background: rgba(30, 48, 39, 0.82);
color: white;
font-size: 10px;
font-weight: 750;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -115%);
box-shadow: 0 8px 22px rgba(25, 42, 33, 0.16);
backdrop-filter: blur(8px);
}
.world-label.route {
background: rgba(73, 102, 73, 0.88);
}
@media (max-width: 760px) {
.header {
top: 15px;
left: 16px;
width: calc(100vw - 32px);
}
h1 {
max-width: 310px;
font-size: 27px;
}
.subtitle {
display: none;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: 310px;
max-height: 63vh;
overflow: auto;
}
.note {
display: none;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<div id="app">
<canvas id="scene" aria-label="Interactive voxel habitat corridor diorama"></canvas>
<header class="header">
<div class="eyebrow">Conceptual landscape · fictional genetics</div>
<h1>Habitat corridor genetics</h1>
<p class="subtitle">
Two woodland patches, an optional candidate route, and mobile voxel
creatures carrying symbolic alleles.
</p>
</header>
<section class="controls" aria-label="Diorama controls">
<div class="control-title">
<strong>Diorama controls</strong>
<span class="concept-pill">Concept only</span>
</div>
<div class="switch-row">
<div>
<label for="corridorToggle">Candidate corridor</label>
<span class="small">Adds an illustrative stepping route between patches.</span>
</div>
<input id="corridorToggle" type="checkbox" checked />
</div>
<div class="switch-row">
<div>
<label for="motionToggle">Run toy simulation</label>
<span class="small">Moves fictional travelers and residents.</span>
</div>
<input id="motionToggle" type="checkbox" checked />
</div>
<div class="switch-row">
<div>
<label for="markerToggle">Allele markers</label>
<span class="small">Shows two symbolic, fictional allele colors.</span>
</div>
<input id="markerToggle" type="checkbox" checked />
</div>
<div class="switch-row">
<div>
<label for="speedControl">Animation speed</label>
<span class="small">Changes timing only, not ecological conclusions.</span>
</div>
</div>
<input
id="speedControl"
type="range"
min="0.5"
max="2"
step="0.1"
value="1"
/>
<div class="range-labels">
<span>Slow</span>
<span>Fast</span>
</div>
<div class="buttons">
<button id="resetSimulation" type="button">Reset simulation</button>
<button id="resetCamera" type="button">Reset view</button>
</div>
<div class="legend">
<span><i class="swatch" style="background:#36b9ef"></i>Allele A</span>
<span><i class="swatch" style="background:#ff6f61"></i>Allele B</span>
<span><i class="swatch" style="background:#8b68d9"></i>Traveler</span>
</div>
<div id="status" class="status" role="status" aria-live="polite"></div>
<div id="eventLog" class="event-log"></div>
</section>
<div id="sourceLabel" class="world-label">Source woodland</div>
<div id="routeLabel" class="world-label route">Candidate route</div>
<div id="destinationLabel" class="world-label">Receiving woodland</div>
<aside class="note">
<strong>Geometry is not evidence of corridor effectiveness.</strong>
<p>
This fictional diorama illustrates one possible movement-and-meeting
opportunity. Its distance, width, and appearance do not predict
real-world gene flow. Actual outcomes depend on species traits,
behavior, barriers, mortality, timing, land use, and population
genetics. No effectiveness score is calculated.
</p>
</aside>
<div class="help">Drag to orbit · wheel to zoom · arrow keys to rotate</div>
</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: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8dd);
scene.fog = new THREE.Fog(0xdce8dd, 24, 48);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(0xf5fff4, 0x8a7964, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cc, 3.2);
sun.position.set(-8, 14, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9dfff, 1.1);
fill.position.set(9, 7, -8);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared voxel construction helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function getMaterial(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true
})
);
}
return materialCache.get(color);
}
function voxel(
width,
height,
depth,
color,
x,
y,
z,
parent = scene,
castShadow = true
) {
const mesh = new THREE.Mesh(
unitBox,
color?.isMaterial ? color : getMaterial(color)
);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let randomSeed = 728341;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smoothstep(value) {
const t = clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
// -------------------------------------------------------------------------
// Diorama base and tiled ground
// -------------------------------------------------------------------------
voxel(
80,
0.22,
80,
0xcfddd0,
0,
-0.72,
0,
scene,
false
);
voxel(15.4, 0.48, 8.3, 0x8a765b, 0, -0.29, 0);
voxel(15.1, 0.12, 8.0, 0xb29b73, 0, -0.02, 0);
const tileGeometry = new THREE.BoxGeometry(0.48, 0.18, 0.48);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95,
metalness: 0
});
const gridColumns = 30;
const gridRows = 16;
const tileCount = gridColumns * gridRows;
const terrain = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCount
);
terrain.receiveShadow = true;
terrain.castShadow = false;
terrain.instanceMatrix.setUsage(THREE.StaticDrawUsage);
scene.add(terrain);
const tileData = [];
const dummy = new THREE.Object3D();
function isWoodland(x, z) {
const left =
Math.pow((x + 5.1) / 2.5, 2) +
Math.pow(z / 2.95, 2) <
1;
const right =
Math.pow((x - 5.1) / 2.5, 2) +
Math.pow(z / 2.95, 2) <
1;
return left || right;
}
function isCandidatePath(x, z) {
return Math.abs(x) <= 3.25 && Math.abs(z) <= 0.76;
}
function groundColor(x, z, index) {
const variation = ((index * 17) % 7) / 100;
if (isCandidatePath(x, z)) {
const path = new THREE.Color(0xd8b97b);
path.offsetHSL(0, 0, variation * 0.35);
return path;
}
if (isWoodland(x, z)) {
const woodland = new THREE.Color(0x57985d);
woodland.offsetHSL(0, 0, variation * 0.6);
return woodland;
}
const grass = new THREE.Color(0x91c478);
grass.offsetHSL(0, 0, variation);
return grass;
}
for (let iz = 0; iz < gridRows; iz++) {
for (let ix = 0; ix < gridColumns; ix++) {
const index = iz * gridColumns + ix;
const x = (ix - (gridColumns - 1) / 2) * 0.5;
const z = (iz - (gridRows - 1) / 2) * 0.5;
dummy.position.set(x, 0.065, z);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
terrain.setMatrixAt(index, dummy.matrix);
terrain.setColorAt(index, groundColor(x, z, index));
tileData.push({ index, x, z });
}
}
terrain.instanceMatrix.needsUpdate = true;
terrain.instanceColor.needsUpdate = true;
// -------------------------------------------------------------------------
// Voxel trees and understory
// -------------------------------------------------------------------------
const trunkColors = [0x765039, 0x684735, 0x805b3d];
const leafColors = [0x2f7047, 0x397d4b, 0x438c53, 0x2d6543];
function addTree(x, z, scale = 1) {
const trunkHeight = (0.72 + random() * 0.22) * scale;
const trunkColor =
trunkColors[Math.floor(random() * trunkColors.length)];
voxel(
0.22 * scale,
trunkHeight,
0.22 * scale,
trunkColor,
x,
0.15 + trunkHeight / 2,
z
);
const canopyBase = 0.15 + trunkHeight;
voxel(
0.7 * scale,
0.55 * scale,
0.68 * scale,
leafColors[Math.floor(random() * leafColors.length)],
x,
canopyBase + 0.18 * scale,
z
);
voxel(
0.52 * scale,
0.48 * scale,
0.56 * scale,
leafColors[Math.floor(random() * leafColors.length)],
x - 0.2 * scale,
canopyBase + 0.5 * scale,
z + 0.08 * scale
);
voxel(
0.48 * scale,
0.45 * scale,
0.5 * scale,
leafColors[Math.floor(random() * leafColors.length)],
x + 0.18 * scale,
canopyBase + 0.48 * scale,
z - 0.13 * scale
);
if (random() > 0.42) {
voxel(
0.42 * scale,
0.38 * scale,
0.44 * scale,
leafColors[Math.floor(random() * leafColors.length)],
x + 0.12 * scale,
canopyBase + 0.78 * scale,
z + 0.12 * scale
);
}
}
function addUnderstory(x, z) {
const height = 0.16 + random() * 0.13;
voxel(
0.28 + random() * 0.15,
height,
0.27 + random() * 0.15,
random() > 0.5 ? 0x3f8050 : 0x5a9b59,
x,
0.15 + height / 2,
z
);
}
function populatePatch(centerX) {
for (let ix = 0; ix < 5; ix++) {
for (let iz = 0; iz < 8; iz++) {
const x = centerX - 1.5 + ix * 0.75;
const z = -2.8 + iz * 0.8;
const normalized =
Math.pow((x - centerX) / 2.15, 2) +
Math.pow(z / 2.85, 2);
if (normalized > 1 || random() < 0.28) {
continue;
}
// Leave clear openings where the candidate route meets each patch.
if (Math.abs(z) < 1.08 && Math.abs(x) < 4.25) {
continue;
}
addTree(x, z, 0.78 + random() * 0.24);
if (random() > 0.48) {
addUnderstory(
x + (random() - 0.5) * 0.48,
z + (random() - 0.5) * 0.48
);
}
}
}
}
populatePatch(-5.1);
populatePatch(5.1);
// Small voxel flowers around the patch margins.
const flowerColors = [0xf2d267, 0xf2a8bb, 0xd9e9f2];
for (let i = 0; i < 42; i++) {
const side = random() > 0.5 ? 1 : -1;
const x = side * (3.5 + random() * 3.5);
const z = (random() - 0.5) * 6.6;
if (Math.abs(z) < 0.8) continue;
const color = flowerColors[Math.floor(random() * flowerColors.length)];
voxel(0.09, 0.09, 0.09, color, x, 0.22, z, scene, false);
}
// -------------------------------------------------------------------------
// Optional corridor and conceptual route markers
// -------------------------------------------------------------------------
const corridorGroup = new THREE.Group();
scene.add(corridorGroup);
// Low shrub blocks along the edges of the candidate route.
for (let x = -3; x <= 3; x += 0.6) {
for (const z of [-1.05, 1.05]) {
const shrubHeight = 0.24 + random() * 0.18;
voxel(
0.31,
shrubHeight,
0.3,
random() > 0.5 ? 0x44824c : 0x5b9654,
x,
0.15 + shrubHeight / 2,
z,
corridorGroup
);
if (random() > 0.45) {
voxel(
0.22,
0.2,
0.24,
0x6ea55d,
x + 0.12,
0.15 + shrubHeight,
z,
corridorGroup
);
}
}
}
const routeMarkers = new THREE.Group();
corridorGroup.add(routeMarkers);
for (let i = 0; i <= 16; i++) {
const x = -3.15 + (6.3 * i) / 16;
voxel(
0.11,
0.075,
0.11,
i % 2 === 0 ? 0x69b9b0 : 0xb4d6a3,
x,
0.19,
0,
routeMarkers,
false
);
}
const routePulse = voxel(
0.18,
0.18,
0.18,
new THREE.MeshStandardMaterial({
color: 0x9ff2d2,
emissive: 0x3ca887,
emissiveIntensity: 0.45,
roughness: 0.6
}),
-3.15,
0.3,
0,
routeMarkers,
false
);
// -------------------------------------------------------------------------
// Fictional voxel animals
// -------------------------------------------------------------------------
const alleleA = new THREE.Color(0x36b9ef);
const alleleB = new THREE.Color(0xff6f61);
function createAnimal({ traveler = false, bodyColor = 0xeee4c8 } = {}) {
const group = new THREE.Group();
const bodyWidth = traveler ? 0.62 : 0.52;
const bodyHeight = traveler ? 0.34 : 0.29;
const bodyDepth = traveler ? 0.4 : 0.34;
voxel(
bodyWidth,
bodyHeight,
bodyDepth,
bodyColor,
0,
0.39,
0,
group
);
voxel(
0.34,
0.31,
0.31,
bodyColor,
0.31,
0.48,
0,
group
);
// Ears.
voxel(0.11, 0.19, 0.11, bodyColor, 0.24, 0.71, 0.11, group);
voxel(0.11, 0.19, 0.11, bodyColor, 0.24, 0.71, -0.11, group);
// Small square eyes.
voxel(
0.055,
0.065,
0.055,
0x24352d,
0.49,
0.53,
0.105,
group,
false
);
voxel(
0.055,
0.065,
0.055,
0x24352d,
0.49,
0.53,
-0.105,
group,
false
);
// Four blocky legs.
for (const legX of [-0.18, 0.17]) {
for (const legZ of [-0.13, 0.13]) {
voxel(
0.11,
0.24,
0.11,
0x665343,
legX,
0.2,
legZ,
group
);
}
}
// Stepped voxel tail.
voxel(
0.17,
0.15,
0.16,
bodyColor,
-0.34,
0.43,
0,
group
);
voxel(
0.16,
0.14,
0.15,
bodyColor,
-0.49,
0.55,
0,
group
);
voxel(
0.13,
0.12,
0.13,
0xf4d7c3,
-0.6,
0.65,
0,
group
);
const markerMaterial = new THREE.MeshStandardMaterial({
color: alleleA.clone(),
emissive: alleleA.clone(),
emissiveIntensity: 0.35,
roughness: 0.45,
metalness: 0.05,
flatShading: true
});
const marker = new THREE.Mesh(unitBox, markerMaterial);
marker.scale.set(0.19, 0.19, 0.19);
marker.position.set(-0.04, 1.03, 0);
marker.castShadow = true;
group.add(marker);
scene.add(group);
return {
group,
marker,
traveler,
engaged: false,
phase: random() * Math.PI * 2,
home: new THREE.Vector3(),
wanderRadius: 0.35 + random() * 0.3
};
}
const residents = [];
const residentHomes = [
[-5.9, -1.75],
[-4.45, 1.55],
[-6.0, 0.55],
[4.55, -1.55],
[5.85, 0.55],
[4.65, 1.65]
];
residentHomes.forEach(([x, z], index) => {
const resident = createAnimal({
bodyColor:
index % 3 === 0
? 0xead7ad
: index % 3 === 1
? 0xd8e4cf
: 0xe3d5c5
});
resident.home.set(x, 0.15, z);
resident.group.position.copy(resident.home);
resident.marker.material.color.copy(
index % 2 === 0 ? alleleA : alleleB
);
resident.marker.material.emissive.copy(
index % 2 === 0 ? alleleA : alleleB
);
residents.push(resident);
});
const travelers = [];
for (let i = 0; i < 2; i++) {
const traveler = createAnimal({
traveler: true,
bodyColor: 0x8b68d9
});
Object.assign(traveler, {
state: "idle",
timer: 0,
routeOffset: i === 0 ? -0.24 : 0.24,
exchange: null,
idleDelay: i * 0.75
});
traveler.group.position.set(-3.55, 0.15, traveler.routeOffset);
traveler.marker.material.color.copy(
i === 0 ? alleleA : alleleB
);
traveler.marker.material.emissive.copy(
i === 0 ? alleleA : alleleB
);
travelers.push(traveler);
}
// -------------------------------------------------------------------------
// UI state and interaction
// -------------------------------------------------------------------------
const corridorToggle = document.querySelector("#corridorToggle");
const motionToggle = document.querySelector("#motionToggle");
const markerToggle = document.querySelector("#markerToggle");
const speedControl = document.querySelector("#speedControl");
const statusElement = document.querySelector("#status");
const eventLogElement = document.querySelector("#eventLog");
const routeLabel = document.querySelector("#routeLabel");
let corridorEnabled = true;
let motionEnabled = true;
let markersEnabled = true;
let simulationSpeed = 1;
const eventMessages = [];
function logEvent(message) {
eventMessages.unshift(message);
eventMessages.splice(3);
eventLogElement.replaceChildren(
...eventMessages.map((text) => {
const line = document.createElement("div");
line.textContent = `• ${text}`;
return line;
})
);
}
function setStatus(message, exchange = false) {
statusElement.textContent = message;
statusElement.classList.toggle("exchange", exchange);
}
function updateMarkerVisibility() {
for (const animal of [...residents, ...travelers]) {
if (!animal.exchange) {
animal.marker.visible = markersEnabled;
}
}
for (const traveler of travelers) {
if (traveler.exchange?.cube) {
traveler.exchange.cube.visible = markersEnabled;
}
}
}
function restoreTraveler(traveler) {
if (traveler.exchange?.cube) {
scene.remove(traveler.exchange.cube);
traveler.exchange.cube.material.dispose();
}
traveler.exchange = null;
traveler.state = "idle";
traveler.timer = 0;
traveler.group.position.set(
-3.55,
0.15,
traveler.routeOffset
);
traveler.marker.visible = markersEnabled;
}
function applyCorridorState() {
corridorEnabled = corridorToggle.checked;
corridorGroup.visible = corridorEnabled;
for (const { index, x, z } of tileData) {
terrain.setColorAt(index, groundColor(x, z, index));
}
terrain.instanceColor.needsUpdate = true;
if (!corridorEnabled) {
for (const traveler of travelers) {
restoreTraveler(traveler);
}
routeLabel.textContent = "Candidate route absent";
setStatus(
"The candidate corridor is absent. In this toy scenario, travelers remain within the source woodland."
);
} else {
routeLabel.textContent = "Candidate route · illustrative";
setStatus(
"A candidate stepping route is present. Fictional travelers may attempt to cross; this is not an effectiveness estimate."
);
}
}
corridorToggle.addEventListener("change", applyCorridorState);
motionToggle.addEventListener("change", () => {
motionEnabled = motionToggle.checked;
setStatus(
motionEnabled
? "The illustrative movement-and-meeting simulation is running."
: "The simulation is paused. The landscape and candidate route remain visible."
);
});
markerToggle.addEventListener("change", () => {
markersEnabled = markerToggle.checked;
updateMarkerVisibility();
});
speedControl.addEventListener("input", () => {
simulationSpeed = Number(speedControl.value);
});
document.querySelector("#resetSimulation").addEventListener("click", () => {
for (const traveler of travelers) {
restoreTraveler(traveler);
}
for (const resident of residents) {
resident.engaged = false;
resident.group.position.copy(resident.home);
resident.marker.visible = markersEnabled;
}
eventMessages.length = 0;
eventLogElement.replaceChildren();
logEvent("Simulation reset to the initial illustrative state.");
applyCorridorState();
});
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and keyboard rotation
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.62,
pitch: 0.7,
radius: 18.5,
target: new THREE.Vector3(0, 0.45, 0)
};
const defaultCamera = { ...cameraState };
function updateCamera() {
const { yaw, pitch, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.cos(pitch) * Math.sin(yaw),
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.yaw = defaultCamera.yaw;
cameraState.pitch = defaultCamera.pitch;
cameraState.radius = defaultCamera.radius;
cameraState.target.copy(defaultCamera.target);
}
document.querySelector("#resetCamera").addEventListener("click", resetCamera);
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = clamp(
cameraState.pitch + dy * 0.005,
0.28,
1.34
);
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius + event.deltaY * 0.012,
10,
28
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") cameraState.yaw -= 0.08;
if (event.key === "ArrowRight") cameraState.yaw += 0.08;
if (event.key === "ArrowUp") {
cameraState.pitch = clamp(cameraState.pitch - 0.06, 0.28, 1.34);
}
if (event.key === "ArrowDown") {
cameraState.pitch = clamp(cameraState.pitch + 0.06, 0.28, 1.34);
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
});
// -------------------------------------------------------------------------
// Illustrative movement and symbolic allele exchange
// -------------------------------------------------------------------------
function routePoint(progress, offset) {
const u = clamp(progress, 0, 1);
return new THREE.Vector3(
-3.2 + u * 6.4,
0.15,
offset + Math.sin(u * Math.PI * 2) * 0.11
);
}
function faceDirection(animal, dx, dz) {
if (Math.abs(dx) + Math.abs(dz) > 0.0001) {
animal.group.rotation.y = Math.atan2(-dz, dx);
}
}
function beginMeeting(traveler) {
const destination = routePoint(1, traveler.routeOffset);
let closest = residents[3];
let closestDistance = Infinity;
for (const resident of residents) {
const distance = resident.group.position.distanceTo(destination);
if (distance < closestDistance) {
closest = resident;
closestDistance = distance;
}
}
closest.engaged = true;
const cubeMaterial = new THREE.MeshStandardMaterial({
color: traveler.marker.material.color.clone(),
emissive: traveler.marker.material.emissive.clone(),
emissiveIntensity: 0.5,
roughness: 0.45,
flatShading: true
});
const cube = new THREE.Mesh(unitBox, cubeMaterial);
cube.scale.set(0.19, 0.19, 0.19);
cube.castShadow = true;
cube.visible = markersEnabled;
scene.add(cube);
traveler.marker.visible = false;
closest.marker.visible = false;
traveler.exchange = {
resident: closest,
cube,
progress: 0,
travelerStart: traveler.group.position.clone()
};
traveler.state = "meeting";
traveler.timer = 0;
setStatus(
"Illustrative meeting: a traveler reaches the receiving woodland and encounters a resident.",
true
);
logEvent(
"A fictional traveler and resident meet at the receiving patch."
);
}
function finishMeeting(traveler) {
const exchange = traveler.exchange;
if (!exchange) return;
const resident = exchange.resident;
const travelerColor = traveler.marker.material.color.clone();
const residentColor = resident.marker.material.color.clone();
traveler.marker.material.color.copy(residentColor);
traveler.marker.material.emissive.copy(residentColor);
resident.marker.material.color.copy(travelerColor);
resident.marker.material.emissive.copy(travelerColor);
traveler.marker.visible = markersEnabled;
resident.marker.visible = markersEnabled;
scene.remove(exchange.cube);
exchange.cube.material.dispose();
traveler.exchange = null;
resident.engaged = false;
traveler.state = "return";
traveler.timer = 0;
setStatus(
"Symbolic allele markers exchange during the depicted meeting. This illustrates an opportunity, not measured gene flow.",
true
);
logEvent(
"The two symbolic allele colors exchange as a conceptual gene-flow opportunity."
);
}
function updateResidents(time, delta) {
for (const resident of residents) {
if (resident.engaged) continue;
const angle = time * (0.28 + resident.phase * 0.025) + resident.phase;
const radius = resident.wanderRadius;
const x =
resident.home.x + Math.cos(angle) * radius;
const z =
resident.home.z + Math.sin(angle) * radius * 0.72;
const previousX = resident.group.position.x;
const previousZ = resident.group.position.z;
resident.group.position.set(
x,
0.15 + Math.abs(Math.sin(time * 3.4 + resident.phase)) * 0.045,
z
);
faceDirection(
resident,
x - previousX,
z - previousZ
);
}
}
function updateTravelers(time, delta) {
for (const traveler of travelers) {
traveler.timer += delta * simulationSpeed;
if (traveler.state === "idle") {
traveler.group.position.set(
-3.55 + Math.sin(time * 1.6 + traveler.routeOffset * 8) * 0.12,
0.15 + Math.abs(Math.sin(time * 4 + traveler.routeOffset)) * 0.035,
traveler.routeOffset
);
faceDirection(traveler, 1, 0);
if (
corridorEnabled &&
traveler.timer > 1.25 + traveler.idleDelay
) {
traveler.state = "outbound";
traveler.timer = 0;
setStatus(
"Illustrative movement: a fictional traveler begins using the candidate route."
);
}
} else if (traveler.state === "outbound") {
const progress = clamp(traveler.timer / 6.2, 0, 1);
const point = routePoint(progress, traveler.routeOffset);
const previous = traveler.group.position.clone();
traveler.group.position.set(
point.x,
point.y + Math.abs(Math.sin(progress * Math.PI * 12)) * 0.13,
point.z
);
faceDirection(
traveler,
point.x - previous.x,
point.z - previous.z
);
if (progress >= 1) {
beginMeeting(traveler);
}
} else if (traveler.state === "meeting") {
const exchange = traveler.exchange;
if (!exchange) continue;
exchange.progress = clamp(exchange.progress + delta * simulationSpeed, 0, 1);
const eased = smoothstep(exchange.progress);
const residentPosition = exchange.resident.group.position;
traveler.group.position.lerpVectors(
exchange.travelerStart,
residentPosition,
eased
);
faceDirection(
traveler,
residentPosition.x - traveler.group.position.x,
residentPosition.z - traveler.group.position.z
);
scene.updateMatrixWorld(true);
const from = traveler.marker.getWorldPosition(new THREE.Vector3());
const to = exchange.resident.marker.getWorldPosition(new THREE.Vector3());
exchange.cube.position.lerpVectors(from, to, eased);
exchange.cube.rotation.x += delta * 5;
exchange.cube.rotation.y += delta * 7;
if (exchange.progress >= 1) {
finishMeeting(traveler);
}
} else if (traveler.state === "return") {
const progress = clamp(traveler.timer / 5.6, 0, 1);
const routeProgress = 1 - progress;
const point = routePoint(routeProgress, traveler.routeOffset);
const previous = traveler.group.position.clone();
traveler.group.position.set(
point.x,
point.y + Math.abs(Math.sin(progress * Math.PI * 12)) * 0.1,
point.z
);
faceDirection(
traveler,
point.x - previous.x,
point.z - previous.z
);
if (progress >= 1) {
traveler.state = "idle";
traveler.timer = 0;
logEvent("The traveler returns to the source woodland in the toy scenario.");
}
}
}
}
// -------------------------------------------------------------------------
// DOM labels anchored to the diorama
// -------------------------------------------------------------------------
const labelAnchors = [
{
element: document.querySelector("#sourceLabel"),
point: new THREE.Vector3(-5.15, 2.65, -1.5)
},
{
element: routeLabel,
point: new THREE.Vector3(0, 0.45, 0)
},
{
element: document.querySelector("#destinationLabel"),
point: new THREE.Vector3(5.15, 2.65, 1.5)
}
];
function placeLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labelAnchors) {
const projected = label.point.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// -------------------------------------------------------------------------
// Main animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
if (motionEnabled) {
updateResidents(elapsed, delta);
updateTravelers(elapsed, delta);
}
if (corridorEnabled) {
const pulseProgress = (elapsed * 0.13) % 1;
const pulsePoint = routePoint(pulseProgress, 0);
routePulse.position.set(
pulsePoint.x,
0.3 + Math.sin(pulseProgress * Math.PI) * 0.12,
pulsePoint.z
);
routePulse.rotation.x += delta * 2;
routePulse.rotation.y += delta * 3;
}
updateCamera();
placeLabels();
renderer.render(scene, camera);
}
applyCorridorState();
logEvent("The initial candidate route is shown as a conceptual illustration.");
updateCamera();
animate();
</script>
</body>
</html>
```