Prompt specification
Build or customize the Three.js 3D scene "Prior-and-likelihood exploration table" 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: Prior-and-likelihood exploration table
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/prior-and-likelihood-exploration-table-x1768
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: prior-and-likelihood exploration table. Allow changing prior source proportions and evidence likelihoods; compute normalized posterior probabilities and visibly handle impossible observations rather than dividing by zero. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (prior-and-likelihood-exploration-table-x1768):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Prior × Likelihood — Three.js Exploration Table</title>
<style>
:root {
color-scheme: light;
--paper: #f2eee5;
--panel: #fbfaf6;
--ink: #172b37;
--muted: #68757c;
--line: #ddd8cd;
--navy: #19313f;
--blue: #2f80ed;
--amber: #f0a33b;
--violet: #8b62d6;
--danger: #df4b5c;
--good: #23856d;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app {
display: grid;
grid-template-columns: minmax(0, 1fr) 380px;
width: 100%;
height: 100dvh;
min-height: 620px;
}
#scene {
position: relative;
min-width: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 38%, rgba(255, 255, 255, 0.68), transparent 42%),
#e8ece7;
}
#scene canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
#scene::after {
content: "";
position: absolute;
inset: 0;
z-index: 2;
pointer-events: none;
box-shadow: inset 0 0 0 0 rgba(223, 75, 92, 0);
transition: box-shadow 180ms ease;
}
#scene.impossible::after {
box-shadow:
inset 0 0 0 3px rgba(223, 75, 92, 0.72),
inset 0 0 90px rgba(223, 75, 92, 0.13);
}
.hero {
position: absolute;
z-index: 4;
top: 26px;
left: 28px;
max-width: 440px;
pointer-events: none;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
color: #52656e;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
background: var(--amber);
box-shadow: 0 0 0 4px rgba(240, 163, 59, 0.15);
transform: rotate(45deg);
}
h1 {
margin: 0;
max-width: 420px;
font-size: clamp(28px, 3.1vw, 48px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.hero p {
max-width: 390px;
margin: 12px 0 0;
color: #5d6c73;
font-size: 13px;
line-height: 1.55;
}
.scene-hint {
position: absolute;
z-index: 4;
right: 22px;
bottom: 20px;
padding: 8px 11px;
border: 1px solid rgba(23, 43, 55, 0.12);
border-radius: 9px;
background: rgba(250, 249, 244, 0.78);
color: #637178;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.025em;
backdrop-filter: blur(8px);
pointer-events: none;
}
.scene-alert {
position: absolute;
z-index: 5;
left: 24px;
bottom: 64px;
display: none;
width: min(410px, calc(100% - 48px));
padding: 14px 16px;
border: 1px solid rgba(223, 75, 92, 0.36);
border-radius: 12px;
background: rgba(255, 244, 245, 0.94);
box-shadow: 0 12px 35px rgba(90, 30, 38, 0.12);
backdrop-filter: blur(12px);
}
.scene-alert.visible {
display: block;
}
.scene-alert strong {
display: block;
margin-bottom: 4px;
color: #b93649;
font-size: 13px;
}
.scene-alert span {
display: block;
color: #76545a;
font-size: 11px;
line-height: 1.45;
}
#loading {
position: absolute;
z-index: 8;
inset: 0;
display: grid;
place-items: center;
background: #e8ece7;
color: #53666f;
font-size: 13px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel {
position: relative;
z-index: 6;
overflow-y: auto;
padding: 26px 24px 30px;
border-left: 1px solid #d8d4ca;
background: var(--panel);
box-shadow: -18px 0 50px rgba(35, 49, 57, 0.05);
}
.panel-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
margin-bottom: 25px;
}
.panel-kicker {
margin: 0 0 5px;
color: #7a858a;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.panel h2 {
margin: 0;
font-size: 22px;
letter-spacing: -0.035em;
}
.source-count {
flex: 0 0 auto;
padding: 6px 9px;
border: 1px solid #dcd7cc;
border-radius: 999px;
color: #6b777d;
background: #f4f1e9;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.section {
margin-top: 25px;
}
.section-title {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.section-title h3 {
margin: 0;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.section-title span {
color: #899196;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.control-block {
--source: var(--blue);
margin-bottom: 15px;
padding: 12px 13px 10px;
border: 1px solid #e1ddd3;
border-radius: 12px;
background: #fffefa;
}
.control-label {
display: grid;
grid-template-columns: 10px minmax(0, 1fr) auto;
align-items: center;
gap: 8px;
}
.source-dot {
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--source);
transform: rotate(45deg);
}
.control-label label {
overflow: hidden;
font-size: 12px;
font-weight: 750;
text-overflow: ellipsis;
white-space: nowrap;
}
.number-box {
display: flex;
align-items: center;
gap: 2px;
color: #7a858b;
font-size: 10px;
}
.number-box input {
width: 49px;
padding: 4px 2px;
border: 0;
border-bottom: 1px solid #d8d4ca;
outline: none;
background: transparent;
color: var(--ink);
font-size: 12px;
font-weight: 800;
text-align: right;
}
.number-box input:focus {
border-bottom-color: var(--source);
}
input[type="range"] {
width: 100%;
height: 18px;
margin: 8px 0 2px;
accent-color: var(--source);
cursor: pointer;
}
.control-meta {
display: flex;
justify-content: space-between;
gap: 10px;
color: #8a9397;
font-size: 9px;
line-height: 1.35;
}
.control-meta output {
color: #68767d;
font-weight: 750;
text-align: right;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 17px;
}
button {
min-height: 37px;
padding: 8px 10px;
border: 1px solid #d6d2c7;
border-radius: 9px;
background: #f3f0e8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.025em;
cursor: pointer;
transition:
transform 120ms ease,
border-color 120ms ease,
background 120ms ease;
}
button:hover {
border-color: #aeb6b7;
background: #ece9e0;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.danger {
border-color: rgba(223, 75, 92, 0.28);
background: #fff0f1;
color: #b53a4b;
}
.result-card {
margin-top: 27px;
padding: 17px;
border-radius: 15px;
background: var(--navy);
color: #f8f6ef;
box-shadow: 0 17px 35px rgba(25, 49, 63, 0.14);
}
.status-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.status-pill {
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
color: #a7e4d4;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.status-pill.impossible {
border-color: rgba(255, 130, 145, 0.28);
background: rgba(223, 75, 92, 0.17);
color: #ffb0ba;
}
.evidence-mass {
color: #aebdc4;
font-size: 10px;
font-weight: 700;
}
.result-card h3 {
margin: 0 0 6px;
font-size: 16px;
letter-spacing: -0.02em;
}
.status-copy {
min-height: 38px;
margin: 0 0 15px;
color: #aebcc2;
font-size: 10px;
line-height: 1.5;
}
.equation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 12px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 10px;
background: rgba(255, 255, 255, 0.045);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
.fraction {
display: grid;
grid-template-columns: 1fr;
text-align: center;
}
.fraction .top {
padding-bottom: 3px;
border-bottom: 1px solid #82939b;
}
.fraction .bottom {
padding-top: 3px;
color: #f0a33b;
}
.result-list {
margin-top: 14px;
}
.result-row {
display: grid;
grid-template-columns: 82px minmax(0, 1fr) 53px;
align-items: center;
gap: 9px;
margin-top: 9px;
}
.result-name {
color: #d8e0e3;
font-size: 10px;
font-weight: 750;
}
.result-track {
overflow: hidden;
height: 7px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.09);
}
.result-track i {
display: block;
height: 100%;
border-radius: inherit;
transition: width 160ms ease;
}
.result-value {
color: #f7f4eb;
font-size: 10px;
font-weight: 850;
text-align: right;
}
.result-total {
display: flex;
justify-content: space-between;
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.09);
color: #8fa2aa;
font-size: 9px;
}
.note {
margin: 13px 2px 0;
color: #879196;
font-size: 9px;
line-height: 1.55;
}
.note code {
color: #5f6d74;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
#app {
grid-template-columns: 1fr;
grid-template-rows: 58dvh auto;
height: auto;
min-height: 100dvh;
}
.panel {
overflow: visible;
border-top: 1px solid #d8d4ca;
border-left: 0;
}
.hero {
top: 18px;
left: 18px;
}
.hero p {
display: none;
}
.scene-hint {
right: 12px;
bottom: 12px;
}
.scene-alert {
left: 14px;
bottom: 52px;
width: calc(100% - 28px);
}
}
</style>
</head>
<body>
<main id="app">
<section id="scene" aria-label="Interactive low-poly posterior exploration table">
<div id="loading">Building exploration table…</div>
<header class="hero">
<div class="eyebrow">Bayesian table · three candidates</div>
<h1>Prior × Likelihood</h1>
<p>
Adjust source proportions and the likelihood of the observed evidence.
The table normalizes the posterior without ever dividing by a zero denominator.
</p>
</header>
<div id="sceneAlert" class="scene-alert" role="status">
<strong id="sceneAlertTitle">Impossible observation</strong>
<span id="sceneAlertCopy"></span>
</div>
<div class="scene-hint">
Drag to orbit · Shift-drag to pan · Scroll to zoom · Double-click to reset
</div>
</section>
<aside class="panel" aria-label="Bayesian model controls">
<div class="panel-header">
<div>
<p class="panel-kicker">Exploration controls</p>
<h2>Model inputs</h2>
</div>
<div class="source-count">3 sources</div>
</div>
<section class="section">
<div class="section-title">
<h3>01 · Prior source weights</h3>
<span>Auto-normalized</span>
</div>
<div id="priorControls"></div>
</section>
<section class="section">
<div class="section-title">
<h3>02 · Evidence likelihoods</h3>
<span>P(E | source)</span>
</div>
<div id="likelihoodControls"></div>
</section>
<div class="actions">
<button id="resetModel" type="button">Reset model</button>
<button id="balancePriors" type="button">Balance priors</button>
<button id="zeroEvidence" class="danger" type="button">
Make evidence impossible
</button>
<button id="clearEvidence" type="button">Clear likelihoods</button>
</div>
<section class="result-card" aria-live="polite">
<div class="status-line">
<span id="statusPill" class="status-pill">Normalized</span>
<span class="evidence-mass">P(E) = <b id="evidenceMass">—</b></span>
</div>
<h3 id="statusTitle">Posterior ready</h3>
<p id="statusCopy" class="status-copy"></p>
<div class="equation" aria-label="Bayes formula">
<span>P(Sᵢ | E)</span>
<span>=</span>
<span class="fraction">
<span class="top">P(E | Sᵢ) · P(Sᵢ)</span>
<span id="formulaDenominator" class="bottom">—</span>
</span>
</div>
<div id="posteriorList" class="result-list"></div>
<div class="result-total">
<span>Σ posterior</span>
<strong id="posteriorTotal">—</strong>
</div>
</section>
<p class="note">
Posterior:
<code>P(Sᵢ | E) = P(E | Sᵢ)P(Sᵢ) / ΣⱼP(E | Sⱼ)P(Sⱼ)</code>.
If every likelihood is zero, the evidence has probability zero and the posterior
is reported as undefined rather than normalized.
</p>
</aside>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const sceneEl = $("scene");
const EPSILON = 1e-12;
const sources = [
{ code: "A", name: "Source A", css: "#2f80ed", hex: 0x2f80ed },
{ code: "B", name: "Source B", css: "#f0a33b", hex: 0xf0a33b },
{ code: "C", name: "Source C", css: "#8b62d6", hex: 0x8b62d6 }
];
const defaults = {
prior: [0.55, 0.30, 0.15],
likelihood: [0.80, 0.20, 0.05]
};
const state = {
prior: [...defaults.prior],
likelihood: [...defaults.likelihood]
};
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
} catch (error) {
$("loading").textContent = "WebGL could not be initialized.";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional Bayesian table. Drag to orbit and use the mouse wheel to zoom."
);
sceneEl.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8ece7);
scene.fog = new THREE.Fog(0xe8ece7, 24, 48);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.15, 0);
let cameraTheta = 0.46;
let cameraPhi = 0.82;
let cameraRadius = 19.5;
let userZoomed = false;
const hemisphere = new THREE.HemisphereLight(0xfff5df, 0x657581, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(-7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 14;
keyLight.shadow.camera.bottom = -14;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb9d7ff, 1.35);
fillLight.position.set(8, 7, -9);
scene.add(fillLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xe2e5dd,
roughness: 1,
flatShading: true
});
const ground = new THREE.Mesh(new THREE.CircleGeometry(17, 16), groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.035;
ground.receiveShadow = true;
scene.add(ground);
function addBox(width, height, depth, x, y, z, material, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x203744,
roughness: 0.82,
flatShading: true
});
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0xd7a35c,
roughness: 0.88,
flatShading: true
});
const surfaceMaterial = new THREE.MeshStandardMaterial({
color: 0xf1dfb8,
roughness: 0.94,
flatShading: true
});
const rowMaterial = new THREE.MeshStandardMaterial({
color: 0xfaf7ef,
roughness: 0.96,
flatShading: true
});
const headerMaterial = new THREE.MeshStandardMaterial({
color: 0x203744,
roughness: 0.9,
flatShading: true
});
const separatorMaterial = new THREE.MeshStandardMaterial({
color: 0xd4cbb9,
roughness: 1
});
addBox(13.85, 0.48, 7.65, 0, 1.25, 0, woodMaterial);
addBox(13.62, 0.07, 7.42, 0, 1.515, 0, surfaceMaterial);
const legPositions = [
[-5.85, -2.9],
[5.85, -2.9],
[-5.85, 2.9],
[5.85, 2.9]
];
for (const [x, z] of legPositions) {
const leg = new THREE.Mesh(
new THREE.CylinderGeometry(0.33, 0.43, 1.15, 6),
frameMaterial
);
leg.position.set(x, 0.58, z);
leg.castShadow = true;
leg.receiveShadow = true;
scene.add(leg);
const foot = new THREE.Mesh(
new THREE.CylinderGeometry(0.46, 0.5, 0.16, 6),
frameMaterial
);
foot.position.set(x, 0.08, z);
foot.castShadow = true;
scene.add(foot);
}
addBox(13.2, 0.08, 0.88, 0, 1.575, -2.92, headerMaterial);
const rowZ = [-1.65, 0, 1.65];
const columnEdges = [-3.55, -0.75, 2.4, 4.3];
for (const z of rowZ) {
addBox(13.15, 0.035, 1.46, 0, 1.555, z, rowMaterial);
}
for (const x of columnEdges) {
addBox(0.025, 0.025, 6.55, x, 1.585, -0.25, separatorMaterial);
}
const accentMaterials = sources.map(
(source) =>
new THREE.MeshStandardMaterial({
color: source.hex,
roughness: 0.75,
flatShading: true
})
);
for (let i = 0; i < rowZ.length; i++) {
addBox(0.12, 0.065, 1.25, -6.47, 1.605, rowZ[i], accentMaterials[i]);
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
const fontStack =
'ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif';
function canvasRecord(width, height) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return { canvas, ctx, texture };
}
function createCell(worldWidth, worldHeight, x, z) {
const record = canvasRecord(Math.round(worldWidth * 120), 220);
const material = new THREE.MeshBasicMaterial({
map: record.texture,
transparent: true,
depthWrite: false,
toneMapped: false
});
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(worldWidth, worldHeight),
material
);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(x, 1.615, z);
mesh.renderOrder = 4;
scene.add(mesh);
record.mesh = mesh;
return record;
}
function createHeader(worldWidth, label, formula, x) {
const record = canvasRecord(Math.round(worldWidth * 120), 140);
const material = new THREE.MeshBasicMaterial({
map: record.texture,
transparent: true,
depthWrite: false,
toneMapped: false
});
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(worldWidth, 0.56),
material
);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(x, 1.63, -2.92);
mesh.renderOrder = 4;
scene.add(mesh);
const { ctx, canvas, texture } = record;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#f7f5ed";
ctx.font = `800 ${canvas.width < 250 ? 22 : 26}px ${fontStack}`;
ctx.fillText(label, canvas.width / 2, 45, canvas.width - 18);
ctx.fillStyle = "#b8c8ce";
ctx.font = `700 ${canvas.width < 250 ? 19 : 22}px ${fontStack}`;
ctx.fillText(formula, canvas.width / 2, 94, canvas.width - 18);
texture.needsUpdate = true;
return record;
}
function setCell(record, config) {
const { ctx, canvas, texture } = record;
const width = canvas.width;
const height = canvas.height;
ctx.clearRect(0, 0, width, height);
ctx.textAlign = "left";
ctx.textBaseline = "alphabetic";
if (config.accent) {
ctx.fillStyle = config.accent;
roundedRect(ctx, 0, 6, 7, height - 12, 3);
ctx.fill();
}
const left = 16;
const titleSize = width < 250 ? 16 : 20;
ctx.fillStyle = "#65747b";
ctx.font = `800 ${titleSize}px ${fontStack}`;
ctx.fillText(config.title.toUpperCase(), left, 32, width - 30);
let valueSize = width < 250 ? 42 : 58;
if (String(config.value).length > 8) valueSize = width < 250 ? 30 : 38;
ctx.fillStyle = config.valueColor || "#172b37";
ctx.font = `850 ${valueSize}px ${fontStack}`;
ctx.fillText(String(config.value), left, 108, width - 30);
ctx.fillStyle = "#859096";
ctx.font = `650 ${width < 250 ? 14 : 16}px ${fontStack}`;
ctx.fillText(config.sub || "", left, 151, width - 30);
if (config.showBar !== false) {
const barX = 16;
const barY = 181;
const barWidth = width - 32;
const barHeight = 14;
ctx.fillStyle = "#e4dfd4";
roundedRect(ctx, barX, barY, barWidth, barHeight, 7);
ctx.fill();
const ratio = Math.max(0, Math.min(1, config.bar || 0));
if (ratio > 0) {
ctx.fillStyle = config.accent || "#f0a33b";
roundedRect(ctx, barX, barY, Math.max(5, barWidth * ratio), barHeight, 7);
ctx.fill();
}
}
texture.needsUpdate = true;
}
createHeader(3.0, "SOURCE", "candidate", -5.05);
createHeader(2.45, "PRIOR", "P(S)", -2.05);
createHeader(2.55, "LIKELIHOOD", "P(E | S)", 0.65);
createHeader(2.05, "JOINT", "P(S, E)", 3.15);
createHeader(1.95, "POSTERIOR", "P(S | E)", 5.35);
const sourceCells = [];
const priorCells = [];
const likelihoodCells = [];
const jointCells = [];
const posteriorCells = [];
const posteriorBars = [];
for (let i = 0; i < sources.length; i++) {
sourceCells.push(createCell(2.95, 0.78, -5.05, rowZ[i]));
priorCells.push(createCell(2.42, 0.78, -2.05, rowZ[i]));
likelihoodCells.push(createCell(2.52, 0.78, 0.65, rowZ[i]));
jointCells.push(createCell(2.02, 0.78, 3.15, rowZ[i]));
posteriorCells.push(createCell(1.91, 0.60, 5.35, rowZ[i] - 0.20));
const track = addBox(
1.72,
0.045,
0.15,
5.35,
1.63,
rowZ[i] + 0.39,
separatorMaterial
);
track.castShadow = false;
const fillMaterial = new THREE.MeshStandardMaterial({
color: sources[i].hex,
roughness: 0.65,
flatShading: true
});
const fill = addBox(1, 1, 1, 5.35, 1.655, rowZ[i] + 0.39, fillMaterial);
fill.castShadow = false;
fill.visible = false;
posteriorBars.push(fill);
}
function createSprite(text, color, background, width = 2.75, height = 0.56) {
const record = canvasRecord(640, 120);
const { ctx, canvas, texture } = record;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = background;
roundedRect(ctx, 8, 8, canvas.width - 16, canvas.height - 16, 28);
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 3;
roundedRect(ctx, 8, 8, canvas.width - 16, canvas.height - 16, 28);
ctx.stroke();
ctx.fillStyle = color;
ctx.font = `850 31px ${fontStack}`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2, canvas.width - 45);
texture.needsUpdate = true;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 10;
return sprite;
}
const evidenceGroup = new THREE.Group();
evidenceGroup.position.set(0, 3.35, -1.2);
scene.add(evidenceGroup);
const evidenceMaterial = new THREE.MeshStandardMaterial({
color: 0xf0a33b,
emissive: 0xf0a33b,
emissiveIntensity: 0.23,
roughness: 0.48,
flatShading: true
});
const evidenceCrystal = new THREE.Mesh(
new THREE.OctahedronGeometry(0.72, 0),
evidenceMaterial
);
evidenceCrystal.castShadow = true;
evidenceGroup.add(evidenceCrystal);
const wireMaterial = new THREE.MeshBasicMaterial({
color: 0xf0a33b,
wireframe: true,
transparent: true,
opacity: 0.42
});
const wireShell = new THREE.Mesh(
new THREE.DodecahedronGeometry(1.02, 0),
wireMaterial
);
evidenceGroup.add(wireShell);
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0xf0a33b,
roughness: 0.55,
flatShading: true
});
const evidenceRing = new THREE.Mesh(
new THREE.TorusGeometry(1.12, 0.055, 6, 28),
ringMaterial
);
evidenceRing.rotation.x = Math.PI / 2;
evidenceGroup.add(evidenceRing);
const beaconStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 1.6, 6),
separatorMaterial
);
beaconStem.position.y = -1.18;
evidenceGroup.add(beaconStem);
const beaconLight = new THREE.PointLight(0xf0a33b, 2.0, 7, 2);
evidenceGroup.add(beaconLight);
const normalBeaconLabel = createSprite(
"OBSERVED EVIDENCE E",
"#8a5a17",
"rgba(255,246,225,0.96)",
2.8,
0.55
);
normalBeaconLabel.position.set(0, 1.25, 0);
evidenceGroup.add(normalBeaconLabel);
const warningBeaconLabel = createSprite(
"IMPOSSIBLE · P(E) = 0",
"#a92e40",
"rgba(255,235,238,0.98)",
3.2,
0.62
);
warningBeaconLabel.position.set(0, 1.25, 0);
warningBeaconLabel.visible = false;
evidenceGroup.add(warningBeaconLabel);
function controlMarkup(kind, index, source) {
const id = `${kind}-${index}`;
const value = Math.round(state[kind][index] * 1000) / 10;
const meta =
kind === "prior"
? `<output id="${id}-meta">normalized ${value.toFixed(1)}%</output>`
: `<output id="${id}-meta">P(E | ${source.code}) = ${value.toFixed(1)}%</output>`;
return `
<div class="control-block" style="--source:${source.css}">
<div class="control-label">
<span class="source-dot" aria-hidden="true"></span>
<label for="${id}-range">${source.name}</label>
<span class="number-box">
<input
id="${id}-number"
type="number"
min="0"
max="100"
step="0.1"
value="${value.toFixed(1)}"
aria-label="${source.name} ${kind === "prior" ? "prior weight" : "evidence likelihood"} percentage"
>
<span>%</span>
</span>
</div>
<input
id="${id}-range"
type="range"
min="0"
max="100"
step="1"
value="${value}"
aria-label="${source.name} ${kind === "prior" ? "prior weight" : "evidence likelihood"}"
>
<div class="control-meta">
<span>${kind === "prior" ? "raw source weight" : "evidence fit"}</span>
${meta}
</div>
</div>
`;
}
$("priorControls").innerHTML = sources
.map((source, index) => controlMarkup("prior", index, source))
.join("");
$("likelihoodControls").innerHTML = sources
.map((source, index) => controlMarkup("likelihood", index, source))
.join("");
function syncInputs() {
for (const kind of ["prior", "likelihood"]) {
for (let i = 0; i < sources.length; i++) {
const percentage = Math.round(state[kind][i] * 1000) / 10;
$(`${kind}-${i}-range`).value = percentage;
$(`${kind}-${i}-number`).value = percentage.toFixed(1);
}
}
}
function bindInputs(kind) {
for (let i = 0; i < sources.length; i++) {
const range = $(`${kind}-${i}-range`);
const number = $(`${kind}-${i}-number`);
range.addEventListener("input", () => {
const percentage = Number(range.value);
state[kind][i] = percentage / 100;
number.value = percentage.toFixed(1);
recompute();
});
number.addEventListener("input", () => {
if (number.value.trim() === "") return;
const percentage = Number(number.value);
if (!Number.isFinite(percentage)) return;
const clamped = Math.max(0, Math.min(100, percentage));
state[kind][i] = clamped / 100;
range.value = clamped;
recompute();
});
number.addEventListener("change", () => {
const percentage = Math.max(
0,
Math.min(100, Number(number.value) || 0)
);
state[kind][i] = percentage / 100;
number.value = percentage.toFixed(1);
range.value = percentage;
recompute();
});
}
}
bindInputs("prior");
bindInputs("likelihood");
function formatPercent(value, digits = 1) {
return `${(value * 100).toFixed(digits)}%`;
}
function updatePosteriorList(posterior, valid) {
$("posteriorList").innerHTML = sources
.map((source, index) => {
const value = valid ? posterior[index] : 0;
const width = valid ? value * 100 : 0;
const text = valid ? formatPercent(value) : "UNDEF.";
return `
<div class="result-row" aria-label="${source.name} posterior ${text}">
<div class="result-name">${source.name}</div>
<div class="result-track">
<i style="width:${width}%;background:${source.css}"></i>
</div>
<div class="result-value" style="${valid ? "" : "color:#ff9eaa"}">${text}</div>
</div>
`;
})
.join("");
}
let observationValid;
function recompute() {
const priorSum = state.prior.reduce((sum, value) => sum + value, 0);
const priorValid = priorSum > EPSILON;
const normalizedPriors = priorValid
? state.prior.map((value) => value / priorSum)
: state.prior.map(() => 0);
const joint = normalizedPriors.map(
(prior, index) => prior * state.likelihood[index]
);
const denominator = joint.reduce((sum, value) => sum + value, 0);
observationValid = denominator > EPSILON;
const posteriorValid = priorValid && observationValid;
// Division is performed only after the denominator has been checked.
const posterior = posteriorValid
? joint.map((value) => value / denominator)
: joint.map(() => 0);
for (let i = 0; i < sources.length; i++) {
const source = sources[i];
setCell(sourceCells[i], {
title: "Candidate source",
value: source.name.toUpperCase(),
sub: "hypothesis",
accent: source.css,
showBar: false
});
setCell(priorCells[i], {
title: "Prior P(S)",
value: priorValid ? formatPercent(normalizedPriors[i]) : "N/A",
sub: priorValid ? "normalized source mass" : "no prior mass",
accent: source.css,
bar: normalizedPriors[i],
valueColor: priorValid ? "#172b37" : "#b93649"
});
setCell(likelihoodCells[i], {
title: "Likelihood P(E|S)",
value: formatPercent(state.likelihood[i]),
sub: `evidence given ${source.code}`,
accent: source.css,
bar: state.likelihood[i]
});
setCell(jointCells[i], {
title: "Joint P(S,E)",
value: joint[i].toFixed(4),
sub: "prior × likelihood",
accent: source.css,
bar: joint[i]
});
setCell(posteriorCells[i], {
title: "Posterior P(S|E)",
value: posteriorValid ? formatPercent(posterior[i]) : "UNDEF.",
sub: posteriorValid ? "normalized answer" : "normalization blocked",
accent: source.css,
bar: posteriorValid ? posterior[i] : 0,
valueColor: posteriorValid ? "#172b37" : "#b93649"
});
const barWidth = 1.72 * posterior[i];
const bar = posteriorBars[i];
bar.visible = posteriorValid && posterior[i] > EPSILON;
bar.scale.set(Math.max(0.001, barWidth), 0.07, 0.115);
bar.position.x = 4.49 + barWidth / 2;
$(`prior-${i}-meta`).textContent = priorValid
? `normalized ${formatPercent(normalizedPriors[i])}`
: "cannot normalize: total weight = 0";
$(`likelihood-${i}-meta`).textContent =
`P(E | ${source.name}) = ${formatPercent(state.likelihood[i])}`;
}
if (!priorValid) {
$("statusPill").textContent = "Undefined";
$("statusPill").className = "status-pill impossible";
$("statusTitle").textContent = "Prior has no mass";
$("statusCopy").textContent =
"All prior weights are zero, so the prior cannot be normalized. No posterior division was performed.";
$("sceneAlertTitle").textContent = "Prior has no mass";
$("sceneAlertCopy").textContent =
"The total prior weight is zero. The prior and posterior remain undefined.";
} else if (!observationValid) {
$("statusPill").textContent = "Impossible";
$("statusPill").className = "status-pill impossible";
$("statusTitle").textContent = "Impossible observation";
$("statusCopy").textContent =
"Every P(E | source) is zero, so P(E) = 0 and Bayes’ denominator is zero. The posterior is undefined, not divided by zero.";
$("sceneAlertTitle").textContent = "Impossible observation";
$("sceneAlertCopy").textContent =
"All evidence likelihoods are zero. The red beacon marks P(E) = 0; posterior normalization is safely blocked.";
} else {
$("statusPill").textContent = "Normalized";
$("statusPill").className = "status-pill";
$("statusTitle").textContent = "Posterior ready";
$("statusCopy").textContent =
"Joint probabilities have been divided by their positive total. The three posterior values sum to one.";
$("sceneAlertTitle").textContent = "";
$("sceneAlertCopy").textContent = "";
}
$("evidenceMass").textContent = denominator.toFixed(4);
$("formulaDenominator").textContent = denominator.toFixed(4);
$("formulaDenominator").style.color = observationValid ? "#f0a33b" : "#ff9eaa";
$("posteriorTotal").textContent = posteriorValid
? formatPercent(posterior.reduce((sum, value) => sum + value, 0))
: "—";
updatePosteriorList(posterior, posteriorValid);
sceneEl.classList.toggle("impossible", !posteriorValid);
$("sceneAlert").classList.toggle("visible", !posteriorValid);
const beaconColor = observationValid ? 0xf0a33b : 0xe34b5c;
evidenceMaterial.color.setHex(beaconColor);
evidenceMaterial.emissive.setHex(beaconColor);
evidenceMaterial.emissiveIntensity = observationValid ? 0.23 : 0.56;
wireMaterial.color.setHex(beaconColor);
ringMaterial.color.setHex(beaconColor);
beaconLight.color.setHex(beaconColor);
beaconLight.intensity = observationValid ? 2.0 : 3.2;
normalBeaconLabel.visible = observationValid;
warningBeaconLabel.visible = !observationValid;
}
$("resetModel").addEventListener("click", () => {
state.prior = [...defaults.prior];
state.likelihood = [...defaults.likelihood];
syncInputs();
recompute();
});
$("balancePriors").addEventListener("click", () => {
state.prior = [1 / 3, 1 / 3, 1 / 3];
syncInputs();
recompute();
});
$("zeroEvidence").addEventListener("click", () => {
state.likelihood = [0, 0, 0];
syncInputs();
recompute();
});
$("clearEvidence").addEventListener("click", () => {
state.likelihood = [0, 0, 0];
syncInputs();
recompute();
});
function updateCamera() {
const offset = new THREE.Vector3();
offset.setFromSphericalCoords(cameraRadius, cameraPhi, cameraTheta);
camera.position.copy(cameraTarget).add(offset);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function fittedRadius() {
const aspect = sceneEl.clientWidth / Math.max(sceneEl.clientHeight, 1);
if (aspect < 0.9) return 28;
if (aspect < 1.2) return 24;
return 19.5;
}
function resize() {
const width = Math.max(1, sceneEl.clientWidth);
const height = Math.max(1, sceneEl.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
if (!userZoomed) cameraRadius = fittedRadius();
updateCamera();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(sceneEl);
const pointer = {
active: false,
id: null,
x: 0,
y: 0,
shift: false
};
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointer.active = true;
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
pointer.shift = event.shiftKey;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (event.shiftKey || pointer.shift) {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0)
.normalize();
const up = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 1)
.normalize();
const panScale = cameraRadius * 0.00135;
cameraTarget.addScaledVector(right, -dx * panScale);
cameraTarget.addScaledVector(up, dy * panScale);
} else {
cameraTheta -= dx * 0.005;
cameraPhi = Math.max(0.34, Math.min(1.28, cameraPhi - dy * 0.005));
}
updateCamera();
});
function endPointer(event) {
if (!pointer.active || event.pointerId !== pointer.id) return;
pointer.active = false;
pointer.id = null;
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = Math.max(
9,
Math.min(34, cameraRadius * Math.exp(event.deltaY * 0.001))
);
userZoomed = true;
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraTheta = 0.46;
cameraPhi = 0.82;
cameraRadius = fittedRadius();
cameraTarget.set(0, 1.15, 0);
userZoomed = false;
updateCamera();
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
syncInputs();
resize();
recompute();
const clock = new THREE.Clock();
let firstFrame = true;
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
evidenceCrystal.rotation.y += 0.004;
evidenceCrystal.rotation.x = Math.sin(elapsed * 0.55) * 0.09;
wireShell.rotation.y -= 0.0025;
wireShell.rotation.z = elapsed * 0.12;
evidenceRing.rotation.z = elapsed * 0.22;
evidenceGroup.position.y = 3.35 + Math.sin(elapsed * 1.35) * 0.075;
if (!observationValid) {
const pulse = 1 + Math.sin(elapsed * 4.2) * 0.045;
evidenceCrystal.scale.setScalar(pulse);
} else {
evidenceCrystal.scale.setScalar(1);
}
updateCamera();
renderer.render(scene, camera);
if (firstFrame) {
$("loading")?.remove();
firstFrame = false;
}
});
</script>
</body>
</html>
```