Prompt specification
Build or customize the Three.js 3D scene "Contamination Under a Toy Sorting Rule" 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: Contamination Under a Toy Sorting Rule
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/contamination-under-a-toy-sorting-rule-x3992
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: contamination under a toy sorting rule. Create a synthetic sorting simulation with a known mislabeled item and explicit process assumptions. Show the effect on the toy batch without claiming real recovery rates. Return one self-contained HTML file.
Working Three.js Starter (contamination-under-a-toy-sorting-rule-x3992):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Sorting Lab — One Mislabeled Reference</title>
<style>
:root {
color-scheme: dark;
--bg: #080c14;
--panel: rgba(15, 23, 38, 0.88);
--panel-strong: rgba(19, 29, 48, 0.96);
--border: rgba(153, 177, 214, 0.18);
--text: #eef4ff;
--muted: #94a5c2;
--cyan: #67e8f9;
--teal: #59e1bd;
--amber: #ffbd59;
--red: #ff557d;
--violet: #9b8cff;
--shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
}
* { 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; }
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.topbar {
position: fixed;
z-index: 3;
top: 22px;
left: 24px;
right: 24px;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 24px;
pointer-events: none;
}
.title-block {
max-width: 650px;
padding: 17px 20px 16px;
border: 1px solid var(--border);
border-radius: 18px;
background: linear-gradient(135deg, rgba(18, 28, 48, 0.94), rgba(11, 17, 30, 0.78));
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 7px;
color: var(--cyan);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 18px rgba(103, 232, 249, 0.75);
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 40px);
line-height: 1.04;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 590px;
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.status-chip {
display: flex;
align-items: center;
gap: 10px;
min-width: 235px;
padding: 12px 15px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
font: 600 12px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.status-dot {
flex: 0 0 auto;
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 14px rgba(89, 225, 189, 0.7);
}
.status-dot.paused {
background: var(--amber);
box-shadow: 0 0 14px rgba(255, 189, 89, 0.65);
}
#statusText { color: var(--text); }
#statusSub { margin-top: 3px; color: var(--muted); font-size: 10px; }
.panel {
position: fixed;
z-index: 3;
top: 138px;
width: 326px;
max-height: calc(100vh - 246px);
overflow: auto;
border: 1px solid var(--border);
border-radius: 17px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
scrollbar-width: thin;
scrollbar-color: rgba(155, 140, 255, 0.45) transparent;
}
#assumptions { left: 24px; }
#metrics { right: 24px; }
.panel-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 16px 12px;
border-bottom: 1px solid var(--border);
}
.panel-title {
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.panel-kicker {
color: var(--muted);
font: 600 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.panel-body { padding: 14px 16px 16px; }
.assumption-list {
display: grid;
gap: 11px;
margin: 0;
padding: 0;
list-style: none;
counter-reset: assumption;
}
.assumption-list li {
position: relative;
padding-left: 29px;
color: #c5d1e6;
font-size: 12px;
line-height: 1.45;
counter-increment: assumption;
}
.assumption-list li::before {
content: counter(assumption, decimal-leading-zero);
position: absolute;
top: 0;
left: 0;
width: 20px;
height: 20px;
display: grid;
place-items: center;
border: 1px solid rgba(155, 140, 255, 0.35);
border-radius: 6px;
color: var(--violet);
background: rgba(155, 140, 255, 0.08);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
code, .mono {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
code {
color: #d8d1ff;
font-size: 0.94em;
}
.known-item {
margin-top: 13px;
padding: 12px;
border: 1px solid rgba(255, 85, 125, 0.32);
border-radius: 12px;
background: rgba(255, 85, 125, 0.08);
}
.known-item strong {
display: block;
margin-bottom: 5px;
color: #ff7898;
font-size: 12px;
}
.known-item span {
color: #d6c0ca;
font-size: 11px;
line-height: 1.5;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.metric {
min-height: 92px;
padding: 13px;
border: 1px solid rgba(153, 177, 214, 0.12);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.metric.wide { grid-column: 1 / -1; }
.metric-label {
color: var(--muted);
font: 700 9px/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.metric-value {
margin-top: 9px;
font: 700 26px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: -0.06em;
}
.metric-value.cyan { color: var(--cyan); }
.metric-value.red { color: var(--red); }
.metric-value.amber { color: var(--amber); }
.metric-value.small { font-size: 19px; letter-spacing: -0.03em; }
.metric-note {
margin-top: 7px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.split-bar {
display: flex;
height: 8px;
margin-top: 10px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.06);
}
#smallSplitBar { background: var(--teal); }
#largeSplitBar { background: var(--amber); }
.counterfactual {
margin-top: 11px;
padding: 11px 12px;
border-left: 3px solid var(--violet);
border-radius: 4px 10px 10px 4px;
background: rgba(155, 140, 255, 0.07);
color: #c8c3ed;
font-size: 11px;
line-height: 1.5;
}
.toolbar {
position: fixed;
z-index: 4;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 9px;
padding: 9px;
border: 1px solid var(--border);
border-radius: 16px;
background: rgba(13, 20, 34, 0.94);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
white-space: nowrap;
}
.control-group {
display: flex;
gap: 5px;
padding: 3px;
border-radius: 11px;
background: rgba(255, 255, 255, 0.045);
}
button {
min-height: 36px;
padding: 0 13px;
border: 1px solid transparent;
border-radius: 9px;
color: #bdc9df;
background: transparent;
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition: color 140ms ease, background 140ms ease, border-color 140ms ease,
transform 140ms ease;
}
button:hover {
color: white;
background: rgba(255, 255, 255, 0.07);
}
button:active { transform: translateY(1px); }
button.active {
color: #eef4ff;
border-color: rgba(155, 140, 255, 0.42);
background: rgba(155, 140, 255, 0.18);
}
#restart {
color: #08121b;
background: var(--cyan);
}
#restart:hover { background: #8cf1fb; }
.separator {
width: 1px;
height: 28px;
background: var(--border);
}
.progress-wrap {
position: fixed;
z-index: 3;
left: 24px;
right: 24px;
bottom: 95px;
height: 3px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.06);
pointer-events: none;
}
#progress {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--violet), var(--cyan));
box-shadow: 0 0 12px rgba(103, 232, 249, 0.5);
}
.legend {
position: fixed;
z-index: 3;
left: 24px;
bottom: 28px;
display: flex;
gap: 12px;
align-items: center;
color: var(--muted);
font-size: 10px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 3px;
}
.swatch.red {
background: var(--red);
box-shadow: 0 0 10px rgba(255, 85, 125, 0.6);
}
.swatch.teal { background: var(--teal); }
.swatch.amber { background: var(--amber); }
.camera-hint {
position: fixed;
z-index: 3;
right: 24px;
bottom: 29px;
color: rgba(180, 195, 220, 0.62);
font-size: 10px;
pointer-events: none;
}
.disclaimer {
position: fixed;
z-index: 3;
left: 50%;
bottom: 102px;
transform: translateX(-50%);
max-width: 620px;
color: rgba(188, 203, 227, 0.78);
text-align: center;
font-size: 10px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 1040px) {
.panel { width: 286px; }
.title-block { max-width: 510px; }
.status-chip { min-width: 190px; }
.legend { display: none; }
}
@media (max-width: 780px) {
.topbar {
top: 10px;
left: 10px;
right: 10px;
gap: 8px;
}
.title-block {
padding: 12px 14px;
max-width: calc(100vw - 20px);
}
.title-block .subtitle { display: none; }
.status-chip { display: none; }
.panel {
top: auto;
bottom: 151px;
width: calc(50vw - 15px);
max-height: 34vh;
}
#assumptions { left: 10px; }
#metrics { right: 10px; }
.panel-body { padding: 11px; }
.assumption-list { gap: 8px; }
.assumption-list li { font-size: 10px; }
.known-item { display: none; }
.metric { min-height: 72px; padding: 10px; }
.metric-value { font-size: 21px; }
.toolbar {
bottom: 10px;
width: calc(100vw - 20px);
justify-content: center;
flex-wrap: wrap;
gap: 5px;
}
.toolbar button { padding: 0 9px; min-height: 32px; font-size: 10px; }
.separator { display: none; }
.progress-wrap { left: 10px; right: 10px; bottom: 72px; }
.disclaimer { display: none; }
.camera-hint { display: none; }
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive three-dimensional synthetic sorting simulation"></div>
<header class="topbar">
<div class="title-block">
<div class="eyebrow">Synthetic process lab · deterministic model</div>
<h1>One bad label moves the rule.</h1>
<p class="subtitle">
A voxel sorting batch uses a nearest-reference size rule. One known mislabeled
reference shifts the effective split and redirects part of the toy batch.
</p>
</div>
<div class="status-chip">
<span id="statusDot" class="status-dot"></span>
<div>
<div id="statusText">Starting simulation</div>
<div id="statusSub">48 synthetic items · 6 reference examples</div>
</div>
</div>
</header>
<section id="assumptions" class="panel" aria-label="Process assumptions">
<div class="panel-header">
<span class="panel-title">Process assumptions</span>
<span class="panel-kicker">v1 · toy only</span>
</div>
<div class="panel-body">
<ol class="assumption-list">
<li>
The batch contains <strong>48 synthetic items</strong>. Ground truth is
<code>size < 10 → SMALL</code> and <code>size ≥ 10 → LARGE</code>.
</li>
<li>
The sorter uses a <strong>1-nearest labeled reference</strong> in one size
dimension. Exact distance ties favor the smaller reference size.
</li>
<li>
Six calibration references are used. The sorter sees only their stored
labels; ground truth is used afterward for the audit overlay.
</li>
<li>
The clean counterfactual changes <strong>only C-09's stored label</strong>.
Batch data, order, rule, and geometry remain unchanged.
</li>
<li>
There is <strong>no online learning</strong>, no queue feedback, and no
batch item can change the policy during this run.
</li>
</ol>
<div class="known-item">
<strong>Known contaminated reference: C-09</strong>
<span>
Actual size <span class="mono">9.0</span> · true class <span class="mono">SMALL</span> ·
stored class <span class="mono">LARGE</span>. The coral block on the
reference shelf marks this known error.
</span>
</div>
</div>
</section>
<section id="metrics" class="panel" aria-label="Simulation audit metrics">
<div class="panel-header">
<span class="panel-title">Toy batch audit</span>
<span id="policyLabel" class="panel-kicker">contaminated</span>
</div>
<div class="panel-body">
<div class="metric-grid">
<div class="metric">
<div class="metric-label">Processed</div>
<div id="processedMetric" class="metric-value cyan">0 / 48</div>
<div class="metric-note">items settled after routing</div>
</div>
<div class="metric">
<div class="metric-label">Toy errors</div>
<div id="errorMetric" class="metric-value red">0</div>
<div class="metric-note">truth audit after routing</div>
</div>
<div class="metric">
<div class="metric-label">Routes changed</div>
<div id="changedMetric" class="metric-value amber">0</div>
<div class="metric-note">versus the clean counterfactual</div>
</div>
<div class="metric">
<div class="metric-label">Effective split</div>
<div id="splitMetric" class="metric-value small">≈ 8.0</div>
<div class="metric-note">toy size units · clean: ≈ 10.0</div>
</div>
<div class="metric wide">
<div class="metric-label">Predicted route split</div>
<div id="routeMetric" class="metric-value small">0 SMALL / 0 LARGE</div>
<div class="split-bar">
<div id="smallSplitBar"></div>
<div id="largeSplitBar"></div>
</div>
<div class="metric-note">Counts, not a real-world rate or recovery estimate.</div>
</div>
</div>
<div class="counterfactual">
Clean-label counterfactual: <strong id="cleanErrorsMetric">0 toy errors</strong>.
The reference shelf and all geometry remain fixed; only C-09's label changes.
</div>
</div>
</section>
<div class="progress-wrap"><div id="progress"></div></div>
<div class="toolbar" aria-label="Simulation controls">
<button id="restart">▶ Replay batch</button>
<div class="separator"></div>
<div class="control-group" aria-label="Policy">
<button id="contaminatedPolicy" class="active">Contaminated rule</button>
<button id="cleanPolicy">Clean counterfactual</button>
</div>
<div class="separator"></div>
<button id="speedButton">Speed 1×</button>
<button id="auditButton" class="active">Truth audit ON</button>
</div>
<div class="legend">
<div class="legend-item"><span class="swatch teal"></span> predicted SMALL</div>
<div class="legend-item"><span class="swatch amber"></span> predicted LARGE</div>
<div class="legend-item"><span class="swatch red"></span> audit mismatch</div>
</div>
<div class="camera-hint">Drag to orbit · wheel to zoom · double-click to reset view</div>
<div class="disclaimer">
Synthetic demonstration only. Counts are deterministic outputs of this toy setup and do not
estimate real-world contamination, sorting performance, or recovery rates.
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Deterministic synthetic data
// -------------------------------------------------------------------------
const SMALL = "SMALL";
const LARGE = "LARGE";
// Ground truth: score < 10 is SMALL; score >= 10 is LARGE.
const batchSizes = [
// 24 SMALL truth items. 13 remain SMALL under the contaminated rule.
5.2, 5.7, 6.1, 6.5, 6.9, 7.2, 7.5, 7.7, 7.9, 8.0, 8.0, 6.3,
7.0, 8.1, 8.3, 8.5, 8.7, 8.9, 9.1, 9.3, 9.5, 9.7, 9.9, 8.2,
// 24 LARGE truth items.
10.2, 10.5, 10.8, 11.1, 11.4, 11.7, 12.0, 12.3, 12.6, 12.9,
13.2, 13.5, 13.8, 14.1, 14.4, 14.7, 15.0, 15.3, 15.6, 15.9,
16.2, 16.5, 16.8, 17.1
];
const batchItems = batchSizes.map((score, index) => ({
id: `B-${String(index + 1).padStart(2, "0")}`,
score,
truth: score < 10 ? SMALL : LARGE
}));
// C-09 is the sole known mislabeled reference.
const cleanReferences = [
{ id: "R-01", score: 5.0, label: SMALL, truth: SMALL },
{ id: "R-03", score: 7.0, label: SMALL, truth: SMALL },
{ id: "C-09", score: 9.0, label: SMALL, truth: SMALL },
{ id: "R-11", score: 11.0, label: LARGE, truth: LARGE },
{ id: "R-13", score: 13.0, label: LARGE, truth: LARGE },
{ id: "R-15", score: 15.0, label: LARGE, truth: LARGE }
];
const contaminatedReferences = cleanReferences.map(ref =>
ref.id === "C-09" ? { ...ref, label: LARGE } : { ...ref }
);
// A deterministic 1-NN rule. On an exact tie, the smaller reference size wins.
function predict(score, references) {
let best = references[0];
let bestDistance = Math.abs(score - best.score);
for (let i = 1; i < references.length; i++) {
const candidate = references[i];
const distance = Math.abs(score - candidate.score);
if (
distance < bestDistance ||
(distance === bestDistance && candidate.score < best.score)
) {
best = candidate;
bestDistance = distance;
}
}
return best.label;
}
const cleanPredictions = batchItems.map(item => predict(item.score, cleanReferences));
const contaminatedPredictions = batchItems.map(item => predict(item.score, contaminatedReferences));
const cleanErrorCount = batchItems.reduce(
(sum, item, i) => sum + Number(cleanPredictions[i] !== item.truth), 0
);
const contaminatedErrorCount = batchItems.reduce(
(sum, item, i) => sum + Number(contaminatedPredictions[i] !== item.truth), 0
);
const changedRouteCount = batchItems.reduce(
(sum, item, i) => sum + Number(cleanPredictions[i] !== contaminatedPredictions[i]), 0
);
// -------------------------------------------------------------------------
// Renderer, scene, camera, and direct camera controls
// -------------------------------------------------------------------------
const host = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080c14);
scene.fog = new THREE.Fog(0x080c14, 23, 43);
let renderer;
try {
renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
} catch (error) {
host.innerHTML = `
<div style="position:absolute;inset:0;display:grid;place-items:center;padding:30px;
color:#eef4ff;background:#080c14;text-align:center;font:15px system-ui">
WebGL could not start. This synthetic scene requires a browser with WebGL enabled.
</div>`;
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
host.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-12, 12, 9, -9, 0.1, 100);
const cameraTarget = new THREE.Vector3(-0.1, 1.0, 0);
const cameraState = {
theta: 0.70,
phi: 0.88,
zoom: 1
};
function updateCamera() {
const radius = 24;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + radius * Math.cos(cameraState.phi),
cameraTarget.z + radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(16, 22 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
updateCamera();
}
window.addEventListener("resize", resize);
resize();
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
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;
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi + dy * 0.004, 0.35, 1.30);
updateCamera();
});
const stopDragging = event => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
};
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.85
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
cameraState.theta = 0.70;
cameraState.phi = 0.88;
cameraState.zoom = 1;
updateCamera();
});
// -------------------------------------------------------------------------
// Lighting and reusable geometry helpers
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xabc9ff, 0x111827, 1.65));
const keyLight = new THREE.DirectionalLight(0xe9f2ff, 2.5);
keyLight.position.set(-5, 13, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 14;
keyLight.shadow.camera.bottom = -14;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 38;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x50dff4, 9, 12, 2);
cyanLight.position.set(-1.1, 3.2, 1.0);
scene.add(cyanLight);
const redLight = new THREE.PointLight(0xff416d, 7, 8, 2);
redLight.position.set(-4.9, 2.8, 4.6);
scene.add(redLight);
function makeMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.76,
metalness: options.metalness ?? 0.05,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
side: options.side ?? THREE.FrontSide,
depthWrite: options.depthWrite ?? true
});
}
function addBox(parent, width, height, depth, x, y, z, color, options = {}) {
const material = color?.isMaterial ? color : makeMaterial(color, options);
const mesh = new THREE.Mesh(new THREE.BoxGeometry(width, height, depth), material);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
parent.add(mesh);
return mesh;
}
function addCylinder(parent, radius, length, x, y, z, color, options = {}) {
const material = color?.isMaterial ? color : makeMaterial(color, options);
const mesh = new THREE.Mesh(new THREE.CylinderGeometry(radius, radius, length, 12), material);
mesh.rotation.x = Math.PI / 2;
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
parent.add(mesh);
return mesh;
}
function addBeamBetween(parent, start, end, width, thickness, color) {
const dx = end.x - start.x;
const dz = end.z - start.z;
const length = Math.hypot(dx, dz);
const mesh = addBox(
parent,
length,
thickness,
width,
(start.x + end.x) / 2,
(start.y + end.y) / 2,
(start.z + end.z) / 2,
color
);
mesh.rotation.y = -Math.atan2(dz, dx);
return mesh;
}
function roundedRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function makeLabel(text, worldWidth, worldHeight, background, foreground = "#eef4ff") {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(context, 8, 8, 752, 112, 20);
context.fillStyle = background;
context.fill();
context.strokeStyle = "rgba(255,255,255,0.16)";
context.lineWidth = 3;
context.stroke();
context.fillStyle = foreground;
context.font = "700 38px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 384, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldHeight, 1);
return sprite;
}
// -------------------------------------------------------------------------
// Environment, conveyor, scanner, bins, and reference shelf
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
makeMaterial(0x0b1220, { roughness: 0.96, metalness: 0 })
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(44, 44, 0x263754, 0x172238);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
const environment = new THREE.Group();
scene.add(environment);
// Main conveyor.
const BELT_Y = 1.02;
const START_X = -7.25;
const GATE_X = 0.95;
const beltCenterX = (START_X + GATE_X) / 2;
const beltLength = GATE_X - START_X;
addBox(environment, beltLength, 0.30, 2.05, beltCenterX, 0.82, 0, 0x172337, {
roughness: 0.84,
metalness: 0.12
});
addBox(environment, beltLength - 0.12, 0.055, 1.72, beltCenterX, 1.005, 0, 0x253653, {
roughness: 0.66,
metalness: 0.18
});
for (let x = START_X + 0.35; x < GATE_X; x += 0.72) {
addCylinder(environment, 0.14, 2.18, x, 0.83, 0, 0x405674, {
roughness: 0.5,
metalness: 0.45
});
}
for (const z of [-1.17, 1.17]) {
addBox(environment, beltLength + 0.2, 0.32, 0.18, beltCenterX, 0.91, z, 0x334863, {
roughness: 0.65,
metalness: 0.32
});
for (let x = START_X + 0.3; x <= GATE_X; x += 2.4) {
addBox(environment, 0.18, 0.72, 0.18, x, 0.38, z, 0x263a54, {
roughness: 0.68,
metalness: 0.3
});
}
}
// Scanner arch.
const scannerMaterial = makeMaterial(0x24465f, {
roughness: 0.45,
metalness: 0.45,
emissive: 0x123a48,
emissiveIntensity: 0.25
});
for (const z of [-1.26, 1.26]) {
addBox(environment, 0.28, 1.85, 0.28, -1.15, 1.78, z, scannerMaterial);
}
addBox(environment, 0.28, 0.28, 2.80, -1.15, 2.69, 0, scannerMaterial);
const scanStripMaterial = makeMaterial(0x73ebff, {
roughness: 0.2,
metalness: 0.1,
emissive: 0x34d8f4,
emissiveIntensity: 2.4
});
addBox(environment, 0.10, 0.08, 2.22, -1.31, 2.49, 0, scanStripMaterial, {
castShadow: false
});
const scanLabel = makeLabel("SIZE SCAN", 1.65, 0.32, "rgba(17,50,68,0.94)", "#91f3ff");
scanLabel.position.set(-1.15, 3.03, 0);
environment.add(scanLabel);
// Divider/gate.
const gate = addBox(environment, 0.22, 0.82, 1.75, 1.03, 1.26, 0, 0x7969d5, {
roughness: 0.38,
metalness: 0.36,
emissive: 0x302866,
emissiveIntensity: 0.45
});
gate.rotation.z = -0.18;
// Branch conveyors.
const smallBinZ = 3.70;
const largeBinZ = -3.70;
const binX = 6.10;
addBeamBetween(
environment,
new THREE.Vector3(0.95, 0.80, 0),
new THREE.Vector3(4.95, 0.80, smallBinZ),
1.78,
0.24,
0x20334e
);
addBeamBetween(
environment,
new THREE.Vector3(0.95, 0.80, 0),
new THREE.Vector3(4.95, 0.80, largeBinZ),
1.78,
0.24,
0x20334e
);
function buildBin(z, accentColor, labelText) {
const bin = new THREE.Group();
bin.position.set(binX, 0, z);
scene.add(bin);
addBox(bin, 3.25, 0.28, 4.35, 0, 0.16, 0, 0x172337, {
roughness: 0.78,
metalness: 0.18
});
const accent = makeMaterial(accentColor, {
roughness: 0.48,
metalness: 0.18,
emissive: accentColor,
emissiveIntensity: 0.12
});
for (const x of [-1.68, 1.68]) {
addBox(bin, 0.18, 0.86, 4.50, x, 0.61, 0, accent, {
roughness: 0.58,
metalness: 0.22
});
}
for (const wallZ of [-2.22, 2.22]) {
addBox(bin, 3.45, 0.72, 0.18, 0, 0.53, wallZ, accent, {
roughness: 0.58,
metalness: 0.22
});
}
for (const x of [-1.68, 1.68]) {
for (const cornerZ of [-2.22, 2.22]) {
addBox(bin, 0.25, 1.08, 0.25, x, 0.78, cornerZ, 0x425a78, {
roughness: 0.48,
metalness: 0.38
});
}
}
const label = makeLabel(labelText, 2.35, 0.40, "rgba(10,17,29,0.92)");
label.position.set(0, 1.63, 0);
bin.add(label);
return bin;
}
buildBin(smallBinZ, 0x3acbaa, "SMALL · PREDICTED");
buildBin(largeBinZ, 0xd99132, "LARGE · PREDICTED");
// Reference shelf.
const shelf = new THREE.Group();
shelf.position.set(-4.95, 0, 4.45);
scene.add(shelf);
addBox(shelf, 5.45, 0.22, 1.45, 0, 0.78, 0, 0x202f48, {
roughness: 0.78,
metalness: 0.18
});
for (const x of [-2.45, 2.45]) {
addBox(shelf, 0.20, 0.70, 0.20, x, 0.36, -0.50, 0x2c405b, {
roughness: 0.65,
metalness: 0.25
});
addBox(shelf, 0.20, 0.70, 0.20, x, 0.36, 0.50, 0x2c405b, {
roughness: 0.65,
metalness: 0.25
});
}
const referenceLabel = makeLabel("REFERENCE SET · 1 KNOWN BAD LABEL", 4.10, 0.44, "rgba(44,19,34,0.94)", "#ff7898");
referenceLabel.position.set(0, 2.10, 0);
shelf.add(referenceLabel);
// Voxel parcels: lower body plus a smaller top block.
function createParcel(score, options = {}) {
const group = new THREE.Group();
const width = 0.38 + score * 0.020;
const height = 0.32 + score * 0.012;
const depth = width * 0.86;
const bodyMaterial = makeMaterial(options.bodyColor ?? 0xaebbea, {
roughness: 0.62,
metalness: 0.12
});
const capMaterial = makeMaterial(options.capColor ?? 0x7d8bd3, {
roughness: 0.5,
metalness: 0.16
});
const body = addBox(group, width, height * 0.68, depth, 0, height * 0.34, 0, bodyMaterial);
const cap = addBox(group, width * 0.65, height * 0.32, depth * 0.62, 0, height * 0.84, 0, capMaterial);
const outline = new THREE.Mesh(
new THREE.BoxGeometry(width * 1.09, height * 1.07, depth * 1.09),
new THREE.MeshBasicMaterial({
color: 0xff557d,
transparent: true,
opacity: 0.68,
wireframe: true,
depthWrite: false
})
);
outline.position.y = height * 0.5;
outline.visible = false;
group.add(outline);
const marker = addBox(
group,
0.13,
0.13,
0.13,
0,
height + 0.22,
0,
makeMaterial(0xff557d, {
emissive: 0xff315f,
emissiveIntensity: 1.8,
roughness: 0.25
}),
{ castShadow: false }
);
marker.visible = false;
group.userData = {
bodyMaterial,
capMaterial,
outline,
marker,
height
};
return group;
}
// Reference blocks, with C-09 visibly marked in coral.
cleanReferences.forEach((reference, index) => {
const isKnownBad = reference.id === "C-09";
const parcel = createParcel(reference.score, isKnownBad
? { bodyColor: 0xff557d, capColor: 0xff8aa5 }
: { bodyColor: 0x65799f, capColor: 0x91a0c8 }
);
parcel.position.set(-2.20 + index * 0.88, 0.90, 0);
shelf.add(parcel);
if (isKnownBad) {
const markerMaterial = makeMaterial(0xff557d, {
emissive: 0xff315f,
emissiveIntensity: 1.8
});
const beacon = addBox(
parcel,
0.13,
0.13,
0.13,
0,
reference.score * 0.012 + 0.32 + 0.22,
0,
markerMaterial,
{ castShadow: false }
);
beacon.visible = true;
const idLabel = makeLabel("C-09 · KNOWN BAD LABEL", 2.15, 0.36, "rgba(95,18,42,0.96)", "#ff9ab0");
idLabel.position.set(0, reference.score * 0.012 + 0.32 + 0.73, 0);
parcel.add(idLabel);
}
});
// Decorative voxel crates at the back.
for (let i = 0; i < 6; i++) {
const x = -7.6 + i * 0.62;
const height = 0.35 + (i % 3) * 0.30;
addBox(environment, 0.52, height, 0.52, x, height / 2, -5.65, 0x18263c, {
roughness: 0.8,
metalness: 0.14
});
addBox(environment, 0.57, 0.09, 0.57, x, height, -5.65, 0x2c405e, {
roughness: 0.65,
metalness: 0.25
});
}
// -------------------------------------------------------------------------
// Batch objects and animation state
// -------------------------------------------------------------------------
const parcels = [];
batchItems.forEach((item, index) => {
const group = createParcel(item.score);
scene.add(group);
parcels.push({
...item,
group,
index,
lane: -0.38 + (index % 3) * 0.38,
prediction: null,
cleanPrediction: cleanPredictions[index],
target: new THREE.Vector3(),
counted: false
});
});
const state = {
policy: "contaminated",
auditVisible: true,
running: true,
elapsed: 0,
speed: 1,
processed: 0,
errors: 0,
changed: 0,
smallRouted: 0,
largeRouted: 0,
lastStatusId: ""
};
const START_DELAY = 0.24;
const SCAN_TIME = 2.20;
const ROUTE_TIME = 1.40;
const TOTAL_LOCAL_TIME = SCAN_TIME + ROUTE_TIME;
const TOTAL_DURATION = START_DELAY * (batchItems.length - 1) + TOTAL_LOCAL_TIME + 0.15;
function currentReferences() {
return state.policy === "contaminated" ? contaminatedReferences : cleanReferences;
}
function assignSlots() {
const buckets = { [SMALL]: [], [LARGE]: [] };
parcels.forEach(parcel => {
parcel.prediction = predict(parcel.score, currentReferences());
buckets[parcel.prediction].push(parcel);
});
for (const [label, bucket] of Object.entries(buckets)) {
const binZ = label === SMALL ? smallBinZ : largeBinZ;
const columns = Math.min(6, bucket.length);
const rows = Math.ceil(bucket.length / columns);
bucket.forEach((parcel, index) => {
const column = index % columns;
const row = Math.floor(index / columns);
const xOffset = column - (columns - 1) / 2;
const zOffset = row - (rows - 1) / 2;
parcel.target.set(
binX + xOffset * 0.62,
0.31,
binZ + zOffset * 0.62
);
});
}
}
function setParcelAuditVisual(parcel) {
const wrong = parcel.counted && parcel.prediction !== parcel.truth;
const { bodyMaterial, outline, marker } = parcel.group.userData;
if (wrong && state.auditVisible) {
bodyMaterial.emissive.setHex(0x7a1537);
bodyMaterial.emissiveIntensity = 0.55;
outline.visible = true;
marker.visible = true;
} else {
bodyMaterial.emissive.setHex(0x000000);
bodyMaterial.emissiveIntensity = 0;
outline.visible = false;
marker.visible = false;
}
}
function updateMetrics() {
document.getElementById("processedMetric").textContent = `${state.processed} / ${parcels.length}`;
document.getElementById("errorMetric").textContent = String(state.errors);
document.getElementById("changedMetric").textContent = String(state.changed);
document.getElementById("routeMetric").textContent =
`${state.smallRouted} SMALL / ${state.largeRouted} LARGE`;
document.getElementById("splitMetric").textContent =
state.policy === "contaminated" ? "≈ 8.0" : "≈ 10.0";
document.getElementById("policyLabel").textContent = state.policy;
const total = Math.max(1, state.processed);
document.getElementById("smallSplitBar").style.width = `${state.smallRouted / total * 100}%`;
document.getElementById("largeSplitBar").style.width = `${state.largeRouted / total * 100}%`;
document.getElementById("progress").style.width = `${Math.min(100, state.elapsed / TOTAL_DURATION * 100)}%`;
}
function setStatus(title, subtitle, paused = false) {
document.getElementById("statusText").textContent = title;
document.getElementById("statusSub").textContent = subtitle;
document.getElementById("statusDot").classList.toggle("paused", paused);
}
function resetSimulation() {
state.elapsed = 0;
state.running = true;
state.processed = 0;
state.errors = 0;
state.changed = 0;
state.smallRouted = 0;
state.largeRouted = 0;
state.lastStatusId = "";
assignSlots();
for (const parcel of parcels) {
parcel.counted = false;
parcel.group.visible = false;
parcel.group.position.set(START_X, BELT_Y, parcel.lane);
parcel.group.rotation.set(0, 0, 0);
setParcelAuditVisual(parcel);
}
setStatus(
state.policy === "contaminated" ? "Contaminated rule running" : "Clean counterfactual running",
state.policy === "contaminated"
? "C-09 is stored as LARGE; truth is audit-only"
: "Only C-09's stored label has been corrected",
false
);
updateMetrics();
}
function settleParcel(parcel) {
if (parcel.counted) return;
parcel.counted = true;
state.processed += 1;
if (parcel.prediction === SMALL) state.smallRouted += 1;
else state.largeRouted += 1;
if (parcel.prediction !== parcel.truth) state.errors += 1;
if (parcel.prediction !== parcel.cleanPrediction) state.changed += 1;
setParcelAuditVisual(parcel);
updateMetrics();
}
const statusDot = document.getElementById("statusDot");
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min(0.05, (now - (animate.lastTime ?? now)) / 1000);
animate.lastTime = now;
if (state.running) {
state.elapsed += delta * state.speed;
}
let activeId = "";
let activeStage = "";
for (const parcel of parcels) {
const localTime = state.elapsed - parcel.index * START_DELAY;
if (localTime < 0) {
parcel.group.visible = false;
continue;
}
parcel.group.visible = true;
if (localTime < SCAN_TIME) {
const u = localTime / SCAN_TIME;
parcel.group.position.set(
THREE.MathUtils.lerp(START_X, GATE_X, u),
BELT_Y,
parcel.lane
);
parcel.group.rotation.y = Math.sin(u * Math.PI) * 0.06;
activeId = parcel.id;
activeStage = "on conveyor";
} else {
const u = THREE.MathUtils.clamp((localTime - SCAN_TIME) / ROUTE_TIME, 0, 1);
const eased = u * u * (3 - 2 * u);
const target = parcel.target;
const x = THREE.MathUtils.lerp(GATE_X, target.x, eased);
const z = THREE.MathUtils.lerp(0, target.z, eased);
const drop = THREE.MathUtils.smoothstep(u, 0.68, 1);
const y = THREE.MathUtils.lerp(BELT_Y, target.y, drop);
parcel.group.position.set(x, y, z);
parcel.group.rotation.y = -Math.atan2(target.z, x - GATE_X);
activeId = parcel.id;
activeStage = parcel.prediction === SMALL ? "routing to SMALL" : "routing to LARGE";
if (u >= 1) {
parcel.group.position.set(target.x, target.y, target.z);
parcel.group.rotation.y = 0;
settleParcel(parcel);
}
}
}
if (state.running) {
if (state.elapsed >= TOTAL_DURATION) {
state.running = false;
setStatus(
"Batch complete",
`${state.errors} toy audit errors · ${state.changed} routes changed vs clean`,
true
);
} else if (activeId) {
setStatus(
`Scanning ${activeId}`,
`${activeStage} · ${state.processed} of ${parcels.length} settled`
);
}
updateMetrics();
}
// Gentle scanner pulse and contamination beacon.
const pulse = 0.72 + Math.sin(now * 0.006) * 0.22;
scanStripMaterial.emissiveIntensity = 1.8 + pulse * 1.4;
redLight.intensity = 5.4 + pulse * 2.8;
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Controls
// -------------------------------------------------------------------------
const restartButton = document.getElementById("restart");
const contaminatedButton = document.getElementById("contaminatedPolicy");
const cleanButton = document.getElementById("cleanPolicy");
const speedButton = document.getElementById("speedButton");
const auditButton = document.getElementById("auditButton");
restartButton.addEventListener("click", resetSimulation);
contaminatedButton.addEventListener("click", () => {
if (state.policy === "contaminated") return;
state.policy = "contaminated";
contaminatedButton.classList.add("active");
cleanButton.classList.remove("active");
resetSimulation();
});
cleanButton.addEventListener("click", () => {
if (state.policy === "clean") return;
state.policy = "clean";
cleanButton.classList.add("active");
contaminatedButton.classList.remove("active");
resetSimulation();
});
speedButton.addEventListener("click", () => {
const speeds = [1, 2, 4];
const nextIndex = (speeds.indexOf(state.speed) + 1) % speeds.length;
state.speed = speeds[nextIndex];
speedButton.textContent = `Speed ${state.speed}×`;
});
auditButton.addEventListener("click", () => {
state.auditVisible = !state.auditVisible;
auditButton.classList.toggle("active", state.auditVisible);
auditButton.textContent = state.auditVisible ? "Truth audit ON" : "Truth audit OFF";
for (const parcel of parcels) setParcelAuditVisual(parcel);
});
document.getElementById("cleanErrorsMetric").textContent =
`${cleanErrorCount} toy error${cleanErrorCount === 1 ? "" : "s"}`;
resetSimulation();
requestAnimationFrame(animate);
</script>
</body>
</html>
```