Prompt specification
Build or customize the Three.js 3D scene "Outcome and event grouping display" 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: Outcome and event grouping display
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/outcome-and-event-grouping-display-x1826
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows sample spaces and events in chance experiments. Highlight all outcome cells producing selected sums while preserving equal single-outcome probabilities; show why different sums have different probabilities without resizing the cells. Stylized solid forms.
Technical checks: Derive count and probability from the highlighted ordered outcomes. For two fair dice, sum 7 selects 6 of 36 cells and sum 2 selects 1 of 36.
Return one complete HTML document that works with the cdnjs Three.js r160 module inside a sandbox with scripts allowed. Make the requested subject visible at the default camera. Use procedural assets and local button handlers. Do not use native form submission, pointer lock, external assets or runtime requests other than Three.js.
Working Three.js Starter (outcome-and-event-grouping-display-x1826):
```html
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Espaço amostral — Dois dados</title>
<style>
:root {
--navy: #173447;
--navy-2: #24495e;
--blue: #496a7c;
--cream: #f7f3e9;
--paper: #fffdf8;
--gold: #f2b83f;
--gold-dark: #a96b08;
--ink: #183244;
--muted: #6f7d84;
--line: #dce2df;
--green: #237a57;
--red: #b64242;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background: var(--cream);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
.app {
display: flex;
flex-direction: column;
height: 100%;
}
.topbar {
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
min-height: 76px;
padding: 14px 22px;
border-bottom: 1px solid rgba(23, 52, 71, 0.1);
background: rgba(255, 253, 248, 0.96);
box-shadow: 0 5px 22px rgba(23, 52, 71, 0.05);
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
position: relative;
width: 48px;
height: 48px;
flex: 0 0 auto;
border-radius: 14px;
background: var(--navy);
box-shadow: 0 7px 14px rgba(23, 52, 71, 0.18);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
width: 17px;
height: 17px;
border-radius: 5px;
background: var(--cream);
box-shadow: inset 0 -3px 0 rgba(23, 52, 71, 0.15);
}
.brand-mark::before {
left: 8px;
top: 8px;
}
.brand-mark::after {
right: 8px;
bottom: 8px;
background: var(--gold);
}
.brand-copy span {
display: block;
margin-bottom: 3px;
color: var(--gold-dark);
font-size: 0.68rem;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.brand-copy h1 {
margin: 0;
color: var(--navy);
font-size: clamp(1rem, 2vw, 1.28rem);
line-height: 1.1;
letter-spacing: -0.02em;
}
.top-actions {
display: flex;
align-items: center;
gap: 12px;
}
.top-note {
color: var(--muted);
font-size: 0.8rem;
font-weight: 650;
}
.icon-button {
display: inline-flex;
align-items: center;
gap: 8px;
min-height: 40px;
padding: 9px 13px;
border: 1px solid #cfd9dc;
border-radius: 11px;
color: var(--navy);
background: #fff;
cursor: pointer;
font-size: 0.8rem;
font-weight: 800;
transition: transform 140ms ease, border-color 140ms ease, background 140ms ease;
}
.icon-button:hover {
transform: translateY(-1px);
border-color: #9fb4bd;
background: #f5fafb;
}
.icon-button svg {
width: 17px;
height: 17px;
stroke: currentColor;
fill: none;
stroke-width: 2;
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
min-height: 0;
flex: 1;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #dfe8eb;
isolation: isolate;
}
.stage canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
.stage canvas.dragging {
cursor: grabbing;
}
.stage::after {
content: "";
position: absolute;
inset: 0;
z-index: 1;
pointer-events: none;
background:
radial-gradient(circle at 50% 42%, transparent 38%, rgba(23, 52, 71, 0.06) 100%),
linear-gradient(180deg, rgba(255, 255, 255, 0.12), transparent 24%);
}
.stage-card {
position: absolute;
z-index: 3;
top: 20px;
left: 20px;
max-width: min(340px, calc(100% - 40px));
padding: 14px 16px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 16px;
color: var(--navy);
background: rgba(255, 253, 248, 0.88);
box-shadow: 0 10px 30px rgba(23, 52, 71, 0.12);
backdrop-filter: blur(12px);
pointer-events: none;
}
.stage-card .eyebrow {
display: block;
margin-bottom: 5px;
color: var(--gold-dark);
font-size: 0.65rem;
font-weight: 900;
letter-spacing: 0.15em;
}
.stage-card strong {
display: block;
font-size: 1rem;
letter-spacing: -0.01em;
}
.stage-card small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 0.72rem;
font-weight: 600;
}
.stage-hint {
position: absolute;
z-index: 3;
right: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 8px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 12px;
color: rgba(23, 52, 71, 0.75);
background: rgba(255, 253, 248, 0.82);
box-shadow: 0 7px 22px rgba(23, 52, 71, 0.09);
backdrop-filter: blur(10px);
pointer-events: none;
font-size: 0.72rem;
font-weight: 750;
}
.stage-hint .mouse-icon {
width: 15px;
height: 21px;
border: 2px solid currentColor;
border-radius: 8px;
position: relative;
}
.stage-hint .mouse-icon::after {
content: "";
position: absolute;
top: 3px;
left: 50%;
width: 2px;
height: 5px;
border-radius: 2px;
background: currentColor;
transform: translateX(-50%);
}
.legend {
position: absolute;
z-index: 3;
left: 20px;
bottom: 18px;
display: flex;
align-items: center;
gap: 16px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 12px;
background: rgba(255, 253, 248, 0.86);
box-shadow: 0 7px 22px rgba(23, 52, 71, 0.09);
backdrop-filter: blur(10px);
pointer-events: none;
color: var(--muted);
font-size: 0.71rem;
font-weight: 750;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 13px;
height: 13px;
border-radius: 4px;
box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.12);
}
.swatch.highlight {
background: var(--gold);
}
.swatch.other {
background: var(--blue);
}
.tooltip {
position: absolute;
z-index: 6;
display: none;
min-width: 175px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 11px;
color: #fff;
background: rgba(20, 43, 58, 0.94);
box-shadow: 0 9px 24px rgba(16, 35, 48, 0.22);
pointer-events: none;
font-size: 0.76rem;
line-height: 1.4;
}
.tooltip strong {
color: var(--gold);
font-size: 0.84rem;
}
.tooltip small {
display: block;
margin-top: 2px;
color: #bcd0da;
}
.loading {
position: absolute;
inset: 0;
z-index: 9;
display: grid;
place-items: center;
color: var(--navy);
background: #dfe8eb;
font-size: 0.9rem;
font-weight: 800;
}
.panel {
min-height: 0;
border-left: 1px solid rgba(23, 52, 71, 0.09);
background: var(--paper);
box-shadow: -12px 0 32px rgba(23, 52, 71, 0.055);
}
.panel-scroll {
height: 100%;
overflow-y: auto;
padding: 24px 24px 28px;
scrollbar-width: thin;
scrollbar-color: #c2cdd0 transparent;
}
.panel .eyebrow {
color: var(--gold-dark);
font-size: 0.67rem;
font-weight: 900;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.panel h2 {
margin: 7px 0 8px;
color: var(--navy);
font-size: 1.72rem;
line-height: 1.05;
letter-spacing: -0.045em;
}
.intro {
margin: 0 0 21px;
color: var(--muted);
font-size: 0.84rem;
line-height: 1.55;
}
.section {
margin-top: 18px;
padding-top: 18px;
border-top: 1px solid var(--line);
}
.section-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 11px;
}
.section-heading h3 {
margin: 0;
color: var(--navy);
font-size: 0.82rem;
letter-spacing: -0.01em;
}
.section-heading span {
color: var(--muted);
font-size: 0.68rem;
font-weight: 650;
}
.sum-grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 7px;
}
.sum-button {
position: relative;
min-width: 0;
padding: 9px 3px 8px;
border: 1px solid #d5dee0;
border-radius: 10px;
color: var(--navy);
background: #f7f9f7;
cursor: pointer;
transition: transform 130ms ease, border-color 130ms ease,
color 130ms ease, background 130ms ease, box-shadow 130ms ease;
}
.sum-button span {
display: block;
font-size: 0.9rem;
font-weight: 900;
line-height: 1;
}
.sum-button small {
display: block;
margin-top: 5px;
color: #879499;
font-size: 0.59rem;
font-weight: 800;
}
.sum-button:hover {
transform: translateY(-1px);
border-color: #9db3bd;
background: #fff;
}
.sum-button.active {
border-color: #e6a824;
color: #513200;
background: var(--gold);
box-shadow: 0 6px 14px rgba(242, 184, 63, 0.24);
}
.sum-button.active small {
color: rgba(81, 51, 0, 0.67);
}
.metric-card {
position: relative;
overflow: hidden;
margin-top: 16px;
padding: 18px;
border-radius: 18px;
color: #fff;
background: var(--navy);
box-shadow: 0 12px 25px rgba(23, 52, 71, 0.16);
}
.metric-card::after {
content: "";
position: absolute;
width: 110px;
height: 110px;
right: -42px;
top: -48px;
border-radius: 50%;
background: rgba(242, 184, 63, 0.14);
}
.metric-label {
position: relative;
z-index: 1;
color: #bcd0da;
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric-main {
position: relative;
z-index: 1;
display: flex;
align-items: baseline;
gap: 6px;
margin-top: 7px;
}
.metric-main strong {
color: var(--gold);
font-size: 2.65rem;
line-height: 1;
letter-spacing: -0.055em;
}
.metric-main span {
color: #dce8ed;
font-size: 1.08rem;
font-weight: 750;
}
.percent {
position: relative;
z-index: 1;
margin-top: 5px;
font-size: 0.9rem;
font-weight: 800;
}
.event-meter {
position: relative;
z-index: 1;
height: 5px;
margin-top: 15px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.12);
}
.event-meter-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--gold);
transition: width 220ms ease;
}
.metric-caption {
position: relative;
z-index: 1;
margin: 9px 0 0;
color: #bcd0da;
font-size: 0.69rem;
line-height: 1.45;
}
.pairs {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 11px;
}
.pair-chip {
display: inline-flex;
align-items: center;
gap: 5px;
padding: 7px 9px;
border: 1px solid #e8d29d;
border-radius: 9px;
color: #61430d;
background: #fff7df;
font-size: 0.74rem;
font-weight: 800;
}
.pair-chip em {
color: #aa7a1a;
font-style: normal;
}
.explanation {
padding: 13px 14px;
border-left: 4px solid var(--gold);
border-radius: 4px 12px 12px 4px;
color: #53666f;
background: #f5f7f4;
font-size: 0.76rem;
line-height: 1.55;
}
.explanation strong {
color: var(--navy);
}
.histogram {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 5px;
height: 126px;
padding: 20px 4px 0;
border-bottom: 1px solid #d8e0df;
}
.hist-item {
display: flex;
flex: 1;
flex-direction: column;
align-items: center;
justify-content: flex-end;
height: 100%;
min-width: 0;
padding: 0;
border: 0;
color: var(--muted);
background: transparent;
cursor: pointer;
}
.hist-value {
height: 15px;
color: #7d8a90;
font-size: 0.56rem;
font-weight: 850;
}
.hist-bar {
width: 100%;
max-width: 22px;
min-height: 4px;
border-radius: 5px 5px 1px 1px;
background: #a9bcc3;
transition: height 220ms ease, background 160ms ease;
}
.hist-label {
position: absolute;
margin-top: 8px;
transform: translateY(22px);
font-size: 0.61rem;
font-weight: 800;
}
.hist-item.active .hist-bar {
background: var(--gold);
box-shadow: 0 3px 8px rgba(242, 184, 63, 0.25);
}
.hist-item.active .hist-value,
.hist-item.active .hist-label {
color: var(--gold-dark);
}
.hist-wrap {
padding-bottom: 22px;
}
.hist-note {
margin: 8px 0 0;
color: var(--muted);
font-size: 0.67rem;
line-height: 1.45;
}
.integrity {
display: flex;
align-items: flex-start;
gap: 9px;
margin-top: 18px;
padding: 11px 12px;
border: 1px solid #bcd8cc;
border-radius: 11px;
color: #285f49;
background: #edf8f3;
font-size: 0.68rem;
font-weight: 750;
line-height: 1.45;
}
.integrity.bad {
border-color: #e3b4b4;
color: var(--red);
background: #fff0f0;
}
.integrity-icon {
display: grid;
place-items: center;
width: 18px;
height: 18px;
flex: 0 0 auto;
border-radius: 50%;
color: #fff;
background: var(--green);
font-size: 0.65rem;
font-weight: 900;
}
.integrity.bad .integrity-icon {
background: var(--red);
}
@media (max-width: 940px) {
html,
body {
overflow: auto;
}
.app {
min-height: 100%;
height: auto;
}
.workspace {
grid-template-columns: 1fr;
}
.stage {
height: 62vh;
min-height: 480px;
}
.panel {
overflow: visible;
}
.panel-scroll {
height: auto;
overflow: visible;
}
.top-note {
display: none;
}
}
@media (max-width: 560px) {
.topbar {
min-height: 68px;
padding: 11px 14px;
}
.brand-mark {
width: 42px;
height: 42px;
}
.brand-copy h1 {
font-size: 0.95rem;
}
.brand-copy span {
font-size: 0.58rem;
}
.icon-button span {
display: none;
}
.stage {
min-height: 440px;
}
.stage-card {
top: 13px;
left: 13px;
}
.legend {
left: 13px;
bottom: 13px;
}
.stage-hint {
right: 13px;
bottom: 13px;
}
.stage-hint span {
display: none;
}
.panel-scroll {
padding: 21px 17px 28px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div class="brand-copy">
<span>Probabilidade · espaço amostral</span>
<h1>Amostra, evento e chance</h1>
</div>
</div>
<div class="top-actions">
<span class="top-note">36 resultados igualmente prováveis</span>
<button class="icon-button" id="resetCamera" type="button" aria-label="Restaurar câmera">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M4 7v5h5"></path>
<path d="M5.6 16.5a8 8 0 1 0 .2-10.2L4 8"></path>
</svg>
<span>Restaurar câmera</span>
</button>
</div>
</header>
<main class="workspace">
<section class="stage" id="stage" aria-label="Visualização tridimensional do espaço amostral">
<div class="stage-card">
<span class="eyebrow">Dois dados justos</span>
<strong>Grade 6 × 6 = 36 pares ordenados</strong>
<small>Linhas: D2 · Colunas: D1 · cada casa vale 1/36</small>
</div>
<div class="legend" aria-hidden="true">
<span class="legend-item"><i class="swatch highlight"></i> Evento selecionado</span>
<span class="legend-item"><i class="swatch other"></i> Demais resultados</span>
</div>
<div class="stage-hint" aria-hidden="true">
<i class="mouse-icon"></i>
<span>Arraste para girar · role para aproximar</span>
</div>
<div class="tooltip" id="tooltip" role="status"></div>
<div class="loading" id="loading">Preparando o espaço amostral…</div>
</section>
<aside class="panel" aria-label="Controle e análise do evento">
<div class="panel-scroll">
<div class="eyebrow">Experimento</div>
<h2>Dois dados</h2>
<p class="intro">
Cada casa representa um par ordenado <strong>(D1, D2)</strong>.
Os 36 pares têm a mesma probabilidade: <strong>1/36</strong>.
</p>
<section>
<div class="section-heading">
<h3>Selecione a soma do evento</h3>
<span>número de pares</span>
</div>
<div class="sum-grid" id="sumButtons" aria-label="Somas possíveis"></div>
<div class="metric-card" aria-live="polite">
<div class="metric-label">Probabilidade do evento</div>
<div class="metric-main">
<strong id="favorableCount">6</strong>
<span>/ 36</span>
</div>
<div class="percent" id="probability">16,7%</div>
<div class="event-meter" aria-hidden="true">
<div class="event-meter-fill" id="eventMeter"></div>
</div>
<p class="metric-caption" id="metricCaption">
6 pares favoráveis. A cor muda, mas todas as casas continuam do mesmo tamanho.
</p>
</div>
</section>
<section class="section">
<div class="section-heading">
<h3>Pares favoráveis</h3>
<span id="eventNotation">A₇</span>
</div>
<div class="pairs" id="favorablePairs"></div>
</section>
<section class="section">
<div class="section-heading">
<h3>Por que as probabilidades diferem?</h3>
</div>
<div class="explanation">
A soma é um <strong>evento</strong> formado por várias casas do espaço amostral.
Somar <strong>7</strong> reúne 6 pares; somar <strong>2</strong> reúne apenas 1.
Como cada par continua valendo <strong>1/36</strong>, os eventos têm probabilidades diferentes
sem precisar redimensionar nenhuma casa.
</div>
</section>
<section class="section">
<div class="section-heading">
<h3>Distribuição das somas</h3>
<span>2 a 12</span>
</div>
<div class="hist-wrap">
<div class="histogram" id="histogram" aria-label="Frequência de cada soma"></div>
<p class="hist-note">
A altura das barras mostra quantos pares produzem cada soma. No tabuleiro 3D,
porém, todas as 36 casas têm exatamente a mesma área.
</p>
</div>
</section>
<div class="integrity" id="integrity" role="status">
<span class="integrity-icon" id="integrityIcon">✓</span>
<span id="integrityText">Verificando dados e geometria…</span>
</div>
</div>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const loading = document.getElementById("loading");
const tooltip = document.getElementById("tooltip");
const sumButtonsElement = document.getElementById("sumButtons");
const histogramElement = document.getElementById("histogram");
const favorableCountElement = document.getElementById("favorableCount");
const probabilityElement = document.getElementById("probability");
const eventMeterElement = document.getElementById("eventMeter");
const metricCaptionElement = document.getElementById("metricCaption");
const favorablePairsElement = document.getElementById("favorablePairs");
const eventNotationElement = document.getElementById("eventNotation");
const integrityElement = document.getElementById("integrity");
const integrityIconElement = document.getElementById("integrityIcon");
const integrityTextElement = document.getElementById("integrityText");
const DIE_VALUES = [1, 2, 3, 4, 5, 6];
const outcomes = [];
for (const d1 of DIE_VALUES) {
for (const d2 of DIE_VALUES) {
outcomes.push({ d1, d2, sum: d1 + d2 });
}
}
const sumCounts = Array(13).fill(0);
for (const outcome of outcomes) sumCounts[outcome.sum]++;
const percentFormatter = new Intl.NumberFormat("pt-BR", {
minimumFractionDigits: 1,
maximumFractionDigits: 1
});
let selectedSum = 7;
let hoveredCell = null;
sumButtonsElement.innerHTML = DIE_VALUES.length
? [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
.map((sum) => `
<button class="sum-button" type="button" data-sum="${sum}" aria-pressed="false">
<span>${sum}</span>
<small>${sumCounts[sum]}</small>
</button>
`)
.join("")
: "";
histogramElement.innerHTML = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
.map((sum) => {
const height = 8 + (sumCounts[sum] / 6) * 72;
return `
<button class="hist-item" type="button" data-sum="${sum}" aria-label="Soma ${sum}: ${sumCounts[sum]} pares">
<span class="hist-value">${sumCounts[sum]}</span>
<span class="hist-bar" style="height:${height}px"></span>
<span class="hist-label">${sum}</span>
</button>
`;
})
.join("");
document.querySelectorAll("[data-sum]").forEach((button) => {
button.addEventListener("click", () => selectSum(Number(button.dataset.sum)));
});
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe8eb);
scene.fog = new THREE.Fog(0xdfe8eb, 19, 34);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 60);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
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.setAttribute("aria-label", "Tabuleiro tridimensional com os 36 resultados de dois dados");
renderer.domElement.tabIndex = 0;
stage.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x536b78, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff6df, 3.2);
keyLight.position.set(5.5, 11, 7.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1536, 1536);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 28;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xb9d9e8, 1.35);
rimLight.position.set(-8, 5, -7);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({ color: 0xd4dfe2, roughness: 0.95, metalness: 0 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.34;
ground.receiveShadow = true;
scene.add(ground);
function createRoundedPrism(width, depth, height, radius) {
const w = width / 2;
const d = depth / 2;
const shape = new THREE.Shape();
shape.moveTo(-w + radius, -d);
shape.lineTo(w - radius, -d);
shape.quadraticCurveTo(w, -d, w, -d + radius);
shape.lineTo(w, d - radius);
shape.quadraticCurveTo(w, d, w - radius, d);
shape.lineTo(-w + radius, d);
shape.quadraticCurveTo(-w, d, -w, d - radius);
shape.lineTo(-w, -d + radius);
shape.quadraticCurveTo(-w, -d, -w + radius, -d);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: height,
steps: 1,
curveSegments: 8,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: Math.min(0.035, radius * 0.2),
bevelThickness: 0.035
});
geometry.translate(0, 0, -height / 2);
geometry.rotateX(-Math.PI / 2);
geometry.computeVertexNormals();
return geometry;
}
const board = new THREE.Mesh(
createRoundedPrism(7.25, 7.25, 0.3, 0.28),
new THREE.MeshStandardMaterial({
color: 0x173447,
roughness: 0.68,
metalness: 0.08
})
);
board.position.y = -0.16;
board.castShadow = true;
board.receiveShadow = true;
scene.add(board);
const innerBoard = new THREE.Mesh(
createRoundedPrism(6.9, 6.9, 0.08, 0.2),
new THREE.MeshStandardMaterial({
color: 0x102a3a,
roughness: 0.8,
metalness: 0.04
})
);
innerBoard.position.y = 0.015;
innerBoard.receiveShadow = true;
scene.add(innerBoard);
const cellSize = 0.96;
const cellRadius = 0.14;
const cellGeometry = createRoundedPrism(cellSize, cellSize, 0.2, cellRadius);
const cellBeveledExtent = cellSize + 2 * Math.min(0.035, cellRadius * 0.2);
const cellMeshes = [];
const cellMaterials = [];
const spacing = 1.08;
const normalColor = new THREE.Color(0x496a7c);
const selectedColor = new THREE.Color(0xf2b83f);
for (const outcome of outcomes) {
const material = new THREE.MeshStandardMaterial({
color: normalColor,
roughness: 0.72,
metalness: 0.035,
emissive: 0x000000,
emissiveIntensity: 0
});
const cell = new THREE.Mesh(cellGeometry, material);
cell.position.set(
(outcome.d1 - 3.5) * spacing,
0.14,
(outcome.d2 - 3.5) * spacing
);
cell.castShadow = true;
cell.receiveShadow = true;
cell.userData.outcome = outcome;
cell.userData.isHighlighted = false;
cellMeshes.push(cell);
cellMaterials.push(material);
scene.add(cell);
}
const pipPatterns = {
1: [[0, 0]],
2: [[-1, -1], [1, 1]],
3: [[-1, -1], [0, 0], [1, 1]],
4: [[-1, -1], [-1, 1], [1, -1], [1, 1]],
5: [[-1, -1], [-1, 1], [0, 0], [1, -1], [1, 1]],
6: [[-1, -1], [0, -1], [1, -1], [-1, 1], [0, 1], [1, 1]]
};
const pipTransforms = [];
outcomes.forEach((outcome, cellIndex) => {
const cellX = (outcome.d1 - 3.5) * spacing;
const cellZ = (outcome.d2 - 3.5) * spacing;
for (const [value, halfOffset] of [[outcome.d1, -0.225], [outcome.d2, 0.225]]) {
for (const [patternX, patternZ] of pipPatterns[value]) {
pipTransforms.push({
cellIndex,
x: cellX + halfOffset + patternX * 0.095,
z: cellZ + patternZ * 0.095
});
}
}
});
const pipGeometry = new THREE.SphereGeometry(0.037, 12, 8);
const pipMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.4,
metalness: 0.08
});
const pipMesh = new THREE.InstancedMesh(pipGeometry, pipMaterial, pipTransforms.length);
pipMesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
pipMesh.castShadow = true;
pipMesh.receiveShadow = true;
const pipDummy = new THREE.Object3D();
pipTransforms.forEach((pip, index) => {
pipDummy.position.set(pip.x, 0.292, pip.z);
pipDummy.scale.set(1, 0.58, 1);
pipDummy.updateMatrix();
pipMesh.setMatrixAt(index, pipDummy.matrix);
pipMesh.setColorAt(index, new THREE.Color(0xe3edf0));
});
pipMesh.instanceMatrix.needsUpdate = true;
scene.add(pipMesh);
const dividerGeometry = new THREE.BoxGeometry(0.018, 0.018, 0.79);
const dividerMaterial = new THREE.MeshStandardMaterial({
color: 0x173447,
roughness: 0.7,
metalness: 0.05
});
const dividerMesh = new THREE.InstancedMesh(
dividerGeometry,
dividerMaterial,
cellMeshes.length
);
cellMeshes.forEach((cell, index) => {
pipDummy.position.set(cell.position.x, 0.282, cell.position.z);
pipDummy.scale.set(1, 1, 1);
pipDummy.updateMatrix();
dividerMesh.setMatrixAt(index, pipDummy.matrix);
});
dividerMesh.instanceMatrix.needsUpdate = true;
scene.add(dividerMesh);
const cameraState = {
radius: 13.8,
theta: 0.67,
phi: 0.82,
target: new THREE.Vector3(0, 0.05, 0)
};
function defaultRadius() {
const aspect = stage.clientWidth / Math.max(1, stage.clientHeight);
if (aspect < 0.95) return 17;
if (aspect < 1.3) return 15.2;
return 13.8;
}
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.radius = defaultRadius();
cameraState.theta = 0.67;
cameraState.phi = 0.82;
updateCamera();
}
document.getElementById("resetCamera").addEventListener("click", resetCamera);
function resizeRenderer() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(stage);
resizeRenderer();
resetCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function cellFromPointer(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;
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObjects(cellMeshes, false)[0]?.object ?? null;
}
function updateHover(cell) {
if (hoveredCell === cell) return;
hoveredCell = cell;
for (const mesh of cellMeshes) {
const isHovered = mesh === hoveredCell;
if (mesh.userData.isHighlighted) {
mesh.material.emissive.setHex(0x7a4300);
mesh.material.emissiveIntensity = isHovered ? 0.22 : 0.1;
} else {
mesh.material.emissive.setHex(0x6b9fb1);
mesh.material.emissiveIntensity = isHovered ? 0.2 : 0;
}
}
if (!cell) {
tooltip.style.display = "none";
return;
}
const outcome = cell.userData.outcome;
tooltip.innerHTML = `
<strong>D1 = ${outcome.d1} · D2 = ${outcome.d2}</strong><br>
Soma = ${outcome.sum}<small>Este par vale 1/36.</small>
`;
tooltip.style.display = "block";
}
function positionTooltip(event) {
const rect = stage.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const maxX = Math.max(10, rect.width - 205);
const maxY = Math.max(10, rect.height - 95);
tooltip.style.left = `${Math.min(maxX, Math.max(10, x + 15))}px`;
tooltip.style.top = `${Math.min(maxY, Math.max(10, y + 15))}px`;
}
const dragState = {
active: false,
pointerId: null,
lastX: 0,
lastY: 0,
moved: false
};
renderer.domElement.addEventListener("pointerdown", (event) => {
dragState.active = true;
dragState.pointerId = event.pointerId;
dragState.lastX = event.clientX;
dragState.lastY = event.clientY;
dragState.moved = false;
renderer.domElement.classList.add("dragging");
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (dragState.active && event.pointerId === dragState.pointerId) {
const dx = event.clientX - dragState.lastX;
const dy = event.clientY - dragState.lastY;
if (Math.abs(dx) + Math.abs(dy) > 3) dragState.moved = true;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.36,
1.27
);
dragState.lastX = event.clientX;
dragState.lastY = event.clientY;
updateCamera();
updateHover(null);
} else {
const cell = cellFromPointer(event);
updateHover(cell);
if (cell) positionTooltip(event);
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!dragState.active || event.pointerId !== dragState.pointerId) return;
if (!dragState.moved) {
const cell = cellFromPointer(event);
if (cell) selectSum(cell.userData.outcome.sum);
}
dragState.active = false;
renderer.domElement.classList.remove("dragging");
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragState.active = false;
renderer.domElement.classList.remove("dragging");
updateHover(null);
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragState.active) updateHover(null);
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
9.5,
22
);
updateCamera();
},
{ passive: false }
);
function updateIntegrityStatus() {
const favorable = outcomes.filter((outcome) => outcome.sum === selectedSum);
const highlightedCount = cellMeshes.filter((cell) => cell.userData.isHighlighted).length;
const fixedGeometry =
Math.abs(cellGeometry.boundingBox.max.x - cellGeometry.boundingBox.min.x - cellBeveledExtent) < 0.001 &&
Math.abs(cellGeometry.boundingBox.max.z - cellGeometry.boundingBox.min.z - cellBeveledExtent) < 0.001 &&
cellMeshes.every((cell) => cell.scale.x === 1 && cell.scale.y === 1 && cell.scale.z === 1);
const valid =
outcomes.length === 36 &&
sumCounts[2] === 1 &&
sumCounts[7] === 6 &&
favorable.length === highlightedCount &&
fixedGeometry;
integrityElement.classList.toggle("bad", !valid);
integrityIconElement.textContent = valid ? "✓" : "!";
if (valid) {
integrityTextElement.textContent =
`Teste consistente: ${highlightedCount} casa${highlightedCount === 1 ? "" : "s"} ` +
`= ${highlightedCount}/36; P(2) = 1/36; P(7) = 6/36. Todas as casas têm o mesmo tamanho.`;
} else {
integrityTextElement.textContent =
"Falha na verificação: revise a contagem dos pares ou a geometria das casas.";
}
}
function selectSum(sum) {
selectedSum = sum;
const favorable = outcomes.filter((outcome) => outcome.sum === selectedSum);
const probability = favorable.length / outcomes.length;
const percentage = probability * 100;
cellMeshes.forEach((cell, index) => {
const highlighted = cell.userData.outcome.sum === selectedSum;
cell.userData.isHighlighted = highlighted;
cell.material.color.copy(highlighted ? selectedColor : normalColor);
cell.material.emissive.setHex(highlighted ? 0x7a4300 : 0x000000);
cell.material.emissiveIntensity = highlighted ? 0.1 : 0;
});
pipTransforms.forEach((pip, index) => {
const highlighted = cellMeshes[pip.cellIndex].userData.isHighlighted;
pipMesh.setColorAt(
index,
new THREE.Color(highlighted ? 0x173447 : 0xe3edf0)
);
});
pipMesh.instanceColor.needsUpdate = true;
document.querySelectorAll(".sum-button, .hist-item").forEach((button) => {
const active = Number(button.dataset.sum) === selectedSum;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
favorableCountElement.textContent = String(favorable.length);
probabilityElement.textContent = `${percentFormatter.format(percentage)}%`;
eventMeterElement.style.width = `${percentage}%`;
metricCaptionElement.textContent =
`${favorable.length} par${favorable.length === 1 ? "" : "es"} favorável${favorable.length === 1 ? "" : "is"}. ` +
`A cor muda, mas todas as casas continuam do mesmo tamanho.`;
eventNotationElement.textContent = `A${selectedSum}`;
favorablePairsElement.innerHTML = favorable
.map(
(outcome) => `
<span class="pair-chip">
<strong>${outcome.d1}</strong><em>+</em><strong>${outcome.d2}</strong>
</span>
`
)
.join("");
updateHover(null);
updateIntegrityStatus();
}
cellGeometry.computeBoundingBox();
selectSum(7);
loading.remove();
function animate(time) {
const pulse = 0.1 + Math.sin(time * 0.0022) * 0.018;
for (const cell of cellMeshes) {
if (cell.userData.isHighlighted && cell !== hoveredCell) {
cell.material.emissiveIntensity = pulse;
}
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```