It runs without errors (not working functionaltiy yet)

This commit is contained in:
Pascal Serrarens 2026-01-29 11:30:33 +01:00
parent e7dd1e50cb
commit 5aedb30d36
9 changed files with 46 additions and 76 deletions

View File

@ -7,7 +7,8 @@ using static Unity.Mathematics.math;
[System.Serializable] [System.Serializable]
public class Cluster : INucleus { public class Cluster : INucleus {
// The ScriptableObject asset from which the runtime object has been created // The ScriptableObject asset from which the runtime object has been created
public readonly ClusterPrefab prefab; [SerializeField]
public ClusterPrefab prefab;
public ClusterPrefab cluster { get; set; } public ClusterPrefab cluster { get; set; }
@ -18,9 +19,18 @@ public class Cluster : INucleus {
set => _name = value; set => _name = value;
} }
public Cluster(ClusterPrefab prefab) {
this.prefab = UnityEngine.Object.Instantiate(prefab);
this.name = prefab.name;
this.cluster = null;
if (this.cluster != null)
this.cluster.nuclei.Add(this);
}
// Hmm, a cluster instance can never be part of a scriptable object...(Cluster) // Hmm, a cluster instance can never be part of a scriptable object...(Cluster)
public Cluster(ClusterPrefab parent, ClusterPrefab prefab) { public Cluster(ClusterPrefab parent, ClusterPrefab prefab) {
this.prefab = prefab; this.prefab = prefab.Clone(); //UnityEngine.Object.Instantiate(prefab);
this.name = prefab.name;
this.cluster = parent; this.cluster = parent;
if (this.cluster != null) if (this.cluster != null)
this.cluster.nuclei.Add(this); this.cluster.nuclei.Add(this);
@ -46,6 +56,8 @@ public class Cluster : INucleus {
return clone; return clone;
} }
public INucleus output => prefab.output;
// Not sure if this belongs here... // Not sure if this belongs here...
[SerializeReference] [SerializeReference]
private NucleusArray _array; private NucleusArray _array;

View File

@ -33,6 +33,12 @@ public class ClusterPrefab : ScriptableObject {
} }
} }
public virtual ClusterPrefab Clone() {
ClusterPrefab clone = Instantiate(this);
return clone;
}
// Call this function to ensure that there is at least one nucleus // Call this function to ensure that there is at least one nucleus
// This is an invariant and should be ensured before the nucleus is used // This is an invariant and should be ensured before the nucleus is used
// because output requires it. // because output requires it.

View File

