Brain
src/brain.ts is the bridge between Three.js and the WASM World. It owns the
World instance, builds the geometry bound to WASM memory, and exposes a small
API the rest of the app uses.
Construction
createBrain(assets, renderer, backend) initializes the WASM module and builds
a Brain. The constructor imports the generated module and the wasm binary URL:
import init, { World } from '../rust/brain_sim/pkg/brain_sim'
import wasmUrl from '../rust/brain_sim/pkg/brain_sim_bg.wasm?url'
It mirrors the WASM-side defaults into a JS-side settings object and reads the
live style fields (colors, sizes, opacity) from the World getters, then builds
three things:
- a
ParticlePoolBridgefor the moving signals, - the neuron
Pointsgeometry, - the axon
LineSegmentsgeometry.
All three bind their buffers to WASM memory via typed-array views.
Per-frame update
update(deltaTime: number): void {
this.refreshIfMemoryGrew()
this.world.step_cpu(deltaTime)
this.particlePool.update()
}
refreshIfMemoryGrew() rebinds views if WASM memory grew (see
Overview),
step_cpu advances the simulation and writes positions, and the particle pool
flags its attribute for upload.
Public API
| Member | Purpose |
|---|---|
update(dt) | Advance one frame. |
injectStimulus(opts) | Forward { type, intensity, durationMs } to World::inject_stimulus. |
getTelemetrySnapshot() | Return the typed TelemetrySnapshot. |
updateSettings() | Push all settings, colors, and material uniforms into the World. |
numNeurons / numAxons / numSignals | Live counts (getters). |
travelBackend | Always 'cpu' in the current build. |
meshComponents | The Object3D added to the scene. |
Settings round-trip
updateSettings() writes every setting and color into the World and also
updates the material uniforms and the neuron color buffer:
this.world.set_setting('currentMaxSignals', this.settings.currentMaxSignals)
this.world.set_setting('signalMinSpeed', this.settings.signalMinSpeed)
this.world.set_setting('signalMaxSpeed', this.settings.signalMaxSpeed)
this.world.set_setting('neuronSizeMultiplier', this.neuronSizeMultiplier)
this.world.set_setting('neuronOpacity', this.neuronOpacity)
this.world.set_setting('axonOpacityMultiplier', this.axonOpacityMultiplier)
this.world.set_color('neuronColor', hexStringToInt(this.neuronColor))
this.world.set_color('axonColor', hexStringToInt(this.axonColor))
This is the function every GUI control calls through onChange. See
UI Guide.
ParticlePoolBridge
A class inside brain.ts that manages the signal Points. Its position
attribute views the WASM-owned particle buffer that step_cpu writes each
frame. It re-checks wasm.memory.buffer every frame and rewraps its view when
the buffer changes, then flags needsUpdate so the GPU upload happens. It also
holds the pColor and pSize controls used by the GUI.