浏览代码

Replace OnGraphChange with polling-based change tracking system

/main
Peter Bay Bastian 7 年前
当前提交
867e4ea2
共有 8 个文件被更改,包括 173 次插入113 次删除
  1. 68
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs
  2. 8
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraph.cs
  3. 27
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  4. 27
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/LayeredShaderGraph.cs
  5. 53
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  6. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  7. 37
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs
  8. 58
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs

68
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs


[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableEdges = new List<SerializationHelper.JSONSerializedElement>();
[NonSerialized]
List<INode> m_AddedNodes = new List<INode>();
[NonSerialized]
List<INode> m_RemovedNodes = new List<INode>();
[NonSerialized]
List<IEdge> m_AddedEdges = new List<IEdge>();
[NonSerialized]
List<IEdge> m_RemovedEdges = new List<IEdge>();
public IEnumerable<INode> addedNodes
{
get { return m_AddedNodes; }
}
public IEnumerable<INode> removedNodes
{
get { return m_RemovedNodes; }
}
public IEnumerable<IEdge> addedEdges
{
get { return m_AddedEdges; }
}
public IEnumerable<IEdge> removedEdges
{
get { return m_RemovedEdges; }
}
public IGraphObject owner { get; set; }
public virtual void ClearChanges()
{
m_AddedNodes.Clear();
m_RemovedNodes.Clear();
m_AddedEdges.Clear();
m_RemovedEdges.Clear();
}
public IEnumerable<T> GetNodes<T>() where T : INode
{
return m_Nodes.Values.OfType<T>();

{
m_Nodes.Add(node.guid, node);
node.owner = this;
NotifyChange(new NodeAddedGraphChange(node));
m_AddedNodes.Add(node);
}
public virtual void RemoveNode(INode node)

m_Nodes.Remove(node.guid);
NotifyChange(new NodeRemovedGraphChange(node));
m_RemovedNodes.Add(node);
ValidateGraph();
}

return;
m_Nodes.Remove(node.guid);
NotifyChange(new NodeRemovedGraphChange(node));
m_RemovedNodes.Add(node);
}
void AddEdgeToNodeEdges(IEdge edge)

var newEdge = new Edge(outputSlot, inputSlot);
m_Edges.Add(newEdge);
NotifyChange(new EdgeAddedGraphChange(newEdge));
m_AddedEdges.Add(newEdge);
AddEdgeToNodeEdges(newEdge);
Debug.Log("Connected edge: " + newEdge);

if (m_NodeEdges.TryGetValue(e.outputSlot.nodeGuid, out outputNodeEdges))
outputNodeEdges.Remove(e);
NotifyChange(new EdgeRemovedGraphChange(e));
m_RemovedEdges.Add(e);
}
public INode GetNodeFromGuid(Guid guid)

AddEdgeToNodeEdges(edge);
OnEnable();
ValidateGraph();
ValidateGraph();
}
public virtual void ValidateGraph()

//manually modifies serialized data
//of if they delete a node in the inspector
//debug view.
foreach (var edge in edges.ToArray())
//debug view.
foreach (var edge in edges.ToArray())
{
var outputNode = GetNodeFromGuid(edge.outputSlot.nodeGuid);
var inputNode = GetNodeFromGuid(edge.inputSlot.nodeGuid);

{
node.OnEnable();
}
}
public OnGraphChange onChange { get; set; }
public IGraphObject owner { get; set; }
protected void NotifyChange(GraphChange change)
{
if (onChange != null)
onChange(change);
}
}
}

8
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraph.cs


namespace UnityEngine.Graphing
{
public delegate void OnGraphChange(GraphChange change);
public interface IGraph : IOnAssetEnabled
{
IEnumerable<T> GetNodes<T>() where T : INode;

IEnumerable<IEdge> GetEdges(SlotReference s);
void ValidateGraph();
void ReplaceWith(IGraph other);
OnGraphChange onChange { get; set; }
IEnumerable<INode> addedNodes { get; }
IEnumerable<INode> removedNodes { get; }
IEnumerable<IEdge> addedEdges { get; }
IEnumerable<IEdge> removedEdges { get; }
void ClearChanges();
}
}

27
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs


