Prompt specification
Build or customize the Three.js 3D scene "Defect-detector population grid" 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: Defect-detector population grid
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/defect-detector-population-grid-x1761
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts base rates and predictive values of a detector. Build a large synthetic product grid with a small defective subset and marked detector outcomes; distinguish true positives, false positives, and all flagged items. Low-poly.
Working Three.js Starter (defect-detector-population-grid-x1761):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Detector Base Rates and Predictive Values</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 22, 38, 0.88);
--border: rgba(147, 190, 220, 0.18);
--text: #e8f3fb;
--muted: #91a8bd;
--cyan: #62d9ff;
--yellow: #ffd45a;
--green: #5ce6a4;
--orange: #ff9d52;
--red: #ff5d6c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
pointer-events: auto;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 18px 55px rgba(0, 0, 0, 0.32),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px);
}
#summary {
top: 22px;
left: 22px;
width: 350px;
padding: 20px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 25px;
line-height: 1.12;
letter-spacing: -0.035em;
}
.intro {
margin: 10px 0 16px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.metric {
min-height: 73px;
padding: 10px 11px;
border: 1px solid rgba(147, 190, 220, 0.12);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.metric strong {
font-size: 25px;
font-variant-numeric: tabular-nums;
}
.metric strong small {
color: var(--muted);
font-size: 11px;
font-weight: 600;
}
.metric.base strong {
color: var(--red);
}
.metric.ppv strong {
color: var(--green);
}
.metric.npv strong {
color: var(--cyan);
}
.section-title {
margin: 18px 0 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.probability {
margin: 10px 0;
}
.probability label {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 5px;
color: #c5d5e2;
font-size: 11px;
}
.probability label b {
color: var(--text);
font-variant-numeric: tabular-nums;
}
.track {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
.track i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 500ms ease;
}
#barBase {
background: var(--red);
}
#barPositive {
background: var(--green);
}
#barNegative {
background: var(--cyan);
}
.counts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px 14px;
margin-top: 13px;
font-size: 12px;
}
.counts div {
display: flex;
justify-content: space-between;
padding-bottom: 5px;
border-bottom: 1px solid rgba(147, 190, 220, 0.09);
color: var(--muted);
}
.counts b {
color: var(--text);
font-variant-numeric: tabular-nums;
}
.insight {
margin: 15px 0 0;
padding: 10px 11px;
border-left: 3px solid var(--yellow);
border-radius: 4px;
background: rgba(255, 212, 90, 0.07);
color: #d7e3ec;
font-size: 11px;
line-height: 1.45;
}
#legend {
top: 22px;
right: 22px;
width: 286px;
padding: 17px;
}
#legend h2 {
margin: 0 0 11px;
font-size: 14px;
}
.legend-row {
display: grid;
grid-template-columns: 28px 1fr auto;
align-items: center;
gap: 9px;
min-height: 35px;
color: #c5d5e2;
font-size: 11px;
}
.legend-row b {
color: var(--text);
font-variant-numeric: tabular-nums;
}
.icon {
position: relative;
width: 22px;
height: 22px;
margin: auto;
}
.icon.box {
border: 1px solid #7890a5;
border-radius: 3px;
background: #35495c;
box-shadow: inset -4px -4px rgba(0, 0, 0, 0.16);
}
.icon.cross::before,
.icon.cross::after {
position: absolute;
top: 10px;
left: 2px;
width: 19px;
height: 3px;
border-radius: 2px;
background: var(--red);
content: "";
}
.icon.cross::before {
transform: rotate(45deg);
}
.icon.cross::after {
transform: rotate(-45deg);
}
.icon.diamond {
width: 15px;
height: 15px;
margin-top: 3px;
background: var(--green);
clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
filter: drop-shadow(0 0 5px rgba(92, 230, 164, 0.7));
}
.icon.triangle {
width: 0;
height: 0;
margin-top: 3px;
border-right: 10px solid transparent;
border-bottom: 18px solid var(--orange);
border-left: 10px solid transparent;
filter: drop-shadow(0 0 4px rgba(255, 157, 82, 0.6));
}
.icon.ring {
width: 21px;
height: 21px;
margin-top: 0;
border: 3px solid var(--yellow);
border-radius: 50%;
box-shadow: 0 0 7px rgba(255, 212, 90, 0.45);
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 13px;
padding-top: 13px;
border-top: 1px solid rgba(147, 190, 220, 0.12);
}
.controls label,
button {
color: #c5d5e2;
font: inherit;
font-size: 10px;
}
.controls label {
display: flex;
align-items: center;
gap: 5px;
cursor: pointer;
}
input {
accent-color: var(--cyan);
}
button {
margin-left: auto;
padding: 6px 9px;
border: 1px solid rgba(98, 217, 255, 0.25);
border-radius: 7px;
background: rgba(98, 217, 255, 0.08);
cursor: pointer;
}
button:hover {
background: rgba(98, 217, 255, 0.16);
}
#hint {
position: absolute;
right: 22px;
bottom: 18px;
padding: 8px 11px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(7, 17, 31, 0.72);
color: var(--muted);
font-size: 10px;
backdrop-filter: blur(10px);
}
#tooltip {
position: fixed;
z-index: 20;
display: none;
min-width: 190px;
max-width: 250px;
padding: 11px 12px;
border: 1px solid rgba(98, 217, 255, 0.3);
border-radius: 10px;
background: rgba(5, 13, 25, 0.96);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.45);
pointer-events: none;
}
#tooltip strong {
display: block;
margin-bottom: 4px;
font-size: 12px;
letter-spacing: 0.04em;
}
#tooltip span {
display: block;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
@media (max-width: 780px) {
#summary {
top: 10px;
left: 10px;
width: min(350px, calc(100vw - 20px));
max-height: 54vh;
overflow: auto;
padding: 15px;
}
h1 {
font-size: 21px;
}
.intro {
display: none;
}
#legend {
top: auto;
right: 10px;
bottom: 10px;
width: min(286px, calc(100vw - 20px));
padding: 13px;
}
#legend .legend-row:nth-child(5) {
display: none;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div class="hud">
<section class="panel" id="summary">
<p class="eyebrow">Base rates in the factory</p>
<h1>Good detection can still mean many false alarms.</h1>
<p class="intro">
A synthetic batch of 416 products contains 40 defective units.
Each marker shows the detector's outcome.
</p>
<div class="metrics">
<div class="metric base">
<span>Defect base rate</span>
<strong id="baseRate">—</strong>
</div>
<div class="metric ppv">
<span>PPV after a flag</span>
<strong id="ppv">—</strong>
</div>
<div class="metric npv">
<span>NPV after no flag</span>
<strong id="npv">—</strong>
</div>
<div class="metric">
<span>Sensitivity</span>
<strong id="sensitivity">—</strong>
</div>
</div>
<div class="section-title">Probability a unit is defective</div>
<div class="probability">
<label>
<span>Before testing · base rate</span>
<b id="priorText">—</b>
</label>
<div class="track"><i id="barBase"></i></div>
</div>
<div class="probability">
<label>
<span>After a positive flag · PPV</span>
<b id="positiveText">—</b>
</label>
<div class="track"><i id="barPositive"></i></div>
</div>
<div class="probability">
<label>
<span>After a negative result · 1 − NPV</span>
<b id="negativeText">—</b>
</label>
<div class="track"><i id="barNegative"></i></div>
</div>
<div class="counts">
<div><span>True positives</span><b id="countTP">—</b></div>
<div><span>False positives</span><b id="countFP">—</b></div>
<div><span>False negatives</span><b id="countFN">—</b></div>
<div><span>True negatives</span><b id="countTN">—</b></div>
</div>
<p class="insight" id="insight"></p>
</section>
<section class="panel" id="legend">
<h2>Reading the grid</h2>
<div class="legend-row">
<i class="icon diamond"></i>
<span>True positive</span>
<b id="legendTP">—</b>
</div>
<div class="legend-row">
<i class="icon triangle"></i>
<span>False positive</span>
<b id="legendFP">—</b>
</div>
<div class="legend-row">
<i class="icon cross"></i>
<span>Defective · flagged or missed</span>
<b id="legendDefects">—</b>
</div>
<div class="legend-row">
<i class="icon ring"></i>
<span>Any flagged unit</span>
<b id="legendFlagged">—</b>
</div>
<div class="legend-row">
<i class="icon box"></i>
<span>True negative</span>
<b id="legendTN">—</b>
</div>
<div class="controls">
<label>
<input id="toggleFlags" type="checkbox" checked>
Flags
</label>
<label>
<input id="toggleDefects" type="checkbox" checked>
Defect marks
</label>
<button id="resetView" type="button">Reset view</button>
</div>
</section>
<div id="hint">Drag to orbit · Shift/right-drag to pan · Wheel to zoom · Hover a unit</div>
</div>
<div id="tooltip" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Synthetic population
// -------------------------------------------------------------------------
const TOTAL = 416;
const COLUMNS = 26;
const ROWS = 16;
const DEFECTS = 40;
const TRUE_POSITIVES = 32;
const FALSE_POSITIVES = 34;
const SPACING = 1.12;
const GRID_WIDTH = (COLUMNS - 1) * SPACING;
const GRID_DEPTH = (ROWS - 1) * SPACING;
function mulberry32(seed) {
return function () {
let value = seed += 0x6D2B79F5;
value = Math.imul(value ^ value >>> 15, value | 1);
value ^= value + Math.imul(value ^ value >>> 7, value | 61);
return ((value ^ value >>> 14) >>> 0) / 4294967296;
};
}
const random = mulberry32(20250308);
const shuffled = Array.from({ length: TOTAL }, (_, index) => index);
for (let index = shuffled.length - 1; index > 0; index--) {
const swapIndex = Math.floor(random() * (index + 1));
[shuffled[index], shuffled[swapIndex]] =
[shuffled[swapIndex], shuffled[index]];
}
const defectIndices = shuffled.slice(0, DEFECTS);
const truePositiveIndices = new Set(defectIndices.slice(0, TRUE_POSITIVES));
const falsePositiveIndices = new Set(
shuffled.slice(DEFECTS, DEFECTS + FALSE_POSITIVES)
);
const defectSet = new Set(defectIndices);
const stats = {
total: TOTAL,
defects: DEFECTS,
tp: TRUE_POSITIVES,
fn: DEFECTS - TRUE_POSITIVES,
fp: FALSE_POSITIVES,
tn: TOTAL - DEFECTS - FALSE_POSITIVES
};
stats.flagged = stats.tp + stats.fp;
stats.notFlagged = stats.fn + stats.tn;
const baseRate = stats.defects / stats.total;
const ppv = stats.tp / stats.flagged;
const npv = stats.tn / stats.notFlagged;
const sensitivity = stats.tp / stats.defects;
const specificity = stats.tn / (stats.total - stats.defects);
const negativeRisk = 1 - npv;
const percent = value => `${(value * 100).toFixed(1)}%`;
document.getElementById("baseRate").textContent = percent(baseRate);
document.getElementById("ppv").textContent = percent(ppv);
document.getElementById("npv").textContent = percent(npv);
document.getElementById("sensitivity").textContent = percent(sensitivity);
document.getElementById("priorText").textContent = percent(baseRate);
document.getElementById("positiveText").textContent = percent(ppv);
document.getElementById("negativeText").textContent = percent(negativeRisk);
document.getElementById("barBase").style.width = percent(baseRate);
document.getElementById("barPositive").style.width = percent(ppv);
document.getElementById("barNegative").style.width = percent(negativeRisk);
document.getElementById("countTP").textContent = stats.tp;
document.getElementById("countFP").textContent = stats.fp;
document.getElementById("countFN").textContent = stats.fn;
document.getElementById("countTN").textContent = stats.tn;
document.getElementById("legendTP").textContent = stats.tp;
document.getElementById("legendFP").textContent = stats.fp;
document.getElementById("legendDefects").textContent = stats.defects;
document.getElementById("legendFlagged").textContent = stats.flagged;
document.getElementById("legendTN").textContent = stats.tn;
document.getElementById("insight").textContent =
`${stats.tp} of ${stats.flagged} flags are true defects. ` +
`The positive predictive value is ${percent(ppv)}, even though ` +
`only ${percent(baseRate)} of the batch was defective before testing.`;
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 52, 112);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
180
);
const hemisphere = new THREE.HemisphereLight(0x9ddfff, 0x101722, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(13, 29, 18);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -25;
keyLight.shadow.camera.right = 25;
keyLight.shadow.camera.top = 23;
keyLight.shadow.camera.bottom = -23;
keyLight.shadow.camera.near = 2;
keyLight.shadow.camera.far = 70;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x31bfff, 28, 38, 2);
rimLight.position.set(-18, 11, -12);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffbd70, 18, 32, 2);
warmLight.position.set(19, 7, 12);
scene.add(warmLight);
// Floor and raised inspection platform.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(160, 160),
new THREE.MeshStandardMaterial({
color: 0x081321,
roughness: 0.94,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.36;
floor.receiveShadow = true;
scene.add(floor);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(GRID_WIDTH + 2.0, 0.34, GRID_DEPTH + 2.0),
new THREE.MeshStandardMaterial({
color: 0x14263a,
roughness: 0.72,
metalness: 0.18,
flatShading: true
})
);
platform.position.y = 0;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const platformEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(platform.geometry),
new THREE.LineBasicMaterial({
color: 0x4d7997,
transparent: true,
opacity: 0.42
})
);
platformEdges.position.copy(platform.position);
scene.add(platformEdges);
// Subtle grid lines.
const gridPositions = [];
const platformTop = 0.175;
for (let column = 0; column < COLUMNS; column++) {
const x = (column - (COLUMNS - 1) / 2) * SPACING;
gridPositions.push(
x, platformTop, -GRID_DEPTH / 2,
x, platformTop, GRID_DEPTH / 2
);
}
for (let row = 0; row < ROWS; row++) {
const z = (row - (ROWS - 1) / 2) * SPACING;
gridPositions.push(
-GRID_WIDTH / 2, platformTop, z,
GRID_WIDTH / 2, platformTop, z
);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
const gridLines = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x426883,
transparent: true,
opacity: 0.22
})
);
scene.add(gridLines);
// -------------------------------------------------------------------------
// Products
// -------------------------------------------------------------------------
const productGeometry = new THREE.BoxGeometry(0.82, 0.58, 0.82);
const productMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.78,
metalness: 0.08,
flatShading: true
});
const products = new THREE.InstancedMesh(
productGeometry,
productMaterial,
TOTAL
);
products.instanceMatrix.setUsage(THREE.StaticDrawUsage);
products.castShadow = true;
products.receiveShadow = true;
const itemPositions = [];
const matrix = new THREE.Matrix4();
const instanceColor = new THREE.Color();
let itemIndex = 0;
for (let row = 0; row < ROWS; row++) {
for (let column = 0; column < COLUMNS; column++) {
const x = (column - (COLUMNS - 1) / 2) * SPACING;
const z = (row - (ROWS - 1) / 2) * SPACING;
const y = platformTop + 0.29;
itemPositions[itemIndex] = new THREE.Vector3(x, y, z);
matrix.makeTranslation(x, y, z);
products.setMatrixAt(itemIndex, matrix);
if (defectSet.has(itemIndex)) {
instanceColor.setHSL(0.015, 0.44, 0.31 + random() * 0.035);
} else {
instanceColor.setHSL(0.57, 0.16, 0.30 + random() * 0.075);
}
products.setColorAt(itemIndex, instanceColor);
itemIndex++;
}
}
products.instanceMatrix.needsUpdate = true;
products.instanceColor.needsUpdate = true;
scene.add(products);
// -------------------------------------------------------------------------
// Defect marks: red X on defective units.
// -------------------------------------------------------------------------
const defectGroup = new THREE.Group();
scene.add(defectGroup);
const defectMaterial = new THREE.MeshStandardMaterial({
color: 0xff4059,
emissive: 0x7d1022,
emissiveIntensity: 0.8,
roughness: 0.55,
flatShading: true
});
const defectBarGeometry = new THREE.BoxGeometry(0.57, 0.035, 0.075);
for (const index of defectIndices) {
const position = itemPositions[index];
const mark = new THREE.Group();
mark.position.set(position.x, platformTop + 0.605, position.z);
const first = new THREE.Mesh(defectBarGeometry, defectMaterial);
first.rotation.y = Math.PI / 4;
first.castShadow = true;
const second = new THREE.Mesh(defectBarGeometry, defectMaterial);
second.rotation.y = -Math.PI / 4;
second.castShadow = true;
mark.add(first, second);
defectGroup.add(mark);
}
// -------------------------------------------------------------------------
// Detector outcomes:
// yellow ring = all flagged units
// green diamond = true positive
// orange triangle = false positive
// -------------------------------------------------------------------------
const outcomeGroup = new THREE.Group();
scene.add(outcomeGroup);
const ringGeometry = new THREE.TorusGeometry(0.53, 0.035, 4, 12);
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0xffd45a,
emissive: 0x9b6810,
emissiveIntensity: 1.35,
roughness: 0.45,
flatShading: true
});
const poleGeometry = new THREE.CylinderGeometry(0.024, 0.024, 0.65, 6);
const poleMaterial = new THREE.MeshStandardMaterial({
color: 0x52677a,
roughness: 0.5,
metalness: 0.4,
flatShading: true
});
const truePositiveMaterial = new THREE.MeshStandardMaterial({
color: 0x5ce6a4,
emissive: 0x168d5b,
emissiveIntensity: 1.8,
roughness: 0.35,
flatShading: true
});
const falsePositiveMaterial = new THREE.MeshStandardMaterial({
color: 0xff9d52,
emissive: 0xa8460d,
emissiveIntensity: 1.5,
roughness: 0.4,
flatShading: true
});
const truePositiveShape = new THREE.OctahedronGeometry(0.17, 0);
const falsePositiveShape = new THREE.ConeGeometry(0.18, 0.31, 4);
for (let index = 0; index < TOTAL; index++) {
const isTruePositive = truePositiveIndices.has(index);
const isFalsePositive = falsePositiveIndices.has(index);
if (!isTruePositive && !isFalsePositive) continue;
const position = itemPositions[index];
const ring = new THREE.Mesh(ringGeometry, ringMaterial);
ring.rotation.x = Math.PI / 2;
ring.position.set(position.x, platformTop + 0.035, position.z);
ring.castShadow = true;
outcomeGroup.add(ring);
const pole = new THREE.Mesh(poleGeometry, poleMaterial);
pole.position.set(position.x, platformTop + 0.90, position.z);
pole.castShadow = true;
outcomeGroup.add(pole);
const marker = new THREE.Mesh(
isTruePositive ? truePositiveShape : falsePositiveShape,
isTruePositive ? truePositiveMaterial : falsePositiveMaterial
);
marker.position.set(position.x, platformTop + 1.32, position.z);
marker.castShadow = true;
outcomeGroup.add(marker);
}
// -------------------------------------------------------------------------
// Industrial detector gantry
// -------------------------------------------------------------------------
const gantry = new THREE.Group();
gantry.position.z = -GRID_DEPTH / 2 - 0.8;
scene.add(gantry);
const gantryMaterial = new THREE.MeshStandardMaterial({
color: 0x1b3046,
roughness: 0.58,
metalness: 0.45,
flatShading: true
});
const cyanMaterial = new THREE.MeshStandardMaterial({
color: 0x54d9ff,
emissive: 0x1588b5,
emissiveIntensity: 1.7,
roughness: 0.3,
flatShading: true
});
for (const side of [-1, 1]) {
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.5, 4.2, 0.5),
gantryMaterial
);
post.position.set(side * (GRID_WIDTH / 2 + 0.55), 2.05, 0);
post.castShadow = true;
gantry.add(post);
const postLight = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 2.8, 0.54),
cyanMaterial
);
postLight.position.set(
side * (GRID_WIDTH / 2 + 0.31),
2.05,
0
);
gantry.add(postLight);
}
const crossbar = new THREE.Mesh(
new THREE.BoxGeometry(GRID_WIDTH + 1.6, 0.46, 0.58),
gantryMaterial
);
crossbar.position.y = 4.12;
crossbar.castShadow = true;
gantry.add(crossbar);
const scannerStrip = new THREE.Mesh(
new THREE.BoxGeometry(GRID_WIDTH + 0.8, 0.08, 0.62),
cyanMaterial
);
scannerStrip.position.y = 3.86;
gantry.add(scannerStrip);
const scanPlane = new THREE.Mesh(
new THREE.BoxGeometry(GRID_WIDTH, 3.55, 0.025),
new THREE.MeshBasicMaterial({
color: 0x43cfff,
transparent: true,
opacity: 0.035,
depthWrite: false
})
);
scanPlane.position.y = 2.05;
gantry.add(scanPlane);
function createLabel(text, color = "#9cecff") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 1024;
labelCanvas.height = 160;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 1024, 160);
context.fillStyle = "rgba(7, 20, 35, 0.88)";
context.fillRect(0, 0, 1024, 160);
context.strokeStyle = color;
context.lineWidth = 4;
context.strokeRect(2, 2, 1020, 156);
context.fillStyle = color;
context.font = "700 46px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 512, 80);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false
})
);
sprite.scale.set(8.7, 1.36, 1);
sprite.renderOrder = 10;
return sprite;
}
const detectorLabel = createLabel("OPTICAL DETECTOR // OUTCOME MAP");
detectorLabel.position.set(0, 4.9, 0);
gantry.add(detectorLabel);
// Distant low-poly factory silhouettes.
const backgroundMaterial = new THREE.MeshStandardMaterial({
color: 0x0d1d2d,
roughness: 0.9,
metalness: 0.1,
flatShading: true
});
for (let index = 0; index < 12; index++) {
const height = 3 + random() * 8;
const building = new THREE.Mesh(
new THREE.BoxGeometry(
2 + random() * 4,
height,
2 + random() * 4
),
backgroundMaterial
);
building.position.set(
(random() - 0.5) * 78,
-0.36 + height / 2,
-27 - random() * 27
);
building.castShadow = true;
building.receiveShadow = true;
scene.add(building);
}
// -------------------------------------------------------------------------
// Selection outline
// -------------------------------------------------------------------------
const selection = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(0.89, 0.65, 0.89)),
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.9
})
);
selection.visible = false;
scene.add(selection);
// -------------------------------------------------------------------------
// Camera controls implemented directly, without addons.
// -------------------------------------------------------------------------
const initialView = {
theta: 0.72,
phi: 1.02,
radius: 44,
target: new THREE.Vector3(0, 0.35, 0)
};
const view = {
theta: initialView.theta,
phi: initialView.phi,
radius: initialView.radius,
target: initialView.target.clone()
};
function updateCamera() {
camera.position.setFromSphericalCoords(
view.radius,
view.phi,
view.theta
);
camera.position.add(view.target);
camera.lookAt(view.target);
camera.updateMatrixWorld();
}
function resetView() {
view.theta = initialView.theta;
view.phi = initialView.phi;
view.radius = initialView.radius;
view.target.copy(initialView.target);
updateCamera();
}
updateCamera();
let drag = null;
canvas.addEventListener("contextmenu", event => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
drag = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
startX: event.clientX,
startY: event.clientY,
mode:
event.button === 2 || event.shiftKey ||
event.pointerType === "touch"
? "pan"
: "orbit",
moved: false
};
tooltip.style.display = "none";
selection.visible = false;
});
const screenRight = new THREE.Vector3();
const screenUp = new THREE.Vector3();
canvas.addEventListener("pointermove", event => {
if (drag && drag.pointerId === event.pointerId) {
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
drag.x = event.clientX;
drag.y = event.clientY;
if (
Math.abs(event.clientX - drag.startX) +
Math.abs(event.clientY - drag.startY) > 3
) {
drag.moved = true;
}
if (drag.mode === "orbit") {
view.theta -= dx * 0.006;
view.phi = THREE.MathUtils.clamp(
view.phi + dy * 0.005,
0.32,
1.42
);
} else {
screenRight.setFromMatrixColumn(camera.matrix, 0);
screenUp.setFromMatrixColumn(camera.matrix, 1);
const panScale = view.radius * 0.00135;
view.target
.addScaledVector(screenRight, -dx * panScale)
.addScaledVector(screenUp, dy * panScale);
view.target.x = THREE.MathUtils.clamp(
view.target.x,
-12,
12
);
view.target.z = THREE.MathUtils.clamp(
view.target.z,
-9,
9
);
view.target.y = THREE.MathUtils.clamp(
view.target.y,
-1,
5
);
}
updateCamera();
return;
}
inspectProduct(event);
});
function endDrag(event) {
if (drag && drag.pointerId === event.pointerId) {
drag = null;
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("pointerleave", () => {
if (!drag) {
tooltip.style.display = "none";
selection.visible = false;
}
});
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius * Math.exp(event.deltaY * 0.001),
15,
82
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", event => {
const key = event.key.toLowerCase();
if (key === "r") {
resetView();
} else if (key === "arrowleft") {
view.theta -= 0.08;
updateCamera();
} else if (key === "arrowright") {
view.theta += 0.08;
updateCamera();
} else if (key === "arrowup") {
view.phi = Math.max(0.32, view.phi - 0.05);
updateCamera();
} else if (key === "arrowdown") {
view.phi = Math.min(1.42, view.phi + 0.05);
updateCamera();
} else if (key === "+" || key === "=") {
view.radius = Math.max(15, view.radius * 0.9);
updateCamera();
} else if (key === "-") {
view.radius = Math.min(82, view.radius * 1.1);
updateCamera();
}
});
document.getElementById("resetView").addEventListener("click", resetView);
document.getElementById("toggleFlags").addEventListener("change", event => {
outcomeGroup.visible = event.target.checked;
});
document.getElementById("toggleDefects").addEventListener("change", event => {
defectGroup.visible = event.target.checked;
});
// -------------------------------------------------------------------------
// Hover inspection
// -------------------------------------------------------------------------
const tooltip = document.getElementById("tooltip");
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function inspectProduct(event) {
const bounds = canvas.getBoundingClientRect();
pointer.x =
((event.clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y =
-((event.clientY - bounds.top) / bounds.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObject(products, false);
if (!hits.length) {
tooltip.style.display = "none";
selection.visible = false;
return;
}
const index = hits[0].instanceId;
const position = itemPositions[index];
const isDefective = defectSet.has(index);
const isTruePositive = truePositiveIndices.has(index);
const isFalsePositive = falsePositiveIndices.has(index);
let title = "TRUE NEGATIVE";
let color = "#91a8bd";
let detail = "Sound unit · detector did not flag it";
if (isTruePositive) {
title = "TRUE POSITIVE";
color = "#5ce6a4";
detail = "Defective unit · correctly flagged";
} else if (isFalsePositive) {
title = "FALSE POSITIVE";
color = "#ff9d52";
detail = "Sound unit · incorrectly flagged";
} else if (isDefective) {
title = "FALSE NEGATIVE";
color = "#ff5d6c";
detail = "Defective unit · detector missed it";
}
tooltip.innerHTML = `
<strong style="color:${color}">${title}</strong>
<span>${detail}</span>
<span>Unit ${index + 1} of ${TOTAL}</span>
`;
tooltip.style.display = "block";
const tooltipWidth = tooltip.offsetWidth;
const tooltipHeight = tooltip.offsetHeight;
tooltip.style.left =
`${Math.min(
window.innerWidth - tooltipWidth - 12,
event.clientX + 16
)}px`;
tooltip.style.top =
`${Math.min(
window.innerHeight - tooltipHeight - 12,
event.clientY + 16
)}px`;
selection.position.set(
position.x,
position.y,
position.z
);
selection.visible = true;
}
// -------------------------------------------------------------------------
// Resize and render
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```