Prompt specification
Build or customize the Three.js 3D scene "Two training-program classroom exhibit" 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 training-program classroom exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/two-training-program-classroom-exhibit-x1835
Scene Description Prompt:
Show Simpson’s paradox in an HTML voxel classroom exhibit using Three.js from cdnjs, comparing pooled and grouped synthetic outcomes from two training programs.
Working Three.js Starter (two-training-program-classroom-exhibit-x1835):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Simpson’s Paradox — Voxel Classroom</title>
<style>
:root {
--ink: #172630;
--muted: #61727b;
--paper: #fffdf7;
--glass: rgba(255, 253, 247, 0.88);
--line: rgba(23, 38, 48, 0.13);
--program-a: #5968f2;
--program-b: #ee9b35;
--pass: #24ad82;
--fail: #e8666c;
--shadow: 0 18px 50px rgba(28, 48, 60, 0.15);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe8ed;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.app-header {
position: fixed;
z-index: 5;
top: 27px;
left: 30px;
max-width: 520px;
pointer-events: none;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.45);
}
.eyebrow {
display: block;
margin-bottom: 9px;
color: #536871;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(36px, 4.2vw, 64px);
font-weight: 850;
letter-spacing: -0.055em;
line-height: 0.98;
}
.app-header p {
max-width: 440px;
margin: 13px 0 0;
color: #536871;
font-size: 14px;
line-height: 1.5;
}
.controls {
position: fixed;
z-index: 8;
top: 27px;
right: 28px;
display: flex;
align-items: center;
gap: 10px;
}
.view-switch {
display: flex;
padding: 5px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 15px;
background: var(--glass);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.view-switch button,
.reset-button {
min-height: 40px;
padding: 0 16px;
border: 0;
border-radius: 11px;
background: transparent;
color: #53636c;
cursor: pointer;
font-size: 13px;
font-weight: 750;
transition:
color 180ms ease,
background 180ms ease,
transform 180ms ease;
}
.view-switch button:hover,
.reset-button:hover {
color: var(--ink);
background: rgba(23, 38, 48, 0.06);
}
.view-switch button.active {
color: white;
background: var(--ink);
box-shadow: 0 5px 13px rgba(23, 38, 48, 0.22);
}
.reset-button {
border: 1px solid rgba(255, 255, 255, 0.75);
background: var(--glass);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.readout {
position: fixed;
z-index: 6;
top: 101px;
right: 28px;
width: min(365px, calc(100vw - 40px));
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.76);
border-radius: 22px;
background: var(--glass);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
pointer-events: none;
}
.readout h2 {
margin: 0 0 7px;
font-size: 21px;
letter-spacing: -0.025em;
}
.readout > p {
margin: 0 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.48;
}
.metric-section {
margin-top: 10px;
padding: 13px;
border: 1px solid transparent;
border-radius: 15px;
background: rgba(23, 38, 48, 0.035);
opacity: 0.46;
transition:
opacity 250ms ease,
border-color 250ms ease,
background 250ms ease;
}
.metric-section.active {
border-color: rgba(23, 38, 48, 0.12);
background: rgba(255, 255, 255, 0.72);
opacity: 1;
}
.metric-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 9px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.metric-heading span:last-child {
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
}
.cohort-row {
display: grid;
grid-template-columns: 13px 1fr 58px 58px 37px;
align-items: center;
gap: 7px;
min-height: 29px;
font-size: 12px;
}
.cohort-row + .cohort-row {
border-top: 1px solid var(--line);
}
.cohort-row b {
text-align: right;
font-variant-numeric: tabular-nums;
}
.cohort-row em {
color: var(--muted);
font-size: 10px;
font-style: normal;
font-weight: 800;
text-align: right;
text-transform: uppercase;
}
.program-dot {
width: 10px;
height: 10px;
border-radius: 3px;
}
.program-dot.a {
background: var(--program-a);
}
.program-dot.b {
background: var(--program-b);
}
.pool-row {
margin-top: 8px;
}
.pool-label {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
font-size: 12px;
font-weight: 750;
}
.pool-label span:last-child {
font-variant-numeric: tabular-nums;
}
.bar-track {
height: 9px;
overflow: hidden;
border-radius: 999px;
background: rgba(23, 38, 48, 0.08);
}
.bar-fill {
width: var(--width);
height: 100%;
border-radius: inherit;
transition: width 700ms cubic-bezier(0.22, 1, 0.36, 1);
}
.bar-fill.a {
background: var(--program-a);
}
.bar-fill.b {
background: var(--program-b);
}
.legend {
position: fixed;
z-index: 6;
bottom: 27px;
left: 30px;
display: grid;
grid-template-columns: auto auto;
gap: 8px 18px;
padding: 14px 16px;
border: 1px solid rgba(255, 255, 255, 0.68);
border-radius: 16px;
background: rgba(255, 253, 247, 0.8);
box-shadow: 0 12px 34px rgba(28, 48, 60, 0.11);
backdrop-filter: blur(12px);
pointer-events: none;
font-size: 11px;
font-weight: 700;
color: #4e616b;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
}
.body-chip {
width: 13px;
height: 17px;
border-radius: 3px;
}
.body-chip.a {
background: var(--program-a);
}
.body-chip.b {
background: var(--program-b);
}
.cap-chip {
width: 13px;
height: 8px;
border-radius: 2px;
}
.cap-chip.pass {
background: var(--pass);
}
.cap-chip.fail {
background: var(--fail);
}
.footer-note {
position: fixed;
z-index: 5;
right: 29px;
bottom: 25px;
max-width: 370px;
color: #52656e;
font-size: 11px;
line-height: 1.45;
text-align: right;
pointer-events: none;
}
.control-hint {
position: fixed;
z-index: 5;
left: 50%;
bottom: 28px;
transform: translateX(-50%);
padding: 9px 13px;
border-radius: 999px;
background: rgba(255, 253, 247, 0.72);
color: #5a6b74;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.035em;
pointer-events: none;
white-space: nowrap;
}
@media (max-width: 850px) {
.app-header {
top: 16px;
left: 16px;
max-width: 45vw;
}
.app-header p,
.eyebrow {
display: none;
}
h1 {
font-size: 34px;
}
.controls {
top: 14px;
right: 12px;
gap: 5px;
}
.view-switch button,
.reset-button {
min-height: 36px;
padding: 0 10px;
font-size: 11px;
}
.reset-button {
padding: 0 11px;
}
.readout {
top: auto;
right: 12px;
bottom: 67px;
left: 12px;
width: auto;
max-height: 45vh;
padding: 15px;
overflow: auto;
}
.readout h2 {
font-size: 18px;
}
.legend,
.control-hint {
display: none;
}
.footer-note {
right: 12px;
bottom: 10px;
left: 12px;
max-width: none;
text-align: center;
}
}
</style>
</</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive 3D voxel classroom showing Simpson's paradox for two training programs"
></canvas>
<header class="app-header">
<span class="eyebrow">Voxel Classroom / Data Lab 04</span>
<h1>Simpson’s Paradox</h1>
<p>
Two training programs. Two pre-existing cohorts.
The winning program changes when the learners are pooled.
</p>
</header>
<nav class="controls" aria-label="Exhibit controls">
<div class="view-switch" role="group" aria-label="Choose data arrangement">
<button id="groupedButton" class="active" aria-pressed="true">
Grouped
</button>
<button id="pooledButton" aria-pressed="false">
Pooled
</button>
</div>
<button id="resetButton" class="reset-button" title="Reset camera">
Reset view
</button>
</nav>
<aside class="readout" aria-live="polite">
<span class="eyebrow">The reversal</span>
<h2 id="insightTitle">Program A leads inside each cohort.</h2>
<p id="insightText">
A beats B in both groups. The cohort composition is what changes
the result after pooling.
</p>
<section id="groupedMetrics" class="metric-section active">
<div class="metric-heading">
<span>Grouped outcomes</span>
<span>A leads both cohorts</span>
</div>
<div class="cohort-row">
<i class="program-dot a"></i>
<span>Cohort 1</span>
<b>60%</b>
<b>50%</b>
<em>A</em>
</div>
<div class="cohort-row">
<i class="program-dot a"></i>
<span>Cohort 2</span>
<b>80%</b>
<b>70%</b>
<em>A</em>
</div>
</section>
<section id="pooledMetrics" class="metric-section">
<div class="metric-heading">
<span>Pooled outcomes</span>
<span>B leads overall</span>
</div>
<div class="pool-row">
<div class="pool-label">
<span>Program A</span>
<span>64%</span>
</div>
<div class="bar-track">
<div class="bar-fill a" style="--width: 64%"></div>
</div>
</div>
<div class="pool-row">
<div class="pool-label">
<span>Program B</span>
<span>66%</span>
</div>
<div class="bar-track">
<div class="bar-fill b" style="--width: 66%"></div>
</div>
</div>
</section>
</aside>
<div class="legend" aria-label="Voxel legend">
<div class="legend-item">
<i class="body-chip a"></i>
<span>Program A learner</span>
</div>
<div class="legend-item">
<i class="body-chip b"></i>
<span>Program B learner</span>
</div>
<div class="legend-item">
<i class="cap-chip pass"></i>
<span>Passed</span>
</div>
<div class="legend-item">
<i class="cap-chip fail"></i>
<span>Did not pass</span>
</div>
</div>
<div class="control-hint">Drag to orbit · wheel to zoom · shift-drag to pan</div>
<div class="footer-note">
Synthetic outcomes with 100 learners per program. Different cohort
mixes explain the reversal; the exhibit does not establish causation.
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Synthetic data
//
// Program A wins inside both cohorts:
// Cohort 1: A 48/80 = 60%, B 10/20 = 50%
// Cohort 2: A 16/20 = 80%, B 56/80 = 70%
//
// But after pooling:
// Program A: 64/100 = 64%
// Program B: 66/100 = 66%
// -------------------------------------------------------------------------
const DATA = {
A: {
name: "PROGRAM A",
color: 0x5968f2,
cohorts: [
{ name: "Cohort 1", count: 80, passed: 48 },
{ name: "Cohort 2", count: 20, passed: 16 }
]
},
B: {
name: "PROGRAM B",
color: 0xee9b35,
cohorts: [
{ name: "Cohort 1", count: 20, passed: 10 },
{ name: "Cohort 2", count: 80, passed: 56 }
]
}
};
const sceneCanvas = document.querySelector("#scene");
const groupedButton = document.querySelector("#groupedButton");
const pooledButton = document.querySelector("#pooledButton");
const resetButton = document.querySelector("#resetButton");
const groupedMetrics = document.querySelector("#groupedMetrics");
const pooledMetrics = document.querySelector("#pooledMetrics");
const insightTitle = document.querySelector("#insightTitle");
const insightText = document.querySelector("#insightText");
const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
// -------------------------------------------------------------------------
// Renderer, scene and lights
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas: sceneCanvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbe8ed);
scene.fog = new THREE.Fog(0xdbe8ed, 27, 56);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
scene.add(new THREE.HemisphereLight(0xffffff, 0x8d806f, 2.1));
const sun = new THREE.DirectionalLight(0xfff4de, 3.2);
sun.position.set(-9, 18, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -19;
sun.shadow.camera.right = 19;
sun.shadow.camera.top = 19;
sun.shadow.camera.bottom = -19;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 50;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xcce9ff, 1.1);
fill.position.set(12, 9, -4);
scene.add(fill);
// -------------------------------------------------------------------------
// Small utility functions
// -------------------------------------------------------------------------
function makeBox(
width,
height,
depth,
color,
x,
y,
z,
parent = scene,
options = {}
) {
const material =
options.material ||
new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.75,
metalness: options.metalness ?? 0
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
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();
}
function canvasTexture(canvas) {
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
function makeLabelTexture(title, subtitle, accentHex) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 280;
const ctx = canvas.getContext("2d");
const accent = `#${accentHex.toString(16).padStart(6, "0")}`;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.shadowColor = "rgba(28, 48, 60, 0.16)";
ctx.shadowBlur = 18;
ctx.shadowOffsetY = 7;
ctx.fillStyle = "rgba(255, 253, 247, 0.97)";
roundedRect(ctx, 18, 18, 988, 244, 28);
ctx.fill();
ctx.shadowColor = "transparent";
ctx.fillStyle = accent;
roundedRect(ctx, 18, 18, 22, 244, 11);
ctx.fill();
ctx.fillStyle = "#172630";
ctx.font = "800 62px system-ui, sans-serif";
ctx.textBaseline = "middle";
ctx.fillText(title, 72, 105);
ctx.fillStyle = "#5a6b74";
ctx.font = "650 38px system-ui, sans-serif";
ctx.fillText(subtitle, 72, 180);
ctx.fillStyle = accent;
roundedRect(ctx, 812, 46, 145, 48, 14);
ctx.fill();
ctx.fillStyle = "white";
ctx.font = "800 22px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText("PASS RATE", 884, 71);
return canvasTexture(canvas);
}
function makeLabel(title, subtitle, accent, width = 5.25) {
const material = new THREE.SpriteMaterial({
map: makeLabelTexture(title, subtitle, accent),
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * 280 / 1024, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeBoardTexture() {
const canvas = document.createElement("canvas");
canvas.width = 1600;
canvas.height = 500;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#fffef9";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#172630";
ctx.font = "850 83px system-ui, sans-serif";
ctx.textBaseline = "middle";
ctx.fillText("SIMPSON’S PARADOX", 90, 115);
ctx.fillStyle = "#65757d";
ctx.font = "550 35px system-ui, sans-serif";
ctx.fillText(
"THE WINNER CHANGES WHEN THE COHORT MIX IS HIDDEN.",
94,
210
);
const rows = [
{ x: 94, color: "#5968f2", label: "A", value: "60% / 80%" },
{ x: 94, color: "#5968f2", label: "A", value: "80% / 70%" },
{ x: 845, color: "#ee9b35", label: "B", value: "66% OVERALL" }
];
ctx.font = "750 27px system-ui, sans-serif";
ctx.fillStyle = "#172630";
ctx.fillText("GROUPED", 94, 320);
ctx.fillText("POOLED", 845, 320);
rows.forEach((row, index) => {
const y = 375 + index * 38;
ctx.fillStyle = row.color;
roundedRect(ctx, row.x, y - 12, 18, 18, 4);
ctx.fill();
ctx.fillStyle = "#5a6b74";
ctx.font = "650 27px system-ui, sans-serif";
ctx.fillText(
`${row.label} ${row.value}`,
row.x + 34,
y
);
});
ctx.strokeStyle = "#d9dfde";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(785, 290);
ctx.lineTo(785, 470);
ctx.stroke();
return canvasTexture(canvas);
}
// -------------------------------------------------------------------------
// Voxel classroom architecture
// -------------------------------------------------------------------------
const floorGeometry = new THREE.BoxGeometry(0.98, 0.12, 0.98);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95
});
const floorTiles = new THREE.InstancedMesh(
floorGeometry,
floorMaterial,
28 * 20
);
const floorDummy = new THREE.Object3D();
const floorColorA = new THREE.Color(0xe7dfd0);
const floorColorB = new THREE.Color(0xefe8da);
let floorIndex = 0;
for (let row = 0; row < 20; row++) {
for (let column = 0; column < 28; column++) {
floorDummy.position.set(
-13.5 + column,
-0.08,
-9.5 + row
);
floorDummy.updateMatrix();
floorTiles.setMatrixAt(floorIndex, floorDummy.matrix);
floorTiles.setColorAt(
floorIndex,
(row + column) % 2 === 0 ? floorColorA : floorColorB
);
floorIndex++;
}
}
floorTiles.instanceMatrix.needsUpdate = true;
floorTiles.instanceColor.needsUpdate = true;
floorTiles.receiveShadow = true;
scene.add(floorTiles);
// Back wall and trim.
makeBox(28, 7.4, 0.4, 0xe9e4d9, 0, 3.6, -10.2);
makeBox(28, 0.42, 0.48, 0x53636b, 0, 0.18, -9.97);
makeBox(28, 0.18, 0.56, 0xc8bbaa, 0, 6.95, -9.92);
// Whiteboard.
makeBox(12.7, 4.55, 0.18, 0x47575f, 0, 4.05, -9.9);
makeBox(12.15, 4.02, 0.1, 0xfffef9, 0, 4.05, -9.78);
const board = new THREE.Mesh(
new THREE.PlaneGeometry(11.75, 3.67),
new THREE.MeshBasicMaterial({
map: makeBoardTexture(),
toneMapped: false
})
);
board.position.set(0, 4.05, -9.715);
scene.add(board);
// Voxel windows.
for (const x of [-9.65, 9.65]) {
makeBox(4.45, 3.35, 0.18, 0x4d6069, x, 4.05, -9.91);
makeBox(3.92, 2.82, 0.1, 0xa9d6e9, x, 4.05, -9.79);
makeBox(0.12, 2.9, 0.12, 0xf5f0e7, x, 4.05, -9.72);
makeBox(4.0, 0.12, 0.12, 0xf5f0e7, x, 4.05, -9.72);
}
// Voxel clock.
makeBox(1.18, 1.18, 0.16, 0x35454e, 0, 6.76, -9.9);
makeBox(0.96, 0.96, 0.09, 0xfffdf7, 0, 6.76, -9.79);
makeBox(0.08, 0.33, 0.08, 0x35454e, 0.06, 6.85, -9.72);
makeBox(0.24, 0.08, 0.08, 0x35454e, -0.08, 6.71, -9.71);
// Teacher desk.
makeBox(3.4, 0.26, 1.45, 0x9d7557, 10.6, 1.42, -7.15);
makeBox(0.28, 1.35, 1.18, 0x735541, 9.25, 0.68, -7.15);
makeBox(0.28, 1.35, 1.18, 0x735541, 11.95, 0.68, -7.15);
makeBox(0.7, 0.8, 0.7, 0x3c4b54, 12.5, 0.38, -5.95);
// Bookshelf.
makeBox(2.1, 3.4, 0.65, 0x9b7658, -11.9, 1.7, -7.0);
for (const y of [0.55, 1.4, 2.25, 3.1]) {
makeBox(1.8, 0.12, 0.72, 0x745541, -11.9, y, -6.98);
}
const bookColors = [0x5968f2, 0xee9b35, 0x24ad82, 0xe8666c, 0x6d8794];
for (let shelf = 0; shelf < 3; shelf++) {
for (let book = 0; book < 5; book++) {
makeBox(
0.24,
0.48 + (book % 2) * 0.15,
0.46,
bookColors[(book + shelf) % bookColors.length],
-12.55 + book * 0.32,
0.88 + shelf * 0.85,
-6.9
);
}
}
// Voxel plant.
makeBox(1.05, 0.8, 1.05, 0xc78357, 12.4, 0.4, 3.0);
makeBox(0.18, 1.9, 0.18, 0x526d4b, 12.4, 1.55, 3.0);
makeBox(1.3, 0.55, 0.7, 0x5f8b59, 12.05, 2.0, 3.0);
makeBox(0.85, 0.6, 0.95, 0x76a568, 12.9, 2.35, 3.0);
makeBox(0.65, 0.5, 0.65, 0x4e7950, 12.2, 2.62, 3.0);
// -------------------------------------------------------------------------
// Exhibit platforms
// -------------------------------------------------------------------------
const groupedSet = new THREE.Group();
const pooledSet = new THREE.Group();
scene.add(groupedSet, pooledSet);
const groupedLayouts = {
A0: { x: -5.4, z: -3.5, cols: 10, rows: 8 },
B0: { x: 0.3, z: -4.45, cols: 10, rows: 2 },
A1: { x: -5.4, z: 0.8, cols: 10, rows: 2 },
B1: { x: 0.3, z: 0.0, cols: 10, rows: 8 }
};
const pooledLayouts = {
A: { x: -4.7, z: -0.7, cols: 10, rows: 10 },
B: { x: 4.7, z: -0.7, cols: 10, rows: 10 }
};
function addPlatform(
parent,
layout,
title,
subtitle,
accent,
labelY
) {
const width = layout.cols * 0.48 + 0.68;
const depth = layout.rows * 0.48 + 0.68;
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0xfffcf4,
roughness: 0.82,
transparent: true,
opacity: 1
});
const base = makeBox(
width,
0.2,
depth,
0xfffcf4,
layout.x,
0.08,
layout.z,
parent,
{ material: baseMaterial }
);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x80909a,
transparent: true,
opacity: 0.45
});
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(base.geometry),
edgeMaterial
);
edge.position.copy(base.position);
parent.add(edge);
const label = makeLabel(title, subtitle, accent, 5.25);
label.position.set(layout.x, labelY, layout.z);
parent.add(label);
return {
group: parent,
materials: [baseMaterial, edgeMaterial, label.material]
};
}
const groupedPlatforms = [
addPlatform(
groupedSet,
groupedLayouts.A0,
"PROGRAM A",
"COHORT 1 · 48 / 80 PASS",
DATA.A.color,
2.05
),
addPlatform(
groupedSet,
groupedLayouts.B0,
"PROGRAM B",
"COHORT 1 · 10 / 20 PASS",
DATA.B.color,
1.58
),
addPlatform(
groupedSet,
groupedLayouts.A1,
"PROGRAM A",
"COHORT 2 · 16 / 20 PASS",
DATA.A.color,
1.58
),
addPlatform(
groupedSet,
groupedLayouts.B1,
"PROGRAM B",
"COHORT 2 · 56 / 80 PASS",
DATA.B.color,
2.05
)
];
const pooledPlatforms = [
addPlatform(
pooledSet,
pooledLayouts.A,
"PROGRAM A",
"ALL LEARNERS · 64 / 100 PASS",
DATA.A.color,
2.05
),
addPlatform(
pooledSet,
pooledLayouts.B,
"PROGRAM B",
"ALL LEARNERS · 66 / 100 PASS",
DATA.B.color,
2.05
)
];
// -------------------------------------------------------------------------
// Learner voxels
// -------------------------------------------------------------------------
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 shuffledStatuses(count, passed, seed) {
const statuses = Array(count).fill(false);
for (let i = 0; i < passed; i++) {
statuses[i] = true;
}
const random = mulberry32(seed);
for (let i = statuses.length - 1; i > 0; i--) {
const j = Math.floor(random() * (i + 1));
[statuses[i], statuses[j]] = [statuses[j], statuses[i]];
}
return statuses;
}
function gridPosition(layout, index) {
const column = index % layout.cols;
const row = Math.floor(index / layout.cols);
return {
x: layout.x + (column - (layout.cols - 1) / 2) * 0.48,
z: layout.z + (row - (layout.rows - 1) / 2) * 0.48
};
}
const students = [];
for (const programId of ["A", "B"]) {
const program = DATA[programId];
let pooledIndex = 0;
program.cohorts.forEach((cohort, cohortIndex) => {
const statuses = shuffledStatuses(
cohort.count,
cohort.passed,
programId === "A" ? 1101 + cohortIndex : 2202 + cohortIndex
);
const groupedLayout = groupedLayouts[`${programId}${cohortIndex}`];
const pooledLayout = pooledLayouts[programId];
statuses.forEach((passed, localIndex) => {
students.push({
programId,
cohortIndex,
passed,
grouped: gridPosition(groupedLayout, localIndex),
pooled: gridPosition(pooledLayout, pooledIndex),
current: { ...gridPosition(groupedLayout, localIndex) }
});
pooledIndex++;
});
});
}
const learnerGeometry = new THREE.BoxGeometry(0.32, 0.42, 0.32);
const learnerMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.62,
metalness: 0.02
});
const outcomeGeometry = new THREE.BoxGeometry(0.21, 0.14, 0.21);
const outcomeMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.45,
metalness: 0.02
});
const learnerMesh = new THREE.InstancedMesh(
learnerGeometry,
learnerMaterial,
students.length
);
const outcomeMesh = new THREE.InstancedMesh(
outcomeGeometry,
outcomeMaterial,
students.length
);
learnerMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
outcomeMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
learnerMesh.castShadow = true;
learnerMesh.receiveShadow = true;
outcomeMesh.castShadow = true;
const passColor = new THREE.Color(0x24ad82);
const failColor = new THREE.Color(0xe8666c);
students.forEach((student, index) => {
learnerMesh.setColorAt(
index,
new THREE.Color(DATA[student.programId].color)
);
outcomeMesh.setColorAt(
index,
student.passed ? passColor : failColor
);
});
learnerMesh.instanceColor.needsUpdate = true;
outcomeMesh.instanceColor.needsUpdate = true;
scene.add(learnerMesh, outcomeMesh);
const instanceDummy = new THREE.Object3D();
function updateLearnerMatrices(time) {
students.forEach((student, index) => {
const float = reducedMotion
? 0
: Math.sin(time * 0.002 + index * 0.83) * 0.022;
instanceDummy.position.set(
student.current.x,
0.43 + float,
student.current.z
);
instanceDummy.rotation.set(0, 0, 0);
instanceDummy.updateMatrix();
learnerMesh.setMatrixAt(index, instanceDummy.matrix);
instanceDummy.position.y = 0.72 + float;
instanceDummy.updateMatrix();
outcomeMesh.setMatrixAt(index, instanceDummy.matrix);
});
learnerMesh.instanceMatrix.needsUpdate = true;
outcomeMesh.instanceMatrix.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Mode transition
// -------------------------------------------------------------------------
let mode = "grouped";
let transition = null;
function switchMode(nextMode) {
if (nextMode === mode) return;
mode = nextMode;
const start = performance.now();
const duration = reducedMotion ? 1 : 1250;
transition = {
start,
duration,
students: students.map((student) => ({
from: { ...student.current },
to: nextMode === "grouped"
? { ...student.grouped }
: { ...student.pooled }
}))
};
groupedButton.classList.toggle("active", mode === "grouped");
pooledButton.classList.toggle("active", mode === "pooled");
groupedButton.setAttribute("aria-pressed", String(mode === "grouped"));
pooledButton.setAttribute("aria-pressed", String(mode === "pooled"));
groupedMetrics.classList.toggle("active", mode === "grouped");
pooledMetrics.classList.toggle("active", mode === "pooled");
if (mode === "grouped") {
insightTitle.textContent = "Program A leads inside each cohort.";
insightText.textContent =
"A beats B in both groups. The cohort composition is what changes the result after pooling.";
} else {
insightTitle.textContent = "Program B leads after pooling.";
insightText.textContent =
"A is concentrated in Cohort 1, while B is concentrated in Cohort 2. The averages now favor B.";
}
focusCameraForMode();
}
groupedButton.addEventListener("click", () => switchMode("grouped"));
pooledButton.addEventListener("click", () => switchMode("pooled"));
// -------------------------------------------------------------------------
// Custom camera controls: orbit, zoom, pan and keyboard input
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(-2.35, 0.7, -1.4),
desiredTarget: new THREE.Vector3(-2.35, 0.7, -1.4),
radius: 20,
desiredRadius: 20,
theta: 0.55,
desiredTheta: 0.55,
phi: 0.88,
desiredPhi: 0.88
};
const groupFocus = new THREE.Vector3(-2.35, 0.7, -1.4);
const poolFocus = new THREE.Vector3(0, 0.7, -0.25);
function focusCameraForMode() {
const target = mode === "grouped" ? groupFocus : poolFocus;
cameraState.desiredTarget.copy(target);
cameraState.desiredRadius = mode === "grouped" ? 20 : 19.5;
cameraState.desiredTheta = 0.55;
cameraState.desiredPhi = 0.88;
}
function resetCamera() {
focusCameraForMode();
}
resetButton.addEventListener("click", resetCamera);
sceneCanvas.addEventListener("dblclick", resetCamera);
let dragging = false;
let draggingButton = 0;
let lastPointerX = 0;
let lastPointerY = 0;
sceneCanvas.addEventListener("pointerdown", (event) => {
dragging = true;
draggingButton = event.button;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
sceneCanvas.setPointerCapture(event.pointerId);
sceneCanvas.style.cursor = "grabbing";
});
sceneCanvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
const panning = event.shiftKey || draggingButton === 2;
if (panning) {
const right = new THREE.Vector3(
Math.cos(cameraState.desiredTheta),
0,
-Math.sin(cameraState.desiredTheta)
);
const forward = new THREE.Vector3(
-Math.sin(cameraState.desiredTheta),
0,
-Math.cos(cameraState.desiredTheta)
);
const scale = cameraState.desiredRadius * 0.0011;
cameraState.desiredTarget.addScaledVector(right, -dx * scale);
cameraState.desiredTarget.addScaledVector(forward, -dy * scale);
} else {
cameraState.desiredTheta -= dx * 0.006;
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi - dy * 0.005,
0.35,
1.36
);
}
});
function endDrag(event) {
dragging = false;
sceneCanvas.style.cursor = "grab";
if (event.pointerId !== undefined &&
sceneCanvas.hasPointerCapture(event.pointerId)) {
sceneCanvas.releasePointerCapture(event.pointerId);
}
}
sceneCanvas.addEventListener("pointerup", endDrag);
sceneCanvas.addEventListener("pointercancel", endDrag);
sceneCanvas.addEventListener("contextmenu", (event) => event.preventDefault());
sceneCanvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius * Math.exp(event.deltaY * 0.001),
10,
35
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") cameraState.desiredTheta += 0.08;
if (key === "arrowright") cameraState.desiredTheta -= 0.08;
if (key === "arrowup") {
cameraState.desiredPhi = Math.max(
0.35,
cameraState.desiredPhi - 0.06
);
}
if (key === "arrowdown") {
cameraState.desiredPhi = Math.min(
1.36,
cameraState.desiredPhi + 0.06
);
}
if (key === "+" || key === "=") {
cameraState.desiredRadius = Math.max(
10,
cameraState.desiredRadius - 1
);
}
if (key === "-" || key === "_") {
cameraState.desiredRadius = Math.min(
35,
cameraState.desiredRadius + 1
);
}
if (key === "g") switchMode("grouped");
if (key === "p") switchMode("pooled");
if (key === "r") resetCamera();
});
// -------------------------------------------------------------------------
// Platform fading and animation loop
// -------------------------------------------------------------------------
const setState = [
{ objects: groupedPlatforms, opacity: 1, target: 1 },
{ objects: pooledPlatforms, opacity: 0, target: 0 }
];
function updatePlatformSets(delta) {
const factor = reducedMotion ? 1 : 1 - Math.exp(-delta * 5);
setState.forEach((state) => {
state.opacity = THREE.MathUtils.lerp(
state.opacity,
state.target,
factor
);
state.objects.forEach((object) => {
object.materials.forEach((material) => {
material.opacity =
state.opacity * (material === object.materials[1] ? 0.45 : 1);
});
});
state.objects[0].group.visible = state.opacity > 0.005;
});
}
function updateTransition(time) {
if (!transition) return;
const raw = THREE.MathUtils.clamp(
(time - transition.start) / transition.duration,
0,
1
);
const eased =
raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
transition.students.forEach((state, index) => {
const student = students[index];
student.current.x = THREE.MathUtils.lerp(
state.from.x,
state.to.x,
eased
);
student.current.z = THREE.MathUtils.lerp(
state.from.z,
state.to.z,
eased
);
});
if (raw >= 1) transition = null;
}
const clock = new THREE.Clock();
function animate(time) {
const delta = Math.min(clock.getDelta(), 0.05);
updateTransition(time);
updatePlatformSets(delta);
updateLearnerMatrices(time);
const cameraFactor = reducedMotion ? 1 : 1 - Math.exp(-delta * 5);
cameraState.target.lerp(
cameraState.desiredTarget,
cameraFactor
);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraState.desiredRadius,
cameraFactor
);
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
cameraState.desiredTheta,
cameraFactor
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
cameraState.desiredPhi,
cameraFactor
);
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraState.target);
camera.lookAt(cameraState.target);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
// -------------------------------------------------------------------------
// Resize handling
// -------------------------------------------------------------------------
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight, false);
}
window.addEventListener("resize", resize);
sceneCanvas.style.cursor = "grab";
</script>
</body>
</html>
```