Prompt specification
Build or customize the Three.js 3D scene "From individual switches to a logic board" 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: From individual switches to a logic board
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/from-individual-switches-to-a-logic-board-x2551
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this low-poly scene: Build a display linking one transistor switch, two-switch logic arrangement, and compact board. Show the same binary state represented consistently at each scale.
Working Three.js Starter (from-individual-switches-to-a-logic-board-x2551):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>One State · Three Scales</title>
<style>
:root {
--bg: #07121d;
--panel: rgba(8, 24, 37, 0.84);
--line: rgba(123, 205, 230, 0.22);
--text: #e9f5f7;
--muted: #8ba7b2;
--a: #54ddff;
--b: #ffb45c;
--q: #78f0bf;
}
* {
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;
}
#stage,
#stage canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
touch-action: none;
}
.ui {
position: absolute;
inset: 0;
pointer-events: none;
z-index: 5;
}
.title {
position: absolute;
top: 28px;
left: 32px;
max-width: 440px;
text-shadow: 0 2px 16px rgba(0, 0, 0, 0.55);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #7edff3;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 2px;
background: #54ddff;
content: "";
box-shadow: 0 0 10px rgba(84, 221, 255, 0.55);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 46px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
max-width: 410px;
margin: 12px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.state-panel {
position: absolute;
top: 26px;
right: 28px;
width: 268px;
padding: 17px;
border: 1px solid var(--line);
border-radius: 16px;
background:
linear-gradient(135deg, rgba(21, 51, 68, 0.72), rgba(8, 23, 35, 0.92)),
var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.live {
display: flex;
align-items: center;
gap: 6px;
color: var(--q);
}
.live::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--q);
box-shadow: 0 0 10px var(--q);
content: "";
}
.state-bits {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin: 14px 0;
}
.state-bit {
position: relative;
display: flex;
align-items: center;
justify-content: space-between;
min-height: 64px;
padding: 11px 13px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 11px;
background: rgba(0, 0, 0, 0.17);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.state-bit::after {
position: absolute;
right: -11px;
bottom: -18px;
width: 60px;
height: 60px;
border-radius: 50%;
background: currentColor;
opacity: 0.07;
content: "";
}
.state-bit.a {
color: var(--a);
}
.state-bit.b {
color: var(--b);
}
.state-bit .bit-name {
color: var(--muted);
font-family: inherit;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
}
.state-bit strong {
font-size: 35px;
font-weight: 700;
line-height: 1;
}
.state-bit.off strong {
color: #425867;
}
.input-buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.input-buttons button {
display: flex;
align-items: center;
justify-content: space-between;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.08em;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.input-buttons button:hover {
border-color: rgba(255, 255, 255, 0.25);
background: rgba(255, 255, 255, 0.07);
transform: translateY(-1px);
}
.input-buttons button:active {
transform: translateY(1px);
}
.input-buttons .dot {
width: 8px;
height: 8px;
margin-right: 7px;
border-radius: 50%;
background: #304754;
}
.input-buttons button:first-child .dot {
background: var(--a);
box-shadow: 0 0 9px var(--a);
}
.input-buttons button:last-child .dot {
background: var(--b);
box-shadow: 0 0 9px var(--b);
}
.input-buttons button b {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 14px;
}
.logic-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
}
.logic-row strong {
color: var(--q);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
}
.legend {
position: absolute;
left: 32px;
bottom: 28px;
display: flex;
align-items: center;
gap: 18px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.legend span {
display: flex;
align-items: center;
gap: 7px;
}
.legend i {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 9px currentColor;
}
.legend .a {
color: var(--a);
}
.legend .b {
color: var(--b);
}
.legend .q {
color: var(--q);
}
.help {
position: absolute;
right: 30px;
bottom: 29px;
color: rgba(169, 195, 203, 0.72);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.help kbd {
padding: 2px 5px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 4px;
color: #d6e6e9;
background: rgba(255, 255, 255, 0.04);
font: inherit;
}
.reset {
position: absolute;
right: 30px;
bottom: 59px;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 7px;
background: rgba(8, 24, 37, 0.72);
color: #b8d0d6;
font: inherit;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
cursor: pointer;
pointer-events: auto;
}
@media (max-width: 760px) {
.title {
top: 19px;
left: 18px;
max-width: calc(100% - 36px);
}
.subtitle {
display: none;
}
.state-panel {
top: auto;
right: 14px;
bottom: 14px;
left: 14px;
width: auto;
padding: 12px;
}
.state-bits {
margin: 9px 0;
}
.state-bit {
min-height: 47px;
padding: 7px 10px;
}
.state-bit strong {
font-size: 27px;
}
.logic-row {
margin-top: 8px;
padding-top: 8px;
}
.legend,
.help,
.reset {
display: none;
}
}
</style>
</head>
<body>
<div id="stage" aria-label="Interactive low-poly binary logic scene"></div>
<div class="ui">
<header class="title">
<div class="eyebrow">Binary systems · 03 scales</div>
<h1>ONE STATE.<br />THREE SCALES.</h1>
<p class="subtitle">
A transistor switch, a two-switch logic array, and a compact board.
The same two-bit signal is carried through every representation.
</p>
</header>
<section class="state-panel" aria-label="Shared binary state">
<div class="panel-heading">
<span>Shared binary state</span>
<span class="live">Live</span>
</div>
<div class="state-bits">
<div class="state-bit a" id="stateCardA">
<span class="bit-name">BIT A</span>
<strong id="stateA">1</strong>
</div>
<div class="state-bit b" id="stateCardB">
<span class="bit-name">BIT B</span>
<strong id="stateB">0</strong>
</div>
</div>
<div class="input-buttons">
<button id="buttonA" type="button" aria-pressed="true">
<span><i class="dot"></i>INPUT A</span>
<b id="buttonAValue">1</b>
</button>
<button id="buttonB" type="button" aria-pressed="false">
<span><i class="dot"></i>INPUT B</span>
<b id="buttonBValue">0</b>
</button>
</div>
<div class="logic-row">
<span>DERIVED OUTPUT · Q = A ⊕ B</span>
<strong id="outputQ">1</strong>
</div>
</section>
<div class="legend">
<span class="a"><i></i>Bit A</span>
<span class="b"><i></i>Bit B</span>
<span class="q"><i></i>XOR output</span>
</div>
<button class="reset" id="resetView" type="button">Reset camera</button>
<div class="help">
<kbd>Drag</kbd> orbit · <kbd>Scroll</kbd> zoom · <kbd>Click</kbd> switches
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -----------------------------------------------------------------------
// Scene and renderer
// -----------------------------------------------------------------------
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07121d);
scene.fog = new THREE.Fog(0x07121d, 27, 55);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
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.08;
stage.appendChild(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional low-poly transistor, switch logic, and circuit board display"
);
const COLORS = {
A: 0x54ddff,
B: 0xffb45c,
Q: 0x78f0bf,
OFF: 0x172b37,
floor: 0x081722,
platform: 0x102a3a,
platformTop: 0x17394c,
edge: 0x2b6078,
metal: 0x9db5bd,
dark: 0x101820,
black: 0x080d12,
pcb: 0x175a4c,
pcbEdge: 0x0c3d34,
copper: 0xd49c58,
white: 0xeaf7f8
};
const state = {
A: 1,
B: 0
};
const bitIndicators = [];
const switchEntries = [];
const stateLamps = [];
const stateDisplays = [];
const bridgePulses = [];
const pickables = [];
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.08,
flatShading: true,
...options
});
}
const MAT = {
platform: standard(COLORS.platform),
platformTop: standard(COLORS.platformTop),
edge: standard(COLORS.edge, {
emissive: COLORS.edge,
emissiveIntensity: 0.12
}),
metal: standard(COLORS.metal, {
roughness: 0.42,
metalness: 0.72
}),
dark: standard(COLORS.dark),
black: standard(COLORS.black, {
roughness: 0.88
}),
pcb: standard(COLORS.pcb, {
roughness: 0.9
}),
pcbEdge: standard(COLORS.pcbEdge),
copper: standard(COLORS.copper, {
roughness: 0.5,
metalness: 0.48
}),
white: standard(COLORS.white)
};
function addBox(parent, size, position, material, rotation = null) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
if (rotation) {
mesh.rotation.set(rotation[0], rotation[1], rotation[2]);
}
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
position,
material,
segments = 8
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(parent, start, end, radius, material) {
const a = new THREE.Vector3(...start);
const b = new THREE.Vector3(...end);
const direction = b.clone().sub(a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 6),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function addTube(parent, points, color, opacity = 0.45, radius = 0.025) {
const curve = new THREE.CatmullRomCurve3(
points.map((point) => new THREE.Vector3(...point))
);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
const mesh = new THREE.Mesh(
new THREE.TubeGeometry(curve, 22, radius, 4, false),
material
);
mesh.renderOrder = 3;
parent.add(mesh);
return { curve, mesh };
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function colorHex(color) {
return `#${color.toString(16).padStart(6, "0")}`;
}
function makePlaque(
parent,
title,
subtitle,
position,
width = 4.1,
accent = COLORS.A
) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 8, 1008, 240, 24);
ctx.fillStyle = "rgba(6, 20, 31, 0.88)";
ctx.fill();
ctx.strokeStyle = colorHex(accent);
ctx.globalAlpha = 0.55;
ctx.lineWidth = 4;
ctx.stroke();
ctx.globalAlpha = 1;
ctx.fillStyle = colorHex(accent);
ctx.fillRect(35, 35, 8, 186);
ctx.textBaseline = "middle";
ctx.fillStyle = "#eaf7f8";
ctx.font = "700 58px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(title, 72, 98);
ctx.fillStyle = "#89a9b5";
ctx.font = "700 29px ui-sans-serif, system-ui, sans-serif";
ctx.letterSpacing = "2px";
ctx.fillText(subtitle, 74, 170);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sprite.position.set(...position);
sprite.scale.set(width, width / 4, 1);
sprite.renderOrder = 20;
parent.add(sprite);
return sprite;
}
function makeSmallLabel(
parent,
text,
position,
width = 0.8,
color = COLORS.white
) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 512, 128);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = colorHex(color);
ctx.font = "800 58px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sprite.position.set(...position);
sprite.scale.set(width, width / 4, 1);
sprite.renderOrder = 22;
parent.add(sprite);
return sprite;
}
// -----------------------------------------------------------------------
// Reusable scene components
// -----------------------------------------------------------------------
function makePlatform(x, width, depth) {
const group = new THREE.Group();
group.position.x = x;
addBox(
group,
[width, 0.34, depth],
[0, 0.20, 0],
MAT.platform
);
addBox(
group,
[width - 0.14, 0.13, depth - 0.14],
[0, 0.435, 0],
MAT.platformTop
);
addBox(
group,
[width - 0.42, 0.035, 0.035],
[0, 0.40, depth / 2 - 0.08],
MAT.edge
);
scene.add(group);
return group;
}
function registerPickable(mesh, bit) {
mesh.userData.bit = bit;
pickables.push(mesh);
return mesh;
}
function makeToggle(parent, x, y, z, scale, bit) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
parent.add(group);
const base = registerPickable(
addCylinder(group, 0.34, 0.39, 0.12, [0, 0.06, 0], MAT.dark, 10),
bit
);
const ringMaterial = standard(COLORS[bit], {
emissive: COLORS[bit],
emissiveIntensity: 0.85,
roughness: 0.45
});
registerPickable(
addCylinder(
group,
0.235,
0.235,
0.045,
[0, 0.145, 0],
ringMaterial,
12
),
bit
);
const pivot = new THREE.Group();
pivot.position.y = 0.17;
pivot.rotation.x = -0.65;
group.add(pivot);
const lever = registerPickable(
addBox(
pivot,
[0.105, 0.62, 0.105],
[0, 0.31, 0],
MAT.metal
),
bit
);
const tipMaterial = standard(COLORS[bit], {
emissive: COLORS[bit],
emissiveIntensity: 0.7
});
const tip = registerPickable(
new THREE.Mesh(
new THREE.SphereGeometry(0.135, 8, 6),
tipMaterial
),
bit
);
tip.position.y = 0.65;
tip.castShadow = true;
pivot.add(tip);
makeSmallLabel(group, bit, [0, -0.08, 0.43], 0.44, COLORS[bit]);
const entry = {
bit,
pivot,
targetAngle: state[bit] ? 0.65 : -0.65
};
switchEntries.push(entry);
bitIndicators.push({
bit,
materials: [ringMaterial, tipMaterial]
});
return { group, pivot, base, lever, ringMaterial, tipMaterial };
}
function makeLamp(parent, x, y, z, color, scale = 1, key = null) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
parent.add(group);
addCylinder(group, 0.17, 0.20, 0.09, [0, 0.045, 0], MAT.dark, 8);
const lensMaterial = standard(color, {
emissive: color,
emissiveIntensity: 0.95,
roughness: 0.3
});
const lens = new THREE.Mesh(
new THREE.SphereGeometry(0.145, 8, 6),
lensMaterial
);
lens.scale.y = 0.72;
lens.position.y = 0.145;
lens.castShadow = true;
group.add(lens);
if (key) {
stateLamps.push({ key, material: lensMaterial });
}
return { group, lensMaterial };
}
function makeStateDisplay(parent, position, scale = 1) {
const group = new THREE.Group();
group.position.set(...position);
group.scale.setScalar(scale);
parent.add(group);
addBox(
group,
[1.70, 1.48, 0.22],
[0, 0, 0],
MAT.dark
);
addBox(
group,
[1.52, 1.29, 0.035],
[0, 0, 0.125],
MAT.black
);
const segmentGeometry = {
a: [0.43, 0.062],
b: [0.062, 0.27],
c: [0.062, 0.27],
d: [0.43, 0.062],
e: [0.062, 0.27],
f: [0.062, 0.27],
g: [0.43, 0.062]
};
const segmentPositions = {
a: [0, 0.35, 0],
b: [0.22, 0.175, 0],
c: [0.22, -0.175, 0],
d: [0, -0.35, 0],
e: [-0.22, -0.175, 0],
f: [-0.22, 0.175, 0],
g: [0, 0, 0]
};
const digitColors = [COLORS.A, COLORS.B];
const digits = [];
for (let digitIndex = 0; digitIndex < 2; digitIndex++) {
const digitX = digitIndex === 0 ? -0.38 : 0.38;
const segments = [];
for (const name of Object.keys(segmentGeometry)) {
const size = segmentGeometry[name];
const position = segmentPositions[name];
const material = standard(COLORS.OFF, {
emissive: 0x000000,
emissiveIntensity: 0
});
const mesh = addBox(
group,
[size[0], size[1], 0.035],
[digitX + position[0], position[1], 0.16],
material
);
segments.push({ name, material });
}
makeSmallLabel(
group,
digitIndex === 0 ? "A" : "B",
[digitX, 0.59, 0.18],
0.34,
digitColors[digitIndex]
);
digits.push(segments);
}
makeSmallLabel(group, "STATE", [0, 0.86, 0.18], 0.72, 0x86aab5);
const display = { digits, digitColors };
stateDisplays.push(display);
return display;
}
function setIndicator(material, on, color) {
material.color.set(on ? color : COLORS.OFF);
material.emissive.set(on ? color : 0x000000);
material.emissiveIntensity = on ? 1.05 : 0;
}
// -----------------------------------------------------------------------
// Environment
// -----------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0x8edfff, 0x10202c, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d2, 3.1);
keyLight.position.set(-7, 14, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(COLORS.A, 18, 13, 2);
cyanLight.position.set(-5, 4, 5);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(COLORS.B, 13, 12, 2);
amberLight.position.set(7, 3.5, 4);
scene.add(amberLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
standard(COLORS.floor, { roughness: 1 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.02;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(32, 32, 0x245268, 0x102b3a);
grid.position.y = 0.005;
grid.material.transparent = true;
grid.material.opacity = 0.33;
scene.add(grid);
// -----------------------------------------------------------------------
// Station 01: one transistor switch
// -----------------------------------------------------------------------
const transistorStation = makePlatform(-7.2, 4.6, 4.0);
const transistorProfile = [
new THREE.Vector2(0.06, 0.0),
new THREE.Vector2(0.40, 0.035),
new THREE.Vector2(0.58, 0.24),
new THREE.Vector2(0.54, 0.70),
new THREE.Vector2(0.29, 0.98),
new THREE.Vector2(0.05, 1.05)
];
const transistorBody = new THREE.Mesh(
new THREE.LatheGeometry(transistorProfile, 6),
MAT.black
);
transistorBody.position.set(-0.57, 1.02, -0.56);
transistorBody.rotation.y = Math.PI / 6;
transistorBody.castShadow = true;
transistorBody.receiveShadow = true;
transistorStation.add(transistorBody);
const transistorLeadOffsets = [-0.24, 0, 0.24];
transistorLeadOffsets.forEach((offset, index) => {
const top = [-0.57 + offset, 1.04, -0.56];
const knee = [-0.57 + offset, 0.64, -0.37 - index * 0.03];
const end = [-0.57 + offset, 0.51, -0.19 - index * 0.03];
cylinderBetween(
transistorStation,
top,
knee,
0.033,
MAT.metal
);
cylinderBetween(
transistorStation,
knee,
end,
0.033,
MAT.metal
);
});
makeToggle(transistorStation, 0.92, 0.51, 0.58, 1.08, "A");
addTube(
transistorStation,
[
[0.92, 0.58, 0.42],
[0.73, 0.73, 0.05],
[-0.25, 0.72, -0.3],
[-0.56, 1.07, -0.52]
],
COLORS.A,
0.7,
0.025
);
makeLamp(
transistorStation,
0.98,
0.52,
-0.62,
COLORS.A,
0.92,
"A"
);
makeSmallLabel(
transistorStation,
"TRANSISTOR",
[-0.57, 2.28, -0.55],
1.1,
0x9eb9c2
);
makeStateDisplay(transistorStation, [0, 3.05, 0.18], 1.02);
makePlaque(
transistorStation,
"01 / TRANSISTOR",
"ONE SWITCH · CHANNEL A",
[0, 4.18, 0.32],
4.15,
COLORS.A
);
// -----------------------------------------------------------------------
// Station 02: two-switch logic arrangement
// -----------------------------------------------------------------------
const logicStation = makePlatform(0, 4.8, 4.0);
makeToggle(logicStation, -1.30, 0.51, 0.54, 1.02, "A");
makeToggle(logicStation, 1.30, 0.51, 0.54, 1.02, "B");
const gateBody = addBox(
logicStation,
[1.25, 0.70, 0.30],
[0, 1.30, -0.58],
MAT.dark
);
gateBody.castShadow = true;
addBox(
logicStation,
[0.92, 0.44, 0.025],
[0, 1.30, -0.415],
MAT.pcbEdge
);
makeSmallLabel(
logicStation,
"XOR",
[0, 1.31, -0.37],
0.72,
COLORS.Q
);
addTube(
logicStation,
[
[-1.30, 0.59, 0.38],
[-1.14, 0.91, -0.03],
[-0.54, 1.03, -0.40],
[-0.60, 1.27, -0.54]
],
COLORS.A,
0.68,
0.026
);
addTube(
logicStation,
[
[1.30, 0.59, 0.38],
[1.13, 0.91, -0.03],
[0.54, 1.03, -0.40],
[0.60, 1.27, -0.54]
],
COLORS.B,
0.68,
0.026
);
addTube(
logicStation,
[
[0, 1.02, -0.58],
[0, 0.86, -0.08],
[0, 0.63, 0.64],
[0, 0.58, 0.91]
],
COLORS.Q,
0.58,
0.024
);
makeLamp(
logicStation,
0,
0.51,
0.96,
COLORS.Q,
1.0,
"Q"
);
makeSmallLabel(
logicStation,
"Q",
[0.38, 0.76, 0.95],
0.38,
COLORS.Q
);
makeStateDisplay(logicStation, [0, 3.14, -0.04], 0.93);
makePlaque(
logicStation,
"02 / LOGIC ARRAY",
"TWO SWITCHES · XOR OUTPUT",
[0, 4.17, 0.30],
4.15,
COLORS.Q
);
// -----------------------------------------------------------------------
// Station 03: compact board
// -----------------------------------------------------------------------
const boardStation = makePlatform(7.2, 4.5, 3.8);
addBox(
boardStation,
[3.50, 0.15, 2.45],
[0, 0.60, 0],
MAT.pcb
);
addBox(
boardStation,
[3.56, 0.065, 2.51],
[0, 0.535, 0],
MAT.pcbEdge
);
// Copper traces on the board.
addBox(
boardStation,
[0.035, 0.018, 1.15],
[-1.05, 0.686, 0.12],
MAT.copper
);
addBox(
boardStation,
[0.035, 0.018, 1.15],
[-0.22, 0.686, 0.12],
MAT.copper
);
addBox(
boardStation,
[1.00, 0.018, 0.035],
[-0.64, 0.687, 0.67],
MAT.copper
);
addBox(
boardStation,
[1.02, 0.018, 0.035],
[0.96, 0.687, -0.53],
MAT.copper
);
addBox(
boardStation,
[0.035, 0.018, 0.86],
[1.50, 0.687, -0.12],
MAT.copper
);
// Main low-profile integrated circuit.
addBox(
boardStation,
[1.18, 0.25, 0.68],
[0.62, 0.79, -0.30],
MAT.black
);
for (let i = 0; i < 7; i++) {
const z = -0.56 + i * 0.087;
addBox(
boardStation,
[0.22, 0.035, 0.042],
[0.01, 0.725, z],
MAT.metal
);
addBox(
boardStation,
[0.22, 0.035, 0.042],
[1.23, 0.725, z],
MAT.metal
);
}
makeSmallLabel(
boardStation,
"LOGIC",
[0.62, 0.94, -0.02],
0.64,
0xa8c0c6
);
// Tiny regulator.
addBox(
boardStation,
[0.42, 0.18, 0.34],
[-1.18, 0.77, -0.55],
MAT.dark
);
// Compact input toggles.
makeToggle(boardStation, -1.05, 0.69, 0.62, 0.53, "A");
makeToggle(boardStation, -0.22, 0.69, 0.62, 0.53, "B");
makeLamp(boardStation, 0.88, 0.69, 0.63, COLORS.A, 0.58, "A");
makeLamp(boardStation, 1.31, 0.69, 0.63, COLORS.B, 0.58, "B");
makeSmallLabel(
boardStation,
"A",
[0.88, 0.93, 0.67],
0.26,
COLORS.A
);
makeSmallLabel(
boardStation,
"B",
[1.31, 0.93, 0.67],
0.26,
COLORS.B
);
// Display support.
cylinderBetween(
boardStation,
[0, 0.69, -0.91],
[0, 2.08, -0.91],
0.045,
MAT.metal
);
makeStateDisplay(boardStation, [0, 2.44, -0.89], 0.58);
makePlaque(
boardStation,
"03 / COMPACT BOARD",
"TWO CHANNELS · ONE SHARED STATE",
[0, 3.54, -0.78],
4.15,
COLORS.B
);
// -----------------------------------------------------------------------
// Signal bridges between scales
// -----------------------------------------------------------------------
function addBridge(start, end, bit, lane) {
const s = new THREE.Vector3(...start);
const e = new THREE.Vector3(...end);
const mid = s.clone().lerp(e, 0.5);
mid.y = 1.88 + lane * 0.16;
mid.z += lane * 0.11;
const points = [
s,
s.clone().lerp(mid, 0.42).add(new THREE.Vector3(0, 0.36, 0)),
mid,
e.clone().lerp(mid, 0.58).add(new THREE.Vector3(0, 0.36, 0)),
e
];
const color = COLORS[bit];
const { curve, mesh } = addTube(scene, points, color, 0.28, 0.024);
const pulse = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 8, 6),
new THREE.MeshBasicMaterial({ color })
);
pulse.renderOrder = 8;
scene.add(pulse);
bridgePulses.push({
bit,
curve,
mesh,
pulse,
offset: bit === "A" ? 0 : 0.48
});
}
const leftBridgeStart = [-5.20, 0.72, 0.77];
const centerBridgeStart = [-2.48, 0.76, 0.77];
const centerBridgeEnd = [2.47, 0.76, 0.77];
const boardBridgeEnd = [5.17, 0.72, 0.77];
addBridge(leftBridgeStart, centerBridgeStart, "A", -1);
addBridge(leftBridgeStart, centerBridgeStart, "B", 1);
addBridge(centerBridgeEnd, boardBridgeEnd, "A", -1);
addBridge(centerBridgeEnd, boardBridgeEnd, "B", 1);
// -----------------------------------------------------------------------
// Shared state update
// -----------------------------------------------------------------------
const stateAElement = document.getElementById("stateA");
const stateBElement = document.getElementById("stateB");
const stateCardA = document.getElementById("stateCardA");
const stateCardB = document.getElementById("stateCardB");
const buttonA = document.getElementById("buttonA");
const buttonB = document.getElementById("buttonB");
const buttonAValue = document.getElementById("buttonAValue");
const buttonBValue = document.getElementById("buttonBValue");
const outputQElement = document.getElementById("outputQ");
const segmentMap = {
0: new Set(["a", "b", "c", "d", "e", "f"]),
1: new Set(["b", "c"])
};
function applyState() {
const bits = {
A: state.A,
B: state.B,
Q: state.A ^ state.B
};
// Physical switch angles.
switchEntries.forEach((entry) => {
entry.targetAngle = state[entry.bit] ? 0.65 : -0.65;
});
// Colored indicators.
bitIndicators.forEach((entry) => {
entry.materials.forEach((material) => {
setIndicator(material, state[entry.bit], COLORS[entry.bit]);
});
});
// Lamps: A, B and XOR output.
stateLamps.forEach((lamp) => {
setIndicator(lamp.material, bits[lamp.key], COLORS[lamp.key]);
});
// Seven-segment state displays.
stateDisplays.forEach((display) => {
const digits = [state.A, state.B];
display.digits.forEach((segments, digitIndex) => {
segments.forEach((segment) => {
const active = segmentMap[digits[digitIndex]].has(segment.name);
const color = display.digitColors[digitIndex];
segment.material.color.set(active ? color : COLORS.OFF);
segment.material.emissive.set(active ? color : 0x000000);
segment.material.emissiveIntensity = active ? 1.05 : 0;
});
});
});
// Bridge signal visibility.
bridgePulses.forEach((entry) => {
entry.pulse.visible = state[entry.bit] === 1;
});
// HTML representation.
stateAElement.textContent = state.A;
stateBElement.textContent = state.B;
stateCardA.classList.toggle("off", state.A === 0);
stateCardB.classList.toggle("off", state.B === 0);
buttonAValue.textContent = state.A;
buttonBValue.textContent = state.B;
buttonA.setAttribute("aria-pressed", String(state.A === 1));
buttonB.setAttribute("aria-pressed", String(state.B === 1));
outputQElement.textContent = bits.Q;
outputQElement.style.color = bits.Q ? "var(--q)" : "#425867";
}
function toggleBit(bit) {
state[bit] = state[bit] ? 0 : 1;
applyState();
}
buttonA.addEventListener("click", () => toggleBit("A"));
buttonB.addEventListener("click", () => toggleBit("B"));
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "a") toggleBit("A");
if (event.key.toLowerCase() === "b") toggleBit("B");
});
applyState();
// -----------------------------------------------------------------------
// Direct camera controls — no Three.js addons
// -----------------------------------------------------------------------
const cameraControl = {
yaw: -0.055,
pitch: 0.49,
zoom: 1,
target: new THREE.Vector3(0, 1.72, 0)
};
function updateCamera() {
const width = stage.clientWidth || window.innerWidth;
const height = stage.clientHeight || window.innerHeight;
const aspect = width / height;
const verticalSize = Math.max(10.7, 21.6 / aspect) / cameraControl.zoom;
camera.left = (-verticalSize * aspect) / 2;
camera.right = (verticalSize * aspect) / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
camera.updateProjectionMatrix();
const distance = 24;
const cp = Math.cos(cameraControl.pitch);
const sp = Math.sin(cameraControl.pitch);
camera.position.set(
cameraControl.target.x +
distance * Math.sin(cameraControl.yaw) * cp,
cameraControl.target.y + distance * sp,
cameraControl.target.z +
distance * Math.cos(cameraControl.yaw) * cp
);
camera.lookAt(cameraControl.target);
}
function resetCamera() {
cameraControl.yaw = -0.055;
cameraControl.pitch = 0.49;
cameraControl.zoom = 1;
updateCamera();
}
document.getElementById("resetView").addEventListener("click", resetCamera);
const canvas = renderer.domElement;
const pointers = new Map();
let previousPinchDistance = 0;
function pointerDistance() {
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 pickAt(clientX, clientY) {
const rect = canvas.getBoundingClientRect();
pointerNDC.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObjects(pickables, false);
return hits.length ? hits[0].object.userData.bit : null;
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
startX: event.clientX,
startY: event.clientY,
moved: false
});
if (pointers.size === 2) {
previousPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const pointer = pointers.get(event.pointerId);
if (!pointer) {
if (event.pointerType === "mouse") {
const bit = pickAt(event.clientX, event.clientY);
canvas.style.cursor = bit ? "pointer" : "grab";
}
return;
}
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (
Math.hypot(
event.clientX - pointer.startX,
event.clientY - pointer.startY
) > 5
) {
pointer.moved = true;
}
if (pointers.size === 1) {
cameraControl.yaw = THREE.MathUtils.clamp(
cameraControl.yaw - dx * 0.006,
-0.82,
0.82
);
cameraControl.pitch = THREE.MathUtils.clamp(
cameraControl.pitch + dy * 0.0045,
0.22,
0.95
);
canvas.style.cursor = "grabbing";
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
cameraControl.zoom = THREE.MathUtils.clamp(
cameraControl.zoom * (distance / previousPinchDistance),
0.7,
2.25
);
}
previousPinchDistance = distance;
}
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
const pointer = pointers.get(event.pointerId);
const wasSinglePointer = pointers.size === 1;
if (wasSinglePointer && pointer && !pointer.moved) {
const bit = pickAt(event.clientX, event.clientY);
if (bit) toggleBit(bit);
}
pointers.delete(event.pointerId);
if (pointers.size < 2) {
previousPinchDistance = 0;
}
canvas.style.cursor = "grab";
});
canvas.addEventListener("pointercancel", (event) => {
pointers.delete(event.pointerId);
previousPinchDistance = 0;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraControl.zoom = THREE.MathUtils.clamp(
cameraControl.zoom * Math.exp(-event.deltaY * 0.001),
0.7,
2.25
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
function resize() {
const width = stage.clientWidth || window.innerWidth;
const height = stage.clientHeight || window.innerHeight;
renderer.setSize(width, height, false);
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// -----------------------------------------------------------------------
// Animation
// -----------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
const smoothing = 1 - Math.exp(-delta * 12);
switchEntries.forEach((entry) => {
entry.pivot.rotation.x = THREE.MathUtils.lerp(
entry.pivot.rotation.x,
entry.targetAngle,
smoothing
);
});
bridgePulses.forEach((entry) => {
if (!entry.pulse.visible) return;
const progress = (elapsed * 0.29 + entry.offset) % 1;
const eased = progress * progress * (3 - 2 * progress);
entry.pulse.position.copy(entry.curve.getPoint(eased));
const pulseScale = 0.84 + Math.sin(elapsed * 8 + entry.offset * 8) * 0.16;
entry.pulse.scale.setScalar(pulseScale);
});
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```