Prompt specification
Build or customize the Three.js 3D scene "Sandwich Line Queue Formation" 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: Sandwich Line Queue Formation
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/sandwich-line-queue-formation-x3182
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts production bottlenecks and queues. Animate bread, filling, and wrapping stations with different service times. Show completed sandwiches only after wrapping and let the slow station accumulate a visible queue. Low-poly.
Working Three.js Starter (sandwich-line-queue-formation-x3182):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Sandwich Production Bottleneck</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(17, 30, 33, 0.88);
--border: rgba(255, 255, 255, 0.13);
--muted: #9eb1b2;
--text: #f4f8f6;
--accent: #f1bd62;
--danger: #f16c5d;
--good: #55c9a5;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce6e3;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
z-index: 0;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
top: 20px;
left: 20px;
width: min(350px, calc(100vw - 40px));
padding: 20px;
color: var(--text);
background: var(--panel);
border: 1px solid var(--border);
border-radius: 18px;
box-shadow: 0 18px 55px rgba(20, 34, 35, 0.22);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
color: #b9c9c8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--good);
box-shadow: 0 0 0 5px rgba(85, 201, 165, 0.12);
animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse {
50% {
box-shadow: 0 0 0 8px rgba(85, 201, 165, 0.02);
}
}
h1 {
margin: 9px 0 7px;
font-size: clamp(24px, 3vw, 31px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.description {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin: 17px 0;
}
.metric {
min-width: 0;
padding: 11px 10px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 12px;
background: rgba(255, 255, 255, 0.035);
}
.metric strong {
display: block;
color: var(--text);
font-size: 24px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.metric span {
display: block;
margin-top: 6px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
}
.metric.alert strong {
color: var(--danger);
}
.section-title {
margin: 17px 0 8px;
color: #829696;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
}
.station-row {
margin-top: 7px;
padding: 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-left: 3px solid var(--station-color);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
transition: border-color 160ms ease, background 160ms ease;
}
.station-row.active {
background: rgba(255, 255, 255, 0.07);
}
.station-row.blocked {
border-color: var(--danger);
background: rgba(241, 108, 93, 0.08);
}
.station-head,
.station-bottom {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.station-head strong {
font-size: 12px;
letter-spacing: 0.04em;
}
.service-time {
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.station-bottom {
margin-top: 8px;
}
.queue-label {
color: var(--muted);
font-size: 10px;
white-space: nowrap;
}
.queue-label b {
color: var(--text);
font-size: 12px;
}
.mini-track {
width: 58%;
height: 4px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
.mini-track i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--station-color);
transition: width 80ms linear;
}
.note {
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.09);
color: #91a5a5;
font-size: 10px;
line-height: 1.45;
}
.controls {
position: fixed;
z-index: 12;
top: 20px;
right: 20px;
display: flex;
gap: 8px;
}
button {
padding: 10px 14px;
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
background: rgba(17, 30, 33, 0.88);
border: 1px solid var(--border);
border-radius: 11px;
box-shadow: 0 10px 30px rgba(20, 34, 35, 0.15);
backdrop-filter: blur(12px);
cursor: pointer;
}
button:hover {
background: rgba(30, 47, 49, 0.95);
border-color: rgba(255, 255, 255, 0.25);
}
.help {
position: fixed;
z-index: 10;
left: 20px;
bottom: 20px;
padding: 10px 13px;
color: #dce9e6;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.035em;
background: rgba(17, 30, 33, 0.76);
border: 1px solid var(--border);
border-radius: 11px;
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend {
position: fixed;
z-index: 10;
right: 20px;
bottom: 20px;
display: flex;
gap: 12px;
padding: 10px 13px;
color: #e1ece9;
font-size: 10px;
font-weight: 700;
background: rgba(17, 30, 33, 0.76);
border: 1px solid var(--border);
border-radius: 11px;
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 2px;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
color: #edf5f2;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
background: #17282a;
transition: opacity 350ms ease;
}
#loading.out {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: min(320px, calc(100vw - 24px));
padding: 15px;
}
.description,
.note,
.legend {
display: none;
}
.metrics {
margin: 12px 0;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
}
.help {
left: 12px;
bottom: 12px;
max-width: calc(100vw - 145px);
}
}
</style>
</head>
<body>
<div id="loading">Starting production line…</div>
<aside class="panel">
<div class="eyebrow">
<span class="live-dot"></span>
Live process simulation
</div>
<h1>Sandwich production line</h1>
<p class="description">
Filling takes substantially longer than breading and wrapping, so work
accumulates in front of the filling station.
</p>
<div class="metrics">
<div class="metric alert">
<strong id="fillQueueMetric">0</strong>
<span>FILL QUEUE</span>
</div>
<div class="metric">
<strong id="wipMetric">0</strong>
<span>IN PROCESS</span>
</div>
<div class="metric">
<strong id="completedMetric">0</strong>
<span>WRAPPED</span>
</div>
</div>
<div class="section-title">Station load</div>
<div id="stationRows"></div>
<div class="note">
Finished sandwiches enter the output area only after the wrapping station
completes its service cycle.
</div>
</aside>
<div class="controls">
<button id="pauseButton" type="button">Pause</button>
<button id="resetButton" type="button">Reset</button>
</div>
<div class="help">
Drag to orbit · Scroll to zoom · Space to pause · R to reset
</div>
<div class="legend">
<span><i style="background:#d9caa4"></i>Bread</span>
<span><i style="background:#68ad50"></i>Filling</span>
<span><i style="background:#f1e6ce"></i>Wrapped</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, and camera
// -------------------------------------------------------------------------
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;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly sandwich production line"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce6e3);
scene.fog = new THREE.Fog(0xdce6e3, 34, 61);
const camera = new THREE.OrthographicCamera(-20, 20, 12, -12, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.1, 0);
let cameraAzimuth = 0.58;
let cameraPolar = 1.0;
let cameraZoom = 1;
function resize() {
const aspect = window.innerWidth / window.innerHeight;
const frustumHeight = Math.max(12, 35 / aspect);
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
function updateCamera() {
const spherical = new THREE.Spherical(
31,
cameraPolar,
cameraAzimuth
);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
// Direct camera controls: no addons or examples 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 dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraAzimuth -= dx * 0.006;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar + dy * 0.005,
0.48,
1.34
);
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.65
);
},
{ passive: false }
);
function resetCamera() {
cameraAzimuth = 0.58;
cameraPolar = 1.0;
cameraZoom = 1;
}
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Shared materials and geometry helpers
// -------------------------------------------------------------------------
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.02,
flatShading: true,
...options
});
}
const M = {
floor: standard(0xcbd8d2, { roughness: 0.95 }),
wall: standard(0xbfcfca, { roughness: 0.9 }),
wallTrim: standard(0x8ea39e),
belt: standard(0x293a3b, { roughness: 0.88 }),
beltEdge: standard(0x6d7b78, { metalness: 0.35, roughness: 0.42 }),
metal: standard(0x738785, { metalness: 0.55, roughness: 0.35 }),
dark: standard(0x1d292b),
machine: standard(0x607270),
raw: standard(0xd9caa4),
bread: standard(0xd99a4e),
breadTop: standard(0xe4ae60),
seed: standard(0xf5dfa9),
lettuce: standard(0x68ad50),
tomato: standard(0xe65345),
cheese: standard(0xf4c84c),
wrapper: standard(0xf1e6ce),
band: standard(0xe76f51),
orange: standard(0xf09c49, {
emissive: 0xf07824,
emissiveIntensity: 0.25
}),
yellow: standard(0xf2c84d),
teal: standard(0x4eafa1),
red: standard(0xe56b5c)
};
function addMesh(geometry, material, parent = scene) {
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function box(
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0,
parent = scene,
castShadow = true
) {
const mesh = addMesh(
new THREE.BoxGeometry(width, height, depth),
material,
parent
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
return mesh;
}
function cylinder(
topRadius,
bottomRadius,
height,
segments,
material,
x = 0,
y = 0,
z = 0,
parent = scene
) {
const mesh = addMesh(
new THREE.CylinderGeometry(
topRadius,
bottomRadius,
height,
segments
),
material,
parent
);
mesh.position.set(x, y, z);
return mesh;
}
function makeLabelSprite(title, subtitle, color, width = 4.8) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 180;
const ctx = canvas.getContext("2d");
const accent = `#${new THREE.Color(color).getHexString()}`;
ctx.fillStyle = "rgba(16, 28, 30, 0.94)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 14, canvas.height);
ctx.strokeStyle = "rgba(255,255,255,0.18)";
ctx.lineWidth = 4;
ctx.strokeRect(2, 2, canvas.width - 4, canvas.height - 4);
ctx.textBaseline = "alphabetic";
ctx.fillStyle = "#f5faf7";
ctx.font = "800 48px system-ui, sans-serif";
ctx.fillText(title, 36, 73);
ctx.fillStyle = accent;
ctx.font = "700 27px system-ui, sans-serif";
ctx.fillText(subtitle, 37, 127);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, width * (180 / 768), 1);
sprite.renderOrder = 20;
return sprite;
}
// -------------------------------------------------------------------------
// Lighting and factory environment
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf5fbff, 0x71827a, 2.15));
const sun = new THREE.DirectionalLight(0xfff2d9, 3.0);
sun.position.set(-8, 18, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -19;
sun.shadow.camera.right = 19;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -16;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 48;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0xc9e8f0, 1.1);
fillLight.position.set(10, 8, -10);
scene.add(fillLight);
const floor = addMesh(
new THREE.PlaneGeometry(36, 18),
M.floor,
scene
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.015;
floor.castShadow = false;
const grid = new THREE.GridHelper(36, 36, 0x7e9690, 0x91a59f);
grid.position.y = 0.008;
grid.scale.z = 0.5;
grid.material.transparent = true;
grid.material.opacity = 0.16;
scene.add(grid);
box(36, 2.9, 0.25, M.wall, 0, 1.45, -7.8);
box(36, 0.16, 0.34, M.wallTrim, 0, 2.85, -7.76);
for (const x of [-15, -8, -1, 6, 13]) {
box(0.32, 3.1, 0.4, M.wallTrim, x, 1.55, -7.58);
}
// Conveyor structure.
box(30, 0.42, 1.8, M.belt, 0, 0.72, 0);
box(30.2, 0.12, 0.13, M.beltEdge, 0, 0.45, 0.95);
box(30.2, 0.12, 0.13, M.beltEdge, 0, 0.45, -0.95);
box(30.2, 0.08, 0.08, M.dark, 0, 0.96, 0.89);
box(30.2, 0.08, 0.08, M.dark, 0, 0.96, -0.89);
for (let x = -14; x <= 14; x += 4) {
box(0.18, 0.52, 0.18, M.metal, x, 0.25, 0.78);
box(0.18, 0.52, 0.18, M.metal, x, 0.25, -0.78);
box(0.18, 0.13, 1.75, M.metal, x, 0.46, 0);
}
// Belt slats.
for (let x = -14.5; x < 14.6; x += 0.62) {
box(
0.26,
0.025,
1.54,
M.metal,
x,
0.946,
0,
scene,
false
);
}
// Floor flow arrows.
for (const x of [-13.5, -5.8, 3.8, 11.5]) {
const arrow = addMesh(
new THREE.ConeGeometry(0.16, 0.42, 3),
M.teal
);
arrow.position.set(x, 0.075, 1.7);
arrow.rotation.z = -Math.PI / 2;
arrow.castShadow = false;
}
// Safety stripe near the bottleneck.
for (let i = 0; i < 11; i++) {
box(
0.38,
0.025,
0.22,
i % 2 === 0 ? M.yellow : M.dark,
-5.5 + i * 0.55,
0.025,
1.45,
scene,
false
);
}
const inputSign = makeLabelSprite(
"RAW INPUT",
"DOUGH BATCHES",
0xd9caa4,
3.0
);
inputSign.position.set(-14.1, 2.55, 0);
scene.add(inputSign);
const outputSign = makeLabelSprite(
"FINISHED",
"WRAPPED ONLY",
0x4eafa1,
3.2
);
outputSign.position.set(12.8, 2.8, 0);
scene.add(outputSign);
// -------------------------------------------------------------------------
// Stations
// -------------------------------------------------------------------------
const QUEUE_CAPACITY = 5;
const QUEUE_SPACING = 1.35;
const stations = [
{
key: "bread",
number: "01",
name: "BREAD",
x: -9,
duration: 1.8,
color: 0xe3a24e
},
{
key: "filling",
number: "02",
name: "FILLING",
x: 0,
duration: 4.8,
color: 0xe56b5c
},
{
key: "wrap",
number: "03",
name: "WRAP",
x: 9,
duration: 3.0,
color: 0x4eafa1
}
];
function addQueueSlots(station) {
const outlineMaterial = new THREE.MeshBasicMaterial({
color: station.color,
transparent: true,
opacity: 0.36
});
for (let i = 0; i < QUEUE_CAPACITY; i++) {
const slot = new THREE.Group();
slot.position.x = -i * QUEUE_SPACING;
station.group.add(slot);
box(
0.96,
0.018,
0.035,
outlineMaterial,
0,
0.978,
0.49,
slot,
false
);
box(
0.96,
0.018,
0.035,
outlineMaterial,
0,
0.978,
-0.49,
slot,
false
);
box(
0.035,
0.018,
0.95,
outlineMaterial,
0.47,
0.978,
0,
slot,
false
);
box(
0.035,
0.018,
0.95,
outlineMaterial,
-0.47,
0.978,
0,
slot,
false
);
}
}
function buildBreadMachine(station) {
const group = station.group;
box(2.25, 1.85, 1.45, M.machine, 0, 1.65, -1.7, group);
box(1.65, 0.9, 0.09, M.dark, 0, 1.55, -0.94, group);
station.ovenGlow = box(
1.48,
0.12,
0.1,
M.orange,
0,
1.14,
-0.87,
group
);
station.press = box(
1.22,
0.16,
1.3,
M.metal,
0,
2.55,
0,
group
);
cylinder(
0.17,
0.2,
0.8,
8,
M.metal,
0.72,
3.05,
-1.72,
group
);
box(
0.22,
2.2,
0.22,
station.accent,
-1.25,
2.1,
-1.45,
group
);
}
function buildFillingMachine(station) {
const group = station.group;
// Three low-poly ingredient silos.
const siloColors = [M.lettuce, M.tomato, M.cheese];
for (let i = 0; i < 3; i++) {
const x = (i - 1) * 0.8;
cylinder(
0.3,
0.36,
0.95,
8,
siloColors[i],
x,
2.65,
-1.65,
group
);
cylinder(
0.12,
0.12,
0.8,
8,
M.metal,
x,
1.85,
-1.65,
group
);
}
box(2.7, 0.16, 0.18, M.metal, 0, 3.12, -0.1, group);
box(0.18, 2.45, 0.18, station.accent, -1.45, 2.1, -1.42, group);
box(0.18, 2.45, 0.18, station.accent, 1.45, 2.1, -1.42, group);
station.nozzle = new THREE.Group();
station.nozzle.position.set(0, 2.2, 0);
group.add(station.nozzle);
cylinder(
0.09,
0.09,
0.72,
8,
M.metal,
0,
0.22,
0,
station.nozzle
);
const cone = addMesh(
new THREE.ConeGeometry(0.18, 0.3, 6),
station.accent,
station.nozzle
);
cone.position.y = -0.25;
cone.rotation.z = Math.PI;
station.particles = [];
for (let i = 0; i < 9; i++) {
const color = [M.lettuce, M.tomato, M.cheese][i % 3];
const particle = addMesh(
new THREE.OctahedronGeometry(0.075, 0),
color,
group
);
particle.visible = false;
station.particles.push(particle);
}
}
function buildWrapMachine(station) {
const group = station.group;
station.wrapPivot = new THREE.Group();
station.wrapPivot.position.set(0, 1.48, 0);
group.add(station.wrapPivot);
const ring = addMesh(
new THREE.TorusGeometry(0.93, 0.08, 6, 16),
station.accent,
station.wrapPivot
);
ring.rotation.y = Math.PI / 2;
box(
0.18,
0.15,
0.58,
M.metal,
0,
0,
0.65,
station.wrapPivot
);
box(
0.18,
0.15,
0.58,
M.metal,
0,
0,
-0.65,
station.wrapPivot
);
station.paperRoll = cylinder(
0.28,
0.28,
1.6,
12,
M.wrapper,
0.9,
2.2,
-0.6,
group
);
station.paperRoll.rotation.x = Math.PI / 2;
box(
0.2,
2.9,
0.2,
station.accent,
-1.4,
2.15,
-1.4,
group
);
box(
0.2,
2.9,
0.2,
station.accent,
1.4,
2.15,
-1.4,
group
);
box(
2.9,
0.2,
0.2,
station.accent,
0,
3.55,
-1.4,
group
);
}
function buildStation(station) {
station.group = new THREE.Group();
station.group.position.x = station.x;
scene.add(station.group);
station.accent = standard(station.color, {
roughness: 0.55,
metalness: 0.1
});
station.queue = [];
station.busy = false;
station.blocked = false;
station.elapsed = 0;
station.displayProgress = 0;
// Service platform.
box(
2.0,
0.035,
1.72,
standard(station.color, {
transparent: true,
opacity: 0.38,
roughness: 0.7
}),
0,
0.96,
0,
station.group,
false
);
addQueueSlots(station);
const subtitle =
station.key === "filling"
? "4.8 SEC | BOTTLENECK"
: `${station.duration.toFixed(1)} SEC SERVICE`;
const label = makeLabelSprite(
`${station.number} ${station.name}`,
subtitle,
station.color,
4.85
);
label.position.set(0, 4.08, 0);
station.group.add(label);
// Service progress bar.
station.progressGroup = new THREE.Group();
station.progressGroup.position.set(0, 3.47, 0.18);
station.group.add(station.progressGroup);
box(
2.45,
0.13,
0.12,
M.dark,
0,
0,
0,
station.progressGroup,
false
);
const fillGeometry = new THREE.BoxGeometry(2.32, 0.085, 0.09);
fillGeometry.translate(1.16, 0, 0);
station.progressFill = addMesh(
fillGeometry,
standard(station.color, {
emissive: station.color,
emissiveIntensity: 0.25
}),
station.progressGroup
);
station.progressFill.position.x = -1.16;
station.progressFill.scale.x = 0.001;
station.progressFill.castShadow = false;
station.statusLamp = addMesh(
new THREE.SphereGeometry(0.13, 8, 6),
standard(0xf4c85a, {
emissive: 0xf4c85a,
emissiveIntensity: 0.6
}),
station.progressGroup
);
station.statusLamp.position.set(1.48, 0, 0.02);
// Queue-count lamps.
station.queueLamps = [];
for (let i = 0; i < QUEUE_CAPACITY; i++) {
const lampMaterial = standard(0x425255, {
emissive: 0x425255,
emissiveIntensity: 0.1
});
const lamp = box(
0.22,
0.12,
0.12,
lampMaterial,
-1.2 + i * 0.6,
3.17,
0.18,
station.progressGroup,
false
);
station.queueLamps.push(lamp);
}
if (station.key === "bread") {
buildBreadMachine(station);
} else if (station.key === "filling") {
buildFillingMachine(station);
} else {
buildWrapMachine(station);
}
}
stations.forEach(buildStation);
const stationByKey = Object.fromEntries(
stations.map((station) => [station.key, station])
);
// -------------------------------------------------------------------------
// Sandwich entities
// -------------------------------------------------------------------------
const allEntities = [];
const completedItems = [];
let entityId = 0;
function createEntity(stage, x) {
const group = new THREE.Group();
group.position.set(x, 0.985, 0);
scene.add(group);
// Raw dough.
const rawGroup = new THREE.Group();
group.add(rawGroup);
const dough = addMesh(
new THREE.DodecahedronGeometry(0.49, 0),
M.raw,
rawGroup
);
dough.scale.set(0.95, 0.52, 0.82);
dough.position.y = 0.25;
// Toasted bread.
const breadGroup = new THREE.Group();
group.add(breadGroup);
box(
0.86,
0.18,
0.72,
M.bread,
0,
0.12,
0,
breadGroup
);
box(
0.86,
0.18,
0.72,
M.breadTop,
0,
0.67,
0,
breadGroup
);
const seedPositions = [
[-0.22, -0.15, 0.12],
[0.18, -0.08, -0.08],
[-0.06, 0.19, -0.08],
[0.25, 0.18, 0.13]
];
for (const [sx, sz, rotation] of seedPositions) {
const seed = box(
0.075,
0.025,
0.035,
M.seed,
sx,
0.77,
sz,
breadGroup,
false
);
seed.rotation.y = rotation;
}
// Ingredients appear only after the filling cycle.
const ingredientGroup = new THREE.Group();
group.add(ingredientGroup);
box(
0.91,
0.09,
0.76,
M.lettuce,
0,
0.28,
0,
ingredientGroup
);
const tomato = cylinder(
0.39,
0.39,
0.11,
8,
M.tomato,
0,
0.37,
0,
ingredientGroup
);
const cheese = box(
0.82,
0.055,
0.72,
M.cheese,
0,
0.46,
0,
ingredientGroup
);
cheese.rotation.y = 0.1;
// Wrapper appears only after wrapping completes.
const wrapperGroup = new THREE.Group();
wrapperGroup.position.y = 0.39;
group.add(wrapperGroup);
box(
0.98,
0.58,
0.82,
M.wrapper,
0,
0,
0,
wrapperGroup
);
box(
1.0,
0.13,
0.84,
M.band,
0,
0.02,
0,
wrapperGroup
);
const entity = {
id: entityId++,
group,
rawGroup,
breadGroup,
ingredientGroup,
wrapperGroup,
stage,
x,
targetX: x,
wrapperGrowth: 0
};
setEntityStage(entity, stage);
allEntities.push(entity);
return entity;
}
function setEntityStage(entity, stage) {
entity.stage = stage;
entity.rawGroup.visible = stage === "raw";
entity.breadGroup.visible = stage !== "raw";
entity.ingredientGroup.visible =
stage === "filled" || stage === "done";
entity.wrapperGroup.visible = stage === "done";
if (stage === "done") {
entity.wrapperGrowth = 0;
entity.wrapperGroup.scale.set(0.68, 0.05, 0.68);
}
}
// -------------------------------------------------------------------------
// Simulation
// -------------------------------------------------------------------------
let paused = false;
let simulationTime = 0;
let spawnTimer = 1.0;
let completedCount = 0;
const INPUT_X = -14.2;
function spawnRawItem() {
if (stationByKey.bread.queue.length >= QUEUE_CAPACITY - 1) {
return;
}
const entity = createEntity(
"raw",
INPUT_X + (Math.random() - 0.5) * 0.2
);
stationByKey.bread.queue.push(entity);
}
function resetSimulation() {
for (const entity of allEntities) {
scene.remove(entity.group);
}
allEntities.length = 0;
completedItems.length = 0;
completedCount = 0;
simulationTime = 0;
spawnTimer = 1.0;
for (const station of stations) {
station.queue.length = 0;
station.busy = false;
station.blocked = false;
station.elapsed = 0;
station.displayProgress = 0;
}
// Start with a small visible backlog at the bottleneck.
const breadItem = createEntity("raw", stationByKey.bread.x);
stationByKey.bread.queue.push(breadItem);
const secondBreadItem = createEntity(
"raw",
stationByKey.bread.x - QUEUE_SPACING
);
stationByKey.bread.queue.push(secondBreadItem);
for (let i = 0; i < 3; i++) {
const item = createEntity(
"bread",
stationByKey.filling.x - i * QUEUE_SPACING
);
stationByKey.filling.queue.push(item);
}
// One item is already being wrapped, so the output activates quickly.
for (let i = 0; i < 2; i++) {
const item = createEntity(
"filled",
stationByKey.wrap.x - i * QUEUE_SPACING
);
stationByKey.wrap.queue.push(item);
}
}
function downstreamIsFull(station) {
if (station.key === "bread") {
return stationByKey.filling.queue.length >= QUEUE_CAPACITY;
}
if (station.key === "filling") {
return stationByKey.wrap.queue.length >= QUEUE_CAPACITY;
}
return false;
}
function updateQueueTargets() {
for (const station of stations) {
station.queue.forEach((entity, index) => {
entity.targetX = station.x - index * QUEUE_SPACING;
});
}
}
function updateEntities(delta) {
const smoothing = 1 - Math.exp(-delta * 3.25);
for (const entity of allEntities) {
entity.x += (entity.targetX - entity.x) * smoothing;
entity.group.position.x = entity.x;
entity.group.position.y =
0.985 + Math.sin(simulationTime * 4 + entity.id) * 0.012;
if (entity.stage === "done") {
entity.wrapperGrowth = Math.min(
1,
entity.wrapperGrowth + delta * 2.8
);
const growth = 1 - Math.pow(1 - entity.wrapperGrowth, 3);
entity.wrapperGroup.scale.set(
0.68 + growth * 0.32,
0.05 + growth * 0.95,
0.68 + growth * 0.32
);
}
}
}
function completeService(station, entity) {
if (station.key === "bread") {
setEntityStage(entity, "bread");
stationByKey.filling.queue.push(entity);
entity.targetX =
stationByKey.filling.x -
(stationByKey.filling.queue.length - 1) * QUEUE_SPACING;
} else if (station.key === "filling") {
setEntityStage(entity, "filled");
stationByKey.wrap.queue.push(entity);
entity.targetX =
stationByKey.wrap.x -
(stationByKey.wrap.queue.length - 1) * QUEUE_SPACING;
} else {
// Only the wrapping station can produce a completed sandwich.
setEntityStage(entity, "done");
entity.targetX = 11.4 + (completedCount % 4) * 1.35;
completedItems.push(entity);
completedCount++;
}
}
function updateStations(delta) {
for (const station of stations) {
station.blocked = downstreamIsFull(station);
if (
!station.busy &&
station.queue.length > 0 &&
!station.blocked &&
Math.abs(station.queue[0].x - station.x) < 0.075
) {
station.busy = true;
station.elapsed = 0;
}
if (station.busy) {
station.elapsed += delta;
if (station.elapsed >= station.duration) {
const entity = station.queue.shift();
station.busy = false;
station.elapsed = 0;
completeService(station, entity);
}
}
}
// Remove finished products after they reach the end of the output lane.
for (let i = completedItems.length - 1; i >= 0; i--) {
const entity = completedItems[i];
if (entity.x > 16.1) {
completedItems.splice(i, 1);
allEntities.splice(allEntities.indexOf(entity), 1);
scene.remove(entity.group);
}
}
}
function updateStationIndicators(delta) {
for (const station of stations) {
const targetProgress = station.busy
? station.elapsed / station.duration
: 0;
station.displayProgress +=
(targetProgress - station.displayProgress) *
(1 - Math.exp(-delta * 12));
station.progressFill.scale.x = Math.max(
0.001,
station.displayProgress
);
let lampColor = 0xf4c85a;
let lampIntensity = 0.45;
if (station.busy) {
lampColor = 0x55c9a5;
lampIntensity = 1.1;
} else if (station.blocked) {
lampColor = 0xf16c5d;
lampIntensity =
0.7 + Math.sin(simulationTime * 9) * 0.35;
}
station.statusLamp.material.color.setHex(lampColor);
station.statusLamp.material.emissive.setHex(lampColor);
station.statusLamp.material.emissiveIntensity = lampIntensity;
station.queueLamps.forEach((lamp, index) => {
const lit = index < station.queue.length;
const color = lit ? station.color : 0x425255;
lamp.material.color.setHex(color);
lamp.material.emissive.setHex(color);
lamp.material.emissiveIntensity = lit ? 0.7 : 0.08;
});
}
}
function updateMachines() {
const bread = stationByKey.bread;
const filling = stationByKey.filling;
const wrap = stationByKey.wrap;
// Bread press and oven glow.
bread.press.position.y = bread.busy
? 2.12 + Math.sin(simulationTime * 9) * 0.16
: 2.55;
bread.ovenGlow.material.emissiveIntensity = bread.busy
? 1.7 + Math.sin(simulationTime * 10) * 0.6
: 0.25;
// Filling nozzle and falling ingredient particles.
filling.nozzle.position.y = filling.busy
? 1.95 + Math.sin(simulationTime * 12) * 0.09
: 2.2;
filling.particles.forEach((particle, index) => {
particle.visible = filling.busy;
if (filling.busy) {
const cycle = (simulationTime * 1.8 + index / 9) % 1;
particle.position.set(
Math.sin(index * 2.1) * 0.08,
1.86 - cycle * 0.82,
Math.cos(index * 1.7) * 0.06
);
particle.scale.setScalar(0.8 + cycle * 0.35);
particle.rotation.x = simulationTime * 4 + index;
}
});
// Wrapping ring and arms.
wrap.wrapPivot.rotation.x = simulationTime * (wrap.busy ? 3.8 : 0.4);
wrap.paperRoll.rotation.z = simulationTime * (wrap.busy ? 3.0 : 0.25);
}
// -------------------------------------------------------------------------
// HUD
// -------------------------------------------------------------------------
const stationRows = document.getElementById("stationRows");
for (const station of stations) {
const row = document.createElement("div");
row.className = "station-row";
row.style.setProperty(
"--station-color",
`#${new THREE.Color(station.color).getHexString()}`
);
row.innerHTML = `
<div class="station-head">
<strong>${station.number} ${station.name}</strong>
<span class="service-time">${station.duration.toFixed(1)}s</span>
</div>
<div class="station-bottom">
<span class="queue-label">
<b class="queue-count">${station.queue.length}</b>
at station
</span>
<div class="mini-track">
<i></i>
</div>
</div>
`;
stationRows.appendChild(row);
station.ui = {
row,
count: row.querySelector(".queue-count"),
progress: row.querySelector(".mini-track i")
};
}
const fillQueueMetric = document.getElementById("fillQueueMetric");
const wipMetric = document.getElementById("wipMetric");
const completedMetric = document.getElementById("completedMetric");
function updateHUD() {
fillQueueMetric.textContent = stationByKey.filling.queue.length;
wipMetric.textContent = allEntities.filter(
(entity) => entity.stage !== "done"
).length;
completedMetric.textContent = completedCount;
for (const station of stations) {
station.ui.count.textContent = station.queue.length;
station.ui.progress.style.width =
`${station.displayProgress * 100}%`;
station.ui.row.classList.toggle("active", station.busy);
station.ui.row.classList.toggle("blocked", station.blocked);
}
}
const pauseButton = document.getElementById("pauseButton");
function togglePause() {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
}
pauseButton.addEventListener("click", togglePause);
document.getElementById("resetButton").addEventListener("click", () => {
resetSimulation();
resetCamera();
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "r") {
resetSimulation();
resetCamera();
}
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
resetSimulation();
const clock = new THREE.Clock();
let hudTimer = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationTime += delta;
spawnTimer -= delta;
if (spawnTimer <= 0) {
spawnRawItem();
spawnTimer += 1.25;
}
updateQueueTargets();
updateEntities(delta);
updateStations(delta);
updateStationIndicators(delta);
updateMachines();
hudTimer -= delta;
if (hudTimer <= 0) {
updateHUD();
hudTimer = 0.1;
}
}
updateCamera();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("out");
});
</script>
</body>
</html>
```