Prompt specification
Build or customize the Three.js 3D scene "Polarity disruption comparison model" 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: Polarity disruption comparison model
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/polarity-disruption-comparison-model-x0824
Scene Description Prompt:
Rotate one low-poly symbolic cell in an aligned sheet to reveal polarity-route mismatch as a teaching abstraction. Three.js from cdnjs, one HTML file.
Working Three.js Starter (polarity-disruption-comparison-model-x0824):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Polarity / Route — Teaching Abstraction</title>
<style>
:root {
--bg: #071115;
--panel: rgba(10, 25, 29, 0.78);
--line: rgba(194, 235, 223, 0.16);
--text: #e6f3ee;
--muted: #8da6a2;
--mint: #4de1c2;
--violet: #a392ff;
--amber: #ffc857;
--danger: #ff7055;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 73% 20%, rgba(43, 111, 104, 0.22), transparent 34%),
radial-gradient(circle at 18% 78%, rgba(88, 72, 139, 0.16), transparent 35%),
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%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.glass {
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 20px 70px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.intro {
position: absolute;
top: 28px;
left: 30px;
width: min(430px, calc(100vw - 60px));
padding: 21px 23px 22px;
border-radius: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
color: var(--mint);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--mint);
box-shadow: 0 0 16px rgba(77, 225, 194, 0.75);
transform: rotate(45deg);
}
h1 {
margin: 0;
font-size: clamp(28px, 3.2vw, 47px);
line-height: 0.98;
letter-spacing: -0.055em;
font-weight: 650;
}
.intro p {
margin: 14px 0 0;
max-width: 36em;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.intro p strong {
color: var(--amber);
font-weight: 700;
}
.status {
position: absolute;
top: 28px;
right: 30px;
min-width: 205px;
padding: 15px 17px;
border-radius: 16px;
transition: border-color 180ms ease, background 180ms ease;
}
.status-row {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 14px rgba(77, 225, 194, 0.8);
transition: 180ms ease;
}
.status strong {
display: block;
margin-top: 7px;
font-size: 21px;
letter-spacing: -0.035em;
font-weight: 700;
}
.status.mismatch {
border-color: rgba(255, 112, 85, 0.52);
background: rgba(40, 19, 18, 0.84);
}
.status.mismatch .status-dot {
background: var(--danger);
box-shadow: 0 0 17px rgba(255, 112, 85, 0.9);
}
.status.mismatch strong { color: #ff9a84; }
.legend {
position: absolute;
left: 30px;
bottom: 29px;
width: 215px;
padding: 15px 17px;
border-radius: 15px;
}
.legend-title {
margin-bottom: 11px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
margin-top: 8px;
font-size: 11px;
color: #b7cbc6;
}
.swatch {
width: 21px;
height: 4px;
border-radius: 4px;
background: var(--mint);
box-shadow: 0 0 10px rgba(77, 225, 194, 0.4);
}
.swatch.violet {
background: var(--violet);
box-shadow: 0 0 10px rgba(163, 146, 255, 0.4);
}
.swatch.danger {
background: var(--danger);
box-shadow: 0 0 10px rgba(255, 112, 85, 0.4);
}
.diagnosis {
position: absolute;
right: 30px;
bottom: 104px;
width: min(330px, calc(100vw - 60px));
padding: 18px 19px;
border-radius: 17px;
}
.diagnosis-index {
color: var(--amber);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.diagnosis h2 {
margin: 7px 0 8px;
font-size: 18px;
letter-spacing: -0.025em;
}
.diagnosis p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.diagnosis.mismatch {
border-color: rgba(255, 112, 85, 0.38);
}
.diagnosis.mismatch .diagnosis-index { color: var(--danger); }
.controls {
position: absolute;
right: 30px;
bottom: 29px;
display: flex;
align-items: center;
gap: 9px;
padding: 9px;
border-radius: 16px;
pointer-events: auto;
}
button {
border: 0;
border-radius: 11px;
padding: 12px 14px;
font: inherit;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.02em;
cursor: pointer;
transition: transform 140ms ease, filter 140ms ease, background 140ms ease;
}
button:hover { transform: translateY(-1px); filter: brightness(1.08); }
button:active { transform: translateY(1px); }
button:focus-visible { outline: 2px solid var(--mint); outline-offset: 2px; }
.rotate {
display: flex;
align-items: center;
gap: 9px;
color: #15211d;
background: var(--amber);
}
.rotate svg { width: 15px; height: 15px; }
.reset {
color: #c1d4ce;
background: rgba(255, 255, 255, 0.065);
}
.angle {
min-width: 52px;
padding: 0 6px;
color: var(--amber);
font-variant-numeric: tabular-nums;
font-size: 13px;
font-weight: 800;
text-align: center;
}
.hint {
position: absolute;
left: 50%;
bottom: 27px;
transform: translateX(-50%);
padding: 9px 13px;
border: 1px solid rgba(194, 235, 223, 0.11);
border-radius: 999px;
color: rgba(205, 224, 218, 0.7);
background: rgba(7, 17, 21, 0.56);
font-size: 10px;
letter-spacing: 0.025em;
white-space: nowrap;
}
@media (max-width: 900px) {
.intro {
top: 16px;
left: 16px;
width: calc(100vw - 32px);
}
.intro p { max-width: 75vw; }
.status {
top: auto;
right: 16px;
bottom: 82px;
min-width: 176px;
}
.diagnosis {
right: 16px;
bottom: 164px;
width: min(300px, calc(100vw - 32px));
}
.legend { display: none; }
.controls {
right: 16px;
bottom: 16px;
}
.hint {
left: 16px;
bottom: 82px;
transform: none;
max-width: calc(100vw - 220px);
white-space: normal;
}
}
@media (max-width: 560px) {
.intro { padding: 17px 18px; }
h1 { font-size: 30px; }
.intro p { font-size: 12px; }
.diagnosis { display: none; }
.hint { display: none; }
.status { bottom: 78px; }
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="hud">
<section class="intro glass">
<div class="eyebrow">Teaching model · Polarity 04</div>
<h1>Polarity meets<br />the route.</h1>
<p>
Rotate the amber cell <strong>C-07</strong>. Its keyed contacts will
separate from the fixed positive and negative traces, exposing a
polarity–route mismatch.
</p>
</section>
<section id="statusCard" class="status glass" aria-live="polite">
<div class="status-row">
<span class="status-dot"></span>
Route integrity
</div>
<strong id="statusText">ALIGNED</strong>
</section>
<section class="legend glass">
<div class="legend-title">Trace language</div>
<div class="legend-item"><span class="swatch"></span> Positive route</div>
<div class="legend-item"><span class="swatch violet"></span> Negative route</div>
<div class="legend-item"><span class="swatch danger"></span> Diagnostic mismatch</div>
</section>
<section id="diagnosisCard" class="diagnosis glass">
<div class="diagnosis-index">C-07 / Contact diagnosis</div>
<h2 id="diagnosisTitle">Contacts match</h2>
<p id="diagnosisText">
The + terminal meets the + route and the − terminal meets the − route.
</p>
</section>
<div class="controls glass">
<button id="rotateButton" class="rotate" aria-label="Rotate cell C-07 by 90 degrees">
<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M19.5 8.5A8 8 0 1 0 20 15" stroke="currentColor" stroke-width="2.2" stroke-linecap="round"/>
<path d="M15 5.5h5v5" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
Rotate 90°
</button>
<div id="angleValue" class="angle">0°</div>
<button id="resetButton" class="reset">Reset</button>
</div>
<div class="hint">
Drag C-07 to rotate · drag empty space to orbit · Shift-drag to pan · wheel to zoom
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, and scene
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x071115, 20, 36);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Camera controls are implemented directly here: no addons are used.
const orbit = {
target: new THREE.Vector3(0, 0.38, 0),
radius: 18.2,
phi: 0.97,
theta: 0.72
};
function updateCamera() {
const spherical = new THREE.Spherical(
orbit.radius,
THREE.MathUtils.clamp(orbit.phi, 0.36, 1.38),
orbit.theta
);
camera.position.setFromSpherical(spherical).add(orbit.target);
camera.lookAt(orbit.target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xc8f4ff, 0x101b1a, 2.25);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff0ce, 3.25);
key.position.set(7, 13, 8);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -12;
key.shadow.camera.right = 12;
key.shadow.camera.top = 12;
key.shadow.camera.bottom = -12;
key.shadow.camera.near = 1;
key.shadow.camera.far = 35;
key.shadow.bias = -0.0004;
scene.add(key);
const rim = new THREE.DirectionalLight(0x8f8dff, 1.25);
rim.position.set(-10, 7, -9);
scene.add(rim);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const palette = {
board: 0x10272a,
boardTop: 0x163135,
frame: 0x29464a,
body: 0xc6d3cc,
bodyDark: 0x809994,
positive: 0x4de1c2,
negative: 0xa392ff,
amber: 0xffc857,
danger: 0xff7055,
metal: 0x9aa9a5
};
function standard(color, roughness = 0.65, metalness = 0.08, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness,
flatShading: true,
...extra
});
}
const materials = {
board: standard(palette.board, 0.78, 0.12),
boardTop: standard(palette.boardTop, 0.72, 0.12),
frame: standard(palette.frame, 0.5, 0.35),
body: standard(palette.body, 0.56, 0.18),
bodyDark: standard(palette.bodyDark, 0.58, 0.2),
positive: standard(palette.positive, 0.4, 0.3, {
emissive: 0x0b6658,
emissiveIntensity: 0.55
}),
negative: standard(palette.negative, 0.43, 0.27, {
emissive: 0x3c2e79,
emissiveIntensity: 0.5
}),
amber: standard(palette.amber, 0.45, 0.22, {
emissive: 0x7a4b00,
emissiveIntensity: 0.45
}),
metal: standard(palette.metal, 0.35, 0.7),
darkMetal: standard(0x405654, 0.42, 0.65)
};
function mesh(geometry, material, parent = scene) {
const object = new THREE.Mesh(geometry, material);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function box(width, height, depth, material, parent = scene) {
return mesh(new THREE.BoxGeometry(width, height, depth), material, parent);
}
function makeLabel(text, color, scale = 1.65, filled = false) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (filled) {
ctx.fillStyle = "rgba(7, 17, 21, 0.88)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = color;
ctx.lineWidth = 3;
ctx.strokeRect(2, 2, canvas.width - 4, canvas.height - 4);
}
ctx.fillStyle = color;
ctx.font = "700 42px Arial, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(scale, scale * 0.25, 1);
sprite.renderOrder = 20;
return sprite;
}
// -------------------------------------------------------------------------
// Ground and aligned circuit sheet
// -------------------------------------------------------------------------
const floor = mesh(
new THREE.PlaneGeometry(70, 70),
standard(0x081417, 0.95, 0.02)
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.24;
floor.castShadow = false;
const board = new THREE.Group();
scene.add(board);
const slab = box(12.65, 0.36, 8.45, materials.board, board);
slab.position.y = 0;
const top = box(12.34, 0.045, 8.14, materials.boardTop, board);
top.position.y = 0.202;
// Thin raised frame.
const framePieces = [
[12.55, 0.08, 0.07, 0, 0.255, -4.2],
[12.55, 0.08, 0.07, 0, 0.255, 4.2],
[0.07, 0.08, 8.4, -6.28, 0.255, 0],
[0.07, 0.08, 8.4, 6.28, 0.255, 0]
];
for (const [w, h, d, x, y, z] of framePieces) {
const piece = box(w, h, d, materials.frame, board);
piece.position.set(x, y, z);
}
// Corner fasteners.
const screwGeometry = new THREE.CylinderGeometry(0.09, 0.09, 0.07, 6);
for (const x of [-6.05, 6.05]) {
for (const z of [-4.02, 4.02]) {
const screw = mesh(screwGeometry, materials.metal, board);
screw.position.set(x, 0.285, z);
}
}
const cols = [-3.8, -1.9, 0, 1.9, 3.8];
const rows = [-2.55, -0.85, 0.85, 2.55];
const cellY = 0.62;
const positiveBusX = 5.72;
const negativeBusX = -5.72;
const positiveBusMat = standard(palette.positive, 0.34, 0.42, {
emissive: 0x0c725e,
emissiveIntensity: 0.8
});
const negativeBusMat = standard(palette.negative, 0.36, 0.38, {
emissive: 0x49328a,
emissiveIntensity: 0.75
});
const positiveBus = box(0.11, 0.08, 7.78, positiveBusMat, board);
positiveBus.position.set(positiveBusX, 0.285, 0);
const negativeBus = box(0.11, 0.08, 7.78, negativeBusMat, board);
negativeBus.position.set(negativeBusX, 0.285, 0);
const positiveRowMaterials = [];
const negativeRowMaterials = [];
// A positive and negative row trace run through the corresponding sockets.
rows.forEach((z, rowIndex) => {
const posMat = standard(palette.positive, 0.42, 0.28, {
emissive: 0x0b6658,
emissiveIntensity: 0.5
});
const negMat = standard(palette.negative, 0.44, 0.28, {
emissive: 0x3c2e79,
emissiveIntensity: 0.5
});
positiveRowMaterials.push(posMat);
negativeRowMaterials.push(negMat);
const positiveLength = positiveBusX - (cols[0] + 0.55);
const positiveTrace = box(
positiveLength,
0.045,
0.075,
posMat,
board
);
positiveTrace.position.set(
(positiveBusX + cols[0] + 0.55) * 0.5,
0.255,
z + 0.075
);
const negativeLength = (cols[4] - 0.55) - negativeBusX;
const negativeTrace = box(
negativeLength,
0.045,
0.075,
negMat,
board
);
negativeTrace.position.set(
(negativeBusX + cols[4] - 0.55) * 0.5,
0.255,
z - 0.075
);
});
const positiveBusLabel = makeLabel("+ BUS", "#65f0d0", 1.55, true);
positiveBusLabel.position.set(positiveBusX, 1.42, 0);
scene.add(positiveBusLabel);
const negativeBusLabel = makeLabel("− BUS", "#b7aaff", 1.55, true);
negativeBusLabel.position.set(negativeBusX, 1.42, 0);
scene.add(negativeBusLabel);
// -------------------------------------------------------------------------
// Low-poly cells and keyed contact sockets
// -------------------------------------------------------------------------
const targetRow = 1;
const targetCol = 1;
const targetId = "C-07";
const targetPickMeshes = [];
function addPolaritySign(parent, positive) {
const signGroup = new THREE.Group();
parent.add(signGroup);
const material = positive
? new THREE.MeshBasicMaterial({ color: palette.positive })
: new THREE.MeshBasicMaterial({ color: palette.negative });
const horizontal = box(
positive ? 0.18 : 0.18,
0.025,
positive ? 0.045 : 0.045,
material,
signGroup
);
horizontal.position.set(positive ? 0.20 : -0.20, 0.974, 0);
if (positive) {
const vertical = box(0.045, 0.025, 0.18, material, signGroup);
vertical.position.set(0.20, 0.974, 0);
}
}
function createCell(colIndex, rowIndex, isTarget = false) {
const group = new THREE.Group();
group.position.set(cols[colIndex], 0, rows[rowIndex]);
scene.add(group);
const base = mesh(
new THREE.CylinderGeometry(0.49, 0.49, 0.06, 8),
isTarget ? materials.amber : materials.bodyDark,
group
);
base.position.y = 0.25;
const body = mesh(
new THREE.CylinderGeometry(0.34, 0.34, 0.74, 6),
materials.body,
group
);
body.rotation.z = Math.PI / 2;
body.position.y = cellY;
const band = mesh(
new THREE.CylinderGeometry(0.348, 0.348, 0.17, 6),
isTarget ? materials.amber : materials.bodyDark,
group
);
band.rotation.z = Math.PI / 2;
band.position.y = cellY;
const positiveCap = mesh(
new THREE.CylinderGeometry(0.205, 0.205, 0.16, 6),
materials.positive,
group
);
positiveCap.rotation.z = Math.PI / 2;
positiveCap.position.set(0.45, cellY, 0);
const negativeCap = mesh(
new THREE.CylinderGeometry(0.205, 0.205, 0.16, 6),
materials.negative,
group
);
negativeCap.rotation.z = Math.PI / 2;
negativeCap.position.set(-0.45, cellY, 0);
addPolaritySign(group, true);
addPolaritySign(group, false);
if (isTarget) {
body.userData.isTarget = true;
band.userData.isTarget = true;
positiveCap.userData.isTarget = true;
negativeCap.userData.isTarget = true;
targetPickMeshes.push(body, band, positiveCap, negativeCap);
}
// Fixed board sockets do not rotate with the cell.
const socketGeometry = new THREE.TorusGeometry(0.22, 0.043, 4, 12);
for (const side of [-1, 1]) {
const x = cols[colIndex] + side * 0.55;
const socket = mesh(
socketGeometry,
side > 0 ? materials.positive : materials.negative
);
socket.rotation.y = Math.PI / 2;
socket.position.set(x, cellY, rows[rowIndex]);
const stem = box(
0.075,
0.34,
0.13,
side > 0 ? materials.positive : materials.negative
);
stem.position.set(x, 0.445, rows[rowIndex]);
const pad = box(
0.17,
0.045,
0.25,
side > 0 ? materials.positive : materials.negative
);
pad.position.set(x, 0.25, rows[rowIndex]);
}
return group;
}
let targetCell;
rows.forEach((_, rowIndex) => {
cols.forEach((__, colIndex) => {
const cell = createCell(
colIndex,
rowIndex,
rowIndex === targetRow && colIndex === targetCol
);
if (rowIndex === targetRow && colIndex === targetCol) {
targetCell = cell;
}
});
});
// Selection ring around the one editable cell.
const selectionRing = mesh(
new THREE.TorusGeometry(0.64, 0.026, 4, 36),
new THREE.MeshBasicMaterial({
color: palette.amber,
transparent: true,
opacity: 0.9,
depthTest: false
})
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.position.set(
cols[targetCol],
0.293,
rows[targetRow]
);
selectionRing.renderOrder = 10;
const targetLabel = makeLabel(`${targetId} / ROTATE`, "#ffd576", 2.05, true);
targetLabel.position.set(cols[targetCol], 1.62, rows[targetRow]);
scene.add(targetLabel);
// -------------------------------------------------------------------------
// Mismatch diagnostics
// -------------------------------------------------------------------------
const diagnosticGroup = new THREE.Group();
scene.add(diagnosticGroup);
function makeCross(color) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthTest: false
});
for (const angle of [-Math.PI / 4, Math.PI / 4]) {
const bar = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.31, 0.048),
material
);
bar.rotation.x = angle;
group.add(bar);
}
group.renderOrder = 15;
scene.add(group);
return group;
}
const positiveCross = makeCross(palette.danger);
positiveCross.position.set(
cols[targetCol] + 0.575,
cellY,
rows[targetRow]
);
const negativeCross = makeCross(palette.amber);
negativeCross.position.set(
cols[targetCol] - 0.575,
cellY,
rows[targetRow]
);
const actualPositiveHalo = mesh(
new THREE.TorusGeometry(0.25, 0.025, 4, 18),
new THREE.MeshBasicMaterial({
color: palette.danger,
transparent: true,
opacity: 0.95,
depthTest: false
}),
targetCell
);
actualPositiveHalo.rotation.y = Math.PI / 2;
actualPositiveHalo.position.set(0.56, cellY, 0);
actualPositiveHalo.visible = false;
actualPositiveHalo.renderOrder = 14;
const actualNegativeHalo = mesh(
new THREE.TorusGeometry(0.25, 0.025, 4, 18),
new THREE.MeshBasicMaterial({
color: palette.amber,
transparent: true,
opacity: 0.95,
depthTest: false
}),
targetCell
);
actualNegativeHalo.rotation.y = Math.PI / 2;
actualNegativeHalo.position.set(-0.56, cellY, 0);
actualNegativeHalo.visible = false;
actualNegativeHalo.renderOrder = 14;
function disposeDiagnosticChildren() {
while (diagnosticGroup.children.length) {
const child = diagnosticGroup.children.pop();
child.geometry?.dispose();
child.material?.dispose();
}
}
function addDiagnosticArc(start, end, color) {
const lift = new THREE.Vector3(0, 0.55, 0);
const midpoint = start.clone().lerp(end, 0.5).add(lift);
const curve = new THREE.CatmullRomCurve3([
start.clone(),
start.clone().add(new THREE.Vector3(0, 0.22, 0)),
midpoint,
end.clone().add(new THREE.Vector3(0, 0.22, 0)),
end.clone()
]);
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 28, 0.023, 5, false),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.9,
depthTest: false
})
);
tube.renderOrder = 12;
diagnosticGroup.add(tube);
}
const HALF_PI = Math.PI / 2;
let currentRotation = 0;
let rotationGoal = 0;
let selectedStep = 0;
let lastAligned = null;
function isAligned() {
return Math.abs(Math.cos(currentRotation) - 1) < 0.035;
}
function rebuildDiagnostics() {
disposeDiagnosticChildren();
targetCell.updateMatrixWorld(true);
const aligned = isAligned();
actualPositiveHalo.visible = !aligned;
actualNegativeHalo.visible = !aligned;
positiveCross.visible = !aligned;
negativeCross.visible = !aligned;
if (aligned) return;
const actualPositive = targetCell.localToWorld(
new THREE.Vector3(0.56, cellY, 0)
);
const actualNegative = targetCell.localToWorld(
new THREE.Vector3(-0.56, cellY, 0)
);
const expectedPositive = new THREE.Vector3(
cols[targetCol] + 0.55,
cellY,
rows[targetRow]
);
const expectedNegative = new THREE.Vector3(
cols[targetCol] - 0.55,
cellY,
rows[targetRow]
);
addDiagnosticArc(expectedPositive, actualPositive, palette.danger);
addDiagnosticArc(expectedNegative, actualNegative, palette.amber);
}
const statusCard = document.getElementById("statusCard");
const statusText = document.getElementById("statusText");
const diagnosisCard = document.getElementById("diagnosisCard");
const diagnosisTitle = document.getElementById("diagnosisTitle");
const diagnosisText = document.getElementById("diagnosisText");
const angleValue = document.getElementById("angleValue");
function updateRouteMaterials(mismatch) {
const pos = positiveRowMaterials[targetRow];
const neg = negativeRowMaterials[targetRow];
pos.color.set(mismatch ? palette.danger : palette.positive);
pos.emissive.set(mismatch ? 0x7d2415 : 0x0b6658);
pos.emissiveIntensity = mismatch ? 0.9 : 0.5;
neg.color.set(mismatch ? 0xd7a94d : palette.negative);
neg.emissive.set(mismatch ? 0x6a4510 : 0x3c2e79);
neg.emissiveIntensity = mismatch ? 0.7 : 0.5;
}
function updateUIState() {
const aligned = isAligned();
const degrees = selectedStep * 90;
angleValue.textContent = `${degrees}°`;
if (aligned === lastAligned) return;
lastAligned = aligned;
statusCard.classList.toggle("mismatch", !aligned);
diagnosisCard.classList.toggle("mismatch", !aligned);
if (aligned) {
statusText.textContent = "ALIGNED";
diagnosisTitle.textContent = "Contacts match";
diagnosisText.textContent =
"The + terminal meets the + route and the − terminal meets the − route.";
updateRouteMaterials(false);
} else {
statusText.textContent = "MISMATCH";
diagnosisTitle.textContent = `${degrees}° out of key`;
diagnosisText.textContent =
"The + terminal no longer meets the positive trace. The orange arc shows the missing polarity-to-route correspondence.";
updateRouteMaterials(true);
}
rebuildDiagnostics();
}
// -------------------------------------------------------------------------
// Cell rotation: button, keyboard, and direct dragging
// -------------------------------------------------------------------------
const rotateButton = document.getElementById("rotateButton");
const resetButton = document.getElementById("resetButton");
function rotateTarget() {
selectedStep = (selectedStep + 1) % 4;
rotationGoal = selectedStep * HALF_PI;
}
rotateButton.addEventListener("click", rotateTarget);
resetButton.addEventListener("click", () => {
selectedStep = 0;
rotationGoal = 0;
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r" && event.target.tagName !== "BUTTON") {
rotateTarget();
}
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pointerHitsTarget(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);
return raycaster.intersectObjects(targetPickMeshes, false).length > 0;
}
let pointerMode = null;
let activePointerId = null;
let pointerStartX = 0;
let pointerStartY = 0;
let dragStartAngle = 0;
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", event => {
activePointerId = event.pointerId;
pointerStartX = event.clientX;
pointerStartY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
if (
event.button === 0 &&
!event.shiftKey &&
pointerHitsTarget(event)
) {
pointerMode = "rotate";
dragStartAngle = currentRotation;
renderer.domElement.style.cursor = "grabbing";
} else if (event.shiftKey || event.button === 1) {
pointerMode = "pan";
} else {
pointerMode = "orbit";
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointerMode || event.pointerId !== activePointerId) {
if (event.buttons === 0) {
renderer.domElement.style.cursor = pointerHitsTarget(event)
? "grab"
: "default";
}
return;
}
const dx = event.clientX - pointerStartX;
const dy = event.clientY - pointerStartY;
if (pointerMode === "rotate") {
currentRotation = dragStartAngle + dx * 0.012;
rotationGoal = currentRotation;
selectedStep =
((Math.round(currentRotation / HALF_PI) % 4) + 4) % 4;
} else if (pointerMode === "orbit") {
orbit.theta -= dx * 0.006;
orbit.phi -= dy * 0.005;
orbit.phi = THREE.MathUtils.clamp(orbit.phi, 0.36, 1.38);
} else if (pointerMode === "pan") {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
1
);
const scale = orbit.radius * 0.00135;
orbit.target.addScaledVector(right, -dx * scale);
orbit.target.addScaledVector(up, dy * scale);
}
updateCamera();
});
function endPointer(event) {
if (event.pointerId !== activePointerId) return;
if (pointerMode === "rotate") {
const nearest = Math.round(currentRotation / HALF_PI);
selectedStep = ((nearest % 4) + 4) % 4;
rotationGoal = selectedStep * HALF_PI;
}
pointerMode = null;
activePointerId = null;
renderer.domElement.style.cursor = "default";
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
10,
29
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Animation and resize
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let lastDiagnosticAngle = -1;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!pointerMode) {
const difference = rotationGoal - currentRotation;
currentRotation += difference * (1 - Math.exp(-delta * 10));
if (Math.abs(difference) < 0.0005) {
currentRotation = rotationGoal;
}
}
targetCell.rotation.y = currentRotation;
if (Math.abs(currentRotation - lastDiagnosticAngle) > 0.001) {
lastDiagnosticAngle = currentRotation;
rebuildDiagnostics();
}
updateUIState();
const pulse = 1 + Math.sin(clock.elapsedTime * 2.7) * 0.022;
selectionRing.scale.setScalar(pulse);
selectionRing.material.opacity = 0.74 + Math.sin(clock.elapsedTime * 2.7) * 0.16;
targetLabel.position.y = 1.62 + Math.sin(clock.elapsedTime * 1.7) * 0.035;
updateCamera();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = camera.aspect < 0.8 ? 51 : 40;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
if (camera.aspect < 0.8) {
camera.fov = 51;
camera.updateProjectionMatrix();
}
updateUIState();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```