neuronal / web /particles.js
amogaddy's picture
amogaddyofficial
claude sonnet 4.6
Neuronal
d601f7c
Raw History Blame Contribute Delete
3.15 kB
import * as THREE from 'three';
// Particelle di luce che viaggiano lungo le sinapsi: ognuna porta un messaggio da un neurone a un altro.
export class Particles {
constructor(scene, sprite, cap = 8000) {
this.cap = cap;
this.n = 0;
this.from = new Float32Array(cap * 3);
this.to = new Float32Array(cap * 3);
this.rgb = new Float32Array(cap * 3);
this.t = new Float32Array(cap);
this.speed = new Float32Array(cap);
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(new Float32Array(cap * 3), 3).setUsage(THREE.DynamicDrawUsage));
geo.setAttribute('color', new THREE.BufferAttribute(new Float32Array(cap * 3), 3).setUsage(THREE.DynamicDrawUsage));
geo.setDrawRange(0, 0);
this.points = new THREE.Points(geo, new THREE.PointsMaterial({
size: 0.1, map: sprite, vertexColors: true, transparent: true,
blending: THREE.AdditiveBlending, depthWrite: false, depthTest: false }));
this.points.frustumCulled = false;
this.points.renderOrder = 3;
scene.add(this.points);
}
// a, b: [x, y, z]; color: THREE.Color; strength 0..1; dur in secondi
spawn(a, b, color, strength = 1, dur = 0.7) {
if (this.n >= this.cap) return;
const i = this.n++;
this.from.set(a, i * 3);
this.to.set(b, i * 3);
const k = 0.35 + 0.65 * strength;
this.rgb[i * 3] = color.r * k; this.rgb[i * 3 + 1] = color.g * k; this.rgb[i * 3 + 2] = color.b * k;
this.t[i] = 0;
this.speed[i] = 1 / (dur * (0.85 + Math.random() * 0.3));
}
clear() { this.n = 0; this.points.geometry.setDrawRange(0, 0); }
update(dt) {
const p = this.points.geometry.attributes.position.array;
const c = this.points.geometry.attributes.color.array;
for (let i = 0; i < this.n; i++) {
this.t[i] += dt * this.speed[i];
if (this.t[i] >= 1) { // arrivata: prende il posto dell'ultima
const last = --this.n;
if (i !== last) {
this.from.copyWithin(i * 3, last * 3, last * 3 + 3);
this.to.copyWithin(i * 3, last * 3, last * 3 + 3);
this.rgb.copyWithin(i * 3, last * 3, last * 3 + 3);
this.t[i] = this.t[last]; this.speed[i] = this.speed[last];
}
i--;
continue;
}
const t = this.t[i], e = t * t * (3 - 2 * t); // parte lenta, accelera, frena all'arrivo
const ax = this.from[i * 3], ay = this.from[i * 3 + 1], az = this.from[i * 3 + 2];
const bx = this.to[i * 3], by = this.to[i * 3 + 1], bz = this.to[i * 3 + 2];
const dist = Math.hypot(bx - ax, by - ay, bz - az);
p[i * 3] = ax + (bx - ax) * e;
p[i * 3 + 1] = ay + (by - ay) * e + Math.sin(Math.PI * e) * dist * 0.18; // arco
p[i * 3 + 2] = az + (bz - az) * e;
const fade = Math.sin(Math.PI * t);
c[i * 3] = this.rgb[i * 3] * fade; c[i * 3 + 1] = this.rgb[i * 3 + 1] * fade; c[i * 3 + 2] = this.rgb[i * 3 + 2] * fade;
}
this.points.geometry.attributes.position.needsUpdate = true;
this.points.geometry.attributes.color.needsUpdate = true;
this.points.geometry.setDrawRange(0, this.n);
return this.n;
}
}