Skip to content

Commit d15e74a

Browse files
committed
feat: set edge alpha by input weight
1 parent a230aa0 commit d15e74a

9 files changed

Lines changed: 145 additions & 80 deletions

File tree

Editor/Scripts/GraphView/PlayableGraphView.cs

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,15 @@ public PlayableGraphView()
2323
SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
2424
}
2525

26-
public PlayableGraph GetPlayableGraph()
27-
{
28-
return _playableGraph;
29-
}
30-
3126
public void Update(PlayableGraph playableGraph)
3227
{
28+
_playableGraph = playableGraph;
3329
if (!_playableGraph.IsValid())
3430
{
3531
ClearView();
3632
}
3733

38-
if (IsEqual(ref _playableGraph, ref playableGraph))
39-
{
40-
DiffOutputNodes();
41-
}
42-
else
43-
{
44-
// playable graph changed
45-
_playableGraph = playableGraph;
46-
47-
PopulateView();
48-
}
34+
PopulateView();
4935

5036
CalculateLayout();
5137
}
@@ -68,30 +54,6 @@ private void PopulateView()
6854
return;
6955
}
7056

71-
// create nodes
72-
for (int i = 0; i < _playableGraph.GetOutputCount(); i++)
73-
{
74-
var playableOutput = _playableGraph.GetOutput(i);
75-
var playableOutputNode = PlayableOutputNodeFactory.CreateNode(playableOutput);
76-
playableOutputNode.AddToContainer(this);
77-
78-
_rootOutputNodes.Add(playableOutputNode);
79-
}
80-
81-
for (int i = 0; i < _rootOutputNodes.Count; i++)
82-
{
83-
//_rootOutputNodes[i].CreateAndConnectInputNodes();
84-
_rootOutputNodes[i].Update();
85-
}
86-
}
87-
88-
private void DiffOutputNodes()
89-
{
90-
if (!_playableGraph.IsValid())
91-
{
92-
return;
93-
}
94-
9557
// mark all root nodes inactive
9658
for (int i = 0; i < _rootOutputNodes.Count; i++)
9759
{
@@ -158,24 +120,5 @@ private void CalculateLayout()
158120
origin.y += treeSize.y + NodeLayoutInfo.VerticalSpace;
159121
}
160122
}
161-
162-
163-
private static bool IsEqual(ref PlayableGraph a, ref PlayableGraph b)
164-
{
165-
if (!a.IsValid())
166-
{
167-
return !b.IsValid();
168-
}
169-
170-
if (!b.IsValid())
171-
{
172-
return false;
173-
}
174-
175-
var nameA = a.GetEditorName();
176-
var nameB = b.GetEditorName();
177-
178-
return nameA.Equals(nameB);
179-
}
180123
}
181124
}

Editor/Scripts/Node/GraphViewNode.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ public enum NodeFlag : uint
1616

1717
public readonly struct NodeInput
1818
{
19-
public UEdge Edge { get; }
19+
public readonly UEdge Edge;
2020

21-
public GraphViewNode Node { get; }
21+
public readonly GraphViewNode Node;
2222

23+
public readonly int PortIndex;
2324

24-
public NodeInput(UEdge edge, GraphViewNode node)
25+
26+
public NodeInput(UEdge edge, GraphViewNode node, int portIndex)
2527
{
2628
Edge = edge;
2729
Node = node;
30+
PortIndex = portIndex;
31+
}
32+
33+
public NodeInput Copy(NodeInput other, int portIndexOverride)
34+
{
35+
return new NodeInput(other.Edge, other.Node, portIndexOverride);
2836
}
2937
}
3038

Editor/Scripts/Node/PlayableNode.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
1+
using GBG.PlayableGraphMonitor.Editor.Utility;
2+
using System;
23
using UnityEditor.Experimental.GraphView;
3-
using UnityEngine;
44
using UnityEngine.Playables;
55

66
namespace GBG.PlayableGraphMonitor.Editor.Node
@@ -27,6 +27,10 @@ public override void Update()
2727

2828
if (!Playable.IsValid())
2929
{
30+
foreach (var port in InternalOutputPorts)
31+
{
32+
port.portColor = GraphTool.GetPortInvalidColor(0);
33+
}
3034
return;
3135
}
3236

@@ -36,8 +40,13 @@ public override void Update()
3640
InternalInputs[i].Node.RemoveFlag(NodeFlag.Active);
3741
}
3842

