浏览代码

MaterialGraphPresenter now relies on change notifications rather than diffing

/main
Peter Bay Bastian 7 年前
当前提交
609307fe
共有 4 个文件被更改,包括 106 次插入21 次删除
  1. 21
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/GraphChange.cs
  2. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs
  3. 64
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  4. 34
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs

21
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/GraphChange.cs


using System;
namespace UnityEngine.Graphing
{
public abstract class GraphChange {}

}
public IEdge edge { get; private set; }
}
public static class GraphChangeExtensions
{
public static void Match(this GraphChange change,
Action<NodeAddedGraphChange> nodeAdded = null,
Action<NodeRemovedGraphChange> nodeRemoved = null,
Action<EdgeAddedGraphChange> edgeAdded = null,
Action<EdgeRemovedGraphChange> edgeRemoved = null)
{
if (change is NodeAddedGraphChange && nodeAdded != null)
nodeAdded((NodeAddedGraphChange)change);
else if (change is NodeRemovedGraphChange && nodeRemoved != null)
nodeRemoved((NodeRemovedGraphChange)change);
else if (change is EdgeAddedGraphChange && edgeAdded != null)
edgeAdded((EdgeAddedGraphChange)change);
else if (change is EdgeRemovedGraphChange && edgeRemoved != null)
edgeRemoved((EdgeRemovedGraphChange)change);
}
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs


this.assetName = assetName;
selectedNodes = new List<INode>();
Change(ChangeType.Graph | ChangeType.SelectedNodes | ChangeType.AssetName);
NotifyChange(ChangeType.Graph | ChangeType.SelectedNodes | ChangeType.AssetName);
Change(ChangeType.PreviewTexture);
NotifyChange(ChangeType.PreviewTexture);
}
public void UpdateSelection(IEnumerable<INode> nodes)

Change(ChangeType.SelectedNodes);
NotifyChange(ChangeType.SelectedNodes);
void Change(ChangeType changeType)
void NotifyChange(ChangeType changeType)
{
if (onChange != null)
onChange(changeType);

64
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs


using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Graphing;
using Debug = System.Diagnostics.Debug;
namespace UnityEditor.MaterialGraph.Drawing
{

void UpdateData()
{
return;
var deletedElementPresenters = new List<GraphElementPresenter>();
// Find all nodes currently being drawn which are no longer in the graph (i.e. deleted)

return;
UpdateData();
foreach (var node in graph.GetNodes<INode>())
NodeAdded(new NodeAddedGraphChange(node));
foreach (var edge in graph.edges)
EdgeAdded(new EdgeAddedGraphChange(edge));
this.graph.onChange += OnChange;
}
void OnChange(GraphChange change)
{
change.Match(NodeAdded, NodeRemoved, EdgeAdded, EdgeRemoved);
}
void NodeAdded(NodeAddedGraphChange change)
{
var nodePresenter = (MaterialNodePresenter)typeMapper.Create(change.node);
change.node.onModified += OnNodeChanged;
nodePresenter.Initialize(change.node, m_PreviewSystem);
m_Elements.Add(nodePresenter);
}
void NodeRemoved(NodeRemovedGraphChange change)
{
change.node.onModified -= OnNodeChanged;
var nodePresenter = m_Elements.OfType<MaterialNodePresenter>().FirstOrDefault(p => p.node.guid == change.node.guid);
if (nodePresenter != null)
m_Elements.Remove(nodePresenter);
}
void EdgeAdded(EdgeAddedGraphChange change)
{
var edge = change.edge;
var sourceNode = graph.GetNodeFromGuid(edge.outputSlot.nodeGuid);
var sourceSlot = sourceNode.FindOutputSlot<ISlot>(edge.outputSlot.slotId);
var sourceNodePresenter = m_Elements.OfType<MaterialNodePresenter>().FirstOrDefault(x => x.node == sourceNode);
var sourceAnchorPresenter = sourceNodePresenter.outputAnchors.OfType<GraphAnchorPresenter>().FirstOrDefault(x => x.slot == sourceSlot);
var targetNode = graph.GetNodeFromGuid(edge.inputSlot.nodeGuid);
var targetSlot = targetNode.FindInputSlot<ISlot>(edge.inputSlot.slotId);
var targetNodePresenter = m_Elements.OfType<MaterialNodePresenter>().FirstOrDefault(x => x.node == targetNode);
var targetAnchor = targetNodePresenter.inputAnchors.OfType<GraphAnchorPresenter>().FirstOrDefault(x => x.slot == targetSlot);
var edgePresenter = CreateInstance<GraphEdgePresenter>();
edgePresenter.Initialize(edge);
edgePresenter.output = sourceAnchorPresenter;
edgePresenter.output.Connect(edgePresenter);
edgePresenter.input = targetAnchor;
edgePresenter.input.Connect(edgePresenter);
m_Elements.Add(edgePresenter);
}
void EdgeRemoved(EdgeRemovedGraphChange change)
{
var edgePresenter = m_Elements.OfType<GraphEdgePresenter>().FirstOrDefault(p => p.edge == change.edge);
if (edgePresenter != null)
{
edgePresenter.output.Disconnect(edgePresenter);
edgePresenter.input.Disconnect(edgePresenter);
m_Elements.Remove(edgePresenter);
}
}
public void AddNode(INode node)

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


void OnGraphChange(GraphChange change)
{
if (change is NodeAddedGraphChange)
AddPreview(((NodeAddedGraphChange)change).node);
else if (change is NodeRemovedGraphChange)
RemovePreview(((NodeRemovedGraphChange)change).node);
else if (change is EdgeAddedGraphChange)
{
var edge = ((EdgeAddedGraphChange)change).edge;
m_DirtyShaders.Add(edge.inputSlot.nodeGuid);
}
else if (change is EdgeRemovedGraphChange)
{
var edge = ((EdgeRemovedGraphChange)change).edge;
m_DirtyShaders.Add(edge.inputSlot.nodeGuid);
}
change.Match(
nodeAdded: c => AddPreview(c.node),
nodeRemoved: c => RemovePreview(c.node),
edgeAdded: c => m_DirtyShaders.Add(c.edge.inputSlot.nodeGuid),
edgeRemoved: c => m_DirtyShaders.Add(c.edge.inputSlot.nodeGuid));
}
void AddPreview(INode node)

node.onModified -= OnNodeModified;
m_Previews.Remove(node.guid);
m_TimeDependentPreviews.Remove(node.guid);
m_DirtyPreviews.Remove(node.guid);
m_DirtyShaders.Remove(node.guid);
}
void OnNodeModified(INode node, ModificationScope scope)

foreach (var nodeGuid in m_PropertyNodeGuids)
{
var node = m_Graph.GetNodeFromGuid<AbstractMaterialNode>(nodeGuid);
if (node == null)
continue;
node.CollectPreviewMaterialProperties(m_PreviewProperties);
foreach (var prop in m_Graph.properties)
m_PreviewProperties.Add(prop.GetPreviewMaterialProperty());

var time = Time.realtimeSinceStartup;
foreach (var nodeGuid in m_DirtyPreviews)
{
var previewData = m_Previews[nodeGuid];
PreviewData previewData;
if (!m_Previews.TryGetValue(nodeGuid, out previewData))
continue;
if (previewData.shader == null)
{
previewData.texture = null;

foreach (var nodeGuid in m_DirtyPreviews)
{
var previewData = m_Previews[nodeGuid];
PreviewData previewData;
if (!m_Previews.TryGetValue(nodeGuid, out previewData))
continue;
if (previewData.onPreviewChanged != null)
previewData.onPreviewChanged();
}

正在加载...
取消
保存