CopyPasteGraph.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEditor.ShaderGraph;
  6. using UnityEditor.ShaderGraph.Internal;
  7. namespace UnityEditor.Graphing.Util
  8. {
  9. [Serializable]
  10. sealed class CopyPasteGraph : ISerializationCallbackReceiver
  11. {
  12. [NonSerialized]
  13. HashSet<IEdge> m_Edges = new HashSet<IEdge>();
  14. [NonSerialized]
  15. HashSet<AbstractMaterialNode> m_Nodes = new HashSet<AbstractMaterialNode>();
  16. [SerializeField]
  17. List<GroupData> m_Groups = new List<GroupData>();
  18. [SerializeField]
  19. List<StickyNoteData> m_StickyNotes = new List<StickyNoteData>();
  20. [NonSerialized]
  21. HashSet<ShaderInput> m_Inputs = new HashSet<ShaderInput>();
  22. // The meta properties are properties that are not copied into the target graph
  23. // but sent along to allow property nodes to still hvae the data from the original
  24. // property present.
  25. [NonSerialized]
  26. HashSet<AbstractShaderProperty> m_MetaProperties = new HashSet<AbstractShaderProperty>();
  27. // The meta keywords are keywords that are required by keyword nodes
  28. // These are copied into the target graph when there is no collision
  29. [NonSerialized]
  30. HashSet<ShaderKeyword> m_MetaKeywords = new HashSet<ShaderKeyword>();
  31. [SerializeField]
  32. string m_SourceGraphGuid;
  33. [SerializeField]
  34. List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();
  35. [SerializeField]
  36. List<SerializationHelper.JSONSerializedElement> m_SerializableEdges = new List<SerializationHelper.JSONSerializedElement>();
  37. [SerializeField]
  38. List<SerializationHelper.JSONSerializedElement> m_SerilaizeableInputs = new List<SerializationHelper.JSONSerializedElement>();
  39. [SerializeField]
  40. List<SerializationHelper.JSONSerializedElement> m_SerializableMetaProperties = new List<SerializationHelper.JSONSerializedElement>();
  41. [SerializeField]
  42. List<SerializationHelper.JSONSerializedElement> m_SerializableMetaKeywords = new List<SerializationHelper.JSONSerializedElement>();
  43. public CopyPasteGraph() {}
  44. public CopyPasteGraph(string sourceGraphGuid, IEnumerable<GroupData> groups, IEnumerable<AbstractMaterialNode> nodes, IEnumerable<IEdge> edges, IEnumerable<ShaderInput> inputs, IEnumerable<AbstractShaderProperty> metaProperties, IEnumerable<ShaderKeyword> metaKeywords, IEnumerable<StickyNoteData> notes)
  45. {
  46. m_SourceGraphGuid = sourceGraphGuid;
  47. foreach (var groupData in groups)
  48. {
  49. AddGroup(groupData);
  50. }
  51. foreach (var stickyNote in notes)
  52. {
  53. AddNote(stickyNote);
  54. }
  55. foreach (var node in nodes)
  56. {
  57. if (!node.canCopyNode)
  58. {
  59. throw new InvalidOperationException($"Cannot copy node {node.name} ({node.guid}).");
  60. }
  61. AddNode(node);
  62. foreach (var edge in NodeUtils.GetAllEdges(node))
  63. AddEdge(edge);
  64. }
  65. foreach (var edge in edges)
  66. AddEdge(edge);
  67. foreach (var input in inputs)
  68. AddInput(input);
  69. foreach (var metaProperty in metaProperties)
  70. AddMetaProperty(metaProperty);
  71. foreach (var metaKeyword in metaKeywords)
  72. AddMetaKeyword(metaKeyword);
  73. }
  74. public void AddGroup(GroupData group)
  75. {
  76. m_Groups.Add(group);
  77. }
  78. public void AddNote(StickyNoteData stickyNote)
  79. {
  80. m_StickyNotes.Add(stickyNote);
  81. }
  82. public void AddNode(AbstractMaterialNode node)
  83. {
  84. m_Nodes.Add(node);
  85. }
  86. public void AddEdge(IEdge edge)
  87. {
  88. m_Edges.Add(edge);
  89. }
  90. public void AddInput(ShaderInput input)
  91. {
  92. m_Inputs.Add(input);
  93. }
  94. public void AddMetaProperty(AbstractShaderProperty metaProperty)
  95. {
  96. m_MetaProperties.Add(metaProperty);
  97. }
  98. public void AddMetaKeyword(ShaderKeyword metaKeyword)
  99. {
  100. m_MetaKeywords.Add(metaKeyword);
  101. }
  102. public IEnumerable<T> GetNodes<T>()
  103. {
  104. return m_Nodes.OfType<T>();
  105. }
  106. public IEnumerable<GroupData> groups
  107. {
  108. get { return m_Groups; }
  109. }
  110. public IEnumerable<StickyNoteData> stickyNotes => m_StickyNotes;
  111. public IEnumerable<IEdge> edges
  112. {
  113. get { return m_Edges; }
  114. }
  115. public IEnumerable<ShaderInput> inputs
  116. {
  117. get { return m_Inputs; }
  118. }
  119. public IEnumerable<AbstractShaderProperty> metaProperties
  120. {
  121. get { return m_MetaProperties; }
  122. }
  123. public IEnumerable<ShaderKeyword> metaKeywords
  124. {
  125. get { return m_MetaKeywords; }
  126. }
  127. public string sourceGraphGuid
  128. {
  129. get { return m_SourceGraphGuid; }
  130. }
  131. public void OnBeforeSerialize()
  132. {
  133. m_SerializableNodes = SerializationHelper.Serialize<AbstractMaterialNode>(m_Nodes);
  134. m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges);
  135. m_SerilaizeableInputs = SerializationHelper.Serialize<ShaderInput>(m_Inputs);
  136. m_SerializableMetaProperties = SerializationHelper.Serialize<AbstractShaderProperty>(m_MetaProperties);
  137. m_SerializableMetaKeywords = SerializationHelper.Serialize<ShaderKeyword>(m_MetaKeywords);
  138. }
  139. public void OnAfterDeserialize()
  140. {
  141. var nodes = SerializationHelper.Deserialize<AbstractMaterialNode>(m_SerializableNodes, GraphUtil.GetLegacyTypeRemapping());
  142. m_Nodes.Clear();
  143. foreach (var node in nodes)
  144. m_Nodes.Add(node);
  145. m_SerializableNodes = null;
  146. var edges = SerializationHelper.Deserialize<IEdge>(m_SerializableEdges, GraphUtil.GetLegacyTypeRemapping());
  147. m_Edges.Clear();
  148. foreach (var edge in edges)
  149. m_Edges.Add(edge);
  150. m_SerializableEdges = null;
  151. var inputs = SerializationHelper.Deserialize<ShaderInput>(m_SerilaizeableInputs, GraphUtil.GetLegacyTypeRemapping());
  152. m_Inputs.Clear();
  153. foreach (var input in inputs)
  154. m_Inputs.Add(input);
  155. m_SerilaizeableInputs = null;
  156. var metaProperties = SerializationHelper.Deserialize<AbstractShaderProperty>(m_SerializableMetaProperties, GraphUtil.GetLegacyTypeRemapping());
  157. m_MetaProperties.Clear();
  158. foreach (var metaProperty in metaProperties)
  159. {
  160. m_MetaProperties.Add(metaProperty);
  161. }
  162. m_SerializableMetaProperties = null;
  163. var metaKeywords = SerializationHelper.Deserialize<ShaderKeyword>(m_SerializableMetaKeywords, GraphUtil.GetLegacyTypeRemapping());
  164. m_MetaKeywords.Clear();
  165. foreach (var metaKeyword in metaKeywords)
  166. {
  167. m_MetaKeywords.Add(metaKeyword);
  168. }
  169. m_SerializableMetaKeywords = null;
  170. }
  171. internal static CopyPasteGraph FromJson(string copyBuffer)
  172. {
  173. try
  174. {
  175. return JsonUtility.FromJson<CopyPasteGraph>(copyBuffer);
  176. }
  177. catch
  178. {
  179. // ignored. just means copy buffer was not a graph :(
  180. return null;
  181. }
  182. }
  183. }
  184. }