43+
// diff child nodes
3944
for (int i = 0; i < Playable.GetInputCount(); i++)
4045
{
46+
// update port color
47+
var inputWeight = Playable.GetInputWeight(i);
48+
InternalInputPorts[i].portColor = GraphTool.GetPortColor(Playable, inputWeight);
49+
4150
var inputPlayable = Playable.GetInput(i);
4251
if (!inputPlayable.IsValid())
4352
{
@@ -47,7 +56,9 @@ public override void Update()
4756
var childNodeIndex = FindChildPlayableNode(inputPlayable);
4857
if (childNodeIndex >= 0)
4958
{
50-
InternalInputs[i].Node.AddFlag(NodeFlag.Active);
59+
var input = InternalInputs[i];
60+
input.Node.AddFlag(NodeFlag.Active);
61+
InternalInputs[i] = input.Copy(input, i);
5162
continue;
5263
}
5364

@@ -60,12 +71,15 @@ public override void Update()
6071
var selfInputPort = InternalInputPorts[i];
6172
var edge = selfInputPort.ConnectTo(inputPlayableNodeOutputPort);
6273
Container.AddElement(edge);
63-
InternalInputs.Add(new NodeInput(edge, inputPlayableNode));
74+
InternalInputs.Add(new NodeInput(edge, inputPlayableNode, i));
6475
}
6576

77+
// update child nodes
6678
for (int i = InternalInputs.Count - 1; i >= 0; i--)
6779
{
6880
var input = InternalInputs[i];
81+
82+
// remove inactive child node
6983
if (!input.Node.CheckFlag(NodeFlag.Active))
7084
{
7185
Container.RemoveElement(input.Edge);
@@ -75,6 +89,11 @@ public override void Update()
7589
continue;
7690
}
7791

92+
// update port color
93+
var inputWeight = Playable.GetInputWeight(input.PortIndex);
94+
input.Node.OutputPorts[0].portColor = GraphTool.GetPortColor(Playable, inputWeight);
95+
input.Edge.UpdateEdgeControl();
96+
7897
InternalInputs[i].Node.Update();
7998
}
8099
}
@@ -105,7 +124,7 @@ private void CreatePorts()
105124
{
106125
var inputPort = InstantiatePort<Playable>(Direction.Input);
107126
inputPort.portName = $"Input {i}";
108-
inputPort.portColor = Color.white;
127+
inputPort.portColor = GraphTool.GetPortColor(Playable, Playable.GetInputWeight(i));
109128
inputContainer.Add(inputPort);
110129
InternalInputPorts.Add(inputPort);
111130
}
@@ -114,7 +133,7 @@ private void CreatePorts()
114133
{
115134
var outputPort = InstantiatePort<Playable>(Direction.Output);
116135
outputPort.portName = $"Output {i}";
117-
outputPort.portColor = Color.white;
136+
outputPort.portColor = GraphTool.GetPortColor(Playable, 1);
118137
outputContainer.Add(outputPort);
119138
InternalOutputPorts.Add(outputPort);
120139
}

Editor/Scripts/Node/PlayableNodeFactory.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@ namespace GBG.PlayableGraphMonitor.Editor.Node
55
{
66
public static class PlayableNodeFactory
77
{
8+
public const string PlayableHeader = "Playable";
9+
810
public static PlayableNode CreateNode(Playable playable)
911
{
1012
var playableType = playable.GetPlayableType();
1113
var playableTypeName = playableType.Name;
14+
var playableTypeSortName = playableTypeName
15+
.Remove(playableTypeName.Length - PlayableHeader.Length);
16+
var nodeTitle = $"{PlayableHeader}\n{playableTypeSortName}";
17+
18+
PlayableNode playableNode;
19+
20+
// todo create node by playable type
1221

1322
if (playableType == typeof(AnimationClipPlayable))
1423
{
15-
24+
//goto SET_NODE_TITLE;
1625
}
1726

1827
if (playableType == typeof(AnimationMixerPlayable))
@@ -28,10 +37,11 @@ public static PlayableNode CreateNode(Playable playable)
2837
// ...
2938

3039
// default node
31-
var playableNode = new PlayableNode(playable)
32-
{
33-
title = playableTypeName
34-
};
40+
playableNode = new PlayableNode(playable);
41+
42+
SET_NODE_TITLE:
43+
playableNode.title = nodeTitle;
44+
3545
return playableNode;
3646
}
3747
}

Editor/Scripts/Node/PlayableOutputNode.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using UnityEditor.Experimental.GraphView;
2-
using UnityEngine;
1+
using GBG.PlayableGraphMonitor.Editor.Utility;
2+
using UnityEditor.Experimental.GraphView;
33
using UnityEngine.Playables;
44

55
namespace GBG.PlayableGraphMonitor.Editor.Node
@@ -52,7 +52,7 @@ public override void Update()
5252
var selfSourcePort = InternalInputPorts[0];
5353
var edge = selfSourcePort.ConnectTo(sourcePlayableOutputPort);
5454
Container.AddElement(edge);
55-
InternalInputs.Add(new NodeInput(edge, sourcePlayableNode));
55+
InternalInputs.Add(new NodeInput(edge, sourcePlayableNode, 0));
5656
}
5757
}
5858

