Prompt specification
Build or customize the Three.js 3D scene "Divergence across isolated generations" 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: Divergence across isolated generations
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/divergence-across-isolated-generations-x0958
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows speciation through reproductive isolation. Show two fictional populations accumulating different symbolic variants under explicit toy rules; state that visual difference alone does not establish separate biological species. Stylized solid forms.
Working Three.js Starter (divergence-across-isolated-generations-x0958):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark">
<title>Reproductive Isolation — A Toy Speciation Model</title>
<style>
:root {
--bg: #091019;
--panel: rgba(11, 18, 28, 0.91);
--line: rgba(255, 255, 255, 0.12);
--text: #eef5f7;
--muted: #9db0bb;
--a: #3dd6c6;
--b: #ff725e;
--shared: #f6ce68;
--danger: #ff806d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(390px, calc(100vw - 36px));
max-height: calc(100vh - 112px);
overflow: auto;
padding: 20px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(14px);
scrollbar-width: thin;
}
.eyebrow {
margin-bottom: 8px;
color: var(--shared);
font-size: 0.69rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.65rem, 3vw, 2.2rem);
line-height: 1.02;
letter-spacing: -0.045em;
}
.intro {
margin: 12px 0 18px;
color: var(--muted);
font-size: 0.92rem;
line-height: 1.5;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.stat {
min-width: 0;
padding: 11px 10px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.035);
}
.stat-label {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 0.64rem;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.stat-value {
display: block;
font-size: 1.25rem;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.meter {
height: 7px;
margin-top: 13px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.09);
}
#compat-fill {
width: 100%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--a), var(--shared));
transition: width 500ms ease, background 500ms ease;
}
#status {
min-height: 2.6em;
margin-top: 10px;
color: #d8e5e8;
font-size: 0.82rem;
line-height: 1.35;
}
.section-title {
margin: 18px 0 9px;
color: var(--muted);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.rules {
margin: 0;
padding-left: 1.25rem;
color: #c8d6da;
font-size: 0.8rem;
line-height: 1.48;
}
.rules li {
margin-bottom: 7px;
padding-left: 3px;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 13px;
margin-top: 13px;
color: #cbd8dc;
font-size: 0.73rem;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.symbol {
display: inline-block;
width: 11px;
height: 11px;
background: currentColor;
}
.symbol.shared {
color: var(--shared);
border-radius: 50%;
}
.symbol.a {
color: var(--a);
clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.symbol.b {
color: var(--b);
border-radius: 2px;
}
.caution {
margin-top: 17px;
padding: 12px 13px;
border: 1px solid rgba(246, 206, 104, 0.28);
border-radius: 12px;
background: rgba(246, 206, 104, 0.07);
color: #dce4df;
font-size: 0.77rem;
line-height: 1.45;
}
.caution strong {
display: block;
margin-bottom: 3px;
color: var(--shared);
}
.transport {
position: fixed;
z-index: 20;
left: 50%;
bottom: 18px;
display: flex;
align-items: center;
gap: 8px;
width: max-content;
max-width: calc(100vw - 24px);
padding: 9px;
transform: translateX(-50%);
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(9, 16, 25, 0.9);
box-shadow: 0 14px 38px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(12px);
}
button {
min-height: 38px;
padding: 0 13px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 10px;
background: rgba(255, 255, 255, 0.06);
color: var(--text);
font: inherit;
font-size: 0.78rem;
font-weight: 700;
cursor: pointer;
}
button:hover:not(:disabled) {
background: rgba(255, 255, 255, 0.12);
}
button.primary {
border-color: rgba(61, 214, 198, 0.38);
background: rgba(61, 214, 198, 0.15);
color: #bffaf2;
}
button:disabled {
opacity: 0.38;
cursor: default;
}
#timeline {
width: 130px;
accent-color: var(--a);
cursor: pointer;
}
.scene-label {
position: fixed;
z-index: 5;
left: 50%;
top: 50%;
padding: 6px 9px;
transform: translate(-50%, -100%);
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 8px;
background: rgba(7, 12, 19, 0.78);
color: #e9f2f3;
font-size: 0.66rem;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
}
#label-a {
border-color: rgba(61, 214, 198, 0.42);
color: var(--a);
}
#label-b {
border-color: rgba(255, 114, 94, 0.42);
color: var(--b);
}
.hint {
position: fixed;
z-index: 4;
right: 18px;
bottom: 20px;
color: rgba(220, 233, 238, 0.55);
font-size: 0.7rem;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 50;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
color: var(--muted);
font-size: 0.85rem;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
.hud {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
max-height: 53vh;
padding: 15px;
border-radius: 14px;
}
.intro {
margin: 9px 0 13px;
font-size: 0.82rem;
}
.stat {
padding: 8px;
}
.stat-value {
font-size: 1.05rem;
}
.rules,
.caution {
font-size: 0.73rem;
}
.transport {
bottom: 10px;
gap: 5px;
padding: 7px;
}
button {
padding: 0 10px;
font-size: 0.7rem;
}
#timeline {
width: 90px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="loading">Loading the 3D model…</div>
<div id="label-ancestor" class="scene-label">Shared ancestral pool</div>
<div id="label-a" class="scene-label">Pool A · cone alleles</div>
<div id="label-b" class="scene-label">Pool B · cube alleles</div>
<aside class="hud" aria-label="Toy speciation model explanation">
<div class="eyebrow">Fictional populations · deterministic toy model</div>
<h1>When does difference become isolation?</h1>
<p class="intro">
A barrier separates one founding pool into two populations.
Symbolic alleles accumulate independently while interbreeding becomes
less likely.
</p>
<div class="stats" aria-live="polite">
<div class="stat">
<span class="stat-label">Generation</span>
<span class="stat-value" id="generation">G0</span>
</div>
<div class="stat">
<span class="stat-label">Mismatched loci</span>
<span class="stat-value" id="mismatch">0 / 3</span>
</div>
<div class="stat">
<span class="stat-label">Toy interbreeding</span>
<span class="stat-value" id="compatibility">100%</span>
</div>
</div>
<div class="meter" aria-hidden="true">
<div id="compat-fill"></div>
</div>
<div id="status">Same symbolic genome · interbreeding allowed</div>
<div class="section-title">Explicit toy rules</div>
<ol class="rules">
<li>
The ancestor has three haploid loci, each carrying the same shared
symbol: <strong>S / S / S</strong>.
</li>
<li>
At G1, a barrier closes. In each later generation, exactly one
remaining locus changes in each population. Mutations never revert.
</li>
<li>
Pool A changes a locus to a teal cone; Pool B changes it to a coral
cube. No exchange occurs after the barrier.
</li>
<li>
Toy fertility is
<strong>(3 − mismatched loci) / 3</strong>. At G3 all three loci
differ, so fertility is zero by definition.
</li>
</ol>
<div class="legend" aria-label="Symbol legend">
<span class="legend-item">
<span class="symbol shared"></span> Shared sphere
</span>
<span class="legend-item">
<span class="symbol a"></span> Pool A cone
</span>
<span class="legend-item">
<span class="symbol b"></span> Pool B cube
</span>
</div>
<div class="caution">
<strong>Important biological distinction</strong>
Visual difference alone does not establish separate biological
species. In this toy, separation is defined by the stated zero-
interbreeding rule—not by the creatures’ colors, shapes, or symbols.
Real speciation is not modeled here.
</div>
</aside>
<nav class="transport" aria-label="Model controls">
<button id="toggle" class="primary" type="button">Pause</button>
<button id="step" type="button">Next generation</button>
<button id="reset" type="button">Restart</button>
<input
id="timeline"
type="range"
min="0"
max="3"
step="1"
value="0"
aria-label="Choose generation"
>
</nav>
<div class="hint">Drag to orbit · Scroll or pinch to zoom · Double-click to reset view</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// -------------------------------------------------------------------------
const stage = document.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D toy model of two populations accumulating symbolic variants"
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x091019);
scene.fog = new THREE.Fog(0x091019, 17, 31);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const target = new THREE.Vector3(0, 0.45, 0);
let cameraTheta = 0.18;
let cameraPhi = 1.03;
let cameraRadius = window.innerWidth < 720 ? 20 : 14.5;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
target.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
target.y + cameraRadius * Math.cos(cameraPhi),
target.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc9e7ff, 0x18212c, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -11;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.normalBias = 0.035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x65e4d5, 1.4);
rimLight.position.set(-7, 4, -6);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Materials and reusable geometry
// -------------------------------------------------------------------------
const material = {
ground: new THREE.MeshStandardMaterial({
color: 0x101923,
roughness: 0.94,
metalness: 0.04
}),
centralDisk: new THREE.MeshStandardMaterial({
color: 0x27313b,
roughness: 0.78
}),
platformA: new THREE.MeshStandardMaterial({
color: 0x193b3b,
roughness: 0.72
}),
platformB: new THREE.MeshStandardMaterial({
color: 0x402b2a,
roughness: 0.72
}),
barrier: new THREE.MeshStandardMaterial({
color: 0x596775,
roughness: 0.7,
metalness: 0.12
}),
barrierEdge: new THREE.MeshStandardMaterial({
color: 0xa8b5bd,
roughness: 0.55,
metalness: 0.2
}),
leg: new THREE.MeshStandardMaterial({
color: 0x27303a,
roughness: 0.82
}),
eye: new THREE.MeshStandardMaterial({
color: 0x10151b,
roughness: 0.3
}),
shared: new THREE.MeshStandardMaterial({
color: 0xf6ce68,
roughness: 0.55
}),
alleleA: new THREE.MeshStandardMaterial({
color: 0x3dd6c6,
roughness: 0.48
}),
alleleB: new THREE.MeshStandardMaterial({
color: 0xff725e,
roughness: 0.48
}),
bridge: new THREE.MeshStandardMaterial({
color: 0x3dd6c6,
roughness: 0.42,
metalness: 0.12
}),
bridgePad: new THREE.MeshStandardMaterial({
color: 0x5a6773,
roughness: 0.68
})
};
const bodyGeometry = new THREE.IcosahedronGeometry(0.52, 1);
const headGeometry = new THREE.SphereGeometry(0.29, 16, 12);
const legGeometry = new THREE.CylinderGeometry(0.045, 0.065, 0.35, 6);
const eyeGeometry = new THREE.SphereGeometry(0.045, 10, 8);
const alleleGeometry = [
new THREE.SphereGeometry(0.095, 16, 12),
new THREE.ConeGeometry(0.13, 0.22, 3),
new THREE.BoxGeometry(0.16, 0.16, 0.16)
];
const alleleMaterials = [
material.shared,
material.alleleA,
material.alleleB
];
// -------------------------------------------------------------------------
// Ground, platforms, and central barrier
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
material.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.02;
ground.receiveShadow = true;
scene.add(ground);
const centralDisk = new THREE.Mesh(
new THREE.CylinderGeometry(1.85, 1.95, 0.14, 48),
material.centralDisk
);
centralDisk.position.y = 0.07;
centralDisk.receiveShadow = true;
scene.add(centralDisk);
function addPlatform(x, platformMaterial, accentColor) {
const group = new THREE.Group();
const disk = new THREE.Mesh(
new THREE.CylinderGeometry(2.05, 2.18, 0.14, 48),
platformMaterial
);
disk.position.set(x, 0.07, 0);
disk.receiveShadow = true;
group.add(disk);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(2.04, 0.026, 8, 64),
new THREE.MeshBasicMaterial({ color: accentColor })
);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, 0.15, 0);
group.add(ring);
scene.add(group);
}
addPlatform(-3.55, material.platformA, 0x3dd6c6);
addPlatform(3.55, material.platformB, 0xff725e);
const barrierGroup = new THREE.Group();
const barrierBody = new THREE.Mesh(
new THREE.BoxGeometry(0.26, 2.7, 5.4),
material.barrier
);
barrierBody.position.set(0, 1.35, -0.35);
barrierBody.castShadow = true;
barrierBody.receiveShadow = true;
barrierGroup.add(barrierBody);
const barrierCap = new THREE.Mesh(
new THREE.BoxGeometry(0.31, 0.08, 5.48),
material.barrierEdge
);
barrierCap.position.set(0, 2.68, -0.35);
barrierCap.castShadow = true;
barrierGroup.add(barrierCap);
scene.add(barrierGroup);
// -------------------------------------------------------------------------
// Toy fertility coupling: three solid bridge pieces represent fertility
// -------------------------------------------------------------------------
const bridgeGroup = new THREE.Group();
bridgeGroup.position.z = 3.55;
const padGeometry = new THREE.CylinderGeometry(0.35, 0.4, 0.22, 24);
[-1.42, 1.42].forEach((x) => {
const pad = new THREE.Mesh(padGeometry, material.bridgePad);
pad.position.set(x, 0.22, 0);
pad.castShadow = true;
bridgeGroup.add(pad);
});
const bridgeSegments = [];
for (let i = 0; i < 3; i++) {
const segment = new THREE.Mesh(
new THREE.BoxGeometry(0.68, 0.13, 0.34),
material.bridge
);
const baseX = (i - 1) * 0.72;
segment.position.set(baseX, 0.36, 0);
segment.castShadow = true;
bridgeGroup.add(segment);
const bead = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 12, 8),
material.shared
);
bead.position.set(baseX, 0.56, 0);
bead.userData.baseX = baseX;
bridgeGroup.add(bead);
bridgeSegments.push({ segment, bead });
}
scene.add(bridgeGroup);
// -------------------------------------------------------------------------
// Creatures: solid low-poly forms with three symbolic allele markers
// -------------------------------------------------------------------------
const slots = [
{ x: -0.72, z: -0.82 },
{ x: 0.60, z: -0.88 },
{ x: -0.64, z: 0.62 },
{ x: 0.66, z: 0.60 }
];
const creatures = [];
function createCreature(side, slot, index) {
const group = new THREE.Group();
const shellMaterial = new THREE.MeshStandardMaterial({
color: side < 0 ? 0xd4ebe7 : 0xf0dcd6,
roughness: 0.8
});
const body = new THREE.Mesh(bodyGeometry, shellMaterial);
body.position.y = 0.43;
body.scale.set(1.08, 0.68, 0.95);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const head = new THREE.Mesh(headGeometry, shellMaterial);
head.position.set(0, 0.43, 0.49);
head.scale.set(1, 0.86, 0.9);
head.castShadow = true;
group.add(head);
[-0.115, 0.115].forEach((x) => {
const eye = new THREE.Mesh(eyeGeometry, material.eye);
eye.position.set(x, 0.49, 0.72);
group.add(eye);
});
[-0.28, 0.28].forEach((x) => {
[-0.20, 0.20].forEach((z) => {
const leg = new THREE.Mesh(legGeometry, material.leg);
leg.position.set(x, 0.17, z);
leg.castShadow = true;
group.add(leg);
});
});
const markerGroups = [];
[-0.28, 0, 0.28].forEach((x) => {
const markerGroup = new THREE.Group();
markerGroup.position.set(x, 0.68, -0.02);
alleleGeometry.forEach((geometry, code) => {
const marker = new THREE.Mesh(geometry, alleleMaterials[code]);
marker.position.y = code === 0 ? 0.10 : code === 1 ? 0.11 : 0.08;
marker.castShadow = true;
markerGroup.add(marker);
});
group.add(markerGroup);
markerGroups.push(markerGroup);
});
// Face inward toward the other population.
group.rotation.y = side < 0 ? Math.PI / 2 : -Math.PI / 2;
group.position.y = 0.15;
group.userData = {
side,
slot,
index,
markerGroups
};
scene.add(group);
creatures.push(group);
}
for (let i = 0; i < slots.length; i++) {
createCreature(-1, slots[i], i);
createCreature(1, slots[i], i);
}
// -------------------------------------------------------------------------
// Labels and their 3D anchors
// -------------------------------------------------------------------------
const labelAncestor = document.querySelector("#label-ancestor");
const labelA = document.querySelector("#label-a");
const labelB = document.querySelector("#label-b");
const anchorAncestor = new THREE.Object3D();
const anchorA = new THREE.Object3D();
const anchorB = new THREE.Object3D();
scene.add(anchorAncestor, anchorA, anchorB);
const projected = new THREE.Vector3();
function placeLabel(element, anchor, opacity) {
projected.copy(anchor.position).project(camera);
const visible =
opacity > 0.01 && projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? String(opacity) : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// -------------------------------------------------------------------------
// Toy state and animation
//
// Generation 0: S S S in both populations.
// Generation 1: locus 0 differs.
// Generation 2: loci 0 and 1 differ.
// Generation 3: all loci differ; toy fertility is zero.
// -------------------------------------------------------------------------
let currentGeneration = 0;
let transition = null;
let playing = true;
let nextGenerationAt = performance.now() + 1700;
const ui = {
generation: document.querySelector("#generation"),
mismatch: document.querySelector("#mismatch"),
compatibility: document.querySelector("#compatibility"),
compatFill: document.querySelector("#compat-fill"),
status: document.querySelector("#status"),
toggle: document.querySelector("#toggle"),
step: document.querySelector("#step"),
reset: document.querySelector("#reset"),
timeline: document.querySelector("#timeline")
};
function alleleCode(generation, locus, side) {
if (locus < generation) {
return side < 0 ? 1 : 2;
}
return 0;
}
function positionFor(generation, side, slot) {
if (generation === 0) {
return {
x: slot.x * 0.72 + side * 0.32,
z: slot.z * 0.85
};
}
return {
x: side * 3.55 + slot.x,
z: slot.z
};
}
function smoothstep(t) {
return t * t * (3 - 2 * t);
}
function updateUI() {
const generation = currentGeneration;
const mismatch = generation;
const compatibility = Math.max(0, (3 - mismatch) / 3);
const percent = Math.round(compatibility * 100);
ui.generation.textContent = `G${generation}`;
ui.mismatch.textContent = `${mismatch} / 3`;
ui.compatibility.textContent = `${percent}%`;
ui.compatFill.style.width = `${percent}%`;
if (percent === 0) {
ui.compatFill.style.background = "var(--danger)";
} else {
ui.compatFill.style.background =
"linear-gradient(90deg, var(--a), var(--shared))";
}
const messages = [
"Same symbolic genome · interbreeding allowed",
"Barrier closed · one locus differs",
"Two loci differ · interbreeding is reduced",
"Zero toy interbreeding · reproductively isolated"
];
ui.status.textContent = messages[generation];
ui.timeline.value = String(generation);
ui.step.disabled = generation === 3 || transition !== null;
ui.toggle.textContent = playing ? "Pause" : "Play";
}
function setGeneration(generation, animate = true) {
const next = THREE.MathUtils.clamp(Math.round(generation), 0, 3);
if (animate && next !== currentGeneration) {
transition = {
from: currentGeneration,
to: next,
start: performance.now(),
duration: 1250
};
} else {
transition = null;
}
currentGeneration = next;
updateUI();
}
function applyState(fromGeneration, toGeneration, progress, now) {
const eased = smoothstep(THREE.MathUtils.clamp(progress, 0, 1));
// Move and split the creatures.
creatures.forEach((creature) => {
const { side, slot, markerGroups } = creature.userData;
const fromPosition = positionFor(fromGeneration, side, slot);
const toPosition = positionFor(toGeneration, side, slot);
creature.position.x = THREE.MathUtils.lerp(
fromPosition.x,
toPosition.x,
eased
);
creature.position.z = THREE.MathUtils.lerp(
fromPosition.z,
toPosition.z,
eased
);
// Scale the old allele down while the new allele grows.
markerGroups.forEach((markerGroup, locus) => {
const oldCode = alleleCode(fromGeneration, locus, side);
const newCode = alleleCode(toGeneration, locus, side);
markerGroup.children.forEach((marker, code) => {
let scale = 0;
if (oldCode === newCode && code === oldCode) {
scale = 1;
} else if (code === oldCode) {
scale = 1 - eased;
} else if (code === newCode) {
scale = eased;
}
marker.scale.setScalar(Math.max(0.001, scale));
marker.visible = scale > 0.005;
});
});
});
// The barrier rises after the split.
const barrierFactors = [0.001, 0.72, 0.88, 1];
const barrierFactor = THREE.MathUtils.lerp(
barrierFactors[fromGeneration],
barrierFactors[toGeneration],
eased
);
barrierGroup.scale.y = barrierFactor;
barrierGroup.visible = barrierFactor > 0.01;
// The three bridge pieces encode the toy fertility fraction.
const fromCompatibility = (3 - fromGeneration) / 3;
const toCompatibility = (3 - toGeneration) / 3;
const compatibility = THREE.MathUtils.lerp(
fromCompatibility,
toCompatibility,
eased
);
bridgeSegments.forEach(({ segment, bead }, index) => {
const segmentScale = THREE.MathUtils.clamp(
compatibility * 3 - index,
0,
1
);
segment.visible = segmentScale > 0.01;
segment.scale.x = Math.max(0.001, segmentScale);
bead.visible = segmentScale > 0.08;
bead.scale.setScalar(0.6 + segmentScale * 0.4);
bead.position.x =
bead.userData.baseX +
Math.sin(now * 0.0025 + index * 1.8) * 0.22;
});
// Place the floating labels.
const splitFrom = fromGeneration === 0 ? 0 : 1;
const splitTo = toGeneration === 0 ? 0 : 1;
const split = THREE.MathUtils.lerp(splitFrom, splitTo, eased);
anchorAncestor.position.set(0, 1.42, 0);
anchorA.position.set(-3.55 * split, 1.42, 0);
anchorB.position.set(3.55 * split, 1.42, 0);
placeLabel(labelAncestor, anchorAncestor, 1 - split);
placeLabel(labelA, anchorA, split);
placeLabel(labelB, anchorB, split);
}
// -------------------------------------------------------------------------
// Custom camera controls — no addons
// -------------------------------------------------------------------------
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.48,
1.36
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
9,
28
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraTheta = 0.18;
cameraPhi = 1.03;
cameraRadius = window.innerWidth < 720 ? 20 : 14.5;
updateCamera();
});
// -------------------------------------------------------------------------
// Controls
// -------------------------------------------------------------------------
ui.toggle.addEventListener("click", () => {
playing = !playing;
if (playing && currentGeneration < 3 && !transition) {
nextGenerationAt = performance.now() + 700;
}
updateUI();
});
ui.step.addEventListener("click", () => {
if (currentGeneration < 3 && !transition) {
setGeneration(currentGeneration + 1, true);
}
});
ui.reset.addEventListener("click", () => {
currentGeneration = 0;
transition = null;
nextGenerationAt = performance.now() + 1200;
updateUI();
});
ui.timeline.addEventListener("input", (event) => {
setGeneration(Number(event.target.value), false);
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
ui.toggle.click();
}
if (event.code === "ArrowRight" && currentGeneration < 3 && !transition) {
setGeneration(currentGeneration + 1, true);
}
if (event.code === "ArrowLeft" && currentGeneration > 0 && !transition) {
setGeneration(currentGeneration - 1, true);
}
if (event.code === "KeyR") {
ui.reset.click();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
if (transition) {
const rawProgress = (now - transition.start) / transition.duration;
const progress = THREE.MathUtils.clamp(rawProgress, 0, 1);
applyState(transition.from, transition.to, progress, now);
if (rawProgress >= 1) {
transition = null;
nextGenerationAt = now + 2000;
updateUI();
}
} else {
applyState(
currentGeneration,
currentGeneration,
1,
now
);
if (
playing &&
currentGeneration < 3 &&
now >= nextGenerationAt
) {
setGeneration(currentGeneration + 1, true);
}
}
updateCamera();
renderer.render(scene, camera);
}
updateUI();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```