move to float3 (to prep for SIMD)
This commit is contained in:
parent
3a67652578
commit
5f6ec71c7b
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user