Prompt specification
Build or customize the Three.js 3D scene "Train-yard and cafeteria data-structure gallery" 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: Train-yard and cafeteria data-structure gallery
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/train-yard-and-cafeteria-data-structure-gallery-x1911
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: train-yard and cafeteria data-structure gallery. Compose a siding-like stack analogy beside a service-line queue; clearly mark where the physical analogies simplify actual movement and software operations. Return one self-contained HTML file.
Working Three.js Starter (train-yard-and-cafeteria-data-structure-gallery-x1911):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Train Yard × Cafeteria — Data Structure Gallery</title>
<style>
:root {
--ink: #26352e;
--muted: #65736b;
--paper: #f7f3e7;
--paper-2: #ebe8dc;
--line: #26352e;
--yellow: #e9c46a;
--teal: #2a9d8f;
--coral: #e76f51;
--blue: #4d7c9e;
--shadow: 5px 5px 0 rgba(38, 53, 46, 0.2);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8ede4;
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
button {
font: inherit;
}
#app,
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 20;
top: 18px;
left: 20px;
right: 20px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.brand {
max-width: 620px;
padding: 14px 17px 13px;
border: 2px solid var(--line);
background: rgba(247, 243, 231, 0.94);
box-shadow: var(--shadow);
}
.eyebrow {
margin-bottom: 5px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.2vw, 34px);
line-height: 1;
letter-spacing: -0.055em;
text-transform: uppercase;
}
.subtitle {
margin-top: 7px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.top-chip {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 9px 12px;
border: 2px solid var(--line);
background: var(--yellow);
box-shadow: 4px 4px 0 rgba(38, 53, 46, 0.18);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.08em;
white-space: nowrap;
}
.top-chip .dot {
width: 9px;
height: 9px;
background: var(--teal);
border: 1px solid var(--line);
}
.card {
border: 2px solid var(--line);
background: rgba(247, 243, 231, 0.95);
box-shadow: var(--shadow);
}
.story {
position: fixed;
z-index: 15;
top: 110px;
left: 20px;
width: 275px;
padding: 14px;
pointer-events: none;
}
.card-label {
margin-bottom: 8px;
color: var(--teal);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.story p {
margin: 0;
font-size: 12px;
line-height: 1.55;
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 12px;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
padding: 7px;
border: 1px solid #aeb7ad;
background: #f1eee2;
font-size: 10px;
font-weight: 800;
}
.swatch {
width: 13px;
height: 13px;
flex: 0 0 auto;
border: 1px solid var(--line);
}
.swatch.stack {
background: var(--coral);
}
.swatch.queue {
background: var(--teal);
}
.limits {
position: fixed;
z-index: 16;
top: 110px;
right: 20px;
width: 326px;
padding: 14px;
}
.limits .card-label {
color: #a64b2f;
}
.limits h2 {
margin: 0 0 7px;
font-size: 16px;
line-height: 1.2;
letter-spacing: -0.04em;
}
.limits p,
.limits li {
font-size: 11px;
line-height: 1.48;
}
.limits p {
margin: 0 0 9px;
}
.limits ul {
margin: 8px 0 10px;
padding: 0;
list-style: none;
}
.limits li {
position: relative;
margin: 7px 0;
padding-left: 13px;
}
.limits li::before {
content: "";
position: absolute;
top: 0.47em;
left: 0;
width: 6px;
height: 6px;
background: var(--coral);
border: 1px solid var(--line);
}
.limits strong {
color: #9d3f27;
}
.limits .warning {
margin: 0;
padding: 9px;
border: 1px solid var(--line);
background: var(--yellow);
font-size: 10px;
font-weight: 900;
line-height: 1.4;
}
.state {
position: fixed;
z-index: 16;
bottom: 20px;
left: 20px;
width: 310px;
padding: 12px 13px;
}
.state-row {
display: grid;
grid-template-columns: 68px 1fr;
gap: 9px;
align-items: baseline;
margin: 4px 0;
font-size: 11px;
}
.state-row span:first-child {
color: var(--muted);
font-weight: 900;
font-size: 9px;
letter-spacing: 0.1em;
}
.state-value {
overflow: hidden;
font-weight: 900;
text-overflow: ellipsis;
white-space: nowrap;
}
.operation {
min-height: 27px;
margin-top: 9px;
padding-top: 8px;
border-top: 1px dashed #9da79f;
color: #405148;
font-size: 10px;
line-height: 1.4;
}
.dock {
position: fixed;
z-index: 18;
bottom: 20px;
left: 50%;
display: flex;
align-items: stretch;
transform: translateX(-50%);
padding: 7px;
gap: 7px;
}
.control-group {
display: flex;
align-items: center;
gap: 4px;
padding: 4px;
border-right: 1px solid #aeb6ad;
}
.control-group:last-of-type {
border-right: 0;
}
.control-label {
padding: 0 7px 0 5px;
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.1em;
writing-mode: vertical-rl;
transform: rotate(180deg);
}
.control-button {
min-width: 82px;
min-height: 44px;
padding: 7px 10px;
border: 2px solid var(--line);
border-radius: 0;
background: #fffdf4;
color: var(--ink);
box-shadow: 2px 2px 0 rgba(38, 53, 46, 0.17);
cursor: pointer;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.04em;
text-transform: uppercase;
transition: transform 100ms ease, box-shadow 100ms ease,
background 100ms ease;
}
.control-button:hover:not(:disabled) {
transform: translate(-1px, -1px);
box-shadow: 3px 3px 0 rgba(38, 53, 46, 0.2);
background: #fff;
}
.control-button:active:not(:disabled) {
transform: translate(1px, 1px);
box-shadow: 1px 1px 0 rgba(38, 53, 46, 0.2);
}
.control-button:focus-visible {
outline: 3px solid var(--teal);
outline-offset: 2px;
}
.control-button:disabled {
cursor: not-allowed;
opacity: 0.38;
}
.control-button.stack-action {
background: #f9d9cd;
}
.control-button.queue-action {
background: #d2e8df;
}
.control-button.auto.active {
background: var(--yellow);
}
.view-tools {
position: fixed;
z-index: 17;
right: 20px;
bottom: 20px;
display: grid;
grid-template-columns: repeat(3, 38px);
border: 2px solid var(--line);
background: rgba(247, 243, 231, 0.95);
box-shadow: 3px 3px 0 rgba(38, 53, 46, 0.18);
}
.view-tools button {
height: 38px;
border: 0;
border-right: 1px solid #aeb6ad;
border-radius: 0;
background: transparent;
color: var(--ink);
cursor: pointer;
font-size: 16px;
font-weight: 900;
}
.view-tools button:last-child {
border-right: 0;
font-size: 9px;
}
.view-tools button:hover {
background: var(--yellow);
}
.gesture-hint {
position: fixed;
z-index: 10;
right: 20px;
bottom: 68px;
padding: 6px 8px;
border: 1px solid rgba(38, 53, 46, 0.55);
background: rgba(247, 243, 231, 0.82);
color: #526159;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
pointer-events: none;
}
.annotation-layer {
position: fixed;
inset: 0;
z-index: 8;
pointer-events: none;
}
.pin {
position: absolute;
min-width: 150px;
padding: 8px 10px;
border: 2px solid var(--line);
background: rgba(255, 253, 244, 0.94);
box-shadow: 3px 3px 0 rgba(38, 53, 46, 0.2);
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
}
.pin::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 2px;
height: 17px;
background: var(--line);
}
.pin b {
display: block;
font-size: 10px;
letter-spacing: 0.05em;
}
.pin span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
#pin-stack {
border-top: 5px solid var(--coral);
}
#pin-queue {
border-top: 5px solid var(--teal);
}
#pin-limits {
border-top: 5px solid var(--yellow);
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #e8ede4;
color: var(--ink);
font-size: 12px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 250ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 1120px) {
.story {
display: none;
}
.state {
bottom: 96px;
}
.gesture-hint {
display: none;
}
}
@media (max-width: 820px) {
.topbar {
top: 10px;
left: 10px;
right: 10px;
}
.brand {
max-width: calc(100vw - 130px);
padding: 10px 12px;
}
.subtitle {
display: none;
}
.top-chip {
padding: 8px;
font-size: 9px;
}
.limits {
top: 83px;
right: 10px;
width: 245px;
padding: 10px;
}
.limits h2 {
font-size: 13px;
}
.limits p,
.limits li {
font-size: 9px;
}
.limits .warning {
font-size: 8px;
}
.state {
left: 10px;
bottom: 100px;
width: 245px;
}
.dock {
right: 10px;
bottom: 10px;
left: 10px;
justify-content: center;
transform: none;
overflow-x: auto;
}
.control-button {
min-width: 66px;
padding-inline: 7px;
font-size: 9px;
}
.control-label {
display: none;
}
.view-tools {
top: 83px;
right: auto;
bottom: auto;
left: 10px;
}
.annotation-layer {
display: none;
}
}
@media (max-width: 520px) {
.limits {
top: auto;
right: 10px;
bottom: 153px;
width: 205px;
}
.limits ul,
.limits p {
display: none;
}
.limits h2 {
margin-bottom: 4px;
}
.state {
bottom: 153px;
width: calc(100vw - 235px);
min-width: 125px;
}
.state-row {
display: block;
}
.state-row span:first-child {
display: block;
margin-bottom: 2px;
}
.state-value {
display: block;
font-size: 9px;
}
.operation {
display: none;
}
.control-group {
padding: 2px;
}
.control-button {
min-width: 58px;
min-height: 40px;
}
}
</style>
</head>
<body>
<div id="app">
<div id="scene"></div>
<header class="topbar">
<div class="brand">
<div class="eyebrow">Interactive data structures · voxel edition</div>
<h1>Train Yard × Cafeteria</h1>
<div class="subtitle">
A side-by-side gallery of stack and queue ordering.
</div>
</div>
<div class="top-chip">
<span class="dot"></span>
ORDERING ≠ IMPLEMENTATION
</div>
</header>
<section class="story card" aria-label="How to read the gallery">
<div class="card-label">How to read the gallery</div>
<p>
One freight siding reveals <strong>LIFO</strong>: the most recently
stacked item leaves first. One lunch line reveals <strong>FIFO</strong>:
the first arrival is served first.
</p>
<div class="legend">
<div class="legend-item">
<span class="swatch stack"></span>
STACK / PUSH–POP
</div>
<div class="legend-item">
<span class="swatch queue"></span>
QUEUE / ENQUEUE–DEQUEUE
</div>
</div>
</section>
<aside class="limits card" aria-label="Limits of the physical analogies">
<div class="card-label">Model boundary</div>
<h2>ANALOGY ≠ IMPLEMENTATION</h2>
<p>
The voxel scene makes <strong>ordering</strong> visible. It does not
model memory, allocation, concurrency, or failure behavior.
</p>
<ul>
<li>
<strong>Siding:</strong> one visible wagon is not literally one memory
slot. Stack storage may resize, reallocate, or contain references.
</li>
<li>
<strong>Cafeteria:</strong> people physically walk forward. A software
dequeue often removes or advances a logical index instead.
</li>
<li>
<strong>Animation:</strong> time is paused and simplified. Real stacks
and queues can grow, block, fail, and be accessed concurrently.
</li>
</ul>
<p class="warning">
USE THIS AS A MENTAL MODEL — NOT A LITERAL MACHINE.
</p>
</aside>
<section class="state card" aria-label="Current data structure state">
<div class="card-label">Live structure state</div>
<div class="state-row">
<span>STACK ↓</span>
<span class="state-value" id="stack-state">C → B → A</span>
</div>
<div class="state-row">
<span>QUEUE →</span>
<span class="state-value" id="queue-state">Mara → Theo → Inez</span>
</div>
<div class="operation" id="operation" aria-live="polite">
Ready. Push, pop, enqueue, or serve an item.
</div>
</section>
<nav class="dock card" aria-label="Data structure controls">
<div class="control-group">
<span class="control-label">STACK</span>
<button class="control-button stack-action" id="push">
Push
</button>
<button class="control-button stack-action" id="pop">
Pop
</button>
</div>
<div class="control-group">
<span class="control-label">QUEUE</span>
<button class="control-button queue-action" id="enqueue">
Enqueue
</button>
<button class="control-button queue-action" id="serve">
Serve
</button>
</div>
<div class="control-group">
<button class="control-button auto" id="auto">Play tour</button>
<button class="control-button" id="reset">Reset</button>
</div>
</nav>
<div class="view-tools" aria-label="Camera controls">
<button id="zoom-out" title="Zoom out" aria-label="Zoom out">−</button>
<button id="zoom-in" title="Zoom in" aria-label="Zoom in">+</button>
<button id="reset-view" title="Reset camera" aria-label="Reset camera">
VIEW
</button>
</div>
<div class="gesture-hint">
DRAG TO ORBIT · SCROLL TO ZOOM · ARROW KEYS ROTATE · R RESETS VIEW
</div>
<div class="annotation-layer" aria-hidden="true">
<div class="pin" id="pin-stack">
<b>STACK / SIDING YARD</b>
<span>LIFO · last pushed leaves first</span>
</div>
<div class="pin" id="pin-queue">
<b>QUEUE / SERVICE LINE</b>
<span>FIFO · first joined leaves first</span>
</div>
<div class="pin" id="pin-limits">
<b>ANALOGY LIMITS</b>
<span>ordering shown · memory hidden</span>
</div>
</div>
<div id="loading">Building the gallery…</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8ede4);
scene.fog = new THREE.Fog(0xe8ede4, 88, 145);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance",
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel gallery comparing a stack and a queue"
);
renderer.domElement.tabIndex = 0;
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-35, 35, 22, -22, 0.1, 250);
const cameraTarget = new THREE.Vector3(0, 2, 0);
let cameraTheta = 0.64;
let cameraPhi = 0.82;
let cameraRadius = 88;
const hemisphere = new THREE.HemisphereLight(0xfff8df, 0x718174, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.1);
sun.position.set(-22, 42, 28);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -42;
sun.shadow.camera.right = 42;
sun.shadow.camera.top = 35;
sun.shadow.camera.bottom = -35;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d8e7, 0.75);
fill.position.set(30, 18, -22);
scene.add(fill);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0.02,
})
);
}
return materialCache.get(color);
}
function box(
parent,
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
color && color.isMaterial ? color : material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
if (options.rx !== undefined) mesh.rotation.x = options.rx;
if (options.ry !== undefined) mesh.rotation.y = options.ry;
if (options.rz !== undefined) mesh.rotation.z = options.rz;
if (options.castShadow === false) mesh.castShadow = false;
(parent || scene).add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Canvas-generated sign textures
// -------------------------------------------------------------------------
function signTexture(title, subtitle = "", bg = "#f7f3e7", fg = "#26352e") {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 512;
const ctx = canvas.getContext("2d");
ctx.fillStyle = bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = fg;
ctx.fillRect(0, 0, canvas.width, 26);
ctx.fillRect(0, canvas.height - 26, canvas.width, 26);
ctx.strokeStyle = fg;
ctx.lineWidth = 12;
ctx.strokeRect(16, 16, canvas.width - 32, canvas.height - 32);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = fg;
const titleSize = title.length > 20 ? 54 : title.length > 15 ? 64 : 76;
ctx.font = `900 ${titleSize}px monospace`;
ctx.fillText(title, canvas.width / 2, subtitle ? 205 : 256, 930);
if (subtitle) {
ctx.font = "700 31px monospace";
ctx.fillText(subtitle, canvas.width / 2, 340, 930);
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.LinearMipmapLinearFilter;
return texture;
}
function badgeTexture(text, bg = "#f7f3e7", fg = "#26352e") {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = bg;
ctx.fillRect(0, 0, 256, 128);
ctx.strokeStyle = fg;
ctx.lineWidth = 10;
ctx.strokeRect(5, 5, 246, 118);
ctx.fillStyle = fg;
ctx.font = "900 68px monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 128, 67);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
return texture;
}
function uprightSign(
title,
subtitle,
x,
y,
z,
width,
height,
bg = "#f7f3e7",
fg = "#26352e"
) {
const group = new THREE.Group();
group.position.set(x, y, z);
scene.add(group);
box(group, width, height, 0.48, 0x526159, 0, 0, 0);
box(
group,
width - 0.22,
height - 0.22,
0.08,
0xf7f3e7,
0,
0,
0.27
);
const texture = signTexture(title, subtitle, bg, fg);
const face = new THREE.Mesh(
new THREE.PlaneGeometry(width - 0.42, height - 0.42),
new THREE.MeshBasicMaterial({ map: texture })
);
face.position.z = 0.32;
group.add(face);
const postHeight = y + height / 2;
const postY = -y + postHeight / 2;
box(group, 0.42, postHeight, 0.42, 0x526159, -width * 0.37, postY, 0);
box(group, 0.42, postHeight, 0.42, 0x526159, width * 0.37, postY, 0);
return group;
}
function floorLabel(
lines,
x,
z,
width,
depth,
bg = "#f7f3e7",
fg = "#26352e",
rotation = 0
) {
const texture = signTexture(
lines[0],
lines[1] || "",
bg,
fg
);
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, depth),
new THREE.MeshBasicMaterial({
map: texture,
side: THREE.DoubleSide,
})
);
mesh.rotation.set(-Math.PI / 2, 0, rotation);
mesh.position.set(x, 0.19, z);
mesh.receiveShadow = false;
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Gallery base, paths, grid, and landscaping
// -------------------------------------------------------------------------
box(scene, 62, 1.15, 38, 0xaebcaf, 0, -0.76, 0);
box(scene, 60, 0.28, 36, 0xe7eadf, 0, -0.05, 0);
for (let x = -28; x <= 28; x += 4) {
box(scene, 0.035, 0.018, 35.5, 0xc6cec3, x, 0.105, 0, {
castShadow: false,
});
}
for (let z = -16; z <= 16; z += 4) {
box(scene, 59.5, 0.018, 0.035, 0xc6cec3, 0, 0.105, z, {
castShadow: false,
});
}
box(scene, 19, 0.11, 28.5, 0xcfc7b7, -13.4, 0.12, 0.2);
box(scene, 19, 0.11, 28.5, 0xdce2d7, 13.4, 0.12, 0.2);
box(scene, 8.2, 0.13, 30, 0xeadba7, 0, 0.13, 0);
for (let z = -13; z <= 13; z += 3.2) {
box(scene, 1.6, 0.045, 1.35, 0xe9c46a, 0, 0.23, z);
}
// Raised perimeter blocks emphasize the diorama's voxel construction.
for (let x = -30; x <= 30; x += 2) {
box(scene, 1.65, 0.38, 0.55, 0x81958a, x, 0.12, -18.55);
box(scene, 1.65, 0.38, 0.55, 0x81958a, x, 0.12, 18.55);
}
function voxelTree(x, z, crown = 0x5d8d66) {
box(scene, 0.48, 1.35, 0.48, 0x76543d, x, 0.82, z);
box(scene, 1.65, 1.1, 1.65, crown, x, 1.75, z);
box(scene, 1.05, 0.8, 1.05, 0x73a077, x + 0.35, 2.5, z - 0.15);
box(scene, 0.72, 0.65, 0.72, 0x4f795a, x - 0.5, 1.65, z + 0.35);
}
voxelTree(-27.3, -14.7);
voxelTree(-26.6, 14.8, 0x6f9a6c);
voxelTree(27.2, -14.7, 0x4f8061);
voxelTree(26.5, 14.8);
function lamp(x, z) {
box(scene, 0.28, 4.2, 0.28, 0x526159, x, 2.25, z);
box(scene, 1.3, 0.25, 0.3, 0x526159, x + 0.48, 4.25, z);
box(
scene,
0.72,
0.28,
0.72,
new THREE.MeshStandardMaterial({
color: 0xffe7a0,
emissive: 0xffcc66,
emissiveIntensity: 0.4,
}),
x + 0.92,
4.05,
z
);
}
lamp(-24.3, -10.5);
lamp(24.1, -10.5);
lamp(-24.2, 11.5);
lamp(24.1, 11.5);
// -------------------------------------------------------------------------
// Left: train-yard siding stack
// -------------------------------------------------------------------------
function buildTrack(centerX) {
const railZ = 0.4;
const railLength = 23.3;
box(scene, 0.18, 0.18, railLength, 0x52605a, centerX - 0.72, 0.4, railZ);
box(scene, 0.18, 0.18, railLength, 0x52605a, centerX + 0.72, 0.4, railZ);
for (let z = -10.8; z <= 11.2; z += 1.35) {
box(scene, 2.9, 0.17, 0.38, 0x725744, centerX, 0.3, z);
}
box(scene, 2.9, 0.28, 0.35, 0xb85f45, centerX, 0.75, 11.75);
box(scene, 0.25, 1.2, 0.25, 0xb85f45, centerX - 0.95, 0.92, 11.3);
box(scene, 0.25, 1.2, 0.25, 0xb85f45, centerX + 0.95, 0.92, 11.3);
}
buildTrack(-18.2);
buildTrack(-14.1);
buildTrack(-9.4);
// Stack pad and a small data-yard control box.
box(scene, 5.8, 0.16, 4.8, 0xe9c46a, -9.4, 0.18, 0.5);
box(scene, 5.35, 0.05, 0.13, 0x26352e, -9.4, 0.29, 2.8);
box(scene, 5.35, 0.05, 0.13, 0x26352e, -9.4, 0.29, -1.8);
box(scene, 2.4, 1.6, 1.8, 0x6f8278, -20.4, 1.02, -4.9);
box(scene, 1.65, 0.78, 0.1, 0x283b33, -20.4, 1.28, -3.96);
box(scene, 0.48, 0.48, 0.12, 0xe76f51, -19.95, 1.28, -3.88);
box(scene, 0.22, 4.6, 0.22, 0x526159, -20.4, 2.45, -5.15);
box(scene, 1.05, 0.7, 0.18, 0x26352e, -19.95, 4.2, -5.05);
box(scene, 0.32, 0.32, 0.1, 0xe9c46a, -19.7, 4.2, -4.92);
uprightSign(
"STACK / SIDING",
"PUSH AT THE END · POP FROM THE END",
-11.8,
4.5,
-13.8,
11.2,
3.5,
"#f4d2c8",
"#26352e"
);
floorLabel(
["LIFO", "LAST IN · FIRST OUT"],
-15.8,
7.6,
8.2,
2.6,
"#f7f3e7",
"#a34d34"
);
// Track direction chevrons.
for (let z = 4; z <= 10; z += 2) {
box(scene, 0.32, 0.06, 1.5, 0xffffff, -9.4, 0.54, z, {
castShadow: false,
});
box(scene, 0.32, 0.06, 1.05, 0xffffff, -9.72, 0.55, z + 0.48, {
rz: -0.62,
castShadow: false,
});
box(scene, 0.32, 0.06, 1.05, 0xffffff, -9.08, 0.55, z + 0.48, {
rz: 0.62,
castShadow: false,
});
}
// -------------------------------------------------------------------------
// Right: cafeteria service line
// -------------------------------------------------------------------------
box(scene, 11.3, 1.12, 2.25, 0x789387, 13, 0.75, -7.55);
box(scene, 11.7, 0.3, 2.7, 0xe8dfc9, 13, 1.46, -7.55);
box(scene, 11.1, 0.18, 0.12, 0xe9c46a, 13, 1.33, -6.38);
for (let x = 8.1; x <= 17.9; x += 2.45) {
box(scene, 0.13, 0.72, 0.09, 0xdfe9dd, x, 0.78, -6.38);
}
// Serving window frame.
box(scene, 0.35, 4.4, 0.35, 0x526159, 7.7, 2.35, -8.25);
box(scene, 0.35, 4.4, 0.35, 0x526159, 18.3, 2.35, -8.25);
box(scene, 11, 0.42, 0.5, 0x526159, 13, 4.45, -8.25);
uprightSign(
"PICKUP",
"THE HEAD OF THE LINE IS SERVED",
13,
5.55,
-8.2,
7.4,
2.15,
"#d3e9df",
"#26352e"
);
// Trays and a service bell.
box(scene, 2.0, 0.18, 1.15, 0xd7a06b, 9.7, 1.72, -7.35);
box(scene, 0.8, 0.5, 0.72, 0xe9c46a, 16.4, 1.86, -7.35);
box(scene, 0.28, 0.18, 0.28, 0xe76f51, 16.4, 2.18, -7.35);
uprightSign(
"QUEUE / CAFETERIA",
"JOIN AT THE TAIL · SERVE AT THE HEAD",
13,
4.55,
-13.85,
12.2,
3.55,
"#d3e9df",
"#26352e"
);
floorLabel(
["FIFO", "FIRST IN · FIRST OUT"],
18.2,
8.2,
8.2,
2.6,
"#f7f3e7",
"#176e65"
);
floorLabel(
["HEAD"],
10.2,
-1.7,
2.7,
1.05,
"#d3e9df",
"#26352e",
Math.PI / 2
);
floorLabel(
["TAIL"],
10.2,
10.2,
2.7,
1.05,
"#e9c46a",
"#26352e",
Math.PI / 2
);
// Five queue positions. The first is highlighted as the service head.
for (let i = 0; i < 5; i++) {
const z = -2.2 + i * 4.1;
const color = i === 0 ? 0x2a9d8f : 0xa7b1a7;
box(scene, 0.48, 0.05, 0.16, color, 11.65, 0.23, z - 0.72);
box(scene, 0.48, 0.05, 0.16, color, 11.65, 0.23, z + 0.72);
box(scene, 0.16, 0.05, 0.48, color, 11.35, 0.23, z);
box(scene, 0.16, 0.05, 0.48, color, 11.95, 0.23, z);
}
// -------------------------------------------------------------------------
// Central gallery and explicit analogy-limit marker
// -------------------------------------------------------------------------
uprightSign(
"DATA STRUCTURE GALLERY",
"TWO ORDERING MODELS · ONE SHARED IDEA",
0,
7.0,
-16.3,
15.3,
3.35,
"#e9c46a",
"#26352e"
);
uprightSign(
"ANALOGY LIMITS",
"SHOWS ORDERING · HIDES MEMORY & RUNTIME",
0,
3.1,
-9.4,
9.5,
2.9,
"#f7f3e7",
"#8e3d28"
);
// Caution blocks beneath the limits sign.
for (let x = -3.8; x <= 3.8; x += 0.95) {
const stripe = Math.round((x + 3.8) / 0.95) % 2 === 0;
box(
scene,
0.72,
0.28,
0.58,
stripe ? 0xe9c46a : 0x26352e,
x,
0.31,
-7.88
);
}
floorLabel(
["ORDERING, NOT IMPLEMENTATION"],
0,
7.2,
9.3,
2.1,
"#f7f3e7",
"#8e3d28"
);
// -------------------------------------------------------------------------
// Dynamic stack and queue objects
// -------------------------------------------------------------------------
const dynamicRoot = new THREE.Group();
scene.add(dynamicRoot);
const stackPalette = [
0xe76f51,
0x4d7c9e,
0x2a9d8f,
0xe9c46a,
0x7b6d9b,
0x8a9b5b,
];
const queuePalette = [
0x4d7c9e,
0xe76f51,
0x2a9d8f,
0xe9c46a,
0x7b6d9b,
];
const skinPalette = [0xe4b98b, 0xb97c57, 0x8f5d42, 0xf0c8a3, 0x6f4938];
const hairPalette = [0x2f2926, 0x513b2f, 0xb47b42, 0x252525, 0x7b4b32];
function createStackVisual(label, colorIndex) {
const group = new THREE.Group();
dynamicRoot.add(group);
const color = stackPalette[colorIndex % stackPalette.length];
box(group, 4.15, 1.16, 2.45, color, 0, 0.68, 0);
box(group, 3.78, 0.12, 2.12, 0xf7f3e7, 0, 1.31, 0);
box(group, 0.18, 1.22, 2.5, 0x3a4a42, -1.68, 0.68, 0);
box(group, 0.18, 1.22, 2.5, 0x3a4a42, 1.68, 0.68, 0);
for (let x = -1.1; x <= 1.1; x += 0.55) {
box(group, 0.22, 0.05, 0.18, 0x26352e, x, 1.4, 0.38, {
castShadow: false,
});
}
const badge = new THREE.Mesh(
new THREE.PlaneGeometry(1.15, 0.58),
new THREE.MeshBasicMaterial({
map: badgeTexture(label, "#f7f3e7", "#26352e"),
})
);
badge.position.set(0, 0.7, 1.235);
group.add(badge);
const beacon = box(
group,
0.36,
0.2,
0.36,
new THREE.MeshStandardMaterial({
color: 0xffe08a,
emissive: 0xffb52e,
emissiveIntensity: 0.35,
}),
1.45,
1.47,
0
);
return { group, beacon, label };
}
function createPerson(colorIndex, personIndex) {
const group = new THREE.Group();
dynamicRoot.add(group);
const shirt = queuePalette[colorIndex % queuePalette.length];
const skin = skinPalette[personIndex % skinPalette.length];
const hair = hairPalette[personIndex % hairPalette.length];
box(group, 0.34, 0.2, 0.56, 0x29332f, -0.24, 0.12, 0.08);
box(group, 0.34, 0.2, 0.56, 0x29332f, 0.24, 0.12, 0.08);
box(group, 0.3, 0.86, 0.38, 0x465d69, -0.23, 0.57, 0);
box(group, 0.3, 0.86, 0.38, 0x465d69, 0.23, 0.57, 0);
box(group, 0.88, 1.02, 0.56, shirt, 0, 1.39, 0);
box(group, 0.26, 0.82, 0.34, shirt, -0.56, 1.37, 0);
box(group, 0.26, 0.82, 0.34, shirt, 0.56, 1.37, 0);
box(group, 0.64, 0.64, 0.62, skin, 0, 2.22, 0);
box(group, 0.68, 0.22, 0.66, hair, 0, 2.53, 0);
box(group, 0.18, 0.16, 0.06, 0x26352e, -0.16, 2.25, 0.33);
box(group, 0.18, 0.16, 0.06, 0x26352e, 0.16, 2.25, 0.33);
const badge = box(group, 0.48, 0.34, 0.08, 0xf7f3e7, 0, 1.47, 0.34);
badge.castShadow = false;
return { group, label: "" };
}
let stackState = [];
let queueState = [];
let stackVisuals = [];
let queueVisuals = [];
let stackSequence = 4;
let queueSequence = 3;
let busy = false;
const queueNames = [
"Mara",
"Theo",
"Inez",
"Jules",
"Ada",
"Rin",
"Sol",
"Noor",
"Kit",
];
const stackTarget = (index) =>
new THREE.Vector3(-9.4, 0.28 + index * 1.48, 0.5);
const queueTarget = (index) =>
new THREE.Vector3(13, 0.2, -2.2 + index * 4.1);
function updateTopBeacons() {
stackVisuals.forEach((visual, index) => {
visual.beacon.visible = index === stackVisuals.length - 1;
});
}
function updateStateDisplay(message) {
const stackText = [...stackState].reverse().join(" → ") || "∅";
const queueText = queueState.join(" → ") || "∅";
document.getElementById("stack-state").textContent = stackText;
document.getElementById("queue-state").textContent = queueText;
if (message) {
document.getElementById("operation").textContent = message;
}
document.getElementById("push").disabled =
busy || stackState.length >= 6;
document.getElementById("pop").disabled = busy || stackState.length === 0;
document.getElementById("enqueue").disabled =
busy || queueState.length >= 5;
document.getElementById("serve").disabled =
busy || queueState.length === 0;
}
function announce(message) {
document.getElementById("operation").textContent = message;
}
function rebuildDynamic() {
dynamicRoot.traverse((object) => {
if (object.geometry) object.geometry.dispose();
});
dynamicRoot.clear();
stackVisuals = [];
queueVisuals = [];
stackState.forEach((label, index) => {
const visual = createStackVisual(label, index + stackSequence);
visual.group.position.copy(stackTarget(index));
stackVisuals.push(visual);
});
queueState.forEach((name, index) => {
const visual = createPerson(index + queueSequence, index + queueSequence);
visual.label = name;
visual.group.position.copy(queueTarget(index));
queueVisuals.push(visual);
});
updateTopBeacons();
updateStateDisplay(
"Reset. The model shows ordering only; storage and runtime remain hidden."
);
}
// -------------------------------------------------------------------------
// Tiny tween engine
// -------------------------------------------------------------------------
const tweens = [];
function easeInOutCubic(value) {
return value < 0.5
? 4 * value * value * value
: 1 - Math.pow(-2 * value + 2, 3) / 2;
}
function tween(duration, update, complete = null, delay = 0) {
tweens.push({
start: performance.now() + delay,
duration,
update,
complete,
});
}
function updateTweens(now) {
for (let i = tweens.length - 1; i >= 0; i--) {
const item = tweens[i];
if (now < item.start) continue;
const raw = Math.min(1, (now - item.start) / item.duration);
const progress = easeInOutCubic(raw);
item.update(progress);
if (raw >= 1) {
tweens.splice(i, 1);
if (item.complete) item.complete();
}
}
}
// -------------------------------------------------------------------------
// Data operations and animations
// -------------------------------------------------------------------------
function pushData() {
if (busy) return;
if (stackState.length >= 6) {
announce("Push rejected: this gallery stack is limited to six visible items.");
return;
}
busy = true;
const label = String.fromCharCode(65 + (stackSequence % 26));
stackSequence += 1;
const visual = createStackVisual(label, stackSequence);
const index = stackState.length;
stackState.push(label);
stackVisuals.push(visual);
updateTopBeacons();
const start = new THREE.Vector3(-15.8, 3.0, 8.7);
const end = stackTarget(index);
visual.group.position.copy(start);
visual.group.scale.setScalar(0.7);
announce(
`PUSH ${label}: the new item enters the siding and becomes the next pop candidate.`
);
updateStateDisplay();
tween(
900,
(progress) => {
visual.group.position.lerpVectors(start, end, progress);
visual.group.position.y += Math.sin(progress * Math.PI) * 1.8;
visual.group.scale.setScalar(0.7 + progress * 0.3);
},
() => {
visual.group.position.copy(end);
visual.group.scale.setScalar(1);
busy = false;
updateStateDisplay();
}
);
}
function popData() {
if (busy) return;
if (stackState.length === 0) {
announce("Pop rejected: the stack is empty.");
return;
}
busy = true;
const visual = stackVisuals.pop();
const label = stackState.pop();
updateTopBeacons();
const start = visual.group.position.clone();
announce(
`POP ${label}: the newest visible item leaves the same end of the siding.`
);
updateStateDisplay();
tween(
950,
(progress) => {
if (progress < 0.32) {
const q = progress / 0.32;
visual.group.position.set(start.x, start.y + q * 3.4, start.z);
visual.group.scale.setScalar(1);
} else {
const q = (progress - 0.32) / 0.68;
visual.group.position.x = THREE.MathUtils.lerp(start.x, -15.8, q);
visual.group.position.z = THREE.MathUtils.lerp(start.z, 9.6, q);
visual.group.position.y = start.y + 3.4;
visual.group.scale.setScalar(1 - q * 0.9);
}
},
() => {
visual.group.parent?.remove(visual.group);
busy = false;
updateStateDisplay();
}
);
}
function enqueueData() {
if (busy) return;
if (queueState.length >= 5) {
announce("Enqueue rejected: this gallery queue has five visible positions.");
return;
}
busy = true;
const name = queueNames[queueSequence % queueNames.length];
queueSequence += 1;
const visual = createPerson(queueSequence, queueSequence);
visual.label = name;
const index = queueState.length;
queueState.push(name);
queueVisuals.push(visual);
const start = new THREE.Vector3(18.3, 0.2, 15.8);
const end = queueTarget(index);
visual.group.position.copy(start);
visual.group.scale.setScalar(0.72);
announce(
`ENQUEUE ${name}: the new arrival joins the tail; existing guests keep their order.`
);
updateStateDisplay();
tween(
950,
(progress) => {
visual.group.position.lerpVectors(start, end, progress);
visual.group.position.y += Math.sin(progress * Math.PI) * 0.35;
visual.group.scale.setScalar(0.72 + progress * 0.28);
},
() => {
visual.group.position.copy(end);
visual.group.scale.setScalar(1);
busy = false;
updateStateDisplay();
}
);
}
function serveData() {
if (busy) return;
if (queueState.length === 0) {
announce("Dequeue rejected: the service line is empty.");
return;
}
busy = true;
const visual = queueVisuals.shift();
const name = queueState.shift();
const headStart = visual.group.position.clone();
const headEnd = new THREE.Vector3(13, 0.2, -6.45);
const movers = queueVisuals.map((member) => {
const from = member.group.position.clone();
const to = from.clone();
to.z -= 4.1;
return { member, from, to };
});
announce(
`DEQUEUE ${name}: the head reaches the counter; the remaining order is preserved.`
);
updateStateDisplay();
tween(
950,
(progress) => {
visual.group.position.lerpVectors(headStart, headEnd, progress);
visual.group.scale.setScalar(1 - progress * 0.28);
movers.forEach(({ member, from, to }) => {
const delayed = Math.max(0, progress - 0.12) / 0.88;
member.group.position.lerpVectors(from, to, delayed);
});
},
() => {
visual.group.parent?.remove(visual.group);
busy = false;
updateStateDisplay();
}
);
}
function resetData() {
stopAuto();
tweens.length = 0;
busy = false;
stackSequence = 4;
queueSequence = 3;
stackState = ["A", "B", "C"];
queueState = ["Mara", "Theo", "Inez"];
rebuildDynamic();
}
// -------------------------------------------------------------------------
// Controls
// -------------------------------------------------------------------------
document.getElementById("push").addEventListener("click", pushData);
document.getElementById("pop").addEventListener("click", popData);
document.getElementById("enqueue").addEventListener("click", enqueueData);
document.getElementById("serve").addEventListener("click", serveData);
document.getElementById("reset").addEventListener("click", resetData);
let autoTimer = null;
let autoStep = 0;
const autoButton = document.getElementById("auto");
function stopAuto() {
if (autoTimer !== null) {
clearInterval(autoTimer);
autoTimer = null;
}
autoButton.classList.remove("active");
autoButton.textContent = "Play tour";
}
function autoTick() {
if (busy) return;
switch (autoStep % 4) {
case 0:
pushData();
break;
case 1:
popData();
break;
case 2:
enqueueData();
break;
case 3:
serveData();
break;
}
autoStep += 1;
}
autoButton.addEventListener("click", () => {
if (autoTimer !== null) {
stopAuto();
return;
}
autoButton.classList.add("active");
autoButton.textContent = "Pause tour";
autoTick();
autoTimer = setInterval(autoTick, 1650);
});
// -------------------------------------------------------------------------
// Custom orbit, zoom, keyboard, and pointer controls
// -------------------------------------------------------------------------
function updateCamera() {
const horizontal = Math.sin(cameraPhi) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraTheta),
cameraTarget.y + Math.cos(cameraPhi) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.updateProjectionMatrix();
}
function resetView() {
cameraTheta = 0.64;
cameraPhi = 0.82;
camera.zoom = 1.04;
updateCamera();
}
function zoomBy(factor) {
camera.zoom = THREE.MathUtils.clamp(camera.zoom * factor, 0.72, 1.8);
updateCamera();
}
document.getElementById("zoom-in").addEventListener("click", () => {
zoomBy(1.12);
});
document.getElementById("zoom-out").addEventListener("click", () => {
zoomBy(1 / 1.12);
});
document.getElementById("reset-view").addEventListener("click", resetView);
let dragging = false;
let previousX = 0;
let previousY = 0;
let activePointer = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
activePointer = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointer) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + deltaY * 0.004,
0.36,
1.22
);
updateCamera();
});
function endDrag(event) {
if (event.pointerId !== activePointer) return;
dragging = false;
activePointer = null;
renderer.domElement.classList.remove("dragging");
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoomBy(Math.exp(-event.deltaY * 0.001));
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") {
cameraTheta += 0.12;
updateCamera();
event.preventDefault();
} else if (key === "arrowright") {
cameraTheta -= 0.12;
updateCamera();
event.preventDefault();
} else if (key === "arrowup") {
cameraPhi = Math.max(0.36, cameraPhi - 0.08);
updateCamera();
event.preventDefault();
} else if (key === "arrowdown") {
cameraPhi = Math.min(1.22, cameraPhi + 0.08);
updateCamera();
event.preventDefault();
} else if (key === "+" || key === "=") {
zoomBy(1.1);
} else if (key === "-") {
zoomBy(1 / 1.1);
} else if (key === "r") {
resetView();
}
});
// -------------------------------------------------------------------------
// Screen-space annotations
// -------------------------------------------------------------------------
const annotationItems = [
{
element: document.getElementById("pin-stack"),
anchor: new THREE.Vector3(-9.4, 7.4, 0.5),
},
{
element: document.getElementById("pin-queue"),
anchor: new THREE.Vector3(13, 6.5, 6.8),
},
{
element: document.getElementById("pin-limits"),
anchor: new THREE.Vector3(0, 5.3, -9.4),
},
];
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
annotationItems.forEach((item) => {
const projected = item.anchor.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
item.element.style.opacity = visible ? "1" : "0";
item.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
item.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
});
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = 43;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
resetView();
rebuildDynamic();
function frame(now) {
updateTweens(now);
updateAnnotations();
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```