Prompt specification
Build or customize the Three.js 3D scene "Umbrella-and-rain teaching street" 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: Umbrella-and-rain teaching street
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/umbrella-and-rain-teaching-street-x1783
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows confounding by a common causal variable. Compose an invented rainy-street dataset linking rain, umbrella use, and wet pavement; explain that umbrellas need not cause wet pavement merely because they appear together. Voxel style.
Working Three.js Starter (umbrella-and-rain-teaching-street-x1783):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Confounding Lab — Rain, Umbrellas & Wet Pavement</title>
<style>
:root {
--bg: #07111f;
--panel: #0e1a2a;
--panel-2: #132338;
--line: #263a50;
--text: #ecf7ff;
--muted: #91a8bd;
--cyan: #5ce8ff;
--blue: #67a7ff;
--amber: #ffc857;
--green: #72e6a8;
--shadow: #030914;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--text);
background:
linear-gradient(rgba(92, 232, 255, 0.025) 1px, transparent 1px),
linear-gradient(90deg, rgba(92, 232, 255, 0.025) 1px, transparent 1px),
var(--bg);
background-size: 24px 24px;
font-family: "Courier New", Courier, monospace;
}
button,
input {
font: inherit;
}
button {
color: var(--text);
}
#app {
display: grid;
grid-template-rows: auto 1fr auto;
width: 100%;
height: 100vh;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
min-height: 76px;
padding: 13px 18px;
border-bottom: 2px solid var(--line);
background: rgba(7, 17, 31, 0.96);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.pixel-mark {
position: relative;
width: 42px;
height: 42px;
flex: 0 0 auto;
border: 2px solid var(--cyan);
box-shadow: 4px 4px 0 var(--shadow);
}
.pixel-mark::before,
.pixel-mark::after {
content: "";
position: absolute;
background: var(--cyan);
}
.pixel-mark::before {
width: 10px;
height: 10px;
left: 7px;
top: 7px;
box-shadow: 14px 0 0 var(--cyan), 0 14px 0 var(--cyan), 14px 14px 0 var(--cyan);
}
.pixel-mark::after {
width: 8px;
height: 8px;
right: 4px;
bottom: 4px;
background: var(--amber);
}
.brand h1 {
margin: 0;
font-size: clamp(16px, 2vw, 24px);
line-height: 1;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.brand p {
margin: 7px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.header-right {
display: flex;
align-items: center;
gap: 9px;
flex-wrap: wrap;
justify-content: flex-end;
}
.badge {
display: inline-flex;
align-items: center;
min-height: 30px;
padding: 7px 9px;
border: 1px solid var(--line);
background: var(--panel);
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge strong {
margin-left: 7px;
color: var(--cyan);
}
main {
display: grid;
grid-template-columns: minmax(0, 1.55fr) minmax(360px, 0.75fr);
gap: 12px;
min-height: 0;
padding: 12px 14px;
}
.viewport {
position: relative;
min-width: 0;
min-height: 480px;
overflow: hidden;
border: 2px solid var(--line);
background: #081321;
box-shadow: 6px 6px 0 rgba(0, 0, 0, 0.28);
isolation: isolate;
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#scene:active {
cursor: grabbing;
}
.viewport::after {
content: "";
position: absolute;
inset: 0;
z-index: 1;
pointer-events: none;
background:
linear-gradient(180deg, rgba(7, 17, 31, 0.68), transparent 28%),
linear-gradient(0deg, rgba(7, 17, 31, 0.58), transparent 25%);
}
.scene-copy {
position: absolute;
z-index: 3;
top: 20px;
left: 20px;
max-width: 360px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 9px;
color: var(--cyan);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.scene-copy h2 {
margin: 0;
max-width: 340px;
font-size: clamp(25px, 3.3vw, 44px);
line-height: 0.98;
letter-spacing: -0.055em;
text-transform: uppercase;
text-shadow: 4px 4px 0 #030914;
}
.scene-copy p:last-child {
max-width: 330px;
margin: 12px 0 0;
color: #b7c9d8;
font-size: 12px;
line-height: 1.55;
}
.scene-controls {
position: absolute;
z-index: 5;
left: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
max-width: calc(100% - 36px);
padding: 10px;
border: 2px solid var(--line);
background: rgba(8, 19, 33, 0.92);
box-shadow: 4px 4px 0 var(--shadow);
}
.button {
min-height: 36px;
padding: 8px 11px;
border: 1px solid #38536f;
border-radius: 0;
background: #172b40;
box-shadow: 3px 3px 0 #030914;
cursor: pointer;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.button:hover {
border-color: var(--cyan);
color: var(--cyan);
}
.button:active {
transform: translate(2px, 2px);
box-shadow: 1px 1px 0 #030914;
}
.button.primary {
border-color: var(--amber);
background: var(--amber);
color: #142033;
}
.button.primary:hover {
color: #142033;
background: #ffd77c;
}
.rain-slider {
display: grid;
grid-template-columns: auto 125px auto;
align-items: center;
gap: 9px;
min-height: 36px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.rain-slider input {
width: 125px;
accent-color: var(--cyan);
cursor: pointer;
}
.rain-slider output {
min-width: 34px;
color: var(--cyan);
text-align: right;
}
.scene-status {
position: absolute;
z-index: 4;
right: 18px;
bottom: 18px;
display: grid;
gap: 7px;
padding: 9px 11px;
border-left: 3px solid var(--cyan);
background: rgba(8, 19, 33, 0.88);
pointer-events: none;
}
.scene-status span {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.scene-status strong {
color: var(--cyan);
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.world-label {
position: absolute;
z-index: 4;
padding: 6px 8px;
border: 1px solid currentColor;
background: rgba(7, 17, 31, 0.91);
box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.7);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
line-height: 1.25;
text-align: center;
text-transform: uppercase;
pointer-events: none;
transform: translate(-50%, -100%);
white-space: nowrap;
}
.world-label.rain {
color: var(--cyan);
}
.world-label.umbrella {
color: var(--amber);
}
.world-label.wet {
color: var(--blue);
}
.orbit-help {
position: absolute;
z-index: 3;
top: 18px;
right: 18px;
padding: 7px 9px;
border: 1px solid var(--line);
background: rgba(7, 17, 31, 0.76);
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
.panel {
min-width: 0;
overflow-y: auto;
border: 2px solid var(--line);
background: var(--panel);
box-shadow: 6px 6px 0 rgba(0, 0, 0, 0.28);
scrollbar-color: #36516d var(--panel);
scrollbar-width: thin;
}
.panel-section {
padding: 17px;
border-bottom: 1px solid var(--line);
}
.panel-section:last-child {
border-bottom: 0;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.section-heading h2,
.section-heading h3 {
margin: 0;
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-number {
color: var(--muted);
font-size: 10px;
}
.dataset-line {
margin: 0 0 13px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.dag-wrap {
border: 1px solid var(--line);
background:
linear-gradient(rgba(92, 232, 255, 0.025) 1px, transparent 1px),
linear-gradient(90deg, rgba(92, 232, 255, 0.025) 1px, transparent 1px),
#091522;
background-size: 16px 16px;
}
.dag-svg {
display: block;
width: 100%;
height: auto;
}
.dag-svg .node {
stroke-width: 2;
}
.dag-svg .node-rain {
fill: #213548;
stroke: var(--cyan);
}
.dag-svg .node-umbrella {
fill: #3a301f;
stroke: var(--amber);
}
.dag-svg .node-wet {
fill: #172b48;
stroke: var(--blue);
}
.dag-svg .edge {
fill: none;
stroke: #7893aa;
stroke-width: 2;
marker-end: url(#arrow);
}
.dag-svg text {
fill: var(--text);
font-family: "Courier New", Courier, monospace;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.06em;
text-anchor: middle;
}
.dag-svg .small {
fill: var(--muted);
font-size: 8px;
font-weight: 400;
}
.dag-note {
margin: 10px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.association {
display: grid;
grid-template-columns: 1fr auto;
align-items: end;
gap: 12px;
padding: 13px;
border: 1px solid #39516a;
background: #0a1625;
}
.association-label {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.association strong {
display: block;
margin-top: 5px;
color: var(--amber);
font-size: 24px;
line-height: 1;
}
.association .ratio {
padding: 7px 8px;
border: 1px solid var(--amber);
color: var(--amber);
font-size: 11px;
font-weight: 700;
}
.compare {
display: grid;
gap: 11px;
margin-top: 14px;
}
.compare-row {
display: grid;
grid-template-columns: 88px 1fr 78px;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 10px;
}
.bar {
height: 13px;
border: 1px solid #2c4258;
background: #08121f;
}
.bar i {
display: block;
width: 0;
height: 100%;
background: var(--blue);
transition: width 800ms steps(8, end);
}
.compare-row b {
color: var(--text);
font-size: 10px;
text-align: right;
}
.strata {
width: 100%;
margin-top: 14px;
border-collapse: collapse;
font-size: 10px;
}
.strata th,
.strata td {
padding: 9px 6px;
border-bottom: 1px solid var(--line);
text-align: right;
}
.strata th:first-child,
.strata td:first-child {
text-align: left;
}
.strata th {
color: var(--muted);
font-size: 9px;
font-weight: 400;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.strata td {
color: var(--text);
}
.zero-gap {
color: var(--green) !important;
font-weight: 700;
}
.callout {
margin-top: 13px;
padding: 11px;
border-left: 3px solid var(--amber);
background: #172333;
color: #c8d6e2;
font-size: 11px;
line-height: 1.55;
}
.callout strong {
color: var(--amber);
}
.explanation {
color: #b4c6d5;
font-size: 11px;
line-height: 1.65;
}
.explanation strong {
color: var(--text);
}
.no-direct-link {
margin-top: 13px;
padding: 11px;
border: 1px dashed #536b81;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
details {
margin-top: 13px;
border: 1px solid var(--line);
background: #0a1625;
}
summary {
padding: 10px;
color: var(--cyan);
cursor: pointer;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.method-body {
padding: 0 11px 11px;
color: var(--muted);
font-size: 10px;
line-height: 1.6;
}
.method-body code {
color: var(--text);
}
.reveal-button {
width: 100%;
margin-top: 14px;
}
footer {
display: flex;
justify-content: space-between;
gap: 14px;
padding: 8px 16px 10px;
border-top: 1px solid var(--line);
color: #6f879d;
font-size: 9px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
body.revealing .dag-svg .node-rain {
animation: node-pulse 700ms steps(3, end) 3;
}
body.revealing .dag-svg .edge {
animation: edge-pulse 700ms steps(3, end) 3;
}
@keyframes node-pulse {
50% {
fill: #24516a;
stroke-width: 4;
}
}
@keyframes edge-pulse {
50% {
stroke: var(--cyan);
stroke-width: 4;
}
}
@media (max-width: 960px) {
body {
overflow: auto;
}
#app {
height: auto;
min-height: 100vh;
}
main {
grid-template-columns: 1fr;
}
.viewport {
height: 62vh;
min-height: 520px;
}
.panel {
overflow: visible;
}
.scene-status {
display: none;
}
}
@media (max-width: 600px) {
header {
align-items: flex-start;
flex-direction: column;
}
.header-right {
justify-content: flex-start;
}
.viewport {
height: 70vh;
min-height: 540px;
}
.scene-copy {
top: 15px;
left: 15px;
right: 15px;
}
.scene-copy h2 {
max-width: 270px;
font-size: 28px;
}
.scene-copy p:last-child {
display: none;
}
.orbit-help {
display: none;
}
.scene-controls {
left: 10px;
right: 10px;
bottom: 10px;
}
.rain-slider {
grid-template-columns: auto 1fr auto;
width: 100%;
}
.rain-slider input {
width: 100%;
}
.compare-row {
grid-template-columns: 78px 1fr 68px;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 1ms !important;
animation-iteration-count: 1 !important;
transition-duration: 1ms !important;
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="pixel-mark" aria-hidden="true"></div>
<div>
<h1>Confounding Lab</h1>
<p>Rainy-street causal dataset</p>
</div>
</div>
<div class="header-right">
<div class="badge">Synthetic rows <strong id="sampleSize">1,440</strong></div>
<div class="badge">Common cause <strong>RAIN</strong></div>
</div>
</header>
<main>
<section class="viewport" id="viewport" aria-label="Interactive voxel rainy street">
<canvas
id="scene"
aria-label="Voxel rainy street showing umbrellas, rain, and wet pavement"
></canvas>
<div class="scene-copy">
<p class="eyebrow">Live causal diorama</p>
<h2>Same street.<br />Different weather.</h2>
<p>
Rain is the shared cause behind both umbrella use and wet pavement.
Change the weather and watch both outcomes respond.
</p>
</div>
<div class="orbit-help">Drag to orbit · scroll to zoom</div>
<div class="world-label rain" id="labelRain">Rain · common cause</div>
<div class="world-label umbrella" id="labelUmbrella">Umbrella use</div>
<div class="world-label wet" id="labelWet">Wet pavement</div>
<div class="scene-controls">
<button class="button primary" id="rainToggle" type="button">
Stop the rain
</button>
<label class="rain-slider" for="rainRange">
<span>Rain</span>
<input id="rainRange" type="range" min="0" max="100" value="82" />
<output id="rainOutput" for="rainRange">82%</output>
</label>
<button class="button" id="pathsToggle" type="button">
Causal paths: on
</button>
<button class="button" id="rotateToggle" type="button">
Auto rotate
</button>
</div>
<div class="scene-status">
<span>Weather state</span>
<strong id="weatherState">Heavy rain</strong>
<span id="sceneNarrative">Rain causes both observed variables</span>
</div>
</section>
<aside class="panel" aria-label="Dataset and causal explanation">
<section class="panel-section">
<div class="section-heading">
<h2>1 · Read the causal map</h2>
<span class="section-number">DAG</span>
</div>
<p class="dataset-line">
Each row represents one city-hour. The causal direction is
<strong>rain → umbrella use</strong> and
<strong>rain → wet pavement</strong>.
</p>
<div class="dag-wrap">
<svg
class="dag-svg"
viewBox="0 0 360 210"
role="img"
aria-label="Rain causes umbrella use and rain causes wet pavement"
>
<defs>
<marker
id="arrow"
viewBox="0 0 10 10"
refX="8"
refY="5"
markerWidth="7"
markerHeight="7"
orient="auto-start-reverse"
>
<polygon points="0 0, 10 5, 0 10" fill="#7893aa"></polygon>
</marker>
</defs>
<path class="edge" d="M160 62 L87 137"></path>
<path class="edge" d="M200 62 L273 137"></path>
<rect class="node node-rain" x="130" y="10" width="100" height="52"></rect>
<text x="180" y="34">RAIN</text>
<text class="small" x="180" y="50">COMMON CAUSE</text>
<rect class="node node-umbrella" x="12" y="145" width="145" height="53"></rect>
<text x="84" y="170">UMBRELLA USE</text>
<text class="small" x="84" y="186">RESPONSE TO RAIN</text>
<rect class="node node-wet" x="203" y="145" width="145" height="53"></rect>
<text x="275" y="170">WET PAVEMENT</text>
<text class="small" x="275" y="186">RESULT OF RAIN</text>
</svg>
</div>
<p class="dag-note">
There is no direct arrow from umbrella use to wet pavement in this
synthetic model.
</p>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>2 · Observe the association</h2>
<span class="section-number">Pooled</span>
</div>
<div class="association">
<div>
<span class="association-label">Wet pavement when umbrellas are used</span>
<strong id="pooledRate">—</strong>
</div>
<div class="ratio" id="riskRatio">—</div>
</div>
<div class="compare">
<div class="compare-row">
<span>Umbrella</span>
<div class="bar"><i id="barUmbrella"></i></div>
<b id="umbrellaWet">—</b>
</div>
<div class="compare-row">
<span>No umbrella</span>
<div class="bar"><i id="barNoUmbrella"></i></div>
<b id="noUmbrellaWet">—</b>
</div>
</div>
<p class="dataset-line" style="margin: 13px 0 0">
Umbrella hours look much wetter in the pooled data. That is real
association in the sample—but it is not yet a causal effect.
</p>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>3 · Condition on the common cause</h2>
<span class="section-number">Stratified</span>
</div>
<table class="strata">
<thead>
<tr>
<th>Rain state</th>
<th>Umbrella</th>
<th>No umbrella</th>
<th>Gap</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rainy</td>
<td id="rainyUmbrella">—</td>
<td id="rainyNoUmbrella">—</td>
<td id="rainyGap" class="zero-gap">—</td>
</tr>
<tr>
<td>Dry</td>
<td id="dryUmbrella">—</td>
<td id="dryNoUmbrella">—</td>
<td id="dryGap" class="zero-gap">—</td>
</tr>
</tbody>
</table>
<div class="callout">
<strong>Result:</strong> within rainy and dry hours, umbrella use
adds no wet-pavement difference. The pooled gap was produced by
the shared cause: rain.
</div>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>4 · The causal takeaway</h2>
<span class="section-number">≠ Causation</span>
</div>
<p class="explanation">
<strong>Umbrellas need not cause wet pavement merely because they
appear together.</strong> Here, both variables respond to rain.
Rain raises the probability of carrying an umbrella and also
wets the street.
</p>
<div class="no-direct-link">
A useful test is to intervene on the suspected cause: remove the
rain, and both umbrella use and wet pavement fall together.
</div>
<details>
<summary>Synthetic data specification</summary>
<div class="method-body">
<code>N = 1,440</code> city-hours.<br />
<code>P(rain) = 0.45</code><br />
<code>P(umbrella | rain) = 0.82</code><br />
<code>P(umbrella | dry) = 0.08</code><br />
<code>P(wet | rain) = 0.74</code><br />
<code>P(wet | dry) = 0.08</code><br /><br />
Wet pavement is sampled independently of umbrella use within each
rain stratum. This makes the dataset a clean confounding example,
not a claim about every real-world street.
</div>
</details>
<button
class="button primary reveal-button"
id="revealButton"
type="button"
>
Run the causal reveal
</button>
</section>
</aside>
</main>
<footer>
<span>Invented rainy-street dataset · deterministic synthetic sample</span>
<span>Three.js core · custom voxel camera controls</span>
</footer>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Deterministic synthetic dataset
// -------------------------------------------------------------------------
const SAMPLE_COUNT = 1440;
function mulberry32(seed) {
return function random() {
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 generateDataset(n) {
const random = mulberry32(20260417);
const rows = [];
for (let i = 0; i < n; i++) {
const rain = random() < 0.45;
const umbrella = random() < (rain ? 0.82 : 0.08);
const wet = random() < (rain ? 0.74 : 0.08);
rows.push({ rain, umbrella, wet });
}
return rows;
}
const data = generateDataset(SAMPLE_COUNT);
function stats(rows) {
const umbrella = rows.filter((row) => row.umbrella);
const noUmbrella = rows.filter((row) => !row.umbrella);
const wetRate = (subset) =>
subset.length
? subset.reduce((sum, row) => sum + Number(row.wet), 0) / subset.length
: 0;
return {
umbrellaRate: wetRate(umbrella),
noUmbrellaRate: wetRate(noUmbrella)
};
}
const pooled = stats(data);
const rainy = stats(data.filter((row) => row.rain));
const dry = stats(data.filter((row) => !row.rain));
const percent = (value) => `${Math.round(value * 100)}%`;
const percentagePoints = (value) => `${Math.round(value * 100)} pp`;
document.getElementById("sampleSize").textContent = SAMPLE_COUNT.toLocaleString();
document.getElementById("pooledRate").textContent = percent(pooled.umbrellaRate);
document.getElementById("umbrellaWet").textContent = `${percent(pooled.umbrellaRate)} wet`;
document.getElementById("noUmbrellaWet").textContent = `${percent(pooled.noUmbrellaRate)} wet`;
document.getElementById("riskRatio").textContent =
`${(pooled.umbrellaRate / pooled.noUmbrellaRate).toFixed(1)}× RISK`;
document.getElementById("barUmbrella").style.width = `${pooled.umbrellaRate * 100}%`;
document.getElementById("barNoUmbrella").style.width = `${pooled.noUmbrellaRate * 100}%`;
document.getElementById("rainyUmbrella").textContent = percent(rainy.umbrellaRate);
document.getElementById("rainyNoUmbrella").textContent = percent(rainy.noUmbrellaRate);
document.getElementById("dryUmbrella").textContent = percent(dry.umbrellaRate);
document.getElementById("dryNoUmbrella").textContent = percent(dry.noUmbrellaRate);
document.getElementById("rainyGap").textContent =
percentagePoints(rainy.umbrellaRate - rainy.noUmbrellaRate);
document.getElementById("dryGap").textContent =
percentagePoints(dry.umbrellaRate - dry.noUmbrellaRate);
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const canvas = document.getElementById("scene");
const viewport = document.getElementById("viewport");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x081321);
scene.fog = new THREE.Fog(0x081321, 25, 52);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 2.2, 0);
let theta = 0.78;
let phi = 0.92;
let radius = 27;
let thetaTarget = theta;
let phiTarget = phi;
let radiusTarget = radius;
let dragging = false;
let autoRotate = false;
let lastPointerX = 0;
let lastPointerY = 0;
const hemi = new THREE.HemisphereLight(0xb9dcff, 0x172033, 2.1);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffffff, 2.6);
sun.position.set(-10, 18, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 50;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x54cfff, 1.25);
rim.position.set(12, 8, -12);
scene.add(rim);
const materials = {
road: new THREE.MeshStandardMaterial({
color: 0x172433,
roughness: 0.95,
metalness: 0.02
}),
roadEdge: new THREE.MeshStandardMaterial({
color: 0x0d1824,
roughness: 1
}),
dryTile: new THREE.MeshStandardMaterial({
color: 0x263747,
roughness: 0.92,
metalness: 0.02
}),
wetTile: new THREE.MeshStandardMaterial({
color: 0x337fc1,
roughness: 0.2,
metalness: 0.28,
emissive: 0x071a2a,
emissiveIntensity: 0.45
}),
sidewalk: new THREE.MeshStandardMaterial({
color: 0x516171,
roughness: 0.9
}),
curb: new THREE.MeshStandardMaterial({
color: 0x82909b,
roughness: 0.86
}),
buildingA: new THREE.MeshStandardMaterial({
color: 0x263a50,
roughness: 0.88
}),
buildingB: new THREE.MeshStandardMaterial({
color: 0x33485c,
roughness: 0.88
}),
window: new THREE.MeshStandardMaterial({
color: 0xffd47a,
emissive: 0xffb84a,
emissiveIntensity: 0.8,
roughness: 0.45
}),
person: new THREE.MeshStandardMaterial({
color: 0x7c91a4,
roughness: 0.8
}),
skin: new THREE.MeshStandardMaterial({
color: 0xd7a77f,
roughness: 0.82
}),
umbrella: new THREE.MeshStandardMaterial({
color: 0xffc857,
roughness: 0.58
}),
umbrellaDark: new THREE.MeshStandardMaterial({
color: 0xd99832,
roughness: 0.62
}),
cloud: new THREE.MeshStandardMaterial({
color: 0x8aa6bc,
roughness: 0.95
}),
cloudDark: new THREE.MeshStandardMaterial({
color: 0x607b92,
roughness: 0.95
}),
pole: new THREE.MeshStandardMaterial({
color: 0x293b4c,
roughness: 0.65,
metalness: 0.45
}),
leaf: new THREE.MeshStandardMaterial({
color: 0x3f8067,
roughness: 0.95
}),
trunk: new THREE.MeshStandardMaterial({
color: 0x654936,
roughness: 1
})
};
function addBox(
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0,
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;
}
// Street, curbs, and sidewalk.
addBox(13.6, 0.5, 19.2, materials.roadEdge, 0, -0.35, 0);
addBox(12.7, 0.18, 18.4, materials.road, 0, -0.05, 0);
for (const side of [-1, 1]) {
addBox(2.25, 0.28, 20.5, materials.sidewalk, side * 7.35, -0.16, 0);
addBox(0.25, 0.4, 20.5, materials.curb, side * 6.12, -0.08, 0);
}
// Wet pavement is represented as voxel tiles.
const tileGeometry = new THREE.BoxGeometry(0.9, 0.11, 0.9);
const tiles = [];
const tileRandom = mulberry32(91021);
for (let row = 0; row < 18; row++) {
for (let col = 0; col < 12; col++) {
const mesh = new THREE.Mesh(tileGeometry, materials.dryTile);
mesh.position.set(
(col - 5.5) * 0.99,
0.08,
(row - 8.5) * 0.99
);
mesh.receiveShadow = true;
mesh.castShadow = false;
scene.add(mesh);
tiles.push({
mesh,
threshold: tileRandom()
});
}
}
// Voxel buildings lining the street.
const buildingRandom = mulberry32(22881);
for (const side of [-1, 1]) {
for (let i = 0; i < 4; i++) {
const height = 2.5 + buildingRandom() * 3.8;
const width = 2.35 + buildingRandom() * 0.55;
const depth = 3.6 + buildingRandom() * 0.55;
const x = side * (8.75 + buildingRandom() * 0.15);
const z = -7.1 + i * 4.75;
const material = i % 2 === 0 ? materials.buildingA : materials.buildingB;
addBox(width, height, depth, material, x, height / 2 - 0.02, z);
addBox(
width + 0.18,
0.22,
depth + 0.18,
materials.roadEdge,
x,
height + 0.08,
z
);
for (let row = 0; row < Math.max(1, Math.floor(height / 1.15)); row++) {
for (let col = 0; col < 2; col++) {
if (buildingRandom() > 0.23) {
addBox(
0.06,
0.34,
0.42,
materials.window,
x - side * (width / 2 + 0.035),
0.65 + row * 1.05,
z - 0.62 + col * 1.15
);
}
}
}
}
}
// Street lamps and blocky trees.
for (const z of [-5.8, 1.2, 7.3]) {
for (const side of [-1, 1]) {
addBox(0.12, 3.1, 0.12, materials.pole, side * 6.55, 1.4, z);
addBox(0.7, 0.12, 0.7, materials.pole, side * 6.55, 2.95, z);
addBox(0.48, 0.12, 0.48, materials.window, side * 6.55, 2.82, z);
}
}
for (const side of [-1, 1]) {
for (const z of [-2.9, 4.1]) {
addBox(0.32, 1.15, 0.32, materials.trunk, side * 7.15, 0.42, z);
addBox(0.85, 0.75, 0.85, materials.leaf, side * 7.15, 1.28, z);
addBox(0.55, 0.55, 0.55, materials.leaf, side * 7.15, 1.83, z);
}
}
// Rain cloud assembled from cubes.
const cloud = new THREE.Group();
cloud.position.set(-2.1, 8.3, -1.8);
scene.add(cloud);
addBox(2.1, 0.85, 1.25, materials.cloud, 0, 0, 0, cloud);
addBox(1.35, 0.85, 1.45, materials.cloud, -1.55, -0.1, 0.1, cloud);
addBox(1.45, 0.8, 1.2, materials.cloudDark, 1.55, -0.18, -0.05, cloud);
addBox(1.0, 0.65, 1.0, materials.cloudDark, 0.7, 0.62, 0.1, cloud);
addBox(0.9, 0.6, 1.0, materials.cloudDark, -0.9, 0.55, 0, cloud);
// Pedestrians, some carrying voxel umbrellas.
const umbrellaGroups = [];
const personPositions = [];
for (let i = 0; i < 16; i++) {
const side = i % 2 === 0 ? -1 : 1;
const x = side * (5.55 + (i % 3) * 0.18);
const z = -7.6 + i * 0.98 + (i % 2) * 0.15;
personPositions.push(new THREE.Vector3(x, 2.7, z));
const person = new THREE.Group();
person.position.set(x, 0, z);
scene.add(person);
addBox(0.18, 0.55, 0.2, materials.person, -0.13, 0.3, 0, person);
addBox(0.18, 0.55, 0.2, materials.person, 0.13, 0.3, 0, person);
addBox(0.48, 0.7, 0.32, materials.person, 0, 0.91, 0, person);
addBox(0.34, 0.34, 0.34, materials.skin, 0, 1.42, 0, person);
const umbrellaGroup = new THREE.Group();
umbrellaGroup.position.set(0.12, 0, 0);
person.add(umbrellaGroup);
addBox(0.065, 1.15, 0.065, materials.umbrellaDark, 0.1, 1.48, 0, umbrellaGroup);
addBox(0.55, 0.18, 0.55, materials.umbrella, 0.1, 2.02, 0, umbrellaGroup);
addBox(0.46, 0.18, 0.38, materials.umbrella, -0.42, 1.99, 0, umbrellaGroup);
addBox(0.46, 0.18, 0.38, materials.umbrella, 0.62, 1.99, 0, umbrellaGroup);
addBox(0.38, 0.18, 0.46, materials.umbrella, 0.1, 1.99, -0.42, umbrellaGroup);
addBox(0.38, 0.18, 0.46, materials.umbrella, 0.1, 1.99, 0.42, umbrellaGroup);
addBox(0.12, 0.25, 0.12, materials.umbrellaDark, 0.1, 2.22, 0, umbrellaGroup);
umbrellaGroups.push(umbrellaGroup);
}
// Instanced voxel rain.
const RAIN_COUNT = 190;
const rainGeometry = new THREE.BoxGeometry(0.035, 0.43, 0.035);
const rainMaterial = new THREE.MeshBasicMaterial({
color: 0x65d9ff,
transparent: true,
opacity: 0.76
});
const rainMesh = new THREE.InstancedMesh(rainGeometry, rainMaterial, RAIN_COUNT);
rainMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
rainMesh.frustumCulled = false;
scene.add(rainMesh);
const rainRandom = mulberry32(443121);
const rainDrops = [];
for (let i = 0; i < RAIN_COUNT; i++) {
rainDrops.push({
x: -5.8 + rainRandom() * 11.6,
y: rainRandom() * 9,
z: -8 + rainRandom() * 16,
speed: 7 + rainRandom() * 5
});
}
const rainMatrix = new THREE.Matrix4();
const rainQuaternion = new THREE.Quaternion();
const rainScale = new THREE.Vector3(1, 1, 1);
const rainPosition = new THREE.Vector3();
// Dotted voxel paths: rain -> umbrella and rain -> wet pavement.
const causalPaths = new THREE.Group();
scene.add(causalPaths);
const pathMaterials = [];
function createVoxelPath(start, end, color, count = 20) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.68,
depthWrite: false
});
pathMaterials.push(material);
const direction = new THREE.Vector3().subVectors(end, start);
for (let i = 0; i < count; i++) {
const t = 0.06 + (i / count) * 0.84;
const point = new THREE.Vector3().lerpVectors(start, end, t);
const cube = addBox(
0.11,
0.11,
0.11,
material,
point.x,
point.y,
point.z,
causalPaths
);
cube.castShadow = false;
cube.receiveShadow = false;
}
const arrow = addBox(
0.25,
0.25,
0.25,
material,
end.x,
end.y,
end.z,
causalPaths
);
arrow.castShadow = false;
arrow.receiveShadow = false;
}
const rainPathStart = new THREE.Vector3(-2.1, 7.8, -1.8);
const umbrellaPathEnd = new THREE.Vector3(5.4, 2.5, 3.3);
const wetPathEnd = new THREE.Vector3(3.6, 0.35, 4.0);
createVoxelPath(rainPathStart, umbrellaPathEnd, 0xffc857, 23);
createVoxelPath(rainPathStart, wetPathEnd, 0x5ce8ff, 24);
// -------------------------------------------------------------------------
// UI and custom camera controls
// -------------------------------------------------------------------------
const rainRange = document.getElementById("rainRange");
const rainOutput = document.getElementById("rainOutput");
const rainToggle = document.getElementById("rainToggle");
const pathsToggle = document.getElementById("pathsToggle");
const rotateToggle = document.getElementById("rotateToggle");
const weatherState = document.getElementById("weatherState");
const sceneNarrative = document.getElementById("sceneNarrative");
let targetRain = Number(rainRange.value) / 100;
let currentRain = targetRain;
let pathsVisible = true;
let revealUntil = 0;
function updateWeatherUI() {
const value = Math.round(targetRain * 100);
rainOutput.value = `${value}%`;
rainRange.value = value;
if (value === 0) {
weatherState.textContent = "Dry street";
sceneNarrative.textContent = "Removing rain lowers both outcomes";
rainToggle.textContent = "Bring back rain";
} else if (value < 40) {
weatherState.textContent = "Light rain";
sceneNarrative.textContent = "Rain causes both observed variables";
rainToggle.textContent = "Stop the rain";
} else {
weatherState.textContent = "Heavy rain";
sceneNarrative.textContent = "Rain causes both observed variables";
rainToggle.textContent = "Stop the rain";
}
}
rainRange.addEventListener("input", () => {
targetRain = Number(rainRange.value) / 100;
updateWeatherUI();
});
rainToggle.addEventListener("click", () => {
targetRain = targetRain > 0.05 ? 0 : 0.82;
rainRange.value = Math.round(targetRain * 100);
updateWeatherUI();
});
pathsToggle.addEventListener("click", () => {
pathsVisible = !pathsVisible;
pathsToggle.textContent = `Causal paths: ${pathsVisible ? "on" : "off"}`;
causalPaths.visible = pathsVisible;
});
rotateToggle.addEventListener("click", () => {
autoRotate = !autoRotate;
rotateToggle.textContent = `Auto rotate: ${autoRotate ? "on" : "off"}`;
});
document.getElementById("revealButton").addEventListener("click", () => {
document.body.classList.remove("revealing");
void document.body.offsetWidth;
document.body.classList.add("revealing");
revealUntil = performance.now() + 5000;
targetRain = 0.82;
rainRange.value = 82;
updateWeatherUI();
window.setTimeout(() => {
document.body.classList.remove("revealing");
}, 5000);
});
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
thetaTarget -= dx * 0.006;
phiTarget = THREE.MathUtils.clamp(phiTarget + dy * 0.005, 0.38, 1.32);
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
radiusTarget = THREE.MathUtils.clamp(
radiusTarget + event.deltaY * 0.018,
17,
38
);
}, { passive: false });
// Labels are projected from 3D positions into the HTML overlay.
const labelRain = document.getElementById("labelRain");
const labelUmbrella = document.getElementById("labelUmbrella");
const labelWet = document.getElementById("labelWet");
const labelPositions = {
rain: new THREE.Vector3(-2.1, 9.4, -1.8),
umbrella: new THREE.Vector3(5.3, 2.8, 3.1),
wet: new THREE.Vector3(3.8, 0.55, 4.0)
};
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = projected.z < 1 ? "1" : "0";
}
function resizeRenderer() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(viewport);
resizeRenderer();
updateWeatherUI();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = performance.now();
currentRain += (targetRain - currentRain) * (1 - Math.exp(-dt * 4.5));
if (autoRotate && !dragging) {
thetaTarget += dt * 0.12;
}
theta += (thetaTarget - theta) * (1 - Math.exp(-dt * 9));
phi += (phiTarget - phi) * (1 - Math.exp(-dt * 9));
radius += (radiusTarget - radius) * (1 - Math.exp(-dt * 9));
camera.position.setFromSphericalCoords(radius, phi, theta);
camera.position.y += cameraTarget.y;
camera.lookAt(cameraTarget);
// Wetness and umbrella prevalence both respond to the common cause.
const wetProbability = 0.08 + currentRain * 0.66;
for (const tile of tiles) {
const shouldBeWet = tile.threshold < wetProbability;
const desiredMaterial = shouldBeWet ? materials.wetTile : materials.dryTile;
if (tile.mesh.material !== desiredMaterial) {
tile.mesh.material = desiredMaterial;
}
}
const visibleUmbrellas = 1 + Math.round(currentRain * 13);
umbrellaGroups.forEach((group, index) => {
group.visible = index < visibleUmbrellas;
});
rainMaterial.opacity = 0.12 + currentRain * 0.7;
rainMesh.visible = currentRain > 0.025;
for (let i = 0; i < RAIN_COUNT; i++) {
const drop = rainDrops[i];
drop.y -= dt * drop.speed;
drop.x += dt * 0.25;
if (drop.y < -0.1) {
drop.y = 8.4 + rainRandom() * 1.5;
drop.x = -5.8 + rainRandom() * 11.6;
}
rainPosition.set(drop.x, drop.y, drop.z);
rainScale.set(1, 1 + currentRain * 1.4, 1);
rainMatrix.compose(rainPosition, rainQuaternion, rainScale);
rainMesh.setMatrixAt(i, rainMatrix);
}
rainMesh.instanceMatrix.needsUpdate = true;
if (pathsVisible) {
const revealed = time < revealUntil;
const pulse = revealed
? 0.72 + Math.sin(time * 0.012) * 0.2
: 0.64;
for (const material of pathMaterials) {
material.opacity = pulse;
}
}
placeLabel(labelRain, labelPositions.rain);
placeLabel(labelUmbrella, labelPositions.umbrella);
placeLabel(labelWet, labelPositions.wet);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```