@@ -96,7 +96,7 @@ private void CreatePorts()
9696

9797
var inputPort = InstantiatePort<Playable>(Direction.Input);
9898
inputPort.portName = "Source";
99-
inputPort.portColor = Color.white;
99+
inputPort.portColor = GraphTool.GetPortColor(PlayableOutput);
100100
inputContainer.Add(inputPort);
101101
InternalInputPorts.Add(inputPort);
102102
}

Editor/Scripts/Node/PlayableOutputNodeFactory.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@ namespace GBG.PlayableGraphMonitor.Editor.Node
66
{
77
public static class PlayableOutputNodeFactory
88
{
9+
public const string PlayableOutputHeader = "PlayableOutput";
10+
911
public static PlayableOutputNode CreateNode(PlayableOutput playableOutput)
1012
{
1113
var playableOutputType = playableOutput.GetPlayableOutputType();
1214
var playableOutputTypeName = playableOutputType.Name;
15+
var playableOutputTypeSortName = playableOutputTypeName
16+
.Remove(playableOutputTypeName.Length - PlayableOutputHeader.Length);
17+
var playableOutputEditorName = playableOutput.GetEditorName();
18+
var nodeTitle = $"{PlayableOutputHeader}\n" +
19+
$"{playableOutputTypeSortName} ({playableOutputEditorName})";
1320

1421
if (playableOutputType == typeof(AnimationPlayableOutput))
1522
{
1623

1724
}
1825

19-
// ...
26+
// todo create node by playable output type
2027

2128
// default node
22-
var playableOutputEditorName = playableOutput.GetEditorName();
2329
var playableOutputNode = new PlayableOutputNode(playableOutput)
2430
{
25-
title = $"{playableOutputTypeName} ({playableOutputEditorName})"
31+
title = nodeTitle
2632
};
2733
return playableOutputNode;
2834
}

Editor/Scripts/Utility.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using UnityEngine;
2+
using UnityEngine.Playables;
3+
4+
namespace GBG.PlayableGraphMonitor.Editor.Utility
5+
{
6+
public static class GraphTool
7+
{
8+
// Ensure edge is always visible.
9+
private const float _colorAlphaFactor = 1f / 9;
10+
11+
12+
public static Color GetPortColor(PlayableOutput playableOutput)
13+
{
14+
var portColor = Color.white;
15+
portColor.a = 1;
16+
17+
var playableOutputType = playableOutput.GetPlayableOutputType();
18+
// todo set port color by playable output type
19+
20+
return portColor;
21+
}
22+
23+
public static Color GetPortColor(Playable playable, float weight)
24+
{
25+
var portColor = Color.white;
26+
portColor.a = (weight + _colorAlphaFactor) / (1 + weight);
27+
28+
var playableType = playable.GetPlayableType();
29+
// todo set port color by playable type
30+
31+
return portColor;
32+
}
33+
34+
public static Color GetPortInvalidColor(float weight)
35+
{
36+
var portColor = Color.red;
37+
portColor.a = (weight + _colorAlphaFactor) / (1 + weight);
38+
39+
return portColor;
40+
}
41+
42+
public static bool IsEqual(ref PlayableGraph a, ref PlayableGraph b)
43+
{
44+
if (!a.IsValid())
45+
{
46+
return !b.IsValid();
47+
}
48+
49+
if (!b.IsValid())
50+
{
51+
return false;
52+
}
53+
54+
var nameA = a.GetEditorName();
55+
var nameB = b.GetEditorName();
56+
57+
return nameA.Equals(nameB);
58+
}
59+
}
60+
}

Editor/Scripts/Utility/GraphTool.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)