Add selector neuron

This commit is contained in:
Pascal Serrarens 2026-02-04 09:44:34 +01:00
parent aab6f59934
commit 811ddd1761
23 changed files with 657 additions and 1312 deletions

View File

@ -9,6 +9,21 @@ using static Unity.Mathematics.math;
[Serializable]
public class Neuron : INucleus {
public Neuron(Cluster parent, string name) {
this.parent = parent;
this.name = name;
this.parent?.nuclei.Add(this);
}
public Neuron(ClusterPrefab parent, string name) {
this.cluster = parent;
this.name = name;
if (this.cluster != null) {
this.cluster.nuclei.Add(this);
}
// else
// Debug.LogError("No neuroid network");
}
[SerializeField]
protected string _name;
public virtual string name {
@ -165,21 +180,6 @@ public class Neuron : INucleus {
#endregion Runtime state
public Neuron(Cluster parent, string name) {
this.parent = parent;
this.name = name;
this.parent?.nuclei.Add(this);
}
public Neuron(ClusterPrefab parent, string name) {
this.cluster = parent;
this.name = name;
if (this.cluster != null) {
this.cluster.nuclei.Add(this);
}
// else
// Debug.LogError("No neuroid network");
}
// this clone the nucleus without the synapses and receivers
public virtual IReceptor ShallowCloneTo(Cluster newParent) {
Neuron clone = new(newParent, this.name) {

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 4f343147e37db9eeda3e98058c553c92
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,365 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!29 &1
OcclusionCullingSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_OcclusionBakeSettings:
smallestOccluder: 5
smallestHole: 0.25
backfaceThreshold: 100
m_SceneGUID: 00000000000000000000000000000000
m_OcclusionCullingData: {fileID: 0}
--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 10
m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3
m_FogDensity: 0.01
m_LinearFogStart: 0
m_LinearFogEnd: 300
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
m_AmbientIntensity: 1
m_AmbientMode: 0
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0}
m_HaloStrength: 0.5
m_FlareStrength: 1
m_FlareFadeSpeed: 3
m_HaloTexture: {fileID: 0}
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0}
m_DefaultReflectionMode: 0
m_DefaultReflectionResolution: 128
m_ReflectionBounces: 1
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 13
m_BakeOnSceneLoad: 0
m_GISettings:
serializedVersion: 2
m_BounceScale: 1
m_IndirectOutputScale: 1
m_AlbedoBoost: 1
m_EnvironmentLightingMode: 0
m_EnableBakedLightmaps: 1
m_EnableRealtimeLightmaps: 0
m_LightmapEditorSettings:
serializedVersion: 12
m_Resolution: 2
m_BakeResolution: 40
m_AtlasSize: 1024
m_AO: 0
m_AOMaxDistance: 1
m_CompAOExponent: 1
m_CompAOExponentDirect: 0
m_ExtractAmbientOcclusion: 0
m_Padding: 2
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
m_TextureCompression: 1
m_ReflectionCompression: 2
m_MixedBakeMode: 2
m_BakeBackend: 2
m_PVRSampling: 1
m_PVRDirectSampleCount: 32
m_PVRSampleCount: 512
m_PVRBounces: 2
m_PVREnvironmentSampleCount: 256
m_PVREnvironmentReferencePointCount: 2048
m_PVRFilteringMode: 1
m_PVRDenoiserTypeDirect: 1
m_PVRDenoiserTypeIndirect: 1
m_PVRDenoiserTypeAO: 1
m_PVRFilterTypeDirect: 0
m_PVRFilterTypeIndirect: 0
m_PVRFilterTypeAO: 0
m_PVREnvironmentMIS: 1
m_PVRCulling: 1
m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 1
m_PVRFilteringGaussRadiusAO: 1
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_ExportTrainingData: 0
m_TrainingDataDestination: TrainingData
m_LightProbeSampleCountMultiplier: 4
m_LightingDataAsset: {fileID: 20201, guid: 0000000000000000f000000000000000, type: 0}
m_LightingSettings: {fileID: 0}
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
agentSlope: 45
agentClimb: 0.4
ledgeDropHeight: 0
maxJumpAcrossDistance: 0
minRegionArea: 2
manualCellSize: 0
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &388118692
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 388118694}
- component: {fileID: 388118693}
m_Layer: 0
m_Name: GameObject
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &388118693
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 388118692}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9051408e82b511584998506096af4bf0, type: 3}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::SelectorBrain
defaultBrain: {fileID: 11400000, guid: d5b3a22d9bb7d13aeb3174077125967b, type: 2}
input1: {x: 0, y: 0, z: 1}
input2: {x: 0, y: -2, z: 0}
output: {x: 0, y: 0, z: 0}
--- !u!4 &388118694
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 388118692}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -2.01476, y: -0, z: 0.65362}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &968074744
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 968074747}
- component: {fileID: 968074746}
- component: {fileID: 968074745}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!81 &968074745
AudioListener:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 968074744}
m_Enabled: 1
--- !u!20 &968074746
Camera:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 968074744}
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 1
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: 0.3
far clip plane: 1000
field of view: 60
orthographic: 0
orthographic size: 5
m_Depth: -1
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 1
m_AllowMSAA: 1
m_AllowDynamicResolution: 0
m_ForceIntoRT: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: 0.022
--- !u!4 &968074747
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 968074744}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2011285159
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2011285161}
- component: {fileID: 2011285160}
m_Layer: 0
m_Name: Directional Light
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!108 &2011285160
Light:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2011285159}
m_Enabled: 1
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_CookieSize2D: {x: 0.5, y: 0.5}
m_Shadows:
m_Type: 2
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1
m_Bias: 0.05
m_NormalBias: 0.4
m_NearPlane: 0.2
m_CullingMatrixOverride:
e00: 1
e01: 0
e02: 0
e03: 0
e10: 0
e11: 1
e12: 0
e13: 0
e20: 0
e21: 0
e22: 1
e23: 0
e30: 0
e31: 0
e32: 0
e33: 1
m_UseCullingMatrixOverride: 0
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingLayerMask: 1
m_Lightmapping: 4
m_LightShadowCasterMode: 0
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0}
m_UseBoundingSphereOverride: 0
m_UseViewFrustumForShadowCasterCull: 1
m_ForceVisible: 0
m_ShadowRadius: 0
m_ShadowAngle: 0
m_LightUnit: 1
m_LuxAtDistance: 1
m_EnableSpotReflector: 1
--- !u!4 &2011285161
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2011285159}
serializedVersion: 2
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
m_LocalPosition: {x: 0, y: 3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 968074747}
- {fileID: 2011285161}
- {fileID: 388118694}

8
Scripts.meta Normal file
View File

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

View File

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

View File

