Prompt specification
Build or customize the Three.js 3D scene "Same detector different prevalence comparison" 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: Same detector different prevalence comparison
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/same-detector-different-prevalence-comparison-x1762
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring base rates and predictive values of a detector. Apply identical stated sensitivity and false-positive rate to two synthetic product populations with different defect rates; compare the fraction of flagged items actually defective. Stylized solid forms.
Working Three.js Starter (same-detector-different-prevalence-comparison-x1762):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Detector Base Rate Lab</title>
<style>
:root {
--ink: #17302f;
--muted: #687573;
--paper: #f4f1e9;
--panel: #fbf9f4;
--line: #dcd9d0;
--teal: #4d8b86;
--teal-light: #87b4aa;
--amber: #e59a45;
--amber-light: #f3bd70;
--coral: #e14b3b;
--purple: #6d5c91;
--shadow: 0 18px 55px rgba(25, 43, 40, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
color: var(--ink);
background: var(--paper);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
min-height: 100vh;
display: grid;
grid-template-rows: 76px minmax(0, 1fr);
}
.topbar {
position: relative;
z-index: 20;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 28px;
border-bottom: 1px solid var(--line);
background: rgba(251, 249, 244, 0.94);
backdrop-filter: blur(14px);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
width: 38px;
height: 38px;
position: relative;
display: grid;
place-items: center;
border-radius: 12px;
background: var(--ink);
box-shadow: 0 8px 20px rgba(23, 48, 47, 0.2);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
border-radius: 50%;
}
.brand-mark::before {
width: 16px;
height: 16px;
border: 2px solid #f6f0e6;
}
.brand-mark::after {
width: 4px;
height: 4px;
background: var(--coral);
box-shadow:
9px 0 0 var(--amber),
0 9px 0 var(--teal-light);
}
.brand strong {
display: block;
font-size: 15px;
letter-spacing: 0.02em;
}
.brand small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.header-status {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 12px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #4da878;
box-shadow: 0 0 0 5px rgba(77, 168, 120, 0.12);
}
.main {
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
}
.stage {
position: relative;
min-width: 0;
min-height: 620px;
overflow: hidden;
background: #e8e4da;
isolation: isolate;
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
.stage-copy {
position: absolute;
z-index: 4;
top: 28px;
left: 30px;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 9px;
color: var(--coral);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.stage h1 {
margin: 0;
font-size: clamp(30px, 4.2vw, 56px);
line-height: 0.98;
letter-spacing: -0.055em;
font-weight: 760;
}
.stage h1 em {
color: var(--teal);
font-family: Georgia, "Times New Roman", serif;
font-weight: 500;
}
.stage-copy p:last-child {
max-width: 390px;
margin: 16px 0 0;
color: #5c6966;
font-size: 13px;
line-height: 1.6;
}
.view-tools {
position: absolute;
z-index: 8;
top: 26px;
right: 26px;
display: flex;
gap: 8px;
}
.tool-button {
min-height: 36px;
padding: 0 13px;
border: 1px solid rgba(23, 48, 47, 0.14);
border-radius: 999px;
background: rgba(251, 249, 244, 0.85);
box-shadow: 0 7px 22px rgba(23, 48, 47, 0.08);
backdrop-filter: blur(10px);
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition:
transform 150ms ease,
background 150ms ease;
}
.tool-button:hover {
transform: translateY(-1px);
background: #fffdf8;
}
.tool-button[aria-pressed="true"] {
color: #fff;
border-color: var(--ink);
background: var(--ink);
}
.population-label {
position: absolute;
z-index: 5;
display: flex;
align-items: center;
gap: 9px;
min-width: 158px;
padding: 9px 12px 9px 9px;
border: 1px solid rgba(23, 48, 47, 0.12);
border-radius: 13px;
background: rgba(251, 249, 244, 0.91);
box-shadow: var(--shadow);
backdrop-filter: blur(10px);
pointer-events: none;
transform: translate(-50%, -100%);
will-change: left, top;
}
.population-label .letter {
width: 33px;
height: 33px;
display: grid;
place-items: center;
flex: 0 0 auto;
border-radius: 10px;
color: white;
font-size: 13px;
font-weight: 850;
}
.population-label.a .letter {
background: var(--teal);
}
.population-label.b .letter {
background: var(--purple);
}
.population-label strong {
display: block;
font-size: 12px;
line-height: 1.25;
}
.population-label small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.legend {
position: absolute;
z-index: 6;
left: 26px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
gap: 16px;
max-width: 480px;
padding: 11px 14px;
border: 1px solid rgba(23, 48, 47, 0.11);
border-radius: 13px;
background: rgba(251, 249, 244, 0.88);
box-shadow: 0 10px 35px rgba(23, 48, 47, 0.09);
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #52615e;
font-size: 11px;
font-weight: 650;
}
.swatch {
width: 14px;
height: 14px;
border-radius: 4px;
background: var(--teal);
}
.swatch.defective {
background: var(--amber);
}
.swatch.flagged {
position: relative;
border: 3px solid var(--coral);
background: transparent;
border-radius: 50%;
}
.scene-hint {
position: absolute;
z-index: 6;
right: 26px;
bottom: 27px;
color: #6b7774;
font-size: 11px;
font-weight: 650;
pointer-events: none;
}
.tooltip {
position: absolute;
z-index: 30;
display: none;
width: 190px;
padding: 12px 13px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 12px;
color: #f8f6ef;
background: rgba(23, 48, 47, 0.95);
box-shadow: 0 12px 30px rgba(10, 25, 23, 0.25);
pointer-events: none;
backdrop-filter: blur(10px);
}
.tooltip strong {
display: block;
margin-bottom: 6px;
font-size: 12px;
}
.tooltip span {
display: block;
margin-top: 3px;
color: #c9d3ce;
font-size: 11px;
}
.panel {
position: relative;
z-index: 10;
min-height: 0;
overflow-y: auto;
padding: 25px 24px 30px;
border-left: 1px solid var(--line);
background: var(--panel);
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 22px;
}
.panel-heading h2 {
margin: 0;
font-size: 21px;
letter-spacing: -0.03em;
}
.panel-heading p {
margin: 6px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.restore-button {
flex: 0 0 auto;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: transparent;
cursor: pointer;
color: var(--muted);
font-size: 10px;
font-weight: 750;
}
.restore-button:hover {
color: var(--ink);
border-color: #bfc3bc;
background: #fff;
}
.section {
margin-top: 21px;
padding-top: 20px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
}
.section-title h3 {
margin: 0;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.section-number {
color: #a3aaa5;
font-family: Georgia, serif;
font-size: 12px;
font-style: italic;
}
.control {
margin-bottom: 17px;
}
.control:last-child {
margin-bottom: 0;
}
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 9px;
}
.control label {
font-size: 12px;
font-weight: 700;
}
.control output {
min-width: 52px;
padding: 4px 8px;
border-radius: 7px;
color: var(--ink);
background: #ebe9e2;
text-align: center;
font-size: 11px;
font-weight: 800;
}
.control.accent-a output {
color: #2b6c66;
background: #deebe7;
}
.control.accent-b output {
color: #5f4f80;
background: #e8e1f0;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 4px 0;
border-radius: 999px;
appearance: none;
background: #d9d8d1;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid var(--panel);
border-radius: 50%;
appearance: none;
background: var(--ink);
box-shadow: 0 0 0 1px var(--ink);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid var(--panel);
border-radius: 50%;
background: var(--ink);
box-shadow: 0 0 0 1px var(--ink);
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #9aa19c;
font-size: 9px;
font-weight: 700;
text-transform: uppercase;
}
.detector-badges {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 17px;
}
.badge {
padding: 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: #fffefa;
}
.badge span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge strong {
display: block;
margin-top: 3px;
font-size: 17px;
}
.comparison {
display: grid;
gap: 10px;
}
.result-card {
position: relative;
overflow: hidden;
padding: 15px;
border: 1px solid var(--line);
border-radius: 15px;
background: #fffefa;
box-shadow: 0 8px 24px rgba(30, 46, 42, 0.045);
}
.result-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 4px;
background: var(--teal);
}
.result-card.b::before {
background: var(--purple);
}
.result-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.result-head .rate {
color: var(--muted);
}
.ppv-line {
display: flex;
align-items: baseline;
gap: 10px;
margin: 9px 0 11px;
}
.ppv-line strong {
font-size: 36px;
line-height: 1;
letter-spacing: -0.055em;
}
.ppv-line span {
max-width: 125px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.result-stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
padding-top: 10px;
border-top: 1px solid #ebe8e0;
}
.result-stats span {
display: block;
color: var(--muted);
font-size: 9px;
}
.result-stats b {
display: block;
margin-top: 2px;
font-size: 13px;
}
.model-line,
.batch-line {
margin-top: 10px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.batch-line {
margin-top: 5px;
color: var(--ink);
font-weight: 700;
}
.insight {
margin-top: 11px;
padding: 12px 13px;
border-left: 3px solid var(--coral);
border-radius: 0 10px 10px 0;
color: #4e5c59;
background: #f1ebe5;
font-size: 11px;
line-height: 1.5;
}
.formula {
padding: 14px;
border: 1px solid #d9ddd6;
border-radius: 13px;
background: #eef1eb;
}
.formula code {
display: block;
color: #38504c;
font-family: Georgia, "Times New Roman", serif;
font-size: 13px;
line-height: 1.55;
}
.formula p {
margin: 9px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.note {
margin: 13px 0 0;
color: #89928e;
font-size: 9px;
line-height: 1.5;
}
@media (max-width: 1050px) {
.main {
grid-template-columns: minmax(0, 1fr) 350px;
}
.panel {
padding-inline: 20px;
}
.stage-copy {
max-width: 340px;
}
.scene-hint {
display: none;
}
}
@media (max-width: 820px) {
.app {
grid-template-rows: 68px auto;
}
.main {
display: block;
}
.stage {
height: 68vh;
min-height: 570px;
}
.panel {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
.stage-copy {
top: 22px;
left: 22px;
}
.stage h1 {
font-size: 36px;
}
.stage-copy p:last-child {
max-width: 310px;
font-size: 11px;
}
.view-tools {
top: auto;
right: 18px;
bottom: 20px;
}
.legend {
left: 18px;
bottom: 20px;
max-width: calc(100% - 220px);
gap: 10px;
}
.legend-item {
font-size: 9px;
}
.population-label {
min-width: 135px;
padding: 7px 9px 7px 7px;
}
.population-label .letter {
width: 28px;
height: 28px;
}
.population-label strong {
font-size: 10px;
}
.population-label small {
font-size: 8px;
}
}
@media (max-width: 520px) {
.topbar {
padding: 0 18px;
}
.header-status {
display: none;
}
.stage {
min-height: 530px;
}
.stage-copy {
right: 20px;
}
.stage h1 {
font-size: 32px;
}
.stage-copy p:last-child {
display: none;
}
.legend {
right: 18px;
max-width: none;
}
.view-tools {
top: 20px;
right: 18px;
bottom: auto;
flex-direction: column;
}
.tool-button {
min-height: 32px;
padding-inline: 10px;
font-size: 9px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<strong>Signal / Check</strong>
<small>Base rate laboratory</small>
</div>
</div>
<div class="header-status">
<span class="status-dot"></span>
Interactive probability model
</div>
</header>
<main class="main">
<section class="stage" aria-label="Interactive 3D comparison of two product populations">
<div id="scene"></div>
<div class="stage-copy">
<p class="eyebrow">Prevalence changes what a positive means</p>
<h1>Same detector.<br /><em>Different truth.</em></h1>
<p>
Both populations use identical sensitivity and false-positive settings.
Change their defect rates and watch predictive value shift.
</p>
</div>
<div class="view-tools">
<button
id="focusFlags"
class="tool-button"
type="button"
aria-pressed="false"
>
Flagged only
</button>
<button id="resetView" class="tool-button" type="button">
Reset view
</button>
</div>
<div id="labelA" class="population-label a">
<div class="letter">A</div>
<div>
<strong class="rate">4% defective</strong>
<small>Lower prevalence</small>
</div>
</div>
<div id="labelB" class="population-label b">
<div class="letter">B</div>
<div>
<strong class="rate">40% defective</strong>
<small>Higher prevalence</small>
</div>
</div>
<div class="legend" aria-label="Scene legend">
<div class="legend-item">
<span class="swatch"></span>
Not defective
</div>
<div class="legend-item">
<span class="swatch defective"></span>
Defective
</div>
<div class="legend-item">
<span class="swatch flagged"></span>
Flagged by detector
</div>
</div>
<div class="scene-hint">Drag to orbit · Scroll to zoom · Hover an item</div>
<div id="tooltip" class="tooltip"></div>
</section>
<aside class="panel" aria-label="Detector and population controls">
<div class="panel-heading">
<div>
<h2>Change the priors</h2>
<p>Keep the detector fixed. Change only the populations.</p>
</div>
<button id="restoreExample" class="restore-button" type="button">
Restore example
</button>
</div>
<section class="section">
<div class="section-title">
<h3>Population base rates</h3>
<span class="section-number">01</span>
</div>
<div class="control accent-a">
<div class="control-head">
<label for="rateA">Population A defect rate</label>
<output id="outRateA" for="rateA">4%</output>
</div>
<input id="rateA" type="range" min="0" max="80" value="4" step="1" />
<div class="range-ends"><span>0%</span><span>80%</span></div>
</div>
<div class="control accent-b">
<div class="control-head">
<label for="rateB">Population B defect rate</label>
<output id="outRateB" for="rateB">40%</output>
</div>
<input id="rateB" type="range" min="0" max="95" value="40" step="1" />
<div class="range-ends"><span>0%</span><span>95%</span></div>
</div>
</section>
<section class="section">
<div class="section-title">
<h3>One detector, applied identically</h3>
<span class="section-number">02</span>
</div>
<div class="detector-badges">
<div class="badge">
<span>Sensitivity</span>
<strong id="sensBadge">90%</strong>
</div>
<div class="badge">
<span>False positives</span>
<strong id="fprBadge">5%</strong>
</div>
</div>
<div class="control">
<div class="control-head">
<label for="sensitivity">Sensitivity</label>
<output id="outSensitivity" for="sensitivity">90%</output>
</div>
<input id="sensitivity" type="range" min="50" max="100" value="90" step="1" />
<div class="range-ends"><span>50%</span><span>100%</span></div>
</div>
<div class="control">
<div class="control-head">
<label for="falsePositive">False-positive rate</label>
<output id="outFalsePositive" for="falsePositive">5%</output>
</div>
<input id="falsePositive" type="range" min="0" max="25" value="5" step="1" />
<div class="range-ends"><span>0%</span><span>25%</span></div>
</div>
</section>
<section class="section">
<div class="section-title">
<h3>Predictive value compared</h3>
<span class="section-number">03</span>
</div>
<div class="comparison">
<article id="cardA" class="result-card a"></article>
<article id="cardB" class="result-card b"></article>
</div>
<div id="insight" class="insight" aria-live="polite"></div>
<div class="formula" style="margin-top: 12px">
<code>
PPV = (sensitivity × base rate)<br />
÷ (sensitivity × base rate + false-positive rate × (1 − base rate))
</code>
<p>
PPV is the probability that a flagged item is actually defective.
The base rate changes this posterior probability even when the detector does not.
</p>
</div>
<p class="note">
Each scene contains a deterministic 400-item synthetic batch. Body color encodes
actual defect status; the coral ring encodes a detector flag. Population PPV is
the expected value from the probability model.
</p>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const ITEM_COUNT = 400;
const COLUMNS = 20;
const ROWS = 20;
const SPACING = 0.34;
const sceneElement = document.getElementById("scene");
const tooltip = document.getElementById("tooltip");
const controls = {
rateA: document.getElementById("rateA"),
rateB: document.getElementById("rateB"),
sensitivity: document.getElementById("sensitivity"),
falsePositive: document.getElementById("falsePositive")
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e4da);
scene.fog = new THREE.Fog(0xe8e4da, 30, 58);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.03;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneElement.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfffaf0, 0x777065, 2.3);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.4);
keyLight.position.set(-10, 18, 12);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -24;
keyLight.shadow.camera.right = 24;
keyLight.shadow.camera.top = 18;
keyLight.shadow.camera.bottom = -18;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 55;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xbfd8e8, 1.1);
fillLight.position.set(14, 9, -10);
scene.add(fillLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xded9ce,
roughness: 0.96,
metalness: 0
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.72;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(60, 30, 0x9ca39d, 0xb9b8b1);
grid.position.y = -0.705;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const bodyGeometry = new THREE.CylinderGeometry(0.115, 0.145, 0.52, 8, 1);
const capGeometry = new THREE.CylinderGeometry(0.09, 0.108, 0.075, 8, 1);
const ringGeometry = new THREE.TorusGeometry(0.205, 0.024, 8, 18);
const pickGeometry = new THREE.CylinderGeometry(0.21, 0.21, 0.65, 6, 1);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.64,
metalness: 0.03
});
const capMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.52,
metalness: 0.02
});
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0xe14b3b,
roughness: 0.46,
metalness: 0.02
});
const pickMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
const platformTopMaterial = new THREE.MeshStandardMaterial({
color: 0xf8f5ed,
roughness: 0.78,
metalness: 0
});
const platformBaseMaterial = new THREE.MeshStandardMaterial({
color: 0x273b39,
roughness: 0.7,
metalness: 0.04
});
const accentMaterials = {
a: new THREE.MeshStandardMaterial({
color: 0x4d8b86,
roughness: 0.56
}),
b: new THREE.MeshStandardMaterial({
color: 0x6d5c91,
roughness: 0.56
})
};
const pickMeshes = [];
const populations = [
createPopulation("a", -5.65),
createPopulation("b", 5.65)
];
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
function createPopulation(id, x) {
const group = new THREE.Group();
group.position.x = x;
scene.add(group);
const top = new THREE.Mesh(
new THREE.BoxGeometry(8.5, 0.56, 8.1),
platformTopMaterial
);
top.position.y = -0.28;
top.castShadow = true;
top.receiveShadow = true;
group.add(top);
const base = new THREE.Mesh(
new THREE.BoxGeometry(8.78, 0.25, 8.38),
platformBaseMaterial
);
base.position.y = -0.66;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const accent = new THREE.Mesh(
new THREE.BoxGeometry(2.2, 0.075, 0.13),
accentMaterials[id]
);
accent.position.set(0, 0.06, 3.78);
accent.castShadow = true;
group.add(accent);
const bodyMesh = new THREE.InstancedMesh(
bodyGeometry,
bodyMaterial,
ITEM_COUNT
);
bodyMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
bodyMesh.castShadow = true;
bodyMesh.receiveShadow = true;
bodyMesh.frustumCulled = false;
group.add(bodyMesh);
const capMesh = new THREE.InstancedMesh(
capGeometry,
capMaterial,
ITEM_COUNT
);
capMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
capMesh.castShadow = true;
capMesh.frustumCulled = false;
group.add(capMesh);
const ringMesh = new THREE.InstancedMesh(
ringGeometry,
ringMaterial,
ITEM_COUNT
);
ringMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
ringMesh.castShadow = true;
ringMesh.frustumCulled = false;
group.add(ringMesh);
const pickMesh = new THREE.InstancedMesh(
pickGeometry,
pickMaterial,
ITEM_COUNT
);
pickMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
pickMesh.frustumCulled = false;
group.add(pickMesh);
pickMesh.userData.populationId = id;
pickMeshes.push(pickMesh);
return {
id,
x,
group,
bodyMesh,
capMesh,
ringMesh,
pickMesh,
metadata: []
};
}
const hoverHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.25, 0.016, 8, 20),
new THREE.MeshBasicMaterial({ color: 0x17302f })
);
hoverHalo.rotation.x = Math.PI / 2;
hoverHalo.visible = false;
scene.add(hoverHalo);
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function shuffledIndices(length, random) {
const indices = Array.from({ length }, (_, index) => index);
for (let index = length - 1; index > 0; index--) {
const swapIndex = Math.floor(random() * (index + 1));
[indices[index], indices[swapIndex]] = [indices[swapIndex], indices[index]];
}
return indices;
}
function setInstanceMatrix(mesh, index, x, y, z, rotation, scale) {
dummy.position.set(x, y, z);
dummy.rotation.set(rotation.x, rotation.y, rotation.z);
dummy.scale.setScalar(scale);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
}
let flaggedOnly = false;
function updateScene() {
const rateA = Number(controls.rateA.value);
const rateB = Number(controls.rateB.value);
const sensitivity = Number(controls.sensitivity.value);
const falsePositiveRate = Number(controls.falsePositive.value);
const batchStats = [];
populations.forEach((population, populationIndex) => {
const rate = populationIndex === 0 ? rateA : rateB;
const random = mulberry32(
1701 +
populationIndex * 10007 +
rate * 101 +
sensitivity * 17 +
falsePositiveRate * 31
);
const defectCount = Math.round(ITEM_COUNT * rate / 100);
const truePositiveCount = Math.round(defectCount * sensitivity / 100);
const falsePositiveCount = Math.round(
(ITEM_COUNT - defectCount) * falsePositiveRate / 100
);
const allIndices = shuffledIndices(ITEM_COUNT, random);
const defectIndices = allIndices.slice(0, defectCount);
const normalIndices = allIndices.slice(defectCount);
const defective = new Array(ITEM_COUNT).fill(false);
const flagged = new Array(ITEM_COUNT).fill(false);
defectIndices.forEach((index) => defective[index] = true);
defectIndices.slice(0, truePositiveCount).forEach((index) => flagged[index] = true);
normalIndices.slice(0, falsePositiveCount).forEach((index) => flagged[index] = true);
population.metadata = [];
let truePositive = 0;
let falseNegative = 0;
let falsePositive = 0;
let trueNegative = 0;
for (let index = 0; index < ITEM_COUNT; index++) {
const column = index % COLUMNS;
const row = Math.floor(index / COLUMNS);
const x = (column - (COLUMNS - 1) / 2) * SPACING;
const z = (row - (ROWS - 1) / 2) * SPACING;
const isDefective = defective[index];
const isFlagged = flagged[index];
const visibleScale = flaggedOnly && !isFlagged ? 0 : 1;
setInstanceMatrix(
population.bodyMesh,
index,
x,
0.26,
z,
{ x: 0, y: 0, z: 0 },
visibleScale
);
setInstanceMatrix(
population.capMesh,
index,
x,
0.565,
z,
{ x: 0, y: 0, z: 0 },
visibleScale
);
setInstanceMatrix(
population.ringMesh,
index,
x,
0.075,
z,
{ x: Math.PI / 2, y: 0, z: 0 },
isFlagged ? 1 : 0
);
setInstanceMatrix(
population.pickMesh,
index,
x,
0.28,
z,
{ x: 0, y: 0, z: 0 },
1
);
tempColor.setHex(isDefective ? 0xe59a45 : 0x4d8b86);
population.bodyMesh.setColorAt(index, tempColor);
tempColor.setHex(isDefective ? 0xf3bd70 : 0x87b4aa);
population.capMesh.setColorAt(index, tempColor);
let outcome;
if (isDefective && isFlagged) {
outcome = "True positive";
truePositive++;
} else if (isDefective && !isFlagged) {
outcome = "False negative";
falseNegative++;
} else if (!isDefective && isFlagged) {
outcome = "False positive";
falsePositive++;
} else {
outcome = "True negative";
trueNegative++;
}
population.metadata[index] = {
populationId: population.id,
itemNumber: index + 1,
isDefective,
isFlagged,
outcome,
worldX: population.x + x,
worldZ: z
};
}
population.bodyMesh.instanceMatrix.needsUpdate = true;
population.capMesh.instanceMatrix.needsUpdate = true;
population.ringMesh.instanceMatrix.needsUpdate = true;
population.pickMesh.instanceMatrix.needsUpdate = true;
if (population.bodyMesh.instanceColor) {
population.bodyMesh.instanceColor.needsUpdate = true;
}
if (population.capMesh.instanceColor) {
population.capMesh.instanceColor.needsUpdate = true;
}
batchStats.push({
truePositive,
falseNegative,
falsePositive,
trueNegative,
flaggedTotal: truePositive + falsePositive,
defectiveTotal: truePositive + falseNegative
});
});
return batchStats;
}
function modelValues(baseRate) {
const sensitivity = Number(controls.sensitivity.value) / 100;
const falsePositive = Number(controls.falsePositive.value) / 100;
const truePositiveRate = sensitivity * baseRate;
const falsePositiveRate = falsePositive * (1 - baseRate);
const flaggedRate = truePositiveRate + falsePositiveRate;
return {
truePositiveRate,
falsePositiveRate,
flaggedRate,
ppv: flaggedRate > 0 ? truePositiveRate / flaggedRate : null
};
}
function formatNumber(value) {
return new Intl.NumberFormat("en-US", {
maximumFractionDigits: 1
}).format(value);
}
function formatPercent(value) {
if (value === null || !Number.isFinite(value)) return "—";
return `${(value * 100).toFixed(1)}%`;
}
function updateMetricCard(element, title, rate, model, batch) {
const expectedTP = model.truePositiveRate * 1000;
const expectedFP = model.falsePositiveRate * 1000;
const batchPPV = batch.flaggedTotal
? batch.truePositive / batch.flaggedTotal
: null;
element.innerHTML = `
<div class="result-head">
<span>${title}</span>
<span class="rate">${rate}% base rate</span>
</div>
<div class="ppv-line">
<strong>${formatPercent(model.ppv)}</strong>
<span>expected fraction of flags that are defective</span>
</div>
<div class="result-stats">
<div>
<span>Expected flag rate</span>
<b>${formatPercent(model.flaggedRate)}</b>
</div>
<div>
<span>True defect rate</span>
<b>${rate}%</b>
</div>
</div>
<div class="model-line">
Per 1,000 items: ${formatNumber(expectedTP)} true-positive flags +
${formatNumber(expectedFP)} false-positive flags.
</div>
<div class="batch-line">
400-item scene: ${batch.truePositive} of ${batch.flaggedTotal} flags are
defective (${formatPercent(batchPPV)}).
</div>
`;
}
function updateMetrics(batchStats) {
const rateA = Number(controls.rateA.value);
const rateB = Number(controls.rateB.value);
const modelA = modelValues(rateA / 100);
const modelB = modelValues(rateB / 100);
updateMetricCard(
document.getElementById("cardA"),
"Population A",
rateA,
modelA,
batchStats[0]
);
updateMetricCard(
document.getElementById("cardB"),
"Population B",
rateB,
modelB,
batchStats[1]
);
const insight = document.getElementById("insight");
if (modelA.ppv === null || modelB.ppv === null) {
insight.textContent =
"At least one population produces no flags under the current detector settings.";
return;
}
const difference = (modelB.ppv - modelA.ppv) * 100;
const absoluteDifference = Math.abs(difference);
if (absoluteDifference < 0.05) {
insight.textContent =
"The two populations have nearly identical predictive values under this detector.";
} else {
const higher = difference > 0 ? "B" : "A";
const lower = difference > 0 ? "A" : "B";
insight.textContent =
`Population ${higher} has ${absoluteDifference.toFixed(1)} percentage points ` +
`higher PPV than Population ${lower}. The detector has not changed; ` +
`the prevalence has changed the meaning of a positive result.`;
}
}
function updateInterface() {
const rateA = Number(controls.rateA.value);
const rateB = Number(controls.rateB.value);
const sensitivity = Number(controls.sensitivity.value);
const falsePositive = Number(controls.falsePositive.value);
document.getElementById("outRateA").textContent = `${rateA}%`;
document.getElementById("outRateB").textContent = `${rateB}%`;
document.getElementById("outSensitivity").textContent = `${sensitivity}%`;
document.getElementById("outFalsePositive").textContent = `${falsePositive}%`;
document.getElementById("sensBadge").textContent = `${sensitivity}%`;
document.getElementById("fprBadge").textContent = `${falsePositive}%`;
document.querySelector("#labelA .rate").textContent = `${rateA}% defective`;
document.querySelector("#labelB .rate").textContent = `${rateB}% defective`;
document.querySelector("#labelA small").textContent =
rateA < rateB ? "Lower prevalence" :
rateA > rateB ? "Higher prevalence" : "Equal prevalence";
document.querySelector("#labelB small").textContent =
rateB < rateA ? "Lower prevalence" :
rateB > rateA ? "Higher prevalence" : "Equal prevalence";
const batchStats = updateScene();
updateMetrics(batchStats);
}
Object.values(controls).forEach((control) => {
control.addEventListener("input", updateInterface);
});
document.getElementById("restoreExample").addEventListener("click", () => {
controls.rateA.value = 4;
controls.rateB.value = 40;
controls.sensitivity.value = 90;
controls.falsePositive.value = 5;
updateInterface();
});
document.getElementById("focusFlags").addEventListener("click", (event) => {
flaggedOnly = !flaggedOnly;
event.currentTarget.setAttribute("aria-pressed", String(flaggedOnly));
event.currentTarget.textContent = flaggedOnly ? "Show all items" : "Flagged only";
updateInterface();
});
// Camera controls implemented directly, without addons.
const cameraTarget = new THREE.Vector3(0, 0.25, 0);
const spherical = {
theta: 0,
phi: 0.91,
distance: 28
};
const desired = {
theta: spherical.theta,
phi: spherical.phi,
distance: spherical.distance
};
function resetCamera() {
desired.theta = 0;
desired.phi = 0.91;
desired.distance = 28;
}
document.getElementById("resetView").addEventListener("click", resetCamera);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let activePointer = null;
let pointerStartX = 0;
let pointerStartY = 0;
let previousX = 0;
let previousY = 0;
let dragging = false;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
activePointer = event.pointerId;
pointerStartX = event.clientX;
pointerStartY = event.clientY;
previousX = event.clientX;
previousY = event.clientY;
dragging = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (activePointer === event.pointerId) {
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
const totalDistance = Math.hypot(
event.clientX - pointerStartX,
event.clientY - pointerStartY
);
if (totalDistance > 3) dragging = true;
if (dragging) {
desired.theta -= deltaX * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi + deltaY * 0.005,
0.42,
1.28
);
}
previousX = event.clientX;
previousY = event.clientY;
return;
}
updateHover(event);
});
function endPointer(event) {
if (activePointer !== event.pointerId) return;
activePointer = null;
dragging = false;
renderer.domElement.classList.remove("dragging");
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("pointerleave", () => {
if (activePointer === null) {
tooltip.style.display = "none";
hoverHalo.visible = false;
}
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
desired.distance = THREE.MathUtils.clamp(
desired.distance * Math.exp(event.deltaY * 0.001),
17,
42
);
}, { passive: false });
function updateHover(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickMeshes, false);
if (!hits.length) {
tooltip.style.display = "none";
hoverHalo.visible = false;
return;
}
const hit = hits[0];
const population = populations.find(
(item) => item.id === hit.object.userData.populationId
);
const metadata = population.metadata[hit.instanceId];
hoverHalo.position.set(metadata.worldX, 0.092, metadata.worldZ);
hoverHalo.visible = true;
const truthText = metadata.isDefective ? "Actually defective" : "Not defective";
const flagText = metadata.isFlagged ? "Flagged" : "Not flagged";
tooltip.innerHTML = `
<strong>Population ${metadata.populationId.toUpperCase()} · Item ${metadata.itemNumber}</strong>
<span>${truthText}</span>
<span>${flagText} · ${metadata.outcome}</span>
`;
tooltip.style.display = "block";
const localX = event.clientX - rect.left;
const localY = event.clientY - rect.top;
tooltip.style.left = `${Math.min(localX + 14, rect.width - 205)}px`;
tooltip.style.top = `${Math.max(8, localY - 78)}px`;
}
function resizeRenderer() {
const width = sceneElement.clientWidth;
const height = sceneElement.clientHeight;
if (!width || !height) return;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
new ResizeObserver(resizeRenderer).observe(sceneElement);
resizeRenderer();
const labelA = document.getElementById("labelA");
const labelB = document.getElementById("labelB");
const projected = new THREE.Vector3();
function positionPopulationLabel(element, population) {
projected.set(population.x, 1.25, -3.7).project(camera);
const width = sceneElement.clientWidth;
const height = sceneElement.clientHeight;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity =
projected.z > -1 && projected.z < 1 ? "1" : "0";
}
function animate() {
requestAnimationFrame(animate);
spherical.theta += (desired.theta - spherical.theta) * 0.1;
spherical.phi += (desired.phi - spherical.phi) * 0.1;
spherical.distance += (desired.distance - spherical.distance) * 0.1;
const sinPhi = Math.sin(spherical.phi);
camera.position.set(
cameraTarget.x + spherical.distance * sinPhi * Math.sin(spherical.theta),
cameraTarget.y + spherical.distance * Math.cos(spherical.phi),
cameraTarget.z + spherical.distance * sinPhi * Math.cos(spherical.theta)
);
camera.lookAt(cameraTarget);
positionPopulationLabel(labelA, populations[0]);
positionPopulationLabel(labelB, populations[1]);
renderer.render(scene, camera);
}
updateInterface();
animate();
</script>
</body>
</html>
```