Prompt specification
Build or customize the Three.js 3D scene "Two-die outcome grid sculpture" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Two-die outcome grid sculpture
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-die-outcome-grid-sculpture-x1825
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: two-die outcome grid sculpture. Build a six-by-six grid of paired die outcomes with equal cell sizes; distinguish the thirty-six ordered outcomes from the eleven possible sums. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (two-die-outcome-grid-sculpture-x1825):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Two-Die Outcome Grid</title>
<style>
:root {
color-scheme: dark;
--ink: #edf5ff;
--muted: #91a3ba;
--panel: rgba(12, 20, 35, 0.78);
--line: rgba(157, 185, 220, 0.18);
--a: #e8dfcf;
--b: #b9dde2;
}
* { box-sizing: border-box; }
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0b1020;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging { cursor: grabbing; }
.intro {
position: fixed;
z-index: 5;
top: 24px;
left: 26px;
max-width: 430px;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.55);
}
.eyebrow {
margin-bottom: 8px;
color: #7ed8e3;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(28px, 4vw, 49px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
.copy {
max-width: 395px;
margin: 13px 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.stats {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.stat {
display: flex;
align-items: baseline;
gap: 7px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--panel);
backdrop-filter: blur(10px);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.stat strong {
color: #fff;
font-size: 16px;
letter-spacing: -0.04em;
}
.legend {
position: fixed;
z-index: 5;
right: 26px;
bottom: 25px;
display: flex;
align-items: center;
gap: 17px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: var(--panel);
backdrop-filter: blur(10px);
pointer-events: none;
font-size: 11px;
color: #b7c4d5;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
white-space: nowrap;
}
.swatch {
width: 11px;
height: 11px;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(255,255,255,.14);
}
.swatch.a { background: var(--a); }
.swatch.b { background: var(--b); }
.spectrum {
width: 52px;
height: 8px;
border-radius: 99px;
background: linear-gradient(90deg,
#6657d9, #4776dc, #348fc4, #2ca596, #3da879,
#72ad48, #a58b38, #bc7138, #b9553f, #a84455, #8b456f);
}
.help {
position: fixed;
z-index: 5;
left: 26px;
bottom: 27px;
color: #71839b;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
#reset {
position: fixed;
z-index: 7;
top: 25px;
right: 26px;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--panel);
color: #c9d7e8;
font: 700 10px/1 inherit;
letter-spacing: 0.12em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(10px);
}
#reset:hover {
color: white;
border-color: rgba(126, 216, 227, 0.55);
}
#tooltip {
position: fixed;
z-index: 9;
display: none;
min-width: 165px;
padding: 9px 11px;
border: 1px solid rgba(126, 216, 227, 0.28);
border-radius: 8px;
background: rgba(7, 12, 23, 0.92);
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
pointer-events: none;
backdrop-filter: blur(12px);
}
#tooltip.visible { display: block; }
#tooltip b {
display: block;
margin-bottom: 3px;
color: #fff;
font-size: 13px;
}
#tooltip span {
color: #91a7be;
font-size: 10px;
letter-spacing: 0.03em;
}
@media (max-width: 720px) {
.intro {
top: 17px;
left: 17px;
max-width: calc(100vw - 34px);
}
h1 { font-size: 30px; }
.copy { display: none; }
.stats { margin-top: 12px; }
.stat { padding: 6px 8px; }
.stat strong { font-size: 14px; }
.legend {
right: 14px;
bottom: 15px;
gap: 10px;
padding: 8px 10px;
}
.legend-item { font-size: 9px; }
.help { display: none; }
#reset {
top: auto;
right: 14px;
bottom: 62px;
}
}
</style>
</head>
<body>
<div class="intro">
<div class="eyebrow">Low-poly probability study</div>
<h1>Six by six,<br>twice interpreted.</h1>
<p class="copy">
Every equal tile is one ordered die pair: the warm die is A and the cool die is B.
The colored hex shows that pair’s sum; the separate spectrum reduces all
36 outcomes to 11 possible totals.
</p>
<div class="stats">
<div class="stat"><strong>36</strong> ordered outcomes</div>
<div class="stat"><strong>11</strong> possible sums</div>
</div>
</div>
<button id="reset" type="button">Reset view</button>
<div class="legend" aria-label="Scene legend">
<div class="legend-item"><span class="swatch a"></span>Die A</div>
<div class="legend-item"><span class="swatch b"></span>Die B</div>
<div class="legend-item"><span class="spectrum"></span>Sum class</div>
</div>
<div class="help">Drag to orbit · Shift-drag to pan · Scroll to zoom · R to reset</div>
<div id="tooltip"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, and camera
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1020);
scene.fog = new THREE.Fog(0x0b1020, 31, 58);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
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",
"Interactive three-dimensional grid of thirty-six ordered two-die outcomes"
);
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xbadcff, 0x201625, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffead2, 3.4);
keyLight.position.set(-7, 18, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -18;
keyLight.shadow.camera.right = 18;
keyLight.shadow.camera.top = 18;
keyLight.shadow.camera.bottom = -18;
keyLight.shadow.camera.near = 2;
keyLight.shadow.camera.far = 48;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6fc7ff, 1.55);
rimLight.position.set(11, 9, -13);
scene.add(rimLight);
const baseRadius = 24.5;
const state = {
target: new THREE.Vector3(-1.5, 0.25, 0),
theta: 0.18,
phi: 0.84,
zoom: 1,
distanceScale: 1
};
function updateCamera() {
const radius = baseRadius * state.distanceScale * state.zoom;
const sinPhi = Math.sin(state.phi);
camera.position.set(
state.target.x + radius * sinPhi * Math.sin(state.theta),
state.target.y + radius * Math.cos(state.phi),
state.target.z + radius * sinPhi * Math.cos(state.theta)
);
camera.lookAt(state.target);
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
// Maintain a useful framing on narrow screens.
state.distanceScale = Math.max(1, 1.3 / camera.aspect);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
updateCamera();
}
addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const sumColors = [
0x6657d9, // 2
0x4776dc, // 3
0x348fc4, // 4
0x2ca596, // 5
0x3da879, // 6
0x72ad48, // 7
0xa58b38, // 8
0xbc7138, // 9
0xb9553f, // 10
0xa84455, // 11
0x8b456f // 12
];
const dieAMaterial = new THREE.MeshStandardMaterial({
color: 0xe8dfcf,
roughness: 0.66,
metalness: 0.03,
flatShading: true
});
const dieBMaterial = new THREE.MeshStandardMaterial({
color: 0xb9dde2,
roughness: 0.63,
metalness: 0.04,
flatShading: true
});
const pipMaterial = new THREE.MeshStandardMaterial({
color: 0x142033,
roughness: 0.8,
metalness: 0.02,
flatShading: true
});
const dieEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x253247,
transparent: true,
opacity: 0.52
});
function makeChamferedBox(width, height, depth, bevel = 0.07) {
const innerWidth = width - bevel * 2;
const innerHeight = height - bevel * 2;
const x = innerWidth / 2;
const y = innerHeight / 2;
const shape = new THREE.Shape();
shape.moveTo(-x, -y);
shape.lineTo(x, -y);
shape.lineTo(x, y);
shape.lineTo(-x, y);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: Math.max(0.01, depth - bevel * 2),
bevelEnabled: true,
bevelThickness: bevel,
bevelSize: bevel,
bevelSegments: 1,
steps: 1,
curveSegments: 1
});
geometry.center();
geometry.computeBoundingBox();
const size = new THREE.Vector3();
geometry.boundingBox.getSize(size);
geometry.scale(width / size.x, height / size.y, depth / size.z);
geometry.computeVertexNormals();
return geometry;
}
const dieSize = 0.76;
const dieGeometry = makeChamferedBox(dieSize, dieSize, dieSize, 0.075);
const dieEdgesGeometry = new THREE.EdgesGeometry(dieGeometry, 24);
const pipGeometry = new THREE.CylinderGeometry(
0.061, 0.066, 0.032, 8, 1, false
);
const textureCache = new Map();
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function getTextTexture(
text,
{
width = 256,
height = 128,
fontSize = 62,
color = "#ffffff",
background = null,
border = null,
weight = 700,
shadow = false
} = {}
) {
const key = JSON.stringify([
text, width, height, fontSize, color, background, border, weight, shadow
]);
if (textureCache.has(key)) return textureCache.get(key);
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, width, height);
if (background) {
ctx.fillStyle = background;
roundedRect(ctx, 2, 2, width - 4, height - 4, 18);
ctx.fill();
}
if (border) {
ctx.strokeStyle = border;
ctx.lineWidth = 3;
roundedRect(ctx, 3, 3, width - 6, height - 6, 17);
ctx.stroke();
}
ctx.font = `${weight} ${fontSize}px Inter, ui-sans-serif, system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
if (shadow) {
ctx.shadowColor = "rgba(0,0,0,.75)";
ctx.shadowBlur = 8;
ctx.shadowOffsetY = 3;
}
ctx.fillText(text, width / 2, height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
texture.anisotropy = Math.min(4, renderer.capabilities.getMaxAnisotropy());
textureCache.set(key, texture);
return texture;
}
function makeSprite(text, worldWidth, worldHeight, options = {}) {
const texture = getTextTexture(text, options);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldHeight, 1);
return sprite;
}
function makeFlatText(text, worldWidth, worldHeight, options = {}) {
const texture = getTextTexture(text, options);
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(worldWidth, worldHeight),
material
);
mesh.rotation.x = -Math.PI / 2;
return mesh;
}
const pipPatterns = {
1: [[0, 0]],
2: [[-0.20, -0.20], [0.20, 0.20]],
3: [[-0.20, -0.20], [0, 0], [0.20, 0.20]],
4: [[-0.20, -0.20], [-0.20, 0.20], [0.20, -0.20], [0.20, 0.20]],
5: [
[-0.20, -0.20], [-0.20, 0.20], [0, 0],
[0.20, -0.20], [0.20, 0.20]
],
6: [
[-0.20, -0.22], [-0.20, 0], [-0.20, 0.22],
[0.20, -0.22], [0.20, 0], [0.20, 0.22]
]
};
function createDie(value, material) {
const group = new THREE.Group();
const body = new THREE.Mesh(dieGeometry, material);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const edges = new THREE.LineSegments(dieEdgesGeometry, dieEdgeMaterial);
group.add(edges);
for (const [x, z] of pipPatterns[value]) {
const pip = new THREE.Mesh(pipGeometry, pipMaterial);
pip.position.set(x, dieSize / 2 + 0.017, z);
group.add(pip);
}
return group;
}
// -------------------------------------------------------------------------
// Environment
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.CircleGeometry(27, 48),
new THREE.MeshStandardMaterial({
color: 0x0e1728,
roughness: 0.96,
metalness: 0,
flatShading: true
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.245;
ground.receiveShadow = true;
scene.add(ground);
const floorGrid = new THREE.GridHelper(48, 24, 0x26354a, 0x1c2a3e);
floorGrid.position.y = -0.238;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.13;
scene.add(floorGrid);
// -------------------------------------------------------------------------
// Six-by-six ordered-outcome grid
// -------------------------------------------------------------------------
const gridCenterX = -4.2;
const gridCenterZ = 0;
const cellPitch = 2.05;
const cellSize = 1.91;
const gridSpan = cellPitch * 6;
const gridLeft = gridCenterX - gridSpan / 2;
const gridTop = gridCenterZ - gridSpan / 2;
const gridBase = new THREE.Mesh(
new THREE.BoxGeometry(gridSpan + 0.18, 0.13, gridSpan + 0.18),
new THREE.MeshStandardMaterial({
color: 0x101a2c,
roughness: 0.88,
metalness: 0.02
})
);
gridBase.position.set(gridCenterX, -0.165, gridCenterZ);
gridBase.receiveShadow = true;
scene.add(gridBase);
const tileGeometry = new THREE.BoxGeometry(cellSize, 0.12, cellSize);
const tileEdgeGeometry = new THREE.EdgesGeometry(tileGeometry);
const tileEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x40536d,
transparent: true,
opacity: 0.34
});
const chipGeometry = new THREE.CylinderGeometry(
0.235, 0.235, 0.065, 6, 1, false
);
const interactiveTiles = [];
for (let row = 0; row < 6; row++) {
const a = row + 1;
for (let column = 0; column < 6; column++) {
const b = column + 1;
const sum = a + b;
const x = gridCenterX + (column - 2.5) * cellPitch;
const z = gridCenterZ + (row - 2.5) * cellPitch;
const tileMaterial = new THREE.MeshStandardMaterial({
color: (row + column) % 2 === 0 ? 0x172338 : 0x19263c,
roughness: 0.82,
metalness: 0.025,
emissive: 0x000000,
emissiveIntensity: 0
});
const tile = new THREE.Mesh(tileGeometry, tileMaterial);
tile.position.set(x, -0.06, z);
tile.receiveShadow = true;
tile.userData = { a, b, sum };
interactiveTiles.push(tile);
scene.add(tile);
const tileEdges = new THREE.LineSegments(
tileEdgeGeometry,
tileEdgeMaterial
);
tileEdges.position.copy(tile.position);
scene.add(tileEdges);
const dieA = createDie(a, dieAMaterial);
dieA.position.set(x - 0.39, 0.385, z - 0.10);
scene.add(dieA);
const dieB = createDie(b, dieBMaterial);
dieB.position.set(x + 0.39, 0.385, z - 0.10);
scene.add(dieB);
const chipMaterial = new THREE.MeshStandardMaterial({
color: sumColors[sum - 2],
roughness: 0.66,
metalness: 0.04,
flatShading: true
});
const chip = new THREE.Mesh(chipGeometry, chipMaterial);
chip.position.set(x, 0.032, z + 0.71);
chip.castShadow = true;
scene.add(chip);
const sumLabel = makeFlatText(String(sum), 0.29, 0.18, {
width: 128,
height: 80,
fontSize: 56,
color: "#ffffff",
weight: 800
});
sumLabel.position.set(x, 0.068, z + 0.71);
scene.add(sumLabel);
}
}
// Row headers identify the first die; column headers identify the second.
for (let i = 0; i < 6; i++) {
const rowLabel = makeSprite(`A${i + 1}`, 0.76, 0.36, {
width: 220,
height: 100,
fontSize: 48,
color: "#f0e8db",
background: "rgba(17,25,40,.92)",
border: "rgba(232,223,207,.48)"
});
rowLabel.position.set(
gridLeft - 0.62,
0.34,
gridCenterZ + (i - 2.5) * cellPitch
);
scene.add(rowLabel);
const columnLabel = makeSprite(`B${i + 1}`, 0.76, 0.36, {
width: 220,
height: 100,
fontSize: 48,
color: "#d9f5f8",
background: "rgba(17,25,40,.92)",
border: "rgba(185,221,226,.48)"
});
columnLabel.position.set(
gridCenterX + (i - 2.5) * cellPitch,
0.34,
gridTop - 0.65
);
scene.add(columnLabel);
}
const gridTitle = makeSprite("36 ORDERED OUTCOMES", 4.25, 0.55, {
width: 512,
height: 96,
fontSize: 42,
color: "#edf5ff",
background: "rgba(11,18,32,.88)",
border: "rgba(126,216,227,.34)",
shadow: true
});
gridTitle.position.set(gridCenterX, 0.34, gridTop + gridSpan + 0.66);
scene.add(gridTitle);
// -------------------------------------------------------------------------
// Separate eleven-sum spectrum
// -------------------------------------------------------------------------
const panelCenterX = 5.05;
const panelCenterZ = 0.1;
const panelWidth = 5.15;
const panelDepth = 6.75;
const panelBase = new THREE.Mesh(
new THREE.BoxGeometry(panelWidth, 0.16, panelDepth),
new THREE.MeshStandardMaterial({
color: 0x111c2f,
roughness: 0.86,
metalness: 0.025
})
);
panelBase.position.set(panelCenterX, -0.08, panelCenterZ);
panelBase.receiveShadow = true;
scene.add(panelBase);
const panelEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(panelBase.geometry),
new THREE.LineBasicMaterial({
color: 0x40536d,
transparent: true,
opacity: 0.42
})
);
panelEdges.position.copy(panelBase.position);
scene.add(panelEdges);
const panelTitle = makeSprite("11 SUM CLASSES", 3.55, 0.52, {
width: 512,
height: 96,
fontSize: 43,
color: "#f4f7ff",
background: "rgba(12,20,35,.94)",
border: "rgba(157,185,220,.3)",
shadow: true
});
panelTitle.position.set(panelCenterX, 0.35, panelCenterZ - 2.78);
scene.add(panelTitle);
const panelSubtitle = makeSprite("FREQUENCY OF TOTALS 2–12", 3.45, 0.38, {
width: 512,
height: 80,
fontSize: 34,
color: "#91a3ba",
background: "rgba(12,20,35,.88)"
});
panelSubtitle.position.set(panelCenterX, 0.28, panelCenterZ + 2.72);
scene.add(panelSubtitle);
const frequencies = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1];
const barGeometry = new THREE.BoxGeometry(0.25, 1, 0.68);
const barPadGeometry = new THREE.BoxGeometry(0.34, 0.055, 1.0);
for (let i = 0; i < 11; i++) {
const sum = i + 2;
const count = frequencies[i];
const x = panelCenterX + (i - 5) * 0.43;
const height = 0.55 + count * 0.38;
const z = panelCenterZ - 0.10;
const pad = new THREE.Mesh(
barPadGeometry,
new THREE.MeshStandardMaterial({
color: 0x243249,
roughness: 0.8,
metalness: 0.03
})
);
pad.position.set(x, 0.027, z);
pad.receiveShadow = true;
scene.add(pad);
const barMaterial = new THREE.MeshStandardMaterial({
color: sumColors[i],
roughness: 0.61,
metalness: 0.05,
flatShading: true
});
const bar = new THREE.Mesh(barGeometry, barMaterial);
bar.scale.y = height;
bar.position.set(x, 0.055 + height / 2, z);
bar.castShadow = true;
bar.receiveShadow = true;
scene.add(bar);
const countLabel = makeSprite(String(count), 0.30, 0.30, {
width: 128,
height: 96,
fontSize: 55,
color: "#eef5ff",
shadow: true
});
countLabel.position.set(x, 0.34 + height, z);
scene.add(countLabel);
const sumLabel = makeFlatText(String(sum), 0.34, 0.20, {
width: 128,
height: 80,
fontSize: 54,
color: "#dce8f7",
weight: 800
});
sumLabel.position.set(x, 0.063, panelCenterZ + 0.76);
scene.add(sumLabel);
}
// -------------------------------------------------------------------------
// Custom camera controls — no addons or examples modules
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
const pointers = new Map();
let dragMode = "orbit";
let lastX = 0;
let lastY = 0;
let lastPinchDistance = 0;
let isDragging = false;
function resetCamera() {
state.target.set(-1.5, 0.25, 0);
state.theta = 0.18;
state.phi = 0.84;
state.zoom = 1;
updateCamera();
}
function panCamera(dx, dy) {
camera.updateMatrixWorld();
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0);
right.y = 0;
right.normalize();
const forward = camera.getWorldDirection(new THREE.Vector3());
forward.y = 0;
forward.normalize();
const scale = baseRadius * state.distanceScale * state.zoom * 0.00145;
state.target.addScaledVector(right, -dx * scale);
state.target.addScaledVector(forward, dy * scale);
}
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
isDragging = true;
lastX = event.clientX;
lastY = event.clientY;
dragMode = event.shiftKey || event.button === 2 ? "pan" : "orbit";
canvas.classList.add("dragging");
hideTooltip();
} else if (pointers.size === 2) {
const [first, second] = [...pointers.values()];
lastPinchDistance = Math.hypot(
first.x - second.x,
first.y - second.y
);
}
});
canvas.addEventListener("pointermove", event => {
const previous = pointers.get(event.pointerId);
if (!previous) {
updatePointer(event);
return;
}
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (dragMode === "pan") {
panCamera(dx, dy);
} else {
state.theta -= dx * 0.005;
state.phi = THREE.MathUtils.clamp(
state.phi - dy * 0.005,
0.30,
1.38
);
}
} else if (pointers.size === 2) {
const [first, second] = [...pointers.values()];
const distance = Math.hypot(
first.x - second.x,
first.y - second.y
);
if (lastPinchDistance > 0 && distance > 0) {
state.zoom = THREE.MathUtils.clamp(
state.zoom * (lastPinchDistance / distance),
0.55,
1.85
);
}
lastPinchDistance = distance;
}
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
lastPinchDistance = 0;
if (pointers.size === 0) {
isDragging = false;
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
state.zoom = THREE.MathUtils.clamp(
state.zoom * Math.exp(event.deltaY * 0.001),
0.55,
1.85
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#reset").addEventListener("click", resetCamera);
addEventListener("keydown", event => {
if (event.key.toLowerCase() === "r") resetCamera();
});
// -------------------------------------------------------------------------
// Hover inspection
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2(2, 2);
const tooltip = document.querySelector("#tooltip");
let hoveredTile = null;
let pointerClientX = 0;
let pointerClientY = 0;
let pointerDirty = false;
function updatePointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
pointerClientX = event.clientX;
pointerClientY = event.clientY;
pointerDirty = true;
}
canvas.addEventListener("pointermove", event => {
if (!isDragging) updatePointer(event);
});
canvas.addEventListener("pointerleave", () => {
pointer.set(2, 2);
pointerDirty = true;
hideTooltip();
});
function hideTooltip() {
if (hoveredTile) {
hoveredTile.material.emissive.setHex(0x000000);
hoveredTile.material.emissiveIntensity = 0;
hoveredTile = null;
}
tooltip.classList.remove("visible");
}
function inspectPointer() {
if (!pointerDirty || isDragging) return;
pointerDirty = false;
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(interactiveTiles, false)[0];
if (!hit) {
hideTooltip();
return;
}
const tile = hit.object;
const { a, b, sum } = tile.userData;
if (hoveredTile !== tile) {
if (hoveredTile) {
hoveredTile.material.emissive.setHex(0x000000);
hoveredTile.material.emissiveIntensity = 0;
}
hoveredTile = tile;
hoveredTile.material.emissive.setHex(0x315174);
hoveredTile.material.emissiveIntensity = 0.62;
}
tooltip.innerHTML =
`<b>A${a} + B${b} = ${sum}</b>` +
`<span>ordered outcome (${a}, ${b}) → sum ${sum}</span>`;
tooltip.style.left =
Math.min(innerWidth - 205, pointerClientX + 15) + "px";
tooltip.style.top =
Math.min(innerHeight - 70, pointerClientY + 15) + "px";
tooltip.classList.add("visible");
}
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
inspectPointer();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```