VFXCopy.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEditor.VFX;
  6. using UnityEngine.UIElements;
  7. using System.Reflection;
  8. using NodeID = System.UInt32;
  9. namespace UnityEditor.VFX.UI
  10. {
  11. class VFXCopy : VFXCopyPasteCommon
  12. {
  13. VFXContextController[] contexts;
  14. int[] contextsIndices;
  15. VFXOperatorController[] operators;
  16. int[] operatorIndices;
  17. VFXParameterNodeController[] parameters;
  18. int[] parameterIndices;
  19. VFXData[] datas;
  20. Dictionary<VFXNodeController, uint> modelIndices = new Dictionary<VFXNodeController, NodeID>();
  21. static VFXCopy s_Instance;
  22. public static object Copy(IEnumerable<Controller> elements, Rect bounds)
  23. {
  24. if (s_Instance == null)
  25. s_Instance = new VFXCopy();
  26. return s_Instance.CreateCopy(elements, bounds);
  27. }
  28. public static string SerializeElements(IEnumerable<Controller> elements, Rect bounds)
  29. {
  30. if (s_Instance == null)
  31. s_Instance = new VFXCopy();
  32. var serializableGraph = s_Instance.CreateCopy(elements, bounds) as SerializableGraph;
  33. return JsonUtility.ToJson(serializableGraph);
  34. }
  35. public static object CopyBlocks(IEnumerable<VFXBlockController> blocks)
  36. {
  37. if (s_Instance == null)
  38. s_Instance = new VFXCopy();
  39. return s_Instance.DoCopyBlocks(blocks);
  40. }
  41. object CreateCopy(IEnumerable<Controller> elements, Rect bounds)
  42. {
  43. IEnumerable<VFXContextController> contexts = elements.OfType<VFXContextController>();
  44. IEnumerable<VFXNodeController> nodes = elements.Where(t => t is VFXOperatorController || t is VFXParameterNodeController).Cast<VFXNodeController>();
  45. IEnumerable<VFXBlockController> blocks = elements.OfType<VFXBlockController>();
  46. SerializableGraph serializableGraph = new SerializableGraph();
  47. serializableGraph.controllerCount = elements.Count();
  48. if (contexts.Count() == 0 && nodes.Count() == 0 && blocks.Count() > 0)
  49. {
  50. var copiedBlocks = new List<VFXBlockController>(blocks);
  51. modelIndices.Clear();
  52. serializableGraph.operators = CopyBlocks(copiedBlocks, 0);
  53. serializableGraph.blocksOnly = true;
  54. }
  55. else
  56. {
  57. //Don't copy VFXBlockSubgraphContext because they can't be pasted anywhere.
  58. CopyNodes(serializableGraph, elements, contexts.Where(t => !(t.model is VFXBlockSubgraphContext)), nodes, bounds);
  59. }
  60. return serializableGraph;
  61. }
  62. void CopyNodes(SerializableGraph serializableGraph, IEnumerable<Controller> elements, IEnumerable<VFXContextController> copiedContexts, IEnumerable<VFXNodeController> nodes, Rect bounds)
  63. {
  64. Controller[] copiedElements = elements.ToArray();
  65. serializableGraph.bounds = bounds;
  66. IEnumerable<VFXNodeController> dataEdgeTargets = nodes.Concat(copiedContexts.Cast<VFXNodeController>()).Concat(copiedContexts.SelectMany(t => t.blockControllers).Cast<VFXNodeController>()).ToArray();
  67. // consider only edges contained in the selection
  68. IEnumerable<VFXDataEdgeController> dataEdges = elements.OfType<VFXDataEdgeController>().Where(t =>
  69. dataEdgeTargets.Contains((t.input as VFXDataAnchorController).sourceNode as VFXNodeController) &&
  70. dataEdgeTargets.Contains((t.output as VFXDataAnchorController).sourceNode as VFXNodeController)).ToArray();
  71. IEnumerable<VFXFlowEdgeController> flowEdges = elements.OfType<VFXFlowEdgeController>().Where(t =>
  72. copiedContexts.Contains((t.input as VFXFlowAnchorController).context) &&
  73. copiedContexts.Contains((t.output as VFXFlowAnchorController).context)
  74. ).ToArray();
  75. modelIndices.Clear();
  76. contexts = copiedContexts.ToArray();
  77. operators = nodes.OfType<VFXOperatorController>().ToArray();
  78. parameters = nodes.OfType<VFXParameterNodeController>().ToArray();
  79. contextsIndices = contexts.Select(t => Array.IndexOf(copiedElements, t)).ToArray();
  80. operatorIndices = operators.Select(t => Array.IndexOf(copiedElements, t)).ToArray();
  81. parameterIndices = parameters.Select(t => Array.IndexOf(copiedElements, t)).ToArray();
  82. datas = contexts.Select(t => t.model.GetData()).Where(t => t != null).ToArray();
  83. CopyOperatorsAndContexts(ref serializableGraph);
  84. CopyParameters(ref serializableGraph);
  85. CopyGroupNodesAndStickyNotes(ref serializableGraph, elements);
  86. CopyDatas(ref serializableGraph);
  87. CopyDataEdge(ref serializableGraph, dataEdges);
  88. CopyFlowEdges(ref serializableGraph, flowEdges);
  89. }
  90. void CopyGroupNodesAndStickyNotes(ref SerializableGraph serializableGraph, IEnumerable<Controller> elements)
  91. {
  92. VFXGroupNodeController[] groupNodes = elements.OfType<VFXGroupNodeController>().ToArray();
  93. VFXStickyNoteController[] stickyNotes = elements.OfType<VFXStickyNoteController>().ToArray();
  94. if (groupNodes.Length > 0 || stickyNotes.Length > 0)
  95. {
  96. var stickyNodeIndexToCopiedIndex = new Dictionary<int, int>();
  97. if (stickyNotes.Length > 0)
  98. {
  99. serializableGraph.stickyNotes = new VFXUI.StickyNoteInfo[stickyNotes.Length];
  100. for (int i = 0; i < stickyNotes.Length; ++i)
  101. {
  102. VFXStickyNoteController stickyNote = stickyNotes[i];
  103. stickyNodeIndexToCopiedIndex[stickyNote.index] = i;
  104. VFXUI.StickyNoteInfo info = stickyNote.model.stickyNoteInfos[stickyNote.index];
  105. serializableGraph.stickyNotes[i] = new VFXUI.StickyNoteInfo(info);
  106. }
  107. }
  108. if (groupNodes.Length > 0)
  109. {
  110. serializableGraph.groupNodes = new GroupNode[groupNodes.Length];
  111. for (int i = 0; i < groupNodes.Length; ++i)
  112. {
  113. VFXGroupNodeController groupNode = groupNodes[i];
  114. VFXUI.GroupInfo info = groupNode.model.groupInfos[groupNode.index];
  115. serializableGraph.groupNodes[i] = new GroupNode { infos = new VFXUI.UIInfo(info) };
  116. // only keep nodes and sticky notes that are copied because a element can not be in two groups at the same time.
  117. if (info.contents != null)
  118. {
  119. var nodeIndices = groupNode.nodes.OfType<VFXNodeController>().Where(t => contexts.Contains(t) || operators.Contains(t) || parameters.Contains(t)).Select(t => modelIndices[t]);
  120. var stickNoteIndices = info.contents.Where(t => t.isStickyNote && stickyNodeIndexToCopiedIndex.ContainsKey(t.id)).Select(t => (uint)stickyNodeIndexToCopiedIndex[t.id]);
  121. serializableGraph.groupNodes[i].contents = nodeIndices.Concat(stickNoteIndices).ToArray();
  122. serializableGraph.groupNodes[i].stickNodeCount = stickNoteIndices.Count();
  123. }
  124. }
  125. }
  126. }
  127. }
  128. void CopyDataEdge(ref SerializableGraph serializableGraph, IEnumerable<VFXDataEdgeController> dataEdges)
  129. {
  130. serializableGraph.dataEdges = new DataEdge[dataEdges.Count()];
  131. int cpt = 0;
  132. foreach (var edge in dataEdges)
  133. {
  134. DataEdge copyPasteEdge = new DataEdge();
  135. var inputController = edge.input as VFXDataAnchorController;
  136. var outputController = edge.output as VFXDataAnchorController;
  137. copyPasteEdge.input.slotPath = MakeSlotPath(inputController.model, true);
  138. copyPasteEdge.input.targetIndex = modelIndices[inputController.sourceNode];
  139. copyPasteEdge.output.slotPath = MakeSlotPath(outputController.model, false);
  140. copyPasteEdge.output.targetIndex = modelIndices[outputController.sourceNode];
  141. serializableGraph.dataEdges[cpt++] = copyPasteEdge;
  142. }
  143. }
  144. void CopyFlowEdges(ref SerializableGraph serializableGraph, IEnumerable<VFXFlowEdgeController> flowEdges)
  145. {
  146. serializableGraph.flowEdges = new FlowEdge[flowEdges.Count()];
  147. int cpt = 0;
  148. foreach (var edge in flowEdges)
  149. {
  150. FlowEdge copyPasteEdge = new FlowEdge();
  151. var inputController = edge.input as VFXFlowAnchorController;
  152. var outputController = edge.output as VFXFlowAnchorController;
  153. copyPasteEdge.input.contextIndex = modelIndices[inputController.context];
  154. copyPasteEdge.input.flowIndex = inputController.slotIndex;
  155. copyPasteEdge.output.contextIndex = modelIndices[outputController.context];
  156. copyPasteEdge.output.flowIndex = outputController.slotIndex;
  157. serializableGraph.flowEdges[cpt++] = copyPasteEdge;
  158. }
  159. }
  160. void CopyDatas(ref SerializableGraph serializableGraph)
  161. {
  162. serializableGraph.datas = new Data[datas.Length];
  163. for (int i = 0; i < datas.Length; ++i)
  164. {
  165. CopyModelSettings(ref serializableGraph.datas[i].settings, datas[i]);
  166. }
  167. }
  168. void CopyModelSettings(ref Property[] properties, VFXModel model)
  169. {
  170. // Copy all fields that are either VFXSettings or serialized by unity
  171. Type type = model.GetType();
  172. var fields = GetFields(type);
  173. properties = new Property[fields.Count()];
  174. for (int i = 0; i < properties.Length; ++i)
  175. {
  176. properties[i].name = fields[i].Name;
  177. properties[i].value = new VFXSerializableObject(fields[i].FieldType, fields[i].GetValue(model));
  178. }
  179. }
  180. ParameterNode CopyParameterNode(int parameterIndex, int nodeIndex, VFXParameterNodeController controller, int indexInClipboard)
  181. {
  182. ParameterNode n = new ParameterNode();
  183. n.position = controller.position;
  184. n.collapsed = controller.superCollapsed;
  185. n.expandedOutput = controller.infos.expandedSlots.Select(t => t.path).ToArray();
  186. n.indexInClipboard = indexInClipboard;
  187. if (parameterIndex < (1 << 18) && nodeIndex < (1 << 11))
  188. modelIndices[controller] = GetParameterNodeID((uint)parameterIndex, (uint)nodeIndex);
  189. else
  190. modelIndices[controller] = InvalidID;
  191. return n;
  192. }
  193. void CopyParameters(ref SerializableGraph serializableGraph)
  194. {
  195. int cpt = 0;
  196. serializableGraph.parameters = parameters.GroupBy(t => t.parentController, t => t, (p, c) =>
  197. {
  198. ++cpt;
  199. return new Parameter()
  200. {
  201. originalInstanceID = p.model.GetInstanceID(),
  202. name = p.model.exposedName,
  203. value = new VFXSerializableObject(p.model.type, p.model.value),
  204. exposed = p.model.exposed,
  205. isOutput = p.model.isOutput,
  206. valueFilter = p.valueFilter,
  207. min = p.valueFilter == VFXValueFilter.Range ? new VFXSerializableObject(p.model.type, p.model.min) : null,
  208. max = p.valueFilter == VFXValueFilter.Range ? new VFXSerializableObject(p.model.type, p.model.max) : null,
  209. enumValue = p.valueFilter == VFXValueFilter.Enum ? p.model.enumValues.ToArray() : null,
  210. tooltip = p.model.tooltip,
  211. nodes = c.Select((u, i) => CopyParameterNode(cpt - 1, i, u, parameterIndices[Array.IndexOf(parameters, u)])).ToArray()
  212. };
  213. }
  214. ).ToArray();
  215. }
  216. void CopyOperatorsAndContexts(ref SerializableGraph serializableGraph)
  217. {
  218. serializableGraph.contexts = new Context[contexts.Length];
  219. for (int i = 0; i < contexts.Length; ++i)
  220. {
  221. NodeID id = CopyContext(ref serializableGraph.contexts[i], contexts[i], i);
  222. modelIndices[contexts[i]] = id;
  223. }
  224. serializableGraph.operators = new Node[operators.Length];
  225. for (int i = 0; i < operators.Length; ++i)
  226. {
  227. uint id = CopyNode(ref serializableGraph.operators[i], operators[i].model, (NodeID)i);
  228. serializableGraph.operators[i].indexInClipboard = operatorIndices[i];
  229. modelIndices[operators[i]] = id;
  230. }
  231. }
  232. NodeID CopyNode(ref Node node, VFXModel model, uint index)
  233. {
  234. // Copy node infos
  235. node.position = model.position;
  236. node.type = model.GetType();
  237. node.flags = (model as VFXBlock)?.enabled != false ? Node.Flags.Enabled : 0;
  238. if (model.collapsed)
  239. node.flags |= Node.Flags.Collapsed;
  240. if (model.superCollapsed)
  241. node.flags |= Node.Flags.SuperCollapsed;
  242. uint id = 0;
  243. if (model is VFXOperator)
  244. {
  245. id = OperatorFlag;
  246. }
  247. else if (model is VFXContext)
  248. {
  249. id = ContextFlag;
  250. }
  251. id |= (uint)index;
  252. //Copy settings value
  253. CopyModelSettings(ref node.settings, model);
  254. var inputSlots = (model as IVFXSlotContainer).inputSlots;
  255. node.inputSlots = new Property[inputSlots.Count];
  256. for (int i = 0; i < inputSlots.Count; i++)
  257. {
  258. node.inputSlots[i].name = inputSlots[i].name;
  259. if (inputSlots[i].spaceable)
  260. node.inputSlots[i].space = inputSlots[i].space;
  261. node.inputSlots[i].value = new VFXSerializableObject(inputSlots[i].property.type, inputSlots[i].value);
  262. }
  263. node.expandedInputs = AllSlots(inputSlots).Where(t => !t.collapsed).Select(t => t.path).ToArray();
  264. node.expandedOutputs = AllSlots((model as IVFXSlotContainer).outputSlots).Where(t => !t.collapsed).Select(t => t.path).ToArray();
  265. return id;
  266. }
  267. SerializableGraph DoCopyBlocks(IEnumerable<VFXBlockController> blocks)
  268. {
  269. var newBlocks = new Node[blocks.Count()];
  270. uint cpt = 0;
  271. foreach (var block in blocks)
  272. {
  273. CopyNode(ref newBlocks[(int)cpt], block.model, cpt);
  274. ++cpt;
  275. }
  276. var graph = new SerializableGraph();
  277. graph.blocksOnly = true;
  278. graph.operators = newBlocks;
  279. return graph;
  280. }
  281. Node[] CopyBlocks(IList<VFXBlockController> blocks, int contextIndex)
  282. {
  283. var newBlocks = new Node[blocks.Count];
  284. for (uint i = 0; i < newBlocks.Length; ++i)
  285. {
  286. CopyNode(ref newBlocks[(int)i], blocks[(int)i].model, i);
  287. if (blocks[(int)i].model.enabled)
  288. {
  289. newBlocks[i].flags |= Node.Flags.Enabled;
  290. }
  291. if (contextIndex < (1 << 18) && i < (1 << 11))
  292. modelIndices[blocks[(int)i]] = BlockFlag | (i << 18) | (uint)contextIndex;
  293. else
  294. modelIndices[blocks[(int)i]] = InvalidID;
  295. }
  296. return newBlocks;
  297. }
  298. NodeID CopyContext(ref Context context, VFXContextController controller, int index)
  299. {
  300. NodeID id = CopyNode(ref context.node, controller.model, (NodeID)index);
  301. var blocks = controller.blockControllers;
  302. context.label = controller.model.label;
  303. context.systemName = VFXSystemNames.GetSystemName(controller.model);
  304. if (controller.model.GetData() != null)
  305. context.dataIndex = Array.IndexOf(datas, controller.model.GetData());
  306. else
  307. context.dataIndex = -1;
  308. context.blocks = CopyBlocks(controller.blockControllers, index);
  309. context.node.indexInClipboard = contextsIndices[index];
  310. if (controller.model is VFXAbstractRenderedOutput)
  311. context.subOutputs = CopySubOutputs(((VFXAbstractRenderedOutput)controller.model).GetSubOutputs());
  312. else
  313. context.subOutputs = null;
  314. return id;
  315. }
  316. SubOutput[] CopySubOutputs(List<VFXSRPSubOutput> subOutputs)
  317. {
  318. var newSubOutputs = new SubOutput[subOutputs.Count];
  319. for (int i = 0; i < newSubOutputs.Length; ++i)
  320. {
  321. if (subOutputs[i] != null) // Can be null if associated SRP is unknown
  322. {
  323. newSubOutputs[i].type = subOutputs[i].GetType();
  324. CopyModelSettings(ref newSubOutputs[i].settings, subOutputs[i]);
  325. }
  326. }
  327. return newSubOutputs;
  328. }
  329. static int[] MakeSlotPath(VFXSlot slot, bool input)
  330. {
  331. List<int> slotPath = new List<int>(slot.depth + 1);
  332. while (slot.GetParent() != null)
  333. {
  334. slotPath.Add(slot.GetParent().GetIndex(slot));
  335. slot = slot.GetParent();
  336. }
  337. slotPath.Add((input ? (slot.owner as IVFXSlotContainer).inputSlots : (slot.owner as IVFXSlotContainer).outputSlots).IndexOf(slot));
  338. return slotPath.ToArray();
  339. }
  340. }
  341. }