Prompt specification
Build or customize the Three.js 3D scene "Small grain-to-flour workshop" 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: Small grain-to-flour workshop
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/small-grain-to-flour-workshop-x1083
Scene Description Prompt:
Please make a stylized Three.js scene in one standalone HTML file using cdnjs. Compose grain sacks, mill, sieve table, flour bins, and bakery doorway; trace one batch through changing forms while material quantities remain conceptually connected.
Working Three.js Starter (small-grain-to-flour-workshop-x1083):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>One Batch, Many Forms</title>
<style>
:root {
--cream: #f6ead3;
--muted: #c0b9a8;
--gold: #e9b858;
--flour: #f1d9aa;
--bran: #a86b37;
--green: #9bc49b;
--panel: rgba(19, 28, 31, 0.88);
--border: rgba(246, 234, 211, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #182229;
color: var(--cream);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
.brand {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.45);
}
.eyebrow {
margin-bottom: 7px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
max-width: 520px;
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 4vw, 56px);
font-weight: 500;
line-height: 0.96;
letter-spacing: -0.045em;
}
.subtitle {
margin-top: 10px;
color: #ddd4c2;
font-size: 12px;
letter-spacing: 0.03em;
}
#current {
position: fixed;
top: 158px;
left: 26px;
z-index: 5;
width: 275px;
padding: 15px 17px;
border-radius: 14px;
pointer-events: none;
}
.current-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
}
#current-kicker {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.balanced {
padding: 4px 7px;
border: 1px solid rgba(155, 196, 155, 0.35);
border-radius: 999px;
color: var(--green);
background: rgba(155, 196, 155, 0.09);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
white-space: nowrap;
}
#current-title {
font-family: Georgia, "Times New Roman", serif;
font-size: 23px;
font-weight: 500;
}
#current-copy {
margin-top: 6px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
#ledger {
position: fixed;
top: 24px;
right: 24px;
z-index: 5;
width: 315px;
padding: 17px;
border-radius: 16px;
pointer-events: none;
}
.ledger-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
margin-bottom: 12px;
}
.ledger-title {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.ledger-title strong {
display: block;
margin-top: 4px;
color: var(--cream);
font-size: 12px;
letter-spacing: 0;
text-transform: none;
}
.ledger-total {
color: var(--gold);
font-family: Georgia, "Times New Roman", serif;
font-size: 27px;
line-height: 1;
text-align: right;
}
.ledger-total span {
display: block;
margin-top: 5px;
color: var(--muted);
font-family: Inter, ui-sans-serif, system-ui, sans-serif;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.split-bar {
display: flex;
height: 8px;
overflow: hidden;
margin: 4px 0 15px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
.split-bar span:first-child {
width: 75%;
background: var(--flour);
}
.split-bar span:last-child {
width: 25%;
background: var(--bran);
}
.split-legend {
display: flex;
justify-content: space-between;
margin: -9px 0 14px;
color: var(--muted);
font-size: 9px;
}
.chain {
position: relative;
}
.chain-row {
position: relative;
display: grid;
grid-template-columns: 16px 1fr auto;
align-items: center;
gap: 8px;
min-height: 31px;
padding: 4px 6px;
border-radius: 7px;
color: #a9a396;
transition: color 180ms ease, background 180ms ease;
}
.chain-row::before {
content: "";
position: absolute;
left: 13px;
top: 23px;
width: 1px;
height: 16px;
background: rgba(246, 234, 211, 0.14);
}
.chain-row:last-child::before {
display: none;
}
.chain-row i {
position: relative;
z-index: 1;
width: 7px;
height: 7px;
margin-left: 4px;
border: 1px solid rgba(246, 234, 211, 0.35);
border-radius: 50%;
background: #1b272c;
}
.chain-row span {
font-size: 10px;
}
.chain-row b {
color: inherit;
font-size: 10px;
font-weight: 750;
white-space: nowrap;
}
.chain-row.active {
color: var(--cream);
background: rgba(233, 184, 88, 0.09);
}
.chain-row.active i {
border-color: var(--gold);
background: var(--gold);
box-shadow: 0 0 11px rgba(233, 184, 88, 0.65);
}
.ledger-note {
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--border);
color: #918d83;
font-size: 9px;
line-height: 1.45;
}
#timeline-panel {
position: fixed;
left: 50%;
bottom: 22px;
z-index: 10;
width: min(940px, calc(100vw - 48px));
padding: 13px 15px 12px;
border-radius: 17px;
transform: translateX(-50%);
}
.transport {
display: grid;
grid-template-columns: auto 1fr auto auto;
align-items: center;
gap: 12px;
}
.icon-button,
#speed {
height: 35px;
border: 1px solid var(--border);
border-radius: 9px;
color: var(--cream);
background: rgba(255, 255, 255, 0.055);
font: inherit;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
}
.icon-button {
width: 39px;
font-size: 15px;
}
#speed {
min-width: 53px;
padding: 0 10px;
font-size: 10px;
font-weight: 800;
}
.icon-button:hover,
#speed:hover {
background: rgba(233, 184, 88, 0.15);
transform: translateY(-1px);
}
#scrubber {
width: 100%;
height: 5px;
margin: 0;
accent-color: var(--gold);
cursor: pointer;
}
#time {
min-width: 83px;
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
text-align: right;
}
.stages {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 6px;
margin-top: 11px;
}
.stage-chip {
position: relative;
min-height: 32px;
overflow: hidden;
padding: 7px 8px;
border: 1px solid rgba(246, 234, 211, 0.09);
border-radius: 8px;
color: #a9a396;
background: rgba(255, 255, 255, 0.025);
font: inherit;
font-size: 9px;
font-weight: 700;
text-align: left;
cursor: pointer;
transition: color 150ms ease, border-color 150ms ease;
}
.stage-chip::before {
content: "";
position: absolute;
inset: auto 0 0;
height: 2px;
background: var(--gold);
transform: scaleX(var(--fill, 0));
transform-origin: left;
opacity: 0.85;
}
.stage-chip.active {
color: var(--cream);
border-color: rgba(233, 184, 88, 0.35);
background: rgba(233, 184, 88, 0.08);
}
.stage-chip:hover {
color: var(--cream);
border-color: rgba(233, 184, 88, 0.3);
}
.hint {
position: fixed;
right: 26px;
bottom: 128px;
z-index: 4;
color: rgba(246, 234, 211, 0.58);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
text-shadow: 0 2px 8px #000;
}
@media (max-width: 820px) {
#ledger {
top: 18px;
right: 14px;
width: 270px;
}
.brand {
top: 18px;
left: 16px;
}
h1 {
max-width: 330px;
font-size: 34px;
}
.subtitle {
display: none;
}
#current {
top: 112px;
left: 16px;
}
}
@media (max-width: 620px) {
.brand {
max-width: calc(100vw - 32px);
}
h1 {
max-width: 250px;
font-size: 29px;
}
#ledger {
top: 95px;
right: 12px;
width: 245px;
padding: 12px;
}
.ledger-note,
.split-legend {
display: none;
}
.chain-row {
min-height: 27px;
}
#current {
display: none;
}
#timeline-panel {
width: calc(100vw - 20px);
bottom: 10px;
padding: 10px;
}
.transport {
grid-template-columns: auto 1fr auto;
gap: 8px;
}
#speed {
display: none;
}
#time {
min-width: 69px;
font-size: 9px;
}
.stages {
grid-template-columns: repeat(3, 1fr);
}
.stage-chip {
min-height: 27px;
padding: 5px 6px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive stylized bakery diorama"></canvas>
<header class="brand">
<div class="eyebrow">Millhouse No. 04 · Batch 01</div>
<h1>One batch,<br>many forms.</h1>
<div class="subtitle">A material-continuous journey from grain to bakery</div>
</header>
<section id="current" class="panel">
<div class="current-top">
<span id="current-kicker">Stage 01 / 06</span>
<span class="balanced">Balanced</span>
</div>
<div id="current-title">Whole grain</div>
<div id="current-copy">
The original batch is measured and ready to travel.
</div>
</section>
<aside id="ledger" class="panel">
<div class="ledger-head">
<div class="ledger-title">
Conserved batch basis
<strong>Material stays connected</strong>
</div>
<div class="ledger-total">
24.0
<span>kg total</span>
</div>
</div>
<div class="split-bar" title="18 kg flour and 6 kg bran">
<span></span><span></span>
</div>
<div class="split-legend">
<span>18 kg flour</span>
<span>6 kg bran</span>
</div>
<div class="chain">
<div class="chain-row" data-stages="0">
<i></i><span>Whole grain</span><b>24.0 kg</b>
</div>
<div class="chain-row" data-stages="1">
<i></i><span>Stone-ground meal</span><b>24.0 kg</b>
</div>
<div class="chain-row" data-stages="2,3">
<i></i><span>Flour + bran</span><b>18.0 + 6.0</b>
</div>
<div class="chain-row" data-stages="4">
<i></i><span>Combined dough</span><b>24.0 kg</b>
</div>
<div class="chain-row" data-stages="5">
<i></i><span>Baked batch</span><b>24.0 kg</b>
</div>
</div>
<div class="ledger-note">
Illustrative dry-basis quantities. The sieve separates but never creates
or loses material.
</div>
</aside>
<div class="hint">Drag to orbit · Wheel to zoom · Space to pause</div>
<section id="timeline-panel" class="panel">
<div class="transport">
<button id="play" class="icon-button" aria-label="Pause animation">Ⅱ</button>
<input
id="scrubber"
type="range"
min="0"
max="1000"
value="0"
step="1"
aria-label="Batch timeline"
>
<div id="time">00:00 / 00:28</div>
<button id="speed" class="icon-button" aria-label="Change playback speed">1×</button>
</div>
<div class="stages">
<button class="stage-chip active" data-stage="0">01 · Sacks</button>
<button class="stage-chip" data-stage="1">02 · Mill</button>
<button class="stage-chip" data-stage="2">03 · Sieve</button>
<button class="stage-chip" data-stage="3">04 · Bins</button>
<button class="stage-chip" data-stage="4">05 · Dough</button>
<button class="stage-chip" data-stage="5">06 · Bakery</button>
</div>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer, camera, lighting
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x182229);
scene.fog = new THREE.Fog(0x182229, 13, 24);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const orbit = {
target: new THREE.Vector3(0, 1.1, 0),
yaw: 0.58,
pitch: 0.51,
radius: 12.8,
desiredYaw: 0.58,
desiredPitch: 0.51,
desiredRadius: 12.8,
dragging: false,
pointerX: 0,
pointerY: 0
};
function updateCamera() {
const cp = Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + orbit.radius * Math.sin(orbit.yaw) * cp,
orbit.target.y + orbit.radius * Math.sin(orbit.pitch),
orbit.target.z + orbit.radius * Math.cos(orbit.yaw) * cp
);
camera.lookAt(orbit.target);
}
updateCamera();
canvas.addEventListener("pointerdown", (event) => {
orbit.dragging = true;
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.pointerX;
const dy = event.clientY - orbit.pointerY;
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
orbit.desiredYaw -= dx * 0.005;
orbit.desiredPitch = THREE.MathUtils.clamp(
orbit.desiredPitch + dy * 0.004,
0.18,
1.15
);
});
function releasePointer(event) {
orbit.dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredRadius = THREE.MathUtils.clamp(
orbit.desiredRadius + event.deltaY * 0.008,
7.5,
17
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
orbit.desiredYaw = 0.58;
orbit.desiredPitch = 0.51;
orbit.desiredRadius = 12.8;
});
const hemisphere = new THREE.HemisphereLight(0xcfe4ed, 0x72513b, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffd9a1, 3.5);
sun.position.set(-3.5, 9, 6.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.0005;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0x9fc8df, 1.1);
coolFill.position.set(6, 5, -2);
scene.add(coolFill);
// ---------------------------------------------------------------------
// Procedural textures and helpers
// ---------------------------------------------------------------------
function seededRandom(seed) {
return function () {
seed |= 0;
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(1824);
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeWoodTexture() {
const c = document.createElement("canvas");
c.width = c.height = 512;
const ctx = c.getContext("2d");
ctx.fillStyle = "#8c5a36";
ctx.fillRect(0, 0, 512, 512);
for (let y = 0; y < 512; y += 64) {
ctx.fillStyle = `rgba(${random() > 0.5 ? "65,34,18" : "155,94,51"}, 0.25)`;
ctx.fillRect(0, y, 512, 64);
ctx.fillStyle = "rgba(43,25,16,0.42)";
ctx.fillRect(0, y, 512, 3);
}
for (let i = 0; i < 180; i++) {
ctx.strokeStyle = `rgba(46,26,15,${0.03 + random() * 0.1})`;
ctx.lineWidth = 0.5 + random() * 2;
ctx.beginPath();
const y = random() * 512;
ctx.moveTo(0, y);
ctx.bezierCurveTo(
130, y + random() * 12 - 6,
350, y + random() * 14 - 7,
512, y + random() * 8 - 4
);
ctx.stroke();
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(2, 1);
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function makeBurlapTexture() {
const c = document.createElement("canvas");
c.width = c.height = 256;
const ctx = c.getContext("2d");
ctx.fillStyle = "#b28b56";
ctx.fillRect(0, 0, 256, 256);
for (let i = 0; i < 256; i += 4) {
ctx.strokeStyle = i % 8 === 0
? "rgba(75,49,28,0.16)"
: "rgba(255,224,163,0.13)";
ctx.beginPath();
ctx.moveTo(i, 0);
ctx.lineTo(i, 256);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i);
ctx.lineTo(256, i);
ctx.stroke();
}
for (let i = 0; i < 550; i++) {
ctx.fillStyle = random() > 0.5
? "rgba(67,43,24,0.09)"
: "rgba(255,235,190,0.10)";
ctx.fillRect(random() * 256, random() * 256, 1, 2);
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(3, 3);
return texture;
}
function makePlankTexture() {
const c = document.createElement("canvas");
c.width = c.height = 512;
const ctx = c.getContext("2d");
ctx.fillStyle = "#76685a";
ctx.fillRect(0, 0, 512, 512);
for (let row = 0; row < 8; row++) {
const y = row * 64;
ctx.fillStyle = `rgba(${random() > 0.5 ? "78,66,53" : "105,91,75"}, 0.8)`;
ctx.fillRect(0, y, 512, 63);
ctx.fillStyle = "rgba(32,27,25,0.35)";
ctx.fillRect(0, y, 512, 2);
ctx.fillRect((row * 83) % 512, y, 2, 64);
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(3, 2);
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function makeLabel(text, subtitle = "", accent = "#e9b858") {
const c = document.createElement("canvas");
c.width = 512;
c.height = 150;
const ctx = c.getContext("2d");
roundedRect(ctx, 4, 4, 504, 142, 20);
ctx.fillStyle = "rgba(24,34,39,0.94)";
ctx.fill();
ctx.strokeStyle = "rgba(246,234,211,0.28)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(25, 27, 7, 91);
ctx.fillStyle = "#f6ead3";
ctx.font = "700 39px Georgia";
ctx.fillText(text, 52, 70);
if (subtitle) {
ctx.fillStyle = "#aaa595";
ctx.font = "700 21px sans-serif";
ctx.fillText(subtitle, 53, 108);
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(1.55, 0.455, 1);
return sprite;
}
const woodTexture = makeWoodTexture();
const burlapTexture = makeBurlapTexture();
const floorTexture = makePlankTexture();
const materials = {
wood: new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xd6a06c,
roughness: 0.82
}),
darkWood: new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0x70452c,
roughness: 0.9
}),
burlap: new THREE.MeshStandardMaterial({
map: burlapTexture,
color: 0xd3ad74,
roughness: 1
}),
stone: new THREE.MeshStandardMaterial({
color: 0xa7a28f,
roughness: 0.95
}),
darkStone: new THREE.MeshStandardMaterial({
color: 0x4d5351,
roughness: 0.95
}),
brass: new THREE.MeshStandardMaterial({
color: 0xb88a42,
metalness: 0.68,
roughness: 0.34
}),
flour: new THREE.MeshStandardMaterial({
color: 0xf0dbb4,
roughness: 1
}),
bran: new THREE.MeshStandardMaterial({
color: 0x9d6434,
roughness: 1
}),
plaster: new THREE.MeshStandardMaterial({
color: 0xb7aa91,
roughness: 0.95
}),
dark: new THREE.MeshStandardMaterial({
color: 0x273035,
roughness: 0.9
})
};
function addBox(parent, size, position, material, cast = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(...position);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
position,
material,
segments = 24,
openEnded = false
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments,
1,
openEnded
),
material
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addTorus(parent, radius, tube, position, material, horizontal = false) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 10, 48),
material
);
mesh.position.set(...position);
if (horizontal) mesh.rotation.x = Math.PI / 2;
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function beamBetween(parent, a, b, width, depth, material) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const beam = new THREE.Mesh(
new THREE.BoxGeometry(width, length, depth),
material
);
beam.position.copy(start).add(end).multiplyScalar(0.5);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
beam.castShadow = true;
beam.receiveShadow = true;
parent.add(beam);
return beam;
}
function addLabel(parent, text, subtitle, position, accent) {
const label = makeLabel(text, subtitle, accent);
label.position.set(...position);
parent.add(label);
return label;
}
// ---------------------------------------------------------------------
// Platform, wall and bakery doorway
// ---------------------------------------------------------------------
const world = new THREE.Group();
scene.add(world);
const floorMaterial = new THREE.MeshStandardMaterial({
map: floorTexture,
color: 0xb8a894,
roughness: 0.94
});
addBox(world, [12.4, 0.46, 7.2], [0, -0.29, 0], floorMaterial);
addBox(
world,
[12.8, 0.18, 7.6],
[0, -0.55, 0],
new THREE.MeshStandardMaterial({ color: 0x403a35, roughness: 0.9 })
);
// Back wall, leaving an opening for the bakery.
addBox(world, [7.9, 3.65, 0.24], [-1.95, 1.54, -2.94], materials.plaster);
addBox(world, [1.35, 3.65, 0.24], [5.43, 1.54, -2.94], materials.plaster);
addBox(world, [2.85, 0.93, 0.24], [3.48, 3.19, -2.94], materials.plaster);
// Door jambs and wooden arch.
addBox(world, [0.18, 2.5, 0.24], [2.07, 1.18, -2.78], materials.darkWood);
addBox(world, [0.18, 2.5, 0.24], [4.9, 1.18, -2.78], materials.darkWood);
const arch = new THREE.Mesh(
new THREE.TorusGeometry(1.42, 0.12, 12, 48, Math.PI),
materials.darkWood
);
arch.position.set(3.48, 2.43, -2.76);
arch.castShadow = true;
world.add(arch);
// Dark oven recess.
addBox(
world,
[2.72, 2.48, 0.12],
[3.48, 1.2, -3.05],
new THREE.MeshStandardMaterial({ color: 0x241d1a, roughness: 1 })
);
function makeArchShape(width, height) {
const shape = new THREE.Shape();
const radius = width / 2;
shape.moveTo(-radius, 0);
shape.lineTo(radius, 0);
shape.lineTo(radius, height - radius);
shape.absarc(0, height - radius, radius, 0, Math.PI, false);
shape.lineTo(-radius, 0);
return shape;
}
const ovenOpening = new THREE.Mesh(
new THREE.ShapeGeometry(makeArchShape(1.72, 2.12)),
new THREE.MeshStandardMaterial({
color: 0xd16b29,
emissive: 0xff6d1b,
emissiveIntensity: 2.2,
roughness: 0.75
})
);
ovenOpening.position.set(3.48, 0.18, -2.965);
world.add(ovenOpening);
const ovenLight = new THREE.PointLight(0xff7a28, 16, 6, 2);
ovenLight.position.set(3.48, 1.35, -2.35);
world.add(ovenLight);
const floorGlow = new THREE.Mesh(
new THREE.PlaneGeometry(2.4, 3.7),
new THREE.MeshBasicMaterial({
color: 0xff9a47,
transparent: true,
opacity: 0.12,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
floorGlow.rotation.x = -Math.PI / 2;
floorGlow.position.set(3.48, -0.045, -1.1);
world.add(floorGlow);
// An open wooden door.
const doorHinge = new THREE.Group();
doorHinge.position.set(4.87, 0, -2.62);
doorHinge.rotation.y = -1.03;
world.add(doorHinge);
addBox(doorHinge, [1.28, 2.33, 0.12], [-0.64, 1.17, 0], materials.darkWood);
addBox(doorHinge, [0.12, 0.12, 0.16], [-1.1, 1.12, 0.1], materials.brass);
addLabel(world, "BAKERY", "BATCH 01 · FINAL FORM", [3.48, 3.86, -2.73], "#e9b858");
// ---------------------------------------------------------------------
// Grain sacks
// ---------------------------------------------------------------------
const sackProfile = [
new THREE.Vector2(0.38, 0.02),
new THREE.Vector2(0.59, 0.13),
new THREE.Vector2(0.68, 0.48),
new THREE.Vector2(0.63, 0.82),
new THREE.Vector2(0.49, 1.08),
new THREE.Vector2(0.27, 1.26),
new THREE.Vector2(0.2, 1.4),
new THREE.Vector2(0.03, 1.44)
];
function createSack(x, z, scale = 1, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, -0.04, z);
group.scale.setScalar(scale);
group.rotation.y = rotation;
const body = new THREE.Mesh(
new THREE.LatheGeometry(sackProfile, 24),
materials.burlap
);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const tie = addTorus(
group,
0.22,
0.035,
[0, 1.27, 0],
materials.darkWood,
true
);
const knot = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.09, 1),
materials.burlap
);
knot.position.set(0.04, 1.34, 0.02);
knot.castShadow = true;
group.add(knot);
world.add(group);
return group;
}
createSack(-4.82, 1.32, 1.0, -0.22);
createSack(-3.98, 1.83, 0.92, 0.18);
createSack(-4.73, 0.08, 0.94, 0.35);
createSack(-3.77, 0.2, 0.86, -0.18);
const grainHeapCenter = new THREE.Vector3(-3.92, 0.18, 1.03);
const grainGeometry = new THREE.IcosahedronGeometry(0.075, 1);
const grainMaterial = new THREE.MeshStandardMaterial({
color: 0xd9a43e,
roughness: 0.86
});
for (let i = 0; i < 58; i++) {
const angle = random() * Math.PI * 2;
const radius = Math.sqrt(random()) * 0.62;
const grain = new THREE.Mesh(grainGeometry, grainMaterial);
grain.position.set(
grainHeapCenter.x + Math.cos(angle) * radius,
0.03 + (1 - radius / 0.62) * 0.25 + random() * 0.04,
grainHeapCenter.z + Math.sin(angle) * radius
);
grain.scale.set(0.7, 1.35, 0.7);
grain.rotation.set(random() * 3, random() * 3, random() * 3);
grain.castShadow = true;
world.add(grain);
}
addLabel(world, "GRAIN", "24.0 KG · WHOLE", [-4.12, 2.16, 1.17], "#e9b858");
// ---------------------------------------------------------------------
// Stone mill
// ---------------------------------------------------------------------
const mill = new THREE.Group();
mill.position.set(-2.48, -0.04, -0.38);
world.add(mill);
addBox(mill, [1.65, 0.16, 1.45], [0, 0.22, 0], materials.darkWood);
addBox(mill, [0.16, 1.2, 0.16], [-0.66, 0.8, -0.55], materials.darkWood);
addBox(mill, [0.16, 1.2, 0.16], [0.66, 0.8, -0.55], materials.darkWood);
addBox(mill, [0.16, 1.2, 0.16], [-0.66, 0.8, 0.55], materials.darkWood);
addBox(mill, [0.16, 1.2, 0.16], [0.66, 0.8, 0.55], materials.darkWood);
addCylinder(mill, 0.93, 1.03, 1.08, [0, 1.04, 0], materials.stone, 32);
addTorus(mill, 0.94, 0.055, [0, 0.82, 0], materials.darkStone, true);
addTorus(mill, 0.91, 0.055, [0, 1.3, 0], materials.darkStone, true);
addCylinder(mill, 0.92, 0.34, 0.7, [0, 1.93, 0], materials.wood, 28);
addCylinder(
mill,
0.84,
0.84,
0.025,
[0, 2.285, 0],
new THREE.MeshStandardMaterial({ color: 0x4a3526, roughness: 1 }),
28
);
// Mill wheel.
const millWheel = new THREE.Group();
millWheel.position.set(1.03, 1.12, 0);
mill.add(millWheel);
const wheelRing = addTorus(
millWheel,
0.62,
0.075,
[0, 0, 0],
materials.darkWood
);
wheelRing.rotation.y = Math.PI / 2;
for (let i = 0; i < 6; i++) {
const spoke = addBox(
millWheel,
[0.075, 1.16, 0.075],
[0, 0, 0],
materials.wood
);
spoke.rotation.x = (i / 6) * Math.PI;
}
const axle = addCylinder(
millWheel,
0.12,
0.12,
0.48,
[0, 0, 0],
materials.brass,
20
);
axle.rotation.z = Math.PI / 2;
beamBetween(
world,
[-1.83, 1.03, -0.26],
[-0.85, 1.35, 0.38],
0.42,
0.2,
materials.wood
);
addLabel(world, "MILL", "24.0 KG · GROUND", [-2.48, 2.98, -0.38], "#e9b858");
// ---------------------------------------------------------------------
// Sieve table
// ---------------------------------------------------------------------
const sieveTable = new THREE.Group();
sieveTable.position.set(-0.1, -0.04, 0.55);
world.add(sieveTable);
addBox(sieveTable, [1.85, 0.17, 1.34], [0, 1.08, 0], materials.wood);
addBox(sieveTable, [0.15, 1.0, 0.15], [-0.72, 0.53, -0.47], materials.darkWood);
addBox(sieveTable, [0.15, 1.0, 0.15], [0.72, 0.53, -0.47], materials.darkWood);
addBox(sieveTable, [0.15, 1.0, 0.15], [-0.72, 0.53, 0.47], materials.darkWood);
addBox(sieveTable, [0.15, 1.0, 0.15], [0.72, 0.53, 0.47], materials.darkWood);
addTorus(
sieveTable,
0.59,
0.055,
[0, 1.31, 0],
materials.brass,
true
);
const sieveMeshMaterial = new THREE.MeshStandardMaterial({
color: 0x6b7068,
metalness: 0.45,
roughness: 0.6,
wireframe: true
});
const sieveMesh = new THREE.Mesh(
new THREE.CircleGeometry(0.53, 36),
sieveMeshMaterial
);
sieveMesh.rotation.x = -Math.PI / 2;
sieveMesh.position.y = 1.31;
sieveTable.add(sieveMesh);
for (let i = -4; i <= 4; i++) {
addBox(
sieveTable,
[1.02, 0.012, 0.015],
[0, 1.335, i * 0.115],
materials.brass,
false
);
addBox(
sieveTable,
[0.015, 0.012, 1.02],
[i * 0.115, 1.337, 0],
materials.brass,
false
);
}
// Bran caught below the sieve.
addCylinder(
sieveTable,
0.43,
0.34,
0.14,
[0, 0.55, 0.05],
materials.bran,
28
);
addLabel(world, "SIEVE", "18 FLOUR + 6 BRAN", [-0.1, 2.22, 0.55], "#e9b858");
// ---------------------------------------------------------------------
// Flour bins and bran tray
// ---------------------------------------------------------------------
const binCenters = [
new THREE.Vector3(1.25, 1.46, -0.55),
new THREE.Vector3(2.02, 1.46, -0.55),
new THREE.Vector3(2.79, 1.46, -0.55)
];
const binQuantities = [10, 5, 3];
const binLetters = ["A", "B", "C"];
const binFillMaterials = [];
const glassMaterial = new THREE.MeshStandardMaterial({
color: 0xc8d7cf,
transparent: true,
opacity: 0.25,
roughness: 0.35,
metalness: 0.05,
depthWrite: false
});
for (let i = 0; i < 3; i++) {
const bin = new THREE.Group();
bin.position.set(binCenters[i].x, -0.04, binCenters[i].z);
world.add(bin);
addCylinder(bin, 0.34, 0.38, 1.28, [0, 0.77, 0], glassMaterial, 28, true);
addCylinder(bin, 0.4, 0.4, 0.12, [0, 0.11, 0], materials.darkWood, 28);
addTorus(bin, 0.345, 0.045, [0, 1.42, 0], materials.brass, true);
addTorus(bin, 0.375, 0.035, [0, 0.16, 0], materials.brass, true);
const fillHeight = 0.2 + binQuantities[i] * 0.071;
const fillMaterial = materials.flour.clone();
fillMaterial.emissive = new THREE.Color(0x4e3a1f);
fillMaterial.emissiveIntensity = 0;
binFillMaterials.push(fillMaterial);
addCylinder(
bin,
0.31,
0.31,
fillHeight,
[0, 0.17 + fillHeight / 2, 0],
fillMaterial,
28
);
const label = addLabel(
bin,
`FLOUR ${binLetters[i]}`,
`${binQuantities[i]}.0 KG`,
[0, 1.95, 0],
"#f1d9aa"
);
label.scale.set(1.08, 0.317, 1);
}
// Bran tray.
const branTray = new THREE.Group();
branTray.position.set(1.15, -0.04, 1.15);
world.add(branTray);
addCylinder(
branTray,
0.48,
0.4,
0.13,
[0, 0.55, 0],
materials.bran,
30
);
addTorus(
branTray,
0.48,
0.045,
[0, 0.62, 0],
materials.darkWood,
true
);
const branLabel = addLabel(
branTray,
"BRAN",
"6.0 KG",
[0, 1.42, 0],
"#b77942"
);
branLabel.scale.set(0.94, 0.275, 1);
addLabel(world, "FLOUR BINS", "10 + 5 + 3 + 6 = 24", [2.0, 2.48, -0.55], "#f1d9aa");
// ---------------------------------------------------------------------
// Dough table and bakery products
// ---------------------------------------------------------------------
const doughCenter = new THREE.Vector3(3.35, 1.43, 1.18);
const doughTable = new THREE.Group();
doughTable.position.set(doughCenter.x, -0.04, doughCenter.z);
world.add(doughTable);
addBox(doughTable, [1.55, 0.16, 1.1], [0, 1.08, 0], materials.wood);
addBox(doughTable, [0.14, 1.0, 0.14], [-0.58, 0.53, -0.38], materials.darkWood);
addBox(doughTable, [0.14, 1.0, 0.14], [0.58, 0.53, -0.38], materials.darkWood);
addBox(doughTable, [0.14, 1.0, 0.14], [-0.58, 0.53, 0.38], materials.darkWood);
addBox(doughTable, [0.14, 1.0, 0.14], [0.58, 0.53, 0.38], materials.darkWood);
addCylinder(
doughTable,
0.52,
0.5,
0.06,
[0, 1.2, 0],
new THREE.MeshStandardMaterial({
color: 0xc69b63,
roughness: 0.9
}),
32
);
const doughGroup = new THREE.Group();
doughGroup.position.set(doughCenter.x, 1.39, doughCenter.z);
doughGroup.scale.setScalar(0.01);
world.add(doughGroup);
const doughMaterial = new THREE.MeshStandardMaterial({
color: 0xe8c98e,
roughness: 0.82
});
const doughBall = new THREE.Mesh(
new THREE.SphereGeometry(0.43, 24, 16),
doughMaterial
);
doughBall.scale.set(1.15, 0.62, 0.95);
doughBall.castShadow = true;
doughGroup.add(doughBall);
for (let i = -1; i <= 1; i++) {
const score = new THREE.Mesh(
new THREE.BoxGeometry(0.05, 0.02, 0.42),
new THREE.MeshStandardMaterial({ color: 0x9d7547, roughness: 1 })
);
score.position.set(i * 0.15, 0.25, 0);
score.rotation.y = i * 0.42;
doughGroup.add(score);
}
addLabel(world, "DOUGH", "24.0 KG · COMBINED", [3.35, 2.38, 1.18], "#e9b858");
const loaf = new THREE.Group();
loaf.position.set(3.48, 0.77, -2.72);
loaf.scale.setScalar(0.01);
world.add(loaf);
const loafMaterial = new THREE.MeshStandardMaterial({
color: 0xb76c31,
emissive: 0x3b1606,
emissiveIntensity: 0.45,
roughness: 0.85
});
const loafBody = new THREE.Mesh(
new THREE.SphereGeometry(0.48, 24, 16),
loafMaterial
);
loafBody.scale.set(1.4, 0.72, 0.82);
loafBody.castShadow = true;
loaf.add(loafBody);
for (let i = -1; i <= 1; i++) {
const score = new THREE.Mesh(
new THREE.BoxGeometry(0.055, 0.025, 0.52),
new THREE.MeshStandardMaterial({
color: 0xf0bd6c,
emissive: 0x5d2a08,
emissiveIntensity: 0.4
})
);
score.position.set(i * 0.18, 0.31, 0);
score.rotation.y = i * 0.4;
loaf.add(score);
}
// ---------------------------------------------------------------------
// Connected batch paths
// ---------------------------------------------------------------------
const intakeCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-3.92, 0.5, 1.03),
new THREE.Vector3(-2.5, 2.55, -0.38),
new THREE.Vector3(-2.0, 1.08, -0.2),
new THREE.Vector3(-0.1, 1.5, 0.55)
],
false,
"centripetal"
);
const flourACurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-0.1, 1.5, 0.55),
new THREE.Vector3(0.55, 1.7, 0.05),
binCenters[0],
new THREE.Vector3(1.25, 1.48, -0.2)
],
false,
"centripetal"
);
const flourBCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-0.1, 1.5, 0.55),
new THREE.Vector3(0.8, 1.72, 0.0),
binCenters[1],
new THREE.Vector3(2.02, 1.48, -0.2)
],
false,
"centripetal"
);
const flourCCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-0.1, 1.5, 0.55),
new THREE.Vector3(1.0, 1.72, 0.0),
binCenters[2],
new THREE.Vector3(2.79, 1.48, -0.2)
],
false,
"centripetal"
);
const branCenter = new THREE.Vector3(1.15, 0.72, 1.15);
const branCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-0.1, 1.5, 0.55),
new THREE.Vector3(0.25, 1.58, 1.18),
branCenter,
new THREE.Vector3(1.15, 0.78, 1.35)
],
false,
"centripetal"
);
const bakeCurve = new THREE.CatmullRomCurve3(
[
doughCenter,
new THREE.Vector3(3.55, 1.55, 0.2),
new THREE.Vector3(3.52, 1.2, -1.25),
new THREE.Vector3(3.48, 0.8, -2.7)
],
false,
"centripetal"
);
const routeLines = [];
const routeCurves = [
intakeCurve,
flourACurve,
flourBCurve,
flourCCurve,
branCurve,
bakeCurve
];
for (const curve of routeCurves) {
const geometry = new THREE.BufferGeometry().setFromPoints(
curve.getPoints(90)
);
const material = new THREE.LineDashedMaterial({
color: 0xe9b858,
dashSize: 0.11,
gapSize: 0.08,
transparent: true,
opacity: 0.13,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
scene.add(line);
routeLines.push(line);
}
// Stage halos.
const halos = [];
const haloData = [
[-3.92, 0.03, 1.03, 1.25],
[-2.48, 0.03, -0.38, 1.2],
[-0.1, 0.03, 0.55, 1.0],
[2.0, 0.03, -0.55, 1.35],
[3.35, 0.03, 1.18, 0.92],
[3.48, 0.03, -2.45, 1.16]
];
for (const [x, y, z, radius] of haloData) {
const material = new THREE.MeshBasicMaterial({
color: 0xe9b858,
transparent: true,
opacity: 0.1,
depthWrite: false
});
const halo = addTorus(
scene,
radius,
0.018,
[x, y, z],
material,
true
);
halo.castShadow = false;
halos.push(halo);
}
// ---------------------------------------------------------------------
// Animated batch: 96 visible units, divided 40 / 20 / 12 flour and
// 24 bran. This preserves the displayed 10 / 5 / 3 / 6 ratio.
// ---------------------------------------------------------------------
const PARTICLE_COUNT = 96;
const particleGeometry = new THREE.IcosahedronGeometry(0.075, 1);
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.72,
metalness: 0
});
const batch = new THREE.InstancedMesh(
particleGeometry,
particleMaterial,
PARTICLE_COUNT
);
batch.castShadow = true;
batch.frustumCulled = false;
scene.add(batch);
const heapOffsets = [];
for (let i = 0; i < PARTICLE_COUNT; i++) {
const angle = random() * Math.PI * 2;
const radius = Math.sqrt(random()) * 0.58;
heapOffsets.push(
new THREE.Vector3(
Math.cos(angle) * radius,
0.12 + (1 - radius / 0.58) * 0.22,
Math.sin(angle) * radius
)
);
}
function particleGroup(index) {
if (index < 40) return "A";
if (index < 60) return "B";
if (index < 72) return "C";
return "BRAN";
}
function particleStart(index) {
const group = particleGroup(index);
if (group === "A") return binCenters[0];
if (group === "B") return binCenters[1];
if (group === "C") return binCenters[2];
return branCenter;
}
const stages = [
{
start: 0,
end: 0.15,
title: "Whole grain",
mass: "24.0 kg",
copy: "The original batch is measured and ready to travel.",
chip: "Sacks"
},
{
start: 0.15,
end: 0.35,
title: "Stone-ground meal",
mass: "24.0 kg",
copy: "The kernels change form, but the batch remains complete.",
chip: "Mill"
},
{
start: 0.35,
end: 0.55,
title: "The sieve separates",
mass: "18.0 kg flour + 6.0 kg bran",
copy: "One meal becomes two complementary fractions.",
chip: "Sieve"
},
{
start: 0.55,
end: 0.72,
title: "Fractions are stored",
mass: "A 10 + B 5 + C 3 + bran 6 = 24",
copy: "Flour and bran wait in distinct containers, still one batch.",
chip: "Bins"
},
{
start: 0.72,
end: 0.88,
title: "Fractions recombine",
mass: "24.0 kg combined dough",
copy: "Every separated portion returns to the same dough.",
chip: "Dough"
},
{
start: 0.88,
end: 1,
title: "The bakery batch",
mass: "24.0 kg baked",
copy: "The final loaf carries the complete conserved batch.",
chip: "Bakery"
}
];
const stageChips = [...document.querySelectorAll(".stage-chip")];
const chainRows = [...document.querySelectorAll(".chain-row")];
const currentKicker = document.querySelector("#current-kicker");
const currentTitle = document.querySelector("#current-title");
const currentCopy = document.querySelector("#current-copy");
const scrubber = document.querySelector("#scrubber");
const timeLabel = document.querySelector("#time");
const playButton = document.querySelector("#play");
const speedButton = document.querySelector("#speed");
const DURATION = 28;
let timeline = 0;
let playing = true;
let playbackSpeed = 1;
let activeStage = -1;
function getStage(t) {
for (let i = stages.length - 1; i >= 0; i--) {
if (t >= stages[i].start) return i;
}
return 0;
}
function updateUI(stageIndex) {
if (stageIndex === activeStage) return;
activeStage = stageIndex;
const stage = stages[stageIndex];
currentKicker.textContent =
`Stage ${String(stageIndex + 1).padStart(2, "0")} / 06`;
currentTitle.textContent = stage.title;
currentCopy.textContent = stage.copy;
stageChips.forEach((chip, index) => {
chip.classList.toggle("active", index === stageIndex);
});
chainRows.forEach((row) => {
const activeStages = row.dataset.stages.split(",").map(Number);
row.classList.toggle("active", activeStages.includes(stageIndex));
});
}
function formatTime(seconds) {
const whole = Math.floor(seconds);
const minutes = String(Math.floor(whole / 60)).padStart(2, "0");
const secs = String(whole % 60).padStart(2, "0");
return `${minutes}:${secs}`;
}
function setTimeline(value) {
timeline = THREE.MathUtils.clamp(value, 0, 0.99999);
const stageIndex = getStage(timeline);
const stage = stages[stageIndex];
const local = (timeline - stage.start) / (stage.end - stage.start);
scrubber.value = String(Math.round(timeline * 1000));
timeLabel.textContent =
`${formatTime(timeline * DURATION)} / ${formatTime(DURATION)}`;
stageChips.forEach((chip, index) => {
const fill =
index < stageIndex
? 1
: index === stageIndex
? local
: 0;
chip.style.setProperty("--fill", fill.toFixed(3));
});
updateUI(stageIndex);
}
function togglePlayback() {
playing = !playing;
playButton.textContent = playing ? "Ⅱ" : "▶";
playButton.setAttribute(
"aria-label",
playing ? "Pause animation" : "Play animation"
);
}
playButton.addEventListener("click", togglePlayback);
scrubber.addEventListener("input", () => {
setTimeline(Number(scrubber.value) / 1000);
});
stageChips.forEach((chip, index) => {
chip.addEventListener("click", () => {
setTimeline(stages[index].start + 0.0001);
});
});
speedButton.addEventListener("click", () => {
const speeds = [0.5, 1, 1.5, 2];
const next = (speeds.indexOf(playbackSpeed) + 1) % speeds.length;
playbackSpeed = speeds[next];
speedButton.textContent = `${playbackSpeed}×`;
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePlayback();
}
if (event.key.toLowerCase() === "r") {
setTimeline(0);
}
});
// ---------------------------------------------------------------------
// Batch animation
// ---------------------------------------------------------------------
const dummy = new THREE.Object3D();
const color = new THREE.Color();
const gold = new THREE.Color(0xd9a43e);
const mealColor = new THREE.Color(0xc58d4e);
const flourColor = new THREE.Color(0xf0dbb4);
const branColor = new THREE.Color(0x9d6434);
const doughColor = new THREE.Color(0xe8c98e);
const bakedColor = new THREE.Color(0xb76c31);
function smooth01(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function updateBatch(t) {
const stageIndex = getStage(t);
const stage = stages[stageIndex];
const local = THREE.MathUtils.clamp(
(t - stage.start) / (stage.end - stage.start),
0,
1
);
const eased = smooth01(local);
for (let i = 0; i < PARTICLE_COUNT; i++) {
const group = particleGroup(i);
const position = new THREE.Vector3();
let scaleX = 0.75;
let scaleY = 1.35;
let scaleZ = 0.75;
let particleColor = gold.clone();
if (stageIndex === 0) {
position.copy(grainHeapCenter).add(heapOffsets[i]);
position.y += Math.sin(t * Math.PI * 12 + i * 0.4) * 0.015;
}
if (stageIndex === 1) {
position.copy(intakeCurve.getPointAt(eased));
const jitter = Math.sin(i * 17.13) * 0.08;
position.x += jitter;
position.z += Math.cos(i * 9.41) * 0.08;
position.y += Math.sin(i * 3.17) * 0.035;
particleColor = gold;
}
if (stageIndex === 2) {
const angle = i * 2.39996 + t * Math.PI * 2 * 7;
const radius = Math.sin(local * Math.PI) * 0.48;
position.set(
-0.1 + Math.cos(angle) * radius,
1.52 + Math.sin(angle * 1.8) * 0.12,
0.55 + Math.sin(angle) * radius
);
const targetColor =
group === "BRAN" ? branColor : flourColor;
particleColor.copy(mealColor).lerp(targetColor, eased);
scaleX = 0.68;
scaleY = 0.82;
scaleZ = 0.68;
}
if (stageIndex === 3) {
let curve;
if (group === "A") curve = flourACurve;
else if (group === "B") curve = flourBCurve;
else if (group === "C") curve = flourCCurve;
else curve = branCurve;
position.copy(curve.getPointAt(eased));
position.x += Math.sin(i * 4.9) * 0.035;
position.y += Math.cos(i * 6.3) * 0.025;
position.z += Math.sin(i * 8.1) * 0.035;
particleColor.copy(group === "BRAN" ? branColor : flourColor);
scaleX = 0.56;
scaleY = 0.56;
scaleZ = 0.56;
}
if (stageIndex === 4) {
const start = particleStart(i);
const gather = smooth01(local);
position.copy(start).lerp(doughCenter, gather);
const swirl = Math.sin(local * Math.PI) * 0.19;
position.x += Math.cos(i * 2.4 + local * Math.PI * 3) * swirl;
position.y += Math.sin(i * 1.9 + local * Math.PI * 2) * swirl * 0.6;
position.z += Math.sin(i * 2.4 + local * Math.PI * 3) * swirl;
const startColor = group === "BRAN" ? branColor : flourColor;
particleColor.copy(startColor).lerp(doughColor, gather);
scaleX = 0.75 + gather * 0.35;
scaleY = 0.75 + gather * 0.35;
scaleZ = 0.75 + gather * 0.35;
}
if (stageIndex === 5) {
position.copy(bakeCurve.getPointAt(eased));
const cluster = 0.13 * (1 - eased * 0.45);
position.x += Math.cos(i * 2.4) * cluster;
position.y += Math.sin(i * 1.7) * cluster * 0.55;
position.z += Math.sin(i * 2.4) * cluster;
particleColor.copy(doughColor).lerp(bakedColor, eased);
scaleX = 1.15;
scaleY = 0.68;
scaleZ = 0.9;
}
dummy.position.copy(position);
dummy.rotation.set(
i * 0.37 + t * 2,
i * 0.21 + t * 1.4,
i * 0.13
);
dummy.scale.set(scaleX, scaleY, scaleZ);
dummy.updateMatrix();
batch.setMatrixAt(i, dummy.matrix);
batch.setColorAt(i, color.copy(particleColor));
}
batch.instanceMatrix.needsUpdate = true;
if (batch.instanceColor) batch.instanceColor.needsUpdate = true;
// The mill wheel completes twelve turns per full timeline loop.
millWheel.rotation.x = -t * Math.PI * 2 * 12;
// Dough becomes visible as the separated streams recombine.
const doughReveal = stageIndex === 4 ? smooth01(local * 1.8) : 0;
doughGroup.scale.setScalar(Math.max(0.01, doughReveal));
const loafReveal = stageIndex === 5 ? smooth01(local * 1.7) : 0;
loaf.scale.setScalar(Math.max(0.01, loafReveal));
// Active station halo.
halos.forEach((halo, index) => {
const active = index === stageIndex;
halo.material.opacity = active ? 0.62 : 0.075;
halo.rotation.z = t * Math.PI * 2 * (index % 2 ? -1 : 1);
});
// Route highlighting.
routeLines.forEach((line, index) => {
let active = false;
if (stageIndex === 0) active = index === 0;
if (stageIndex === 1) active = index === 0;
if (stageIndex === 2) active = index >= 1 && index <= 4;
if (stageIndex === 3) active = index >= 1 && index <= 4;
if (stageIndex === 4) active = false;
if (stageIndex === 5) active = index === 5;
line.material.opacity = active ? 0.52 : 0.1;
});
// A gentle warm pulse at the bakery.
const pulse = 0.5 + 0.5 * Math.sin(t * Math.PI * 2 * 4);
ovenLight.intensity = 13 + pulse * 4;
ovenOpening.material.emissiveIntensity = 1.9 + pulse * 0.65;
floorGlow.material.opacity = 0.1 + pulse * 0.035;
binFillMaterials.forEach((material, index) => {
const highlight = stageIndex === 3 && index <= 2;
material.emissiveIntensity = highlight ? 0.24 : 0;
});
}
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
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);
}
window.addEventListener("resize", resize);
let lastTime = performance.now();
function animate(now) {
const delta = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
if (playing) {
timeline = (timeline + (delta * playbackSpeed) / DURATION) % 1;
}
setTimeline(timeline);
updateBatch(timeline);
orbit.yaw = THREE.MathUtils.lerp(
orbit.yaw,
orbit.desiredYaw,
0.09
);
orbit.pitch = THREE.MathUtils.lerp(
orbit.pitch,
orbit.desiredPitch,
0.09
);
orbit.radius = THREE.MathUtils.lerp(
orbit.radius,
orbit.desiredRadius,
0.09
);
updateCamera();
renderer.render(scene, camera);
}
setTimeline(0);
updateBatch(0);
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```