[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializedProperties = new List<SerializationHelper.JSONSerializedElement>();
[NonSerialized]
List<IShaderProperty> m_AddedProperties = new List<IShaderProperty>();
[NonSerialized]
List<Guid> m_RemovedProperties = new List<Guid>();
public IEnumerable<IShaderProperty> addedProperties
{
get { return m_AddedProperties; }
}
public IEnumerable<Guid> removedProperties
{
get { return m_RemovedProperties; }
}
public override void ClearChanges()
{
base.ClearChanges();
m_AddedProperties.Clear();
m_RemovedProperties.Clear();
}
public override void AddNode(INode node)
{
if (node is AbstractMaterialNode)

return;
m_Properties.Add(property);
NotifyChange(new ShaderPropertyAdded(property));
m_AddedProperties.Add(property);
NotifyChange(new ShaderPropertyRemoved(guid));
m_RemovedProperties.Add(guid);
}
public override Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo> GetLegacyTypeRemapping()

27
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/LayeredShaderGraph.cs


[NonSerialized]
private List<Layer> m_Layers = new List<Layer>();
[NonSerialized]
List<Layer> m_AddedLayers = new List<Layer>();
[NonSerialized]
List<Guid> m_RemovedLayers = new List<Guid>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializedLayers = new List<SerializationHelper.JSONSerializedElement>();

}
public List<Layer> addedLayers
{
get { return m_AddedLayers; }
}
public List<Guid> removedLayers
{
get { return m_RemovedLayers; }
}
[NonSerialized]
private LayerWeightsOutputNode m_OutputNode;

}
}
public override void ClearChanges()
{
base.ClearChanges();
m_AddedLayers.Clear();
m_RemovedLayers.Clear();
}
public override void AddNode(INode node)
{
if (outputNode != null && node is LayerWeightsOutputNode)

{
var layer = new Layer();
m_Layers.Add(layer);
NotifyChange(new LayerAdded(layer));
m_AddedLayers.Add(layer);
if (outputNode != null)
outputNode.onModified(outputNode, ModificationScope.Graph);

if (num > 0)
{
NotifyChange(new LayerRemoved(id));
m_RemovedLayers.Add(id);
if (outputNode != null)
outputNode.onModified(outputNode, ModificationScope.Graph);

53
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Experimental.UIElements;
using UnityEditor.Graphing.Util;
using UnityEngine;
using UnityEngine.Experimental.UIElements;

}
bottomContainer.Add(propertiesContainer);
//if (m_Presenter.graph is LayeredShaderGraph)
if (m_Graph is LayeredShaderGraph)
{
var layersContainer = new VisualElement {name = "properties"};
{

foreach (var property in m_Graph.properties)
m_PropertyItems.Add(new ShaderPropertyView(m_Graph, property));
m_Graph.onChange += OnGraphChange;
var layerGraph = m_Graph as LayeredShaderGraph;
if (layerGraph != null)

}
}
void OnGraphChange(GraphChange change)
public void HandleGraphChanges()
var propertyAdded = change as ShaderPropertyAdded;
if (propertyAdded != null)
foreach (var propertyGuid in m_Graph.removedProperties)
m_PropertyItems.Add(new ShaderPropertyView(m_Graph, propertyAdded.shaderProperty));
return;
var propertyView = m_PropertyItems.OfType<ShaderPropertyView>().FirstOrDefault(v => v.property.guid == propertyGuid);if (propertyView != null)
m_PropertyItems.Remove(propertyView);
var propertyRemoved = change as ShaderPropertyRemoved;
if (propertyRemoved != null)
{
var propertyView = m_PropertyItems.OfType<ShaderPropertyView>().FirstOrDefault(v => v.property.guid == propertyRemoved.guid);
if (propertyView != null)
m_PropertyItems.Remove(propertyView);
return;
}
foreach (var property in m_Graph.addedProperties)
m_PropertyItems.Add(new ShaderPropertyView(m_Graph, property));
var layerAdded = change as LayerAdded;
if (layerAdded != null)
m_LayerItems.Add(new ShaderLayerView(layerGraph, layerAdded.layer));
var layerRemoved = change as LayerRemoved;
if (layerRemoved != null)
foreach (var id in layerGraph.removedLayers)
var view = m_LayerItems.OfType<ShaderLayerView>().FirstOrDefault(v => v.layer.guid == layerRemoved.id);
var view = m_LayerItems.OfType<ShaderLayerView>().FirstOrDefault(v => v.layer.guid == id);
foreach (var layer in layerGraph.addedLayers)
m_LayerItems.Add(new ShaderLayerView(layerGraph, layer));
var nodeAdded = change as NodeAddedGraphChange;
if (nodeAdded != null)
if (masterNode != null)
var node = nodeAdded.node as MasterNode;
if (node != null && masterNode == null)
masterNode = node;
return;
if (m_Graph.removedNodes.Contains(masterNode))
masterNode = null;
var nodeRemoved = change as NodeRemovedGraphChange;
if (nodeRemoved != null)
if (masterNode == null)
if (nodeRemoved.node == masterNode)
masterNode = null;
return;
var addedMasterNode = m_Graph.addedNodes.OfType<MasterNode>().FirstOrDefault();
if (addedMasterNode != null)
masterNode = addedMasterNode;
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


return;
if (graphEditorView == null)
graphEditorView = new GraphEditorView(materialGraph, selected) { persistenceKey = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(selected)) };
if (graphEditorView != null)
graphEditorView.previewSystem.Update();
graphEditorView.previewSystem.HandleGraphChanges();
graphEditorView.previewSystem.RenderPreviews();
graphEditorView.HandleGraphChanges();
graphEditorView.inspectorView.HandleGraphChanges();
graphObject.graph.ClearChanges();
}
void OnDisable()

