Prompt specification
Build or customize the Three.js 3D scene "Parity-bit transmission row" 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: Parity-bit transmission row
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/parity-bit-transmission-row-x1865
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Build a short binary message with an added even-parity bit and a receiving checker; show how one flipped bit violates the stated parity rule.
Working Three.js Starter (parity-bit-transmission-row-x1865):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Even Parity — One Flipped Bit</title>
<style>
:root {
color-scheme: dark;
--bg: #07111c;
--panel: rgba(12, 27, 42, 0.82);
--line: rgba(154, 218, 255, 0.18);
--text: #eef8ff;
--muted: #91a9bb;
--cyan: #53e7ff;
--gold: #ffc857;
--green: #4df2a5;
--red: #ff617d;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#threeCanvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 45%, transparent 38%, rgba(2, 7, 13, 0.42) 100%),
linear-gradient(to bottom, rgba(3, 9, 16, 0.28), transparent 25%, transparent 72%, rgba(3, 9, 16, 0.34));
z-index: 1;
}
header {
position: fixed;
top: 26px;
left: 30px;
z-index: 3;
max-width: 480px;
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 4px;
font-size: clamp(28px, 4vw, 48px);
line-height: 1;
letter-spacing: -0.045em;
}
header p {
margin: 9px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.panel {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 16px;
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#statusCard {
position: fixed;
top: 24px;
right: 24px;
z-index: 4;
width: 344px;
padding: 18px;
}
.status-head {
display: flex;
align-items: center;
gap: 11px;
}
#signal {
width: 12px;
height: 12px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 5px rgba(77, 242, 165, 0.12), 0 0 20px var(--green);
}
body[data-state="fail"] #signal {
background: var(--red);
box-shadow: 0 0 0 5px rgba(255, 97, 125, 0.12), 0 0 20px var(--red);
}
.status-title {
display: flex;
align-items: baseline;
gap: 8px;
}
.status-title small {
color: var(--muted);
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
#verdict {
font: 800 18px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
}
body[data-state="pass"] #verdict { color: var(--green); }
body[data-state="fail"] #verdict { color: var(--red); }
#reason {
margin: 11px 0 16px;
color: #bed0dd;
font-size: 12px;
line-height: 1.45;
}
.section-label {
margin-bottom: 8px;
color: var(--muted);
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.13em;
}
#sequence {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 5px;
margin-bottom: 14px;
}
.bit-chip {
display: grid;
place-items: center;
width: 26px;
height: 30px;
border: 1px solid rgba(83, 231, 255, 0.28);
border-radius: 6px;
background: rgba(83, 231, 255, 0.07);
color: #dffaff;
font: 700 14px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.bit-chip.bad {
color: #fff;
border-color: var(--red);
background: rgba(255, 97, 125, 0.2);
box-shadow: 0 0 14px rgba(255, 97, 125, 0.18);
}
.bit-chip.parity {
color: #201600;
border-color: var(--gold);
background: var(--gold);
}
.separator {
margin: 0 3px;
color: #6e8495;
font-weight: 800;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.metric {
padding: 9px 8px;
border: 1px solid rgba(154, 218, 255, 0.1);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font: 700 9px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.metric strong {
font: 800 15px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.metric.bad strong { color: var(--red); }
.metric.good strong { color: var(--green); }
.rule {
margin-top: 12px;
padding: 10px 11px;
border-left: 2px solid var(--cyan);
border-radius: 0 8px 8px 0;
background: rgba(83, 231, 255, 0.06);
color: #c5d8e4;
font-size: 11px;
line-height: 1.45;
}
.rule strong { color: #f0fbff; }
#event {
margin: 11px 1px 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.dock {
position: fixed;
left: 50%;
bottom: 22px;
z-index: 5;
display: flex;
align-items: center;
gap: 8px;
width: max-content;
max-width: calc(100vw - 24px);
padding: 10px;
transform: translateX(-50%);
}
.control-label {
margin: 0 3px 0 5px;
color: var(--muted);
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
select, button {
height: 38px;
border: 1px solid rgba(154, 218, 255, 0.2);
border-radius: 9px;
background: #102235;
color: var(--text);
font: 700 12px ui-sans-serif, system-ui, sans-serif;
cursor: pointer;
}
select {
min-width: 78px;
padding: 0 30px 0 10px;
}
button {
padding: 0 14px;
transition: border-color 150ms ease, background 150ms ease, transform 150ms ease;
}
button:hover {
border-color: rgba(83, 231, 255, 0.58);
background: #163149;
transform: translateY(-1px);
}
button:focus-visible, select:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
button.primary {
border-color: transparent;
background: var(--cyan);
color: #031118;
}
button.primary:hover {
background: #8cf0ff;
}
button.ghost {
background: transparent;
}
.hint {
margin-left: 6px;
color: #72899a;
font-size: 10px;
white-space: nowrap;
}
@media (max-width: 900px) {
header {
top: 18px;
left: 18px;
}
header p { display: none; }
#statusCard {
top: 18px;
right: 18px;
width: 305px;
}
.hint { display: none; }
}
@media (max-width: 620px) {
h1 { font-size: 28px; }
#statusCard {
top: auto;
right: 12px;
bottom: 102px;
width: calc(100vw - 24px);
max-height: 39vh;
overflow: auto;
padding: 13px;
}
#statusCard .rule,
#statusCard #event {
display: none;
}
#reason { margin-bottom: 10px; }
.dock {
bottom: 10px;
flex-wrap: wrap;
justify-content: center;
width: calc(100vw - 20px);
}
.control-label { display: none; }
select, button {
height: 36px;
padding-left: 10px;
padding-right: 10px;
}
}
</style>
</head>
<body data-state="fail">
<div class="vignette"></div>
<header>
<div class="eyebrow">Binary Lab / Parity Demo</div>
<h1>Even parity</h1>
<p>One flipped bit changes the count from even to odd. The receiver catches it.</p>
</header>
<aside id="statusCard" class="panel" aria-live="polite">
<div class="status-head">
<span id="signal"></span>
<div class="status-title">
<small>RECEIVER</small>
<strong id="verdict">REJECTED</strong>
</div>
</div>
<p id="reason"></p>
<div class="section-label">RECEIVED FRAME</div>
<div id="sequence"></div>
<div class="metrics">
<div class="metric">
<span>DATA 1s</span>
<strong id="dataOnes">3</strong>
</div>
<div class="metric">
<span>PARITY P</span>
<strong id="parityValue">0</strong>
</div>
<div class="metric" id="totalMetric">
<span>TOTAL 1s</span>
<strong id="totalOnes">3</strong>
</div>
</div>
<div class="rule">
<strong>Stated rule:</strong> the 8 data bits plus parity bit P must contain an
<strong>even</strong> number of 1s.
</div>
<p id="event"></p>
</aside>
<div class="dock panel">
<label class="control-label" for="targetBit">Data bit</label>
<select id="targetBit" aria-label="Bit to flip">
<option value="0">B1</option>
<option value="1">B2</option>
<option value="2">B3</option>
<option value="3" selected>B4</option>
<option value="4">B5</option>
<option value="5">B6</option>
<option value="6">B7</option>
<option value="7">B8</option>
</select>
<button id="cleanButton" class="ghost" type="button">Reset clean</button>
<button id="flipButton" class="primary" type="button">Flip selected bit</button>
<button id="demoButton" type="button">Auto demo</button>
<span class="hint">Drag to orbit · Scroll to zoom · Click a cube</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -----------------------------------------------------------------------
// Renderer, scene, camera
// -----------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111c);
scene.fog = new THREE.Fog(0x07111c, 13, 29);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.domElement.id = "threeCanvas";
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xa7ddff, 0x17202c, 1.55);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.7);
keyLight.position.set(-3, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x38dfff, 24, 12, 2);
cyanLight.position.set(-4.5, 3.4, 3.2);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xffb84c, 18, 9, 2);
amberLight.position.set(4.8, 3, -1.8);
scene.add(amberLight);
// Custom camera controls: no external controls/addons.
const cameraTarget = new THREE.Vector3(0.15, 0.85, 0);
let cameraRadius = 13.8;
let cameraTheta = 0;
let cameraPhi = 1.01;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -----------------------------------------------------------------------
// Materials and reusable helpers
// -----------------------------------------------------------------------
const flatMaterial = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.62,
metalness: 0.2,
flatShading: true,
...options
});
const boardMaterial = flatMaterial(0x102b38, {
roughness: 0.48,
metalness: 0.42
});
const boardEdgeMaterial = flatMaterial(0x1b5362, {
roughness: 0.4,
metalness: 0.5
});
const traceMaterial = new THREE.MeshBasicMaterial({
color: 0x2ccbe3,
transparent: true,
opacity: 0.45
});
function canvasTexture(canvas) {
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
return texture;
}
function makeTextSprite(
text,
width = 2,
color = "#dff8ff",
fontSize = 58,
weight = 700
) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 160;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = color;
ctx.font = `${weight} ${fontSize}px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
const texture = canvasTexture(canvas);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * (160 / 768), 1);
sprite.renderOrder = 20;
return sprite;
}
function makeChangeSprite() {
const canvas = document.createElement("canvas");
canvas.width = 320;
canvas.height = 100;
const ctx = canvas.getContext("2d");
const texture = canvasTexture(canvas);
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sprite.scale.set(1.08, 0.338, 1);
sprite.visible = false;
sprite.renderOrder = 25;
sprite.userData.setText = (before, after) => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#ff617d";
ctx.font = "800 50px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(`${before} → ${after}`, canvas.width / 2, canvas.height / 2);
texture.needsUpdate = true;
};
return sprite;
}
function addBox(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;
}
// -----------------------------------------------------------------------
// Low-poly laboratory set
// -----------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.CircleGeometry(22, 32),
flatMaterial(0x091621, { roughness: 0.96, metalness: 0.02 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.015;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x174251, 0x102b38);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.3;
scene.add(grid);
addBox(15.5, 0.28, 3.7, boardMaterial, 0, 0.16, 0);
addBox(15.6, 0.08, 3.82, boardEdgeMaterial, 0, 0.035, 0);
// Decorative circuit traces beneath the bit cubes.
for (let i = 0; i < 8; i++) {
const x = -4.75 + i * 0.98;
addBox(0.62, 0.025, 0.055, traceMaterial, x + 0.47, 0.325, -0.72, false);
const node = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.035, 8),
traceMaterial
);
node.position.set(x, 0.34, -0.72);
scene.add(node);
}
addBox(1.35, 0.04, 0.08, traceMaterial, 4.5, 0.34, -0.7, false);
// Small faceted environment pieces.
const crystalData = [
[-8.0, -1.0, 0x1b8598, 0.8],
[8.1, -0.8, 0xb67637, 0.65],
[-7.7, 1.3, 0x294d78, 0.55]
];
for (const [x, z, color, scale] of crystalData) {
const crystal = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.4, 0),
flatMaterial(color, { roughness: 0.4, metalness: 0.48 })
);
crystal.position.set(x, 0.34, z);
crystal.scale.set(scale, scale * 1.5, scale);
crystal.rotation.set(0.3, x, -0.15);
crystal.castShadow = true;
scene.add(crystal);
}
const messageLabel = makeTextSprite("MESSAGE / 8 BITS", 2.7, "#76ddec", 52);
messageLabel.position.set(-1.35, 1.92, 0.05);
scene.add(messageLabel);
const parityLabel = makeTextSprite("EVEN P", 1.3, "#ffc857", 54);
parityLabel.position.set(3.35, 1.9, 0.05);
scene.add(parityLabel);
const receiverLabel = makeTextSprite("RX CHECKER", 1.75, "#9eecff", 53);
receiverLabel.position.set(5.68, 3.05, 0);
scene.add(receiverLabel);
// -----------------------------------------------------------------------
// Data bits and parity bit
// -----------------------------------------------------------------------
const bitVisuals = [];
const clickTargets = [];
function createBit(x, isParity = false) {
const group = new THREE.Group();
group.position.set(x, 0.95, 0);
scene.add(group);
const bodyMaterial = flatMaterial(
isParity ? 0xd59a28 : 0x24798b,
{
roughness: 0.32,
metalness: 0.42,
emissive: isParity ? 0x4a2600 : 0x062b34,
emissiveIntensity: 0.12
}
);
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 1.02, 0.7),
bodyMaterial
);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const capMaterial = flatMaterial(
isParity ? 0xffce64 : 0x48a9b8,
{
roughness: 0.3,
metalness: 0.55,
emissive: isParity ? 0x7a4a00 : 0x0a4b59,
emissiveIntensity: 0.18
}
);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.24, 0.07, 0.25),
capMaterial
);
cap.position.set(0, 0.545, 0.1);
cap.castShadow = true;
group.add(cap);
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 256;
labelCanvas.height = 256;
const labelTexture = canvasTexture(labelCanvas);
const label = new THREE.Mesh(
new THREE.PlaneGeometry(0.57, 0.76),
new THREE.MeshBasicMaterial({
map: labelTexture,
transparent: true,
depthWrite: false,
toneMapped: false
})
);
label.position.z = 0.356;
label.renderOrder = 8;
group.add(label);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.54, 0.025, 6, 32),
new THREE.MeshBasicMaterial({
color: 0xff617d,
transparent: true,
opacity: 0.9,
depthWrite: false,
toneMapped: false
})
);
halo.position.z = 0.37;
halo.visible = false;
halo.renderOrder = 9;
group.add(halo);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.14, 0.18, 6),
boardEdgeMaterial
);
stem.position.set(x, 0.39, 0);
stem.castShadow = true;
scene.add(stem);
const indexLabel = makeTextSprite(
isParity ? "P" : `B${bitVisuals.length + 1}`,
isParity ? 0.35 : 0.43,
isParity ? "#ffc857" : "#6f8c9e",
58
);
indexLabel.position.set(x, 0.37, 0.58);
scene.add(indexLabel);
const changeSprite = isParity ? null : makeChangeSprite();
if (changeSprite) {
changeSprite.position.set(x, 1.67, 0.15);
scene.add(changeSprite);
}
const visual = {
group,
body,
bodyMaterial,
capMaterial,
label,
labelCanvas,
labelTexture,
halo,
changeSprite,
isParity,
isCorrupt: false,
animating: false,
animT: 0,
baseY: 0.95
};
if (!isParity) {
body.userData.bitIndex = bitVisuals.length;
label.userData.bitIndex = bitVisuals.length;
clickTargets.push(body, label);
bitVisuals.push(visual);
}
return visual;
}
for (let i = 0; i < 8; i++) {
createBit(-4.75 + i * 0.98, false);
}
const parityVisual = createBit(3.35, true);
function paintBit(visual, digit, corrupt) {
const ctx = visual.labelCanvas.getContext("2d");
ctx.clearRect(0, 0, 256, 256);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = corrupt
? "#ff7189"
: visual.isParity
? "#ffe39a"
: "#e9fbff";
if (visual.isParity) {
ctx.font = "800 42px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText("P", 128, 72);
ctx.font = "800 132px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(String(digit), 128, 175);
} else {
ctx.font = "800 142px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(String(digit), 128, 137);
}
visual.labelTexture.needsUpdate = true;
visual.isCorrupt = corrupt;
visual.halo.visible = corrupt;
if (visual.isParity) {
visual.bodyMaterial.color.setHex(0xd59a28);
visual.bodyMaterial.emissive.setHex(0x4a2600);
visual.bodyMaterial.emissiveIntensity = 0.14;
visual.capMaterial.color.setHex(0xffce64);
return;
}
visual.bodyMaterial.color.setHex(corrupt ? 0xb9344e : 0x24798b);
visual.bodyMaterial.emissive.setHex(corrupt ? 0x7a1026 : 0x062b34);
visual.bodyMaterial.emissiveIntensity = corrupt ? 0.48 : 0.12;
visual.capMaterial.color.setHex(corrupt ? 0xff7189 : 0x48a9b8);
visual.capMaterial.emissive.setHex(corrupt ? 0x8f1930 : 0x0a4b59);
visual.capMaterial.emissiveIntensity = corrupt ? 0.65 : 0.18;
visual.animating = true;
visual.animT = 0;
visual.group.rotation.z = 0;
}
// -----------------------------------------------------------------------
// Receiving checker robot
// -----------------------------------------------------------------------
const receiver = new THREE.Group();
receiver.position.x = 5.68;
scene.add(receiver);
const robotMaterial = flatMaterial(0x456a7d, {
roughness: 0.48,
metalness: 0.38
});
const robotDarkMaterial = flatMaterial(0x172b39, {
roughness: 0.55,
metalness: 0.5
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.86, 1.0, 0.3, 6),
robotDarkMaterial
);
base.position.y = 0.48;
base.castShadow = true;
base.receiveShadow = true;
receiver.add(base);
const torso = new THREE.Mesh(
new THREE.BoxGeometry(0.9, 1.1, 0.76),
robotMaterial
);
torso.position.y = 1.15;
torso.castShadow = true;
receiver.add(torso);
const head = new THREE.Mesh(
new THREE.BoxGeometry(1.18, 0.66, 0.88),
robotMaterial
);
head.position.y = 2.0;
head.castShadow = true;
receiver.add(head);
const eyeMaterial = flatMaterial(0x4df2a5, {
roughness: 0.24,
metalness: 0.25,
emissive: 0x28d982,
emissiveIntensity: 0.7
});
const eye = new THREE.Mesh(
new THREE.BoxGeometry(0.76, 0.21, 0.06),
eyeMaterial
);
eye.position.set(0, 2.04, 0.47);
receiver.add(eye);
const antenna = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.05, 0.48, 6),
robotDarkMaterial
);
antenna.position.y = 2.55;
receiver.add(antenna);
const beaconMaterial = flatMaterial(0x4df2a5, {
emissive: 0x28d982,
emissiveIntensity: 0.8
});
const beacon = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.12, 0),
beaconMaterial
);
beacon.position.y = 2.82;
receiver.add(beacon);
for (const side of [-1, 1]) {
const ear = new THREE.Mesh(
new THREE.CylinderGeometry(0.15, 0.15, 0.18, 6),
robotDarkMaterial
);
ear.rotation.z = Math.PI / 2;
ear.position.set(side * 0.68, 2.0, 0);
ear.castShadow = true;
receiver.add(ear);
}
const statusCanvas = document.createElement("canvas");
statusCanvas.width = 512;
statusCanvas.height = 220;
const statusTexture = canvasTexture(statusCanvas);
const statusPanel = new THREE.Mesh(
new THREE.PlaneGeometry(1.1, 0.47),
new THREE.MeshBasicMaterial({
map: statusTexture,
toneMapped: false
})
);
statusPanel.position.set(0, 1.12, 0.39);
statusPanel.renderOrder = 8;
receiver.add(statusPanel);
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function drawReceiverStatus(pass, total) {
const ctx = statusCanvas.getContext("2d");
ctx.clearRect(0, 0, 512, 220);
roundedRect(ctx, 8, 8, 496, 204, 24);
ctx.fillStyle = pass ? "rgba(19, 88, 65, 0.98)" : "rgba(105, 26, 48, 0.98)";
ctx.fill();
ctx.strokeStyle = pass ? "#4df2a5" : "#ff617d";
ctx.lineWidth = 8;
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = pass ? "#8affcb" : "#ff9aae";
ctx.font = "800 76px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(pass ? "PASS" : "FAIL", 256, 91);
ctx.fillStyle = "#e7f8ff";
ctx.font = "700 31px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(`${total} ONES / ${pass ? "EVEN" : "ODD"}`, 256, 158);
statusTexture.needsUpdate = true;
}
// -----------------------------------------------------------------------
// Parity state
// -----------------------------------------------------------------------
const originalBits = [1, 0, 1, 1, 0, 0, 1, 0];
function addEvenParity(bits) {
const ones = bits.reduce((sum, bit) => sum + bit, 0);
return ones % 2 === 0 ? 0 : 1;
}
const parityBit = addEvenParity(originalBits);
let currentBits = [...originalBits];
// Start in the failure state so the one-bit violation is immediately visible.
let corruptedIndex = 3;
currentBits[3] = originalBits[3] ^ 1;
const targetSelect = document.getElementById("targetBit");
const cleanButton = document.getElementById("cleanButton");
const flipButton = document.getElementById("flipButton");
const demoButton = document.getElementById("demoButton");
const sequenceElement = document.getElementById("sequence");
const verdictElement = document.getElementById("verdict");
const reasonElement = document.getElementById("reason");
const dataOnesElement = document.getElementById("dataOnes");
const parityValueElement = document.getElementById("parityValue");
const totalOnesElement = document.getElementById("totalOnes");
const totalMetricElement = document.getElementById("totalMetric");
const eventElement = document.getElementById("event");
function updateVisuals() {
bitVisuals.forEach((visual, index) => {
const corrupt = index === corruptedIndex;
paintBit(visual, currentBits[index], corrupt);
if (visual.changeSprite) {
visual.changeSprite.visible = corrupt;
if (corrupt) {
visual.changeSprite.userData.setText(
originalBits[index],
currentBits[index]
);
}
}
});
paintBit(parityVisual, parityBit, false);
}
function updateUI() {
const dataOnes = currentBits.reduce((sum, bit) => sum + bit, 0);
const totalOnes = dataOnes + parityBit;
const pass = totalOnes % 2 === 0;
document.body.dataset.state = pass ? "pass" : "fail";
verdictElement.textContent = pass ? "ACCEPTED" : "REJECTED";
verdictElement.setAttribute("aria-label", verdictElement.textContent);
reasonElement.textContent = pass
? "The 9-bit received frame contains an even number of 1s."
: "The 9-bit received frame contains an odd number of 1s. Even-parity check failed.";
sequenceElement.replaceChildren();
currentBits.forEach((bit, index) => {
const chip = document.createElement("span");
chip.className = "bit-chip";
chip.textContent = String(bit);
chip.title = `B${index + 1}`;
if (index === corruptedIndex) chip.classList.add("bad");
sequenceElement.appendChild(chip);
});
const separator = document.createElement("span");
separator.className = "separator";
separator.textContent = "|";
sequenceElement.appendChild(separator);
const parityChip = document.createElement("span");
parityChip.className = "bit-chip parity";
parityChip.textContent = String(parityBit);
parityChip.title = "Even parity bit";
sequenceElement.appendChild(parityChip);
dataOnesElement.textContent = String(dataOnes);
parityValueElement.textContent = String(parityBit);
totalOnesElement.textContent = String(totalOnes);
totalMetricElement.className = pass ? "metric good" : "metric bad";
if (corruptedIndex === null) {
eventElement.textContent =
`Original 10110010 has 4 ones, so P = 0. The clean frame contains ` +
`${totalOnes} ones and is accepted.`;
} else {
const index = corruptedIndex;
eventElement.textContent =
`B${index + 1} changed from ${originalBits[index]} to ${currentBits[index]}. ` +
`P remains ${parityBit}, so the altered frame contains ${totalOnes} ones — odd, not even.`;
}
const selected = Number(targetSelect.value);
flipButton.textContent =
selected === corruptedIndex ? "Restore selected bit" : "Flip selected bit";
eyeMaterial.color.setHex(pass ? 0x4df2a5 : 0xff617d);
eyeMaterial.emissive.setHex(pass ? 0x28d982 : 0xff294f);
beaconMaterial.color.setHex(pass ? 0x4df2a5 : 0xff617d);
beaconMaterial.emissive.setHex(pass ? 0x28d982 : 0xff294f);
drawReceiverStatus(pass, totalOnes);
}
// -----------------------------------------------------------------------
// Scanning animation
// -----------------------------------------------------------------------
const scanMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 1.24, 0.86),
new THREE.MeshBasicMaterial({
color: 0x6ff3ff,
transparent: true,
opacity: 0,
depthWrite: false,
blending: THREE.AdditiveBlending,
toneMapped: false
})
);
scanMesh.position.set(-5.2, 0.98, 0.05);
scanMesh.visible = false;
scene.add(scanMesh);
let scanProgress = -1;
function startScan() {
scanProgress = 0;
scanMesh.visible = true;
}
function cleanFrame(animate = true) {
currentBits = [...originalBits];
corruptedIndex = null;
updateVisuals();
updateUI();
if (animate) startScan();
}
function flipBit(index) {
if (index < 0 || index > 7) return;
if (corruptedIndex === index) {
currentBits[index] = originalBits[index];
corruptedIndex = null;
} else {
if (corruptedIndex !== null) {
currentBits[corruptedIndex] = originalBits[corruptedIndex];
}
currentBits[index] = originalBits[index] ^ 1;
corruptedIndex = index;
}
updateVisuals();
updateUI();
startScan();
}
cleanButton.addEventListener("click", () => cleanFrame());
flipButton.addEventListener("click", () => {
flipBit(Number(targetSelect.value));
});
targetSelect.addEventListener("change", updateUI);
let demoTimer;
demoButton.addEventListener("click", () => {
clearTimeout(demoTimer);
cleanFrame(false);
demoTimer = setTimeout(() => {
targetSelect.value = "3";
updateUI();
flipBit(3);
}, 650);
});
// Clicking a data cube flips that bit.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickBit(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);
const hit = raycaster.intersectObjects(clickTargets, false)[0];
if (hit && hit.object.userData.bitIndex !== undefined) {
const index = hit.object.userData.bitIndex;
targetSelect.value = String(index);
updateUI();
flipBit(index);
}
}
// Direct pointer camera controls.
let pointerDown = null;
let pointerTravel = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = { x: event.clientX, y: event.clientY };
pointerTravel = 0;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
pointerTravel += Math.abs(dx) + Math.abs(dy);
cameraTheta -= dx * 0.005;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + dy * 0.004,
0.52,
1.34
);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerDown && pointerTravel < 6) pickBit(event);
pointerDown = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.009,
9.5,
19
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// -----------------------------------------------------------------------
// Resize and render loop
// -----------------------------------------------------------------------
function resize() {
const width = innerWidth;
const height = innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = camera.aspect < 0.8 ? 53 : 42;
camera.updateProjectionMatrix();
}
addEventListener("resize", resize);
resize();
updateVisuals();
updateUI();
let previousTime = performance.now();
renderer.setAnimationLoop((now) => {
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
const time = now / 1000;
for (const visual of bitVisuals) {
if (visual.animating) {
visual.animT += dt * 2.35;
const t = Math.min(visual.animT, 1);
const decay = 1 - t;
visual.group.rotation.z =
Math.sin(t * Math.PI * 4) * decay * 0.13;
visual.group.position.y =
visual.baseY + Math.sin(t * Math.PI) * 0.18;
visual.halo.scale.setScalar(1 + Math.sin(t * Math.PI) * 0.14);
if (t >= 1) {
visual.animating = false;
visual.group.rotation.z = 0;
visual.group.position.y = visual.baseY;
visual.halo.scale.setScalar(1);
}
}
if (visual.isCorrupt) {
visual.bodyMaterial.emissiveIntensity =
0.42 + Math.sin(time * 4.2) * 0.12;
visual.halo.rotation.z += dt * 0.65;
}
}
if (scanProgress >= 0) {
scanProgress += dt / 1.35;
const p = Math.min(scanProgress, 1);
scanMesh.position.x = THREE.MathUtils.lerp(-5.2, 6.45, p);
scanMesh.material.opacity = Math.sin(p * Math.PI) * 0.72;
eyeMaterial.emissiveIntensity = 1.1 + Math.sin(p * Math.PI * 5) * 0.35;
beaconMaterial.emissiveIntensity = 0.9 + Math.sin(p * Math.PI * 8) * 0.4;
if (p >= 1) {
scanProgress = -1;
scanMesh.visible = false;
eyeMaterial.emissiveIntensity = 0.7;
beaconMaterial.emissiveIntensity = 0.8;
}
}
renderer.render(scene, camera);
});
</script>
</body>
</html>
```