Prompt specification
Build or customize the Three.js 3D scene "Grain shortage changes a bakery schedule" 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: Grain shortage changes a bakery schedule
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/grain-shortage-changes-a-bakery-schedule-x2312
Scene Description Prompt:
Reduce a fictional grain delivery to a low-poly Roman bakery and watch fewer counted bread batches reach its counter. Three.js from cdnjs, one HTML file.
Working Three.js Starter (grain-shortage-changes-a-bakery-schedule-x2312):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Panis Roma — Grain to Counter</title>
<style>
:root {
--ink: #30251d;
--muted: #776a5b;
--paper: #f5ead3;
--paper-deep: #e9d7b7;
--red: #9d3f2f;
--red-dark: #6f2921;
--gold: #d7a746;
--green: #65794b;
--line: rgba(70, 48, 30, 0.18);
--shadow: 0 18px 50px rgba(46, 29, 16, 0.22);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cdb58d;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene.dragging { cursor: grabbing; }
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(90deg, rgba(49, 32, 20, .10), transparent 26%),
radial-gradient(circle at 55% 42%, transparent 48%, rgba(50, 30, 15, .18));
}
.brand {
position: fixed;
top: 22px;
left: 24px;
z-index: 5;
display: flex;
align-items: center;
gap: 12px;
pointer-events: none;
}
.seal {
width: 50px;
height: 50px;
display: grid;
place-items: center;
border-radius: 50%;
border: 2px solid rgba(255, 244, 216, .85);
color: #fff0cf;
background: var(--red);
box-shadow: 0 8px 24px rgba(52, 27, 13, .28);
font: 700 18px/1 Georgia, serif;
}
.brand h1 {
margin: 0;
color: #fff3d8;
text-shadow: 0 2px 12px rgba(39, 22, 12, .45);
font: 700 25px/1 Georgia, serif;
letter-spacing: .04em;
}
.brand p {
margin: 5px 0 0;
color: rgba(255, 244, 218, .86);
font-size: 11px;
letter-spacing: .18em;
text-transform: uppercase;
}
.stats {
position: fixed;
top: 20px;
right: 22px;
z-index: 5;
display: grid;
grid-template-columns: repeat(4, minmax(92px, 1fr));
overflow: hidden;
border: 1px solid rgba(255, 244, 217, .38);
border-radius: 14px;
background: rgba(244, 229, 198, .91);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.stat {
min-width: 94px;
padding: 11px 15px 10px;
border-right: 1px solid var(--line);
}
.stat:last-child { border-right: 0; }
.stat span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .13em;
text-transform: uppercase;
}
.stat strong {
display: block;
margin-top: 3px;
font: 700 21px/1.1 Georgia, serif;
}
.stat.counter strong { color: var(--red); }
.panel {
position: fixed;
z-index: 6;
left: 22px;
top: 92px;
width: 330px;
max-height: calc(100vh - 115px);
overflow: auto;
padding: 18px;
border: 1px solid rgba(255, 246, 223, .58);
border-radius: 17px;
background: rgba(247, 235, 211, .94);
box-shadow: var(--shadow);
backdrop-filter: blur(15px);
scrollbar-width: thin;
}
.eyebrow {
margin: 0 0 5px;
color: var(--red);
font-size: 9px;
font-weight: 900;
letter-spacing: .17em;
text-transform: uppercase;
}
.panel h2 {
margin: 0;
font: 700 23px/1.1 Georgia, serif;
}
.intro {
margin: 9px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
fieldset {
min-width: 0;
margin: 0;
padding: 0;
border: 0;
}
fieldset:disabled { opacity: .64; }
.control {
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.control-title {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
.control-title label,
.control-title strong {
font-size: 11px;
font-weight: 850;
letter-spacing: .07em;
text-transform: uppercase;
}
.control-title strong {
color: var(--red);
letter-spacing: 0;
text-transform: none;
}
.segments {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.segments button {
min-height: 42px;
padding: 7px 5px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--ink);
background: rgba(255, 250, 235, .62);
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition: transform .15s, background .15s, border-color .15s;
}
.segments button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(157, 63, 47, .45);
}
.segments button.selected {
border-color: var(--red);
color: #fff7e7;
background: var(--red);
box-shadow: 0 4px 10px rgba(112, 41, 33, .18);
}
.segments button small {
display: block;
margin-top: 2px;
font-size: 9px;
font-weight: 500;
opacity: .78;
}
.range-row {
display: flex;
align-items: center;
gap: 10px;
}
input[type="range"] {
width: 100%;
height: 5px;
border-radius: 10px;
outline: none;
appearance: none;
background: linear-gradient(90deg, #a94a33, #d9a84d 55%, #8a6a43);
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid var(--paper);
border-radius: 50%;
appearance: none;
background: var(--red);
box-shadow: 0 2px 7px rgba(45, 25, 13, .35);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid var(--paper);
border-radius: 50%;
background: var(--red);
box-shadow: 0 2px 7px rgba(45, 25, 13, .35);
cursor: pointer;
}
.temperature {
min-width: 56px;
padding: 6px 7px;
border-radius: 8px;
color: #fff4db;
background: var(--ink);
text-align: center;
font-size: 11px;
font-weight: 800;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: var(--muted);
font-size: 9px;
}
.quality {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 9px;
padding: 8px 10px;
border: 1px solid rgba(101, 121, 75, .24);
border-radius: 9px;
color: #50603d;
background: rgba(101, 121, 75, .10);
font-size: 10px;
font-weight: 750;
}
.preview {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: center;
margin: 17px 0 13px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 250, 237, .55);
}
.preview span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .1em;
text-transform: uppercase;
}
.preview strong {
display: block;
margin-top: 3px;
font: 700 18px Georgia, serif;
}
.preview .preview-number {
color: var(--red);
font: 700 28px Georgia, serif;
}
.actions {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
}
.primary, .secondary {
min-height: 43px;
border-radius: 10px;
cursor: pointer;
font-size: 11px;
font-weight: 850;
letter-spacing: .07em;
text-transform: uppercase;
transition: transform .15s, filter .15s;
}
.primary {
border: 1px solid var(--red-dark);
color: #fff8e8;
background: var(--red);
box-shadow: 0 6px 14px rgba(112, 41, 33, .2);
}
.secondary {
padding: 0 14px;
border: 1px solid var(--line);
color: var(--ink);
background: rgba(255, 250, 237, .65);
}
.primary:hover, .secondary:hover { transform: translateY(-1px); filter: brightness(1.04); }
.primary.paused { background: #8a6a43; }
.summary {
margin-top: 13px;
padding: 12px;
border-left: 3px solid var(--gold);
border-radius: 8px;
color: #594936;
background: rgba(215, 167, 70, .13);
font-size: 11px;
line-height: 1.45;
}
.summary strong {
display: block;
margin-bottom: 3px;
color: var(--ink);
font: 700 16px Georgia, serif;
}
.process {
position: fixed;
z-index: 5;
left: 50%;
bottom: 22px;
width: min(540px, calc(100vw - 44px));
transform: translateX(-50%);
padding: 12px 15px 13px;
border: 1px solid rgba(255, 244, 217, .42);
border-radius: 14px;
color: #fff2d5;
background: rgba(54, 38, 25, .84);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.process-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
font-size: 9px;
font-weight: 850;
letter-spacing: .13em;
text-transform: uppercase;
}
#progressText { color: #e6c583; }
.progress-track {
height: 3px;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, .12);
}
#progressFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--gold), #f4d47f);
transition: width .2s linear;
}
.stages {
display: grid;
grid-template-columns: repeat(4, 1fr);
margin-top: 10px;
color: rgba(255, 242, 213, .44);
font-size: 9px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
.stage {
position: relative;
text-align: center;
}
.stage:not(:last-child)::after {
position: absolute;
top: 4px;
left: calc(50% + 17px);
width: calc(100% - 34px);
height: 1px;
content: "";
background: rgba(255, 255, 255, .12);
}
.stage i {
position: relative;
z-index: 1;
display: grid;
width: 9px;
height: 9px;
margin: 0 auto 5px;
border: 2px solid #5e4b38;
border-radius: 50%;
background: #49382a;
}
.stage.active { color: #f8d990; }
.stage.active i {
border-color: #f0c569;
background: #d7a746;
box-shadow: 0 0 0 4px rgba(215, 167, 70, .12);
}
.stage.done { color: rgba(236, 220, 185, .72); }
.stage.done i { border-color: #84925f; background: #65794b; }
.log {
position: fixed;
z-index: 5;
right: 22px;
bottom: 22px;
width: 290px;
padding: 12px 14px;
border: 1px solid rgba(255, 244, 217, .4);
border-radius: 13px;
color: #f7e9cb;
background: rgba(54, 38, 25, .8);
box-shadow: var(--shadow);
backdrop-filter: blur(10px);
}
.log-title {
margin-bottom: 7px;
color: #ddb86d;
font-size: 9px;
font-weight: 850;
letter-spacing: .14em;
text-transform: uppercase;
}
.log-line {
overflow: hidden;
margin-top: 5px;
color: rgba(255, 243, 217, .82);
font-size: 10px;
line-height: 1.35;
text-overflow: ellipsis;
white-space: nowrap;
}
.log-line.bad { color: #e7a18f; }
.log-line.good { color: #c8d49d; }
.help {
position: fixed;
z-index: 4;
left: 24px;
bottom: 22px;
color: rgba(255, 242, 214, .72);
font-size: 10px;
pointer-events: none;
text-shadow: 0 1px 4px rgba(36, 22, 13, .7);
}
.world-label {
position: fixed;
z-index: 3;
top: 0;
left: 0;
padding: 5px 9px;
border: 1px solid rgba(255, 242, 207, .45);
border-radius: 999px;
color: #fff1cc;
background: rgba(67, 43, 27, .78);
box-shadow: 0 6px 16px rgba(40, 23, 12, .18);
font-size: 9px;
font-weight: 850;
letter-spacing: .1em;
pointer-events: none;
text-transform: uppercase;
transform: translate(-50%, -100%);
white-space: nowrap;
}
.world-label.counter { border-color: rgba(239, 194, 100, .7); }
[hidden] { display: none !important; }
@media (max-width: 820px) {
.brand { top: 13px; left: 14px; }
.brand h1 { font-size: 20px; }
.brand p { display: none; }
.seal { width: 40px; height: 40px; font-size: 15px; }
.stats {
top: 62px;
right: 12px;
left: 12px;
grid-template-columns: repeat(4, 1fr);
}
.stat { min-width: 0; padding: 8px 9px; }
.stat span { font-size: 7px; }
.stat strong { font-size: 17px; }
.panel {
top: auto;
right: 10px;
bottom: 87px;
left: 10px;
width: auto;
max-height: 46vh;
padding: 14px;
}
.panel h2 { font-size: 19px; }
.intro { display: none; }
.control { margin-top: 11px; padding-top: 10px; }
.preview { margin: 12px 0 10px; }
.log { display: none; }
.help { display: none; }
.process { bottom: 10px; width: calc(100vw - 20px); }
}
@media (max-height: 690px) and (min-width: 821px) {
.panel { top: 84px; max-height: calc(100vh - 100px); }
.intro { display: none; }
.control { margin-top: 10px; padding-top: 10px; }
.preview { margin: 12px 0 10px; }
.process, .log { bottom: 12px; }
.help { display: none; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly Roman bakery"></canvas>
<div class="vignette"></div>
<header class="brand">
<div class="seal">XII</div>
<div>
<h1>PANIS ROMA</h1>
<p>Grain to counter</p>
</div>
</header>
<section class="stats" aria-label="Bakery counters">
<div class="stat">
<span>Delivered</span>
<strong id="grainStat">12 sacks</strong>
</div>
<div class="stat">
<span>Flour batches</span>
<strong id="flourStat">6 batches</strong>
</div>
<div class="stat counter" aria-live="polite">
<span>At counter</span>
<strong id="counterStat">0</strong>
</div>
<div class="stat">
<span>Lost batches</span>
<strong id="lostStat">0</strong>
</div>
</section>
<aside class="panel">
<p class="eyebrow">Fictional delivery plan</p>
<h2>Run the bakery</h2>
<p class="intro">
Reduce a grain delivery into flour, bake each batch, and count only the
bread batches that survive the journey to the counter.
</p>
<fieldset id="settings">
<div class="control">
<div class="control-title">
<label>Delivery size</label>
<strong id="deliveryHint">River granary</strong>
</div>
<div class="segments" id="deliveryOptions">
<button type="button" data-sacks="6">6 sacks<small>Small</small></button>
<button type="button" data-sacks="9">9 sacks<small>Medium</small></button>
<button type="button" data-sacks="12" class="selected">12 sacks<small>Full</small></button>
</div>
</div>
<div class="control">
<div class="control-title">
<label>Mill efficiency</label>
<strong id="yieldHint">4 sacks / batch</strong>
</div>
<div class="segments" id="yieldOptions">
<button type="button" data-yield="2">2 sacks<small>Fine flour</small></button>
<button type="button" data-yield="3">3 sacks<small>Balanced</small></button>
<button type="button" data-yield="4" class="selected">4 sacks<small>Coarse flour</small></button>
</div>
</div>
<div class="control">
<div class="control-title">
<label for="temperature">Oven heat</label>
<strong id="heatHint">225°C</strong>
</div>
<div class="range-row">
<input id="temperature" type="range" min="160" max="300" value="225" step="5" />
<output class="temperature" id="temperatureValue">225°C</output>
</div>
<div class="range-labels">
<span>Cool</span><span>Ideal 235°</span><span>Hot</span>
</div>
<div class="quality">
<span>Bake confidence</span>
<span id="qualityValue">91%</span>
</div>
</div>
</fieldset>
<div class="preview">
<div>
<span>Possible counter batches</span>
<strong id="previewText">12 sacks → 3 batches</strong>
</div>
<div class="preview-number" id="previewNumber">3</div>
</div>
<div class="actions">
<button class="primary" id="mainButton" type="button">Start shift</button>
<button class="secondary" id="resetButton" type="button">Reset</button>
</div>
<div class="summary" id="summary" hidden aria-live="polite">
<strong id="summaryTitle">Shift complete</strong>
<span id="summaryText"></span>
</div>
</aside>
<section class="process" aria-label="Production progress">
<div class="process-head">
<span>Production line</span>
<span id="progressText">Awaiting delivery</span>
</div>
<div class="progress-track"><div id="progressFill"></div></div>
<div class="stages" id="stages">
<div class="stage"><i></i>Delivery</div>
<div class="stage"><i></i>Mill</div>
<div class="stage"><i></i>Oven</div>
<div class="stage"><i></i>Counter</div>
</div>
</section>
<section class="log" aria-live="polite">
<div class="log-title">Bakery ledger</div>
<div id="logLines"></div>
</section>
<div class="help">Drag to orbit · Scroll to zoom</div>
<div class="world-label" id="millLabel">Stone mill</div>
<div class="world-label" id="ovenLabel">Domed oven</div>
<div class="world-label counter" id="counterLabel">Counter · 0</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#d8c39b");
scene.fog = new THREE.Fog("#d8c39b", 15, 30);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
80
);
// Camera controls are implemented directly without Three.js addons.
const controls = {
target: new THREE.Vector3(0, 1.25, -0.35),
yaw: 0.34,
pitch: 0.48,
distance: 15.2,
dragging: false,
pointerX: 0,
pointerY: 0
};
function updateCamera() {
const cosPitch = Math.cos(controls.pitch);
camera.position.set(
controls.target.x + Math.sin(controls.yaw) * cosPitch * controls.distance,
controls.target.y + Math.sin(controls.pitch) * controls.distance,
controls.target.z + Math.cos(controls.yaw) * cosPitch * controls.distance
);
camera.lookAt(controls.target);
}
updateCamera();
canvas.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const dx = event.clientX - controls.pointerX;
const dy = event.clientY - controls.pointerY;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
controls.yaw -= dx * 0.0055;
controls.pitch = THREE.MathUtils.clamp(
controls.pitch + dy * 0.0045,
0.18,
1.05
);
updateCamera();
});
function endDrag(event) {
controls.dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
controls.distance = THREE.MathUtils.clamp(
controls.distance + event.deltaY * 0.012,
9,
22
);
updateCamera();
}, { passive: false });
// Lighting.
scene.add(new THREE.HemisphereLight("#fff0cc", "#5f4634", 2.25));
const sun = new THREE.DirectionalLight("#fff0cc", 2.5);
sun.position.set(-7, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0003;
scene.add(sun);
const warmFill = new THREE.DirectionalLight("#e59864", 0.7);
warmFill.position.set(8, 5, -7);
scene.add(warmFill);
// Shared materials.
const material = (color, options = {}) => new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.84,
metalness: options.metalness ?? 0,
flatShading: options.flatShading ?? true,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
side: options.side ?? THREE.FrontSide
});
const mats = {
stone: material("#b7a17e"),
stoneLight: material("#cdb994"),
stoneDark: material("#75634f"),
plaster: material("#d8c29a"),
recess: material("#4d4135"),
red: material("#984637"),
redDark: material("#642c25"),
roof: material("#a84f36"),
roofDark: material("#823a2d"),
wood: material("#765038"),
woodLight: material("#a7784e"),
woodDark: material("#483326"),
sack: material("#c7a36b"),
sackDark: material("#a87e4d"),
flour: material("#eee0be"),
bread: material("#c8833e"),
breadLight: material("#e1a95e"),
breadCut: material("#704025"),
charcoal: material("#29241f"),
iron: material("#46413a", { metalness: 0.35, roughness: 0.55 }),
leaf: material("#65794b"),
skin: material("#b87850"),
tunic: material("#d6c8a8"),
tunicRed: material("#9d4937"),
rope: material("#d0b277")
};
function addMesh(
geometry,
mat,
position = [0, 0, 0],
rotation = [0, 0, 0],
scale = [1, 1, 1],
parent = scene
) {
const object = new THREE.Mesh(geometry, mat);
object.position.set(...position);
object.rotation.set(...rotation);
object.scale.set(...scale);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function addBox(w, h, d, mat, x, y, z, parent = scene, rotation = [0, 0, 0]) {
return addMesh(
new THREE.BoxGeometry(w, h, d),
mat,
[x, y, z],
rotation,
[1, 1, 1],
parent
);
}
function addCylinder(
rt, rb, h, segments, mat,
x, y, z, parent = scene,
rotation = [0, 0, 0]
) {
return addMesh(
new THREE.CylinderGeometry(rt, rb, h, segments),
mat,
[x, y, z],
rotation,
[1, 1, 1],
parent
);
}
// Ground and tiled floor.
addBox(16, 0.28, 12, mats.stoneDark, 0, -0.2, 0);
const tileMaterials = [
material("#b49d78"),
material("#c1aa82"),
material("#a9906d"),
material("#c8b38c")
];
for (let x = 0; x < 14; x++) {
for (let z = 0; z < 9; z++) {
const tileMat = tileMaterials[(x + z * 3) % tileMaterials.length];
addBox(
1.08, 0.08, 1.08,
tileMat,
-7.25 + x * 1.11,
-0.015,
-4.45 + z * 1.1
);
}
}
// Back wall and painted arches.
addBox(15.2, 4.6, 0.42, mats.plaster, 0, 2.25, -4.72);
addBox(15.5, 0.42, 0.75, mats.stone, 0, 0.12, -4.48);
const archCenters = [-5.25, -1.75, 1.75, 5.25];
for (const x of archCenters) {
addMesh(
new THREE.CircleGeometry(1.1, 14),
mats.recess,
[x, 2.0, -4.49]
);
addMesh(
new THREE.TorusGeometry(1.11, 0.16, 5, 14, Math.PI),
mats.red,
[x, 2.0, -4.34]
);
addBox(2.0, 0.12, 0.22, mats.red, x, 0.94, -4.31);
}
const columnXs = [-6.35, -3.5, -0.75, 2, 4.75, 6.5];
for (const x of columnXs) {
addCylinder(0.22, 0.27, 2.05, 8, mats.stoneLight, x, 1.28, -4.15);
addCylinder(0.35, 0.35, 0.18, 8, mats.stone, x, 2.38, -4.15);
addCylinder(0.35, 0.35, 0.16, 8, mats.stone, x, 0.22, -4.15);
}
addBox(15.4, 0.42, 0.65, mats.redDark, 0, 4.45, -4.38);
// Low-poly tiled roof.
addBox(7.8, 0.22, 5.2, mats.roof, -3.75, 5.18, -4.55, scene, [0, 0, 0.27]);
addBox(7.8, 0.22, 5.2, mats.roofDark, 3.75, 5.18, -4.55, scene, [0, 0, -0.27]);
for (let i = 0; i < 12; i++) {
const x = -7.1 + i * 1.28;
addBox(0.11, 0.1, 5.0, mats.roofDark, x, 5.02 - Math.abs(x) * 0.055, -4.55,
scene, [0, 0, x < 0 ? 0.27 : -0.27]);
}
// Side walls.
addBox(0.42, 3.6, 8.6, mats.plaster, -7.45, 1.75, -0.55);
addBox(0.42, 3.6, 8.6, mats.plaster, 7.45, 1.75, -0.55);
// Decorative fresco panels.
for (const x of [-3.5, -0.75, 2, 4.75]) {
addBox(0.85, 1.1, 0.08, material("#b75e43"), x, 3.42, -4.43);
addBox(0.18, 0.84, 0.09, material("#d3aa64"), x - 0.18, 3.42, -4.36);
addBox(0.18, 0.84, 0.09, material("#d3aa64"), x + 0.18, 3.42, -4.36);
}
// Roman mill.
const mill = new THREE.Group();
mill.position.set(-2.35, 0, -0.65);
scene.add(mill);
addCylinder(1.0, 1.12, 0.38, 10, mats.stoneDark, 0, 0.18, 0, mill);
addCylinder(0.68, 0.88, 0.86, 10, mats.stoneLight, 0, 0.76, 0, mill);
addCylinder(0.42, 0.62, 0.64, 10, mats.stone, 0, 1.45, 0, mill);
addCylinder(0.68, 0.45, 0.22, 10, mats.stoneLight, 0, 1.83, 0, mill);
const millRotor = new THREE.Group();
millRotor.position.set(0, 1.62, 0.4);
mill.add(millRotor);
addBox(2.55, 0.16, 0.18, mats.woodDark, 0, 0, 0, millRotor);
addBox(0.45, 0.55, 0.16, mats.wood, 1.0, -0.2, 0, millRotor);
addBox(0.45, 0.55, 0.16, mats.wood, -1.0, -0.2, 0, millRotor);
addMesh(new THREE.SphereGeometry(0.24, 8, 6), mats.woodDark, [0, 0, 0], [0, 0, 0], [1, 1, 1], millRotor);
// Oven.
const oven = new THREE.Group();
oven.position.set(2.2, 0, -1.35);
scene.add(oven);
addCylinder(1.28, 1.35, 0.72, 12, mats.redDark, 0, 0.35, 0, oven);
addMesh(
new THREE.SphereGeometry(1.28, 12, 7, 0, Math.PI * 2, 0, Math.PI / 2),
mats.red,
[0, 0.72, 0],
[0, 0, 0],
[1, 1, 0.82],
oven
);
addBox(0.62, 0.8, 0.12, mats.charcoal, 0, 0.49, 1.08, oven);
addMesh(
new THREE.CircleGeometry(0.49, 12, 0, Math.PI),
mats.charcoal,
[0, 0.88, 1.1],
[0, 0, 0],
[1, 1, 1],
oven
);
addCylinder(0.28, 0.34, 1.45, 7, mats.redDark, 0.58, 2.08, -0.35, oven);
addCylinder(0.4, 0.4, 0.16, 7, mats.stoneDark, 0.58, 2.82, -0.35, oven);
for (let i = 0; i < 5; i++) {
addBox(
1.75, 0.08, 0.08,
mats.roofDark,
0,
0.32 + i * 0.28,
1.15,
oven,
[0, 0, (i % 2 ? 1 : -1) * 0.03]
);
}
// Fire and oven light.
const fire = [];
const flameMaterials = [
new THREE.MeshBasicMaterial({ color: "#ff7a24" }),
new THREE.MeshBasicMaterial({ color: "#ffd15a" })
];
for (let i = 0; i < 6; i++) {
const flame = addMesh(
new THREE.ConeGeometry(0.11, 0.43, 5),
flameMaterials[i % 2],
[-0.32 + i * 0.13, 0.35, 1.17],
[0, 0, 0],
[1, 1, 1],
oven
);
flame.castShadow = false;
fire.push(flame);
}
const ovenLight = new THREE.PointLight("#ff742d", 2.5, 5.5, 2);
ovenLight.position.set(2.2, 0.75, -0.1);
scene.add(ovenLight);
// Counter and awning.
const counter = new THREE.Group();
counter.position.set(0.8, 0, 2.35);
scene.add(counter);
addBox(3.45, 0.85, 1.12, mats.woodDark, 0, 0.43, 0, counter);
addBox(3.75, 0.18, 1.4, mats.woodLight, 0, 0.95, 0, counter);
addBox(3.3, 0.12, 0.08, mats.rope, 0, 0.5, 0.58, counter);
addCylinder(0.09, 0.09, 1.75, 7, mats.wood, -1.58, 1.72, 0.55, counter);
addCylinder(0.09, 0.09, 1.75, 7, mats.wood, 1.58, 1.72, 0.55, counter);
for (let i = 0; i < 7; i++) {
const awningMat = i % 2 === 0 ? mats.red : material("#ead8ad");
addBox(
0.54, 0.08, 1.65,
awningMat,
-1.62 + i * 0.54,
2.58,
0.13,
counter,
[0.08, 0, 0]
);
addBox(
0.54, 0.28, 0.08,
awningMat,
-1.62 + i * 0.54,
2.39,
0.91,
counter
);
}
// Painted counter sign.
const signCanvas = document.createElement("canvas");
signCanvas.width = 512;
signCanvas.height = 128;
const signContext = signCanvas.getContext("2d");
signContext.fillStyle = "#733126";
signContext.fillRect(0, 0, 512, 128);
signContext.strokeStyle = "#d8ad58";
signContext.lineWidth = 7;
signContext.strokeRect(8, 8, 496, 112);
signContext.fillStyle = "#f4dfb0";
signContext.font = "bold 48px Georgia";
signContext.textAlign = "center";
signContext.textBaseline = "middle";
signContext.fillText("PANIS • ROMA", 256, 64);
const signTexture = new THREE.CanvasTexture(signCanvas);
signTexture.colorSpace = THREE.SRGBColorSpace;
addMesh(
new THREE.PlaneGeometry(2.5, 0.62),
new THREE.MeshBasicMaterial({ map: signTexture }),
[0, 1.87, -0.32],
[0, 0, 0],
[1, 1, 1],
counter
);
// Bread basket.
addBox(1.35, 0.17, 0.75, mats.rope, 0.35, 1.08, 0, counter);
for (let i = 0; i < 5; i++) {
addBox(
0.05, 0.25, 0.78,
mats.woodLight,
-0.25 + i * 0.3,
1.22,
0,
counter
);
}
// Grain delivery cart.
const cart = new THREE.Group();
const CART_X = -4.9;
const CART_Z = 1.45;
cart.position.set(CART_X - 4, 0, CART_Z);
scene.add(cart);
addBox(1.85, 0.2, 1.15, mats.woodDark, 0, 0.62, 0, cart);
addBox(1.85, 0.45, 0.1, mats.wood, 0, 0.88, -0.52, cart);
addBox(1.85, 0.45, 0.1, mats.wood, 0, 0.88, 0.52, cart);
addBox(0.1, 0.45, 1.05, mats.wood, -0.88, 0.88, 0, cart);
addBox(0.1, 0.45, 1.05, mats.wood, 0.88, 0.88, 0, cart);
addBox(0.12, 0.12, 1.9, mats.woodDark, -0.55, 0.54, 0.25, cart, [0.35, 0, 0]);
addBox(0.12, 0.12, 1.9, mats.woodDark, 0.55, 0.54, 0.25, cart, [0.35, 0, 0]);
const cartWheels = [];
for (const x of [-0.78, 0.78]) {
for (const z of [-0.62, 0.62]) {
const wheel = addCylinder(
0.3, 0.3, 0.14, 10,
mats.woodDark,
x, 0.33, z, cart,
[0, 0, Math.PI / 2]
);
cartWheels.push(wheel);
}
}
function makeSack(scale = 1, parent = scene) {
const sack = new THREE.Group();
parent.add(sack);
addCylinder(0.17, 0.23, 0.43, 7, mats.sack, 0, 0.22, 0, sack, [0, 0, 0]);
addCylinder(0.12, 0.17, 0.12, 7, mats.sackDark, 0, 0.48, 0, sack);
addCylinder(0.125, 0.125, 0.035, 7, mats.rope, 0, 0.55, 0, sack);
sack.scale.setScalar(scale);
return sack;
}
const cartSacks = [];
for (let i = 0; i < 12; i++) {
const sack = makeSack(0.73, cart);
const column = i % 4;
const row = Math.floor(i / 4);
sack.position.set(
-0.63 + column * 0.42,
0.72 + row * 0.35,
-0.18 + (row % 2) * 0.25
);
sack.rotation.y = (i % 3) * 0.13;
cartSacks.push(sack);
}
// Amphorae and flour jars.
function makeAmphora(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
addCylinder(0.22, 0.12, 0.56, 8, mats.redDark, 0, 0.3, 0, group);
addMesh(new THREE.SphereGeometry(0.37, 8, 6), mats.red, [0, 0.68, 0], [0, 0, 0], [1, 0.8, 1], group);
addCylinder(0.14, 0.19, 0.35, 8, mats.red, 0, 1.0, 0, group);
addCylinder(0.2, 0.13, 0.09, 8, mats.redDark, 0, 1.2, 0, group);
}
makeAmphora(-6.3, -3.4, 1.1);
makeAmphora(5.7, 3.55, 0.9);
// Stylized workers.
const workers = [];
function makeWorker(x, z, color, rotation = 0) {
const worker = new THREE.Group();
worker.position.set(x, 0, z);
worker.rotation.y = rotation;
scene.add(worker);
addCylinder(0.25, 0.37, 0.83, 7, color, 0, 0.73, 0, worker);
addMesh(new THREE.SphereGeometry(0.24, 8, 6), mats.skin, [0, 1.39, 0], [0, 0, 0], [1, 1, 1], worker);
addCylinder(0.27, 0.27, 0.08, 8, mats.woodDark, 0, 1.58, 0, worker);
addCylinder(0.08, 0.08, 0.54, 6, color, -0.31, 0.88, 0, worker, [0, 0, -0.26]);
addCylinder(0.08, 0.08, 0.54, 6, color, 0.31, 0.88, 0, worker, [0, 0, 0.26]);
addCylinder(0.1, 0.09, 0.55, 6, mats.tunic, -0.15, 0.18, 0, worker);
addCylinder(0.1, 0.09, 0.55, 6, mats.tunic, 0.15, 0.18, 0, worker);
workers.push(worker);
return worker;
}
makeWorker(-1.05, -0.05, mats.tunic, 0.4);
makeWorker(3.65, -0.2, mats.tunicRed, -1.2);
makeWorker(2.55, 2.45, mats.tunic, -0.5);
// Smoke puffs.
const smoke = [];
for (let i = 0; i < 10; i++) {
const puff = addMesh(
new THREE.SphereGeometry(0.12, 6, 4),
new THREE.MeshBasicMaterial({
color: "#eee2c9",
transparent: true,
opacity: 0,
depthWrite: false
}),
[2.78, 2.8, -1.7],
[0, 0, 0],
[1, 1, 1],
scene
);
puff.castShadow = false;
puff.receiveShadow = false;
smoke.push({ mesh: puff, phase: i / 10 });
}
// Simulation state.
const state = {
status: "idle", // idle, running, done
paused: false,
deliverySacks: 12,
yieldPerBatch: 4,
temperature: 225,
potential: 3,
processed: 0,
consumed: 0,
counter: 0,
lost: 0,
elapsed: 0,
active: null,
deliveredGroups: [],
nextDelay: 0,
endDelay: 0,
cartProgress: 0,
logItems: []
};
const DELIVERY_POINT = new THREE.Vector3(-4.75, 0.26, 1.92);
const MILL_POINT = new THREE.Vector3(-2.35, 0.34, -0.12);
const OVEN_POINT = new THREE.Vector3(2.2, 0.45, -0.02);
const DISCARD_POINT = new THREE.Vector3(5.2, 0.22, -0.65);
function counterPosition(index) {
return new THREE.Vector3(
0.8 + ((index % 4) - 1.5) * 0.35,
1.2,
2.27 + Math.floor(index / 4) * 0.12
);
}
function qualityChance() {
const distance = Math.abs(state.temperature - 235);
return THREE.MathUtils.clamp(1 - distance / 110, 0.18, 0.98);
}
function makeLoaf(parent, materialOverride = null) {
const loaf = new THREE.Group();
parent.add(loaf);
const loafMat = materialOverride || mats.bread;
addMesh(
new THREE.SphereGeometry(0.5, 10, 6),
loafMat,
[0, 0, 0],
[0, 0, 0],
[0.78, 0.32, 0.38],
loaf
);
for (let i = 0; i < 3; i++) {
addBox(
0.025, 0.015, 0.16,
mats.breadCut,
-0.13 + i * 0.13,
0.145,
0,
loaf,
[0, 0, -0.25 + i * 0.2]
);
}
return loaf;
}
function createBatchVisual(batchNumber, sackCount) {
const group = new THREE.Group();
scene.add(group);
const deliveryGroup = new THREE.Group();
const flourGroup = new THREE.Group();
const breadGroup = new THREE.Group();
group.add(deliveryGroup, flourGroup, breadGroup);
for (let i = 0; i < sackCount; i++) {
const sack = makeSack(0.72, deliveryGroup);
sack.position.set(
(i % 2 - 0.5) * 0.38,
i >= 2 ? 0.34 : 0,
(Math.floor(i / 2) - 0.5) * 0.36
);
sack.rotation.y = i * 0.19;
}
addCylinder(0.24, 0.29, 0.48, 7, mats.flour, 0, 0.25, 0, flourGroup);
addMesh(
new THREE.SphereGeometry(0.25, 8, 5),
mats.flour,
[0, 0.49, 0],
[0, 0, 0],
[1, 0.45, 1],
flourGroup
);
for (let i = 0; i < 3; i++) {
const loaf = makeLoaf(breadGroup);
loaf.position.set((i - 1) * 0.42, 0.18, (i % 2) * 0.12);
loaf.rotation.y = i * 0.28;
}
flourGroup.visible = false;
breadGroup.visible = false;
return { group, deliveryGroup, flourGroup, breadGroup };
}
// DOM references.
const ui = {
grain: document.querySelector("#grainStat"),
flour: document.querySelector("#flourStat"),
counter: document.querySelector("#counterStat"),
lost: document.querySelector("#lostStat"),
deliveryHint: document.querySelector("#deliveryHint"),
yieldHint: document.querySelector("#yieldHint"),
heatHint: document.querySelector("#heatHint"),
temperatureValue: document.querySelector("#temperatureValue"),
quality: document.querySelector("#qualityValue"),
previewText: document.querySelector("#previewText"),
previewNumber: document.querySelector("#previewNumber"),
main: document.querySelector("#mainButton"),
reset: document.querySelector("#resetButton"),
settings: document.querySelector("#settings"),
summary: document.querySelector("#summary"),
summaryTitle: document.querySelector("#summaryTitle"),
summaryText: document.querySelector("#summaryText"),
progressText: document.querySelector("#progressText"),
progressFill: document.querySelector("#progressFill"),
log: document.querySelector("#logLines"),
counterLabel: document.querySelector("#counterLabel"),
millLabel: document.querySelector("#millLabel"),
ovenLabel: document.querySelector("#ovenLabel"),
stages: [...document.querySelectorAll(".stage")]
};
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60).toString().padStart(2, "0");
const remainder = Math.floor(seconds % 60).toString().padStart(2, "0");
return `${minutes}:${remainder}`;
}
function addLog(message, tone = "") {
state.logItems.push({ message, tone, time: formatTime(state.elapsed) });
if (state.logItems.length > 5) state.logItems.shift();
ui.log.replaceChildren();
for (const item of state.logItems) {
const line = document.createElement("div");
line.className = `log-line ${item.tone}`;
line.textContent = `${item.time} ${item.message}`;
ui.log.appendChild(line);
}
}
function updatePreview() {
state.potential = Math.floor(state.deliverySacks / state.yieldPerBatch);
const chance = Math.round(qualityChance() * 100);
ui.grain.textContent = `${state.deliverySacks} sacks`;
ui.flour.textContent = `${state.potential} batches`;
ui.counter.textContent = state.counter;
ui.lost.textContent = state.lost;
ui.counterLabel.textContent = `Counter · ${state.counter}`;
ui.deliveryHint.textContent = state.deliverySacks === 6
? "Small cart"
: state.deliverySacks === 9
? "Half wagon"
: "Full wagon";
ui.yieldHint.textContent = `${state.yieldPerBatch} sacks / batch`;
ui.heatHint.textContent = `${state.temperature}°C`;
ui.temperatureValue.textContent = `${state.temperature}°C`;
ui.quality.textContent = `${chance}%`;
ui.previewText.textContent =
`${state.deliverySacks} sacks → ${state.potential} batches`;
ui.previewNumber.textContent = state.potential;
}
function selectButton(container, attribute, value) {
for (const button of container.querySelectorAll("button")) {
button.classList.toggle("selected", button.dataset[attribute] === String(value));
}
}
document.querySelector("#deliveryOptions").addEventListener("click", (event) => {
const button = event.target.closest("button");
if (!button || state.status !== "idle") return;
state.deliverySacks = Number(button.dataset.sacks);
selectButton(event.currentTarget, "sacks", state.deliverySacks);
updatePreview();
});
document.querySelector("#yieldOptions").addEventListener("click", (event) => {
const button = event.target.closest("button");
if (!button || state.status !== "idle") return;
state.yieldPerBatch = Number(button.dataset.yield);
selectButton(event.currentTarget, "yield", state.yieldPerBatch);
updatePreview();
});
document.querySelector("#temperature").addEventListener("input", (event) => {
state.temperature = Number(event.target.value);
updatePreview();
});
function updateStageUI(stageName = null) {
const stageMap = {
delivery: 0,
toMill: 1,
mill: 1,
toOven: 2,
oven: 2,
toDiscard: 2,
toCounter: 3,
counter: 3
};
const activeIndex = stageMap[stageName] ?? -1;
ui.stages.forEach((node, index) => {
node.classList.toggle("active", index === activeIndex);
node.classList.toggle("done", activeIndex >= 0 && index < activeIndex);
});
}
function updateProgress() {
if (state.status === "idle") {
ui.progressText.textContent = "Awaiting delivery";
ui.progressFill.style.width = "0%";
return;
}
if (state.status === "done") {
ui.progressText.textContent = "Shift complete";
ui.progressFill.style.width = "100%";
updateStageUI("counter");
return;
}
let current = state.processed;
if (state.active) {
current += THREE.MathUtils.clamp(state.active.t / state.active.duration, 0, 1);
}
const percent = state.potential ? Math.min(100, current / state.potential * 100) : 0;
ui.progressFill.style.width = `${percent}%`;
const labels = {
delivery: "Unloading grain",
toMill: "Grinding flour",
mill: "Grinding flour",
toOven: "Loading the oven",
oven: "Baking bread",
toDiscard: "Batch rejected",
toCounter: "Carrying to counter",
counter: "Counting at counter"
};
ui.progressText.textContent = labels[state.active?.stage] || "Preparing batches";
}
function beginStage(batch, stage) {
batch.stage = stage;
batch.t = 0;
batch.from = batch.group.position.clone();
const durations = {
delivery: 0.85,
toMill: 0.65,
mill: 1.25,
toOven: 0.65,
oven: 1.25,
toCounter: 0.85,
counter: 0.35,
toDiscard: 0.9
};
batch.duration = durations[stage];
if (stage === "delivery") {
batch.group.position.copy(DELIVERY_POINT);
} else if (stage === "toMill") {
batch.target = MILL_POINT.clone();
} else if (stage === "mill") {
batch.group.position.copy(MILL_POINT);
batch.deliveryGroup.visible = false;
batch.flourGroup.visible = true;
} else if (stage === "toOven") {
batch.target = OVEN_POINT.clone();
} else if (stage === "oven") {
batch.group.position.copy(OVEN_POINT);
batch.flourGroup.visible = false;
batch.breadGroup.visible = true;
} else if (stage === "toCounter") {
batch.target = counterPosition(state.counter);
} else if (stage === "counter") {
batch.group.position.copy(counterPosition(state.counter - 1));
} else if (stage === "toDiscard") {
batch.target = DISCARD_POINT.clone();
}
updateStageUI(stage);
}
function advanceStage(batch) {
switch (batch.stage) {
case "delivery":
beginStage(batch, "toMill");
break;
case "toMill":
beginStage(batch, "mill");
addLog(`Batch ${batch.number}: ${state.yieldPerBatch} sacks ground to flour.`);
break;
case "mill":
beginStage(batch, "toOven");
break;
case "toOven":
beginStage(batch, "oven");
addLog(`Batch ${batch.number} entered the oven.`);
break;
case "oven": {
const success = Math.random() < qualityChance();
batch.success = success;
if (!success) {
batch.breadGroup.traverse((child) => {
if (child.isMesh && child.material === mats.bread) {
child.material = mats.charcoal;
}
});
beginStage(batch, "toDiscard");
} else {
beginStage(batch, "toCounter");
}
break;
}
case "toCounter":
state.counter += 1;
batch.group.position.copy(counterPosition(state.counter - 1));
updatePreview();
addLog(`Batch ${batch.number} reached the counter.`, "good");
beginStage(batch, "counter");
break;
case "counter":
state.active = null;
state.deliveredGroups.push(batch.group);
if (state.processed >= state.potential) {
state.endDelay = 0.65;
} else {
state.nextDelay = 0.35;
}
break;
case "toDiscard":
state.lost += 1;
state.active = null;
scene.remove(batch.group);
updatePreview();
addLog(`Batch ${batch.number} was lost; it never reached the counter.`, "bad");
if (state.processed >= state.potential) {
state.endDelay = 0.65;
} else {
state.nextDelay = 0.35;
}
break;
}
}
function updateBatch(dt) {
const batch = state.active;
if (!batch) return;
batch.t += dt;
const u = THREE.MathUtils.clamp(batch.t / batch.duration, 0, 1);
if (batch.target) {
const smooth = u * u * (3 - 2 * u);
batch.group.position.lerpVectors(batch.from, batch.target, smooth);
}
if (u >= 1) {
advanceStage(batch);
}
}
function startBatch() {
const batchNumber = state.processed + 1;
const visual = createBatchVisual(batchNumber, state.yieldPerBatch);
visual.group.position.copy(DELIVERY_POINT);
state.active = {
number: batchNumber,
group: visual.group,
deliveryGroup: visual.deliveryGroup,
flourGroup: visual.flourGroup,
breadGroup: visual.breadGroup,
stage: "delivery",
t: 0,
duration: 0.85,
from: DELIVERY_POINT.clone(),
target: null,
success: false
};
state.processed += 1;
state.consumed += state.yieldPerBatch;
cartSacks.forEach((sack, index) => {
sack.visible = index >= state.consumed;
});
addLog(
`Batch ${batchNumber}: ${state.yieldPerBatch} sacks taken to the mill.`
);
beginStage(state.active, "delivery");
}
function completeShift() {
state.status = "done";
state.paused = false;
state.active = null;
ui.settings.disabled = false;
ui.main.textContent = "Run again";
ui.main.classList.remove("paused");
ui.summary.hidden = false;
const leftover = state.deliverySacks - state.consumed;
ui.summaryTitle.textContent = `${state.counter} batches reached the counter`;
ui.summaryText.textContent =
`${state.counter} of ${state.potential} possible batches were counted. ` +
(leftover > 0
? `${leftover} sacks remained too coarse for a full batch.`
: "The delivery was fully reduced into flour batches.");
addLog(
`Shift finished: ${state.counter} counted, ${state.lost} lost.`,
state.counter ? "good" : "bad"
);
updateProgress();
}
function startShift() {
resetSimulation(false);
state.status = "running";
state.paused = false;
state.cartProgress = 0;
state.nextDelay = 0.55;
state.endDelay = 0;
ui.settings.disabled = true;
ui.main.textContent = "Pause shift";
ui.main.classList.remove("paused");
ui.summary.hidden = true;
addLog(`${state.deliverySacks} sacks arrived from the fictional river granary.`);
updatePreview();
updateProgress();
}
function resetSimulation(resetSettings = true) {
if (state.active) scene.remove(state.active.group);
for (const group of state.deliveredGroups) scene.remove(group);
state.status = "idle";
state.paused = false;
state.processed = 0;
state.consumed = 0;
state.counter = 0;
state.lost = 0;
state.elapsed = 0;
state.active = null;
state.deliveredGroups = [];
state.nextDelay = 0;
state.endDelay = 0;
state.cartProgress = 0;
cartSacks.forEach((sack) => { sack.visible = true; });
cart.position.set(CART_X - 4, 0, CART_Z);
ui.settings.disabled = false;
ui.main.textContent = "Start shift";
ui.main.classList.remove("paused");
ui.summary.hidden = true;
ui.log.replaceChildren();
state.logItems = [];
if (resetSettings) {
state.deliverySacks = 12;
state.yieldPerBatch = 4;
state.temperature = 225;
document.querySelector("#temperature").value = "225";
selectButton(document.querySelector("#deliveryOptions"), "sacks", 12);
selectButton(document.querySelector("#yieldOptions"), "yield", 4);
}
updatePreview();
updateStageUI(null);
updateProgress();
}
ui.main.addEventListener("click", () => {
if (state.status === "idle" || state.status === "done") {
startShift();
} else if (state.status === "running") {
state.paused = !state.paused;
ui.main.textContent = state.paused ? "Resume shift" : "Pause shift";
ui.main.classList.toggle("paused", state.paused);
addLog(state.paused ? "The bakery paused." : "The bakery resumed.");
}
});
ui.reset.addEventListener("click", () => resetSimulation(true));
// World-space labels.
const labelData = [
{ element: ui.millLabel, point: new THREE.Vector3(-2.35, 2.25, -0.65) },
{ element: ui.ovenLabel, point: new THREE.Vector3(2.2, 2.2, -1.35) },
{ element: ui.counterLabel, point: new THREE.Vector3(0.8, 2.75, 2.35) }
];
function updateWorldLabels() {
for (const label of labelData) {
const projected = label.point.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
let lastTime = performance.now();
let visualTime = 0;
function animate(now) {
const dt = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
visualTime += dt;
// Ambient bakery motion.
millRotor.rotation.z += dt * (state.active?.stage === "mill" ? 2.4 : 0.32);
ovenLight.intensity = 2.1 + Math.sin(visualTime * 9.1) * 0.45 +
Math.sin(visualTime * 13.7) * 0.18;
fire.forEach((flame, index) => {
const flicker = 0.85 + Math.sin(visualTime * 11 + index * 1.7) * 0.18;
flame.scale.y = flicker;
flame.rotation.y += dt * 0.3;
});
for (const worker of workers) {
worker.position.y = Math.sin(visualTime * 2.2 + worker.position.x) * 0.012;
}
cartWheels.forEach((wheel) => {
wheel.rotation.x += dt * 0.6;
});
smoke.forEach((puff, index) => {
const phase = (visualTime * 0.19 + puff.phase) % 1;
puff.mesh.position.set(
2.78 + Math.sin(index * 2.1 + phase * 4) * 0.22,
2.9 + phase * 2.0,
-1.7 + Math.cos(index + phase * 3) * 0.13
);
puff.mesh.scale.setScalar(0.65 + phase * 1.8);
puff.mesh.material.opacity = Math.sin(phase * Math.PI) * 0.22;
});
if (state.status === "running" && !state.paused) {
state.elapsed += dt;
if (state.cartProgress < 1) {
state.cartProgress = Math.min(1, state.cartProgress + dt * 0.75);
const smooth = state.cartProgress * state.cartProgress * (3 - 2 * state.cartProgress);
cart.position.x = THREE.MathUtils.lerp(CART_X - 4, CART_X, smooth);
}
if (state.active) {
updateBatch(dt);
} else if (state.processed < state.potential) {
state.nextDelay -= dt;
if (state.nextDelay <= 0) startBatch();
} else {
state.endDelay -= dt;
if (state.endDelay <= 0) completeShift();
}
updateProgress();
}
updateWorldLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
resetSimulation(true);
</script>
</body>
</html>
```