Prompt specification
Build or customize the Three.js 3D scene "Rule-and-initial-state exploration table" 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: Rule-and-initial-state exploration table
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rule-and-initial-state-exploration-table-x1860
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts cellular automata with local update rules. Let users choose a small set of automaton rules and starting patterns; step the simulation deterministically and separate effects of the rule from effects of initialization. Low-poly.
Working Three.js Starter (rule-and-initial-state-exploration-table-x1860):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cellular Field Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(12, 24, 39, 0.94);
--panel-2: rgba(17, 33, 52, 0.78);
--line: rgba(151, 190, 220, 0.16);
--line-strong: rgba(151, 190, 220, 0.28);
--text: #eaf4fb;
--muted: #91a7b9;
--accent: #61e6c4;
--accent-2: #ff9a72;
--shadow: rgba(0, 0, 0, 0.35);
}
* {
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;
}
button,
select,
input {
font: inherit;
}
button,
select {
color: inherit;
}
.app {
display: grid;
grid-template-columns: 342px minmax(0, 1fr);
width: 100%;
height: 100%;
}
.controls {
position: relative;
z-index: 10;
display: flex;
flex-direction: column;
min-height: 0;
padding: 24px 22px 18px;
overflow: auto;
background:
linear-gradient(180deg, rgba(15, 31, 49, 0.98), rgba(8, 19, 33, 0.98));
border-right: 1px solid var(--line);
box-shadow: 18px 0 60px rgba(0, 0, 0, 0.16);
scrollbar-width: thin;
scrollbar-color: #294158 transparent;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
margin-bottom: 22px;
}
.brand-mark {
position: relative;
width: 42px;
height: 42px;
flex: 0 0 auto;
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
width: 18px;
height: 18px;
border: 1px solid var(--accent);
transform: rotate(30deg);
box-shadow: inset 0 0 14px rgba(97, 230, 196, 0.15);
}
.brand-mark::before {
top: 3px;
left: 3px;
}
.brand-mark::after {
right: 3px;
bottom: 3px;
border-color: var(--accent-2);
}
.eyebrow {
margin: 0 0 4px;
color: var(--accent);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 21px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.brand-subtitle {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 21px;
padding: 9px 11px;
background: rgba(255, 255, 255, 0.025);
border: 1px solid var(--line);
border-radius: 10px;
}
.status-label {
display: flex;
align-items: center;
gap: 8px;
color: #b9c9d5;
font-size: 11px;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #6f8496;
box-shadow: 0 0 0 4px rgba(111, 132, 150, 0.1);
}
.status-dot.running {
background: var(--accent);
box-shadow: 0 0 0 4px rgba(97, 230, 196, 0.11);
animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.45;
}
}
.status-code {
color: #62798b;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
letter-spacing: 0.08em;
}
.section {
padding: 18px 0;
border-top: 1px solid var(--line);
}
.section:first-of-type {
border-top: 0;
padding-top: 0;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 12px;
color: #cbd9e3;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-title span:last-child {
color: #5e7487;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: none;
}
.experiment-card {
position: relative;
padding: 13px;
margin-bottom: 10px;
background: var(--panel-2);
border: 1px solid var(--line);
border-radius: 12px;
}
.experiment-card.variant {
border-color: rgba(97, 230, 196, 0.28);
box-shadow: inset 0 0 24px rgba(97, 230, 196, 0.025);
}
.card-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
}
.card-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.card-swatch {
width: 7px;
height: 7px;
border-radius: 50%;
background: #7f95a8;
}
.variant .card-swatch {
background: var(--accent);
box-shadow: 0 0 10px rgba(97, 230, 196, 0.55);
}
.card-code {
color: #698095;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
}
.field {
display: grid;
grid-template-columns: 71px minmax(0, 1fr);
align-items: center;
gap: 8px;
margin-top: 8px;
}
.field label {
color: var(--muted);
font-size: 10px;
}
select {
width: 100%;
height: 35px;
padding: 0 30px 0 10px;
overflow: hidden;
text-overflow: ellipsis;
background:
linear-gradient(45deg, transparent 50%, #71879a 50%) calc(100% - 14px) 15px / 5px 5px no-repeat,
linear-gradient(135deg, #71879a 50%, transparent 50%) calc(100% - 9px) 15px / 5px 5px no-repeat,
rgba(4, 13, 24, 0.62);
border: 1px solid var(--line-strong);
border-radius: 8px;
outline: none;
font-size: 11px;
cursor: pointer;
appearance: none;
}
select:focus-visible {
border-color: rgba(97, 230, 196, 0.7);
box-shadow: 0 0 0 3px rgba(97, 230, 196, 0.08);
}
.isolation {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
.isolation button,
.secondary-button {
min-height: 36px;
padding: 8px;
color: #aebfcd;
background: rgba(255, 255, 255, 0.035);
border: 1px solid var(--line);
border-radius: 8px;
font-size: 10px;
line-height: 1.25;
cursor: pointer;
transition: 160ms ease;
}
.isolation button:hover,
.secondary-button:hover {
color: var(--text);
background: rgba(97, 230, 196, 0.07);
border-color: rgba(97, 230, 196, 0.34);
transform: translateY(-1px);
}
.compare-switch {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 12px;
padding: 10px 11px;
background: rgba(255, 255, 255, 0.025);
border: 1px solid var(--line);
border-radius: 9px;
cursor: pointer;
}
.compare-switch-text {
display: grid;
gap: 3px;
}
.compare-switch strong {
font-size: 11px;
font-weight: 650;
}
.compare-switch small {
color: var(--muted);
font-size: 9px;
}
.switch {
position: relative;
width: 35px;
height: 20px;
flex: 0 0 auto;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: absolute;
inset: 0;
background: #273847;
border: 1px solid #3a4d5d;
border-radius: 20px;
transition: 160ms ease;
}
.switch-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 12px;
height: 12px;
background: #8fa1af;
border-radius: 50%;
transition: 160ms ease;
}
.switch input:checked + .switch-track {
background: rgba(97, 230, 196, 0.22);
border-color: rgba(97, 230, 196, 0.55);
}
.switch input:checked + .switch-track::after {
left: 18px;
background: var(--accent);
box-shadow: 0 0 8px rgba(97, 230, 196, 0.5);
}
.transport {
display: grid;
grid-template-columns: 1fr 1fr 0.72fr;
gap: 8px;
}
.transport button {
height: 40px;
border-radius: 9px;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: 160ms ease;
}
.primary-button {
color: #062019;
background: var(--accent);
border: 1px solid var(--accent);
box-shadow: 0 7px 22px rgba(97, 230, 196, 0.12);
}
.primary-button:hover {
background: #80f0d4;
transform: translateY(-1px);
}
.primary-button.running {
color: #fff1e9;
background: var(--accent-2);
border-color: var(--accent-2);
}
.transport .secondary-button {
min-height: 40px;
}
.speed-row {
display: grid;
grid-template-columns: auto 1fr 44px;
align-items: center;
gap: 9px;
margin-top: 13px;
color: var(--muted);
font-size: 10px;
}
input[type="range"] {
width: 100%;
height: 3px;
accent-color: var(--accent);
cursor: pointer;
}
.speed-value {
color: #9eb3c2;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
text-align: right;
}
.rule-card {
padding: 14px;
background:
radial-gradient(circle at 100% 0%, rgba(97, 230, 196, 0.08), transparent 48%),
rgba(255, 255, 255, 0.025);
border: 1px solid var(--line);
border-radius: 12px;
}
.rule-card h3 {
margin: 0 0 5px;
font-size: 15px;
letter-spacing: -0.02em;
}
.rule-code-large {
display: inline-block;
margin-bottom: 9px;
padding: 4px 7px;
color: var(--accent);
background: rgba(97, 230, 196, 0.07);
border: 1px solid rgba(97, 230, 196, 0.14);
border-radius: 5px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
.rule-description {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.control-note {
margin: 11px 0 0;
color: #667d90;
font-size: 9px;
line-height: 1.5;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 40%, rgba(32, 66, 92, 0.25), transparent 55%),
#07111f;
}
#viewport {
position: absolute;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
.stage-header {
position: absolute;
top: 23px;
left: 25px;
z-index: 3;
pointer-events: none;
}
.stage-kicker {
margin-bottom: 6px;
color: #5f7b90;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.stage-title {
margin: 0;
font-size: 16px;
font-weight: 650;
letter-spacing: -0.02em;
}
.stage-chips {
display: flex;
gap: 7px;
margin-top: 10px;
}
.chip {
padding: 5px 8px;
color: #94adbe;
background: rgba(7, 17, 31, 0.65);
border: 1px solid var(--line);
border-radius: 6px;
backdrop-filter: blur(10px);
font-size: 9px;
}
.pane-label {
position: absolute;
z-index: 4;
top: 82px;
display: flex;
align-items: center;
gap: 8px;
padding: 7px 9px;
color: #c2d2dd;
background: rgba(7, 17, 31, 0.72);
border: 1px solid var(--line);
border-radius: 8px;
backdrop-filter: blur(12px);
pointer-events: none;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.pane-label::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: #7f95a8;
}
.pane-label.variant::before {
background: var(--accent);
box-shadow: 0 0 9px rgba(97, 230, 196, 0.55);
}
.legend {
position: absolute;
left: 24px;
bottom: 22px;
z-index: 3;
display: flex;
align-items: center;
gap: 16px;
padding: 9px 11px;
color: #71889a;
background: rgba(7, 17, 31, 0.62);
border: 1px solid var(--line);
border-radius: 9px;
backdrop-filter: blur(10px);
pointer-events: none;
font-size: 9px;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-cell {
width: 10px;
height: 10px;
background: #163044;
border: 1px solid #315069;
clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
}
.legend-cell.live {
background: var(--accent);
border-color: #a6f8e4;
box-shadow: 0 0 8px rgba(97, 230, 196, 0.3);
}
.hud {
position: absolute;
right: 24px;
bottom: 22px;
z-index: 3;
display: flex;
gap: 20px;
padding: 10px 13px;
background: rgba(7, 17, 31, 0.68);
border: 1px solid var(--line);
border-radius: 9px;
backdrop-filter: blur(10px);
pointer-events: none;
}
.hud-item {
display: grid;
gap: 3px;
}
.hud-item span:first-child {
color: #587186;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.12em;
}
.hud-item span:last-child {
color: #c6d5df;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
}
.camera-hint {
position: absolute;
top: 24px;
right: 24px;
z-index: 3;
color: #60788b;
font-size: 9px;
text-align: right;
line-height: 1.55;
pointer-events: none;
}
.toast {
position: absolute;
left: 50%;
bottom: 78px;
z-index: 8;
max-width: 90%;
padding: 10px 13px;
color: #cde0ea;
background: rgba(9, 22, 36, 0.92);
border: 1px solid rgba(97, 230, 196, 0.3);
border-radius: 9px;
box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(12px);
opacity: 0;
transform: translate(-50%, 8px);
pointer-events: none;
transition: 200ms ease;
font-size: 10px;
text-align: center;
}
.toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
.credits {
margin-top: auto;
padding-top: 18px;
color: #4f6577;
font-size: 9px;
line-height: 1.5;
}
@media (max-width: 900px) {
.app {
grid-template-columns: 300px minmax(0, 1fr);
}
.controls {
padding-inline: 17px;
}
.camera-hint {
display: none;
}
}
@media (max-width: 700px) {
html,
body {
height: auto;
min-height: 100%;
overflow: auto;
}
.app {
display: flex;
flex-direction: column;
height: auto;
}
.controls {
overflow: visible;
border-right: 0;
border-bottom: 1px solid var(--line);
}
.stage {
height: 72vh;
min-height: 540px;
}
.credits {
margin-top: 8px;
}
.stage-header {
top: 18px;
left: 18px;
}
.pane-label {
top: 104px;
font-size: 8px;
}
.legend {
left: 14px;
bottom: 14px;
gap: 9px;
}
.hud {
right: 14px;
bottom: 14px;
gap: 11px;
}
}
</style>
</head>
<body>
<div class="app">
<aside class="controls">
<header class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<p class="eyebrow">Cellular Field Lab</p>
<h1>Local rules. Global patterns.</h1>
<p class="brand-subtitle">Deterministic 2D automaton laboratory</p>
</div>
</header>
<div class="status-row">
<div class="status-label">
<span id="statusDot" class="status-dot"></span>
<span id="statusText">Simulation paused</span>
</div>
<span class="status-code">MOORE / 8N</span>
</div>
<section class="section">
<h2 class="section-title">
<span>A/B experiment</span>
<span>independent variables</span>
</h2>
<div class="experiment-card">
<div class="card-heading">
<div class="card-name">
<span class="card-swatch"></span>
Reference
</div>
<span id="refRuleCode" class="card-code">B3/S23</span>
</div>
<div class="field">
<label for="refRule">Rule</label>
<select id="refRule">
<option value="life">Conway's Life</option>
<option value="highlife">HighLife</option>
<option value="seeds">Seeds</option>
<option value="maze">Maze</option>
</select>
</div>
<div class="field">
<label for="refPattern">Seed</label>
<select id="refPattern">
<option value="empty">Empty field</option>
<option value="single">Single cell</option>
<option value="blinker">Blinker</option>
<option value="glider">Glider</option>
<option value="blocks">Twin blocks</option>
<option value="noise">Sparse noise</option>
</select>
</div>
</div>
<div class="experiment-card variant">
<div class="card-heading">
<div class="card-name">
<span class="card-swatch"></span>
Variant
</div>
<span id="varRuleCode" class="card-code">B36/S23</span>
</div>
<div class="field">
<label for="varRule">Rule</label>
<select id="varRule">
<option value="life">Conway's Life</option>
<option value="highlife" selected>HighLife</option>
<option value="seeds">Seeds</option>
<option value="maze">Maze</option>
</select>
</div>
<div class="field">
<label for="varPattern">Seed</label>
<select id="varPattern">
<option value="empty">Empty field</option>
<option value="single">Single cell</option>
<option value="blinker">Blinker</option>
<option value="glider" selected>Glider</option>
<option value="blocks">Twin blocks</option>
<option value="noise">Sparse noise</option>
</select>
</div>
</div>
<div class="isolation" aria-label="Isolate one experimental variable">
<button id="sameSeed" title="Copy the reference initialization into the variant">
Same seed<br>→ vary rule
</button>
<button id="sameRule" title="Copy the reference rule into the variant">
Same rule<br>→ vary seed
</button>
</div>
<label class="compare-switch">
<span class="compare-switch-text">
<strong>Split comparison</strong>
<small>Render reference and variant together</small>
</span>
<span class="switch">
<input id="compareToggle" type="checkbox" checked>
<span class="switch-track"></span>
</span>
</label>
</section>
<section class="section">
<h2 class="section-title">
<span>Simulation</span>
<span id="compareStatus">A + B synchronized</span>
</h2>
<div class="transport">
<button id="stepButton" class="primary-button">Step</button>
<button id="playButton" class="primary-button">Play</button>
<button id="resetButton" class="secondary-button">Reset</button>
</div>
<div class="speed-row">
<label for="speed">Speed</label>
<input id="speed" type="range" min="160" max="1000" step="20" value="420">
<span id="speedValue" class="speed-value">420 ms</span>
</div>
</section>
<section class="section">
<h2 class="section-title">
<span>Variant rule</span>
<span>local neighborhood</span>
</h2>
<div class="rule-card">
<h3 id="ruleName">HighLife</h3>
<span id="ruleCodeLarge" class="rule-code-large">B36/S23</span>
<p id="ruleDescription" class="rule-description"></p>
</div>
<p class="control-note">
Changing a rule or seed restarts that simulation at generation zero.
The reference and variant always advance in lockstep.
</p>
</section>
<footer class="credits">
Fixed finite boundary · synchronous updates · deterministic initialization<br>
Three.js core · low-poly hex cells · custom camera controls
</footer>
</aside>
<main class="stage" id="stage">
<div id="viewport"></div>
<div class="stage-header">
<div class="stage-kicker">Live experiment / 32 × 32</div>
<h2 class="stage-title">Neighborhood field</h2>
<div class="stage-chips">
<span id="topRule" class="chip">HIGHLIFE · B36/S23</span>
<span id="topPattern" class="chip">GLIDER</span>
</div>
</div>
<div id="refLabel" class="pane-label">A · Reference</div>
<div id="varLabel" class="pane-label variant">B · Variant</div>
<div class="camera-hint">
Drag to orbit · wheel to zoom<br>
Double-click to reset camera
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-cell"></span>
dormant
</div>
<div class="legend-item">
<span class="legend-cell live"></span>
alive
</div>
<div class="legend-item">height = cell age</div>
</div>
<div class="hud" aria-live="polite">
<div class="hud-item">
<span>GENERATION</span>
<span id="generationHud">000</span>
</div>
<div class="hud-item">
<span>POPULATION</span>
<span id="populationHud">000</span>
</div>
<div class="hud-item">
<span>BIRTHS / DEATHS</span>
<span id="deltaHud">+0 / −0</span>
</div>
</div>
<div id="toast" class="toast"></div>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const GRID_SIZE = 32;
const CELL_SPACING = 0.92;
const CELL_COUNT = GRID_SIZE * GRID_SIZE;
const rules = {
life: {
name: "Conway's Life",
code: "B3/S23",
birth: [3],
survive: [2, 3],
color: "#61e6c4",
description:
"A live cell survives with two or three neighbors. A dead cell is born with exactly three."
},
highlife: {
name: "HighLife",
code: "B36/S23",
birth: [3, 6],
survive: [2, 3],
color: "#ff9a72",
description:
"Like Conway's Life, but a dead cell with exactly six neighbors is also born."
},
seeds: {
name: "Seeds",
code: "B2/S",
birth: [2],
survive: [],
color: "#c7f45a",
description:
"No live cell survives. Every empty cell with exactly two live neighbors becomes alive."
},
maze: {
name: "Maze",
code: "B3/S12345",
birth: [3],
survive: [1, 2, 3, 4, 5],
color: "#a78bfa",
description:
"Live cells survive with one through five neighbors, while exactly three neighbors create a new cell."
}
};
const patterns = {
empty: { name: "Empty field" },
single: { name: "Single cell" },
blinker: { name: "Blinker" },
glider: { name: "Glider" },
blocks: { name: "Twin blocks" },
noise: { name: "Sparse noise" }
};
const stage = document.getElementById("stage");
const viewport = document.getElementById("viewport");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x07111f, 1);
viewport.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-25, 25, 25, -25, 0.1, 180);
const cameraState = {
yaw: -0.68,
pitch: 0.77,
zoom: 1.04
};
const cellGeometry = new THREE.CylinderGeometry(
0.40,
0.34,
0.50,
6,
1,
false
);
cellGeometry.rotateY(Math.PI / 6);
const cellMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.72,
metalness: 0.08,
flatShading: true
});
const dummy = new THREE.Object3D();
function cellPosition(index) {
const x = index % GRID_SIZE;
const z = Math.floor(index / GRID_SIZE);
const offset = (GRID_SIZE - 1) * 0.5;
return {
x: (x - offset) * CELL_SPACING,
z: (z - offset) * CELL_SPACING
};
}
function seededRandom(seed) {
let value = seed >>> 0;
return function () {
value += 0x6d2b79f5;
let t = value;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function buildPattern(patternId) {
const cells = new Uint8Array(CELL_COUNT);
const center = Math.floor(GRID_SIZE / 2);
const set = (x, z) => {
if (x >= 0 && x < GRID_SIZE && z >= 0 && z < GRID_SIZE) {
cells[z * GRID_SIZE + x] = 1;
}
};
switch (patternId) {
case "single":
set(center, center);
break;
case "blinker":
set(center - 1, center);
set(center, center);
set(center + 1, center);
break;
case "glider":
set(center + 1, center - 1);
set(center + 2, center);
set(center, center + 1);
set(center + 1, center + 1);
set(center + 2, center + 1);
break;
case "blocks":
for (let z = 0; z < 2; z++) {
for (let x = 0; x < 2; x++) {
set(center - 5 + x, center - 5 + z);
set(center + 3 + x, center + 3 + z);
}
}
break;
case "noise": {
const random = seededRandom(20240517);
for (let z = 0; z < GRID_SIZE; z++) {
for (let x = 0; x < GRID_SIZE; x++) {
const dx = x - center;
const dz = z - center;
const inside = dx * dx + dz * dz < 13 * 13;
if (inside && random() < 0.29) {
set(x, z);
}
}
}
break;
}
case "empty":
default:
break;
}
return cells;
}
function createAutomatonScene() {
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 55, 110);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.MeshStandardMaterial({
color: 0x081624,
roughness: 0.98,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.045;
floor.receiveShadow = true;
scene.add(floor);
const ring = new THREE.Mesh(
new THREE.RingGeometry(18.2, 18.28, 64),
new THREE.MeshBasicMaterial({
color: 0x244358,
transparent: true,
opacity: 0.45,
side: THREE.DoubleSide
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.y = -0.025;
scene.add(ring);
const mesh = new THREE.InstancedMesh(
cellGeometry,
cellMaterial,
CELL_COUNT
);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.frustumCulled = false;
scene.add(mesh);
const half = (GRID_SIZE * CELL_SPACING) * 0.5 + 0.28;
const borderPoints = [
new THREE.Vector3(-half, -0.015, -half),
new THREE.Vector3(half, -0.015, -half),
new THREE.Vector3(half, -0.015, half),
new THREE.Vector3(-half, -0.015, half)
];
const border = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(borderPoints),
new THREE.LineBasicMaterial({
color: 0x37627a,
transparent: true,
opacity: 0.62
})
);
scene.add(border);
const hemisphere = new THREE.HemisphereLight(0xbceeff, 0x17233c, 1.75);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(12, 24, 9);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -22;
key.shadow.camera.right = 22;
key.shadow.camera.top = 22;
key.shadow.camera.bottom = -22;
key.shadow.camera.near = 1;
key.shadow.camera.far = 65;
key.shadow.bias = -0.0004;
scene.add(key);
const cyanLight = new THREE.PointLight(0x4dddc8, 30, 55, 2);
cyanLight.position.set(-13, 8, -9);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xff8f6b, 22, 50, 2);
warmLight.position.set(12, 7, 10);
scene.add(warmLight);
const random = seededRandom(714203);
const dustPositions = [];
for (let i = 0; i < 130; i++) {
dustPositions.push(
(random() - 0.5) * 65,
3 + random() * 25,
(random() - 0.5) * 65
);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x6996ad,
size: 0.055,
transparent: true,
opacity: 0.35,
sizeAttenuation: true
})
);
scene.add(dust);
return {
scene,
mesh,
heights: new Float32Array(CELL_COUNT),
targets: new Float32Array(CELL_COUNT)
};
}
function createState(name, ruleId, patternId) {
const visual = createAutomatonScene();
return {
name,
rule: ruleId,
pattern: patternId,
cells: new Uint8Array(CELL_COUNT),
ages: new Uint16Array(CELL_COUNT),
generation: 0,
stats: { born: 0, died: 0 },
visual
};
}
const reference = createState("reference", "life", "glider");
const variant = createState("variant", "highlife", "glider");
function updateVisualTargets(state) {
const aliveColor = new THREE.Color(rules[state.rule].color);
const white = new THREE.Color(0xffffff);
const darkA = new THREE.Color(0x102235);
const darkB = new THREE.Color(0x142b3e);
for (let index = 0; index < CELL_COUNT; index++) {
const alive = state.cells[index] === 1;
const age = state.ages[index];
const x = index % GRID_SIZE;
const z = Math.floor(index / GRID_SIZE);
state.visual.targets[index] = alive
? 1 + Math.min(age, 8) * 0.075
: 0.12;
if (alive) {
const color = aliveColor.clone();
color.lerp(white, Math.min(age * 0.075, 0.52));
state.visual.mesh.setColorAt(index, color);
} else {
state.visual.mesh.setColorAt(
index,
(x + z) % 2 === 0 ? darkA : darkB
);
}
}
if (state.visual.mesh.instanceColor) {
state.visual.mesh.instanceColor.needsUpdate = true;
}
}
function writeCellMatrices(state, snap = false) {
for (let index = 0; index < CELL_COUNT; index++) {
if (snap) {
state.visual.heights[index] = state.visual.targets[index];
}
const position = cellPosition(index);
const height = state.visual.heights[index];
dummy.position.set(position.x, 0.25 * height, position.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, height, 1);
dummy.updateMatrix();
state.visual.mesh.setMatrixAt(index, dummy.matrix);
}
state.visual.mesh.instanceMatrix.needsUpdate = true;
}
function resetState(state) {
state.cells = buildPattern(state.pattern);
state.ages = new Uint16Array(CELL_COUNT);
state.generation = 0;
state.stats = { born: 0, died: 0 };
updateVisualTargets(state);
writeCellMatrices(state, true);
}
function neighborCount(cells, x, z) {
let count = 0;
for (let dz = -1; dz <= 1; dz++) {
for (let dx = -1; dx <= 1; dx++) {
if (dx === 0 && dz === 0) continue;
const nx = x + dx;
const nz = z + dz;
if (
nx >= 0 &&
nx < GRID_SIZE &&
nz >= 0 &&
nz < GRID_SIZE &&
cells[nz * GRID_SIZE + nx]
) {
count++;
}
}
}
return count;
}
function advanceState(state) {
const rule = rules[state.rule];
const next = new Uint8Array(CELL_COUNT);
const nextAges = new Uint16Array(CELL_COUNT);
let born = 0;
let died = 0;
for (let z = 0; z < GRID_SIZE; z++) {
for (let x = 0; x < GRID_SIZE; x++) {
const index = z * GRID_SIZE + x;
const alive = state.cells[index] === 1;
const neighbors = neighborCount(state.cells, x, z);
const survives = rule.survive.includes(neighbors);
const isBorn = rule.birth.includes(neighbors);
const nextAlive = alive ? survives : isBorn;
if (nextAlive) {
next[index] = 1;
nextAges[index] = alive ? state.ages[index] + 1 : 0;
if (!alive) born++;
} else if (alive) {
died++;
}
}
}
state.cells = next;
state.ages = nextAges;
state.generation++;
state.stats = { born, died };
updateVisualTargets(state);
}
function population(state) {
let count = 0;
for (let i = 0; i < state.cells.length; i++) {
count += state.cells[i];
}
return count;
}
const elements = {
refRule: document.getElementById("refRule"),
refPattern: document.getElementById("refPattern"),
varRule: document.getElementById("varRule"),
varPattern: document.getElementById("varPattern"),
sameSeed: document.getElementById("sameSeed"),
sameRule: document.getElementById("sameRule"),
compareToggle: document.getElementById("compareToggle"),
compareStatus: document.getElementById("compareStatus"),
stepButton: document.getElementById("stepButton"),
playButton: document.getElementById("playButton"),
resetButton: document.getElementById("resetButton"),
speed: document.getElementById("speed"),
speedValue: document.getElementById("speedValue"),
statusDot: document.getElementById("statusDot"),
statusText: document.getElementById("statusText"),
refRuleCode: document.getElementById("refRuleCode"),
varRuleCode: document.getElementById("varRuleCode"),
ruleName: document.getElementById("ruleName"),
ruleCodeLarge: document.getElementById("ruleCodeLarge"),
ruleDescription: document.getElementById("ruleDescription"),
topRule: document.getElementById("topRule"),
topPattern: document.getElementById("topPattern"),
generationHud: document.getElementById("generationHud"),
populationHud: document.getElementById("populationHud"),
deltaHud: document.getElementById("deltaHud"),
refLabel: document.getElementById("refLabel"),
varLabel: document.getElementById("varLabel"),
toast: document.getElementById("toast")
};
function pad(value, length = 3) {
return String(value).padStart(length, "0");
}
function updateUI() {
const rule = rules[variant.rule];
elements.refRuleCode.textContent = rules[reference.rule].code;
elements.varRuleCode.textContent = rule.code;
elements.ruleName.textContent = rule.name;
elements.ruleCodeLarge.textContent = rule.code;
elements.ruleDescription.textContent = rule.description;
elements.topRule.textContent =
`${rule.name.toUpperCase()} · ${rule.code}`;
elements.topPattern.textContent =
patterns[variant.pattern].name.toUpperCase();
elements.generationHud.textContent = pad(variant.generation);
elements.populationHud.textContent = pad(population(variant));
elements.deltaHud.textContent =
`+${variant.stats.born} / −${variant.stats.died}`;
elements.compareStatus.textContent = elements.compareToggle.checked
? "A + B synchronized"
: "Variant only";
elements.refLabel.hidden = !elements.compareToggle.checked;
elements.varLabel.hidden = !elements.compareToggle.checked;
}
let toastTimer = 0;
function showToast(message) {
elements.toast.textContent = message;
elements.toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
elements.toast.classList.remove("visible");
}, 2200);
}
elements.refRule.addEventListener("change", () => {
reference.rule = elements.refRule.value;
resetState(reference);
updateUI();
});
elements.refPattern.addEventListener("change", () => {
reference.pattern = elements.refPattern.value;
resetState(reference);
updateUI();
});
elements.varRule.addEventListener("change", () => {
variant.rule = elements.varRule.value;
resetState(variant);
updateUI();
});
elements.varPattern.addEventListener("change", () => {
variant.pattern = elements.varPattern.value;
resetState(variant);
updateUI();
});
elements.sameSeed.addEventListener("click", () => {
variant.pattern = reference.pattern;
elements.varPattern.value = variant.pattern;
resetState(variant);
updateUI();
showToast("Initialization matched. The rule is now the only changed variable.");
});
elements.sameRule.addEventListener("click", () => {
variant.rule = reference.rule;
elements.varRule.value = variant.rule;
resetState(variant);
updateUI();
showToast("Rule matched. The initialization is now the only changed variable.");
});
elements.compareToggle.addEventListener("change", () => {
updateUI();
updateCompareLabelPositions();
});
function advancePair() {
advanceState(reference);
advanceState(variant);
updateUI();
}
elements.stepButton.addEventListener("click", advancePair);
let playbackTimer = null;
function stopPlayback() {
if (playbackTimer !== null) {
clearInterval(playbackTimer);
playbackTimer = null;
}
elements.playButton.textContent = "Play";
elements.playButton.classList.remove("running");
elements.statusDot.classList.remove("running");
elements.statusText.textContent = "Simulation paused";
}
function startPlayback() {
stopPlayback();
playbackTimer = setInterval(
advancePair,
Number(elements.speed.value)
);
elements.playButton.textContent = "Pause";
elements.playButton.classList.add("running");
elements.statusDot.classList.add("running");
elements.statusText.textContent = "Simulation running";
}
elements.playButton.addEventListener("click", () => {
if (playbackTimer === null) {
startPlayback();
} else {
stopPlayback();
}
});
elements.resetButton.addEventListener("click", () => {
stopPlayback();
resetState(reference);
resetState(variant);
updateUI();
showToast("Both simulations reset to generation zero.");
});
elements.speed.addEventListener("input", () => {
elements.speedValue.textContent = `${elements.speed.value} ms`;
if (playbackTimer !== null) {
startPlayback();
}
});
function updateCamera() {
const distance = 46;
const cosPitch = Math.cos(cameraState.pitch);
camera.position.set(
Math.sin(cameraState.yaw) * cosPitch * distance,
Math.sin(cameraState.pitch) * distance,
Math.cos(cameraState.yaw) * cosPitch * distance
);
camera.lookAt(0, 0, 0);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function setCameraProjection(width, height) {
const aspect = width / Math.max(height, 1);
const span = Math.max(42, 42 / aspect);
camera.left = (-span * aspect) / 2;
camera.right = (span * aspect) / 2;
camera.top = span / 2;
camera.bottom = -span / 2;
camera.updateProjectionMatrix();
}
function resetCamera() {
cameraState.yaw = -0.68;
cameraState.pitch = 0.77;
cameraState.zoom = 1.04;
updateCamera();
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.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 = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.22,
1.35
);
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();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.85
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
function updateCompareLabelPositions() {
const vertical = viewport.clientWidth < 680;
if (!elements.compareToggle.checked) {
elements.refLabel.style.left = "";
elements.refLabel.style.top = "";
elements.varLabel.style.left = "";
elements.varLabel.style.top = "";
return;
}
if (vertical) {
elements.refLabel.style.left = "18px";
elements.refLabel.style.top = "108px";
elements.varLabel.style.left = "18px";
elements.varLabel.style.top = "calc(50% + 18px)";
} else {
elements.refLabel.style.left = "25px";
elements.refLabel.style.top = "82px";
elements.varLabel.style.left = "calc(50% + 25px)";
elements.varLabel.style.top = "82px";
}
}
function render() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
if (!width || !height) return;
renderer.setSize(width, height, false);
const comparing = elements.compareToggle.checked;
const vertical = width < 680;
renderer.setScissorTest(true);
if (comparing && vertical) {
const halfHeight = Math.floor(height / 2);
setCameraProjection(width, halfHeight);
renderer.setViewport(0, halfHeight, width, height - halfHeight);
renderer.setScissor(0, halfHeight, width, height - halfHeight);
renderer.render(reference.visual.scene, camera);
renderer.setViewport(0, 0, width, halfHeight);
renderer.setScissor(0, 0, width, halfHeight);
renderer.render(variant.visual.scene, camera);
} else if (comparing) {
const halfWidth = Math.floor(width / 2);
setCameraProjection(halfWidth, height);
renderer.setViewport(0, 0, halfWidth, height);
renderer.setScissor(0, 0, halfWidth, height);
renderer.render(reference.visual.scene, camera);
renderer.setViewport(halfWidth, 0, width - halfWidth, height);
renderer.setScissor(halfWidth, 0, width - halfWidth, height);
renderer.render(variant.visual.scene, camera);
} else {
setCameraProjection(width, height);
renderer.setViewport(0, 0, width, height);
renderer.setScissor(0, 0, width, height);
renderer.render(variant.visual.scene, camera);
}
renderer.setScissorTest(false);
}
const resizeObserver = new ResizeObserver(() => {
updateCompareLabelPositions();
render();
});
resizeObserver.observe(viewport);
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
const smoothing = 1 - Math.exp(-delta * 11);
for (const state of [reference, variant]) {
for (let index = 0; index < CELL_COUNT; index++) {
const current = state.visual.heights[index];
const target = state.visual.targets[index];
state.visual.heights[index] =
current + (target - current) * smoothing;
}
writeCellMatrices(state);
}
render();
requestAnimationFrame(animate);
}
resetState(reference);
resetState(variant);
updateCamera();
updateUI();
updateCompareLabelPositions();
requestAnimationFrame(animate);
</script>
</body>
</html>
```