37
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs


foreach (var node in m_Graph.GetNodes<INode>())
AddPreview(node);
m_Graph.onChange += OnGraphChange;
}
void OnGraphChange(GraphChange change)
{
var nodeAdded = change as NodeAddedGraphChange;
if (nodeAdded != null)
AddPreview(nodeAdded.node);
var nodeRemoved = change as NodeRemovedGraphChange;
if (nodeRemoved != null)
RemovePreview(nodeRemoved.node);
var edgeAdded = change as EdgeAddedGraphChange;
if (edgeAdded != null)
m_DirtyShaders.Add(edgeAdded.edge.inputSlot.nodeGuid);
var edgeRemoved = change as EdgeRemovedGraphChange;
if (edgeRemoved != null)
m_DirtyShaders.Add(edgeRemoved.edge.inputSlot.nodeGuid);
}
void AddPreview(INode node)

HashSet<Guid> m_PropertyNodeGuids = new HashSet<Guid>();
List<PreviewProperty> m_PreviewProperties = new List<PreviewProperty>();
public void Update()
public void HandleGraphChanges()
{
foreach (var node in m_Graph.removedNodes)
RemovePreview(node);
foreach (var node in m_Graph.addedNodes)
AddPreview(node);
foreach (var edge in m_Graph.removedEdges)
m_DirtyShaders.Add(edge.inputSlot.nodeGuid);
foreach (var edge in m_Graph.addedEdges)
m_DirtyShaders.Add(edge.inputSlot.nodeGuid);
}
public void RenderPreviews()
{
if (previewRate == PreviewRate.Off)
return;

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


set { m_PreviewSystem = value; }
}
public GraphInspectorView inspectorView
{
get { return m_GraphInspectorView; }
}
public GraphEditorView(AbstractMaterialGraph graph, Object asset)
{
m_Graph = graph;

}
}
graph.onChange += OnGraphChange;
Add(content);
}

}
}
void OnGraphChange(GraphChange change)
public void HandleGraphChanges()
var nodeAdded = change as NodeAddedGraphChange;
if (nodeAdded != null)
foreach (var node in m_Graph.removedNodes)
{
node.onModified -= OnNodeChanged;
var nodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(p => p.node != null && p.node.guid == node.guid);
if (nodeView != null)
m_GraphView.RemoveElement(nodeView);
}
foreach (var node in m_Graph.addedNodes)
var nodeView = new MaterialNodeView(nodeAdded.node as AbstractMaterialNode, m_PreviewSystem);
nodeView.userData = nodeAdded.node;
nodeAdded.node.onModified += OnNodeChanged;
var nodeView = new MaterialNodeView(node as AbstractMaterialNode, m_PreviewSystem) { userData = node };
node.onModified += OnNodeChanged;
var nodeRemoved = change as NodeRemovedGraphChange;
if (nodeRemoved != null)
foreach (var edge in m_Graph.removedEdges)
nodeRemoved.node.onModified -= OnNodeChanged;
var nodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(p => p.node != null && p.node.guid == nodeRemoved.node.guid);
if (nodeView != null)
m_GraphView.RemoveElement(nodeView);
var edgeView = m_GraphView.graphElements.ToList().OfType<Edge>().FirstOrDefault(p => p.userData is IEdge && Equals((IEdge)p.userData, edge));
if (edgeView != null)
{
edgeView.output.Disconnect(edgeView);
edgeView.input.Disconnect(edgeView);
m_GraphView.RemoveElement(edgeView);
}
var edgeAdded = change as EdgeAddedGraphChange;
if (edgeAdded != null)
foreach (var edge in m_Graph.addedEdges)
var edge = edgeAdded.edge;
var sourceNode = m_Graph.GetNodeFromGuid(edge.outputSlot.nodeGuid);
var sourceSlot = sourceNode.FindOutputSlot<ISlot>(edge.outputSlot.slotId);

m_GraphView.AddElement(edgeView);
}
}
var edgeRemoved = change as EdgeRemovedGraphChange;
if (edgeRemoved != null)
{
var edgeView = m_GraphView.graphElements.ToList().OfType<Edge>().FirstOrDefault(p => p.userData is IEdge && Equals((IEdge)p.userData, edgeRemoved.edge));
if (edgeView != null)
{
edgeView.output.Disconnect(edgeView);
edgeView.input.Disconnect(edgeView);
m_GraphView.RemoveElement(edgeView);
}
}
}
public void Dispose()

onShowInProjectClick = null;
m_Graph.onChange -= OnGraphChange;
if (m_GraphInspectorView != null) m_GraphInspectorView.Dispose();
if (previewSystem != null)
{

正在加载...
取消
保存