Prompt specification
Build or customize the Three.js 3D scene "Interaction-rule coevolution explorer" 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: Interaction-rule coevolution explorer
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/interaction-rule-coevolution-explorer-x0892
Scene Description Prompt:
Explore coevolution in two fictional low-poly populations by changing a declared compatibility rule. Show both trait frequencies. Three.js from cdnjs, one HTML file.
Working Three.js Starter (interaction-rule-coevolution-explorer-x0892):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mutual — Coevolution Field Lab</title>
<style>
:root {
--bg: #071117;
--panel: #0c1a21;
--panel-2: #10232b;
--line: rgba(188, 232, 226, 0.12);
--text: #e8f4f0;
--muted: #8da8a7;
--mint: #65e6b0;
--amber: #ffb45e;
--danger: #ff7d73;
--shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
width: 100%;
height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
select,
input {
font: inherit;
}
button,
select {
color: inherit;
}
.app {
display: grid;
grid-template-rows: 68px minmax(0, 1fr);
height: 100dvh;
min-height: 620px;
}
.topbar {
z-index: 20;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24px;
background: rgba(8, 19, 25, 0.96);
border-bottom: 1px solid var(--line);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 35px;
height: 35px;
flex: 0 0 auto;
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
width: 21px;
height: 21px;
transform: rotate(45deg);
border: 2px solid;
border-radius: 4px;
}
.brand-mark::before {
left: 1px;
top: 2px;
border-color: var(--mint);
background: rgba(101, 230, 176, 0.12);
}
.brand-mark::after {
right: 1px;
bottom: 2px;
border-color: var(--amber);
background: rgba(255, 180, 94, 0.12);
}
.brand h1 {
margin: 0;
font-size: 15px;
line-height: 1.1;
letter-spacing: 0.18em;
font-weight: 750;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.04em;
}
.top-status {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 0 5px rgba(101, 230, 176, 0.08);
animation: breathe 2s ease-in-out infinite;
}
@keyframes breathe {
50% {
opacity: 0.55;
box-shadow: 0 0 0 8px rgba(101, 230, 176, 0);
}
}
.workspace {
display: grid;
grid-template-columns: 318px minmax(0, 1fr);
min-height: 0;
}
.sidebar {
z-index: 10;
min-height: 0;
overflow-y: auto;
padding: 21px 20px 24px;
background:
linear-gradient(180deg, rgba(255, 255, 255, 0.018), transparent 28%),
var(--panel);
border-right: 1px solid var(--line);
scrollbar-width: thin;
scrollbar-color: #29424a transparent;
}
.eyebrow {
margin: 0 0 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.section-title {
margin: 0;
font-size: 18px;
line-height: 1.25;
letter-spacing: -0.025em;
}
.section-copy {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.rule-card {
margin-top: 24px;
padding: 16px;
border: 1px solid rgba(101, 230, 176, 0.2);
border-radius: 14px;
background:
radial-gradient(circle at 100% 0%, rgba(101, 230, 176, 0.09), transparent 45%),
rgba(255, 255, 255, 0.025);
}
.rule-card .eyebrow {
color: var(--mint);
}
select {
width: 100%;
height: 42px;
padding: 0 36px 0 12px;
appearance: none;
border: 1px solid var(--line);
border-radius: 9px;
outline: none;
background:
linear-gradient(45deg, transparent 50%, var(--muted) 50%) calc(100% - 16px) 18px / 5px 5px no-repeat,
linear-gradient(135deg, var(--muted) 50%, transparent 50%) calc(100% - 11px) 18px / 5px 5px no-repeat,
#10232b;
font-size: 12px;
font-weight: 700;
cursor: pointer;
}
select:focus-visible {
border-color: var(--mint);
box-shadow: 0 0 0 3px rgba(101, 230, 176, 0.08);
}
.rule-description {
min-height: 60px;
margin: 12px 0 15px;
color: #b6cbc8;
font-size: 12px;
line-height: 1.55;
}
.matrix-title {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.matrix {
display: grid;
grid-template-columns: 48px 1fr 1fr;
gap: 4px;
font-size: 9px;
}
.matrix > div {
display: flex;
align-items: center;
justify-content: center;
min-height: 30px;
padding: 5px;
border-radius: 6px;
}
.matrix .axis {
color: var(--muted);
text-align: center;
line-height: 1.2;
}
.matrix .cell {
color: #d9e8e4;
background: rgba(255, 255, 255, 0.045);
font-variant-numeric: tabular-nums;
transition: background 180ms ease, color 180ms ease;
}
.matrix .cell.high {
color: #071117;
background: var(--mint);
font-weight: 800;
}
.matrix-note {
margin: 9px 0 0;
color: #6f8d8c;
font-size: 9px;
line-height: 1.45;
}
.control-section {
margin-top: 24px;
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
font-size: 11px;
font-weight: 700;
}
.control-label output {
color: var(--mint);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
appearance: none;
border-radius: 99px;
outline: none;
background: #263b42;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid var(--panel);
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 0 1px var(--mint);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid var(--panel);
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 0 1px var(--mint);
}
.button-row {
display: grid;
grid-template-columns: 1.25fr 1fr 1fr;
gap: 7px;
margin-top: 20px;
}
button {
min-height: 38px;
padding: 0 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.04em;
cursor: pointer;
transition: transform 140ms ease, background 140ms ease, border-color 140ms ease;
}
button:hover {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.075);
border-color: rgba(188, 232, 226, 0.24);
}
button:active {
transform: translateY(0);
}
button.primary {
color: #071117;
border-color: var(--mint);
background: var(--mint);
}
.speed-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
margin-top: 12px;
}
.speed-row button {
min-height: 30px;
padding: 0;
color: var(--muted);
font-size: 9px;
}
.speed-row button.active {
color: var(--text);
border-color: rgba(101, 230, 176, 0.42);
background: rgba(101, 230, 176, 0.09);
}
.population-list {
display: grid;
gap: 8px;
margin-top: 13px;
}
.population-row {
display: grid;
grid-template-columns: 9px 1fr auto;
align-items: center;
gap: 10px;
padding: 11px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.018);
}
.species-dot {
width: 8px;
height: 8px;
border-radius: 50%;
}
.population-name {
font-size: 11px;
font-weight: 750;
}
.population-detail {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.population-value {
font-size: 12px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.metric {
padding: 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.018);
}
.metric span {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 6px;
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.coevolution-note {
margin-top: 21px;
padding-top: 17px;
border-top: 1px solid var(--line);
}
.signal {
display: inline-flex;
align-items: center;
gap: 7px;
margin-bottom: 8px;
color: var(--mint);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.signal::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
}
.stage {
display: grid;
grid-template-rows: minmax(0, 1fr) 184px;
min-width: 0;
min-height: 0;
background: #07131a;
}
.viewport {
position: relative;
min-height: 0;
overflow: hidden;
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.scene-topline {
position: absolute;
z-index: 3;
top: 18px;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 9px;
padding: 7px 11px;
border: 1px solid var(--line);
border-radius: 99px;
background: rgba(7, 19, 25, 0.62);
backdrop-filter: blur(10px);
color: var(--muted);
font-size: 9px;
letter-spacing: 0.09em;
text-transform: uppercase;
pointer-events: none;
}
.scene-topline i {
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--mint);
}
.population-hud {
position: absolute;
z-index: 4;
top: 20px;
width: 218px;
padding: 13px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(8, 22, 28, 0.76);
backdrop-filter: blur(12px);
box-shadow: var(--shadow);
pointer-events: none;
}
.population-hud.left {
left: 20px;
}
.population-hud.right {
right: 20px;
}
.hud-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.hud-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 11px;
font-weight: 800;
}
.hud-number {
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.frequency-track {
height: 5px;
margin-top: 11px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
.frequency-fill {
width: 50%;
height: 100%;
border-radius: inherit;
transition: width 350ms ease;
}
.frequency-labels {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.camera-hint {
position: absolute;
z-index: 3;
left: 20px;
bottom: 18px;
display: flex;
align-items: center;
gap: 8px;
color: rgba(177, 202, 199, 0.6);
font-size: 9px;
letter-spacing: 0.04em;
pointer-events: none;
}
.camera-hint span {
padding: 4px 7px;
border: 1px solid var(--line);
border-radius: 5px;
background: rgba(8, 22, 28, 0.56);
}
.chart-panel {
z-index: 6;
display: grid;
grid-template-columns: 175px minmax(0, 1fr) 190px;
gap: 18px;
align-items: center;
padding: 15px 22px;
border-top: 1px solid var(--line);
background:
linear-gradient(90deg, rgba(101, 230, 176, 0.025), transparent 28%),
#0a171e;
}
.chart-heading h2 {
margin: 0;
font-size: 14px;
letter-spacing: -0.015em;
}
.chart-heading p {
margin: 6px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.chart-svg {
width: 100%;
height: 150px;
overflow: visible;
}
.chart-side {
display: grid;
gap: 11px;
}
.legend-item {
display: grid;
grid-template-columns: 18px 1fr auto;
align-items: center;
gap: 8px;
color: #b7cbc8;
font-size: 10px;
}
.legend-line {
height: 2px;
border-radius: 99px;
}
.legend-value {
color: var(--text);
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.chart-stat {
display: flex;
justify-content: space-between;
padding-top: 10px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
}
.chart-stat strong {
color: var(--text);
font-variant-numeric: tabular-nums;
}
@media (max-width: 1050px) {
.workspace {
grid-template-columns: 285px minmax(0, 1fr);
}
.sidebar {
padding-inline: 16px;
}
.chart-panel {
grid-template-columns: 145px minmax(0, 1fr) 160px;
gap: 10px;
padding-inline: 14px;
}
.population-hud {
width: 190px;
}
}
@media (max-width: 760px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100dvh;
grid-template-rows: 62px auto;
}
.topbar {
position: sticky;
top: 0;
padding-inline: 15px;
}
.top-status span:last-child {
display: none;
}
.workspace {
display: flex;
flex-direction: column;
}
.stage {
order: 1;
grid-template-rows: 520px 176px;
}
.sidebar {
order: 2;
overflow: visible;
border-top: 1px solid var(--line);
border-right: 0;
}
.population-hud {
top: 52px;
width: calc(50% - 26px);
}
.population-hud.left {
left: 12px;
}
.population-hud.right {
right: 12px;
}
.scene-topline {
top: 14px;
}
.chart-panel {
grid-template-columns: 120px minmax(0, 1fr);
}
.chart-side {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.001ms !important;
animation-iteration-count: 1 !important;
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>MUTUAL</h1>
<p>Coevolution field lab</p>
</div>
</div>
<div class="top-status">
<i class="status-dot"></i>
<span id="liveStatus">Simulation running</span>
</div>
</header>
<main class="workspace">
<aside class="sidebar">
<p class="eyebrow">Experiment 01</p>
<h2 class="section-title">Two populations.<br>One shared trait.</h2>
<p class="section-copy">
Change the declared mating rule and watch the SPIRE signal evolve in
response to the other population.
</p>
<section class="rule-card">
<p class="eyebrow">Declared compatibility rule</p>
<select id="ruleSelect" aria-label="Compatibility rule">
<option value="match">Matching signals</option>
<option value="contrast">Contrasting signals</option>
<option value="open">No signal preference</option>
</select>
<p id="ruleDescription" class="rule-description"></p>
<div class="matrix-title">
<span>Local × other</span>
<span>Compatibility</span>
</div>
<div class="matrix" aria-label="Compatibility weight matrix">
<div class="axis"></div>
<div class="axis">Other<br>DOME</div>
<div class="axis">Other<br>SPIRE</div>
<div class="axis">Local<br>DOME</div>
<div id="m00" class="cell">1.00</div>
<div id="m01" class="cell">0.12</div>
<div class="axis">Local<br>SPIRE</div>
<div id="m10" class="cell">0.12</div>
<div id="m11" class="cell">1.00</div>
</div>
<p class="matrix-note">
Rows represent the local signal; columns represent the possible
partner signal.
</p>
</section>
<section class="control-section">
<div class="control-label">
<label for="mutation">Trait mutation</label>
<output id="mutationValue" for="mutation">4%</output>
</div>
<input
id="mutation"
type="range"
min="0"
max="16"
step="1"
value="4"
>
<div class="control-label" style="margin-top:18px">
<span>Generation speed</span>
<output id="speedValue">1×</output>
</div>
<div class="speed-row" aria-label="Simulation speed">
<button type="button" data-speed="0.5">0.5×</button>
<button type="button" data-speed="1" class="active">1×</button>
<button type="button" data-speed="2">2×</button>
</div>
</section>
<div class="button-row">
<button type="button" id="playButton" class="primary">Pause</button>
<button type="button" id="stepButton">Step</button>
<button type="button" id="resetButton">Reset</button>
</div>
<section class="control-section">
<p class="eyebrow">Trait frequency</p>
<div class="population-list">
<div class="population-row">
<i class="species-dot" style="background:var(--mint)"></i>
<div>
<div class="population-name">Mossbacks</div>
<div class="population-detail">Mint population · 24</div>
</div>
<div id="populationAFrequency" class="population-value">67%</div>
</div>
<div class="population-row">
<i class="species-dot" style="background:var(--amber)"></i>
<div>
<div class="population-name">Sunshapes</div>
<div class="population-detail">Amber population · 24</div>
</div>
<div id="populationBFrequency" class="population-value">42%</div>
</div>
</div>
<div class="metric-grid">
<div class="metric">
<span>Generation</span>
<strong id="generationMetric">00</strong>
</div>
<div class="metric">
<span>Population gap</span>
<strong id="gapMetric">25%</strong>
</div>
</div>
</section>
<section class="coevolution-note">
<div id="coevolutionSignal" class="signal">Awaiting divergence</div>
<p class="section-copy">
Coevolution occurs when each population changes the selective
environment of the other. Switch rules mid-run to compare the
resulting feedback loops.
</p>
</section>
</aside>
<section class="stage">
<div class="viewport" id="viewport">
<div id="scene" aria-label="Interactive 3D population diorama"></div>
<div class="scene-topline">
<i></i>
Living diorama · 24 individuals per population
</div>
<div class="population-hud left">
<div class="hud-heading">
<div class="hud-name">
<i class="species-dot" style="background:var(--mint)"></i>
Mossbacks
</div>
<span id="hudAValue" class="hud-number">67%</span>
</div>
<div class="frequency-track">
<div id="hudABar" class="frequency-fill" style="background:var(--mint)"></div>
</div>
<div class="frequency-labels">
<span>Spire</span>
<span>Dome</span>
</div>
</div>
<div class="population-hud right">
<div class="hud-heading">
<div class="hud-name">
<i class="species-dot" style="background:var(--amber)"></i>
Sunshapes
</div>
<span id="hudBValue" class="hud-number">42%</span>
</div>
<div class="frequency-track">
<div id="hudBBar" class="frequency-fill" style="background:var(--amber)"></div>
</div>
<div class="frequency-labels">
<span>Spire</span>
<span>Dome</span>
</div>
</div>
<div class="camera-hint">
<span>Drag</span> orbit
<span>Scroll</span> zoom
<span>Double-click</span> reset view
</div>
</div>
<section class="chart-panel">
<div class="chart-heading">
<p class="eyebrow">Selection trace</p>
<h2>SPIRE frequency</h2>
<p>Each point is one simulated generation.</p>
</div>
<svg
id="frequencyChart"
class="chart-svg"
viewBox="0 0 560 150"
role="img"
aria-label="Chart of spire trait frequencies over generations"
></svg>
<div class="chart-side">
<div class="legend-item">
<i class="legend-line" style="background:var(--mint)"></i>
<span>Mossbacks</span>
<strong id="chartAValue" class="legend-value">67%</strong>
</div>
<div class="legend-item">
<i class="legend-line" style="background:var(--amber)"></i>
<span>Sunshapes</span>
<strong id="chartBValue" class="legend-value">42%</strong>
</div>
<div class="chart-stat">
<span>Signal separation</span>
<strong id="separationValue">25%</strong>
</div>
<div class="chart-stat">
<span>Feedback state</span>
<strong id="feedbackValue">Starting</strong>
</div>
</div>
</section>
</section>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Simulation state
// ------------------------------------------------------------
const POPULATION_SIZE = 24;
const BASE_STEP_MS = 1350;
const rules = {
match: {
name: "Matching signals",
description:
"Like signals are compatible. Each population is selected toward the signal most common in its potential partners.",
matrix: [
[1.0, 0.12],
[0.12, 1.0]
]
},
contrast: {
name: "Contrasting signals",
description:
"Unlike signals are compatible. A local signal gains an edge when it is uncommon in the other population.",
matrix: [
[0.12, 1.0],
[1.0, 0.12]
]
},
open: {
name: "No signal preference",
description:
"Every signal pairing is equally compatible. Genetic drift and mutation become the only forces changing frequency.",
matrix: [
[1.0, 1.0],
[1.0, 1.0]
]
}
};
let currentRule = "match";
let generation = 0;
let populationA = [];
let populationB = [];
let history = [];
let running = true;
let speed = 1;
let lastStep = performance.now();
let generationPulse = 0;
const mutationInput = document.querySelector("#mutation");
const mutationValue = document.querySelector("#mutationValue");
const ruleSelect = document.querySelector("#ruleSelect");
const ruleDescription = document.querySelector("#ruleDescription");
const playButton = document.querySelector("#playButton");
const liveStatus = document.querySelector("#liveStatus");
function shuffledPopulation(spireCount) {
const values = [
...Array(spireCount).fill(1),
...Array(POPULATION_SIZE - spireCount).fill(0)
];
for (let i = values.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[values[i], values[j]] = [values[j], values[i]];
}
return values;
}
function frequencies(population) {
const spire = population.reduce((sum, value) => sum + value, 0);
return {
dome: (POPULATION_SIZE - spire) / POPULATION_SIZE,
spire: spire / POPULATION_SIZE
};
}
function resetSimulation() {
generation = 0;
populationA = shuffledPopulation(16);
populationB = shuffledPopulation(10);
const a = frequencies(populationA);
const b = frequencies(populationB);
history = [
{ generation, a: a.spire * 100, b: b.spire * 100 }
];
populationA.forEach((trait, index) => setCreatureTrait(creaturesA[index], trait));
populationB.forEach((trait, index) => setCreatureTrait(creaturesB[index], trait));
updateInterface();
renderChart();
}
// Average compatibility of each local signal against the other population.
function localFitnessWeights(otherFrequency, matrix) {
return {
dome:
otherFrequency.dome * matrix[0][0] +
otherFrequency.spire * matrix[0][1],
spire:
otherFrequency.dome * matrix[1][0] +
otherFrequency.spire * matrix[1][1]
};
}
function selectedDomeProbability(population, otherPopulation) {
const local = frequencies(population);
const other = frequencies(otherPopulation);
const weights = localFitnessWeights(other, rules[currentRule].matrix);
const weightedDome = local.dome * weights.dome;
const weightedSpire = local.spire * weights.spire;
const total = weightedDome + weightedSpire;
return total > 0 ? weightedDome / total : local.dome;
}
function evolvePopulation(population, otherPopulation) {
const selectedDome = selectedDomeProbability(population, otherPopulation);
const mutationRate = Number(mutationInput.value) / 100;
const next = [];
for (let i = 0; i < POPULATION_SIZE; i++) {
let inherited = Math.random() < selectedDome ? 0 : 1;
if (Math.random() < mutationRate) {
inherited = 1 - inherited;
}
next.push(inherited);
}
return next;
}
function advanceGeneration() {
const previousA = populationA;
const previousB = populationB;
populationA = evolvePopulation(previousA, previousB);
populationB = evolvePopulation(previousB, previousA);
generation++;
const a = frequencies(populationA);
const b = frequencies(populationB);
history.push({
generation,
a: a.spire * 100,
b: b.spire * 100
});
if (history.length > 40) {
history.shift();
}
populationA.forEach((trait, index) => setCreatureTrait(creaturesA[index], trait));
populationB.forEach((trait, index) => setCreatureTrait(creaturesB[index], trait));
generationPulse = 1;
updateInterface();
renderChart();
}
function feedbackState() {
if (history.length < 3) {
return "Starting";
}
const recent = history.slice(-6);
let product = 0;
for (let i = 1; i < recent.length; i++) {
const da = recent[i].a - recent[i - 1].a;
const db = recent[i].b - recent[i - 1].b;
product += da * db;
}
if (product < -2) return "Reciprocal";
if (product > 2) return "Coupled";
return "Mixed";
}
function updateInterface() {
const a = frequencies(populationA);
const b = frequencies(populationB);
const aPercent = Math.round(a.spire * 100);
const bPercent = Math.round(b.spire * 100);
const gap = Math.abs(aPercent - bPercent);
document.querySelector("#populationAFrequency").textContent = `${aPercent}%`;
document.querySelector("#populationBFrequency").textContent = `${bPercent}%`;
document.querySelector("#hudAValue").textContent = `${aPercent}%`;
document.querySelector("#hudBValue").textContent = `${bPercent}%`;
document.querySelector("#chartAValue").textContent = `${aPercent}%`;
document.querySelector("#chartBValue").textContent = `${bPercent}%`;
document.querySelector("#hudABar").style.width = `${aPercent}%`;
document.querySelector("#hudBBar").style.width = `${bPercent}%`;
document.querySelector("#generationMetric").textContent =
String(generation).padStart(2, "0");
document.querySelector("#gapMetric").textContent = `${gap}%`;
document.querySelector("#separationValue").textContent = `${gap}%`;
document.querySelector("#feedbackValue").textContent = feedbackState();
const signal = document.querySelector("#coevolutionSignal");
signal.textContent =
feedbackState() === "Reciprocal"
? "Reciprocal selection detected"
: feedbackState() === "Coupled"
? "Coupled frequency shift"
: "Mixed feedback";
signal.style.color =
feedbackState() === "Reciprocal"
? "var(--mint)"
: feedbackState() === "Coupled"
? "var(--amber)"
: "var(--muted)";
}
function updateRuleUI() {
const rule = rules[currentRule];
ruleDescription.textContent = rule.description;
const cells = [
[document.querySelector("#m00"), rule.matrix[0][0]],
[document.querySelector("#m01"), rule.matrix[0][1]],
[document.querySelector("#m10"), rule.matrix[1][0]],
[document.querySelector("#m11"), rule.matrix[1][1]]
];
cells.forEach(([cell, value]) => {
cell.textContent = value.toFixed(2);
cell.classList.toggle("high", value >= 0.5);
});
const colors = {
match: 0x65e6b0,
contrast: 0xffb45e,
open: 0x8fa7b0
};
if (centralCoreMaterial) {
centralCoreMaterial.color.setHex(colors[currentRule]);
centralCoreMaterial.emissive.setHex(colors[currentRule]);
}
}
function renderChart() {
const svg = document.querySelector("#frequencyChart");
const width = 560;
const height = 150;
const padding = { left: 30, right: 10, top: 10, bottom: 23 };
const plotWidth = width - padding.left - padding.right;
const plotHeight = height - padding.top - padding.bottom;
const visible = history.slice(-18);
const maxGeneration = Math.max(1, visible[visible.length - 1].generation);
const x = generationValue =>
padding.left +
(generationValue / maxGeneration) * plotWidth;
const y = value =>
padding.top + (1 - value / 100) * plotHeight;
const pathFor = key =>
visible
.map(
(point, index) =>
`${index === 0 ? "M" : "L"} ${x(point.generation).toFixed(2)} ${y(point[key]).toFixed(2)}`
)
.join(" ");
const areaFor = key => {
if (!visible.length) return "";
const baseline = padding.top + plotHeight;
return (
`${pathFor(key)} ` +
`L ${x(visible[visible.length - 1].generation).toFixed(2)} ${baseline} ` +
`L ${x(visible[0].generation).toFixed(2)} ${baseline} Z`
);
};
const grid = [0, 25, 50, 75, 100]
.map(value => {
const yy = y(value);
return `
<line x1="${padding.left}" y1="${yy}" x2="${width - padding.right}" y2="${yy}"
stroke="rgba(188,232,226,0.09)" stroke-width="1" />
<text x="${padding.left - 7}" y="${yy + 3}" text-anchor="end"
fill="#6f8d8c" font-size="8">${value}%</text>
`;
})
.join("");
const latest = visible[visible.length - 1];
svg.innerHTML = `
<defs>
<linearGradient id="mintArea" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stop-color="#65e6b0" stop-opacity="0.17" />
<stop offset="100%" stop-color="#65e6b0" stop-opacity="0" />
</linearGradient>
<linearGradient id="amberArea" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stop-color="#ffb45e" stop-opacity="0.14" />
<stop offset="100%" stop-color="#ffb45e" stop-opacity="0" />
</linearGradient>
</defs>
${grid}
<path d="${areaFor("a")}" fill="url(#mintArea)" />
<path d="${areaFor("b")}" fill="url(#amberArea)" />
<path d="${pathFor("a")}" fill="none" stroke="#65e6b0"
stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" />
<path d="${pathFor("b")}" fill="none" stroke="#ffb45e"
stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" />
<circle cx="${x(latest.generation)}" cy="${y(latest.a)}" r="3.5"
fill="#65e6b0" stroke="#0a171e" stroke-width="2" />
<circle cx="${x(latest.generation)}" cy="${y(latest.b)}" r="3.5"
fill="#ffb45e" stroke="#0a171e" stroke-width="2" />
<text x="${padding.left}" y="${height - 5}" fill="#6f8d8c" font-size="8">
GEN ${visible[0].generation}
</text>
<text x="${width - padding.right}" y="${height - 5}" text-anchor="end"
fill="#6f8d8c" font-size="8">
GEN ${latest.generation}
</text>
`;
}
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const viewport = document.querySelector("#viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131a);
scene.fog = new THREE.Fog(0x07131a, 18, 36);
const camera = new THREE.PerspectiveCamera(35, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xbceee8, 0x16252d, 1.8);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xd8fff1, 3.4);
keyLight.position.set(-5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xffbd78, 2.1);
rimLight.position.set(8, 5, -7);
scene.add(rimLight);
const mintLight = new THREE.PointLight(0x65e6b0, 12, 12, 2);
mintLight.position.set(-4, 2.5, 2);
scene.add(mintLight);
const amberLight = new THREE.PointLight(0xffa951, 12, 12, 2);
amberLight.position.set(4, 2.5, 2);
scene.add(amberLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x08141a,
roughness: 0.94,
metalness: 0.03
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.52;
ground.receiveShadow = true;
scene.add(ground);
const platformMaterialA = new THREE.MeshStandardMaterial({
color: 0x173b37,
roughness: 0.82,
metalness: 0.08,
flatShading: true
});
const platformMaterialB = new THREE.MeshStandardMaterial({
color: 0x493226,
roughness: 0.84,
metalness: 0.07,
flatShading: true
});
const platformTopMaterialA = new THREE.MeshStandardMaterial({
color: 0x234c43,
roughness: 0.92,
flatShading: true
});
const platformTopMaterialB = new THREE.MeshStandardMaterial({
color: 0x60442d,
roughness: 0.92,
flatShading: true
});
function createPlatform(x, side) {
const group = new THREE.Group();
group.position.x = x;
const base = new THREE.Mesh(
new THREE.CylinderGeometry(3.18, 3.38, 0.48, 6),
side === "A" ? platformMaterialA : platformMaterialB
);
base.position.y = -0.27;
base.rotation.y = Math.PI / 6;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const top = new THREE.Mesh(
new THREE.CylinderGeometry(3.04, 3.12, 0.12, 6),
side === "A" ? platformTopMaterialA : platformTopMaterialB
);
top.position.y = 0.015;
top.rotation.y = Math.PI / 6;
top.receiveShadow = true;
group.add(top);
const rimMaterial = new THREE.MeshStandardMaterial({
color: side === "A" ? 0x65e6b0 : 0xffb45e,
emissive: side === "A" ? 0x174d39 : 0x603513,
emissiveIntensity: 1.2,
roughness: 0.45,
metalness: 0.18
});
const rim = new THREE.Mesh(
new THREE.TorusGeometry(3.08, 0.025, 4, 6),
rimMaterial
);
rim.rotation.x = Math.PI / 2;
rim.rotation.z = Math.PI / 6;
rim.position.y = 0.08;
group.add(rim);
scene.add(group);
return group;
}
createPlatform(-3.45, "A");
createPlatform(3.45, "B");
const crystalGeometry = new THREE.ConeGeometry(0.16, 0.62, 5);
const crystalMaterialA = new THREE.MeshStandardMaterial({
color: 0x4a9d84,
emissive: 0x123c31,
roughness: 0.58,
flatShading: true
});
const crystalMaterialB = new THREE.MeshStandardMaterial({
color: 0xb77743,
emissive: 0x49230f,
roughness: 0.6,
flatShading: true
});
function addCrystal(x, z, height, material, rotation = 0) {
const crystal = new THREE.Mesh(crystalGeometry, material);
crystal.position.set(x, 0.22 + height / 2, z);
crystal.scale.setScalar(height / 0.62);
crystal.rotation.y = rotation;
crystal.castShadow = true;
scene.add(crystal);
}
[
[-6.15, -1.6, 0.8],
[-5.8, 1.6, 0.52],
[-1.7, -2.55, 0.42],
[1.6, 2.7, 0.46]
].forEach(([x, z, h]) => addCrystal(x, z, h, crystalMaterialA, x));
[
[6.15, -1.5, 0.75],
[5.8, 1.65, 0.5],
[1.7, -2.6, 0.43],
[-1.65, 2.65, 0.48]
].forEach(([x, z, h]) => addCrystal(x, z, h, crystalMaterialB, x));
// Central gene-flow gate.
const connectionCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-1.05, 0.35, 0),
new THREE.Vector3(-0.55, 0.95, 0),
new THREE.Vector3(0, 1.45, 0),
new THREE.Vector3(0.55, 0.95, 0),
new THREE.Vector3(1.05, 0.35, 0)
]);
const connectionMaterial = new THREE.MeshStandardMaterial({
color: 0x789995,
emissive: 0x24413f,
emissiveIntensity: 1.5,
roughness: 0.35,
transparent: true,
opacity: 0.7
});
const connection = new THREE.Mesh(
new THREE.TubeGeometry(connectionCurve, 48, 0.027, 5, false),
connectionMaterial
);
scene.add(connection);
const centralCoreMaterial = new THREE.MeshStandardMaterial({
color: 0x65e6b0,
emissive: 0x65e6b0,
emissiveIntensity: 1.3,
roughness: 0.25,
metalness: 0.3,
flatShading: true
});
const centralCore = new THREE.Mesh(
new THREE.OctahedronGeometry(0.28, 0),
centralCoreMaterial
);
centralCore.position.set(0, 1.45, 0);
centralCore.castShadow = true;
scene.add(centralCore);
const gateMaterial = new THREE.MeshStandardMaterial({
color: 0x9cd9cd,
emissive: 0x315d54,
emissiveIntensity: 1.25,
roughness: 0.4,
metalness: 0.25
});
const gateRingA = new THREE.Mesh(
new THREE.TorusGeometry(0.66, 0.025, 4, 36),
gateMaterial
);
gateRingA.position.copy(centralCore.position);
scene.add(gateRingA);
const gateRingB = new THREE.Mesh(
new THREE.TorusGeometry(0.88, 0.018, 4, 36),
gateMaterial
);
gateRingB.position.copy(centralCore.position);
gateRingB.rotation.y = Math.PI / 2;
scene.add(gateRingB);
const flowParticles = [];
const particleGeometry = new THREE.IcosahedronGeometry(0.055, 0);
const particleMaterialA = new THREE.MeshStandardMaterial({
color: 0x8affd0,
emissive: 0x3bdc9d,
emissiveIntensity: 1.8
});
const particleMaterialB = new THREE.MeshStandardMaterial({
color: 0xffce8b,
emissive: 0xff9d3d,
emissiveIntensity: 1.8
});
for (let i = 0; i < 22; i++) {
const mesh = new THREE.Mesh(
particleGeometry,
Math.random() > 0.5 ? particleMaterialA : particleMaterialB
);
mesh.scale.setScalar(0.7 + Math.random() * 0.9);
scene.add(mesh);
flowParticles.push({
mesh,
offset: Math.random(),
speed: 0.12 + Math.random() * 0.13,
direction: Math.random() > 0.5 ? 1 : -1
});
}
// Floating dust adds depth without hiding the low-poly silhouettes.
const dustGeometry = new THREE.BufferGeometry();
const dustPositions = new Float32Array(280 * 3);
for (let i = 0; i < 280; i++) {
dustPositions[i * 3] = (Math.random() - 0.5) * 25;
dustPositions[i * 3 + 1] = Math.random() * 8 - 0.2;
dustPositions[i * 3 + 2] = (Math.random() - 0.5) * 18;
}
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x8fc6bd,
size: 0.025,
transparent: true,
opacity: 0.32,
depthWrite: false
})
);
scene.add(dust);
// ------------------------------------------------------------
// Low-poly creatures
// ------------------------------------------------------------
const materials = {
mossBody: new THREE.MeshStandardMaterial({
color: 0x4fba8f,
roughness: 0.76,
flatShading: true
}),
mossHead: new THREE.MeshStandardMaterial({
color: 0x75d6aa,
roughness: 0.72,
flatShading: true
}),
mossDark: new THREE.MeshStandardMaterial({
color: 0x204c43,
roughness: 0.88,
flatShading: true
}),
sunBody: new THREE.MeshStandardMaterial({
color: 0xe69b54,
roughness: 0.76,
flatShading: true
}),
sunHead: new THREE.MeshStandardMaterial({
color: 0xffbf72,
roughness: 0.72,
flatShading: true
}),
sunDark: new THREE.MeshStandardMaterial({
color: 0x6d4229,
roughness: 0.9,
flatShading: true
}),
eye: new THREE.MeshStandardMaterial({
color: 0x081116,
roughness: 0.3,
metalness: 0.05
}),
spireA: new THREE.MeshStandardMaterial({
color: 0xd8fff0,
emissive: 0x3ac994,
emissiveIntensity: 0.75,
roughness: 0.36,
flatShading: true
}),
spireB: new THREE.MeshStandardMaterial({
color: 0xfff1d2,
emissive: 0xed963d,
emissiveIntensity: 0.75,
roughness: 0.36,
flatShading: true
}),
domeA: new THREE.MeshStandardMaterial({
color: 0x1e4940,
roughness: 0.82,
flatShading: true
}),
domeB: new THREE.MeshStandardMaterial({
color: 0x75482d,
roughness: 0.82,
flatShading: true
})
};
function mesh(geometry, material, position, scale) {
const object = new THREE.Mesh(geometry, material);
if (position) object.position.set(...position);
if (scale) object.scale.set(...scale);
object.castShadow = true;
object.receiveShadow = true;
return object;
}
function createMossback() {
const group = new THREE.Group();
group.add(
mesh(
new THREE.IcosahedronGeometry(0.34, 0),
materials.mossBody,
[0, 0.38, 0],
[1.05, 0.68, 1.22]
)
);
group.add(
mesh(
new THREE.IcosahedronGeometry(0.24, 0),
materials.mossHead,
[0, 0.49, 0.32],
[1.0, 0.9, 1.05]
)
);
const legGeometry = new THREE.ConeGeometry(0.075, 0.2, 4);
[
[-0.22, 0.16, 0.2],
[0.22, 0.16, 0.2],
[-0.22, 0.16, -0.2],
[0.22, 0.16, -0.2]
].forEach(position => {
group.add(mesh(legGeometry, materials.mossDark, position));
});
const eyeGeometry = new THREE.SphereGeometry(0.033, 5, 4);
group.add(mesh(eyeGeometry, materials.eye, [-0.11, 0.54, 0.525]));
group.add(mesh(eyeGeometry, materials.eye, [0.11, 0.54, 0.525]));
const tail = mesh(
new THREE.ConeGeometry(0.12, 0.3, 5),
materials.mossDark,
[0, 0.41, -0.37]
);
tail.rotation.x = -Math.PI / 2.4;
group.add(tail);
const spire = new THREE.Group();
spire.add(
mesh(
new THREE.ConeGeometry(0.135, 0.38, 5),
materials.spireA,
[0, 0.72, -0.04]
)
);
group.add(spire);
const dome = new THREE.Group();
dome.add(
mesh(
new THREE.IcosahedronGeometry(0.21, 0),
materials.domeA,
[0, 0.59, -0.04],
[1.15, 0.43, 1.1]
)
);
group.add(dome);
return { group, spire, dome };
}
function createSunshape() {
const group = new THREE.Group();
group.add(
mesh(
new THREE.OctahedronGeometry(0.34, 0),
materials.sunBody,
[0, 0.39, 0],
[0.95, 0.9, 1.12]
)
);
group.add(
mesh(
new THREE.IcosahedronGeometry(0.225, 0),
materials.sunHead,
[0, 0.67, 0.18],
[1.0, 0.95, 1.0]
)
);
const legGeometry = new THREE.CylinderGeometry(0.055, 0.075, 0.28, 5);
group.add(mesh(legGeometry, materials.sunDark, [-0.16, 0.17, 0.02]));
group.add(mesh(legGeometry, materials.sunDark, [0.16, 0.17, 0.02]));
const beak = mesh(
new THREE.ConeGeometry(0.105, 0.25, 4),
materials.sunHead,
[0, 0.65, 0.43]
);
beak.rotation.x = Math.PI / 2;
group.add(beak);
const eyeGeometry = new THREE.SphereGeometry(0.031, 5, 4);
group.add(mesh(eyeGeometry, materials.eye, [-0.105, 0.72, 0.355]));
group.add(mesh(eyeGeometry, materials.eye, [0.105, 0.72, 0.355]));
const tail = mesh(
new THREE.ConeGeometry(0.14, 0.34, 4),
materials.sunDark,
[0, 0.48, -0.38]
);
tail.rotation.x = -Math.PI / 2.25;
group.add(tail);
const spire = new THREE.Group();
spire.add(
mesh(
new THREE.ConeGeometry(0.125, 0.34, 5),
materials.spireB,
[0, 0.98, 0.13]
)
);
group.add(spire);
const dome = new THREE.Group();
dome.add(
mesh(
new THREE.OctahedronGeometry(0.19, 0),
materials.domeB,
[0, 0.84, 0.15],
[1.15, 0.45, 1.1]
)
);
group.add(dome);
return { group, spire, dome };
}
function setCreatureTrait(creature, trait) {
if (!creature) return;
creature.spire.visible = trait === 1;
creature.dome.visible = trait === 0;
}
const ringLayout = [
{ radius: 0.72, count: 6, offset: 0.12 },
{ radius: 1.52, count: 8, offset: 0.34 },
{ radius: 2.34, count: 10, offset: 0.05 }
];
function createPopulation(species, x) {
const creatures = [];
let index = 0;
ringLayout.forEach(ring => {
for (let i = 0; i < ring.count; i++) {
const angle =
(i / ring.count) * Math.PI * 2 +
ring.offset +
(Math.random() - 0.5) * 0.08;
const radius = ring.radius + (Math.random() - 0.5) * 0.12;
const localX = Math.sin(angle) * radius;
const localZ = Math.cos(angle) * radius;
const creature =
species === "A" ? createMossback() : createSunshape();
creature.group.position.set(
x + localX,
0.09,
localZ
);
creature.group.rotation.y = angle + (Math.random() - 0.5) * 0.2;
creature.group.scale.setScalar(
species === "A" ? 0.76 : 0.79
);
creature.baseY = 0.09;
creature.phase = Math.random() * Math.PI * 2;
creature.spire.scale.setScalar(0.001);
creature.dome.scale.setScalar(0.001);
scene.add(creature.group);
creatures.push(creature);
index++;
}
});
return creatures;
}
const creaturesA = createPopulation("A", -3.45);
const creaturesB = createPopulation("B", 3.45);
// ------------------------------------------------------------
// Camera controls implemented without external addons
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.5, 0);
const cameraState = {
radius: 17,
phi: 1.02,
theta: 0.04
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + deltaY * 0.004,
0.55,
1.35
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
10,
25
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.radius = 17;
cameraState.phi = 1.02;
cameraState.theta = 0.04;
});
function resizeRenderer() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}
new ResizeObserver(resizeRenderer).observe(viewport);
resizeRenderer();
updateCamera();
// ------------------------------------------------------------
// UI events
// ------------------------------------------------------------
ruleSelect.addEventListener("change", () => {
currentRule = ruleSelect.value;
updateRuleUI();
lastStep = performance.now();
});
mutationInput.addEventListener("input", () => {
mutationValue.textContent = `${mutationInput.value}%`;
});
playButton.addEventListener("click", () => {
running = !running;
playButton.textContent = running ? "Pause" : "Play";
liveStatus.textContent = running
? "Simulation running"
: "Simulation paused";
lastStep = performance.now();
});
document.querySelector("#stepButton").addEventListener("click", () => {
advanceGeneration();
lastStep = performance.now();
});
document.querySelector("#resetButton").addEventListener("click", () => {
resetSimulation();
lastStep = performance.now();
});
document.querySelectorAll("[data-speed]").forEach(button => {
button.addEventListener("click", () => {
speed = Number(button.dataset.speed);
document.querySelector("#speedValue").textContent = `${speed}×`;
document.querySelectorAll("[data-speed]").forEach(item => {
item.classList.toggle("active", item === button);
});
lastStep = performance.now();
});
});
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animateCreatures(creatures, time) {
creatures.forEach((creature, index) => {
creature.group.position.y =
creature.baseY + Math.sin(time * 2.1 + creature.phase) * 0.026;
const turn = Math.sin(time * 0.55 + index * 1.7) * 0.025;
creature.group.rotation.y += turn * 0.002;
const targetScale = creature.spire.visible ? 1 : 0.001;
creature.spire.scale.lerp(
new THREE.Vector3(targetScale, targetScale, targetScale),
0.16
);
const domeScale = creature.dome.visible ? 1 : 0.001;
creature.dome.scale.lerp(
new THREE.Vector3(domeScale, domeScale, domeScale),
0.16
);
});
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
if (running && performance.now() - lastStep >= BASE_STEP_MS / speed) {
advanceGeneration();
lastStep = performance.now();
}
animateCreatures(creaturesA, elapsed);
animateCreatures(creaturesB, elapsed);
gateRingA.rotation.z += delta * 0.34;
gateRingB.rotation.x += delta * 0.25;
centralCore.rotation.y += delta * 0.65;
centralCore.rotation.x += delta * 0.31;
generationPulse *= 0.92;
const pulseScale = 1 + generationPulse * 0.24;
centralCore.scale.setScalar(pulseScale);
flowParticles.forEach(particle => {
let t = (particle.offset + elapsed * particle.speed) % 1;
if (particle.direction < 0) {
t = 1 - t;
}
const point = connectionCurve.getPoint(t);
particle.mesh.position.copy(point);
particle.mesh.position.z += Math.sin(elapsed * 1.7 + particle.offset * 9) * 0.12;
});
dust.rotation.y = elapsed * 0.008;
updateCamera();
renderer.render(scene, camera);
}
updateRuleUI();
resetSimulation();
animate();
</script>
</body>
</html>
```