Prompt specification
Build or customize the Three.js 3D scene "Picture-free shape reconstruction game" 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: Picture-free shape reconstruction game
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/picture-free-shape-reconstruction-game-x0344
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts jigsaw puzzle boundaries. Create an irregular puzzle with no image texture; users rely on geometry and boundary constraints, and a connectivity check confirms when every piece forms one continuous object. Low-poly.
Working Three.js Starter (picture-free-shape-reconstruction-game-x0344):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Boundary Lab — Geometric Jigsaw</title>
<style>
:root {
color-scheme: dark;
--bg: #0a1016;
--panel: rgba(15, 23, 31, 0.88);
--border: rgba(173, 208, 224, 0.16);
--text: #e8f0f3;
--muted: #8ea2ad;
--accent: #77e0d2;
--accent-dark: #163d3d;
--warn: #efb66a;
--danger: #ed8177;
}
* {
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;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.brand {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
display: flex;
align-items: center;
gap: 13px;
pointer-events: none;
}
.brand-mark {
width: 43px;
height: 43px;
display: grid;
place-items: center;
color: var(--accent);
border: 1px solid rgba(119, 224, 210, 0.35);
background: rgba(12, 29, 34, 0.68);
clip-path: polygon(25% 6%, 75% 6%, 100% 50%, 75% 94%, 25% 94%, 0 50%);
}
.brand-mark svg {
width: 24px;
height: 24px;
}
.eyebrow {
margin: 0 0 3px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.2vw, 31px);
font-weight: 680;
letter-spacing: -0.045em;
}
.brand-note {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.025em;
}
.panel {
position: fixed;
z-index: 6;
top: 22px;
right: 22px;
width: min(326px, calc(100vw - 44px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 70px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px) saturate(125%);
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 17px;
}
.panel-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.live-dot {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.live-dot::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--warn);
box-shadow: 0 0 12px rgba(239, 182, 106, 0.5);
content: "";
}
.metric {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 20px;
padding: 3px 0 17px;
border-bottom: 1px solid var(--border);
}
.metric-number {
font-size: 38px;
font-weight: 720;
line-height: 0.95;
letter-spacing: -0.06em;
}
.metric-number span {
color: var(--muted);
font-size: 18px;
font-weight: 500;
}
.metric-caption {
max-width: 142px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
text-align: right;
}
.integrity {
min-height: 60px;
padding: 16px 0 13px;
}
.integrity strong {
display: block;
margin-bottom: 5px;
color: var(--text);
font-size: 13px;
letter-spacing: 0.045em;
}
.integrity p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.piece-states {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 6px;
margin: 2px 0 17px;
}
.piece-state {
position: relative;
height: 8px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
transition: background 180ms ease, box-shadow 180ms ease;
}
.piece-state.linked {
background: var(--accent);
box-shadow: 0 0 11px rgba(119, 224, 210, 0.44);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.action {
min-height: 40px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
color: var(--text);
cursor: pointer;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.035em;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
.action:hover {
border-color: rgba(119, 224, 210, 0.42);
background: rgba(119, 224, 210, 0.08);
}
.action:active {
transform: translateY(1px);
}
.action.primary {
grid-column: 1 / -1;
border-color: rgba(119, 224, 210, 0.4);
background: var(--accent);
color: #09201f;
}
.action.primary:hover {
background: #91eadf;
}
.action.secondary-wide {
grid-column: 1 / -1;
}
.field-label {
position: fixed;
z-index: 3;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
color: rgba(172, 202, 213, 0.24);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.28em;
pointer-events: none;
text-transform: uppercase;
}
.instructions {
position: fixed;
z-index: 5;
left: 26px;
bottom: 23px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100vw - 410px);
pointer-events: none;
}
.instruction {
padding: 8px 11px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(12, 18, 24, 0.72);
color: var(--muted);
font-size: 10px;
letter-spacing: 0.02em;
backdrop-filter: blur(10px);
}
.instruction b {
color: var(--text);
font-weight: 750;
}
.toast {
position: fixed;
z-index: 12;
left: 50%;
bottom: 30px;
max-width: min(500px, calc(100vw - 36px));
padding: 11px 15px;
border: 1px solid rgba(119, 224, 210, 0.28);
border-radius: 10px;
background: rgba(10, 24, 28, 0.94);
color: #dffaf5;
font-size: 12px;
line-height: 1.4;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.success {
position: fixed;
z-index: 11;
left: 50%;
top: 50%;
width: min(440px, calc(100vw - 36px));
padding: 27px;
border: 1px solid rgba(119, 224, 210, 0.45);
border-radius: 18px;
background: rgba(9, 28, 31, 0.94);
box-shadow:
0 30px 100px rgba(0, 0, 0, 0.5),
0 0 70px rgba(119, 224, 210, 0.1);
opacity: 0;
pointer-events: none;
text-align: center;
transform: translate(-50%, -46%) scale(0.96);
transition:
opacity 240ms ease,
transform 240ms ease;
}
.success.show {
opacity: 1;
transform: translate(-50%, -50%) scale(1);
}
.success-mark {
display: grid;
place-items: center;
width: 52px;
height: 52px;
margin: 0 auto 15px;
border: 1px solid rgba(119, 224, 210, 0.45);
border-radius: 50%;
color: var(--accent);
font-size: 26px;
}
.success h2 {
margin: 0 0 8px;
font-size: 21px;
letter-spacing: -0.025em;
}
.success p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 15px;
}
.brand-mark {
width: 37px;
height: 37px;
}
.brand-note {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 13px;
border-radius: 14px;
}
.panel-header,
.integrity {
display: none;
}
.metric {
align-items: center;
padding: 0 0 11px;
}
.metric-number {
font-size: 27px;
}
.metric-caption {
text-align: right;
}
.piece-states {
margin: 10px 0;
}
.actions {
grid-template-columns: repeat(4, 1fr);
}
.action {
min-height: 36px;
padding: 6px;
font-size: 9px;
}
.action.primary {
grid-column: span 2;
}
.action.secondary-wide {
grid-column: span 2;
}
.instructions {
display: none;
}
.field-label {
display: none;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive low-poly geometric jigsaw"></div>
<header class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none">
<path
d="M4 4h6v3a3 3 0 1 0 4 0V4h6v6h-3a3 3 0 1 0 0 4h3v6h-6v-3a3 3 0 1 0-4 0v3H4v-6h3a3 3 0 1 0 0-4H4V4Z"
stroke="currentColor"
stroke-width="1.35"
stroke-linejoin="round"
/>
</svg>
</div>
<div>
<p class="eyebrow">Boundary Lab / Study 06</p>
<h1>Geometric Assembly</h1>
<p class="brand-note">Six irregular pieces. No image data. Geometry only.</p>
</div>
</header>
<aside class="panel" aria-label="Puzzle connectivity controls">
<div class="panel-header">
<span class="panel-label">Continuity monitor</span>
<span class="live-dot">Live</span>
</div>
<div class="metric">
<div class="metric-number">
<b id="linked-count">0</b><span> / 7</span>
</div>
<div class="metric-caption">
matched internal boundary interfaces
</div>
</div>
<div class="integrity" aria-live="polite">
<strong id="integrity-title">AWAITING CONTINUITY CHECK</strong>
<p id="integrity-copy">
Arrange every piece, then inspect the shared boundary geometry.
</p>
</div>
<div id="piece-states" class="piece-states" aria-label="Individual piece states"></div>
<div class="actions">
<button id="check" class="action primary">Check connections</button>
<button id="rotate-left" class="action" title="Rotate selected piece counterclockwise">
↶ Rotate
</button>
<button id="rotate-right" class="action" title="Rotate selected piece clockwise">
Rotate ↷
</button>
<button id="hint" class="action">Show hint</button>
<button id="scatter" class="action secondary-wide">Scatter pieces</button>
</div>
</aside>
<div class="field-label">Assembly field</div>
<div class="instructions" aria-hidden="true">
<div class="instruction"><b>Drag</b> to move</div>
<div class="instruction"><b>R / wheel</b> to rotate</div>
<div class="instruction"><b>Right-click</b> rotates left</div>
<div class="instruction"><b>Hint</b> reveals one boundary fit</div>
</div>
<div id="toast" class="toast" role="status" aria-live="polite"></div>
<div id="success" class="success" role="status" aria-live="assertive">
<div class="success-mark">✓</div>
<h2>One continuous object</h2>
<p>
All six pieces are linked through all seven internal boundary interfaces.
The geometric assembly is complete.
</p>
</div>
<noscript>This scene requires JavaScript.</noscript>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Configuration and scene
// ---------------------------------------------------------------------
const COLS = 3;
const ROWS = 2;
const CELL_W = 2.05;
const CELL_H = 1.38;
const TAB_SCALE = 0.30;
const OFFSET_X = -(COLS * CELL_W) / 2;
const OFFSET_Y = -(ROWS * CELL_H) / 2;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a1016);
scene.fog = new THREE.Fog(0x0a1016, 19, 38);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.getElementById("scene").appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-9, 9, 5, -5, 0.1, 80);
camera.position.set(0, -4.2, 18);
camera.lookAt(0, 0, 0);
const hemisphere = new THREE.HemisphereLight(0xddeef5, 0x253039, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2dc, 3.4);
keyLight.position.set(-5, 7, 12);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x70d9d0, 1.35);
rimLight.position.set(8, -4, 8);
scene.add(rimLight);
const board = new THREE.Mesh(
new THREE.PlaneGeometry(18.4, 10.2),
new THREE.MeshStandardMaterial({
color: 0x111a22,
roughness: 0.96,
metalness: 0.02
})
);
board.position.z = -0.38;
board.receiveShadow = true;
scene.add(board);
// ---------------------------------------------------------------------
// Low-poly workbench facets
// ---------------------------------------------------------------------
let seed = 1937;
const random = () => {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
};
const facetColors = [
0x172631,
0x1b2c36,
0x14232d,
0x20333d,
0x182a34
];
for (let i = 0; i < 42; i++) {
const cx = (random() - 0.5) * 17.5;
const cy = (random() - 0.5) * 9.5;
const radius = 0.45 + random() * 1.3;
const a = random() * Math.PI * 2;
const b = random() * Math.PI * 2;
const c = random() * Math.PI * 2;
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
cx + Math.cos(a) * radius,
cy + Math.sin(a) * radius,
0
],
3
)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(
[
0.08 + random() * 0.04,
0.14 + random() * 0.05,
0.18 + random() * 0.05
],
3
)
);
const facet = new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial({
vertexColors: true,
transparent: true,
opacity: 0.18,
depthWrite: false,
side: THREE.DoubleSide
})
);
facet.position.z = -0.35;
scene.add(facet);
}
const gridPositions = [];
for (let x = -8; x <= 8; x += 1) {
gridPositions.push(x, -4.8, -0.33, x, 4.8, -0.33);
}
for (let y = -4; y <= 4; y += 1) {
gridPositions.push(-8.8, y, -0.33, 8.8, y, -0.33);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
scene.add(
new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x8aa6b3,
transparent: true,
opacity: 0.075
})
)
);
const fieldCurve = new THREE.EllipseCurve(0, 0, 3.3, 1.48, 0, Math.PI * 2);
const fieldPoints = fieldCurve.getPoints(120).map(
(point) => new THREE.Vector3(point.x, point.y, -0.29)
);
const fieldRing = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(fieldPoints),
new THREE.LineDashedMaterial({
color: 0x6fbdb4,
transparent: true,
opacity: 0.28,
dashSize: 0.13,
gapSize: 0.09
})
);
fieldRing.computeLineDistances();
scene.add(fieldRing);
// ---------------------------------------------------------------------
// Jigsaw profile functions
// ---------------------------------------------------------------------
function cubicPoint(p0, c1, c2, p1, t) {
const u = 1 - t;
const uu = u * u;
const tt = t * t;
return [
uu * u * p0[0] +
3 * uu * t * c1[0] +
3 * u * tt * c2[0] +
tt * t * p1[0],
uu * u * p0[1] +
3 * u * t * c1[1] +
3 * u * tt * c2[1] +
tt * t * p1[1]
];
}
// A faceted jigsaw neck and bulb. Coordinates are normalized along the
// edge and perpendicular to it.
const profileSegments = [
{
p0: [0.00, 0.00],
c1: [0.12, 0.01],
c2: [0.24, 0.01],
p1: [0.32, 0.08]
},
{
p0: [0.32, 0.08],
c1: [0.41, 0.31],
c2: [0.50, 0.90],
p1: [0.58, 1.00]
},
{
p0: [0.58, 1.00],
c1: [0.66, 1.00],
c2: [0.74, 0.78],
p1: [0.72, 0.48]
},
{
p0: [0.72, 0.48],
c1: [0.70, 0.20],
c2: [0.66, -0.10],
p1: [0.55, -0.28]
},
{
p0: [0.55, -0.28],
c1: [0.45, -0.43],
c2: [0.36, -0.25],
p1: [0.34, -0.08]
},
{
p0: [0.34, -0.08],
c1: [0.38, -0.04],
c2: [0.66, -0.01],
p1: [1.00, 0.00]
}
];
function rawProfile(t) {
const clamped = THREE.MathUtils.clamp(t, 0, 1);
const scaled = clamped * profileSegments.length;
const index = Math.min(profileSegments.length - 1, Math.floor(scaled));
const local = scaled - index;
const segment = profileSegments[index];
return cubicPoint(
segment.p0,
segment.c1,
segment.c2,
segment.p1,
local
);
}
function knobProfile(t, flipped = false) {
if (!flipped) return rawProfile(t);
const source = rawProfile(1 - t);
return [1 - source[0], source[1]];
}
function outerLeftAt(row, t) {
return {
x:
-0.17 +
0.12 * Math.sin(Math.PI * t) -
0.035 * Math.sin(3 * Math.PI * t),
y: row * CELL_H + t * CELL_H
};
}
function outerRightAt(row, t) {
return {
x:
COLS * CELL_W +
0.15 +
0.11 * Math.sin(Math.PI * t) +
0.035 * Math.sin(4 * Math.PI * t),
y: row * CELL_H + t * CELL_H
};
}
function outerBottomAt(col, t) {
return {
x: col * CELL_W + t * CELL_W,
y:
-0.14 +
0.09 * Math.sin(Math.PI * t) +
0.025 * Math.sin(3 * Math.PI * t)
};
}
function outerTopAt(col, t) {
return {
x: col * CELL_W + t * CELL_W,
y:
ROWS * CELL_H +
0.15 +
0.08 * Math.sin(Math.PI * t) -
0.03 * Math.sin(4 * Math.PI * t)
};
}
function verticalInternalAt(boundary, row, t) {
const flipped = (boundary + row) % 2 === 1;
const profile = knobProfile(t, flipped);
const sign =
boundary === 1
? row === 0
? 1
: -1
: row === 0
? -1
: 1;
return {
x: boundary * CELL_W + sign * TAB_SCALE * profile[1],
y: row * CELL_H + profile[0] * CELL_H
};
}
function horizontalInternalAt(col, t) {
const flipped = col % 2 === 1;
const profile = knobProfile(t, flipped);
const sign = col % 2 === 0 ? 1 : -1;
return {
x: col * CELL_W + profile[0] * CELL_W,
y: CELL_H + sign * TAB_SCALE * profile[1]
};
}
function shifted(point) {
return {
x: point.x + OFFSET_X,
y: point.y + OFFSET_Y
};
}
function makePieceOutline(col, row) {
const edgeSamples = 30;
const outerSamples = 8;
let bottom = [];
let right = [];
let top = [];
let left = [];
// Bottom: left to right.
if (row === 0) {
for (let i = 0; i < edgeSamples; i++) {
const t = i / (edgeSamples - 1);
bottom.push(shifted(outerBottomAt(col, t)));
}
} else {
for (let i = 0; i < edgeSamples; i++) {
const t = i / (edgeSamples - 1);
bottom.push(shifted(horizontalInternalAt(col, t)));
}
}
// Right: bottom to top.
if (col === COLS - 1) {
for (let i = 0; i < outerSamples; i++) {
const t = i / (outerSamples - 1);
right.push(shifted(outerRightAt(row, t)));
}
} else {
for (let i = 0; i < edgeSamples; i++) {
const t = i / (edgeSamples - 1);
right.push(shifted(verticalInternalAt(col + 1, row, t)));
}
}
// Top: right to left.
if (row === ROWS - 1) {
const topLeftToRight = [];
for (let i = 0; i < outerSamples; i++) {
const t = i / (outerSamples - 1);
topLeftToRight.push(shifted(outerTopAt(col, t)));
}
top = topLeftToRight.reverse();
} else {
const topLeftToRight = [];
for (let i = 0; i < edgeSamples; i++) {
const t = i / (edgeSamples - 1);
topLeftToRight.push(shifted(horizontalInternalAt(col, t)));
}
top = topLeftToRight.reverse();
}
// Left: top to bottom.
if (col === 0) {
for (let i = 0; i < outerSamples; i++) {
const t = 1 - i / (outerSamples - 1);
left.push(shifted(outerLeftAt(row, t)));
}
} else {
const leftBottomToTop = [];
for (let i = 0; i < edgeSamples; i++) {
const t = i / (edgeSamples - 1);
leftBottomToTop.push(shifted(verticalInternalAt(col, row, t)));
}
left = leftBottomToTop.reverse();
}
const outline = [...bottom, ...right, ...top, ...left].filter(
(point, index, all) =>
index === 0 ||
Math.hypot(
point.x - all[index - 1].x,
point.y - all[index - 1].y
) > 0.00001
);
return { outline, bottom, right, top, left };
}
// ---------------------------------------------------------------------
// Piece meshes
// ---------------------------------------------------------------------
const palette = [
0xefa45f,
0xe8c96c,
0x78c5a2,
0x68a7d7,
0xa58bcf,
0xe67d76
];
const pieces = [];
const pieceMeshes = [];
const scatterPositions = [
{ x: -5.25, y: 2.55, angle: 0 },
{ x: -1.75, y: 2.75, angle: Math.PI / 2 },
{ x: 1.85, y: 2.48, angle: 0 },
{ x: -5.15, y: -2.40, angle: Math.PI },
{ x: -1.65, y: -2.65, angle: -Math.PI / 2 },
{ x: 1.95, y: -2.42, angle: Math.PI / 2 }
];
function createPiece(col, row, index) {
const data = makePieceOutline(col, row);
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (const point of data.outline) {
minX = Math.min(minX, point.x);
minY = Math.min(minY, point.y);
maxX = Math.max(maxX, point.x);
maxY = Math.max(maxY, point.y);
}
const home = {
x: (minX + maxX) / 2,
y: (minY + maxY) / 2
};
const localOutline = data.outline.map(
(point) => new THREE.Vector2(point.x - home.x, point.y - home.y)
);
const shape = new THREE.Shape();
shape.moveTo(localOutline[0].x, localOutline[0].y);
for (let i = 1; i < localOutline.length; i++) {
shape.lineTo(localOutline[i].x, localOutline[i].y);
}
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.16,
steps: 1,
curveSegments: 1,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: 0.035,
bevelThickness: 0.035
});
geometry.computeVertexNormals();
const positions = geometry.attributes.position;
const colors = new Float32Array(positions.count * 3);
const baseColor = new THREE.Color(palette[index]);
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const y = positions.getY(i);
const z = positions.getZ(i);
const variation =
0.035 * Math.sin(x * 2.8 + y * 1.7) +
0.018 * Math.cos(y * 4.1 - x * 1.3 + z * 7.0);
const vertexColor = baseColor.clone().offsetHSL(0, 0, variation);
colors[i * 3] = vertexColor.r;
colors[i * 3 + 1] = vertexColor.g;
colors[i * 3 + 2] = vertexColor.b;
}
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
const faceMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
roughness: 0.78,
metalness: 0.025,
flatShading: true
});
const sideMaterial = faceMaterial.clone();
sideMaterial.color.multiplyScalar(0.72);
sideMaterial.roughness = 0.9;
const group = new THREE.Group();
const mesh = new THREE.Mesh(geometry, [faceMaterial, sideMaterial]);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.pieceIndex = index;
group.add(mesh);
pieceMeshes.push(mesh);
const outlineMaterial = new THREE.LineBasicMaterial({
color: 0x172028,
transparent: true,
opacity: 0.94
});
const outlineLine = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(
localOutline.map((point) => new THREE.Vector3(point.x, point.y, 0.205))
),
outlineMaterial
);
group.add(outlineLine);
const edgeLines = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 38),
new THREE.LineBasicMaterial({
color: 0x0b1218,
transparent: true,
opacity: 0.62
})
);
group.add(edgeLines);
const ghostMaterial = new THREE.LineDashedMaterial({
color: 0xefb66a,
transparent: true,
opacity: 0.86,
dashSize: 0.12,
gapSize: 0.075
});
const ghost = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(
localOutline.map((point) => new THREE.Vector3(point.x, point.y, -0.13))
),
ghostMaterial
);
ghost.position.set(home.x, home.y, 0);
ghost.computeLineDistances();
ghost.visible = false;
scene.add(ghost);
const localEdge = (edge) =>
edge.map((point) => ({
x: point.x - home.x,
y: point.y - home.y
}));
const piece = {
index,
col,
row,
group,
mesh,
faceMaterial,
sideMaterial,
outline: outlineLine,
ghost,
home,
x: scatterPositions[index].x,
y: scatterPositions[index].y,
z: 0,
angle: scatterPositions[index].angle,
anim: null,
links: 0,
edges: {
bottom: localEdge(data.bottom),
right: localEdge(data.right),
top: localEdge(data.top),
left: localEdge(data.left)
}
};
applyPieceTransform(piece);
scene.add(group);
return piece;
}
function applyPieceTransform(piece) {
piece.group.position.set(piece.x, piece.y, piece.z);
piece.group.rotation.z = piece.angle;
}
let index = 0;
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
pieces.push(createPiece(col, row, index++));
}
}
// ---------------------------------------------------------------------
// Shared-boundary inspection lines
// ---------------------------------------------------------------------
const adjacencyPairs = [];
// Vertical interfaces: left piece's right edge to right piece's left edge.
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS - 1; col++) {
adjacencyPairs.push({
a: row * COLS + col,
b: row * COLS + col + 1,
edgeA: "right",
edgeB: "left",
reverseB: true
});
}
}
// Horizontal interfaces: lower piece's top edge to upper piece's bottom.
for (let col = 0; col < COLS; col++) {
adjacencyPairs.push({
a: col,
b: COLS + col,
edgeA: "top",
edgeB: "bottom",
reverseB: true
});
}
for (const pair of adjacencyPairs) {
const line = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineBasicMaterial({
color: 0xed8177,
transparent: true,
opacity: 0.3
})
);
line.visible = false;
line.renderOrder = 4;
pair.line = line;
scene.add(line);
}
// ---------------------------------------------------------------------
// UI state
// ---------------------------------------------------------------------
const linkedCount = document.getElementById("linked-count");
const integrityTitle = document.getElementById("integrity-title");
const integrityCopy = document.getElementById("integrity-copy");
const pieceStates = document.getElementById("piece-states");
const successPanel = document.getElementById("success");
const toast = document.getElementById("toast");
const stateElements = pieces.map((piece, i) => {
const element = document.createElement("span");
element.className = "piece-state";
element.title = `Piece ${i + 1}`;
pieceStates.appendChild(element);
return element;
});
let selectedPiece = null;
let hintIndex = -1;
let toastTimer = null;
let successTimer = null;
function showToast(message) {
clearTimeout(toastTimer);
toast.textContent = message;
toast.classList.add("show");
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 2800);
}
function setSelected(piece) {
selectedPiece = piece;
for (const candidate of pieces) {
const active = candidate === piece;
candidate.faceMaterial.emissive.set(active ? 0x0d292a : 0x000000);
candidate.sideMaterial.emissive.set(active ? 0x0d292a : 0x000000);
candidate.outline.material.color.set(active ? 0x77e0d2 : 0x172028);
candidate.outline.material.opacity = active ? 1 : 0.94;
}
renderer.domElement.style.cursor = piece ? "grab" : "default";
}
function normalizeAngle(angle) {
return Math.atan2(Math.sin(angle), Math.cos(angle));
}
function animatePiece(piece, x, y, angle, duration = 0.28) {
piece.anim = {
time: 0,
duration,
x0: piece.x,
y0: piece.y,
angle0: piece.angle,
x1: x,
y1: y,
angle1: angle
};
}
function rotatePiece(delta) {
if (!selectedPiece) {
showToast("Select a piece before rotating it.");
return;
}
const piece = selectedPiece;
piece.anim = null;
const targetAngle = piece.angle + delta;
animatePiece(
piece,
piece.x,
piece.y,
normalizeAngle(targetAngle),
0.22
);
}
function resetPieces() {
for (const piece of pieces) {
const position = scatterPositions[piece.index];
piece.anim = null;
piece.x = position.x;
piece.y = position.y;
piece.z = 0;
piece.angle = position.angle;
piece.links = 0;
piece.ghost.visible = false;
applyPieceTransform(piece);
}
hintIndex = -1;
setSelected(null);
successPanel.classList.remove("show");
clearTimeout(successTimer);
for (const pair of adjacencyPairs) {
pair.line.visible = false;
}
stateElements.forEach((element) => element.classList.remove("linked"));
linkedCount.textContent = "0";
integrityTitle.textContent = "AWAITING CONTINUITY CHECK";
integrityCopy.textContent =
"Arrange every piece, then inspect the shared boundary geometry.";
showToast("Pieces scattered. Build one continuous boundary.");
}
// ---------------------------------------------------------------------
// Geometry-based connectivity
// ---------------------------------------------------------------------
function transformLocalPoint(point, piece) {
const cosine = Math.cos(piece.angle);
const sine = Math.sin(piece.angle);
return {
x: piece.x + cosine * point.x - sine * point.y,
y: piece.y + sine * point.x + cosine * point.y
};
}
function edgeMatchDistance(pair) {
const edgeA = pieces[pair.a].edges[pair.edgeA];
const edgeB = pieces[pair.b].edges[pair.edgeB];
let maximum = 0;
let total = 0;
for (let i = 0; i < edgeA.length; i++) {
const a = transformLocalPoint(edgeA[i], pieces[pair.a]);
const bIndex = pair.reverseB ? edgeB.length - 1 - i : i;
const b = transformLocalPoint(edgeB[bIndex], pieces[pair.b]);
const distance = Math.hypot(a.x - b.x, a.y - b.y);
maximum = Math.max(maximum, distance);
total += distance;
}
return {
maximum,
average: total / edgeA.length
};
}
function updateConnectionLines() {
for (const pair of adjacencyPairs) {
const edge = pieces[pair.a].edges[pair.edgeA];
const points = edge.map((point) => {
const transformed = transformLocalPoint(point, pieces[pair.a]);
return new THREE.Vector3(transformed.x, transformed.y, 0.255);
});
pair.line.geometry.setFromPoints(points);
}
}
function checkConnections(showLines = true, announce = true) {
const graph = pieces.map(() => []);
let matchedPairs = 0;
for (const piece of pieces) {
piece.links = 0;
}
for (const pair of adjacencyPairs) {
const distance = edgeMatchDistance(pair);
const matched =
distance.maximum <= 0.145 && distance.average <= 0.075;
pair.matched = matched;
if (matched) {
matchedPairs++;
graph[pair.a].push(pair.b);
graph[pair.b].push(pair.a);
pieces[pair.a].links++;
pieces[pair.b].links++;
}
if (showLines) {
pair.line.visible = true;
pair.line.material.color.set(matched ? 0x77e0d2 : 0xed8177);
pair.line.material.opacity = matched ? 0.95 : 0.25;
}
}
updateConnectionLines();
// Count connected components in the actual matched-boundary graph.
const visited = new Set();
let componentCount = 0;
for (let i = 0; i < pieces.length; i++) {
if (visited.has(i)) continue;
componentCount++;
const stack = [i];
visited.add(i);
while (stack.length) {
const current = stack.pop();
for (const neighbor of graph[current]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
stack.push(neighbor);
}
}
}
}
const everyPieceLinked = pieces.every((piece) => piece.links > 0);
const networkConnected =
everyPieceLinked && componentCount === 1;
const complete = matchedPairs === adjacencyPairs.length;
linkedCount.textContent = String(matchedPairs);
stateElements.forEach((element, i) => {
element.classList.toggle("linked", pieces[i].links > 0);
});
if (complete && networkConnected) {
integrityTitle.textContent = "CONTINUOUS OBJECT VERIFIED";
integrityCopy.textContent =
"All seven internal interfaces match. Six pieces form one sealed geometric object.";
fieldRing.material.color.set(0x77e0d2);
fieldRing.material.opacity = 0.82;
successPanel.classList.add("show");
clearTimeout(successTimer);
successTimer = setTimeout(() => {
successPanel.classList.remove("show");
}, 4300);
if (announce) {
showToast("Continuity confirmed: all seven boundaries are locked.");
}
} else if (networkConnected) {
integrityTitle.textContent = "CHAIN CONNECTED · SEAL INCOMPLETE";
integrityCopy.textContent =
`${matchedPairs} of 7 interfaces match. Close the remaining boundary gaps to complete the object.`;
fieldRing.material.color.set(0xefb66a);
fieldRing.material.opacity = 0.48;
if (announce) {
showToast("The chain is connected, but some boundaries remain open.");
}
} else {
integrityTitle.textContent = "BOUNDARY NETWORK INCOMPLETE";
integrityCopy.textContent =
`${matchedPairs} of 7 interfaces match across ${componentCount} network component${componentCount === 1 ? "" : "s"}.`;
fieldRing.material.color.set(0x6fbdb4);
fieldRing.material.opacity = 0.28;
if (announce) {
showToast("Continuity check complete: more boundary connections are needed.");
}
}
}
// ---------------------------------------------------------------------
// Pointer dragging
// ---------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), -0.2);
const planeHit = new THREE.Vector3();
let dragging = false;
let dragOffset = new THREE.Vector2();
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
setPointerFromEvent(event);
const hits = raycaster.intersectObjects(pieceMeshes, false);
if (!hits.length) {
setSelected(null);
return;
}
const piece = pieces[hits[0].object.userData.pieceIndex];
setSelected(piece);
piece.anim = null;
piece.z = 0.09;
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
dragOffset.set(piece.x - planeHit.x, piece.y - planeHit.y);
} else {
dragOffset.set(0, 0);
}
dragging = true;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || !selectedPiece) return;
setPointerFromEvent(event);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
selectedPiece.x = planeHit.x + dragOffset.x;
selectedPiece.y = planeHit.y + dragOffset.y;
applyPieceTransform(selectedPiece);
}
});
function releasePointer(event) {
if (!dragging || !selectedPiece) return;
dragging = false;
selectedPiece.z = 0;
const piece = selectedPiece;
const distance = Math.hypot(
piece.x - piece.home.x,
piece.y - piece.home.y
);
const angularError = Math.abs(normalizeAngle(piece.angle));
// A piece locks only when it is close to the geometrically correct
// location and its orientation agrees with the shared boundaries.
if (distance < 0.56 && angularError < 0.16) {
animatePiece(piece, piece.home.x, piece.home.y, 0, 0.25);
showToast("Boundary fit detected. Piece locked into place.");
} else {
applyPieceTransform(piece);
}
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
renderer.domElement.style.cursor = "grab";
setTimeout(() => checkConnections(true, false), 270);
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
if (!selectedPiece) return;
event.preventDefault();
const direction = event.deltaY > 0 ? 1 : -1;
const amount = event.shiftKey ? Math.PI / 2 : Math.PI / 12;
rotatePiece(direction * amount);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
rotatePiece(-Math.PI / 2);
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.key.toLowerCase() === "r") {
rotatePiece(event.shiftKey ? -Math.PI / 2 : Math.PI / 2);
}
});
// ---------------------------------------------------------------------
// Buttons and hints
// ---------------------------------------------------------------------
document.getElementById("check").addEventListener("click", () => {
checkConnections(true, true);
});
document.getElementById("rotate-left").addEventListener("click", () => {
rotatePiece(-Math.PI / 2);
});
document.getElementById("rotate-right").addEventListener("click", () => {
rotatePiece(Math.PI / 2);
});
document.getElementById("scatter").addEventListener("click", resetPieces);
document.getElementById("hint").addEventListener("click", () => {
const candidates = pieces.filter((piece) => {
const distance = Math.hypot(
piece.x - piece.home.x,
piece.y - piece.home.y
);
return distance > 0.35 || Math.abs(normalizeAngle(piece.angle)) > 0.2;
});
if (!candidates.length) {
showToast("Every piece is already in its fitted position. Check the connections.");
return;
}
for (const piece of pieces) {
piece.ghost.visible = false;
}
const piece = candidates[hintIndex % candidates.length];
hintIndex = (hintIndex + 1) % candidates.length;
piece.ghost.visible = true;
setSelected(piece);
showToast(
`Hint: the amber outline marks a valid fit for piece ${piece.index + 1}.`
);
});
// ---------------------------------------------------------------------
// Resize and animation loop
// ---------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const halfWidth = 9.2;
const halfHeight = halfWidth / aspect;
camera.left = -halfWidth;
camera.right = halfWidth;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
for (const piece of pieces) {
if (!piece.anim) continue;
const animation = piece.anim;
animation.time = Math.min(
1,
animation.time + delta / animation.duration
);
const eased = 1 - Math.pow(1 - animation.time, 3);
const angleDifference = normalizeAngle(
animation.angle1 - animation.angle0
);
piece.x = THREE.MathUtils.lerp(
animation.x0,
animation.x1,
eased
);
piece.y = THREE.MathUtils.lerp(
animation.y0,
animation.y1,
eased
);
piece.angle = animation.angle0 + angleDifference * eased;
piece.z = THREE.MathUtils.lerp(piece.z, 0, eased * 0.25);
applyPieceTransform(piece);
if (animation.time >= 1) {
piece.anim = null;
piece.x = animation.x1;
piece.y = animation.y1;
piece.angle = animation.angle1;
piece.z = 0;
applyPieceTransform(piece);
}
}
const pulse = 0.27 + Math.sin(performance.now() * 0.002) * 0.035;
fieldRing.material.opacity = pulse;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```