Prompt specification
Build or customize the Three.js 3D scene "Two-input threshold neuron model" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Two-input threshold neuron model
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-input-threshold-neuron-model-x1893
Scene Description Prompt:
Make a low-poly threshold neuron analogy with two input sliders, weights, bias, calculated output, and indicator lamp. One HTML file using Three.js from cdnjs.
Working Three.js Starter (two-input-threshold-neuron-model-x1893):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Low-Poly Threshold Neuron</title>
<style>
:root {
--bg: #07111f;
--panel: #0b1727;
--panel-2: #101f32;
--line: #24354a;
--text: #edf4fb;
--muted: #8fa3b8;
--cyan: #45d6e6;
--pink: #ff6f91;
--orange: #ffbf69;
--green: #7cf2ad;
--red: #ff7085;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
.app {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
height: 100svh;
min-height: 620px;
}
.viewport {
position: relative;
min-width: 0;
overflow: hidden;
background:
radial-gradient(circle at 45% 40%, rgba(44, 93, 126, 0.18), transparent 40%),
linear-gradient(145deg, #081523, #050c15);
}
.webgl {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
.webgl:active {
cursor: grabbing;
}
.scene-header {
position: absolute;
z-index: 3;
top: 24px;
left: 26px;
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.scene-header h1 {
margin: 7px 0 4px;
font-size: clamp(23px, 3vw, 38px);
letter-spacing: -0.04em;
}
.scene-header p {
margin: 0;
color: var(--muted);
font-size: 13px;
}
.scene-badge {
position: absolute;
z-index: 3;
top: 25px;
right: 24px;
display: flex;
align-items: center;
gap: 9px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 17, 31, 0.72);
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.scene-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px var(--green);
content: "";
}
.scene-badge.inactive::before {
background: var(--red);
box-shadow: 0 0 12px var(--red);
}
.scene-label {
position: absolute;
z-index: 2;
top: 0;
left: 0;
padding: 5px 8px;
border: 1px solid rgba(143, 163, 184, 0.24);
border-radius: 6px;
background: rgba(5, 12, 21, 0.78);
color: #dce9f4;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
white-space: nowrap;
pointer-events: none;
backdrop-filter: blur(8px);
will-change: transform;
}
.scene-label.cyan {
color: var(--cyan);
border-color: rgba(69, 214, 230, 0.35);
}
.scene-label.pink {
color: var(--pink);
border-color: rgba(255, 111, 145, 0.35);
}
.scene-label.green {
color: var(--green);
border-color: rgba(124, 242, 173, 0.35);
}
.view-hint {
position: absolute;
z-index: 3;
bottom: 20px;
left: 24px;
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
.view-hint kbd {
padding: 4px 7px;
border: 1px solid var(--line);
border-bottom-width: 2px;
border-radius: 5px;
background: rgba(12, 27, 43, 0.85);
color: #c6d5e2;
font: inherit;
}
.reset-view {
position: absolute;
z-index: 4;
right: 22px;
bottom: 18px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 7px;
background: rgba(9, 20, 33, 0.88);
color: var(--muted);
font-size: 11px;
cursor: pointer;
}
.reset-view:hover {
border-color: #48627d;
color: var(--text);
}
.panel {
position: relative;
z-index: 5;
overflow-y: auto;
padding: 25px 24px 22px;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(255, 255, 255, 0.025), transparent 180px),
var(--panel);
scrollbar-color: #2a4057 transparent;
}
.panel-title {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 15px;
margin-bottom: 20px;
}
.panel-title h2 {
margin: 5px 0 0;
font-size: 20px;
letter-spacing: -0.025em;
}
.theta-chip {
padding: 7px 9px;
border: 1px solid rgba(255, 191, 105, 0.35);
border-radius: 6px;
background: rgba(255, 191, 105, 0.08);
color: var(--orange);
font-size: 11px;
font-weight: 800;
white-space: nowrap;
}
.section {
margin-top: 19px;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
}
.section-heading h3 {
margin: 0;
color: #b8c8d8;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-heading span {
color: #647b91;
font-size: 10px;
}
.control {
padding: 11px 12px 9px;
border: 1px solid #203248;
border-radius: 9px;
background: rgba(15, 31, 49, 0.72);
}
.control + .control {
margin-top: 9px;
}
.control label {
display: grid;
grid-template-columns: 1fr auto;
gap: 10px;
align-items: center;
margin-bottom: 9px;
font-size: 12px;
font-weight: 650;
}
.control output {
min-width: 43px;
padding: 2px 6px;
border-radius: 5px;
background: #0a1422;
color: #dbe8f3;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
text-align: right;
}
input[type="range"] {
--fill: 50%;
--track: var(--cyan);
width: 100%;
height: 4px;
margin: 5px 0 2px;
border-radius: 999px;
outline: none;
appearance: none;
background: linear-gradient(
90deg,
var(--track) 0 var(--fill),
#26394e var(--fill) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 2px solid #07111f;
border-radius: 50%;
appearance: none;
background: var(--track);
box-shadow: 0 0 0 1px var(--track), 0 0 10px color-mix(in srgb, var(--track) 50%, transparent);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #07111f;
border-radius: 50%;
background: var(--track);
box-shadow: 0 0 0 1px var(--track);
}
.output-card {
position: relative;
overflow: hidden;
padding: 16px;
border: 1px solid #294057;
border-radius: 11px;
background:
linear-gradient(135deg, rgba(69, 214, 230, 0.08), transparent 60%),
var(--panel-2);
}
.output-card::after {
position: absolute;
top: -40px;
right: -35px;
width: 115px;
height: 115px;
border: 18px solid rgba(255, 255, 255, 0.018);
border-radius: 50%;
content: "";
}
.output-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.output-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.net-value {
margin-top: 5px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 28px;
font-weight: 750;
letter-spacing: -0.05em;
}
.meter {
position: relative;
height: 7px;
margin: 15px 0 7px;
border-radius: 999px;
background: #26364a;
}
.meter-fill {
position: absolute;
top: 0;
bottom: 0;
left: 50%;
width: 0;
border-radius: 999px;
background: var(--red);
transition: left 180ms ease, width 180ms ease, background 180ms ease;
}
.threshold-marker {
position: absolute;
top: -4px;
bottom: -4px;
left: 50%;
width: 2px;
background: var(--orange);
box-shadow: 0 0 8px rgba(255, 191, 105, 0.7);
}
.meter-labels {
display: flex;
justify-content: space-between;
color: #657b90;
font-size: 9px;
}
.lamp-wrap {
display: flex;
align-items: center;
gap: 14px;
min-width: 115px;
}
.lamp {
position: relative;
width: 54px;
height: 54px;
flex: 0 0 auto;
border: 3px solid #26394d;
border-radius: 50%;
background: #091320;
box-shadow:
inset 0 0 0 4px #101f30,
0 7px 15px rgba(0, 0, 0, 0.35);
}
.lamp::before {
position: absolute;
top: 8px;
left: 10px;
width: 13px;
height: 8px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.22);
content: "";
transform: rotate(-25deg);
}
.lamp.active {
border-color: #3e8f65;
background: var(--green);
box-shadow:
inset 0 0 0 4px #152e25,
0 0 25px rgba(124, 242, 173, 0.55);
}
.output-number {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 31px;
font-weight: 800;
line-height: 1;
}
.output-state {
margin-top: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.output-card.active .output-number,
.output-card.active .output-state {
color: var(--green);
}
.output-card.inactive .output-number,
.output-card.inactive .output-state {
color: var(--red);
}
.formula {
margin-top: 13px;
padding: 11px 12px;
border-left: 2px solid var(--cyan);
background: rgba(69, 214, 230, 0.045);
color: #9db2c5;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
line-height: 1.6;
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 13px;
}
.preset {
padding: 8px 4px;
border: 1px solid #26394e;
border-radius: 6px;
background: #0b1726;
color: #9eb2c6;
font-size: 10px;
font-weight: 750;
cursor: pointer;
}
.preset:hover {
border-color: #4a6c8b;
color: var(--text);
background: #112238;
}
.notes {
margin: 20px 0 0;
padding-top: 15px;
border-top: 1px solid var(--line);
color: #72899e;
font-size: 10px;
line-height: 1.6;
}
@media (max-width: 850px) {
body {
overflow: auto;
}
.app {
grid-template-columns: 1fr;
grid-template-rows: 56svh auto;
height: auto;
min-height: 100svh;
}
.panel {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
.scene-header {
top: 17px;
left: 18px;
}
.scene-header h1 {
font-size: 24px;
}
.scene-header p,
.scene-badge {
display: none;
}
.view-hint {
bottom: 13px;
left: 17px;
}
.reset-view {
right: 15px;
bottom: 12px;
}
}
</style>
</head>
<body>
<main class="app">
<section class="viewport" id="viewport" aria-label="Interactive 3D threshold neuron">
<div class="scene-header">
<div class="eyebrow">Neural Lab / Unit 01</div>
<h1>Threshold Neuron</h1>
<p>Weighted signals become a single binary decision.</p>
</div>
<div class="scene-badge" id="sceneBadge">Output active</div>
<div class="scene-label cyan" id="labelInput1">INPUT x₁</div>
<div class="scene-label pink" id="labelInput2">INPUT x₂</div>
<div class="scene-label" id="labelSoma">Σ + BIAS</div>
<div class="scene-label" id="labelGate">θ GATE</div>
<div class="scene-label green" id="labelOutput">OUTPUT</div>
<div class="view-hint">
<span><kbd>Drag</kbd> orbit</span>
<span><kbd>Scroll</kbd> zoom</span>
</div>
<button class="reset-view" id="resetView" type="button">Reset view</button>
</section>
<aside class="panel">
<div class="panel-title">
<div>
<div class="eyebrow">Parameters</div>
<h2>Configure the neuron</h2>
</div>
<div class="theta-chip">θ = 0.50</div>
</div>
<section class="section">
<div class="section-heading">
<h3>Input signals</h3>
<span>0.00 — 1.00</span>
</div>
<div class="control">
<label for="x1">
<span>Input x₁</span>
<output id="x1Value">0.80</output>
</label>
<input
id="x1"
type="range"
min="0"
max="1"
step="0.01"
value="0.80"
style="--track: var(--cyan)"
/>
</div>
<div class="control">
<label for="x2">
<span>Input x₂</span>
<output id="x2Value">0.70</output>
</label>
<input
id="x2"
type="range"
min="0"
max="1"
step="0.01"
value="0.70"
style="--track: var(--pink)"
/>
</div>
</section>
<section class="section">
<div class="section-heading">
<h3>Synaptic weights</h3>
<span>−1.00 — 1.00</span>
</div>
<div class="control">
<label for="w1">
<span>Weight w₁</span>
<output id="w1Value">0.90</output>
</label>
<input
id="w1"
type="range"
min="-1"
max="1"
step="0.01"
value="0.90"
style="--track: var(--orange)"
/>
</div>
<div class="control">
<label for="w2">
<span>Weight w₂</span>
<output id="w2Value">0.60</output>
</label>
<input
id="w2"
type="range"
min="-1"
max="1"
step="0.01"
value="0.60"
style="--track: var(--orange)"
/>
</div>
</section>
<section class="section">
<div class="section-heading">
<h3>Cell bias</h3>
<span>−1.00 — 1.00</span>
</div>
<div class="control">
<label for="bias">
<span>Bias b</span>
<output id="biasValue">−0.15</output>
</label>
<input
id="bias"
type="range"
min="-1"
max="1"
step="0.01"
value="-0.15"
style="--track: var(--cyan)"
/>
</div>
</section>
<section class="section">
<div class="section-heading">
<h3>Calculated output</h3>
<span>Hard threshold</span>
</div>
<div class="output-card active" id="outputCard">
<div class="output-top">
<div>
<div class="output-label">Net activation</div>
<div class="net-value" id="netValue">+0.99</div>
</div>
<div class="lamp-wrap">
<div class="lamp active" id="indicatorLamp"></div>
<div>
<div class="output-number" id="outputValue" aria-live="polite">1</div>
<div class="output-state" id="outputState">Firing</div>
</div>
</div>
</div>
<div class="meter" aria-hidden="true">
<div class="meter-fill" id="meterFill"></div>
<div class="threshold-marker"></div>
</div>
<div class="meter-labels">
<span>−3</span>
<span>threshold</span>
<span>+3</span>
</div>
<div class="formula">
z = (x₁ × w₁) + (x₂ × w₂) + b<br />
y = 1 if z ≥ 0.50, otherwise 0
</div>
<div class="presets">
<button class="preset" type="button" data-preset="fire">Fire</button>
<button class="preset" type="button" data-preset="hold">Hold</button>
<button class="preset" type="button" data-preset="silent">Silent</button>
</div>
</div>
</section>
<p class="notes">
This is a simplified perceptron: incoming signals are multiplied by their
synaptic weights, summed with the bias, and compared against a fixed
threshold. Negative weights can inhibit the neuron.
</p>
</aside>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(viewport.clientWidth, viewport.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.className = "webgl";
viewport.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 12, 24);
const camera = new THREE.PerspectiveCamera(
43,
viewport.clientWidth / viewport.clientHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0x9edcff, 0x182031, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffe1b0, 3.2);
keyLight.position.set(-3, 7, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x45d6e6, 2.1);
rimLight.position.set(6, 1, -6);
scene.add(rimLight);
const pinkLight = new THREE.PointLight(0xff6f91, 4, 8, 2);
pinkLight.position.set(-4.5, -1.8, 2.5);
scene.add(pinkLight);
// -------------------------------------------------------------------------
// Low-poly neuron construction
// -------------------------------------------------------------------------
const neuron = new THREE.Group();
scene.add(neuron);
const somaMaterial = new THREE.MeshStandardMaterial({
color: 0xffbf69,
roughness: 0.72,
metalness: 0.04,
flatShading: true,
emissive: 0x7b3900,
emissiveIntensity: 0.25
});
const dendriteMaterial = new THREE.MeshStandardMaterial({
color: 0xd58a53,
roughness: 0.8,
flatShading: true
});
const branchMaterial = new THREE.MeshStandardMaterial({
color: 0xbc704b,
roughness: 0.85,
flatShading: true
});
const axonMaterial = new THREE.MeshStandardMaterial({
color: 0xe6a85d,
roughness: 0.68,
flatShading: true
});
const cyanMaterial = new THREE.MeshStandardMaterial({
color: 0x45d6e6,
emissive: 0x087786,
emissiveIntensity: 0.8,
roughness: 0.45,
flatShading: true
});
const pinkMaterial = new THREE.MeshStandardMaterial({
color: 0xff6f91,
emissive: 0x8a1738,
emissiveIntensity: 0.75,
roughness: 0.45,
flatShading: true
});
const outputMaterial = new THREE.MeshStandardMaterial({
color: 0x7cf2ad,
emissive: 0x21a95f,
emissiveIntensity: 2,
roughness: 0.35,
flatShading: true
});
const thresholdMaterial = new THREE.MeshStandardMaterial({
color: 0xffbf69,
emissive: 0x7d4500,
emissiveIntensity: 0.45,
roughness: 0.5,
flatShading: true
});
function addTube(points, radius, material, tubularSegments = 16) {
const curve = new THREE.CatmullRomCurve3(
points.map((point) => new THREE.Vector3(...point))
);
const mesh = new THREE.Mesh(
new THREE.TubeGeometry(curve, tubularSegments, radius, 5, false),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
neuron.add(mesh);
return { curve, mesh };
}
// Cell body and nucleus.
const soma = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.05, 1),
somaMaterial
);
soma.position.set(-0.35, 0, 0);
soma.rotation.set(0.15, -0.25, -0.1);
soma.castShadow = true;
soma.receiveShadow = true;
neuron.add(soma);
const nucleus = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.42, 1),
new THREE.MeshStandardMaterial({
color: 0xb85a3b,
roughness: 0.78,
flatShading: true
})
);
nucleus.position.set(-0.32, 0.06, 0.77);
nucleus.scale.set(1, 1.12, 0.65);
nucleus.castShadow = true;
neuron.add(nucleus);
// Two primary input dendrites.
const inputCurve1 = addTube(
[
[-0.65, 0.35, 0],
[-1.45, 0.62, 0.08],
[-2.4, 0.98, 0.06],
[-3.35, 1.43, 0.02],
[-4.15, 1.55, 0]
],
0.15,
dendriteMaterial,
18
);
const inputCurve2 = addTube(
[
[-0.65, -0.35, 0.08],
[-1.45, -0.72, 0.13],
[-2.35, -1.05, 0.18],
[-3.25, -1.47, 0.18],
[-4.05, -1.55, 0.18]
],
0.15,
dendriteMaterial,
18
);
// Smaller dendritic branches.
addTube(
[
[-2.05, 0.88, 0.05],
[-1.95, 1.55, 0.03],
[-1.45, 2.05, -0.02],
[-0.85, 2.35, -0.05]
],
0.095,
branchMaterial,
12
);
addTube(
[
[-2.55, -1.02, 0.16],
[-2.85, -1.65, 0.13],
[-3.35, -1.98, 0.1],
[-3.95, -2.05, 0.08]
],
0.09,
branchMaterial,
12
);
addTube(
[
[-1.6, 0.65, 0.08],
[-1.25, 1.25, 0.04],
[-0.75, 1.55, 0]
],
0.075,
branchMaterial,
10
);
addTube(
[
[-1.65, -0.75, 0.12],
[-1.4, -1.45, 0.1],
[-0.9, -1.85, 0.06]
],
0.075,
branchMaterial,
10
);
// Main axon.
addTube(
[
[0.45, 0, 0],
[1.1, 0.04, 0.02],
[1.9, -0.08, 0.04],
[2.7, 0.12, 0.03],
[3.45, 0.02, 0],
[4.05, 0, 0]
],
0.17,
axonMaterial,
20
);
// Axon terminal branches and synaptic knobs.
const terminalMaterial = new THREE.MeshStandardMaterial({
color: 0xe7ae69,
roughness: 0.7,
flatShading: true
});
const terminalPoints = [
[3.42, 0.02, 0],
[3.82, 0.52, 0.02],
[4.2, 0.85, 0.03]
];
addTube(terminalPoints, 0.075, terminalMaterial, 10);
addTube(
[
[3.45, 0.02, 0],
[3.78, -0.45, 0.05],
[4.18, -0.78, 0.08]
],
0.075,
terminalMaterial,
10
);
for (const point of [
[4.2, 0.85, 0.03],
[4.18, -0.78, 0.08]
]) {
const knob = new THREE.Mesh(
new THREE.OctahedronGeometry(0.17, 0),
terminalMaterial
);
knob.position.set(...point);
knob.castShadow = true;
neuron.add(knob);
}
// Input synapse pads.
function createSynapse(position, material) {
const group = new THREE.Group();
group.position.set(...position);
const pad = new THREE.Mesh(
new THREE.OctahedronGeometry(0.3, 0),
material
);
pad.castShadow = true;
group.add(pad);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.43, 0.035, 4, 20),
material
);
ring.position.z = 0.05;
group.add(ring);
neuron.add(group);
return { group, pad, ring };
}
const synapse1 = createSynapse([-4.15, 1.55, 0], cyanMaterial);
const synapse2 = createSynapse([-4.05, -1.55, 0.18], pinkMaterial);
// Threshold gate around the axon.
const thresholdGate = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.055, 4, 24),
thresholdMaterial
);
thresholdGate.position.set(1.25, 0, 0);
thresholdGate.rotation.y = Math.PI / 2;
neuron.add(thresholdGate);
for (let i = 0; i < 3; i++) {
const fin = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.16, 0.34),
thresholdMaterial
);
fin.position.set(1.25, 0, 0);
fin.rotation.z = (i * Math.PI * 2) / 3;
fin.translateY(0.39);
neuron.add(fin);
}
// Output lamp in the 3D scene.
const outputLamp = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.46, 1),
outputMaterial
);
outputLamp.position.set(4.55, 0, 0);
outputLamp.castShadow = true;
neuron.add(outputLamp);
const outputRing = new THREE.Mesh(
new THREE.TorusGeometry(0.62, 0.045, 4, 24),
outputMaterial
);
outputRing.position.set(4.55, 0, 0);
outputRing.rotation.y = Math.PI / 2;
neuron.add(outputRing);
const outputLight = new THREE.PointLight(0x7cf2ad, 4, 6, 2);
outputLight.position.set(4.55, 0, 0.8);
neuron.add(outputLight);
// Floor and grid.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(28, 22),
new THREE.MeshStandardMaterial({
color: 0x081421,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.25;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(22, 22, 0x31516a, 0x183148);
grid.position.y = -2.23;
grid.material.transparent = true;
grid.material.opacity = 0.2;
scene.add(grid);
// Small low-poly background particles.
const starGeometry = new THREE.TetrahedronGeometry(0.035, 0);
const starMaterial = new THREE.MeshBasicMaterial({
color: 0x62849d,
transparent: true,
opacity: 0.45
});
let randomSeed = 127;
function seededRandom() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
for (let i = 0; i < 42; i++) {
const star = new THREE.Mesh(starGeometry, starMaterial);
star.position.set(
(seededRandom() - 0.5) * 18,
(seededRandom() - 0.5) * 11,
-4 - seededRandom() * 8
);
star.rotation.set(
seededRandom() * Math.PI,
seededRandom() * Math.PI,
seededRandom() * Math.PI
);
scene.add(star);
}
// -------------------------------------------------------------------------
// Moving signal particles
// -------------------------------------------------------------------------
const inputPulseMaterial1 = new THREE.MeshBasicMaterial({ color: 0x8af4ff });
const inputPulseMaterial2 = new THREE.MeshBasicMaterial({ color: 0xffa0b8 });
const outputPulseMaterial = new THREE.MeshBasicMaterial({ color: 0xa0ffd0 });
function createPulse(material, radius = 0.09) {
const mesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius, 0),
material
);
neuron.add(mesh);
return mesh;
}
const inputPulses1 = Array.from({ length: 3 }, () =>
createPulse(inputPulseMaterial1, 0.095)
);
const inputPulses2 = Array.from({ length: 3 }, () =>
createPulse(inputPulseMaterial2, 0.095)
);
const outputPulses = Array.from({ length: 9 }, () =>
createPulse(outputPulseMaterial, 0.07)
);
// -------------------------------------------------------------------------
// Direct orbit controls: no external controls addon is used
// -------------------------------------------------------------------------
const orbit = {
yaw: 0.48,
pitch: 0.22,
radius: 10.4,
target: new THREE.Vector3(0, 0, 0)
};
const defaultOrbit = { ...orbit };
function updateCamera() {
const horizontal = Math.cos(orbit.pitch) * orbit.radius;
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontal,
orbit.target.y + Math.sin(orbit.pitch) * orbit.radius,
orbit.target.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(orbit.target);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
orbit.yaw -= deltaX * 0.006;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + deltaY * 0.005,
-0.55,
0.8
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("lostpointercapture", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
6.6,
14
);
updateCamera();
},
{ passive: false }
);
document.getElementById("resetView").addEventListener("click", () => {
orbit.yaw = defaultOrbit.yaw;
orbit.pitch = defaultOrbit.pitch;
orbit.radius = defaultOrbit.radius;
orbit.target.copy(defaultOrbit.target);
updateCamera();
});
updateCamera();
// -------------------------------------------------------------------------
// DOM labels projected from 3D positions
// -------------------------------------------------------------------------
const labels = [
{
element: document.getElementById("labelInput1"),
position: new THREE.Vector3(-4.15, 2.08, 0)
},
{
element: document.getElementById("labelInput2"),
position: new THREE.Vector3(-4.05, -2.08, 0.18)
},
{
element: document.getElementById("labelSoma"),
position: new THREE.Vector3(-0.35, 1.48, 0)
},
{
element: document.getElementById("labelGate"),
position: new THREE.Vector3(1.25, 0.72, 0)
},
{
element: document.getElementById("labelOutput"),
position: new THREE.Vector3(4.55, 0.88, 0)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
for (const label of labels) {
projected.copy(label.position).project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
label.element.style.opacity =
projected.z > -1 && projected.z < 1 ? "1" : "0";
}
}
// -------------------------------------------------------------------------
// Controls and threshold calculation
// -------------------------------------------------------------------------
const THRESHOLD = 0.5;
const controls = {
x1: document.getElementById("x1"),
x2: document.getElementById("x2"),
w1: document.getElementById("w1"),
w2: document.getElementById("w2"),
bias: document.getElementById("bias")
};
const valueElements = {
x1: document.getElementById("x1Value"),
x2: document.getElementById("x2Value"),
w1: document.getElementById("w1Value"),
w2: document.getElementById("w2Value"),
bias: document.getElementById("biasValue")
};
const netValue = document.getElementById("netValue");
const outputValue = document.getElementById("outputValue");
const outputState = document.getElementById("outputState");
const outputCard = document.getElementById("outputCard");
const indicatorLamp = document.getElementById("indicatorLamp");
const meterFill = document.getElementById("meterFill");
const sceneBadge = document.getElementById("sceneBadge");
function formatSigned(value) {
const rounded = Math.abs(value) < 0.005 ? 0 : value;
return `${rounded >= 0 ? "+" : "−"}${Math.abs(rounded).toFixed(2)}`;
}
function formatPlain(value) {
const rounded = Math.abs(value) < 0.005 ? 0 : value;
return `${rounded < 0 ? "−" : ""}${Math.abs(rounded).toFixed(2)}`;
}
function updateRangeAppearance(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
let currentState = {
x1: 0.8,
x2: 0.7,
w1: 0.9,
w2: 0.6,
bias: -0.15,
net: 0.99,
active: true
};
function updateSimulation() {
const values = {};
for (const key of Object.keys(controls)) {
values[key] = Number(controls[key].value);
valueElements[key].textContent =
key === "bias" ? formatSigned(values[key]) : formatPlain(values[key]);
updateRangeAppearance(controls[key]);
}
const net =
values.x1 * values.w1 +
values.x2 * values.w2 +
values.bias;
const active = net >= THRESHOLD;
currentState = { ...values, net, active };
netValue.textContent = formatSigned(net);
outputValue.textContent = active ? "1" : "0";
outputState.textContent = active ? "Firing" : "Silent";
outputCard.classList.toggle("active", active);
outputCard.classList.toggle("inactive", !active);
indicatorLamp.classList.toggle("active", active);
sceneBadge.classList.toggle("inactive", !active);
sceneBadge.textContent = active ? "Output active" : "Output silent";
// Meter maps the theoretical −3…+3 range to 0…100%.
const netPercent = ((net + 3) / 6) * 100;
const center = 50;
const fillStart = Math.min(center, netPercent);
const fillWidth = Math.abs(netPercent - center);
meterFill.style.left = `${fillStart}%`;
meterFill.style.width = `${fillWidth}%`;
meterFill.style.background = active
? "var(--green)"
: "var(--red)";
// Neuron body and lamp respond to the threshold decision.
somaMaterial.color.set(active ? 0xffbf69 : 0x607b96);
somaMaterial.emissive.set(active ? 0x7b3900 : 0x071525);
somaMaterial.emissiveIntensity = active ? 0.34 : 0.12;
thresholdMaterial.color.set(active ? 0x7cf2ad : 0xff8b87);
thresholdMaterial.emissive.set(active ? 0x168345 : 0x721a22);
thresholdMaterial.emissiveIntensity = active ? 1.05 : 0.3;
outputMaterial.color.set(active ? 0x7cf2ad : 0xff7085);
outputMaterial.emissive.set(active ? 0x21a95f : 0x8a1738);
outputMaterial.emissiveIntensity = active ? 2.2 : 0.35;
outputLight.color.set(active ? 0x7cf2ad : 0xff7085);
outputLight.intensity = active ? 4.2 : 0.35;
sceneBadge.classList.toggle("inactive", !active);
sceneBadge.textContent = active ? "Output active" : "Output silent";
}
for (const input of Object.values(controls)) {
input.addEventListener("input", updateSimulation);
}
const presets = {
fire: { x1: 0.85, x2: 0.70, w1: 0.90, w2: 0.70, bias: 0.05 },
hold: { x1: 0.50, x2: 0.50, w1: 0.00, w2: 0.00, bias: 0.45 },
silent: { x1: 0.20, x2: 0.30, w1: -0.80, w2: 0.80, bias: -0.20 }
};
document.querySelectorAll("[data-preset]").forEach((button) => {
button.addEventListener("click", () => {
const preset = presets[button.dataset.preset];
for (const key of Object.keys(preset)) {
controls[key].value = preset[key];
}
updateSimulation();
});
});
updateSimulation();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
soma.rotation.y = -0.25 + Math.sin(elapsed * 0.35) * 0.06;
soma.rotation.z = -0.1 + Math.cos(elapsed * 0.3) * 0.035;
nucleus.rotation.y = elapsed * 0.15;
synapse1.ring.rotation.z = elapsed * 0.7;
synapse2.ring.rotation.z = -elapsed * 0.7;
const inputScale1 = 0.86 + currentState.x1 * 0.28;
const inputScale2 = 0.86 + currentState.x2 * 0.28;
synapse1.pad.scale.setScalar(inputScale1);
synapse2.pad.scale.setScalar(inputScale2);
synapse1.ring.scale.setScalar(0.95 + Math.sin(elapsed * 2.2) * 0.06);
synapse2.ring.scale.setScalar(0.95 + Math.sin(elapsed * 2.2 + 1) * 0.06);
// Input pulses travel from the synapses toward the soma.
inputPulses1.forEach((pulse, index) => {
const progress = (elapsed * 0.4 + index / 3) % 1;
pulse.position.copy(inputCurve1.curve.getPoint(1 - progress));
pulse.scale.setScalar(0.65 + currentState.x1 * 0.65);
pulse.visible = currentState.x1 > 0.015;
});
inputPulses2.forEach((pulse, index) => {
const progress = (elapsed * 0.4 + index / 3) % 1;
pulse.position.copy(inputCurve2.curve.getPoint(1 - progress));
pulse.scale.setScalar(0.65 + currentState.x2 * 0.65);
pulse.visible = currentState.x2 > 0.015;
});
// Output pulses only move when the threshold is crossed.
outputPulses.forEach((pulse, index) => {
const progress = (elapsed * 0.52 + index / outputPulses.length) % 1;
pulse.position.set(
0.58 + progress * 3.72,
Math.sin(progress * Math.PI * 3) * 0.07,
0.11
);
pulse.scale.setScalar(0.7 + Math.sin(progress * Math.PI) * 0.5);
pulse.visible = currentState.active;
});
const lampPulse =
1 + Math.sin(elapsed * 4.5) * (currentState.active ? 0.045 : 0.015);
outputLamp.scale.setScalar(lampPulse);
outputRing.rotation.z = elapsed * 0.35;
thresholdGate.rotation.x = elapsed * 0.35;
thresholdGate.scale.setScalar(
currentState.active ? 1 + Math.sin(elapsed * 4) * 0.035 : 1
);
pinkLight.intensity = 3.4 + Math.sin(elapsed * 1.5) * 0.5;
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
// -------------------------------------------------------------------------
// Resize handling
// -------------------------------------------------------------------------
function onResize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", onResize);
</script>
</body>
</html>
```