Prompt specification
Build or customize the Three.js 3D scene "Equivalent logic circuit comparison" 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: Equivalent logic circuit comparison
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/equivalent-logic-circuit-comparison-x1854
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring Boolean logic gates and compound rules. Place two small logically equivalent expressions side by side and test all input combinations; show matching outputs while revealing their different internal arrangements. Stylized solid forms.
Working Three.js Starter (equivalent-logic-circuit-comparison-x1854):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Boolean Equivalence Lab</title>
<style>
:root {
--ink: #152238;
--muted: #65748a;
--panel: rgba(250, 253, 255, 0.9);
--line: rgba(35, 52, 78, 0.12);
--green: #19b978;
--green-dark: #087a55;
--amber: #f3b83f;
--purple: #765cff;
--cyan: #18a99d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfe8ec;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 28px;
width: min(360px, calc(100vw - 56px));
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: #50657c;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 3px;
border-radius: 3px;
background: var(--purple);
content: "";
}
h1 {
margin: 0;
font-size: clamp(24px, 2.4vw, 38px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.brand p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.formula-row {
position: absolute;
top: 26px;
left: 50%;
display: flex;
gap: 12px;
transform: translateX(-50%);
}
.formula {
min-width: 276px;
padding: 12px 15px 13px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 16px;
background: rgba(249, 252, 254, 0.82);
box-shadow: 0 12px 32px rgba(45, 67, 90, 0.09);
backdrop-filter: blur(14px);
}
.formula small {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.formula b {
display: block;
font-size: 15px;
letter-spacing: -0.015em;
}
.formula.left b {
color: #6149db;
}
.formula.right b {
color: #087f78;
}
.output-panel {
position: absolute;
top: 24px;
right: 28px;
width: 224px;
padding: 14px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 14px 34px rgba(43, 65, 88, 0.11);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.output-panel header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.output-panel header span:first-child {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.match {
padding: 4px 7px;
border-radius: 999px;
background: #dff8ec;
color: var(--green-dark);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
}
.outputs {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.output {
padding: 10px;
border-radius: 12px;
background: #edf2f6;
text-align: center;
transition: 180ms ease;
}
.output span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.output strong {
display: block;
margin-top: 2px;
font-size: 27px;
line-height: 1;
color: #526275;
}
.output.on {
background: #dff8ec;
box-shadow: inset 0 0 0 1px rgba(25, 185, 120, 0.18);
}
.output.on strong {
color: var(--green-dark);
}
.dock {
position: absolute;
bottom: 18px;
left: 50%;
width: min(1160px, calc(100vw - 36px));
padding: 13px 14px 12px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 21px;
background: rgba(249, 252, 254, 0.91);
box-shadow: 0 18px 48px rgba(40, 61, 82, 0.17);
backdrop-filter: blur(16px);
transform: translateX(-50%);
pointer-events: auto;
}
.control-row {
display: flex;
align-items: center;
gap: 10px;
min-height: 42px;
}
.control-label {
margin-right: 2px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.toggle {
width: 42px;
height: 34px;
border: 1px solid #d7e0e8;
border-radius: 11px;
background: #eef3f7;
color: #64748b;
cursor: pointer;
font-weight: 900;
transition: 160ms ease;
}
.toggle:hover {
transform: translateY(-1px);
border-color: #aebdca;
}
.toggle.on {
border-color: #f0b82f;
background: #ffd76a;
color: #624700;
box-shadow: 0 5px 12px rgba(243, 184, 63, 0.28);
}
.action {
height: 34px;
padding: 0 14px;
border: 0;
border-radius: 11px;
cursor: pointer;
font-size: 12px;
font-weight: 800;
transition: 160ms ease;
}
.action.primary {
margin-left: 4px;
background: var(--ink);
color: white;
box-shadow: 0 6px 14px rgba(21, 34, 56, 0.2);
}
.action.secondary {
background: #e8eef4;
color: #35465c;
}
.action:hover {
transform: translateY(-1px);
filter: brightness(1.04);
}
#message {
flex: 1;
min-width: 0;
overflow: hidden;
color: var(--muted);
font-size: 11px;
text-align: right;
text-overflow: ellipsis;
white-space: nowrap;
}
#message strong {
color: var(--green-dark);
}
.truth-wrap {
margin-top: 10px;
overflow-x: auto;
scrollbar-width: thin;
}
.truth-grid {
display: grid;
grid-template-columns: repeat(8, minmax(94px, 1fr));
gap: 6px;
min-width: 790px;
}
.truth-cell {
position: relative;
display: flex;
align-items: center;
justify-content: space-between;
min-height: 40px;
padding: 7px 9px;
border: 1px solid #dce4eb;
border-radius: 11px;
background: #f4f7fa;
color: #536478;
cursor: pointer;
transition: 150ms ease;
}
.truth-cell:hover {
border-color: #aebdca;
transform: translateY(-1px);
}
.truth-cell.selected {
border-color: #735bff;
background: #eeeaff;
box-shadow: 0 5px 12px rgba(118, 92, 255, 0.15);
}
.bits {
display: flex;
gap: 4px;
}
.bits i {
display: grid;
width: 19px;
height: 22px;
place-items: center;
border-radius: 6px;
background: #e6ecf2;
color: #64748b;
font-size: 9px;
font-style: normal;
font-weight: 900;
}
.bits i.one {
background: #ffd76a;
color: #624700;
}
.truth-result {
min-width: 28px;
color: var(--green-dark);
font-size: 14px;
font-weight: 950;
text-align: right;
}
.camera-hint {
position: absolute;
right: 28px;
bottom: 184px;
padding: 7px 10px;
border-radius: 9px;
background: rgba(248, 251, 253, 0.64);
color: #63758a;
font-size: 10px;
letter-spacing: 0.03em;
backdrop-filter: blur(8px);
}
@media (max-width: 1120px) {
.formula-row {
top: 128px;
}
.brand {
width: 310px;
}
}
@media (max-width: 720px) {
.brand {
top: 17px;
left: 18px;
}
.brand p {
display: none;
}
h1 {
font-size: 25px;
}
.output-panel {
top: 17px;
right: 16px;
width: 175px;
padding: 10px;
}
.output-panel header span:first-child {
display: none;
}
.formula-row {
top: 104px;
width: calc(100vw - 28px);
gap: 7px;
}
.formula {
min-width: 0;
flex: 1;
padding: 10px;
}
.formula b {
font-size: 11px;
}
.dock {
bottom: 9px;
width: calc(100vw - 18px);
padding: 10px;
}
.control-row {
flex-wrap: wrap;
}
#message {
order: 5;
flex-basis: 100%;
text-align: left;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<header class="brand">
<div class="eyebrow">Boolean Logic Lab</div>
<h1>One truth.<br>Two architectures.</h1>
<p>
A factored expression and its distributed equivalent produce the same
result for every possible input.
</p>
</header>
<div class="formula-row">
<div class="formula left">
<small>01 · Factored circuit</small>
<b>A AND (B OR C)</b>
</div>
<div class="formula right">
<small>02 · Distributed circuit</small>
<b>(A AND B) OR (A AND C)</b>
</div>
</div>
<section class="output-panel" aria-label="Circuit outputs">
<header>
<span>Live outputs</span>
<span class="match">MATCH</span>
</header>
<div class="outputs">
<div class="output" id="leftOutput">
<span>Form 01</span>
<strong id="leftValue">0</strong>
</div>
<div class="output" id="rightOutput">
<span>Form 02</span>
<strong id="rightValue">0</strong>
</div>
</div>
</section>
<div class="camera-hint">Drag to orbit · scroll to zoom · click an input puck</div>
<section class="dock" aria-label="Boolean circuit controls">
<div class="control-row">
<span class="control-label">Inputs</span>
<button class="toggle" data-input="A" aria-label="Toggle input A">A</button>
<button class="toggle" data-input="B" aria-label="Toggle input B">B</button>
<button class="toggle" data-input="C" aria-label="Toggle input C">C</button>
<button class="action primary" id="runButton">Run all 8</button>
<button class="action secondary" id="stepButton">Next</button>
<div id="message" aria-live="polite">Preparing truth-table test…</div>
</div>
<div class="truth-wrap">
<div class="truth-grid" id="truthGrid"></div>
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe8ec);
scene.fog = new THREE.Fog(0xdfe8ec, 34, 78);
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.05;
document.getElementById("scene").appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
150
);
// Camera controls are implemented directly, without Three.js addons.
const cameraState = {
theta: 0,
phi: 0.73,
radius: 19.5,
target: new THREE.Vector3(0, 0.15, 0)
};
function updateCamera() {
const fit = Math.max(1, 1.48 / camera.aspect);
const radius = cameraState.radius * fit;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + radius * Math.cos(cameraState.phi),
cameraState.target.z +
radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
let dragging = false;
let dragX = 0;
let dragY = 0;
let moved = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
moved = 0;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
moved += Math.abs(dx) + Math.abs(dy);
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.34,
1.24
);
dragX = event.clientX;
dragY = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
renderer.domElement.releasePointerCapture(event.pointerId);
if (moved < 7) {
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(inputHitMeshes, false)[0];
if (hit) toggleInput(hit.object.userData.input);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
13,
28
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.theta = 0;
cameraState.phi = 0.73;
cameraState.radius = 19.5;
updateCamera();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const inputHitMeshes = [];
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
}
window.addEventListener("resize", resize);
// Lighting
scene.add(new THREE.HemisphereLight(0xffffff, 0x65788b, 2.2));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4, 13, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -18;
keyLight.shadow.camera.right = 18;
keyLight.shadow.camera.top = 14;
keyLight.shadow.camera.bottom = -14;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x94b7ff, 1.15);
fillLight.position.set(-10, 7, -7);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xffffff, 22, 30, 2);
rimLight.position.set(0, 9, -8);
scene.add(rimLight);
// Ground and subtle center divider.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.MeshStandardMaterial({
color: 0xd7e2e7,
roughness: 0.94,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.38;
ground.receiveShadow = true;
scene.add(ground);
const divider = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.025, 9.5),
new THREE.MeshStandardMaterial({
color: 0xaebdc7,
roughness: 0.85
})
);
divider.position.set(0, -0.355, 0);
scene.add(divider);
function roundedRectShape(width, depth, radius) {
const x = -width / 2;
const y = -depth / 2;
const r = Math.min(radius, width / 2, depth / 2);
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + depth - r);
shape.quadraticCurveTo(
x + width,
y + depth,
x + width - r,
y + depth
);
shape.lineTo(x + r, y + depth);
shape.quadraticCurveTo(x, y + depth, x, y + depth - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
return shape;
}
function extrudedMesh(width, depth, height, radius, material) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectShape(width, depth, radius),
{
depth: height,
steps: 1,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.045,
bevelThickness: 0.04,
curveSegments: 10
}
);
geometry.rotateX(-Math.PI / 2);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
class Label3D {
constructor(text, width, height, color = "#26364b") {
this.canvas = document.createElement("canvas");
this.canvas.width = 512;
this.canvas.height = 160;
this.context = this.canvas.getContext("2d");
this.texture = new THREE.CanvasTexture(this.canvas);
this.texture.colorSpace = THREE.SRGBColorSpace;
this.texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
this.material = new THREE.MeshBasicMaterial({
map: this.texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
this.mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
this.material
);
this.mesh.rotation.x = -Math.PI / 2;
this.mesh.renderOrder = 3;
this.setText(text, color);
}
setText(text, color = "#26364b") {
const ctx = this.context;
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
ctx.fillStyle = color;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "800 62px Inter, Arial, sans-serif";
ctx.fillText(text, 256, 82);
this.texture.needsUpdate = true;
}
}
class Wire {
constructor(parent, points) {
this.points = points.map(
(p) => new THREE.Vector3(p[0], p[1], p[2])
);
this.material = new THREE.MeshStandardMaterial({
color: 0x8b9aaa,
roughness: 0.4,
metalness: 0.04,
emissive: 0x000000,
emissiveIntensity: 0
});
this.segments = [];
this.totalLength = 0;
const up = new THREE.Vector3(0, 1, 0);
for (let i = 0; i < this.points.length - 1; i++) {
const a = this.points[i];
const b = this.points[i + 1];
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
0.055,
0.055,
length,
12
);
const segment = new THREE.Mesh(geometry, this.material);
segment.position.copy(a).add(b).multiplyScalar(0.5);
segment.quaternion.setFromUnitVectors(
up,
direction.clone().normalize()
);
segment.castShadow = true;
parent.add(segment);
this.segments.push({ a, b, length });
this.totalLength += length;
}
const jointGeometry = new THREE.SphereGeometry(0.075, 14, 10);
for (let i = 1; i < this.points.length - 1; i++) {
const joint = new THREE.Mesh(jointGeometry, this.material);
joint.position.copy(this.points[i]);
joint.castShadow = true;
parent.add(joint);
}
this.active = false;
this.particles = [];
for (let i = 0; i < 2; i++) {
const particle = new THREE.Mesh(
new THREE.SphereGeometry(0.095, 14, 10),
new THREE.MeshBasicMaterial({
color: 0xd6ffea,
transparent: true,
opacity: 0.95
})
);
particle.visible = false;
particle.renderOrder = 4;
parent.add(particle);
this.particles.push(particle);
}
}
setSignal(active) {
this.active = active;
this.material.color.set(active ? 0x20bb7e : 0x8b9aaa);
this.material.emissive.set(active ? 0x0b7048 : 0x000000);
this.material.emissiveIntensity = active ? 0.38 : 0;
for (const particle of this.particles) {
particle.visible = active;
}
}
update(time) {
if (!this.active) return;
this.particles.forEach((particle, index) => {
let distance =
(time * 2.1 + index * this.totalLength * 0.5) %
this.totalLength;
for (const segment of this.segments) {
if (distance <= segment.length) {
particle.position
.copy(segment.a)
.lerp(segment.b, distance / segment.length);
break;
}
distance -= segment.length;
}
});
}
}
function makeBoard(centerX, formNumber) {
const group = new THREE.Group();
group.position.x = centerX;
scene.add(group);
const base = extrudedMesh(
7.8,
7.8,
0.28,
0.36,
new THREE.MeshStandardMaterial({
color: 0x273750,
roughness: 0.68,
metalness: 0.05
})
);
base.position.y = -0.3;
group.add(base);
const top = extrudedMesh(
7.62,
7.62,
0.24,
0.32,
new THREE.MeshStandardMaterial({
color: 0xf5f8fa,
roughness: 0.78,
metalness: 0
})
);
top.position.y = 0.02;
group.add(top);
const tag = new Label3D(
formNumber === 1 ? "FORM 01" : "FORM 02",
1.55,
0.4,
"#6e7f91"
);
tag.mesh.position.set(-2.65, 0.325, 3.22);
group.add(tag.mesh);
const boltGeometry = new THREE.CylinderGeometry(0.07, 0.07, 0.045, 16);
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0xb6c2cc,
roughness: 0.35,
metalness: 0.45
});
for (const x of [-3.42, 3.42]) {
for (const z of [-3.42, 3.42]) {
const bolt = new THREE.Mesh(boltGeometry, boltMaterial);
bolt.position.set(x, 0.325, z);
group.add(bolt);
}
}
return group;
}
function makeInput(group, key, x, z) {
const material = new THREE.MeshStandardMaterial({
color: 0xcbd5df,
roughness: 0.52,
metalness: 0.05,
emissive: 0x000000,
emissiveIntensity: 0
});
const puck = new THREE.Mesh(
new THREE.CylinderGeometry(0.46, 0.49, 0.24, 48),
material
);
puck.position.set(x, 0.44, z);
puck.castShadow = true;
puck.receiveShadow = true;
puck.userData.input = key;
group.add(puck);
inputHitMeshes.push(puck);
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.065, 0.19, 14),
new THREE.MeshStandardMaterial({
color: 0x65758a,
roughness: 0.4
})
);
post.position.set(x, 0.64, z);
post.castShadow = true;
group.add(post);
const label = new Label3D(key, 0.52, 0.52, "#24354b");
label.mesh.position.set(x, 0.575, z);
group.add(label.mesh);
return {
key,
mesh: puck,
material,
setValue(value) {
material.color.set(value ? 0xf6bd3c : 0xcbd5df);
material.emissive.set(value ? 0x7a4d00 : 0x000000);
material.emissiveIntensity = value ? 0.18 : 0;
label.setText(key, value ? "#493000" : "#617286");
}
};
}
class Gate {
constructor(group, type, x, z) {
this.type = type;
this.group = new THREE.Group();
this.group.position.set(x, 0, z);
group.add(this.group);
this.offColor = new THREE.Color(
type === "AND" ? 0x6857a6 : 0x2d7775
);
this.onColor = new THREE.Color(
type === "AND" ? 0x8568ff : 0x20c1af
);
this.material = new THREE.MeshStandardMaterial({
color: this.offColor,
roughness: 0.47,
metalness: 0.06,
emissive: this.offColor,
emissiveIntensity: 0
});
this.block = extrudedMesh(
type === "AND" ? 1.78 : 1.86,
type === "AND" ? 1.05 : 1.12,
0.3,
type === "AND" ? 0.2 : 0.48,
this.material
);
this.block.position.y = 0.31;
this.group.add(this.block);
this.label = new Label3D(`${type} 0`, 1.4, 0.4, "#617286");
this.label.mesh.position.set(0, 0.705, 0);
this.group.add(this.label.mesh);
this.beaconMaterial = new THREE.MeshStandardMaterial({
color: 0x29384d,
roughness: 0.25,
emissive: 0x000000,
emissiveIntensity: 0
});
this.beacon = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 18, 12),
this.beaconMaterial
);
this.beacon.position.set(
type === "AND" ? 0.7 : 0.75,
0.79,
-0.34
);
this.beacon.castShadow = true;
this.group.add(this.beacon);
}
setValue(value) {
this.material.color.copy(value ? this.onColor : this.offColor);
this.material.emissive.copy(this.onColor);
this.material.emissiveIntensity = value ? 0.11 : 0;
this.beaconMaterial.color.set(value ? 0xc9ffe0 : 0x29384d);
this.beaconMaterial.emissive.set(value ? 0x36d993 : 0x000000);
this.beaconMaterial.emissiveIntensity = value ? 0.7 : 0;
this.label.setText(
`${this.type} ${value ? 1 : 0}`,
value
? this.type === "AND"
? "#5d42d4"
: "#087b73"
: "#617286"
);
}
}
function makeOutput(group, x, z) {
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.52, 0.61, 0.22, 40),
new THREE.MeshStandardMaterial({
color: 0x273750,
roughness: 0.58
})
);
base.position.set(x, 0.44, z);
base.castShadow = true;
group.add(base);
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.08, 0.1, 0.48, 18),
new THREE.MeshStandardMaterial({
color: 0x61738a,
roughness: 0.4,
metalness: 0.1
})
);
post.position.set(x, 0.78, z);
post.castShadow = true;
group.add(post);
const bulbMaterial = new THREE.MeshStandardMaterial({
color: 0x334259,
roughness: 0.22,
emissive: 0x000000,
emissiveIntensity: 0
});
const bulb = new THREE.Mesh(
new THREE.SphereGeometry(0.27, 28, 18),
bulbMaterial
);
bulb.position.set(x, 1.1, z);
bulb.castShadow = true;
group.add(bulb);
const label = new Label3D("OUT 0", 1.4, 0.42, "#617286");
label.mesh.position.set(x, 0.325, z + 0.78);
group.add(label.mesh);
return {
bulbMaterial,
label,
setValue(value) {
bulbMaterial.color.set(value ? 0x65e8aa : 0x334259);
bulbMaterial.emissive.set(value ? 0x18b879 : 0x000000);
bulbMaterial.emissiveIntensity = value ? 0.8 : 0;
label.setText(
`OUT ${value ? 1 : 0}`,
value ? "#087a55" : "#617286"
);
}
};
}
const W = 0.73;
// Factored circuit: A AND (B OR C)
const leftGroup = makeBoard(-4.65, 1);
const left = {
group: leftGroup,
inputs: {
A: makeInput(leftGroup, "A", -2.5, -2.45),
B: makeInput(leftGroup, "B", 0, -2.45),
C: makeInput(leftGroup, "C", 2.5, -2.45)
},
gates: {
or: new Gate(leftGroup, "OR", 1.65, -0.2),
and: new Gate(leftGroup, "AND", -1.35, 0.65)
},
wires: {}
};
left.wires.a = new Wire(leftGroup, [
[-2.5, W, -2.45],
[-2.5, W, -0.7],
[-1.75, W, -0.7],
[-1.75, W, 0.125]
]);
left.wires.b = new Wire(leftGroup, [
[0, W, -2.45],
[0, W, -1.25],
[1.25, W, -1.25],
[1.25, W, -0.725]
]);
left.wires.c = new Wire(leftGroup, [
[2.5, W, -2.45],
[2.5, W, -1.25],
[2.05, W, -1.25],
[2.05, W, -0.725]
]);
left.wires.orToAnd = new Wire(leftGroup, [
[1.65, W, 0.325],
[1.65, W, 0.65],
[-0.95, W, 0.65],
[-0.95, W, 0.125]
]);
left.wires.result = new Wire(leftGroup, [
[-1.35, W, 1.175],
[-1.35, W, 2.7]
]);
left.output = makeOutput(leftGroup, -1.35, 2.7);
// Distributed circuit: (A AND B) OR (A AND C)
const rightGroup = makeBoard(4.65, 2);
const right = {
group: rightGroup,
inputs: {
A: makeInput(rightGroup, "A", -2.5, -2.45),
B: makeInput(rightGroup, "B", 0, -2.45),
C: makeInput(rightGroup, "C", 2.5, -2.45)
},
gates: {
and1: new Gate(rightGroup, "AND", -1.65, -0.15),
and2: new Gate(rightGroup, "AND", 1.65, -0.15),
or: new Gate(rightGroup, "OR", 0, 1.45)
},
wires: {}
};
right.wires.a1 = new Wire(rightGroup, [
[-2.5, W, -2.45],
[-2.5, W, -1.35],
[-2.05, W, -1.35],
[-2.05, W, -0.65]
]);
right.wires.b = new Wire(rightGroup, [
[0, W, -2.45],
[0, W, -0.95],
[-1.25, W, -0.95],
[-1.25, W, -0.65]
]);
right.wires.c = new Wire(rightGroup, [
[2.5, W, -2.45],
[2.5, W, -0.95],
[2.05, W, -0.95],
[2.05, W, -0.65]
]);
// A takes a rear branch to the second AND gate, exposing the fan-out.
right.wires.a2 = new Wire(rightGroup, [
[-2.5, W, -2.45],
[-2.5, W, -3.02],
[1.25, W, -3.02],
[1.25, W, -0.65]
]);
right.wires.and1ToOr = new Wire(rightGroup, [
[-1.65, W, 0.35],
[-1.65, W, 0.65],
[-0.4, W, 0.65],
[-0.4, W, 0.95]
]);
right.wires.and2ToOr = new Wire(rightGroup, [
[1.65, W, 0.35],
[1.65, W, 0.65],
[0.4, W, 0.65],
[0.4, W, 0.95]
]);
right.wires.result = new Wire(rightGroup, [
[0, W, 1.95],
[0, W, 2.7]
]);
right.output = makeOutput(rightGroup, 0, 2.7);
const combinations = [
{ A: 0, B: 0, C: 0 },
{ A: 0, B: 0, C: 1 },
{ A: 0, B: 1, C: 0 },
{ A: 0, B: 1, C: 1 },
{ A: 1, B: 0, C: 0 },
{ A: 1, B: 0, C: 1 },
{ A: 1, B: 1, C: 0 },
{ A: 1, B: 1, C: 1 }
];
const state = { A: 0, B: 0, C: 0 };
let selectedIndex = 0;
let autoRunning = false;
let autoTimer = null;
const toggleButtons = {
A: document.querySelector('[data-input="A"]'),
B: document.querySelector('[data-input="B"]'),
C: document.querySelector('[data-input="C"]')
};
const leftValue = document.getElementById("leftValue");
const rightValue = document.getElementById("rightValue");
const leftOutput = document.getElementById("leftOutput");
const rightOutput = document.getElementById("rightOutput");
const message = document.getElementById("message");
const runButton = document.getElementById("runButton");
const stepButton = document.getElementById("stepButton");
const truthGrid = document.getElementById("truthGrid");
function evaluate(values) {
const leftOr = values.B || values.C;
const leftAnd = values.A && leftOr;
const rightAnd1 = values.A && values.B;
const rightAnd2 = values.A && values.C;
const rightOr = rightAnd1 || rightAnd2;
return {
leftOr,
leftAnd,
rightAnd1,
rightAnd2,
rightOr
};
}
function updateScene(result) {
const { A, B, C } = state;
left.inputs.A.setValue(A);
left.inputs.B.setValue(B);
left.inputs.C.setValue(C);
left.gates.or.setValue(result.leftOr);
left.gates.and.setValue(result.leftAnd);
left.wires.a.setSignal(A);
left.wires.b.setSignal(B);
left.wires.c.setSignal(C);
left.wires.orToAnd.setSignal(result.leftOr);
left.wires.result.setSignal(result.leftAnd);
left.output.setValue(result.leftAnd);
right.inputs.A.setValue(A);
right.inputs.B.setValue(B);
right.inputs.C.setValue(C);
right.gates.and1.setValue(result.rightAnd1);
right.gates.and2.setValue(result.rightAnd2);
right.gates.or.setValue(result.rightOr);
right.wires.a1.setSignal(A);
right.wires.a2.setSignal(A);
right.wires.b.setSignal(B);
right.wires.c.setSignal(C);
right.wires.and1ToOr.setSignal(result.rightAnd1);
right.wires.and2ToOr.setSignal(result.rightAnd2);
right.wires.result.setSignal(result.rightOr);
right.output.setValue(result.rightOr);
}
function updateInterface(result) {
const leftResult = Number(Boolean(result.leftAnd));
const rightResult = Number(Boolean(result.rightOr));
leftValue.textContent = leftResult;
rightValue.textContent = rightResult;
leftOutput.classList.toggle("on", leftResult === 1);
rightOutput.classList.toggle("on", rightResult === 1);
for (const key of ["A", "B", "C"]) {
toggleButtons[key].classList.toggle("on", state[key] === 1);
toggleButtons[key].setAttribute("aria-pressed", String(state[key] === 1));
}
[...truthGrid.children].forEach((cell, index) => {
cell.classList.toggle("selected", index === selectedIndex);
});
}
function applyState() {
const result = evaluate(state);
updateScene(result);
updateInterface(result);
}
function selectCombination(index) {
selectedIndex = index;
const combination = combinations[index];
state.A = combination.A;
state.B = combination.B;
state.C = combination.C;
applyState();
}
function stopAuto() {
autoRunning = false;
clearTimeout(autoTimer);
runButton.textContent = "Run all 8";
}
function scheduleAuto() {
autoTimer = setTimeout(() => {
if (!autoRunning) return;
selectedIndex++;
if (selectedIndex >= combinations.length) {
selectedIndex = combinations.length - 1;
applyState();
stopAuto();
message.innerHTML =
"<strong>8 / 8 verified</strong> · outputs match for every input.";
return;
}
selectCombination(selectedIndex);
message.innerHTML =
`Testing <strong>${selectedIndex + 1} / 8</strong> · ` +
`A=${state.A}, B=${state.B}, C=${state.C}`;
scheduleAuto();
}, 720);
}
function runAll() {
stopAuto();
autoRunning = true;
runButton.textContent = "Pause";
selectCombination(0);
message.innerHTML =
"<strong>Testing 1 / 8</strong> · A=0, B=0, C=0";
scheduleAuto();
}
function toggleInput(key) {
stopAuto();
state[key] = state[key] ? 0 : 1;
selectedIndex = combinations.findIndex(
(combination) =>
combination.A === state.A &&
combination.B === state.B &&
combination.C === state.C
);
applyState();
message.innerHTML =
"<strong>Custom inputs</strong> · both circuits still match.";
}
for (const key of ["A", "B", "C"]) {
toggleButtons[key].addEventListener("click", () => toggleInput(key));
}
runButton.addEventListener("click", () => {
if (autoRunning) {
stopAuto();
message.innerHTML =
"<strong>Test paused</strong> · drag the scene or edit an input.";
} else {
runAll();
}
});
stepButton.addEventListener("click", () => {
stopAuto();
selectedIndex = (selectedIndex + 1) % combinations.length;
selectCombination(selectedIndex);
message.innerHTML =
`Combination <strong>${selectedIndex + 1} / 8</strong> · outputs match.`;
});
combinations.forEach((combination, index) => {
const result = evaluate(combination);
const output = Number(Boolean(result.leftAnd));
const button = document.createElement("button");
button.className = "truth-cell";
button.setAttribute(
"aria-label",
`Inputs A ${combination.A}, B ${combination.B}, C ${combination.C}, output ${output}`
);
button.innerHTML = `
<span class="bits">
<i class="${combination.A ? "one" : ""}">A${combination.A}</i>
<i class="${combination.B ? "one" : ""}">B${combination.B}</i>
<i class="${combination.C ? "one" : ""}">C${combination.C}</i>
</span>
<span class="truth-result">→ ${output}</span>
`;
button.addEventListener("click", () => {
stopAuto();
selectCombination(index);
message.innerHTML =
`Combination <strong>${index + 1} / 8</strong> · outputs match.`;
});
truthGrid.appendChild(button);
});
// Initial state.
selectCombination(0);
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
if (!reducedMotion) {
setTimeout(runAll, 900);
} else {
message.innerHTML =
"<strong>Ready</strong> · run all eight combinations when ready.";
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
for (const wire of Object.values(left.wires)) wire.update(time);
for (const wire of Object.values(right.wires)) wire.update(time);
renderer.render(scene, camera);
}
updateCamera();
animate();
</script>
</body>
</html>
```