Prompt specification
Build or customize the Three.js 3D scene "Diode-model comparison 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: Diode-model comparison explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/diode-model-comparison-explorer-x1620
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Let users compare ideal and simple threshold diode approximations on the same input; reveal their differing output curves and note that actual devices have richer behavior.
Working Three.js Starter (diode-model-comparison-explorer-x1620):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Diode Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #080d18;
--panel: #101725;
--panel-2: #151e2f;
--border: #273249;
--text: #edf2ff;
--muted: #8e9ab5;
--purple: #a68cff;
--cyan: #4de0d2;
--orange: #ffb454;
--pink: #ff6f91;
--radius: 16px;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--purple);
outline-offset: 3px;
}
.app {
min-height: 100vh;
display: grid;
grid-template-rows: auto 1fr auto;
}
.topbar {
min-height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 15px 24px;
border-bottom: 1px solid var(--border);
background: rgba(10, 15, 27, 0.92);
backdrop-filter: blur(18px);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
width: 38px;
height: 38px;
position: relative;
border: 2px solid var(--purple);
border-radius: 10px;
transform: rotate(-8deg);
box-shadow: 0 0 24px rgba(166, 140, 255, 0.22);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--purple);
}
.brand-mark::before {
width: 20px;
height: 3px;
left: 7px;
top: 10px;
box-shadow: 0 12px 0 var(--purple);
}
.brand-mark::after {
width: 3px;
height: 21px;
right: 7px;
top: 7px;
background: var(--cyan);
}
.brand h1 {
margin: 0;
font-size: 17px;
letter-spacing: 0.02em;
}
.brand p {
margin: 3px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.top-status {
display: flex;
align-items: center;
gap: 12px;
}
.live-pill,
.same-source {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(255, 255, 255, 0.025);
color: #bdc8df;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
white-space: nowrap;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
animation: pulse 1.7s ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.45;
transform: scale(0.78);
}
}
.workspace {
min-height: 0;
display: grid;
grid-template-columns: 300px minmax(0, 1fr);
}
.sidebar {
padding: 20px;
overflow-y: auto;
border-right: 1px solid var(--border);
background:
linear-gradient(180deg, rgba(21, 30, 47, 0.82), rgba(13, 19, 32, 0.94)),
var(--panel);
}
.section {
margin-bottom: 22px;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 12px;
color: #aab5cc;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.section-heading span:last-child {
color: #59667f;
font-weight: 700;
}
.segmented {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
padding: 4px;
border: 1px solid var(--border);
border-radius: 11px;
background: #0b111e;
}
.segmented button {
min-height: 32px;
border: 0;
border-radius: 7px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition: 160ms ease;
}
.segmented button:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.045);
}
.segmented button.active {
color: #fff;
background: #2b3150;
box-shadow: inset 0 0 0 1px #444b70;
}
.control {
margin-top: 17px;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 14px;
margin-bottom: 9px;
color: #c9d1e2;
font-size: 12px;
}
.control-label output {
color: var(--text);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 12px;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
appearance: none;
border-radius: 999px;
background: linear-gradient(
90deg,
var(--purple) 0%,
var(--purple) var(--fill, 50%),
#283248 var(--fill, 50%),
#283248 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid #111827;
border-radius: 50%;
background: var(--purple);
box-shadow: 0 0 0 1px #66588e, 0 0 14px rgba(166, 140, 255, 0.35);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #111827;
border-radius: 50%;
background: var(--purple);
box-shadow: 0 0 0 1px #66588e;
}
.range-scale {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: #59667f;
font-size: 9px;
}
.model-card {
margin-top: 9px;
padding: 13px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(255, 255, 255, 0.022);
}
.model-card.ideal {
border-left: 3px solid var(--cyan);
}
.model-card.threshold {
border-left: 3px solid var(--orange);
}
.model-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
font-size: 12px;
font-weight: 800;
}
.model-title span:last-child {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.equation {
margin-top: 9px;
color: #bdc8dc;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
line-height: 1.6;
}
.sidebar-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.button {
min-height: 38px;
border: 1px solid var(--border);
border-radius: 10px;
background: #1a2437;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: 160ms ease;
}
.button:hover {
transform: translateY(-1px);
border-color: #465472;
background: #222d43;
}
.button.primary {
border-color: #685b99;
background: #49406f;
}
.visual {
min-width: 0;
min-height: 0;
display: grid;
grid-template-rows: minmax(350px, 1fr) 300px;
}
.scene-wrap {
position: relative;
min-height: 350px;
overflow: hidden;
background:
radial-gradient(circle at 50% 35%, rgba(80, 65, 145, 0.17), transparent 42%),
linear-gradient(180deg, #111827, #0a101c);
}
#threeHost {
position: absolute;
inset: 0;
}
#threeHost canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.scene-overlay {
position: absolute;
z-index: 2;
pointer-events: none;
}
.scene-title {
top: 20px;
left: 22px;
}
.scene-title .eyebrow {
color: var(--purple);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.scene-title h2 {
margin: 7px 0 4px;
font-size: clamp(20px, 2.4vw, 31px);
letter-spacing: -0.035em;
}
.scene-title p {
margin: 0;
color: var(--muted);
font-size: 12px;
}
.scene-tools {
top: 18px;
right: 18px;
display: flex;
gap: 7px;
pointer-events: auto;
}
.icon-button {
width: 36px;
height: 36px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(12, 18, 31, 0.78);
color: #b9c4dc;
cursor: pointer;
backdrop-filter: blur(10px);
}
.icon-button:hover {
color: #fff;
border-color: #53607d;
}
.scene-legend {
left: 22px;
bottom: 20px;
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.legend-chip {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid rgba(75, 89, 120, 0.72);
border-radius: 8px;
background: rgba(9, 15, 27, 0.78);
color: #aab6ce;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.legend-chip i {
width: 8px;
height: 8px;
border-radius: 2px;
box-shadow: 0 0 9px currentColor;
}
.scene-help {
right: 20px;
bottom: 21px;
color: #68758f;
font-size: 10px;
letter-spacing: 0.04em;
}
.load-error {
position: absolute;
inset: 0;
z-index: 5;
display: none;
place-items: center;
padding: 30px;
color: var(--muted);
text-align: center;
}
.analysis {
min-width: 0;
padding: 16px 20px 18px;
border-top: 1px solid var(--border);
background: #0d1422;
}
.analysis-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
margin-bottom: 8px;
}
.analysis-title {
display: flex;
align-items: center;
gap: 10px;
}
.analysis-title strong {
font-size: 12px;
letter-spacing: 0.04em;
}
.analysis-title span {
color: var(--muted);
font-size: 10px;
}
.chart-legend {
display: flex;
gap: 13px;
color: #9da9c0;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
}
.chart-legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.chart-legend i {
width: 14px;
height: 2px;
background: currentColor;
}
.chart-legend .input-key {
color: var(--purple);
}
.chart-legend .ideal-key {
color: var(--cyan);
}
.chart-legend .threshold-key {
color: var(--orange);
}
.analysis-body {
display: grid;
grid-template-columns: minmax(0, 1fr) 175px;
gap: 18px;
min-height: 0;
}
.chart-shell {
position: relative;
min-width: 0;
min-height: 180px;
overflow: hidden;
border: 1px solid #202b40;
border-radius: 12px;
background:
linear-gradient(rgba(255, 255, 255, 0.016) 1px, transparent 1px),
linear-gradient(90deg, rgba(255, 255, 255, 0.016) 1px, transparent 1px),
#0a111e;
background-size: 26px 26px;
}
#chart {
display: block;
width: 100%;
height: 100%;
min-height: 180px;
cursor: crosshair;
}
.chart-readout {
position: absolute;
left: 10px;
bottom: 7px;
padding: 4px 6px;
border-radius: 5px;
background: rgba(7, 12, 22, 0.83);
color: #8290aa;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
pointer-events: none;
}
.stats {
display: grid;
grid-template-rows: repeat(3, 1fr);
gap: 8px;
}
.stat {
padding: 10px 11px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(255, 255, 255, 0.022);
}
.stat span {
display: block;
margin-bottom: 5px;
color: #68758f;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.stat strong {
display: block;
font-size: 13px;
}
.stat strong.cyan {
color: var(--cyan);
}
.stat strong.orange {
color: var(--orange);
}
.reality {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 18px;
min-height: 68px;
padding: 12px 24px;
border-top: 1px solid var(--border);
background: #0b111e;
}
.reality-badge {
padding: 7px 9px;
border: 1px solid rgba(255, 111, 145, 0.32);
border-radius: 7px;
background: rgba(255, 111, 145, 0.07);
color: var(--pink);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
}
.reality-copy {
color: #98a5bd;
font-size: 11px;
line-height: 1.55;
}
.reality-copy strong {
color: #dce4f4;
}
.reality-tags {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 6px;
}
.reality-tags span {
padding: 5px 7px;
border: 1px solid #263149;
border-radius: 6px;
color: #7f8ca5;
font-size: 8px;
font-weight: 700;
white-space: nowrap;
}
@media (max-width: 980px) {
.workspace {
grid-template-columns: 260px minmax(0, 1fr);
}
.analysis-body {
grid-template-columns: 1fr;
}
.stats {
display: none;
}
.reality {
grid-template-columns: auto 1fr;
}
.reality-tags {
display: none;
}
}
@media (max-width: 760px) {
.app {
display: block;
}
.topbar {
padding: 13px 16px;
}
.same-source {
display: none;
}
.workspace {
display: flex;
flex-direction: column;
}
.visual {
order: 0;
grid-template-rows: 440px 285px;
}
.sidebar {
order: 1;
border-top: 1px solid var(--border);
border-right: 0;
}
.scene-help {
display: none;
}
.analysis {
padding: 13px;
}
.analysis-title span,
.chart-legend {
display: none;
}
.reality {
padding: 14px 16px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>Voxel Diode Lab</h1>
<p>Two approximations · one source signal</p>
</div>
</div>
<div class="top-status">
<div class="live-pill">
<span class="live-dot"></span>
<span id="runStatus">Running</span>
</div>
<div class="same-source">Same input / same phase</div>
</div>
</header>
<main class="workspace">
<aside class="sidebar">
<section class="section">
<h2 class="section-heading">
<span>Source signal</span>
<span>01</span>
</h2>
<div class="segmented" aria-label="Input waveform">
<button class="active" data-wave="sine" aria-pressed="true">Sine</button>
<button data-wave="triangle" aria-pressed="false">Triangle</button>
<button data-wave="square" aria-pressed="false">Square</button>
</div>
<div class="control">
<label class="control-label" for="amplitude">
<span>Amplitude</span>
<output id="amplitudeValue">2.40 V</output>
</label>
<input id="amplitude" type="range" min="0.2" max="2.5" step="0.05" value="2.4" />
<div class="range-scale"><span>0.2 V</span><span>2.5 V</span></div>
</div>
<div class="control">
<label class="control-label" for="bias">
<span>DC bias</span>
<output id="biasValue">+0.00 V</output>
</label>
<input id="bias" type="range" min="-0.8" max="0.8" step="0.05" value="0" />
<div class="range-scale"><span>−0.8 V</span><span>+0.8 V</span></div>
</div>
<div class="control">
<label class="control-label" for="frequency">
<span>Frequency</span>
<output id="frequencyValue">1.0 Hz</output>
</label>
<input id="frequency" type="range" min="0.25" max="3" step="0.05" value="1" />
<div class="range-scale"><span>0.25 Hz</span><span>3 Hz</span></div>
</div>
</section>
<section class="section">
<h2 class="section-heading">
<span>Diode models</span>
<span>02</span>
</h2>
<div class="model-card ideal">
<div class="model-title">
<span>Ideal diode</span>
<span>True switch</span>
</div>
<div class="equation">
v<sub>out</sub> = 0 if v<sub>in</sub> ≤ 0<br />
v<sub>out</sub> = v<sub>in</sub> otherwise
</div>
</div>
<div class="model-card threshold">
<div class="model-title">
<span>Simple threshold</span>
<span>Fixed drop</span>
</div>
<div class="equation">
v<sub>out</sub> = 0 if v<sub>in</sub> ≤ V<sub>f</sub><br />
v<sub>out</sub> = v<sub>in</sub> − V<sub>f</sub> otherwise
</div>
</div>
<div class="control">
<label class="control-label" for="threshold">
<span>Threshold voltage V<sub>f</sub></span>
<output id="thresholdValue">0.70 V</output>
</label>
<input id="threshold" type="range" min="0.2" max="1.2" step="0.05" value="0.7" />
<div class="range-scale"><span>0.2 V</span><span>1.2 V</span></div>
</div>
</section>
<section class="section">
<h2 class="section-heading">
<span>Playback</span>
<span>03</span>
</h2>
<div class="sidebar-actions">
<button class="button primary" id="playButton">Pause</button>
<button class="button" id="resetButton">Reset</button>
</div>
</section>
</aside>
<section class="visual">
<div class="scene-wrap" id="sceneWrap">
<div id="threeHost"></div>
<div class="scene-overlay scene-title">
<div class="eyebrow">Voxel waveform laboratory</div>
<h2>One input. Two output stories.</h2>
<p>The orange model delays conduction and subtracts its threshold drop.</p>
</div>
<div class="scene-overlay scene-tools">
<button class="icon-button" id="cameraReset" title="Reset camera" aria-label="Reset camera">↺</button>
</div>
<div class="scene-overlay scene-legend">
<div class="legend-chip"><i style="color:#a68cff;background:#a68cff"></i>Input</div>
<div class="legend-chip"><i style="color:#4de0d2;background:#4de0d2"></i>Ideal</div>
<div class="legend-chip"><i style="color:#ffb454;background:#ffb454"></i>Threshold</div>
</div>
<div class="scene-overlay scene-help">Drag to orbit · Scroll to zoom</div>
<div class="load-error" id="loadError">
The 3D scene could not load. Check that the cdnjs Three.js module is reachable.
</div>
</div>
<div class="analysis">
<div class="analysis-head">
<div class="analysis-title">
<strong>VOLTAGE TRANSFER</strong>
<span>Hover the plot to inspect values</span>
</div>
<div class="chart-legend">
<span class="input-key"><i></i>Input</span>
<span class="ideal-key"><i></i>Ideal</span>
<span class="threshold-key"><i></i>Threshold</span>
</div>
</div>
<div class="analysis-body">
<div class="chart-shell">
<canvas id="chart" aria-label="Input, ideal diode output, and threshold diode output curves"></canvas>
<div class="chart-readout" id="chartReadout">Hover over the waveform to inspect a point</div>
</div>
<div class="stats">
<div class="stat">
<span>Ideal conduction knee</span>
<strong class="cyan">0.00 V</strong>
</div>
<div class="stat">
<span>Threshold conduction knee</span>
<strong class="orange" id="kneeStat">0.70 V</strong>
</div>
<div class="stat">
<span>Peak output mismatch</span>
<strong id="mismatchStat">0.70 V</strong>
</div>
</div>
</div>
</div>
</section>
</main>
<footer class="reality">
<div class="reality-badge">Reality check</div>
<div class="reality-copy">
<strong>Actual devices have richer behavior.</strong>
Forward current rises exponentially, reverse current is not exactly zero,
and junction capacitance, temperature, switching speed, and breakdown all matter.
</div>
<div class="reality-tags">
<span>Exponential I–V</span>
<span>Reverse leakage</span>
<span>Breakdown</span>
<span>Temperature</span>
<span>Junction capacitance</span>
</div>
</footer>
</div>
<script type="module">
const THREE_URL =
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const TAU = Math.PI * 2;
const state = {
wave: "sine",
amplitude: 2.4,
bias: 0,
frequency: 1,
threshold: 0.7,
running: true,
phase: 0.22,
reveal: 0.58,
revealTarget: 0.58,
hoverPhase: null
};
const ui = {
amplitude: document.querySelector("#amplitude"),
bias: document.querySelector("#bias"),
frequency: document.querySelector("#frequency"),
threshold: document.querySelector("#threshold"),
amplitudeValue: document.querySelector("#amplitudeValue"),
biasValue: document.querySelector("#biasValue"),
frequencyValue: document.querySelector("#frequencyValue"),
thresholdValue: document.querySelector("#thresholdValue"),
kneeStat: document.querySelector("#kneeStat"),
mismatchStat: document.querySelector("#mismatchStat"),
playButton: document.querySelector("#playButton"),
runStatus: document.querySelector("#runStatus"),
resetButton: document.querySelector("#resetButton"),
cameraReset: document.querySelector("#cameraReset"),
chart: document.querySelector("#chart"),
chartReadout: document.querySelector("#chartReadout")
};
let sceneAPI = null;
function clamp(value, min, max) {
return Math.min(max, Math.max(min, value));
}
function signed(value) {
return `${value >= 0 ? "+" : ""}${value.toFixed(2)} V`;
}
function inputAt(phase) {
let normalized = 1;
if (state.wave === "sine") {
normalized = Math.sin(phase);
} else if (state.wave === "triangle") {
normalized = (4 / Math.PI) * Math.asin(Math.sin(phase));
} else {
normalized = Math.sin(phase) >= 0 ? 1 : -1;
}
return state.bias + state.amplitude * normalized;
}
function outputsAt(vin) {
return {
input: vin,
ideal: vin > 0 ? vin : 0,
threshold: vin > state.threshold ? vin - state.threshold : 0
};
}
function setRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
input.style.setProperty("--fill", `${((value - min) / (max - min)) * 100}%`);
}
function updateControlLabels() {
ui.amplitudeValue.textContent = `${state.amplitude.toFixed(2)} V`;
ui.biasValue.textContent = signed(state.bias);
ui.frequencyValue.textContent = `${state.frequency.toFixed(2)} Hz`;
ui.thresholdValue.textContent = `${state.threshold.toFixed(2)} V`;
ui.kneeStat.textContent = `${state.threshold.toFixed(2)} V`;
[ui.amplitude, ui.bias, ui.frequency, ui.threshold].forEach(setRangeFill);
updateStats();
}
function updateStats() {
let maxMismatch = 0;
for (let i = 0; i <= 180; i++) {
const vin = inputAt((i / 180) * TAU);
const result = outputsAt(vin);
maxMismatch = Math.max(
maxMismatch,
Math.abs(result.ideal - result.threshold)
);
}
ui.mismatchStat.textContent = `${maxMismatch.toFixed(2)} V`;
}
function bindRange(input, key, formatter = (v) => v) {
input.addEventListener("input", () => {
state[key] = Number(input.value);
updateControlLabels();
});
}
bindRange(ui.amplitude, "amplitude");
bindRange(ui.bias, "bias");
bindRange(ui.frequency, "frequency");
bindRange(ui.threshold, "threshold");
document.querySelectorAll("[data-wave]").forEach((button) => {
button.addEventListener("click", () => {
state.wave = button.dataset.wave;
document.querySelectorAll("[data-wave]").forEach((other) => {
const selected = other === button;
other.classList.toggle("active", selected);
other.setAttribute("aria-pressed", String(selected));
});
});
});
function updatePlaybackUI() {
ui.playButton.textContent = state.running ? "Pause" : "Play";
ui.runStatus.textContent = state.running ? "Running" : "Paused";
}
ui.playButton.addEventListener("click", () => {
state.running = !state.running;
updatePlaybackUI();
});
ui.resetButton.addEventListener("click", () => {
state.wave = "sine";
state.amplitude = 2.4;
state.bias = 0;
state.frequency = 1;
state.threshold = 0.7;
state.phase = 0.22;
state.running = true;
state.revealTarget = 0.58;
ui.amplitude.value = state.amplitude;
ui.bias.value = state.bias;
ui.frequency.value = state.frequency;
ui.threshold.value = state.threshold;
document.querySelectorAll("[data-wave]").forEach((button) => {
const selected = button.dataset.wave === state.wave;
button.classList.toggle("active", selected);
button.setAttribute("aria-pressed", String(selected));
});
updateControlLabels();
updatePlaybackUI();
});
const revealButton = document.createElement("button");
revealButton.className = "button";
revealButton.id = "revealButton";
revealButton.textContent = "Reveal gap";
revealButton.title = "Highlight the difference between the output curves";
document.querySelector(".analysis-head").appendChild(revealButton);
revealButton.addEventListener("click", () => {
state.revealTarget = state.revealTarget > 0.9 ? 0.18 : 1;
revealButton.textContent =
state.revealTarget > 0.9 ? "Hide gap" : "Reveal gap";
});
// ---------------------------------------------------------------------
// 2D voltage-transfer chart
// ---------------------------------------------------------------------
const chartContext = ui.chart.getContext("2d");
const chartSize = { width: 1, height: 1, dpr: 1 };
function resizeChart() {
const rect = ui.chart.getBoundingClientRect();
chartSize.width = Math.max(1, rect.width);
chartSize.height = Math.max(1, rect.height);
chartSize.dpr = Math.min(window.devicePixelRatio || 1, 2);
ui.chart.width = Math.round(chartSize.width * chartSize.dpr);
ui.chart.height = Math.round(chartSize.height * chartSize.dpr);
drawChart();
}
function chartGeometry(samples) {
const width = chartSize.width;
const height = chartSize.height;
const margin = {
left: 44,
right: 15,
top: 14,
bottom: 29
};
let yMin = -0.15;
let yMax = 0.85;
for (const sample of samples) {
yMin = Math.min(yMin, sample.input);
yMax = Math.max(yMax, sample.input, sample.ideal, sample.threshold);
}
const padding = Math.max(0.12, (yMax - yMin) * 0.08);
yMin -= padding;
yMax += padding;
const plotWidth = width - margin.left - margin.right;
const plotHeight = height - margin.top - margin.bottom;
const x = (phase) =>
margin.left + (phase / TAU) * plotWidth;
const y = (value) =>
margin.top + ((yMax - value) / (yMax - yMin)) * plotHeight;
return { width, height, margin, plotWidth, plotHeight, yMin, yMax, x, y };
}
function drawChart() {
if (!chartContext || chartSize.width < 2) return;
const ctx = chartContext;
ctx.setTransform(chartSize.dpr, 0, 0, chartSize.dpr, 0, 0);
ctx.clearRect(0, 0, chartSize.width, chartSize.height);
const samples = [];
const sampleCount = 190;
for (let i = 0; i <= sampleCount; i++) {
const phase = (i / sampleCount) * TAU;
samples.push({ phase, ...outputsAt(inputAt(phase)) });
}
const g = chartGeometry(samples);
// Horizontal grid and voltage labels.
ctx.font = "9px ui-monospace, SFMono-Regular, Consolas, monospace";
ctx.textAlign = "right";
ctx.textBaseline = "middle";
for (let i = 0; i <= 4; i++) {
const value = g.yMin + ((g.yMax - g.yMin) * i) / 4;
const py = g.y(value);
ctx.strokeStyle = value === 0 ? "#3a465f" : "#202a3d";
ctx.lineWidth = value === 0 ? 1.2 : 1;
ctx.beginPath();
ctx.moveTo(g.margin.left, py);
ctx.lineTo(g.width - g.margin.right, py);
ctx.stroke();
ctx.fillStyle = "#65718a";
ctx.fillText(value.toFixed(1), g.margin.left - 7, py);
}
// Vertical phase grid.
ctx.textAlign = "center";
ctx.textBaseline = "top";
for (let i = 0; i <= 4; i++) {
const phase = (i / 4) * TAU;
const px = g.x(phase);
ctx.strokeStyle = "#1c2638";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(px, g.margin.top);
ctx.lineTo(px, g.height - g.margin.bottom);
ctx.stroke();
ctx.fillStyle = "#65718a";
const label =
i === 0 ? "0" : i === 4 ? "1 cycle" : `${i / 4}`;
ctx.fillText(label, px, g.height - g.margin.bottom + 9);
}
// Difference area between ideal and threshold outputs.
if (state.reveal > 0.03) {
ctx.beginPath();
for (let i = 0; i < samples.length; i++) {
const sample = samples[i];
const px = g.x(sample.phase);
const py = g.y(sample.ideal);
if (i === 0) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
}
for (let i = samples.length - 1; i >= 0; i--) {
const sample = samples[i];
ctx.lineTo(g.x(sample.phase), g.y(sample.threshold));
}
ctx.closePath();
ctx.fillStyle = `rgba(255, 180, 84, ${0.14 * state.reveal})`;
ctx.fill();
}
function drawPath(key, color, lineWidth, dash = []) {
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.setLineDash(dash);
for (let i = 0; i < samples.length; i++) {
const sample = samples[i];
const px = g.x(sample.phase);
const py = g.y(sample[key]);
if (i === 0) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
}
ctx.stroke();
ctx.setLineDash([]);
}
drawPath("input", "#8d7ee0", 1.3, [4, 5]);
drawPath("ideal", "#4de0d2", 2.25);
drawPath(
"threshold",
`rgba(255, 180, 84, ${0.35 + 0.65 * state.reveal})`,
2.25
);
// Live phase marker.
const currentPhase = ((state.phase % TAU) + TAU) % TAU;
const current = outputsAt(inputAt(currentPhase));
const currentX = g.x(currentPhase);
ctx.strokeStyle = "rgba(229, 236, 255, 0.48)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(currentX, g.margin.top);
ctx.lineTo(currentX, g.height - g.margin.bottom);
ctx.stroke();
function marker(value, color, radius) {
ctx.beginPath();
ctx.fillStyle = "#0a111e";
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.arc(currentX, g.y(value), radius, 0, TAU);
ctx.fill();
ctx.stroke();
}
marker(current.input, "#a68cff", 3.3);
marker(current.ideal, "#4de0d2", 3.3);
marker(current.threshold, "#ffb454", 3.3);
// Hover guide.
if (state.hoverPhase !== null) {
const hoverX = g.x(state.hoverPhase);
const hover = outputsAt(inputAt(state.hoverPhase));
ctx.strokeStyle = "rgba(255,255,255,0.24)";
ctx.setLineDash([2, 3]);
ctx.beginPath();
ctx.moveTo(hoverX, g.margin.top);
ctx.lineTo(hoverX, g.height - g.margin.bottom);
ctx.stroke();
ctx.setLineDash([]);
[
[hover.input, "#a68cff"],
[hover.ideal, "#4de0d2"],
[hover.threshold, "#ffb454"]
].forEach(([value, color]) => {
ctx.beginPath();
ctx.fillStyle = color;
ctx.arc(hoverX, g.y(value), 2.7, 0, TAU);
ctx.fill();
});
}
// Units label.
ctx.save();
ctx.translate(12, g.margin.top + g.plotHeight / 2);
ctx.rotate(-Math.PI / 2);
ctx.textAlign = "center";
ctx.fillStyle = "#59667f";
ctx.font = "8px ui-sans-serif, system-ui";
ctx.fillText("VOLTS", 0, 0);
ctx.restore();
}
ui.chart.addEventListener("pointermove", (event) => {
const rect = ui.chart.getBoundingClientRect();
const px = event.clientX - rect.left;
const marginLeft = 44;
const marginRight = 15;
const plotWidth = rect.width - marginLeft - marginRight;
const normalized = clamp(
(px - marginLeft) / plotWidth,
0,
1
);
state.hoverPhase = normalized * TAU;
const value = outputsAt(inputAt(state.hoverPhase));
const percent = Math.round(normalized * 100);
ui.chartReadout.textContent =
`t ${percent}% | in ${value.input.toFixed(2)} V | ` +
`ideal ${value.ideal.toFixed(2)} V | threshold ${value.threshold.toFixed(2)} V`;
});
ui.chart.addEventListener("pointerleave", () => {
state.hoverPhase = null;
ui.chartReadout.textContent =
"Hover over the waveform to inspect a point";
});
// ---------------------------------------------------------------------
// Three.js voxel scene. Only the Three.js core module is imported.
// ---------------------------------------------------------------------
async function initThreeScene() {
const THREE = await import(THREE_URL);
const host = document.querySelector("#threeHost");
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
43,
1,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
host.appendChild(renderer.domElement);
const target = new THREE.Vector3(0, 1.35, 0);
let yaw = 0.24;
let pitch = 0.83;
let radius = 17.4;
function updateCamera() {
camera.position.set(
target.x + radius * Math.sin(pitch) * Math.sin(yaw),
target.y + radius * Math.cos(pitch),
target.z + radius * Math.sin(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
}
function resetCamera() {
yaw = 0.24;
pitch = 0.83;
radius = 17.4;
updateCamera();
}
ui.cameraReset.addEventListener("click", resetCamera);
// Direct, addon-free orbit and zoom controls.
let dragging = false;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
yaw -= dx * 0.006;
pitch = clamp(pitch + dy * 0.005, 0.42, 1.22);
updateCamera();
});
const stopDragging = () => {
dragging = false;
};
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = clamp(radius + event.deltaY * 0.012, 10.5, 25);
updateCamera();
},
{ passive: false }
);
const hemisphere = new THREE.HemisphereLight(
0xaebfff,
0x111526,
1.55
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xdce5ff, 3.2);
keyLight.position.set(-7, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x4de0d2, 16, 13, 2);
cyanLight.position.set(5, 4, 1);
scene.add(cyanLight);
const orangeLight = new THREE.PointLight(0xffb454, 13, 12, 2);
orangeLight.position.set(-3, 3, 5);
scene.add(orangeLight);
function box(width, height, depth, color, x, y, z, options = {}) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const material = new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.68,
metalness: options.metalness ?? 0.08,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
scene.add(mesh);
return mesh;
}
// Ground and raised voxel circuit board.
box(
30,
0.16,
20,
0x0b1220,
0,
-0.25,
0,
{ roughness: 0.95, castShadow: false }
);
box(
13.4,
0.35,
8.2,
0x172235,
0,
0,
0,
{ roughness: 0.82 }
);
const edgeMaterial = new THREE.MeshBasicMaterial({
color: 0x33415d,
transparent: true,
opacity: 0.58
});
const gridVertical = new THREE.BoxGeometry(0.018, 0.014, 7.7);
const gridHorizontal = new THREE.BoxGeometry(12.9, 0.014, 0.018);
for (let x = -6.4; x <= 6.4; x += 0.8) {
const line = new THREE.Mesh(gridVertical, edgeMaterial);
line.position.set(x, 0.185, 0);
scene.add(line);
}
for (let z = -3.6; z <= 3.6; z += 0.6) {
const line = new THREE.Mesh(gridHorizontal, edgeMaterial);
line.position.set(0, 0.185, z);
scene.add(line);
}
// Text sprites are generated locally; no font or helper dependency.
function labelSprite(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 96;
const context = canvas.getContext("2d");
context.fillStyle = "rgba(8, 14, 26, 0.88)";
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = color;
context.globalAlpha = 0.55;
context.strokeRect(2, 2, canvas.width - 4, canvas.height - 4);
context.globalAlpha = 1;
context.fillStyle = color;
context.font = "800 38px ui-sans-serif, system-ui";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(1.9, 0.36, 1);
sprite.renderOrder = 10;
return sprite;
}
const rowZ = [-2.45, 0, 2.45];
const rowColors = [0xa68cff, 0x4de0d2, 0xffb454];
const rowNames = ["INPUT", "IDEAL", "THRESHOLD"];
const rowData = [];
const sampleCount = 96;
const dummy = new THREE.Object3D();
function createWaveRow(z, color, index) {
const markerMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: index === 0 ? 0.2 : 0.38,
roughness: 0.32,
metalness: 0.12,
transparent: true,
opacity: 1
});
const stemMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.18,
roughness: 0.5,
transparent: true,
opacity: 0.42
});
const markers = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.17, 0.17, 0.17),
markerMaterial,
sampleCount
);
const stems = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.045, 1, 0.045),
stemMaterial,
sampleCount
);
markers.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
stems.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
markers.castShadow = true;
stems.castShadow = false;
scene.add(markers, stems);
const label = labelSprite(rowNames[index], `#${color.toString(16).padStart(6, "0")}`);
label.position.set(-6.12, 2.75, z);
scene.add(label);
const pulse = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.27, 0.27),
new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: color,
emissiveIntensity: 1.8,
roughness: 0.25
})
);
pulse.castShadow = true;
scene.add(pulse);
return {
z,
color,
markers,
stems,
markerMaterial,
stemMaterial,
pulse
};
}
rowZ.forEach((z, index) => {
rowData.push(createWaveRow(z, rowColors[index], index));
});
// Source and load towers.
const sourceBody = box(
0.82,
0.62,
0.82,
0x26324a,
-6.15,
0.48,
rowZ[0],
{ metalness: 0.22 }
);
const sourceIndicator = box(
0.44,
0.44,
0.44,
0xa68cff,
-6.15,
1.02,
rowZ[0],
{
emissive: 0xa68cff,
emissiveIntensity: 1.1,
roughness: 0.3
}
);
const loadIndicators = [];
[rowZ[1], rowZ[2]].forEach((z, index) => {
const color = index === 0 ? 0x4de0d2 : 0xffb454;
box(
0.82,
0.62,
0.82,
0x26324a,
6.15,
0.48,
z,
{ metalness: 0.22 }
);
const indicator = box(
0.44,
0.44,
0.44,
color,
6.15,
1.02,
z,
{
emissive: color,
emissiveIntensity: 1.0,
roughness: 0.3
}
);
loadIndicators.push(indicator);
});
// Floating voxel diode model.
const diodeGroup = new THREE.Group();
diodeGroup.position.set(0, 4.25, 0);
scene.add(diodeGroup);
const body = new THREE.Mesh(
new THREE.BoxGeometry(1.16, 1.16, 1.16),
new THREE.MeshStandardMaterial({
color: 0x171b25,
roughness: 0.32,
metalness: 0.4
})
);
body.castShadow = true;
diodeGroup.add(body);
const band = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 1.22, 1.22),
new THREE.MeshStandardMaterial({
color: 0xbac5d8,
roughness: 0.26,
metalness: 0.8
})
);
band.position.x = 0.27;
band.castShadow = true;
diodeGroup.add(band);
const leadMaterial = new THREE.MeshStandardMaterial({
color: 0x8590a7,
roughness: 0.3,
metalness: 0.75
});
const leftLead = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 0.14, 0.14),
leadMaterial
);
leftLead.position.x = -0.88;
leftLead.castShadow = true;
diodeGroup.add(leftLead);
const rightLead = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 0.14, 0.14),
leadMaterial
);
rightLead.position.x = 0.88;
rightLead.castShadow = true;
diodeGroup.add(rightLead);
const diodeLabel = labelSprite("DIODE", "#dce5ff");
diodeLabel.position.set(0, 5.13, 0);
scene.add(diodeLabel);
function resize() {
const width = Math.max(1, host.clientWidth);
const height = Math.max(1, host.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(host);
resize();
updateCamera();
const zeroY = 1.85;
const voltageScale = 0.38;
const xStart = -5.45;
const xEnd = 5.45;
function updateWaveRow(row, key, phase) {
for (let i = 0; i < sampleCount; i++) {
const samplePhase = (i / (sampleCount - 1)) * TAU;
const value = outputsAt(inputAt(samplePhase))[key];
const x = xStart + (i / (sampleCount - 1)) * (xEnd - xStart);
const y = zeroY + value * voltageScale;
dummy.position.set(x, y, row.z);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
row.markers.setMatrixAt(i, dummy.matrix);
const floor = 0.195;
const stemHeight = Math.max(0.03, y - floor);
dummy.position.set(x, floor + stemHeight / 2, row.z);
dummy.scale.set(1, stemHeight, 1);
dummy.updateMatrix();
row.stems.setMatrixAt(i, dummy.matrix);
}
row.markers.instanceMatrix.needsUpdate = true;
row.stems.instanceMatrix.needsUpdate = true;
const currentValue = outputsAt(inputAt(phase))[key];
const progress = ((phase % TAU) + TAU) % TAU / TAU;
row.pulse.position.set(
xStart + progress * (xEnd - xStart),
zeroY + currentValue * voltageScale,
row.z
);
}
function update(phase) {
const wrappedPhase = ((phase % TAU) + TAU) % TAU;
updateWaveRow(rowData[0], "input", wrappedPhase);
updateWaveRow(rowData[1], "ideal", wrappedPhase);
updateWaveRow(rowData[2], "threshold", wrappedPhase);
rowData[2].markerMaterial.opacity = 0.25 + 0.75 * state.reveal;
rowData[2].stemMaterial.opacity = 0.1 + 0.48 * state.reveal;
const current = outputsAt(inputAt(wrappedPhase));
sourceIndicator.material.emissiveIntensity =
0.35 + Math.max(0, current.input) * 0.55;
sourceIndicator.scale.y =
0.85 + clamp((current.input + 1.5) / 4.5, 0, 1) * 0.4;
loadIndicators[0].material.emissiveIntensity =
0.25 + current.ideal * 0.65;
loadIndicators[1].material.emissiveIntensity =
(0.2 + current.threshold * 0.65) * state.reveal;
diodeGroup.rotation.y = Math.sin(phase * 0.45) * 0.08;
renderer.render(scene, camera);
}
return { update, resetCamera };
}
initThreeScene()
.then((api) => {
sceneAPI = api;
document.querySelector("#loadError").style.display = "none";
})
.catch((error) => {
console.error(error);
document.querySelector("#loadError").style.display = "grid";
});
// ---------------------------------------------------------------------
// Main animation loop
// ---------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (state.running) {
state.phase += delta * state.frequency * TAU;
}
state.reveal +=
(state.revealTarget - state.reveal) * Math.min(1, delta * 7);
drawChart();
sceneAPI?.update(state.phase);
requestAnimationFrame(animate);
}
updateControlLabels();
updatePlaybackUI();
resizeChart();
const chartObserver = new ResizeObserver(resizeChart);
chartObserver.observe(ui.chart);
requestAnimationFrame(animate);
</script>
</body>
</html>
```