Prompt specification
Build or customize the Three.js 3D scene "Compare two reconstructions of uncertain rules" 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: Compare two reconstructions of uncertain rules
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/compare-two-reconstructions-of-uncertain-rules-x2244
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing ancient board games and reconstructed rules. Let users switch between two labeled rulesets on the same ancient-style board. Show how legal moves change while preserving the identical physical object and starting arrangement. Low-poly.
Working Three.js Starter (compare-two-reconstructions-of-uncertain-rules-x2244):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>The Ur Table — Two Reconstructed Rulesets</title>
<style>
:root {
--ink: #f1e8d3;
--muted: #a99f8d;
--panel: rgba(19, 20, 23, 0.88);
--line: rgba(236, 215, 171, 0.17);
--gold: #e5b85c;
--lapis: #57a9df;
--carnelian: #e47752;
--green: #85d6a4;
--danger: #f08b76;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 68% 38%, #252a2b 0%, #15191b 48%, #090b0d 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(90deg, rgba(4, 6, 8, 0.38), transparent 37%),
radial-gradient(circle at center, transparent 55%, rgba(3, 5, 7, 0.4) 100%);
}
.brand {
position: fixed;
top: 24px;
left: 27px;
z-index: 4;
pointer-events: none;
text-shadow: 0 2px 18px #000;
}
.brand .eyebrow {
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
}
.brand h1 {
margin: 5px 0 1px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 3vw, 46px);
font-weight: 500;
letter-spacing: 0.03em;
}
.brand p {
margin: 0;
color: #c3b9a6;
font-size: 12px;
letter-spacing: 0.08em;
}
.panel {
position: fixed;
z-index: 5;
top: 20px;
bottom: 20px;
left: 20px;
width: 365px;
padding: 18px;
overflow: auto;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
backdrop-filter: blur(18px) saturate(120%);
scrollbar-width: thin;
scrollbar-color: #5e574b transparent;
}
.section-label {
display: flex;
align-items: center;
gap: 8px;
margin: 0 0 9px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.section-label::after {
content: "";
height: 1px;
flex: 1;
background: var(--line);
}
.rules-tabs {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-bottom: 10px;
}
.rules-tab {
min-height: 62px;
padding: 10px;
text-align: left;
color: var(--muted);
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
transition: 160ms ease;
}
.rules-tab:hover {
border-color: rgba(229, 184, 92, 0.4);
color: var(--ink);
}
.rules-tab.active {
color: var(--ink);
border-color: rgba(229, 184, 92, 0.72);
background:
linear-gradient(135deg, rgba(229, 184, 92, 0.15), rgba(229, 184, 92, 0.035));
box-shadow: inset 0 0 0 1px rgba(229, 184, 92, 0.08);
}
.rules-tab strong {
display: block;
margin-bottom: 5px;
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
font-weight: 500;
}
.rules-tab span {
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.object-lock {
display: flex;
gap: 10px;
align-items: center;
padding: 10px 11px;
margin-bottom: 14px;
border: 1px solid rgba(133, 214, 164, 0.2);
border-radius: 10px;
background: rgba(133, 214, 164, 0.055);
color: #b9cfbd;
font-size: 10px;
line-height: 1.45;
}
.lock-icon {
flex: 0 0 auto;
width: 24px;
height: 24px;
display: grid;
place-items: center;
border: 1px solid rgba(133, 214, 164, 0.35);
border-radius: 50%;
color: var(--green);
font-size: 11px;
}
.status-row {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
margin-bottom: 12px;
}
.turn-label {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#turnName {
display: flex;
align-items: center;
gap: 8px;
margin-top: 4px;
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
}
#turnDot {
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--lapis);
box-shadow: 0 0 18px currentColor;
}
#rulesBadge {
padding: 6px 8px;
border: 1px solid var(--line);
border-radius: 7px;
color: var(--gold);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.dice-panel {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: center;
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
#diceMarks {
display: flex;
gap: 7px;
margin-bottom: 8px;
}
.die-mark {
width: 24px;
height: 24px;
display: grid;
place-items: center;
border: 1px solid var(--line);
border-radius: 7px;
color: #5f5b53;
background: #18191a;
font-size: 13px;
transition: 160ms ease;
}
.die-mark.marked {
color: #15120d;
border-color: var(--gold);
background: var(--gold);
box-shadow: 0 0 16px rgba(229, 184, 92, 0.22);
}
.dice-caption {
color: var(--muted);
font-size: 9px;
line-height: 1.45;
}
#rollValue {
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
font-size: 24px;
}
.primary-button {
width: 100%;
margin-top: 8px;
padding: 11px 14px;
border: 1px solid #efd089;
border-radius: 10px;
color: #18140d;
background: linear-gradient(180deg, #f0ce7e, #d6a747);
box-shadow: 0 7px 24px rgba(214, 167, 71, 0.16);
cursor: pointer;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
transition: 150ms ease;
}
.primary-button:hover:not(:disabled) {
transform: translateY(-1px);
filter: brightness(1.07);
}
.primary-button:disabled {
opacity: 0.42;
cursor: default;
}
kbd {
margin-left: 7px;
padding: 2px 5px;
border: 1px solid rgba(0, 0, 0, 0.25);
border-radius: 4px;
font-size: 9px;
font-family: inherit;
}
.rule-card {
margin-top: 14px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.018);
}
.rule-card h2 {
margin: 0 0 4px;
font-family: Georgia, "Times New Roman", serif;
font-size: 17px;
font-weight: 500;
}
.rule-card .era {
margin-bottom: 10px;
color: var(--gold);
font-size: 9px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.rule-card ul {
margin: 0;
padding-left: 16px;
color: #c2b8a7;
font-size: 10px;
line-height: 1.6;
}
.rule-card li + li {
margin-top: 4px;
}
.moves-header {
display: flex;
justify-content: space-between;
align-items: center;
}
#legalMoves {
display: grid;
gap: 6px;
min-height: 42px;
}
.empty-moves {
padding: 10px;
color: #7f796f;
border: 1px dashed var(--line);
border-radius: 9px;
font-size: 10px;
line-height: 1.45;
}
.move-chip {
display: grid;
grid-template-columns: 42px 1fr auto;
align-items: center;
gap: 9px;
width: 100%;
padding: 8px 10px;
color: var(--ink);
text-align: left;
border: 1px solid rgba(133, 214, 164, 0.22);
border-radius: 9px;
background: rgba(133, 214, 164, 0.055);
cursor: pointer;
transition: 140ms ease;
}
.move-chip:hover {
border-color: rgba(133, 214, 164, 0.62);
background: rgba(133, 214, 164, 0.11);
transform: translateX(2px);
}
.token-dot {
color: var(--green);
font-size: 10px;
font-weight: 900;
}
.move-chip strong {
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
font-weight: 500;
}
.move-chip small {
color: #8e9e8e;
font-size: 9px;
}
.controls {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-top: 8px;
}
.secondary-button {
padding: 9px 6px;
color: #bdb3a2;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.secondary-button:hover,
.secondary-button.active {
color: var(--gold);
border-color: rgba(229, 184, 92, 0.45);
background: rgba(229, 184, 92, 0.08);
}
.log {
max-height: 110px;
overflow: auto;
}
.log-entry {
padding: 7px 0;
color: #958d80;
border-bottom: 1px solid rgba(255, 255, 255, 0.045);
font-size: 9px;
line-height: 1.4;
}
.log-entry:first-child {
color: #c0b6a4;
}
.object-note {
position: fixed;
z-index: 3;
top: 25px;
right: 26px;
padding: 10px 13px;
color: #c9bea9;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(12, 14, 16, 0.65);
backdrop-filter: blur(10px);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
pointer-events: none;
}
.object-note span {
color: var(--green);
}
.help {
position: fixed;
z-index: 3;
right: 25px;
bottom: 21px;
color: rgba(225, 216, 198, 0.55);
font-size: 9px;
letter-spacing: 0.06em;
pointer-events: none;
text-align: right;
}
#toast {
position: fixed;
z-index: 10;
left: 50%;
bottom: 26px;
max-width: min(90vw, 520px);
padding: 11px 16px;
color: #eee3cc;
border: 1px solid rgba(229, 184, 92, 0.35);
border-radius: 999px;
background: rgba(18, 19, 21, 0.92);
box-shadow: 0 12px 45px rgba(0, 0, 0, 0.4);
opacity: 0;
transform: translate(-50%, 14px);
transition: 220ms ease;
pointer-events: none;
font-size: 10px;
text-align: center;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 800px) {
.brand {
top: 14px;
left: 16px;
}
.brand h1 {
font-size: 28px;
}
.brand p,
.object-note {
display: none;
}
.panel {
top: auto;
left: 10px;
right: 10px;
bottom: 10px;
width: auto;
max-height: 52vh;
padding: 14px;
border-radius: 14px;
}
.help {
display: none;
}
.rules-tab {
min-height: 52px;
}
.rule-card ul {
font-size: 9px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly reconstruction of the Royal Game of Ur"></canvas>
<div id="vignette"></div>
<header class="brand">
<div class="eyebrow">Ancient Games Lab · Reconstruction 01</div>
<h1>THE UR TABLE</h1>
<p>One board. Two laws.</p>
</header>
<div class="object-note">
<span>●</span> 20-square Ur board · identical setup
</div>
<aside class="panel" aria-label="Game controls">
<div class="section-label">Choose reconstructed rules</div>
<div class="rules-tabs" role="tablist" aria-label="Rulesets">
<button class="rules-tab active" data-rules="classical" role="tab" aria-selected="true">
<strong>I · Direct Race</strong>
<span>Common reconstruction</span>
</button>
<button class="rules-tab" data-rules="variant" role="tab" aria-selected="false">
<strong>II · Gate & Contest</strong>
<span>Experimental variant</span>
</button>
</div>
<div class="object-lock">
<div class="lock-icon">▣</div>
<div>
<strong>Object lock:</strong> changing rules never rebuilds or resets the board,
pieces, or current positions.
</div>
</div>
<div class="status-row">
<div>
<div class="turn-label">Turn</div>
<div id="turnName"><span id="turnDot"></span><span>Lapis to move</span></div>
</div>
<div id="rulesBadge">Direct Race</div>
</div>
<div class="dice-panel">
<div>
<div id="diceMarks" aria-label="Four tetrahedral dice"></div>
<div class="dice-caption">Four binary tetrahedral dice · their sum gives the move count.</div>
</div>
<div id="rollValue">—</div>
</div>
<button id="rollBtn" class="primary-button">
Roll the dice <kbd>R</kbd>
</button>
<section class="rule-card">
<h2 id="ruleTitle">Direct Race</h2>
<div id="ruleEra" class="era">Common reconstruction</div>
<ul id="ruleList"></ul>
</section>
<section style="margin-top: 14px">
<div class="section-label moves-header">
<span>Legal moves</span>
<span id="moveCount" style="letter-spacing: 0.05em">0</span>
</div>
<div id="legalMoves"></div>
</section>
<section style="margin-top: 14px">
<div class="section-label">Board actions</div>
<div class="controls">
<button id="resetBtn" class="secondary-button">Reset</button>
<button id="pathsBtn" class="secondary-button active">Paths</button>
<button id="cameraBtn" class="secondary-button">Camera</button>
</div>
</section>
<section style="margin-top: 14px">
<div class="section-label">Record</div>
<div id="log" class="log" aria-live="polite"></div>
</section>
</aside>
<div class="help">
Drag to orbit · wheel to zoom · click a gold destination<br />
Keyboard: R roll · 1/2 rules · P paths · C camera
</div>
<div id="toast"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = id => document.getElementById(id);
const COLORS = {
lapis: 0x57a9df,
carnelian: 0xe47752,
gold: 0xe5b85c,
green: 0x85d6a4,
limestone: 0x9a8061,
darkStone: 0x3a332d
};
const PLAYER_NAMES = ["Lapis", "Carnelian"];
const PLAYER_COLORS = [COLORS.lapis, COLORS.carnelian];
const RULE_COPY = {
classical: {
title: "Direct Race",
era: "Common reconstruction",
badge: "Direct Race",
lines: [
"Advance the rolled number of track spaces; enter from reserve when possible.",
"Land on an opponent to capture, except on a rosette.",
"All five rosettes are safe and grant another roll.",
"Pieces may leap over occupied spaces, but cannot share one."
]
},
variant: {
title: "Gate & Contest",
era: "Experimental reconstruction",
badge: "Gate & Contest",
lines: [
"Uses the same board, track, pieces, and starting arrangement as Ruleset I.",
"The central gate G5 may be entered only on a roll of 3 or 4.",
"G5 is contested: an opponent there can be captured.",
"The four outer rosettes remain safe and grant another roll; G5 does not."
]
}
};
// Royal Game of Ur-style 3 × 8 board with a two-square middle bridge.
const BOARD_CELLS = [];
for (const row of [0, 1, 2]) {
for (const col of [0, 1, 2, 3, 5, 6, 7]) BOARD_CELLS.push([row, col]);
}
for (const col of [4]) BOARD_CELLS.push([1, col]);
const cellKey = ([row, col]) => `${row}:${col}`;
const CELL_MAP = new Map(BOARD_CELLS.map(cell => [cellKey(cell), cell]));
const ROSETTE_CELLS = [
[0, 1], [0, 6], [1, 4], [2, 1], [2, 6]
];
const ROSETTE_KEYS = new Set(ROSETTE_CELLS.map(cellKey));
const GATE_CELL = [1, 4];
const PATHS = [
[
[0, 0], [0, 1], [0, 2], [0, 3],
[1, 3], [1, 4], [1, 5],
[0, 5], [0, 6], [0, 7],
[1, 7], [2, 7], [2, 6], [2, 5]
],
[
[2, 0], [2, 1], [2, 2], [2, 3],
[1, 3], [1, 4], [1, 5],
[2, 5], [2, 6], [2, 7],
[1, 7], [0, 7], [0, 6], [0, 5]
]
];
const canvas = $("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x111517, 0.024);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0.75, 0, 0);
let cameraYaw = 0.62;
let cameraPitch = 0.72;
let cameraRadius = 14.2;
function updateCamera() {
const horizontal = cameraRadius * Math.cos(cameraPitch);
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = 0.62;
cameraPitch = 0.72;
cameraRadius = 14.2;
updateCamera();
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xe6e1d2, 0x283038, 1.45);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xffe0ad, 3.2);
keyLight.position.set(-4, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6ca6d9, 1.5);
rimLight.position.set(8, 5, -8);
scene.add(rimLight);
const warmPoint = new THREE.PointLight(0xffb75e, 20, 16, 2);
warmPoint.position.set(4, 3, 4);
scene.add(warmPoint);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x171b1d,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.48;
ground.receiveShadow = true;
scene.add(ground);
const world = new THREE.Group();
world.position.x = 0.7;
scene.add(world);
const stoneMaterial = new THREE.MeshStandardMaterial({
color: COLORS.limestone,
roughness: 0.93,
metalness: 0.02,
flatShading: true
});
const edgeMaterial = new THREE.MeshStandardMaterial({
color: 0x6f5842,
roughness: 0.96,
flatShading: true
});
const tileMaterials = [
new THREE.MeshStandardMaterial({ color: 0xa68a67, roughness: 0.93, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0x9b7e5e, roughness: 0.94, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0xb09370, roughness: 0.92, flatShading: true })
];
function addBox(parent, width, height, depth, x, y, z, material) {
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;
}
// Physical stone body: two larger blocks connected by a narrow middle bridge.
addBox(world, 4.18, 0.43, 3.26, -2.16, -0.215, 0, edgeMaterial);
addBox(world, 3.10, 0.43, 3.26, 2.70, -0.215, 0, edgeMaterial);
addBox(world, 1.26, 0.43, 1.08, 0.54, -0.215, 0, edgeMaterial);
// Slightly raised top slabs preserve the distinctive block-and-bridge silhouette.
addBox(world, 4.02, 0.12, 3.10, -2.16, 0.02, 0, stoneMaterial);
addBox(world, 2.94, 0.12, 3.10, 2.70, 0.02, 0, stoneMaterial);
addBox(world, 1.20, 0.12, 1.03, 0.54, 0.02, 0, stoneMaterial);
function cellPosition(cell) {
const [row, col] = cell;
return new THREE.Vector3(
(col - 3.5) * 1.08,
0.115,
(row - 1) * 1.08
);
}
const tileMeshes = new Map();
BOARD_CELLS.forEach((cell, index) => {
const position = cellPosition(cell);
const tile = new THREE.Mesh(
new THREE.BoxGeometry(0.94, 0.105, 0.94),
tileMaterials[(index * 7 + cell[0] + cell[1]) % tileMaterials.length]
);
tile.position.copy(position);
tile.position.y = 0.115;
tile.castShadow = true;
tile.receiveShadow = true;
world.add(tile);
tileMeshes.set(cellKey(cell), tile);
});
function createRosette(cell) {
const group = new THREE.Group();
const shape = new THREE.Shape();
const points = 20;
for (let i = 0; i < points; i++) {
const angle = (i / points) * Math.PI * 2;
const radius = i % 2 === 0 ? 0.29 : 0.13;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius;
if (i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
shape.closePath();
const rosetteMaterial = new THREE.MeshStandardMaterial({
color: 0xd2a052,
roughness: 0.78,
metalness: 0.08,
flatShading: true,
side: THREE.DoubleSide
});
const star = new THREE.Mesh(new THREE.ShapeGeometry(shape), rosetteMaterial);
star.rotation.x = -Math.PI / 2;
star.position.y = 0.175;
star.receiveShadow = true;
group.add(star);
const center = new THREE.Mesh(
new THREE.CircleGeometry(0.065, 10),
new THREE.MeshStandardMaterial({
color: 0x6e4d2f,
roughness: 0.8,
flatShading: true,
side: THREE.DoubleSide
})
);
center.rotation.x = -Math.PI / 2;
center.position.y = 0.179;
group.add(center);
const position = cellPosition(cell);
group.position.set(position.x, 0, position.z);
world.add(group);
}
ROSETTE_CELLS.forEach(createRosette);
// Reserve trays and finishing dishes are part of the scene, not recreated on rule changes.
const reserveTrayMaterial = new THREE.MeshStandardMaterial({
color: 0x66523e,
roughness: 0.95,
flatShading: true
});
for (let player = 0; player < 2; player++) {
const z = player === 0 ? -2.02 : 2.02;
addBox(world, 1.85, 0.16, 0.78, -4.70, -0.08, z, reserveTrayMaterial);
addBox(world, 1.62, 0.06, 0.56, -4.70, 0.02, z, stoneMaterial);
const dishZ = player === 0 ? -1.08 : 1.08;
const dish = new THREE.Mesh(
new THREE.CylinderGeometry(0.64, 0.72, 0.13, 12),
reserveTrayMaterial
);
dish.position.set(4.72, -0.03, dishZ);
dish.castShadow = true;
dish.receiveShadow = true;
world.add(dish);
const inner = new THREE.Mesh(
new THREE.CylinderGeometry(0.48, 0.48, 0.035, 12),
new THREE.MeshStandardMaterial({
color: player === 0 ? 0x315d79 : 0x754431,
roughness: 0.92,
flatShading: true
})
);
inner.position.set(4.72, 0.055, dishZ);
inner.receiveShadow = true;
world.add(inner);
}
function createPiece(color) {
const group = new THREE.Group();
const bodyMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.56,
metalness: 0.08,
flatShading: true
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color(color).multiplyScalar(0.72),
roughness: 0.72,
flatShading: true
});
const jewelMaterial = new THREE.MeshStandardMaterial({
color: 0xf1d38c,
roughness: 0.45,
metalness: 0.18,
flatShading: true
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.26, 0.29, 0.10, 10),
darkMaterial
);
base.position.y = 0.05;
group.add(base);
const collar = new THREE.Mesh(
new THREE.TorusGeometry(0.19, 0.035, 4, 10),
bodyMaterial
);
collar.rotation.x = Math.PI / 2;
collar.position.y = 0.16;
group.add(collar);
const body = new THREE.Mesh(
new THREE.ConeGeometry(0.21, 0.42, 9),
bodyMaterial
);
body.position.y = 0.37;
group.add(body);
const jewel = new THREE.Mesh(
new THREE.OctahedronGeometry(0.135, 0),
jewelMaterial
);
jewel.position.y = 0.66;
group.add(jewel);
group.traverse(object => {
if (object.isMesh) {
object.castShadow = true;
object.receiveShadow = true;
}
});
return group;
}
const pieces = [
Array.from({ length: 7 }, () => createPiece(PLAYER_COLORS[0])),
Array.from({ length: 7 }, () => createPiece(PLAYER_COLORS[1]))
];
pieces.forEach(playerPieces => {
playerPieces.forEach(piece => world.add(piece));
});
// Four tetrahedral dice with a small illuminated outcome marker.
const diceTray = new THREE.Group();
diceTray.position.set(4.64, 0, 2.83);
world.add(diceTray);
addBox(diceTray, 1.78, 0.14, 0.80, 0, -0.05, 0, reserveTrayMaterial);
addBox(diceTray, 1.60, 0.055, 0.62, 0, 0.035, 0, stoneMaterial);
const diceRigs = [];
const diceMaterial = new THREE.MeshStandardMaterial({
color: 0xd6b66e,
roughness: 0.52,
metalness: 0.12,
flatShading: true
});
for (let i = 0; i < 4; i++) {
const rig = new THREE.Group();
rig.position.set((i - 1.5) * 0.38, 0.27, 0);
const tetra = new THREE.Mesh(
new THREE.TetrahedronGeometry(0.25, 0),
diceMaterial
);
tetra.rotation.set(0.3, 0.4, 0.2);
tetra.castShadow = true;
rig.add(tetra);
const pipMaterial = new THREE.MeshStandardMaterial({
color: 0x484034,
emissive: 0x000000,
roughness: 0.6,
flatShading: true
});
const pip = new THREE.Mesh(
new THREE.ConeGeometry(0.045, 0.12, 4),
pipMaterial
);
pip.position.y = 0.31;
pip.rotation.x = Math.PI;
rig.add(pip);
rig.userData.pip = pip;
diceRigs.push(rig);
diceTray.add(rig);
}
// Current-player route line.
const pathMaterial = new THREE.LineBasicMaterial({
color: COLORS.gold,
transparent: true,
opacity: 0.65
});
const pathLine = new THREE.Line(new THREE.BufferGeometry(), pathMaterial);
world.add(pathLine);
let pathsVisible = true;
function updatePathLine() {
const points = PATHS[currentPlayer].map(cell => {
const p = cellPosition(cell);
p.y = 0.19;
return p;
});
pathLine.geometry.dispose();
pathLine.geometry = new THREE.BufferGeometry().setFromPoints(points);
pathMaterial.color.setHex(PLAYER_COLORS[currentPlayer]);
pathLine.visible = pathsVisible;
}
let ruleset = "classical";
let currentPlayer = 0;
let roll = null;
let legalMoves = [];
let busy = false;
let stateVersion = 0;
let diceValues = [false, false, false, false];
const initialPositions = () => [
[0, 1, 2, 3, -1, -1, -1],
[0, 1, 2, 3, -1, -1, -1]
];
let positions = initialPositions();
function reservePosition(player, slot) {
return new THREE.Vector3(
-5.26 + slot * 0.57,
0.12,
player === 0 ? -2.02 : 2.02
);
}
function finishedPosition(player, pieceIndex) {
const finishedPieces = positions[player]
.map((position, index) => ({ position, index }))
.filter(item => item.position === 14);
const slot = Math.max(0, finishedPieces.findIndex(item => item.index === pieceIndex));
const angle = (slot / 7) * Math.PI * 2;
const dishZ = player === 0 ? -1.08 : 1.08;
return new THREE.Vector3(
4.72 + Math.cos(angle) * 0.34,
0.12,
dishZ + Math.sin(angle) * 0.34
);
}
function layoutReserve(player) {
const reservePieces = positions[player]
.map((position, index) => ({ position, index }))
.filter(item => item.position === -1);
reservePieces.forEach((item, slot) => {
pieces[player][item.index].position.copy(reservePosition(player, slot));
});
}
function squareName(cell) {
if (!cell) return "Exit";
const rowNames = ["N", "G", "S"];
return `${rowNames[cell[0]]}${cell[1] + 1}`;
}
function isRosette(cell) {
return ROSETTE_KEYS.has(cellKey(cell));
}
function isGate(cell) {
return cellKey(cell) === cellKey(GATE_CELL);
}
function computeLegalMoves() {
const moves = [];
if (roll === null) return moves;
const other = 1 - currentPlayer;
positions[currentPlayer].forEach((position, pieceIndex) => {
let target;
if (position === -1) {
target = roll - 1;
} else {
target = position + roll;
}
if (target < 0 || target > 14) return;
const targetCell = target < 14 ? PATHS[currentPlayer][target] : null;
const ownOccupant = positions[currentPlayer].findIndex(
(p, index) => index !== pieceIndex && p === target
);
if (ownOccupant !== -1) return;
const opponentOccupant = positions[other].findIndex(p => p === target);
if (opponentOccupant !== -1 && targetCell) {
if (ruleset === "classical" && isRosette(targetCell)) return;
if (ruleset === "variant" && isRosette(targetCell) && !isGate(targetCell)) return;
}
if (
ruleset === "variant" &&
targetCell &&
isGate(targetCell) &&
roll < 3
) {
return;
}
moves.push({
pieceIndex,
from: position,
to: target,
targetCell,
captureIndex: opponentOccupant
});
});
return moves;
}
const highlightTargets = [];
function clearHighlights() {
for (const mesh of highlightTargets) {
world.remove(mesh);
mesh.geometry.dispose();
mesh.material.dispose();
}
highlightTargets.length = 0;
}
function buildHighlights() {
clearHighlights();
legalMoves.forEach((move, moveIndex) => {
let position;
if (move.to === 14) {
const dishZ = currentPlayer === 0 ? -1.08 : 1.08;
position = new THREE.Vector3(4.72, 0.19, dishZ);
} else {
position = cellPosition(move.targetCell);
position.y = 0.205;
}
const material = new THREE.MeshBasicMaterial({
color: COLORS.green,
transparent: true,
opacity: 0.42,
depthWrite: false,
side: THREE.DoubleSide
});
const disc = new THREE.Mesh(
new THREE.CylinderGeometry(0.34, 0.34, 0.025, 16),
material
);
disc.position.copy(position);
disc.userData.moveIndex = moveIndex;
disc.renderOrder = 4;
world.add(disc);
highlightTargets.push(disc);
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.34, 0.39, 16),
new THREE.MeshBasicMaterial({
color: 0xf7e3a0,
transparent: true,
opacity: 0.9,
depthWrite: false,
side: THREE.DoubleSide
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.copy(position);
ring.position.y += 0.016;
ring.renderOrder = 5;
world.add(ring);
});
}
function logEvent(text) {
const entry = document.createElement("div");
entry.className = "log-entry";
entry.textContent = text;
$("log").prepend(entry);
while ($("log").children.length > 7) {
$("log").lastElementChild.remove();
}
}
let toastTimer;
function toast(text) {
$("toast").textContent = text;
$("toast").classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => $("toast").classList.remove("show"), 2200);
}
function updateRuleUI() {
const copy = RULE_COPY[ruleset];
$("ruleTitle").textContent = copy.title;
$("ruleEra").textContent = copy.era;
$("rulesBadge").textContent = copy.badge;
$("ruleList").innerHTML = copy.lines.map(line => `<li>${line}</li>`).join("");
document.querySelectorAll(".rules-tab").forEach(button => {
const active = button.dataset.rules === ruleset;
button.classList.toggle("active", active);
button.setAttribute("aria-selected", String(active));
});
}
function updateDiceUI() {
$("diceMarks").innerHTML = diceValues.map(value =>
`<span class="die-mark ${value ? "marked" : ""}">${value ? "◆" : "◇"}</span>`
).join("");
$("rollValue").textContent = roll === null ? "—" : roll;
diceRigs.forEach((rig, index) => {
const pip = rig.userData.pip;
pip.material.color.setHex(diceValues[index] ? 0xffe08a : 0x484034);
pip.material.emissive.setHex(diceValues[index] ? 0x8a5a12 : 0x000000);
});
}
function updateTurnUI() {
const color = `#${PLAYER_COLORS[currentPlayer].toString(16).padStart(6, "0")}`;
$("turnDot").style.background = color;
$("turnDot").style.color = color;
$("turnName").innerHTML =
`<span id="turnDot"></span><span>${PLAYER_NAMES[currentPlayer]} to move</span>`;
$("turnDot").style.background = color;
$("turnDot").style.color = color;
}
function updateLegalUI() {
const container = $("legalMoves");
container.innerHTML = "";
$("moveCount").textContent = roll === null ? "—" : legalMoves.length;
if (roll === null) {
container.innerHTML = '<div class="empty-moves">Roll the dice to reveal which destinations are legal under the selected rules.</div>';
return;
}
if (!legalMoves.length) {
container.innerHTML = '<div class="empty-moves">No legal move. The turn will pass automatically.</div>';
return;
}
legalMoves.forEach((move, index) => {
const button = document.createElement("button");
button.className = "move-chip";
button.dataset.move = String(index);
const token = move.from === -1 ? "Reserve" : `Token ${move.pieceIndex + 1}`;
const destination = move.to === 14 ? "Finish" : squareName(move.targetCell);
const track = move.to === 14 ? "exact exit" : `track ${move.to + 1}`;
button.innerHTML = `
<span class="token-dot">${token}</span>
<strong>→ ${destination}</strong>
<small>${track}</small>
`;
button.addEventListener("click", () => executeMove(index));
container.appendChild(button);
});
}
function renderAll() {
updateRuleUI();
updateDiceUI();
updateTurnUI();
updateLegalUI();
$("rollBtn").disabled = busy || roll !== null;
updatePathLine();
}
const wait = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
async function rollDice() {
if (busy || roll !== null) return;
busy = true;
legalMoves = [];
clearHighlights();
renderAll();
diceValues = Array.from({ length: 4 }, () => Math.random() < 0.5);
const version = stateVersion;
const start = performance.now();
await new Promise(resolve => {
function animate(now) {
const elapsed = now - start;
diceRigs.forEach((rig, index) => {
rig.rotation.x += 0.11 + index * 0.012;
rig.rotation.y += 0.17 + index * 0.009;
rig.position.y = 0.27 + Math.sin(elapsed * 0.012 + index) * 0.025;
});
if (elapsed < 650) requestAnimationFrame(animate);
else resolve();
}
requestAnimationFrame(animate);
});
if (version !== stateVersion) return;
roll = diceValues.reduce((sum, value) => sum + (value ? 1 : 0), 0);
legalMoves = computeLegalMoves();
busy = false;
buildHighlights();
renderAll();
logEvent(
`${PLAYER_NAMES[currentPlayer]} rolled ${roll} under ${RULE_COPY[ruleset].title}. ` +
`${legalMoves.length} legal move${legalMoves.length === 1 ? "" : "s"}.`
);
if (roll === 0 || legalMoves.length === 0) {
await wait(650);
if (version !== stateVersion) return;
passTurn();
}
}
function passTurn() {
currentPlayer = 1 - currentPlayer;
roll = null;
legalMoves = [];
clearHighlights();
renderAll();
logEvent(`No legal move. Turn passes to ${PLAYER_NAMES[currentPlayer]}.`);
}
function tweenPiece(group, target, duration = 560, hop = true) {
return new Promise(resolve => {
const start = group.position.clone();
const startTime = performance.now();
function frame(now) {
const t = Math.min(1, (now - startTime) / duration);
const eased = t < 0.5
? 2 * t * t
: 1 - Math.pow(-2 * t + 2, 2) / 2;
group.position.lerpVectors(start, target, eased);
if (hop) group.position.y += Math.sin(Math.PI * eased) * 0.48;
if (t < 1) requestAnimationFrame(frame);
else {
group.position.copy(target);
resolve();
}
}
requestAnimationFrame(frame);
});
}
function shrinkPiece(group) {
return new Promise(resolve => {
const initialScale = group.scale.clone();
const startTime = performance.now();
function frame(now) {
const t = Math.min(1, (now - startTime) / 280);
group.scale.setScalar(1 - t * 0.98);
if (t < 1) requestAnimationFrame(frame);
else {
group.scale.copy(initialScale);
resolve();
}
}
requestAnimationFrame(frame);
});
}
async function executeMove(moveIndex) {
if (busy || roll === null) return;
const move = legalMoves[moveIndex];
if (!move) return;
busy = true;
clearHighlights();
renderAll();
const version = stateVersion;
const player = currentPlayer;
const other = 1 - player;
const movingPiece = pieces[player][move.pieceIndex];
const landedRosette = move.targetCell && isRosette(move.targetCell);
const grantsExtraRoll = Boolean(
landedRosette &&
(ruleset === "classical" || !isGate(move.targetCell))
);
positions[player][move.pieceIndex] = move.to;
let targetPosition;
if (move.to === 14) {
targetPosition = finishedPosition(player, move.pieceIndex);
} else {
targetPosition = cellPosition(move.targetCell);
}
const animations = [tweenPiece(movingPiece, targetPosition)];
if (move.captureIndex !== -1) {
const capturedPiece = pieces[other][move.captureIndex];
positions[other][move.captureIndex] = -1;
animations.push(shrinkPiece(capturedPiece));
}
await Promise.all(animations);
if (version !== stateVersion) return;
if (move.captureIndex !== -1) {
const capturedPiece = pieces[other][move.captureIndex];
const reserveCount = positions[other].filter(position => position === -1).length;
capturedPiece.position.copy(reservePosition(other, reserveCount - 1));
}
layoutReserve(player);
layoutReserve(other);
roll = null;
legalMoves = [];
busy = false;
if (grantsExtraRoll) {
logEvent(
`${PLAYER_NAMES[player]} lands on a rosette and receives another roll.`
);
toast("Rosette landing — the same player rolls again.");
} else {
currentPlayer = other;
logEvent(
`${PLAYER_NAMES[player]} moves token ${move.pieceIndex + 1} to ` +
`${move.to === 14 ? "the finish" : squareName(move.targetCell)}.`
);
}
renderAll();
}
function setRuleset(nextRuleset) {
if (ruleset === nextRuleset) return;
ruleset = nextRuleset;
// The board, pieces, physical track, and positions are intentionally untouched.
if (roll !== null) {
legalMoves = computeLegalMoves();
buildHighlights();
}
renderAll();
logEvent(`Rules changed to ${RULE_COPY[ruleset].title}; the physical setup is unchanged.`);
toast("Rules changed — board and starting arrangement preserved.");
}
function resetGame() {
stateVersion++;
busy = false;
currentPlayer = 0;
roll = null;
legalMoves = [];
positions = initialPositions();
diceValues = [false, false, false, false];
clearHighlights();
for (let player = 0; player < 2; player++) {
positions[player].forEach((position, pieceIndex) => {
const piece = pieces[player][pieceIndex];
piece.visible = true;
piece.scale.setScalar(1);
if (position === -1) {
const reservePieces = positions[player]
.map((p, index) => ({ p, index }))
.filter(item => item.p === -1);
const slot = reservePieces.findIndex(item => item.index === pieceIndex);
piece.position.copy(reservePosition(player, slot));
} else if (position === 14) {
piece.position.copy(finishedPosition(player, pieceIndex));
} else {
piece.position.copy(cellPosition(PATHS[player][position]));
}
});
}
$("log").innerHTML = "";
logEvent("The identical starting arrangement has been restored.");
renderAll();
toast("Starting arrangement reset. The board object itself was not rebuilt.");
}
document.querySelectorAll(".rules-tab").forEach(button => {
button.addEventListener("click", () => setRuleset(button.dataset.rules));
});
$("rollBtn").addEventListener("click", rollDice);
$("resetBtn").addEventListener("click", resetGame);
$("pathsBtn").addEventListener("click", () => {
pathsVisible = !pathsVisible;
$("pathsBtn").classList.toggle("active", pathsVisible);
updatePathLine();
});
$("cameraBtn").addEventListener("click", () => {
resetCamera();
toast("Camera reset.");
});
window.addEventListener("keydown", event => {
if (event.repeat) return;
const key = event.key.toLowerCase();
if (key === "r") rollDice();
if (key === "1") setRuleset("classical");
if (key === "2") setRuleset("variant");
if (key === "p") $("pathsBtn").click();
if (key === "c") resetCamera();
});
// Custom orbit and zoom controls. No camera-control addons are used.
let pointerDown = false;
let pointerMoved = 0;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
pointerDown = true;
pointerMoved = 0;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!pointerDown) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
pointerMoved += Math.abs(dx) + Math.abs(dy);
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.005,
0.22,
1.28
);
updateCamera();
});
canvas.addEventListener("pointerup", event => {
if (!pointerDown) return;
pointerDown = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (pointerMoved > 6 || !legalMoves.length) return;
const rect = canvas.getBoundingClientRect();
const pointer = new THREE.Vector2(
((event.clientX - rect.left) / rect.width) * 2 - 1,
-((event.clientY - rect.top) / rect.height) * 2 + 1
);
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(highlightTargets, false);
if (intersections.length) {
executeMove(intersections[0].object.userData.moveIndex);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
});
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
8,
22
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
highlightTargets.forEach((mesh, index) => {
const pulse = 1 + Math.sin(elapsed * 4.2 + index * 0.9) * 0.075;
mesh.scale.set(pulse, 1, pulse);
mesh.material.opacity = 0.35 + Math.sin(elapsed * 4.2 + index) * 0.08;
});
renderer.render(scene, camera);
}
resetGame();
animate();
</script>
</body>
</html>
```