Prompt specification
Build or customize the Three.js 3D scene "Pattern-block edge matching" 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: Pattern-block edge matching
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pattern-block-edge-matching-x0361
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows geometric pattern blocks. Arrange hexagon, triangle, and rhombus blocks with equal compatible edge lengths; show a partly completed larger hexagon whose remaining gap matches one visible piece. Voxel style.
Working Three.js Starter (pattern-block-edge-matching-x0361):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Pattern Blocks</title>
<style>
:root {
--ink: #eef5ff;
--muted: #9aabc4;
--panel: rgba(12, 22, 39, 0.78);
--line: rgba(180, 207, 244, 0.16);
--coral: #ff748b;
--mint: #48dbc5;
--gold: #f6c85f;
--blue: #62b7ff;
--violet: #9181ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background:
radial-gradient(circle at 52% 43%, rgba(50, 89, 139, 0.19), transparent 35%),
radial-gradient(circle at 15% 85%, rgba(82, 54, 133, 0.14), transparent 32%),
linear-gradient(145deg, #09111f 0%, #0b1628 48%, #07101d 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body::before {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
opacity: 0.22;
background-image:
linear-gradient(rgba(255, 255, 255, 0.018) 1px, transparent 1px),
linear-gradient(90deg, rgba(255, 255, 255, 0.018) 1px, transparent 1px);
background-size: 42px 42px;
mask-image: linear-gradient(to bottom, transparent, black 28%, black 76%, transparent);
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
pointer-events: auto;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.intro {
position: absolute;
top: 28px;
left: 28px;
width: min(390px, calc(100vw - 56px));
padding: 22px 24px 20px;
border-radius: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--mint);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--mint);
box-shadow: 0 0 18px rgba(72, 219, 197, 0.75);
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 48px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.intro p {
max-width: 32ch;
margin: 13px 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.badges {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 27px;
padding: 5px 9px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 999px;
color: #c8d6e8;
background: rgba(255, 255, 255, 0.045);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge strong {
color: white;
font-size: 11px;
}
.actions {
position: absolute;
top: 28px;
right: 28px;
display: flex;
gap: 9px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid var(--line);
color: var(--ink);
background: var(--panel);
border-radius: 11px;
padding: 10px 13px;
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
cursor: pointer;
backdrop-filter: blur(12px);
transition: transform 160ms ease, border-color 160ms ease, background 160ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.3);
background: rgba(24, 39, 61, 0.9);
}
button:active {
transform: translateY(0);
}
.legend {
position: absolute;
left: 28px;
bottom: 28px;
width: min(355px, calc(100vw - 56px));
padding: 16px;
border-radius: 16px;
}
.legend-title {
margin-bottom: 11px;
color: #8498b5;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
color: #c8d6e8;
font-size: 11px;
}
.shape-icon {
width: 21px;
height: 21px;
flex: 0 0 21px;
filter: drop-shadow(0 4px 7px rgba(0, 0, 0, 0.3));
}
.shape-icon.hex {
background: var(--mint);
clip-path: polygon(25% 4%, 75% 4%, 100% 50%, 75% 96%, 25% 96%, 0 50%);
}
.shape-icon.triangle {
background: var(--gold);
clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.shape-icon.rhombus {
background: var(--coral);
clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}
.inspector {
display: flex;
align-items: center;
gap: 10px;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.inspector-dot {
width: 9px;
height: 9px;
flex: 0 0 9px;
border-radius: 3px;
background: linear-gradient(135deg, var(--mint), var(--blue));
box-shadow: 0 0 14px rgba(98, 183, 255, 0.42);
}
.inspector small {
display: block;
margin-bottom: 2px;
color: #758aa8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.inspector strong {
display: block;
color: #e9f2ff;
font-size: 11px;
font-weight: 650;
}
.hint {
position: absolute;
right: 28px;
bottom: 28px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 999px;
color: #879ab7;
background: rgba(9, 18, 31, 0.58);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.04em;
backdrop-filter: blur(12px);
}
.scene-label {
position: absolute;
z-index: 3;
pointer-events: none;
opacity: 0;
transform: translate(-50%, -50%);
transition: opacity 180ms ease;
white-space: nowrap;
}
.scene-label.visible {
opacity: 1;
}
.scene-label span {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(255, 116, 139, 0.42);
border-radius: 999px;
color: #ffdce3;
background: rgba(43, 18, 30, 0.82);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.28);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.scene-label span::before {
content: "";
width: 6px;
height: 6px;
border-radius: 2px;
background: var(--coral);
box-shadow: 0 0 12px rgba(255, 116, 139, 0.8);
}
.scene-label.piece span {
color: #e9e5ff;
border-color: rgba(145, 129, 255, 0.48);
background: rgba(28, 24, 55, 0.84);
}
.scene-label.piece span::before {
background: var(--violet);
box-shadow: 0 0 12px rgba(145, 129, 255, 0.8);
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #09111f;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader {
display: grid;
gap: 12px;
justify-items: center;
color: #a9bad2;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.loader-shape {
width: 42px;
height: 42px;
background: var(--mint);
clip-path: polygon(25% 4%, 75% 4%, 100% 50%, 75% 96%, 25% 96%, 0 50%);
animation: loader 1.1s ease-in-out infinite;
}
@keyframes loader {
0%, 100% { transform: translateY(0) rotate(0deg); opacity: 0.55; }
50% { transform: translateY(-7px) rotate(30deg); opacity: 1; }
}
@media (max-width: 760px) {
.intro {
top: 14px;
left: 14px;
width: calc(100vw - 92px);
padding: 16px 17px;
border-radius: 15px;
}
.intro p {
display: none;
}
.eyebrow {
margin-bottom: 7px;
font-size: 9px;
}
h1 {
font-size: 29px;
}
.badges {
margin-top: 12px;
}
.badge {
min-height: 23px;
padding: 4px 7px;
font-size: 8px;
}
.actions {
top: 14px;
right: 14px;
}
button {
padding: 10px;
font-size: 10px;
}
.legend {
left: 14px;
bottom: 14px;
width: calc(100vw - 28px);
padding: 12px;
border-radius: 14px;
}
.legend-title {
margin-bottom: 8px;
}
.legend-item {
gap: 6px;
font-size: 10px;
}
.inspector {
margin-top: 10px;
padding-top: 10px;
}
.hint,
.scene-label {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D arrangement of geometric pattern blocks"></canvas>
<div class="hud">
<section class="intro panel">
<div class="eyebrow">Tessera / Pattern Study 01</div>
<h1>Pattern<br />Blocks</h1>
<p>
A larger hexagon assembled from equal-edge hexagons, triangles, and
rhombi. The outlined notch is exactly matched by the loose coral piece.
</p>
<div class="badges">
<span class="badge"><strong>1u</strong> edge</span>
<span class="badge"><strong>2u</strong> outer hex</span>
<span class="badge"><strong>24</strong> triangle units</span>
</div>
</section>
<div class="actions">
<button id="resetView" type="button" title="Reset camera view">Reset view</button>
</div>
<section class="legend panel">
<div class="legend-title">Compatible unit pieces</div>
<div class="legend-row">
<div class="legend-item">
<span class="shape-icon hex"></span>
<span>Hex · 6△</span>
</div>
<div class="legend-item">
<span class="shape-icon triangle"></span>
<span>Tri · 1△</span>
</div>
<div class="legend-item">
<span class="shape-icon rhombus"></span>
<span>Rhomb · 2△</span>
</div>
</div>
<div class="inspector" aria-live="polite">
<span id="inspectorDot" class="inspector-dot"></span>
<div>
<small>Block inspector</small>
<strong id="inspectorText">All three shapes share compatible edges</strong>
</div>
</div>
</section>
<div class="hint">Drag to rotate · Scroll to zoom · Tap a block</div>
<div id="gapLabel" class="scene-label"><span>Rhombus gap · 2△</span></div>
<div id="pieceLabel" class="scene-label piece"><span>Matching piece · 1u sides</span></div>
</div>
<div id="loading">
<div class="loader">
<div class="loader-shape"></div>
<div>Building pattern</div>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const resetButton = document.querySelector("#resetView");
const inspectorText = document.querySelector("#inspectorText");
const inspectorDot = document.querySelector("#inspectorDot");
const gapLabel = document.querySelector("#gapLabel");
const pieceLabel = document.querySelector("#pieceLabel");
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.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-5, 5, 4, -4, 0.1, 100);
const cameraState = {
azimuth: -0.94,
tilt: 0.69,
zoom: 1,
target: new THREE.Vector3(0.65, 0.02, 0)
};
const hemi = new THREE.HemisphereLight(0xccecff, 0x182238, 2.25);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.3);
keyLight.position.set(-4, -5, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x70dfff, 1.35);
rimLight.position.set(7, 5, 4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff8a9e, 18, 15, 2);
warmLight.position.set(4.2, -0.4, 3.5);
scene.add(warmLight);
// A transparent shadow catcher and a restrained world-space grid.
const shadowPlane = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.ShadowMaterial({ color: 0x020711, opacity: 0.28 })
);
shadowPlane.position.z = -0.43;
shadowPlane.receiveShadow = true;
scene.add(shadowPlane);
const grid = new THREE.GridHelper(24, 48, 0x35506f, 0x263d58);
grid.rotation.x = Math.PI / 2;
grid.position.z = -0.405;
const gridMaterial = Array.isArray(grid.material) ? grid.material[0] : grid.material;
gridMaterial.transparent = true;
gridMaterial.opacity = 0.2;
gridMaterial.depthWrite = false;
scene.add(grid);
const COLORS = {
hex: 0x48dbc5,
triangleA: 0xf6c85f,
triangleB: 0x62b7ff,
triangleC: 0xff7891,
rhombus: 0x9181ff,
matching: 0xff748b,
base: 0x0e1c30,
baseSide: 0x07111f,
floor: 0x14263c
};
const pickables = [];
const edgeColor = 0x07121f;
function darkened(hex, amount = 0.5) {
return new THREE.Color(hex).multiplyScalar(amount);
}
function polygonArea(points) {
let area = 0;
for (let i = 0; i < points.length; i++) {
const a = points[i];
const b = points[(i + 1) % points.length];
area += a.x * b.y - b.x * a.y;
}
return area * 0.5;
}
function makePrism(
inputPoints,
depth,
topColor,
sideColor = darkened(topColor, 0.5),
options = {}
) {
const points = inputPoints.map((p) => new THREE.Vector2(p.x, p.y));
if (polygonArea(points) < 0) points.reverse();
const shape = new THREE.Shape();
shape.moveTo(points[0].x, points[0].y);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i].x, points[i].y);
}
shape.closePath();
const bevelSize = options.bevelSize ?? 0.018;
const bevelThickness = options.bevelThickness ?? 0.025;
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
curveSegments: 1,
bevelEnabled: true,
bevelSegments: 1,
bevelSize,
bevelThickness,
bevelOffset: 0
});
geometry.computeVertexNormals();
const topMaterial = new THREE.MeshStandardMaterial({
color: topColor,
roughness: 0.73,
metalness: 0.035,
flatShading: true,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
const sideMaterial = new THREE.MeshStandardMaterial({
color: sideColor,
roughness: 0.88,
metalness: 0.015,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, [topMaterial, sideMaterial]);
mesh.castShadow = true;
mesh.receiveShadow = true;
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 26),
new THREE.LineBasicMaterial({
color: edgeColor,
transparent: true,
opacity: 0.34
})
);
outline.renderOrder = 4;
mesh.add(outline);
mesh.userData.edge = outline;
mesh.userData.topMaterial = topMaterial;
return mesh;
}
function regularPolygon(radius, sides = 6, rotation = 0) {
const points = [];
for (let i = 0; i < sides; i++) {
const angle = rotation + (i / sides) * Math.PI * 2;
points.push(
new THREE.Vector2(
Math.cos(angle) * radius,
Math.sin(angle) * radius
)
);
}
return points;
}
function centroid(points) {
const sum = points.reduce(
(acc, p) => acc.add(new THREE.Vector2(p.x, p.y)),
new THREE.Vector2()
);
return sum.multiplyScalar(1 / points.length);
}
function registerPiece(mesh, type, color, areaText) {
mesh.userData.type = type;
mesh.userData.color = color;
mesh.userData.areaText = areaText;
pickables.push(mesh);
scene.add(mesh);
return mesh;
}
// Exact triangular-grid construction:
// the outer hexagon has side 2u; every puzzle piece has side 1u.
const outerVertices = regularPolygon(2, 6, 0);
const sectorMids = outerVertices.map((a, i) => {
const b = outerVertices[(i + 1) % 6];
return new THREE.Vector2((a.x + b.x) * 0.5, (a.y + b.y) * 0.5);
});
// Raised puzzle tray.
const base = makePrism(
regularPolygon(2.07, 6, 0),
0.18,
COLORS.base,
COLORS.baseSide,
{ bevelSize: 0.035, bevelThickness: 0.035 }
);
base.position.z = -0.36;
scene.add(base);
const floor = makePrism(
regularPolygon(2.0, 6, 0),
0.025,
COLORS.floor,
darkened(COLORS.floor, 0.6),
{ bevelSize: 0.008, bevelThickness: 0.008 }
);
floor.position.z = -0.14;
floor.receiveShadow = true;
scene.add(floor);
// One central regular hexagonal block.
const centerHex = makePrism(
regularPolygon(1, 6, 0),
0.25,
COLORS.hex,
darkened(COLORS.hex, 0.46)
);
centerHex.position.z = -0.105;
registerPiece(
centerHex,
"Hexagon",
COLORS.hex,
"HEXAGON · 6 triangular units · side 1u"
);
// Six equilateral triangle blocks.
const triangleColors = [
COLORS.triangleA,
COLORS.triangleB,
COLORS.triangleC,
COLORS.triangleA,
COLORS.triangleB,
COLORS.triangleC
];
for (let i = 0; i < 6; i++) {
const a = outerVertices[i];
const b = outerVertices[(i + 1) % 6];
const p = a.clone().multiplyScalar(0.5);
const q = b.clone().multiplyScalar(0.5);
const m = sectorMids[i];
const triangle = makePrism(
[p, q, m],
0.25,
triangleColors[i],
darkened(triangleColors[i], 0.48)
);
triangle.position.z = -0.105;
registerPiece(
triangle,
"Triangle",
triangleColors[i],
"TRIANGLE · 1 triangular unit · side 1u"
);
}
// Six rhombus blocks are formed from pairs of adjacent unit triangles.
// The rhombus at vertex zero is intentionally omitted to form the gap.
const gapIndex = 0;
const rhombusPoints = [];
for (let i = 0; i < 6; i++) {
const a = outerVertices[i];
const previousMid = sectorMids[(i + 5) % 6];
const currentMid = sectorMids[i];
const inner = a.clone().multiplyScalar(0.5);
const points = [previousMid, a, currentMid, inner];
rhombusPoints[i] = points;
if (i === gapIndex) continue;
const rhombus = makePrism(
points,
0.25,
COLORS.rhombus,
darkened(COLORS.rhombus, 0.47)
);
rhombus.position.z = -0.105;
registerPiece(
rhombus,
"Rhombus",
COLORS.rhombus,
"RHOMBUS · 2 triangular units · side 1u"
);
}
// Animated outline around the missing rhombus.
const gapPoints = rhombusPoints[gapIndex];
const gapLineMaterial = new THREE.LineDashedMaterial({
color: COLORS.matching,
dashSize: 0.11,
gapSize: 0.065,
transparent: true,
opacity: 0.92,
depthTest: true
});
const gapLinePoints = gapPoints.map((p) => new THREE.Vector3(p.x, p.y, -0.075));
gapLinePoints.push(gapLinePoints[0].clone());
const gapLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(gapLinePoints),
gapLineMaterial
);
gapLine.computeLineDistances();
scene.add(gapLine);
// Short vertical markers make the recess read as a missing block.
const guidePositions = [];
for (const p of gapPoints) {
guidePositions.push(
p.x, p.y, -0.09,
p.x, p.y, 0.17
);
}
const gapGuides = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute(
"position",
new THREE.Float32BufferAttribute(guidePositions, 3)
),
new THREE.LineBasicMaterial({
color: COLORS.matching,
transparent: true,
opacity: 0.38
})
);
scene.add(gapGuides);
// Separate inspection pad and the exact loose rhombus that fills the gap.
const looseOffset = new THREE.Vector2(2.1, 0.15);
const loosePoints = gapPoints.map((p) => p.clone().add(looseOffset));
const looseCenter = centroid(loosePoints);
const pad = new THREE.Mesh(
new THREE.CircleGeometry(1.22, 64),
new THREE.MeshStandardMaterial({
color: 0x101d31,
roughness: 0.9,
metalness: 0,
transparent: true,
opacity: 0.82
})
);
pad.position.set(looseCenter.x, looseCenter.y, -0.13);
pad.receiveShadow = true;
scene.add(pad);
const padRing = new THREE.Mesh(
new THREE.RingGeometry(1.16, 1.19, 64),
new THREE.MeshBasicMaterial({
color: 0x586985,
transparent: true,
opacity: 0.35,
side: THREE.DoubleSide
})
);
padRing.position.set(looseCenter.x, looseCenter.y, -0.118);
scene.add(padRing);
const looseRhombus = makePrism(
loosePoints,
0.25,
COLORS.matching,
darkened(COLORS.matching, 0.46),
{
emissive: COLORS.matching,
emissiveIntensity: 0.045
}
);
looseRhombus.position.z = -0.105;
registerPiece(
looseRhombus,
"Rhombus",
COLORS.matching,
"MATCHING RHOMBUS · 2 triangular units · side 1u"
);
// A subtle dashed arc visually links the gap to its matching piece.
const matchCurve = new THREE.QuadraticBezierCurve3(
new THREE.Vector3(1.5, 0, 0.23),
new THREE.Vector3(2.85, 0.82, 0.72),
new THREE.Vector3(looseCenter.x, looseCenter.y, 0.24)
);
const matchLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(matchCurve.getPoints(48)),
new THREE.LineDashedMaterial({
color: 0xb38cff,
dashSize: 0.08,
gapSize: 0.07,
transparent: true,
opacity: 0.4
})
);
matchLine.computeLineDistances();
scene.add(matchLine);
const gapAnchor = new THREE.Vector3(
centroid(gapPoints).x,
centroid(gapPoints).y,
0.23
);
const pieceAnchor = new THREE.Vector3(
looseCenter.x,
looseCenter.y,
0.27
);
// Camera controls are implemented directly, without external addons.
function updateCamera() {
const radius = 12;
const s = Math.sin(cameraState.tilt);
const c = Math.cos(cameraState.tilt);
camera.position.set(
cameraState.target.x + radius * s * Math.cos(cameraState.azimuth),
cameraState.target.y + radius * s * Math.sin(cameraState.azimuth),
cameraState.target.z + radius * c
);
camera.lookAt(cameraState.target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
const frustumHeight = Math.max(6.45, 7.6 / aspect);
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
updateCamera();
}
function resetView() {
cameraState.azimuth = -0.94;
cameraState.tilt = 0.69;
cameraState.zoom = 1;
cameraState.target.set(0.65, 0.02, 0);
updateCamera();
}
resetButton.addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetView();
});
let dragging = false;
let pointerId = null;
let previousX = 0;
let previousY = 0;
let movement = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
movement = 0;
canvas.setPointerCapture(pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (dragging && event.pointerId === pointerId) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
movement += Math.abs(dx) + Math.abs(dy);
cameraState.azimuth -= dx * 0.006;
cameraState.tilt = THREE.MathUtils.clamp(
cameraState.tilt + dy * 0.005,
0.36,
1.18
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
} else {
updateHover(event);
}
});
function endPointer(event) {
if (!dragging || event.pointerId !== pointerId) return;
dragging = false;
canvas.style.cursor = "default";
if (canvas.hasPointerCapture(pointerId)) {
canvas.releasePointerCapture(pointerId);
}
if (movement < 6) {
selectAt(event);
}
pointerId = null;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("pointerleave", () => {
if (!dragging) canvas.style.cursor = "default";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.7
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function updateRay(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;
raycaster.setFromCamera(pointer, camera);
}
function updateHover(event) {
updateRay(event);
const hit = raycaster.intersectObjects(pickables, false)[0];
canvas.style.cursor = hit ? "pointer" : "default";
}
function selectAt(event) {
updateRay(event);
const hit = raycaster.intersectObjects(pickables, false)[0];
for (const mesh of pickables) {
const selected = mesh === hit?.object;
mesh.userData.edge.material.color.setHex(selected ? 0xffffff : edgeColor);
mesh.userData.edge.material.opacity = selected ? 1 : 0.34;
}
if (!hit) {
inspectorText.textContent = "All three shapes share compatible edges";
inspectorDot.style.background =
"linear-gradient(135deg, #48dbc5, #62b7ff)";
return;
}
const mesh = hit.object;
inspectorText.textContent = mesh.userData.areaText;
inspectorDot.style.background = `#${mesh.userData.color.toString(16).padStart(6, "0")}`;
inspectorDot.style.boxShadow = `0 0 14px #${mesh.userData.color.toString(16)}88`;
}
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition, offsetX, offsetY) {
projected.copy(worldPosition).project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.classList.toggle(
"visible",
projected.z > -1 && projected.z < 1 && x > -100 && x < window.innerWidth + 100
);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
gapLineMaterial.opacity = 0.66 + Math.sin(elapsed * 3.1) * 0.25;
gapGuides.material.opacity = 0.25 + Math.sin(elapsed * 3.1) * 0.12;
looseRhombus.userData.topMaterial.emissiveIntensity =
0.045 + (Math.sin(elapsed * 3.1) * 0.5 + 0.5) * 0.085;
looseRhombus.position.z =
-0.105 + (Math.sin(elapsed * 2.15) * 0.5 + 0.5) * 0.025;
positionLabel(gapLabel, gapAnchor, 34, 33);
positionLabel(pieceLabel, pieceAnchor, 18, -38);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 350);
});
</script>
</body>
</html>
```