Prompt specification
Build or customize the Three.js 3D scene "Grain storage teaching cutaway" 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 storage teaching cutaway
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/grain-storage-teaching-cutaway-x1391
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring granular piles and particle flow. Compose a small hopper, sloped walls, and outlet above a conveyor; show schematic flow paths and identify dead zones as illustrative rather than engineered performance. Voxel style.
Working Three.js Starter (grain-storage-teaching-cutaway-x1391):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Granular Flow — Voxel Hopper Study</title>
<style>
:root {
color-scheme: dark;
--bg: #0b1418;
--panel: rgba(13, 25, 29, 0.88);
--line: rgba(139, 205, 199, 0.22);
--text: #dce9e6;
--muted: #87a09d;
--cyan: #68e3df;
--amber: #f0ae4b;
--magenta: #e36394;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#scene.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--panel);
backdrop-filter: blur(12px);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
}
.intro {
top: 24px;
left: 24px;
width: min(360px, calc(100vw - 48px));
padding: 18px 20px;
border-radius: 4px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(104, 227, 223, 0.65);
}
h1 {
margin: 0;
color: var(--text);
font-size: clamp(22px, 3vw, 31px);
font-weight: 750;
line-height: 1.05;
letter-spacing: -0.035em;
text-transform: uppercase;
}
.intro p {
margin: 11px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.cutaway {
display: inline-block;
margin-top: 13px;
padding: 5px 7px;
border: 1px solid rgba(104, 227, 223, 0.25);
color: #9ac5c1;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.controls {
top: 24px;
right: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
max-width: 440px;
}
.controls button {
min-height: 36px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 3px;
outline: none;
background: rgba(13, 25, 29, 0.92);
color: #a9c0bd;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition:
border-color 160ms ease,
color 160ms ease,
background 160ms ease;
}
.controls button:hover,
.controls button:focus-visible {
border-color: rgba(104, 227, 223, 0.65);
color: var(--text);
background: rgba(22, 45, 49, 0.95);
}
.controls button[data-active="true"] {
border-color: rgba(104, 227, 223, 0.48);
color: var(--cyan);
}
.legend {
left: 24px;
bottom: 74px;
width: 240px;
padding: 14px 16px;
border-radius: 4px;
}
.legend-title {
margin-bottom: 11px;
color: #75918e;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 8px;
color: #a7bcba;
font-size: 11px;
}
.swatch {
width: 11px;
height: 11px;
flex: 0 0 auto;
border: 1px solid rgba(255, 255, 255, 0.14);
}
.swatch.grain {
background: var(--amber);
}
.swatch.path {
background: transparent;
border-color: var(--cyan);
box-shadow: inset 0 0 0 2px rgba(104, 227, 223, 0.18);
}
.swatch.zone {
background: var(--magenta);
}
.disclaimer {
position: fixed;
z-index: 10;
right: 24px;
bottom: 24px;
width: min(470px, calc(100vw - 48px));
padding: 13px 16px;
border: 1px solid rgba(227, 99, 148, 0.28);
border-radius: 4px;
background: rgba(29, 17, 25, 0.9);
color: #bda7b1;
font-size: 10px;
line-height: 1.55;
backdrop-filter: blur(12px);
}
.disclaimer strong {
color: #efa1be;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#hint {
position: fixed;
z-index: 10;
bottom: 26px;
left: 24px;
color: #68817e;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#labels {
position: fixed;
inset: 0;
z-index: 5;
overflow: hidden;
pointer-events: none;
}
.scene-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
padding: 6px 8px;
border: 1px solid rgba(151, 194, 188, 0.28);
border-radius: 2px;
background: rgba(10, 20, 23, 0.82);
color: #b8cbc8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
box-shadow: 0 7px 22px rgba(0, 0, 0, 0.22);
}
.scene-label::before {
content: "";
display: inline-block;
width: 6px;
height: 6px;
margin-right: 7px;
vertical-align: 1px;
background: #90aaa7;
}
#label-flow {
border-color: rgba(104, 227, 223, 0.4);
color: #94f1ed;
}
#label-flow::before {
background: var(--cyan);
box-shadow: 0 0 8px rgba(104, 227, 223, 0.8);
}
#label-dead {
border-color: rgba(227, 99, 148, 0.48);
color: #f0a1c0;
}
#label-dead::before {
background: var(--magenta);
box-shadow: 0 0 8px rgba(227, 99, 148, 0.8);
}
@media (max-width: 760px) {
.intro {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 13px 15px;
}
.intro p {
display: none;
}
.controls {
top: auto;
right: 14px;
bottom: 66px;
max-width: calc(100vw - 28px);
}
.legend {
display: none;
}
.disclaimer {
right: 14px;
bottom: 14px;
width: calc(100vw - 28px);
}
#hint {
display: none;
}
.scene-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive voxel-style hopper, granular material flow, and conveyor scene"
></canvas>
<div id="labels" aria-hidden="true">
<div class="scene-label" id="label-hopper">Hopper / bulk solids</div>
<div class="scene-label" id="label-outlet">Outlet</div>
<div class="scene-label" id="label-conveyor">Conveyor</div>
<div class="scene-label" id="label-pile">Granular pile</div>
<div class="scene-label" id="label-flow">Schematic flow path</div>
<div class="scene-label" id="label-dead">Dead zone · illustrative</div>
</div>
<section class="panel intro">
<div class="eyebrow">Material flow study / 01</div>
<h1>Granular<br>Flow Voxel Lab</h1>
<p>
A cutaway hopper, stepped discharge geometry, moving granular particles,
and downstream accumulation on a conveyor.
</p>
<span class="cutaway">Schematic cutaway view</span>
</section>
<nav class="controls" aria-label="Scene controls">
<button id="pause" type="button" data-active="false">Pause flow</button>
<button id="paths" type="button" data-active="true">Paths: on</button>
<button id="zones" type="button" data-active="true">Dead zones: on</button>
<button id="reset" type="button">Reset flow</button>
<button id="view" type="button">Reset view</button>
</nav>
<aside class="panel legend" aria-label="Scene legend">
<div class="legend-title">Visual key</div>
<div class="legend-row">
<span class="swatch grain"></span>
<span>Granular material</span>
</div>
<div class="legend-row">
<span class="swatch path"></span>
<span>Schematic flow direction</span>
</div>
<div class="legend-row">
<span class="swatch zone"></span>
<span>Illustrative dead zone</span>
</div>
</aside>
<div id="hint">Drag to orbit · wheel or pinch to zoom</div>
<div class="disclaimer">
<strong>Illustrative only.</strong>
Flow paths and dead-zone locations are schematic visual cues, not engineered
performance predictions, validated simulations, or design guidance.
</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,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
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(0x0b1418);
scene.fog = new THREE.Fog(0x0b1418, 15, 31);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
// ------------------------------------------------------------------
// Camera controls implemented directly in this file.
// ------------------------------------------------------------------
const target = new THREE.Vector3(1.45, 2.85, 0);
const desiredOrbit = {
theta: 0.45,
phi: 1.04,
radius: 13.4
};
const currentOrbit = {
theta: desiredOrbit.theta,
phi: desiredOrbit.phi,
radius: desiredOrbit.radius
};
function applyCamera() {
const sinPhi = Math.sin(currentOrbit.phi);
camera.position.set(
target.x +
currentOrbit.radius * sinPhi * Math.sin(currentOrbit.theta),
target.y + currentOrbit.radius * Math.cos(currentOrbit.phi),
target.z +
currentOrbit.radius * sinPhi * Math.cos(currentOrbit.theta)
);
camera.lookAt(target);
}
applyCamera();
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desiredOrbit.theta -= dx * 0.006;
desiredOrbit.phi = THREE.MathUtils.clamp(
desiredOrbit.phi + dy * 0.005,
0.38,
1.46
);
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredOrbit.radius = THREE.MathUtils.clamp(
desiredOrbit.radius * Math.exp(event.deltaY * 0.001),
7.5,
22
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
// ------------------------------------------------------------------
// Lighting and environment.
// ------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xbde7e2, 0x172127, 1.65));
const keyLight = new THREE.DirectionalLight(0xffe1b0, 3.1);
keyLight.position.set(-5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x69d6d2, 1.25);
rimLight.position.set(8, 5, -7);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x101b20,
roughness: 0.94,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.03;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(34, 68, 0x315a5b, 0x1a3034);
grid.position.y = -0.005;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
// ------------------------------------------------------------------
// Shared materials and helpers.
// ------------------------------------------------------------------
const materials = {
platform: new THREE.MeshStandardMaterial({
color: 0x18272c,
roughness: 0.82,
metalness: 0.18
}),
frame: new THREE.MeshStandardMaterial({
color: 0x284146,
roughness: 0.7,
metalness: 0.28
}),
frameDark: new THREE.MeshStandardMaterial({
color: 0x1a3035,
roughness: 0.72,
metalness: 0.25
}),
belt: new THREE.MeshStandardMaterial({
color: 0x101a1d,
roughness: 0.96,
metalness: 0.03
}),
slat: new THREE.MeshStandardMaterial({
color: 0x30474b,
roughness: 0.8,
metalness: 0.16
}),
teal: new THREE.MeshStandardMaterial({
color: 0x28585a,
roughness: 0.75,
metalness: 0.18
}),
tealLight: new THREE.MeshStandardMaterial({
color: 0x477d79,
roughness: 0.68,
metalness: 0.18
}),
tealDark: new THREE.MeshStandardMaterial({
color: 0x173d42,
roughness: 0.8,
metalness: 0.16
}),
accent: new THREE.MeshStandardMaterial({
color: 0xd78634,
roughness: 0.64,
metalness: 0.22
}),
cyan: new THREE.MeshBasicMaterial({
color: 0x68e3df,
transparent: true,
opacity: 0.9,
depthTest: false
})
};
function addEdges(mesh, color = 0x9fc0bc, opacity = 0.2) {
const edges = new THREE.EdgesGeometry(mesh.geometry);
const lines = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(lines);
return mesh;
}
function box(
width,
height,
depth,
material,
x,
y,
z,
parent = scene,
edged = false
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (edged) addEdges(mesh);
return mesh;
}
const dummy = new THREE.Object3D();
function createVoxelField(items, size = 0.18) {
const geometry = new THREE.BoxGeometry(size, size, size);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.8,
metalness: 0.05
});
const mesh = new THREE.InstancedMesh(
geometry,
material,
items.length
);
items.forEach((item, index) => {
dummy.position.set(item.p[0], item.p[1], item.p[2]);
dummy.rotation.set(
item.r ? item.r[0] : 0,
item.r ? item.r[1] : 0,
item.r ? item.r[2] : 0
);
dummy.scale.setScalar(item.s || 1);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, new THREE.Color(item.c));
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// ------------------------------------------------------------------
// Conveyor platform, frame, legs, and animated voxel slats.
// ------------------------------------------------------------------
box(
9.1,
0.16,
3.35,
materials.platform,
3.2,
0.08,
0,
scene,
true
);
const conveyorLength = 8.4;
const conveyorCenter = 3.2;
box(
conveyorLength,
0.28,
2.0,
materials.belt,
conveyorCenter,
0.77,
0,
scene,
true
);
box(
conveyorLength + 0.25,
0.17,
0.14,
materials.frame,
conveyorCenter,
0.91,
-1.12,
scene,
true
);
box(
conveyorLength + 0.25,
0.17,
0.14,
materials.frame,
conveyorCenter,
0.91,
1.12,
scene,
true
);
for (const x of [-0.35, 2.15, 4.65, 7.0]) {
for (const z of [-0.88, 0.88]) {
box(
0.16,
0.72,
0.16,
materials.frameDark,
x,
0.44,
z,
scene,
true
);
}
}
for (const x of [-0.55, 7.05]) {
box(
0.28,
0.4,
2.25,
materials.frame,
x,
0.72,
0,
scene,
true
);
}
const slatCount = 42;
const slatGeometry = new THREE.BoxGeometry(0.115, 0.045, 1.93);
const slatMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.82,
metalness: 0.12
});
const slats = new THREE.InstancedMesh(
slatGeometry,
slatMaterial,
slatCount
);
slats.receiveShadow = true;
for (let i = 0; i < slatCount; i++) {
dummy.position.set(-1 + i * 0.2, 0.935, 0);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
slats.setMatrixAt(i, dummy.matrix);
slats.setColorAt(
i,
new THREE.Color(i % 7 === 0 ? 0x416a69 : 0x263d41)
);
}
slats.instanceMatrix.needsUpdate = true;
if (slats.instanceColor) slats.instanceColor.needsUpdate = true;
scene.add(slats);
// Direction chevrons on the front conveyor rail.
for (const x of [1.6, 2.65, 3.7]) {
const chevron = new THREE.Group();
const upper = box(
0.34,
0.055,
0.055,
materials.cyan,
0,
0.075,
0,
chevron
);
const lower = box(
0.34,
0.055,
0.055,
materials.cyan,
0,
-0.075,
0,
chevron
);
upper.rotation.z = -0.55;
lower.rotation.z = 0.55;
chevron.position.set(x, 0.92, 1.205);
scene.add(chevron);
}
// ------------------------------------------------------------------
// Voxel hopper: tapered back wall, stepped side walls, outlet, frame.
// ------------------------------------------------------------------
const hopper = new THREE.Group();
scene.add(hopper);
const hopperCenterX = 0.5;
const hopperBottomY = 3.28;
const hopperTopY = 6.62;
const backWallItems = [];
const wallSize = 0.38;
for (let x = -2.65; x <= 3.65; x += wallSize) {
for (let y = 3.43; y <= 6.55; y += wallSize) {
const t = (y - hopperBottomY) / (hopperTopY - hopperBottomY);
const left = THREE.MathUtils.lerp(-0.04, -2.78, t);
const right = THREE.MathUtils.lerp(1.04, 3.78, t);
if (x >= left && x <= right) {
backWallItems.push({
p: [x, y, -0.9],
c: (Math.round(x * 10) + Math.round(y * 10)) % 3
? 0x244b4e
: 0x2b5859
});
}
}
}
const backWall = createVoxelField(backWallItems, wallSize);
backWall.receiveShadow = true;
const sideSteps = 15;
for (let i = 0; i < sideSteps; i++) {
const t = i / (sideSteps - 1);
const y = THREE.MathUtils.lerp(hopperBottomY, hopperTopY, t);
const leftX = THREE.MathUtils.lerp(-0.04, -2.78, t);
const rightX = THREE.MathUtils.lerp(1.04, 3.78, t);
box(
0.44,
0.49,
1.82,
i % 3 === 0 ? materials.tealLight : materials.teal,
leftX,
y,
0,
hopper,
true
);
box(
0.44,
0.49,
1.82,
i % 3 === 1 ? materials.tealLight : materials.teal,
rightX,
y,
0,
hopper,
true
);
}
// Front and back top rims establish the hopper volume.
for (const z of [-0.9, 0.9]) {
box(
6.9,
0.18,
0.18,
materials.tealLight,
hopperCenterX,
6.72,
z,
hopper,
true
);
}
// Structural posts stand outside the conveyor width.
for (const x of [-2.45, 3.45]) {
for (const z of [-1.3, 1.3]) {
box(
0.17,
3.3,
0.17,
materials.frame,
x,
1.65,
z,
hopper,
true
);
}
box(
0.18,
0.18,
2.75,
materials.frameDark,
x,
3.2,
0,
hopper,
true
);
}
// Outlet neck.
box(
0.16,
0.91,
1.65,
materials.tealLight,
-0.15,
2.93,
0,
hopper,
true
);
box(
0.16,
0.91,
1.65,
materials.tealLight,
1.15,
2.93,
0,
hopper,
true
);
box(
1.5,
0.13,
1.74,
materials.tealDark,
0.5,
2.49,
0,
hopper,
true
);
// Small gate hardware, kept to the side of the opening.
box(
0.48,
0.3,
0.36,
materials.accent,
1.43,
3.02,
0.25,
hopper,
true
);
box(
0.68,
0.1,
0.12,
materials.frameDark,
1.15,
3.02,
0.25,
hopper,
true
);
// ------------------------------------------------------------------
// Static granular material inside the hopper.
// ------------------------------------------------------------------
const grainColors = [
0xc8792c,
0xe39a38,
0xf0b34d,
0xb86328,
0xd98b32
];
const hopperGrains = [];
let randomSeed = 1729;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
for (let ix = 0; ix < 31; ix++) {
const x = -2.42 + ix * 0.19;
for (let iz = 0; iz < 6; iz++) {
const z = -0.72 + iz * 0.19;
const xFactor = Math.min(1, Math.abs(x - 0.5) / 2.95);
const zFactor = Math.min(1, Math.abs(z + 0.24) / 0.63);
const edgeFactor = Math.min(1, Math.max(xFactor, zFactor));
const surface =
4.12 + 1.47 * (1 - edgeFactor) + random() * 0.15;
const layers = Math.floor((surface - 3.35) / 0.18);
for (let layer = 0; layer <= layers; layer++) {
hopperGrains.push({
p: [
x + (random() - 0.5) * 0.035,
3.38 + layer * 0.18,
z + (random() - 0.5) * 0.035
],
c: grainColors[Math.floor(random() * grainColors.length)],
s: 0.82 + random() * 0.24,
r: [
(random() - 0.5) * 0.18,
(random() - 0.5) * 0.18,
(random() - 0.5) * 0.18
]
});
}
}
}
createVoxelField(hopperGrains, 0.18);
// ------------------------------------------------------------------
// Downstream granular pile on the conveyor.
// ------------------------------------------------------------------
const pileGrains = [];
for (let ix = 0; ix < 11; ix++) {
const x = 5.0 + ix * 0.18;
for (let iz = 0; iz < 8; iz++) {
const z = -0.75 + iz * 0.18;
const radius = Math.max(
Math.abs((x - 5.92) / 1.02),
Math.abs((z + 0.1) / 0.86)
);
if (radius >= 1) continue;
const height = 0.97 * (1 - radius * radius);
const layers = Math.floor(height / 0.18);
for (let layer = 0; layer <= layers; layer++) {
pileGrains.push({
p: [
x + (random() - 0.5) * 0.025,
1.03 + layer * 0.18,
z + (random() - 0.5) * 0.025
],
c: grainColors[Math.floor(random() * grainColors.length)],
s: 0.82 + random() * 0.22,
r: [
(random() - 0.5) * 0.16,
(random() - 0.5) * 0.16,
(random() - 0.5) * 0.16
]
});
}
}
}
createVoxelField(pileGrains, 0.18);
// ------------------------------------------------------------------
// Dynamic particles: hopper route -> outlet -> fall -> conveyor.
// ------------------------------------------------------------------
const routeCurves = [
new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-1.65, 6.05, 0.82),
new THREE.Vector3(-0.92, 5.08, 0.88),
new THREE.Vector3(-0.28, 3.88, 0.86),
new THREE.Vector3(0.38, 3.05, 0.74),
new THREE.Vector3(0.5, 2.66, 0.62)
],
false,
"catmullrom",
0.25
),
new THREE.CatmullRomCurve3(
[
new THREE.Vector3(0.5, 6.25, 0.9),
new THREE.Vector3(0.44, 5.2, 0.92),
new THREE.Vector3(0.58, 4.15, 0.9),
new THREE.Vector3(0.5, 3.12, 0.74),
new THREE.Vector3(0.5, 2.66, 0.62)
],
false,
"catmullrom",
0.25
),
new THREE.CatmullRomCurve3(
[
new THREE.Vector3(2.55, 6.03, 0.8),
new THREE.Vector3(1.78, 5.08, 0.9),
new THREE.Vector3(1.02, 3.9, 0.86),
new THREE.Vector3(0.66, 3.08, 0.72),
new THREE.Vector3(0.5, 2.66, 0.62)
],
false,
"catmullrom",
0.25
),
new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-0.58, 5.9, 0.94),
new THREE.Vector3(-0.28, 4.72, 0.92),
new THREE.Vector3(0.12, 3.63, 0.86),
new THREE.Vector3(0.46, 2.88, 0.7),
new THREE.Vector3(0.5, 2.66, 0.62)
],
false,
"catmullrom",
0.25
)
];
const particleCount = 180;
const particleGeometry = new THREE.BoxGeometry(0.13, 0.13, 0.13);
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.72,
metalness: 0.06
});
const particleMesh = new THREE.InstancedMesh(
particleGeometry,
particleMaterial,
particleCount
);
particleMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particleMesh.castShadow = true;
particleMesh.receiveShadow = true;
scene.add(particleMesh);
const particles = [];
const particleColors = [
0xffc15a,
0xf0a43a,
0xe5872e,
0xffd17a
];
function resetFall(particle) {
particle.phase = "fall";
particle.x = 0.5 + (Math.random() - 0.5) * 0.52;
particle.y = 2.62;
particle.z = 0.4 + Math.random() * 0.3;
particle.vx = (Math.random() - 0.5) * 0.18;
particle.vy = -0.2 - Math.random() * 0.2;
particle.vz = (Math.random() - 0.5) * 0.08;
particle.beltBounce = Math.random() * Math.PI * 2;
}
function initializeParticle(particle, index, immediateFall = false) {
particle.route = Math.floor(Math.random() * routeCurves.length);
particle.progress = Math.random();
particle.speed = 0.8 + Math.random() * 0.5;
particle.scale = 0.75 + Math.random() * 0.42;
particle.rotation = Math.random() * Math.PI * 2;
particle.spin = (Math.random() - 0.5) * 2.4;
if (immediateFall || index % 4 === 0) {
resetFall(particle);
} else {
particle.phase = "hopper";
}
}
for (let i = 0; i < particleCount; i++) {
const particle = {};
initializeParticle(particle, i);
particles.push(particle);
particleMesh.setColorAt(
i,
new THREE.Color(
particleColors[Math.floor(Math.random() * particleColors.length)]
)
);
}
if (particleMesh.instanceColor) {
particleMesh.instanceColor.needsUpdate = true;
}
function updateParticles(dt) {
particles.forEach((particle, index) => {
if (particle.phase === "hopper") {
particle.progress += dt * (0.095 + particle.speed * 0.075);
if (particle.progress >= 1) {
resetFall(particle);
} else {
const point = routeCurves[particle.route].getPointAt(
particle.progress
);
dummy.position.copy(point);
dummy.position.x +=
Math.sin(elapsed * 2.2 + index) * 0.018;
dummy.position.z +=
Math.cos(elapsed * 2.5 + index * 0.7) * 0.012;
}
} else if (particle.phase === "fall") {
particle.vy -= 5.4 * dt;
particle.x += particle.vx * dt;
particle.y += particle.vy * dt;
particle.z += particle.vz * dt;
particle.x += Math.sin(elapsed * 8 + index) * 0.006;
particle.z += Math.cos(elapsed * 7 + index) * 0.004;
if (particle.y <= 1.02) {
particle.phase = "belt";
particle.y = 1.03;
particle.beltSpeed = 1.15 + Math.random() * 0.42;
} else {
dummy.position.set(particle.x, particle.y, particle.z);
}
} else if (particle.phase === "belt") {
particle.x += particle.beltSpeed * dt;
particle.z +=
Math.sin(elapsed * 2.3 + particle.beltBounce) * 0.025 * dt;
particle.y =
1.03 +
Math.abs(Math.sin(elapsed * 9 + particle.beltBounce)) * 0.035;
if (particle.x > 5.05) {
// The particle is visually absorbed by the downstream pile.
resetFall(particle);
} else {
dummy.position.set(particle.x, particle.y, particle.z);
}
}
if (particle.phase !== "fall" || particle.y > 1.02) {
particle.rotation += particle.spin * dt;
dummy.rotation.set(
particle.rotation * 0.35,
particle.rotation,
particle.rotation * 0.22
);
} else {
dummy.rotation.set(
particle.rotation * 0.7,
particle.rotation * 0.8,
particle.rotation
);
}
dummy.scale.setScalar(particle.scale);
dummy.updateMatrix();
particleMesh.setMatrixAt(index, dummy.matrix);
});
particleMesh.instanceMatrix.needsUpdate = true;
}
// ------------------------------------------------------------------
// Schematic flow paths.
// ------------------------------------------------------------------
const flowGroup = new THREE.Group();
scene.add(flowGroup);
function addChevron(curve, t) {
const point = curve.getPointAt(t);
const tangent = curve.getTangentAt(t);
const group = new THREE.Group();
const upper = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.052, 0.052),
materials.cyan
);
const lower = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.052, 0.052),
materials.cyan
);
upper.position.y = 0.055;
lower.position.y = -0.055;
upper.rotation.z = -0.55;
lower.rotation.z = 0.55;
group.add(upper, lower);
group.position.copy(point);
group.rotation.z = Math.atan2(tangent.y, tangent.x);
group.renderOrder = 8;
flowGroup.add(group);
}
function addFlowPath(points) {
const curve = new THREE.CatmullRomCurve3(
points.map((point) => new THREE.Vector3(...point)),
false,
"catmullrom",
0.35
);
const geometry = new THREE.BufferGeometry().setFromPoints(
curve.getPoints(100)
);
const material = new THREE.LineDashedMaterial({
color: 0x68e3df,
dashSize: 0.13,
gapSize: 0.095,
transparent: true,
opacity: 0.88,
depthTest: false,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
line.renderOrder = 7;
flowGroup.add(line);
addChevron(curve, 0.39);
addChevron(curve, 0.71);
addChevron(curve, 0.9);
}
addFlowPath([
[-1.65, 6.05, 1.03],
[-0.92, 5.08, 1.03],
[-0.28, 3.88, 1.03],
[0.38, 3.05, 0.95],
[0.5, 2.62, 0.86],
[0.22, 1.2, 0.72]
]);
addFlowPath([
[0.5, 6.25, 1.03],
[0.44, 5.2, 1.03],
[0.58, 4.15, 1.03],
[0.5, 3.12, 0.96],
[0.5, 2.62, 0.86],
[0.5, 1.15, 0.58]
]);
addFlowPath([
[2.55, 6.03, 1.03],
[1.78, 5.08, 1.03],
[1.02, 3.9, 1.03],
[0.66, 3.08, 0.95],
[0.5, 2.62, 0.86],
[0.83, 1.15, 0.3]
]);
// ------------------------------------------------------------------
// Illustrative dead zones.
// ------------------------------------------------------------------
const zoneGroup = new THREE.Group();
scene.add(zoneGroup);
const deadZoneItems = [];
for (const centerX of [-0.03, 1.03]) {
for (let i = 0; i < 22; i++) {
deadZoneItems.push({
p: [
centerX + (Math.random() - 0.5) * 0.35,
3.34 + Math.random() * 0.38,
0.56 + (Math.random() - 0.5) * 0.42
],
c: i % 3 === 0 ? 0xb74879 : 0xe36394,
s: 0.68 + Math.random() * 0.34,
r: [
Math.random() * 0.3,
Math.random() * 0.3,
Math.random() * 0.3
]
});
}
}
const deadVoxels = createVoxelField(deadZoneItems, 0.18);
zoneGroup.attach(deadVoxels);
function addZoneOutline(x) {
const geometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(0.52, 0.58, 0.76)
);
const material = new THREE.LineDashedMaterial({
color: 0xe36394,
dashSize: 0.09,
gapSize: 0.06,
transparent: true,
opacity: 0.8,
depthTest: false
});
const outline = new THREE.LineSegments(geometry, material);
outline.computeLineDistances();
outline.position.set(x, 3.51, 0.57);
outline.renderOrder = 9;
zoneGroup.add(outline);
}
addZoneOutline(-0.03);
addZoneOutline(1.03);
// ------------------------------------------------------------------
// Labels projected from world positions.
// ------------------------------------------------------------------
const labelDefinitions = {
hopper: {
element: document.querySelector("#label-hopper"),
position: new THREE.Vector3(-2.25, 6.88, 0.25)
},
outlet: {
element: document.querySelector("#label-outlet"),
position: new THREE.Vector3(0.55, 2.47, 0.98)
},
conveyor: {
element: document.querySelector("#label-conveyor"),
position: new THREE.Vector3(2.7, 0.82, 1.24)
},
pile: {
element: document.querySelector("#label-pile"),
position: new THREE.Vector3(6.02, 2.02, 0.2)
},
flow: {
element: document.querySelector("#label-flow"),
position: new THREE.Vector3(1.75, 4.72, 1.05)
},
dead: {
element: document.querySelector("#label-dead"),
position: new THREE.Vector3(1.1, 3.55, 0.92)
}
};
const projected = new THREE.Vector3();
function placeLabel(definition, visible = true) {
if (!visible) {
definition.element.style.display = "none";
return;
}
projected.copy(definition.position).project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
Math.abs(projected.x) > 1.2 ||
Math.abs(projected.y) > 1.2
) {
definition.element.style.display = "none";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
definition.element.style.display = "block";
definition.element.style.left = `${x}px`;
definition.element.style.top = `${y}px`;
}
function updateLabels() {
placeLabel(labelDefinitions.hopper);
placeLabel(labelDefinitions.outlet);
placeLabel(labelDefinitions.conveyor);
placeLabel(labelDefinitions.pile);
placeLabel(labelDefinitions.flow, flowGroup.visible);
placeLabel(labelDefinitions.dead, zoneGroup.visible);
}
// ------------------------------------------------------------------
// UI controls.
// ------------------------------------------------------------------
let paused = false;
let beltOffset = 0;
const pauseButton = document.querySelector("#pause");
const pathsButton = document.querySelector("#paths");
const zonesButton = document.querySelector("#zones");
const resetButton = document.querySelector("#reset");
const viewButton = document.querySelector("#view");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume flow" : "Pause flow";
pauseButton.dataset.active = String(!paused);
});
pathsButton.addEventListener("click", () => {
flowGroup.visible = !flowGroup.visible;
pathsButton.textContent = `Paths: ${flowGroup.visible ? "on" : "off"}`;
pathsButton.dataset.active = String(flowGroup.visible);
});
zonesButton.addEventListener("click", () => {
zoneGroup.visible = !zoneGroup.visible;
zonesButton.textContent = `Dead zones: ${zoneGroup.visible ? "on" : "off"}`;
zonesButton.dataset.active = String(zoneGroup.visible);
});
resetButton.addEventListener("click", () => {
particles.forEach((particle, index) =>
initializeParticle(particle, index, false)
);
beltOffset = 0;
});
function resetView() {
desiredOrbit.theta = 0.45;
desiredOrbit.phi = 1.04;
desiredOrbit.radius = 13.4;
}
viewButton.addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
if (event.key.toLowerCase() === "r") {
resetButton.click();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
// ------------------------------------------------------------------
// Animation loop.
// ------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.033);
if (!paused) {
elapsed += dt;
updateParticles(dt);
beltOffset = (beltOffset + dt * 0.72) % 0.2;
for (let i = 0; i < slatCount; i++) {
const x = -1 + ((i * 0.2 + beltOffset) % conveyorLength);
dummy.position.set(x, 0.935, 0);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
slats.setMatrixAt(i, dummy.matrix);
}
slats.instanceMatrix.needsUpdate = true;
}
const damping = 1 - Math.exp(-dt * 11);
currentOrbit.theta = THREE.MathUtils.lerp(
currentOrbit.theta,
desiredOrbit.theta,
damping
);
currentOrbit.phi = THREE.MathUtils.lerp(
currentOrbit.phi,
desiredOrbit.phi,
damping
);
currentOrbit.radius = THREE.MathUtils.lerp(
currentOrbit.radius,
desiredOrbit.radius,
damping
);
applyCamera();
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```