From 5f6ec71c7b05db442f7e1885e22e4630dc059346 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 12 Jan 2026 17:45:46 +0100 Subject: [PATCH] move to float3 (to prep for SIMD) --- Assets/NanoBrain/Cluster.cs | 6 ++++-- Assets/NanoBrain/INucleus.cs | 9 +++++++-- Assets/NanoBrain/Neuroid.cs | 20 ++++++++++--------- Assets/NanoBrain/Nucleus.cs | 6 ++++-- Assets/NanoBrain/Receptor.cs | 3 ++- .../VisualEditor/Editor/ClusterInspector.cs | 14 +++++++------ 6 files changed, 36 insertions(+), 22 deletions(-) diff --git a/Assets/NanoBrain/Cluster.cs b/Assets/NanoBrain/Cluster.cs index 54b9c92..0cf80dc 100644 --- a/Assets/NanoBrain/Cluster.cs +++ b/Assets/NanoBrain/Cluster.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; using UnityEngine; +using Unity.Mathematics; +using static Unity.Mathematics.math; [CreateAssetMenu(menuName = "Passer/Cluster")] public class Cluster : ScriptableObject, INucleus { @@ -93,8 +95,8 @@ public class Cluster : ScriptableObject, INucleus { #region Dynamics - public Vector3 outputValue => this.output.outputValue; - public bool isSleeping => this.outputValue.sqrMagnitude == 0; + public float3 outputValue => this.output.outputValue; + public bool isSleeping => lengthsq(this.outputValue) == 0; public void UpdateState() { // Don't know if this is right diff --git a/Assets/NanoBrain/INucleus.cs b/Assets/NanoBrain/INucleus.cs index dedd500..c565c3e 100644 --- a/Assets/NanoBrain/INucleus.cs +++ b/Assets/NanoBrain/INucleus.cs @@ -1,5 +1,8 @@ using System.Collections.Generic; using UnityEngine; +using Unity.Burst; +using Unity.Collections; +using Unity.Mathematics; public interface INucleus : IReceptor { @@ -41,9 +44,11 @@ public interface IReceptor { #region dynamic - public Vector3 outputValue { get; } + // float3 to prepare for SIMD + public float3 outputValue { get; } public bool isSleeping { get; } #endregion dynamic -} \ No newline at end of file +} + diff --git a/Assets/NanoBrain/Neuroid.cs b/Assets/NanoBrain/Neuroid.cs index b8e14d3..a28583a 100644 --- a/Assets/NanoBrain/Neuroid.cs +++ b/Assets/NanoBrain/Neuroid.cs @@ -1,4 +1,6 @@ using UnityEngine; +using Unity.Mathematics; +using static Unity.Mathematics.math; [System.Serializable] public class Neuroid : Nucleus { @@ -39,13 +41,13 @@ public class Neuroid : Nucleus { } public override void UpdateState() { - Vector3 sum = Vector3.zero; + float3 sum = new(0, 0, 0); int n = 0; - + //Applying the weight factgors foreach (Synapse synapse in this.synapses) { - sum += synapse.weight * synapse.nucleus.outputValue; - if (synapse.nucleus.outputValue.sqrMagnitude != 0) + sum = sum + (synapse.weight * synapse.nucleus.outputValue); + if (lengthsq(synapse.nucleus.outputValue) != 0) n++; } if (average) @@ -58,17 +60,17 @@ public class Neuroid : Nucleus { result = sum; break; case CurvePresets.Sqrt: - result = sum.normalized * System.MathF.Sqrt(sum.magnitude); + result = normalize(sum) * System.MathF.Sqrt(length(sum)); break; case CurvePresets.Power: - result = sum.normalized * System.MathF.Pow(sum.magnitude, 2); + result = normalize(sum) * System.MathF.Pow(length(sum), 2); break; case CurvePresets.Reciprocal: - result = sum.normalized * (1 / sum.magnitude); + result = normalize(sum) * (1 / length(sum)); break; default: - float activatedValue = this.curve.Evaluate(sum.magnitude); - result = sum.normalized * activatedValue; + float activatedValue = this.curve.Evaluate(length(sum)); + result = normalize(sum) * activatedValue; break; } UpdateResult(result); diff --git a/Assets/NanoBrain/Nucleus.cs b/Assets/NanoBrain/Nucleus.cs index 9c72fa1..65a30c7 100644 --- a/Assets/NanoBrain/Nucleus.cs +++ b/Assets/NanoBrain/Nucleus.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using UnityEngine; +using Unity.Mathematics; +using static Unity.Mathematics.math; [Serializable] public class Nucleus : INucleus { @@ -76,8 +78,8 @@ public class Nucleus : INucleus { public Cluster cluster { get; set; } - private Vector3 _outputValue; - public Vector3 outputValue { + private float3 _outputValue; + public float3 outputValue { get { return _outputValue; } set { this.stale = 0; diff --git a/Assets/NanoBrain/Receptor.cs b/Assets/NanoBrain/Receptor.cs index 403367d..fe47dfb 100644 --- a/Assets/NanoBrain/Receptor.cs +++ b/Assets/NanoBrain/Receptor.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using UnityEngine; +using Unity.Mathematics; public class Receptor : IReceptor { [SerializeField] @@ -29,7 +30,7 @@ public class Receptor : IReceptor { public float distanceResolution = 0.1f; public float directionResolution = 5; - public Vector3 outputValue => this.localPosition; + public float3 outputValue => this.localPosition; public Receptor(Cluster cluster, INucleus nucleus) { this.AddReceiver(nucleus); diff --git a/Assets/NanoBrain/VisualEditor/Editor/ClusterInspector.cs b/Assets/NanoBrain/VisualEditor/Editor/ClusterInspector.cs index e9ee84a..c3744e6 100644 --- a/Assets/NanoBrain/VisualEditor/Editor/ClusterInspector.cs +++ b/Assets/NanoBrain/VisualEditor/Editor/ClusterInspector.cs @@ -4,6 +4,8 @@ using UnityEditor; using UnityEngine; using UnityEngine.UIElements; +using Unity.Mathematics; +using static Unity.Mathematics.math; [CustomEditor(typeof(Cluster))] public class ClusterInspector : Editor { @@ -216,7 +218,7 @@ public class ClusterInspector : Editor { // Draw selected Nucleus Handles.color = Color.white; Handles.DrawSolidDisc(position, Vector3.forward, size + 2); - DrawNucleus(this.currentNucleus, position, this.currentNucleus.outputValue.magnitude, 20); + DrawNucleus(this.currentNucleus, position, length(this.currentNucleus.outputValue), 20); } private void DrawReceivers(INucleus nucleus, Vector3 parentPos, float size) { @@ -227,7 +229,7 @@ public class ClusterInspector : Editor { float maxValue = 0; foreach (INucleus receiver in nucleus.receivers) { if (receiver is Neuroid neuroid) { - float value = neuroid.outputValue.magnitude; + float value = length(neuroid.outputValue); if (value > maxValue) maxValue = value; } @@ -267,7 +269,7 @@ public class ClusterInspector : Editor { drawnArrays.Add(neuroid.array); neuronCount++; - float value = neuroid.outputValue.magnitude; + float value = length(neuroid.outputValue); if (value > maxValue) maxValue = value; } @@ -299,7 +301,7 @@ public class ClusterInspector : Editor { Handles.color = Color.darkRed; else { if (Application.isPlaying) { - float brightness = nucleus.outputValue.magnitude / maxValue; + float brightness = length(nucleus.outputValue) / maxValue; Handles.color = new Color(brightness, brightness, brightness, 1f); } else @@ -416,7 +418,7 @@ public class ClusterInspector : Editor { } if (Application.isPlaying) - EditorGUILayout.FloatField("Output", this.currentNucleus.outputValue.magnitude); + EditorGUILayout.FloatField("Output", length(this.currentNucleus.outputValue)); else EditorGUILayout.LabelField(" "); @@ -428,7 +430,7 @@ public class ClusterInspector : Editor { EditorGUI.BeginDisabledGroup(synapse.nucleus.isSleeping); if (Application.isPlaying) - EditorGUILayout.FloatField(synapse.nucleus.name, synapse.nucleus.outputValue.magnitude * synapse.weight); + EditorGUILayout.FloatField(synapse.nucleus.name, length(synapse.nucleus.outputValue) * synapse.weight); else { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(synapse.nucleus.name);