Prompt specification
Build or customize the Three.js 3D scene "Compare the same logic across packaging scales" 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: Compare the same logic across packaging scales
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/compare-the-same-logic-across-packaging-scales-x2496
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing integrated circuits and electronic miniaturization. Let users switch a small logical circuit between discrete and integrated representations. Preserve input-output behavior while showing differences in connection length and component grouping. Low-poly.
Working Three.js Starter (compare-the-same-logic-across-packaging-scales-x2496):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Logic / Scale — Circuit Miniaturization Explorer</title>
<style>
:root {
--bg: #050812;
--panel: rgba(10, 17, 31, 0.82);
--panel-border: rgba(143, 191, 226, 0.18);
--text: #eaf7ff;
--muted: #89a0b6;
--accent: #73e7ff;
--accent-rgb: 115, 231, 255;
--warm: #ffbd59;
--green: #79efb4;
--danger: #ff7085;
}
body[data-mode="integrated"] {
--accent: #79efb4;
--accent-rgb: 121, 239, 180;
}
* {
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;
}
button {
color: inherit;
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 52% 45%, transparent 38%, rgba(0, 0, 0, 0.33) 100%),
linear-gradient(180deg, rgba(3, 7, 15, 0.22), transparent 26%, transparent 75%, rgba(3, 7, 15, 0.38));
z-index: 2;
}
.topbar {
position: fixed;
z-index: 10;
top: 0;
left: 0;
right: 0;
height: 76px;
padding: 16px 24px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
background: linear-gradient(180deg, rgba(5, 8, 18, 0.88), transparent);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
border: 1px solid rgba(var(--accent-rgb), 0.55);
border-radius: 9px;
background: rgba(var(--accent-rgb), 0.08);
box-shadow: inset 0 0 22px rgba(var(--accent-rgb), 0.08);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--accent);
border-radius: 2px;
}
.brand-mark::before {
width: 18px;
height: 18px;
left: 8px;
top: 8px;
border: 1px solid var(--accent);
background: transparent;
}
.brand-mark::after {
width: 4px;
height: 4px;
left: 15px;
top: 15px;
box-shadow:
-9px 0 0 var(--accent),
9px 0 0 var(--accent),
0 -9px 0 var(--accent),
0 9px 0 var(--accent);
}
.brand h1 {
margin: 0;
font-size: 15px;
line-height: 1;
letter-spacing: 0.16em;
font-weight: 800;
}
.brand p {
margin: 7px 0 0;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.top-actions {
display: flex;
align-items: center;
gap: 12px;
pointer-events: auto;
}
.status-pill {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border: 1px solid rgba(121, 239, 180, 0.22);
border-radius: 999px;
background: rgba(121, 239, 180, 0.06);
color: #a9f4ce;
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px var(--green);
}
.ghost-button {
border: 1px solid var(--panel-border);
border-radius: 9px;
background: rgba(10, 17, 31, 0.72);
padding: 9px 12px;
cursor: pointer;
color: #bfd0df;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
transition:
border-color 160ms ease,
color 160ms ease,
background 160ms ease;
}
.ghost-button:hover {
color: white;
border-color: rgba(var(--accent-rgb), 0.55);
background: rgba(var(--accent-rgb), 0.08);
}
.panel {
position: fixed;
z-index: 8;
border: 1px solid var(--panel-border);
border-radius: 16px;
background:
linear-gradient(135deg, rgba(var(--accent-rgb), 0.035), transparent 38%),
var(--panel);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.3),
inset 0 1px 0 rgba(255, 255, 255, 0.025);
backdrop-filter: blur(15px);
}
.control-panel {
top: 92px;
left: 24px;
width: 324px;
padding: 18px;
}
.analysis-panel {
top: 92px;
right: 24px;
width: 306px;
padding: 18px;
}
.kicker {
margin-bottom: 12px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.mode-card {
width: 100%;
padding: 15px;
border: 1px solid rgba(var(--accent-rgb), 0.26);
border-radius: 12px;
background:
linear-gradient(135deg, rgba(var(--accent-rgb), 0.1), transparent),
rgba(5, 11, 21, 0.58);
text-align: left;
cursor: pointer;
transition:
border-color 180ms ease,
transform 180ms ease,
background 180ms ease;
}
.mode-card:hover {
transform: translateY(-1px);
border-color: rgba(var(--accent-rgb), 0.62);
}
.mode-card-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.mode-card small {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.mode-card strong {
display: block;
color: var(--accent);
font-size: 20px;
letter-spacing: 0.07em;
}
.mode-icon {
width: 47px;
height: 47px;
flex: 0 0 auto;
display: grid;
place-items: center;
border: 1px solid rgba(var(--accent-rgb), 0.35);
border-radius: 11px;
color: var(--accent);
font-size: 19px;
background: rgba(var(--accent-rgb), 0.07);
box-shadow: inset 0 0 18px rgba(var(--accent-rgb), 0.06);
}
.mode-note {
margin: 10px 0 0;
color: #a9bac9;
font-size: 11px;
line-height: 1.45;
}
.section-divider {
height: 1px;
margin: 17px 0;
background: linear-gradient(90deg, var(--panel-border), transparent);
}
.input-heading {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.input-heading span {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.input-heading button {
border: 0;
padding: 0;
color: var(--accent);
background: transparent;
cursor: pointer;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.input-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.input-button {
position: relative;
display: flex;
align-items: center;
gap: 11px;
min-height: 64px;
padding: 11px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 11px;
background: rgba(4, 9, 17, 0.52);
cursor: pointer;
overflow: hidden;
transition:
border-color 160ms ease,
background 160ms ease;
}
.input-button:hover {
border-color: rgba(var(--accent-rgb), 0.38);
}
.input-button.on {
border-color: rgba(var(--accent-rgb), 0.48);
background: rgba(var(--accent-rgb), 0.09);
}
.input-letter {
width: 32px;
height: 32px;
flex: 0 0 auto;
display: grid;
place-items: center;
border-radius: 8px;
color: #08111e;
background: #617287;
font-size: 13px;
font-weight: 900;
transition:
background 160ms ease,
box-shadow 160ms ease;
}
#inputA.on .input-letter {
background: var(--accent);
box-shadow: 0 0 18px rgba(var(--accent-rgb), 0.35);
}
#inputB.on .input-letter {
background: #c694ff;
box-shadow: 0 0 18px rgba(198, 148, 255, 0.35);
}
.input-copy {
text-align: left;
}
.input-copy span {
display: block;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.input-copy strong {
display: block;
margin-top: 3px;
font-size: 20px;
line-height: 1;
}
.logic-expression {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 13px;
padding: 12px;
border-radius: 10px;
background: rgba(0, 0, 0, 0.22);
border: 1px solid rgba(255, 255, 255, 0.055);
}
.logic-expression code {
color: #d8e8f5;
font-size: 15px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.output-readout {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.output-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: #3b4658;
transition:
background 160ms ease,
box-shadow 160ms ease;
}
.output-dot.on {
background: var(--warm);
box-shadow: 0 0 17px rgba(255, 189, 89, 0.8);
}
.output-number {
color: var(--text);
font-weight: 800;
}
.metric {
margin-bottom: 15px;
}
.metric-top {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 12px;
}
.metric-name {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric-value {
color: var(--text);
font-size: 14px;
font-weight: 750;
}
.metric-track {
height: 4px;
margin-top: 8px;
border-radius: 999px;
overflow: hidden;
background: rgba(255, 255, 255, 0.07);
}
.metric-fill {
height: 100%;
width: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--accent), rgba(var(--accent-rgb), 0.25));
box-shadow: 0 0 10px rgba(var(--accent-rgb), 0.22);
transition: width 420ms cubic-bezier(0.22, 1, 0.36, 1);
}
.metric-note {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: #60778d;
font-size: 8px;
}
.truth-wrap {
margin-top: 18px;
}
.truth-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.truth-title small {
color: #5f7489;
font-size: 8px;
letter-spacing: 0.04em;
text-transform: none;
}
.truth-table {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 4px;
}
.truth-cell {
height: 31px;
display: grid;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.055);
border-radius: 6px;
color: #7890a5;
background: rgba(0, 0, 0, 0.18);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
}
.truth-cell.header {
height: 23px;
color: #536b80;
background: transparent;
border-color: transparent;
font-family: inherit;
font-size: 8px;
font-weight: 800;
}
.truth-cell.active {
color: #06101a;
border-color: var(--accent);
background: var(--accent);
font-weight: 900;
box-shadow: 0 0 14px rgba(var(--accent-rgb), 0.2);
}
.truth-cell.output-cell.active {
color: #171006;
background: var(--warm);
border-color: var(--warm);
}
.footnote {
margin-top: 13px;
color: #50667b;
font-size: 8px;
line-height: 1.45;
}
.comparison-strip {
position: fixed;
z-index: 7;
left: 50%;
bottom: 24px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 14px;
padding: 9px 13px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 999px;
background: rgba(6, 11, 21, 0.64);
backdrop-filter: blur(12px);
pointer-events: none;
color: #70869b;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.comparison-strip b {
color: var(--accent);
font-weight: 800;
}
.comparison-divider {
width: 1px;
height: 16px;
background: rgba(255, 255, 255, 0.1);
}
.camera-hint {
position: fixed;
z-index: 6;
right: 26px;
bottom: 24px;
color: #526a80;
font-size: 8px;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: #050812;
transition:
opacity 450ms ease,
visibility 450ms ease;
}
#loading.hidden {
opacity: 0;
visibility: hidden;
}
.loader {
text-align: center;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.loader::before {
content: "";
display: block;
width: 34px;
height: 34px;
margin: 0 auto 16px;
border: 2px solid rgba(115, 231, 255, 0.16);
border-top-color: var(--accent);
border-radius: 50%;
animation: spin 900ms linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (max-width: 900px) {
.control-panel {
left: 14px;
width: 300px;
}
.analysis-panel {
right: 14px;
width: 274px;
}
.camera-hint {
display: none;
}
}
@media (max-width: 680px) {
.topbar {
height: 64px;
padding: 12px 14px;
}
.brand p,
.status-pill {
display: none;
}
.control-panel {
top: 72px;
left: 10px;
width: calc(100% - 20px);
padding: 13px;
}
.analysis-panel {
top: auto;
right: 10px;
bottom: 10px;
width: calc(100% - 20px);
padding: 13px;
}
.analysis-panel .truth-wrap,
.analysis-panel .footnote,
.analysis-panel .kicker {
display: none;
}
.analysis-panel .metric {
margin-bottom: 9px;
}
.comparison-strip {
display: none;
}
}
</style>
</head>
<body data-mode="discrete">
<div id="scene"></div>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>LOGIC / SCALE</h1>
<p>Electronic miniaturization explorer</p>
</div>
</div>
<div class="top-actions">
<div class="status-pill">
<span class="status-dot"></span>
Logic stable across forms
</div>
<button id="resetCamera" class="ghost-button" type="button">Reset view</button>
</div>
</header>
<section class="panel control-panel" aria-label="Circuit controls">
<div class="kicker">01 / Representation</div>
<button id="modeToggle" class="mode-card" type="button">
<div class="mode-card-top">
<div>
<small>Current implementation</small>
<strong id="modeValue">DISCRETE</strong>
</div>
<div id="modeIcon" class="mode-icon" aria-hidden="true">◫</div>
</div>
<p id="modeNote" class="mode-note">
Separate switches and long point-to-point jumpers.
</p>
</button>
<div class="section-divider"></div>
<div class="input-heading">
<span>02 / Inputs</span>
<button id="cycleInputs" type="button">Cycle truth table</button>
</div>
<div class="input-grid">
<button id="inputA" class="input-button" type="button" aria-pressed="false">
<div class="input-letter">A</div>
<div class="input-copy">
<span>Input A</span>
<strong id="valueA">0</strong>
</div>
</button>
<button id="inputB" class="input-button" type="button" aria-pressed="false">
<div class="input-letter">B</div>
<div class="input-copy">
<span>Input B</span>
<strong id="valueB">0</strong>
</div>
</button>
</div>
<div class="logic-expression">
<code>OUT = A ∧ B</code>
<div class="output-readout">
<span id="outputDot" class="output-dot"></span>
Out <span id="outputNumber" class="output-number">0</span>
</div>
</div>
</section>
<section class="panel analysis-panel" aria-label="Miniaturization measurements">
<div class="kicker">03 / Miniaturization</div>
<div class="metric">
<div class="metric-top">
<span class="metric-name">Connection length</span>
<span id="wireValue" class="metric-value">84 cm</span>
</div>
<div class="metric-track">
<div id="wireFill" class="metric-fill"></div>
</div>
<div class="metric-note">
<span id="wireNote">Individual jumpers</span>
<span>relative</span>
</div>
</div>
<div class="metric">
<div class="metric-top">
<span class="metric-name">Component grouping</span>
<span id="groupValue" class="metric-value">6 loose parts</span>
</div>
<div class="metric-track">
<div id="groupFill" class="metric-fill"></div>
</div>
<div class="metric-note">
<span id="groupNote">Open, distributed layout</span>
<span>relative</span>
</div>
</div>
<div class="metric">
<div class="metric-top">
<span class="metric-name">Board footprint</span>
<span id="footprintValue" class="metric-value">100%</span>
</div>
<div class="metric-track">
<div id="footprintFill" class="metric-fill"></div>
</div>
<div class="metric-note">
<span>Illustrative comparison</span>
<span id="scaleNote">1× scale</span>
</div>
</div>
<div class="truth-wrap">
<div class="truth-title">
<span>Boolean truth table</span>
<small>AND gate</small>
</div>
<div class="truth-table" id="truthTable">
<div class="truth-cell header">A</div>
<div class="truth-cell header">B</div>
<div class="truth-cell header">OUT</div>
<div class="truth-cell header">STATE</div>
<div class="truth-cell" data-a="0" data-b="0">0</div>
<div class="truth-cell" data-a="0" data-b="0">0</div>
<div class="truth-cell output-cell" data-a="0" data-b="0">0</div>
<div class="truth-cell" data-a="0" data-b="0">LOW</div>
<div class="truth-cell" data-a="0" data-b="1">0</div>
<div class="truth-cell" data-a="0" data-b="1">1</div>
<div class="truth-cell output-cell" data-a="0" data-b="1">0</div>
<div class="truth-cell" data-a="0" data-b="1">LOW</div>
<div class="truth-cell" data-a="1" data-b="0">1</div>
<div class="truth-cell" data-a="1" data-b="0">0</div>
<div class="truth-cell output-cell" data-a="1" data-b="0">0</div>
<div class="truth-cell" data-a="1" data-b="0">LOW</div>
<div class="truth-cell" data-a="1" data-b="1">1</div>
<div class="truth-cell" data-a="1" data-b="1">1</div>
<div class="truth-cell output-cell" data-a="1" data-b="1">1</div>
<div class="truth-cell" data-a="1" data-b="1">HIGH</div>
</div>
</div>
<p class="footnote">
The package view is a stylized cutaway. Values illustrate the change in
connection length, grouping, and physical scale.
</p>
</section>
<div class="comparison-strip" aria-hidden="true">
<span>Discrete</span>
<b id="comparisonMode">Open switch network</b>
<span class="comparison-divider"></span>
<span>Integrated</span>
<b>Monolithic AND IC</b>
</div>
<div class="camera-hint">
Drag to orbit · Scroll to zoom · Click A, B, or the output
</div>
<div id="loading">
<div class="loader">Building miniature circuit</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Application state
// ------------------------------------------------------------
const state = {
mode: "discrete",
a: false,
b: false
};
const getOutput = () => state.a && state.b;
const host = document.getElementById("scene");
const loading = document.getElementById("loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050812);
scene.fog = new THREE.Fog(0x050812, 15, 29);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
host.appendChild(renderer.domElement);
const world = new THREE.Group();
scene.add(world);
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xa7eaff, 0x111827, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.8);
keyLight.position.set(6, 10, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x55dfff, 12, 18, 2);
rimLight.position.set(-6, 4, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9f43, 8, 13, 2);
warmLight.position.set(5, 2.5, 3);
scene.add(warmLight);
// ------------------------------------------------------------
// Low-poly geometry helpers
// ------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.68,
metalness: 0.08,
flatShading: true,
...options
});
}
function addMesh(parent, geometry, material, position = [0, 0, 0]) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBox(parent, size, position, material) {
return addMesh(
parent,
new THREE.BoxGeometry(size[0], size[1], size[2]),
material,
position
);
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
position,
material,
segments = 8
) {
return addMesh(
parent,
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
material,
position
);
}
function addEdges(mesh, color = 0x8adfff, opacity = 0.32) {
const edgeMaterial = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 25),
edgeMaterial
);
mesh.add(edges);
return edges;
}
function roundedRectPath(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 makeLabel(text, accent = "#73e7ff", width = 1.45) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRectPath(ctx, 8, 12, 496, 104, 18);
ctx.fillStyle = "rgba(5, 12, 24, 0.86)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(24, 35, 6, 58);
ctx.fillStyle = "#edfaff";
ctx.font = "800 43px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 270, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * 0.25, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeWire(
parent,
points,
color,
radius = 0.026,
opacity = 1
) {
const group = new THREE.Group();
parent.add(group);
const material = standardMaterial(color, {
roughness: 0.46,
metalness: 0.28,
transparent: opacity < 1,
opacity
});
const vectors = points.map(
(point) =>
point.isVector3
? point.clone()
: new THREE.Vector3(point[0], point[1], point[2])
);
const up = new THREE.Vector3(0, 1, 0);
for (let i = 0; i < vectors.length - 1; i++) {
const start = vectors[i];
const end = vectors[i + 1];
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
if (length < 0.0001) continue;
const segment = addCylinder(
group,
radius,
radius,
length,
start.clone().add(end).multiplyScalar(0.5).toArray(),
material,
6
);
segment.quaternion.setFromUnitVectors(
up,
direction.clone().normalize()
);
}
for (let i = 1; i < vectors.length - 1; i++) {
addMesh(
group,
new THREE.SphereGeometry(radius * 1.5, 6, 4),
material,
vectors[i].toArray()
);
}
return group;
}
// ------------------------------------------------------------
// Common board and environment
// ------------------------------------------------------------
const groundMaterial = standardMaterial(0x070c15, {
roughness: 0.96,
metalness: 0
});
const ground = addMesh(
scene,
new THREE.CircleGeometry(19, 32),
groundMaterial,
[0, -0.08, 0]
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
const grid = new THREE.GridHelper(30, 30, 0x24425a, 0x142638);
grid.position.y = -0.065;
grid.material.transparent = true;
grid.material.opacity = 0.3;
scene.add(grid);
const boardMaterial = standardMaterial(0x123b40, {
roughness: 0.82,
metalness: 0.12
});
const board = addBox(
world,
[10.2, 0.25, 5.4],
[0, 0.125, 0],
boardMaterial
);
addEdges(board, 0x6be1e7, 0.24);
const boardSurfaceMaterial = standardMaterial(0x102f36, {
roughness: 0.86,
metalness: 0.08
});
const boardSurface = addBox(
world,
[10.02, 0.035, 5.22],
[0, 0.267, 0],
boardSurfaceMaterial
);
const boardGridPositions = [];
for (let x = -4.75; x <= 4.75; x += 0.5) {
boardGridPositions.push(x, 0.29, -2.55, x, 0.29, 2.55);
}
for (let z = -2.25; z <= 2.25; z += 0.5) {
boardGridPositions.push(-4.95, 0.29, z, 4.95, 0.29, z);
}
const boardGridGeometry = new THREE.BufferGeometry();
boardGridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(boardGridPositions, 3)
);
const boardGridMaterial = new THREE.LineBasicMaterial({
color: 0x73c9c7,
transparent: true,
opacity: 0.12
});
const boardGrid = new THREE.LineSegments(
boardGridGeometry,
boardGridMaterial
);
world.add(boardGrid);
const screwMaterial = standardMaterial(0xa5b8c5, {
roughness: 0.28,
metalness: 0.78
});
for (const x of [-4.73, 4.73]) {
for (const z of [-2.38, 2.38]) {
addCylinder(
world,
0.085,
0.085,
0.06,
[x, 0.31, z],
screwMaterial,
8
);
}
}
const discreteGroup = new THREE.Group();
const integratedGroup = new THREE.Group();
world.add(discreteGroup, integratedGroup);
const interactiveDiscrete = [];
const interactiveIntegrated = [];
const currentBeads = [];
// ------------------------------------------------------------
// Discrete implementation
// ------------------------------------------------------------
function markDiscrete(object, input) {
object.userData.input = input;
interactiveDiscrete.push(object);
return object;
}
function createSwitch(parent, x, z, color, input, label) {
const group = new THREE.Group();
group.position.set(x, 0, z);
parent.add(group);
const baseMaterial = standardMaterial(0x202c3b, {
roughness: 0.64,
metalness: 0.26
});
const base = addBox(
group,
[1.18, 0.16, 0.76],
[0, 0.37, 0],
baseMaterial
);
addEdges(base, 0x90a9b9, 0.18);
markDiscrete(base, input);
const stripe = addBox(
group,
[1.05, 0.035, 0.16],
[0, 0.465, 0.26],
standardMaterial(color, {
emissive: color,
emissiveIntensity: 0.08,
roughness: 0.38
})
);
markDiscrete(stripe, input);
const contactMaterial = standardMaterial(0xc5d4dc, {
roughness: 0.25,
metalness: 0.82
});
const leftContact = addCylinder(
group,
0.085,
0.11,
0.22,
[-0.38, 0.55, 0],
contactMaterial,
8
);
const rightContact = addCylinder(
group,
0.085,
0.11,
0.22,
[0.38, 0.55, 0],
contactMaterial,
8
);
markDiscrete(leftContact, input);
markDiscrete(rightContact, input);
const pivot = new THREE.Group();
pivot.position.set(-0.38, 0.68, 0);
group.add(pivot);
const leverMaterial = standardMaterial(color, {
roughness: 0.3,
metalness: 0.36,
emissive: color,
emissiveIntensity: 0.05
});
const lever = addBox(
pivot,
[0.78, 0.09, 0.16],
[0.39, 0, 0],
leverMaterial
);
markDiscrete(lever, input);
const knob = addMesh(
pivot,
new THREE.IcosahedronGeometry(0.13, 0),
leverMaterial,
[0.79, 0, 0]
);
markDiscrete(knob, input);
const labelSprite = makeLabel(label, "#73e7ff", 1.08);
labelSprite.position.set(0, 1.14, 0);
group.add(labelSprite);
markDiscrete(labelSprite, input);
return {
group,
pivot,
lever,
leverMaterial,
targetAngle: 0.38
};
}
function createLED(parent, x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
parent.add(group);
const base = addCylinder(
group,
0.27,
0.32,
0.28,
[0, 0.43, 0],
standardMaterial(0x151d29, {
roughness: 0.45,
metalness: 0.35
}),
8
);
const rim = addCylinder(
group,
0.29,
0.29,
0.055,
[0, 0.585, 0],
standardMaterial(0x8b99a7, {
roughness: 0.25,
metalness: 0.72
}),
8
);
const domeMaterial = standardMaterial(0x3b4658, {
roughness: 0.24,
metalness: 0.04,
emissive: 0xffa52b,
emissiveIntensity: 0
});
const dome = addMesh(
group,
new THREE.SphereGeometry(
0.255,
10,
5,
0,
Math.PI * 2,
0,
Math.PI / 2
),
domeMaterial,
[0, 0.59, 0]
);
dome.scale.y = 0.74;
const light = new THREE.PointLight(0xffb64a, 0, 3.6, 2);
light.position.set(0, 0.72, 0);
group.add(light);
const labelSprite = makeLabel("OUT", "#ffbd59", 1.03);
labelSprite.position.set(0, 1.17, 0);
group.add(labelSprite);
return { group, base, rim, dome, domeMaterial, light };
}
const discreteSwitchA = createSwitch(
discreteGroup,
-2.8,
1.8,
0x73e7ff,
"a",
"INPUT A"
);
const discreteSwitchB = createSwitch(
discreteGroup,
-2.8,
-1.8,
0xc694ff,
"b",
"INPUT B"
);
// Battery / supply block.
const battery = new THREE.Group();
battery.position.set(-4.12, 0, 1.8);
discreteGroup.add(battery);
const batteryBody = addBox(
battery,
[0.92, 0.42, 0.76],
[0, 0.5, 0],
standardMaterial(0x263242, {
roughness: 0.62,
metalness: 0.2
})
);
addEdges(batteryBody, 0x8499ab, 0.22);
addCylinder(
battery,
0.13,
0.16,
0.18,
[0.24, 0.79, 0],
standardMaterial(0xff657a, {
roughness: 0.32,
metalness: 0.45
}),
8
);
addCylinder(
battery,
0.13,
0.16,
0.18,
[-0.24, 0.79, 0],
standardMaterial(0x4b5a6c, {
roughness: 0.32,
metalness: 0.45
}),
8
);
const vccLabel = makeLabel("VCC", "#ff7085", 0.92);
vccLabel.position.set(0, 1.17, 0);
battery.add(vccLabel);
const discreteLED = createLED(discreteGroup, 2.55, 0);
// Long, visibly separate point-to-point connections.
makeWire(
discreteGroup,
[
[-3.73, 0.68, 1.8],
[-3.18, 0.68, 1.8]
],
0xff657a
);
makeWire(
discreteGroup,
[
[-2.42, 0.68, 1.8],
[-1.55, 0.43, 1.8],
[-1.55, 0.43, 2.58],
[-3.75, 0.43, 2.58],
[-3.75, 0.43, -1.8],
[-3.18, 0.68, -1.8]
],
0xffbd59
);
makeWire(
discreteGroup,
[
[-2.42, 0.68, -1.8],
[-0.48, 0.43, -1.8],
[-0.48, 0.43, 0],
[2.25, 0.64, 0]
],
0xff9b43
);
makeWire(
discreteGroup,
[
[2.85, 0.64, 0],
[3.55, 0.43, 0],
[3.55, 0.43, 2.24],
[-4.55, 0.43, 2.24],
[-4.55, 0.68, 1.8]
],
0x4c9de8
);
const discreteCurrentPath = [
[-3.73, 0.68, 1.8],
[-3.18, 0.68, 1.8],
[-2.42, 0.68, 1.8],
[-1.55, 0.43, 1.8],
[-1.55, 0.43, 2.58],
[-3.75, 0.43, 2.58],
[-3.75, 0.43, -1.8],
[-3.18, 0.68, -1.8],
[-2.42, 0.68, -1.8],
[-0.48, 0.43, -1.8],
[-0.48, 0.43, 0],
[2.25, 0.64, 0],
[2.85, 0.64, 0],
[3.55, 0.43, 0],
[3.55, 0.43, 2.24],
[-4.55, 0.43, 2.24],
[-4.55, 0.68, 1.8]
];
// ------------------------------------------------------------
// Integrated implementation
// ------------------------------------------------------------
function markIntegrated(object, input) {
object.userData.input = input;
interactiveIntegrated.push(object);
return object;
}
function createInputPad(parent, x, z, color, input, label) {
const group = new THREE.Group();
group.position.set(x, 0, z);
parent.add(group);
const material = standardMaterial(0x263449, {
roughness: 0.38,
metalness: 0.4,
emissive: color,
emissiveIntensity: 0.03
});
const pad = addBox(
group,
[0.52, 0.14, 0.5],
[0, 0.37, 0],
material
);
addEdges(pad, color, 0.42);
markIntegrated(pad, input);
const labelSprite = makeLabel(label, "#73e7ff", 1.02);
labelSprite.position.set(0, 0.98, 0);
group.add(labelSprite);
markIntegrated(labelSprite, input);
return { group, pad, material, color };
}
const packageMaterial = standardMaterial(0x111822, {
roughness: 0.56,
metalness: 0.28
});
const chipBody = addBox(
integratedGroup,
[2.76, 0.36, 1.84],
[0, 0.47, 0],
packageMaterial
);
addEdges(chipBody, 0x8ba1b3, 0.4);
const frameMaterial = standardMaterial(0x273240, {
roughness: 0.5,
metalness: 0.32
});
addBox(
integratedGroup,
[2.54, 0.12, 0.14],
[0, 0.69, 0.85],
frameMaterial
);
addBox(
integratedGroup,
[2.54, 0.12, 0.14],
[0, 0.69, -0.85],
frameMaterial
);
addBox(
integratedGroup,
[0.14, 0.12, 1.56],
[-1.2, 0.69, 0],
frameMaterial
);
addBox(
integratedGroup,
[0.14, 0.12, 1.56],
[1.2, 0.69, 0],
frameMaterial
);
const dieMaterial = standardMaterial(0x2e7869, {
roughness: 0.4,
metalness: 0.22,
emissive: 0x0b382f,
emissiveIntensity: 0.15
});
const die = addBox(
integratedGroup,
[2.18, 0.055, 1.24],
[0, 0.675, 0],
dieMaterial
);
addEdges(die, 0x8affcd, 0.45);
const pinMaterial = standardMaterial(0xc3a45e, {
roughness: 0.28,
metalness: 0.82
});
for (const side of [-1, 1]) {
for (let i = 0; i < 6; i++) {
const z = -0.7 + i * 0.28;
addBox(
integratedGroup,
[0.45, 0.075, 0.13],
[side * 1.59, 0.39, z],
pinMaterial
);
}
}
// Miniature transistor-like cells on the exposed die.
const transistorMaterial = standardMaterial(0x16242a, {
roughness: 0.4,
metalness: 0.48
});
const gateMaterial = standardMaterial(0x9ff2c8, {
roughness: 0.3,
metalness: 0.52,
emissive: 0x1b7a55,
emissiveIntensity: 0.12
});
for (const z of [-0.31, 0.31]) {
for (const x of [-0.69, -0.23, 0.23, 0.69]) {
addBox(
integratedGroup,
[0.29, 0.12, 0.2],
[x, 0.76, z],
transistorMaterial
);
addBox(
integratedGroup,
[0.045, 0.135, 0.14],
[x - 0.07, 0.775, z],
gateMaterial
);
}
}
const core = addBox(
integratedGroup,
[0.36, 0.15, 0.39],
[0, 0.79, 0],
standardMaterial(0x4ce2ae, {
roughness: 0.32,
metalness: 0.38,
emissive: 0x1e9b70,
emissiveIntensity: 0.18
})
);
addEdges(core, 0xb8ffe4, 0.4);
makeWire(
integratedGroup,
[
[-1.06, 0.73, -0.31],
[-0.85, 0.73, -0.31],
[-0.85, 0.73, 0],
[-0.18, 0.73, 0]
],
0xf3cf74,
0.014
);
makeWire(
integratedGroup,
[
[-1.06, 0.73, 0.31],
[-0.68, 0.73, 0.31],
[-0.68, 0.73, 0],
[-0.18, 0.73, 0]
],
0xf3cf74,
0.014
);
makeWire(
integratedGroup,
[
[0.18, 0.73, 0],
[0.82, 0.73, 0],
[1.06, 0.73, 0.14]
],
0xf3cf74,
0.014
);
const integratedPadA = createInputPad(
integratedGroup,
-2.18,
-0.62,
0x73e7ff,
"a",
"A"
);
const integratedPadB = createInputPad(
integratedGroup,
-2.18,
0.62,
0xc694ff,
"b",
"B"
);
const integratedLED = createLED(integratedGroup, 2.38, 0);
makeWire(
integratedGroup,
[
[-1.92, 0.43, -0.62],
[-1.73, 0.43, -0.62],
[-1.59, 0.43, -0.7]
],
0x73e7ff,
0.022
);
makeWire(
integratedGroup,
[
[-1.92, 0.43, 0.62],
[-1.73, 0.43, 0.62],
[-1.59, 0.43, 0.7]
],
0xc694ff,
0.022
);
makeWire(
integratedGroup,
[
[1.59, 0.43, 0.14],
[2.08, 0.43, 0.14],
[2.08, 0.64, 0]
],
0xffbd59,
0.022
);
makeWire(
integratedGroup,
[
[2.68, 0.64, 0],
[2.92, 0.43, 0],
[2.92, 0.43, -0.88],
[1.59, 0.43, -0.7]
],
0x4c9de8,
0.022
);
const integratedLabel = makeLabel("AND IC · 8 CELLS", "#79efb4", 2.15);
integratedLabel.position.set(0, 1.3, 0);
integratedGroup.add(integratedLabel);
const cutawayLabel = makeLabel("CUTAWAY DIE", "#79efb4", 1.15);
cutawayLabel.position.set(0, 0.92, 1.18);
integratedGroup.add(cutawayLabel);
const integratedCurrentPath = [
[-1.92, 0.47, -0.62],
[-1.58, 0.47, -0.7],
[-1.05, 0.75, -0.31],
[-0.65, 0.75, 0],
[0.18, 0.75, 0],
[0.82, 0.75, 0],
[1.58, 0.45, 0.14],
[2.08, 0.45, 0.14],
[2.08, 0.66, 0],
[2.68, 0.66, 0]
];
// ------------------------------------------------------------
// Animated current particles
// ------------------------------------------------------------
function preparePath(points) {
const vectors = points.map(
(point) => new THREE.Vector3(point[0], point[1], point[2])
);
const lengths = [];
let total = 0;
for (let i = 0; i < vectors.length - 1; i++) {
const length = vectors[i].distanceTo(vectors[i + 1]);
lengths.push(length);
total += length;
}
return { vectors, lengths, total };
}
function samplePath(path, t) {
let distance = ((t % 1) + 1) % 1 * path.total;
for (let i = 0; i < path.lengths.length; i++) {
if (distance <= path.lengths[i]) {
const local = path.lengths[i] === 0
? 0
: distance / path.lengths[i];
return new THREE.Vector3().lerpVectors(
path.vectors[i],
path.vectors[i + 1],
local
);
}
distance -= path.lengths[i];
}
return path.vectors[path.vectors.length - 1].clone();
}
function createCurrentBead(parent, pathPoints, color, offset, speed) {
const path = preparePath(pathPoints);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95
});
const bead = addMesh(
parent,
new THREE.IcosahedronGeometry(0.075, 0),
material,
path.vectors[0].toArray()
);
bead.castShadow = false;
currentBeads.push({
bead,
path,
offset,
speed
});
}
createCurrentBead(
discreteGroup,
discreteCurrentPath,
0xffe08b,
0,
0.17
);
createCurrentBead(
integratedGroup,
integratedCurrentPath,
0x9dffdb,
0.12,
0.42
);
createCurrentBead(
integratedGroup,
integratedCurrentPath,
0x73e7ff,
0.58,
0.48
);
// ------------------------------------------------------------
// Cross-fade support for switching representations
// ------------------------------------------------------------
function prepareFade(group) {
const materials = new Set();
group.traverse((object) => {
if (!object.material) return;
const list = Array.isArray(object.material)
? object.material
: [object.material];
for (const material of list) {
materials.add(material);
}
});
const entries = [];
for (const material of materials) {
entries.push({
material,
baseOpacity: material.opacity,
baseDepthWrite: material.depthWrite
});
material.transparent = true;
}
return entries;
}
const discreteFade = prepareFade(discreteGroup);
const integratedFade = prepareFade(integratedGroup);
function applyFade(entries, opacity) {
for (const entry of entries) {
entry.material.opacity = entry.baseOpacity * opacity;
entry.material.depthWrite =
opacity > 0.98 ? entry.baseDepthWrite : false;
}
}
let modeMix = 0;
let modeTransition = null;
const boardDiscreteColor = new THREE.Color(0x123b40);
const boardIntegratedColor = new THREE.Color(0x173e48);
const surfaceDiscreteColor = new THREE.Color(0x102f36);
const surfaceIntegratedColor = new THREE.Color(0x14313d);
// ------------------------------------------------------------
// UI and state synchronization
// ------------------------------------------------------------
const modeToggle = document.getElementById("modeToggle");
const modeValue = document.getElementById("modeValue");
const modeIcon = document.getElementById("modeIcon");
const modeNote = document.getElementById("modeNote");
const comparisonMode = document.getElementById("comparisonMode");
const inputAButton = document.getElementById("inputA");
const inputBButton = document.getElementById("inputB");
const valueA = document.getElementById("valueA");
const valueB = document.getElementById("valueB");
const outputDot = document.getElementById("outputDot");
const outputNumber = document.getElementById("outputNumber");
const wireValue = document.getElementById("wireValue");
const wireFill = document.getElementById("wireFill");
const wireNote = document.getElementById("wireNote");
const groupValue = document.getElementById("groupValue");
const groupFill = document.getElementById("groupFill");
const groupNote = document.getElementById("groupNote");
const footprintValue = document.getElementById("footprintValue");
const footprintFill = document.getElementById("footprintFill");
const scaleNote = document.getElementById("scaleNote");
function updateUI() {
const output = getOutput();
inputAButton.classList.toggle("on", state.a);
inputBButton.classList.toggle("on", state.b);
inputAButton.setAttribute("aria-pressed", String(state.a));
inputBButton.setAttribute("aria-pressed", String(state.b));
valueA.textContent = state.a ? "1" : "0";
valueB.textContent = state.b ? "1" : "0";
outputDot.classList.toggle("on", output);
outputNumber.textContent = output ? "1" : "0";
document.querySelectorAll(".truth-cell[data-a]").forEach((cell) => {
const matches =
Number(cell.dataset.a) === Number(state.a) &&
Number(cell.dataset.b) === Number(state.b);
cell.classList.toggle("active", matches);
});
const integrated = state.mode === "integrated";
document.body.dataset.mode = state.mode;
modeValue.textContent = integrated ? "INTEGRATED" : "DISCRETE";
modeIcon.textContent = integrated ? "▣" : "◫";
modeNote.textContent = integrated
? "Many microscopic cells, short on-die paths, one package."
: "Separate switches and long point-to-point jumpers.";
comparisonMode.textContent = integrated
? "Monolithic package"
: "Open switch network";
wireValue.textContent = integrated ? "0.8 cm" : "84 cm";
wireFill.style.width = integrated ? "1.5%" : "100%";
wireNote.textContent = integrated
? "On-die metallization"
: "Individual jumpers";
groupValue.textContent = integrated ? "1 IC package" : "6 loose parts";
groupFill.style.width = integrated ? "16%" : "100%";
groupNote.textContent = integrated
? "8 cells grouped internally"
: "Open, distributed layout";
footprintValue.textContent = integrated ? "7%" : "100%";
footprintFill.style.width = integrated ? "7%" : "100%";
scaleNote.textContent = integrated ? "105× shorter paths" : "1× scale";
}
function syncCircuitVisuals() {
const output = getOutput();
discreteSwitchA.targetAngle = state.a ? 0 : 0.38;
discreteSwitchB.targetAngle = state.b ? 0 : 0.38;
discreteSwitchA.leverMaterial.emissiveIntensity = state.a ? 0.72 : 0.05;
discreteSwitchB.leverMaterial.emissiveIntensity = state.b ? 0.72 : 0.05;
function syncPad(pad, on) {
pad.material.color.setHex(on ? pad.color : 0x263449);
pad.material.emissiveIntensity = on ? 0.65 : 0.03;
}
syncPad(integratedPadA, state.a);
syncPad(integratedPadB, state.b);
function syncLED(led) {
led.domeMaterial.color.setHex(output ? 0xffd46c : 0x3b4658);
led.domeMaterial.emissiveIntensity = output ? 2.2 : 0;
led.light.intensity = output ? 1.35 : 0;
}
syncLED(discreteLED);
syncLED(integratedLED);
}
function setInputs(a, b) {
state.a = Boolean(a);
state.b = Boolean(b);
syncCircuitVisuals();
updateUI();
}
function toggleInput(input) {
if (input === "a") {
state.a = !state.a;
} else if (input === "b") {
state.b = !state.b;
} else {
return;
}
syncCircuitVisuals();
updateUI();
}
function toggleMode() {
const target = state.mode === "discrete" ? 1 : 0;
state.mode = target === 1 ? "integrated" : "discrete";
modeTransition = {
from: modeMix,
to: target,
start: performance.now()
};
updateUI();
}
modeToggle.addEventListener("click", toggleMode);
inputAButton.addEventListener("click", () => toggleInput("a"));
inputBButton.addEventListener("click", () => toggleInput("b"));
// Truth-table cycling.
const cycleButton = document.getElementById("cycleInputs");
const cycleSequence = [
[false, false],
[true, false],
[false, true],
[true, true]
];
let cycleTimer = null;
let cycleIndex = 0;
cycleButton.addEventListener("click", () => {
if (cycleTimer) {
clearInterval(cycleTimer);
cycleTimer = null;
cycleButton.textContent = "Cycle truth table";
return;
}
cycleIndex = 0;
setInputs(...cycleSequence[cycleIndex]);
cycleButton.textContent = "Stop cycling";
cycleTimer = setInterval(() => {
cycleIndex = (cycleIndex + 1) % cycleSequence.length;
setInputs(...cycleSequence[cycleIndex]);
}, 900);
});
updateUI();
syncCircuitVisuals();
// ------------------------------------------------------------
// Custom camera controls — no external controls module
// ------------------------------------------------------------
const orbit = {
yaw: 0.54,
pitch: 0.59,
radius: 13.4,
target: new THREE.Vector3(0, 0.25, 0),
desiredYaw: 0.54,
desiredPitch: 0.59,
desiredRadius: 13.4
};
function updateCamera() {
orbit.yaw += (orbit.desiredYaw - orbit.yaw) * 0.09;
orbit.pitch += (orbit.desiredPitch - orbit.pitch) * 0.09;
orbit.radius += (orbit.desiredRadius - orbit.radius) * 0.09;
const horizontal = orbit.radius * Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontal,
orbit.target.y + Math.sin(orbit.pitch) * orbit.radius,
orbit.target.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.desiredYaw = 0.54;
orbit.desiredPitch = 0.59;
orbit.desiredRadius = 13.4;
}
document
.getElementById("resetCamera")
.addEventListener("click", resetCamera);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredRadius = THREE.MathUtils.clamp(
orbit.desiredRadius + event.deltaY * 0.009,
7.5,
21
);
},
{ passive: false }
);
// ------------------------------------------------------------
// Raycast interaction with circuit inputs
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = false;
let lastPointerX = 0;
let lastPointerY = 0;
function pointerToNDC(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;
}
function findInput(object) {
let current = object;
while (current) {
if (current.userData.input) {
return current.userData.input;
}
current = current.parent;
}
return null;
}
function raycastInput(event) {
pointerToNDC(event);
raycaster.setFromCamera(pointer, camera);
const objects =
state.mode === "discrete"
? interactiveDiscrete
: interactiveIntegrated;
const hits = raycaster.intersectObjects(objects, false);
return hits.length ? findInput(hits[0].object) : null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) return;
pointerDown = true;
pointerMoved = false;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
if (Math.abs(dx) + Math.abs(dy) > 3) {
pointerMoved = true;
}
orbit.desiredYaw -= dx * 0.006;
orbit.desiredPitch = THREE.MathUtils.clamp(
orbit.desiredPitch + dy * 0.005,
0.24,
1.2
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else {
const hit = raycastInput(event);
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerDown && !pointerMoved) {
const input = raycastInput(event);
if (input) toggleInput(input);
}
pointerDown = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
const hit = raycastInput(event);
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
// ------------------------------------------------------------
// Resize
// ------------------------------------------------------------
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", onResize);
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const now = performance.now();
const elapsed = clock.getElapsedTime();
if (modeTransition) {
const progress = THREE.MathUtils.clamp(
(now - modeTransition.start) / 680,
0,
1
);
const eased = 1 - Math.pow(1 - progress, 3);
modeMix = THREE.MathUtils.lerp(
modeTransition.from,
modeTransition.to,
eased
);
if (progress >= 1) {
modeMix = modeTransition.to;
modeTransition = null;
}
}
const discreteOpacity = 1 - modeMix;
const integratedOpacity = modeMix;
applyFade(discreteFade, discreteOpacity);
applyFade(integratedFade, integratedOpacity);
discreteGroup.visible = discreteOpacity > 0.002;
integratedGroup.visible = integratedOpacity > 0.002;
discreteGroup.scale.setScalar(1 - modeMix * 0.08);
discreteGroup.position.y = -modeMix * 0.16;
integratedGroup.scale.setScalar(0.92 + modeMix * 0.08);
integratedGroup.position.y = (1 - modeMix) * 0.28;
boardMaterial.color
.copy(boardDiscreteColor)
.lerp(boardIntegratedColor, modeMix);
boardSurfaceMaterial.color
.copy(surfaceDiscreteColor)
.lerp(surfaceIntegratedColor, modeMix);
boardGridMaterial.opacity = 0.12 + modeMix * 0.025;
// Smooth switch movement.
discreteSwitchA.lever.rotation.z = THREE.MathUtils.lerp(
discreteSwitchA.lever.rotation.z,
discreteSwitchA.targetAngle,
0.14
);
discreteSwitchB.lever.rotation.z = THREE.MathUtils.lerp(
discreteSwitchB.lever.rotation.z,
discreteSwitchB.targetAngle,
0.14
);
// Animated current beads.
for (const item of currentBeads) {
item.bead.visible = getOutput();
if (!item.bead.visible) continue;
const t = elapsed * item.speed + item.offset;
const position = samplePath(item.path, t);
item.bead.position.copy(position);
item.bead.rotation.x += 0.04;
item.bead.rotation.y += 0.06;
}
updateCamera();
renderer.render(scene, camera);
}
updateCamera();
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
});
</script>
</body>
</html>
```