Prompt specification
Build or customize the Three.js 3D scene "Shape Sorter With a Borderline Item" 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: Shape Sorter With a Borderline Item
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/shape-sorter-with-a-borderline-item-x2042
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this voxel scene: Create a sorter using explicit size and aspect-ratio rules. Include a borderline case and display which measurement crosses the threshold before routing the item.
Working Three.js Starter (shape-sorter-with-a-borderline-item-x2042):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Sorting Bay</title>
<style>
:root {
--bg: #071019;
--panel: rgba(9, 20, 31, 0.88);
--line: rgba(137, 214, 255, 0.18);
--text: #eaf8ff;
--muted: #87a8b9;
--cyan: #64e8ff;
--green: #5ce6a5;
--orange: #ffb454;
--pink: #ff6fae;
--blue: #72a8ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 20px;
left: 22px;
display: flex;
align-items: center;
gap: 12px;
text-shadow: 0 2px 16px #000;
}
.brand-mark {
width: 12px;
height: 12px;
background: var(--cyan);
box-shadow:
6px 0 0 rgba(100, 232, 255, 0.35),
0 6px 0 rgba(100, 232, 255, 0.35);
}
.brand h1 {
margin: 0;
font-size: 15px;
letter-spacing: 0.16em;
font-weight: 800;
}
.brand p {
margin: 3px 0 0;
color: var(--muted);
font: 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
.panel {
position: absolute;
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--line);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.35),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(15px);
border-radius: 12px;
}
.measurement {
left: 22px;
top: 84px;
width: 318px;
padding: 17px;
}
.panel-kicker {
margin: 0 0 6px;
color: var(--cyan);
font: 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.measurement h2 {
margin: 0;
font-size: 18px;
letter-spacing: -0.02em;
}
.state-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 14px 0 12px;
}
#phase {
color: var(--muted);
font: 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
}
.state-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
}
.metric {
display: grid;
grid-template-columns: 1fr auto;
gap: 5px 12px;
padding: 10px 0;
border-top: 1px solid rgba(137, 214, 255, 0.1);
}
.metric-name {
color: var(--muted);
font: 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
}
.metric-value {
text-align: right;
font: 700 14px/1.1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.metric-threshold {
grid-column: 1 / -1;
color: #6f91a3;
font: 10px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.result {
align-self: center;
min-width: 55px;
padding: 4px 6px;
border-radius: 4px;
text-align: center;
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.result.pass {
color: var(--green);
background: rgba(92, 230, 165, 0.1);
}
.result.crossed {
color: var(--orange);
background: rgba(255, 180, 84, 0.12);
}
.result.boundary {
color: #071019;
background: var(--orange);
}
.trigger {
margin-top: 12px;
padding: 12px;
min-height: 62px;
border: 1px solid rgba(255, 180, 84, 0.3);
border-radius: 8px;
background: rgba(255, 180, 84, 0.06);
}
.trigger strong {
display: block;
margin-bottom: 5px;
color: var(--orange);
font: 800 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.trigger span {
color: #d8eaf1;
font: 11px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.rules {
top: 20px;
right: 22px;
width: 302px;
padding: 17px;
}
.rules h2 {
margin: 0 0 12px;
font-size: 14px;
letter-spacing: 0.02em;
}
.rule-row {
display: grid;
grid-template-columns: 26px 1fr;
gap: 9px;
padding: 9px 0;
border-top: 1px solid rgba(137, 214, 255, 0.1);
}
.rule-index {
width: 23px;
height: 23px;
display: grid;
place-items: center;
border: 1px solid var(--line);
border-radius: 5px;
color: var(--cyan);
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.rule-code {
color: #effbff;
font: 700 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.rule-description {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.lane {
color: var(--cyan);
font-weight: 800;
}
.controls {
position: absolute;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
display: flex;
align-items: stretch;
gap: 8px;
pointer-events: auto;
padding: 8px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(7, 16, 25, 0.88);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.36);
backdrop-filter: blur(15px);
}
.case-button {
min-width: 108px;
padding: 9px 12px;
color: var(--muted);
text-align: left;
border: 1px solid transparent;
border-radius: 7px;
background: transparent;
cursor: pointer;
transition: 150ms ease;
}
.case-button:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.04);
}
.case-button.active {
color: var(--text);
border-color: rgba(100, 232, 255, 0.38);
background: rgba(100, 232, 255, 0.09);
}
.case-button span {
display: block;
font-size: 11px;
font-weight: 750;
}
.case-button small {
display: block;
margin-top: 4px;
color: var(--muted);
font: 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#replay {
min-width: 102px;
padding: 9px 15px;
color: #061018;
border: 0;
border-radius: 7px;
background: var(--cyan);
font-size: 11px;
font-weight: 850;
cursor: pointer;
}
#replay:hover {
background: #a4f3ff;
}
.camera-help {
position: absolute;
right: 23px;
bottom: 24px;
color: rgba(151, 183, 199, 0.62);
font: 9px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.07em;
text-align: right;
}
@media (max-width: 900px) {
.rules {
display: none;
}
.measurement {
width: 286px;
top: 76px;
}
.controls {
max-width: calc(100vw - 20px);
overflow-x: auto;
}
.case-button {
min-width: 96px;
}
.camera-help {
display: none;
}
}
@media (max-width: 560px) {
.measurement {
left: 10px;
top: 69px;
width: 260px;
padding: 13px;
}
.brand {
left: 12px;
top: 15px;
}
.brand h1 {
font-size: 12px;
}
.brand p {
font-size: 8px;
}
.metric {
padding: 7px 0;
}
.controls {
left: 10px;
right: 10px;
bottom: 10px;
transform: none;
}
#replay {
min-width: 82px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div class="hud">
<header class="brand">
<div class="brand-mark"></div>
<div>
<h1>VOXEL SORTING BAY</h1>
<p>MEASUREMENT ENGINE / BAY 04</p>
</div>
</header>
<section class="panel measurement">
<p class="panel-kicker">Live measurement</p>
<h2 id="caseName">Borderline Wide</h2>
<div class="state-row">
<span id="phase">INITIALIZING</span>
<span class="state-dot" id="stateDot"></span>
</div>
<div class="metric">
<span class="metric-name">WIDTH</span>
<span class="metric-value" id="widthValue">—</span>
<span class="metric-threshold">Compact rule: width ≤ 1.20 units</span>
<span class="result pass" id="widthResult">PASS</span>
</div>
<div class="metric">
<span class="metric-name">HEIGHT</span>
<span class="metric-value" id="heightValue">—</span>
<span class="metric-threshold">Tall rule: height > 1.80 units</span>
<span class="result pass" id="heightResult">PASS</span>
</div>
<div class="metric">
<span class="metric-name">ASPECT RATIO</span>
<span class="metric-value" id="aspectValue">—</span>
<span class="metric-threshold">Wide rule: width / height ≥ 1.50</span>
<span class="result pass" id="aspectResult">PASS</span>
</div>
<div class="trigger">
<strong id="triggerTitle">WAITING FOR SCANNER</strong>
<span id="triggerText">No routing decision has been committed.</span>
</div>
</section>
<aside class="panel rules">
<p class="panel-kicker">Fixed decision order</p>
<h2>Routing rules</h2>
<div class="rule-row">
<div class="rule-index">01</div>
<div>
<div class="rule-code"><span class="lane">WIDE</span> if aspect ≥ 1.50</div>
<div class="rule-description">Equality counts, so an exact 1.50 is wide.</div>
</div>
</div>
<div class="rule-row">
<div class="rule-index">02</div>
<div>
<div class="rule-code"><span class="lane">TALL</span> if height > 1.80</div>
<div class="rule-description">Strict comparison: exactly 1.80 is not tall.</div>
</div>
</div>
<div class="rule-row">
<div class="rule-index">03</div>
<div>
<div class="rule-code"><span class="lane">COMPACT</span> if width ≤ 1.20</div>
<div class="rule-description">Both width and height must be ≤ 1.20.</div>
</div>
</div>
<div class="rule-row">
<div class="rule-index">04</div>
<div>
<div class="rule-code"><span class="lane">STANDARD</span> otherwise</div>
<div class="rule-description">Catch-all lane after the first three tests.</div>
</div>
</div>
</aside>
<div class="controls">
<button class="case-button active" data-case="border">
<span>Borderline</span>
<small>2.25 × 1.50 / ratio 1.50</small>
</button>
<button class="case-button" data-case="near">
<span>Near miss</span>
<small>2.24 × 1.50 / ratio 1.49</small>
</button>
<button class="case-button" data-case="tall">
<span>Tall item</span>
<small>1.20 × 2.10 / height</small>
</button>
<button class="case-button" data-case="compact">
<span>Compact item</span>
<small>1.20 × 1.20 / size</small>
</button>
<button id="replay">RUN CASE</button>
</div>
<div class="camera-help">
DRAG TO ORBIT · WHEEL TO ZOOM<br>
DOUBLE-CLICK TO RESET CAMERA
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Constants and test cases
// ------------------------------------------------------------
const RULES = Object.freeze({
wideAspect: 1.50,
tallHeight: 1.80,
compactWidth: 1.20,
compactHeight: 1.20
});
const CASES = {
border: {
name: "Borderline Wide",
width: 2.25,
height: 1.50,
color: 0xffb454
},
near: {
name: "Near-Miss Standard",
width: 2.24,
height: 1.50,
color: 0x72a8ff
},
tall: {
name: "Tall Item",
width: 1.20,
height: 2.10,
color: 0xff6fae
},
compact: {
name: "Compact Item",
width: 1.20,
height: 1.20,
color: 0x5ce6a5
}
};
const LANES = {
WIDE: {
z: -4.2,
color: 0xffb454,
css: "#ffb454"
},
TALL: {
z: 0,
color: 0xff6fae,
css: "#ff6fae"
},
STANDARD: {
z: 2.1,
color: 0x72a8ff,
css: "#72a8ff"
},
COMPACT: {
z: 4.2,
color: 0x5ce6a5,
css: "#5ce6a5"
}
};
const $ = (id) => document.getElementById(id);
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
const smoothstep = (t) => t * t * (3 - 2 * t);
function classify(item) {
const aspect = item.width / item.height;
// The epsilon only protects the inclusive decimal boundary.
if (aspect + 1e-9 >= RULES.wideAspect) {
return {
lane: "WIDE",
trigger: "ASPECT RATIO",
reason: `${aspect.toFixed(2)} ≥ ${RULES.wideAspect.toFixed(2)}`,
crossed: true
};
}
if (item.height > RULES.tallHeight) {
return {
lane: "TALL",
trigger: "HEIGHT",
reason: `${item.height.toFixed(2)} > ${RULES.tallHeight.toFixed(2)}`,
crossed: true
};
}
if (
item.width <= RULES.compactWidth &&
item.height <= RULES.compactHeight
) {
return {
lane: "COMPACT",
trigger: "COMPACT SIZE",
reason:
`${item.width.toFixed(2)} ≤ ${RULES.compactWidth.toFixed(2)} and ` +
`${item.height.toFixed(2)} ≤ ${RULES.compactHeight.toFixed(2)}`,
crossed: false
};
}
return {
lane: "STANDARD",
trigger: "NO THRESHOLD",
reason: "Falls through to the standard lane",
crossed: false
};
}
// ------------------------------------------------------------
// Renderer, scene and camera
// ------------------------------------------------------------
const app = $("app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 18, 38);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.2;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb8eaff, 0x101521, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xe4f7ff, 3.3);
keyLight.position.set(3, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x43dfff, 25, 15, 2);
rimLight.position.set(-4, 4, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9f55, 18, 12, 2);
warmLight.position.set(6, 3, 4);
scene.add(warmLight);
// Custom orbit implementation; no Three.js examples/addons are used.
const orbit = {
theta: 0.68,
phi: 1.02,
radius: innerWidth < 700 ? 21 : 18,
target: new THREE.Vector3(0, 1.1, 0)
};
const defaultOrbit = {
theta: 0.68,
phi: 1.02,
radius: innerWidth < 700 ? 21 : 18
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
let dragging = false;
let previousPointer = { x: 0, y: 0 };
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointer = { x: event.clientX, y: event.clientY };
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
orbit.theta -= dx * 0.006;
orbit.phi = clamp(orbit.phi - dy * 0.005, 0.45, 1.38);
previousPointer = { x: event.clientX, y: event.clientY };
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
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();
orbit.radius = clamp(orbit.radius + event.deltaY * 0.012, 11, 28);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.theta = defaultOrbit.theta;
orbit.phi = defaultOrbit.phi;
orbit.radius = defaultOrbit.radius;
orbit.target.set(0, 1.1, 0);
});
// ------------------------------------------------------------
// Geometry helpers
// ------------------------------------------------------------
const materialCache = new Map();
function getMaterial(color, options = {}) {
const key = `${color}-${JSON.stringify(options)}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.08,
...options
})
);
}
return materialCache.get(key);
}
function addBox(width, height, depth, color, x, y, z, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
getMaterial(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function makeTextSprite(text, color = "#dff9ff", width = 512) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = 128;
const context = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(3.2, 0.8, 1);
function update(nextText, nextColor = color) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(6, 15, 24, 0.88)";
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = nextColor;
context.lineWidth = 5;
context.strokeRect(3, 3, canvas.width - 6, canvas.height - 6);
context.fillStyle = nextColor;
context.font = "700 38px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(nextText, canvas.width / 2, canvas.height / 2 + 2);
texture.needsUpdate = true;
}
update(text);
return { sprite, update, texture, material };
}
// ------------------------------------------------------------
// Voxel factory scene
// ------------------------------------------------------------
const floor = addBox(30, 0.4, 25, 0x09131d, 0, -0.35, 0);
floor.receiveShadow = true;
const grid = new THREE.GridHelper(30, 30, 0x244052, 0x132633);
grid.position.y = -0.14;
scene.add(grid);
// Conveyor body.
addBox(14.3, 0.38, 3.15, 0x152a38, -0.1, 0.37, 0);
addBox(14.0, 0.18, 2.75, 0x071017, -0.1, 0.67, 0);
addBox(14.4, 0.18, 0.18, 0x3a5968, -0.1, 0.59, 1.62);
addBox(14.4, 0.18, 0.18, 0x3a5968, -0.1, 0.59, -1.62);
for (const x of [-6, -3, 0, 3]) {
for (const z of [-1.35, 1.35]) {
addBox(0.28, 1.25, 0.28, 0x253d4b, x, -0.18, z);
addBox(0.65, 0.16, 0.65, 0x142631, x, -0.82, z);
}
}
// Moving belt markers.
const beltMarkers = [];
for (let i = 0; i < 18; i++) {
const marker = addBox(
0.075,
0.025,
2.45,
0x294451,
-6.45 + i * 0.72,
0.785,
0,
{ emissive: 0x0b1c23, emissiveIntensity: 0.35 }
);
beltMarkers.push(marker);
}
// Scanner gantry.
const SCANNER_X = -1.7;
const BELT_Y = 0.80;
for (const z of [-1.72, 1.72]) {
addBox(0.55, 3.65, 0.55, 0x213846, SCANNER_X, 2.22, z);
addBox(0.64, 0.42, 0.64, 0x64e8ff, SCANNER_X, 3.7, z, {
emissive: 0x1a91a7,
emissiveIntensity: 1.1
});
}
addBox(0.55, 0.55, 3.95, 0x213846, SCANNER_X, 4.08, 0);
addBox(0.62, 0.16, 3.2, 0x64e8ff, SCANNER_X, 3.76, 0, {
emissive: 0x168ca0,
emissiveIntensity: 1.2
});
const scannerLabel = makeTextSprite("MEASURE / 04", "#64e8ff");
scannerLabel.sprite.position.set(SCANNER_X, 4.65, 0);
scene.add(scannerLabel.sprite);
const scanSheetMaterial = new THREE.MeshBasicMaterial({
color: 0x54eaff,
transparent: true,
opacity: 0,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const scanSheet = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 3.0, 2.45),
scanSheetMaterial
);
scanSheet.position.set(SCANNER_X, 2.0, 0);
scene.add(scanSheet);
const scanLineMaterial = new THREE.MeshBasicMaterial({
color: 0xb6f8ff,
transparent: true,
opacity: 0,
depthWrite: false
});
const scanLine = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.045, 2.55),
scanLineMaterial
);
scanLine.position.set(SCANNER_X, 1.0, 0);
scene.add(scanLine);
// Voxel side machinery.
for (let i = 0; i < 8; i++) {
addBox(
0.72,
0.72 + (i % 3) * 0.35,
0.72,
i % 2 ? 0x193744 : 0x142b38,
-8.8 + (i % 4) * 0.85,
0.25 + (0.72 + (i % 3) * 0.35) / 2,
-5.3 - Math.floor(i / 4) * 0.9
);
}
// Sorting deck and branch ramps.
const DIVERTER_X = 2.8;
addBox(2.0, 0.32, 3.2, 0x203a48, 2.8, 0.47, 0);
addBox(0.30, 0.95, 3.0, 0x1a303d, DIVERTER_X, 0.94, 0);
for (const [name, lane] of Object.entries(LANES)) {
const ramp = addBox(
4.4,
0.22,
1.42,
0x142c38,
5.0,
0.56,
lane.z,
{ emissive: lane.color, emissiveIntensity: 0.035 }
);
ramp.rotation.z = -0.075;
for (const side of [-0.67, 0.67]) {
const rail = addBox(
4.4,
0.16,
0.10,
lane.color,
5.0,
0.69,
lane.z + side,
{ emissive: lane.color, emissiveIntensity: 0.55 }
);
rail.rotation.z = -0.075;
}
addBox(
1.45,
0.22,
1.7,
lane.color,
7.55,
0.08,
lane.z,
{ emissive: lane.color, emissiveIntensity: 0.12 }
);
const label = makeTextSprite(name, lane.css);
label.sprite.position.set(7.45, 2.0, lane.z);
scene.add(label.sprite);
}
// Diverter arrow made from small blocks.
const arrow = new THREE.Group();
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0x64e8ff,
emissive: 0x197d90,
emissiveIntensity: 0.8,
roughness: 0.7
});
const arrowShaft = new THREE.Mesh(
new THREE.BoxGeometry(0.65, 0.09, 0.13),
arrowMaterial
);
arrowShaft.position.x = 0.22;
arrow.add(arrowShaft);
const arrowHeadA = new THREE.Mesh(
new THREE.BoxGeometry(0.32, 0.09, 0.13),
arrowMaterial
);
arrowHeadA.position.set(0.61, 0, 0.13);
arrowHeadA.rotation.y = -0.65;
arrow.add(arrowHeadA);
const arrowHeadB = arrowHeadA.clone();
arrowHeadB.position.z = -0.13;
arrowHeadB.rotation.y = 0.65;
arrow.add(arrowHeadB);
arrow.position.set(DIVERTER_X, 0.91, 0);
scene.add(arrow);
// ------------------------------------------------------------
// Voxel item
// ------------------------------------------------------------
const voxelGeometry = new THREE.BoxGeometry(1, 1, 1);
let itemGroup = null;
let itemMaterial = null;
let itemLabel = null;
function removeCurrentItem() {
if (!itemGroup) return;
scene.remove(itemGroup);
itemMaterial.dispose();
itemLabel.material.dispose();
itemLabel.texture.dispose();
}
function createVoxelItem(item) {
removeCurrentItem();
const group = new THREE.Group();
const nx = Math.max(3, Math.round(item.width / 0.25));
const ny = Math.max(3, Math.round(item.height / 0.25));
const nz = 4;
const sx = item.width / nx;
const sy = item.height / ny;
const sz = 1 / nz;
itemMaterial = new THREE.MeshStandardMaterial({
color: item.color,
roughness: 0.63,
metalness: 0.1,
emissive: item.color,
emissiveIntensity: 0.055
});
const mesh = new THREE.InstancedMesh(
voxelGeometry,
itemMaterial,
nx * ny * nz
);
mesh.castShadow = true;
mesh.receiveShadow = true;
const dummy = new THREE.Object3D();
let index = 0;
for (let ix = 0; ix < nx; ix++) {
for (let iy = 0; iy < ny; iy++) {
for (let iz = 0; iz < nz; iz++) {
dummy.position.set(
(ix - (nx - 1) / 2) * sx,
(iy - (ny - 1) / 2) * sy,
(iz - (nz - 1) / 2) * sz
);
dummy.scale.set(sx * 0.89, sy * 0.89, sz * 0.89);
dummy.updateMatrix();
mesh.setMatrixAt(index++, dummy.matrix);
}
}
}
mesh.instanceMatrix.needsUpdate = true;
group.add(mesh);
itemLabel = makeTextSprite(
`${item.width.toFixed(2)} × ${item.height.toFixed(2)}`,
`#${item.color.toString(16).padStart(6, "0")}`
);
itemLabel.sprite.position.y = item.height / 2 + 0.55;
group.add(itemLabel.sprite);
itemGroup = group;
scene.add(group);
return group;
}
// ------------------------------------------------------------
// UI and sorter state machine
// ------------------------------------------------------------
let selectedCase = "border";
let currentItem = null;
let currentDecision = null;
let phase = "idle";
let phaseStarted = 0;
let routeStarted = 0;
let beltOffset = 0;
function setResult(element, text, mode) {
element.textContent = text;
element.className = `result ${mode}`;
}
function updatePanel() {
if (!currentItem) return;
const aspect = currentItem.width / currentItem.height;
$("caseName").textContent = currentItem.name;
$("widthValue").textContent = `${currentItem.width.toFixed(2)} u`;
$("heightValue").textContent = `${currentItem.height.toFixed(2)} u`;
$("aspectValue").textContent = aspect.toFixed(2);
setResult(
$("widthResult"),
currentItem.width <= RULES.compactWidth ? "PASS" : "OVER",
currentItem.width <= RULES.compactWidth ? "pass" : "crossed"
);
setResult(
$("heightResult"),
currentItem.height > RULES.tallHeight ? "CROSSED" : "PASS",
currentItem.height > RULES.tallHeight ? "crossed" : "pass"
);
const aspectCrossed = aspect + 1e-9 >= RULES.wideAspect;
setResult(
$("aspectResult"),
aspectCrossed
? (Math.abs(aspect - RULES.wideAspect) < 0.00001 ? "AT LIMIT" : "CROSSED")
: "PASS",
aspectCrossed ? "boundary" : "pass"
);
const lane = currentDecision.lane;
const laneColor = LANES[lane].css;
$("stateDot").style.background = laneColor;
$("stateDot").style.boxShadow = `0 0 14px ${laneColor}`;
if (phase === "approach") {
$("phase").textContent = "MOVING TO SCANNER";
$("triggerTitle").textContent = "WAITING FOR SCANNER";
$("triggerText").textContent = "No routing decision has been committed.";
} else if (phase === "scan") {
$("phase").textContent = "MEASUREMENT COMPLETE";
$("triggerTitle").textContent =
`TRIGGER · ${currentDecision.trigger} CROSSES THRESHOLD`;
$("triggerText").textContent =
`${currentDecision.reason} → ${lane}. Routing begins after this reading.`;
} else if (phase === "route") {
$("phase").textContent = `ROUTING TO ${lane}`;
$("triggerTitle").textContent = `${currentDecision.trigger} → ${lane}`;
$("triggerText").textContent =
`${currentDecision.reason}. Measurement was locked before diversion.`;
} else {
$("phase").textContent = "SORT COMPLETE";
$("triggerTitle").textContent = `COMPLETE · ${lane}`;
$("triggerText").textContent =
`${currentDecision.trigger}: ${currentDecision.reason}.`;
}
}
function startCase(key) {
selectedCase = key;
currentItem = CASES[key];
currentDecision = classify(currentItem);
document.querySelectorAll(".case-button").forEach((button) => {
button.classList.toggle("active", button.dataset.case === key);
});
createVoxelItem(currentItem);
itemGroup.position.set(-6.3, BELT_Y + currentItem.height / 2, 0);
phase = "approach";
phaseStarted = performance.now();
routeStarted = 0;
arrow.rotation.y = 0;
arrowMaterial.color.setHex(0x64e8ff);
arrowMaterial.emissive.setHex(0x197d90);
updatePanel();
}
document.querySelectorAll(".case-button").forEach((button) => {
button.addEventListener("click", () => startCase(button.dataset.case));
});
$("replay").addEventListener("click", () => startCase(selectedCase));
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const startX = -6.3;
const endX = 7.15;
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - (animate.previousTime || now)) / 1000, 0.05);
animate.previousTime = now;
beltOffset = (beltOffset + delta * 0.8) % 13;
beltMarkers.forEach((marker, i) => {
marker.position.x = -6.45 + ((i * 0.72 + beltOffset) % 13);
});
if (currentItem && itemGroup) {
if (phase === "approach") {
const progress = clamp((now - phaseStarted) / 2200, 0, 1);
const eased = smoothstep(progress);
itemGroup.position.x = lerp(startX, SCANNER_X, eased);
itemGroup.position.y = BELT_Y + currentItem.height / 2;
itemGroup.position.z = 0;
if (progress >= 1) {
phase = "scan";
phaseStarted = now;
updatePanel();
}
} else if (phase === "scan") {
const progress = clamp((now - phaseStarted) / 1800, 0, 1);
itemGroup.position.x = SCANNER_X;
itemGroup.position.y = BELT_Y + currentItem.height / 2;
itemGroup.position.z = 0;
scanSheetMaterial.opacity = 0.12 + Math.sin(progress * Math.PI) * 0.20;
scanLineMaterial.opacity = 0.75;
scanLine.position.y =
0.95 + (1 - Math.cos(progress * Math.PI * 2)) * 0.62;
if (progress >= 1) {
phase = "route";
phaseStarted = now;
routeStarted = now;
const lane = LANES[currentDecision.lane];
arrowMaterial.color.setHex(lane.color);
arrowMaterial.emissive.setHex(lane.color);
arrow.rotation.y = -Math.atan2(lane.z, endX - DIVERTER_X);
updatePanel();
}
} else if (phase === "route") {
const progress = clamp((now - routeStarted) / 2500, 0, 1);
const lane = LANES[currentDecision.lane];
if (progress < 0.42) {
const p = smoothstep(progress / 0.42);
itemGroup.position.x = lerp(SCANNER_X, DIVERTER_X, p);
itemGroup.position.z = 0;
itemGroup.position.y = BELT_Y + currentItem.height / 2;
} else {
const p = smoothstep((progress - 0.42) / 0.58);
itemGroup.position.x = lerp(DIVERTER_X, endX, p);
itemGroup.position.z = lerp(0, lane.z, p);
itemGroup.position.y =
BELT_Y + currentItem.height / 2 - p * 0.33;
}
if (progress >= 1) {
phase = "complete";
updatePanel();
}
}
}
const scannerIdlePulse = 0.035 + Math.sin(now * 0.0025) * 0.015;
if (phase !== "scan") {
scanSheetMaterial.opacity = scannerIdlePulse;
scanLineMaterial.opacity = 0;
}
updateCamera();
renderer.render(scene, camera);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// Start with the explicit borderline case.
setTimeout(() => startCase("border"), 550);
requestAnimationFrame(animate);
</script>
</body>
</html>
```