Prompt specification
Build or customize the Three.js 3D scene "One Flipped Bit on a Signal Track" 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: One Flipped Bit on a Signal Track
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/one-flipped-bit-on-a-signal-track-x3374
Scene Description Prompt:
Animate a bit sequence crossing a toy channel with one selected flip. Recompute parity and show the detected mismatch while preserving the original sequence for comparison. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (one-flipped-bit-on-a-signal-track-x3374):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Parity Channel</title>
<style>
:root {
color-scheme: dark;
--bg: #07101d;
--panel: rgba(10, 19, 34, 0.88);
--line: rgba(148, 180, 219, 0.22);
--text: #edf6ff;
--muted: #8ca1bc;
--cyan: #48dcff;
--pink: #ff609f;
--green: #61f0aa;
--amber: #ffd166;
--red: #ff526c;
}
* {
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;
}
body {
position: relative;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(16px);
}
#titleCard {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
padding: 16px 18px;
border-radius: 16px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 3vw, 30px);
line-height: 1.05;
letter-spacing: -0.035em;
}
#description {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#controls {
position: fixed;
z-index: 20;
top: 18px;
right: 18px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border-radius: 14px;
}
button {
border: 0;
color: var(--text);
font: inherit;
cursor: pointer;
}
.control-button {
min-height: 38px;
padding: 0 14px;
border: 1px solid rgba(130, 170, 210, 0.24);
border-radius: 9px;
background: rgba(255, 255, 255, 0.055);
font-size: 12px;
font-weight: 750;
transition: background 150ms ease, transform 150ms ease;
}
.control-button:hover {
background: rgba(72, 220, 255, 0.12);
transform: translateY(-1px);
}
.control-button.primary {
border-color: rgba(72, 220, 255, 0.42);
background: rgba(72, 220, 255, 0.14);
color: #bff3ff;
}
.speed-control {
display: flex;
align-items: center;
gap: 8px;
padding: 0 8px;
color: var(--muted);
font: 650 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
text-transform: uppercase;
}
input[type="range"] {
width: 76px;
accent-color: var(--cyan);
}
#parityPanel {
position: fixed;
z-index: 15;
left: 50%;
bottom: 18px;
width: min(820px, calc(100vw - 36px));
padding: 14px 16px 15px;
border-radius: 17px;
transform: translateX(-50%);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 11px;
}
.panel-heading strong {
font-size: 13px;
letter-spacing: 0.01em;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid transparent;
border-radius: 999px;
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.status::before {
width: 7px;
height: 7px;
border-radius: 2px;
background: currentColor;
content: "";
box-shadow: 0 0 12px currentColor;
}
.status.good {
border-color: rgba(97, 240, 170, 0.28);
background: rgba(97, 240, 170, 0.09);
color: var(--green);
}
.status.warn {
border-color: rgba(255, 209, 102, 0.28);
background: rgba(255, 209, 102, 0.09);
color: var(--amber);
}
.status.bad {
border-color: rgba(255, 82, 108, 0.35);
background: rgba(255, 82, 108, 0.11);
color: var(--red);
}
.sequence-grid {
display: grid;
grid-template-columns: 102px 1fr;
align-items: center;
gap: 9px 12px;
}
.sequence-label {
color: var(--muted);
font: 750 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.sequence-label small {
display: block;
margin-top: 4px;
color: #637995;
font-size: 9px;
font-weight: 600;
letter-spacing: 0.03em;
}
.bits {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.bit {
position: relative;
display: grid;
width: 34px;
height: 34px;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 8px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
font: 800 16px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
button.bit:hover {
border-color: rgba(255, 255, 255, 0.35);
background: rgba(255, 255, 255, 0.08);
}
.bit::before {
position: absolute;
top: 2px;
left: 4px;
color: rgba(225, 239, 255, 0.45);
content: attr(data-label);
font: 700 7px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.bit.zero {
color: var(--cyan);
background: rgba(72, 220, 255, 0.09);
border-color: rgba(72, 220, 255, 0.25);
}
.bit.one {
color: var(--pink);
background: rgba(255, 96, 159, 0.09);
border-color: rgba(255, 96, 159, 0.25);
}
.bit.selected {
outline: 2px solid var(--amber);
outline-offset: 2px;
}
.bit.corrupt {
border-color: var(--red);
color: #fff;
background: rgba(255, 82, 108, 0.38);
box-shadow: 0 0 16px rgba(255, 82, 108, 0.22);
}
.bit.check {
width: 42px;
color: var(--green);
background: rgba(97, 240, 170, 0.08);
border-color: rgba(97, 240, 170, 0.25);
font-size: 12px;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 12px;
}
.metric {
min-width: 0;
padding: 9px 10px;
border: 1px solid rgba(148, 180, 219, 0.13);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font: 700 9px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.metric strong {
display: block;
overflow: hidden;
font: 800 13px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
text-overflow: ellipsis;
white-space: nowrap;
}
.metric strong.good {
color: var(--green);
}
.metric strong.bad {
color: var(--red);
}
.decision {
margin-top: 9px;
padding: 9px 11px;
border-left: 3px solid var(--green);
border-radius: 4px 9px 9px 4px;
background: rgba(97, 240, 170, 0.075);
color: var(--green);
font: 750 12px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.decision.warn {
border-left-color: var(--amber);
background: rgba(255, 209, 102, 0.075);
color: var(--amber);
}
.decision.bad {
border-left-color: var(--red);
background: rgba(255, 82, 108, 0.09);
color: var(--red);
}
#hint {
position: fixed;
z-index: 8;
right: 20px;
bottom: 180px;
color: rgba(190, 210, 233, 0.56);
font: 650 10px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
pointer-events: none;
}
@media (max-width: 720px) {
#titleCard {
width: calc(100vw - 32px);
padding: 13px 14px;
}
#description {
display: none;
}
#controls {
top: 103px;
right: 16px;
left: 16px;
justify-content: center;
}
#parityPanel {
bottom: 10px;
width: calc(100vw - 20px);
padding: 11px;
}
.sequence-grid {
grid-template-columns: 72px 1fr;
gap: 7px;
}
.sequence-label small {
display: none;
}
.bit {
width: 29px;
height: 29px;
font-size: 14px;
}
.bit.check {
width: 36px;
}
.metrics {
gap: 5px;
}
.metric {
padding: 7px;
}
.metric strong {
font-size: 10px;
}
#hint {
display: none;
}
}
@media (max-height: 650px) {
#description,
#hint {
display: none;
}
#parityPanel {
padding: 10px 12px;
}
.metrics {
margin-top: 7px;
}
.decision {
margin-top: 6px;
padding: 6px 9px;
}
}
</style>
</head>
<body>
<div id="titleCard" class="panel">
<div class="eyebrow">Voxel Signal Lab / 05</div>
<h1>Parity Channel</h1>
<p id="description">
A source packet travels beside an immutable reference copy.
One selected bit flips inside the scanner, then the channel recomputes parity.
</p>
</div>
<div id="controls" class="panel">
<button id="replayButton" class="control-button primary" type="button">
Replay channel
</button>
<button id="pauseButton" class="control-button" type="button">
Pause
</button>
<label class="speed-control">
Speed
<input id="speedSlider" type="range" min="0.5" max="1.8" step="0.1" value="1">
<span id="speedValue">1.0×</span>
</label>
</div>
<section id="parityPanel" class="panel" aria-live="polite">
<div class="panel-heading">
<strong>Packet comparison</strong>
<span id="statusBadge" class="status good">Streaming · parity armed</span>
</div>
<div class="sequence-grid">
<div class="sequence-label">
Source
<small>Immutable original</small>
</div>
<div id="sourceBits" class="bits"></div>
<div class="sequence-label">
Received
<small>Live channel copy</small>
</div>
<div id="receivedBits" class="bits"></div>
</div>
<div class="metrics">
<div class="metric">
<span>Stored check bit</span>
<strong id="storedCheck" class="good">P = 1</strong>
</div>
<div class="metric">
<span>Original parity</span>
<strong id="sourceParity" class="good">EVEN</strong>
</div>
<div class="metric">
<span>Recomputed parity</span>
<strong id="receivedParity" class="good">EVEN</strong>
</div>
</div>
<div id="decision" class="decision">
Bit 05 is selected and will flip in the scanner.
</div>
</section>
<div id="hint">DRAG TO ORBIT · WHEEL TO ZOOM · CLICK A SOURCE BIT TO SELECT THE FAULT</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Packet state
// -------------------------------------------------------------------------
const ORIGINAL = Object.freeze([1, 0, 1, 1, 0, 0, 1, 1]);
const EXPECTED_CHECK_BIT = ORIGINAL.reduce((sum, bit) => sum + bit, 0) % 2;
const DATA_COUNT = ORIGINAL.length;
const SPACING = 1.35;
const CENTER_INDEX = (DATA_COUNT - 1) / 2;
const sourceBitsElement = document.querySelector("#sourceBits");
const receivedBitsElement = document.querySelector("#receivedBits");
const sourceChipElements = [];
const receivedChipElements = [];
for (let i = 0; i < DATA_COUNT; i++) {
const sourceChip = document.createElement("button");
sourceChip.className = "bit";
sourceChip.type = "button";
sourceChip.dataset.index = String(i);
sourceChip.dataset.label = String(i + 1).padStart(2, "0");
sourceChip.title = `Select bit ${String(i + 1).padStart(2, "0")} for the injected flip`;
sourceChip.addEventListener("click", () => selectFaultBit(i));
sourceBitsElement.appendChild(sourceChip);
sourceChipElements.push(sourceChip);
const receivedChip = document.createElement("div");
receivedChip.className = "bit";
receivedChip.dataset.index = String(i);
receivedChip.dataset.label = String(i + 1).padStart(2, "0");
receivedBitsElement.appendChild(receivedChip);
receivedChipElements.push(receivedChip);
}
const sourceCheckChip = document.createElement("div");
sourceCheckChip.className = "bit check";
sourceCheckChip.textContent = `P${EXPECTED_CHECK_BIT}`;
sourceCheckChip.title = "Even-parity check bit";
sourceBitsElement.appendChild(sourceCheckChip);
const receivedCheckChip = document.createElement("div");
receivedCheckChip.className = "bit check";
receivedCheckChip.textContent = `P${EXPECTED_CHECK_BIT}`;
receivedCheckChip.title = "Unchanged check bit";
receivedBitsElement.appendChild(receivedCheckChip);
let selectedIndex = 4;
let received = ORIGINAL.slice();
const statusBadge = document.querySelector("#statusBadge");
const storedCheckElement = document.querySelector("#storedCheck");
const sourceParityElement = document.querySelector("#sourceParity");
const receivedParityElement = document.querySelector("#receivedParity");
const decisionElement = document.querySelector("#decision");
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.08;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101d);
scene.fog = new THREE.Fog(0x07101d, 22, 48);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.75, 0);
const cameraState = {
theta: 0.66,
phi: 0.98,
radius: 20
};
const hemisphere = new THREE.HemisphereLight(0x9bc9ff, 0x152039, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(5, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -17;
keyLight.shadow.camera.right = 17;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.normalBias = 0.035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4bd8ff, 1.45);
rimLight.position.set(-10, 7, -8);
scene.add(rimLight);
const pinkRim = new THREE.PointLight(0xff4f98, 16, 18, 2);
pinkRim.position.set(10, 5, -4);
scene.add(pinkRim);
const COLORS = {
background: 0x07101d,
floorA: 0x0d192b,
floorB: 0x101e33,
conveyor: 0x17263b,
conveyorEdge: 0x263c56,
belt: 0x0c1524,
beltTick: 0x17334a,
frame: 0x29445b,
frameLight: 0x3c6178,
cyan: 0x48dcff,
pink: 0xff609f,
green: 0x61f0aa,
amber: 0xffd166,
red: 0xff526c,
dark: 0x18263a
};
const materials = {
conveyor: new THREE.MeshStandardMaterial({
color: COLORS.conveyor,
roughness: 0.78,
metalness: 0.16
}),
conveyorEdge: new THREE.MeshStandardMaterial({
color: COLORS.conveyorEdge,
roughness: 0.58,
metalness: 0.38
}),
belt: new THREE.MeshStandardMaterial({
color: COLORS.belt,
roughness: 0.92
}),
frame: new THREE.MeshStandardMaterial({
color: COLORS.frame,
roughness: 0.5,
metalness: 0.45
}),
frameLight: new THREE.MeshStandardMaterial({
color: COLORS.frameLight,
roughness: 0.4,
metalness: 0.5
}),
dark: new THREE.MeshStandardMaterial({
color: COLORS.dark,
roughness: 0.72
}),
cyan: new THREE.MeshStandardMaterial({
color: COLORS.cyan,
emissive: COLORS.cyan,
emissiveIntensity: 0.42,
roughness: 0.45
}),
pink: new THREE.MeshStandardMaterial({
color: COLORS.pink,
emissive: COLORS.pink,
emissiveIntensity: 0.42,
roughness: 0.45
}),
green: new THREE.MeshStandardMaterial({
color: COLORS.green,
emissive: COLORS.green,
emissiveIntensity: 0.3,
roughness: 0.45
}),
amber: new THREE.MeshStandardMaterial({
color: COLORS.amber,
emissive: COLORS.amber,
emissiveIntensity: 0.28,
roughness: 0.45
}),
red: new THREE.MeshStandardMaterial({
color: COLORS.red,
emissive: COLORS.red,
emissiveIntensity: 0.35,
roughness: 0.45
})
};
function makeBox(width, height, depth, material, x, y, z, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Voxel floor.
const floorUnderlay = makeBox(
40,
0.2,
28,
new THREE.MeshStandardMaterial({
color: 0x081321,
roughness: 1
}),
0,
-0.22,
0,
false
);
floorUnderlay.receiveShadow = true;
const floorGeometry = new THREE.BoxGeometry(0.94, 0.12, 0.94);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96
});
const floorColumns = 27;
const floorRows = 17;
const floorTiles = new THREE.InstancedMesh(
floorGeometry,
floorMaterial,
floorColumns * floorRows
);
floorTiles.receiveShadow = true;
const floorMatrix = new THREE.Matrix4();
const floorColor = new THREE.Color();
let floorIndex = 0;
for (let row = 0; row < floorRows; row++) {
for (let column = 0; column < floorColumns; column++) {
const x = (column - (floorColumns - 1) / 2) * 1.02;
const z = (row - (floorRows - 1) / 2) * 1.02 - 1.2;
floorMatrix.makeTranslation(x, -0.04, z);
floorTiles.setMatrixAt(floorIndex, floorMatrix);
floorColor.setHex(
(column + row) % 3 === 0 ? COLORS.floorB : COLORS.floorA
);
floorTiles.setColorAt(floorIndex, floorColor);
floorIndex++;
}
}
floorTiles.instanceMatrix.needsUpdate = true;
if (floorTiles.instanceColor) floorTiles.instanceColor.needsUpdate = true;
scene.add(floorTiles);
// Toy conveyor and two comparison lanes.
makeBox(23, 0.55, 5.7, materials.conveyor, 0, 0.25, 0);
makeBox(23.2, 0.17, 0.18, materials.conveyorEdge, 0, 0.59, -2.88);
makeBox(23.2, 0.17, 0.18, materials.conveyorEdge, 0, 0.59, 2.88);
makeBox(23, 0.18, 0.12, materials.conveyorEdge, 0, 0.67, 0);
const laneZ = {
original: -1.35,
received: 1.35
};
makeBox(23, 0.12, 2.2, materials.belt, 0, 0.56, laneZ.original);
makeBox(23, 0.12, 2.2, materials.belt, 0, 0.56, laneZ.received);
// Conveyor edge rollers.
for (let x = -10.8; x <= 10.8; x += 0.9) {
makeBox(
0.25,
0.27,
0.19,
materials.frameLight,
x,
0.37,
2.98,
false
);
}
// Moving belt ticks.
const beltTicks = [];
const tickGeometry = new THREE.BoxGeometry(0.09, 0.035, 1.5);
const tickMaterial = new THREE.MeshStandardMaterial({
color: COLORS.beltTick,
emissive: 0x0b2230,
emissiveIntensity: 0.2,
roughness: 0.85
});
for (const z of [laneZ.original, laneZ.received]) {
for (let x = -11.2; x < 11.5; x += 1.55) {
const tick = new THREE.Mesh(tickGeometry, tickMaterial);
tick.position.set(x, 0.638, z);
tick.receiveShadow = true;
scene.add(tick);
beltTicks.push(tick);
}
}
// Direction arrows made from voxels.
for (const x of [-8, -4, 0, 4, 8]) {
const arrow = new THREE.Group();
arrow.position.set(x, 0.12, 3.65);
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0x24445d,
emissive: 0x102c3d,
emissiveIntensity: 0.25
});
for (const offset of [-0.18, 0.18]) {
const cube = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.08, 0.22),
arrowMaterial
);
cube.position.x = offset;
cube.rotation.y = offset < 0 ? -0.65 : 0.65;
arrow.add(cube);
}
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(0.65, 0.08, 0.2),
arrowMaterial
);
shaft.position.x = -0.48;
arrow.add(shaft);
scene.add(arrow);
}
// -------------------------------------------------------------------------
// Scanner gate around the received lane
// -------------------------------------------------------------------------
makeBox(0.64, 3.0, 0.64, materials.frame, 0, 1.85, -0.05);
makeBox(0.64, 3.0, 0.64, materials.frame, 0, 1.85, 2.75);
makeBox(0.72, 0.58, 3.48, materials.frameLight, 0, 3.35, 1.35);
for (const z of [-0.05, 2.75]) {
for (let y = 0.65; y < 3.1; y += 0.46) {
makeBox(
0.69,
0.12,
0.69,
materials.frameLight,
0,
y,
z,
false
);
}
}
const scanMaterial = new THREE.MeshBasicMaterial({
color: COLORS.green,
transparent: true,
opacity: 0.075,
depthWrite: false,
side: THREE.DoubleSide
});
const scanPlane = new THREE.Mesh(
new THREE.BoxGeometry(0.055, 2.25, 2.35),
scanMaterial
);
scanPlane.position.set(0, 1.65, laneZ.received);
scene.add(scanPlane);
const scannerLight = new THREE.PointLight(COLORS.green, 12, 6, 2);
scannerLight.position.set(0.8, 2.5, laneZ.received);
scene.add(scannerLight);
// Nine audit lamps: eight data bits plus the check bit.
const auditLamps = [];
for (let i = 0; i < 9; i++) {
const material = new THREE.MeshStandardMaterial({
color: 0x173b38,
emissive: 0x0c241f,
emissiveIntensity: 0.35,
roughness: 0.42
});
const lamp = new THREE.Mesh(
new THREE.BoxGeometry(0.32, 0.22, 0.22),
material
);
lamp.position.set(0.39, 3.35, 0.12 + i * 0.305);
scene.add(lamp);
auditLamps.push(lamp);
}
// -------------------------------------------------------------------------
// Labels and bit visuals
// -------------------------------------------------------------------------
function makeTextSprite(text, color = "#ffffff", width = 512, height = 128) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
context.clearRect(0, 0, width, height);
const radius = 22;
const x = 8;
const y = 8;
const w = width - 16;
const h = height - 16;
context.beginPath();
context.moveTo(x + radius, y);
context.lineTo(x + w - radius, y);
context.quadraticCurveTo(x + w, y, x + w, y + radius);
context.lineTo(x + w, y + h - radius);
context.quadraticCurveTo(x + w, y + h, x + w - radius, y + h);
context.lineTo(x + radius, y + h);
context.quadraticCurveTo(x, y + h, x, y + h - radius);
context.lineTo(x, y + radius);
context.quadraticCurveTo(x, y, x + radius, y);
context.closePath();
context.fillStyle = "rgba(6, 14, 26, 0.88)";
context.fill();
context.strokeStyle = color;
context.lineWidth = 5;
context.stroke();
context.fillStyle = color;
context.font = "800 36px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, width / 2, height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
return sprite;
}
const originalLabel = makeTextSprite("IMMUTABLE SOURCE", "#61f0aa");
originalLabel.scale.set(3.45, 0.78, 1);
originalLabel.position.set(-7.5, 3.5, laneZ.original);
scene.add(originalLabel);
const receivedLabel = makeTextSprite("LIVE CHANNEL", "#ffd166");
receivedLabel.scale.set(3.1, 0.78, 1);
receivedLabel.position.set(-7.5, 3.5, laneZ.received);
scene.add(receivedLabel);
const bitPatterns = {
0: [
[1, 1, 1],
[1, 0, 1],
[1, 0, 1],
[1, 0, 1],
[1, 1, 1]
],
1: [
[0, 1, 0],
[1, 1, 0],
[0, 1, 0],
[0, 1, 0],
[1, 1, 1]
]
};
function createBitVisual(isOriginal, isParityBit = false) {
const group = new THREE.Group();
const baseMaterial = isParityBit
? new THREE.MeshStandardMaterial({
color: 0x253d45,
roughness: 0.7,
metalness: 0.18
})
: materials.dark;
const base = new THREE.Mesh(
new THREE.BoxGeometry(1.04, 0.16, 1.04),
baseMaterial
);
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const accentMaterial = (isOriginal ? materials.green : materials.amber).clone();
const accentMeshes = [];
for (const x of [-0.43, 0.43]) {
const accent = new THREE.Mesh(
new THREE.BoxGeometry(0.11, 0.08, 0.11),
accentMaterial
);
accent.position.set(x, 0.1, 0.48);
group.add(accent);
accentMeshes.push(accent);
}
const pixels = [];
for (let row = 0; row < 5; row++) {
for (let column = 0; column < 3; column++) {
const pixel = new THREE.Mesh(
new THREE.BoxGeometry(0.2, 0.16, 0.22),
materials.cyan
);
pixel.position.set(
(column - 1) * 0.24,
0.17 + (2 - row) * 0.18,
0.2
);
pixel.castShadow = true;
group.add(pixel);
pixels.push(pixel);
}
}
const visual = {
group,
pixels,
accentMeshes,
value: 0,
setValue(value) {
this.value = value;
const pattern = bitPatterns[value];
for (let row = 0; row < 5; row++) {
for (let column = 0; column < 3; column++) {
const pixel = pixels[row * 3 + column];
const occupied = pattern[row][column] === 1;
pixel.visible = occupied;
pixel.material = value === 0 ? materials.cyan : materials.pink;
}
}
},
setAlert(alert) {
const material = alert ? materials.red : accentMaterial;
for (const accent of accentMeshes) {
accent.material = material;
}
}
};
visual.setValue(0);
return visual;
}
function createLane(z, isOriginal) {
const root = new THREE.Group();
root.position.set(-9.6, 0.75, z);
scene.add(root);
const visuals = [];
for (let i = 0; i < DATA_COUNT; i++) {
const visual = createBitVisual(isOriginal);
visual.group.position.x = (i - CENTER_INDEX) * SPACING;
visual.setValue(ORIGINAL[i]);
root.add(visual.group);
visuals.push(visual);
}
// The unchanged parity check bit follows the eight data bits.
const checkVisual = createBitVisual(isOriginal, true);
checkVisual.group.position.x = (DATA_COUNT - CENTER_INDEX) * SPACING;
checkVisual.setValue(EXPECTED_CHECK_BIT);
root.add(checkVisual.group);
visuals.push(checkVisual);
return { root, visuals };
}
const originalLane = createLane(laneZ.original, true);
const receivedLane = createLane(laneZ.received, false);
// Mismatch marker attached to the selected received bit.
const mismatchMarker = new THREE.Group();
const mismatchLabel = makeTextSprite("MISMATCH", "#ff526c", 512, 112);
mismatchLabel.scale.set(1.9, 0.42, 1);
mismatchLabel.position.y = 1.95;
mismatchMarker.add(mismatchLabel);
const markerTop = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.22, 0.22),
materials.red
);
markerTop.position.y = 1.48;
mismatchMarker.add(markerTop);
const markerLeft = markerTop.clone();
markerLeft.position.set(-0.15, 1.25, 0);
mismatchMarker.add(markerLeft);
const markerRight = markerTop.clone();
markerRight.position.set(0.15, 1.25, 0);
mismatchMarker.add(markerRight);
mismatchMarker.visible = false;
receivedLane.visuals[selectedIndex].group.add(mismatchMarker);
// -------------------------------------------------------------------------
// UI and state transitions
// -------------------------------------------------------------------------
const replayButton = document.querySelector("#replayButton");
const pauseButton = document.querySelector("#pauseButton");
const speedSlider = document.querySelector("#speedSlider");
const speedValue = document.querySelector("#speedValue");
let speed = 1;
let paused = false;
let travel = -9.6;
let elapsed = 0;
let phase = "streaming";
let flipping = false;
let flipped = false;
let flipSwapped = false;
let auditing = false;
let auditFinished = false;
let packetComplete = false;
const flipTravel = -(selectedIndex - CENTER_INDEX) * SPACING;
const auditTravel = CENTER_INDEX * SPACING + 0.12;
function bitLabel(index) {
return String(index + 1).padStart(2, "0");
}
function setScanColor(color) {
scanMaterial.color.setHex(color);
scannerLight.color.setHex(color);
}
function setPhase(nextPhase) {
phase = nextPhase;
const phaseData = {
streaming: {
badge: "Streaming · parity armed",
badgeClass: "good",
decision: `Bit ${bitLabel(selectedIndex)} is selected and will flip in the scanner.`,
decisionClass: ""
},
flipping: {
badge: `Injecting flip · bit ${bitLabel(selectedIndex)}`,
badgeClass: "warn",
decision: `Selected bit ${bitLabel(selectedIndex)} is rotating inside the scanner.`,
decisionClass: "warn"
},
pending: {
badge: "Flip captured · audit queued",
badgeClass: "warn",
decision: "The received packet is waiting to clear the scanner before parity is recomputed.",
decisionClass: "warn"
},
auditing: {
badge: "Recomputing parity",
badgeClass: "warn",
decision: "The channel is recounting all eight data bits and the unchanged check bit.",
decisionClass: "warn"
},
mismatch: {
badge: `Mismatch detected · bit ${bitLabel(selectedIndex)}`,
badgeClass: "bad",
decision: `FAULT FOUND: received bit ${bitLabel(selectedIndex)} differs from the immutable source.`,
decisionClass: "bad"
}
};
const data = phaseData[nextPhase];
statusBadge.textContent = data.badge;
statusBadge.className = `status ${data.badgeClass}`;
decisionElement.textContent = data.decision;
decisionElement.className = `decision ${data.decisionClass}`;
}
function parityLabel(bits) {
const totalOnes = bits.reduce((sum, bit) => sum + bit, 0);
return {
ones: totalOnes,
even: totalOnes % 2 === 0,
label: totalOnes % 2 === 0 ? "EVEN" : "ODD"
};
}
function updateComparison() {
sourceChipElements.forEach((chip, index) => {
chip.classList.toggle("zero", ORIGINAL[index] === 0);
chip.classList.toggle("one", ORIGINAL[index] === 1);
chip.classList.toggle("selected", index === selectedIndex);
});
receivedChipElements.forEach((chip, index) => {
const differs = received[index] !== ORIGINAL[index];
chip.textContent = String(received[index]);
chip.classList.toggle("zero", received[index] === 0);
chip.classList.toggle("one", received[index] === 1);
chip.classList.toggle("selected", index === selectedIndex);
chip.classList.toggle("corrupt", differs);
});
receivedLane.visuals.forEach((visual, index) => {
visual.setAlert(index < DATA_COUNT && received[index] !== ORIGINAL[index]);
});
const source = parityLabel([...ORIGINAL, EXPECTED_CHECK_BIT]);
const live = parityLabel([...received, EXPECTED_CHECK_BIT]);
storedCheckElement.textContent = `P = ${EXPECTED_CHECK_BIT}`;
sourceParityElement.textContent = `${source.label} · ${source.ones} ones`;
receivedParityElement.textContent = `${live.label} · ${live.ones} ones`;
sourceParityElement.className = source.even ? "good" : "bad";
receivedParityElement.className = live.even ? "good" : "bad";
}
function resetAuditLamps() {
for (const lamp of auditLamps) {
lamp.material.color.setHex(0x173b38);
lamp.material.emissive.setHex(0x0c241f);
lamp.material.emissiveIntensity = 0.35;
}
}
function resetDemo() {
received = ORIGINAL.slice();
travel = -9.6;
elapsed = 0;
phase = "streaming";
paused = false;
flipping = false;
flipped = false;
flipSwapped = false;
auditing = false;
auditFinished = false;
packetComplete = false;
originalLane.root.position.x = travel;
receivedLane.root.position.x = travel;
originalLane.visuals.forEach((visual, index) => {
visual.setValue(index < DATA_COUNT ? ORIGINAL[index] : EXPECTED_CHECK_BIT);
visual.group.rotation.z = 0;
visual.setAlert(false);
});
receivedLane.visuals.forEach((visual, index) => {
visual.setValue(index < DATA_COUNT ? ORIGINAL[index] : EXPECTED_CHECK_BIT);
visual.group.rotation.z = 0;
visual.setAlert(false);
});
mismatchMarker.visible = false;
resetAuditLamps();
setScanColor(COLORS.green);
setPhase("streaming");
updateComparison();
pauseButton.textContent = "Pause";
}
function selectFaultBit(index) {
selectedIndex = index;
receivedLane.visuals.forEach((visual) => {
if (mismatchMarker.parent === visual.group) {
visual.group.remove(mismatchMarker);
}
});
receivedLane.visuals[index].group.add(mismatchMarker);
resetDemo();
}
function beginFlip() {
flipping = true;
flipSwapped = false;
setPhase("flipping");
setScanColor(COLORS.amber);
}
function updateFlip() {
const progress = Math.min((elapsed - state.flipStart) / 0.78, 1);
const eased = progress * progress * (3 - 2 * progress);
const selectedVisual = receivedLane.visuals[selectedIndex];
selectedVisual.group.rotation.z = eased * Math.PI * 2;
if (progress >= 0.5 && !flipSwapped) {
received[selectedIndex] = ORIGINAL[selectedIndex] === 0 ? 1 : 0;
selectedVisual.setValue(received[selectedIndex]);
flipSwapped = true;
updateComparison();
}
if (progress >= 1) {
selectedVisual.group.rotation.z = 0;
flipping = false;
flipped = true;
setPhase("pending");
}
}
function beginAudit() {
auditing = true;
auditFinished = false;
setPhase("auditing");
setScanColor(COLORS.amber);
}
function updateAudit() {
const progress = Math.min((elapsed - state.auditStart) / 1.35, 1);
const auditBits = [...received, EXPECTED_CHECK_BIT];
auditLamps.forEach((lamp, index) => {
const localProgress = progress * 9 - index;
const active = localProgress >= 0 && localProgress <= 1;
if (active) {
const color = auditBits[index] === 0 ? COLORS.cyan : COLORS.pink;
lamp.material.color.setHex(color);
lamp.material.emissive.setHex(color);
lamp.material.emissiveIntensity = 1.1;
} else {
lamp.material.color.setHex(0x173b38);
lamp.material.emissive.setHex(0x0c241f);
lamp.material.emissiveIntensity = 0.35;
}
});
if (progress >= 1 && !auditFinished) {
auditFinished = true;
for (const lamp of auditLamps) {
lamp.material.color.setHex(COLORS.red);
lamp.material.emissive.setHex(COLORS.red);
lamp.material.emissiveIntensity = 0.9;
}
mismatchMarker.visible = true;
setScanColor(COLORS.red);
setPhase("mismatch");
}
}
// Mutable timing fields live in one object so reset cannot leave stale
// timestamps behind.
const state = {
flipStart: 0,
auditStart: 0
};
replayButton.addEventListener("click", resetDemo);
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
speedSlider.addEventListener("input", () => {
speed = Number(speedSlider.value);
speedValue.textContent = `${speed.toFixed(1)}×`;
});
// -------------------------------------------------------------------------
// Direct camera controls: no external controls addon
// -------------------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
let activePointerId = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
activePointerId = event.pointerId;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointerId) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi -= deltaY * 0.005;
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi, 0.45, 1.32);
});
function stopDragging(event) {
if (event && activePointerId !== null &&
event.pointerId !== activePointerId) {
return;
}
dragging = false;
activePointerId = null;
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("pointerleave", (event) => {
if (!dragging) stopDragging(event);
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
12,
29
);
}, { passive: false });
renderer.domElement.style.cursor = "grab";
function updateCamera() {
const portraitScale = camera.aspect < 1 ? 1.48 : 1;
const radius = cameraState.radius * portraitScale;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + radius * Math.cos(cameraState.phi),
cameraTarget.z + radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
}
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Main animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
elapsed += delta;
if (!packetComplete) {
travel += delta * 2.0 * speed;
if (travel >= 10) {
travel = 10;
packetComplete = true;
}
}
if (!flipping && !flipped && travel >= flipTravel) {
state.flipStart = elapsed;
beginFlip();
}
if (flipping) {
updateFlip();
}
if (flipped && !auditing && travel >= auditTravel) {
state.auditStart = elapsed;
beginAudit();
}
if (auditing) {
updateAudit();
}
for (const tick of beltTicks) {
tick.position.x += delta * 1.45 * speed;
if (tick.position.x > 11.5) tick.position.x = -11.5;
}
}
const bob = Math.sin(elapsed * 3.2) * 0.012;
originalLane.root.position.set(travel, 0.75 + bob, laneZ.original);
receivedLane.root.position.set(travel, 0.75 + bob, laneZ.received);
if (mismatchMarker.visible) {
mismatchMarker.position.y = Math.sin(elapsed * 4.5) * 0.08;
mismatchMarker.children.forEach((child) => {
if (child.material && "emissiveIntensity" in child.material) {
child.material.emissiveIntensity = 0.75 + Math.sin(elapsed * 5) * 0.22;
}
});
}
updateCamera();
renderer.render(scene, camera);
}
resetDemo();
resize();
animate();
</script>
</body>
</html>
```