@ -5,7 +5,7 @@ using static Unity.Mathematics.math;
public class Receptor : IReceptor { public class Receptor : IReceptor {
private ClusterPrefab cluster; private Cluster cluster;
[SerializeField] [SerializeField]
protected string _name; protected string _name;
@ -14,25 +14,25 @@ public class Receptor : IReceptor {
set => _name = value; set => _name = value;
} }
public Receptor(ClusterPrefab cluster) { public Receptor(Cluster cluster) {
this.cluster = cluster; this.cluster = cluster;
if (cluster != null) if (cluster != null)
cluster.nuclei.Add(this); cluster.prefab.nuclei.Add(this);
} }
public Receptor(ClusterPrefab cluster, INucleus nucleus) { public Receptor(Cluster cluster, INucleus nucleus) {
this.cluster = cluster; this.cluster = cluster;
if (cluster != null) if (cluster != null)
cluster.nuclei.Add(this); cluster.prefab.nuclei.Add(this);
this.AddReceiver(nucleus); this.AddReceiver(nucleus);
} }
public static Receptor CreateReceptor(ClusterPrefab cluster, string nucleusName) { public static Receptor CreateReceptor(Cluster cluster, string nucleusName) {
if (cluster == null) if (cluster == null)
return null; return null;
Receptor receptor = new(cluster); Receptor receptor = new(cluster);
foreach (INucleus nucleus in cluster.inputs) { foreach (INucleus nucleus in cluster.prefab.inputs) {
if (nucleus != null && nucleus.name == nucleusName) { if (nucleus != null && nucleus.name == nucleusName) {
// Receptor receptor = new(cluster, nucleus); // Receptor receptor = new(cluster, nucleus);
// return receptor; // return receptor;
@ -45,7 +45,7 @@ public class Receptor : IReceptor {
return receptor; return receptor;
} }
public virtual IReceptor CloneTo(ClusterPrefab parent) { public virtual IReceptor CloneTo(Cluster parent) {
Receptor clone = new(parent); Receptor clone = new(parent);
foreach (INucleus receiver in this.receivers) { foreach (INucleus receiver in this.receivers) {

View File

@ -11,26 +11,20 @@ MonoBehaviour:
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 60a957541c24c57e78018c202ebb1d9b, type: 3} m_Script: {fileID: 11500000, guid: 60a957541c24c57e78018c202ebb1d9b, type: 3}
m_Name: Velocity m_Name: Velocity
m_EditorClassIdentifier: Assembly-CSharp::Cluster m_EditorClassIdentifier: Assembly-CSharp::ClusterPrefab
nuclei: nuclei:
- rid: 2243601403683012671 - rid: 2243601420268863545
- rid: 2243601403683012676
references: references:
version: 2 version: 2
RefIds: RefIds:
- rid: -2 - rid: 2243601420268863545
type: {class: , ns: , asm: }
- rid: 2243601403683012671
type: {class: Neuron, ns: , asm: Assembly-CSharp} type: {class: Neuron, ns: , asm: Assembly-CSharp}
data: data:
_name: Output _name: Output
_synapses: _synapses: []
- nucleus:
rid: -2
weight: 1
_receivers: [] _receivers: []
_array: _array:
rid: 2243601403683012672 rid: 2243601420268863546
_curvePreset: 0 _curvePreset: 0
curve: curve:
serializedVersion: 2 serializedVersion: 2
@ -58,51 +52,9 @@ MonoBehaviour:
m_RotationOrder: 4 m_RotationOrder: 4
curveMax: 1 curveMax: 1
average: 0 average: 0
- rid: 2243601403683012672 - rid: 2243601420268863546
type: {class: NucleusArray, ns: , asm: Assembly-CSharp} type: {class: NucleusArray, ns: , asm: Assembly-CSharp}
data: data:
_nuclei: _nuclei:
- rid: 2243601403683012671 - rid: 2243601420268863545
name: Output name: Output
- rid: 2243601403683012676
type: {class: Neuron, ns: , asm: Assembly-CSharp}
data:
_name: Position
_synapses: []
_receivers:
- rid: 2243601403683012671
_array:
rid: 2243601403683012677
_curvePreset: 0
curve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 1
tangentMode: 0
weightedMode: 0
inWeight: 0
outWeight: 0
- serializedVersion: 3
time: 1000
value: 1000
inSlope: 1
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
curveMax: 1
average: 0
- rid: 2243601403683012677
type: {class: NucleusArray, ns: , asm: Assembly-CSharp}
data:
_nuclei:
- rid: 2243601403683012676
name: New neuron

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: dd622ac7ed09e70ea8edac595047ac82 guid: c61aecac62c26de4aaefb2612bcc9a5d
NativeFormatImporter: NativeFormatImporter:
externalObjects: {} externalObjects: {}
mainObjectFileID: 11400000 mainObjectFileID: 11400000

View File

@ -3,14 +3,14 @@ using UnityEngine;
using System; using System;
using System.Linq; using System.Linq;
public class BrainPickerWindow : EditorWindow { public class ClusterPickerWindow : EditorWindow {
private Vector2 scroll; private Vector2 scroll;
private ClusterPrefab[] items = new ClusterPrefab[0]; private ClusterPrefab[] items = new ClusterPrefab[0];
private Action<ClusterPrefab> onPicked; private Action<ClusterPrefab> onPicked;
private string search = ""; private string search = "";
public static void ShowPicker(Action<ClusterPrefab> onPicked, string title = "Select Cluster") { public static void ShowPicker(Action<ClusterPrefab> onPicked, string title = "Select Cluster") {
var w = CreateInstance<BrainPickerWindow>(); var w = CreateInstance<ClusterPickerWindow>();
w.titleContent = new GUIContent(title); w.titleContent = new GUIContent(title);
w.minSize = new Vector2(360, 320); w.minSize = new Vector2(360, 320);
w.onPicked = onPicked; w.onPicked = onPicked;
@ -21,7 +21,7 @@ public class BrainPickerWindow : EditorWindow {
private void OnEnable() => RefreshList(); private void OnEnable() => RefreshList();
private void RefreshList() { private void RefreshList() {
var guids = AssetDatabase.FindAssets("t:Cluster"); var guids = AssetDatabase.FindAssets("t:ClusterPrefab");
items = guids items = guids
.Select(g => AssetDatabase.LoadAssetAtPath<ClusterPrefab>(AssetDatabase.GUIDToAssetPath(g))) .Select(g => AssetDatabase.LoadAssetAtPath<ClusterPrefab>(AssetDatabase.GUIDToAssetPath(g)))
.Where(b => b != null) .Where(b => b != null)

View File

@ -617,7 +617,7 @@ public class ClusterInspector : Editor {
} }
protected virtual void AddCluster(INucleus nucleus) { protected virtual void AddCluster(INucleus nucleus) {
BrainPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster"); ClusterPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster");
} }
private void OnClusterPicked(INucleus nucleus, ClusterPrefab subCluster) { private void OnClusterPicked(INucleus nucleus, ClusterPrefab subCluster) {

View File

@ -23,7 +23,7 @@ public class NanoBrainComponent_Editor : Editor {
public override VisualElement CreateInspectorGUI() { public override VisualElement CreateInspectorGUI() {
//NanoBrainComponent component = target as NanoBrainComponent; //NanoBrainComponent component = target as NanoBrainComponent;
ClusterPrefab brain = Application.isPlaying ? component.brain : component.defaultBrain; ClusterPrefab brain = Application.isPlaying ? component.brain.prefab : component.defaultBrain;
if (Application.isPlaying == false) if (Application.isPlaying == false)
serializedObject.Update(); serializedObject.Update();

View File

@ -2,13 +2,13 @@ using UnityEngine;
public class NanoBrainComponent : MonoBehaviour { public class NanoBrainComponent : MonoBehaviour {
public ClusterPrefab defaultBrain; public ClusterPrefab defaultBrain;
private ClusterPrefab brainInstance; private Cluster brainInstance;
public INucleus root => brainInstance.output; //public INucleus root => brainInstance.output;
public ClusterPrefab brain { public Cluster brain {
get { get {
if (brainInstance == null && defaultBrain != null) { if (brainInstance == null && defaultBrain != null) {
brainInstance = Instantiate(defaultBrain); brainInstance = new Cluster(defaultBrain); //Instantiate(defaultBrain);
brainInstance.name = defaultBrain.name + " (Instance)"; brainInstance.name = defaultBrain.name + " (Instance)";
SwarmControl sc = FindFirstObjectByType<SwarmControl>(); SwarmControl sc = FindFirstObjectByType<SwarmControl>();
@ -23,7 +23,7 @@ public class NanoBrainComponent : MonoBehaviour {
} }
} }
public static void UpdateWeight(ClusterPrefab brain, string name, float weight) { public static void UpdateWeight(Cluster brain, string name, float weight) {
INucleus root = brain.output; INucleus root = brain.output;
foreach (Synapse synapse in root.synapses) { foreach (Synapse synapse in root.synapses) {
if (synapse.nucleus.name == name) { if (synapse.nucleus.name == name) {