GitHub
7 年前
当前提交
8e31e809
共有 52 个文件被更改,包括 834 次插入 和 999 次删除
-
377MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
-
128MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
-
206MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
-
6MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
-
15MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/SerializationTests.cs
-
4MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/AbstractMaterialGraphTests.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
-
175MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/SerializedGraphTests.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/package.json
-
4MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/GraphObject.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/Graph.meta
-
7MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestMaterialGraph.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestMaterialGraph.cs.meta
-
7MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestNode.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestNode.cs.meta
-
33MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestSlot.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestSlot.cs.meta
-
11MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/MaterialGraphChange.cs.meta
-
38MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/MaterialGraphChange.cs
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph.meta
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/Graph/GraphChange.cs.meta
-
46MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/Graph/GraphChange.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/SerializableGraph.cs.meta
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/SerializableNode.cs.meta
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/SerializableSlot.cs.meta
-
134MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/SerializableSlot.cs
-
360MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/SerializableGraph.cs
-
219MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/SerializableNode.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/Graph/IGraphAsset.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/Graph/IGraphAsset.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ListPool.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/Logging.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ObjectPool.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/SerializationHelper.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/PooledObject.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/DictionaryPool.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/QueuePool.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/StackPool.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/DictionaryPool.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ListPool.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/Logging.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ObjectPool.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/PooledObject.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/QueuePool.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/SerializationHelper.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/StackPool.cs
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Interfaces/Graph
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/GraphObject.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/GraphObject.cs
|
|||
{ |
|||
"name": "com.unity.shadergraph", |
|||
"description": "Shader Graph", |
|||
"version": "0.1.8", |
|||
"version": "0.1.9", |
|||
"unity": "2018.1", |
|||
"dependencies": { |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 20d63795c62940409089af61496db945 |
|||
timeCreated: 1513348939 |
|
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
class TestMaterialGraph : AbstractMaterialGraph |
|||
{ |
|||
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 803f320a1aae4bb28cd8234a7623f9df |
|||
timeCreated: 1513335106 |
|
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
public class TestNode : AbstractMaterialNode |
|||
{ |
|||
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5b3ff0f8519f4c0aa38ae4251a329e34 |
|||
timeCreated: 1513341923 |
|
|||
using UnityEditor.Graphing; |
|||
|
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
public class TestSlot : MaterialSlot |
|||
{ |
|||
public TestSlot(int slotId, string displayName, SlotType slotType, ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false) |
|||
: base(slotId, displayName, displayName, slotType, shaderStage, hidden) {} |
|||
|
|||
public TestSlot(int slotId, string displayName, SlotType slotType, int priority, ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false) |
|||
: base(slotId, displayName, displayName, slotType, priority, shaderStage, hidden) {} |
|||
|
|||
public override SlotValueType valueType |
|||
{ |
|||
get { throw new System.NotImplementedException(); } |
|||
} |
|||
|
|||
public override ConcreteSlotValueType concreteValueType |
|||
{ |
|||
get { throw new System.NotImplementedException(); } |
|||
} |
|||
|
|||
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode) |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
public override void CopyValuesFrom(MaterialSlot foundSlot) |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: e7e4d50a46ff471096f3c60fb06a364d |
|||
timeCreated: 1513346708 |
|
|||
fileFormatVersion: 2 |
|||
guid: 53af639a9c00c2740ba7f23e2b38cf34 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using UnityEditor.Graphing; |
|||
|
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
public class ShaderPropertyAdded : GraphChange |
|||
{ |
|||
public ShaderPropertyAdded(IShaderProperty shaderProperty) |
|||
{ |
|||
this.shaderProperty = shaderProperty; |
|||
} |
|||
|
|||
public IShaderProperty shaderProperty { get; private set; } |
|||
} |
|||
|
|||
public class ShaderPropertyRemoved : GraphChange |
|||
{ |
|||
public ShaderPropertyRemoved(Guid guid) |
|||
{ |
|||
this.guid = guid; |
|||
} |
|||
|
|||
public Guid guid { get; private set; } |
|||
} |
|||
|
|||
public class LayerRemoved : GraphChange |
|||
{ |
|||
public LayerRemoved(Guid id) |
|||
{ |
|||
this.id = id; |
|||
} |
|||
|
|||
public Guid id { get; private set; } |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: e47723110397ff04388ed94332cd6208 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: cbfac7ed81744ca281be9e21eae861c7 |
|||
timeCreated: 1505620762 |
|
|||
using System; |
|||
|
|||
namespace UnityEditor.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; } |
|||
} |
|||
|
|||
public sealed class EdgeAddedGraphChange : GraphChange |
|||
{ |
|||
public EdgeAddedGraphChange(IEdge edge) |
|||
{ |
|||
this.edge = edge; |
|||
} |
|||
|
|||
public IEdge edge { get; private set; } |
|||
} |
|||
|
|||
public sealed class EdgeRemovedGraphChange : GraphChange |
|||
{ |
|||
public EdgeRemovedGraphChange(IEdge edge) |
|||
{ |
|||
this.edge = edge; |
|||
} |
|||
|
|||
public IEdge edge { get; private set; } |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 94b39bb81d63f9c4d99f87da537af3ef |
|||
timeCreated: 1464264925 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: bd99903e0e5626448a8518284feeb047 |
|||
timeCreated: 1464264925 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 236912145b2fff1409d2b69157f5de23 |
|||
timeCreated: 1464264924 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Linq; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.Graphing |
|||
{ |
|||
[Serializable] |
|||
public class SerializableSlot : ISlot |
|||
{ |
|||
private const string kNotInit = "Not Initilaized"; |
|||
|
|||
[SerializeField] |
|||
private int m_Id; |
|||
|
|||
[SerializeField] |
|||
private string m_DisplayName = kNotInit; |
|||
|
|||
[SerializeField] |
|||
private SlotType m_SlotType = SlotType.Input; |
|||
|
|||
[SerializeField] |
|||
private int m_Priority = int.MaxValue; |
|||
|
|||
[SerializeField] |
|||
private bool m_Hidden; |
|||
|
|||
public SlotReference slotReference |
|||
{ |
|||
get { return new SlotReference(owner.guid, m_Id); } |
|||
} |
|||
|
|||
public INode owner { get; set; } |
|||
|
|||
public bool hidden |
|||
{ |
|||
get { return m_Hidden; } |
|||
set { m_Hidden = value; } |
|||
} |
|||
|
|||
public int id |
|||
{ |
|||
get { return m_Id; } |
|||
} |
|||
|
|||
public virtual string displayName |
|||
{ |
|||
get { return m_DisplayName; } |
|||
set { m_DisplayName = value; } |
|||
} |
|||
|
|||
public int priority |
|||
{ |
|||
get { return m_Priority; } |
|||
set { m_Priority = value; } |
|||
} |
|||
|
|||
public bool isInputSlot |
|||
{ |
|||
get { return m_SlotType == SlotType.Input; } |
|||
} |
|||
|
|||
public bool isOutputSlot |
|||
{ |
|||
get { return m_SlotType == SlotType.Output; } |
|||
} |
|||
|
|||
public SlotType slotType |
|||
{ |
|||
get { return m_SlotType; } |
|||
} |
|||
|
|||
// used via reflection / serialization after deserialize
|
|||
// to reconstruct this slot.
|
|||
public SerializableSlot() |
|||
{} |
|||
|
|||
public SerializableSlot(int id, string displayName, SlotType slotType, int priority, bool hidden = false) |
|||
{ |
|||
m_Id = id; |
|||
m_DisplayName = displayName; |
|||
m_SlotType = slotType; |
|||
m_Priority = priority; |
|||
m_Hidden = hidden; |
|||
} |
|||
|
|||
public SerializableSlot(int id, string displayName, SlotType slotType, bool hidden = false) |
|||
{ |
|||
m_Id = id; |
|||
m_DisplayName = displayName; |
|||
m_SlotType = slotType; |
|||
m_Hidden = hidden; |
|||
} |
|||
|
|||
protected bool Equals(SerializableSlot other) |
|||
{ |
|||
return m_Id == other.m_Id && owner.guid.Equals(other.owner.guid); |
|||
} |
|||
|
|||
public bool Equals(ISlot other) |
|||
{ |
|||
return Equals(other as object); |
|||
} |
|||
|
|||
public override bool Equals(object obj) |
|||
{ |
|||
if (ReferenceEquals(null, obj)) return false; |
|||
if (ReferenceEquals(this, obj)) return true; |
|||
if (obj.GetType() != this.GetType()) return false; |
|||
return Equals((SerializableSlot)obj); |
|||
} |
|||
|
|||
public override int GetHashCode() |
|||
{ |
|||
unchecked |
|||
{ |
|||
return (m_Id * 397) ^ (owner != null ? owner.GetHashCode() : 0); |
|||
} |
|||
} |
|||
|
|||
public bool isConnected |
|||
{ |
|||
get |
|||
{ |
|||
// node and graph respectivly
|
|||
if (owner == null || owner.owner == null) |
|||
return false; |
|||
|
|||
var graph = owner.owner; |
|||
var edges = graph.GetEdges(slotReference); |
|||
return edges.Any(); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.Graphing |
|||
{ |
|||
[Serializable] |
|||
public class SerializableGraph : IGraph, ISerializationCallbackReceiver |
|||
{ |
|||
[NonSerialized] |
|||
List<IEdge> m_Edges = new List<IEdge>(); |
|||
|
|||
[NonSerialized] |
|||
Dictionary<Guid, List<IEdge>> m_NodeEdges = new Dictionary<Guid, List<IEdge>>(); |
|||
|
|||
[NonSerialized] |
|||
Dictionary<Guid, INode> m_Nodes = new Dictionary<Guid, INode>(); |
|||
|
|||
[SerializeField] |
|||
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>(); |
|||
|
|||
[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>(); |
|||
} |
|||
|
|||
public IEnumerable<IEdge> edges |
|||
{ |
|||
get { return m_Edges; } |
|||
} |
|||
|
|||
public virtual void AddNode(INode node) |
|||
{ |
|||
AddNodeNoValidate(node); |
|||
ValidateGraph(); |
|||
} |
|||
|
|||
protected void AddNodeNoValidate(INode node) |
|||
{ |
|||
m_Nodes.Add(node.guid, node); |
|||
node.owner = this; |
|||
m_AddedNodes.Add(node); |
|||
} |
|||
|
|||
public virtual void RemoveNode(INode node) |
|||
{ |
|||
if (!node.canDeleteNode) |
|||
return; |
|||
|
|||
m_Nodes.Remove(node.guid); |
|||
m_RemovedNodes.Add(node); |
|||
ValidateGraph(); |
|||
} |
|||
|
|||
protected void RemoveNodeNoValidate(INode node) |
|||
{ |
|||
if (!node.canDeleteNode) |
|||
return; |
|||
|
|||
m_Nodes.Remove(node.guid); |
|||
m_RemovedNodes.Add(node); |
|||
} |
|||
|
|||
void AddEdgeToNodeEdges(IEdge edge) |
|||
{ |
|||
List<IEdge> inputEdges; |
|||
if (!m_NodeEdges.TryGetValue(edge.inputSlot.nodeGuid, out inputEdges)) |
|||
m_NodeEdges[edge.inputSlot.nodeGuid] = inputEdges = new List<IEdge>(); |
|||
inputEdges.Add(edge); |
|||
|
|||
List<IEdge> outputEdges; |
|||
if (!m_NodeEdges.TryGetValue(edge.outputSlot.nodeGuid, out outputEdges)) |
|||
m_NodeEdges[edge.outputSlot.nodeGuid] = outputEdges = new List<IEdge>(); |
|||
outputEdges.Add(edge); |
|||
} |
|||
|
|||
public virtual Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo> GetLegacyTypeRemapping() |
|||
{ |
|||
return new Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo>(); |
|||
} |
|||
|
|||
static List<IEdge> s_TempEdges = new List<IEdge>(); |
|||
|
|||
protected IEdge ConnectNoValidate(SlotReference fromSlotRef, SlotReference toSlotRef) |
|||
{ |
|||
var fromNode = GetNodeFromGuid(fromSlotRef.nodeGuid); |
|||
var toNode = GetNodeFromGuid(toSlotRef.nodeGuid); |
|||
|
|||
if (fromNode == null || toNode == null) |
|||
return null; |
|||
|
|||
// if fromNode is already connected to toNode
|
|||
// do now allow a connection as toNode will then
|
|||
// have an edge to fromNode creating a cycle.
|
|||
// if this is parsed it will lead to an infinite loop.
|
|||
var dependentNodes = new List<INode>(); |
|||
NodeUtils.CollectNodesNodeFeedsInto(dependentNodes, toNode); |
|||
if (dependentNodes.Contains(fromNode)) |
|||
return null; |
|||
|
|||
var fromSlot = fromNode.FindSlot<ISlot>(fromSlotRef.slotId); |
|||
var toSlot = toNode.FindSlot<ISlot>(toSlotRef.slotId); |
|||
|
|||
if (fromSlot.isOutputSlot == toSlot.isOutputSlot) |
|||
return null; |
|||
|
|||
var outputSlot = fromSlot.isOutputSlot ? fromSlotRef : toSlotRef; |
|||
var inputSlot = fromSlot.isInputSlot ? fromSlotRef : toSlotRef; |
|||
|
|||
s_TempEdges.Clear(); |
|||
GetEdges(inputSlot, s_TempEdges); |
|||
|
|||
// remove any inputs that exits before adding
|
|||
foreach (var edge in s_TempEdges) |
|||
{ |
|||
RemoveEdgeNoValidate(edge); |
|||
} |
|||
|
|||
var newEdge = new Edge(outputSlot, inputSlot); |
|||
m_Edges.Add(newEdge); |
|||
m_AddedEdges.Add(newEdge); |
|||
AddEdgeToNodeEdges(newEdge); |
|||
|
|||
//Debug.LogFormat("Connected edge: {0} -> {1} ({2} -> {3})\n{4}", newEdge.outputSlot.nodeGuid, newEdge.inputSlot.nodeGuid, fromNode.name, toNode.name, Environment.StackTrace);
|
|||
return newEdge; |
|||
} |
|||
|
|||
public virtual IEdge Connect(SlotReference fromSlotRef, SlotReference toSlotRef) |
|||
{ |
|||
var newEdge = ConnectNoValidate(fromSlotRef, toSlotRef); |
|||
ValidateGraph(); |
|||
return newEdge; |
|||
} |
|||
|
|||
public virtual void RemoveEdge(IEdge e) |
|||
{ |
|||
RemoveEdgeNoValidate(e); |
|||
ValidateGraph(); |
|||
} |
|||
|
|||
public void RemoveElements(IEnumerable<INode> nodes, IEnumerable<IEdge> edges) |
|||
{ |
|||
foreach (var edge in edges.ToArray()) |
|||
RemoveEdgeNoValidate(edge); |
|||
|
|||
foreach (var serializableNode in nodes.ToArray()) |
|||
RemoveNodeNoValidate(serializableNode); |
|||
|
|||
ValidateGraph(); |
|||
} |
|||
|
|||
protected void RemoveEdgeNoValidate(IEdge e) |
|||
{ |
|||
e = m_Edges.FirstOrDefault(x => x.Equals(e)); |
|||
if (e == null) |
|||
throw new ArgumentException("Trying to remove an edge that does not exist.", "e"); |
|||
m_Edges.Remove(e); |
|||
|
|||
List<IEdge> inputNodeEdges; |
|||
if (m_NodeEdges.TryGetValue(e.inputSlot.nodeGuid, out inputNodeEdges)) |
|||
inputNodeEdges.Remove(e); |
|||
|
|||
List<IEdge> outputNodeEdges; |
|||
if (m_NodeEdges.TryGetValue(e.outputSlot.nodeGuid, out outputNodeEdges)) |
|||
outputNodeEdges.Remove(e); |
|||
|
|||
m_RemovedEdges.Add(e); |
|||
} |
|||
|
|||
public INode GetNodeFromGuid(Guid guid) |
|||
{ |
|||
INode node; |
|||
m_Nodes.TryGetValue(guid, out node); |
|||
return node; |
|||
} |
|||
|
|||
public bool ContainsNodeGuid(Guid guid) |
|||
{ |
|||
return m_Nodes.ContainsKey(guid); |
|||
} |
|||
|
|||
public T GetNodeFromGuid<T>(Guid guid) where T : INode |
|||
{ |
|||
var node = GetNodeFromGuid(guid); |
|||
if (node is T) |
|||
return (T)node; |
|||
return default(T); |
|||
} |
|||
|
|||
public void GetEdges(SlotReference s, List<IEdge> foundEdges) |
|||
{ |
|||
var node = GetNodeFromGuid(s.nodeGuid); |
|||
if (node == null) |
|||
{ |
|||
Debug.LogWarning("Node does not exist"); |
|||
return; |
|||
} |
|||
ISlot slot = node.FindSlot<ISlot>(s.slotId); |
|||
|
|||
List<IEdge> candidateEdges; |
|||
if (!m_NodeEdges.TryGetValue(s.nodeGuid, out candidateEdges)) |
|||
return; |
|||
|
|||
foreach (var edge in candidateEdges) |
|||
{ |
|||
var cs = slot.isInputSlot ? edge.inputSlot : edge.outputSlot; |
|||
if (cs.nodeGuid == s.nodeGuid && cs.slotId == s.slotId) |
|||
foundEdges.Add(edge); |
|||
} |
|||
} |
|||
|
|||
public virtual void OnBeforeSerialize() |
|||
{ |
|||
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes.Values); |
|||
m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges); |
|||
} |
|||
|
|||
public virtual void OnAfterDeserialize() |
|||
{ |
|||
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, GetLegacyTypeRemapping()); |
|||
m_Nodes = new Dictionary<Guid, INode>(nodes.Count); |
|||
foreach (var node in nodes) |
|||
{ |
|||
node.owner = this; |
|||
node.UpdateNodeAfterDeserialization(); |
|||
m_Nodes.Add(node.guid, node); |
|||
} |
|||
|
|||
m_SerializableNodes = null; |
|||
|
|||
m_Edges = SerializationHelper.Deserialize<IEdge>(m_SerializableEdges, null); |
|||
m_SerializableEdges = null; |
|||
foreach (var edge in m_Edges) |
|||
AddEdgeToNodeEdges(edge); |
|||
} |
|||
|
|||
public virtual void ValidateGraph() |
|||
{ |
|||
//First validate edges, remove any
|
|||
//orphans. This can happen if a user
|
|||
//manually modifies serialized data
|
|||
//of if they delete a node in the inspector
|
|||
//debug view.
|
|||
foreach (var edge in edges.ToArray()) |
|||
{ |
|||
var outputNode = GetNodeFromGuid(edge.outputSlot.nodeGuid); |
|||
var inputNode = GetNodeFromGuid(edge.inputSlot.nodeGuid); |
|||
|
|||
if (outputNode == null |
|||
|| inputNode == null |
|||
|| outputNode.FindOutputSlot<ISlot>(edge.outputSlot.slotId) == null |
|||
|| inputNode.FindInputSlot<ISlot>(edge.inputSlot.slotId) == null) |
|||
{ |
|||
//orphaned edge
|
|||
RemoveEdgeNoValidate(edge); |
|||
} |
|||
} |
|||
|
|||
foreach (var node in GetNodes<INode>()) |
|||
node.ValidateNode(); |
|||
|
|||
foreach (var edge in m_AddedEdges.ToList()) |
|||
{ |
|||
if (!ContainsNodeGuid(edge.outputSlot.nodeGuid) || !ContainsNodeGuid(edge.inputSlot.nodeGuid)) |
|||
{ |
|||
Debug.LogWarningFormat("Added edge is invalid: {0} -> {1}\n{2}", edge.outputSlot.nodeGuid, edge.inputSlot.nodeGuid, Environment.StackTrace); |
|||
m_AddedEdges.Remove(edge); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public virtual void ReplaceWith(IGraph other) |
|||
{ |
|||
other.ValidateGraph(); |
|||
ValidateGraph(); |
|||
|
|||
// Current tactic is to remove all nodes and edges and then re-add them, such that depending systems
|
|||
// will re-initialize with new references.
|
|||
using (var pooledList = ListPool<IEdge>.GetDisposable()) |
|||
{ |
|||
var removedNodeEdges = pooledList.value; |
|||
removedNodeEdges.AddRange(m_Edges); |
|||
foreach (var edge in removedNodeEdges) |
|||
RemoveEdgeNoValidate(edge); |
|||
} |
|||
|
|||
using (var removedNodesPooledObject = ListPool<Guid>.GetDisposable()) |
|||
{ |
|||
var removedNodeGuids = removedNodesPooledObject.value; |
|||
removedNodeGuids.AddRange(m_Nodes.Keys); |
|||
foreach (var nodeGuid in removedNodeGuids) |
|||
RemoveNodeNoValidate(m_Nodes[nodeGuid]); |
|||
} |
|||
|
|||
ValidateGraph(); |
|||
|
|||
foreach (var node in other.GetNodes<INode>()) |
|||
AddNodeNoValidate(node); |
|||
|
|||
foreach (var edge in other.edges) |
|||
ConnectNoValidate(edge.outputSlot, edge.inputSlot); |
|||
|
|||
ValidateGraph(); |
|||
} |
|||
|
|||
public void OnEnable() |
|||
{ |
|||
foreach (var node in GetNodes<INode>().OfType<IOnAssetEnabled>()) |
|||
{ |
|||
node.OnEnable(); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.Graphing |
|||
{ |
|||
[Serializable] |
|||
public class SerializableNode : INode, ISerializationCallbackReceiver |
|||
{ |
|||
[NonSerialized] |
|||
private Guid m_Guid; |
|||
|
|||
[SerializeField] |
|||
private string m_GuidSerialized; |
|||
|
|||
[SerializeField] |
|||
private string m_Name; |
|||
|
|||
[SerializeField] |
|||
private DrawState m_DrawState; |
|||
|
|||
[NonSerialized] |
|||
private List<ISlot> m_Slots = new List<ISlot>(); |
|||
|
|||
[SerializeField] |
|||
List<SerializationHelper.JSONSerializedElement> m_SerializableSlots = new List<SerializationHelper.JSONSerializedElement>(); |
|||
|
|||
public IGraph owner { get; set; } |
|||
|
|||
public Guid guid |
|||
{ |
|||
get { return m_Guid; } |
|||
} |
|||
|
|||
public string name |
|||
{ |
|||
get { return m_Name; } |
|||
set { m_Name = value; } |
|||
} |
|||
|
|||
public virtual bool canDeleteNode |
|||
{ |
|||
get { return true; } |
|||
} |
|||
|
|||
public DrawState drawState |
|||
{ |
|||
get { return m_DrawState; } |
|||
set |
|||
{ |
|||
m_DrawState = value; |
|||
if (onModified != null) |
|||
onModified(this, ModificationScope.Node); |
|||
} |
|||
} |
|||
|
|||
public virtual bool hasError { get; protected set; } |
|||
|
|||
public SerializableNode() |
|||
{ |
|||
m_DrawState.expanded = true; |
|||
m_Guid = Guid.NewGuid(); |
|||
} |
|||
|
|||
public Guid RewriteGuid() |
|||
{ |
|||
m_Guid = Guid.NewGuid(); |
|||
return m_Guid; |
|||
} |
|||
|
|||
public virtual void ValidateNode() |
|||
{} |
|||
|
|||
public OnNodeModified onModified { get; set; } |
|||
|
|||
public void GetInputSlots<T>(List<T> foundSlots) where T : ISlot |
|||
{ |
|||
foreach (var slot in m_Slots) |
|||
{ |
|||
if (slot.isInputSlot && slot is T) |
|||
foundSlots.Add((T)slot); |
|||
} |
|||
} |
|||
|
|||
public void GetOutputSlots<T>(List<T> foundSlots) where T : ISlot |
|||
{ |
|||
foreach (var slot in m_Slots) |
|||
{ |
|||
if (slot.isOutputSlot && slot is T) |
|||
foundSlots.Add((T)slot); |
|||
} |
|||
} |
|||
|
|||
public void GetSlots<T>(List<T> foundSlots) where T : ISlot |
|||
{ |
|||
foreach (var slot in m_Slots) |
|||
{ |
|||
if (slot is T) |
|||
foundSlots.Add((T)slot); |
|||
} |
|||
} |
|||
|
|||
public virtual void AddSlot(ISlot slot) |
|||
{ |
|||
if (slot == null) |
|||
return; |
|||
|
|||
m_Slots.RemoveAll(x => x.id == slot.id); |
|||
m_Slots.Add(slot); |
|||
slot.owner = this; |
|||
|
|||
if (onModified != null) |
|||
{ |
|||
onModified(this, ModificationScope.Topological); |
|||
} |
|||
} |
|||
|
|||
public void RemoveSlot(int slotId) |
|||
{ |
|||
// Remove edges that use this slot
|
|||
// no owner can happen after creation
|
|||
// but before added to graph
|
|||
if (owner != null) |
|||
{ |
|||
var edges = owner.GetEdges(GetSlotReference(slotId)); |
|||
|
|||
foreach (var edge in edges.ToArray()) |
|||
owner.RemoveEdge(edge); |
|||
} |
|||
|
|||
//remove slots
|
|||
m_Slots.RemoveAll(x => x.id == slotId); |
|||
|
|||
if (onModified != null) |
|||
{ |
|||
onModified(this, ModificationScope.Topological); |
|||
} |
|||
} |
|||
|
|||
public void RemoveSlotsNameNotMatching(IEnumerable<int> slotIds, bool supressWarnings = false) |
|||
{ |
|||
var invalidSlots = m_Slots.Select(x => x.id).Except(slotIds); |
|||
|
|||
foreach (var invalidSlot in invalidSlots.ToArray()) |
|||
{ |
|||
if (!supressWarnings) |
|||
Debug.LogWarningFormat("Removing Invalid MaterialSlot: {0}", invalidSlot); |
|||
RemoveSlot(invalidSlot); |
|||
} |
|||
} |
|||
|
|||
public SlotReference GetSlotReference(int slotId) |
|||
{ |
|||
var slot = FindSlot<ISlot>(slotId); |
|||
if (slot == null) |
|||
throw new ArgumentException("Slot could not be found", "slotId"); |
|||
return new SlotReference(guid, slotId); |
|||
} |
|||
|
|||
public T FindSlot<T>(int slotId) where T : ISlot |
|||
{ |
|||
foreach (var slot in m_Slots) |
|||
{ |
|||
if (slot.id == slotId && slot is T) |
|||
return (T)slot; |
|||
} |
|||
return default(T); |
|||
} |
|||
|
|||
public T FindInputSlot<T>(int slotId) where T : ISlot |
|||
{ |
|||
foreach (var slot in m_Slots) |
|||
{ |
|||
if (slot.isInputSlot && slot.id == slotId && slot is T) |
|||
return (T)slot; |
|||
} |
|||
return default(T); |
|||
} |
|||
|
|||
public T FindOutputSlot<T>(int slotId) where T : ISlot |
|||
{ |
|||
foreach (var slot in m_Slots) |
|||
{ |
|||
if (slot.isOutputSlot && slot.id == slotId && slot is T) |
|||
return (T)slot; |
|||
} |
|||
return default(T); |
|||
} |
|||
|
|||
public virtual IEnumerable<ISlot> GetInputsWithNoConnection() |
|||
{ |
|||
return this.GetInputSlots<ISlot>().Where(x => !owner.GetEdges(GetSlotReference(x.id)).Any()); |
|||
} |
|||
|
|||
public virtual void OnBeforeSerialize() |
|||
{ |
|||
m_GuidSerialized = m_Guid.ToString(); |
|||
m_SerializableSlots = SerializationHelper.Serialize<ISlot>(m_Slots); |
|||
} |
|||
|
|||
public virtual void OnAfterDeserialize() |
|||
{ |
|||
if (!string.IsNullOrEmpty(m_GuidSerialized)) |
|||
m_Guid = new Guid(m_GuidSerialized); |
|||
else |
|||
m_Guid = Guid.NewGuid(); |
|||
|
|||
m_Slots = SerializationHelper.Deserialize<ISlot>(m_SerializableSlots, null); |
|||
m_SerializableSlots = null; |
|||
foreach (var s in m_Slots) |
|||
s.owner = this; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
public virtual void UpdateNodeAfterDeserialization() |
|||
{} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue