浏览代码

Add change notifications for graph, used for managing previews better

/main
Peter Bay Bastian 7 年前
当前提交
a93f2736
共有 7 个文件被更改,包括 83 次插入79 次删除
  1. 11
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs
  2. 3
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/IGraph.cs
  3. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs
  4. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialNodePresenter.cs
  5. 100
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs
  6. 24
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/GraphChange.cs
  7. 3
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/GraphChange.cs.meta

11
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs


{
m_Nodes.Add(node.guid, node);
node.owner = this;
NotifyChange(new NodeAddedGraphChange(node));
ValidateGraph();
}

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

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

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

3
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/IGraph.cs


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

T GetNodeFromGuid<T>(Guid guid) where T : INode;
IEnumerable<IEdge> GetEdges(SlotReference s);
void ValidateGraph();
OnGraphChange onChange { get; set; }
}
}

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


{
public class GraphInspectorPresenter : ScriptableObject, IDisposable
{
PreviewHandle m_PreviewHandle;
PreviewData m_PreviewHandle;
public IGraph graph { get; set; }

var masterNode = graph.GetNodes<AbstractMasterNode>().FirstOrDefault();
if (masterNode != null)
{
m_PreviewHandle = previewSystem.GetPreviewHandle(masterNode.guid);
m_PreviewHandle = previewSystem.GetPreview(masterNode);
m_PreviewHandle.onPreviewChanged += OnPreviewChanged;
}
this.graph = graph;

{
if (m_PreviewHandle != null)
{
m_PreviewHandle.Dispose();
m_PreviewHandle.onPreviewChanged -= OnPreviewChanged;
m_PreviewHandle = null;
};
}

15
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialNodePresenter.cs


[SerializeField]
int m_Version;
PreviewHandle m_PreviewHandle;
PreviewData m_Preview;
public Texture previewTexture { get; private set; }

m_Version = 0;
m_PreviewHandle = previewSystem.GetPreviewHandle(inNode.guid);
m_PreviewHandle.onPreviewChanged += OnPreviewChanged;
m_Preview = previewSystem.GetPreview(inNode);
m_Preview.onPreviewChanged += OnPreviewChanged;
previewTexture = m_PreviewHandle.texture;
previewTexture = m_Preview.texture;
if (m_PreviewHandle != null)
if (m_Preview != null)
m_PreviewHandle.Dispose();
m_PreviewHandle = null;
m_Preview.onPreviewChanged -= OnPreviewChanged;
m_Preview = null;
}
}
}

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


using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

m_ErrorTexture.SetPixel(1, 0, Color.black);
m_ErrorTexture.SetPixel(1, 1, Color.magenta);
m_ErrorTexture.Apply();
m_Graph.onChange += OnGraphChange;
foreach (var node in m_Graph.GetNodes<INode>())
AddPreview(node);
public PreviewHandle GetPreviewHandle(Guid nodeGuid)
public PreviewData GetPreview(INode node)
{
return m_Previews[node.guid];
}
void OnGraphChange(GraphChange change)
{
if (change is NodeAddedGraphChange)
AddPreview(((NodeAddedGraphChange)change).node);
else if (change is NodeRemovedGraphChange)
RemovePreview(((NodeRemovedGraphChange)change).node);
}
void AddPreview(INode node)
PreviewData previewData;
if (!m_Previews.TryGetValue(nodeGuid, out previewData))
var previewData = new PreviewData
var node = m_Graph.GetNodeFromGuid(nodeGuid);
if (node == null)
throw new ArgumentException("Node does not exist.", "nodeGuid");
previewData = new PreviewData();
previewData.renderTexture = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default) { hideFlags = HideFlags.HideAndDontSave };
m_Previews.Add(nodeGuid, previewData);
m_DirtyShaders.Add(nodeGuid);
m_DirtyPreviews.Add(nodeGuid);
node.onModified += OnNodeModified;
}
previewData.refCount++;
return new PreviewHandle(nodeGuid, previewData, this);
renderTexture = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default) { hideFlags = HideFlags.HideAndDontSave }
};
m_Previews.Add(node.guid, previewData);
m_DirtyShaders.Add(node.guid);
node.onModified += OnNodeModified;
}
void RemovePreview(INode node)
{
node.onModified -= OnNodeModified;
m_Previews.Remove(node.guid);
}
void OnNodeModified(INode node, ModificationScope scope)

}
}
public void DestroyPreviewHandle(Guid nodeGuid)
{
PreviewData previewData;
if (!m_Previews.TryGetValue(nodeGuid, out previewData))
return;
previewData.refCount--;
if (previewData.refCount == 0)
DestroyPreview(nodeGuid, previewData);
}
void DestroyPreview(Guid nodeGuid, PreviewData previewData)
{
if (m_Previews.Remove(nodeGuid))

public PreviewMode previewMode { get; set; }
public RenderTexture renderTexture { get; set; }
public Texture texture { get; set; }
public int refCount { get; set; }
}
public class PreviewHandle : IDisposable
{
Guid m_NodeGuid;
PreviewData m_PreviewData;
PreviewSystem m_PreviewSystem;
public PreviewHandle(Guid nodeGuid, PreviewData previewData, PreviewSystem previewSystem)
{
m_NodeGuid = nodeGuid;
m_PreviewData = previewData;
m_PreviewSystem = previewSystem;
}
public OnPreviewChanged onPreviewChanged
{
get { return m_PreviewData.onPreviewChanged; }
set { m_PreviewData.onPreviewChanged = value; }
}
public Texture texture
{
get { return m_PreviewData.texture; }
}
void ReleaseUnmanagedResources()
{
m_PreviewSystem.DestroyPreviewHandle(m_NodeGuid);
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~PreviewHandle()
{
ReleaseUnmanagedResources();
}
}
}

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


namespace UnityEngine.Graphing
{
public abstract class GraphChange {}
public sealed class NodeAddedGraphChange : GraphChange
{
public NodeAddedGraphChange(INode node)
{
this.node = node;
}
public INode node { get; private set; }
}
public sealed class NodeRemovedGraphChange : GraphChange
{
public NodeRemovedGraphChange(INode node)
{
this.node = node;
}
public INode node { get; private set; }
}
}

3
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/GraphChange.cs.meta


fileFormatVersion: 2
guid: cbfac7ed81744ca281be9e21eae861c7
timeCreated: 1505620762
正在加载...
取消
保存