Prompt specification
Build or customize the Three.js 3D scene "Seed germination toy study garden" 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: Seed germination toy study garden
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/seed-germination-toy-study-garden-x1771
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring binomial counts from repeated independent trials. Compose a fictional seed tray with independent synthetic germination outcomes and a count histogram; state the independence assumption and avoid presenting the probabilities as real botanical data. Voxel style.
Working Three.js Starter (seed-germination-toy-study-garden-x1771):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Seed Lab — Synthetic Binomial Study</title>
<style>
:root {
--ink: #24352f;
--paper: #f4efdf;
--paper-soft: rgba(247, 242, 226, 0.92);
--line: #24352f;
--orange: #e66f46;
--teal: #3ca6a0;
--gold: #e6b84b;
--shadow: rgba(36, 53, 47, 0.18);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e1cf;
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
}
.panel {
position: fixed;
z-index: 5;
border: 2px solid var(--line);
background: var(--paper-soft);
box-shadow: 6px 6px 0 var(--shadow);
backdrop-filter: blur(5px);
}
#intro {
top: 18px;
left: 18px;
width: min(470px, calc(100vw - 36px));
padding: 16px 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: #52665f;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 36px);
line-height: 1;
letter-spacing: -0.05em;
text-transform: uppercase;
}
.badge {
display: inline-block;
margin: 11px 0 9px;
padding: 5px 8px;
border: 2px solid var(--line);
background: var(--gold);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.09em;
}
#intro p {
margin: 0;
max-width: 62ch;
font-size: 12px;
line-height: 1.55;
}
strong { font-weight: 900; }
#controls {
left: 18px;
bottom: 18px;
width: min(340px, calc(100vw - 36px));
padding: 15px;
}
.control-title {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 7px;
font-size: 12px;
font-weight: 900;
text-transform: uppercase;
}
#pValue {
color: #b64d2d;
font-size: 20px;
}
input[type="range"] {
width: 100%;
margin: 5px 0 13px;
accent-color: var(--orange);
cursor: pointer;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin: 10px 0 12px;
}
.stat {
min-width: 0;
padding: 8px 7px;
border: 2px solid rgba(36, 53, 47, 0.72);
background: rgba(255, 255, 255, 0.32);
}
.stat span {
display: block;
margin-bottom: 4px;
color: #617069;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
}
.stat b {
display: block;
overflow: hidden;
font-size: 17px;
text-overflow: ellipsis;
white-space: nowrap;
}
.check {
display: flex;
align-items: center;
gap: 8px;
margin: 9px 0 12px;
font-size: 11px;
font-weight: 800;
cursor: pointer;
}
.check input {
width: 16px;
height: 16px;
margin: 0;
accent-color: var(--teal);
}
button {
width: 100%;
padding: 11px 12px;
border: 2px solid var(--line);
border-radius: 0;
background: var(--orange);
color: #fff9e9;
box-shadow: 3px 3px 0 var(--line);
font: 900 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
}
button:hover { background: #d85d36; }
button:active {
transform: translate(3px, 3px);
box-shadow: 0 0 0 var(--line);
}
#legend {
right: 18px;
bottom: 18px;
width: min(310px, calc(100vw - 36px));
padding: 13px 15px;
font-size: 11px;
}
#legend .row {
display: flex;
align-items: center;
gap: 8px;
margin: 6px 0;
}
.swatch {
width: 13px;
height: 13px;
flex: 0 0 auto;
border: 2px solid var(--line);
}
.observed { background: var(--orange); }
.expected { background: rgba(60, 166, 160, 0.25); border-color: var(--teal); }
.current { background: var(--gold); }
.hint {
margin-top: 9px;
padding-top: 9px;
border-top: 1px solid rgba(36, 53, 47, 0.35);
color: #617069;
line-height: 1.45;
}
#readout {
position: fixed;
z-index: 5;
right: 18px;
bottom: 185px;
max-width: min(310px, calc(100vw - 36px));
padding: 9px 11px;
border: 2px solid var(--line);
background: rgba(244, 239, 223, 0.94);
box-shadow: 4px 4px 0 var(--shadow);
font-size: 11px;
font-weight: 800;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #e8e1cf;
font-size: 13px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
@media (max-width: 760px) {
#intro {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 12px;
}
#intro p { font-size: 10px; }
.badge { margin: 8px 0 6px; }
#controls {
left: 10px;
bottom: 10px;
width: calc(100vw - 20px);
padding: 11px;
}
#legend { display: none; }
#readout { display: none; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel seed tray and synthetic binomial count histogram"></canvas>
<section id="intro" class="panel">
<div class="eyebrow">Procedural Study 07 · Count Model</div>
<h1>Voxel Seed Lab</h1>
<div class="badge">Fictional synthetic model — not botanical data</div>
<p>
Each fictional tray contains <strong>20 independent Bernoulli trials</strong> with synthetic
probability <strong>p</strong>. The number germinated is
<strong>X ~ Binomial(20, p)</strong>. The histogram summarizes 320 independent synthetic trays.
No value here represents a measured seed-germination probability.
</p>
</section>
<section id="controls" class="panel">
<div class="control-title">
<label for="probability">Synthetic p</label>
<output id="pValue" for="probability">0.65</output>
</div>
<input id="probability" type="range" min="0.05" max="0.95" step="0.01" value="0.65">
<div class="stats">
<div class="stat"><span>TRIALS / TRAY</span><b>20</b></div>
<div class="stat"><span>CURRENT X</span><b id="currentK">—</b></div>
<div class="stat"><span>E[X]</span><b id="meanK">—</b></div>
</div>
<label class="check">
<input id="showExpected" type="checkbox" checked>
Show theoretical binomial outline
</label>
<button id="resample" type="button">Resample synthetic tray</button>
</section>
<div id="readout">Hover an observed column for its count.</div>
<aside id="legend" class="panel">
<div class="row"><i class="swatch observed"></i> Observed frequency across 320 trays</div>
<div class="row"><i class="swatch expected"></i> Expected Binomial(20, p) frequency</div>
<div class="row"><i class="swatch current"></i> Bin containing the displayed tray’s count</div>
<div class="hint">
Drag to rotate · Wheel/pinch to zoom · Double-click to reset.<br>
One voxel block represents approximately five synthetic trays.
</div>
</aside>
<div id="loading">Building synthetic voxel study…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const N = 20;
const BATCHES = 320;
const BLOCKS_PER_CUBE = 5;
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e1cf);
scene.fog = new THREE.Fog(0xe8e1cf, 28, 58);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(0, 1.15, 0);
let yaw = 0.34;
let pitch = 0.52;
let radius = window.innerWidth / window.innerHeight < 0.8 ? 34 : 25;
function updateCamera() {
const horizontal = radius * Math.cos(pitch);
camera.position.set(
target.x + horizontal * Math.sin(yaw),
target.y + radius * Math.sin(pitch),
target.z + horizontal * Math.cos(yaw)
);
camera.lookAt(target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xfff5dc, 0x718077, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffefd0, 3.1);
sun.position.set(-8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -17;
sun.shadow.camera.right = 17;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8bc9c1, 1.1);
fill.position.set(10, 7, -8);
scene.add(fill);
const materials = {
ground: new THREE.MeshStandardMaterial({ color: 0xd9d0b8, roughness: 1 }),
desk: new THREE.MeshStandardMaterial({ color: 0xb8ac90, roughness: 0.92 }),
tray: new THREE.MeshStandardMaterial({ color: 0x7d4a34, roughness: 0.86 }),
trayEdge: new THREE.MeshStandardMaterial({ color: 0xa7653e, roughness: 0.82 }),
soil0: new THREE.MeshStandardMaterial({ color: 0x493126, roughness: 1 }),
soil1: new THREE.MeshStandardMaterial({ color: 0x56392b, roughness: 1 }),
soil2: new THREE.MeshStandardMaterial({ color: 0x3c2a23, roughness: 1 }),
seed: new THREE.MeshStandardMaterial({ color: 0xd9b75f, roughness: 0.8 }),
seedTip: new THREE.MeshStandardMaterial({ color: 0x866331, roughness: 0.9 }),
stem: new THREE.MeshStandardMaterial({ color: 0x3e9148, roughness: 0.75 }),
leaf0: new THREE.MeshStandardMaterial({ color: 0x5bb94f, roughness: 0.72 }),
leaf1: new THREE.MeshStandardMaterial({ color: 0x82c85b, roughness: 0.72 }),
platform: new THREE.MeshStandardMaterial({ color: 0xeee5cc, roughness: 0.9 }),
platformEdge: new THREE.MeshStandardMaterial({ color: 0xc8b997, roughness: 0.95 }),
observed: new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.7 }),
expectedFill: new THREE.MeshBasicMaterial({
color: 0x3ca6a0,
transparent: true,
opacity: 0.13,
depthWrite: false
}),
expectedLine: new THREE.LineBasicMaterial({
color: 0x277d79,
transparent: true,
opacity: 0.95
})
};
function box(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function textSprite(text, options = {}) {
const {
color = "#24352f",
scale = 0.011,
background = null,
border = null
} = options;
const measure = document.createElement("canvas").getContext("2d");
const font = "800 42px ui-monospace, SFMono-Regular, Menlo, monospace";
measure.font = font;
const width = Math.max(64, Math.ceil(measure.measureText(text).width) + 34);
const height = 72;
const c = document.createElement("canvas");
c.width = width;
c.height = height;
const ctx = c.getContext("2d");
if (background) {
ctx.fillStyle = background;
ctx.fillRect(0, 0, width, height);
}
if (border) {
ctx.strokeStyle = border;
ctx.lineWidth = 6;
ctx.strokeRect(3, 3, width - 6, height - 6);
}
ctx.font = font;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.fillText(text, width / 2, height / 2 + 1);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width * scale, height * scale, 1);
sprite.renderOrder = 20;
sprite.userData.ownMaterial = true;
return sprite;
}
function disposeGroup(group) {
group.traverse(object => {
if (object.geometry) object.geometry.dispose();
if (object.userData.ownMaterial && object.material) {
if (object.material.map) object.material.map.dispose();
object.material.dispose();
}
});
}
// Shared tabletop and ground.
box(46, 0.18, 34, materials.ground, 0, -0.63, 0);
box(21.5, 0.45, 9.3, materials.desk, 0, -0.25, 0);
box(21.7, 0.16, 9.5, materials.platformEdge, 0, -0.48, 0);
// A few small decorative voxel blocks.
const decorative = [
[-10.2, -0.36, -3.8, 0.45, 0.42, 0.45, 0x9ca68d],
[-9.4, -0.39, -3.9, 0.28, 0.30, 0.28, 0xc3aa76],
[9.8, -0.38, -3.5, 0.55, 0.35, 0.55, 0x7f9d91],
[10.5, -0.39, -3.1, 0.28, 0.28, 0.28, 0xbba36d],
[10.2, -0.39, 3.7, 0.34, 0.32, 0.34, 0x92a58e]
];
for (const [x, y, z, w, h, d, color] of decorative) {
const material = new THREE.MeshStandardMaterial({ color, roughness: 0.95 });
box(w, h, d, material, x, y, z);
}
let probability = 0.65;
let currentOutcomes = [];
let currentCount = 0;
let batchCounts = [];
let trayGroup = null;
let histogramGroup = null;
let expectedGroup = null;
let histogramMesh = null;
let histogramMeta = new Map();
function drawOutcome() {
// Every outcome is an independent synthetic Bernoulli draw.
return Math.random() < probability ? 1 : 0;
}
function drawTray() {
const outcomes = [];
for (let i = 0; i < N; i++) outcomes.push(drawOutcome());
return outcomes;
}
function drawBinomialCount() {
let count = 0;
for (let i = 0; i < N; i++) count += drawOutcome();
return count;
}
function buildTray(outcomes) {
if (trayGroup) {
scene.remove(trayGroup);
disposeGroup(trayGroup);
}
trayGroup = new THREE.Group();
trayGroup.position.set(-5.2, 0, 0.2);
scene.add(trayGroup);
// Fictional tray.
box(8.3, 0.34, 6.0, materials.tray, 0, 0.18, 0, trayGroup);
box(8.3, 0.48, 0.26, materials.trayEdge, 0, 0.49, 2.88, trayGroup);
box(8.3, 0.48, 0.26, materials.trayEdge, 0, 0.49, -2.88, trayGroup);
box(0.26, 0.48, 5.5, materials.trayEdge, 4.02, 0.49, 0, trayGroup);
box(0.26, 0.48, 5.5, materials.trayEdge, -4.02, 0.49, 0, trayGroup);
for (const x of [-4.02, 4.02]) {
for (const z of [-2.88, 2.88]) {
box(0.38, 0.68, 0.38, materials.trayEdge, x, 0.49, z, trayGroup);
}
}
const xs = [-2.85, -1.425, 0, 1.425, 2.85];
const zs = [-1.9, -0.63, 0.63, 1.9];
let cellIndex = 0;
for (let row = 0; row < zs.length; row++) {
for (let col = 0; col < xs.length; col++) {
const x = xs[col];
const z = zs[row];
const soilMaterial = materials[`soil${cellIndex % 3}`];
box(1.18, 0.20, 1.05, soilMaterial, x, 0.43, z, trayGroup);
// Small voxel soil crumbs.
const crumbA = (cellIndex * 7 % 5) / 10 - 0.2;
const crumbB = (cellIndex * 11 % 4) / 10 - 0.15;
box(0.16, 0.09, 0.14, soilMaterial, x + crumbA, 0.57, z + crumbB, trayGroup);
if (outcomes[cellIndex] === 1) {
const height = 0.48 + ((cellIndex * 13) % 6) * 0.055;
const stemHeight = height;
box(
0.13, stemHeight, 0.13,
materials.stem,
x, 0.55 + stemHeight / 2, z,
trayGroup
);
const leafY = 0.55 + stemHeight * 0.74;
const leafMaterial = materials.leaf0 + (cellIndex % 2);
box(0.43, 0.11, 0.23, leafMaterial, x + 0.18, leafY, z, trayGroup);
box(
0.43, 0.11, 0.23,
materials.leaf1,
x - 0.18, leafY + 0.12, z,
trayGroup
);
box(0.14, 0.15, 0.14, materials.leaf0, x, 0.55 + stemHeight, z, trayGroup);
} else {
box(0.22, 0.16, 0.18, materials.seed, x, 0.62, z, trayGroup);
box(0.07, 0.05, 0.07, materials.seedTip, x, 0.72, z, trayGroup);
}
cellIndex++;
}
}
const title = textSprite("TRAY 07 // FICTIONAL", {
scale: 0.009,
color: "#f7f0dd",
background: "#31473f",
border: "#f7f0dd"
});
title.position.set(0, 2.55, -2.95);
trayGroup.add(title);
const countSign = textSprite(`X = ${currentCount} / ${N}`, {
scale: 0.008,
color: "#24352f",
background: "#e6b84b",
border: "#24352f"
});
countSign.position.set(-2.65, 0.64, 3.12);
trayGroup.add(countSign);
const syntheticSign = textSprite("SYNTHETIC OUTCOMES", {
scale: 0.007,
color: "#f7f0dd"
});
syntheticSign.position.set(1.65, 0.68, 3.13);
trayGroup.add(syntheticSign);
}
function binomialProbability(k, n, p) {
let coefficient = 1;
for (let i = 1; i <= k; i++) coefficient *= (n - i + 1) / i;
return coefficient * Math.pow(p, k) * Math.pow(1 - p, n - k);
}
function buildHistogram() {
if (histogramGroup) {
scene.remove(histogramGroup);
disposeGroup(histogramGroup);
}
histogramGroup = new THREE.Group();
histogramGroup.position.set(4.5, 0, 0.2);
scene.add(histogramGroup);
expectedGroup = new THREE.Group();
histogramGroup.add(expectedGroup);
box(9.5, 0.30, 4.35, materials.platform, 0, 0.17, 0, histogramGroup);
box(9.7, 0.12, 4.55, materials.platformEdge, 0, -0.01, 0, histogramGroup);
const frequencies = Array(N + 1).fill(0);
for (const count of batchCounts) frequencies[count]++;
const maxInstances = (N + 1) * Math.ceil(BATCHES / BLOCKS_PER_CUBE);
const blockGeometry = new THREE.BoxGeometry(0.32, 0.22, 0.62);
histogramMesh = new THREE.InstancedMesh(
blockGeometry,
materials.observed,
maxInstances
);
histogramMesh.castShadow = true;
histogramMesh.receiveShadow = true;
histogramGroup.add(histogramMesh);
const dummy = new THREE.Object3D();
const orange = new THREE.Color(0xe66f46);
const gold = new THREE.Color(0xe6b84b);
const darkOrange = new THREE.Color(0xce5836);
const blockStep = 0.235;
const baseY = 0.34;
let instanceIndex = 0;
histogramMeta = new Map();
for (let k = 0; k <= N; k++) {
const x = -4.2 + k * 0.42;
const levels = Math.ceil(frequencies[k] / BLOCKS_PER_CUBE);
for (let level = 0; level < levels; level++) {
dummy.position.set(x, baseY + level * blockStep + 0.11, 0.20);
dummy.updateMatrix();
histogramMesh.setMatrixAt(instanceIndex, dummy.matrix);
const color = k === currentCount
? gold
: (level % 3 === 0 ? darkOrange : orange);
histogramMesh.setColorAt(instanceIndex, color);
histogramMeta.set(instanceIndex, {
count: k,
frequency: frequencies[k],
x,
height: levels * blockStep
});
instanceIndex++;
}
}
histogramMesh.count = instanceIndex;
histogramMesh.instanceMatrix.needsUpdate = true;
if (histogramMesh.instanceColor) histogramMesh.instanceColor.needsUpdate = true;
histogramMesh.computeBoundingBox();
histogramMesh.computeBoundingSphere();
// Theoretical expected frequencies, shown as a transparent voxel outline.
for (let k = 0; k <= N; k++) {
const x = -4.2 + k * 0.42;
const expectedFrequency = BATCHES * binomialProbability(k, N, probability);
const expectedHeight = Math.max(0.035, expectedFrequency / BLOCKS_PER_CUBE * blockStep);
const geometry = new THREE.BoxGeometry(0.40, expectedHeight, 0.78);
const fill = new THREE.Mesh(geometry, materials.expectedFill);
fill.position.set(x, baseY + expectedHeight / 2, -0.04);
fill.renderOrder = 2;
expectedGroup.add(fill);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
materials.expectedLine
);
edges.position.copy(fill.position);
edges.renderOrder = 3;
expectedGroup.add(edges);
}
expectedGroup.visible = document.querySelector("#showExpected").checked;
const title = textSprite("COUNT HISTOGRAM", {
scale: 0.010,
color: "#24352f"
});
title.position.set(0, 3.75, -1.72);
histogramGroup.add(title);
const subtitle = textSprite("320 INDEPENDENT SYNTHETIC TRAYS", {
scale: 0.0067,
color: "#52665f"
});
subtitle.position.set(0, 3.38, -1.72);
histogramGroup.add(subtitle);
const expectedLabel = textSprite("TEAL OUTLINE = EXPECTED BINOMIAL", {
scale: 0.0057,
color: "#277d79"
});
expectedLabel.position.set(0, 3.08, -1.72);
histogramGroup.add(expectedLabel);
for (let k = 0; k <= N; k += 2) {
const x = -4.2 + k * 0.42;
box(0.04, 0.04, 0.18, materials.platformEdge, x, 0.35, 2.10, histogramGroup);
const label = textSprite(String(k), {
scale: 0.0052,
color: "#52665f"
});
label.position.set(x, 0.48, 2.36);
histogramGroup.add(label);
}
const endpoint = textSprite(String(N), {
scale: 0.0052,
color: "#52665f"
});
endpoint.position.set(-4.2 + N * 0.42, 0.48, 2.36);
histogramGroup.add(endpoint);
const axisLabel = textSprite("GERMINATED COUNT k", {
scale: 0.0055,
color: "#52665f"
});
axisLabel.position.set(0, 0.49, 2.76);
histogramGroup.add(axisLabel);
const currentMeta = histogramMeta.get(
[...histogramMeta.entries()].find(([, meta]) => meta.count === currentCount)?.[0]
);
const currentMarker = textSprite("NOW", {
scale: 0.0055,
color: "#24352f",
background: "#e6b84b",
border: "#24352f"
});
const markerX = -4.2 + currentCount * 0.42;
const markerHeight = currentMeta
? currentMeta.height
: 0;
currentMarker.position.set(markerX, baseY + markerHeight + 0.32, 0.35);
histogramGroup.add(currentMarker);
}
function updateStats() {
document.querySelector("#pValue").textContent = probability.toFixed(2);
document.querySelector("#currentK").textContent = `${currentCount}/${N}`;
document.querySelector("#meanK").textContent = (N * probability).toFixed(1);
}
function initializeSimulation() {
currentOutcomes = drawTray();
currentCount = currentOutcomes.reduce((sum, value) => sum + value, 0);
batchCounts = Array.from({ length: BATCHES }, drawBinomialCount);
batchCounts[0] = currentCount;
buildTray(currentOutcomes);
buildHistogram();
updateStats();
}
function resampleDisplayedTray() {
currentOutcomes = drawTray();
currentCount = currentOutcomes.reduce((sum, value) => sum + value, 0);
// The displayed tray is sample zero. Replacing another independently
// sampled tray keeps the 320-batch histogram an iid synthetic sample.
batchCounts[0] = currentCount;
batchCounts[1] = drawBinomialCount();
buildTray(currentOutcomes);
buildHistogram();
updateStats();
}
document.querySelector("#probability").addEventListener("input", event => {
probability = Number(event.target.value);
document.querySelector("#pValue").textContent = probability.toFixed(2);
});
document.querySelector("#probability").addEventListener("change", event => {
probability = Number(event.target.value);
initializeSimulation();
});
document.querySelector("#resample").addEventListener("click", resampleDisplayedTray);
document.querySelector("#showExpected").addEventListener("change", event => {
if (expectedGroup) expectedGroup.visible = event.target.checked;
});
// Camera controls implemented directly here, without addon imports.
let dragging = false;
let lastX = 0;
let lastY = 0;
const pointers = new Map();
let pinchDistance = 0;
canvas.addEventListener("pointerdown", event => {
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
canvas.setPointerCapture(event.pointerId);
if (pointers.size === 1) {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
canvas.addEventListener("pointermove", event => {
if (pointers.has(event.pointerId)) {
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
}
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0) {
radius = THREE.MathUtils.clamp(
radius * (pinchDistance / Math.max(distance, 1)),
16,
38
);
updateCamera();
}
pinchDistance = distance;
return;
}
if (dragging) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
yaw -= dx * 0.006;
pitch = THREE.MathUtils.clamp(pitch - dy * 0.0045, 0.20, 1.12);
lastX = event.clientX;
lastY = event.clientY;
updateCamera();
} else {
updateHistogramHover(event);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
if (pointers.size === 0) dragging = false;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("pointerleave", event => {
if (!dragging) {
document.querySelector("#readout").textContent =
"Hover an observed column for its count.";
}
if (pointers.has(event.pointerId)) endPointer(event);
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius * Math.exp(event.deltaY * 0.001),
16,
38
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", () => {
yaw = 0.34;
pitch = 0.52;
radius = window.innerWidth / window.innerHeight < 0.8 ? 34 : 25;
updateCamera();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function updateHistogramHover(event) {
if (!histogramMesh) return;
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObject(histogramMesh, false)[0];
if (hit && hit.instanceId !== undefined) {
const meta = histogramMeta.get(hit.instanceId);
if (meta) {
document.querySelector("#readout").textContent =
`k = ${meta.count} germinated · ${meta.frequency}/${BATCHES} synthetic trays`;
canvas.style.cursor = "crosshair";
}
} else {
document.querySelector("#readout").textContent =
"Hover an observed column for its count.";
canvas.style.cursor = "grab";
}
}
canvas.addEventListener("pointerdown", () => {
canvas.style.cursor = "grabbing";
});
window.addEventListener("pointerup", () => {
canvas.style.cursor = "grab";
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
initializeSimulation();
document.querySelector("#loading").style.display = "none";
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```