@ -0,0 +1,23 @@
using UnityEngine;
public class SelectorBrain : NanoBrain {
public Vector3 input1;
public Vector3 input2;
public Vector3 output;
public Receptor receptor1;
public Receptor receptor2;
protected void Awake() {
receptor1 = Receptor.CreateReceptor(this.brain, "Input 1");
receptor2 = Receptor.CreateReceptor(this.brain, "Input 2");
}
protected void Update() {
receptor1.ProcessStimulus(0, input1);
receptor2.ProcessStimulus(1, input2);
output = this.brain.outputValue;
this.brain.UpdateNuclei();
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9051408e82b511584998506096af4bf0

138
Selector.asset Normal file
View File

@ -0,0 +1,138 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 60a957541c24c57e78018c202ebb1d9b, type: 3}
m_Name: Selector
m_EditorClassIdentifier: Assembly-CSharp::ClusterPrefab
nuclei:
- rid: 5479437130421501952
- rid: 5479437130421501954
- rid: 5479437130421501956
references:
version: 2
RefIds:
- rid: 5479437130421501952
type: {class: Neuron, ns: , asm: Assembly-CSharp}
data:
_name: Output
_synapses:
- nucleus:
rid: 5479437130421501954
weight: 0.1
- nucleus:
rid: 5479437130421501956
weight: 0.9
_receivers: []
_array:
rid: 5479437130421501953
_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: 1
- rid: 5479437130421501953
type: {class: NucleusArray, ns: , asm: Assembly-CSharp}
data:
_nuclei:
- rid: 5479437130421501952
name: Output
- rid: 5479437130421501954
type: {class: Neuron, ns: , asm: Assembly-CSharp}
data:
_name: Input
_synapses: []
_receivers:
- rid: 5479437130421501952
- rid: 5479437130421501956
_array:
rid: 5479437130421501955
_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: 5479437130421501955
type: {class: NucleusArray, ns: , asm: Assembly-CSharp}
data:
_nuclei:
- rid: 5479437130421501954
name: New neuron
- rid: 5479437130421501956
type: {class: MemoryCell, ns: , asm: Assembly-CSharp}
data:
_name: Previous Input
_synapses:
- nucleus:
rid: 5479437130421501954
weight: 1
_receivers:
- rid: 5479437130421501952
_array:
rid: 5479437130421501957
_curvePreset: 0
curve:
serializedVersion: 2
m_Curve: []
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
curveMax: 1
average: 0
- rid: 5479437130421501957
type: {class: NucleusArray, ns: , asm: Assembly-CSharp}
data:
_nuclei:
- rid: 5479437130421501956
name: New memory cell

8
Selector.asset.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d5b3a22d9bb7d13aeb3174077125967b
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

49
Selector.cs Normal file
View File

@ -0,0 +1,49 @@
using Unity.Mathematics;
using static Unity.Mathematics.math;
public class Selector : Neuron {
public Selector(Cluster parent, string name) : base(parent, name) { }
public override void UpdateStateIsolated(float3 bias) {
float3 max = bias;
float maxSqrLength = lengthsq(max);
//Applying the weight factors
foreach (Synapse synapse in this.synapses) {
float3 input = synapse.weight * synapse.nucleus.outputValue;
float inputSqrlength = lengthsq(input);
if (inputSqrlength > maxSqrLength) {
max = input;
maxSqrLength = inputSqrlength;
}
}
// Activation function
float3 result;
switch (this.curvePreset) {
case CurvePresets.Linear:
result = max;
break;
case CurvePresets.Sqrt:
result = normalize(max) * System.MathF.Sqrt(length(max));
break;
case CurvePresets.Power:
result = normalize(max) * System.MathF.Pow(length(max), 2);
break;
case CurvePresets.Reciprocal: {
float magnitude = length(max);
if (magnitude > 0)
result = normalize(max) * (1 / magnitude);
else
result = float3(0, 0, 0);
break;
}
default:
float activatedValue = this.curve.Evaluate(length(max));
result = normalize(max) * activatedValue;
break;
}
this.outputValue = result;
}
}

2
Selector.cs.meta Normal file
View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4218c2f3f15af944db0eadd6e1500d17

View File

@ -1,511 +0,0 @@
/*
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.Callbacks;
using System.Linq;
using System.Collections.Generic;
public class NucleusLayer {
public int ix = 0;
public List<Nucleus> neuroids = new();
}
public class NanoBrainEditor : EditorWindow {
public NanoBrain brain;
public static VisualElement inspectorContainer;
[MenuItem("Window/NanoBrain Editor")]
public static void ShowWindow() {
GetWindow<NanoBrainEditor>("NanoBrain Editor");
}
public static void Open(NanoBrain asset) {
NanoBrainEditor editor = GetWindow<NanoBrainEditor>("NanoBrain Editor");
editor.brain = asset;
editor.Show();
}
GraphBoardView board;
private void OnEnable() {
OnFocus();
}
private void OnFocus() {
if (brain == null) {
// brain = CreateInstance<NanoBrainObj>();
// EditorUtility.SetDirty(brain);
return;
}
VisualElement root = rootVisualElement;
root.Clear();
root.styleSheets.Add(Resources.Load<StyleSheet>("GraphStyles"));
VisualElement main = new() {
name = "main",
style = {
flexDirection = FlexDirection.Row,
flexGrow = 1
}
};
board = new GraphBoardView();
board.style.flexGrow = 1;
inspectorContainer = new VisualElement {
name = "inspector",
style = {
width = 400
}
};
main.Add(board);
main.Add(inspectorContainer);
root.Add(main);
board.SetGraph(brain, brain.root);
}
}
public class GraphBoardView : VisualElement {
NanoBrain brain;
SerializedObject serializedBrain;
Nucleus currentNucleus;
private List<NeuroidLayer> layers = new();
private Dictionary<Nucleus, Vector2Int> neuroidPositions = new();
Vector2 pan = Vector2.zero;
//float zoom = 1f;
bool draggingCanvas = false;
Vector2 lastMouse;
GraphNodeWrapper currentWrapper;
public GraphBoardView() {
name = "content";
style.flexGrow = 1;
IMGUIContainer imguiContainer = new(OnIMGUI);
imguiContainer.style.position = Position.Absolute;
imguiContainer.style.left = 0; imguiContainer.style.top = 0;
imguiContainer.style.right = 0; imguiContainer.style.bottom = 0;
imguiContainer.pickingMode = PickingMode.Position;
imguiContainer.focusable = true;
Add(imguiContainer);
//RegisterCallback<WheelEvent>(OnWheel);
RegisterCallback<MouseDownEvent>(OnMouseDown);
RegisterCallback<MouseMoveEvent>(OnMouseMove);
RegisterCallback<MouseUpEvent>(OnMouseUp);
}
public void SetGraph(NanoBrain brain, Nucleus nucleus) {
this.brain = brain;
this.serializedBrain = new SerializedObject(brain);
this.currentNucleus = nucleus;
Rebuild();
}
void Rebuild() {
BuildLayers();
if (currentNucleus == null) {
NanoBrainEditor.inspectorContainer.Clear();
return;
}
if (currentWrapper != null)
Object.DestroyImmediate(currentWrapper);
currentWrapper = ScriptableObject.CreateInstance<GraphNodeWrapper>().Init(currentNucleus, brain);
DrawInspector();
}
private void BuildLayers() {
// A temporary list to track what's been added to layers
this.layers = new();
int layerIx = 0;
Nucleus selectedNucleus = this.currentNucleus;
if (selectedNucleus == null)
return;
NeuroidLayer currentLayer = new() { ix = layerIx };
//foreach (Nucleus outputNeuroid in selectedNucleus.receivers) {
foreach (Receiver receiver in selectedNucleus.receivers) {
Nucleus outputNeuroid = receiver.nucleus;
if (outputNeuroid != null) {
AddToLayer(currentLayer, outputNeuroid);
// Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}");
}
}
if (currentLayer.neuroids.Count > 0) {
this.layers.Add(currentLayer);
layerIx++;
currentLayer = new() { ix = layerIx };
}
AddToLayer(currentLayer, selectedNucleus);
this.layers.Add(currentLayer);
// Debug.Log($"layer {layerIx} nucleus {selectedNucleus.name}");
layerIx++;
currentLayer = new() { ix = layerIx };
//foreach (Nucleus input in selectedNucleus.synapses.Keys) {
foreach (Synapse synapse in selectedNucleus.synapses) {
Nucleus input = synapse.nucleus;
AddToLayer(currentLayer, input);
// Debug.Log($"layer {layerIx} nucleus {input.name}");
}
if (currentLayer.neuroids.Count > 0) {
this.layers.Add(currentLayer);
}
}
private void AddToLayer(NeuroidLayer layer, Nucleus nucleus) {
if (nucleus == null)
return;
layer.neuroids.Add(nucleus);
nucleus.layerIx = layer.ix;
// Store its position
Vector2Int neuroidPosition = new(layer.ix, layer.neuroids.Count - 1);
neuroidPositions[nucleus] = neuroidPosition;
}
// basic pan/zoom handling
// void OnWheel(WheelEvent e) {
// if (e.ctrlKey) {
// float delta = -e.delta.y * 0.001f;
// zoom = Mathf.Clamp(zoom + delta, 0.25f, 2f);
// content.transform.rotation = Quaternion.identity; // keep transform accessible
// content.transform.scale = new Vector3(zoom, zoom, 1);
// e.StopPropagation();
// }
// else {
// pan += e.delta;
// content.style.left = pan.x;
// content.style.top = pan.y;
// }
// }
void OnMouseDown(MouseDownEvent e) {
if (e.button == 2) { draggingCanvas = true; lastMouse = e.mousePosition; e.StopPropagation(); }
}
void OnMouseMove(MouseMoveEvent e) {
if (draggingCanvas) {
var delta = e.mousePosition - lastMouse;
pan += delta;
//content.style.left = pan.x;
//content.style.top = pan.y;
lastMouse = e.mousePosition;
}
}
void OnMouseUp(MouseUpEvent e) { if (e.button == 2) draggingCanvas = false; }
void OnIMGUI() {
if (currentNucleus == null)
return;
serializedBrain.Update();
Handles.BeginGUI();
foreach (NeuroidLayer layer in layers)
DrawLayer(layer);
Handles.EndGUI();
}
private void DrawLayer(NeuroidLayer layer) {
int nodeCount = layer.neuroids.Count;
float maxValue = 0;
foreach (Nucleus nucleus in layer.neuroids) {
if (nucleus is Neuroid neuroid) {
float value = neuroid.outputValue.magnitude;
if (value > maxValue)
maxValue = value;
}
}
float spacing = 400f / nodeCount;
float margin = 10 + spacing / 2;
foreach (Nucleus layerNucleus in layer.neuroids) {
Vector2Int layerNeuroidPos = this.neuroidPositions[layerNucleus];
Vector3 parentPos = new(100 + layerNeuroidPos.x * 100, margin + layerNeuroidPos.y * spacing, 0.1f);
//int i = 0;
float inputSpacing = 400f / layerNucleus.synapses.Count;
float inputMargin = 10 + inputSpacing / 2;
// int minStale = 10000;
//foreach ((Nucleus nucleus, float weight) in layerNucleus.synapses) {
foreach (Synapse synapse in layerNucleus.synapses) {
Nucleus nucleus = synapse.nucleus;
if (nucleus != null) {
float weight = synapse.weight;
if (this.neuroidPositions.ContainsKey(nucleus)) {
Vector2Int inputNeuroidPos = this.neuroidPositions[nucleus];
if (inputNeuroidPos.x == layerNeuroidPos.x + 1) {
Vector3 pos = new(100 + inputNeuroidPos.x * 100, inputMargin + inputNeuroidPos.y * inputSpacing, 0.0f);
float brightness = weight / 10.0f;
Handles.color = new Color(brightness, brightness, brightness);
Handles.DrawLine(parentPos, pos);
}
}
// if (nucleus is Neuroid neuroid && neuroid.stale < minStale)
// minStale = neuroid.stale;
}
}
// if (layerNucleus.synapses.Count > 0 && minStale > 2 && layerNucleus.stale < 3)
// Debug.LogWarning($"Strange {minStale} is big duing update");
float size = 20;
if (layerNucleus.isSleeping)
Handles.color = Color.darkRed;
else {
float brightness = layerNucleus.outputValue.magnitude / maxValue;
Handles.color = new Color(brightness, brightness, brightness);
}
Handles.DrawSolidDisc(parentPos, Vector3.forward, size);
Vector3 labelPos = parentPos - Vector3.down * (size + 0.2f); // below disc along up axis
GUIStyle style = new GUIStyle(EditorStyles.label) {
alignment = TextAnchor.UpperCenter,
normal = { textColor = Color.white },
fontStyle = FontStyle.Bold
};
Handles.Label(labelPos, layerNucleus.name, style);
Rect neuronRect = new(parentPos.x - size, parentPos.y - size, size * 2, size * 2);
int id = GUIUtility.GetControlID(FocusType.Passive);
Event e = Event.current;
EventType et = e.GetTypeForControl(id);
if (e != null && neuronRect.Contains(e.mousePosition)) {
// Process Hover
HandleMouseHover(layerNucleus, neuronRect);
// Process click
// Debug.Log($"{et} {e.type}");
if (e.type == EventType.MouseDown && e.button == 0) {
// Consume the event so the scene doesn't also handle it
e.Use();
HandleDiscClicked(layerNucleus);
}
}
}
}
private void HandleMouseHover(Nucleus neuroid, Rect rect) {
GUIContent tooltip;
// if (neuroid is SensoryNeuroid sensoryNeuroid) {
// tooltip = new(
// $"{sensoryNeuroid.name}" +
// $"\nThing {sensoryNeuroid.receptor.thingType}" +
// $"\nValue: {neuroid.outputValue}");
// }
// else {
tooltip = new(
$"{neuroid.name}" +
$"\nsynapse count {neuroid.synapses.Count}" +
$"\nValue: {neuroid.outputValue}");
// }
Vector2 mousePosition = Event.current.mousePosition;
// Display tooltip with some offset
Vector2 tooltipSize = GUI.skin.box.CalcSize(tooltip);
Rect tooltipRect = new Rect(mousePosition.x + 10, mousePosition.y + 10, tooltipSize.x, tooltipSize.y);
GUI.Box(tooltipRect, tooltip);
}
private void HandleDiscClicked(Nucleus nucleus) {
this.currentNucleus = nucleus;
BuildLayers();
}
void DrawInspector() {
if (NanoBrainEditor.inspectorContainer == null)
return;
NanoBrainEditor.inspectorContainer.Clear();
if (this.currentNucleus == null)
return;
// create a SerializedObject wrapper so Unity inspector controls work (and Undo)
SerializedObject so = new SerializedObject(currentWrapper);
IMGUIContainer container = new IMGUIContainer(() => {
so.Update();
currentNucleus.name = EditorGUILayout.TextField(currentNucleus.name);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Output Value", GUILayout.Width(100));
EditorGUILayout.Vector3Field(GUIContent.none, currentNucleus.outputValue);
EditorGUILayout.EndHorizontal();
if (currentNucleus.synapses.Count > 0) {
EditorGUILayout.LabelField("Synapses");
EditorGUI.indentLevel++;
//List<Nucleus> nuclei = currentNucleus.synapses.Keys.ToList();
// foreach (Nucleus nucleus in nuclei) {
foreach (Synapse synapse in currentNucleus.synapses) {
EditorGUI.BeginDisabledGroup(synapse.nucleus.isSleeping);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(synapse.nucleus.name, GUILayout.Width(120));
EditorGUI.indentLevel--;
EditorGUILayout.LabelField("Weight", GUILayout.Width(45));
// float weight = currentNucleus.synapses[nucleus];
// currentNucleus.synapses[nucleus] = EditorGUILayout.FloatField(weight, GUILayout.Width(40));
synapse.weight = EditorGUILayout.FloatField(synapse.weight, GUILayout.Width(40));
EditorGUI.indentLevel++;
EditorGUILayout.Vector3Field(GUIContent.none, synapse.nucleus.outputValue, GUILayout.Width(180));
EditorGUILayout.EndHorizontal();
EditorGUI.EndDisabledGroup();
}
EditorGUI.indentLevel--;
}
if (GUILayout.Button("Add Neuron"))
AddInputNeuron(currentNucleus);
});
NanoBrainEditor.inspectorContainer.Add(container);
}
protected virtual void AddInputNeuron(Nucleus receiver) {
Neuroid newNeuroid = new(brain, "New neuron");
newNeuroid.AddReceiver(receiver);
Rebuild();
}
private Vector3 NodePosition(Nucleus nucleus, int layerNodeCount = 1) {
if (this.neuroidPositions.ContainsKey(nucleus)) {
Vector2Int nucleusPos = this.neuroidPositions[nucleus];
return NodePosition(nucleusPos, layerNodeCount);
}
else {
return Vector3.zero;
}
}
private Vector3 NodePosition(Vector2Int location, int layerNodeCount = 1) {
float spacing = 400f / layerNodeCount;
float margin = 10 + spacing / 2;
float size = 20;
Vector3 parentPos = new(100 + location.x * 100 - size, margin + location.y * spacing - size, 0.1f);
return parentPos;
}
// public void CreateEdge(string fromId, string toId) {
// if (fromId == toId) return;
// Undo.RecordObject(graph, "Create Edge");
// graph.edges.Add(new GraphEdge { fromNodeId = fromId, toNodeId = toId });
// EditorUtility.SetDirty(graph);
// Rebuild();
// }
}
public class NodeView : VisualElement {
Nucleus data;
GraphBoardView board;
Label titleLabel;
//bool dragging = false;
Vector2 localDragStart;
public NodeView(Nucleus node, GraphBoardView boardView) {
data = node;
board = boardView;
name = "node";
style.width = 20; //node.size.x;
style.height = 20; //node.size.y;
titleLabel = new Label(node.name) { name = "title" };
Add(titleLabel);
// ports
// var outPort = new Button(() => StartEdgeDrag(true)) { text = "◀", name = "out" };
// var inPort = new Button(() => StartEdgeDrag(false)) { text = "▶", name = "in" };
// Add(outPort);
// Add(inPort);
RegisterCallback<MouseDownEvent>(OnMouseDown);
// RegisterCallback<MouseMoveEvent>(OnMouseMove);
RegisterCallback<MouseUpEvent>(OnMouseUp);
//RegisterCallback<MouseLeaveEvent>(e => dragging = false);
}
// void StartEdgeDrag(bool isOutput) {
// // simplified: on first click store source; on second click on target port call board.CreateEdge
// if (EdgeDragState.active == null) EdgeDragState.active = new EdgeDragState { fromNode = data, fromIsOutput = isOutput };
// else {
// var src = EdgeDragState.active.fromNode;
// if (src != null && src.id != data.id) board.CreateEdge(src.id, data.id);
// EdgeDragState.active = null;
// }
// }
void OnMouseDown(MouseDownEvent e) {
if (e.button == 0 && e.target == this) {
//dragging = true;
localDragStart = e.mousePosition;
e.StopPropagation();
}
}
// void OnMouseMove(MouseMoveEvent e) {
// if (!dragging) return;
// var delta = e.mousePosition - localDragStart;
// var worldPos = new Vector2(layout.x + delta.x, layout.y + delta.y);
// style.left = worldPos.x;
// style.top = worldPos.y;
// // commit on every move
// board.UpdateNodePosition(data, worldPos);
// }
void OnMouseUp(MouseUpEvent e) {
//dragging = false;
}
}
public class GraphNodeWrapper : ScriptableObject {
// expose fields that map to GraphNode
public string title;
public Vector2 position;
Nucleus node;
NanoBrain graph; // needed to write back and mark dirty
public GraphNodeWrapper Init(Nucleus node, NanoBrain graphAsset) {
this.node = node;
this.graph = graphAsset;
this.title = " A " + node.name;
//position = node.position;
return this;
}
void OnValidate() {
if (node != null) {
node.name = title;
//node.position = position;
#if UNITY_EDITOR
if (graph != null)
UnityEditor.EditorUtility.SetDirty(graph);
#endif
}
}
}
//static class EdgeDragState { public static EdgeDragState active; public GraphNode fromNode; public bool fromIsOutput; }
public static class OpenAssetHandler {
// Called when an asset is double-clicked or opened.
[OnOpenAsset]
public static bool OpenMyScriptableObject(int instanceID, int line) {
NanoBrain obj = EditorUtility.EntityIdToObject(instanceID) as NanoBrain;
if (obj != null) {
NanoBrainEditor.Open(obj);
return true; // handled
}
return false; // let Unity open normally
}
}
*/

View File

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

View File

@ -1,645 +0,0 @@
/*
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
[CustomEditor(typeof(NanoBrain))]
public class NanoBrainInspector : Editor {
protected static VisualElement mainContainer;
protected static VisualElement inspectorContainer;
protected bool breakOnWake = false;
#region Start
public override VisualElement CreateInspectorGUI() {
NanoBrain brain = target as NanoBrain;
serializedObject.Update();
VisualElement root = new();
//root.style.flexDirection = FlexDirection.Row; // side-by-side layout
//root.style.flexGrow = 1;
//root.style.minHeight = 600;
root.style.paddingLeft = 0;
root.style.paddingRight = 0;
root.style.paddingTop = 0;
root.style.paddingBottom = 0;
root.styleSheets.Add(Resources.Load<StyleSheet>("GraphStyles"));
mainContainer = new() {
// name = "main",
style = {
// flexDirection = FlexDirection.Row,
// flexGrow = 1,
height = 450,
}
};
GraphView graph = new();
graph.style.flexGrow = 1;
inspectorContainer = new VisualElement {
// name = "inspector"
};
mainContainer.Add(graph);
mainContainer.Add(inspectorContainer);
root.Add(mainContainer);
// Run once for initial state (use resolved style width if available)
float initialWidth = root.layout.width > 0 ? root.layout.width : root.contentRect.width;
UpdateLayout(initialWidth);
// React to size changes of root (or parent if appropriate)
root.RegisterCallback<GeometryChangedEvent>(evt => {
UpdateLayout(evt.newRect.width);
});
if (brain != null)
graph.SetGraph(null, brain, brain.output, inspectorContainer);
else
Debug.LogWarning(" No brain!");
serializedObject.ApplyModifiedProperties();
return root;
}
public class GraphView : VisualElement {
NanoBrain brain;
SerializedObject serializedBrain;
INucleus currentNucleus;
GameObject gameObject;
private List<NeuroidLayer> layers = new();
private readonly Dictionary<IReceptor, Vector2Int> neuroidPositions = new();
Vector2 pan = Vector2.zero;
//float zoom = 1f;
bool draggingCanvas = false;
Vector2 lastMouse;
GraphNodeWrapper currentWrapper;
public GraphView() {
name = "content";
style.flexGrow = 1;
IMGUIContainer imguiContainer = new(OnIMGUI);
imguiContainer.style.position = Position.Absolute;
imguiContainer.style.left = 0; imguiContainer.style.top = 0;
imguiContainer.style.right = 0; imguiContainer.style.bottom = 0;
imguiContainer.pickingMode = PickingMode.Position;
imguiContainer.focusable = true;
Add(imguiContainer);
//RegisterCallback<WheelEvent>(OnWheel);
RegisterCallback<MouseDownEvent>(OnMouseDown);
RegisterCallback<MouseMoveEvent>(OnMouseMove);
RegisterCallback<MouseUpEvent>(OnMouseUp);
}
public void SetGraph(GameObject gameObject, NanoBrain brain, Nucleus nucleus, VisualElement inspectorContainer) {
this.gameObject = gameObject;
this.brain = brain;
if (Application.isPlaying == false)
this.serializedBrain = new SerializedObject(brain);
this.currentNucleus = nucleus;
Rebuild(inspectorContainer);
}
void Rebuild(VisualElement inspectorContainer) {
BuildLayers();
if (this.currentNucleus == null) {
inspectorContainer.Clear();
return;
}
if (currentWrapper != null)
DestroyImmediate(currentWrapper);
currentWrapper = CreateInstance<GraphNodeWrapper>().Init(this.currentNucleus, brain);
DrawInspector(inspectorContainer);
}
private void BuildLayers() {
// A temporary list to track what's been added to layers
this.layers = new();
int layerIx = 0;
INucleus selectedNucleus = this.currentNucleus;
if (selectedNucleus == null)
return;
NeuroidLayer currentLayer = new() { ix = layerIx };
if (selectedNucleus.receivers != null) {
foreach (INucleus receiver in selectedNucleus.receivers) {
INucleus outputNeuroid = receiver;
if (outputNeuroid != null) {
AddToLayer(currentLayer, outputNeuroid);
// Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}");
}
}
}
if (currentLayer.neuroids.Count > 0) {
this.layers.Add(currentLayer);
layerIx++;
currentLayer = new() { ix = layerIx };
}
AddToLayer(currentLayer, selectedNucleus);
this.layers.Add(currentLayer);
// Debug.Log($"layer {layerIx} nucleus {selectedNucleus.name}");
layerIx++;
currentLayer = new() { ix = layerIx };
if (selectedNucleus.synapses != null) {
foreach (Synapse synapse in selectedNucleus.synapses) {
IReceptor input = synapse.nucleus;
AddToLayer(currentLayer, input);
// Debug.Log($"layer {layerIx} nucleus {input.name}");
}
}
if (currentLayer.neuroids.Count > 0) {
this.layers.Add(currentLayer);
}
}
private void AddToLayer(NeuroidLayer layer, IReceptor nucleus) {
if (nucleus == null)
return;
layer.neuroids.Add(nucleus);
//nucleus.layerIx = layer.ix;
// Store its position
Vector2Int neuroidPosition = new(layer.ix, layer.neuroids.Count - 1);
neuroidPositions[nucleus] = neuroidPosition;
}
void OnMouseDown(MouseDownEvent e) {
if (e.button == 2) { draggingCanvas = true; lastMouse = e.mousePosition; e.StopPropagation(); }
}
void OnMouseMove(MouseMoveEvent e) {
if (draggingCanvas) {
var delta = e.mousePosition - lastMouse;
pan += delta;
//content.style.left = pan.x;
//content.style.top = pan.y;
lastMouse = e.mousePosition;
}
}
void OnMouseUp(MouseUpEvent e) { if (e.button == 2) draggingCanvas = false; }
void OnIMGUI() {
if (currentNucleus == null)
return;
if (Application.isPlaying == false)
serializedBrain.Update();
Handles.BeginGUI();
DrawGraph();
Handles.EndGUI();
}
private void DrawGraph() {
float size = 20;
Vector3 position = new(150, 210, 0);
DrawReceivers(this.currentNucleus, position, size);
DrawSynapses(this.currentNucleus, position, size);
// Draw selected Nucleus
Handles.color = Color.white;
Handles.DrawSolidDisc(position, Vector3.forward, size + 2);
DrawNucleus(this.currentNucleus, position, this.currentNucleus.outputValue.magnitude, 20);
}
private void DrawReceivers(INucleus nucleus, Vector3 parentPos, float size) {
int nodeCount = nucleus.receivers.Count;
// Determine the maximum value in this layer
// This is used to 'scale' the output value colors of the nuclei
float maxValue = 0;
foreach (INucleus receiver in nucleus.receivers) {
if (receiver is Neuroid neuroid) {
float value = neuroid.outputValue.magnitude;
if (value > maxValue)
maxValue = value;
}
}
// Determine the spacing of the nuclei in the layer
float spacing = 400f / nodeCount;
float margin = 10 + spacing / 2;
int row = 0;
foreach (INucleus receiver in nucleus.receivers) {
INucleus receiverNucleus = receiver;
if (receiverNucleus == null)
continue;
Vector3 pos = new(50, margin + row * spacing, 0.0f);
Handles.color = Color.white;
Handles.DrawLine(parentPos, pos);
DrawNucleus(receiverNucleus, pos, maxValue, size);
row++;
}
}
private void DrawSynapses(INucleus nucleus, Vector3 parentPos, float size) {
int nodeCount = nucleus.synapses.Count;
// Determine the maximum value in this layer
// This is used to 'scale' the output value colors of the nuclei
float maxValue = 0;
foreach (Synapse receiver in nucleus.synapses) {
if (receiver.nucleus is Neuroid neuroid) {
float value = neuroid.outputValue.magnitude;
if (value > maxValue)
maxValue = value;
}
}
// Determine the spacing of the nuclei in the layer
float spacing = 400f / nodeCount;
float margin = 10 + spacing / 2;
int row = 0;
List<PercepteiArray> drawnArrays = new();
foreach (Synapse synapse in nucleus.synapses) {
Vector3 pos = new(250, margin + row * spacing, 0.0f);
Handles.color = Color.white;
Handles.DrawLine(parentPos, pos);
// if (synapse.nucleus is Perceptoid perceptoid && perceptoid.array != null) {
// // if (drawnArrays.Contains(perceptoid.array))
// // // We already drawn this array
// // continue;
// drawnArrays.Add(perceptoid.array);
// DrawArray(perceptoid.array, pos, size);
// }
// else {
DrawNucleus(synapse.nucleus, pos, maxValue, size);
row++;
// }
}
}
private void DrawNucleus(IReceptor nucleus, Vector3 position, float maxValue, float size) {
if (nucleus.isSleeping)
Handles.color = Color.darkRed;
else {
if (Application.isPlaying) {
float brightness = nucleus.outputValue.magnitude / maxValue;
Handles.color = new Color(brightness, brightness, brightness, 1f);
}
else
Handles.color = Color.black;
}
Handles.DrawSolidDisc(position, Vector3.forward, size);
Handles.color = Color.white;
// Position the label in front of the disc
Vector3 labelPosition = position + (Vector3.forward * 0.1f);
GUIStyle style = new(EditorStyles.label) {
alignment = TextAnchor.MiddleCenter,
normal = { textColor = Color.white },
fontStyle = FontStyle.Bold,
};
if (nucleus is Perceptoid perceptoid) {
if (perceptoid.array == null || perceptoid.array.perceptei == null || perceptoid.array.perceptei.Length == 0)
perceptoid.array = new PercepteiArray(perceptoid);
if (perceptoid.array.perceptei.Length > 1) {
Handles.Label(labelPosition, perceptoid.array.perceptei.Length.ToString(), style);
}
}
style.alignment = TextAnchor.UpperCenter;
Vector3 labelPos = position - Vector3.down * (size + 0.2f); // below disc along up axis
Handles.Label(labelPos, nucleus.name, style);
Rect neuronRect = new(position.x - size, position.y - size, size * 2, size * 2);
int id = GUIUtility.GetControlID(FocusType.Passive);
Event e = Event.current;
EventType et = e.GetTypeForControl(id);
if (e != null && neuronRect.Contains(e.mousePosition)) {
// Process Hover
HandleMouseHover(nucleus, neuronRect);
// Process click
if (e.type == EventType.MouseDown && e.button == 0) {
// Consume the event so the scene doesn't also handle it
e.Use();
HandleClicked(nucleus);
}
}
}
private void DrawArray(PercepteiArray array, Vector3 position, float size) {
Vector3 offset = new(size / 4, size / 4, 0);
Handles.color = Color.black;
Handles.DrawSolidDisc(position, Vector3.forward, size);
GUIStyle style = new(EditorStyles.label) {
alignment = TextAnchor.UpperCenter,
normal = { textColor = Color.white },
fontStyle = FontStyle.Bold
};
Handles.Label(position, array.perceptei.Length.ToString(), style);
Vector3 labelPos = position - Vector3.down * (size + 0.2f); // below disc along up axis
Handles.Label(labelPos, array.name, style);
// To do: add HandleClick (see above) to expand the array
}
private void HandleMouseHover(IReceptor nucleus, Rect rect) {
GUIContent tooltip;
if (nucleus is Perceptoid perceptoid) {
if (perceptoid.receptor != null) {
tooltip = new(
$"{perceptoid.name}" +
// $"\nType {perceptoid.receptor.thingType}" +
$" Thing {perceptoid.thingId}" +
$"\nValue: {nucleus.outputValue}");
}
else {
tooltip = new(
$"{perceptoid.name}" +
$"\nThing {perceptoid.thingId}" +
$"\nValue: {nucleus.outputValue}");
}
}
else if (nucleus is INucleus n) {
tooltip = new(
$"{nucleus.name}" +
$"\nsynapse count {n.synapses.Count}" +
$"\nValue: {nucleus.outputValue}");
}
else {
tooltip = new(
$"{nucleus.name}" +
$"\nValue: {nucleus.outputValue}");
}
Vector2 mousePosition = Event.current.mousePosition;
// Display tooltip with some offset
Vector2 tooltipSize = GUI.skin.box.CalcSize(tooltip);
Rect tooltipRect = new Rect(mousePosition.x + 10, mousePosition.y + 10, tooltipSize.x, tooltipSize.y);
GUI.Box(tooltipRect, tooltip);
}
private void HandleClicked(IReceptor nucleus) {
if (nucleus is INucleus n) {
this.currentNucleus = n;
BuildLayers();
}
}
void DrawInspector(VisualElement inspectorContainer) {
if (inspectorContainer == null)
return;
inspectorContainer.Clear();
if (this.currentNucleus == null)
return;
// create a SerializedObject wrapper so Unity inspector controls work (and Undo)
SerializedObject so = new(currentWrapper);
IMGUIContainer container = new(() => {
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 perceptoid) {
// perceptoid.receptor.thingType = EditorGUILayout.IntField("Thing Type", perceptoid.receptor.thingType);
if (perceptoid.array == null || perceptoid.array.perceptei == null || perceptoid.array.perceptei.Length == 0)
perceptoid.array = new PercepteiArray(perceptoid);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.IntField("Array size", perceptoid.array.perceptei.Length);
if (GUILayout.Button("Add"))
perceptoid.array.AddPerceptoid();
if (GUILayout.Button("Del"))
perceptoid.array.RemovePerceptoid();
EditorGUILayout.EndHorizontal();
}
else if (this.currentNucleus is Neuroid neuroid) {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150));
if (neuroid.curveMax > 0)
EditorGUILayout.CurveField(neuroid.curve, Color.cyan, new Rect(0, 0, 1, neuroid.curveMax));
else
EditorGUILayout.CurveField(neuroid.curve, Color.cyan, new Rect(0, neuroid.curveMax, 1, -neuroid.curveMax));
neuroid.curvePreset = (Neuroid.CurvePresets)EditorGUILayout.EnumPopup(neuroid.curvePreset, GUILayout.Width(100));
EditorGUILayout.EndHorizontal();
}
if (Application.isPlaying)
EditorGUILayout.FloatField("Output", this.currentNucleus.outputValue.magnitude);
else
EditorGUILayout.LabelField(" ");
if (this.currentNucleus.synapses.Count > 0) {
Synapse[] synapses = this.currentNucleus.synapses.ToArray();
foreach (Synapse synapse in synapses) {
if (synapse.nucleus != null) {
EditorGUILayout.Space();
EditorGUI.BeginDisabledGroup(synapse.nucleus.isSleeping);
if (Application.isPlaying)
EditorGUILayout.FloatField(synapse.nucleus.name, synapse.nucleus.outputValue.magnitude * synapse.weight);
else {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(synapse.nucleus.name);
// if (synapse.nucleus is Perceptoid perceptoid) {
// if (perceptoid.array == null || perceptoid.array.perceptei == null || perceptoid.array.perceptei.Length == 0) {
// perceptoid.array = new PercepteiArray(perceptoid);
// }
// EditorGUILayout.IntField(perceptoid.array.perceptei.Length);
// if (GUILayout.Button("Add"))
// perceptoid.array.AddPerceptoid();
// }
if (GUILayout.Button("Disconnect"))
synapse.nucleus.RemoveReceiver(this.currentNucleus);
EditorGUILayout.EndHorizontal();
}
EditorGUI.indentLevel++;
synapse.weight = EditorGUILayout.FloatField("Weight", synapse.weight);
EditorGUI.indentLevel--;
EditorGUI.EndDisabledGroup();
}
}
}
EditorGUILayout.Space();
ConnectNucleus(this.currentNucleus);
if (GUILayout.Button("Add Input Neuron"))
AddInputNeuron(this.currentNucleus);
if (GUILayout.Button("Add Input Perceptoid"))
AddPerceptoid(this.currentNucleus);
if (GUILayout.Button("Add Input Cluster"))
AddCluster(this.currentNucleus);
EditorGUILayout.Space();
if (GUILayout.Button("Delete this neuron"))
DeleteNeuron(this.currentNucleus);
//DisconnectNucleus(this.currentNucleus);
if (this.gameObject != null) {
Vector3 worldVector = this.gameObject.transform.TransformVector(this.currentNucleus.outputValue);
Debug.DrawRay(this.gameObject.transform.position, worldVector, Color.yellow);
}
});
inspectorContainer.Add(container);
}
protected virtual void AddInputNeuron(INucleus nucleus) {
Neuroid newNeuroid = new(this.brain.cluster, "New neuron");
newNeuroid.AddReceiver(nucleus);
this.currentNucleus = newNeuroid;
BuildLayers();
}
protected virtual void DeleteNeuron(INucleus nucleus) {
if (nucleus == null)
return;
if (nucleus.cluster != null)
this.currentNucleus = nucleus.cluster.output;
foreach (INucleus receiver in nucleus.receivers) {
if (receiver != null) {
this.currentNucleus = receiver;
break;
}
}
Nucleus.Delete(nucleus);
BuildLayers();
}
protected virtual void AddPerceptoid(INucleus nucleus) {
Perceptoid newPerceptoid = new(this.brain, 0, "New Perceptoid");
newPerceptoid.AddReceiver(nucleus);
this.currentNucleus = newPerceptoid;
BuildLayers();
}
protected virtual void AddCluster(INucleus nucleus) {
BrainPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster");
}
private void OnClusterPicked(INucleus nucleus, NanoBrain brain) {
NanoBrain brainInstance = Instantiate(brain);
brainInstance.AddReceiver(nucleus);
}
protected virtual void ConnectNucleus(INucleus nucleus) {
if (this.currentNucleus.cluster == null)
return;
IEnumerable<string> synapseNuclei = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name);
//IEnumerable<string> perceptei = this.currentNucleus.brain.perceptei.Select(i => i.name).Except(synapseNuclei);
IEnumerable<string> nuclei = this.currentNucleus.cluster.nuclei.Select(i => i.name).Except(synapseNuclei);
//string[] names = perceptei.Concat(nuclei).ToArray();
string[] names = nuclei.ToArray();
int selectedIndex = -1;
selectedIndex = EditorGUILayout.Popup("Connect to", selectedIndex, names);
if (selectedIndex >= 0) {
// if (selectedIndex < perceptei.Count()) {
// Nucleus n = this.currentNucleus.brain.perceptei[selectedIndex];
// n.AddReceiver(this.currentNucleus);
// }
// else {
// Nucleus n = this.currentNucleus.brain.nuclei[selectedIndex - perceptei.Count()];
// n.AddReceiver(this.currentNucleus);
// }
INucleus n = this.currentNucleus.cluster.nuclei[selectedIndex];
n.AddReceiver(this.currentNucleus);
}
}
protected virtual void DisconnectNucleus(Nucleus nucleus) {
if (this.currentNucleus.cluster == null)
return;
string[] names = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name).ToArray();
int selectedIndex = -1;
selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names);
//if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.brain.perceptei.Count) {
if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.cluster.nuclei.Count) {
Synapse synapse = this.currentNucleus.synapses[selectedIndex];
synapse.nucleus.RemoveReceiver(this.currentNucleus);
}
}
}
#endregion Start
#region Update
private void UpdateLayout(float containerWidth) {
if (containerWidth > 600f) {
mainContainer.style.flexDirection = FlexDirection.Row;
inspectorContainer.style.width = 300; // fixed sidebar width
inspectorContainer.style.flexGrow = 0;
}
else {
mainContainer.style.flexDirection = FlexDirection.Column;
inspectorContainer.style.width = Length.Percent(100); // full width below
inspectorContainer.style.flexDirection = FlexDirection.Column;
inspectorContainer.style.flexGrow = 1; // can set 0 or keep as needed
}
}
#endregion Update
}
/*
public class NeuroidLayer {
public int ix = 0;
public List<INucleus> neuroids = new();
}
*/
/*
public class GraphNodeWrapper : ScriptableObject {
// expose fields that map to GraphNode
//public string title;
public Vector2 position;
INucleus node;
NanoBrain graph; // needed to write back and mark dirty
public GraphNodeWrapper Init(INucleus node, NanoBrain graphAsset) {
this.node = node;
this.graph = graphAsset;
//this.title = " A " + node.name;
//position = node.position;
return this;
}
void OnValidate() {
if (node != null) {
//node.name = title;
//node.position = position;
#if UNITY_EDITOR
if (graph != null)
UnityEditor.EditorUtility.SetDirty(graph);
#endif
}
}
}
*/

View File

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

View File

@ -3,21 +3,21 @@ using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
[CustomEditor(typeof(NanoBrainComponent))]
[CustomEditor(typeof(NanoBrain))]
public class NanoBrainComponent_Editor : Editor {
protected static VisualElement mainContainer;
protected static VisualElement inspectorContainer;
protected NanoBrainComponent component;
protected NanoBrain component;
private SerializedProperty brainProp;
ClusterInspector.GraphView board;
public void OnEnable() {
component = target as NanoBrainComponent;
component = target as NanoBrain;
if (Application.isPlaying == false)
brainProp = serializedObject.FindProperty(nameof(NanoBrainComponent.defaultBrain));
brainProp = serializedObject.FindProperty(nameof(NanoBrain.defaultBrain));
}

View File

@ -1,102 +1,38 @@
/*
using System;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "Passer/NanoBrain")]
public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
public List<Neuroid> nuclei = new();
public List<Perceptoid> perceptei = new();
public List<Receptor> receptors = new();
public class NanoBrain : MonoBehaviour {
public ClusterPrefab defaultBrain;
// This is probably always the first element in the nuclei list...
[System.NonSerialized]
public Nucleus output;
public int rootId;
public NanoBrain() {
// this.cluster = new();
// this.output = new Neuroid(this.cluster, "Root");
}
public Cluster cluster;
public void AddReceiver(INucleus receiver) {
output.AddReceiver(receiver);
}
public Neuroid AddNeuron(string name) {
Neuroid neuroid = new(this.cluster, name);
return neuroid;
}
public void UpdateNuclei() {
foreach (Nucleus nucleus in nuclei)
nucleus.IncreaseAge();
foreach (Perceptoid perception in perceptei)
perception.IncreaseAge();
}
public void OnBeforeSerialize() {
if (output != null) {
this.rootId = output.id;
}
}
public void OnAfterDeserialize() {
try {
foreach (Nucleus nucleus in this.nuclei.ToArray()) {
if (this.rootId == nucleus.id)
this.output = nucleus;
nucleus.Rebuild(this);
[NonSerialized]
private Cluster brainInstance;
public Cluster brain {
get {
if (brainInstance == null && defaultBrain != null) {
brainInstance = new Cluster(defaultBrain) {
name = defaultBrain.name + " (Instance)"
};
}
foreach (Perceptoid perceptoid in this.perceptei.ToArray())
perceptoid.Rebuild(this);
SwarmControl sc = FindFirstObjectByType<SwarmControl>();
if (sc != null) {
UpdateWeight(brainInstance, "Containment", sc.containmentForce);
UpdateWeight(brainInstance, "Cohesion", sc.cohesionForce);
UpdateWeight(brainInstance, "Separation", sc.separationForce);
UpdateWeight(brainInstance, "Alignment", sc.alignmentForce);
}
return brainInstance;
}
catch (System.Exception) { }
if (this.cluster != null)
this.cluster.GarbageCollection();
}
public void GarbageCollection() {
HashSet<INucleus> visitedNuclei = new();
MarkNuclei(visitedNuclei, this.output);
//Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
this.nuclei.RemoveAll(nucleus => visitedNuclei.Contains(nucleus) == false);
this.perceptei.RemoveAll(perceptoid => visitedNuclei.Contains(perceptoid) == false);
}
public void MarkNuclei(HashSet<INucleus> visitedNuclei, INucleus nucleus) {
if (nucleus is null)
return;
if (nucleus.brain == null)
nucleus.brain = this;
visitedNuclei.Add(nucleus);
if (nucleus.synapses != null) {
HashSet<Synapse> visitedSynapses = new();
foreach (Synapse synapse in nucleus.synapses) {
if (synapse != null && synapse.nucleus != null) {
visitedSynapses.Add(synapse);
MarkNuclei(visitedNuclei, synapse.nucleus);
}
public static void UpdateWeight(Cluster brain, string name, float weight) {
INucleus root = brain.output;
foreach (Synapse synapse in root.synapses) {
if (synapse.nucleus.name == name) {
if (synapse.weight != weight) {
synapse.weight = weight;
// Debug.Log($"Updated weight for {name}");
}
nucleus.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false);
}
if (nucleus.receivers != null) {
HashSet<Receiver> visitedReceivers = new();
foreach (Receiver receiver in nucleus.receivers) {
if (receiver != null && receiver.nucleus != null) {
visitedReceivers.Add(receiver);
visitedNuclei.Add(receiver.nucleus);
}
}
nucleus.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
}
}
}
*/
}
}

