浏览代码

Colored edges! (also, unify node and edge view creation)

/main
Peter Bay Bastian 7 年前
当前提交
347edc88
共有 3 个文件被更改,包括 114 次插入58 次删除
  1. 147
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs
  2. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs
  3. 24
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss

147
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs


}
foreach (var node in graph.GetNodes<INode>())
{
NodeAddedGraphChange change = new NodeAddedGraphChange(node);
var nodeView = new MaterialNodeView(change.node as AbstractMaterialNode, m_PreviewSystem);
nodeView.userData = change.node;
change.node.onModified += OnNodeChanged;
m_GraphView.AddElement(nodeView);
}
AddNode(node);
{
var edge1 = new EdgeAddedGraphChange(edge).edge;
var sourceNode = m_Graph.GetNodeFromGuid(edge1.outputSlot.nodeGuid);
var sourceSlot = sourceNode.FindOutputSlot<ISlot>(edge1.outputSlot.slotId);
var targetNode = m_Graph.GetNodeFromGuid(edge1.inputSlot.nodeGuid);
var targetSlot = targetNode.FindInputSlot<ISlot>(edge1.inputSlot.slotId);
var sourceNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == sourceNode);
if (sourceNodeView != null)
{
var sourceAnchor = sourceNodeView.outputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(sourceSlot));
var targetNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == targetNode);
var targetAnchor = targetNodeView.inputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(targetSlot));
var edgeView = new Edge();
edgeView.userData = edge1;
edgeView.output = sourceAnchor;
edgeView.output.Connect(edgeView);
edgeView.input = targetAnchor;
edgeView.input.Connect(edgeView);
m_GraphView.AddElement(edgeView);
sourceNodeView.RefreshAnchors();
targetNodeView.RefreshAnchors();
}
}
AddEdge(edge);
Add(content);
}

}
}
HashSet<MaterialNodeView> m_NodeViewHashSet = new HashSet<MaterialNodeView>();
public void HandleGraphChanges()
{
foreach (var node in m_Graph.removedNodes)

if (nodeView != null)
{
nodeView.Dispose();
nodeView.userData = null;
{
var nodeView = new MaterialNodeView(node as AbstractMaterialNode, m_PreviewSystem) { userData = node };
node.onModified += OnNodeChanged;
m_GraphView.AddElement(nodeView);
}
AddNode(node);
var nodesToUpdate = m_NodeViewHashSet;
nodesToUpdate.Clear();
foreach (var edge in m_Graph.removedEdges)
{

var nodeView = edgeView.input.node as MaterialNodeView;
if (nodeView != null && nodeView.node != null)
{
nodesToUpdate.Add(nodeView);
}
edgeView.output.Disconnect(edgeView);
edgeView.input.Disconnect(edgeView);
m_GraphView.RemoveElement(edgeView);

foreach (var edge in m_Graph.addedEdges)
{
var sourceNode = m_Graph.GetNodeFromGuid(edge.outputSlot.nodeGuid);
var sourceSlot = sourceNode.FindOutputSlot<ISlot>(edge.outputSlot.slotId);
var edgeView = AddEdge(edge);
if (edgeView != null)
nodesToUpdate.Add((MaterialNodeView)edgeView.input.node);
}
UpdateEdgeColors(nodesToUpdate);
}
void AddNode(INode node)
{
var nodeView = new MaterialNodeView(node as AbstractMaterialNode, m_PreviewSystem) { userData = node };
node.onModified += OnNodeChanged;
m_GraphView.AddElement(nodeView);
}
Edge AddEdge(IEdge edge)
{
var sourceNode = m_Graph.GetNodeFromGuid(edge.outputSlot.nodeGuid);
var sourceSlot = sourceNode.FindOutputSlot<MaterialSlot>(edge.outputSlot.slotId);
var targetNode = m_Graph.GetNodeFromGuid(edge.inputSlot.nodeGuid);
var targetSlot = targetNode.FindInputSlot<MaterialSlot>(edge.inputSlot.slotId);
var sourceNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == sourceNode);
if (sourceNodeView != null)
{
var sourceAnchor = sourceNodeView.outputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(sourceSlot));
var targetNode = m_Graph.GetNodeFromGuid(edge.inputSlot.nodeGuid);
var targetSlot = targetNode.FindInputSlot<ISlot>(edge.inputSlot.slotId);
var targetNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == targetNode);
var targetAnchor = targetNodeView.inputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(targetSlot));
var sourceNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == sourceNode);
if (sourceNodeView != null)
var edgeView = new Edge
var sourceAnchor = sourceNodeView.outputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(sourceSlot));
userData = edge,
output = sourceAnchor,
input = targetAnchor
};
edgeView.AddToClassList(sourceSlot.concreteValueType.ToString());
edgeView.output.Connect(edgeView);
edgeView.input.Connect(edgeView);
m_GraphView.AddElement(edgeView);
sourceNodeView.RefreshAnchors();
targetNodeView.RefreshAnchors();
var targetNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == targetNode);
var targetAnchor = targetNodeView.inputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(targetSlot));
return edgeView;
}
return null;
}
Stack<MaterialNodeView> m_NodeStack = new Stack<MaterialNodeView>();
var edgeView = new Edge();
edgeView.userData = edge;
edgeView.output = sourceAnchor;
edgeView.output.Connect(edgeView);
edgeView.input = targetAnchor;
edgeView.input.Connect(edgeView);
m_GraphView.AddElement(edgeView);
sourceNodeView.RefreshAnchors();
targetNodeView.RefreshAnchors();
void UpdateEdgeColors(HashSet<MaterialNodeView> nodeViews)
{
var nodeStack = m_NodeStack;
nodeStack.Clear();
foreach (var nodeView in nodeViews)
nodeStack.Push(nodeView);
while (nodeStack.Any())
{
var nodeView = nodeStack.Pop();
nodeViews.Add(nodeView);
foreach (var child in nodeView.outputContainer.Children())
{
var anchorView = child as NodeAnchor;
if (anchorView == null)
continue;
var slot = (MaterialSlot)anchorView.userData;
var className = slot.concreteValueType.ToString();
foreach (var connection in anchorView.connections)
{
connection.ClearClassList();
connection.AddToClassList("edge");
connection.AddToClassList(className);
var connectedNodeView = connection.input.node as MaterialNodeView;
if (connectedNodeView != null && !nodeViews.Contains(connectedNodeView))
nodeStack.Push(connectedNodeView);
}
}
}
}

{
foreach (var node in m_GraphView.Children().OfType<MaterialNodeView>())
node.Dispose();
m_GraphView = null;
}
if (m_GraphInspectorView != null) m_GraphInspectorView.Dispose();
if (previewSystem != null)

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs


public void Dispose()
{
node = null;
if (m_Preview != null)
{
m_Preview.onPreviewChanged -= UpdatePreviewTexture;

24
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss


background-color: rgb(0, 225, 25);
}
.edge.Matrix4, .edge.Matrix3, .edge.Matrix2 {
edge-color: #8FC1DF;
}
.edge.Texture2D {
edge-color: #FF8B8B;
}
.edge.Vector4 {
edge-color: #FBCBF4;
}
.edge.Vector3 {
edge-color: #F6FF9A;
}
.edge.Vector2 {
edge-color: #9AEF92;
}
.edge.Vector1 {
edge-color: #84E4E7;
}
GraphInspectorView {
background-color: rgb(56, 56, 56);
flex-direction: column;

正在加载...
取消
保存