Prompt specification
Build or customize the Three.js 3D scene "Symmetry Rule Tile Puzzle" 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: Symmetry Rule Tile Puzzle
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/symmetry-rule-tile-puzzle-x3308
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: symmetry rule tile puzzle. Create a bounded floor puzzle requiring specified reflection and rotational symmetries. Check the actual tile arrangement and let users inspect failed constraints individually. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (symmetry-rule-tile-puzzle-x3308):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Mirror Yard — Symmetry Floor Puzzle</title>
<style>
:root {
--bg: #071219;
--panel: rgba(8, 19, 27, 0.88);
--panel-solid: #0c1c26;
--line: rgba(174, 214, 225, 0.15);
--text: #e8f3f2;
--muted: #8fa8ad;
--teal: #58e0cf;
--violet: #b79cff;
--amber: #ffb45f;
--coral: #ff8f70;
--green: #8be5a7;
--danger: #ff987d;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button { font: inherit; }
#app {
position: fixed;
inset: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 35%, rgba(30, 69, 79, 0.22), transparent 45%),
linear-gradient(180deg, #09151d, #050d12);
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.vignette {
position: absolute;
inset: 0;
pointer-events: none;
background:
linear-gradient(90deg, rgba(0,0,0,.18), transparent 22%, transparent 72%, rgba(0,0,0,.24)),
radial-gradient(ellipse at center, transparent 48%, rgba(0,0,0,.4) 115%);
z-index: 1;
}
.topbar {
position: absolute;
z-index: 5;
top: 16px;
left: 18px;
right: 18px;
height: 58px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
pointer-events: none;
}
.brand, .top-actions, .global-status {
pointer-events: auto;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 250px;
}
.brand-mark {
width: 38px;
height: 38px;
position: relative;
border: 1px solid rgba(88, 224, 207, .38);
background: rgba(88, 224, 207, .07);
transform: rotate(45deg);
border-radius: 8px;
box-shadow: inset 0 0 22px rgba(88, 224, 207, .08);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--teal);
opacity: .85;
}
.brand-mark::before {
width: 1px;
height: 25px;
left: 18px;
top: 6px;
}
.brand-mark::after {
height: 1px;
width: 25px;
left: 6px;
top: 18px;
}
.brand-copy {
line-height: 1;
}
.brand-copy strong {
display: block;
font-size: 14px;
letter-spacing: .18em;
text-transform: uppercase;
}
.brand-copy span {
display: block;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
letter-spacing: .16em;
text-transform: uppercase;
}
.global-status {
position: absolute;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 9px;
min-height: 34px;
padding: 0 14px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 19, 27, .74);
backdrop-filter: blur(14px);
color: #d7e7e7;
font-size: 12px;
white-space: nowrap;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--danger);
box-shadow: 0 0 12px rgba(255, 152, 125, .65);
}
.global-status.success .status-dot {
background: var(--green);
box-shadow: 0 0 12px rgba(139, 229, 167, .65);
}
.top-actions {
display: flex;
align-items: center;
gap: 9px;
}
.button {
appearance: none;
border: 1px solid var(--line);
color: var(--text);
background: rgba(11, 27, 36, .78);
border-radius: 9px;
min-height: 36px;
padding: 0 13px;
cursor: pointer;
transition: transform .15s ease, background .15s ease, border-color .15s ease;
backdrop-filter: blur(12px);
font-size: 12px;
font-weight: 650;
letter-spacing: .02em;
}
.button:hover {
transform: translateY(-1px);
border-color: rgba(174, 214, 225, .32);
background: rgba(18, 41, 52, .92);
}
.button:active { transform: translateY(0); }
.button.primary {
color: #092027;
border-color: transparent;
background: var(--teal);
box-shadow: 0 8px 24px rgba(88, 224, 207, .13);
}
.button.primary:hover { background: #76eadd; }
.inspector {
position: absolute;
z-index: 4;
top: 86px;
right: 18px;
bottom: 18px;
width: 354px;
padding: 20px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
backdrop-filter: blur(20px) saturate(120%);
box-shadow: 0 24px 70px rgba(0, 0, 0, .34);
overflow: auto;
scrollbar-width: thin;
scrollbar-color: rgba(174,214,225,.22) transparent;
}
.eyebrow {
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: .2em;
text-transform: uppercase;
}
.inspector h1 {
margin: 7px 0 8px;
font-size: 24px;
line-height: 1.08;
letter-spacing: -.035em;
}
.inspector-intro {
margin: 0 0 17px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.inspector-tools {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.live-note {
color: #718b91;
font-size: 10px;
letter-spacing: .08em;
text-transform: uppercase;
}
.rule-list {
display: grid;
gap: 9px;
}
.rule-card {
width: 100%;
display: grid;
grid-template-columns: 39px 1fr auto;
gap: 11px;
align-items: center;
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
color: var(--text);
background: rgba(255,255,255,.025);
text-align: left;
cursor: pointer;
transition: border-color .16s ease, background .16s ease, transform .16s ease;
}
.rule-card:hover {
transform: translateX(-2px);
background: rgba(255,255,255,.045);
}
.rule-card.active {
border-color: color-mix(in srgb, var(--rule-color) 58%, transparent);
background: color-mix(in srgb, var(--rule-color) 8%, transparent);
}
.rule-glyph {
width: 39px;
height: 39px;
display: grid;
place-items: center;
border-radius: 10px;
color: var(--rule-color);
border: 1px solid color-mix(in srgb, var(--rule-color) 40%, transparent);
background: color-mix(in srgb, var(--rule-color) 10%, transparent);
font-size: 11px;
font-weight: 850;
letter-spacing: .03em;
}
.rule-main { min-width: 0; }
.rule-name {
display: block;
font-size: 12px;
font-weight: 760;
margin-bottom: 4px;
}
.rule-scope {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.rule-status {
min-width: 43px;
padding: 5px 7px;
border-radius: 999px;
text-align: center;
font-size: 9px;
font-weight: 850;
letter-spacing: .08em;
color: var(--danger);
background: rgba(255, 152, 125, .1);
}
.rule-status.pass {
color: var(--green);
background: rgba(139, 229, 167, .1);
}
.rule-meter {
grid-column: 2 / 4;
height: 3px;
overflow: hidden;
border-radius: 99px;
background: rgba(255,255,255,.055);
}
.rule-meter span {
display: block;
height: 100%;
border-radius: inherit;
background: var(--rule-color);
opacity: .75;
}
.detail-panel {
margin-top: 17px;
padding-top: 17px;
border-top: 1px solid var(--line);
}
.detail-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 5px;
}
.detail-heading h2 {
margin: 0;
font-size: 14px;
letter-spacing: -.01em;
}
.detail-count {
color: var(--danger);
font-size: 10px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
.detail-copy {
margin: 0 0 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.mismatch-list {
display: grid;
gap: 6px;
}
.mismatch {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
padding: 9px 10px;
border: 1px solid rgba(255, 152, 125, .16);
border-radius: 9px;
background: rgba(255, 152, 125, .045);
color: #e9d8d2;
cursor: pointer;
font-size: 11px;
text-align: left;
transition: background .15s ease, border-color .15s ease;
}
.mismatch:hover,
.mismatch.active {
border-color: rgba(88, 224, 207, .36);
background: rgba(88, 224, 207, .075);
}
.mismatch span:last-child {
color: var(--muted);
font-size: 9px;
letter-spacing: .06em;
text-transform: uppercase;
}
.pass-message {
display: flex;
gap: 11px;
align-items: flex-start;
padding: 13px;
border: 1px solid rgba(139, 229, 167, .18);
border-radius: 10px;
background: rgba(139, 229, 167, .055);
color: #c9ead2;
font-size: 11px;
line-height: 1.45;
}
.pass-message b {
color: var(--green);
font-size: 17px;
line-height: 1;
}
.guide {
position: absolute;
z-index: 3;
left: 20px;
top: 101px;
width: 258px;
pointer-events: none;
}
.guide-kicker {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .16em;
text-transform: uppercase;
}
.guide h2 {
margin: 7px 0 8px;
font-size: 22px;
line-height: 1.08;
letter-spacing: -.03em;
}
.guide p {
margin: 0;
color: #829da2;
font-size: 11px;
line-height: 1.6;
}
.guide-steps {
display: grid;
gap: 9px;
margin-top: 18px;
}
.guide-step {
display: grid;
grid-template-columns: 21px 1fr;
gap: 8px;
align-items: start;
color: #92a8ac;
font-size: 10px;
line-height: 1.45;
}
.guide-step i {
width: 20px;
height: 20px;
display: grid;
place-items: center;
border-radius: 50%;
border: 1px solid rgba(174,214,225,.17);
color: var(--teal);
font-style: normal;
font-size: 9px;
font-weight: 800;
}
.selected-bar {
position: absolute;
z-index: 5;
left: calc(50% - 172px);
bottom: 18px;
transform: translateX(-50%);
min-width: 440px;
display: flex;
align-items: center;
gap: 16px;
padding: 11px 12px 11px 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(8, 19, 27, .87);
backdrop-filter: blur(18px);
box-shadow: 0 18px 48px rgba(0,0,0,.3);
}
.cell-readout {
min-width: 90px;
}
.cell-readout small {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .13em;
text-transform: uppercase;
}
.cell-readout strong {
display: block;
margin-top: 4px;
font-size: 13px;
}
.cell-readout span {
color: var(--teal);
}
.palette {
display: flex;
align-items: center;
gap: 7px;
}
.symbol-button {
width: 42px;
height: 42px;
display: grid;
place-items: center;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255,255,255,.03);
color: var(--symbol);
cursor: pointer;
font-size: 20px;
line-height: 1;
transition: transform .14s ease, border-color .14s ease, background .14s ease;
}
.symbol-button:hover {
transform: translateY(-2px);
border-color: color-mix(in srgb, var(--symbol) 55%, transparent);
}
.symbol-button.active {
border-color: var(--symbol);
background: color-mix(in srgb, var(--symbol) 12%, transparent);
box-shadow: inset 0 0 16px color-mix(in srgb, var(--symbol) 10%, transparent);
}
.cycle-button {
margin-left: 2px;
min-width: 66px;
}
.control-hint {
position: absolute;
z-index: 3;
left: 20px;
bottom: 20px;
color: #627d83;
font-size: 9px;
line-height: 1.65;
letter-spacing: .05em;
pointer-events: none;
}
.control-hint b {
color: #9ab0b4;
font-weight: 750;
}
.toast {
position: absolute;
z-index: 20;
left: 50%;
top: 84px;
transform: translate(-50%, -12px);
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 19, 27, .92);
color: #dce9e8;
font-size: 11px;
opacity: 0;
pointer-events: none;
transition: opacity .2s ease, transform .2s ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.success-overlay {
position: absolute;
z-index: 12;
inset: 0;
display: grid;
place-items: center;
padding: 20px;
background: rgba(2, 10, 14, .52);
backdrop-filter: blur(7px);
opacity: 0;
visibility: hidden;
transition: opacity .3s ease, visibility .3s ease;
}
.success-overlay.show {
opacity: 1;
visibility: visible;
}
.success-card {
width: min(390px, 100%);
padding: 31px;
border: 1px solid rgba(139, 229, 167, .3);
border-radius: 20px;
background: rgba(9, 27, 31, .94);
box-shadow: 0 30px 90px rgba(0,0,0,.45);
text-align: center;
transform: translateY(12px) scale(.98);
transition: transform .35s ease;
}
.success-overlay.show .success-card {
transform: translateY(0) scale(1);
}
.success-ring {
width: 68px;
height: 68px;
display: grid;
place-items: center;
margin: 0 auto 18px;
border: 1px solid rgba(139, 229, 167, .38);
border-radius: 50%;
color: var(--green);
background: rgba(139, 229, 167, .09);
font-size: 31px;
}
.success-card h2 {
margin: 0 0 9px;
font-size: 28px;
letter-spacing: -.035em;
}
.success-card p {
margin: 0 0 21px;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
@media (max-width: 1050px) {
.guide { display: none; }
.selected-bar { left: calc(50% - 195px); }
.control-hint { display: none; }
}
@media (max-width: 760px) {
.topbar {
top: 10px;
left: 10px;
right: 10px;
height: 48px;
}
.brand { min-width: 0; }
.brand-copy span { display: none; }
.brand-copy strong { font-size: 11px; }
.brand-mark { width: 31px; height: 31px; }
.brand-mark::before { left: 14px; height: 20px; top: 5px; }
.brand-mark::after { left: 5px; width: 20px; top: 14px; }
.global-status { top: 56px; }
.top-actions { gap: 5px; }
.button { min-height: 32px; padding: 0 9px; font-size: 10px; }
#resetBtn { display: none; }
.inspector {
top: auto;
left: 10px;
right: 10px;
bottom: 102px;
width: auto;
max-height: 39vh;
padding: 15px;
border-radius: 15px;
}
.inspector h1 { font-size: 19px; }
.inspector-intro { display: none; }
.inspector-tools { margin-bottom: 8px; }
.rule-list { gap: 6px; }
.rule-card { padding: 8px; grid-template-columns: 32px 1fr auto; gap: 8px; }
.rule-glyph { width: 32px; height: 32px; }
.rule-name { font-size: 11px; }
.rule-scope { font-size: 9px; }
.rule-status { min-width: 38px; font-size: 8px; }
.detail-panel { margin-top: 11px; padding-top: 11px; }
.selected-bar {
left: 10px;
right: 10px;
bottom: 10px;
transform: none;
min-width: 0;
gap: 9px;
padding: 9px;
}
.cell-readout { min-width: 68px; }
.cell-readout strong { font-size: 11px; }
.palette { gap: 4px; }
.symbol-button { width: 36px; height: 36px; font-size: 17px; }
.cycle-button { display: none; }
.toast { top: 91px; }
}
</style>
</head>
<body>
<main id="app">
<canvas id="scene" aria-label="Interactive 3D symmetry tile puzzle"></canvas>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark"></div>
<div class="brand-copy">
<strong>Mirror Yard</strong>
<span>Symmetry floor · chamber 06</span>
</div>
</div>
<div class="global-status" id="globalStatus" aria-live="polite">
<i class="status-dot"></i>
<span id="globalStatusText">Checking arrangement…</span>
</div>
<div class="top-actions">
<button class="button" id="resetBtn" type="button">Reset floor</button>
<button class="button primary" id="checkBtn" type="button">Verify floor</button>
</div>
</header>
<aside class="inspector" aria-label="Constraint inspector">
<div class="eyebrow">Constraint inspector</div>
<h1>Make every pair agree.</h1>
<p class="inspector-intro">
These rules inspect the actual tile arrangement. Open a rule to isolate each mismatched pair.
</p>
<div class="inspector-tools">
<span class="live-note">Live arrangement check</span>
<button class="button" id="checkBtn2" type="button">Check now</button>
</div>
<div class="rule-list" id="ruleList"></div>
<div class="detail-panel" id="detailPanel"></div>
</aside>
<section class="guide">
<div class="guide-kicker">The bounded floor</div>
<h2>Repair the symmetry.</h2>
<p>
Match the motifs across each marked axis. A valid floor satisfies all three local rules at once.
</p>
<div class="guide-steps">
<div class="guide-step"><i>1</i><span>Click a tile to select its cell.</span></div>
<div class="guide-step"><i>2</i><span>Choose a motif below or press keys 1–4.</span></div>
<div class="guide-step"><i>3</i><span>Open a failed constraint and inspect its pairs.</span></div>
</div>
</section>
<section class="selected-bar" aria-label="Tile editor">
<div class="cell-readout">
<small>Selected cell</small>
<strong id="selectedName">A1</strong>
<span id="selectedMotif">Triangle</span>
</div>
<div class="palette" id="palette">
<button class="symbol-button active" data-symbol="0" style="--symbol:#ff8f70" title="Triangle (1)" aria-label="Set triangle">▲</button>
<button class="symbol-button" data-symbol="1" style="--symbol:#58e0cf" title="Diamond (2)" aria-label="Set diamond">◆</button>
<button class="symbol-button" data-symbol="2" style="--symbol:#b79cff" title="Hexagon (3)" aria-label="Set hexagon">⬡</button>
<button class="symbol-button" data-symbol="3" style="--symbol:#ffc76a" title="Cross (4)" aria-label="Set cross">✚</button>
</div>
<button class="button cycle-button" id="cycleBtn" type="button">Cycle</button>
</section>
<div class="control-hint">
<b>Drag</b> orbit · <b>Wheel</b> zoom · <b>Shift + drag</b> pan · <b>Double-click</b> reset view
</div>
<div class="toast" id="toast" role="status"></div>
<div class="success-overlay" id="successOverlay" aria-hidden="true">
<div class="success-card">
<div class="success-ring">✓</div>
<div class="eyebrow" style="color:var(--green)">Floor stable</div>
<h2>All symmetries agree.</h2>
<p>
Every reflected pair and every rotated pair now matches. The chamber is fully balanced.
</p>
<button class="button primary" id="closeSuccessBtn" type="button">Keep exploring</button>
</div>
</div>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const canvas = $("#scene");
const SYMBOLS = [
{ name: "Triangle", color: 0xff8f70, glyph: "▲" },
{ name: "Diamond", color: 0x58e0cf, glyph: "◆" },
{ name: "Hexagon", color: 0xb79cff, glyph: "⬡" },
{ name: "Cross", color: 0xffc76a, glyph: "✚" }
];
const RULE_META = {
north: {
title: "Vertical reflection",
short: "V",
color: "#58e0cf",
scope: "Rows 1–3 reflect across the vertical centerline"
},
west: {
title: "Horizontal reflection",
short: "H",
color: "#b79cff",
scope: "Columns 1–3 reflect across the horizontal centerline"
},
turn: {
title: "180° rotation",
short: "180",
color: "#ffb45f",
scope: "The southeast 3×3 quarter matches its opposite quarter"
}
};
const ROWS = 6;
const COLS = 6;
const SPACING = 1;
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
function createScrambledFloor() {
const random = mulberry32(0x51A7F10A);
const target = Array.from({ length: ROWS }, () => Array(COLS).fill(0));
for (let r = 0; r < ROWS; r++) {
for (let c = 0; c < COLS; c++) {
target[r][c] = Math.floor(random() * SYMBOLS.length);
}
}
// Construct a floor that obeys the three rules.
for (let r = 0; r < 3; r++) {
for (let c = 0; c < 3; c++) {
target[r][5 - c] = target[r][c];
}
}
for (let c = 0; c < 3; c++) {
for (let r = 0; r < 3; r++) {
target[5 - r][c] = target[r][c];
}
}
for (let r = 3; r < 6; r++) {
for (let c = 3; c < 6; c++) {
target[r][c] = target[5 - r][5 - c];
}
}
const scrambled = target.map(row => row.slice());
// Scramble one member of three pairs for each rule.
const mutations = [
[0, 5], [1, 4], [2, 3], // north
[4, 0], [3, 1], [1, 2], // west
[3, 3], [4, 4], [5, 5] // turn
];
for (const [r, c] of mutations) {
scrambled[r][c] = (scrambled[r][c] + 1) % SYMBOLS.length;
}
return scrambled;
}
const initialBoard = createScrambledFloor();
let board = initialBoard.map(row => row.slice());
let selectedIndex = 0;
let activeRule = "north";
let selectedMismatch = 0;
let hoveredIndex = -1;
let wasSuccessful = false;
let successStarted = 0;
let toastTimer = 0;
// ---------- Three.js scene ----------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071219);
scene.fog = new THREE.FogExp2(0x071219, 0.032);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
yaw: 0.64,
pitch: 0.83,
distance: 12.4,
target: new THREE.Vector3(0, 0, 0)
};
function applyCamera() {
const horizontal = Math.cos(cameraState.pitch) * cameraState.distance;
camera.position.set(
cameraState.target.x + Math.sin(cameraState.yaw) * horizontal,
cameraState.target.y + Math.sin(cameraState.pitch) * cameraState.distance,
cameraState.target.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.yaw = 0.64;
cameraState.pitch = 0.83;
cameraState.distance = 12.4;
cameraState.target.set(0, 0, 0);
applyCamera();
}
applyCamera();
const hemisphere = new THREE.HemisphereLight(0xc9f4ff, 0x14232c, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff3dd, 3.1);
keyLight.position.set(-6, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
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 = 30;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x58e0cf, 1.25);
rimLight.position.set(7, 5, -8);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x08141b,
roughness: 0.94,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.42;
floor.receiveShadow = true;
scene.add(floor);
const floorGrid = new THREE.GridHelper(36, 36, 0x29434b, 0x13272e);
floorGrid.position.y = -0.405;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.38;
scene.add(floorGrid);
const boardGroup = new THREE.Group();
scene.add(boardGroup);
const slab = new THREE.Mesh(
new THREE.BoxGeometry(6.72, 0.4, 6.72),
new THREE.MeshStandardMaterial({
color: 0x243740,
roughness: 0.78,
metalness: 0.08,
flatShading: true
})
);
slab.position.y = -0.2;
slab.castShadow = true;
slab.receiveShadow = true;
boardGroup.add(slab);
const underframe = new THREE.Mesh(
new THREE.BoxGeometry(7.05, 0.13, 7.05),
new THREE.MeshStandardMaterial({
color: 0x0c1b23,
roughness: 0.8,
metalness: 0.15
})
);
underframe.position.y = -0.37;
underframe.castShadow = true;
boardGroup.add(underframe);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x667b80,
roughness: 0.52,
metalness: 0.28,
flatShading: true
});
function addFrameRail(x, z, width, depth) {
const rail = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.31, depth),
frameMaterial
);
rail.position.set(x, 0.08, z);
rail.castShadow = true;
rail.receiveShadow = true;
boardGroup.add(rail);
}
addFrameRail(-3.22, 0, 0.19, 6.62);
addFrameRail(3.22, 0, 0.19, 6.62);
addFrameRail(0, -3.22, 6.62, 0.19);
addFrameRail(0, 3.22, 6.62, 0.19);
const beaconMaterial = new THREE.MeshStandardMaterial({
color: 0x58e0cf,
emissive: 0x174e49,
emissiveIntensity: 0.5,
roughness: 0.5,
flatShading: true
});
const beacons = [];
for (const x of [-3.22, 3.22]) {
for (const z of [-3.22, 3.22]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.18, 0.52, 6),
frameMaterial
);
post.position.set(x, 0.19, z);
post.castShadow = true;
boardGroup.add(post);
const beacon = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.32, 6),
beaconMaterial
);
beacon.position.set(x, 0.59, z);
beacon.castShadow = true;
boardGroup.add(beacon);
beacons.push(beacon);
}
}
function makeGlyphGeometry(type) {
const shape = new THREE.Shape();
let points = [];
if (type === 0) {
points = [
[-0.22, -0.14], [0.22, -0.14], [0, 0.24]
];
} else if (type === 1) {
points = [
[0, 0.23], [0.22, 0], [0, -0.23], [-0.22, 0]
];
} else if (type === 2) {
for (let i = 0; i < 6; i++) {
const a = Math.PI / 6 + i * Math.PI / 3;
points.push([Math.cos(a) * 0.22, Math.sin(a) * 0.22]);
}
} else {
points = [
[-0.075, -0.22], [0.075, -0.22],
[0.075, -0.075], [0.22, -0.075],
[0.22, 0.075], [0.075, 0.075],
[0.075, 0.22], [-0.075, 0.22],
[-0.075, 0.075], [-0.22, 0.075],
[-0.22, -0.075], [-0.075, -0.075]
];
}
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.09,
bevelEnabled: false,
steps: 1
});
geometry.rotateX(-Math.PI / 2);
geometry.computeVertexNormals();
return geometry;
}
const glyphGeometries = SYMBOLS.map((_, i) => makeGlyphGeometry(i));
const glyphMaterials = SYMBOLS.map(symbol => new THREE.MeshStandardMaterial({
color: symbol.color,
emissive: symbol.color,
emissiveIntensity: 0.1,
roughness: 0.52,
metalness: 0.08,
flatShading: true
}));
const tileGeometry = new THREE.BoxGeometry(0.92, 0.18, 0.92);
const tiles = [];
const tileBases = [];
function cellPosition(index) {
const row = Math.floor(index / COLS);
const col = index % COLS;
return {
row,
col,
x: (col - 2.5) * SPACING,
z: (row - 2.5) * SPACING
};
}
for (let index = 0; index < ROWS * COLS; index++) {
const { row, col, x, z } = cellPosition(index);
const group = new THREE.Group();
group.position.set(x, 0, z);
const shade = 0.91 + ((row * 7 + col * 3) % 5) * 0.014;
const baseColor = new THREE.Color(0xb8c5c1).multiplyScalar(shade);
const baseMaterial = new THREE.MeshStandardMaterial({
color: baseColor,
roughness: 0.86,
metalness: 0.03,
flatShading: true
});
const base = new THREE.Mesh(tileGeometry, baseMaterial);
base.position.y = 0.09;
base.castShadow = true;
base.receiveShadow = true;
base.userData.index = index;
group.add(base);
tileBases.push(base);
const glyph = new THREE.Mesh(glyphGeometries[board[Math.floor(index / COLS)][index % COLS]], glyphMaterials[board[Math.floor(index / COLS)][index % COLS]]);
glyph.position.y = 0.195;
glyph.castShadow = true;
group.add(glyph);
boardGroup.add(group);
tiles.push({ group, base, glyph, baseColor });
}
function updateGlyph(index) {
const tile = tiles[index];
tile.glyph.geometry = glyphGeometries[board[Math.floor(index / COLS)][index % COLS]];
tile.glyph.material = glyphMaterials[board[Math.floor(index / COLS)][index % COLS]];
}
// Scope guides and axis lines.
function makeLine(points, color, opacity = 0.7, dashed = false) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map(p => new THREE.Vector3(p[0], p[1], p[2]))
);
const material = dashed
? new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize: 0.14,
gapSize: 0.09
})
: new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
const line = new THREE.Line(geometry, material);
if (dashed) line.computeLineDistances();
boardGroup.add(line);
return line;
}
function rectanglePoints(x1, z1, x2, z2, y = 0.285) {
return [
[x1, y, z1], [x2, y, z1], [x2, y, z2],
[x1, y, z2], [x1, y, z1]
];
}
makeLine([[0, 0.29, -2.94], [0, 0.29, 0]], 0x58e0cf, 0.95);
makeLine([[-2.94, 0.29, 0], [0, 0.29, 0]], 0xb79cff, 0.95);
makeLine(rectanglePoints(-2.98, -2.98, 2.98, 0), 0x58e0cf, 0.19, true);
makeLine(rectanglePoints(-2.98, -2.98, 0, 2.98), 0xb79cff, 0.17, true);
makeLine(rectanglePoints(0, 0, 2.98, 2.98), 0xffb45f, 0.22, true);
// Small center pivot marker.
const pivot = new THREE.Mesh(
new THREE.TorusGeometry(0.33, 0.022, 4, 20),
new THREE.MeshBasicMaterial({
color: 0xffb45f,
transparent: true,
opacity: 0.55
})
);
pivot.rotation.x = Math.PI / 2;
pivot.position.y = 0.295;
boardGroup.add(pivot);
function createLabel(text, color = "#dcebea", width = 1.0) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 256;
labelCanvas.height = 64;
const ctx = labelCanvas.getContext("2d");
ctx.fillStyle = "rgba(7, 18, 25, 0.86)";
ctx.strokeStyle = color;
ctx.lineWidth = 3;
ctx.beginPath();
ctx.roundRect(4, 4, 248, 56, 13);
ctx.fill();
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "700 28px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 128, 33);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.scale.set(width, width * 0.25, 1);
return sprite;
}
const vLabel = createLabel("V", "#58e0cf", 0.64);
vLabel.position.set(0, 0.55, -3.28);
boardGroup.add(vLabel);
const hLabel = createLabel("H", "#b79cff", 0.64);
hLabel.position.set(-3.30, 0.55, 0);
boardGroup.add(hLabel);
const turnLabel = createLabel("180°", "#ffb45f", 0.92);
turnLabel.position.set(3.30, 0.56, 3.30);
boardGroup.add(turnLabel);
for (let c = 0; c < COLS; c++) {
const label = createLabel(String.fromCharCode(65 + c), "#78939a", 0.34);
label.position.set((c - 2.5), 0.26, 3.08);
boardGroup.add(label);
}
for (let r = 0; r < ROWS; r++) {
const label = createLabel(String(r + 1), "#78939a", 0.34);
label.position.set(-3.08, 0.26, (r - 2.5));
boardGroup.add(label);
}
// ---------- Constraint checking ----------
function pairForNorth(row, col) {
return { a: [row, col], b: [row, 5 - col] };
}
function pairForWest(row, col) {
return { a: [row, col], b: [5 - row, col] };
}
function pairForTurn(row, col) {
return { a: [row, col], b: [5 - row, 5 - col] };
}
function getPairs(rule) {
const pairs = [];
if (rule === "north") {
for (let r = 0; r < 3; r++) {
for (let c = 0; c < 3; c++) {
pairs.push(pairForNorth(r, c));
}
}
} else if (rule === "west") {
for (let c = 0; c < 3; c++) {
for (let r = 0; r < 5; r++) {
pairs.push(pairForWest(r, c));
}
}
} else if (rule === "turn") {
for (let r = 3; r < 6; r++) {
for (let c = 3; c < 6; c++) {
pairs.push(pairForTurn(r, c));
}
}
}
return pairs;
}
function evaluateArrangement() {
const results = {};
for (const rule of Object.keys(RULE_META)) {
const pairs = getPairs(rule);
const mismatches = [];
// The verifier compares only the current visible arrangement.
for (const pair of pairs) {
const aValue = board[pair.a[0]][pair.a[1]];
const bValue = board[pair.b[0]][pair.b[1]];
if (aValue !== bValue) mismatches.push(pair);
}
results[rule] = {
total: pairs.length,
mismatches,
passed: mismatches.length === 0
};
}
return results;
}
let results = evaluateArrangement();
function coordinateName([row, col]) {
return `${String.fromCharCode(65 + col)}${row + 1}`;
}
function selectedSymbolName() {
return SYMBOLS[board[selectedIndex]].name;
}
// ---------- Highlight meshes ----------
const highlightGroup = new THREE.Group();
boardGroup.add(highlightGroup);
const markerGeometry = new THREE.TorusGeometry(0.39, 0.026, 4, 24);
const markerMaterials = {
orange: new THREE.MeshBasicMaterial({
color: 0xff987d,
transparent: true,
opacity: 0.92,
depthTest: false
}),
cyan: new THREE.MeshBasicMaterial({
color: 0x71f4e2,
transparent: true,
opacity: 0.98,
depthTest: false
})
};
const lineMaterials = {
orange: new THREE.LineBasicMaterial({
color: 0xff987d,
transparent: true,
opacity: 0.65,
depthTest: false
}),
cyan: new THREE.LineBasicMaterial({
color: 0x71f4e2,
transparent: true,
opacity: 0.85,
depthTest: false
})
};
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(0.45, 0.028, 4, 28),
new THREE.MeshBasicMaterial({
color: 0x71f4e2,
transparent: true,
opacity: 0.94,
depthTest: false
})
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.position.y = 0.295;
boardGroup.add(selectionRing);
function clearHighlightGroup() {
for (const child of [...highlightGroup.children]) {
if (child.userData.ownGeometry) {
child.geometry.dispose();
}
highlightGroup.remove(child);
}
}
function addMarker(index, material, y = 0.30) {
const { x, z } = cellPosition(index);
const marker = new THREE.Mesh(markerGeometry, material);
marker.rotation.x = Math.PI / 2;
marker.position.set(x, y, z);
marker.renderOrder = 10;
highlightGroup.add(marker);
}
function addPairLine(aIndex, bIndex, material) {
const a = cellPosition(aIndex);
const b = cellPosition(bIndex);
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(a.x, 0.34, a.z),
new THREE.Vector3(b.x, 0.34, b.z)
]);
const line = new THREE.Line(geometry, material);
line.userData.ownGeometry = true;
line.renderOrder = 9;
highlightGroup.add(line);
}
function updateHighlights() {
clearHighlightGroup();
const result = results[activeRule];
if (!result || result.passed) {
updateSelectionRing();
return;
}
if (selectedMismatch >= result.mismatches.length) {
selectedMismatch = Math.max(0, result.mismatches.length - 1);
}
result.mismatches.forEach((pair, i) => {
const chosen = selectedMismatch === i;
const material = chosen ? markerMaterials.cyan : markerMaterials.orange;
const lineMaterial = chosen ? lineMaterials.cyan : lineMaterials.orange;
const aIndex = pair.a[0] * COLS + pair.a[1];
const bIndex = pair.b[0] * COLS + pair.b[1];
addMarker(aIndex, material);
addMarker(bIndex, material);
addPairLine(aIndex, bIndex, lineMaterial);
});
updateSelectionRing();
}
function updateSelectionRing() {
const { x, z } = cellPosition(selectedIndex);
selectionRing.position.set(x, 0.295, z);
}
function allMismatchCells() {
const set = new Set();
for (const result of Object.values(results)) {
for (const pair of result.mismatches) {
set.add(pair.a[0] * COLS + pair.a[1]);
set.add(pair.b[0] * COLS + pair.b[1]);
}
}
return set;
}
function refreshTileMaterials() {
const mismatchCells = allMismatchCells();
tiles.forEach((tile, index) => {
const material = tile.base.material;
material.color.copy(tile.baseColor);
if (mismatchCells.has(index)) {
material.color.lerp(new THREE.Color(0xe09a83), 0.38);
material.emissive.set(0xff765d);
material.emissiveIntensity = index === selectedIndex ? 0.24 : 0.1;
} else if (index === selectedIndex) {
material.emissive.set(0x58e0cf);
material.emissiveIntensity = 0.2;
} else if (index === hoveredIndex) {
material.emissive.set(0x58e0cf);
material.emissiveIntensity = 0.08;
} else {
material.emissive.set(0x000000);
material.emissiveIntensity = 0;
}
});
}
// ---------- Inspector rendering ----------
function renderRules() {
const list = $("#ruleList");
list.innerHTML = Object.entries(RULE_META).map(([id, meta]) => {
const result = results[id];
const matched = result.total - result.mismatches.length;
const percentage = Math.round((matched / result.total) * 100);
return `
<button
class="rule-card ${activeRule === id ? "active" : ""}"
data-rule="${id}"
style="--rule-color:${meta.color}"
aria-pressed="${activeRule === id}"
>
<span class="rule-glyph">${meta.short}</span>
<span class="rule-main">
<span class="rule-name">${meta.title}</span>
<span class="rule-scope">${meta.scope}</span>
</span>
<span class="rule-status ${result.passed ? "pass" : ""}">
${result.passed ? "PASS" : `${result.mismatches.length} FAIL`}
</span>
<span class="rule-meter"><span style="width:${percentage}%"></span></span>
</button>
`;
}).join("");
}
function renderDetail() {
const panel = $("#detailPanel");
const meta = RULE_META[activeRule];
const result = results[activeRule];
if (result.passed) {
panel.innerHTML = `
<div class="detail-heading">
<h2>${meta.title}</h2>
<span class="detail-count" style="color:var(--green)">Satisfied</span>
</div>
<div class="pass-message">
<b>✓</b>
<span>All ${result.total} checked pairs currently agree. This constraint is complete.</span>
</div>
`;
return;
}
panel.innerHTML = `
<div class="detail-heading">
<h2>${meta.title}</h2>
<span class="detail-count">${result.mismatches.length} mismatched</span>
</div>
<p class="detail-copy">
${meta.scope}. Select a pair to isolate it, then make both tiles carry the same motif.
</p>
<div class="mismatch-list">
${result.mismatches.map((pair, i) => `
<button class="mismatch ${selectedMismatch === i ? "active" : ""}" data-mismatch="${i}">
<span>${coordinateName(pair.a)} ↔ ${coordinateName(pair.b)}</span>
<span>${SYMBOLS[board[pair.a[0]][pair.a[1]]].glyph} ≠ ${SYMBOLS[board[pair.b[0]][pair.b[1]]].glyph}</span>
</button>
`).join("")}
</div>
`;
}
function renderSelectedBar() {
const { row, col } = cellPosition(selectedIndex);
$("#selectedName").textContent = coordinateName([row, col]);
$("#selectedMotif").textContent = SYMBOLS[board[Math.floor(selectedIndex / COLS)][selectedIndex % COLS]].name;
document.querySelectorAll(".symbol-button").forEach(button => {
button.classList.toggle("active", Number(button.dataset.symbol) === board[Math.floor(selectedIndex / COLS)][selectedIndex % COLS]);
});
}
function renderGlobalStatus() {
const failedRules = Object.values(results).filter(result => !result.passed).length;
const status = $("#globalStatus");
const text = $("#globalStatusText");
if (failedRules === 0) {
status.classList.add("success");
text.textContent = "All constraints satisfied";
} else {
status.classList.remove("success");
text.textContent = `${failedRules} constraint${failedRules === 1 ? "" : "s"} need attention`;
}
}
function renderAll() {
renderRules();
renderDetail();
renderSelectedBar();
renderGlobalStatus();
refreshTileMaterials();
updateHighlights();
}
function checkArrangement(showToast = false) {
results = evaluateArrangement();
const passed = Object.values(results).every(result => result.passed);
if (passed && !wasSuccessful) {
wasSuccessful = true;
successStarted = performance.now();
$("#successOverlay").classList.add("show");
$("#successOverlay").setAttribute("aria-hidden", "false");
} else if (!passed) {
wasSuccessful = false;
$("#successOverlay").classList.remove("show");
$("#successOverlay").setAttribute("aria-hidden", "true");
}
renderAll();
if (showToast) {
const failures = Object.values(results).reduce((sum, result) => sum + result.mismatches.length, 0);
showToastMessage(
passed
? "Verification complete: every symmetry passes."
: `Verification complete: ${failures} mismatched pair${failures === 1 ? "" : "s"} remain.`
);
}
}
function showToastMessage(message) {
const toast = $("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2300);
}
function selectCell(index) {
selectedIndex = Math.max(0, Math.min(ROWS * COLS - 1, index));
const row = Math.floor(selectedIndex / COLS);
const col = selectedIndex % COLS;
const mismatchList = results[activeRule].mismatches;
selectedMismatch = mismatchList.findIndex(pair =>
(pair.a[0] === row && pair.a[1] === col) ||
(pair.b[0] === row && pair.b[1] === col)
);
if (selectedMismatch < 0) selectedMismatch = 0;
renderDetail();
renderSelectedBar();
refreshTileMaterials();
updateHighlights();
}
function setSelectedSymbol(symbol) {
if (!Number.isInteger(symbol) || symbol < 0 || symbol >= SYMBOLS.length) return;
board[Math.floor(selectedIndex / COLS)][selectedIndex % COLS] = symbol;
updateGlyph(selectedIndex);
checkArrangement(false);
}
function cycleSelectedSymbol() {
const next = (board[Math.floor(selectedIndex / COLS)][selectedIndex % COLS] + 1) % SYMBOLS.length;
setSelectedSymbol(next);
}
// ---------- UI events ----------
$("#ruleList").addEventListener("click", event => {
const card = event.target.closest("[data-rule]");
if (!card) return;
activeRule = card.dataset.rule;
selectedMismatch = 0;
renderAll();
});
$("#detailPanel").addEventListener("click", event => {
const button = event.target.closest("[data-mismatch]");
if (!button) return;
selectedMismatch = Number(button.dataset.mismatch);
const pair = results[activeRule].mismatches[selectedMismatch];
if (pair) selectCell(pair.a[0] * COLS + pair.a[1]);
});
$("#palette").addEventListener("click", event => {
const button = event.target.closest("[data-symbol]");
if (!button) return;
setSelectedSymbol(Number(button.dataset.symbol));
});
$("#cycleBtn").addEventListener("click", cycleSelectedSymbol);
$("#checkBtn").addEventListener("click", () => checkArrangement(true));
$("#checkBtn2").addEventListener("click", () => checkArrangement(true));
$("#resetBtn").addEventListener("click", () => {
board = initialBoard.map(row => row.slice());
tiles.forEach((_, index) => updateGlyph(index));
selectedIndex = 0;
selectedMismatch = 0;
wasSuccessful = false;
$("#successOverlay").classList.remove("show");
$("#successOverlay").setAttribute("aria-hidden", "true");
checkArrangement(false);
showToastMessage("The starting arrangement has been restored.");
});
$("#closeSuccessBtn").addEventListener("click", () => {
$("#successOverlay").classList.remove("show");
$("#successOverlay").setAttribute("aria-hidden", "true");
});
window.addEventListener("keydown", event => {
if (event.ctrlKey || event.metaKey || event.altKey) return;
const key = event.key.toLowerCase();
let row = Math.floor(selectedIndex / COLS);
let col = selectedIndex % COLS;
if (key === "arrowup") row--;
else if (key === "arrowdown") row++;
else if (key === "arrowleft") col--;
else if (key === "arrowright") col++;
else if (key === "1") return setSelectedSymbol(0);
else if (key === "2") return setSelectedSymbol(1);
else if (key === "3") return setSelectedSymbol(2);
else if (key === "4") return setSelectedSymbol(3);
else if (key === " " || key === "enter") {
event.preventDefault();
return cycleSelectedSymbol();
} else return;
event.preventDefault();
row = Math.max(0, Math.min(ROWS - 1, row));
col = Math.max(0, Math.min(COLS - 1, col));
selectCell(row * COLS + col);
});
// ---------- Custom camera controls and picking ----------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointerState = {
down: false,
moved: false,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
button: 0,
pan: 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;
}
function pickTile(event) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(tileBases, false);
if (hits.length) {
selectCell(hits[0].object.userData.index);
}
}
canvas.addEventListener("pointerdown", event => {
pointerState.down = true;
pointerState.moved = false;
pointerState.startX = event.clientX;
pointerState.startY = event.clientY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
pointerState.button = event.button;
pointerState.pan = event.button === 2 || event.shiftKey;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", event => {
if (pointerState.down) {
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
const totalDistance = Math.hypot(
event.clientX - pointerState.startX,
event.clientY - pointerState.startY
);
if (totalDistance > 4) pointerState.moved = true;
if (pointerState.moved) {
if (pointerState.pan) {
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 = cameraState.distance * 0.0013;
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.addScaledVector(forward, dy * scale);
cameraState.target.x = THREE.MathUtils.clamp(cameraState.target.x, -3, 3);
cameraState.target.z = THREE.MathUtils.clamp(cameraState.target.z, -3, 3);
} else {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.35,
1.38
);
}
}
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
} else {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(tileBases, false);
const nextHovered = hits.length ? hits[0].object.userData.index : -1;
if (nextHovered !== hoveredIndex) {
hoveredIndex = nextHovered;
refreshTileMaterials();
}
canvas.style.cursor = hoveredIndex >= 0 ? "pointer" : "grab";
}
});
canvas.addEventListener("pointerup", event => {
const shouldPick = pointerState.down && !pointerState.moved && pointerState.button === 0;
pointerState.down = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (shouldPick) pickTile(event);
canvas.style.cursor = hoveredIndex >= 0 ? "pointer" : "grab";
});
canvas.addEventListener("pointercancel", () => {
pointerState.down = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener("pointerleave", () => {
if (!pointerState.down && hoveredIndex !== -1) {
hoveredIndex = -1;
refreshTileMaterials();
}
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.012,
7.2,
19
);
}, { passive: false });
canvas.addEventListener("contextmenu", event => event.preventDefault());
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, false);
});
// ---------- Animation ----------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
applyCamera();
beacons.forEach((beacon, index) => {
beacon.rotation.y += delta * (0.35 + index * 0.04);
});
beaconMaterial.emissiveIntensity = 0.38 + Math.sin(elapsed * 1.6) * 0.13;
if (wasSuccessful) {
const age = (performance.now() - successStarted) / 1000;
const pulse = 1 + Math.sin(Math.min(age, 2) * Math.PI * 2) * Math.max(0, 0.008 - age * 0.003);
boardGroup.scale.setScalar(pulse);
} else {
boardGroup.scale.setScalar(1);
}
renderer.render(scene, camera);
}
renderAll();
animate();
</script>
</body>
</html>
```