Prompt specification
Build or customize the Three.js 3D scene "Conveyor size-sorting sequence" 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: Conveyor size-sorting sequence
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/conveyor-size-sorting-sequence-x1094
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts produce grading. Move fruits along a short conveyor into bins according to stated diameter ranges; retain their individual colors and IDs so correct routing can be checked. Voxel style.
Working Three.js Starter (conveyor-size-sorting-sequence-x1094):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Produce Grading Line</title>
<style>
:root {
--ink: #183b3a;
--muted: #617371;
--paper: rgba(248, 247, 238, 0.94);
--line: rgba(24, 59, 58, 0.16);
--small: #269d96;
--medium: #e7a52e;
--large: #dc5968;
--shadow: 0 10px 30px rgba(31, 57, 55, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e6;
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.panel {
pointer-events: auto;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.8);
box-shadow: var(--shadow);
backdrop-filter: blur(8px);
}
.title-panel {
position: absolute;
top: 18px;
left: 18px;
width: 315px;
padding: 18px;
border-top: 5px solid var(--ink);
}
.eyebrow {
margin: 0 0 5px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.04em;
}
.subtitle {
margin: 8px 0 15px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.ranges {
display: grid;
gap: 7px;
}
.range {
display: grid;
grid-template-columns: 10px 1fr auto;
align-items: center;
gap: 9px;
padding: 7px 9px;
background: rgba(24, 59, 58, 0.055);
font-size: 10px;
}
.range strong {
font-size: 11px;
}
.range span:last-child {
color: var(--muted);
}
.dot {
width: 9px;
height: 9px;
background: var(--c);
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8);
}
.routing-panel {
position: absolute;
top: 18px;
right: 18px;
width: 402px;
max-height: calc(100vh - 36px);
overflow: hidden;
display: flex;
flex-direction: column;
border-top: 5px solid var(--ink);
}
.panel-head {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
padding: 14px 15px 11px;
border-bottom: 1px solid var(--line);
}
.panel-head h2 {
margin: 0;
font-size: 14px;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.08em;
}
.status::before {
content: "";
width: 8px;
height: 8px;
background: #38a36f;
box-shadow: 0 0 0 3px rgba(56, 163, 111, 0.15);
}
.status.paused::before {
background: #d69b2d;
box-shadow: 0 0 0 3px rgba(214, 155, 45, 0.15);
}
.table-wrap {
overflow: auto;
min-height: 0;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 10px;
}
th {
position: sticky;
top: 0;
z-index: 2;
padding: 8px 7px;
text-align: left;
color: var(--muted);
background: #f4f2e8;
font-size: 9px;
letter-spacing: 0.05em;
text-transform: uppercase;
border-bottom: 1px solid var(--line);
}
td {
padding: 8px 7px;
border-bottom: 1px solid rgba(24, 59, 58, 0.09);
white-space: nowrap;
}
tbody tr {
cursor: pointer;
}
tbody tr:hover,
tbody tr.selected {
background: rgba(230, 165, 46, 0.13);
}
.fruit-id {
font-weight: 900;
}
.swatch {
display: inline-block;
width: 12px;
height: 12px;
vertical-align: -2px;
border: 1px solid rgba(0, 0, 0, 0.15);
box-shadow: inset -3px -3px rgba(0, 0, 0, 0.12);
}
.bin-pill {
display: inline-block;
padding: 3px 5px;
color: white;
background: var(--c);
font-weight: 900;
letter-spacing: 0.04em;
}
.check {
color: #25865f;
font-size: 14px;
font-weight: 900;
text-align: center;
}
.waiting {
color: #8c9996;
font-weight: 800;
text-align: center;
}
.inspector {
padding: 12px 15px 14px;
border-top: 1px solid var(--line);
background: rgba(24, 59, 58, 0.035);
min-height: 82px;
}
.inspector .label {
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.inspector .value {
margin-top: 5px;
font-size: 11px;
line-height: 1.5;
}
.controls {
position: absolute;
left: 18px;
bottom: 18px;
width: 315px;
padding: 12px;
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
}
button {
border: 0;
border-bottom: 3px solid #102d2c;
background: var(--ink);
color: white;
padding: 9px 12px;
cursor: pointer;
font: inherit;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.04em;
}
button:hover {
background: #275654;
}
button:active {
transform: translateY(1px);
border-bottom-width: 2px;
}
button.secondary {
color: var(--ink);
background: #e7e6dc;
border-bottom-color: #b7bbb3;
}
.speed {
display: flex;
align-items: center;
gap: 7px;
margin-left: auto;
color: var(--muted);
font-size: 9px;
font-weight: 900;
}
input[type="range"] {
width: 70px;
accent-color: var(--ink);
}
.hint {
position: absolute;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
padding: 8px 12px;
color: rgba(24, 59, 58, 0.72);
background: rgba(248, 247, 238, 0.68);
border: 1px solid rgba(24, 59, 58, 0.12);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.04em;
white-space: nowrap;
}
.progress {
position: absolute;
top: 18px;
left: 50%;
transform: translateX(-50%);
padding: 9px 13px;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.07em;
}
@media (max-width: 900px) {
.title-panel {
width: 270px;
}
.routing-panel {
width: 350px;
}
.progress,
.hint {
display: none;
}
}
@media (max-width: 680px) {
.title-panel {
top: 10px;
left: 10px;
width: calc(100% - 20px);
padding: 12px;
}
.ranges {
grid-template-columns: repeat(3, 1fr);
}
.range {
grid-template-columns: 8px 1fr;
gap: 5px;
}
.range span:last-child {
display: none;
}
.routing-panel {
top: auto;
right: 10px;
bottom: 10px;
width: calc(100% - 20px);
max-height: 46vh;
}
.controls {
display: none;
}
.subtitle {
display: none;
}
}
</style>
</head>
<body>
<div class="hud">
<section class="panel title-panel">
<p class="eyebrow">Produce QA / Cell 03</p>
<h1>Diameter Grader</h1>
<p class="subtitle">
Voxel produce travels through the scanner. Its measured diameter,
original color, and ID are preserved for routing verification.
</p>
<div class="ranges">
<div class="range" style="--c: var(--small)">
<i class="dot"></i>
<strong>SMALL</strong>
<span>45–69 mm</span>
</div>
<div class="range" style="--c: var(--medium)">
<i class="dot"></i>
<strong>MEDIUM</strong>
<span>70–94 mm</span>
</div>
<div class="range" style="--c: var(--large)">
<i class="dot"></i>
<strong>LARGE</strong>
<span>95–120 mm</span>
</div>
</div>
</section>
<div class="panel progress">
GRADED <span id="gradedCount">0</span> / <span id="fruitCount">0</span>
</div>
<section class="panel routing-panel">
<div class="panel-head">
<h2>Routing verification</h2>
<span class="status" id="runStatus">RUNNING</span>
</div>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Color</th>
<th>ID</th>
<th>Ø</th>
<th>Expected</th>
<th>Actual</th>
<th>Check</th>
</tr>
</thead>
<tbody id="routingBody"></tbody>
</table>
</div>
<div class="inspector">
<div class="label">Inspection</div>
<div class="value" id="inspectorText">
Select a table row or produce item to inspect its trace.
</div>
</div>
</section>
<section class="panel controls">
<button id="pauseButton">PAUSE</button>
<button class="secondary" id="resetButton">RESET</button>
<label class="speed">
SPEED
<input id="speedInput" type="range" min="0.5" max="2" step="0.1" value="1" />
<span id="speedValue">1.0×</span>
</label>
</section>
<div class="hint">
DRAG TO ORBIT · WHEEL TO ZOOM · SHIFT-DRAG TO PAN · SPACE TO PAUSE
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e6);
scene.fog = new THREE.Fog(0xdce9e6, 15, 31);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(-0.25, 0.8, 0);
let cameraYaw = 0.58;
let cameraPitch = 1.0;
let cameraRadius = 12.8;
function updateCamera() {
const horizontal = Math.sin(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraYaw),
cameraTarget.y + Math.cos(cameraPitch) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xf7fff4, 0x71827d, 2.15));
const keyLight = new THREE.DirectionalLight(0xfff4d6, 3.25);
keyLight.position.set(-4, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc7e8ff, 1.1);
fillLight.position.set(7, 5, -6);
scene.add(fillLight);
// ------------------------------------------------------------
// Shared geometry and materials
// ------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.03,
flatShading: true,
...options
});
}
const materials = {
ground: standardMaterial(0xd4dfda),
groundAlt: standardMaterial(0xcbd8d2),
belt: standardMaterial(0x263b3a),
beltEdge: standardMaterial(0x55706c),
slat: standardMaterial(0x38504d),
steel: standardMaterial(0x79928e, { metalness: 0.35 }),
darkSteel: standardMaterial(0x405955, { metalness: 0.25 }),
wood: standardMaterial(0xb98a54),
woodDark: standardMaterial(0x90653f),
cream: standardMaterial(0xe8e2c9),
scanner: standardMaterial(0x83ded5, {
emissive: 0x257d76,
emissiveIntensity: 0.75
}),
stem: standardMaterial(0x654631),
leaf: standardMaterial(0x3c8b42),
small: standardMaterial(0x269d96),
medium: standardMaterial(0xe7a52e),
large: standardMaterial(0xdc5968),
white: standardMaterial(0xf5f1df)
};
function addBox(
parent,
size,
position,
material,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Pixel canvas labels
// ------------------------------------------------------------
function createLabelSprite(lines, accent = "#183b3a", swatch = null) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(248,247,238,0.96)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 13, canvas.height);
ctx.strokeStyle = "rgba(24,59,58,0.45)";
ctx.lineWidth = 5;
ctx.strokeRect(2.5, 2.5, canvas.width - 5, canvas.height - 5);
let x = 32;
if (swatch) {
ctx.fillStyle = swatch;
ctx.fillRect(x, 28, 44, 70);
ctx.strokeStyle = "rgba(0,0,0,0.28)";
ctx.lineWidth = 4;
ctx.strokeRect(x, 28, 44, 70);
x += 62;
}
ctx.textBaseline = "middle";
ctx.fillStyle = "#183b3a";
ctx.font = "900 31px monospace";
if (lines.length === 1) {
ctx.fillText(lines[0], x, 66);
} else {
ctx.fillText(lines[0], x, 43);
ctx.fillStyle = "#617371";
ctx.font = "800 24px monospace";
ctx.fillText(lines[1], x, 84);
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.NearestFilter;
texture.magFilter = THREE.NearestFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.userData.texture = texture;
return sprite;
}
// ------------------------------------------------------------
// Tiled factory floor
// ------------------------------------------------------------
const floor = new THREE.Group();
scene.add(floor);
const tileGeo = new THREE.BoxGeometry(1.55, 0.12, 1.55);
const tileMat = standardMaterial(0xffffff);
const tileCountX = 16;
const tileCountZ = 11;
const tiles = new THREE.InstancedMesh(
tileGeo,
tileMat,
tileCountX * tileCountZ
);
const dummy = new THREE.Object3D();
let tileIndex = 0;
for (let x = 0; x < tileCountX; x++) {
for (let z = 0; z < tileCountZ; z++) {
dummy.position.set(
(x - (tileCountX - 1) / 2) * 1.55,
-0.09,
(z - (tileCountZ - 1) / 2) * 1.55
);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const color = new THREE.Color(
(x + z) % 2 === 0 ? 0xd3dfd9 : 0xc7d5cf
);
tiles.setColorAt(tileIndex, color);
tileIndex++;
}
}
tiles.receiveShadow = true;
scene.add(tiles);
// Decorative background blocks.
const background = new THREE.Group();
scene.add(background);
addBox(background, [2.3, 2.8, 2.1], [-6.6, 1.35, -4.7], materials.cream);
addBox(background, [2.5, 0.18, 2.3], [-6.6, 2.85, -4.7], materials.wood);
addBox(background, [1.2, 1.7, 1.2], [-7.2, 0.82, -3.0], materials.woodDark);
addBox(background, [1.4, 0.25, 1.4], [-7.2, 1.78, -3.0], materials.wood);
addBox(background, [3.3, 0.25, 0.3], [1.4, 3.7, -5.0], materials.steel);
addBox(background, [0.28, 3.4, 0.28], [0.0, 1.8, -5.0], materials.darkSteel);
addBox(background, [0.28, 3.4, 0.28], [2.8, 1.8, -5.0], materials.darkSteel);
// ------------------------------------------------------------
// Conveyor and routing lanes
// ------------------------------------------------------------
const beltTop = 0.98;
const scanX = -0.86;
const binCenterX = 3.78;
const binFloorTop = 0.38;
const conveyor = new THREE.Group();
scene.add(conveyor);
function createConveyor(length, centerX, z, accent) {
const group = new THREE.Group();
group.position.z = z;
addBox(group, [length, 0.28, 1.26], [centerX, 0.77, 0], materials.belt);
addBox(
group,
[length + 0.08, 0.13, 0.13],
[centerX, 0.91, -0.68],
materials.beltEdge
);
addBox(
group,
[length + 0.08, 0.13, 0.13],
[centerX, 0.91, 0.68],
materials.beltEdge
);
const slatCount = Math.floor(length / 0.32);
for (let i = 0; i < slatCount; i++) {
const slatX = centerX - length / 2 + 0.16 + i * 0.32;
addBox(
group,
[0.09, 0.06, 1.13],
[slatX, 0.945, 0],
i % 2 ? materials.slat : materials.beltEdge,
false,
true
);
}
const supportCount = Math.max(2, Math.floor(length / 1.4));
for (let i = 0; i < supportCount; i++) {
const supportX =
centerX - length / 2 + 0.35 + (i * (length - 0.7)) / (supportCount - 1);
addBox(group, [0.13, 0.68, 0.13], [supportX, 0.34, -0.47], materials.darkSteel);
addBox(group, [0.13, 0.68, 0.13], [supportX, 0.34, 0.47], materials.darkSteel);
addBox(group, [0.15, 0.12, 1.2], [supportX, 0.08, 0], materials.darkSteel);
}
// Small voxel direction arrows at the lane edge.
for (let i = 0; i < 3; i++) {
const arrowX = centerX - length * 0.28 + i * 0.62;
const arrowZ = 0.47;
addBox(
group,
[0.45, 0.045, 0.10],
[arrowX, 0.99, arrowZ],
accent,
false,
true
);
const headA = addBox(
group,
[0.22, 0.045, 0.10],
[arrowX + 0.22, 0.99, arrowZ + 0.10],
accent,
false,
true
);
headA.rotation.y = -0.65;
const headB = addBox(
group,
[0.22, 0.045, 0.10],
[arrowX + 0.22, 0.99, arrowZ - 0.10],
accent,
false,
true
);
headB.rotation.y = 0.65;
}
conveyor.add(group);
return group;
}
createConveyor(5.2, -3.1, 0, materials.scanner);
const rangeConfigs = [
{
label: "SMALL",
min: 45,
max: 69,
z: 2.25,
accent: materials.small,
hex: "#269d96"
},
{
label: "MEDIUM",
min: 70,
max: 94,
z: 0,
accent: materials.medium,
hex: "#e7a52e"
},
{
label: "LARGE",
min: 95,
max: 120,
z: -2.25,
accent: materials.large,
hex: "#dc5968"
}
];
rangeConfigs.forEach((range) => {
createConveyor(3.45, 1.0, range.z, range.accent);
});
// Scanner gantry.
const scanner = new THREE.Group();
scanner.position.x = scanX;
scene.add(scanner);
addBox(scanner, [0.26, 2.75, 0.26], [0, 1.38, -1.03], materials.darkSteel);
addBox(scanner, [0.26, 2.75, 0.26], [0, 1.38, 1.03], materials.darkSteel);
addBox(scanner, [0.42, 0.28, 2.35], [0, 2.78, 0], materials.darkSteel);
addBox(scanner, [0.58, 0.35, 0.62], [0, 2.43, 0], materials.scanner);
addBox(scanner, [0.12, 1.13, 1.42], [0, 1.61, 0], materials.scanner);
const scanCurtainMaterial = new THREE.MeshBasicMaterial({
color: 0x54e2d4,
transparent: true,
opacity: 0.2,
depthWrite: false
});
const scanCurtain = new THREE.Mesh(
new THREE.BoxGeometry(0.055, 1.12, 1.42),
scanCurtainMaterial
);
scanCurtain.position.set(scanX, 1.56, 0);
scene.add(scanCurtain);
const scannerLabel = createLabelSprite(
["Ø SCAN", "DIAMETER / 100%"],
"#269d96"
);
scannerLabel.scale.set(2.25, 0.56, 1);
scannerLabel.position.set(scanX, 3.25, 0);
scene.add(scannerLabel);
// ------------------------------------------------------------
// Voxel bins
// ------------------------------------------------------------
function createBin(range) {
const group = new THREE.Group();
group.position.z = range.z;
scene.add(group);
addBox(
group,
[2.8, 0.22, 1.85],
[binCenterX, 0.27, 0],
materials.wood
);
addBox(
group,
[0.15, 0.95, 1.85],
[binCenterX + 1.33, 0.84, 0],
materials.woodDark
);
addBox(
group,
[2.72, 0.82, 0.14],
[binCenterX, 0.79, -0.87],
materials.wood
);
addBox(
group,
[2.72, 0.82, 0.14],
[binCenterX, 0.79, 0.87],
materials.wood
);
// Low front curb permits the produce to visibly drop into the bin.
addBox(
group,
[0.14, 0.27, 1.85],
[binCenterX - 1.33, 0.49, 0],
materials.woodDark
);
// Colored corner caps make destination bins easy to distinguish.
for (const x of [binCenterX - 1.33, binCenterX + 1.33]) {
for (const z of [-0.87, 0.87]) {
addBox(
group,
[0.21, 0.22, 0.21],
[x, 1.28, z],
range.accent
);
}
}
addBox(
group,
[2.82, 0.12, 0.14],
[binCenterX, 1.32, -0.87],
range.accent
);
addBox(
group,
[2.82, 0.12, 0.14],
[binCenterX, 1.32, 0.87],
range.accent
);
const label = createLabelSprite(
[`${range.label} BIN`, `${range.min}-${range.max} mm`],
range.hex
);
label.scale.set(2.55, 0.64, 1);
label.position.set(binCenterX, 1.83, range.z);
scene.add(label);
}
rangeConfigs.forEach(createBin);
// ------------------------------------------------------------
// Produce data and voxel fruit construction
// ------------------------------------------------------------
const fruitData = [
{ id: "AP-101", diameter: 52, color: 0xd84945, kind: "apple" },
{ id: "OR-102", diameter: 63, color: 0xf28a28, kind: "orange" },
{ id: "AP-103", diameter: 68, color: 0xa9bd43, kind: "apple" },
{ id: "AP-104", diameter: 74, color: 0xc73548, kind: "apple" },
{ id: "OR-105", diameter: 83, color: 0xef7925, kind: "orange" },
{ id: "AP-106", diameter: 91, color: 0x9e354c, kind: "apple" },
{ id: "PE-107", diameter: 96, color: 0x70a93c, kind: "pear" },
{ id: "AP-108", diameter: 105, color: 0x8d4a93, kind: "apple" },
{ id: "PE-109", diameter: 117, color: 0xb8bd43, kind: "pear" },
{ id: "OR-110", diameter: 58, color: 0xf59d2c, kind: "orange" }
];
const totalByRange = {
SMALL: fruitData.filter((f) => f.diameter <= 69).length,
MEDIUM: fruitData.filter(
(f) => f.diameter >= 70 && f.diameter <= 94
).length,
LARGE: fruitData.filter((f) => f.diameter >= 95).length
};
function createVoxelFruit(data) {
const group = new THREE.Group();
const baseColor = new THREE.Color(data.color);
const lightColor = baseColor.clone().offsetHSL(0, -0.03, 0.12);
const darkColor = baseColor.clone().offsetHSL(0, 0.03, -0.13);
const bodyMaterial = standardMaterial(data.color);
const lightMaterial = standardMaterial(lightColor);
const darkMaterial = standardMaterial(darkColor);
function part(size, position, material = bodyMaterial) {
return addBox(group, size, position, material);
}
if (data.kind === "pear") {
part([0.58, 0.58, 0.58], [0, -0.08, 0]);
part([0.50, 0.24, 0.50], [0, 0.15, 0]);
part([0.39, 0.38, 0.39], [0, 0.38, 0]);
part([0.27, 0.20, 0.27], [0, 0.59, 0], lightMaterial);
part([0.20, 0.18, 0.21], [-0.27, -0.08, 0.22], darkMaterial);
part([0.20, 0.18, 0.21], [0.27, -0.08, -0.22], darkMaterial);
} else {
part([0.58, 0.58, 0.58], [0, 0, 0]);
part([0.43, 0.23, 0.43], [0, 0.34, 0], lightMaterial);
part([0.45, 0.20, 0.45], [0, -0.33, 0], darkMaterial);
part([0.22, 0.49, 0.50], [-0.34, 0, 0]);
part([0.22, 0.49, 0.50], [0.34, 0, 0]);
part([0.18, 0.18, 0.18], [-0.27, 0.27, 0.27], lightMaterial);
part([0.18, 0.18, 0.18], [0.27, 0.27, -0.27], darkMaterial);
}
if (data.kind !== "orange") {
part([0.10, 0.22, 0.10], [0, 0.68, 0], materials.stem);
const leaf = part([0.23, 0.07, 0.12], [0.13, 0.72, 0], materials.leaf);
leaf.rotation.z = -0.25;
} else {
part([0.09, 0.12, 0.09], [0, 0.53, 0], materials.stem);
}
const worldDiameter = 0.48 + data.diameter * 0.006;
group.scale.setScalar(worldDiameter / 1.1);
return {
...data,
group,
worldDiameter,
radius: worldDiameter / 2,
state: "queued",
x: -5.2,
y: beltTop + worldDiameter / 2,
z: 0,
assigned: null,
slotIndex: 0,
slotX: 0,
slotZ: 0,
settleProgress: 0,
settleStartX: 0,
settleStartY: 0,
settleStartZ: 0,
divertTime: 0,
tag: null,
uiState: ""
};
}
const fruits = fruitData.map((data) => {
const fruit = createVoxelFruit(data);
const colorHex = `#${new THREE.Color(data.color).getHexString()}`;
fruit.tag = createLabelSprite(
[`${data.id} ${data.diameter}mm`],
colorHex,
colorHex
);
fruit.tag.scale.set(1.75, 0.44, 1);
fruit.tag.renderOrder = 12;
scene.add(fruit.group);
scene.add(fruit.tag);
return fruit;
});
// Selection outline.
const selectionMaterial = new THREE.MeshBasicMaterial({
color: 0xffb51f,
wireframe: true,
transparent: true,
opacity: 0.9,
depthTest: false
});
const selectionMarker = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
selectionMaterial
);
selectionMarker.visible = false;
selectionMarker.renderOrder = 30;
scene.add(selectionMarker);
let selectedFruit = null;
// ------------------------------------------------------------
// Grading and routing
// ------------------------------------------------------------
function classifyDiameter(diameter) {
return (
rangeConfigs.find(
(range) => diameter >= range.min && diameter <= range.max
) || null
);
}
const binCounts = {
SMALL: 0,
MEDIUM: 0,
LARGE: 0
};
function resetSimulation() {
simTime = 0;
spawnIndex = 3;
nextSpawnAt = 1.35;
Object.keys(binCounts).forEach((key) => {
binCounts[key] = 0;
});
fruits.forEach((fruit) => {
fruit.state = "queued";
fruit.group.visible = false;
fruit.tag.visible = false;
fruit.assigned = null;
fruit.slotIndex = 0;
fruit.x = -5.2;
fruit.y = beltTop + fruit.radius;
fruit.z = 0;
fruit.uiState = "";
});
[-4.75, -3.30, -1.85].forEach((x, index) => {
const fruit = fruits[index];
fruit.state = "belt";
fruit.group.visible = true;
fruit.tag.visible = true;
fruit.x = x;
fruit.y = beltTop + fruit.radius + 0.03;
fruit.z = 0;
});
selectedFruit = null;
selectionMarker.visible = false;
updateTable();
updateInspector();
}
function activateQueuedFruit() {
const fruit = fruits[spawnIndex++];
fruit.state = "belt";
fruit.group.visible = true;
fruit.tag.visible = true;
fruit.x = -5.15;
fruit.y = beltTop + fruit.radius + 0.03;
fruit.z = 0;
}
function assignRoute(fruit) {
const range = classifyDiameter(fruit.diameter);
if (!range) return;
fruit.assigned = range;
fruit.state = "divert";
fruit.divertTime = 0;
fruit.slotIndex = binCounts[range.label]++;
const spacing = range.label === "SMALL" ? 0.68 : 0.78;
const count = totalByRange[range.label];
fruit.slotX =
binCenterX + (fruit.slotIndex - (count - 1) / 2) * spacing;
fruit.slotZ =
range.z + (fruit.slotIndex % 2 === 0 ? -0.27 : 0.27);
}
function smooth01(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
let simTime = 0;
let spawnIndex = 3;
let nextSpawnAt = 1.35;
let speed = 1;
let paused = false;
function updateSimulation(dt) {
if (paused) return;
const scaledDt = dt * speed;
simTime += scaledDt;
if (spawnIndex < fruits.length && simTime >= nextSpawnAt) {
activateQueuedFruit();
nextSpawnAt += 1.35;
}
const conveyorSpeed = 1.05;
for (const fruit of fruits) {
if (fruit.state === "queued") continue;
if (fruit.state === "belt") {
fruit.x += conveyorSpeed * scaledDt;
if (fruit.x >= scanX) {
assignRoute(fruit);
}
} else if (fruit.state === "divert") {
fruit.divertTime += scaledDt;
fruit.x += conveyorSpeed * scaledDt * 0.78;
const targetZ = fruit.assigned.z;
const progress = smooth01(fruit.divertTime / 1.02);
fruit.z = THREE.MathUtils.lerp(0, targetZ, progress);
if (fruit.divertTime >= 1.02) {
fruit.state = "lane";
fruit.z = targetZ;
}
} else if (fruit.state === "lane") {
fruit.x += conveyorSpeed * scaledDt;
fruit.z = fruit.assigned.z;
if (fruit.x >= 2.78) {
fruit.state = "settling";
fruit.settleProgress = 0;
fruit.settleStartX = fruit.x;
fruit.settleStartY = fruit.y;
fruit.settleStartZ = fruit.z;
}
} else if (fruit.state === "settling") {
fruit.settleProgress += scaledDt / 0.9;
const progress = smooth01(fruit.settleProgress);
fruit.x = THREE.MathUtils.lerp(
fruit.settleStartX,
fruit.slotX,
progress
);
fruit.z = THREE.MathUtils.lerp(
fruit.settleStartZ,
fruit.slotZ,
progress
);
const targetY = binFloorTop + fruit.radius + 0.02;
fruit.y = THREE.MathUtils.lerp(
fruit.settleStartY,
targetY,
progress
);
fruit.y += Math.sin(progress * Math.PI) * 0.18;
if (fruit.settleProgress >= 1) {
fruit.state = "done";
fruit.x = fruit.slotX;
fruit.z = fruit.slotZ;
fruit.y = binFloorTop + fruit.radius + 0.02;
}
}
fruit.group.position.set(fruit.x, fruit.y, fruit.z);
const labelHeight =
fruit.kind === "pear" ? fruit.worldDiameter * 0.78 : fruit.worldDiameter * 0.72;
fruit.tag.position.set(
fruit.x,
fruit.y + labelHeight + 0.27,
fruit.z
);
fruit.tag.visible = fruit.state !== "queued";
const currentUiState =
`${fruit.state}-${fruit.assigned?.label || "none"}`;
if (currentUiState !== fruit.uiState) {
fruit.uiState = currentUiState;
updateTable();
}
}
scanCurtainMaterial.opacity =
0.14 + Math.sin(simTime * 6.0) * 0.045;
}
// ------------------------------------------------------------
// UI table and inspector
// ------------------------------------------------------------
const routingBody = document.getElementById("routingBody");
const gradedCount = document.getElementById("gradedCount");
const fruitCount = document.getElementById("fruitCount");
const inspectorText = document.getElementById("inspectorText");
const runStatus = document.getElementById("runStatus");
fruitCount.textContent = fruits.length;
function updateTable() {
routingBody.innerHTML = "";
let graded = 0;
for (const fruit of fruits) {
if (fruit.assigned) graded++;
const row = document.createElement("tr");
if (selectedFruit === fruit) row.classList.add("selected");
const colorHex = `#${new THREE.Color(fruit.color).getHexString()}`;
const expected = classifyDiameter(fruit.diameter);
const actual = fruit.assigned;
const checked = actual && expected && actual.label === expected.label;
row.innerHTML = `
<td>
<span class="swatch" style="background:${colorHex}" title="${colorHex}"></span>
</td>
<td class="fruit-id">${fruit.id}</td>
<td>${fruit.diameter}</td>
<td>
<span class="bin-pill" style="--c:${expected.hex}">
${expected.label}
</span>
</td>
<td>${
actual
? `<span class="bin-pill" style="--c:${actual.hex}">${actual.label}</span>`
: `<span class="waiting">${fruit.state === "queued" ? "WAIT" : "LINE"}</span>`
}</td>
<td class="${checked ? "check" : "waiting"}">
${checked ? "✓" : "·"}
</td>
`;
row.addEventListener("click", () => {
selectedFruit = selectedFruit === fruit ? null : fruit;
updateTable();
updateInspector();
});
routingBody.appendChild(row);
}
gradedCount.textContent = graded;
}
function updateInspector() {
if (!selectedFruit) {
inspectorText.textContent =
"Select a table row or produce item to inspect its trace.";
return;
}
const fruit = selectedFruit;
const colorHex = `#${new THREE.Color(fruit.color).getHexString()}`;
const kind = fruit.kind.toUpperCase();
const route = fruit.assigned
? `${fruit.assigned.label} (${fruit.assigned.min}-${fruit.assigned.max} mm)`
: "Awaiting scan";
inspectorText.innerHTML = `
<strong>${fruit.id}</strong> · ${fruit.diameter} mm · ${kind}
<br>
Original color <span class="swatch" style="background:${colorHex}"></span>
· Destination: <strong>${route}</strong>
· ${fruit.assigned ? "✓ correct route" : "in process"}
`;
}
// ------------------------------------------------------------
// Controls
// ------------------------------------------------------------
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
const speedInput = document.getElementById("speedInput");
const speedValue = document.getElementById("speedValue");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "RESUME" : "PAUSE";
runStatus.textContent = paused ? "PAUSED" : "RUNNING";
runStatus.classList.toggle("paused", paused);
});
resetButton.addEventListener("click", () => {
resetSimulation();
paused = false;
pauseButton.textContent = "PAUSE";
runStatus.textContent = "RUNNING";
runStatus.classList.remove("paused");
});
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
speedValue.textContent = `${speed.toFixed(1)}×`;
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
if (event.key.toLowerCase() === "r") {
resetButton.click();
}
});
// ------------------------------------------------------------
// Direct camera controls — no addons
// ------------------------------------------------------------
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
let activePointerButton = 0;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
activePointerButton = event.button;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (event.shiftKey || activePointerButton === 2) {
const rightX = Math.cos(cameraYaw);
const rightZ = -Math.sin(cameraYaw);
cameraTarget.x -= rightX * dx * 0.012;
cameraTarget.z -= rightZ * dx * 0.012;
cameraTarget.y += dy * 0.012;
cameraTarget.y = THREE.MathUtils.clamp(cameraTarget.y, 0.1, 3.0);
} else {
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch - dy * 0.005,
0.48,
1.36
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
7.2,
21
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraTarget.set(-0.25, 0.8, 0);
cameraYaw = 0.58;
cameraPitch = 1.0;
cameraRadius = 12.8;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ------------------------------------------------------------
// Main loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
const selectionBounds = new THREE.Box3();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
updateSimulation(dt);
if (selectedFruit) {
selectionBounds.setFromObject(selectedFruit.group);
const size = selectionBounds.getSize(new THREE.Vector3());
const center = selectionBounds.getCenter(new THREE.Vector3());
selectionMarker.visible = true;
selectionMarker.position.copy(center);
selectionMarker.scale.set(
Math.max(size.x, 0.2) * 1.15,
Math.max(size.y, 0.2) * 1.15,
Math.max(size.z, 0.2) * 1.15
);
const pulse = 1 + Math.sin(simTime * 6) * 0.035;
selectionMarker.scale.multiplyScalar(pulse);
} else {
selectionMarker.visible = false;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
resetSimulation();
animate();
</script>
</body>
</html>
```