Prompt specification
Build or customize the Three.js 3D scene "Undo stack and service queue 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: Undo stack and service queue comparison
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/undo-stack-and-service-queue-comparison-x1910
Scene Description Prompt:
Animate the same arrival sequence through an undo history and a ticket queue; show different removal orders without losing or duplicating tokens. Build this with Three.js from cdnjs as a stylized scene in one self-contained HTML file.
Working Three.js Starter (undo-stack-and-service-queue-comparison-x1910):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Order Lab — Undo History vs. Ticket Queue</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 23, 40, 0.78);
--panel-border: rgba(163, 211, 255, 0.14);
--text: #eaf5ff;
--muted: #8da8bf;
--cyan: #4fe3ff;
--coral: #ff7a90;
--gold: #ffd166;
--green: #67e8b1;
--red: #ff6b7d;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 18%, #142b47 0, transparent 45%),
linear-gradient(145deg, #07111f, #050a12);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, canvas { -webkit-tap-highlight-color: transparent; }
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#stage canvas:active { cursor: grabbing; }
.chrome {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.topbar {
position: absolute;
top: 24px;
left: 28px;
max-width: 540px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 2px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 43px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 500px;
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.subtitle strong { color: #d8f5ff; }
.stats {
position: absolute;
top: 24px;
right: 28px;
display: grid;
grid-template-columns: repeat(4, minmax(86px, 1fr));
gap: 8px;
}
.stat {
min-width: 88px;
padding: 11px 13px;
border: 1px solid var(--panel-border);
border-radius: 13px;
background: var(--panel);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(14px);
}
.stat span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.stat b {
display: block;
margin-top: 5px;
font-size: 16px;
letter-spacing: 0.03em;
}
#integrityState { color: var(--green); }
#integrityState.bad { color: var(--red); }
.policy {
position: absolute;
bottom: 142px;
width: min(285px, 24vw);
padding: 16px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(16px);
overflow: hidden;
}
.policy::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
height: 2px;
}
.policy.history {
left: 28px;
}
.policy.history::before {
background: var(--cyan);
box-shadow: 0 0 18px var(--cyan);
}
.policy.queue {
right: 28px;
}
.policy.queue::before {
background: var(--coral);
box-shadow: 0 0 18px var(--coral);
}
.policy-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.policy h2 {
margin: 0;
font-size: 15px;
letter-spacing: -0.02em;
}
.policy-tag {
padding: 5px 7px;
border-radius: 7px;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
}
.history .policy-tag {
color: var(--cyan);
background: rgba(79, 227, 255, 0.1);
border: 1px solid rgba(79, 227, 255, 0.2);
}
.queue .policy-tag {
color: var(--coral);
background: rgba(255, 122, 144, 0.1);
border: 1px solid rgba(255, 122, 144, 0.2);
}
.policy-meta {
display: flex;
align-items: baseline;
gap: 7px;
margin: 13px 0 11px;
}
.policy-meta strong {
font-size: 25px;
line-height: 1;
}
.policy-meta span {
color: var(--muted);
font-size: 11px;
}
.mini-label {
margin: 11px 0 6px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.order {
min-height: 27px;
color: #dbeeff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
line-height: 1.65;
word-break: break-word;
}
.order .empty {
color: #59738b;
font-family: inherit;
font-style: italic;
}
.order .released {
color: var(--green);
}
.policy-note {
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid rgba(255, 255, 255, 0.07);
color: #7793aa;
font-size: 10px;
line-height: 1.45;
}
.timeline-wrap {
position: absolute;
left: 50%;
bottom: 140px;
width: min(490px, 42vw);
transform: translateX(-50%);
text-align: center;
}
.timeline-label {
margin-bottom: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.timeline {
display: flex;
justify-content: center;
gap: 5px;
padding: 8px;
border: 1px solid var(--panel-border);
border-radius: 14px;
background: rgba(7, 17, 31, 0.72);
backdrop-filter: blur(12px);
}
.arrival-chip {
position: relative;
width: 48px;
padding: 7px 5px 13px;
border: 1px solid rgba(151, 185, 211, 0.13);
border-radius: 9px;
color: #607b92;
background: rgba(255, 255, 255, 0.025);
font-size: 11px;
font-weight: 800;
transition: 180ms ease;
}
.arrival-chip.arrived {
color: #dcecff;
border-color: rgba(255, 209, 102, 0.35);
background: rgba(255, 209, 102, 0.07);
}
.dots {
position: absolute;
right: 0;
bottom: 4px;
left: 0;
display: flex;
justify-content: center;
gap: 4px;
}
.dots i {
width: 5px;
height: 5px;
border-radius: 50%;
background: #263d50;
}
.dots i.h-live { background: var(--cyan); box-shadow: 0 0 6px var(--cyan); }
.dots i.q-live { background: var(--coral); box-shadow: 0 0 6px var(--coral); }
.dots i.h-out { background: #326f7a; }
.dots i.q-out { background: #7a4253; }
.controls {
position: absolute;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 7px;
padding: 8px;
transform: translateX(-50%);
border: 1px solid var(--panel-border);
border-radius: 17px;
background: rgba(8, 19, 33, 0.86);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.controls button {
min-height: 39px;
padding: 0 14px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 11px;
color: var(--text);
background: rgba(255, 255, 255, 0.055);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease, opacity 120ms ease;
white-space: nowrap;
}
.controls button:hover:not(:disabled) {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.1);
}
.controls button:active:not(:disabled) {
transform: translateY(1px);
}
.controls button:disabled {
cursor: not-allowed;
opacity: 0.33;
}
.controls button.primary {
color: #06131e;
border-color: transparent;
background: linear-gradient(135deg, #74edff, #4fe3ff);
box-shadow: 0 8px 24px rgba(79, 227, 255, 0.18);
}
.controls button.history-action {
color: var(--cyan);
border-color: rgba(79, 227, 255, 0.2);
}
.controls button.queue-action {
color: var(--coral);
border-color: rgba(255, 122, 144, 0.2);
}
.run-status {
position: absolute;
left: 50%;
bottom: 82px;
width: max-content;
max-width: 80vw;
transform: translateX(-50%);
color: #89a4ba;
font-size: 10px;
text-align: center;
}
.camera-hint {
position: absolute;
right: 28px;
bottom: 28px;
color: #5d758b;
font-size: 9px;
letter-spacing: 0.05em;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: var(--cyan);
background: #07111f;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.ready {
opacity: 0;
pointer-events: none;
}
@media (max-width: 950px) {
.topbar { left: 18px; top: 18px; max-width: 440px; }
.stats { top: 18px; right: 18px; grid-template-columns: repeat(2, 90px); }
.stat:nth-child(2) { display: none; }
.policy { bottom: 142px; width: 230px; }
.policy.history { left: 18px; }
.policy.queue { right: 18px; }
.timeline-wrap { width: 360px; }
.camera-hint { display: none; }
}
@media (max-width: 700px) {
.topbar { left: 14px; top: 14px; right: 14px; }
.subtitle { display: none; }
.eyebrow { margin-bottom: 5px; font-size: 9px; }
h1 { font-size: 25px; }
.stats {
top: 70px;
right: auto;
left: 14px;
grid-template-columns: repeat(3, 78px);
}
.stat { min-width: 0; padding: 8px 9px; }
.stat b { font-size: 13px; }
.policy {
top: 150px;
bottom: auto;
width: calc(50% - 21px);
padding: 12px;
}
.policy.history { left: 14px; }
.policy.queue { right: 14px; }
.policy h2 { font-size: 12px; }
.policy-meta { margin: 9px 0 6px; }
.policy-meta strong { font-size: 19px; }
.policy .mini-label:nth-of-type(2),
.policy .order:nth-of-type(2),
.policy-note { display: none; }
.timeline-wrap {
left: 14px;
right: 14px;
bottom: 128px;
width: auto;
transform: none;
}
.arrival-chip { width: 38px; }
.controls {
right: 10px;
bottom: 14px;
left: 10px;
justify-content: center;
transform: none;
flex-wrap: wrap;
}
.controls button { min-height: 35px; padding: 0 10px; font-size: 10px; }
.run-status { bottom: 95px; }
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="chrome">
<header class="topbar">
<div class="eyebrow">Data structure choreography</div>
<h1>Order Lab</h1>
<p class="subtitle">
One arrival sequence, two policy views. The undo history releases
<strong>newest first</strong>; the ticket queue releases
<strong>oldest first</strong>. Every unique arrival is accounted for once in each lane.
</p>
</header>
<div class="stats">
<div class="stat">
<span>Arrivals</span>
<b id="arrivedCount">0</b>
</div>
<div class="stat">
<span>Unique IDs</span>
<b id="uniqueCount">0</b>
</div>
<div class="stat">
<span>Integrity</span>
<b id="integrityState">BALANCED</b>
</div>
<div class="stat">
<span>Mode</span>
<b id="modeState">MANUAL</b>
</div>
</div>
<section class="policy history">
<div class="policy-head">
<h2>Undo History</h2>
<span class="policy-tag">LIFO</span>
</div>
<div class="policy-meta">
<strong id="stackCount">0</strong>
<span>live tokens</span>
</div>
<div class="mini-label">Current stack · newest → oldest</div>
<div class="order" id="stackCurrent"><span class="empty">empty</span></div>
<div class="mini-label">Removal order</div>
<div class="order" id="historyOrder"><span class="empty">nothing released</span></div>
<div class="policy-note">
Undo pops the latest arrival. Released tokens remain visible in the output row as evidence.
</div>
</section>
<section class="policy queue">
<div class="policy-head">
<h2>Ticket Queue</h2>
<span class="policy-tag">FIFO</span>
</div>
<div class="policy-meta">
<strong id="queueCount">0</strong>
<span>live tokens</span>
</div>
<div class="mini-label">Current queue · head → tail</div>
<div class="order" id="queueCurrent"><span class="empty">empty</span></div>
<div class="mini-label">Removal order</div>
<div class="order" id="queueOrder"><span class="empty">nothing released</span></div>
<div class="policy-note">
Serving removes the head. The same arrival IDs move forward without skipping or duplication.
</div>
</section>
<div class="timeline-wrap">
<div class="timeline-label">Shared arrival sequence</div>
<div class="timeline" id="timeline"></div>
</div>
<div class="run-status" id="runStatus" aria-live="polite">
Ready · add an arrival or run both policies
</div>
<nav class="controls" aria-label="Animation controls">
<button id="addBtn" class="primary">+ Arrive</button>
<button id="popBtn" class="history-action">↶ Undo pop</button>
<button id="serveBtn" class="queue-action">→ Serve head</button>
<button id="compareBtn">▶ Run comparison</button>
<button id="resetBtn">Reset</button>
</nav>
<div class="camera-hint">DRAG TO ORBIT · WHEEL TO ZOOM · DOUBLE-CLICK TO RECENTER</div>
</div>
<div id="loading">Loading Three.js scene</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const loading = document.getElementById("loading");
const ui = {
arrivedCount: document.getElementById("arrivedCount"),
uniqueCount: document.getElementById("uniqueCount"),
integrityState: document.getElementById("integrityState"),
modeState: document.getElementById("modeState"),
stackCount: document.getElementById("stackCount"),
queueCount: document.getElementById("queueCount"),
stackCurrent: document.getElementById("stackCurrent"),
queueCurrent: document.getElementById("queueCurrent"),
historyOrder: document.getElementById("historyOrder"),
queueOrder: document.getElementById("queueOrder"),
timeline: document.getElementById("timeline"),
runStatus: document.getElementById("runStatus"),
addBtn: document.getElementById("addBtn"),
popBtn: document.getElementById("popBtn"),
serveBtn: document.getElementById("serveBtn"),
compareBtn: document.getElementById("compareBtn"),
resetBtn: document.getElementById("resetBtn")
};
const COLORS = {
cyan: 0x4fe3ff,
coral: 0xff7a90,
gold: 0xffd166,
green: 0x67e8b1,
text: 0xeaf5ff,
muted: 0x8da8bf
};
const ARRIVALS = ["A1", "A2", "A3", "A4", "A5", "A6", "A7"];
const TOKEN_COLORS = [
0x56e0c2,
0x7b9cff,
0xff7aa2,
0xf7c66b,
0xa78bfa,
0x55d6ff,
0xff9f68
];
const SOURCE = new THREE.Vector3(0, 5.45, 0.15);
const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.FogExp2(0x07111f, 0.026);
const camera = new THREE.PerspectiveCamera(51, innerWidth / innerHeight, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.16;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive 3D order lab");
renderer.domElement.tabIndex = 0;
stage.appendChild(renderer.domElement);
scene.add(new THREE.AmbientLight(0xffffff, 0.48));
const hemi = new THREE.HemisphereLight(0xbce8ff, 0x09111f, 1.8);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 2.7);
key.position.set(3, 11, 8);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -12;
key.shadow.camera.right = 12;
key.shadow.camera.top = 12;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 1;
key.shadow.camera.far = 35;
scene.add(key);
const cyanLight = new THREE.PointLight(COLORS.cyan, 22, 12, 2);
cyanLight.position.set(-5, 4, 3);
scene.add(cyanLight);
const coralLight = new THREE.PointLight(COLORS.coral, 22, 12, 2);
coralLight.position.set(5, 3, 3);
scene.add(coralLight);
const goldLight = new THREE.PointLight(COLORS.gold, 15, 9, 2);
goldLight.position.set(0, 6, 1);
scene.add(goldLight);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.48,
metalness: 0.12,
...options
});
}
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeLabelTexture(text, color = "#eaf5ff", withBackground = false) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
if (withBackground) {
roundedRectPath(ctx, 10, 10, 1004, 236, 34);
ctx.fillStyle = "rgba(5, 13, 24, 0.84)";
ctx.fill();
ctx.strokeStyle = color;
ctx.globalAlpha = 0.45;
ctx.lineWidth = 5;
ctx.stroke();
ctx.globalAlpha = 1;
}
ctx.font = "800 78px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.shadowColor = color;
ctx.shadowBlur = 18;
ctx.fillStyle = color;
ctx.fillText(text, 512, 131);
ctx.shadowBlur = 0;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
return texture;
}
function makeLabel(text, color = "#eaf5ff", width = 3.2, background = true) {
const material = new THREE.SpriteMaterial({
map: makeLabelTexture(text, color, background),
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width / 4, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeTokenLabel(id, color) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.font = "900 64px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.shadowColor = color;
ctx.shadowBlur = 15;
ctx.fillStyle = "#ffffff";
ctx.fillText(id, 128, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.68, 0.34, 1);
sprite.position.z = 0.13;
sprite.renderOrder = 12;
return sprite;
}
function addEdges(mesh, color = 0x35516a) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({ color, transparent: true, opacity: 0.6 })
);
mesh.add(edges);
}
function makeBox(width, height, depth, color, x, y, z, edgeColor) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
standardMaterial(color, { roughness: 0.64, metalness: 0.08 })
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
if (edgeColor !== undefined) addEdges(mesh, edgeColor);
scene.add(mesh);
return mesh;
}
function makePath(points, color) {
const curve = new THREE.CatmullRomCurve3(points, false, "catmullrom", 0.5);
const geometry = new THREE.BufferGeometry().setFromPoints(curve.getPoints(80));
const material = new THREE.LineDashedMaterial({
color,
transparent: true,
opacity: 0.32,
dashSize: 0.17,
gapSize: 0.11
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
scene.add(line);
return line;
}
// Floor and grid.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(36, 28),
standardMaterial(0x091727, { roughness: 0.92, metalness: 0.02 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.03;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x24445e, 0x142a3d);
grid.position.y = 0;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
// Arrival portal.
const portal = new THREE.Mesh(
new THREE.TorusGeometry(0.62, 0.075, 12, 56),
new THREE.MeshBasicMaterial({ color: COLORS.gold })
);
portal.position.set(SOURCE.x, SOURCE.y, -0.52);
scene.add(portal);
const portalInner = new THREE.Mesh(
new THREE.CircleGeometry(0.52, 48),
new THREE.MeshBasicMaterial({
color: COLORS.gold,
transparent: true,
opacity: 0.08,
side: THREE.DoubleSide,
depthWrite: false
})
);
portalInner.position.set(SOURCE.x, SOURCE.y, -0.55);
scene.add(portalInner);
const arrivalLabel = makeLabel("ARRIVAL PORTAL", "#ffd166", 3.2);
arrivalLabel.position.set(0, 6.55, -0.1);
scene.add(arrivalLabel);
// Undo stack tower.
makeBox(2.5, 4.65, 0.22, 0x10253a, -5.05, 2.35, -0.72, 0x244c64);
makeBox(2.75, 0.18, 1.35, 0x0c1b2a, -5.05, 0.1, -0.03, 0x1e3c50);
makeBox(0.055, 4.55, 0.08, COLORS.cyan, -6.22, 2.35, -0.54);
for (let i = 0; i < ARRIVALS.length; i++) {
const y = 0.48 + i * 0.52;
makeBox(1.82, 0.035, 0.38, 0x17364a, -5.05, y, -0.36);
const notch = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.04, 0.08),
new THREE.MeshBasicMaterial({ color: COLORS.cyan })
);
notch.position.set(-6.05, y + 0.02, -0.15);
scene.add(notch);
}
const historyLabel = makeLabel("UNDO HISTORY · LIFO", "#4fe3ff", 3.3);
historyLabel.position.set(-5.05, 5.12, -0.3);
scene.add(historyLabel);
// Ticket queue.
makeBox(5.95, 1.5, 0.22, 0x241b2d, 4.35, 0.73, -0.72, 0x583349);
makeBox(6.15, 0.18, 1.35, 0x0c1b2a, 4.35, 0.1, -0.03, 0x3f3045);
for (let i = 0; i < ARRIVALS.length; i++) {
const x = 2.05 + i * 0.73;
const marker = new THREE.Mesh(
new THREE.RingGeometry(0.16, 0.21, 24),
new THREE.MeshBasicMaterial({
color: COLORS.coral,
transparent: true,
opacity: 0.46,
side: THREE.DoubleSide
})
);
marker.rotation.x = -Math.PI / 2;
marker.position.set(x, 0.23, -0.08);
scene.add(marker);
}
const queueHead = new THREE.Mesh(
new THREE.ConeGeometry(0.16, 0.38, 3),
new THREE.MeshBasicMaterial({ color: COLORS.coral })
);
queueHead.rotation.z = Math.PI / 2;
queueHead.position.set(1.72, 1.0, 0.16);
scene.add(queueHead);
const queueLabel = makeLabel("TICKET QUEUE · FIFO", "#ff7a90", 3.4);
queueLabel.position.set(4.45, 2.28, -0.3);
scene.add(queueLabel);
// Output rows.
makeBox(3.5, 0.12, 1.25, 0x102c37, -5.05, 0.08, 2.15, 0x245466);
makeBox(4.3, 0.12, 1.25, 0x351c29, 4.65, 0.08, 2.15, 0x684050);
const historyOutLabel = makeLabel("HISTORY OUT", "#4fe3ff", 2.3);
historyOutLabel.position.set(-5.05, 1.62, 2.05);
scene.add(historyOutLabel);
const queueOutLabel = makeLabel("QUEUE OUT", "#ff7a90", 2.3);
queueOutLabel.position.set(4.65, 1.62, 2.05);
scene.add(queueOutLabel);
// Soft visual routes from the portal to both policy stations.
makePath([
new THREE.Vector3(-0.2, 5.4, 0.25),
new THREE.Vector3(-2.6, 5.6, 0.75),
new THREE.Vector3(-4.0, 4.8, 0.65),
new THREE.Vector3(-5.05, 3.95, 0.18)
], COLORS.cyan);
makePath([
new THREE.Vector3(0.2, 5.35, 0.3),
new THREE.Vector3(1.8, 5.35, 0.9),
new THREE.Vector3(3.25, 3.0, 0.9),
new THREE.Vector3(6.45, 1.0, 0.18)
], COLORS.coral);
// Background particles.
const particleCount = 90;
const particlePositions = new Float32Array(particleCount * 3);
const particleBaseY = new Float32Array(particleCount);
for (let i = 0; i < particleCount; i++) {
const x = (Math.random() - 0.5) * 25;
const y = Math.random() * 11;
const z = -8 - Math.random() * 12;
particlePositions[i * 3] = x;
particlePositions[i * 3 + 1] = y;
particlePositions[i * 3 + 2] = z;
particleBaseY[i] = y;
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0x8ccfff,
size: 0.045,
transparent: true,
opacity: 0.5,
depthWrite: false
})
);
scene.add(particles);
// Simulation state. The two arrays are independent policy views of the
// same global arrival sequence. Each ID occurs at most once in either view.
let arrived = [];
let stackState = [];
let queueState = [];
let removedHistory = [];
let removedQueue = [];
let running = false;
let stageText = "Ready · add an arrival or run both policies";
const historyViews = new Map();
const queueViews = new Map();
function historyPosition(index) {
return new THREE.Vector3(-5.05, 0.8 + index * 0.52, 0.18);
}
function queuePosition(index) {
return new THREE.Vector3(2.05 + index * 0.73, 1.0, 0.18);
}
function historyOutputPosition(index) {
return new THREE.Vector3(-6.15 + index * 0.48, 0.73, 2.22);
}
function queueOutputPosition(index) {
return new THREE.Vector3(3.3 + index * 0.57, 0.73, 2.22);
}
function createToken(id, index) {
const group = new THREE.Group();
const color = TOKEN_COLORS[index % TOKEN_COLORS.length];
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.29, 0.29, 0.13, 32),
standardMaterial(color, {
roughness: 0.28,
metalness: 0.1,
emissive: color,
emissiveIntensity: 0.13,
transparent: true
})
);
body.rotation.x = Math.PI / 2;
body.castShadow = true;
group.add(body);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.31, 0.035, 10, 36),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.88
})
);
ring.position.z = 0.075;
group.add(ring);
group.add(makeTokenLabel(id, `#${color.toString(16).padStart(6, "0")}`));
group.userData.tokenId = id;
group.userData.released = false;
scene.add(group);
return group;
}
function moveTo(token, destination, duration = 360, lift = 0.2) {
const start = token.position.clone();
const end = destination.clone();
return new Promise(resolve => {
token.userData.animation = {
kind: "linear",
start,
end,
lift,
started: performance.now(),
duration: reducedMotion ? 40 : duration,
onComplete: resolve
};
});
}
function moveAlong(token, destination, duration, control1, control2, onComplete) {
const start = token.position.clone();
const end = destination.clone();
return new Promise(resolve => {
token.userData.animation = {
kind: "bezier",
start,
c1: control1.clone(),
c2: control2.clone(),
end,
started: performance.now(),
duration: reducedMotion ? 50 : duration,
onComplete: () => {
if (onComplete) onComplete();
resolve();
}
};
});
}
function setReleasedStyle(token) {
token.userData.released = true;
token.scale.setScalar(0.76);
token.traverse(object => {
if (!object.material) return;
const materials = Array.isArray(object.material)
? object.material
: [object.material];
for (const material of materials) {
material.transparent = true;
material.opacity = 0.68;
if (material.emissive) material.emissiveIntensity = 0.035;
}
});
}
function disposeToken(token) {
scene.remove(token);
token.traverse(object => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
for (const material of materials) {
if (material.map) material.map.dispose();
material.dispose();
}
}
});
}
function sameMembers(a, b) {
if (a.length !== b.length) return false;
const left = new Set(a);
const right = new Set(b);
return left.size === a.length && right.size === b.length &&
[...left].every(value => right.has(value));
}
function hasUniqueMembers(items) {
return new Set(items).size === items.length;
}
function integrityIsValid() {
const allArrived = arrived.slice().sort();
const expected = ARRIVALS.slice(0, arrived.length).sort();
const activeHistory = stackState.slice();
const activeQueue = queueState.slice();
const historyPartition = [...activeHistory, ...removedHistory];
const queuePartition = [...activeQueue, ...removedQueue];
return (
hasUniqueMembers(arrived) &&
hasUniqueMembers(activeHistory) &&
hasUniqueMembers(activeQueue) &&
hasUniqueMembers(removedHistory) &&
hasUniqueMembers(removedQueue) &&
sameMembers(arrived, expected) &&
sameMembers(historyPartition, allArrived) &&
sameMembers(queuePartition, allArrived)
);
}
function updateUI() {
const valid = integrityIsValid();
ui.arrivedCount.textContent = arrived.length;
ui.uniqueCount.textContent = new Set(arrived).size;
ui.integrityState.textContent = valid ? "BALANCED" : "CHECK";
ui.integrityState.classList.toggle("bad", !valid);
ui.modeState.textContent = running ? "AUTO" : "MANUAL";
ui.stackCount.textContent = stackState.length;
ui.queueCount.textContent = queueState.length;
ui.stackCurrent.innerHTML = stackState.length
? stackState.slice().reverse().join(" <span style='color:#55728a'>→</span> ")
: "<span class='empty'>empty</span>";
ui.queueCurrent.innerHTML = queueState.length
? queueState.join(" <span style='color:#55728a'>→</span> ")
: "<span class='empty'>empty</span>";
ui.historyOrder.innerHTML = removedHistory.length
? removedHistory.map(id => `<span class="released">${id}</span>`).join(" → ")
: "<span class='empty'>nothing released</span>";
ui.queueOrder.innerHTML = removedQueue.length
? removedQueue.map(id => `<span class="released">${id}</span>`).join(" → ")
: "<span class='empty'>nothing released</span>";
ui.timeline.innerHTML = ARRIVALS.map((id, index) => {
const hasArrived = arrived.includes(id);
const hLive = stackState.includes(id);
const qLive = queueState.includes(id);
const hOut = removedHistory.includes(id);
const qOut = removedQueue.includes(id);
return `
<div class="arrival-chip ${hasArrived ? "arrived" : ""}" title="${id}">
${id}
<span class="dots">
<i class="${hLive ? "h-live" : hOut ? "h-out" : ""}"></i>
<i class="${qLive ? "q-live" : qOut ? "q-out" : ""}"></i>
</span>
</div>
`;
}).join("");
ui.addBtn.disabled = running || arrived.length >= ARRIVALS.length;
ui.popBtn.disabled = running || stackState.length === 0;
ui.serveBtn.disabled = running || queueState.length === 0;
ui.compareBtn.disabled = running;
ui.resetBtn.disabled = running;
ui.compareBtn.textContent = running ? "Running…" : "▶ Run comparison";
ui.runStatus.textContent = stageText;
}
function layoutHistory(animate = true) {
stackState.forEach((id, index) => {
const token = historyViews.get(id);
if (!token) return;
if (animate) moveTo(token, historyPosition(index));
else token.position.copy(historyPosition(index));
});
}
function layoutQueue(animate = true) {
queueState.forEach((id, index) => {
const token = queueViews.get(id);
if (!token) return;
if (animate) moveTo(token, queuePosition(index));
else token.position.copy(queuePosition(index));
});
}
async function addArrival() {
if (arrived.length >= ARRIVALS.length) return;
const index = arrived.length;
const id = ARRIVALS[index];
arrived.push(id);
stackState.push(id);
queueState.push(id);
layoutHistory(true);
layoutQueue(true);
const historyToken = createToken(id, index);
const queueToken = createToken(id, index);
historyViews.set(id, historyToken);
queueViews.set(id, queueToken);
historyToken.position.copy(SOURCE);
queueToken.position.copy(SOURCE);
queueToken.position.x += 0.09;
updateUI();
const historyEnd = historyPosition(stackState.length - 1);
const queueEnd = queuePosition(queueState.length - 1);
await Promise.all([
moveAlong(
historyToken,
historyEnd,
720,
new THREE.Vector3(-2.55, 6.05, 0.85),
new THREE.Vector3(-4.25, 5.15, 0.5)
),
moveAlong(
queueToken,
queueEnd,
720,
new THREE.Vector3(1.9, 6.0, 0.95),
new THREE.Vector3(3.35, 3.1, 0.9)
)
]);
updateUI();
}
async function popHistory() {
if (stackState.length === 0) return;
const id = stackState.pop();
const token = historyViews.get(id);
removedHistory.push(id);
layoutHistory(true);
const end = historyOutputPosition(removedHistory.length - 1);
const start = token.position.clone();
await moveAlong(
token,
end,
560,
new THREE.Vector3(start.x - 0.6, start.y + 1.05, start.z + 0.85),
new THREE.Vector3(end.x, end.y + 0.95, end.z + 0.7),
() => setReleasedStyle(token)
);
updateUI();
}
async function serveQueue() {
if (queueState.length === 0) return;
const id = queueState.shift();
const token = queueViews.get(id);
removedQueue.push(id);
layoutQueue(true);
const end = queueOutputPosition(removedQueue.length - 1);
const start = token.position.clone();
await moveAlong(
token,
end,
560,
new THREE.Vector3(start.x - 0.7, start.y + 1.05, start.z + 0.85),
new THREE.Vector3(end.x, end.y + 0.95, end.z + 0.7),
() => setReleasedStyle(token)
);
updateUI();
}
function clearSimulation() {
for (const token of historyViews.values()) disposeToken(token);
for (const token of queueViews.values()) disposeToken(token);
historyViews.clear();
queueViews.clear();
arrived = [];
stackState = [];
queueState = [];
removedHistory = [];
removedQueue = [];
}
function resetSimulation() {
running = false;
clearSimulation();
stageText = "Ready · add an arrival or run both policies";
updateUI();
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function runComparison() {
if (running) return;
clearSimulation();
running = true;
stageText = "Phase 1 of 3 · feeding the shared arrival sequence";
updateUI();
for (const id of ARRIVALS) {
await addArrival();
await sleep(120);
}
stageText = "Phase 2 of 3 · undo history releases newest → oldest";
updateUI();
await sleep(350);
for (const id of ARRIVALS) {
await popHistory();
await sleep(190);
}
stageText = "Phase 3 of 3 · ticket queue releases oldest → newest";
updateUI();
await sleep(350);
for (const id of ARRIVALS) {
await serveQueue();
await sleep(190);
}
running = false;
stageText =
`Complete · LIFO ${removedHistory.join("→")} | FIFO ${removedQueue.join("→")}`;
updateUI();
}
ui.addBtn.addEventListener("click", async () => {
if (running) return;
stageText = "One arrival forked into two independent policy views";
await addArrival();
});
ui.popBtn.addEventListener("click", async () => {
if (running) return;
stageText = "Undo pop removed the newest live arrival";
await popHistory();
});
ui.serveBtn.addEventListener("click", async () => {
if (running) return;
stageText = "Queue service removed the oldest live arrival";
await serveQueue();
});
ui.compareBtn.addEventListener("click", runComparison);
ui.resetBtn.addEventListener("click", resetSimulation);
addEventListener("keydown", event => {
if (event.repeat || running) return;
if (["INPUT", "TEXTAREA", "SELECT"].includes(event.target.tagName)) return;
const key = event.key.toLowerCase();
if (key === "a") ui.addBtn.click();
if (key === "u") ui.popBtn.click();
if (key === "s") ui.serveBtn.click();
if (key === "r") ui.resetBtn.click();
});
// Custom camera controls; no examples/addons modules are used.
const cameraState = {
radius: 15.8,
theta: 0.27,
phi: 1.03,
target: new THREE.Vector3(0, 2.5, 0)
};
function updateCamera() {
const aspectCompensation = camera.aspect < 1 ? 1.28 : 1;
const radius = cameraState.radius * aspectCompensation;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + radius * Math.cos(cameraState.phi),
cameraState.target.z + radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function recenterCamera() {
cameraState.radius = 15.8;
cameraState.theta = 0.27;
cameraState.phi = 1.03;
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.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.55,
1.42
);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
10.5,
23
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", recenterCamera);
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
updateCamera();
}
addEventListener("resize", resize);
function updateAnimations(now) {
for (const token of [...historyViews.values(), ...queueViews.values()]) {
const animation = token.userData.animation;
if (!animation) continue;
const t = THREE.MathUtils.clamp(
(now - animation.started) / animation.duration,
0,
1
);
if (animation.kind === "linear") {
token.position.lerpVectors(animation.start, animation.end, t);
token.position.y += Math.sin(Math.PI * t) * animation.lift;
} else {
const u = 1 - t;
const a = animation.start;
const b = animation.c1;
const c = animation.c2;
const d = animation.end;
token.position.set(
u * u * u * a.x + 3 * u * u * t * b.x + 3 * u * t * t * c.x + t * t * t * d.x,
u * u * u * a.y + 3 * u * u * t * b.y + 3 * u * t * t * c.y + t * t * t * d.y,
u * u * u * a.z + 3 * u * u * t * b.z + 3 * u * t * t * c.z + t * t * t * d.z
);
}
if (t >= 1) {
token.position.copy(animation.end);
token.userData.animation = null;
animation.onComplete?.();
}
}
}
function animate(now) {
requestAnimationFrame(animate);
updateAnimations(now);
const positionAttribute = particleGeometry.attributes.position;
for (let i = 0; i < particleCount; i++) {
positionAttribute.array[i * 3 + 1] =
particleBaseY[i] + Math.sin(now * 0.00035 + i * 0.73) * 0.22;
}
positionAttribute.needsUpdate = true;
portal.rotation.z = now * 0.00018;
portalInner.scale.setScalar(1 + Math.sin(now * 0.0022) * 0.025);
renderer.render(scene, camera);
}
updateCamera();
updateUI();
resize();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
loading.classList.add("ready");
setTimeout(() => loading.remove(), 450);
});
</script>
</body>
</html>
```