Prompt specification
Build or customize the Three.js 3D scene "Logic gates with domino paths" 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: Logic gates with domino paths
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/logic-gates-with-domino-paths-x0332
Scene Description Prompt:
Create a low-poly conceptual domino signal board with resettable branching and required-input gates. Mark the logic schematic. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (logic-gates-with-domino-paths-x0332):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Domino Logic Bench</title>
<style>
:root {
--bg: #071019;
--panel: rgba(12, 24, 35, 0.88);
--panel-border: rgba(111, 183, 211, 0.23);
--text: #e8f6fb;
--muted: #8da8b8;
--cyan: #58e6ff;
--blue: #718dff;
--amber: #ffb24a;
--coral: #ff705f;
--green: #78efad;
}
* {
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;
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.topbar {
top: 22px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
}
.brand {
max-width: 520px;
text-shadow: 0 2px 16px #000;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 2px;
content: "";
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.controls {
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
button {
border: 1px solid var(--panel-border);
color: var(--text);
background: var(--panel);
font: inherit;
cursor: pointer;
transition:
transform 150ms ease,
border-color 150ms ease,
background 150ms ease,
color 150ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(88, 230, 255, 0.6);
background: rgba(20, 42, 58, 0.96);
}
button:active {
transform: translateY(0);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.control-button {
min-height: 39px;
padding: 0 14px;
border-radius: 9px;
font-size: 12px;
font-weight: 700;
letter-spacing: 0.02em;
backdrop-filter: blur(14px);
}
#reset {
border-color: rgba(255, 112, 95, 0.38);
}
.status-pill {
display: flex;
align-items: center;
gap: 9px;
min-height: 39px;
padding: 0 13px;
border: 1px solid var(--panel-border);
border-radius: 999px;
color: var(--muted);
background: var(--panel);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
backdrop-filter: blur(14px);
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #415363;
box-shadow: 0 0 0 4px rgba(65, 83, 99, 0.12);
transition: 200ms ease;
}
.status-pill.active {
color: var(--green);
border-color: rgba(120, 239, 173, 0.34);
}
.status-pill.active .status-dot {
background: var(--green);
box-shadow:
0 0 12px var(--green),
0 0 0 4px rgba(120, 239, 173, 0.12);
}
.schematic-panel {
left: 50%;
bottom: 20px;
width: min(1080px, calc(100% - 40px));
transform: translateX(-50%);
padding: 15px 18px;
border: 1px solid var(--panel-border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.schematic-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding-bottom: 12px;
border-bottom: 1px solid rgba(111, 183, 211, 0.14);
}
.schematic-title {
display: flex;
align-items: center;
gap: 10px;
white-space: nowrap;
}
.schematic-title strong {
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.formula {
color: #b9cbd4;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
letter-spacing: 0.025em;
text-align: right;
}
.formula b {
color: var(--text);
}
.schematic-bottom {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding-top: 12px;
}
.input-bank {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
.input-button {
min-width: 52px;
height: 36px;
border-radius: 8px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
font-weight: 800;
}
.input-button[data-input="A"] {
--input-color: var(--coral);
}
.input-button[data-input="B"] {
--input-color: var(--amber);
}
.input-button[data-input="C"] {
--input-color: var(--cyan);
}
.input-button[data-input="D"] {
--input-color: var(--blue);
}
.input-button.active {
color: #061018;
border-color: var(--input-color);
background: var(--input-color);
box-shadow: 0 0 20px color-mix(in srgb, var(--input-color), transparent 72%);
}
.gate-bank {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 8px;
flex-wrap: wrap;
}
.gate-card {
min-width: 126px;
padding: 8px 10px;
border: 1px solid rgba(111, 183, 211, 0.16);
border-radius: 9px;
color: var(--muted);
background: rgba(4, 11, 17, 0.35);
font-size: 10px;
line-height: 1.4;
}
.gate-card strong {
display: block;
margin-bottom: 2px;
color: #c6d7df;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.gate-card.active {
color: var(--green);
border-color: rgba(120, 239, 173, 0.38);
background: rgba(120, 239, 173, 0.07);
}
.gate-card.active strong {
color: var(--green);
}
.gate-requirements {
display: flex;
gap: 4px;
margin-top: 5px;
}
.req {
padding: 2px 5px;
border-radius: 4px;
color: #617889;
background: rgba(92, 122, 140, 0.1);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.req.lit {
color: #071019;
background: var(--green);
}
.help {
position: fixed;
z-index: 8;
top: 116px;
left: 24px;
color: #617989;
font-size: 10px;
line-height: 1.6;
pointer-events: none;
}
.help kbd {
padding: 2px 5px;
border: 1px solid rgba(111, 183, 211, 0.22);
border-radius: 4px;
color: #a3bdcb;
background: rgba(12, 24, 35, 0.7);
font: inherit;
}
#toast {
position: fixed;
z-index: 20;
top: 94px;
left: 50%;
max-width: min(440px, calc(100% - 40px));
transform: translate(-50%, -12px);
padding: 10px 15px;
border: 1px solid rgba(88, 230, 255, 0.28);
border-radius: 9px;
color: #ccebf3;
background: rgba(8, 20, 29, 0.92);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.35);
font-size: 12px;
opacity: 0;
pointer-events: none;
transition: 180ms ease;
backdrop-filter: blur(12px);
}
#toast.visible {
transform: translate(-50%, 0);
opacity: 1;
}
@media (max-width: 760px) {
.topbar {
top: 15px;
left: 15px;
right: 15px;
}
.subtitle,
.help {
display: none;
}
.status-pill {
display: none;
}
.control-button {
padding: 0 10px;
}
.schematic-panel {
bottom: 12px;
width: calc(100% - 24px);
padding: 12px;
}
.schematic-top {
align-items: flex-start;
flex-direction: column;
gap: 7px;
}
.formula {
text-align: left;
}
.schematic-bottom {
align-items: flex-start;
flex-direction: column;
}
.gate-bank {
justify-content: flex-start;
}
.gate-card {
min-width: 105px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly domino logic board"></canvas>
<header class="hud topbar">
<div class="brand">
<div class="eyebrow">Conceptual Signal Board</div>
<h1>DOMINO / LOGIC BENCH</h1>
<p class="subtitle">
Toggle the physical inputs to propagate domino signals through a branching
network of required-input gates.
</p>
</div>
<div class="controls">
<div class="status-pill" id="statusPill">
<span class="status-dot"></span>
<span id="statusText">OUTPUT Y = 0</span>
</div>
<button class="control-button" id="reset" title="Reset all inputs and dominoes">
↺ Reset board
</button>
<button class="control-button" id="view" title="Reset the camera">
⌖ Reset view
</button>
</div>
</header>
<div class="help">
<kbd>Drag</kbd> orbit
<kbd>Shift / right-drag</kbd> pan
<kbd>Scroll</kbd> zoom<br>
<kbd>1–4</kbd> toggle inputs
<kbd>R</kbd> reset
</div>
<div id="toast"></div>
<footer class="hud schematic-panel">
<div class="schematic-top">
<div class="schematic-title">
<strong>Logic schematic</strong>
<span>All AND gates require every marked input.</span>
</div>
<div class="formula">
<b>Y</b> = (<b>A</b> ∨ <b>B</b>) ∧ <b>C</b> ∧ <b>D</b>
</div>
</div>
<div class="schematic-bottom">
<div class="input-bank" id="inputControls">
<button class="input-button" data-input="A" aria-pressed="false">A</button>
<button class="input-button" data-input="B" aria-pressed="false">B</button>
<button class="input-button" data-input="C" aria-pressed="false">C</button>
<button class="input-button" data-input="D" aria-pressed="false">D</button>
</div>
<div class="gate-bank">
<div class="gate-card" id="gateCard1">
<strong>AND gate 1</strong>
Requires OR + C
<div class="gate-requirements">
<span class="req" data-requirement="or">OR</span>
<span class="req" data-requirement="C">C</span>
</div>
</div>
<div class="gate-card" id="gateCard2">
<strong>AND gate 2</strong>
Requires OR + D
<div class="gate-requirements">
<span class="req" data-requirement="or">OR</span>
<span class="req" data-requirement="D">D</span>
</div>
</div>
<div class="gate-card" id="gateCardFinal">
<strong>Final AND</strong>
Requires both gates
<div class="gate-requirements">
<span class="req" data-requirement="g1">G1</span>
<span class="req" data-requirement="g2">G2</span>
</div>
</div>
</div>
</div>
</footer>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 22, 42);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0x9adfff, 0x17202b, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff4df, 3.2);
keyLight.position.set(-7, 14, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x55dfff, 1.6);
rimLight.position.set(10, 7, -9);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9f5c, 24, 16, 2);
warmLight.position.set(-5, 4, -3);
scene.add(warmLight);
// ------------------------------------------------------------
// Shared helpers
// ------------------------------------------------------------
const COLORS = {
A: 0xff705f,
B: 0xffb24a,
C: 0x58e6ff,
D: 0x718dff,
green: 0x78efad,
orange: 0xffb24a,
cyan: 0x58e6ff,
blue: 0x718dff
};
const state = {
inputs: {
A: false,
B: false,
C: false,
D: false
},
logic: {
or: false,
g1: false,
g2: false,
final: false
}
};
const chains = {};
const tiles = [];
const interactive = [];
const nodeRefs = {};
const switchRefs = {};
const branchCurves = [];
const branchPulses = [];
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.08,
flatShading: true,
...options
});
}
function addEdges(mesh, color = 0x071019, opacity = 0.7) {
const edges = new THREE.EdgesGeometry(mesh.geometry, 28);
const line = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(line);
return line;
}
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeLabel(text, color = "#dff7ff", width = 1.7) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 144;
const ctx = labelCanvas.getContext("2d");
const lines = text.split("\n");
const colorHex = `#${new THREE.Color(color).getHexString()}`;
ctx.clearRect(0, 0, 512, 144);
roundedRect(ctx, 6, 6, 500, 132, 24);
ctx.fillStyle = "rgba(7, 17, 25, 0.90)";
ctx.fill();
ctx.strokeStyle = colorHex;
ctx.lineWidth = 4;
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = colorHex;
if (lines.length === 1) {
ctx.font = "700 48px ui-monospace, monospace";
ctx.fillText(lines[0], 256, 74);
} else {
ctx.font = "800 39px ui-monospace, monospace";
ctx.fillText(lines[0], 256, 54);
ctx.font = "600 30px ui-monospace, monospace";
ctx.fillStyle = "#d7e8ee";
ctx.fillText(lines[1], 256, 103);
}
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
const height = width * 0.28125;
sprite.scale.set(width, height, 1);
return sprite;
}
// ------------------------------------------------------------
// Board and schematic print
// ------------------------------------------------------------
const board = new THREE.Mesh(
new THREE.BoxGeometry(19.4, 0.56, 11.2),
standardMaterial(0x152737, {
roughness: 0.9,
metalness: 0.02
})
);
board.position.y = -0.28;
board.receiveShadow = true;
addEdges(board, 0x34546b, 0.8);
scene.add(board);
const lowerBoard = new THREE.Mesh(
new THREE.BoxGeometry(18.8, 0.32, 10.6),
standardMaterial(0x0b1722)
);
lowerBoard.position.y = -0.64;
scene.add(lowerBoard);
// Grid markings.
const gridPositions = [];
for (let x = -9; x <= 9; x += 1) {
gridPositions.push(x, 0.012, -5.1, x, 0.012, 5.1);
}
for (let z = -5; z <= 5; z += 1) {
gridPositions.push(-9.2, 0.012, z, 9.2, 0.012, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x345064,
transparent: true,
opacity: 0.24
})
);
scene.add(grid);
// Corner fasteners.
const fastenerGeometry = new THREE.CylinderGeometry(0.12, 0.12, 0.08, 6);
const fastenerMaterial = standardMaterial(0x7894a3, {
metalness: 0.65,
roughness: 0.35
});
for (const x of [-9.2, 9.2]) {
for (const z of [-5.15, 5.15]) {
const fastener = new THREE.Mesh(fastenerGeometry, fastenerMaterial);
fastener.position.set(x, 0.04, z);
fastener.castShadow = true;
scene.add(fastener);
}
}
// Printed board schematic.
function makeBoardSchematic() {
const c = document.createElement("canvas");
c.width = 1600;
c.height = 320;
const ctx = c.getContext("2d");
roundedRect(ctx, 8, 8, 1584, 304, 28);
ctx.fillStyle = "#0a1722";
ctx.fill();
ctx.strokeStyle = "#35556b";
ctx.lineWidth = 5;
ctx.stroke();
ctx.textBaseline = "middle";
ctx.textAlign = "left";
ctx.fillStyle = "#58e6ff";
ctx.font = "800 42px ui-monospace, monospace";
ctx.fillText("LOGIC SCHEMATIC", 52, 61);
ctx.fillStyle = "#8eacbc";
ctx.font = "600 25px ui-monospace, monospace";
ctx.fillText("REQUIRED-INPUT DOMINO NETWORK", 52, 109);
ctx.textAlign = "center";
ctx.fillStyle = "#e7f7fb";
ctx.font = "700 67px ui-monospace, monospace";
ctx.fillText("Y = (A OR B) AND C AND D", 800, 190);
ctx.fillStyle = "#78efad";
ctx.font = "600 25px ui-monospace, monospace";
ctx.fillText("EVERY AND PORT MUST BE SATISFIED", 800, 260);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
polygonOffset: true,
polygonOffsetFactor: -2
});
const plane = new THREE.Mesh(new THREE.PlaneGeometry(10.8, 2.16), material);
plane.rotation.x = -Math.PI / 2;
plane.position.set(0, 0.035, 4.28);
scene.add(plane);
}
makeBoardSchematic();
// ------------------------------------------------------------
// Dominoes
// ------------------------------------------------------------
const dominoGeometry = new THREE.BoxGeometry(0.28, 0.82, 0.46);
const pipGeometry = new THREE.CylinderGeometry(0.043, 0.043, 0.015, 8);
const pipMaterial = standardMaterial(0x071019, {
roughness: 1,
metalness: 0
});
function createDomino(position, yaw, material, index) {
const root = new THREE.Group();
root.position.set(position.x, 0.025, position.z);
root.rotation.y = yaw;
const pivot = new THREE.Group();
root.add(pivot);
const body = new THREE.Mesh(dominoGeometry, material);
body.position.y = 0.41;
body.castShadow = true;
body.receiveShadow = true;
addEdges(body, 0x071019, 0.78);
pivot.add(body);
// Small pips make the pieces read as dominoes rather than blocks.
const pipPattern = index % 3;
const pips =
pipPattern === 0
? [[-0.055, 0.31], [0.055, 0.51]]
: pipPattern === 1
? [[0, 0.41]]
: [[-0.055, 0.31], [0, 0.41], [0.055, 0.51]];
for (const [px, py] of pips) {
const pip = new THREE.Mesh(pipGeometry, pipMaterial);
pip.rotation.x = Math.PI / 2;
pip.position.set(px, py, 0.237);
pivot.add(pip);
}
const tile = {
pivot,
current: 0,
from: 0,
target: 0,
startTime: 0,
duration: 0.4,
active: false
};
tiles.push(tile);
return tile;
}
function createChain(id, points, color) {
const material = standardMaterial(color, {
emissive: color,
emissiveIntensity: 0.035,
roughness: 0.62
});
const chain = {
id,
material,
tiles: [],
active: false
};
let dominoIndex = 0;
for (let segment = 0; segment < points.length - 1; segment++) {
const a = points[segment];
const b = points[segment + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const divisions = Math.max(1, Math.ceil(length / 0.58));
const yaw = Math.atan2(dx, dz);
for (let j = 0; j <= divisions; j++) {
if (segment > 0 && j === 0) continue;
const t = j / divisions;
const position = {
x: a.x + dx * t,
z: a.z + dz * t
};
const tile = createDomino(
position,
yaw,
material,
dominoIndex++
);
chain.tiles.push(tile);
}
}
chains[id] = chain;
return chain;
}
createChain(
"A",
[
{ x: -7.25, z: -3.7 },
{ x: -5.3, z: -3.7 },
{ x: -4.65, z: -3.05 }
],
COLORS.A
);
createChain(
"B",
[
{ x: -7.25, z: -1.55 },
{ x: -5.3, z: -1.55 },
{ x: -4.65, z: -1.95 }
],
COLORS.B
);
createChain(
"C",
[
{ x: -7.25, z: 0.25 },
{ x: -5.2, z: 0.25 },
{ x: -3.25, z: 0.25 },
{ x: -1.62, z: -0.67 }
],
COLORS.C
);
createChain(
"D",
[
{ x: -7.25, z: 2.25 },
{ x: -5.2, z: 2.25 },
{ x: -3.25, z: 2.25 },
{ x: -1.62, z: 0.67 }
],
COLORS.D
);
createChain(
"g1Out",
[
{ x: -0.43, z: -1.05 },
{ x: 1.15, z: -1.05 },
{ x: 2.58, z: 0 }
],
COLORS.cyan
);
createChain(
"g2Out",
[
{ x: -0.43, z: 1.05 },
{ x: 1.15, z: 1.05 },
{ x: 2.58, z: 0 }
],
COLORS.blue
);
createChain(
"finalOut",
[
{ x: 4.02, z: 0 },
{ x: 5.25, z: 0 },
{ x: 6.4, z: 0 }
],
COLORS.green
);
function scheduleTile(tile, target, delay, duration = 0.4) {
tile.from = tile.current;
tile.target = target;
tile.startTime = elapsed + delay;
tile.duration = duration;
tile.active = true;
}
function setChain(chainId, on, baseDelay = 0) {
const chain = chains[chainId];
chain.active = on;
const ordered = on ? chain.tiles : [...chain.tiles].reverse();
ordered.forEach((tile, index) => {
scheduleTile(
tile,
on ? 1 : 0,
baseDelay + index * 0.065,
on ? 0.39 : 0.32
);
});
}
// ------------------------------------------------------------
// Input switches
// ------------------------------------------------------------
function createSwitch(key, x, z) {
const group = new THREE.Group();
group.position.set(x, 0.02, z);
scene.add(group);
const baseMaterial = standardMaterial(0x1a3040, {
emissive: COLORS[key],
emissiveIntensity: 0.03,
metalness: 0.3
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.4, 0.46, 0.22, 8),
baseMaterial
);
base.position.y = 0.11;
base.castShadow = true;
base.receiveShadow = true;
addEdges(base, 0x071019, 0.8);
group.add(base);
const leverPivot = new THREE.Group();
leverPivot.position.y = 0.23;
group.add(leverPivot);
const lever = new THREE.Mesh(
new THREE.BoxGeometry(0.52, 0.12, 0.18),
standardMaterial(COLORS[key], {
emissive: COLORS[key],
emissiveIntensity: 0.1,
metalness: 0.15
})
);
lever.position.x = 0.14;
lever.castShadow = true;
leverPivot.add(lever);
const hit = new THREE.Mesh(
new THREE.CylinderGeometry(0.56, 0.56, 0.55, 8),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
hit.position.y = 0.25;
hit.userData.input = key;
group.add(hit);
interactive.push(hit);
const label = makeLabel(`INPUT ${key}`, `#${new THREE.Color(COLORS[key]).getHexString()}`, 1.42);
label.position.set(x, 1.2, z);
scene.add(label);
switchRefs[key] = {
group,
baseMaterial,
leverMaterial: lever.material,
leverPivot,
targetRotation: 0
};
}
createSwitch("A", -8.15, -3.7);
createSwitch("B", -8.15, -1.55);
createSwitch("C", -8.15, 0.25);
createSwitch("D", -8.15, 2.25);
// ------------------------------------------------------------
// Logic gates
// ------------------------------------------------------------
function createGate({
key,
x,
z,
label,
color,
required = [],
radius = 0.7,
or = false
}) {
const group = new THREE.Group();
group.position.set(x, 0.02, z);
scene.add(group);
const baseMaterial = standardMaterial(0x162d3b, {
emissive: color,
emissiveIntensity: 0.025,
metalness: 0.22
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius * 1.04, 0.24, 8),
baseMaterial
);
base.position.y = 0.12;
base.castShadow = true;
base.receiveShadow = true;
addEdges(base, 0x071019, 0.86);
base.userData.gate = key;
group.add(base);
interactive.push(base);
const ringMaterial = standardMaterial(color, {
emissive: color,
emissiveIntensity: 0.08,
metalness: 0.25
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius * 0.88, 0.065, 6, 18),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.26;
group.add(ring);
const ports = [];
required.forEach((requirement, index) => {
const angle = Math.PI * (0.72 + index * 0.56);
const portMaterial = standardMaterial(0x526b78, {
emissive: 0x526b78,
emissiveIntensity: 0.05
});
const port = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.11, 0.14, 8),
portMaterial
);
port.position.set(
Math.cos(angle) * radius * 0.88,
0.29,
Math.sin(angle) * radius * 0.88
);
group.add(port);
ports.push({
requirement,
material: portMaterial
});
});
const labelSprite = makeLabel(
label,
`#${new THREE.Color(color).getHexString()}`,
or ? 1.55 : 1.8
);
labelSprite.position.set(x, 1.43, z);
scene.add(labelSprite);
const ref = {
key,
group,
base,
baseMaterial,
ring,
ringMaterial,
ports,
required,
active: false,
or
};
nodeRefs[key] = ref;
return ref;
}
createGate({
key: "or",
x: -4.15,
z: -2.5,
label: "OR\nA / B",
color: COLORS.orange,
radius: 0.68,
or: true
});
createGate({
key: "g1",
x: -0.9,
z: -1.05,
label: "AND 1\nOR + C",
color: COLORS.cyan,
required: ["or", "C"]
});
createGate({
key: "g2",
x: -0.9,
z: 1.05,
label: "AND 2\nOR + D",
color: COLORS.blue,
required: ["or", "D"]
});
createGate({
key: "final",
x: 3.55,
z: 0,
label: "AND\nG1 + G2",
color: COLORS.green,
required: ["g1", "g2"],
radius: 0.78
});
// Branch conduits from the OR gate.
const branchMaterial = standardMaterial(0x376474, {
emissive: COLORS.cyan,
emissiveIntensity: 0.04,
metalness: 0.25
});
const branchDefinitions = [
[
new THREE.Vector3(-3.75, 0.055, -2.5),
new THREE.Vector3(-2.95, 0.055, -2.5),
new THREE.Vector3(-2.95, 0.055, -1.05),
new THREE.Vector3(-1.55, 0.055, -1.05)
],
[
new THREE.Vector3(-3.75, 0.055, -2.5),
new THREE.Vector3(-2.95, 0.055, -2.5),
new THREE.Vector3(-2.95, 0.055, 1.05),
new THREE.Vector3(-1.55, 0.055, 1.05)
]
];
branchDefinitions.forEach((points, branchIndex) => {
const curve = new THREE.CatmullRomCurve3(points, false, "catmullrom", 0.12);
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 30, 0.035, 5, false),
branchMaterial
);
scene.add(tube);
branchCurves.push(curve);
for (let i = 0; i < 4; i++) {
const pulse = new THREE.Mesh(
new THREE.OctahedronGeometry(0.085, 0),
new THREE.MeshBasicMaterial({ color: COLORS.cyan })
);
pulse.visible = false;
scene.add(pulse);
branchPulses.push({ mesh: pulse, curve, offset: i / 4 + branchIndex * 0.08 });
}
});
const splitLabel = makeLabel("BRANCH SPLIT", "#58e6ff", 1.65);
splitLabel.position.set(-2.95, 0.85, -2.5);
scene.add(splitLabel);
// Output terminal.
const outputGroup = new THREE.Group();
outputGroup.position.set(7.2, 0, 0);
scene.add(outputGroup);
const outputBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.63, 0.7, 0.24, 8),
standardMaterial(0x17342e, {
emissive: COLORS.green,
emissiveIntensity: 0.02
})
);
outputBase.position.y = 0.12;
outputBase.castShadow = true;
outputBase.receiveShadow = true;
addEdges(outputBase, 0x071019, 0.85);
outputGroup.add(outputBase);
const beaconMaterial = standardMaterial(COLORS.green, {
emissive: COLORS.green,
emissiveIntensity: 0.04,
metalness: 0.15
});
const beacon = new THREE.Mesh(
new THREE.OctahedronGeometry(0.37, 0),
beaconMaterial
);
beacon.position.y = 1.04;
beacon.castShadow = true;
outputGroup.add(beacon);
const outputLabel = makeLabel("OUTPUT Y", "#78efad", 1.5);
outputLabel.position.set(7.2, 1.78, 0);
scene.add(outputLabel);
nodeRefs.output = {
baseMaterial: outputBase.material,
beacon,
beaconMaterial
};
// ------------------------------------------------------------
// Logic and UI updates
// ------------------------------------------------------------
function requirementValue(key) {
if (key === "or") return state.logic.or;
if (key === "g1") return state.logic.g1;
if (key === "g2") return state.logic.g2;
return state.inputs[key];
}
function setGateActive(gate, active) {
gate.active = active;
gate.baseMaterial.emissiveIntensity = active ? 0.65 : 0.025;
gate.ringMaterial.emissiveIntensity = active ? 2.3 : 0.08;
for (const port of gate.ports) {
const lit = requirementValue(port.requirement);
port.material.color.setHex(lit ? 0x78efad : 0x526b78);
port.material.emissive.setHex(lit ? 0x78efad : 0x526b78);
port.material.emissiveIntensity = lit ? 2.1 : 0.05;
}
}
function refreshLogic() {
const previous = { ...state.logic };
const next = {
or: state.inputs.A || state.inputs.B,
g1: (state.inputs.A || state.inputs.B) && state.inputs.C,
g2: (state.inputs.A || state.inputs.B) && state.inputs.D,
final:
((state.inputs.A || state.inputs.B) && state.inputs.C) &&
((state.inputs.A || state.inputs.B) && state.inputs.D)
};
state.logic = next;
setGateActive(nodeRefs.or, next.or);
setGateActive(nodeRefs.g1, next.g1);
setGateActive(nodeRefs.g2, next.g2);
setGateActive(nodeRefs.final, next.final);
if (next.g1 !== previous.g1) {
setChain("g1Out", next.g1, next.g1 ? 0.42 : 0);
}
if (next.g2 !== previous.g2) {
setChain("g2Out", next.g2, next.g2 ? 0.42 : 0);
}
if (next.final !== previous.final) {
setChain("finalOut", next.final, next.final ? 0.48 : 0);
}
branchMaterial.emissiveIntensity = next.or ? 2.1 : 0.04;
branchMaterial.color.setHex(next.or ? 0x58e6ff : 0x376474);
nodeRefs.output.baseMaterial.emissiveIntensity = next.final ? 0.8 : 0.02;
nodeRefs.output.beaconMaterial.emissiveIntensity = next.final ? 2.7 : 0.04;
updateUI();
}
const inputButtons = [...document.querySelectorAll(".input-button")];
const gateCards = {
g1: document.querySelector("#gateCard1"),
g2: document.querySelector("#gateCard2"),
final: document.querySelector("#gateCardFinal")
};
const requirementElements = [
...document.querySelectorAll(".req")
];
function updateUI() {
for (const button of inputButtons) {
const key = button.dataset.input;
const active = state.inputs[key];
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
}
gateCards.g1.classList.toggle("active", state.logic.g1);
gateCards.g2.classList.toggle("active", state.logic.g2);
gateCards.final.classList.toggle("active", state.logic.final);
for (const element of requirementElements) {
element.classList.toggle(
"lit",
requirementValue(element.dataset.requirement)
);
}
const statusPill = document.querySelector("#statusPill");
const statusText = document.querySelector("#statusText");
statusPill.classList.toggle("active", state.logic.final);
statusText.textContent = state.logic.final
? "OUTPUT Y = 1"
: "OUTPUT Y = 0";
}
function toggleInput(key) {
state.inputs[key] = !state.inputs[key];
const active = state.inputs[key];
setChain(key, active, active ? 0 : 0);
switchRefs[key].targetRotation = active ? -0.58 : 0;
refreshLogic();
showToast(
active
? `Input ${key} asserted.`
: `Input ${key} released.`
);
}
for (const button of inputButtons) {
button.addEventListener("click", () => {
toggleInput(button.dataset.input);
});
}
let toastTimer;
function showToast(message) {
const toast = document.querySelector("#toast");
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("visible");
}, 1800);
}
function resetBoard() {
for (const key of Object.keys(state.inputs)) {
state.inputs[key] = false;
switchRefs[key].targetRotation = 0;
setChain(key, false, 0);
}
setChain("g1Out", false, 0.14);
setChain("g2Out", false, 0.08);
setChain("finalOut", false, 0);
state.logic = {
or: false,
g1: false,
g2: false,
final: false
};
setGateActive(nodeRefs.or, false);
setGateActive(nodeRefs.g1, false);
setGateActive(nodeRefs.g2, false);
setGateActive(nodeRefs.final, false);
branchMaterial.emissiveIntensity = 0.04;
branchMaterial.color.setHex(0x376474);
nodeRefs.output.baseMaterial.emissiveIntensity = 0.02;
nodeRefs.output.beaconMaterial.emissiveIntensity = 0.04;
updateUI();
showToast("Board reset. All signal paths are clear.");
}
document.querySelector("#reset").addEventListener("click", resetBoard);
// ------------------------------------------------------------
// Camera controls implemented directly
// ------------------------------------------------------------
const cameraState = {
yaw: 0.47,
pitch: 0.57,
distance: 22.5,
target: new THREE.Vector3(0, 0.1, 0)
};
function updateCamera() {
const { yaw, pitch, distance, target } = cameraState;
camera.position.set(
target.x + distance * Math.sin(yaw) * Math.cos(pitch),
target.y + distance * Math.sin(pitch),
target.z + distance * Math.cos(yaw) * Math.cos(pitch)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.yaw = 0.47;
cameraState.pitch = 0.57;
cameraState.distance = 22.5;
cameraState.target.set(0, 0.1, 0);
updateCamera();
}
document.querySelector("#view").addEventListener("click", resetCamera);
updateCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let pointerMoved = false;
let panMode = false;
let lastPointerX = 0;
let lastPointerY = 0;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pickInteractive(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObjects(interactive, false)[0]?.object ?? null;
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerMoved = false;
panMode = event.button === 2 || event.shiftKey;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) {
const hit = pickInteractive(event);
canvas.style.cursor = hit ? "pointer" : "grab";
return;
}
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
if (Math.abs(dx) + Math.abs(dy) > 2) {
pointerMoved = true;
}
if (panMode) {
const scale = cameraState.distance * 0.00135;
cameraState.target.x -= dx * scale;
cameraState.target.z += dy * scale;
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-6,
6
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-5,
5
);
} else {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.27,
1.15
);
}
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
canvas.releasePointerCapture(event.pointerId);
if (!pointerMoved) {
const hit = pickInteractive(event);
if (hit?.userData.input) {
toggleInput(hit.userData.input);
} else if (hit?.userData.gate) {
const messages = {
or: "OR gate: either A or B satisfies this branch.",
g1: "AND gate 1 requires both the OR branch and input C.",
g2: "AND gate 2 requires both the OR branch and input D.",
final: "Final AND gate requires both AND gate 1 and AND gate 2."
};
showToast(messages[hit.userData.gate]);
}
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.012,
12,
34
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
const keyMap = {
"1": "A",
"2": "B",
"3": "C",
"4": "D"
};
if (keyMap[event.key]) {
toggleInput(keyMap[event.key]);
}
if (event.key.toLowerCase() === "r") {
resetBoard();
}
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
let elapsed = 0;
let previousTime = performance.now();
function smoothstep(value) {
return value * value * (3 - 2 * value);
}
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
elapsed += delta;
// Domino fall and reset animation.
for (const tile of tiles) {
if (!tile.active) continue;
const progress = THREE.MathUtils.clamp(
(elapsed - tile.startTime) / tile.duration,
0,
1
);
const eased = smoothstep(progress);
tile.current = THREE.MathUtils.lerp(tile.from, tile.target, eased);
tile.pivot.rotation.x = tile.current * Math.PI * 0.5;
if (progress >= 1) {
tile.current = tile.target;
tile.active = false;
}
}
// Chain material glow.
for (const chain of Object.values(chains)) {
const targetIntensity = chain.active ? 0.48 : 0.035;
chain.material.emissiveIntensity = THREE.MathUtils.lerp(
chain.material.emissiveIntensity,
targetIntensity,
0.08
);
}
// Input switch levers.
for (const reference of Object.values(switchRefs)) {
reference.leverPivot.rotation.z = THREE.MathUtils.lerp(
reference.leverPivot.rotation.z,
reference.targetRotation,
0.13
);
}
// Branch signal pulses.
for (const pulse of branchPulses) {
pulse.mesh.visible = state.logic.or;
if (state.logic.or) {
const t = (elapsed * 0.22 + pulse.offset) % 1;
const point = pulse.curve.getPoint(t);
pulse.mesh.position.copy(point);
pulse.mesh.position.y += 0.075;
pulse.mesh.rotation.y += 0.05;
pulse.mesh.rotation.x += 0.035;
}
}
// Output beacon.
nodeRefs.output.beacon.rotation.y += delta * (state.logic.final ? 1.5 : 0.35);
nodeRefs.output.beacon.position.y =
1.04 + Math.sin(elapsed * 2.2) * (state.logic.final ? 0.09 : 0.025);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateUI();
</script>
</body>
</html>
```