Prompt specification
Build or customize the Three.js 3D scene "Broken symmetry repair task" 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: Broken symmetry repair task
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/broken-symmetry-repair-task-x1738
Scene Description Prompt:
Broken symmetry repair task. Show a tiled motif with one displaced element and its mirrored reference; restore the element so reflection across the marked plane maps every component correctly. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (broken-symmetry-repair-task-x1738):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Broken Symmetry — Repair Lab</title>
<style>
:root {
--bg: #07131d;
--panel: rgba(10, 26, 39, 0.82);
--border: rgba(164, 220, 226, 0.17);
--text: #eef8f5;
--muted: #91abb4;
--mint: #63e6cf;
--gold: #ffd166;
--coral: #ff6b5f;
--danger: #ff786c;
--good: #7cf0bd;
}
* {
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;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.ui {
position: fixed;
z-index: 10;
pointer-events: none;
}
.title-card {
top: 24px;
left: 24px;
width: min(430px, calc(100vw - 48px));
padding: 20px 22px;
border: 1px solid var(--border);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(16, 40, 54, 0.9), rgba(7, 20, 31, 0.76));
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--mint);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 2px;
content: "";
background: var(--mint);
box-shadow: 0 0 12px rgba(99, 230, 207, 0.65);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 36px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.intro {
max-width: 360px;
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.status-card {
top: 24px;
right: 24px;
width: 270px;
padding: 17px 18px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(14px);
}
.status-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.status-label {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.status-value {
display: flex;
align-items: center;
gap: 8px;
margin-top: 7px;
font-size: 15px;
font-weight: 760;
letter-spacing: 0.025em;
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--danger);
box-shadow: 0 0 14px rgba(255, 120, 108, 0.8);
}
.status-card.good .status-dot {
background: var(--good);
box-shadow: 0 0 14px rgba(124, 240, 189, 0.8);
}
.status-card.good .status-value {
color: var(--good);
}
.status-card.busy .status-dot {
background: var(--gold);
box-shadow: 0 0 14px rgba(255, 209, 102, 0.8);
}
.meter {
height: 5px;
margin-top: 14px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
.meter-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--coral), var(--gold));
transition: width 180ms ease;
}
.status-card.good .meter-fill {
background: linear-gradient(90deg, var(--mint), var(--good));
}
.meter-caption {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
}
.controls {
right: 24px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
pointer-events: auto;
}
button {
min-height: 42px;
padding: 0 16px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 11px;
background: rgba(13, 34, 47, 0.9);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 720;
cursor: pointer;
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(12px);
transition:
transform 150ms ease,
border-color 150ms ease,
background 150ms ease;
}
button:hover {
transform: translateY(-2px);
border-color: rgba(99, 230, 207, 0.55);
background: rgba(20, 48, 61, 0.96);
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: rgba(99, 230, 207, 0.45);
background: var(--mint);
color: #06221e;
}
button.primary:hover {
background: #8af2df;
}
button.flash {
border-color: var(--good);
color: var(--good);
}
.help {
left: 24px;
bottom: 24px;
max-width: 410px;
padding: 12px 15px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(7, 20, 31, 0.68);
color: var(--muted);
font-size: 11px;
line-height: 1.55;
backdrop-filter: blur(10px);
}
.help strong {
color: var(--text);
font-weight: 700;
}
.world-label {
position: fixed;
z-index: 8;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.world-label::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 21px;
content: "";
background: currentColor;
opacity: 0.5;
}
.target-label {
padding: 6px 9px;
border: 1px solid rgba(255, 107, 95, 0.6);
border-radius: 7px;
background: rgba(60, 17, 19, 0.88);
color: #ff9b91;
box-shadow: 0 7px 22px rgba(0, 0, 0, 0.28);
}
.target-label.aligned {
border-color: rgba(124, 240, 189, 0.55);
background: rgba(8, 39, 32, 0.9);
color: var(--good);
}
.reference-label {
padding: 6px 9px;
border: 1px solid rgba(255, 209, 102, 0.38);
border-radius: 7px;
background: rgba(47, 37, 13, 0.82);
color: var(--gold);
}
.plane-label {
padding: 5px 8px;
border: 1px solid rgba(99, 230, 207, 0.38);
border-radius: 7px;
background: rgba(6, 31, 35, 0.85);
color: var(--mint);
}
@media (max-width: 760px) {
.title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px 17px;
}
.intro {
display: none;
}
.status-card {
top: 139px;
right: 12px;
width: 225px;
}
.controls {
right: 12px;
bottom: 12px;
left: 12px;
justify-content: stretch;
}
.controls button {
flex: 1 1 auto;
padding: 0 11px;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<section class="ui title-card">
<div class="eyebrow">Symmetry Lab / Task 03</div>
<h1>Broken symmetry repair</h1>
<p class="intro">
One coral component has been displaced from its mirrored home.
Restore it so reflection across the marked plane maps every part correctly.
</p>
</section>
<aside class="ui status-card broken" id="statusCard">
<div class="status-heading">
<span class="status-label">Reflection status</span>
<span class="status-label">x = 0</span>
</div>
<div class="status-value">
<span class="status-dot"></span>
<span id="statusTitle">BROKEN</span>
</div>
<div class="meter">
<div class="meter-fill" id="meterFill"></div>
</div>
<div class="meter-caption">
<span>Component mapping</span>
<span id="meterText">0 / 0</span>
</div>
</aside>
<div class="ui controls">
<button id="resetButton" title="Shortcut: B">Reset break</button>
<button id="verifyButton" title="Shortcut: V">Verify reflection</button>
<button class="primary" id="repairButton" title="Shortcut: R">Repair alignment</button>
</div>
<div class="ui help">
<strong>Drag coral piece</strong> to move it · <strong>Drag background</strong> to orbit ·
<strong>Right-drag</strong> to pan · <strong>Scroll</strong> to zoom ·
<strong>R / B / V</strong> for repair, reset and verify
</div>
<div class="world-label target-label" id="targetLabel">Displaced / drag me</div>
<div class="world-label reference-label" id="referenceLabel">Mirrored reference</div>
<div class="world-label plane-label" id="planeLabel">Reflection plane</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
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;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131d);
scene.fog = new THREE.Fog(0x07131d, 24, 48);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Custom camera controls. No examples/addons modules are used.
const cameraState = {
target: new THREE.Vector3(0, 0, 0.15),
radius: 18.4,
theta: 0.41,
phi: 1.30
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xb9edff, 0x10232b, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff3d8, 3.4);
keyLight.position.set(-5, 9, 11);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x49d9d0, 22, 30, 2);
rimLight.position.set(8, 3, 6);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffad66, 16, 24, 2);
warmLight.position.set(-7, -1, 5);
scene.add(warmLight);
function standardMaterial(color, roughness = 0.56, metalness = 0.06) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
const materials = {
board: standardMaterial(0x102735, 0.72, 0.08),
frame: standardMaterial(0x1d4653, 0.48, 0.18),
plinth: standardMaterial(0x091a25, 0.7, 0.12),
panelA: standardMaterial(0x173643, 0.75, 0.04),
panelB: standardMaterial(0x14313e, 0.75, 0.04),
rivet: standardMaterial(0x6b9ca2, 0.42, 0.34),
teal: standardMaterial(0x39cdbd, 0.48, 0.08),
blue: standardMaterial(0x4e9fcf, 0.5, 0.08),
cream: standardMaterial(0xf6edda, 0.58, 0.02),
footTeal: standardMaterial(0x247f7c, 0.62, 0.05),
footBlue: standardMaterial(0x326c96, 0.62, 0.05),
flagTeal: standardMaterial(0x9af4dc, 0.46, 0.05),
flagBlue: standardMaterial(0xa7ddfa, 0.46, 0.05),
coral: standardMaterial(0xff665b, 0.42, 0.08),
coralDark: standardMaterial(0xc84543, 0.58, 0.04),
yellow: standardMaterial(0xffd166, 0.43, 0.05),
paleYellow: standardMaterial(0xffefb1, 0.46, 0.03),
ground: standardMaterial(0x07131b, 0.9, 0)
};
// Board and floor.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -4.55;
ground.receiveShadow = true;
scene.add(ground);
const board = new THREE.Mesh(
new THREE.BoxGeometry(13.1, 8.35, 0.48),
materials.board
);
board.position.z = -0.36;
board.castShadow = true;
board.receiveShadow = true;
scene.add(board);
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(13.55, 0.42, 1.25),
materials.plinth
);
plinth.position.set(0, -4.22, -0.3);
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
const framePieces = [
[13.35, 0.16, 0.2, 0, 4.12, -0.02],
[13.35, 0.16, 0.2, 0, -4.12, -0.02],
[0.16, 8.22, 0.2, -6.6, 0, -0.02],
[0.16, 8.22, 0.2, 6.6, 0, -0.02]
];
for (const [w, h, d, x, y, z] of framePieces) {
const piece = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
materials.frame
);
piece.position.set(x, y, z);
piece.castShadow = true;
scene.add(piece);
}
const columns = [-4.8, -2.4, 0, 2.4, 4.8];
const rows = [2.55, 0.85, -0.85, -2.55];
// Symmetric tile panels.
for (let columnIndex = 0; columnIndex < columns.length; columnIndex++) {
const x = columns[columnIndex];
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
const y = rows[rowIndex];
const panel = new THREE.Mesh(
new THREE.BoxGeometry(2.18, 1.48, 0.12),
rowIndex % 2 === 0 ? materials.panelA : materials.panelB
);
panel.position.set(x, y, -0.055);
panel.receiveShadow = true;
panel.castShadow = true;
scene.add(panel);
// Matching markers on both sides keep every tile itself symmetric.
for (const side of [-1, 1]) {
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.075, 0.025),
materials.rivet
);
marker.position.set(x + side * 0.94, y + 0.61, 0.018);
scene.add(marker);
}
}
}
const sharedStemGeometry = new THREE.BoxGeometry(0.22, 0.68, 0.28);
const sharedCapGeometry = new THREE.SphereGeometry(0.24, 24, 16);
const sharedFootGeometry = new THREE.BoxGeometry(0.52, 0.15, 0.32);
const sharedFlagGeometry = new THREE.BoxGeometry(0.105, 0.26, 0.32);
/**
* Each half contains four independently visible solid components.
* The small flag is intentionally asymmetric so every child part must
* be mirrored, not merely the overall silhouette.
*/
function makeHalf(handedness, palette) {
const group = new THREE.Group();
const stem = new THREE.Mesh(
sharedStemGeometry,
palette.stem
);
stem.position.set(0, -0.06, 0);
stem.castShadow = true;
stem.receiveShadow = true;
group.add(stem);
const cap = new THREE.Mesh(
sharedCapGeometry,
palette.cap
);
cap.position.set(0, 0.43, 0.01);
cap.scale.set(0.92, 1.16, 0.86);
cap.castShadow = true;
cap.receiveShadow = true;
group.add(cap);
const foot = new THREE.Mesh(
sharedFootGeometry,
palette.foot
);
foot.position.set(0, -0.47, 0);
foot.castShadow = true;
foot.receiveShadow = true;
group.add(foot);
const flag = new THREE.Mesh(
sharedFlagGeometry,
palette.flag
);
flag.position.set(handedness * 0.13, -0.045, 0.01);
flag.castShadow = true;
flag.receiveShadow = true;
group.add(flag);
return group;
}
const normalUpperPalette = {
stem: materials.teal,
cap: materials.cream,
foot: materials.footTeal,
flag: materials.flagTeal
};
const normalLowerPalette = {
stem: materials.blue,
cap: materials.cream,
foot: materials.footBlue,
flag: materials.flagBlue
};
const accentPalette = {
stem: materials.coral,
cap: materials.yellow,
foot: materials.coralDark,
flag: materials.paleYellow
};
const symmetryPairs = [];
let targetGroup = null;
let referenceGroup = null;
// Construct only the x >= 0 source halves, then create their true
// reflections. This guarantees exact left/right component placement.
const sourceColumns = [0, 2.4, 4.8];
for (const column of sourceColumns) {
for (const row of rows) {
const isAccent = column === 2.4 && Math.abs(row - 0.85) < 0.001;
const palette = isAccent
? accentPalette
: row > 0
? normalUpperPalette
: normalLowerPalette;
const source = makeHalf(1, palette);
source.position.set(column + 0.38, row, 0.34);
scene.add(source);
const reflected = makeHalf(-1, palette);
if (column === 0) {
reflected.position.set(-0.38, row, 0.34);
} else {
reflected.position.set(-column - 0.38, row, 0.34);
}
scene.add(reflected);
symmetryPairs.push({
source,
reflected,
sourceGroup: column,
row
});
if (isAccent) {
targetGroup = source;
referenceGroup = reflected;
}
}
}
const homePosition = targetGroup.position.clone();
const homeQuaternion = new THREE.Quaternion();
const brokenPosition = new THREE.Vector3(3.64, 1.48, 0.74);
const brokenQuaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(0, 0, 0.42)
);
targetGroup.position.copy(brokenPosition);
targetGroup.quaternion.copy(brokenQuaternion);
// Ghost of the target's required home position.
const homeGhost = targetGroup.clone(true);
homeGhost.position.copy(homePosition);
homeGhost.quaternion.copy(homeQuaternion);
const ghostMaterial = new THREE.MeshBasicMaterial({
color: 0x8fffea,
transparent: true,
opacity: 0.24,
wireframe: true,
depthWrite: false
});
homeGhost.traverse((object) => {
if (object.isMesh) {
object.material = ghostMaterial;
object.castShadow = false;
object.receiveShadow = false;
object.renderOrder = 4;
}
});
scene.add(homeGhost);
// Selection ring is a visual overlay, not part of the motif.
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(0.52, 0.025, 8, 48),
new THREE.MeshBasicMaterial({
color: 0xff786c,
transparent: true,
opacity: 0.85,
depthWrite: false
})
);
selectionRing.renderOrder = 6;
scene.add(selectionRing);
// The marked reflection plane is x = 0.
const planeMaterial = new THREE.MeshBasicMaterial({
color: 0x5ce4d1,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
depthWrite: false
});
const reflectionPlane = new THREE.Mesh(
new THREE.PlaneGeometry(1.45, 8.18),
planeMaterial
);
reflectionPlane.rotation.y = Math.PI / 2;
reflectionPlane.position.set(0, 0, 0.18);
reflectionPlane.renderOrder = 2;
scene.add(reflectionPlane);
const planeBorderPoints = [
new THREE.Vector3(0, -4.08, -0.72),
new THREE.Vector3(0, -4.08, 0.72),
new THREE.Vector3(0, 4.08, 0.72),
new THREE.Vector3(0, 4.08, -0.72)
];
const planeBorder = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(planeBorderPoints),
new THREE.LineBasicMaterial({
color: 0x67e7d4,
transparent: true,
opacity: 0.56
})
);
scene.add(planeBorder);
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -3.95, 0.58),
new THREE.Vector3(0, 3.95, 0.58)
]);
const axisLine = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0xffd166,
dashSize: 0.14,
gapSize: 0.11,
transparent: true,
opacity: 0.95
})
);
axisLine.computeLineDistances();
scene.add(axisLine);
const arrowGeometry = new THREE.ConeGeometry(0.09, 0.23, 3);
for (const [y, rotation] of [[-2.85, Math.PI], [2.85, 0]]) {
const arrow = new THREE.Mesh(
arrowGeometry,
new THREE.MeshBasicMaterial({ color: 0xffd166 })
);
arrow.position.set(0, y, 0.61);
arrow.rotation.z = rotation;
scene.add(arrow);
}
const reflectionLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]),
new THREE.LineDashedMaterial({
color: 0xffd166,
dashSize: 0.18,
gapSize: 0.1,
transparent: true,
opacity: 0.78
})
);
scene.add(reflectionLine);
const displacementLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]),
new THREE.LineDashedMaterial({
color: 0xff786c,
dashSize: 0.1,
gapSize: 0.08,
transparent: true,
opacity: 0.9
})
);
scene.add(displacementLine);
const statusCard = document.getElementById("statusCard");
const statusTitle = document.getElementById("statusTitle");
const meterFill = document.getElementById("meterFill");
const meterText = document.getElementById("meterText");
const targetLabel = document.getElementById("targetLabel");
const referenceLabel = document.getElementById("referenceLabel");
const planeLabel = document.getElementById("planeLabel");
const repairButton = document.getElementById("repairButton");
const resetButton = document.getElementById("resetButton");
const verifyButton = document.getElementById("verifyButton");
let activeTween = null;
let planePulse = 0;
let forcedStatus = null;
function setLine(line, start, end) {
const position = line.geometry.attributes.position;
position.setXYZ(0, start.x, start.y, start.z);
position.setXYZ(1, end.x, end.y, end.z);
position.needsUpdate = true;
line.computeLineDistances();
}
function nearlyEqual(a, b, epsilon = 0.025) {
return Math.abs(a - b) <= epsilon;
}
/**
* Checks all solid child components. A component is valid only when:
* 1. its reflected world position is correct,
* 2. its mirrored group rotation is correct,
* 3. its material color matches its counterpart.
*/
function checkSymmetry() {
scene.updateMatrixWorld(true);
let matched = 0;
let total = 0;
for (const pair of symmetryPairs) {
const sourceChildren = pair.source.children;
const reflectedChildren = pair.reflected.children;
for (let i = 0; i < sourceChildren.length; i++) {
total++;
const sourceChild = sourceChildren[i];
const reflectedChild = reflectedChildren[i];
const sourceWorld = new THREE.Vector3();
const reflectedWorld = new THREE.Vector3();
sourceChild.getWorldPosition(sourceWorld);
reflectedChild.getWorldPosition(reflectedWorld);
const expectedX = -sourceWorld.x;
const positionOK =
nearlyEqual(reflectedWorld.x, expectedX) &&
nearlyEqual(reflectedWorld.y, sourceWorld.y) &&
nearlyEqual(reflectedWorld.z, sourceWorld.z);
const rotationOK = nearlyEqual(
pair.reflected.rotation.z,
-pair.source.rotation.z,
0.03
);
const colorOK =
sourceChild.material.color.getHex() ===
reflectedChild.material.color.getHex();
if (positionOK && rotationOK && colorOK) {
matched++;
}
}
}
return { matched, total };
}
function updateStatus() {
if (forcedStatus) {
statusTitle.textContent = forcedStatus;
return;
}
if (activeTween) {
statusCard.className = "ui status-card busy";
statusTitle.textContent = "ALIGNING";
return;
}
const result = checkSymmetry();
const complete = result.matched === result.total;
statusCard.className = `ui status-card ${complete ? "good" : "broken"}`;
statusTitle.textContent = complete ? "SYMMETRIC" : "BROKEN";
meterFill.style.width = `${(result.matched / result.total) * 100}%`;
meterText.textContent = `${result.matched} / ${result.total}`;
targetLabel.classList.toggle("aligned", complete);
targetLabel.textContent = complete ? "Aligned" : "Displaced / drag me";
reflectionLine.material.color.setHex(
complete ? 0x7cf0bd : 0xffd166
);
}
function beginTween(destinationPosition, destinationQuaternion, duration = 760) {
activeTween = {
startTime: performance.now(),
duration,
fromPosition: targetGroup.position.clone(),
fromQuaternion: targetGroup.quaternion.clone(),
toPosition: destinationPosition.clone(),
toQuaternion: destinationQuaternion.clone()
};
forcedStatus = null;
updateStatus();
}
function repairElement() {
beginTween(homePosition, homeQuaternion);
}
function resetBreak() {
beginTween(brokenPosition, brokenQuaternion);
}
function verifyReflection() {
const result = checkSymmetry();
planePulse = 1;
verifyButton.classList.add("flash");
forcedStatus = result.matched === result.total ? "VERIFIED" : "CHECK FAILED";
updateStatus();
setTimeout(() => {
verifyButton.classList.remove("flash");
forcedStatus = null;
updateStatus();
}, 1100);
}
repairButton.addEventListener("click", repairElement);
resetButton.addEventListener("click", resetBreak);
verifyButton.addEventListener("click", verifyReflection);
window.addEventListener("keydown", (event) => {
if (event.repeat) return;
const key = event.key.toLowerCase();
if (key === "r") repairElement();
if (key === "b") resetBreak();
if (key === "v") verifyReflection();
});
// Pointer interaction: drag target, orbit, and pan.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane();
const dragIntersection = new THREE.Vector3();
const dragOffset = new THREE.Vector3();
const targetMeshes = targetGroup.children.filter((child) => child.isMesh);
let pointerMode = null;
let previousX = 0;
let previousY = 0;
let activePointerId = null;
let dragMoved = false;
function updatePointer(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("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
updatePointer(event);
previousX = event.clientX;
previousY = event.clientY;
activePointerId = event.pointerId;
dragMoved = false;
if (event.button === 0 && !event.shiftKey) {
const hits = raycaster.intersectObjects(targetMeshes, false);
if (hits.length) {
pointerMode = "drag";
activeTween = null;
forcedStatus = null;
dragPlane.set(
new THREE.Vector3(0, 0, 1),
-targetGroup.position.z
);
if (
raycaster.ray.intersectPlane(
dragPlane,
dragIntersection
)
) {
dragOffset.copy(targetGroup.position).sub(dragIntersection);
}
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
updateStatus();
return;
}
}
pointerMode = event.button === 2 ? "pan" : "orbit";
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor =
pointerMode === "pan" ? "move" : "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (event.pointerId !== activePointerId || !pointerMode) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (pointerMode === "orbit") {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.58,
1.72
);
updateCamera();
} else if (pointerMode === "pan") {
const direction = new THREE.Vector3();
camera.getWorldDirection(direction);
const right = new THREE.Vector3()
.crossVectors(direction, camera.up)
.normalize();
const up = new THREE.Vector3()
.crossVectors(right, direction)
.normalize();
const scale = cameraState.radius * 0.0014;
const movement = new THREE.Vector3()
.addScaledVector(right, -dx * scale)
.addScaledVector(up, dy * scale);
cameraState.target.add(movement);
camera.position.add(movement);
camera.lookAt(cameraState.target);
} else if (pointerMode === "drag") {
updatePointer(event);
if (
raycaster.ray.intersectPlane(
dragPlane,
dragIntersection
)
) {
const next = dragIntersection.clone().add(dragOffset);
next.x = THREE.MathUtils.clamp(next.x, -5.4, 5.4);
next.y = THREE.MathUtils.clamp(next.y, -3.55, 3.55);
targetGroup.position.x = next.x;
targetGroup.position.y = next.y;
dragMoved = true;
}
}
});
function finishPointer(event) {
if (event.pointerId !== activePointerId) return;
if (pointerMode === "drag" && dragMoved) {
const distance = targetGroup.position.distanceTo(homePosition);
// A close drop snaps cleanly into the reflected home position.
if (distance < 0.42) {
repairElement();
} else {
updateStatus();
}
}
pointerMode = null;
activePointerId = null;
renderer.domElement.style.cursor = "default";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", finishPointer);
renderer.domElement.addEventListener("pointercancel", finishPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
10.5,
30
);
updateCamera();
},
{ passive: false }
);
function positionLabel(element, worldPosition, pixelOffsetY = 0) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight + pixelOffsetY}px`;
}
function updateLabels() {
positionLabel(
targetLabel,
targetGroup.position.clone().add(new THREE.Vector3(0, 0.72, 0.25)),
-15
);
positionLabel(
referenceLabel,
referenceGroup.position.clone().add(new THREE.Vector3(0, 0.72, 0.25)),
-15
);
positionLabel(
planeLabel,
new THREE.Vector3(0, 4.32, 0.62),
0
);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate(now) {
requestAnimationFrame(animate);
if (activeTween) {
const raw = (now - activeTween.startTime) / activeTween.duration;
const t = THREE.MathUtils.clamp(raw, 0, 1);
const eased =
t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
targetGroup.position.lerpVectors(
activeTween.fromPosition,
activeTween.toPosition,
eased
);
targetGroup.quaternion.slerpQuaternions(
activeTween.fromQuaternion,
activeTween.toQuaternion,
eased
);
if (t >= 1) {
activeTween = null;
updateStatus();
}
}
const time = clock.getElapsedTime();
selectionRing.position.set(
targetGroup.position.x,
targetGroup.position.y,
targetGroup.position.z + 0.29
);
selectionRing.rotation.z = targetGroup.rotation.z;
const ringScale = 1 + Math.sin(time * 3.2) * 0.035;
selectionRing.scale.setScalar(ringScale);
const displacementDistance = targetGroup.position.distanceTo(homePosition);
homeGhost.visible = displacementDistance > 0.06;
displacementLine.visible = displacementDistance > 0.06;
const referencePoint = referenceGroup.position.clone();
referencePoint.z = 0.75;
const homePoint = homePosition.clone();
homePoint.z = 0.75;
const targetPoint = targetGroup.position.clone();
targetPoint.z = 0.83;
setLine(reflectionLine, referencePoint, homePoint);
setLine(displacementLine, homePoint, targetPoint);
if (planePulse > 0) {
planePulse = Math.max(0, planePulse - 0.025);
planeMaterial.opacity = 0.075 + planePulse * 0.28;
planeBorder.material.opacity = 0.56 + planePulse * 0.44;
} else {
planeMaterial.opacity = 0.075;
planeBorder.material.opacity = 0.56;
}
updateLabels();
renderer.render(scene, camera);
}
updateStatus();
requestAnimationFrame(animate);
</script>
</body>
</html>
```