Prompt specification
Build or customize the Three.js 3D scene "Build a simple logic function from switches" 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: Build a simple logic function from switches
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/build-a-simple-logic-function-from-switches-x2552
Scene Description Prompt:
Let users toggle two inputs in an idealized transistor-switch model. Display output states and a truth table, keeping the educational logic separate from device-level semiconductor simulation. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (build-a-simple-logic-function-from-switches-x2552):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Logic Lab — Ideal Switch Model</title>
<style>
:root {
--bg: #080e19;
--panel: #101a2b;
--panel-2: #142136;
--line: rgba(170, 196, 229, 0.14);
--text: #edf5ff;
--muted: #91a4bd;
--mint: #59f2c3;
--mint-soft: rgba(89, 242, 195, 0.14);
--blue: #67a8ff;
--pink: #ff7fa8;
--amber: #ffbd73;
--red: #ff7185;
--shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
min-height: 100vh;
display: flex;
flex-direction: column;
}
.topbar {
min-height: 72px;
padding: 14px 22px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
border-bottom: 1px solid var(--line);
background:
linear-gradient(90deg, rgba(89, 242, 195, 0.045), transparent 36%),
#0b1321;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 40px;
height: 40px;
flex: 0 0 auto;
border: 1px solid rgba(89, 242, 195, 0.35);
border-radius: 11px;
background:
linear-gradient(135deg, rgba(89, 242, 195, 0.19), rgba(103, 168, 255, 0.06)),
#101d2e;
box-shadow:
inset 0 0 22px rgba(89, 242, 195, 0.07),
0 8px 24px rgba(0, 0, 0, 0.25);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--mint);
border-radius: 2px;
}
.brand-mark::before {
width: 15px;
height: 15px;
left: 10px;
top: 10px;
box-shadow: 6px 6px 0 rgba(103, 168, 255, 0.85);
}
.brand-mark::after {
width: 5px;
height: 5px;
right: 7px;
bottom: 7px;
background: var(--pink);
}
.brand h1 {
margin: 0;
font-size: 16px;
line-height: 1.2;
letter-spacing: 0.01em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 12px;
}
.model-tag {
display: flex;
align-items: center;
gap: 9px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
color: #b8c8dc;
background: rgba(12, 22, 37, 0.74);
font-size: 11px;
white-space: nowrap;
}
.model-tag .dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 12px var(--mint);
}
main {
flex: 1;
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
}
.scene-panel {
position: relative;
min-width: 0;
min-height: 620px;
overflow: hidden;
background:
radial-gradient(circle at 45% 35%, rgba(48, 83, 123, 0.22), transparent 48%),
#080e19;
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.scene-top {
position: absolute;
top: 18px;
left: 18px;
right: 18px;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
pointer-events: none;
}
.scene-badge {
padding: 9px 12px;
border: 1px solid rgba(157, 185, 220, 0.15);
border-radius: 10px;
color: #a9bad0;
background: rgba(8, 15, 27, 0.72);
backdrop-filter: blur(12px);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scene-badge span {
color: var(--mint);
}
.floating-output {
min-width: 145px;
padding: 10px 13px;
display: flex;
align-items: center;
gap: 10px;
border: 1px solid rgba(255, 113, 133, 0.22);
border-radius: 12px;
background: rgba(8, 15, 27, 0.78);
backdrop-filter: blur(12px);
box-shadow: var(--shadow);
}
.floating-output .mini-lamp {
width: 11px;
height: 11px;
border-radius: 3px;
background: var(--red);
box-shadow: 0 0 10px rgba(255, 113, 133, 0.4);
transition: 180ms ease;
}
.floating-output.high {
border-color: rgba(89, 242, 195, 0.24);
}
.floating-output.high .mini-lamp {
background: var(--mint);
box-shadow: 0 0 14px rgba(89, 242, 195, 0.8);
}
.floating-output small {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.floating-output strong {
display: block;
margin-top: 2px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
}
.scene-bottom {
position: absolute;
left: 18px;
right: 18px;
bottom: 18px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 14px;
pointer-events: none;
}
.scene-help {
max-width: 430px;
padding: 10px 12px;
border: 1px solid rgba(157, 185, 220, 0.13);
border-radius: 10px;
color: #9bacc2;
background: rgba(8, 15, 27, 0.72);
backdrop-filter: blur(10px);
font-size: 11px;
line-height: 1.5;
}
.scene-help strong {
color: #dce8f7;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-top: 7px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
white-space: nowrap;
}
.legend i {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--mint);
}
.legend span:nth-child(2) i {
background: var(--pink);
}
.legend span:nth-child(3) i {
background: var(--amber);
}
.scene-actions {
display: flex;
gap: 8px;
pointer-events: auto;
}
.small-button {
border: 1px solid rgba(157, 185, 220, 0.16);
border-radius: 9px;
padding: 9px 11px;
color: #c6d4e6;
background: rgba(8, 15, 27, 0.78);
backdrop-filter: blur(10px);
cursor: pointer;
font-size: 11px;
transition: 150ms ease;
}
.small-button:hover {
border-color: rgba(89, 242, 195, 0.38);
color: white;
background: rgba(18, 34, 52, 0.92);
}
.sidebar {
min-height: 0;
overflow-y: auto;
padding: 16px;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(20, 33, 54, 0.76), rgba(10, 18, 31, 0.96)),
var(--panel);
scrollbar-width: thin;
scrollbar-color: #2d405a transparent;
}
.panel {
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(14, 25, 42, 0.92);
box-shadow: var(--shadow);
overflow: hidden;
}
.panel + .panel {
margin-top: 14px;
}
.panel-header {
padding: 16px 16px 13px;
border-bottom: 1px solid var(--line);
background:
linear-gradient(90deg, rgba(89, 242, 195, 0.06), transparent 72%);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 6px;
color: var(--mint);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 17px;
height: 1px;
background: currentColor;
}
.device-panel .eyebrow {
color: var(--blue);
}
.device-panel .panel-header {
background:
linear-gradient(90deg, rgba(103, 168, 255, 0.07), transparent 72%);
}
.panel h2 {
margin: 0;
font-size: 15px;
letter-spacing: -0.01em;
}
.panel-header p {
margin: 6px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.panel-body {
padding: 15px 16px 17px;
}
.equation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
padding: 12px 13px;
border: 1px solid rgba(89, 242, 195, 0.15);
border-radius: 12px;
background: var(--mint-soft);
}
.equation code {
color: #dffff4;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 16px;
font-weight: 700;
}
.equation small {
color: #8dbbaa;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.input-list {
display: grid;
gap: 8px;
}
.logic-toggle {
width: 100%;
min-height: 59px;
padding: 10px 11px;
display: grid;
grid-template-columns: 1fr auto 32px;
align-items: center;
gap: 10px;
border: 1px solid var(--line);
border-radius: 12px;
color: var(--text);
background: #111d30;
cursor: pointer;
text-align: left;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.logic-toggle:hover {
border-color: rgba(89, 242, 195, 0.28);
background: #15243a;
}
.logic-toggle:active {
transform: translateY(1px);
}
.logic-toggle:focus-visible,
.truth-table tr:focus-visible,
.small-button:focus-visible {
outline: 2px solid var(--mint);
outline-offset: 2px;
}
.toggle-copy strong {
display: block;
font-size: 12px;
}
.toggle-copy small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.toggle-track {
position: relative;
width: 40px;
height: 22px;
border: 1px solid #33445d;
border-radius: 999px;
background: #1b283b;
transition: 160ms ease;
}
.toggle-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 14px;
height: 14px;
border-radius: 50%;
background: #8294aa;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.35);
transition: 180ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.logic-toggle[aria-checked="true"] .toggle-track {
border-color: rgba(89, 242, 195, 0.6);
background: rgba(89, 242, 195, 0.18);
}
.logic-toggle[aria-checked="true"] .toggle-track::after {
transform: translateX(18px);
background: var(--mint);
box-shadow: 0 0 10px rgba(89, 242, 195, 0.55);
}
.bit {
width: 30px;
height: 30px;
display: grid;
place-items: center;
border: 1px solid var(--line);
border-radius: 8px;
color: #9cacc0;
background: #0d1726;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
font-weight: 800;
}
.logic-toggle[aria-checked="true"] .bit {
border-color: rgba(89, 242, 195, 0.28);
color: var(--mint);
background: rgba(89, 242, 195, 0.08);
}
.output-card {
margin-top: 13px;
padding: 13px;
display: grid;
grid-template-columns: auto 1fr;
align-items: center;
gap: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: #0d1727;
}
.output-lamp {
width: 43px;
height: 43px;
border-radius: 10px;
border: 1px solid rgba(255, 113, 133, 0.3);
background:
linear-gradient(145deg, rgba(255, 113, 133, 0.24), rgba(255, 113, 133, 0.06)),
#1c2535;
box-shadow: inset 0 0 18px rgba(255, 113, 133, 0.08);
transition: 180ms ease;
}
.output-lamp.high {
border-color: rgba(89, 242, 195, 0.55);
background: var(--mint);
box-shadow:
inset 0 0 12px rgba(255, 255, 255, 0.42),
0 0 22px rgba(89, 242, 195, 0.32);
}
.output-copy small {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.output-copy strong {
display: block;
margin-top: 3px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 16px;
}
.section-label {
margin: 17px 0 8px;
color: #b7c6d9;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.truth-table {
width: 100%;
border-collapse: separate;
border-spacing: 0;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 10px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
}
.truth-table th {
padding: 9px 8px;
color: #8195af;
background: #0d1726;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.truth-table th + th,
.truth-table td + td {
border-left: 1px solid var(--line);
}
.truth-table td {
padding: 9px 8px;
color: #b9c8da;
background: rgba(16, 28, 46, 0.78);
text-align: center;
}
.truth-table tbody tr {
cursor: pointer;
}
.truth-table tbody tr:hover td {
background: rgba(30, 48, 72, 0.82);
}
.truth-table tbody tr.current td {
color: white;
background: rgba(89, 242, 195, 0.1);
}
.truth-table tbody tr.current td:first-child {
box-shadow: inset 3px 0 var(--mint);
}
.truth-table .result-low {
color: var(--red);
}
.truth-table .result-high {
color: var(--mint);
font-weight: 800;
}
.table-hint {
margin-top: 7px;
color: #73869e;
font-size: 10px;
line-height: 1.45;
}
.schematic {
padding: 14px 10px 12px;
border: 1px solid var(--line);
border-radius: 11px;
background:
linear-gradient(rgba(103, 168, 255, 0.035) 1px, transparent 1px),
linear-gradient(90deg, rgba(103, 168, 255, 0.035) 1px, transparent 1px),
#0c1727;
background-size: 14px 14px;
}
.schematic-row {
display: flex;
align-items: center;
justify-content: center;
gap: 5px;
}
.rail,
.node {
min-width: 37px;
padding: 7px 5px;
border: 1px solid rgba(103, 168, 255, 0.28);
border-radius: 6px;
color: #b9d5ff;
background: rgba(103, 168, 255, 0.08);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
text-align: center;
}
.switch-symbol {
width: 34px;
height: 27px;
display: grid;
place-items: center;
border: 1px solid rgba(255, 189, 115, 0.36);
border-radius: 5px;
color: var(--amber);
background: rgba(255, 189, 115, 0.07);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
font-weight: 800;
}
.wire {
width: 11px;
height: 2px;
background: #49627f;
}
.ground-branch {
width: 1px;
height: 13px;
margin: 0 auto;
background: #49627f;
}
.pull-down {
width: 58px;
margin: 0 auto;
padding: 5px 0;
border-top: 2px solid #49627f;
border-bottom: 2px solid #49627f;
color: #8298b3;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 8px;
text-align: center;
text-transform: uppercase;
}
.device-readout {
display: grid;
gap: 7px;
margin-top: 12px;
}
.device-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 8px 10px;
border: 1px solid rgba(103, 168, 255, 0.1);
border-radius: 8px;
color: #8fa3ba;
background: rgba(11, 21, 35, 0.72);
font-size: 10px;
}
.device-line strong {
color: var(--amber);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
}
.device-line strong.closed,
.device-line strong.complete {
color: var(--mint);
}
.boundary-note {
margin-top: 13px;
padding: 11px 12px;
border: 1px solid rgba(103, 168, 255, 0.17);
border-radius: 10px;
color: #93a9c4;
background: rgba(103, 168, 255, 0.055);
font-size: 10px;
line-height: 1.55;
}
.boundary-note strong {
display: block;
margin-bottom: 4px;
color: #c6dbf7;
font-size: 10px;
}
.no-physics {
margin-top: 9px;
padding: 8px 10px;
border: 1px solid rgba(255, 189, 115, 0.14);
border-radius: 8px;
color: #bba181;
background: rgba(255, 189, 115, 0.045);
font-size: 9px;
line-height: 1.5;
}
@media (max-width: 920px) {
main {
display: block;
}
.scene-panel {
height: 58vh;
min-height: 470px;
}
.sidebar {
border-left: 0;
border-top: 1px solid var(--line);
overflow: visible;
}
}
@media (max-width: 620px) {
.topbar {
padding: 12px 14px;
}
.model-tag {
display: none;
}
.scene-panel {
height: 52vh;
min-height: 420px;
}
.scene-top {
top: 12px;
left: 12px;
right: 12px;
}
.scene-bottom {
left: 12px;
right: 12px;
bottom: 12px;
}
.scene-help {
max-width: 260px;
}
.scene-actions {
flex-direction: column;
}
.legend {
display: none;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>Voxel Logic Lab</h1>
<p>Two-input logic · ideal switch abstraction</p>
</div>
</div>
<div class="model-tag">
<span class="dot"></span>
Binary model only — no semiconductor physics
</div>
</header>
<main>
<section class="scene-panel" aria-label="Interactive voxel switch scene">
<div id="scene"></div>
<div class="scene-top">
<div class="scene-badge">
<span>Device abstraction</span> · closed / open switches
</div>
<div class="floating-output" id="floatingOutput">
<span class="mini-lamp"></span>
<div>
<small>Output Y</small>
<strong id="floatingOutputText">0 · LOW</strong>
</div>
</div>
</div>
<div class="scene-bottom">
<div class="scene-help">
<strong>Drag</strong> to orbit · <strong>scroll</strong> to zoom ·
<strong>click a voxel switch</strong> to toggle it.
<div class="legend">
<span><i></i> active path</span>
<span><i></i> control signal</span>
<span><i></i> open switch</span>
</div>
</div>
<div class="scene-actions">
<button class="small-button" id="resetView" type="button">
Reset view
</button>
</div>
</div>
</section>
<aside class="sidebar">
<section class="panel logic-panel">
<div class="panel-header">
<div class="eyebrow">Layer 01</div>
<h2>Educational logic</h2>
<p>
Boolean inputs, Boolean output, and the complete truth table.
This layer contains no device or semiconductor behavior.
</p>
</div>
<div class="panel-body">
<div class="equation">
<code>Y = A · B</code>
<small>Two-input AND</small>
</div>
<div class="input-list">
<button
class="logic-toggle"
id="toggleA"
type="button"
role="switch"
aria-checked="false"
aria-label="Toggle input A"
>
<span class="toggle-copy">
<strong>Input A</strong>
<small>Controls switch A</small>
</span>
<span class="toggle-track" aria-hidden="true"></span>
<span class="bit" id="aValue">0</span>
</button>
<button
class="logic-toggle"
id="toggleB"
type="button"
role="switch"
aria-checked="false"
aria-label="Toggle input B"
>
<span class="toggle-copy">
<strong>Input B</strong>
<small>Controls switch B</small>
</span>
<span class="toggle-track" aria-hidden="true"></span>
<span class="bit" id="bValue">0</span>
</button>
</div>
<div class="output-card">
<div class="output-lamp" id="outputLamp" aria-hidden="true"></div>
<div class="output-copy">
<small>Boolean output</small>
<strong id="outputText">0 · LOW</strong>
</div>
</div>
<div class="section-label">Truth table</div>
<table class="truth-table" aria-label="Two-input AND truth table">
<thead>
<tr>
<th scope="col">A</th>
<th scope="col">B</th>
<th scope="col">Y</th>
</tr>
</thead>
<tbody id="truthBody"></tbody>
</table>
<div class="table-hint">
Select any row to load that input combination.
</div>
</div>
</section>
<section class="panel device-panel">
<div class="panel-header">
<div class="eyebrow">Layer 02</div>
<h2>Ideal switch abstraction</h2>
<p>
A separate visual model explains the output using two series
ideal switches and a pull-down load.
</p>
</div>
<div class="panel-body">
<div class="schematic" aria-label="High rail through switches A and B to output Y, with pull-down">
<div class="schematic-row">
<div class="rail">HIGH</div>
<div class="wire"></div>
<div class="switch-symbol">A</div>
<div class="wire"></div>
<div class="switch-symbol">B</div>
<div class="wire"></div>
<div class="node">Y</div>
</div>
<div class="ground-branch"></div>
<div class="pull-down">Pull-down</div>
</div>
<div class="device-readout">
<div class="device-line">
<span>Switch A</span>
<strong id="deviceA">Open</strong>
</div>
<div class="device-line">
<span>Switch B</span>
<strong id="deviceB">Open</strong>
</div>
<div class="device-line">
<span>Series path</span>
<strong id="devicePath">Interrupted</strong>
</div>
<div class="device-line">
<span>Device output</span>
<strong id="deviceOutput">Low</strong>
</div>
</div>
<div class="boundary-note">
<strong>Explicit model boundary</strong>
The truth table evaluates Boolean values. The scene renders
symbolic closed/open switches. They are separate layers with
matching behavior for this example.
</div>
<div class="no-physics">
No voltages, current, resistance, capacitance, switching delay,
leakage, carrier density, body effect, or transistor equations
are simulated.
</div>
</div>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/*
* Layer 01 — Educational logic
* Pure Boolean evaluation and truth-table generation.
*/
const EducationalLogic = Object.freeze({
evaluate(a, b) {
return Boolean(a) && Boolean(b);
},
rows: [
[false, false],
[false, true],
[true, false],
[true, true]
]
});
/*
* Layer 02 — Idealized device abstraction
* Switches are binary: closed or open. This is intentionally not a
* semiconductor or electrical simulation.
*/
const IdealSwitchDevice = Object.freeze({
snapshot(a, b) {
const closedA = Boolean(a);
const closedB = Boolean(b);
return {
closedA,
closedB,
pathClosed: closedA && closedB,
outputHigh: closedA && closedB
};
}
});
const state = {
a: false,
b: false
};
const toggleA = document.querySelector("#toggleA");
const toggleB = document.querySelector("#toggleB");
const aValue = document.querySelector("#aValue");
const bValue = document.querySelector("#bValue");
const outputLamp = document.querySelector("#outputLamp");
const outputText = document.querySelector("#outputText");
const floatingOutput = document.querySelector("#floatingOutput");
const floatingOutputText = document.querySelector("#floatingOutputText");
const truthBody = document.querySelector("#truthBody");
const deviceA = document.querySelector("#deviceA");
const deviceB = document.querySelector("#deviceB");
const devicePath = document.querySelector("#devicePath");
const deviceOutput = document.querySelector("#deviceOutput");
function makeTruthTable() {
EducationalLogic.rows.forEach(([a, b]) => {
const result = EducationalLogic.evaluate(a, b);
const row = document.createElement("tr");
row.tabIndex = 0;
row.setAttribute("role", "button");
row.setAttribute(
"aria-label",
`Load input A ${Number(a)}, input B ${Number(b)}, output ${Number(result)}`
);
row.innerHTML = `
<td>${Number(a)}</td>
<td>${Number(b)}</td>
<td class="${result ? "result-high" : "result-low"}">
${Number(result)}
</td>
`;
const loadState = () => {
state.a = a;
state.b = b;
refresh();
};
row.addEventListener("click", loadState);
row.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
loadState();
}
});
truthBody.appendChild(row);
});
}
function refresh() {
const output = EducationalLogic.evaluate(state.a, state.b);
const device = IdealSwitchDevice.snapshot(state.a, state.b);
toggleA.setAttribute("aria-checked", String(state.a));
toggleB.setAttribute("aria-checked", String(state.b));
aValue.textContent = Number(state.a);
bValue.textContent = Number(state.b);
outputLamp.classList.toggle("high", output);
floatingOutput.classList.toggle("high", output);
const outputLabel = `${Number(output)} · ${output ? "HIGH" : "LOW"}`;
outputText.textContent = outputLabel;
floatingOutputText.textContent = outputLabel;
[...truthBody.children].forEach((row, index) => {
const [a, b] = EducationalLogic.rows[index];
row.classList.toggle("current", a === state.a && b === state.b);
});
deviceA.textContent = device.closedA ? "Closed" : "Open";
deviceB.textContent = device.closedB ? "Closed" : "Open";
devicePath.textContent = device.pathClosed ? "Complete" : "Interrupted";
deviceOutput.textContent = device.outputHigh ? "Pulled high" : "Pulled low";
deviceA.classList.toggle("closed", device.closedA);
deviceB.classList.toggle("closed", device.closedB);
devicePath.classList.toggle("complete", device.pathClosed);
deviceOutput.classList.toggle("closed", device.outputHigh);
}
toggleA.addEventListener("click", () => {
state.a = !state.a;
refresh();
});
toggleB.addEventListener("click", () => {
state.b = !state.b;
refresh();
});
makeTruthTable();
refresh();
// -------------------------------------------------------------------------
// Three.js voxel scene
// -------------------------------------------------------------------------
const container = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080e19);
scene.fog = new THREE.Fog(0x080e19, 20, 39);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const hemisphere = new THREE.HemisphereLight(0xb8d8ff, 0x172033, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x578dff, 1.7);
rimLight.position.set(8, 6, -8);
scene.add(rimLight);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
function cube(
parent,
width,
height,
depth,
x,
y,
z,
material,
options = {}
) {
const mesh = new THREE.Mesh(cubeGeometry, material);
mesh.position.set(x, y, z);
mesh.scale.set(width, height, depth);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.62,
metalness: options.metalness ?? 0.18,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
depthWrite: options.depthWrite ?? true,
side: options.side ?? THREE.FrontSide
});
}
function label(text, width = 2.4, options = {}) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 150;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: options.depthTest ?? false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * (150 / 640), 1);
sprite.renderOrder = 20;
function draw(nextText, foreground = options.foreground ?? "#dceaff") {
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
const background = options.background ?? "rgba(10, 20, 34, 0.88)";
ctx.fillStyle = background;
ctx.fillRect(3, 4, canvas.width - 6, canvas.height - 8);
ctx.strokeStyle = options.border ?? "rgba(148, 180, 220, 0.24)";
ctx.lineWidth = 3;
ctx.strokeRect(3, 4, canvas.width - 6, canvas.height - 8);
ctx.fillStyle = foreground;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font =
options.bold === false
? '500 48px ui-monospace, SFMono-Regular, Menlo, monospace'
: '700 48px ui-monospace, SFMono-Regular, Menlo, monospace';
ctx.fillText(
nextText,
canvas.width / 2,
canvas.height / 2 + 2,
canvas.width - 38
);
texture.needsUpdate = true;
}
draw(text);
return {
sprite,
set: draw
};
}
const materials = {
ground: standardMaterial(0x0b1320, {
roughness: 0.95,
metalness: 0.02
}),
board: standardMaterial(0x15243a, {
roughness: 0.76,
metalness: 0.24
}),
boardEdge: standardMaterial(0x263a56, {
roughness: 0.68,
metalness: 0.3
}),
chip: standardMaterial(0x1a2c45, {
roughness: 0.62,
metalness: 0.34
}),
chipTop: standardMaterial(0x27415e, {
roughness: 0.52,
metalness: 0.38
}),
copper: standardMaterial(0xd88e55, {
roughness: 0.38,
metalness: 0.78
}),
contact: standardMaterial(0xffb866, {
roughness: 0.28,
metalness: 0.85,
emissive: 0x7a2e00,
emissiveIntensity: 0.12
}),
pink: standardMaterial(0xff7fa8, {
roughness: 0.38,
metalness: 0.45,
emissive: 0x6d1536,
emissiveIntensity: 0.18
}),
amber: standardMaterial(0xffbd73, {
roughness: 0.4,
metalness: 0.45,
emissive: 0x6f3500,
emissiveIntensity: 0.18
}),
groundSymbol: standardMaterial(0x526b88, {
roughness: 0.55,
metalness: 0.6
})
};
// Ground and shadow receiver.
const ground = cube(
scene,
48,
0.3,
38,
0,
-1.0,
0,
materials.ground,
{ castShadow: false }
);
ground.receiveShadow = true;
// Main voxel circuit board.
cube(scene, 15.4, 0.62, 8.5, 0, -0.36, 0, materials.board);
cube(scene, 15.7, 0.18, 8.8, 0, -0.68, 0, materials.boardEdge);
// Instanced voxel tiles.
const tileCountX = 30;
const tileCountZ = 16;
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.84,
metalness: 0.08,
vertexColors: true
});
const tileMesh = new THREE.InstancedMesh(
cubeGeometry,
tileMaterial,
tileCountX * tileCountZ
);
const matrix = new THREE.Matrix4();
const tileColorA = new THREE.Color(0x17283e);
const tileColorB = new THREE.Color(0x1a2d45);
const tileColorC = new THREE.Color(0x142438);
let tileIndex = 0;
for (let iz = 0; iz < tileCountZ; iz++) {
for (let ix = 0; ix < tileCountX; ix++) {
const x = -7.25 + ix * 0.5;
const z = -3.75 + iz * 0.5;
matrix.makeTranslation(x, 0.015, z);
tileMesh.setMatrixAt(tileIndex, matrix);
const selector = (ix * 7 + iz * 3) % 11;
const color =
selector === 0
? tileColorC
: selector % 3 === 0
? tileColorB
: tileColorA;
tileMesh.setColorAt(tileIndex, color);
tileIndex++;
}
}
tileMesh.instanceMatrix.needsUpdate = true;
if (tileMesh.instanceColor) {
tileMesh.instanceColor.needsUpdate = true;
}
tileMesh.receiveShadow = true;
scene.add(tileMesh);
// Board corner blocks.
for (const x of [-7.3, 7.3]) {
for (const z of [-4.0, 4.0]) {
cube(scene, 0.62, 0.35, 0.62, x, 0.15, z, materials.boardEdge);
cube(scene, 0.25, 0.08, 0.25, x, 0.37, z, materials.copper);
}
}
function addDecorativeChip(x, z, width, depth, text) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
cube(group, width, 0.38, depth, 0, 0.25, 0, materials.chip);
cube(group, width - 0.35, 0.12, depth - 0.3, 0, 0.49, 0, materials.chipTop);
const pinCount = Math.max(3, Math.floor(width / 0.55));
for (let i = 0; i < pinCount; i++) {
const px = -width / 2 + 0.35 + i * ((width - 0.7) / (pinCount - 1));
cube(group, 0.18, 0.16, 0.38, px, 0.12, -depth / 2 - 0.1, materials.copper);
cube(group, 0.18, 0.16, 0.38, px, 0.12, depth / 2 + 0.1, materials.copper);
}
const chipLabel = label(text, Math.min(width * 0.58, 1.8), {
foreground: "#9eb9d9",
background: "rgba(17, 31, 50, 0.85)"
});
chipLabel.sprite.position.set(0, 1.25, 0);
group.add(chipLabel.sprite);
}
addDecorativeChip(-4.1, -2.75, 2.5, 1.2, "CTRL");
addDecorativeChip(1.0, -2.85, 2.1, 1.1, "I/O");
addDecorativeChip(5.2, -2.6, 1.6, 1.05, "LED");
const traceActive = 0x59f2c3;
const traceInactive = 0x2a3c54;
function traceMaterial() {
return standardMaterial(traceInactive, {
roughness: 0.42,
metalness: 0.56,
emissive: traceInactive,
emissiveIntensity: 0.04
});
}
function traceX(parent, startX, endX, z, material, size = 0.34, y = 0.22) {
const step = 0.4;
const count = Math.max(1, Math.ceil(Math.abs(endX - startX) / step));
for (let i = 0; i < count; i++) {
const t = (i + 0.5) / count;
const x = THREE.MathUtils.lerp(startX, endX, t);
cube(parent, size, 0.13, size, x, y, z, material, {
castShadow: false
});
}
}
function traceZ(parent, x, startZ, endZ, material, size = 0.3, y = 0.19) {
const step = 0.38;
const count = Math.max(1, Math.ceil(Math.abs(endZ - startZ) / step));
for (let i = 0; i < count; i++) {
const t = (i + 0.5) / count;
const z = THREE.MathUtils.lerp(startZ, endZ, t);
cube(parent, size, 0.11, size, x, y, z, material, {
castShadow: false
});
}
}
const pathMaterialA = traceMaterial();
const pathMaterialB = traceMaterial();
const pathMaterialC = traceMaterial();
const controlMaterialA = traceMaterial();
const controlMaterialB = traceMaterial();
controlMaterialA.color.setHex(0x7f4565);
controlMaterialA.emissive.setHex(0x7f153d);
controlMaterialB.color.setHex(0x7f4565);
controlMaterialB.emissive.setHex(0x7f153d);
// Main series path.
traceX(scene, -5.95, -5.15, 0, pathMaterialA);
traceX(scene, -2.85, -1.85, 0, pathMaterialB);
traceX(scene, 0.45, 2.75, 0, pathMaterialC);
// High-rail source.
cube(scene, 0.82, 0.72, 0.82, -6.35, 0.38, 0, materials.pink);
cube(scene, 0.38, 0.12, 0.38, -6.35, 0.8, 0, materials.contact);
const sourceLabel = label("HIGH", 1.3, {
foreground: "#ffb3ca",
border: "rgba(255, 127, 168, 0.28)"
});
sourceLabel.sprite.position.set(-6.35, 1.65, 0);
scene.add(sourceLabel.sprite);
const pickTargets = [];
const switchVisuals = [];
function createSwitch(centerX, switchId, title) {
const group = new THREE.Group();
group.position.set(centerX, 0, 0);
scene.add(group);
// Socket.
cube(group, 2.45, 0.26, 1.65, 0, 0.2, 0, materials.boardEdge);
cube(group, 2.1, 0.12, 1.35, 0, 0.39, 0, materials.chip);
// Copper terminals.
cube(group, 0.34, 0.25, 1.15, -0.78, 0.58, 0, materials.contact);
cube(group, 0.34, 0.25, 1.15, 0.78, 0.58, 0, materials.contact);
// Gate post and indicator.
cube(group, 0.2, 0.72, 0.2, 0.72, 0.72, -0.76, materials.pink);
const indicatorMaterial = standardMaterial(0xffbd73, {
roughness: 0.35,
metalness: 0.35,
emissive: 0x6f3500,
emissiveIntensity: 0.3
});
const indicator = cube(
group,
0.28,
0.28,
0.28,
0.72,
1.16,
-0.76,
indicatorMaterial
);
// Hinged switch blade.
const bladePivot = new THREE.Group();
bladePivot.position.set(-0.72, 0.78, 0);
group.add(bladePivot);
const bladeMaterial = standardMaterial(0xffbd73, {
roughness: 0.32,
metalness: 0.65,
emissive: 0x5b2d00,
emissiveIntensity: 0.24
});
cube(bladePivot, 1.5, 0.2, 0.46, 0.73, 0, 0, bladeMaterial);
cube(bladePivot, 0.18, 0.2, 0.55, 1.47, 0, 0, materials.contact);
const switchLabel = label(title, 2.15, {
foreground: "#e8f3ff",
border: "rgba(149, 181, 220, 0.25)"
});
switchLabel.sprite.position.set(0, 1.8, 0);
group.add(switchLabel.sprite);
// Invisible pick volume.
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
const hitbox = cube(
group,
2.7,
1.8,
1.9,
0,
0.85,
0,
hitMaterial,
{ castShadow: false, receiveShadow: false }
);
hitbox.userData.action = switchId;
pickTargets.push(hitbox);
switchVisuals.push({
id: switchId,
group,
bladePivot,
bladeMaterial,
indicatorMaterial,
indicator,
rotation: -0.9
});
}
createSwitch(-4.0, "toggleA", "A · IDEAL SWITCH");
createSwitch(-0.7, "toggleB", "B · IDEAL SWITCH");
// Input control pins and routed control wires.
function createControlRoute(centerX, switchName, material) {
const inputCube = cube(
scene,
0.56,
0.42,
0.56,
centerX,
0.3,
3.15,
materials.pink
);
traceZ(scene, centerX + 1.05, 3.15, -0.76, material, 0.19, 0.17);
traceX(scene, centerX + 0.72, centerX + 1.05, -0.76, material, 0.19, 0.17);
const pinLabel = label(switchName, 1.05, {
foreground: "#ffc1d6",
border: "rgba(255, 127, 168, 0.3)",
background: "rgba(39, 18, 35, 0.9)"
});
pinLabel.sprite.position.set(centerX, 1.18, 3.15);
scene.add(pinLabel.sprite);
return inputCube;
}
createControlRoute(-4.0, "A", controlMaterialA);
createControlRoute(-0.7, "B", controlMaterialB);
// Output node and voxel LED tower.
const outputGroup = new THREE.Group();
outputGroup.position.set(3.35, 0, 0);
scene.add(outputGroup);
cube(outputGroup, 1.45, 0.34, 1.45, 0, 0.24, 0, materials.boardEdge);
cube(outputGroup, 0.95, 0.18, 0.95, 0, 0.48, 0, materials.chipTop);
const ledCoreMaterial = standardMaterial(0xff7185, {
roughness: 0.22,
metalness: 0.05,
emissive: 0xff294e,
emissiveIntensity: 0.25,
transparent: true,
opacity: 0.95
});
cube(outputGroup, 0.56, 0.78, 0.56, 0, 0.94, 0, ledCoreMaterial);
const ledShellMaterial = new THREE.MeshPhysicalMaterial({
color: 0xaee8ff,
roughness: 0.08,
metalness: 0,
transmission: 0.2,
transparent: true,
opacity: 0.24,
depthWrite: false
});
cube(outputGroup, 0.92, 1.12, 0.92, 0, 0.98, 0, ledShellMaterial, {
castShadow: false
});
const outputLight = new THREE.PointLight(0x59f2c3, 0.1, 5, 2);
outputLight.position.set(3.35, 1.35, 0);
scene.add(outputLight);
const outputLabel = label("Y = 0 · LOW", 2.35, {
foreground: "#ff9daf",
border: "rgba(255, 113, 133, 0.28)",
background: "rgba(37, 17, 28, 0.9)"
});
outputLabel.sprite.position.set(3.35, 2.05, 0);
scene.add(outputLabel.sprite);
// Pull-down branch.
traceZ(scene, 3.35, 0.55, 1.45, pathMaterialC, 0.28, 0.18);
cube(scene, 0.72, 0.26, 0.72, 3.35, 0.22, 1.75, materials.chip);
cube(scene, 0.94, 0.22, 0.36, 3.35, 0.22, 1.75, materials.copper);
cube(scene, 0.82, 0.16, 0.24, 3.35, 0.3, 2.35, materials.groundSymbol);
cube(scene, 0.56, 0.16, 0.24, 3.35, 0.3, 2.58, materials.groundSymbol);
cube(scene, 0.28, 0.16, 0.24, 3.35, 0.3, 2.81, materials.groundSymbol);
const pullDownLabel = label("PULL-DOWN", 1.9, {
foreground: "#91a9c7",
border: "rgba(129, 159, 196, 0.18)",
background: "rgba(13, 25, 41, 0.88)"
});
pullDownLabel.sprite.position.set(4.55, 0.95, 1.85);
scene.add(pullDownLabel.sprite);
// Traveling signal pulse.
const pulseMaterial = standardMaterial(0xb8fff0, {
roughness: 0.12,
metalness: 0.05,
emissive: 0x59f2c3,
emissiveIntensity: 2.5
});
const pulse = cube(
scene,
0.22,
0.22,
0.22,
-5.6,
0.78,
0,
pulseMaterial,
{ castShadow: false }
);
pulse.visible = false;
// Small board annotation.
const boardAnnotation = label("SERIES SWITCH ARRAY", 3.2, {
foreground: "#7f99b7",
border: "rgba(113, 144, 184, 0.13)",
background: "rgba(15, 28, 45, 0.72)"
});
boardAnnotation.sprite.position.set(2.6, 0.72, -3.25);
scene.add(boardAnnotation.sprite);
// -------------------------------------------------------------------------
// Direct camera controls — no external controls addon.
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 0.45, 0),
yaw: 0.62,
pitch: 0.59,
radius: 17.2,
desiredYaw: 0.62,
desiredPitch: 0.59,
desiredRadius: 17.2
};
function resetCamera() {
cameraState.desiredYaw = 0.62;
cameraState.desiredPitch = 0.59;
cameraState.desiredRadius = 17.2;
}
document.querySelector("#resetView").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
const pointer = {
down: false,
id: null,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false
};
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.down = true;
pointer.id = event.pointerId;
pointer.startX = event.clientX;
pointer.startY = event.clientY;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
pointer.moved = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.down || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.lastX;
const dy = event.clientY - pointer.lastY;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
if (
Math.abs(event.clientX - pointer.startX) +
Math.abs(event.clientY - pointer.startY) >
4
) {
pointer.moved = true;
}
cameraState.desiredYaw -= dx * 0.006;
cameraState.desiredPitch = THREE.MathUtils.clamp(
cameraState.desiredPitch + dy * 0.005,
0.27,
1.22
);
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointer.down || event.pointerId !== pointer.id) return;
pointer.down = false;
if (!pointer.moved) {
const rect = renderer.domElement.getBoundingClientRect();
pointerNDC.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObjects(pickTargets, false);
if (hits.length > 0) {
const action = hits[0].object.userData.action;
if (action === "toggleA") {
state.a = !state.a;
refresh();
}
if (action === "toggleB") {
state.b = !state.b;
refresh();
}
}
}
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointer.down = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius + event.deltaY * 0.012,
11,
25
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function resize() {
const width = Math.max(1, container.clientWidth);
const height = Math.max(1, container.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(container);
resize();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const activeColor = new THREE.Color(traceActive);
const inactiveColor = new THREE.Color(traceInactive);
const mintColor = new THREE.Color(0x59f2c3);
const amberColor = new THREE.Color(0xffbd73);
const blackColor = new THREE.Color(0x000000);
function setTraceState(material, active) {
material.color.copy(active ? activeColor : inactiveColor);
material.emissive.copy(active ? activeColor : blackColor);
material.emissiveIntensity = active ? 0.62 : 0.025;
}
function updateDeviceVisual(device, delta, elapsed) {
setTraceState(pathMaterialA, device.closedA);
setTraceState(pathMaterialB, device.pathClosed);
setTraceState(pathMaterialC, device.pathClosed);
setTraceState(controlMaterialA, device.closedA);
setTraceState(controlMaterialB, device.closedB);
controlMaterialA.color.lerp(
device.closedA ? mintColor : new THREE.Color(0x7f4565),
0.16
);
controlMaterialB.color.lerp(
device.closedB ? mintColor : new THREE.Color(0x7f4565),
0.16
);
for (const visual of switchVisuals) {
const closed =
visual.id === "toggleA" ? device.closedA : device.closedB;
const targetRotation = closed ? 0 : -0.9;
visual.rotation = THREE.MathUtils.damp(
visual.rotation,
targetRotation,
10,
delta
);
visual.bladePivot.rotation.z = visual.rotation;
visual.bladeMaterial.color.lerp(
closed ? mintColor : amberColor,
0.14
);
visual.bladeMaterial.emissive.lerp(
closed ? mintColor : amberColor,
0.14
);
visual.bladeMaterial.emissiveIntensity = closed ? 0.34 : 0.16;
visual.indicatorMaterial.color.lerp(
closed ? mintColor : amberColor,
0.16
);
visual.indicatorMaterial.emissive.lerp(
closed ? mintColor : amberColor,
0.16
);
visual.indicatorMaterial.emissiveIntensity = closed ? 0.8 : 0.24;
}
ledCoreMaterial.color.lerp(
device.outputHigh ? mintColor : new THREE.Color(0xff7185),
0.12
);
ledCoreMaterial.emissive.lerp(
device.outputHigh ? mintColor : new THREE.Color(0xff294e),
0.12
);
ledCoreMaterial.emissiveIntensity = device.outputHigh ? 2.2 : 0.25;
outputLight.intensity = device.outputHigh ? 2.7 : 0.12;
outputLight.color.setHex(device.outputHigh ? 0x59f2c3 : 0xff294e);
pulse.visible = device.outputHigh;
if (device.outputHigh) {
const progress = (elapsed * 3.1) % 9.55;
pulse.position.x = -5.85 + progress;
}
outputLabel.set(
`Y = ${device.outputHigh ? 1 : 0} · ${device.outputHigh ? "HIGH" : "LOW"}`,
device.outputHigh ? "#b9ffec" : "#ff9daf",
device.outputHigh
? "rgba(13, 47, 43, 0.92)"
: "rgba(37, 17, 28, 0.92)"
);
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const device = IdealSwitchDevice.snapshot(state.a, state.b);
updateDeviceVisual(device, delta, elapsed);
const damping = 1 - Math.exp(-delta * 9);
cameraState.yaw = THREE.MathUtils.lerp(
cameraState.yaw,
cameraState.desiredYaw,
damping
);
cameraState.pitch = THREE.MathUtils.lerp(
cameraState.pitch,
cameraState.desiredPitch,
damping
);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraState.desiredRadius,
damping
);
const horizontalRadius =
cameraState.radius * Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x + horizontalRadius * Math.sin(cameraState.yaw),
cameraState.target.y + cameraState.radius * Math.sin(cameraState.pitch),
cameraState.target.z + horizontalRadius * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```