Prompt specification
Build or customize the Three.js 3D scene "Simple and Compound Growth Towers" 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: Simple and Compound Growth Towers
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/simple-and-compound-growth-towers-x3198
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Animate two block towers under explicit simple and compound rules. Show the base used each round, allowing fractional blocks rather than silently rounding away differences.
Working Three.js Starter (simple-and-compound-growth-towers-x3198):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Rule Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07111d;
--panel: rgba(10, 24, 39, 0.82);
--panel-border: rgba(153, 201, 224, 0.17);
--text: #e8f4fb;
--muted: #8ca8b9;
--cyan: #48eadc;
--cyan-soft: rgba(72, 234, 220, 0.12);
--amber: #ffb454;
--amber-soft: rgba(255, 180, 84, 0.12);
--line: rgba(255, 255, 255, 0.09);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 30%, #10273a 0%, #07111d 48%, #040a11 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
select {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.tower-label {
position: absolute;
min-width: 170px;
padding: 11px 13px 10px;
border: 1px solid var(--panel-border);
border-radius: 12px;
background: rgba(6, 17, 28, 0.86);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
transform: translate(-50%, -100%);
text-align: center;
transition: border-color 180ms ease, box-shadow 180ms ease;
}
.tower-label.cyan {
border-color: rgba(72, 234, 220, 0.42);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.28), 0 0 24px rgba(72, 234, 220, 0.08);
}
.tower-label.amber {
border-color: rgba(255, 180, 84, 0.42);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.28), 0 0 24px rgba(255, 180, 84, 0.08);
}
.label-kicker {
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.cyan .label-kicker {
color: var(--cyan);
}
.amber .label-kicker {
color: var(--amber);
}
.label-value {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 18px;
font-weight: 750;
line-height: 1.2;
letter-spacing: -0.03em;
}
.label-detail {
margin-top: 4px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
#interface {
position: fixed;
inset: 0;
pointer-events: none;
}
header {
position: absolute;
top: 20px;
left: 22px;
right: 22px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 36px);
font-weight: 780;
letter-spacing: -0.045em;
line-height: 1;
}
.subtitle {
margin-top: 7px;
color: var(--muted);
font-size: 12px;
}
.round-chip {
flex: 0 0 auto;
padding: 9px 13px;
border: 1px solid var(--panel-border);
border-radius: 999px;
background: rgba(8, 20, 33, 0.72);
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.round-chip strong {
margin-left: 6px;
color: var(--text);
font-family: "SFMono-Regular", Consolas, monospace;
}
.panel {
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 20px 55px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.rules-panel {
position: absolute;
top: 112px;
left: 22px;
width: 305px;
max-height: calc(100vh - 230px);
padding: 16px;
overflow: auto;
}
.history-panel {
position: absolute;
top: 112px;
right: 22px;
width: 320px;
max-height: calc(100vh - 230px);
overflow: hidden;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.panel-heading h2 {
margin: 0;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.tiny-tag {
padding: 4px 7px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.055);
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.rule {
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.rule + .rule {
margin-top: 9px;
}
.rule-title {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
font-size: 12px;
font-weight: 800;
}
.dot {
width: 8px;
height: 8px;
border-radius: 2px;
box-shadow: 0 0 12px currentColor;
}
.dot.cyan {
color: var(--cyan);
background: var(--cyan);
}
.dot.amber {
color: var(--amber);
background: var(--amber);
}
code,
.formula {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
.formula {
display: block;
padding: 8px 9px;
border-radius: 8px;
background: rgba(0, 0, 0, 0.2);
color: #d9edf7;
font-size: 12px;
line-height: 1.45;
}
.rule-note {
margin-top: 7px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.section-label {
margin: 16px 0 8px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#schedule {
display: grid;
gap: 6px;
}
.schedule-item {
display: grid;
grid-template-columns: 27px 1fr;
gap: 8px;
align-items: center;
padding: 8px 9px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.022);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
line-height: 1.4;
}
.schedule-slot {
color: #d9edf7;
font-weight: 800;
}
.schedule-values {
color: var(--muted);
}
.schedule-values b {
color: var(--text);
font-weight: 650;
}
.exact-note {
margin-top: 13px;
padding: 10px;
border-left: 2px solid var(--cyan);
border-radius: 0 8px 8px 0;
background: var(--cyan-soft);
color: #b9d6df;
font-size: 10px;
line-height: 1.5;
}
.exact-note strong {
color: var(--cyan);
}
.history-list {
max-height: calc(100vh - 305px);
padding: 0 12px 12px;
overflow: auto;
scrollbar-width: thin;
scrollbar-color: #294357 transparent;
}
.history-empty {
padding: 24px 14px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
text-align: center;
}
.history-card {
position: relative;
margin-bottom: 8px;
padding: 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.history-card.active {
border-color: rgba(72, 234, 220, 0.3);
background: linear-gradient(120deg, rgba(72, 234, 220, 0.07), rgba(255, 255, 255, 0.02));
}
.history-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
}
.history-round {
font-size: 11px;
font-weight: 850;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.history-slot {
color: var(--muted);
font-size: 9px;
}
.base-row,
.result-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
padding: 4px 0;
font-size: 10px;
}
.base-row span,
.result-row span {
color: var(--muted);
}
.base-row code,
.result-row code {
color: var(--text);
font-size: 10px;
text-align: right;
}
.base-row .frac {
margin-left: 5px;
color: var(--muted);
font-size: 9px;
}
.result-row {
margin-top: 5px;
padding-top: 8px;
border-top: 1px solid var(--line);
}
.result-row strong {
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 12px;
}
.cyan-text {
color: var(--cyan);
}
.amber-text {
color: var(--amber);
}
.controls {
position: absolute;
left: 50%;
bottom: 20px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border-radius: 14px;
transform: translateX(-50%);
white-space: nowrap;
}
button,
select {
height: 36px;
border: 1px solid var(--panel-border);
border-radius: 9px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button {
padding: 0 13px;
font-size: 11px;
font-weight: 750;
}
button:hover:not(:disabled) {
border-color: rgba(255, 255, 255, 0.26);
background: rgba(255, 255, 255, 0.095);
transform: translateY(-1px);
}
button:active:not(:disabled) {
transform: translateY(0);
}
button:disabled {
cursor: default;
opacity: 0.42;
}
button.primary {
min-width: 92px;
border-color: rgba(72, 234, 220, 0.44);
background: var(--cyan);
color: #041416;
}
button.primary:hover:not(:disabled) {
background: #73f3e8;
}
select {
padding: 0 29px 0 10px;
color: var(--text);
font-size: 10px;
}
.control-divider {
width: 1px;
height: 24px;
background: var(--line);
}
.interaction-hint {
position: absolute;
right: 22px;
bottom: 24px;
color: rgba(170, 194, 209, 0.58);
font-size: 9px;
letter-spacing: 0.04em;
}
@media (max-width: 900px) {
.rules-panel {
left: 12px;
width: 270px;
}
.history-panel {
right: 12px;
width: 280px;
}
.interaction-hint {
display: none;
}
}
@media (max-width: 680px) {
header {
top: 13px;
left: 13px;
right: 13px;
}
.subtitle {
display: none;
}
.rules-panel {
top: 77px;
left: 10px;
width: 205px;
max-height: 42vh;
padding: 11px;
}
.history-panel {
top: 77px;
right: 10px;
width: 205px;
max-height: 42vh;
}
.history-list {
max-height: calc(42vh - 55px);
}
.controls {
bottom: 10px;
max-width: calc(100vw - 20px);
}
button,
select {
height: 34px;
padding-left: 10px;
padding-right: 10px;
}
button.primary {
min-width: 74px;
}
.tower-label {
min-width: 138px;
padding: 8px;
}
.label-value {
font-size: 14px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels">
<div id="labelA" class="tower-label cyan"></div>
<div id="labelB" class="tower-label amber"></div>
</div>
<div id="interface">
<header>
<div>
<div class="eyebrow">Procedural architecture / exact arithmetic</div>
<h1>Voxel Rule Lab</h1>
<div class="subtitle">Two towers, two explicit rules, and every fractional block preserved.</div>
</div>
<div class="round-chip">
Current round <strong id="roundCounter">0</strong>
</div>
</header>
<section class="panel rules-panel">
<div class="panel-heading">
<h2>Rule definitions</h2>
<span class="tiny-tag">Exact rationals</span>
</div>
<div class="rule">
<div class="rule-title">
<span class="dot cyan"></span>
Tower A · Simple rule
</div>
<code class="formula">A₀ = 4<br>Aₙ₊₁ = aₙ · Aₙ</code>
<div class="rule-note">One operation: multiply the current tower by that round’s base.</div>
</div>
<div class="rule">
<div class="rule-title">
<span class="dot amber"></span>
Tower B · Compound rule
</div>
<code class="formula">B₀ = 3<br>Bₙ₊₁ = bₙ · (Bₙ + cₙ)</code>
<div class="rule-note">Add the signed offset, then multiply by that round’s base.</div>
</div>
<div class="section-label">Four-round base cycle</div>
<div id="schedule"></div>
<div class="exact-note">
<strong>No hidden rounding.</strong>
Values are stored as exact fractions. A remainder such as ¾ is drawn as a
three-quarter-height voxel instead of being rounded to a whole block.
</div>
</section>
<section class="panel history-panel">
<div class="panel-heading">
<h2>Base used by round</h2>
<span class="tiny-tag">Newest first</span>
</div>
<div id="history" class="history-list">
<div class="history-empty">
Step the scene to record each base, offset, and exact result.
</div>
</div>
</section>
<div class="panel controls">
<button id="stepButton" class="primary">Step round</button>
<button id="playButton">Play</button>
<div class="control-divider"></div>
<select id="speedSelect" aria-label="Playback speed">
<option value="2100">Slow</option>
<option value="1350" selected>Normal</option>
<option value="750">Fast</option>
</select>
<div class="control-divider"></div>
<button id="resetButton">Reset</button>
<button id="cameraButton" title="Reset the camera">View</button>
</div>
<div class="interaction-hint">
Drag to orbit · Scroll to zoom · Space to play/pause · → to step
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Exact rational arithmetic
// -------------------------------------------------------------------------
class Rational {
constructor(n, d = 1n) {
n = typeof n === "bigint" ? n : BigInt(n);
d = typeof d === "bigint" ? d : BigInt(d);
if (d === 0n) {
throw new Error("A rational number cannot have a zero denominator.");
}
if (d < 0n) {
n = -n;
d = -d;
}
const divisor = gcd(n, d);
this.n = n / divisor;
this.d = d / divisor;
}
static decimal(value) {
const text = String(value).trim();
if (!/^-?(?:\d+(?:\.\d*)?|\.\d+)$/.test(text)) {
throw new Error(`Invalid decimal: ${value}`);
}
const negative = text.startsWith("-");
const unsigned = negative ? text.slice(1) : text;
const [integerPart, fractionPart = ""] = unsigned.split(".");
const digits = `${integerPart || "0"}${fractionPart}`;
const denominator = 10n ** BigInt(fractionPart.length);
let numerator = BigInt(digits);
if (negative) {
numerator = -numerator;
}
return new Rational(numerator, denominator);
}
add(other) {
return new Rational(
this.n * other.d + other.n * this.d,
this.d * other.d
);
}
sub(other) {
return new Rational(
this.n * other.d - other.n * this.d,
this.d * other.d
);
}
mul(other) {
return new Rational(this.n * other.n, this.d * other.d);
}
floor() {
return this.n / this.d;
}
}
function gcd(a, b) {
a = a < 0n ? -a : a;
b = b < 0n ? -b : b;
while (b !== 0n) {
const remainder = a % b;
a = b;
b = remainder;
}
return a;
}
// Formats a terminating decimal exactly. If a denominator does not have
// only factors of 2 and 5, the exact fraction is returned instead.
function exact(value) {
if (value.d === 1n) {
return value.n.toString();
}
let remainder = value.d;
let twos = 0;
let fives = 0;
while (remainder % 2n === 0n) {
remainder /= 2n;
twos++;
}
while (remainder % 5n === 0n) {
remainder /= 5n;
fives++;
}
if (remainder !== 1n) {
return `${value.n}/${value.d}`;
}
const places = Math.max(twos, fives);
const scale = 10n ** BigInt(places);
const scaled = value.n * (scale / value.d);
const negative = scaled < 0n;
const digits = (negative ? -scaled : scaled)
.toString()
.padStart(places + 1, "0");
let result;
if (places === 0) {
result = digits;
} else {
result = `${digits.slice(0, -places)}.${digits.slice(-places)}`;
result = result.replace(/0+$/, "").replace(/\.$/, "");
}
return negative ? `-${result}` : result;
}
function signedExact(value) {
const text = exact(value);
return value.n >= 0n ? `+${text}` : text;
}
function fractionHTML(value) {
if (value.d === 1n) {
return exact(value);
}
return `${exact(value)}<span class="frac">= ${value.n}/${value.d}</span>`;
}
function blockBreakdown(value) {
const whole = value.floor();
const remainder = value.sub(new Rational(whole));
if (remainder.n === 0n) {
return `${whole} whole block${whole === 1n ? "" : "s"}`;
}
return `${whole} whole + ${exact(remainder)} block`;
}
// -------------------------------------------------------------------------
// Explicit rules and base schedules
// -------------------------------------------------------------------------
const schedule = [
{
a: Rational.decimal("1.25"),
b: Rational.decimal("0.80"),
c: Rational.decimal("0.50")
},
{
a: Rational.decimal("0.75"),
b: Rational.decimal("1.20"),
c: Rational.decimal("-0.25")
},
{
a: Rational.decimal("1.50"),
b: Rational.decimal("0.60"),
c: Rational.decimal("0.75")
},
{
a: Rational.decimal("0.50"),
b: Rational.decimal("1.40"),
c: Rational.decimal("-0.50")
}
];
const scheduleElement = document.querySelector("#schedule");
schedule.forEach((rule, index) => {
const item = document.createElement("div");
item.className = "schedule-item";
item.innerHTML = `
<div class="schedule-slot">C${index + 1}</div>
<div class="schedule-values">
<b class="cyan-text">a ${exact(rule.a)}</b><br>
<b class="amber-text">b ${exact(rule.b)}</b> ·
c ${signedExact(rule.c)}
</div>
`;
scheduleElement.appendChild(item);
});
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const container = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.FogExp2(0x07111d, 0.027);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
theta: 0.47,
phi: 1.08,
radius: 10.6,
target: new THREE.Vector3(0, 2.35, 0)
};
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
const horizontal = Math.sin(phi) * radius;
camera.position.set(
target.x + horizontal * Math.sin(theta),
target.y + Math.cos(phi) * radius,
target.z + horizontal * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
// -------------------------------------------------------------------------
// Direct camera controls: no examples/addons modules are used.
// -------------------------------------------------------------------------
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 deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.005,
0.38,
1.45
);
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7.2,
19
);
},
{ passive: false }
);
function resetCamera() {
cameraState.theta = 0.47;
cameraState.phi = 1.08;
cameraState.radius = 10.6;
cameraState.target.set(0, 2.35, 0);
}
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Lighting and environment
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb9eaff, 0x101722, 1.75));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.4);
keyLight.position.set(5, 10, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x48eadc, 18, 9, 2);
cyanLight.position.set(-3.3, 3.5, 2.5);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xffb454, 18, 9, 2);
amberLight.position.set(3.3, 3.2, 2.4);
scene.add(amberLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x07111a,
roughness: 0.92,
metalness: 0.03
})
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 60, 0x22495c, 0x102631);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
const BASE_Y = 0.43;
const towerX = [-1.8, 1.8];
const towerColors = [0x48eadc, 0xffb454];
function makePedestal(x, color) {
const group = new THREE.Group();
group.position.x = x;
const pedestal = new THREE.Mesh(
new THREE.BoxGeometry(2.65, 0.4, 2.65),
new THREE.MeshStandardMaterial({
color: 0x102433,
roughness: 0.54,
metalness: 0.26,
emissive: new THREE.Color(color).multiplyScalar(0.025),
emissiveIntensity: 0.7
})
);
pedestal.position.y = 0.2;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
group.add(pedestal);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(pedestal.geometry),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.34
})
);
edges.position.copy(pedestal.position);
group.add(edges);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.48, 0.025, 8, 64),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.72
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.415;
group.add(ring);
scene.add(group);
return group;
}
const pedestals = [
makePedestal(towerX[0], towerColors[0]),
makePedestal(towerX[1], towerColors[1])
];
// A sparse field of distant voxel markers gives the scene depth.
const markerGeometry = new THREE.BoxGeometry(0.16, 0.16, 0.16);
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0x173247,
emissive: 0x0a2636,
emissiveIntensity: 0.8,
roughness: 0.7
});
const markerCount = 44;
const markers = new THREE.InstancedMesh(markerGeometry, markerMaterial, markerCount);
const markerMatrix = new THREE.Matrix4();
for (let i = 0; i < markerCount; i++) {
const angle = (i / markerCount) * Math.PI * 2;
const radius = 7.5 + (i % 5) * 0.8;
const height = 0.25 + (i % 7) * 0.13;
markerMatrix.makeScale(1, 1 + (i % 4), 1);
markerMatrix.setPosition(
Math.sin(angle) * radius,
height * 0.5,
Math.cos(angle) * radius
);
markers.setMatrixAt(i, markerMatrix);
}
scene.add(markers);
// -------------------------------------------------------------------------
// Voxel tower construction
// -------------------------------------------------------------------------
const blockGeometry = new THREE.BoxGeometry(1.34, 1, 1.34);
const blockEdgeGeometry = new THREE.EdgesGeometry(blockGeometry);
function createBlock(height, index, color, partial = false) {
const baseColor = new THREE.Color(color);
const shade = partial ? 1.15 : 0.84 + (index % 3) * 0.075;
baseColor.multiplyScalar(shade);
const material = new THREE.MeshStandardMaterial({
color: baseColor,
emissive: new THREE.Color(color),
emissiveIntensity: partial ? 0.17 : 0.055,
roughness: partial ? 0.3 : 0.42,
metalness: 0.12,
transparent: true,
opacity: 1
});
const mesh = new THREE.Mesh(blockGeometry, material);
mesh.scale.y = height;
mesh.castShadow = true;
mesh.receiveShadow = true;
const edgeMaterial = new THREE.LineBasicMaterial({
color: partial ? 0xffffff : 0xd9fbff,
transparent: true,
opacity: partial ? 0.72 : 0.24
});
const edges = new THREE.LineSegments(blockEdgeGeometry, edgeMaterial);
edges.userData.isEdge = true;
mesh.add(edges);
return mesh;
}
function buildTower(value, color) {
const group = new THREE.Group();
group.position.y = BASE_Y;
group.position.x = color === towerColors[0] ? towerX[0] : towerX[1];
const whole = value.floor();
const remainder = value.sub(new Rational(whole));
for (let i = 0; i < Number(whole); i++) {
const block = createBlock(1, i, color);
block.position.y = i + 0.5;
group.add(block);
}
if (remainder.n > 0n) {
const partialHeight = Number(remainder.n) / Number(remainder.d);
const partial = createBlock(partialHeight, Number(whole), color, true);
partial.position.y = Number(whole) + partialHeight / 2;
group.add(partial);
}
return group;
}
function setGroupOpacity(group, opacity) {
group.traverse((object) => {
if (object.material) {
object.material.opacity = opacity;
}
});
}
function disposeGroup(group) {
if (!group) return;
group.traverse((object) => {
if (object.material) {
object.material.dispose();
}
});
scene.remove(group);
}
// -------------------------------------------------------------------------
// State, labels, and round history
// -------------------------------------------------------------------------
const INITIAL_A = new Rational(4n);
const INITIAL_B = new Rational(3n);
let currentA = INITIAL_A;
let currentB = INITIAL_B;
let round = 0;
let towerA = buildTower(currentA, towerColors[0]);
let towerB = buildTower(currentB, towerColors[1]);
scene.add(towerA);
scene.add(towerB);
let transition = null;
let lastRule = null;
let playing = false;
let nextAutoRoundAt = 0;
const labelA = document.querySelector("#labelA");
const labelB = document.querySelector("#labelB");
const roundCounter = document.querySelector("#roundCounter");
const historyElement = document.querySelector("#history");
function updateTowerLabel(element, type, value, rule, oldValue = null) {
const isA = type === "A";
const transitioning = oldValue !== null;
let kicker;
if (!rule) {
kicker = isA ? "Simple · awaiting base" : "Compound · awaiting base";
} else if (isA) {
kicker = `Simple · base a = ${exact(rule.a)}`;
} else {
kicker = `Compound · b = ${exact(rule.b)} · c = ${signedExact(rule.c)}`;
}
const displayedValue = transitioning
? `${exact(oldValue)} → ${exact(value)}`
: exact(value);
element.innerHTML = `
<div class="label-kicker">${kicker}</div>
<div class="label-value">${displayedValue}</div>
<div class="label-detail">${blockBreakdown(value)}</div>
`;
}
function updateLabels(oldA = null, oldB = null) {
updateTowerLabel(labelA, "A", currentA, lastRule, oldA);
updateTowerLabel(labelB, "B", currentB, lastRule, oldB);
roundCounter.textContent = String(round);
}
function addHistoryCard(rule, nextA, nextB) {
const empty = historyElement.querySelector(".history-empty");
if (empty) empty.remove();
historyElement.querySelectorAll(".history-card.active").forEach((card) => {
card.classList.remove("active");
});
const card = document.createElement("article");
card.className = "history-card active";
const cycleSlot = (round % schedule.length) + 1;
card.innerHTML = `
<div class="history-top">
<div class="history-round">Round ${round + 1}</div>
<div class="history-slot">cycle slot ${cycleSlot}</div>
</div>
<div class="base-row">
<span class="cyan-text">Simple base a</span>
<code>${fractionHTML(rule.a)}</code>
</div>
<div class="base-row">
<span class="amber-text">Compound base b</span>
<code>${fractionHTML(rule.b)}</code>
</div>
<div class="base-row">
<span class="amber-text">Compound offset c</span>
<code>${fractionHTML(rule.c)}</code>
</div>
<div class="result-row">
<span>Simple result A</span>
<strong class="cyan-text">${exact(nextA)}</strong>
</div>
<div class="result-row">
<span>Compound result B</span>
<strong class="amber-text">${exact(nextB)}</strong>
</div>
`;
historyElement.prepend(card);
}
// -------------------------------------------------------------------------
// Round animation
// -------------------------------------------------------------------------
const stepButton = document.querySelector("#stepButton");
const playButton = document.querySelector("#playButton");
const resetButton = document.querySelector("#resetButton");
const cameraButton = document.querySelector("#cameraButton");
const speedSelect = document.querySelector("#speedSelect");
function smoothstep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function beginRound() {
if (transition) return;
const rule = schedule[round % schedule.length];
const nextA = rule.a.mul(currentA);
const nextB = rule.b.mul(currentB.add(rule.c));
const oldA = towerA;
const oldB = towerB;
const newA = buildTower(nextA, towerColors[0]);
const newB = buildTower(nextB, towerColors[1]);
newA.scale.y = 0.001;
newB.scale.y = 0.001;
setGroupOpacity(newA, 0);
setGroupOpacity(newB, 0);
scene.add(newA);
scene.add(newB);
lastRule = rule;
transition = {
start: performance.now(),
duration: 900,
rule,
nextA,
nextB,
oldA,
oldB,
newA,
newB
};
addHistoryCard(rule, nextA, nextB);
updateLabels(currentA, currentB);
stepButton.disabled = true;
}
function finishTransition(now) {
disposeGroup(transition.oldA);
disposeGroup(transition.oldB);
towerA = transition.newA;
towerB = transition.newB;
towerA.scale.y = 1;
towerB.scale.y = 1;
setGroupOpacity(towerA, 1);
setGroupOpacity(towerB, 1);
currentA = transition.nextA;
currentB = transition.nextB;
round++;
transition = null;
stepButton.disabled = false;
updateLabels();
nextAutoRoundAt = now + Number(speedSelect.value);
}
function updateTransition(now) {
if (!transition) return;
const progress = THREE.MathUtils.clamp(
(now - transition.start) / transition.duration,
0,
1
);
const exitProgress = smoothstep(progress / 0.42);
const enterProgress = smoothstep((progress - 0.25) / 0.75);
transition.oldA.scale.y = Math.max(0.001, 1 - exitProgress);
transition.oldB.scale.y = Math.max(0.001, 1 - exitProgress);
setGroupOpacity(transition.oldA, 1 - exitProgress);
setGroupOpacity(transition.oldB, 1 - exitProgress);
transition.newA.scale.y = Math.max(0.001, enterProgress);
transition.newB.scale.y = Math.max(0.001, enterProgress);
setGroupOpacity(transition.newA, enterProgress);
setGroupOpacity(transition.newB, enterProgress);
if (progress >= 1) {
finishTransition(now);
}
}
function resetScene() {
if (transition) {
disposeGroup(transition.oldA);
disposeGroup(transition.oldB);
disposeGroup(transition.newA);
disposeGroup(transition.newB);
transition = null;
}
disposeGroup(towerA);
disposeGroup(towerB);
currentA = INITIAL_A;
currentB = INITIAL_B;
round = 0;
lastRule = null;
towerA = buildTower(currentA, towerColors[0]);
towerB = buildTower(currentB, towerColors[1]);
scene.add(towerA);
scene.add(towerB);
historyElement.innerHTML = `
<div class="history-empty">
Step the scene to record each base, offset, and exact result.
</div>
`;
updateLabels();
stepButton.disabled = false;
nextAutoRoundAt = 0;
}
// -------------------------------------------------------------------------
// Controls
// -------------------------------------------------------------------------
function setPlaying(value) {
playing = value;
playButton.textContent = playing ? "Pause" : "Play";
if (playing && !transition) {
nextAutoRoundAt = performance.now() + 300;
}
}
stepButton.addEventListener("click", () => {
if (!transition) {
beginRound();
}
});
playButton.addEventListener("click", () => {
setPlaying(!playing);
});
resetButton.addEventListener("click", resetScene);
cameraButton.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
setPlaying(!playing);
}
if (event.code === "ArrowRight" && !transition) {
event.preventDefault();
beginRound();
}
if (event.key.toLowerCase() === "r") {
resetScene();
}
});
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, x, height) {
projected.set(x, BASE_Y + height + 0.78, 0);
projected.project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const screenX = (projected.x * 0.5 + 0.5) * window.innerWidth;
const screenY = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${screenX}px`;
element.style.top = `${screenY}px`;
}
function positionLabels() {
const heightA = transition ? Number(transition.nextA.n) / Number(transition.nextA.d) : Number(currentA.n) / Number(currentA.d);
const heightB = transition ? Number(transition.nextB.n) / Number(transition.nextB.d) : Number(currentB.n) / Number(currentB.d);
positionLabel(labelA, towerX[0], heightA);
positionLabel(labelB, towerX[1], heightB);
}
// -------------------------------------------------------------------------
// Resize and animation loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
function animate(now) {
requestAnimationFrame(animate);
if (transition) {
updateTransition(now);
} else if (playing && now >= nextAutoRoundAt) {
beginRound();
}
updateCamera();
positionLabels();
// A subtle breathing effect on the pedestal lighting.
const pulse = 0.72 + Math.sin(now * 0.0017) * 0.08;
pedestals[0].children[0].material.emissiveIntensity = pulse;
pedestals[1].children[0].material.emissiveIntensity = pulse;
renderer.render(scene, camera);
}
updateLabels();
requestAnimationFrame(animate);
</script>
</body>
</html>
```