From c86b01ae571b223f666c4f5a14490e28036b4f0e Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 5 Jan 2026 13:04:53 +0100 Subject: [PATCH] Updating swarm weights --- Assembly-CSharp-Editor.csproj | 1 + .../Editor/NanoBrainComponent_Editor.cs | 10 ++++-- .../VisualEditor/Editor/NanoBrainInspector.cs | 2 ++ Assets/NanoBrain/VisualEditor/NanoBrainObj.cs | 2 +- Assets/Scenes/Boids/Boids.unity | 12 +++++-- Assets/Scenes/Boids/RoamingBrain.asset | 24 ++++++++++++-- Assets/Scenes/Boids/Scripts/Editor.meta | 8 +++++ .../Scripts/Editor/SwarmControl_Editor.cs | 32 +++++++++++++++++++ .../Editor/SwarmControl_Editor.cs.meta | 2 ++ 9 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 Assets/Scenes/Boids/Scripts/Editor.meta create mode 100644 Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs create mode 100644 Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs.meta diff --git a/Assembly-CSharp-Editor.csproj b/Assembly-CSharp-Editor.csproj index 99bbb27..010371d 100644 --- a/Assembly-CSharp-Editor.csproj +++ b/Assembly-CSharp-Editor.csproj @@ -52,6 +52,7 @@ + diff --git a/Assets/NanoBrain/VisualEditor/Editor/NanoBrainComponent_Editor.cs b/Assets/NanoBrain/VisualEditor/Editor/NanoBrainComponent_Editor.cs index 895092f..7fdec1b 100644 --- a/Assets/NanoBrain/VisualEditor/Editor/NanoBrainComponent_Editor.cs +++ b/Assets/NanoBrain/VisualEditor/Editor/NanoBrainComponent_Editor.cs @@ -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("GraphStyles")); if (Application.isPlaying == false) { - PropertyField brainField = new(brainProp); - brainField.label = "Nano Brain"; + PropertyField brainField = new(brainProp) { + label = "Nano Brain" + }; root.Add(brainField); } diff --git a/Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs b/Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs index 8db7231..705aa68 100644 --- a/Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs +++ b/Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs @@ -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); diff --git a/Assets/NanoBrain/VisualEditor/NanoBrainObj.cs b/Assets/NanoBrain/VisualEditor/NanoBrainObj.cs index 145e14d..096ff4b 100644 --- a/Assets/NanoBrain/VisualEditor/NanoBrainObj.cs +++ b/Assets/NanoBrain/VisualEditor/NanoBrainObj.cs @@ -70,7 +70,7 @@ public class NanoBrainObj : ScriptableObject, ISerializationCallbackReceiver { public void GarbageCollection() { HashSet 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); } diff --git a/Assets/Scenes/Boids/Boids.unity b/Assets/Scenes/Boids/Boids.unity index 9df0ef9..955eb8d 100644 --- a/Assets/Scenes/Boids/Boids.unity +++ b/Assets/Scenes/Boids/Boids.unity @@ -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: diff --git a/Assets/Scenes/Boids/RoamingBrain.asset b/Assets/Scenes/Boids/RoamingBrain.asset index 2ca9ea7..0771c4d 100644 --- a/Assets/Scenes/Boids/RoamingBrain.asset +++ b/Assets/Scenes/Boids/RoamingBrain.asset @@ -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 diff --git a/Assets/Scenes/Boids/Scripts/Editor.meta b/Assets/Scenes/Boids/Scripts/Editor.meta new file mode 100644 index 0000000..ef97f40 --- /dev/null +++ b/Assets/Scenes/Boids/Scripts/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2b86e8b28533d905899af9666a98e804 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs b/Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs new file mode 100644 index 0000000..5feb192 --- /dev/null +++ b/Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs @@ -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(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; + } + } + } +} \ No newline at end of file diff --git a/Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs.meta b/Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs.meta new file mode 100644 index 0000000..51eae02 --- /dev/null +++ b/Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: df2bd18155543a1a09fefea3252981e0 \ No newline at end of file