Prompt specification
Build or customize the Three.js 3D scene "Two Plates AND Versus OR" 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: Two Plates AND Versus OR
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-plates-and-versus-or-x3486
Scene Description Prompt:
In one HTML file with Three.js from cdnjs, compare low-poly AND and OR gate puzzles across all four switch combinations.
Working Three.js Starter (two-plates-and-versus-or-x3486):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>AND vs OR — Interactive Low-Poly Logic Lab</title>
<style>
:root {
--bg: #080d18;
--panel: #101827;
--panel-2: #151f31;
--line: #26344b;
--text: #edf5ff;
--muted: #91a4bd;
--and: #42dfc3;
--or: #ff6f86;
--a: #57d6ff;
--b: #b58cff;
--on: #ffd166;
--danger: #ff8093;
}
* {
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;
}
body {
overflow: hidden;
}
button {
color: inherit;
font: inherit;
}
button:focus-visible,
canvas:focus-visible {
outline: 2px solid var(--on);
outline-offset: 3px;
}
.app-header {
height: 92px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 15px 24px;
border-bottom: 1px solid var(--line);
background:
radial-gradient(circle at 18% -80%, rgba(66, 223, 195, 0.18), transparent 35%),
linear-gradient(180deg, #111a2b, #0b111e);
position: relative;
z-index: 10;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.brand-mark {
width: 48px;
height: 48px;
flex: 0 0 auto;
border: 1px solid #3d526e;
border-radius: 13px;
display: grid;
place-items: center;
background: linear-gradient(145deg, #192b3e, #111827);
box-shadow: inset 0 1px rgba(255, 255, 255, 0.08);
font-weight: 900;
letter-spacing: -2px;
color: var(--and);
}
.brand-mark span {
color: var(--or);
}
.eyebrow {
margin: 0 0 3px;
color: var(--and);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.1vw, 30px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.header-meta {
display: flex;
gap: 8px;
flex-wrap: wrap;
justify-content: flex-end;
}
.meta-pill {
border: 1px solid var(--line);
border-radius: 999px;
padding: 8px 11px;
color: var(--muted);
background: rgba(255, 255, 255, 0.025);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
white-space: nowrap;
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr) 365px;
height: calc(100dvh - 92px);
min-height: 0;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 40%, #142039 0, #0a111e 55%, #070b13 100%);
}
#canvas-wrap {
position: absolute;
inset: 0;
}
#canvas-wrap canvas {
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#canvas-wrap canvas:active {
cursor: grabbing;
}
.stage-top {
position: absolute;
z-index: 5;
top: 17px;
left: 17px;
right: 17px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.input-strip,
.stage-actions {
pointer-events: auto;
}
.strip-label {
display: block;
margin: 0 0 8px 2px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.combo-buttons {
display: flex;
gap: 7px;
}
.combo-button {
min-width: 47px;
height: 39px;
border: 1px solid #30415b;
border-radius: 10px;
background: rgba(13, 21, 35, 0.86);
box-shadow: 0 7px 18px rgba(0, 0, 0, 0.18);
cursor: pointer;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
font-weight: 800;
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
.combo-button:hover {
transform: translateY(-1px);
border-color: #5d7596;
}
.combo-button.active {
color: #07121c;
border-color: var(--on);
background: var(--on);
box-shadow: 0 0 22px rgba(255, 209, 102, 0.22);
}
.stage-actions {
display: flex;
gap: 7px;
}
.small-button {
height: 39px;
padding: 0 12px;
border: 1px solid #30415b;
border-radius: 10px;
background: rgba(13, 21, 35, 0.86);
cursor: pointer;
color: var(--muted);
font-size: 11px;
font-weight: 750;
transition: color 160ms ease, border-color 160ms ease;
}
.small-button:hover {
color: var(--text);
border-color: #60789a;
}
.stage-bottom {
position: absolute;
z-index: 5;
left: 17px;
right: 17px;
bottom: 16px;
display: flex;
align-items: stretch;
justify-content: center;
gap: 10px;
pointer-events: none;
}
.gate-card {
width: 190px;
min-height: 91px;
padding: 11px 13px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(12, 19, 32, 0.88);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
transition: border-color 180ms ease, box-shadow 180ms ease;
}
.gate-card.and {
--gate-color: var(--and);
}
.gate-card.or {
--gate-color: var(--or);
}
.gate-card.energized {
border-color: color-mix(in srgb, var(--gate-color) 68%, white 8%);
box-shadow:
0 12px 35px rgba(0, 0, 0, 0.3),
0 0 26px color-mix(in srgb, var(--gate-color) 15%, transparent);
}
.gate-card-top {
display: flex;
align-items: center;
justify-content: space-between;
}
.gate-symbol {
color: var(--gate-color);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.11em;
}
.mini-led {
width: 9px;
height: 9px;
border-radius: 50%;
background: #263349;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}
.energized .mini-led {
background: var(--on);
box-shadow: 0 0 12px rgba(255, 209, 102, 0.85);
}
.gate-reading {
display: flex;
align-items: baseline;
gap: 7px;
margin: 5px 0 2px;
}
.gate-reading strong {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 24px;
line-height: 1;
}
.gate-reading span {
color: var(--muted);
font-size: 10px;
}
.gate-card code {
color: var(--muted);
font-size: 10px;
}
.stage-help {
position: absolute;
z-index: 4;
left: 18px;
bottom: 118px;
color: rgba(174, 194, 219, 0.62);
font-size: 10px;
pointer-events: none;
}
.sidebar {
min-height: 0;
overflow-y: auto;
padding: 17px;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(19, 29, 46, 0.95), rgba(10, 16, 27, 0.98));
scrollbar-width: thin;
scrollbar-color: #33445e transparent;
}
.panel-section {
padding-bottom: 16px;
margin-bottom: 16px;
border-bottom: 1px solid var(--line);
}
.panel-section:last-child {
border-bottom: 0;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin: 0 0 12px;
color: var(--muted);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.section-heading .status {
color: var(--on);
letter-spacing: 0.06em;
}
.live-panel {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.live-box {
padding: 11px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.live-box-label {
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.switch-row {
display: flex;
gap: 7px;
}
.switch-toggle {
flex: 1;
min-width: 0;
height: 34px;
border: 1px solid #34445e;
border-radius: 8px;
background: #172235;
cursor: pointer;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-weight: 800;
transition: background 150ms ease, color 150ms ease, border-color 150ms ease;
}
.switch-toggle[data-switch="a"].on {
color: #07131d;
border-color: var(--a);
background: var(--a);
}
.switch-toggle[data-switch="b"].on {
color: #100d1c;
border-color: var(--b);
background: var(--b);
}
.output-line {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 34px;
padding: 0 8px;
border-radius: 8px;
background: #111a2b;
}
.output-line + .output-line {
margin-top: 6px;
}
.output-line span {
color: var(--muted);
font-size: 10px;
font-weight: 800;
}
.output-line strong {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 20px;
}
.output-line.and strong {
color: var(--and);
}
.output-line.or strong {
color: var(--or);
}
.truth-wrap {
overflow: hidden;
border: 1px solid var(--line);
border-radius: 12px;
}
.truth-table {
width: 100%;
border-collapse: collapse;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
}
.truth-table th {
height: 34px;
padding: 0 7px;
color: var(--muted);
background: #151f31;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.05em;
text-align: center;
}
.truth-table td {
height: 39px;
padding: 0 7px;
border-top: 1px solid var(--line);
text-align: center;
transition: background 150ms ease;
}
.truth-table tbody tr {
cursor: pointer;
}
.truth-table tbody tr:hover td {
background: rgba(255, 255, 255, 0.035);
}
.truth-table tbody tr.active td {
background: rgba(255, 209, 102, 0.08);
}
.truth-table tbody tr.active td:first-child {
box-shadow: inset 3px 0 var(--on);
}
.truth-table .and-value {
color: var(--and);
font-weight: 800;
}
.truth-table .or-value {
color: var(--or);
font-weight: 800;
}
.truth-table .different {
color: var(--danger);
font-weight: 900;
}
.truth-table .same {
color: var(--muted);
}
.table-note {
margin: 9px 2px 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.insight {
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.insight h3 {
margin: 0 0 8px;
font-size: 12px;
}
.insight p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.difference-note {
display: flex;
align-items: center;
gap: 8px;
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid var(--line);
color: var(--danger);
font-size: 10px;
font-weight: 750;
}
.difference-note strong {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.challenge {
border: 1px solid #3c4a62;
border-radius: 14px;
padding: 13px;
background:
radial-gradient(circle at 100% 0, rgba(255, 209, 102, 0.09), transparent 42%),
#131d2e;
}
.challenge-number {
color: var(--on);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.challenge h3 {
margin: 5px 0 5px;
font-size: 15px;
}
.challenge p {
margin: 0 0 11px;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.challenge-options {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
}
.challenge-options .combo-button {
min-width: 0;
height: 35px;
font-size: 11px;
}
.challenge-status {
min-height: 30px;
margin-top: 10px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.challenge-status.success {
color: var(--on);
}
.challenge-status.warn {
color: var(--danger);
}
.next-button {
width: 100%;
height: 36px;
margin-top: 9px;
border: 1px solid #566783;
border-radius: 9px;
background: #25334a;
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
}
.next-button:disabled {
cursor: not-allowed;
opacity: 0.35;
}
.comparison-banner {
margin-top: 10px;
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.comparison-banner.equal {
border-color: rgba(66, 223, 195, 0.32);
color: #b6f5e8;
}
.comparison-banner.different {
border-color: rgba(255, 111, 134, 0.34);
color: #ffc0cb;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 920px) {
body {
overflow: auto;
}
.app-header {
height: auto;
min-height: 82px;
padding: 13px 15px;
}
.header-meta {
display: none;
}
.workspace {
grid-template-columns: 1fr;
height: auto;
}
.stage {
height: 64dvh;
min-height: 570px;
}
.sidebar {
overflow: visible;
border-left: 0;
border-top: 1px solid var(--line);
}
}
@media (max-width: 560px) {
.brand-mark {
width: 42px;
height: 42px;
}
.stage-top {
flex-direction: column;
}
.stage-actions {
position: absolute;
top: 0;
right: 0;
}
.stage-bottom {
gap: 7px;
}
.gate-card {
width: 50%;
min-height: 86px;
padding: 10px;
}
.stage-help {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
animation-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<header class="app-header">
<div class="brand">
<div class="brand-mark" aria-hidden="true">A<span>O</span></div>
<div>
<p class="eyebrow">Interactive low-poly logic lab</p>
<h1>AND vs OR: Side-by-Side</h1>
</div>
</div>
<div class="header-meta" aria-label="Lab details">
<span class="meta-pill">2 GATES</span>
<span class="meta-pill">2 SWITCHES</span>
<span class="meta-pill">4 INPUT STATES</span>
<span class="meta-pill">LIVE 3D</span>
</div>
</header>
<main class="workspace">
<section class="stage" aria-label="Interactive 3D gate comparison">
<div id="canvas-wrap"></div>
<div class="stage-top">
<div class="input-strip">
<span class="strip-label">Select input combination</span>
<div class="combo-buttons" aria-label="Input combinations">
<button class="combo-button" data-combo="00" aria-pressed="true">00</button>
<button class="combo-button" data-combo="01" aria-pressed="false">01</button>
<button class="combo-button" data-combo="10" aria-pressed="false">10</button>
<button class="combo-button" data-combo="11" aria-pressed="false">11</button>
</div>
</div>
<div class="stage-actions">
<button class="small-button" id="run-button" type="button">
<span id="run-label">▶ Run all</span>
</button>
<button class="small-button" id="camera-button" type="button"
title="Reset camera" aria-label="Reset camera">↺ View</button>
</div>
</div>
<div class="stage-help">
Drag to orbit · Scroll or pinch to zoom · Click the physical switches
</div>
<div class="stage-bottom">
<article class="gate-card and" id="and-card">
<div class="gate-card-top">
<span class="gate-symbol">AND GATE</span>
<span class="mini-led"></span>
</div>
<div class="gate-reading">
<strong id="and-value">0</strong>
<span>output</span>
</div>
<code>A · B</code>
</article>
<article class="gate-card or" id="or-card">
<div class="gate-card-top">
<span class="gate-symbol">OR GATE</span>
<span class="mini-led"></span>
</div>
<div class="gate-reading">
<strong id="or-value">0</strong>
<span>output</span>
</div>
<code>A + B</code>
</article>
</div>
</section>
<aside class="sidebar">
<section class="panel-section">
<h2 class="section-heading">
Live readout
<span class="status" id="combo-status">A=0 · B=0</span>
</h2>
<div class="live-panel">
<div class="live-box">
<div class="live-box-label">Switches</div>
<div class="switch-row">
<button class="switch-toggle" data-switch="a" type="button"
aria-pressed="false" aria-label="Toggle switch A">A 0</button>
<button class="switch-toggle" data-switch="b" type="button"
aria-pressed="false" aria-label="Toggle switch B">B 0</button>
</div>
</div>
<div class="live-box">
<div class="live-box-label">Gate outputs</div>
<div class="output-line and">
<span>AND</span>
<strong id="read-and">0</strong>
</div>
<div class="output-line or">
<span>OR</span>
<strong id="read-or">0</strong>
</div>
</div>
</div>
<div class="comparison-banner" id="comparison-banner">
Both outputs are LOW.
</div>
</section>
<section class="panel-section">
<h2 class="section-heading">Comparison matrix</h2>
<div class="truth-wrap">
<table class="truth-table">
<caption class="sr-only">
AND and OR outputs for all four switch combinations
</caption>
<thead>
<tr>
<th scope="col">A</th>
<th scope="col">B</th>
<th scope="col">AND</th>
<th scope="col">OR</th>
<th scope="col">Result</th>
</tr>
</thead>
<tbody>
<tr data-combo="00" tabindex="0" aria-label="A off, B off">
<td>0</td><td>0</td><td class="and-value">0</td>
<td class="or-value">0</td><td class="same">=</td>
</tr>
<tr data-combo="01" tabindex="0" aria-label="A off, B on">
<td>0</td><td>1</td><td class="and-value">0</td>
<td class="or-value">1</td><td class="different">≠</td>
</tr>
<tr data-combo="10" tabindex="0" aria-label="A on, B off">
<td>1</td><td>0</td><td class="and-value">0</td>
<td class="or-value">1</td><td class="different">≠</td>
</tr>
<tr data-combo="11" tabindex="0" aria-label="A on, B on">
<td>1</td><td>1</td><td class="and-value">1</td>
<td class="or-value">1</td><td class="same">=</td>
</tr>
</tbody>
</table>
</div>
<p class="table-note">
Select a row to reproduce that combination in the 3D circuit.
The two gates disagree for <strong>01</strong> and <strong>10</strong>.
</p>
</section>
<section class="panel-section">
<h2 class="section-heading">Key insight</h2>
<div class="insight">
<h3>Same result, different rule</h3>
<p>
<strong style="color:var(--and)">AND</strong> activates only when both
inputs are 1. <strong style="color:var(--or)">OR</strong> activates when
either input is 1.
</p>
<div class="difference-note">
<span>Different outputs:</span>
<strong>01, 10</strong>
</div>
</div>
</section>
<section class="panel-section">
<h2 class="section-heading">Mini puzzle</h2>
<div class="challenge">
<div class="challenge-number" id="challenge-number">Puzzle 1 of 3</div>
<h3 id="challenge-title">Light both outputs</h3>
<p id="challenge-description">
Find the only combination that makes both the AND and OR lamps turn on.
</p>
<div class="challenge-options" aria-label="Challenge answer choices">
<button class="combo-button" data-combo="00" type="button">00</button>
<button class="combo-button" data-combo="01" type="button">01</button>
<button class="combo-button" data-combo="10" type="button">10</button>
<button class="combo-button" data-combo="11" type="button">11</button>
</div>
<div class="challenge-status" id="challenge-status" aria-live="polite">
Choose a combination to test.
</div>
<button class="next-button" id="next-challenge" type="button" disabled>
Next puzzle
</button>
</div>
</section>
</aside>
</main>
<div class="sr-only" id="live-region" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const $$ = (selector) => [...document.querySelectorAll(selector)];
const state = {
a: false,
b: false
};
const challengeList = [
{
title: "Light both outputs",
description:
"Find the only combination that makes both the AND and OR lamps turn on.",
targets: ["11"]
},
{
title: "Light only the OR output",
description:
"Keep the AND lamp off while making the OR lamp turn on.",
targets: ["01", "10"]
},
{
title: "Turn everything off",
description:
"Find the combination that makes both output lamps turn off.",
targets: ["00"]
}
];
let challengeIndex = 0;
let autoTimer = null;
let autoIndex = 0;
const andOutput = () => Number(state.a && state.b);
const orOutput = () => Number(state.a || state.b);
const comboKey = () =>
`${state.a ? "1" : "0"}${state.b ? "1" : "0"}`;
const canvasWrap = $("#canvas-wrap");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080d18);
scene.fog = new THREE.Fog(0x080d18, 23, 45);
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const canvas = renderer.domElement;
canvas.tabIndex = 0;
canvas.setAttribute(
"aria-label",
"3D AND and OR circuit. Drag to orbit, scroll to zoom, and press A or B to toggle switches."
);
canvasWrap.appendChild(canvas);
scene.add(new THREE.HemisphereLight(0xbfdcff, 0x172034, 1.75));
const ambient = new THREE.AmbientLight(0x7185a8, 0.42);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.5);
keyLight.position.set(-6, 12, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4edfff, 1.2);
rimLight.position.set(9, 5, -7);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9f75, 12, 18, 2);
warmLight.position.set(0, 4, 7);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(31, 16),
new THREE.MeshStandardMaterial({
color: 0x0a111e,
roughness: 0.93,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.22;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x28405b, 0x17283d);
grid.position.y = -0.205;
grid.material.transparent = true;
grid.material.opacity = 0.23;
scene.add(grid);
const panels = [];
const switchTargets = [];
const clock = new THREE.Clock();
const orbit = {
yaw: 0,
pitch: 0.37,
radius: 20,
target: new THREE.Vector3(0, 1.72, 0)
};
const reducedMotion =
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function makeTextSprite(text, color, subtitle = "") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 192;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, 512, 192);
ctx.fillStyle = "rgba(8, 14, 25, 0.88)";
roundedRect(ctx, 8, 8, 496, 176, 28);
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 5;
roundedRect(ctx, 8, 8, 496, 176, 28);
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.font = "900 70px system-ui, sans-serif";
ctx.fillText(text, 256, subtitle ? 70 : 96);
if (subtitle) {
ctx.fillStyle = "#a9bad0";
ctx.font = "700 27px system-ui, sans-serif";
ctx.fillText(subtitle, 256, 132);
}
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.renderOrder = 20;
return sprite;
}
function makeBadgeSprite(text, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 128;
labelCanvas.height = 128;
const ctx = labelCanvas.getContext("2d");
ctx.fillStyle = "rgba(8, 14, 25, 0.9)";
ctx.beginPath();
ctx.arc(64, 64, 55, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 5;
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "900 60px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 64, 67);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.58, 0.58, 1);
sprite.renderOrder = 20;
return sprite;
}
function addEdges(mesh, opacity = 0.22) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 28),
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity
})
);
edges.renderOrder = 2;
mesh.add(edges);
}
function createAndShape() {
const shape = new THREE.Shape();
shape.moveTo(-1.15, -1);
shape.lineTo(0, -1);
shape.absarc(0, 0, 1, -Math.PI / 2, Math.PI / 2, false);
shape.lineTo(-1.15, 1);
shape.closePath();
return shape;
}
function createOrShape() {
const shape = new THREE.Shape();
shape.moveTo(-1.25, -1);
shape.quadraticCurveTo(-0.38, 0, -1.25, 1);
shape.quadraticCurveTo(-0.02, 0.62, 0.9, 0);
shape.quadraticCurveTo(-0.02, -0.62, -1.25, -1);
shape.closePath();
return shape;
}
function makeWireMaterial(color) {
const base = new THREE.Color(color);
const material = new THREE.MeshStandardMaterial({
color: base.clone().multiplyScalar(0.2),
emissive: base,
emissiveIntensity: 0.02,
roughness: 0.52,
metalness: 0.12
});
return material;
}
function addWire(parent, points, color, pulseOffset = 0) {
const curve = new THREE.CatmullRomCurve3(points, false, "catmullrom", 0.35);
const geometry = new THREE.TubeGeometry(curve, 36, 0.046, 6, false);
const material = makeWireMaterial(color);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
parent.add(mesh);
const pulse = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.085, 0),
new THREE.MeshBasicMaterial({ color })
);
pulse.visible = false;
parent.add(pulse);
return { curve, mesh, material, pulse, pulseOffset, active: false };
}
function createSwitch(parent, x, index, color) {
const switchGroup = new THREE.Group();
switchGroup.position.set(x, 0, 2.35);
parent.add(switchGroup);
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x1d2a3f,
roughness: 0.58,
metalness: 0.55,
flatShading: true
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.36, 0.4, 0.14, 8),
baseMaterial
);
base.position.y = 0.1;
base.castShadow = true;
base.receiveShadow = true;
switchGroup.add(base);
const collar = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.3, 0.16, 8),
new THREE.MeshStandardMaterial({
color: 0x33445e,
roughness: 0.4,
metalness: 0.65,
flatShading: true
})
);
collar.position.y = 0.24;
collar.castShadow = true;
switchGroup.add(collar);
const pivot = new THREE.Group();
pivot.position.y = 0.47;
switchGroup.add(pivot);
const leverMaterial = new THREE.MeshStandardMaterial({
color: 0x9fb2c9,
roughness: 0.35,
metalness: 0.72,
flatShading: true
});
const lever = new THREE.Mesh(
new THREE.BoxGeometry(0.11, 0.76, 0.11),
leverMaterial
);
lever.position.y = 0.34;
lever.castShadow = true;
pivot.add(lever);
const knob = new THREE.Mesh(
new THREE.OctahedronGeometry(0.17, 0),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.15,
roughness: 0.3,
metalness: 0.2,
flatShading: true
})
);
knob.position.y = 0.78;
knob.castShadow = true;
pivot.add(knob);
const ledMaterial = new THREE.MeshStandardMaterial({
color: 0x243247,
emissive: color,
emissiveIntensity: 0.02,
roughness: 0.3,
flatShading: true
});
const led = new THREE.Mesh(
new THREE.OctahedronGeometry(0.1, 0),
ledMaterial
);
led.position.set(-0.32, 0.23, 0.12);
switchGroup.add(led);
const terminal = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.11, 0),
new THREE.MeshStandardMaterial({
color,
roughness: 0.35,
metalness: 0.45,
flatShading: true
})
);
terminal.position.set(0.32, 0.26, 0);
switchGroup.add(terminal);
const label = makeBadgeSprite(index === 0 ? "A" : "B", color);
label.position.set(x, 1.48, 2.35);
parent.add(label);
const clickable = [base, lever, knob];
for (const object of clickable) {
object.userData.switchIndex = index;
switchTargets.push(object);
}
return {
group: switchGroup,
pivot,
ledMaterial,
terminal: new THREE.Vector3(x + 0.32, 0.26, 2.35)
};
}
function createPanel(kind, centerX) {
const isAnd = kind === "AND";
const color = isAnd ? 0x42dfc3 : 0xff6f86;
const colorCss = isAnd ? "#42dfc3" : "#ff6f86";
const inputAColor = 0x57d6ff;
const inputBColor = 0xb58cff;
const group = new THREE.Group();
group.position.x = centerX;
scene.add(group);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(7.15, 0.22, 6.25),
new THREE.MeshStandardMaterial({
color: 0x111b2b,
roughness: 0.76,
metalness: 0.25
})
);
platform.position.set(0, -0.08, 0.95);
platform.castShadow = true;
platform.receiveShadow = true;
group.add(platform);
addEdges(platform, 0.12);
const board = new THREE.Mesh(
new THREE.BoxGeometry(6.72, 4.08, 0.18),
new THREE.MeshStandardMaterial({
color: isAnd ? 0x10272b : 0x2b1722,
roughness: 0.68,
metalness: 0.2
})
);
board.position.set(0, 2.11, -0.62);
board.castShadow = true;
board.receiveShadow = true;
group.add(board);
addEdges(board, 0.16);
const traceMaterial = new THREE.MeshBasicMaterial({
color: isAnd ? 0x1c5653 : 0x613044,
transparent: true,
opacity: 0.68
});
for (let i = 0; i < 4; i++) {
const trace = new THREE.Mesh(
new THREE.BoxGeometry(1.15 + i * 0.28, 0.025, 0.02),
traceMaterial
);
trace.position.set(-2.35 + i * 0.32, 0.85 + i * 0.42, -0.51);
group.add(trace);
}
const title = makeTextSprite(
kind,
colorCss,
isAnd ? "both inputs required" : "one input is enough"
);
title.position.set(0, 4.5, 0.12);
title.scale.set(2.2, 0.82, 1);
group.add(title);
const gateGroup = new THREE.Group();
gateGroup.position.set(0, 2.55, 0);
group.add(gateGroup);
const shape = isAnd ? createAndShape() : createOrShape();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.38,
bevelEnabled: true,
bevelSegments: 1,
steps: 1,
bevelSize: 0.065,
bevelThickness: 0.065,
curveSegments: 9
});
geometry.translate(0, 0, -0.19);
const gateMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.06,
roughness: 0.43,
metalness: 0.22,
flatShading: true
});
const gateBody = new THREE.Mesh(geometry, gateMaterial);
gateBody.castShadow = true;
gateBody.receiveShadow = true;
gateGroup.add(gateBody);
addEdges(gateBody, 0.25);
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.42, 1.42, 0.34),
new THREE.MeshStandardMaterial({
color: 0x33445c,
roughness: 0.46,
metalness: 0.55,
flatShading: true
})
);
support.position.set(0, 1.08, -0.28);
support.castShadow = true;
group.add(support);
const inputX = isAnd ? -1.17 : -1.08;
const outputX = isAnd ? 1.01 : 0.9;
const portGeometry = new THREE.IcosahedronGeometry(0.12, 0);
const portMaterial = new THREE.MeshStandardMaterial({
color: 0x9fb4ce,
emissive: color,
emissiveIntensity: 0.12,
roughness: 0.35,
metalness: 0.55,
flatShading: true
});
const portA = new THREE.Mesh(portGeometry, portMaterial);
portA.position.set(inputX, 0.48, 0.24);
gateGroup.add(portA);
const portB = new THREE.Mesh(portGeometry, portMaterial);
portB.position.set(inputX, -0.48, 0.24);
gateGroup.add(portB);
const outputPort = new THREE.Mesh(portGeometry, portMaterial);
outputPort.position.set(outputX, 0, 0.25);
gateGroup.add(outputPort);
const switchA = createSwitch(group, -2.55, 0, "#57d6ff");
const switchB = createSwitch(group, -1.45, 1, "#b58cff");
const wireA = addWire(
group,
[
switchA.terminal,
new THREE.Vector3(-2.23, 0.78, 2.35),
new THREE.Vector3(-1.72, 3.03, 2.05),
new THREE.Vector3(inputX, 3.03, 0.25)
],
inputAColor,
0.08
);
const wireB = addWire(
group,
[
switchB.terminal,
new THREE.Vector3(-1.13, 0.75, 2.35),
new THREE.Vector3(-1.68, 2.07, 2.05),
new THREE.Vector3(inputX, 2.07, 0.25)
],
inputBColor,
0.58
);
const lampStand = new THREE.Mesh(
new THREE.BoxGeometry(0.28, 2.05, 0.3),
new THREE.MeshStandardMaterial({
color: 0x33445c,
roughness: 0.44,
metalness: 0.58,
flatShading: true
})
);
lampStand.position.set(2.55, 1.05, 0);
lampStand.castShadow = true;
group.add(lampStand);
const lampBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.38, 0.45, 0.16, 8),
new THREE.MeshStandardMaterial({
color: 0x26354b,
roughness: 0.45,
metalness: 0.58,
flatShading: true
})
);
lampBase.position.set(2.55, 0.08, 0);
lampBase.castShadow = true;
group.add(lampBase);
const lampMaterial = new THREE.MeshStandardMaterial({
color: 0x29354a,
emissive: 0xffb84d,
emissiveIntensity: 0.02,
roughness: 0.28,
metalness: 0.05,
flatShading: true
});
const lamp = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.37, 1),
lampMaterial
);
lamp.position.set(2.55, 2.55, 0.2);
lamp.castShadow = true;
group.add(lamp);
const lampRing = new THREE.Mesh(
new THREE.TorusGeometry(0.43, 0.045, 5, 12),
new THREE.MeshStandardMaterial({
color: 0x7389a5,
roughness: 0.38,
metalness: 0.7,
flatShading: true
})
);
lampRing.position.copy(lamp.position);
group.add(lampRing);
const outputLabel = makeTextSprite("OUT", "#ffd166");
outputLabel.position.set(2.55, 3.35, 0.2);
outputLabel.scale.set(0.92, 0.34, 1);
group.add(outputLabel);
const outputWire = addWire(
group,
[
new THREE.Vector3(outputX, 2.55, 0.25),
new THREE.Vector3(1.52, 2.55, 0.38),
new THREE.Vector3(2.02, 2.42, 0.32),
new THREE.Vector3(2.35, 2.55, 0.2)
],
0xffd166,
0.3
);
const panel = {
kind,
group,
gateMaterial,
lamp,
lampMaterial,
outputWire,
switches: [switchA, switchB],
wires: { a: wireA, b: wireB },
outputTarget: 0,
gateTarget: 0.06
};
panels.push(panel);
return panel;
}
createPanel("AND", -5.12);
createPanel("OR", 5.12);
function updateCamera() {
const radius = orbit.radius;
const horizontal = Math.cos(orbit.pitch) * radius;
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontal,
orbit.target.y + Math.sin(orbit.pitch) * radius,
orbit.target.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(orbit.target);
}
function homeRadius() {
return camera.aspect < 1.05 ? 22 : 19.5;
}
function resetCamera() {
orbit.yaw = 0;
orbit.pitch = 0.37;
orbit.radius = homeRadius();
updateCamera();
}
let firstResize = true;
const resizeObserver = new ResizeObserver(() => {
const width = Math.max(1, canvasWrap.clientWidth);
const height = Math.max(1, canvasWrap.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.fov = camera.aspect < 1.05 ? 51 : 45;
camera.updateProjectionMatrix();
if (firstResize) {
orbit.radius = homeRadius();
firstResize = false;
}
updateCamera();
});
resizeObserver.observe(canvasWrap);
function setCombination(a, b) {
state.a = Boolean(a);
state.b = Boolean(b);
updateEverything();
}
function updateCircuitVisuals() {
const and = andOutput();
const or = orOutput();
for (const panel of panels) {
const output = panel.kind === "AND" ? and : or;
panel.switches[0].pivot.userData.targetAngle =
state.a ? 0.55 : -0.55;
panel.switches[1].pivot.userData.targetAngle =
state.b ? 0.55 : -0.55;
panel.switches[0].ledMaterial.emissiveIntensity = state.a ? 2.4 : 0.02;
panel.switches[1].ledMaterial.emissiveIntensity = state.b ? 2.4 : 0.02;
panel.wires.a.active = state.a;
panel.wires.b.active = state.b;
panel.outputWire.active = Boolean(output);
panel.wires.a.material.emissiveIntensity = state.a ? 1.65 : 0.02;
panel.wires.b.material.emissiveIntensity = state.b ? 1.65 : 0.02;
panel.outputWire.material.emissiveIntensity = output ? 1.9 : 0.02;
panel.outputTarget = output;
panel.gateTarget = output ? 0.38 : 0.06;
}
}
function updateChallenge() {
const challenge = challengeList[challengeIndex];
const current = comboKey();
const solved = challenge.targets.includes(current);
$("#challenge-number").textContent =
`Puzzle ${challengeIndex + 1} of ${challengeList.length}`;
$("#challenge-title").textContent = challenge.title;
$("#challenge-description").textContent = challenge.description;
const status = $("#challenge-status");
status.classList.toggle("success", solved);
status.classList.toggle("warn", !solved);
if (solved) {
if (challengeIndex === challengeList.length - 1) {
status.textContent = "Solved — you have completed all three puzzles.";
} else {
status.textContent = "Solved! The selected combination satisfies the goal.";
}
} else {
status.textContent = "Try one of the four combinations.";
}
const next = $("#next-challenge");
next.disabled = !solved;
next.textContent =
challengeIndex === challengeList.length - 1
? "Replay puzzles"
: "Next puzzle";
}
function updateEverything() {
const key = comboKey();
const and = andOutput();
const or = orOutput();
$$("button[data-combo]").forEach((button) => {
const active = button.dataset.combo === key;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
$$("tr[data-combo]").forEach((row) => {
row.classList.toggle("active", row.dataset.combo === key);
});
const aValue = state.a ? "1" : "0";
const bValue = state.b ? "1" : "0";
$('[data-switch="a"]').textContent = `A ${aValue}`;
$('[data-switch="b"]').textContent = `B ${bValue}`;
$('[data-switch="a"]').classList.toggle("on", state.a);
$('[data-switch="b"]').classList.toggle("on", state.b);
$('[data-switch="a"]').setAttribute("aria-pressed", String(state.a));
$('[data-switch="b"]').setAttribute("aria-pressed", String(state.b));
$("#read-and").textContent = and;
$("#read-or").textContent = or;
$("#and-value").textContent = and;
$("#or-value").textContent = or;
$("#combo-status").textContent = `A=${aValue} · B=${bValue}`;
$("#and-card").classList.toggle("energized", and === 1);
$("#or-card").classList.toggle("energized", or === 1);
const equal = and === or;
const banner = $("#comparison-banner");
banner.classList.toggle("equal", equal);
banner.classList.toggle("different", !equal);
if (equal) {
banner.textContent =
key === "00"
? "Both outputs are LOW (0)."
: "Both outputs are HIGH (1).";
} else {
banner.textContent =
"The gates disagree: AND is LOW while OR is HIGH.";
}
$("#live-region").textContent =
`Switch A ${aValue}, switch B ${bValue}. ` +
`AND output ${and}, OR output ${or}.`;
updateCircuitVisuals();
updateChallenge();
}
function stopAuto() {
if (autoTimer !== null) {
clearInterval(autoTimer);
autoTimer = null;
}
$("#run-label").textContent = "▶ Run all";
}
function startAuto() {
stopAuto();
autoIndex = 0;
$("#run-label").textContent = "■ Stop";
const applyNext = () => {
const index = autoIndex;
setCombination(
index === 2 || index === 3,
index === 1 || index === 3
);
autoIndex = (index + 1) % 4;
};
applyNext();
autoTimer = setInterval(applyNext, 1400);
}
$$("button[data-combo]").forEach((button) => {
button.addEventListener("click", () => {
stopAuto();
const [a, b] = button.dataset.combo.split("").map(Number);
setCombination(a, b);
});
});
$$("tr[data-combo]").forEach((row) => {
const selectRow = () => {
stopAuto();
const [a, b] = row.dataset.combo.split("").map(Number);
setCombination(a, b);
};
row.addEventListener("click", selectRow);
row.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
selectRow();
}
});
});
$('[data-switch="a"]').addEventListener("click", () => {
stopAuto();
state.a = !state.a;
updateEverything();
});
$('[data-switch="b"]').addEventListener("click", () => {
stopAuto();
state.b = !state.b;
updateEverything();
});
$("#run-button").addEventListener("click", () => {
if (autoTimer === null) {
startAuto();
} else {
stopAuto();
}
});
$("#camera-button").addEventListener("click", resetCamera);
$("#next-challenge").addEventListener("click", () => {
stopAuto();
if (challengeIndex < challengeList.length - 1) {
challengeIndex += 1;
} else {
challengeIndex = 0;
setCombination(false, false);
}
updateChallenge();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointers = new Map();
let pointerTravel = 0;
let multiTouch = false;
let lastPinchDistance = 0;
function getPinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
function hitSwitch(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
scene.updateMatrixWorld(true);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(switchTargets, false);
return hits.length ? hits[0].object.userData.switchIndex : null;
}
canvas.addEventListener("pointerdown", (event) => {
canvas.focus({ preventScroll: true });
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
pointerTravel = 0;
} else {
multiTouch = true;
lastPinchDistance = getPinchDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointers.size === 1 && !multiTouch) {
pointerTravel += Math.abs(dx) + Math.abs(dy);
orbit.yaw -= dx * 0.006;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.0045,
0.17,
0.72
);
updateCamera();
}
if (pointers.size === 2) {
const distance = getPinchDistance();
if (lastPinchDistance > 0 && distance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (lastPinchDistance / distance),
13.5,
28
);
updateCamera();
}
lastPinchDistance = distance;
}
});
function endPointer(event, cancelled = false) {
const wasMulti = multiTouch || pointers.size > 1;
pointers.delete(event.pointerId);
if (!wasMulti && !cancelled && pointerTravel < 7) {
const switchIndex = hitSwitch(event);
if (switchIndex !== null) {
stopAuto();
if (switchIndex === 0) {
state.a = !state.a;
} else {
state.b = !state.b;
}
updateEverything();
}
}
if (pointers.size === 0) {
setTimeout(() => {
multiTouch = false;
pointerTravel = 0;
}, 0);
}
}
canvas.addEventListener("pointerup", (event) => endPointer(event));
canvas.addEventListener("pointercancel", (event) => endPointer(event, true));
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.01,
13.5,
28
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "a") {
stopAuto();
state.a = !state.a;
updateEverything();
} else if (key === "b") {
stopAuto();
state.b = !state.b;
updateEverything();
} else if (key === "r") {
resetCamera();
} else if (key === "arrowright" || key === "arrowleft") {
event.preventDefault();
stopAuto();
const combinations = [
[false, false],
[false, true],
[true, false],
[true, true]
];
const current = combinations.findIndex(
([a, b]) => a === state.a && b === state.b
);
const direction = key === "arrowright" ? 1 : -1;
const next =
(current + direction + combinations.length) % combinations.length;
setCombination(...combinations[next]);
}
});
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
for (const panel of panels) {
for (const switchObject of panel.switches) {
const target = switchObject.pivot.userData.targetAngle ?? -0.55;
switchObject.pivot.rotation.z = THREE.MathUtils.lerp(
switchObject.pivot.rotation.z,
target,
1 - Math.exp(-delta * 12)
);
}
panel.gateMaterial.emissiveIntensity = THREE.MathUtils.lerp(
panel.gateMaterial.emissiveIntensity,
panel.gateTarget,
1 - Math.exp(-delta * 10)
);
const lampOn = panel.outputTarget === 1;
const lampColor = lampOn
? new THREE.Color(0xffd166)
: new THREE.Color(0x29354a);
panel.lampMaterial.color.lerp(
lampColor,
1 - Math.exp(-delta * 11)
);
panel.lampMaterial.emissiveIntensity = THREE.MathUtils.lerp(
panel.lampMaterial.emissiveIntensity,
lampOn ? 2.3 : 0.02,
1 - Math.exp(-delta * 11)
);
const wires = [panel.wires.a, panel.wires.b, panel.outputWire];
for (const wire of wires) {
wire.pulse.visible = wire.active;
if (wire.active) {
const speed = reducedMotion ? 0.16 : 0.42;
const progress = (elapsed * speed + wire.pulseOffset) % 1;
wire.pulse.position.copy(wire.curve.getPoint(progress));
const pulseScale =
0.82 + Math.sin(progress * Math.PI) * 0.35;
wire.pulse.scale.setScalar(pulseScale);
}
}
panel.lamp.rotation.y += delta * 0.35;
}
renderer.render(scene, camera);
}
updateEverything();
resetCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```