Prompt specification
Build or customize the Three.js 3D scene "Synchronous and sequential update comparison" 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: Synchronous and sequential update comparison
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/synchronous-and-sequential-update-comparison-x1858
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing cellular automata with local update rules. Apply the same local rule to identical grids using two update schedules; show differing outcomes and explain why the update convention is part of the model. Stylized solid forms.
Working Three.js Starter (synchronous-and-sequential-update-comparison-x1858):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Same Rule. Different Clock.</title>
<style>
:root {
color-scheme: dark;
--bg: #080d18;
--panel: #101827;
--panel-2: #151f31;
--line: rgba(255, 255, 255, 0.11);
--text: #f2f5fb;
--muted: #9ba8bc;
--accent: #ff6b57;
--accent-2: #ffd166;
--mint: #8ef0d1;
}
* {
box-sizing: border-box;
}
html,
body {
min-height: 100%;
margin: 0;
}
body {
background:
radial-gradient(circle at 12% 0%, rgba(67, 103, 255, 0.13), transparent 32rem),
radial-gradient(circle at 92% 18%, rgba(255, 107, 87, 0.09), transparent 28rem),
var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
-webkit-font-smoothing: antialiased;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
min-height: 100vh;
}
.masthead {
width: min(1480px, calc(100% - 40px));
margin: 0 auto;
padding: 46px 0 30px;
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(340px, 470px);
gap: 42px;
align-items: end;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 15px;
color: var(--mint);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 2px;
content: "";
background: var(--mint);
}
h1 {
max-width: 760px;
margin: 0;
font-size: clamp(3rem, 6vw, 6.4rem);
font-weight: 760;
letter-spacing: -0.065em;
line-height: 0.91;
}
h1 span {
color: var(--accent);
}
.intro {
max-width: 710px;
margin: 22px 0 0;
color: var(--muted);
font-size: clamp(1rem, 1.5vw, 1.2rem);
line-height: 1.55;
}
.model-card {
padding: 22px;
border: 1px solid var(--line);
border-radius: 20px;
background: linear-gradient(145deg, rgba(255, 255, 255, 0.055), rgba(255, 255, 255, 0.018));
box-shadow: 0 22px 80px rgba(0, 0, 0, 0.22);
}
.model-label {
margin-bottom: 12px;
color: var(--muted);
font-size: 0.7rem;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.formula {
margin-bottom: 15px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.91rem;
line-height: 1.65;
}
.formula strong {
color: var(--accent-2);
font-weight: 700;
}
.model-meta {
color: var(--muted);
font-size: 0.78rem;
line-height: 1.5;
}
.stage {
position: relative;
width: min(1480px, calc(100% - 40px));
height: clamp(610px, 68vh, 790px);
margin: 0 auto;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 24px;
background: #0b1020;
box-shadow: 0 40px 100px rgba(0, 0, 0, 0.34);
}
.views {
position: absolute;
inset: 0;
display: grid;
grid-template-columns: 1fr 1fr;
}
.view {
position: relative;
min-width: 0;
overflow: hidden;
background: #0b1020;
}
.view:first-child {
border-right: 1px solid var(--line);
}
.view::after {
position: absolute;
z-index: 1;
inset: 0;
pointer-events: none;
content: "";
background:
linear-gradient(to bottom, rgba(7, 12, 23, 0.38), transparent 27%),
linear-gradient(to top, rgba(7, 12, 23, 0.48), transparent 29%);
}
.scene-canvas {
position: absolute;
z-index: 0;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
.scene-canvas:active {
cursor: grabbing;
}
.view-ui {
position: absolute;
z-index: 2;
inset: 0;
pointer-events: none;
}
.view-head {
position: absolute;
top: 25px;
left: 27px;
right: 27px;
}
.view-kicker {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--mint);
font-size: 0.67rem;
font-weight: 850;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.view-kicker::before {
width: 7px;
height: 7px;
border-radius: 50%;
content: "";
background: currentColor;
box-shadow: 0 0 16px currentColor;
}
.view:nth-child(2) .view-kicker {
color: var(--accent-2);
}
.view h2 {
margin: 0;
font-size: clamp(1.45rem, 2.5vw, 2.4rem);
font-weight: 700;
letter-spacing: -0.035em;
}
.view-subtitle {
margin-top: 5px;
color: var(--muted);
font-size: 0.84rem;
}
.stats {
position: absolute;
right: 27px;
bottom: 72px;
display: flex;
gap: 8px;
}
.stat {
min-width: 78px;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(10, 16, 29, 0.78);
backdrop-filter: blur(10px);
}
.stat span {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 0.61rem;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.stat b {
font-size: 1.12rem;
font-variant-numeric: tabular-nums;
}
.view-foot {
position: absolute;
right: 27px;
bottom: 24px;
left: 27px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 20px;
color: var(--muted);
font-size: 0.77rem;
line-height: 1.45;
}
.view-foot strong {
color: var(--text);
font-weight: 650;
}
.divergence-pill {
position: absolute;
z-index: 5;
bottom: 19px;
left: 50%;
display: flex;
align-items: center;
gap: 8px;
padding: 9px 13px;
transform: translateX(-50%);
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 13, 24, 0.9);
color: var(--muted);
font-size: 0.7rem;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
pointer-events: none;
backdrop-filter: blur(12px);
}
.divergence-pill .dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #657086;
}
.divergence-pill.active {
color: var(--accent-2);
border-color: rgba(255, 209, 102, 0.32);
}
.divergence-pill.active .dot {
background: var(--accent-2);
box-shadow: 0 0 14px rgba(255, 209, 102, 0.75);
}
.orbit-hint {
position: absolute;
z-index: 5;
top: 26px;
left: 50%;
transform: translateX(-50%);
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 13, 24, 0.72);
color: var(--muted);
font-size: 0.65rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
backdrop-filter: blur(10px);
}
.toolbar {
width: min(1480px, calc(100% - 40px));
margin: 18px auto 0;
padding: 13px;
display: flex;
align-items: center;
gap: 10px;
border: 1px solid var(--line);
border-radius: 17px;
background: rgba(16, 24, 39, 0.76);
backdrop-filter: blur(16px);
}
.toolbar button {
min-height: 42px;
padding: 0 16px;
border: 1px solid var(--line);
border-radius: 11px;
background: var(--panel-2);
cursor: pointer;
font-size: 0.84rem;
font-weight: 750;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
.toolbar button:hover {
border-color: rgba(255, 255, 255, 0.28);
background: #1c2940;
transform: translateY(-1px);
}
.toolbar button:focus-visible,
input:focus-visible {
outline: 2px solid var(--mint);
outline-offset: 3px;
}
.toolbar button.primary {
min-width: 108px;
border-color: transparent;
background: var(--text);
color: #0c1321;
}
.toolbar button.primary:hover {
background: #fff;
}
.speed-control {
display: flex;
align-items: center;
gap: 10px;
margin-left: 5px;
padding: 0 11px;
color: var(--muted);
font-size: 0.76rem;
}
.speed-control input {
width: 110px;
accent-color: var(--accent);
}
.speed-control output {
width: 38px;
color: var(--text);
font-variant-numeric: tabular-nums;
}
.mode-status {
margin-left: auto;
padding-right: 8px;
color: var(--muted);
font-size: 0.75rem;
}
.explanation {
width: min(1480px, calc(100% - 40px));
margin: 42px auto 58px;
}
.explanation > h2 {
margin: 0 0 20px;
font-size: clamp(1.8rem, 3vw, 3rem);
letter-spacing: -0.045em;
}
.explain-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
}
.explain-card {
min-height: 205px;
padding: 23px;
border: 1px solid var(--line);
border-radius: 17px;
background: linear-gradient(145deg, rgba(255, 255, 255, 0.045), rgba(255, 255, 255, 0.015));
}
.card-number {
display: block;
margin-bottom: 26px;
color: var(--accent);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.75rem;
}
.explain-card h3 {
margin: 0 0 10px;
font-size: 1.08rem;
}
.explain-card p {
margin: 0;
color: var(--muted);
font-size: 0.87rem;
line-height: 1.65;
}
.takeaway {
margin-top: 12px;
padding: 25px 27px;
display: grid;
grid-template-columns: 180px minmax(0, 1fr);
gap: 28px;
align-items: start;
border: 1px solid rgba(142, 240, 209, 0.22);
border-radius: 17px;
background: rgba(142, 240, 209, 0.055);
}
.takeaway strong {
color: var(--mint);
font-size: 0.74rem;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.takeaway p {
margin: 0;
color: #c7d1df;
font-size: 0.95rem;
line-height: 1.72;
}
.takeaway code {
color: var(--accent-2);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.page-note {
margin-top: 22px;
color: #748196;
font-size: 0.74rem;
text-align: center;
}
@media (max-width: 880px) {
.masthead {
grid-template-columns: 1fr;
gap: 25px;
padding-top: 32px;
}
.model-card {
max-width: none;
}
.stage {
height: 900px;
}
.views {
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr;
}
.view:first-child {
border-right: 0;
border-bottom: 1px solid var(--line);
}
.orbit-hint {
top: auto;
bottom: 7px;
}
.divergence-pill {
bottom: 8px;
}
.toolbar {
flex-wrap: wrap;
}
.mode-status {
width: 100%;
margin: 5px 4px 0;
}
.explain-grid {
grid-template-columns: 1fr;
}
.explain-card {
min-height: auto;
}
.takeaway {
grid-template-columns: 1fr;
gap: 10px;
}
}
@media (max-width: 560px) {
.masthead,
.stage,
.toolbar,
.explanation {
width: min(100% - 20px, 1480px);
}
h1 {
font-size: clamp(3.2rem, 17vw, 5rem);
}
.stage {
height: 820px;
border-radius: 18px;
}
.view-head {
top: 18px;
left: 18px;
right: 18px;
}
.stats {
right: 18px;
bottom: 70px;
}
.view-foot {
right: 18px;
bottom: 22px;
left: 18px;
}
.view-foot > span {
display: none;
}
.toolbar button {
flex: 1 1 auto;
padding: 0 11px;
}
.speed-control {
width: 100%;
margin: 3px 0;
}
.speed-control input {
flex: 1;
}
.takeaway {
padding: 21px;
}
}
@media (prefers-reduced-motion: reduce) {
* {
scroll-behavior: auto !important;
transition-duration: 0.001ms !important;
}
}
</style>
</head>
<body>
<div class="app">
<header class="masthead">
<div>
<div class="eyebrow">Cellular automata / update conventions</div>
<h1>Same rule.<br><span>Different clock.</span></h1>
<p class="intro">
Two grids begin with the same three-cell row and apply the same
Conway rule. The left grid computes a frozen generation; the right
grid writes cells immediately in row-major order.
</p>
</div>
<aside class="model-card">
<div class="model-label">Shared local rule · Conway B3/S23</div>
<div class="formula">
<strong>R(s,n)</strong> = 1 when<br>
s = 1 and n ∈ {2,3},<br>
or s = 0 and n = 3.
</div>
<div class="model-meta">
Eight-cell Moore neighborhood. Cells outside the finite grid are dead.
The only difference between the models is the update schedule.
</div>
</aside>
</header>
<main
class="stage"
aria-label="Two three-dimensional cellular automata grids using different update schedules"
>
<div class="views">
<section class="view" id="syncView">
<div class="view-ui">
<div class="view-head">
<div class="view-kicker">01 / Synchronous</div>
<h2>Snapshot generation</h2>
<div class="view-subtitle">Every cell reads generation t.</div>
</div>
<div class="stats">
<div class="stat">
<span>Live</span>
<b id="syncCount">3</b>
</div>
<div class="stat">
<span>Gen</span>
<b id="syncRound">0</b>
</div>
</div>
<div class="view-foot">
<div>
<strong>Frozen old state → compute all new cells → commit</strong><br>
The horizontal seed becomes a persistent blinking line.
</div>
<span>No write can affect another cell in the same generation.</span>
</div>
</div>
</section>
<section class="view" id="asyncView">
<div class="view-ui">
<div class="view-head">
<div class="view-kicker">02 / Asynchronous</div>
<h2>Ordered in-place sweep</h2>
<div class="view-subtitle">Later cells see earlier writes.</div>
</div>
<div class="stats">
<div class="stat">
<span>Live</span>
<b id="asyncCount">3</b>
</div>
<div class="stat">
<span>Sweep</span>
<b id="asyncRound">0</b>
</div>
</div>
<div class="view-foot">
<div>
<strong>Top-to-bottom, left-to-right → write immediately</strong><br>
The yellow bracket marks the current cell in the sequential sweep.
</div>
<span>Amber tiles mark cells whose value has just changed.</span>
</div>
</div>
</section>
</div>
<div class="divergence-pill" id="divergencePill">
<span class="dot"></span>
<b id="diffCount">0</b>
divergent cells
</div>
<div class="orbit-hint">Drag to orbit · scroll to zoom</div>
</main>
<div class="toolbar" aria-label="Simulation controls">
<button class="primary" id="play" type="button">
<span id="playLabel">Pause</span>
</button>
<button id="step" type="button">Step</button>
<button id="reset" type="button">Reset</button>
<button id="viewReset" type="button">Reset view</button>
<label class="speed-control" for="speed">
Speed
<input id="speed" type="range" min="0.5" max="2" step="0.25" value="1">
<output id="speedValue" for="speed">1.0×</output>
</label>
<div class="mode-status" id="modeStatus" aria-live="polite">
Comparing complete rounds
</div>
</div>
<section class="explanation">
<h2>Why the clock is part of the rule</h2>
<div class="explain-grid">
<article class="explain-card">
<span class="card-number">01 / LOCAL FUNCTION</span>
<h3>Same transition function</h3>
<p>
Both models use B3/S23. An occupied cell survives with two or three
occupied neighbors; an empty cell becomes occupied with exactly
three. The rule itself does not change.
</p>
</article>
<article class="explain-card">
<span class="card-number">02 / SYNCHRONOUS CLOCK</span>
<h3>All neighborhoods come from t</h3>
<p>
The synchronous model freezes generation t, evaluates every cell
against that snapshot, then commits the new generation. Its three-cell
seed becomes a blinker that repeatedly changes orientation.
</p>
</article>
<article class="explain-card">
<span class="card-number">03 / SEQUENTIAL CLOCK</span>
<h3>Neighborhoods mix t and t+1</h3>
<p>
The asynchronous model evaluates cells from top to bottom and left to
right, writing each result immediately. The row above the seed is
evaluated first and is born before the seed row is rewritten, changing
the later cells’ neighborhoods.
</p>
</article>
</div>
<div class="takeaway">
<strong>The model conclusion</strong>
<p>
A cellular automaton is not only a local function such as
<code>R(cell, neighborhood)</code>; it also specifies
<strong>which state each neighborhood observation belongs to</strong>.
In sequential updating, some neighbors may already be from
<code>t+1</code> while others are still from <code>t</code>.
Therefore “synchronous” and “ordered asynchronous” are different
update conventions and, consequently, different models—not merely
different ways of drawing the same result.
</p>
</div>
<p class="page-note">
Built with the Three.js core ES module. All camera controls are implemented locally.
</p>
</section>
</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_N = 17;
const CELL_SIZE = 0.54;
const GRID_SIZE = GRID_N * CELL_SIZE;
const CELL_COUNT = GRID_N * GRID_N;
const CENTER = (GRID_N - 1) / 2;
const BASE_HEIGHT = 0.14;
const LIVE_HEIGHT = 1.16;
const SWEEP_DURATION = 900;
const COLORS = {
background: 0x0b1020,
floor: 0x0c1322,
platform: 0x151f31,
dead: 0x202c43,
alive: 0xff6b57,
changed: 0xffd166
};
const $ = (id) => document.getElementById(id);
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let running = !reducedMotion;
let speed = 1;
let syncRound = 0;
let asyncSweep = 0;
let asyncAnimation = null;
let initialState = createSeed();
let syncState = initialState.slice();
let asyncState = initialState.slice();
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 100);
const orbit = {
theta: -0.18,
phi: 0.98,
zoom: 1
};
const cameraTarget = new THREE.Vector3(0, 0.35, 0);
function createSeed() {
const state = new Uint8Array(CELL_COUNT);
const row = Math.floor(GRID_N / 2);
const col = Math.floor(GRID_N / 2);
state[row * GRID_N + col - 1] = 1;
state[row * GRID_N + col] = 1;
state[row * GRID_N + col + 1] = 1;
return state;
}
function rule(cell, neighbors) {
if (cell) {
return neighbors === 2 || neighbors === 3 ? 1 : 0;
}
return neighbors === 3 ? 1 : 0;
}
function countNeighbors(state, row, col) {
let count = 0;
for (let dr = -1; dr <= 1; dr++) {
for (let dc = -1; dc <= 1; dc++) {
if (dr === 0 && dc === 0) continue;
const r = row + dr;
const c = col + dc;
if (
r >= 0 && r < GRID_N &&
c >= 0 && c < GRID_N
) {
count += state[r * GRID_N + c];
}
}
}
return count;
}
function stepSynchronous(state) {
const next = new Uint8Array(CELL_COUNT);
for (let row = 0; row < GRID_N; row++) {
for (let col = 0; col < GRID_N; col++) {
const index = row * GRID_N + col;
const neighbors = countNeighbors(state, row, col);
next[index] = rule(state[index], neighbors);
}
}
return next;
}
function createCursor() {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color: COLORS.changed,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false
});
const horizontalGeometry = new THREE.BoxGeometry(
CELL_SIZE * 0.94,
0.035,
0.035
);
const verticalGeometry = new THREE.BoxGeometry(
0.035,
0.035,
CELL_SIZE * 0.94
);
for (const sign of [-1, 1]) {
const horizontal = new THREE.Mesh(horizontalGeometry, material);
horizontal.position.z = sign * CELL_SIZE * 0.45;
group.add(horizontal);
const vertical = new THREE.Mesh(verticalGeometry, material);
vertical.position.x = sign * CELL_SIZE * 0.45;
group.add(vertical);
}
group.visible = false;
group.renderOrder = 10;
return group;
}
function createView(element, label) {
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
element.prepend(renderer.domElement);
renderer.domElement.className = "scene-canvas";
renderer.domElement.setAttribute("role", "img");
renderer.domElement.setAttribute("aria-label", label);
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.background);
scene.fog = new THREE.Fog(COLORS.background, 28, 52);
const hemisphere = new THREE.HemisphereLight(
0xdbeafe,
0x131827,
1.55
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1e6, 2.5);
keyLight.position.set(-7, 14, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 38;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
scene.add(keyLight.target);
const fillLight = new THREE.DirectionalLight(0x5478ff, 1.05);
fillLight.position.set(8, 7, -9);
scene.add(fillLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: COLORS.floor,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.22;
floor.receiveShadow = true;
scene.add(floor);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(GRID_SIZE + 0.58, 0.22, GRID_SIZE + 0.58),
new THREE.MeshStandardMaterial({
color: COLORS.platform,
roughness: 0.78,
metalness: 0.08
})
);
platform.position.y = -0.11;
platform.castShadow = true;
platform.receiveShadow = true;
scene.add(platform);
const baseGeometry = new THREE.BoxGeometry(
CELL_SIZE * 0.87,
BASE_HEIGHT,
CELL_SIZE * 0.87
);
const baseMaterial = new THREE.MeshStandardMaterial({
color: COLORS.dead,
roughness: 0.72,
metalness: 0.08
});
const baseMesh = new THREE.InstancedMesh(
baseGeometry,
baseMaterial,
CELL_COUNT
);
baseMesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
baseMesh.receiveShadow = true;
baseMesh.frustumCulled = false;
const dummy = new THREE.Object3D();
for (let row = 0; row < GRID_N; row++) {
for (let col = 0; col < GRID_N; col++) {
const index = row * GRID_N + col;
dummy.position.set(
(col - CENTER) * CELL_SIZE,
BASE_HEIGHT / 2,
(row - CENTER) * CELL_SIZE
);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
baseMesh.setMatrixAt(index, dummy.matrix);
}
}
baseMesh.instanceMatrix.needsUpdate = true;
scene.add(baseMesh);
const liveGeometry = new THREE.BoxGeometry(
CELL_SIZE * 0.72,
1,
CELL_SIZE * 0.72
);
const liveMaterial = new THREE.MeshStandardMaterial({
color: COLORS.alive,
roughness: 0.38,
metalness: 0.08
});
const liveMesh = new THREE.InstancedMesh(
liveGeometry,
liveMaterial,
CELL_COUNT
);
liveMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
liveMesh.castShadow = true;
liveMesh.receiveShadow = true;
liveMesh.frustumCulled = false;
scene.add(liveMesh);
const markerGeometry = new THREE.BoxGeometry(
CELL_SIZE * 0.76,
0.035,
CELL_SIZE * 0.76
);
const markerMaterial = new THREE.MeshBasicMaterial({
color: COLORS.changed,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const markerMesh = new THREE.InstancedMesh(
markerGeometry,
markerMaterial,
CELL_COUNT
);
markerMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
markerMesh.frustumCulled = false;
markerMesh.renderOrder = 4;
scene.add(markerMesh);
const cursor = createCursor();
scene.add(cursor);
const resize = () => {
const width = Math.max(1, element.clientWidth);
const height = Math.max(1, element.clientHeight);
renderer.setSize(width, height, false);
};
const observer = new ResizeObserver(resize);
observer.observe(element);
resize();
return {
element,
renderer,
scene,
baseMesh,
liveMesh,
markerMesh,
cursor,
changedTimes: new Float32Array(CELL_COUNT).fill(-1000),
resize
};
}
const syncView = createView(
$("syncView"),
"Synchronous Conway cellular automaton shown as solid raised blocks"
);
const asyncView = createView(
$("asyncView"),
"Sequential asynchronous Conway cellular automaton shown as solid raised blocks"
);
const views = [syncView, asyncView];
function updateLiveMesh(view, state) {
const dummy = new THREE.Object3D();
for (let row = 0; row < GRID_N; row++) {
for (let col = 0; col < GRID_N; col++) {
const index = row * GRID_N + col;
const alive = state[index] === 1;
dummy.position.set(
(col - CENTER) * CELL_SIZE,
alive
? BASE_HEIGHT + LIVE_HEIGHT / 2
: BASE_HEIGHT,
(row - CENTER) * CELL_SIZE
);
if (alive) {
dummy.scale.set(1, LIVE_HEIGHT, 1);
} else {
dummy.scale.set(0.0001, 0.0001, 0.0001);
}
dummy.updateMatrix();
view.liveMesh.setMatrixAt(index, dummy.matrix);
}
}
view.liveMesh.instanceMatrix.needsUpdate = true;
}
function updateMarkerMesh(view, nowSeconds) {
const dummy = new THREE.Object3D();
const lifetime = 0.9;
for (let row = 0; row < GRID_N; row++) {
for (let col = 0; col < GRID_N; col++) {
const index = row * GRID_N + col;
const age = nowSeconds - view.changedTimes[index];
const visible = age >= 0 && age < lifetime;
const scale = visible ? 1 - age / lifetime : 0.0001;
dummy.position.set(
(col - CENTER) * CELL_SIZE,
BASE_HEIGHT + 0.022,
(row - CENTER) * CELL_SIZE
);
dummy.scale.set(scale, 1, scale);
dummy.updateMatrix();
view.markerMesh.setMatrixAt(index, dummy.matrix);
}
}
view.markerMesh.instanceMatrix.needsUpdate = true;
}
function markChanged(view, index, nowSeconds) {
view.changedTimes[index] = nowSeconds;
}
function advanceSynchronous() {
const previous = syncState;
const next = stepSynchronous(previous);
const nowSeconds = performance.now() / 1000;
for (let i = 0; i < CELL_COUNT; i++) {
if (previous[i] !== next[i]) {
markChanged(syncView, i, nowSeconds);
}
}
syncState = next;
syncRound++;
}
function processAsyncCell(index) {
const row = Math.floor(index / GRID_N);
const col = index % GRID_N;
const before = asyncState[index];
const neighbors = countNeighbors(asyncState, row, col);
const after = rule(before, neighbors);
asyncState[index] = after;
if (before !== after) {
markChanged(asyncView, index, performance.now() / 1000);
}
}
function updateCursor(index) {
if (index === null || index < 0 || index >= CELL_COUNT) {
asyncView.cursor.visible = false;
return;
}
const row = Math.floor(index / GRID_N);
const col = index % GRID_N;
asyncView.cursor.visible = true;
asyncView.cursor.position.set(
(col - CENTER) * CELL_SIZE,
BASE_HEIGHT + LIVE_HEIGHT + 0.14,
(row - CENTER) * CELL_SIZE
);
}
function countLive(state) {
let count = 0;
for (let i = 0; i < state.length; i++) count += state[i];
return count;
}
function updateStats() {
const syncLive = countLive(syncState);
const asyncLive = countLive(asyncState);
let difference = 0;
for (let i = 0; i < CELL_COUNT; i++) {
if (syncState[i] !== asyncState[i]) difference++;
}
$("syncCount").textContent = syncLive;
$("asyncCount").textContent = asyncLive;
$("syncRound").textContent = syncRound;
$("asyncRound").textContent = asyncAnimation
? asyncSweep + 1
: asyncSweep;
$("diffCount").textContent = difference;
$("divergencePill").classList.toggle("active", difference > 0);
$("modeStatus").textContent = asyncAnimation
? `Asynchronous sweep ${asyncSweep + 1} in progress`
: "Comparing complete rounds";
}
function updatePlayButton() {
$("playLabel").textContent = running ? "Pause" : "Play";
$("play").setAttribute("aria-pressed", String(running));
}
function resetSimulation() {
running = false;
syncRound = 0;
asyncSweep = 0;
asyncAnimation = null;
syncState = initialState.slice();
asyncState = initialState.slice();
syncView.changedTimes.fill(-1000);
asyncView.changedTimes.fill(-1000);
updateLiveMesh(syncView, syncState);
updateLiveMesh(asyncView, asyncState);
updateCursor(null);
updateStats();
updatePlayButton();
}
function stepOnce() {
running = false;
if (asyncAnimation) {
while (asyncAnimation.index < CELL_COUNT) {
processAsyncCell(asyncAnimation.index);
asyncAnimation.index++;
}
asyncSweep++;
asyncAnimation = null;
} else {
advanceSynchronous();
asyncAnimation = {
elapsed: 0,
duration: SWEEP_DURATION / speed,
index: 0
};
while (asyncAnimation.index < CELL_COUNT) {
processAsyncCell(asyncAnimation.index);
asyncAnimation.index++;
}
asyncSweep++;
asyncAnimation = null;
}
updateCursor(null);
updateStats();
updatePlayButton();
}
$("play").addEventListener("click", () => {
running = !running;
updatePlayButton();
});
$("step").addEventListener("click", stepOnce);
$("reset").addEventListener("click", resetSimulation);
$("speed").addEventListener("input", (event) => {
speed = Number(event.target.value);
$("speedValue").textContent = `${speed.toFixed(1)}×`;
if (asyncAnimation) {
asyncAnimation.duration = SWEEP_DURATION / speed;
}
});
function resetCamera() {
orbit.theta = -0.18;
orbit.phi = 0.98;
orbit.zoom = 1;
}
$("viewReset").addEventListener("click", resetCamera);
// Camera controls are implemented directly, without Three.js add-ons.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
for (const view of views) {
const canvas = view.renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.62,
1.3
);
});
const stopDragging = (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
};
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.8
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
}
window.addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "BUTTON" || tag === "INPUT") return;
if (event.code === "Space") {
event.preventDefault();
running = !running;
updatePlayButton();
}
if (event.code === "ArrowRight") {
event.preventDefault();
stepOnce();
}
if (event.key.toLowerCase() === "r") {
resetSimulation();
}
});
function updateCamera() {
const radius = 26;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + radius * Math.cos(orbit.phi),
cameraTarget.z + radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
function renderView(view) {
const width = Math.max(1, view.element.clientWidth);
const height = Math.max(1, view.element.clientHeight);
const aspect = width / height;
const halfHeight =
Math.max(7.2, 6.4 / aspect) / orbit.zoom;
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
view.renderer.render(view.scene, camera);
}
updateLiveMesh(syncView, syncState);
updateLiveMesh(asyncView, asyncState);
updateStats();
updatePlayButton();
let lastTime = performance.now();
function frame(now) {
const deltaSeconds = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
if (running) {
if (!asyncAnimation) {
advanceSynchronous();
asyncAnimation = {
elapsed: 0,
duration: SWEEP_DURATION / speed,
index: 0
};
}
asyncAnimation.elapsed += deltaSeconds * 1000;
const progress = asyncAnimation.elapsed / asyncAnimation.duration;
const targetIndex = Math.min(
CELL_COUNT,
Math.floor(progress * CELL_COUNT)
);
while (asyncAnimation.index < targetIndex) {
processAsyncCell(asyncAnimation.index);
asyncAnimation.index++;
}
if (
asyncAnimation.elapsed >= asyncAnimation.duration &&
asyncAnimation.index >= CELL_COUNT
) {
asyncSweep++;
asyncAnimation = null;
}
}
const nowSeconds = now / 1000;
updateLiveMesh(syncView, syncState);
updateLiveMesh(asyncView, asyncState);
updateMarkerMesh(syncView, nowSeconds);
updateMarkerMesh(asyncView, nowSeconds);
updateCursor(
asyncAnimation
? Math.min(asyncAnimation.index, CELL_COUNT - 1)
: null
);
updateStats();
updateCamera();
for (const view of views) {
renderView(view);
}
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>
```