import { Canvas, useFrame } from "@react-three/fiber"; import { useMemo, useRef } from "react"; import { BufferAttribute, Color, type Points } from "three"; import { NEURON_COUNT, NEURON_POSITIONS, neuronGroup, type NeuronGroup } from "./flyBrain"; import type { LiveFlyPositions } from "./useLiveFlyState"; const GROUP_COLOR: Record = { sensory: new Color("#22d3ee"), hidden: new Color("#8e56fc"), motor: new Color("#f97316"), }; const BASE_INTENSITY = 0.45; // dim baseline so idle points stay clearly visible interface CloudProps { selectedFlyId: string | null; livePositionsRef: React.RefObject; } // One THREE.Points draw call, 34 vertices, no lights (self-illuminated via // vertex color) — cheap enough for its own small second WebGL context. function PointCloud({ selectedFlyId, livePositionsRef }: CloudProps) { const pointsRef = useRef(null); const colorArray = useMemo(() => new Float32Array(NEURON_COUNT * 3), []); useFrame((_, dt) => { const points = pointsRef.current; if (!points) return; points.rotation.y += dt * 0.15; const brain = selectedFlyId ? livePositionsRef.current.get(selectedFlyId)?.brain : undefined; const colorAttr = points.geometry.getAttribute("color") as BufferAttribute; for (let i = 0; i < NEURON_COUNT; i++) { const base = GROUP_COLOR[neuronGroup(i)]; const level = BASE_INTENSITY + (brain ? brain.brightness[i] * (1 - BASE_INTENSITY) : 0); colorAttr.setXYZ(i, base.r * level, base.g * level, base.b * level); } colorAttr.needsUpdate = true; }); return ( ); } export default function BrainActivity({ selectedFlyId, livePositionsRef }: CloudProps) { return (
{selectedFlyId ? "neural activity" : "select a fly to see its activity"}
{/* Without this the WebGL clear defaults to opaque black, which hid the surrounding div's background and made the panel look like a bottomless black hole rather than a dark, but visibly bounded, viewport. */}
); }