Prompt specification
Build or customize the Three.js 3D scene "Circuit-break location explorer" 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: Circuit-break location explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/circuit-break-location-explorer-x1616
Scene Description Prompt:
Circuit-break location explorer. Let users open one of several connections in a simple virtual loop; show current stopping throughout the ideal series path without depicting charge as consumed by the lamp. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (circuit-break-location-explorer-x1616):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Circuit Break Explorer</title>
<style>
:root {
color-scheme: dark;
--bg: #080d18;
--panel: #101827;
--panel-2: #141f31;
--line: #26354a;
--text: #e8f2ff;
--muted: #8da1b9;
--cyan: #7ceeff;
--green: #67f0a5;
--red: #ff647c;
--amber: #ffc45c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#app {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
header {
min-height: 74px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 13px 22px;
border-bottom: 1px solid var(--line);
background:
linear-gradient(90deg, rgba(124, 238, 255, 0.045), transparent 38%),
#0b1220;
z-index: 10;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 3px;
transform: rotate(-4deg);
flex: 0 0 auto;
}
.brand-mark i {
display: block;
border-radius: 2px;
background: #254157;
}
.brand-mark i:nth-child(1),
.brand-mark i:nth-child(3),
.brand-mark i:nth-child(5),
.brand-mark i:nth-child(7),
.brand-mark i:nth-child(9) {
background: var(--cyan);
box-shadow: 0 0 12px rgba(124, 238, 255, 0.38);
}
.brand-mark i:nth-child(2),
.brand-mark i:nth-child(8) {
background: var(--amber);
}
.brand h1 {
margin: 0;
font-size: clamp(16px, 2vw, 22px);
letter-spacing: 0.055em;
line-height: 1.1;
text-transform: uppercase;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.04em;
}
.global-status {
display: inline-flex;
align-items: center;
gap: 9px;
flex: 0 0 auto;
border: 1px solid;
border-radius: 999px;
padding: 9px 13px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.global-status::before {
content: "";
width: 8px;
height: 8px;
border-radius: 2px;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.global-status.closed {
color: var(--green);
border-color: rgba(103, 240, 165, 0.3);
background: rgba(103, 240, 165, 0.07);
}
.global-status.open {
color: var(--red);
border-color: rgba(255, 100, 124, 0.3);
background: rgba(255, 100, 124, 0.07);
}
main {
flex: 1;
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 350px;
}
#scenePanel {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, rgba(40, 89, 111, 0.19), transparent 46%),
#080d18;
}
#canvasHost {
position: absolute;
inset: 0;
}
#canvasHost canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.scene-top {
position: absolute;
top: 18px;
left: 18px;
z-index: 4;
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 8px;
pointer-events: none;
}
.scene-chip {
padding: 8px 10px;
border: 1px solid rgba(124, 238, 255, 0.19);
border-radius: 5px;
background: rgba(7, 13, 24, 0.74);
backdrop-filter: blur(8px);
color: #b9d2e5;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scene-chip strong {
color: var(--cyan);
}
.view-actions {
position: absolute;
top: 18px;
right: 18px;
z-index: 5;
display: flex;
gap: 8px;
}
.small-button {
border: 1px solid #2b3b50;
border-radius: 5px;
padding: 8px 11px;
color: #b9c9db;
background: rgba(11, 18, 31, 0.82);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition:
border-color 150ms ease,
color 150ms ease,
background 150ms ease;
}
.small-button:hover {
color: white;
border-color: var(--cyan);
background: rgba(124, 238, 255, 0.08);
}
.scene-bottom {
position: absolute;
left: 18px;
right: 18px;
bottom: 18px;
z-index: 4;
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 16px;
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px;
max-width: 560px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid rgba(82, 105, 132, 0.32);
border-radius: 4px;
background: rgba(7, 13, 24, 0.74);
color: #9eb2c7;
font-size: 10px;
backdrop-filter: blur(8px);
}
.legend i {
width: 8px;
height: 8px;
display: inline-block;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 8px rgba(124, 238, 255, 0.55);
}
.legend .lamp-key {
background: var(--amber);
box-shadow: 0 0 8px rgba(255, 196, 92, 0.55);
}
.legend .break-key {
background: var(--red);
box-shadow: 0 0 8px rgba(255, 100, 124, 0.55);
}
.gesture-hint {
max-width: 235px;
padding: 8px 10px;
border: 1px solid rgba(82, 105, 132, 0.25);
border-radius: 4px;
background: rgba(7, 13, 24, 0.65);
color: #8197ad;
font-size: 10px;
line-height: 1.45;
text-align: right;
backdrop-filter: blur(8px);
}
#labels {
position: absolute;
inset: 0;
z-index: 3;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
min-width: 64px;
padding: 5px 7px;
border: 1px solid #35475d;
border-radius: 4px;
background: rgba(8, 14, 25, 0.84);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.28);
color: #dceaff;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-align: center;
white-space: nowrap;
will-change: transform;
}
.world-label span {
display: block;
margin-top: 2px;
color: var(--green);
font-size: 7px;
letter-spacing: 0.1em;
}
.world-label.open {
border-color: rgba(255, 100, 124, 0.62);
color: #ffd4dc;
}
.world-label.open span {
color: var(--red);
}
.world-label.lamp-label {
border-color: rgba(255, 196, 92, 0.5);
}
.world-label.source-label {
border-color: rgba(124, 238, 255, 0.42);
}
aside {
min-height: 0;
overflow-y: auto;
padding: 16px;
border-left: 1px solid var(--line);
background: #0d1523;
scrollbar-width: thin;
scrollbar-color: #33465c transparent;
}
.panel {
margin-bottom: 13px;
padding: 15px;
border: 1px solid var(--line);
border-radius: 7px;
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.018), transparent 45%),
var(--panel);
}
.eyebrow {
color: var(--cyan);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.readout {
display: flex;
align-items: baseline;
gap: 8px;
margin: 7px 0 9px;
}
#currentValue {
font-size: 42px;
font-weight: 750;
line-height: 1;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.readout .unit {
color: var(--muted);
font-size: 15px;
font-weight: 700;
}
.meter {
height: 6px;
overflow: hidden;
border-radius: 99px;
background: #1c293a;
}
#meterFill {
display: block;
width: 100%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #32bcd1, var(--cyan));
box-shadow: 0 0 14px rgba(124, 238, 255, 0.5);
transition: width 250ms ease;
}
#flowMessage {
min-height: 42px;
margin: 11px 0 0;
color: #9cafc3;
font-size: 12px;
line-height: 1.55;
}
.panel h2 {
margin: 3px 0 7px;
font-size: 15px;
letter-spacing: 0.015em;
}
.panel-copy {
margin: 0 0 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
#connectionList {
display: grid;
gap: 7px;
}
.connection {
width: 100%;
display: grid;
grid-template-columns: 34px 1fr auto;
align-items: center;
gap: 10px;
padding: 10px;
border: 1px solid #26374b;
border-radius: 5px;
color: var(--text);
background: #121d2d;
cursor: pointer;
text-align: left;
transition:
border-color 140ms ease,
transform 140ms ease,
background 140ms ease;
}
.connection:hover {
transform: translateX(-2px);
border-color: #49637d;
background: #172437;
}
.connection.open {
border-color: rgba(255, 100, 124, 0.5);
background: rgba(255, 100, 124, 0.065);
}
.connection .switch-id {
color: var(--cyan);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.07em;
}
.connection.open .switch-id {
color: var(--red);
}
.connection .connection-name {
color: #d7e4f3;
font-size: 12px;
font-weight: 650;
}
.connection .state {
padding: 4px 6px;
border-radius: 3px;
color: var(--green);
background: rgba(103, 240, 165, 0.09);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.1em;
}
.connection.open .state {
color: var(--red);
background: rgba(255, 100, 124, 0.1);
}
.action-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 11px;
}
.action-button {
padding: 9px;
border: 1px solid #31445b;
border-radius: 4px;
color: #b8cbe0;
background: #142033;
cursor: pointer;
font-size: 10px;
font-weight: 800;
transition: 140ms ease;
}
.action-button:hover {
color: white;
border-color: var(--cyan);
background: #182a40;
}
.series-path {
display: flex;
align-items: center;
justify-content: space-between;
gap: 3px;
margin: 13px 0 10px;
}
.series-node {
min-width: 37px;
padding: 7px 5px;
border: 1px solid rgba(103, 240, 165, 0.3);
border-radius: 4px;
color: var(--green);
background: rgba(103, 240, 165, 0.07);
font-size: 8px;
font-weight: 900;
text-align: center;
transition: 180ms ease;
}
.series-node.off {
color: #72859a;
border-color: #2a394d;
background: #111c2b;
}
.series-line {
flex: 1;
height: 1px;
background: rgba(103, 240, 165, 0.4);
transition: background 180ms ease;
}
.series-line.off {
background: #2a394d;
}
#pathNote {
margin: 0;
color: #8499ae;
font-size: 11px;
line-height: 1.5;
}
.charge-ledger {
display: flex;
justify-content: space-between;
gap: 12px;
padding: 9px 10px;
margin-bottom: 11px;
border: 1px solid rgba(124, 238, 255, 0.15);
border-radius: 4px;
background: rgba(124, 238, 255, 0.045);
font-size: 10px;
}
.charge-ledger span {
color: var(--muted);
}
.charge-ledger strong {
color: var(--cyan);
white-space: nowrap;
}
.charge-note {
margin: 0;
color: #91a6bc;
font-size: 11px;
line-height: 1.55;
}
.charge-note strong {
color: #d7e8f8;
}
@media (max-width: 880px) {
html,
body {
height: auto;
min-height: 100%;
overflow: auto;
}
#app {
min-height: 100vh;
height: auto;
}
header {
align-items: flex-start;
padding: 12px 14px;
}
.brand p {
display: none;
}
.global-status {
padding: 8px 9px;
font-size: 8px;
}
main {
display: block;
}
#scenePanel {
height: 66vh;
min-height: 480px;
}
aside {
overflow: visible;
border-left: 0;
border-top: 1px solid var(--line);
}
.gesture-hint {
display: none;
}
}
@media (max-width: 500px) {
.brand-mark {
width: 32px;
height: 32px;
}
.brand h1 {
font-size: 14px;
}
.global-status {
max-width: 125px;
text-align: left;
line-height: 1.35;
}
.scene-top {
top: 12px;
left: 12px;
}
.view-actions {
top: 12px;
right: 12px;
}
.scene-bottom {
left: 12px;
right: 12px;
bottom: 12px;
}
.legend span {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i>
<i></i><i></i><i></i>
<i></i><i></i><i></i>
</div>
<div>
<h1>Circuit Break Explorer</h1>
<p>Open a connection. Watch the entire series path stop.</p>
</div>
</div>
<div id="globalStatus" class="global-status closed" aria-live="polite">
Closed loop · current flows
</div>
</header>
<main>
<section id="scenePanel" aria-label="Interactive voxel circuit">
<div id="canvasHost"></div>
<div id="labels"></div>
<div class="scene-top">
<div class="scene-chip">
<strong>Voxel series loop</strong> · four break points
</div>
</div>
<div class="view-actions">
<button id="resetView" class="small-button" type="button">
Reset view
</button>
</div>
<div class="scene-bottom">
<div class="legend">
<span><i></i>Moving charge / current</span>
<span><i class="lamp-key"></i>Lamp energy</span>
<span><i class="break-key"></i>Open connection</span>
</div>
<div class="gesture-hint">
Drag to orbit · scroll to zoom<br>
Click a switch block to open it
</div>
</div>
</section>
<aside>
<section class="panel">
<div class="eyebrow">Live measurement</div>
<div class="readout">
<span id="currentValue">1.00</span>
<span class="unit">A</span>
</div>
<div class="meter"><i id="meterFill"></i></div>
<p id="flowMessage" aria-live="polite"></p>
</section>
<section class="panel">
<div class="eyebrow">Interactive connections</div>
<h2>Open a break point</h2>
<p class="panel-copy">
Select a connection below or click its voxel switch in the scene.
Multiple opens are supported.
</p>
<div id="connectionList"></div>
<div class="action-row">
<button id="closeAll" class="action-button" type="button">
CLOSE ALL
</button>
<button id="breakOne" class="action-button" type="button">
BREAK ONE
</button>
</div>
</section>
<section class="panel">
<div class="eyebrow">Series-path check</div>
<h2>Same current everywhere</h2>
<div class="series-path" aria-label="Components in series">
<span class="series-node">SOURCE</span>
<i class="series-line"></i>
<span class="series-node">S1</span>
<i class="series-line"></i>
<span class="series-node">LAMP</span>
<i class="series-line"></i>
<span class="series-node">S2</span>
<i class="series-line"></i>
<span class="series-node">S3</span>
<i class="series-line"></i>
<span class="series-node">S4</span>
</div>
<p id="pathNote"></p>
</section>
<section class="panel">
<div class="eyebrow">Charge accounting</div>
<h2>The lamp is not a charge sink</h2>
<div class="charge-ledger">
<span>Charge markers in the loop</span>
<strong id="chargeCount">36 / 36</strong>
</div>
<p class="charge-note">
<strong>Cyan markers keep circulating through the lamp.</strong>
The lamp converts electrical energy into light and heat; it does
not consume charge. Motion of the markers represents current.
</p>
</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";
const host = document.getElementById("canvasHost");
const labelHost = document.getElementById("labels");
const connectionList = document.getElementById("connectionList");
const globalStatus = document.getElementById("globalStatus");
const currentValue = document.getElementById("currentValue");
const meterFill = document.getElementById("meterFill");
const flowMessage = document.getElementById("flowMessage");
const pathNote = document.getElementById("pathNote");
const chargeCount = document.getElementById("chargeCount");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080d18);
scene.fog = new THREE.Fog(0x080d18, 23, 42);
const camera = new THREE.PerspectiveCamera(40, 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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional voxel circuit"
);
host.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0x9bdfff, 0x16101e, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xc9edff, 3.1);
keyLight.position.set(7, 14, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -11;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x537dff, 1.25);
rimLight.position.set(-10, 7, -9);
scene.add(rimLight);
const boardBaseMaterial = new THREE.MeshStandardMaterial({
color: 0x101b2a,
roughness: 0.92,
metalness: 0.08
});
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0x1a2a3b,
roughness: 0.88,
metalness: 0.05
});
const wireMaterial = new THREE.MeshStandardMaterial({
color: 0x1b7184,
emissive: 0x073b48,
emissiveIntensity: 0.08,
roughness: 0.34,
metalness: 0.48
});
const terminalMaterial = new THREE.MeshStandardMaterial({
color: 0xb9c8d5,
emissive: 0x152633,
emissiveIntensity: 0.14,
roughness: 0.3,
metalness: 0.68
});
const leverMaterial = new THREE.MeshStandardMaterial({
color: 0xffb54a,
emissive: 0x7b3600,
emissiveIntensity: 0.24,
roughness: 0.38,
metalness: 0.28
});
const darkComponentMaterial = new THREE.MeshStandardMaterial({
color: 0x25374a,
roughness: 0.45,
metalness: 0.62
});
const cageMaterial = new THREE.MeshStandardMaterial({
color: 0xe8d6a8,
emissive: 0xffb541,
emissiveIntensity: 0.16,
roughness: 0.32,
metalness: 0.2
});
const lampCoreMaterial = new THREE.MeshStandardMaterial({
color: 0xffbd54,
emissive: 0xff8a16,
emissiveIntensity: 2.8,
roughness: 0.2,
metalness: 0.05
});
const redMaterial = new THREE.MeshStandardMaterial({
color: 0xff526c,
emissive: 0x7a1024,
emissiveIntensity: 0.32,
roughness: 0.4,
metalness: 0.22
});
const blueMaterial = new THREE.MeshStandardMaterial({
color: 0x438eff,
emissive: 0x123f91,
emissiveIntensity: 0.35,
roughness: 0.4,
metalness: 0.22
});
const currentMaterial = new THREE.MeshBasicMaterial({
color: 0x8af6ff,
transparent: true,
opacity: 0.96,
depthWrite: false
});
const breakMaterial = new THREE.MeshBasicMaterial({
color: 0xff405f,
transparent: true,
opacity: 0.95,
depthWrite: false
});
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
function addBlock(parent, x, y, z, sx, sy, sz, material) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.position.set(x, y, z);
mesh.scale.set(sx, sy, sz);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Raised voxel-tile ground.
const boardBase = new THREE.Mesh(unitBox, boardBaseMaterial);
boardBase.position.set(0, -0.24, 0);
boardBase.scale.set(16.6, 0.35, 12.1);
boardBase.receiveShadow = true;
scene.add(boardBase);
const tileMatrices = [];
const tileColors = [];
const tileColorA = new THREE.Color(0x172636);
const tileColorB = new THREE.Color(0x1d3041);
for (let x = -7; x <= 7; x++) {
for (let z = -5; z <= 5; z++) {
dummy.position.set(x * 1.05, -0.025, z * 1.05);
dummy.scale.set(0.97, 0.09, 0.97);
dummy.updateMatrix();
tileMatrices.push(dummy.matrix.clone());
tileColors.push(
(x + z) % 2 === 0 ? tileColorA : tileColorB
);
}
}
const tiles = new THREE.InstancedMesh(
unitBox,
tileMaterial,
tileMatrices.length
);
tileMatrices.forEach((matrix, index) => {
tiles.setMatrixAt(index, matrix);
tiles.setColorAt(index, tileColors[index]);
});
tiles.receiveShadow = true;
scene.add(tiles);
// Conductors are assembled from visible voxel blocks.
const wireMatrices = [];
function addWireSegment(x1, z1, x2, z2) {
const dx = x2 - x1;
const dz = z2 - z1;
const length = Math.hypot(dx, dz);
const count = Math.max(1, Math.ceil(length / 0.45));
const step = length / count;
const horizontal = Math.abs(dx) > Math.abs(dz);
for (let i = 0; i < count; i++) {
const t = (i + 0.5) / count;
const x = x1 + dx * t;
const z = z1 + dz * t;
dummy.position.set(x, 0.29, z);
dummy.scale.set(
horizontal ? step * 0.97 : 0.43,
0.28,
horizontal ? 0.43 : step * 0.97
);
dummy.updateMatrix();
wireMatrices.push(dummy.matrix.clone());
}
}
// Top edge, with gaps occupied by switches and the lamp.
addWireSegment(-6, -4, -4, -4);
addWireSegment(-2, -4, -1, -4);
addWireSegment(1, -4, 3, -4);
addWireSegment(5, -4, 6, -4);
// Right edge.
addWireSegment(6, -4, 6, 0);
addWireSegment(6, 2, 6, 4);
// Bottom edge.
addWireSegment(6, 4, -1, 4);
addWireSegment(-3, 4, -6, 4);
// Left edge, with a gap for the source.
addWireSegment(-6, 4, -6, 1.45);
addWireSegment(-6, -1.45, -6, -4);
const wires = new THREE.InstancedMesh(
unitBox,
wireMaterial,
wireMatrices.length
);
wireMatrices.forEach((matrix, index) => {
wires.setMatrixAt(index, matrix);
});
wires.castShadow = true;
wires.receiveShadow = true;
scene.add(wires);
const switches = [];
const switchGroups = [];
const worldLabels = [];
function makeWorldLabel(text, position, extraClass = "") {
const element = document.createElement("div");
element.className = `world-label ${extraClass}`;
element.innerHTML = text;
labelHost.appendChild(element);
const item = {
element,
position: position.clone()
};
worldLabels.push(item);
return item;
}
function createSwitch(id, name, x, z, rotationY) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotationY;
group.userData.switchIndex = switches.length;
scene.add(group);
switchGroups.push(group);
addBlock(group, -0.92, 0.47, 0, 0.45, 0.48, 0.64, terminalMaterial);
addBlock(group, 0.92, 0.47, 0, 0.45, 0.48, 0.64, terminalMaterial);
const bridge = addBlock(
group,
0,
0.39,
0,
1.72,
0.18,
0.34,
wireMaterial
);
const lever = new THREE.Group();
lever.position.set(-0.86, 0.72, 0);
group.add(lever);
addBlock(
lever,
0.86,
0,
0,
1.72,
0.18,
0.37,
leverMaterial
);
const statusMaterial = new THREE.MeshStandardMaterial({
color: 0x67f0a5,
emissive: 0x1b8b52,
emissiveIntensity: 1.2,
roughness: 0.35,
metalness: 0.1
});
addBlock(
group,
-0.92,
0.77,
0.24,
0.17,
0.12,
0.17,
statusMaterial
);
const gap = new THREE.Group();
gap.position.set(0.05, 0.82, 0);
gap.visible = false;
group.add(gap);
addBlock(gap, -0.38, 0, 0, 0.16, 0.16, 0.16, breakMaterial);
addBlock(gap, 0.38, 0, 0, 0.16, 0.16, 0.16, breakMaterial);
addBlock(gap, 0, 0.18, 0, 0.16, 0.16, 0.16, breakMaterial);
const label = makeWorldLabel(
`<b>${id}</b><span>CLOSED</span>`,
new THREE.Vector3(x, 1.62, z)
);
const item = {
id,
name,
open: false,
group,
lever,
bridge,
gap,
statusMaterial,
label,
leverTarget: 0
};
switches.push(item);
return item;
}
createSwitch("S1", "Top-left switch", -3, -4, 0);
createSwitch("S2", "Top-right switch", 4, -4, 0);
createSwitch("S3", "Right-side switch", 6, 1, Math.PI / 2);
createSwitch("S4", "Bottom switch", -2, 4, 0);
// Voxel lamp. The current route continues underneath its cage.
const lamp = new THREE.Group();
lamp.position.set(0, 0, -4);
scene.add(lamp);
addBlock(lamp, -0.91, 0.44, 0, 0.44, 0.48, 0.65, terminalMaterial);
addBlock(lamp, 0.91, 0.44, 0, 0.44, 0.48, 0.65, terminalMaterial);
addBlock(lamp, 0, 0.28, 0, 1.65, 0.22, 0.65, darkComponentMaterial);
addBlock(lamp, 0, 0.74, 0, 0.22, 0.72, 0.22, darkComponentMaterial);
const bulbCage = new THREE.Group();
bulbCage.position.y = 0.03;
lamp.add(bulbCage);
const cagePositions = [
[-0.42, 1.08, -0.42],
[0.42, 1.08, -0.42],
[-0.42, 1.08, 0.42],
[0.42, 1.08, 0.42],
[-0.42, 0.76, 0],
[0.42, 0.76, 0],
[0, 0.76, -0.42],
[0, 0.76, 0.42],
[-0.42, 1.40, 0],
[0.42, 1.40, 0],
[0, 1.40, -0.42],
[0, 1.40, 0.42]
];
cagePositions.forEach(([x, y, z]) => {
addBlock(bulbCage, x, y, z, 0.25, 0.25, 0.25, cageMaterial);
});
const lampCore = addBlock(
bulbCage,
0,
1.08,
0,
0.43,
0.43,
0.43,
lampCoreMaterial
);
const lampLight = new THREE.PointLight(0xffa53b, 3.2, 5.2, 2);
lampLight.position.set(0, 1.12, 0);
lamp.add(lampLight);
makeWorldLabel(
"<b>LAMP</b><span>ENERGY → LIGHT + HEAT</span>",
new THREE.Vector3(0, 2.15, -4),
"lamp-label"
);
// Voxel battery/source on the left side of the loop.
const source = new THREE.Group();
source.position.set(-6, 0, 0);
source.rotation.y = Math.PI / 2;
scene.add(source);
addBlock(source, 0, 0.37, 0, 1.85, 0.65, 1.05, darkComponentMaterial);
for (let row = -1; row <= 1; row++) {
for (let column = -1; column <= 1; column++) {
addBlock(
source,
-0.47 + row * 0.47,
0.76,
column * 0.32,
0.38,
0.34,
0.25,
row < 0 ? redMaterial : blueMaterial
);
}
}
addBlock(source, -1.02, 0.49, 0, 0.3, 0.42, 0.64, redMaterial);
addBlock(source, 1.02, 0.49, 0, 0.3, 0.42, 0.64, blueMaterial);
makeWorldLabel(
"<b>SOURCE</b><span>POSITIVE → NEGATIVE</span>",
new THREE.Vector3(-6, 2.0, 0),
"source-label"
);
// Current follows a continuous closed route through every component.
const route = [
new THREE.Vector3(-6, 0.78, -4),
new THREE.Vector3(-6, 0.78, 4),
new THREE.Vector3(6, 0.78, 4),
new THREE.Vector3(6, 0.78, -4),
new THREE.Vector3(-6, 0.78, -4)
];
const segmentLengths = [];
let routeLength = 0;
for (let i = 0; i < route.length - 1; i++) {
const length = route[i].distanceTo(route[i + 1]);
segmentLengths.push(length);
routeLength += length;
}
function sampleRoute(distance, target) {
let remaining =
((distance % routeLength) + routeLength) % routeLength;
for (let i = 0; i < segmentLengths.length; i++) {
if (remaining <= segmentLengths[i]) {
const t = remaining / segmentLengths[i];
target.lerpVectors(route[i], route[i + 1], t);
return target;
}
remaining -= segmentLengths[i];
}
target.copy(route[route.length - 1]);
return target;
}
const markerCount = 36;
const markerGeometry = new THREE.BoxGeometry(0.18, 0.18, 0.18);
const markers = [];
for (let i = 0; i < markerCount; i++) {
const marker = new THREE.Mesh(markerGeometry, currentMaterial);
marker.castShadow = false;
scene.add(marker);
markers.push(marker);
}
chargeCount.textContent = `${markerCount} / ${markerCount}`;
// Static block arrows show the direction of conventional current.
function addFlowArrow(x, z, rotationY) {
const arrow = new THREE.Group();
arrow.position.set(x, 0.82, z);
arrow.rotation.y = rotationY;
scene.add(arrow);
addBlock(arrow, -0.06, 0, 0, 0.52, 0.13, 0.13, currentMaterial);
addBlock(arrow, 0.22, 0, 0.17, 0.27, 0.13, 0.13, currentMaterial);
addBlock(arrow, 0.22, 0, -0.17, 0.27, 0.13, 0.13, currentMaterial);
}
addFlowArrow(0, 4, 0);
addFlowArrow(6, 0, -Math.PI / 2);
addFlowArrow(0, -4, Math.PI);
addFlowArrow(-6, 0, Math.PI / 2);
// Sidebar connection controls.
switches.forEach((item, index) => {
const button = document.createElement("button");
button.type = "button";
button.className = "connection";
button.innerHTML = `
<span class="switch-id">${item.id}</span>
<span class="connection-name">${item.name}</span>
<span class="state">CLOSED</span>
`;
button.addEventListener("click", () => {
setSwitchOpen(index, !item.open);
});
item.button = button;
connectionList.appendChild(button);
});
let circuitFlowing = true;
function setSwitchOpen(index, open) {
const item = switches[index];
item.open = open;
item.leverTarget = open ? -1.04 : 0;
item.bridge.visible = !open;
item.gap.visible = open;
item.statusMaterial.color.setHex(open ? 0xff526c : 0x67f0a5);
item.statusMaterial.emissive.setHex(open ? 0x8d1028 : 0x1b8b52);
updateInterface();
}
function updateInterface() {
const openSwitches = switches.filter((item) => item.open);
circuitFlowing = openSwitches.length === 0;
switches.forEach((item) => {
item.button.classList.toggle("open", item.open);
item.button.querySelector(".state").textContent = item.open
? "OPEN"
: "CLOSED";
item.label.element.classList.toggle("open", item.open);
item.label.element.innerHTML = `
<b>${item.id}</b>
<span>${item.open ? "OPEN · I = 0 A" : "CLOSED"}</span>
`;
});
document.querySelectorAll(".series-node").forEach((node) => {
node.classList.toggle("off", !circuitFlowing);
});
document.querySelectorAll(".series-line").forEach((line) => {
line.classList.toggle("off", !circuitFlowing);
});
if (circuitFlowing) {
globalStatus.className = "global-status closed";
globalStatus.textContent = "Closed loop · current flows";
currentValue.textContent = "1.00";
meterFill.style.width = "100%";
flowMessage.textContent =
"All four connections are closed. Charge circulates through the lamp and returns to the source.";
pathNote.textContent =
"Every series segment carries the same current: I = 1.00 A.";
} else {
const names = openSwitches.map((item) => item.id).join(", ");
globalStatus.className = "global-status open";
globalStatus.textContent = "Open loop · current stopped";
currentValue.textContent = "0.00";
meterFill.style.width = "0%";
flowMessage.textContent =
`Open at ${names}. Because every component is in series, current is 0 A through the lamp and every other segment.`;
pathNote.textContent =
`Open at ${names}: I = 0 A at the source, all switches, the lamp, and every wire.`;
}
}
document.getElementById("closeAll").addEventListener("click", () => {
switches.forEach((item, index) => setSwitchOpen(index, false));
});
document.getElementById("breakOne").addEventListener("click", () => {
const closed = switches.find((item) => !item.open);
if (closed) {
setSwitchOpen(switches.indexOf(closed), true);
}
});
updateInterface();
// Custom orbit and zoom controls; no Three.js examples/addons are used.
const orbit = {
targetTheta: 0.66,
targetPhi: 0.92,
targetRadius: 20,
theta: 0.66,
phi: 0.92,
radius: 20,
center: new THREE.Vector3(0, 0.35, 0)
};
function resetView() {
orbit.targetTheta = 0.66;
orbit.targetPhi = 0.92;
orbit.targetRadius = 20;
}
document.getElementById("resetView").addEventListener("click", resetView);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let moved = 0;
let lastX = 0;
let lastY = 0;
function findSwitchIndex(object) {
let current = object;
while (current) {
if (typeof current.userData.switchIndex === "number") {
return current.userData.switchIndex;
}
current = current.parent;
}
return -1;
}
function pickSwitch(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(switchGroups, true);
for (const hit of hits) {
const index = findSwitchIndex(hit.object);
if (index !== -1) return index;
}
return -1;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
moved = 0;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
moved += Math.abs(dx) + Math.abs(dy);
lastX = event.clientX;
lastY = event.clientY;
orbit.targetTheta -= dx * 0.006;
orbit.targetPhi = THREE.MathUtils.clamp(
orbit.targetPhi - dy * 0.006,
0.35,
1.42
);
renderer.domElement.style.cursor = "grabbing";
} else {
const index = pickSwitch(event.clientX, event.clientY);
renderer.domElement.style.cursor = index === -1 ? "grab" : "pointer";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
pointerDown = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (moved < 6) {
const index = pickSwitch(event.clientX, event.clientY);
if (index !== -1) {
setSwitchOpen(index, !switches[index].open);
}
}
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius + event.deltaY * 0.012,
11,
31
);
},
{ passive: false }
);
renderer.domElement.style.cursor = "grab";
function resize() {
const width = Math.max(1, host.clientWidth);
const height = Math.max(1, host.clientHeight);
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(host);
resize();
const clock = new THREE.Clock();
let routeDistance = 0;
const markerPosition = new THREE.Vector3();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
orbit.theta += (orbit.targetTheta - orbit.theta) * (1 - Math.exp(-10 * dt));
orbit.phi += (orbit.targetPhi - orbit.phi) * (1 - Math.exp(-10 * dt));
orbit.radius += (orbit.targetRadius - orbit.radius) * (1 - Math.exp(-10 * dt));
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.center.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.center.y + orbit.radius * Math.cos(orbit.phi),
orbit.center.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.center);
switches.forEach((item) => {
item.lever.rotation.z +=
(item.leverTarget - item.lever.rotation.z) *
(1 - Math.exp(-12 * dt));
if (item.open) {
const pulse = 1 + Math.sin(time * 6) * 0.12;
item.gap.scale.setScalar(pulse);
}
});
if (circuitFlowing) {
routeDistance = (routeDistance + dt * 2.65) % routeLength;
}
markers.forEach((marker, index) => {
sampleRoute(
routeDistance + (index * routeLength) / markerCount,
markerPosition
);
marker.position.copy(markerPosition);
if (circuitFlowing) {
marker.rotation.y += dt * 0.9;
marker.rotation.x += dt * 0.45;
}
});
if (circuitFlowing) {
currentMaterial.color.setHex(0x8af6ff);
currentMaterial.opacity = 0.96;
wireMaterial.emissive.setHex(0x074856);
wireMaterial.emissiveIntensity = 0.44 + Math.sin(time * 4) * 0.13;
lampCoreMaterial.emissiveIntensity = 2.6 + Math.sin(time * 5) * 0.28;
cageMaterial.emissiveIntensity = 0.3;
lampLight.intensity = 3.2;
} else {
currentMaterial.color.setHex(0x507080);
currentMaterial.opacity = 0.32;
wireMaterial.emissive.setHex(0x071820);
wireMaterial.emissiveIntensity = 0.035;
lampCoreMaterial.emissiveIntensity = 0.025;
cageMaterial.emissiveIntensity = 0.025;
lampLight.intensity = 0;
}
worldLabels.forEach((label) => {
const projected = label.position.clone().project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
projected.x < -1.2 ||
projected.x > 1.2 ||
projected.y < -1.2 ||
projected.y > 1.2
) {
label.element.style.display = "none";
return;
}
label.element.style.display = "block";
const x = (projected.x * 0.5 + 0.5) * host.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * host.clientHeight;
label.element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```