Prompt specification
Build or customize the Three.js 3D scene "Grain bin hopper geometry" 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 bin hopper geometry
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/grain-bin-hopper-geometry-x1065
Scene Description Prompt:
Show a small storage bin with vertical walls, sloped hopper bottom, and outlet gate; visible grain particles reveal the relationship between container shape and stored volume. Build this with Three.js from cdnjs as a stylized scene in one self-contained HTML file.
Working Three.js Starter (grain-bin-hopper-geometry-x1065):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Storage Bin — Volume Study</title>
<style>
:root {
color-scheme: light;
--ink: #173c3a;
--muted: #617774;
--teal: #2d6861;
--gold: #d9a63a;
--paper: rgba(250, 248, 239, 0.88);
--line: rgba(23, 60, 58, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e5dc;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
}
.glass {
position: fixed;
z-index: 10;
border: 1px solid rgba(255, 255, 255, 0.65);
background: var(--paper);
box-shadow:
0 18px 50px rgba(38, 61, 57, 0.12),
inset 0 0 0 1px rgba(23, 60, 58, 0.035);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title-card {
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 21px 23px 19px;
border-radius: 18px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 36px);
line-height: 1;
letter-spacing: -0.045em;
}
#title-card p {
margin: 12px 0 0;
max-width: 330px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
#readout-card {
top: 24px;
right: 24px;
width: 270px;
padding: 18px;
border-radius: 18px;
}
.readout-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 16px;
margin-bottom: 14px;
}
.readout-label {
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#fill-readout {
font-size: 34px;
font-weight: 750;
line-height: 0.9;
letter-spacing: -0.05em;
}
.fill-track {
position: relative;
height: 9px;
overflow: hidden;
border-radius: 99px;
background: rgba(23, 60, 58, 0.1);
}
#fill-bar {
position: absolute;
inset: 0 auto 0 0;
width: 74%;
border-radius: inherit;
background: linear-gradient(90deg, #bd8123, #e2b650);
transition: width 80ms linear;
}
.capacity-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 15px;
}
.capacity-item {
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.35);
}
.capacity-item strong {
display: block;
margin-bottom: 3px;
font-size: 17px;
}
.capacity-item span {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
#stored-caption {
margin-top: 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
#control-card {
left: 24px;
bottom: 24px;
width: min(380px, calc(100vw - 48px));
padding: 17px 18px;
border-radius: 18px;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.control-heading label {
font-size: 12px;
font-weight: 800;
}
#fill-value {
color: var(--teal);
font-size: 12px;
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 8px 0 15px;
appearance: none;
border-radius: 99px;
outline: none;
background: linear-gradient(
90deg,
var(--gold) 0%,
var(--gold) var(--range, 74%),
rgba(23, 60, 58, 0.13) var(--range, 74%)
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
border: 3px solid #fff;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 2px 8px rgba(23, 60, 58, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #fff;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 2px 8px rgba(23, 60, 58, 0.25);
}
.button-row {
display: flex;
gap: 9px;
}
button {
flex: 1;
min-height: 39px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.5);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.03em;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
color 120ms ease;
}
button:hover {
transform: translateY(-1px);
background: #fff;
}
button:active {
transform: translateY(0);
}
button.active {
border-color: transparent;
background: var(--teal);
color: #fff;
}
#hint {
position: fixed;
right: 25px;
bottom: 24px;
z-index: 8;
color: rgba(23, 60, 58, 0.66);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
pointer-events: none;
}
.tag {
position: fixed;
z-index: 7;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 8px;
background: rgba(250, 248, 239, 0.82);
box-shadow: 0 5px 20px rgba(23, 60, 58, 0.1);
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.1em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
transition: opacity 100ms linear;
}
.tag::after {
content: "";
position: absolute;
top: 50%;
left: 100%;
width: 25px;
height: 1px;
background: rgba(23, 60, 58, 0.35);
}
#tag-hopper::after,
#tag-gate::after {
right: 100%;
left: auto;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #e8e5dc;
color: var(--teal);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
@media (max-width: 760px) {
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 16px 18px;
}
#title-card p {
display: none;
}
#readout-card {
top: 110px;
right: 12px;
width: 220px;
padding: 13px;
}
#control-card {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
#hint {
display: none;
}
.tag {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header id="title-card" class="glass">
<div class="eyebrow">Geometry / Stored Volume</div>
<h1>Storage Bin</h1>
<p>
Grain particles trace the usable space: a constant-section vertical body
sitting over a converging hopper and a controlled outlet.
</p>
</header>
<aside id="readout-card" class="glass">
<div class="readout-row">
<div>
<div class="readout-label">Stored volume</div>
<div id="stored-caption">Calculating…</div>
</div>
<div id="fill-readout">74%</div>
</div>
<div class="fill-track"><div id="fill-bar"></div></div>
<div class="capacity-grid">
<div class="capacity-item">
<strong id="body-capacity">—</strong>
<span>Body capacity</span>
</div>
<div class="capacity-item">
<strong id="hopper-capacity">—</strong>
<span>Hopper capacity</span>
</div>
</div>
</aside>
<section id="control-card" class="glass">
<div class="control-heading">
<label for="fill-slider">Grain level</label>
<span id="fill-value">74%</span>
</div>
<input
id="fill-slider"
type="range"
min="0"
max="100"
value="74"
aria-label="Stored grain level"
/>
<div class="button-row">
<button id="gate-button" type="button">Open outlet gate</button>
<button id="cutaway-button" class="active" type="button">
Cutaway on
</button>
<button id="reset-button" type="button">Reset view</button>
</div>
</section>
<div id="hint">Drag to orbit · Scroll to zoom · Click the gate</div>
<div id="tag-body" class="tag">Vertical body · constant section</div>
<div id="tag-hopper" class="tag">Hopper · tapering section</div>
<div id="tag-gate" class="tag">Sliding outlet gate</div>
<div id="loading">Building storage study…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene setup
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e5dc);
scene.fog = new THREE.Fog(0xe8e5dc, 12, 28);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.querySelector("#scene").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xfff6dc, 0x73817b, 2.15);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff1cf, 4.0);
key.position.set(5.5, 9, 6);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -7;
key.shadow.camera.right = 7;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -5;
key.shadow.normalBias = 0.025;
scene.add(key);
const rimLight = new THREE.DirectionalLight(0xa9d7d0, 2.1);
rimLight.position.set(-6, 5, -5);
scene.add(rimLight);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const materials = {
frame: new THREE.MeshStandardMaterial({
color: 0x244e4b,
roughness: 0.42,
metalness: 0.58
}),
frameLight: new THREE.MeshStandardMaterial({
color: 0x4d7b75,
roughness: 0.44,
metalness: 0.42
}),
solidWall: new THREE.MeshStandardMaterial({
color: 0x527d76,
roughness: 0.56,
metalness: 0.28
}),
clearWall: new THREE.MeshStandardMaterial({
color: 0x75aaa0,
roughness: 0.22,
metalness: 0.1,
transparent: true,
opacity: 0.15,
depthWrite: false,
side: THREE.DoubleSide
}),
solidHopper: new THREE.MeshStandardMaterial({
color: 0x426d67,
roughness: 0.52,
metalness: 0.32,
transparent: true,
opacity: 0.78,
depthWrite: true,
side: THREE.DoubleSide
}),
clearHopper: new THREE.MeshStandardMaterial({
color: 0x78aea4,
roughness: 0.2,
metalness: 0.08,
transparent: true,
opacity: 0.16,
depthWrite: false,
side: THREE.DoubleSide
}),
gate: new THREE.MeshStandardMaterial({
color: 0xc96835,
roughness: 0.38,
metalness: 0.48
}),
gateHandle: new THREE.MeshStandardMaterial({
color: 0x253f3d,
roughness: 0.35,
metalness: 0.7
}),
concrete: new THREE.MeshStandardMaterial({
color: 0xc9c8be,
roughness: 0.9,
metalness: 0.02
}),
floor: new THREE.MeshStandardMaterial({
color: 0xdedbd2,
roughness: 1,
metalness: 0
}),
grain: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.8,
metalness: 0.02
}),
grainSurface: new THREE.MeshStandardMaterial({
color: 0xd5a23b,
roughness: 0.86,
metalness: 0,
transparent: true,
opacity: 0.91
})
};
// ------------------------------------------------------------
// Geometry helpers
// ------------------------------------------------------------
const bin = new THREE.Group();
scene.add(bin);
function addBox(
width,
height,
depth,
x,
y,
z,
material,
castShadow = true,
receiveShadow = true,
parent = bin
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
const up = new THREE.Vector3(0, 1, 0);
function addBeam(
from,
to,
radius,
material = materials.frame,
parent = bin
) {
const a = new THREE.Vector3(...from);
const b = new THREE.Vector3(...to);
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 8),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addSlopedPanel(points, material) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(points.flat(), 3)
);
geometry.setIndex([0, 1, 2, 0, 2, 3]);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = material !== materials.clearHopper;
mesh.receiveShadow = true;
bin.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Bin dimensions
// ------------------------------------------------------------
const W = 3.2;
const D = 2.15;
const BODY_BOTTOM = 1.82;
const BODY_TOP = 4.02;
const BODY_HEIGHT = BODY_TOP - BODY_BOTTOM;
const OUTLET_Y = 1.02;
const OUTLET_W = 0.5;
const OUTLET_D = 0.44;
const HOPPER_HEIGHT = BODY_BOTTOM - OUTLET_Y;
const GATE_Y = 0.72;
// Ground and circular presentation plinth.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
materials.floor
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.19;
floor.receiveShadow = true;
scene.add(floor);
const plinth = new THREE.Mesh(
new THREE.CylinderGeometry(4.7, 5.0, 0.18, 64),
materials.concrete
);
plinth.position.y = -0.09;
plinth.receiveShadow = true;
plinth.castShadow = true;
scene.add(plinth);
const plinthRing = new THREE.Mesh(
new THREE.TorusGeometry(4.72, 0.025, 8, 96),
materials.frameLight
);
plinthRing.rotation.x = Math.PI / 2;
plinthRing.position.y = 0.012;
scene.add(plinthRing);
// ------------------------------------------------------------
// Vertical body walls
// ------------------------------------------------------------
const bodyMidY = (BODY_BOTTOM + BODY_TOP) / 2;
// Rear and left walls remain solid.
addBox(
W,
BODY_HEIGHT,
0.09,
0,
bodyMidY,
-D / 2,
materials.solidWall
);
addBox(
0.09,
BODY_HEIGHT,
D,
-W / 2,
bodyMidY,
0,
materials.solidWall
);
// Front and right walls form the cutaway.
addBox(
W,
BODY_HEIGHT,
0.055,
0,
bodyMidY,
D / 2,
materials.clearWall,
false,
false
);
addBox(
0.055,
BODY_HEIGHT,
D,
W / 2,
bodyMidY,
0,
materials.clearWall,
false,
false
);
// Top rim.
addBox(W + 0.15, 0.13, 0.15, 0, BODY_TOP + 0.04, -D / 2);
addBox(W + 0.15, 0.13, 0.15, 0, BODY_TOP + 0.04, D / 2);
addBox(0.15, 0.13, D + 0.15, -W / 2, BODY_TOP + 0.04, 0);
addBox(0.15, 0.13, D + 0.15, W / 2, BODY_TOP + 0.04, 0);
// Corner posts.
for (const x of [-W / 2, W / 2]) {
for (const z of [-D / 2, D / 2]) {
addBox(0.14, BODY_HEIGHT + 0.08, 0.14, x, bodyMidY, z);
}
}
// Rear reinforcing ribs.
for (const x of [-0.8, 0, 0.8]) {
addBox(0.055, BODY_HEIGHT - 0.08, 0.055, x, bodyMidY, -D / 2 - 0.06);
}
// ------------------------------------------------------------
// Sloped hopper
// ------------------------------------------------------------
const topLeftFront = [-W / 2, BODY_BOTTOM, D / 2];
const topRightFront = [W / 2, BODY_BOTTOM, D / 2];
const bottomLeftFront = [-OUTLET_W / 2, OUTLET_Y, OUTLET_D / 2];
const bottomRightFront = [OUTLET_W / 2, OUTLET_Y, OUTLET_D / 2];
const topRightBack = [W / 2, BODY_BOTTOM, -D / 2];
const topLeftBack = [-W / 2, BODY_BOTTOM, -D / 2];
const bottomRightBack = [OUTLET_W / 2, OUTLET_Y, -OUTLET_D / 2];
const bottomLeftBack = [-OUTLET_W / 2, OUTLET_Y, -OUTLET_D / 2];
addSlopedPanel(
[topLeftFront, topRightFront, bottomRightFront, bottomLeftFront],
materials.clearHopper
);
addSlopedPanel(
[topRightFront, topRightBack, bottomRightBack, bottomRightFront],
materials.clearHopper
);
addSlopedPanel(
[topRightBack, topLeftBack, bottomLeftBack, bottomRightBack],
materials.solidHopper
);
addSlopedPanel(
[topLeftBack, topLeftFront, bottomLeftFront, bottomLeftBack],
materials.solidHopper
);
// Exposed hopper seams emphasize the changing cross-section.
addBeam(topLeftFront, bottomLeftFront, 0.035);
addBeam(topRightFront, bottomRightFront, 0.035);
addBeam(topRightBack, bottomRightBack, 0.035);
addBeam(topLeftBack, bottomLeftBack, 0.035);
addBeam(bottomLeftFront, bottomRightFront, 0.04);
addBeam(bottomLeftBack, bottomRightBack, 0.04);
addBeam(bottomLeftFront, bottomLeftBack, 0.035);
addBeam(bottomRightFront, bottomRightBack, 0.035);
// ------------------------------------------------------------
// Outlet throat and sliding gate
// ------------------------------------------------------------
const throatHeight = OUTLET_Y - GATE_Y;
addBox(
OUTLET_W + 0.08,
throatHeight,
0.05,
0,
GATE_Y + throatHeight / 2,
OUTLET_D / 2,
materials.frameLight
);
addBox(
OUTLET_W + 0.08,
throatHeight,
0.05,
0,
GATE_Y + throatHeight / 2,
-OUTLET_D / 2,
materials.frameLight
);
addBox(
0.05,
throatHeight,
OUTLET_D + 0.08,
-OUTLET_W / 2,
GATE_Y + throatHeight / 2,
0,
materials.frameLight
);
addBox(
0.05,
throatHeight,
OUTLET_D + 0.08,
OUTLET_W / 2,
GATE_Y + throatHeight / 2,
0,
materials.frameLight
);
// Gate rails.
addBox(1.45, 0.065, 0.065, 0.24, GATE_Y - 0.02, OUTLET_D / 2 + 0.02);
addBox(1.45, 0.065, 0.065, 0.24, GATE_Y - 0.02, -OUTLET_D / 2 - 0.02);
const gateGroup = new THREE.Group();
gateGroup.position.y = GATE_Y;
bin.add(gateGroup);
const gatePlate = addBox(
0.73,
0.075,
OUTLET_D + 0.24,
0,
0,
0,
materials.gate,
true,
true,
gateGroup
);
const gateStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 0.35, 10),
materials.gateHandle
);
gateStem.rotation.z = Math.PI / 2;
gateStem.position.set(0.5, 0, 0);
gateStem.castShadow = true;
gateGroup.add(gateStem);
const gateKnob = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 16, 10),
materials.gateHandle
);
gateKnob.position.set(0.69, 0, 0);
gateKnob.castShadow = true;
gateGroup.add(gateKnob);
// ------------------------------------------------------------
// Support frame
// ------------------------------------------------------------
for (const x of [-W / 2 + 0.17, W / 2 - 0.17]) {
for (const z of [-D / 2 + 0.15, D / 2 - 0.15]) {
addBox(0.14, BODY_BOTTOM, 0.14, x, BODY_BOTTOM / 2, z);
addBox(0.34, 0.08, 0.34, x, 0.04, z, materials.frame);
}
}
// Rear cross braces.
addBeam(
[-W / 2 + 0.17, 0.12, -D / 2 + 0.13],
[W / 2 - 0.17, BODY_BOTTOM, -D / 2 + 0.13],
0.035
);
addBeam(
[W / 2 - 0.17, 0.12, -D / 2 + 0.13],
[-W / 2 + 0.17, BODY_BOTTOM, -D / 2 + 0.13],
0.035
);
// Small rear service ladder.
addBeam([0.82, 0.18, -D / 2 - 0.14], [0.82, 4.16, -D / 2 - 0.14], 0.028);
addBeam([1.18, 0.18, -D / 2 - 0.14], [1.18, 4.16, -D / 2 - 0.14], 0.028);
for (let y = 0.42; y < 4.08; y += 0.34) {
addBeam(
[0.82, y, -D / 2 - 0.14],
[1.18, y, -D / 2 - 0.14],
0.022,
materials.frameLight
);
}
// ------------------------------------------------------------
// Volume calculations
// ------------------------------------------------------------
const BODY_AREA = W * D;
const HOPPER_TOP_AREA = W * D;
// Cross-sectional area as a quadratic function of hopper height.
const hopperA0 = OUTLET_W * OUTLET_D;
const hopperA1 =
OUTLET_W * (D - OUTLET_D) + (W - OUTLET_W) * OUTLET_D;
const hopperA2 = (W - OUTLET_W) * (D - OUTLET_D);
function hopperAreaAt(t) {
return (
hopperA0 +
hopperA1 * t +
hopperA2 * t * t
);
}
function hopperVolumeAt(t) {
return (
HOPPER_HEIGHT *
(hopperA0 + (hopperA1 * t) / 2 + (hopperA2 * t * t) / 3)
);
}
const HOPPER_VOLUME = hopperVolumeAt(1);
const BODY_VOLUME = BODY_AREA * BODY_HEIGHT;
const TOTAL_VOLUME = BODY_VOLUME + HOPPER_VOLUME;
function dimensionsAtY(y) {
if (y <= OUTLET_Y) {
return { width: OUTLET_W, depth: OUTLET_D };
}
if (y >= BODY_BOTTOM) {
return { width: W, depth: D };
}
const t = (y - OUTLET_Y) / HOPPER_HEIGHT;
return {
width: THREE.MathUtils.lerp(OUTLET_W, W, t),
depth: THREE.MathUtils.lerp(OUTLET_D, D, t)
};
}
function getFillY(fillFraction) {
const target = fillFraction * TOTAL_VOLUME;
if (target <= HOPPER_VOLUME) {
let low = 0;
let high = 1;
for (let i = 0; i < 24; i++) {
const middle = (low + high) / 2;
if (hopperVolumeAt(middle) < target) {
low = middle;
} else {
high = middle;
}
}
const t = (low + high) / 2;
return OUTLET_Y + t * HOPPER_HEIGHT;
}
return BODY_BOTTOM + (target - HOPPER_VOLUME) / BODY_AREA;
}
document.querySelector("#body-capacity").textContent =
`${BODY_VOLUME.toFixed(1)} m³`;
document.querySelector("#hopper-capacity").textContent =
`${HOPPER_VOLUME.toFixed(1)} m³`;
// ------------------------------------------------------------
// Grain particle systems
// ------------------------------------------------------------
const MAX_GRAINS = 1500;
const MAX_FLOW_GRAINS = 90;
const MAX_PILE_GRAINS = 380;
const grainGeometry = new THREE.SphereGeometry(1, 7, 5);
const grainMesh = new THREE.InstancedMesh(
grainGeometry,
materials.grain,
MAX_GRAINS
);
grainMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
grainMesh.castShadow = true;
grainMesh.receiveShadow = true;
grainMesh.frustumCulled = false;
scene.add(grainMesh);
const flowMesh = new THREE.InstancedMesh(
grainGeometry,
materials.grain,
MAX_FLOW_GRAINS
);
flowMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
flowMesh.castShadow = true;
flowMesh.frustumCulled = false;
scene.add(flowMesh);
const pileMesh = new THREE.InstancedMesh(
grainGeometry,
materials.grain,
MAX_PILE_GRAINS
);
pileMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
pileMesh.castShadow = true;
pileMesh.receiveShadow = true;
pileMesh.frustumCulled = false;
scene.add(pileMesh);
const grainColors = [
new THREE.Color(0xd6a039),
new THREE.Color(0xe0b34d),
new THREE.Color(0xc98c27),
new THREE.Color(0xe7bf61)
];
for (let i = 0; i < MAX_GRAINS; i++) {
grainMesh.setColorAt(
i,
grainColors[i % grainColors.length].clone().offsetHSL(
0,
0,
(random01(i, 44) - 0.5) * 0.08
)
);
}
for (let i = 0; i < MAX_FLOW_GRAINS; i++) {
flowMesh.setColorAt(
i,
grainColors[i % grainColors.length].clone().offsetHSL(
0,
0,
(random01(i, 48) - 0.5) * 0.08
)
);
}
for (let i = 0; i < MAX_PILE_GRAINS; i++) {
pileMesh.setColorAt(
i,
grainColors[i % grainColors.length].clone().offsetHSL(
0,
0,
(random01(i, 52) - 0.5) * 0.08
)
);
}
grainMesh.instanceColor.needsUpdate = true;
flowMesh.instanceColor.needsUpdate = true;
pileMesh.instanceColor.needsUpdate = true;
const grainSurface = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
materials.grainSurface
);
grainSurface.receiveShadow = true;
scene.add(grainSurface);
function random01(index, salt = 0) {
const value = Math.sin(
(index + 1) * 12.9898 + salt * 78.233
) * 43758.5453123;
return value - Math.floor(value);
}
const dummy = new THREE.Object3D();
function updateStoredGrains(fillFraction) {
if (fillFraction <= 0.0001) {
grainMesh.count = 0;
grainSurface.visible = false;
return;
}
const fillY = getFillY(fillFraction);
const count = Math.max(
8,
Math.floor(MAX_GRAINS * Math.min(1, fillFraction))
);
const bulkCount = Math.floor(count * 0.82);
const surfaceCount = count - bulkCount;
// Bulk particles are rejection-sampled so their density remains
// approximately uniform per unit volume as the hopper narrows.
for (let i = 0; i < bulkCount; i++) {
let y = OUTLET_Y;
let normalizedX = 0;
let normalizedZ = 0;
let attempt = 0;
let areaRatio;
do {
const sample = random01(i * 17 + attempt, 1);
y =
OUTLET_Y +
sample * Math.max(0.001, fillY - OUTLET_Y);
normalizedX = random01(i * 19 + attempt, 2) * 2 - 1;
normalizedZ = random01(i * 23 + attempt, 3) * 2 - 1;
const area = hopperAreaAt(
THREE.MathUtils.clamp(
(y - OUTLET_Y) / HOPPER_HEIGHT,
0,
1
)
);
areaRatio = Math.min(1, area / HOPPER_TOP_AREA);
attempt++;
} while (
attempt < 20 &&
random01(i * 29 + attempt, 4) > areaRatio
);
const dimensions = dimensionsAtY(y);
const radius = 0.037 + random01(i, 5) * 0.012;
dummy.position.set(
normalizedX * dimensions.width * 0.47,
y + (random01(i, 6) - 0.5) * 0.025,
normalizedZ * dimensions.depth * 0.47
);
dummy.scale.setScalar(radius);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
grainMesh.setMatrixAt(i, dummy.matrix);
}
// A denser layer at the current surface makes the fill level legible.
const surfaceDimensions = dimensionsAtY(fillY);
for (let j = 0; j < surfaceCount; j++) {
const i = bulkCount + j;
const y =
fillY -
random01(i, 7) * 0.085 -
0.006;
dummy.position.set(
(random01(i, 8) * 2 - 1) * surfaceDimensions.width * 0.47,
y,
(random01(i, 9) * 2 - 1) * surfaceDimensions.depth * 0.47
);
dummy.scale.setScalar(0.039 + random01(i, 10) * 0.012);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
grainMesh.setMatrixAt(i, dummy.matrix);
}
grainMesh.count = count;
grainMesh.instanceMatrix.needsUpdate = true;
grainSurface.visible = true;
grainSurface.position.set(0, fillY + 0.012, 0);
grainSurface.scale.set(
surfaceDimensions.width * 0.94,
0.032,
surfaceDimensions.depth * 0.94
);
}
function updateFlow(time) {
if (!gateOpen || currentFill <= 0.005) {
flowMesh.count = 0;
return;
}
flowMesh.count = MAX_FLOW_GRAINS;
for (let i = 0; i < MAX_FLOW_GRAINS; i++) {
const phase = (time * 1.55 + random01(i, 61)) % 1;
const radius = 0.031 + random01(i, 62) * 0.012;
dummy.position.set(
(random01(i, 63) - 0.5) * 0.12 +
Math.sin(time * 5 + i) * 0.012,
Math.max(0.035, GATE_Y - 0.07 - phase * 0.64),
(random01(i, 64) - 0.5) * 0.11
);
dummy.scale.setScalar(radius);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
flowMesh.setMatrixAt(i, dummy.matrix);
}
flowMesh.instanceMatrix.needsUpdate = true;
}
function updatePile() {
const pileFraction = THREE.MathUtils.clamp(
drainedVolume / TOTAL_VOLUME,
0,
1
);
const count = Math.floor(MAX_PILE_GRAINS * pileFraction);
pileMesh.count = count;
if (count === 0) return;
const pileRadius = 0.18 + Math.min(0.95, pileFraction * 1.45);
const pileHeight = 0.04 + Math.min(0.32, pileFraction * 0.52);
for (let i = 0; i < count; i++) {
const angle = random01(i, 71) * Math.PI * 2;
const radius = Math.sqrt(random01(i, 72)) * pileRadius;
dummy.position.set(
Math.cos(angle) * radius,
0.035 +
pileHeight * (1 - radius / pileRadius) +
random01(i, 73) * 0.03,
Math.sin(angle) * radius
);
dummy.scale.setScalar(0.037 + random01(i, 74) * 0.012);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
pileMesh.setMatrixAt(i, dummy.matrix);
}
pileMesh.instanceMatrix.needsUpdate = true;
}
// ------------------------------------------------------------
// Interaction state
// ------------------------------------------------------------
let currentFill = 0.74;
let desiredFill = 0.74;
let gateOpen = false;
let cutaway = true;
let drainedVolume = 0;
let lastRenderedFill = -1;
const fillSlider = document.querySelector("#fill-slider");
const fillValue = document.querySelector("#fill-value");
const fillReadout = document.querySelector("#fill-readout");
const fillBar = document.querySelector("#fill-bar");
const storedCaption = document.querySelector("#stored-caption");
const gateButton = document.querySelector("#gate-button");
const cutawayButton = document.querySelector("#cutaway-button");
function setGate(open) {
gateOpen = open;
gateButton.textContent = open ? "Close outlet gate" : "Open outlet gate";
gateButton.classList.toggle("active", open);
}
function setCutaway(enabled) {
cutaway = enabled;
materials.clearWall.opacity = enabled ? 0.15 : 0.78;
materials.clearWall.depthWrite = !enabled;
materials.clearWall.needsUpdate = true;
materials.clearHopper.opacity = enabled ? 0.16 : 0.8;
materials.clearHopper.depthWrite = !enabled;
materials.clearHopper.needsUpdate = true;
cutawayButton.textContent = enabled ? "Cutaway on" : "Cutaway off";
cutawayButton.classList.toggle("active", enabled);
}
fillSlider.addEventListener("input", () => {
desiredFill = Number(fillSlider.value) / 100;
drainedVolume = 0;
updatePile();
});
gateButton.addEventListener("click", () => setGate(!gateOpen));
cutawayButton.addEventListener("click", () => setCutaway(!cutaway));
// ------------------------------------------------------------
// Custom camera controls: orbit, zoom, and clickable gate
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 2.05, 0);
const defaultCamera = {
radius: 8.7,
theta: 0.62,
phi: 1.14
};
let cameraRadius = defaultCamera.radius;
let cameraTheta = defaultCamera.theta;
let cameraPhi = defaultCamera.phi;
let autoRotate = true;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
let pointerDownX = 0;
let pointerDownY = 0;
let pointerMoved = false;
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerMoved = false;
pointerX = event.clientX;
pointerY = event.clientY;
pointerDownX = event.clientX;
pointerDownY = event.clientY;
autoRotate = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (
Math.abs(event.clientX - pointerDownX) +
Math.abs(event.clientY - pointerDownY) >
5
) {
pointerMoved = true;
}
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.56,
1.45
);
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
if (!pointerMoved) {
pointerNDC.x = (event.clientX / innerWidth) * 2 - 1;
pointerNDC.y = -(event.clientY / innerHeight) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
if (raycaster.intersectObject(gatePlate, false).length > 0) {
setGate(!gateOpen);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
autoRotate = false;
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
6.2,
13
);
},
{ passive: false }
);
function resetView() {
cameraRadius = defaultCamera.radius;
cameraTheta = defaultCamera.theta;
cameraPhi = defaultCamera.phi;
autoRotate = true;
}
document.querySelector("#reset-button").addEventListener(
"click",
resetView
);
renderer.domElement.addEventListener("dblclick", resetView);
// ------------------------------------------------------------
// Projected labels
// ------------------------------------------------------------
const labels = [
{
element: document.querySelector("#tag-body"),
position: new THREE.Vector3(-1.15, 3.62, 1.02)
},
{
element: document.querySelector("#tag-hopper"),
position: new THREE.Vector3(1.35, 1.35, 0.82)
},
{
element: document.querySelector("#tag-gate"),
position: new THREE.Vector3(0.67, 0.68, 0.2)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of labels) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
label.element.style.opacity = visible ? "1" : "0";
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * innerWidth}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
addEventListener("resize", resize);
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
// Open gate drains the bin gradually.
if (gateOpen && desiredFill > 0) {
const before = desiredFill;
desiredFill = Math.max(0, desiredFill - delta * 0.085);
drainedVolume += (before - desiredFill) * TOTAL_VOLUME;
fillSlider.value = Math.round(desiredFill * 100);
updatePile();
}
currentFill = THREE.MathUtils.lerp(
currentFill,
desiredFill,
1 - Math.exp(-delta * 5.5)
);
if (Math.abs(currentFill - lastRenderedFill) > 0.00001) {
updateStoredGrains(currentFill);
lastRenderedFill = currentFill;
}
// Slide the outlet gate.
const gateTargetX = gateOpen ? 0.55 : 0;
gateGroup.position.x = THREE.MathUtils.lerp(
gateGroup.position.x,
gateTargetX,
1 - Math.exp(-delta * 8)
);
updateFlow(elapsed);
// Camera.
if (autoRotate && !dragging) {
cameraTheta += delta * 0.045;
}
const horizontalRadius = cameraRadius * Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x +
horizontalRadius * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z +
horizontalRadius * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
updateLabels();
// UI readouts.
const percent = Math.round(currentFill * 100);
const stored = currentFill * TOTAL_VOLUME;
fillReadout.textContent = `${percent}%`;
fillValue.textContent = `${percent}%`;
fillBar.style.width = `${currentFill * 100}%`;
storedCaption.textContent =
`${stored.toFixed(2)} of ${TOTAL_VOLUME.toFixed(2)} m³ usable capacity`;
fillSlider.style.setProperty(
"--range",
`${desiredFill * 100}%`
);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
updateStoredGrains(currentFill);
updatePile();
requestAnimationFrame(() => {
const loading = document.querySelector("#loading");
loading.style.opacity = "0";
setTimeout(() => loading.remove(), 320);
});
</script>
</body>
</html>
```