Three.js Spiking Neural Network (Leaky Integrate-and-Fire)
3D spiking neural network in Three.js built from leaky integrate-and-fire neurons, with excitatory and inhibitory cells, distance-based wiring and action potentials traveling along axons at finite conduction speed.
Key topics: three.js neural network, spiking neurons, integrate and fire, neuroscience, action potential, brain simulation
Game Over
What this code does
- LIF Dynamics: τ dv/dt = −v + I + noise, and a neuron spikes at threshold 1, resets to 0 and goes briefly refractory.
- Excitation & Inhibition: 80% of neurons push their targets toward firing (blue), and 20% hold them back (red).
- Conduction Delays: each spike spawns pulses that travel along synapses at a set speed, so distance means delay.
- Network Wiring: connection probability falls off with distance inside clusters, plus sparse long-range links between them.
- Balanced Regime: the drive sits just below threshold, so activity comes from noise and recurrent input, as in cortex.
- Controls: neuron count, inhibitory fraction, connection radius, conduction speed, drive, noise, synaptic weights and τ.
Related Demos
Continue Learning
JavaScript Implementation
// Leaky integrate-and-fire neurons: tau·dv/dt = −v + I, spike at v ≥ 1, then reset
const neurons = Array.from({ length: 200 }, () => ({
p: new THREE.Vector3().randomDirection().multiplyScalar(Math.random() * 8),
v: Math.random(),
out: []
}))
neurons.forEach((a, i) => neurons.forEach((b, j) => {
if (i !== j && a.p.distanceTo(b.p) < 3 && Math.random() < 0.2) a.out.push(j)
}))
const somas = new THREE.InstancedMesh(new THREE.SphereGeometry(0.2), new THREE.MeshBasicMaterial(), neurons.length)
neurons.forEach((n, i) => somas.setMatrixAt(i, new THREE.Matrix4().makeTranslation(n.p.x, n.p.y, n.p.z)))
scene.add(somas)
const color = new THREE.Color()
function step (dt) {
neurons.forEach((n, i) => {
n.v += (dt / 0.02) * (-n.v + 1.05 + (Math.random() - 0.5) * 0.8)
n.glow = Math.max(0, (n.glow || 0) - dt * 4)
if (n.v >= 1) {
n.v = 0
n.glow = 1
n.out.forEach(j => { neurons[j].v += 0.1 }) // instant excitatory kick
}
somas.setColorAt(i, color.setRGB(0.05 + n.glow, 0.3 + n.glow, 0.45 + n.glow))
})
somas.instanceColor.needsUpdate = true
}