Prompt specification
Build or customize the Three.js 3D scene "Two-urn evidence model" 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: Two-urn evidence model
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-urn-evidence-model-x1765
Scene Description Prompt:
Please make a low-poly Three.js scene in one standalone HTML file using cdnjs. Build two urns with different colored-token compositions and a prior selection indicator; distinguish probability of an observation given an urn from probability of the urn given an observation.
Working Three.js Starter (two-urn-evidence-model-x1765):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Two Urns — Likelihood vs. Posterior</title>
<style>
:root {
color-scheme: dark;
--bg: #08111f;
--panel: rgba(12, 23, 39, 0.92);
--panel-border: rgba(158, 196, 226, 0.16);
--text: #edf7ff;
--muted: #91a8bd;
--blue: #43b9ff;
--gold: #ffc857;
--cyan: #61ddff;
--orange: #ffb552;
--green: #74e0b5;
}
* {
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);
}
body {
background:
radial-gradient(circle at 22% 14%, rgba(39, 114, 160, 0.16), transparent 34%),
radial-gradient(circle at 78% 75%, rgba(132, 80, 24, 0.12), transparent 34%),
var(--bg);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
header {
position: fixed;
top: 22px;
left: 24px;
z-index: 4;
max-width: 520px;
pointer-events: none;
text-shadow: 0 2px 16px rgba(0, 0, 0, 0.55);
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 14px currentColor;
content: "";
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 42px);
line-height: 1.05;
letter-spacing: -0.035em;
}
header p {
max-width: 460px;
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.panel {
position: fixed;
top: 16px;
right: 16px;
bottom: 16px;
z-index: 5;
width: 390px;
overflow: auto;
padding: 19px;
border: 1px solid var(--panel-border);
border-radius: 22px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #31465b transparent;
}
.panel-intro {
margin: 0 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
section {
padding: 15px 0;
border-top: 1px solid rgba(158, 196, 226, 0.11);
}
section:first-of-type {
padding-top: 0;
border-top: 0;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
}
.section-title h2 {
margin: 0;
font-size: 13px;
letter-spacing: 0.02em;
}
.step {
display: grid;
width: 24px;
height: 24px;
place-items: center;
border: 1px solid rgba(158, 196, 226, 0.2);
border-radius: 8px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
}
.prior-values {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
font-size: 12px;
font-weight: 700;
}
.prior-values .a {
color: var(--cyan);
}
.prior-values .b {
color: var(--orange);
}
input[type="range"] {
width: 100%;
margin: 3px 0 12px;
accent-color: var(--cyan);
cursor: pointer;
}
.prior-bar {
display: flex;
height: 10px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 999px;
background: #172536;
}
.prior-bar span {
display: block;
height: 100%;
transition: width 180ms ease;
}
#priorBarA {
background: linear-gradient(90deg, #2389bd, var(--cyan));
}
#priorBarB {
background: linear-gradient(90deg, var(--orange), #c97824);
}
.small-note {
margin: 8px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.observation-buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 40px;
border: 1px solid rgba(158, 196, 226, 0.18);
border-radius: 11px;
background: #15253a;
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: border-color 150ms ease, background 150ms ease, transform 150ms ease;
}
button:hover {
border-color: rgba(158, 196, 226, 0.4);
background: #1a2d45;
}
button:active {
transform: translateY(1px);
}
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.token-button {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
.token-button[aria-pressed="true"] {
border-color: rgba(116, 224, 181, 0.7);
background: rgba(116, 224, 181, 0.1);
}
.token-dot {
width: 13px;
height: 13px;
border-radius: 50%;
box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.2);
}
.token-dot.blue {
background: var(--blue);
}
.token-dot.gold {
background: var(--gold);
}
#drawButton {
width: 100%;
margin-top: 9px;
border-color: rgba(97, 221, 255, 0.38);
background: linear-gradient(135deg, rgba(46, 157, 201, 0.26), rgba(45, 86, 127, 0.2));
}
.draw-result {
min-height: 61px;
margin-top: 10px;
padding: 10px 11px;
border: 1px solid rgba(158, 196, 226, 0.12);
border-radius: 11px;
background: rgba(5, 13, 24, 0.48);
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.draw-result strong {
color: var(--text);
}
.math-card {
overflow: hidden;
margin-top: 10px;
border: 1px solid rgba(158, 196, 226, 0.13);
border-radius: 14px;
background: rgba(4, 12, 23, 0.38);
}
.math-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 10px 11px;
border-bottom: 1px solid rgba(158, 196, 226, 0.1);
}
.math-heading strong {
font-size: 12px;
}
.notation {
padding: 4px 7px;
border-radius: 7px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 800;
}
.likelihood .notation {
background: rgba(67, 185, 255, 0.14);
color: var(--blue);
}
.posterior .notation {
background: rgba(116, 224, 181, 0.13);
color: var(--green);
}
.math-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: center;
padding: 8px 11px;
font-size: 12px;
}
.math-row + .math-row {
border-top: 1px solid rgba(158, 196, 226, 0.08);
}
.math-row b {
font-size: 14px;
}
.math-description {
margin: 0;
padding: 0 11px 10px;
color: var(--muted);
font-size: 10.5px;
line-height: 1.45;
}
.formula {
margin: 0;
padding: 9px 11px;
border-top: 1px solid rgba(158, 196, 226, 0.08);
color: #b8cad9;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9.5px;
line-height: 1.5;
}
#posteriorSummary {
margin-top: 10px;
padding: 10px 11px;
border-left: 3px solid var(--green);
border-radius: 4px 10px 10px 4px;
background: rgba(116, 224, 181, 0.07);
color: #c7e8da;
font-size: 11px;
line-height: 1.5;
}
.important-note {
margin: 13px 0 0;
padding: 11px;
border: 1px solid rgba(255, 200, 87, 0.18);
border-radius: 11px;
background: rgba(255, 200, 87, 0.055);
color: #d9c9a7;
font-size: 11px;
line-height: 1.55;
}
.important-note strong {
color: var(--gold);
}
.controls-hint {
position: fixed;
bottom: 21px;
left: 24px;
z-index: 3;
color: rgba(190, 211, 229, 0.62);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.scene-legend {
position: fixed;
top: 24px;
left: 50%;
z-index: 3;
display: flex;
gap: 14px;
align-items: center;
padding: 8px 11px;
border: 1px solid rgba(158, 196, 226, 0.12);
border-radius: 999px;
background: rgba(7, 16, 29, 0.58);
color: var(--muted);
font-size: 10px;
backdrop-filter: blur(10px);
pointer-events: none;
}
.scene-legend span {
display: flex;
align-items: center;
gap: 6px;
}
.legend-line {
width: 15px;
height: 4px;
border-radius: 4px;
}
.legend-prior {
background: linear-gradient(90deg, var(--cyan), var(--orange));
}
.legend-post {
border: 2px solid var(--green);
border-radius: 50%;
}
@media (max-width: 900px) {
header {
top: 15px;
left: 16px;
max-width: calc(100vw - 32px);
}
header p {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 49vh;
border-radius: 18px;
}
.controls-hint,
.scene-legend {
display: none;
}
}
@media (max-height: 720px) and (min-width: 901px) {
.panel {
padding: 14px;
}
section {
padding: 11px 0;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive low-poly scene containing two urns"></div>
<header>
<div class="eyebrow">Bayes in two stages</div>
<h1>Two Urns, Two Directions<br>of Probability</h1>
<p>
First choose an urn using the prior. Then observe a token color.
The same color can be likely under one urn while that urn becomes
more likely after the observation.
</p>
</header>
<div class="scene-legend">
<span><i class="legend-line legend-prior"></i>Top bar: prior</span>
<span><i class="legend-line legend-post"></i>Urn labels: posterior</span>
</div>
<aside class="panel">
<p class="panel-intro">
Urn A contains 7 blue and 3 gold tokens. Urn B contains 3 blue and
7 gold tokens. A token is drawn uniformly from whichever urn was selected.
</p>
<section>
<div class="section-title">
<h2>Prior selection</h2>
<span class="step">1</span>
</div>
<div class="prior-values">
<span class="a">P(A) = <span id="priorAValue">60%</span></span>
<span class="b">P(B) = <span id="priorBValue">40%</span></span>
</div>
<input
id="priorSlider"
type="range"
min="0"
max="100"
value="60"
aria-label="Prior probability of selecting urn A"
>
<div class="prior-bar" aria-hidden="true">
<span id="priorBarA" style="width:60%"></span>
<span id="priorBarB" style="width:40%"></span>
</div>
<p class="small-note">
This is the urn-selection probability before any token is observed.
</p>
</section>
<section>
<div class="section-title">
<h2>Observation</h2>
<span class="step">2</span>
</div>
<div class="observation-buttons">
<button class="token-button" data-observation="blue" aria-pressed="true">
<i class="token-dot blue"></i>
Blue observed
</button>
<button class="token-button" data-observation="gold" aria-pressed="false">
<i class="token-dot gold"></i>
Gold observed
</button>
</div>
<button id="drawButton">Simulate a two-stage draw</button>
<div id="drawResult" class="draw-result">
<strong>Manual evidence:</strong> condition on a blue token without
randomly selecting an urn.
</div>
</section>
<section>
<div class="section-title">
<h2>Read the two directions</h2>
<span class="step">3</span>
</div>
<div class="math-card likelihood">
<div class="math-heading">
<strong>Likelihood</strong>
<span class="notation">P(O | U)</span>
</div>
<div class="math-row">
<span>P(blue | A)</span>
<b id="likelihoodA">70%</b>
</div>
<div class="math-row">
<span>P(blue | B)</span>
<b id="likelihoodB">30%</b>
</div>
<p class="math-description">
If urn U had been selected, how likely was the observed color?
These values describe the <em>observation</em>, not the urn.
</p>
</div>
<div class="math-card posterior">
<div class="math-heading">
<strong>Posterior</strong>
<span class="notation">P(U | O)</span>
</div>
<div class="math-row">
<span>P(A | blue)</span>
<b id="posteriorA">77.8%</b>
</div>
<div class="math-row">
<span>P(B | blue)</span>
<b id="posteriorB">22.2%</b>
</div>
<p class="math-description">
Given the observed color, how likely is each urn?
These values describe the <em>urn</em>, after incorporating the prior.
</p>
<p id="formulaA" class="formula"></p>
<p id="formulaB" class="formula"></p>
</div>
<div id="posteriorSummary"></div>
<p class="important-note">
<strong>They are not interchangeable.</strong>
P(O | U) is the probability of an observation under an urn.
P(U | O) is the probability of an urn given an observation.
Bayes' rule connects them.
</p>
</section>
</aside>
<div class="controls-hint">Drag to orbit · Scroll to zoom · Arrow keys also orbit</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const COLORS = {
blue: 0x43b9ff,
gold: 0xffc857,
cyan: 0x61ddff,
orange: 0xffb552,
green: 0x74e0b5,
background: 0x08111f
};
const COMPOSITIONS = {
A: { blue: 7, gold: 3 },
B: { blue: 3, gold: 7 }
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.background);
scene.fog = new THREE.Fog(COLORS.background, 14, 31);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.querySelector("#scene").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xb9ddff, 0x18202b, 2.0);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4bbdff, 1.7);
rimLight.position.set(7, 4, -6);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb44f, 15, 16, 2);
warmLight.position.set(5, 3.5, 3);
scene.add(warmLight);
// Low-poly ground.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x0d1a29,
roughness: 0.95,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.39;
ground.receiveShadow = true;
scene.add(ground);
const groundRing = new THREE.Mesh(
new THREE.RingGeometry(6.2, 6.24, 64),
new THREE.MeshBasicMaterial({
color: 0x31516a,
transparent: true,
opacity: 0.28,
side: THREE.DoubleSide
})
);
groundRing.rotation.x = -Math.PI / 2;
groundRing.position.y = -0.385;
scene.add(groundRing);
// Small faceted background stones.
let seed = 8137;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x16283a,
roughness: 0.92,
flatShading: true
});
for (let i = 0; i < 15; i++) {
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.15 + random() * 0.38, 0),
rockMaterial
);
const side = i % 2 === 0 ? -1 : 1;
rock.position.set(
side * (4.5 + random() * 5.5),
-0.27,
-2.8 - random() * 6.5
);
rock.rotation.set(random() * 2, random() * 2, random() * 2);
rock.scale.y = 0.65 + random() * 0.55;
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
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.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function drawLabelCanvas(label, lines, accent) {
const canvas = label.canvas;
const ctx = label.ctx;
const w = canvas.width;
const h = canvas.height;
ctx.clearRect(0, 0, w, h);
roundedRect(ctx, 4, 4, w - 8, h - 8, 22);
ctx.fillStyle = "rgba(7, 17, 30, 0.91)";
ctx.fill();
ctx.lineWidth = 4;
ctx.strokeStyle = accent;
ctx.stroke();
ctx.fillStyle = accent;
roundedRect(ctx, 20, 18, 8, h - 36, 4);
ctx.fill();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
for (const line of lines) {
ctx.font = `${line.weight ?? 700} ${line.size}px system-ui, sans-serif`;
ctx.fillStyle = line.color ?? "#edf7ff";
ctx.fillText(line.text, w / 2 + 4, line.y);
}
label.texture.needsUpdate = true;
}
function makeLabel({
width = 640,
height = 180,
worldWidth = 2.6,
accent = "#61ddff",
lines = []
} = {}) {
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;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldWidth * height / width, 1);
sprite.renderOrder = 30;
const label = { canvas, ctx, texture, sprite };
drawLabelCanvas(label, lines, accent);
label.update = (nextLines, nextAccent = accent) => {
drawLabelCanvas(label, nextLines, nextAccent);
};
return label;
}
const blueTokenMaterial = new THREE.MeshStandardMaterial({
color: COLORS.blue,
emissive: 0x073451,
emissiveIntensity: 0.45,
roughness: 0.36,
metalness: 0.08,
flatShading: true
});
const goldTokenMaterial = new THREE.MeshStandardMaterial({
color: COLORS.gold,
emissive: 0x4a2b00,
emissiveIntensity: 0.38,
roughness: 0.4,
metalness: 0.08,
flatShading: true
});
const tokenGeometry = new THREE.CylinderGeometry(
0.16,
0.16,
0.09,
8,
1,
false
);
const tokenSlots = [
[-0.32, 0.31, -0.18],
[ 0.05, 0.31, -0.28],
[ 0.34, 0.33, 0.02],
[-0.12, 0.34, 0.28],
[ 0.24, 0.37, 0.30],
[-0.34, 0.49, -0.32],
[ 0.02, 0.50, -0.12],
[ 0.36, 0.51, -0.30],
[-0.13, 0.54, 0.28],
[ 0.26, 0.58, 0.03]
];
function makeUrn(name, x, accentColor) {
const group = new THREE.Group();
group.position.x = x;
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.22, 1.32, 0.28, 8),
new THREE.MeshStandardMaterial({
color: 0x17283a,
roughness: 0.82,
metalness: 0.12,
flatShading: true
})
);
pedestal.position.y = -0.22;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
group.add(pedestal);
const pedestalTop = new THREE.Mesh(
new THREE.CylinderGeometry(1.08, 1.15, 0.08, 8),
new THREE.MeshStandardMaterial({
color: 0x23394e,
roughness: 0.66,
metalness: 0.18,
flatShading: true
})
);
pedestalTop.position.y = -0.04;
pedestalTop.receiveShadow = true;
group.add(pedestalTop);
const bodyGeometry = new THREE.CylinderGeometry(
0.91,
0.67,
1.56,
8,
1,
true
);
const glass = new THREE.Mesh(
bodyGeometry,
new THREE.MeshPhysicalMaterial({
color: 0x9ddcf1,
transparent: true,
opacity: 0.18,
roughness: 0.12,
metalness: 0,
transmission: 0,
side: THREE.DoubleSide,
depthWrite: false
})
);
glass.position.y = 0.84;
glass.renderOrder = 3;
group.add(glass);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(bodyGeometry, 18),
new THREE.LineBasicMaterial({
color: accentColor,
transparent: true,
opacity: 0.48
})
);
edges.position.y = 0.84;
edges.renderOrder = 4;
group.add(edges);
const bottom = new THREE.Mesh(
new THREE.CylinderGeometry(0.68, 0.68, 0.11, 8),
new THREE.MeshStandardMaterial({
color: accentColor,
roughness: 0.42,
metalness: 0.22,
flatShading: true
})
);
bottom.position.y = 0.1;
bottom.castShadow = true;
group.add(bottom);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.9, 0.065, 4, 8),
new THREE.MeshStandardMaterial({
color: accentColor,
emissive: accentColor,
emissiveIntensity: 0.2,
roughness: 0.35,
metalness: 0.28,
flatShading: true
})
);
rim.rotation.x = Math.PI / 2;
rim.position.y = 1.63;
rim.castShadow = true;
group.add(rim);
const neck = new THREE.Mesh(
new THREE.CylinderGeometry(0.91, 0.91, 0.12, 8, 1, true),
new THREE.MeshStandardMaterial({
color: accentColor,
transparent: true,
opacity: 0.72,
roughness: 0.38,
metalness: 0.18,
side: THREE.DoubleSide
})
);
neck.position.y = 1.61;
group.add(neck);
const composition = [];
for (let i = 0; i < 7; i++) composition.push("blue");
for (let i = 0; i < 3; i++) composition.push("gold");
if (name === "B") {
composition.length = 0;
for (let i = 0; i < 3; i++) composition.push("blue");
for (let i = 0; i < 7; i++) composition.push("gold");
}
composition.forEach((color, index) => {
const token = new THREE.Mesh(
tokenGeometry,
color === "blue" ? blueTokenMaterial : goldTokenMaterial
);
const slot = tokenSlots[index];
token.position.set(slot[0], slot[1], slot[2]);
token.rotation.y = (index % 4) * 0.19;
token.castShadow = true;
token.receiveShadow = true;
group.add(token);
});
const haloMaterial = new THREE.MeshBasicMaterial({
color: accentColor,
transparent: true,
opacity: 0.35,
side: THREE.DoubleSide
});
const halo = new THREE.Mesh(
new THREE.TorusGeometry(1.13, 0.034, 6, 40),
haloMaterial
);
halo.rotation.x = Math.PI / 2;
halo.position.y = 0.025;
group.add(halo);
const label = makeLabel({
width: 640,
height: 190,
worldWidth: 2.65,
accent: `#${new THREE.Color(accentColor).getHexString()}`,
lines: []
});
label.sprite.position.set(0, 2.56, 0);
group.add(label.sprite);
scene.add(group);
return {
name,
group,
halo,
haloMaterial,
label,
selectedAt: -100
};
}
const urnA = makeUrn("A", -2.65, COLORS.cyan);
const urnB = makeUrn("B", 2.65, COLORS.orange);
// 3D prior-selection indicator.
const priorGroup = new THREE.Group();
priorGroup.position.set(0, 3.33, 0);
scene.add(priorGroup);
const barBack = new THREE.Mesh(
new THREE.BoxGeometry(4.34, 0.25, 0.2),
new THREE.MeshStandardMaterial({
color: 0x14283b,
roughness: 0.72,
metalness: 0.12
})
);
priorGroup.add(barBack);
const priorBarA = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: COLORS.cyan,
emissive: 0x0b607c,
emissiveIntensity: 0.35,
roughness: 0.45
})
);
priorGroup.add(priorBarA);
const priorBarB = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: COLORS.orange,
emissive: 0x713800,
emissiveIntensity: 0.3,
roughness: 0.45
})
);
priorGroup.add(priorBarB);
const priorPointer = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.28, 3),
new THREE.MeshBasicMaterial({ color: 0xffffff })
);
priorPointer.rotation.z = Math.PI;
priorPointer.position.y = -0.31;
priorGroup.add(priorPointer);
const priorLabel = makeLabel({
width: 768,
height: 160,
worldWidth: 4.85,
accent: "#9eb9cd",
lines: []
});
priorLabel.sprite.position.set(0, 3.94, 0);
scene.add(priorLabel.sprite);
// Observation pedestal and floating sample token.
const observationPedestal = new THREE.Mesh(
new THREE.CylinderGeometry(0.72, 0.82, 0.18, 8),
new THREE.MeshStandardMaterial({
color: 0x1a3044,
roughness: 0.75,
metalness: 0.16,
flatShading: true
})
);
observationPedestal.position.set(0, -0.26, 1.85);
observationPedestal.castShadow = true;
observationPedestal.receiveShadow = true;
scene.add(observationPedestal);
const sampleGroup = new THREE.Group();
sampleGroup.position.set(0, 0.64, 1.85);
scene.add(sampleGroup);
const sampleMaterial = new THREE.MeshStandardMaterial({
color: COLORS.blue,
emissive: 0x073451,
emissiveIntensity: 0.5,
roughness: 0.3,
metalness: 0.08,
flatShading: true
});
const sampleToken = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.28, 0.14, 8),
sampleMaterial
);
sampleToken.rotation.x = Math.PI / 2;
sampleToken.castShadow = true;
sampleGroup.add(sampleToken);
const sampleRing = new THREE.Mesh(
new THREE.TorusGeometry(0.4, 0.025, 6, 32),
new THREE.MeshBasicMaterial({
color: COLORS.blue,
transparent: true,
opacity: 0.65
})
);
sampleRing.position.z = -0.07;
sampleGroup.add(sampleRing);
const observationLabel = makeLabel({
width: 512,
height: 150,
worldWidth: 2.5,
accent: "#43b9ff",
lines: []
});
observationLabel.sprite.position.set(0, 1.47, 1.85);
scene.add(observationLabel.sprite);
// UI elements.
const priorSlider = document.querySelector("#priorSlider");
const priorAValue = document.querySelector("#priorAValue");
const priorBValue = document.querySelector("#priorBValue");
const priorBarAUI = document.querySelector("#priorBarA");
const priorBarBUI = document.querySelector("#priorBarB");
const likelihoodA = document.querySelector("#likelihoodA");
const likelihoodB = document.querySelector("#likelihoodB");
const posteriorA = document.querySelector("#posteriorA");
const posteriorB = document.querySelector("#posteriorB");
const formulaA = document.querySelector("#formulaA");
const formulaB = document.querySelector("#formulaB");
const posteriorSummary = document.querySelector("#posteriorSummary");
const drawResult = document.querySelector("#drawResult");
const drawButton = document.querySelector("#drawButton");
const observationButtons = [
...document.querySelectorAll("[data-observation]")
];
let currentObservation = "blue";
let lastDraw = null;
let samplePopAt = -100;
const percentage = value => `${(value * 100).toFixed(1)}%`;
const shortPercentage = value => `${(value * 100).toFixed(0)}%`;
function updateObservationButtons() {
for (const button of observationButtons) {
button.setAttribute(
"aria-pressed",
String(button.dataset.observation === currentObservation)
);
}
}
function updatePriorIndicator(pA) {
const pB = 1 - pA;
priorAValue.textContent = shortPercentage(pA);
priorBValue.textContent = shortPercentage(pB);
priorBarAUI.style.width = `${pA * 100}%`;
priorBarBUI.style.width = `${pB * 100}%`;
priorBarA.scale.set(4 * pA, 0.18, 0.17);
priorBarA.position.set(-2 + 2 * pA, 0, 0.03);
priorBarB.scale.set(4 * pB, 0.18, 0.17);
priorBarB.position.set(2 * pA, 0, 0.03);
priorPointer.position.x = -2 + 4 * pA;
priorLabel.update([
{
text: "PRIOR SELECTION",
size: 38,
y: 48,
color: "#d9e9f5"
},
{
text: `P(A) = ${shortPercentage(pA)} P(B) = ${shortPercentage(pB)}`,
size: 31,
y: 108,
color: "#91a8bd"
}
]);
}
function updateProbabilities() {
const pA = Number(priorSlider.value) / 100;
const pB = 1 - pA;
const likelihoodAValue =
currentObservation === "blue"
? COMPOSITIONS.A.blue / 10
: COMPOSITIONS.A.gold / 10;
const likelihoodBValue =
currentObservation === "blue"
? COMPOSITIONS.B.blue / 10
: COMPOSITIONS.B.gold / 10;
const evidence = pA * likelihoodAValue + pB * likelihoodBValue;
const posteriorAValue = (pA * likelihoodAValue) / evidence;
const posteriorBValue = (pB * likelihoodBValue) / evidence;
likelihoodA.textContent = percentage(likelihoodAValue);
likelihoodB.textContent = percentage(likelihoodBValue);
posteriorA.textContent = percentage(posteriorAValue);
posteriorB.textContent = percentage(posteriorBValue);
formulaA.textContent =
`P(A | ${currentObservation}) = ` +
`(${pA.toFixed(2)} × ${likelihoodAValue.toFixed(2)}) / ` +
`(${pA.toFixed(2)} × ${likelihoodAValue.toFixed(2)} + ` +
`${pB.toFixed(2)} × ${likelihoodBValue.toFixed(2)}) = ` +
`${percentage(posteriorAValue)}`;
formulaB.textContent =
`P(B | ${currentObservation}) = ` +
`(${pB.toFixed(2)} × ${likelihoodBValue.toFixed(2)}) / ` +
`(${pA.toFixed(2)} × ${likelihoodAValue.toFixed(2)} + ` +
`${pB.toFixed(2)} × ${likelihoodBValue.toFixed(2)}) = ` +
`${percentage(posteriorBValue)}`;
posteriorSummary.textContent =
`Given a ${currentObservation} observation, urn A is ` +
`${percentage(posteriorAValue)} likely and urn B is ` +
`${percentage(posteriorBValue)} likely.`;
urnA.halo.userData.targetOpacity = 0.18 + posteriorAValue * 0.62;
urnB.halo.userData.targetOpacity = 0.18 + posteriorBValue * 0.62;
urnA.label.update([
{
text: "URN A",
size: 42,
y: 46,
color: "#e9fbff"
},
{
text: "7 BLUE • 3 GOLD",
size: 27,
y: 94,
color: "#91a8bd"
},
{
text: `POSTERIOR ${percentage(posteriorAValue)}`,
size: 27,
y: 145,
color: "#61ddff"
}
]);
urnB.label.update([
{
text: "URN B",
size: 42,
y: 46,
color: "#fff5df"
},
{
text: "3 BLUE • 7 GOLD",
size: 27,
y: 94,
color: "#91a8bd"
},
{
text: `POSTERIOR ${percentage(posteriorBValue)}`,
size: 27,
y: 145,
color: "#ffb552"
}
]);
const sampleColor =
currentObservation === "blue" ? COLORS.blue : COLORS.gold;
sampleMaterial.color.setHex(sampleColor);
sampleMaterial.emissive.setHex(
currentObservation === "blue" ? 0x073451 : 0x4a2b00
);
sampleRing.material.color.setHex(sampleColor);
observationLabel.update([
{
text: "OBSERVATION",
size: 31,
y: 45,
color: "#91a8bd"
},
{
text: `${currentObservation.toUpperCase()} TOKEN`,
size: 42,
y: 101,
color: currentObservation === "blue" ? "#43b9ff" : "#ffc857"
}
]);
updatePriorIndicator(pA);
}
function updateDrawResult() {
if (lastDraw) {
const pA = Number(priorSlider.value) / 100;
drawResult.innerHTML =
`<strong>Prior step:</strong> urn ${lastDraw.urn} was selected ` +
`with ${shortPercentage(lastDraw.urn === "A" ? pA : 1 - pA)} probability. ` +
`<br><strong>Evidence step:</strong> a ` +
`${lastDraw.observation} token was drawn from that urn.`;
} else {
drawResult.innerHTML =
`<strong>Manual evidence:</strong> condition on a ` +
`${currentObservation} token without randomly selecting an urn.`;
}
}
function setObservation(observation) {
currentObservation = observation;
updateObservationButtons();
updateProbabilities();
updateDrawResult();
}
observationButtons.forEach(button => {
button.addEventListener("click", () => {
lastDraw = null;
setObservation(button.dataset.observation);
samplePopAt = performance.now() / 1000;
});
});
priorSlider.addEventListener("input", () => {
updateProbabilities();
updateDrawResult();
});
drawButton.addEventListener("click", () => {
const pA = Number(priorSlider.value) / 100;
// Stage 1: select an urn according to the prior.
const selectedUrn = Math.random() < pA ? "A" : "B";
// Stage 2: draw a token uniformly from that urn.
const blueChance =
selectedUrn === "A"
? COMPOSITIONS.A.blue / 10
: COMPOSITIONS.B.blue / 10;
const observation =
Math.random() < blueChance ? "blue" : "gold";
lastDraw = {
urn: selectedUrn,
observation
};
const now = performance.now() / 1000;
samplePopAt = now;
if (selectedUrn === "A") {
urnA.selectedAt = now;
urnB.selectedAt = -100;
} else {
urnB.selectedAt = now;
urnA.selectedAt = -100;
}
setObservation(observation);
});
// Camera controls implemented directly, without addons.
const cameraState = {
theta: 0.16,
phi: 1.12,
zoom: 1,
baseRadius: 11.5,
target: new THREE.Vector3(0.9, 1.25, 0)
};
function updateCamera() {
const radius = cameraState.baseRadius * cameraState.zoom;
camera.position.set(
cameraState.target.x +
radius * Math.sin(cameraState.phi) * Math.sin(cameraState.theta),
cameraState.target.y + radius * Math.cos(cameraState.phi),
cameraState.target.z +
radius * Math.sin(cameraState.phi) * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
const pointer = {
active: false,
id: null,
x: 0,
y: 0
};
renderer.domElement.addEventListener("pointerdown", event => {
pointer.active = true;
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointer.active || pointer.id !== event.pointerId) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.63,
1.36
);
pointer.x = event.clientX;
pointer.y = event.clientY;
});
function releasePointer(event) {
if (pointer.id !== event.pointerId) return;
pointer.active = false;
pointer.id = null;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom + event.deltaY * 0.0008,
0.7,
1.35
);
},
{ passive: false }
);
window.addEventListener("keydown", event => {
if (event.target instanceof HTMLInputElement) return;
if (event.key === "ArrowLeft") cameraState.theta += 0.08;
if (event.key === "ArrowRight") cameraState.theta -= 0.08;
if (event.key === "ArrowUp") {
cameraState.phi = Math.max(0.63, cameraState.phi - 0.06);
}
if (event.key === "ArrowDown") {
cameraState.phi = Math.min(1.36, cameraState.phi + 0.06);
}
if (event.key === "+" || event.key === "=") {
cameraState.zoom = Math.max(0.7, cameraState.zoom - 0.06);
}
if (event.key === "-" || event.key === "_") {
cameraState.zoom = Math.min(1.35, cameraState.zoom + 0.06);
}
});
function resize() {
const width = innerWidth;
const height = innerHeight;
const mobile = width < 760;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
camera.aspect = width / height;
camera.updateProjectionMatrix();
cameraState.baseRadius = mobile ? 16.5 : 11.5;
cameraState.target.x = width > 900 ? 0.9 : 0;
cameraState.target.y = mobile ? 1.65 : 1.25;
updateCamera();
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
const now = performance.now() / 1000;
for (const urn of [urnA, urnB]) {
const targetOpacity = urn.halo.userData.targetOpacity ?? 0.35;
urn.haloMaterial.opacity +=
(targetOpacity - urn.haloMaterial.opacity) * 0.08;
const pulseAge = now - urn.selectedAt;
const pulse =
pulseAge >= 0 && pulseAge < 2
? Math.exp(-pulseAge * 2.2) *
Math.abs(Math.sin(pulseAge * 10)) *
0.045
: 0;
urn.group.scale.setScalar(1 + pulse);
}
sampleGroup.position.y = 0.64 + Math.sin(elapsed * 2.2) * 0.07;
sampleGroup.rotation.y = Math.sin(elapsed * 0.9) * 0.14;
const popAge = now - samplePopAt;
if (popAge >= 0 && popAge < 0.65) {
const t = Math.min(1, popAge / 0.65);
const eased = 1 - Math.pow(1 - t, 3);
sampleGroup.scale.setScalar(0.15 + eased * 0.85);
} else {
sampleGroup.scale.setScalar(1);
}
sampleRing.material.opacity = 0.48 + Math.sin(elapsed * 2.5) * 0.16;
updateCamera();
renderer.render(scene, camera);
}
resize();
updateObservationButtons();
updateProbabilities();
updateDrawResult();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```