Updating swarm weights

This commit is contained in:
Pascal Serrarens 2026-01-05 13:04:53 +01:00
parent a1d1f7ba24
commit c86b01ae57
9 changed files with 83 additions and 10 deletions

View File

@ -52,6 +52,7 @@
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/NanoBrainEditor.cs" />
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs" />
<Compile Include="Assets/NanoBrain/Editor/NanoBrain_Editor.cs" />
<Compile Include="Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs" />
<Compile Include="Assets/NanoBrain/Editor/NeuroidWindow.cs" />
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/NanoBrainComponent_Editor.cs" />
</ItemGroup>

View File

@ -8,15 +8,18 @@ public class NanoBrainComponent_Editor : Editor {
protected static VisualElement mainContainer;
protected static VisualElement inspectorContainer;
protected NanoBrainComponent component;
private SerializedProperty brainProp;
public void OnEnable() {
component = target as NanoBrainComponent;
if (Application.isPlaying == false)
brainProp = serializedObject.FindProperty(nameof(NanoBrainComponent.defaultBrain));
}
public override VisualElement CreateInspectorGUI() {
NanoBrainComponent component = target as NanoBrainComponent;
//NanoBrainComponent component = target as NanoBrainComponent;
NanoBrainObj brain = Application.isPlaying ? component.brain : component.defaultBrain;
if (Application.isPlaying == false)
@ -35,8 +38,9 @@ public class NanoBrainComponent_Editor : Editor {
root.styleSheets.Add(Resources.Load<StyleSheet>("GraphStyles"));
if (Application.isPlaying == false) {
PropertyField brainField = new(brainProp);
brainField.label = "Nano Brain";
PropertyField brainField = new(brainProp) {
label = "Nano Brain"
};
root.Add(brainField);
}

View File

@ -395,8 +395,10 @@ public class NanoBrainInspector : Editor {
if (so.targetObject == null)
return;
so.Update();
if (this.currentNucleus == null)
return;
this.currentNucleus.name = EditorGUILayout.TextField(this.currentNucleus.name);
if (this.currentNucleus is Perceptoid currentPerceptoid)
currentPerceptoid.thingType = EditorGUILayout.IntField("Thing Type", currentPerceptoid.thingType);

View File

@ -70,7 +70,7 @@ public class NanoBrainObj : ScriptableObject, ISerializationCallbackReceiver {
public void GarbageCollection() {
HashSet<Nucleus> visitedNuclei = new();
MarkNuclei(visitedNuclei, this.root);
Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
//Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
this.nuclei.RemoveAll(nucleus => visitedNuclei.Contains(nucleus) == false);
this.perceptei.RemoveAll(perceptoid => visitedNuclei.Contains(perceptoid) == false);
}

View File

@ -221,6 +221,7 @@ MeshRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_MaskInteraction: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &231291189
MeshFilter:
@ -332,6 +333,7 @@ MeshRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_MaskInteraction: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &246180157
MeshFilter:
@ -375,7 +377,7 @@ MonoBehaviour:
inertia: 0.8
alignmentForce: 2
cohesionForce: 2
avoidanceForce: 1
avoidanceForce: 100
separationDistance: 0.3
perceptionDistance: 2
boundaryForce: 5
@ -515,6 +517,7 @@ MeshRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_MaskInteraction: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &704151346
MeshFilter:
@ -550,14 +553,14 @@ Light:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 837238090}
m_Enabled: 1
serializedVersion: 11
serializedVersion: 12
m_Type: 1
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
m_Intensity: 1
m_Range: 10
m_SpotAngle: 30
m_InnerSpotAngle: 21.80208
m_CookieSize: 10
m_CookieSize2D: {x: 10, y: 10}
m_Shadows:
m_Type: 2
m_Resolution: -1
@ -845,6 +848,7 @@ MeshRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_MaskInteraction: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &1690670940
MeshFilter:
@ -956,6 +960,7 @@ MeshRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_MaskInteraction: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &1738679981
MeshFilter:
@ -1104,6 +1109,7 @@ MeshRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_MaskInteraction: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &1924016464
MeshFilter:

View File

@ -21,7 +21,7 @@ MonoBehaviour:
_name: Root
synapses:
- nucleusId: -112538112
weight: 1
weight: 10
curvePreset: 0
curve:
serializedVersion: 2
@ -80,11 +80,29 @@ MonoBehaviour:
_curvePreset: 0
curve:
serializedVersion: 2
m_Curve: []
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 1
tangentMode: 0
weightedMode: 0
inWeight: 0
outWeight: 0
- serializedVersion: 3
time: 1
value: 1
inSlope: 1
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
curveMax: 0
curveMax: 1
average: 0
inverse: 0
exponent: 1

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2b86e8b28533d905899af9666a98e804
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,32 @@
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(SwarmControl))]
public class SwarmControl_Editor : Editor {
public override void OnInspectorGUI() {
EditorGUI.BeginChangeCheck();
DrawDefaultInspector();
if (EditorGUI.EndChangeCheck()) {
SwarmControl swarmControl = (SwarmControl)target;
NanoBrainObj[] nanoBrains = FindObjectsByType<NanoBrainObj>(FindObjectsSortMode.None);
foreach (NanoBrainObj brain in nanoBrains) {
UpdateWeight(brain, "Avoidance", swarmControl.avoidanceForce);
}
//}
Debug.Log("Updated weights");
}
}
protected void UpdateWeight(NanoBrainObj brain, string name, float weight) {
Nucleus root = brain.root;
foreach (Synapse synapse in root.synapses) {
if (synapse.nucleus.name == name) {
synapse.weight = weight;
}
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: df2bd18155543a1a09fefea3252981e0