View File

@ -1,2 +1,2 @@
fileFormatVersion: 2
guid: 36081359186edfec998d891a1feeb17b
guid: 92f34a5e4027a1dc39efd8ce63cf6aba

View File

@ -1,39 +0,0 @@
using System;
using UnityEngine;
public class NanoBrainComponent : MonoBehaviour {
public ClusterPrefab defaultBrain;
[NonSerialized]
private Cluster brainInstance;
public Cluster brain {
get {
if (brainInstance == null && defaultBrain != null) {
brainInstance = new Cluster(defaultBrain) {
name = defaultBrain.name + " (Instance)"
};
}
SwarmControl sc = FindFirstObjectByType<SwarmControl>();
if (sc != null) {
UpdateWeight(brainInstance, "Containment", sc.containmentForce);
UpdateWeight(brainInstance, "Cohesion", sc.cohesionForce);
UpdateWeight(brainInstance, "Separation", sc.separationForce);
UpdateWeight(brainInstance, "Alignment", sc.alignmentForce);
}
return brainInstance;
}
}
public static void UpdateWeight(Cluster brain, string name, float weight) {
INucleus root = brain.output;
foreach (Synapse synapse in root.synapses) {
if (synapse.nucleus.name == name) {
if (synapse.weight != weight) {
synapse.weight = weight;
// Debug.Log($"Updated weight for {name}");
}
}
}
}
}

View File

@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 92f34a5e4027a1dc39efd8ce63cf6aba