Prompt specification
Build or customize the Three.js 3D scene "Rare-event horizon exploration tool" 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: Rare-event horizon exploration tool
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/rare-event-horizon-exploration-tool-x1824
Scene Description Prompt:
Create a voxel independent-Bernoulli explorer comparing expected event count with at-least-one probability as probability and trial count change. One HTML file, Three.js from cdnjs.
Working Three.js Starter (rare-event-horizon-exploration-tool-x1824):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#080d19" />
<title>Voxel Bernoulli Explorer</title>
<style>
:root {
color-scheme: dark;
--bg: #080d19;
--panel: #0d1424;
--panel-2: #111a2c;
--line: #23304a;
--line-bright: #334563;
--text: #edf4ff;
--muted: #8fa2bd;
--cyan: #4be3cf;
--cyan-soft: rgba(75, 227, 207, 0.14);
--amber: #ffbf69;
--amber-soft: rgba(255, 191, 105, 0.14);
--purple: #9b8cff;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
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 {
height: 100vh;
display: grid;
grid-template-rows: 74px minmax(0, 1fr);
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 24px;
border-bottom: 1px solid var(--line);
background:
linear-gradient(90deg, rgba(75, 227, 207, 0.055), transparent 32%),
#0a101e;
position: relative;
z-index: 20;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 38px;
height: 38px;
position: relative;
flex: 0 0 auto;
}
.brand-mark i {
position: absolute;
width: 13px;
height: 13px;
border-radius: 3px;
background: var(--cyan);
box-shadow: 0 0 18px rgba(75, 227, 207, 0.36);
}
.brand-mark i:nth-child(1) {
left: 1px;
top: 2px;
opacity: 0.95;
}
.brand-mark i:nth-child(2) {
right: 1px;
top: 2px;
background: var(--amber);
box-shadow: 0 0 18px rgba(255, 191, 105, 0.3);
}
.brand-mark i:nth-child(3) {
left: 11px;
bottom: 2px;
background: var(--purple);
box-shadow: 0 0 18px rgba(155, 140, 255, 0.28);
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
margin-bottom: 3px;
}
h1 {
margin: 0;
font-size: clamp(16px, 1.5vw, 21px);
line-height: 1.1;
letter-spacing: -0.025em;
white-space: nowrap;
}
.header-note {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 12px;
white-space: nowrap;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.workspace {
min-height: 0;
display: grid;
grid-template-columns: 340px minmax(0, 1fr);
}
.sidebar {
min-height: 0;
overflow-y: auto;
padding: 22px 20px 28px;
border-right: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(17, 26, 44, 0.72), rgba(10, 16, 30, 0.96)),
var(--panel);
scrollbar-width: thin;
scrollbar-color: #30405d transparent;
}
.section {
padding-bottom: 21px;
margin-bottom: 21px;
border-bottom: 1px solid rgba(35, 48, 74, 0.78);
}
.section:last-child {
border-bottom: 0;
margin-bottom: 0;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 17px;
}
.section-title {
margin: 0;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
color: #cbd8eb;
}
.micro {
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.control + .control {
margin-top: 24px;
}
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.control-label {
font-size: 13px;
font-weight: 700;
}
.control-label span {
color: var(--muted);
font-weight: 500;
}
.value-box {
min-width: 74px;
padding: 7px 9px;
border: 1px solid var(--line-bright);
border-radius: 8px;
background: #0a111f;
color: var(--cyan);
font-size: 13px;
font-weight: 800;
text-align: center;
font-variant-numeric: tabular-nums;
}
.value-box.amber {
color: var(--amber);
}
input[type="range"] {
--fill: 0%;
width: 100%;
height: 5px;
margin: 5px 0 11px;
border-radius: 99px;
outline: none;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(
to right,
var(--cyan) 0,
var(--cyan) var(--fill),
#243047 var(--fill),
#243047 100%
);
cursor: pointer;
}
input[type="range"].amber {
background: linear-gradient(
to right,
var(--amber) 0,
var(--amber) var(--fill),
#243047 var(--fill),
#243047 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
border: 3px solid #0b1220;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(75, 227, 207, 0.7), 0 0 13px rgba(75, 227, 207, 0.28);
}
input[type="range"].amber::-webkit-slider-thumb {
background: var(--amber);
box-shadow: 0 0 0 1px rgba(255, 191, 105, 0.7), 0 0 13px rgba(255, 191, 105, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border-radius: 50%;
border: 3px solid #0b1220;
background: var(--cyan);
}
input[type="range"].amber::-moz-range-thumb {
background: var(--amber);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #647792;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.chips {
display: flex;
gap: 6px;
flex-wrap: wrap;
margin-top: 11px;
}
.chip,
.small-button {
border: 1px solid var(--line);
border-radius: 7px;
background: #111a2b;
color: #aebed4;
padding: 7px 9px;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: 160ms ease;
}
.chip:hover,
.small-button:hover {
color: var(--text);
border-color: var(--line-bright);
background: #172238;
}
.chip.active {
color: var(--cyan);
border-color: rgba(75, 227, 207, 0.5);
background: var(--cyan-soft);
}
.scenario {
width: 100%;
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 10px;
padding: 10px 11px;
margin-top: 8px;
text-align: left;
border: 1px solid var(--line);
border-radius: 9px;
background: #0e1728;
cursor: pointer;
transition: 160ms ease;
}
.scenario:hover {
transform: translateY(-1px);
border-color: #3b4f70;
background: #121d31;
}
.scenario strong {
display: block;
font-size: 12px;
margin-bottom: 3px;
}
.scenario small {
display: block;
color: var(--muted);
font-size: 10px;
}
.scenario-tag {
padding: 4px 6px;
border-radius: 5px;
background: #18243a;
color: #9fb0c9;
font-size: 9px;
font-weight: 800;
white-space: nowrap;
}
.chart-wrap {
position: relative;
width: 100%;
height: 165px;
border: 1px solid var(--line);
border-radius: 10px;
overflow: hidden;
background: #09101e;
}
#curve {
width: 100%;
height: 100%;
display: block;
}
.chart-legend {
display: flex;
gap: 13px;
flex-wrap: wrap;
margin-top: 9px;
color: var(--muted);
font-size: 10px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend-line {
width: 17px;
height: 2px;
background: var(--cyan);
}
.legend-line.amber {
background: repeating-linear-gradient(
90deg,
var(--amber) 0 5px,
transparent 5px 8px
);
}
.formula {
padding: 11px 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: #0a1220;
margin-top: 10px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
color: #c7d5e9;
line-height: 1.8;
}
.formula .cyan {
color: var(--cyan);
}
.formula .amber {
color: var(--amber);
}
.note {
display: flex;
gap: 9px;
padding: 11px;
border-left: 2px solid var(--purple);
background: rgba(155, 140, 255, 0.055);
color: #aebbd0;
font-size: 11px;
line-height: 1.55;
}
.note-icon {
flex: 0 0 auto;
color: var(--purple);
font-weight: 900;
}
.stage {
min-width: 0;
min-height: 0;
position: relative;
overflow: hidden;
background:
radial-gradient(circle at 64% 42%, rgba(42, 70, 105, 0.18), transparent 36%),
linear-gradient(180deg, #0b1221, #070b15);
}
#three-canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.metrics {
position: absolute;
top: 18px;
left: 18px;
display: flex;
gap: 10px;
z-index: 5;
pointer-events: none;
}
.metric-card {
min-width: 174px;
padding: 13px 15px;
border: 1px solid rgba(74, 93, 125, 0.62);
border-radius: 11px;
background: rgba(10, 16, 29, 0.83);
backdrop-filter: blur(12px);
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.16);
}
.metric-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
margin-bottom: 5px;
}
.metric-number {
font-size: 25px;
line-height: 1;
font-weight: 800;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.metric-card.expected .metric-number {
color: var(--amber);
}
.metric-card.atleast .metric-number {
color: var(--cyan);
}
.metric-detail {
margin-top: 6px;
color: #71849f;
font-size: 10px;
}
.scene-actions {
position: absolute;
top: 18px;
right: 18px;
display: flex;
gap: 7px;
z-index: 6;
}
.scene-button {
border: 1px solid rgba(74, 93, 125, 0.7);
background: rgba(10, 16, 29, 0.86);
backdrop-filter: blur(10px);
border-radius: 8px;
padding: 8px 10px;
color: #aebed4;
font-size: 10px;
font-weight: 800;
cursor: pointer;
transition: 160ms ease;
}
.scene-button:hover,
.scene-button.active {
color: var(--cyan);
border-color: rgba(75, 227, 207, 0.52);
background: rgba(75, 227, 207, 0.1);
}
.scene-legend {
position: absolute;
left: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 15px;
padding: 10px 12px;
border: 1px solid rgba(74, 93, 125, 0.55);
border-radius: 9px;
background: rgba(10, 16, 29, 0.82);
backdrop-filter: blur(10px);
z-index: 5;
pointer-events: none;
}
.voxel-key {
display: flex;
align-items: center;
gap: 7px;
color: #aebed4;
font-size: 10px;
}
.cube-key {
width: 11px;
height: 11px;
border-radius: 3px;
background: var(--cyan);
box-shadow: 0 0 10px rgba(75, 227, 207, 0.35);
}
.cube-key.fail {
background: #293852;
box-shadow: none;
}
.sample-count {
color: #71849f;
font-size: 10px;
border-left: 1px solid var(--line);
padding-left: 14px;
}
.insight {
position: absolute;
right: 18px;
bottom: 18px;
max-width: min(430px, 48%);
padding: 11px 13px;
border: 1px solid rgba(74, 93, 125, 0.55);
border-radius: 9px;
background: rgba(10, 16, 29, 0.82);
backdrop-filter: blur(10px);
color: #91a3be;
font-size: 10px;
line-height: 1.55;
z-index: 5;
pointer-events: none;
}
.insight strong {
color: #d9e5f5;
}
.camera-hint {
position: absolute;
left: 50%;
top: 17px;
transform: translateX(-50%);
color: rgba(161, 177, 202, 0.58);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
z-index: 4;
}
.tooltip {
position: absolute;
display: none;
z-index: 12;
min-width: 120px;
padding: 8px 10px;
border: 1px solid #405471;
border-radius: 7px;
background: rgba(8, 13, 24, 0.94);
color: #dce7f7;
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
font-size: 10px;
line-height: 1.5;
pointer-events: none;
}
.tooltip strong {
color: var(--cyan);
}
.loading {
position: absolute;
inset: 0;
display: grid;
place-items: center;
z-index: 30;
color: var(--muted);
background: #080d19;
font-size: 12px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
@media (max-width: 1050px) {
.workspace {
grid-template-columns: 305px minmax(0, 1fr);
}
.camera-hint,
.header-note {
display: none;
}
.metric-card {
min-width: 145px;
}
.insight {
max-width: 55%;
}
}
@media (max-width: 780px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100vh;
grid-template-rows: 66px auto;
}
.topbar {
padding: 0 15px;
}
.workspace {
display: flex;
flex-direction: column;
}
.stage {
order: 1;
height: 66vh;
min-height: 520px;
}
.sidebar {
order: 2;
overflow: visible;
border-right: 0;
border-top: 1px solid var(--line);
}
.metrics {
top: 12px;
left: 12px;
right: 12px;
gap: 7px;
}
.metric-card {
flex: 1 1 0;
min-width: 0;
padding: 10px 11px;
}
.metric-number {
font-size: 20px;
}
.scene-actions {
top: 82px;
right: 12px;
}
.scene-legend {
left: 12px;
bottom: 12px;
}
.insight {
display: none;
}
.camera-hint {
display: none;
}
}
@media (max-width: 480px) {
.brand-mark {
display: none;
}
.metric-detail {
display: none;
}
.scene-legend {
gap: 10px;
}
.sample-count {
padding-left: 9px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i>
</div>
<div>
<div class="eyebrow">Independent Bernoulli trials</div>
<h1>Voxel Probability Explorer</h1>
</div>
</div>
<div class="header-note">
<span class="status-dot"></span>
Live deterministic model · interactive sample realization
</div>
</header>
<div class="workspace">
<aside class="sidebar">
<section class="section">
<div class="section-heading">
<h2 class="section-title">Parameters</h2>
<button class="small-button" id="resetParameters" type="button">Reset</button>
</div>
<div class="control">
<div class="control-head">
<label class="control-label" for="probability">
Success probability <span>p</span>
</label>
<output class="value-box" id="probabilityValue">20%</output>
</div>
<input
id="probability"
type="range"
min="0"
max="1"
step="0.001"
value="0.2"
aria-label="Success probability"
/>
<div class="range-labels">
<span>0%</span>
<span>50%</span>
<span>100%</span>
</div>
<div class="chips" aria-label="Probability presets">
<button class="chip" type="button" data-p="0.05">5%</button>
<button class="chip active" type="button" data-p="0.2">20%</button>
<button class="chip" type="button" data-p="0.5">50%</button>
<button class="chip" type="button" data-p="0.8">80%</button>
</div>
</div>
<div class="control">
<div class="control-head">
<label class="control-label" for="trials">
Trial count <span>n</span>
</label>
<output class="value-box amber" id="trialsValue">32</output>
</div>
<input
id="trials"
class="amber"
type="range"
min="1"
max="120"
step="1"
value="32"
aria-label="Number of trials"
/>
<div class="range-labels">
<span>1</span>
<span>60</span>
<span>120</span>
</div>
<div class="chips" aria-label="Trial count presets">
<button class="chip" type="button" data-n="1">1</button>
<button class="chip" type="button" data-n="12">12</button>
<button class="chip active" type="button" data-n="32">32</button>
<button class="chip" type="button" data-n="96">96</button>
</div>
</div>
</section>
<section class="section">
<div class="section-heading">
<h2 class="section-title">Scenario Presets</h2>
</div>
<button class="scenario" type="button" data-p="0.02" data-n="10">
<span>
<strong>Rare event</strong>
<small>p = 2% · n = 10</small>
</span>
<span class="scenario-tag">LOW p</span>
</button>
<button class="scenario" type="button" data-p="0.2" data-n="40">
<span>
<strong>Frequent encounters</strong>
<small>p = 20% · n = 40</small>
</span>
<span class="scenario-tag">SCALE</span>
</button>
<button class="scenario" type="button" data-p="0.5" data-n="64">
<span>
<strong>Coin-flip field</strong>
<small>p = 50% · n = 64</small>
</span>
<span class="scenario-tag">BALANCED</span>
</button>
<button class="scenario" type="button" data-p="0.9" data-n="24">
<span>
<strong>Near certainty</strong>
<small>p = 90% · n = 24</small>
</span>
<span class="scenario-tag">HIGH p</span>
</button>
</section>
<section class="section">
<div class="section-heading">
<h2 class="section-title">Normalized Evolution</h2>
<span class="micro">0 to 1</span>
</div>
<div class="chart-wrap">
<canvas id="curve" aria-label="Chart comparing normalized expected fraction and probability of at least one success"></canvas>
</div>
<div class="chart-legend">
<span class="legend-item">
<span class="legend-line"></span>
P(at least one)
</span>
<span class="legend-item">
<span class="legend-line amber"></span>
E[X] / n
</span>
</div>
<div class="formula">
<div><span class="amber">E[X]</span> = n · p</div>
<div><span class="cyan">P(X ≥ 1)</span> = 1 − (1 − p)<sup>n</sup></div>
</div>
</section>
<section class="section">
<div class="note">
<span class="note-icon">i</span>
<span>
The towers use separate semantic scales. Expected count is shown out of
<strong>n possible events</strong>; at-least-one probability is shown out of
<strong>100%</strong>.
</span>
</div>
</section>
</aside>
<main class="stage" id="stage">
<div class="loading" id="loading">Building voxel field…</div>
<div class="metrics">
<div class="metric-card expected">
<div class="metric-label">Expected event count</div>
<div class="metric-number" id="expectedMetric">6.40</div>
<div class="metric-detail" id="expectedDetail">out of 32 possible</div>
</div>
<div class="metric-card atleast">
<div class="metric-label">At least one probability</div>
<div class="metric-number" id="probabilityMetric">99.97%</div>
<div class="metric-detail" id="probabilityDetail">chance of any success</div>
</div>
</div>
<div class="camera-hint">Drag to orbit · scroll to zoom · double-click to reset</div>
<div class="scene-actions">
<button class="scene-button" id="shuffleSample" type="button">Shuffle sample</button>
<button class="scene-button" id="toggleOrbit" type="button">Auto orbit</button>
<button class="scene-button" id="resetCamera" type="button">Reset view</button>
</div>
<div class="scene-legend">
<span class="voxel-key">
<span class="cube-key"></span>
sample success
</span>
<span class="voxel-key">
<span class="cube-key fail"></span>
sample failure
</span>
<span class="sample-count" id="sampleCount">Sample: 0 / 32</span>
</div>
<div class="insight" id="insight"></div>
<div class="tooltip" id="tooltip"></div>
</main>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const state = {
p: 0.2,
n: 32,
seed: 7319,
autoOrbit: false
};
const probabilityInput = $("#probability");
const trialsInput = $("#trials");
const probabilityValue = $("#probabilityValue");
const trialsValue = $("#trialsValue");
const expectedMetric = $("#expectedMetric");
const expectedDetail = $("#expectedDetail");
const probabilityMetric = $("#probabilityMetric");
const probabilityDetail = $("#probabilityDetail");
const sampleCount = $("#sampleCount");
const insight = $("#insight");
const tooltip = $("#tooltip");
const stage = $("#stage");
const clamp = (v, min, max) => Math.min(max, Math.max(min, v));
const lerp = (a, b, t) => a + (b - a) * t;
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 expectedCount() {
return state.n * state.p;
}
function atLeastOneProbability() {
if (state.p <= 0) return 0;
if (state.p >= 1) return 1;
return 1 - Math.pow(1 - state.p, state.n);
}
function formatCount(value) {
if (value === 0) return "0";
if (value < 0.01) return value.toFixed(3);
if (Number.isInteger(value)) return String(value);
return value.toFixed(2);
}
function formatProbability(value) {
if (value <= 0) return "0%";
if (value >= 1) return "100%";
if (value < 0.0001) return "<0.01%";
if (value > 0.9999) return ">99.99%";
return (value * 100).toFixed(2) + "%";
}
function formatPercent(value) {
const percent = value * 100;
if (percent === 0) return "0%";
if (percent === 100) return "100%";
if (percent < 1) return percent.toFixed(1) + "%";
return percent.toFixed(0) + "%";
}
// ---------------------------------------------------------------------
// Three.js scene
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080d19);
scene.fog = new THREE.FogExp2(0x080d19, 0.022);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
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.id = "three-canvas";
renderer.domElement.setAttribute("aria-label", "Interactive 3D voxel visualization");
stage.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc9e8ff, 0x101525, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.5);
keyLight.position.set(3, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.bias = -0.001;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x39e4cc, 17, 9, 2);
cyanLight.position.set(7.5, 3.6, 2.8);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xffad54, 12, 8, 2);
amberLight.position.set(3.7, 2.8, 2.4);
scene.add(amberLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(34, 24),
new THREE.MeshStandardMaterial({
color: 0x0b1220,
roughness: 0.9,
metalness: 0.12
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.16;
floor.receiveShadow = true;
scene.add(floor);
const floorGrid = new THREE.GridHelper(28, 28, 0x2b3b57, 0x18243a);
floorGrid.position.y = -0.145;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.42;
scene.add(floorGrid);
const unitBoxGeometry = new THREE.BoxGeometry(1, 1, 1);
const voxelGeometry = new THREE.BoxGeometry(0.42, 0.42, 0.42);
const voxelEdgeGeometry = new THREE.EdgesGeometry(voxelGeometry);
const voxelEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x7691b3,
transparent: true,
opacity: 0.22
});
const trialGroup = new THREE.Group();
scene.add(trialGroup);
let trialMeshes = [];
let trialUniforms = [];
let gridBase = null;
let gridBorder = null;
let gridDimensions = { width: 1, depth: 1 };
function createSpriteCanvas(width, height) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 30;
return { canvas, context, texture, sprite };
}
function createTowerLabel() {
const label = createSpriteCanvas(900, 310);
label.sprite.scale.set(3.65, 1.26, 1);
scene.add(label.sprite);
label.update = (title, value, sub, color) => {
const ctx = label.context;
const w = label.canvas.width;
const h = label.canvas.height;
ctx.clearRect(0, 0, w, h);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.font = "800 31px system-ui, sans-serif";
ctx.fillText(title, w / 2, 47);
ctx.fillStyle = "#f4f8ff";
ctx.font = "800 86px system-ui, sans-serif";
ctx.fillText(value, w / 2, 137);
ctx.fillStyle = "#8296b3";
ctx.font = "600 25px system-ui, sans-serif";
ctx.fillText(sub, w / 2, 220);
ctx.strokeStyle = color;
ctx.globalAlpha = 0.42;
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(w * 0.29, 272);
ctx.lineTo(w * 0.71, 272);
ctx.stroke();
ctx.globalAlpha = 1;
label.texture.needsUpdate = true;
};
return label;
}
function createSmallLabel(text, color = "#8195b2", scale = 2.5) {
const label = createSpriteCanvas(700, 130);
label.sprite.scale.set(scale, scale * 130 / 700, 1);
label.update = (nextText, nextColor = color) => {
const ctx = label.context;
ctx.clearRect(0, 0, label.canvas.width, label.canvas.height);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = nextColor;
ctx.font = "700 48px system-ui, sans-serif";
ctx.fillText(nextText, label.canvas.width / 2, label.canvas.height / 2);
label.texture.needsUpdate = true;
};
label.update(text);
return label;
}
const matrixLabel = createSmallLabel("32 INDEPENDENT TRIALS", "#91a5c2", 3.65);
matrixLabel.sprite.position.set(-3.2, 1.35, 0);
scene.add(matrixLabel.sprite);
const BAR_BASE = 0.23;
const BAR_HEIGHT = 3.9;
const SLOT_COUNT = 12;
const slotGeometry = new THREE.BoxGeometry(0.72, 0.245, 0.72);
function createTower(x, color, title) {
const group = new THREE.Group();
group.position.x = x;
scene.add(group);
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x121c2f,
roughness: 0.55,
metalness: 0.35
});
const base = new THREE.Mesh(unitBoxGeometry, baseMaterial);
base.scale.set(1.35, 0.16, 1.35);
base.position.y = 0.05;
base.receiveShadow = true;
group.add(base);
const baseEdge = new THREE.LineSegments(
new THREE.EdgesGeometry(unitBoxGeometry),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.34
})
);
baseEdge.scale.copy(base.scale);
baseEdge.position.copy(base.position);
group.add(baseEdge);
for (let i = 0; i < SLOT_COUNT; i++) {
const slot = new THREE.Mesh(
slotGeometry,
new THREE.MeshBasicMaterial({
color,
wireframe: true,
transparent: true,
opacity: 0.085,
depthWrite: false
})
);
slot.position.y = BAR_BASE + (i + 0.5) * (BAR_HEIGHT / SLOT_COUNT);
group.add(slot);
}
const barMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.52,
roughness: 0.36,
metalness: 0.22,
transparent: true,
opacity: 0.94
});
const bar = new THREE.Mesh(new THREE.BoxGeometry(0.64, 1, 0.64), barMaterial);
bar.castShadow = true;
bar.receiveShadow = true;
bar.scale.y = 0.025;
bar.position.y = BAR_BASE + 0.0125;
group.add(bar);
const capMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: color,
emissiveIntensity: 1.4,
roughness: 0.25,
metalness: 0.15
});
const cap = new THREE.Mesh(new THREE.BoxGeometry(0.78, 0.11, 0.78), capMaterial);
cap.position.y = BAR_BASE + 0.08;
group.add(cap);
const label = createTowerLabel();
label.sprite.position.set(x, 5.28, 0);
const axisLabel = createSmallLabel("", "#687c98", 2.55);
axisLabel.sprite.position.set(x, 0.12, 1.18);
scene.add(axisLabel.sprite);
return {
group,
bar,
cap,
label,
axisLabel,
current: 0,
target: 0,
title
};
}
const expectedTower = createTower(3.75, 0xffb75f, "EXPECTED COUNT");
const probabilityTower = createTower(7.1, 0x4be3cf, "AT LEAST ONE");
const connectorGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.2, -0.08, 0.8),
new THREE.Vector3(8.5, -0.08, 0.8)
]);
const connector = new THREE.Line(
connectorGeometry,
new THREE.LineBasicMaterial({
color: 0x355071,
transparent: true,
opacity: 0.38
})
);
scene.add(connector);
// Direct camera controls: no external controls addon.
const cameraTarget = new THREE.Vector3(0.35, 2.0, 0);
const desired = {
yaw: 0.1,
pitch: 0.39,
distance: 19.0
};
const current = {
yaw: desired.yaw,
pitch: desired.pitch,
distance: desired.distance
};
function resetCamera() {
desired.yaw = 0.1;
desired.pitch = 0.39;
desired.distance = 19.0;
}
function updateCamera(dt) {
if (state.autoOrbit) {
desired.yaw += dt * 0.13;
}
const damping = 1 - Math.exp(-dt * 9);
current.yaw = lerp(current.yaw, desired.yaw, damping);
current.pitch = lerp(current.pitch, desired.pitch, damping);
current.distance = lerp(current.distance, desired.distance, damping);
const aspect = camera.aspect || 1;
const fit = Math.max(1, 1.18 / aspect);
const distance = current.distance * fit;
const horizontal = Math.cos(current.pitch) * distance;
camera.position.set(
cameraTarget.x + Math.sin(current.yaw) * horizontal,
cameraTarget.y + Math.sin(current.pitch) * distance,
cameraTarget.z + Math.cos(current.yaw) * horizontal
);
camera.lookAt(cameraTarget);
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = false;
let previousPointer = { x: 0, y: 0 };
let hoveredTrial = null;
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function clearHover() {
if (hoveredTrial) {
hoveredTrial.scale.setScalar(1);
hoveredTrial = null;
}
tooltip.style.display = "none";
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = true;
pointerMoved = false;
previousPointer = { x: event.clientX, y: event.clientY };
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
if (Math.abs(dx) + Math.abs(dy) > 1) pointerMoved = true;
desired.yaw -= dx * 0.006;
desired.pitch = clamp(desired.pitch + dy * 0.005, 0.08, 1.08);
previousPointer = { x: event.clientX, y: event.clientY };
if (pointerMoved) clearHover();
return;
}
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(trialMeshes, false)[0];
if (!hit) {
clearHover();
renderer.domElement.style.cursor = "grab";
return;
}
if (hoveredTrial && hoveredTrial !== hit.object) {
hoveredTrial.scale.setScalar(1);
}
hoveredTrial = hit.object;
hoveredTrial.scale.setScalar(1.12);
renderer.domElement.style.cursor = "pointer";
const index = hoveredTrial.userData.index;
const success = hoveredTrial.userData.uniform < state.p;
const rect = stage.getBoundingClientRect();
tooltip.innerHTML = `
<strong>Trial ${index + 1}</strong><br>
${success ? "Success in this sample" : "Failure in this sample"}<br>
P(success) = ${formatPercent(state.p)}
`;
tooltip.style.display = "block";
tooltip.style.left = clamp(event.clientX - rect.left + 13, 8, rect.width - 150) + "px";
tooltip.style.top = clamp(event.clientY - rect.top + 13, 8, rect.height - 70) + "px";
});
function endPointer(event) {
pointerDown = false;
renderer.domElement.style.cursor = "grab";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerDown) clearHover();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.distance = clamp(
desired.distance * Math.exp(event.deltaY * 0.001),
10,
30
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ---------------------------------------------------------------------
// Trial voxel field
// ---------------------------------------------------------------------
function createUniforms(count) {
const random = mulberry32(state.seed);
trialUniforms = Array.from({ length: count }, () => random());
}
function rebuildTrials() {
for (const mesh of trialMeshes) {
mesh.material.dispose();
}
trialMeshes = [];
if (gridBase) {
trialGroup.remove(gridBase);
gridBase = null;
}
if (gridBorder) {
trialGroup.remove(gridBorder);
gridBorder.geometry.dispose();
gridBorder.material.dispose();
gridBorder = null;
}
createUniforms(state.n);
const columns = state.n <= 12 ? state.n : state.n <= 48 ? 8 : 12;
const rows = Math.ceil(state.n / columns);
const spacing = 0.56;
const centerX = -3.2;
const width = Math.max(1, (columns - 1) * spacing + 0.9);
const depth = Math.max(1, (rows - 1) * spacing + 0.9);
gridDimensions = { width, depth };
gridBase = new THREE.Mesh(
unitBoxGeometry,
new THREE.MeshStandardMaterial({
color: 0x111c2e,
roughness: 0.78,
metalness: 0.22
})
);
gridBase.position.set(centerX, -0.035, 0);
gridBase.scale.set(width, 0.13, depth);
gridBase.receiveShadow = true;
trialGroup.add(gridBase);
gridBorder = new THREE.LineSegments(
new THREE.EdgesGeometry(unitBoxGeometry),
new THREE.LineBasicMaterial({
color: 0x36506f,
transparent: true,
opacity: 0.6
})
);
gridBorder.position.copy(gridBase.position);
gridBorder.scale.copy(gridBase.scale);
trialGroup.add(gridBorder);
for (let i = 0; i < state.n; i++) {
const column = i % columns;
const row = Math.floor(i / columns);
const x = centerX + (column - (columns - 1) / 2) * spacing;
const z = (row - (rows - 1) / 2) * spacing;
const material = new THREE.MeshStandardMaterial({
color: 0x273650,
emissive: 0x07101d,
emissiveIntensity: 0.16,
roughness: 0.42,
metalness: 0.18
});
const mesh = new THREE.Mesh(voxelGeometry, material);
mesh.position.set(x, 0.36, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.index = i;
mesh.userData.uniform = trialUniforms[i];
const edge = new THREE.LineSegments(voxelEdgeGeometry, voxelEdgeMaterial);
mesh.add(edge);
trialGroup.add(mesh);
trialMeshes.push(mesh);
}
matrixLabel.update(`${state.n} INDEPENDENT TRIALS`, "#91a5c2");
matrixLabel.sprite.position.set(centerX, 1.28, 0);
updateTrialColors();
}
function updateTrialColors() {
let successes = 0;
for (const mesh of trialMeshes) {
const success = mesh.userData.uniform < state.p;
if (success) successes++;
mesh.material.color.setHex(success ? 0x39d8c0 : 0x273650);
mesh.material.emissive.setHex(success ? 0x087f73 : 0x07101d);
mesh.material.emissiveIntensity = success ? 0.48 : 0.13;
mesh.material.opacity = success ? 1 : 0.72;
}
sampleCount.textContent = `Sample: ${successes} / ${state.n}`;
}
function shuffleSample() {
state.seed = (state.seed * 1664525 + 1013904223) >>> 0;
createUniforms(state.n);
trialMeshes.forEach((mesh, index) => {
mesh.userData.uniform = trialUniforms[index];
});
updateTrialColors();
}
// ---------------------------------------------------------------------
// Comparison chart
// ---------------------------------------------------------------------
const curveCanvas = $("#curve");
const curveContext = curveCanvas.getContext("2d");
function drawChart() {
const rect = curveCanvas.getBoundingClientRect();
if (!rect.width || !rect.height) return;
const dpr = Math.min(window.devicePixelRatio || 1, 2);
curveCanvas.width = Math.round(rect.width * dpr);
curveCanvas.height = Math.round(rect.height * dpr);
curveContext.setTransform(dpr, 0, 0, dpr, 0, 0);
const width = rect.width;
const height = rect.height;
const margin = { left: 34, right: 12, top: 15, bottom: 28 };
const plotWidth = width - margin.left - margin.right;
const plotHeight = height - margin.top - margin.bottom;
const x = (p) => margin.left + p * plotWidth;
const y = (value) => margin.top + (1 - value) * plotHeight;
curveContext.clearRect(0, 0, width, height);
curveContext.strokeStyle = "#1d2940";
curveContext.lineWidth = 1;
for (let i = 0; i <= 4; i++) {
const value = i / 4;
curveContext.beginPath();
curveContext.moveTo(margin.left, y(value));
curveContext.lineTo(width - margin.right, y(value));
curveContext.stroke();
curveContext.fillStyle = "#53647e";
curveContext.font = "9px system-ui, sans-serif";
curveContext.textAlign = "right";
curveContext.textBaseline = "middle";
curveContext.fillText(Math.round(value * 100), margin.left - 6, y(value));
}
curveContext.textAlign = "center";
curveContext.textBaseline = "top";
curveContext.fillText("0", x(0), height - 17);
curveContext.fillText("p", x(0.5), height - 17);
curveContext.fillText("1", x(1), height - 17);
curveContext.save();
curveContext.setLineDash([5, 4]);
curveContext.strokeStyle = "#ffbf69";
curveContext.lineWidth = 1.5;
curveContext.beginPath();
curveContext.moveTo(x(0), y(0));
curveContext.lineTo(x(1), y(1));
curveContext.stroke();
curveContext.restore();
curveContext.strokeStyle = "#4be3cf";
curveContext.lineWidth = 2.2;
curveContext.beginPath();
for (let i = 0; i <= 120; i++) {
const p = i / 120;
const probability = state.p >= 1 ? 1 : 1 - Math.pow(1 - p, state.n);
if (i === 0) curveContext.moveTo(x(p), y(probability));
else curveContext.lineTo(x(p), y(probability));
}
curveContext.stroke();
const currentProbability = atLeastOneProbability();
curveContext.strokeStyle = "rgba(179, 199, 226, 0.35)";
curveContext.lineWidth = 1;
curveContext.beginPath();
curveContext.moveTo(x(state.p), margin.top);
curveContext.lineTo(x(state.p), height - margin.bottom);
curveContext.stroke();
curveContext.fillStyle = "#ffbf69";
curveContext.beginPath();
curveContext.arc(x(state.p), y(state.p), 3.3, 0, Math.PI * 2);
curveContext.fill();
curveContext.fillStyle = "#4be3cf";
curveContext.beginPath();
curveContext.arc(x(state.p), y(currentProbability), 3.3, 0, Math.PI * 2);
curveContext.fill();
}
// ---------------------------------------------------------------------
// UI and state updates
// ---------------------------------------------------------------------
function updateActiveButtons() {
document.querySelectorAll("[data-p]").forEach((button) => {
if (button.dataset.p !== undefined) {
const active = Math.abs(Number(button.dataset.p) - state.p) < 0.0005;
button.classList.toggle("active", active);
}
});
document.querySelectorAll("[data-n]").forEach((button) => {
if (button.dataset.n !== undefined) {
button.classList.toggle("active", Number(button.dataset.n) === state.n);
}
});
}
function updateUI() {
const expected = expectedCount();
const probability = atLeastOneProbability();
probabilityValue.textContent = formatPercent(state.p);
trialsValue.textContent = String(state.n);
probabilityInput.style.setProperty("--fill", `${state.p * 100}%`);
trialsInput.style.setProperty(
"--fill",
`${((state.n - 1) / 119) * 100}%`
);
expectedMetric.textContent = formatCount(expected);
expectedDetail.textContent = `out of ${state.n} possible`;
probabilityMetric.textContent = formatProbability(probability);
probabilityDetail.textContent = "chance of any success";
expectedTower.target = state.p;
expectedTower.label.update(
"EXPECTED COUNT",
formatCount(expected),
`count / ${state.n} possible`,
"#ffbf69"
);
expectedTower.axisLabel.update(`MAX = ${state.n} EVENTS`, "#8b795f");
probabilityTower.target = probability;
probabilityTower.label.update(
"AT LEAST ONE",
formatProbability(probability),
"probability / 100%",
"#4be3cf"
);
probabilityTower.axisLabel.update("MAX = 100%", "#628e8b");
const noneProbability = state.p >= 1 ? 0 : Math.pow(1 - state.p, state.n);
insight.innerHTML = `
<strong>${formatPercent(state.p)} per trial</strong> over
<strong>${state.n} independent trials</strong> gives a
${formatProbability(probability)} chance of at least one success.
The “no success” branch has probability ${formatProbability(noneProbability)}.
`;
updateActiveButtons();
updateTrialColors();
drawChart();
}
probabilityInput.addEventListener("input", () => {
state.p = Number(probabilityInput.value);
updateUI();
});
trialsInput.addEventListener("input", () => {
const nextN = Number(trialsInput.value);
if (nextN === state.n) return;
state.n = nextN;
rebuildTrials();
updateUI();
});
document.querySelectorAll("[data-p]").forEach((button) => {
button.addEventListener("click", () => {
if (button.dataset.p !== undefined) {
state.p = Number(button.dataset.p);
probabilityInput.value = String(state.p);
}
if (button.dataset.n !== undefined) {
const nextN = Number(button.dataset.n);
if (nextN !== state.n) {
state.n = nextN;
trialsInput.value = String(state.n);
rebuildTrials();
}
}
updateUI();
});
});
$("#resetParameters").addEventListener("click", () => {
state.p = 0.2;
state.n = 32;
probabilityInput.value = "0.2";
trialsInput.value = "32";
rebuildTrials();
updateUI();
});
$("#shuffleSample").addEventListener("click", shuffleSample);
$("#resetCamera").addEventListener("click", resetCamera);
$("#toggleOrbit").addEventListener("click", () => {
state.autoOrbit = !state.autoOrbit;
$("#toggleOrbit").classList.toggle("active", state.autoOrbit);
$("#toggleOrbit").textContent = state.autoOrbit ? "Orbiting" : "Auto orbit";
});
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
if (!width || !height) return;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
drawChart();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
updateCamera(dt);
const damping = 1 - Math.exp(-dt * 8);
for (const tower of [expectedTower, probabilityTower]) {
tower.current = lerp(tower.current, tower.target, damping);
const height = Math.max(0.025, tower.current * BAR_HEIGHT);
tower.bar.scale.y = height;
tower.bar.position.y = BAR_BASE + height / 2;
tower.cap.position.y = BAR_BASE + height + 0.055;
const pulse = 1 + Math.sin(clock.elapsedTime * 3.2 + tower.group.position.x) * 0.045;
tower.cap.scale.set(pulse, 1, pulse);
}
renderer.render(scene, camera);
}
rebuildTrials();
updateUI();
resize();
animate();
$("#loading").remove();
</script>
</body>
</html>
```