-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXConvertSubgraph.cs
More file actions
801 lines (668 loc) · 38.4 KB
/
Copy pathVFXConvertSubgraph.cs
File metadata and controls
801 lines (668 loc) · 38.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VFX;
using UnityEditor.VFX;
using UnityEditor.Experimental.GraphView;
using UnityEditor.VFX.Block;
using NodeID = System.UInt32;
namespace UnityEditor.VFX.UI
{
static class VFXConvertSubgraph
{
public static void ConvertToSubgraphContext(VFXView sourceView, IEnumerable<Controller> controllers, Rect rect, string path = null)
{
var ctx = new Context();
ctx.ConvertToSubgraphContext(sourceView, controllers, rect, path);
}
public static void ConvertToSubgraphOperator(VFXView sourceView, IEnumerable<Controller> controllers, Rect rect, string path = null)
{
var ctx = new Context();
ctx.ConvertToSubgraphOperator(sourceView, controllers, rect, path);
}
public static void ConvertToSubgraphBlock(VFXView sourceView, IEnumerable<Controller> controllers, Rect rect, string path = null)
{
var ctx = new Context();
ctx.ConvertToSubgraphBlock(sourceView, controllers, rect, path);
}
enum Type
{
Context,
Operator,
Block
}
static VisualEffectObject CreateUniquePath(VFXView sourceView, Type type)
{
string graphPath = AssetDatabase.GetAssetPath(sourceView.controller.model.asset);
string graphName = Path.GetFileNameWithoutExtension(graphPath);
string graphDirPath = Path.GetDirectoryName(graphPath);
switch (type)
{
case Type.Operator:
{
string targetSubgraphPath = string.Format("{0}/{1}_SubgraphOperator.vfxoperator", graphDirPath, graphName);
int cpt = 1;
while (File.Exists(targetSubgraphPath))
{
targetSubgraphPath = string.Format("{0}/{1}_SubgraphOperator_{2}.vfxoperator", graphDirPath, graphName, cpt++);
}
return VisualEffectAssetEditorUtility.CreateNew<VisualEffectSubgraphOperator>(targetSubgraphPath);
}
case Type.Context:
{
string targetSubgraphPath = string.Format("{0}/{1}_Subgraph.vfx", graphDirPath, graphName);
int cpt = 1;
while (File.Exists(targetSubgraphPath))
{
targetSubgraphPath = string.Format("{0}/{1}_Subgraph_{2}.vfx", graphDirPath, graphName, cpt++);
}
return VisualEffectAssetEditorUtility.CreateNewAsset(targetSubgraphPath);
}
case Type.Block:
{
string targetSubgraphPath = string.Format("{0}/{1}_SubgraphBlock.vfxblock", graphDirPath, graphName);
int cpt = 1;
while (File.Exists(targetSubgraphPath))
{
targetSubgraphPath = string.Format("{0}/{1}_SubgraphBlock_{2}.vfxblock", graphDirPath, graphName, cpt++);
}
return VisualEffectAssetEditorUtility.CreateNew<VisualEffectSubgraphBlock>(targetSubgraphPath);
}
}
return null;
}
class Context
{
List<VFXParameterNodeController> parameterNodeControllers;
VFXViewController m_SourceController;
List<Controller> m_SourceControllers;
VFXView m_SourceView;
VFXModel m_SourceNode;
IVFXSlotContainer m_SourceSlotContainer;
VFXNodeController m_SourceNodeController;
Dictionary<string, VFXParameterNodeController> m_SourceParameters;
VFXViewController m_TargetController;
List<VFXNodeController> m_TargetControllers;
List<VFXParameterController> m_TargetParameters = new List<VFXParameterController>();
VisualEffectObject m_TargetSubgraph;
Rect m_Rect;
void Init(VFXView sourceView, IEnumerable<Controller> controllers)
{
this.m_SourceView = sourceView;
m_SourceControllers = controllers.Concat(sourceView.controller.dataEdges.Where(t => controllers.Contains(t.input.sourceNode) && controllers.Contains(t.output.sourceNode))).Distinct().ToList();
parameterNodeControllers = m_SourceControllers.OfType<VFXParameterNodeController>().ToList();
m_SourceController = sourceView.controller;
VFXGraph sourceGraph = m_SourceController.graph;
m_SourceController.useCount++;
m_SourceParameters = new Dictionary<string, VFXParameterNodeController>();
foreach (var parameterNode in parameterNodeControllers)
{
m_SourceParameters[parameterNode.exposedName] = parameterNode;
}
}
void Uninit()
{
foreach (var element in m_SourceControllers.Where(t => !(t is VFXDataEdgeController) && !(t is VFXParameterNodeController)))
{
m_SourceController.RemoveElement(element);
}
foreach (var element in parameterNodeControllers)
{
if (element.infos.linkedSlots == null || element.infos.linkedSlots.Count() == 0)
m_SourceController.RemoveElement(element);
}
m_TargetController.useCount--;
m_SourceController.useCount--;
}
void UninitSmart()
{
var nodeNotToDelete = new HashSet<Controller>();
foreach (var node in m_SourceControllers.OfType<VFXNodeController>().Where(t => t.outputPorts.Count() > 0))
{
if (nodeNotToDelete.Contains(node))
continue;
var oldBag = new HashSet<VFXNodeController>();
var newBag = new HashSet<VFXNodeController>();
oldBag.Add(node);
while (oldBag.Count > 0)
{
foreach (var n in oldBag)
{
if (n.outputPorts.SelectMany(t => t.connections).Any(t => nodeNotToDelete.Contains(t.input.sourceNode) || !m_SourceControllersWithBlocks.Contains(t.input.sourceNode)))
{
nodeNotToDelete.Add(n);
oldBag.Clear();
break;
}
foreach (var o in n.inputPorts.SelectMany(t => t.connections).Select(t => t.output))
{
newBag.Add(o.sourceNode);
}
}
oldBag.Clear();
var tmp = oldBag;
oldBag = newBag;
newBag = tmp;
}
}
foreach (var element in m_SourceControllers.Where(t => !(t is VFXDataEdgeController) && !(t is VFXParameterNodeController) && !nodeNotToDelete.Contains(t)))
{
m_SourceController.RemoveElement(element);
}
foreach (var element in parameterNodeControllers)
{
if (element.infos.linkedSlots == null || element.infos.linkedSlots.Count() == 0)
m_SourceController.RemoveElement(element);
}
m_TargetController.useCount--;
m_SourceController.useCount--;
}
void CopyPasteNodes()
{
object result = VFXCopy.Copy(m_SourceControllers, m_Rect);
VFXPaste.Paste(m_TargetController, m_Rect.center, result, null, null, m_TargetControllers);
}
List<VFXNodeController> m_SourceOperatorAndParameters;
List<VFXNodeController> m_TargetOperatorAndParameters;
void CopyPasteOperators(Dictionary<VFXNodeController, VFXNodeController> targetNodes)
{
m_SourceOperatorAndParameters = m_SourceControllers.OfType<VFXNodeController>().Where(t => !(t is VFXBlockController)).ToList();
object result = VFXCopy.Copy(m_SourceOperatorAndParameters, m_Rect);
m_TargetOperatorAndParameters = new List<VFXNodeController>();
VFXPaste.Paste(m_TargetController, m_Rect.center, result, null, null, m_TargetOperatorAndParameters);
foreach (var st in m_SourceOperatorAndParameters.Zip(m_TargetOperatorAndParameters, (s, t) => new { source = s, target = t }))
{
targetNodes[st.source] = st.target;
}
}
void SetupTargetParameters()
{
// Change each parameter created by copy paste ( and therefore a parameter copied ) to exposed
foreach (var parameter in m_TargetController.parameterControllers)
{
m_TargetParameters.Add(parameter);
parameter.exposed = true;
}
}
public void ConvertToSubgraphContext(VFXView sourceView, IEnumerable<Controller> controllers, Rect rect, string path)
{
this.m_Rect = rect;
Init(sourceView, controllers);
if (path == null)
{
if (!CreateUniqueSubgraph("Subgraph", VisualEffectResource.Extension, VisualEffectAssetEditorUtility.CreateNewAsset))
return;
}
else
{
m_TargetSubgraph = VisualEffectAssetEditorUtility.CreateNewAsset(path);
m_TargetController = VFXViewController.GetController(m_TargetSubgraph.GetResource());
m_TargetController.useCount++;
m_TargetControllers = new List<VFXNodeController>();
}
CopyPasteNodes();
m_SourceNode = ScriptableObject.CreateInstance<VFXSubgraphContext>();
PostSetupNode();
m_SourceControllersWithBlocks = m_SourceControllers.Concat(m_SourceControllers.OfType<VFXContextController>().SelectMany(t => t.blockControllers));
TransferEdges();
//TransferContextsFlowEdges();
UninitSmart();
}
public void ConvertToSubgraphOperator(VFXView sourceView, IEnumerable<Controller> controllers, Rect rect, string path)
{
this.m_Rect = rect;
Init(sourceView, controllers);
if (path == null)
{
if (!CreateUniqueSubgraph("SubgraphOperator", VisualEffectSubgraphOperator.Extension, VisualEffectAssetEditorUtility.CreateNew<VisualEffectSubgraphOperator>))
return;
}
else
{
m_TargetSubgraph = VisualEffectAssetEditorUtility.CreateNew<VisualEffectSubgraphOperator>(path);
m_TargetController = VFXViewController.GetController(m_TargetSubgraph.GetResource());
m_TargetController.useCount++;
m_TargetControllers = new List<VFXNodeController>();
}
CopyCustomAttributes<VFXOperatorController>(sourceView, controllers);
CopyPasteNodes();
m_SourceNode = ScriptableObject.CreateInstance<VFXSubgraphOperator>();
PostSetupNode();
m_SourceControllersWithBlocks = m_SourceControllers.Concat(m_SourceControllers.OfType<VFXContextController>().SelectMany(t => t.blockControllers));
TransferEdges();
TransfertOperatorOutputEdges();
Uninit();
//The PrepareSubgraphs was initially in compilation
//This change has been canceled to prevent creation of model in the wrong place
//Be sure the newly created operator has expected slot
var subGraphOperator = m_SourceNode as VFXSubgraphOperator;
subGraphOperator.RecreateCopy();
subGraphOperator.ResyncSlots(true);
}
List<VFXBlockController> m_SourceBlockControllers;
List<VFXBlockController> m_TargetBlocks = null;
public void ConvertToSubgraphBlock(VFXView sourceView, IEnumerable<Controller> controllers, Rect rect, string path)
{
this.m_Rect = rect;
Init(sourceView, controllers);
if (string.IsNullOrEmpty(path))
{
if (!CreateUniqueSubgraph("SubgraphBlock", VisualEffectSubgraphBlock.Extension,
VisualEffectAssetEditorUtility.CreateNew<VisualEffectSubgraphBlock>))
return;
}
else
{
m_TargetSubgraph = VisualEffectAssetEditorUtility.CreateNew<VisualEffectSubgraphBlock>(path);
m_TargetController = VFXViewController.GetController(m_TargetSubgraph.GetResource());
m_TargetController.useCount++;
m_TargetControllers = new List<VFXNodeController>();
}
m_SourceControllers.RemoveAll(t => t is VFXContextController); // Don't copy contexts
m_SourceBlockControllers = m_SourceControllers.OfType<VFXBlockController>().OrderBy(t => t.index).ToList();
VFXContextController sourceContextController = m_SourceBlockControllers.First().contextController;
object copyData = VFXCopy.CopyBlocks(m_SourceBlockControllers, m_SourceControllers);
var targetContext = m_TargetController.graph.children.OfType<VFXBlockSubgraphContext>().FirstOrDefault();
if (targetContext == null)
{
targetContext = ScriptableObject.CreateInstance<VFXBlockSubgraphContext>();
m_TargetController.graph.AddChild(targetContext);
}
m_TargetController.LightApplyChanges();
targetContext.position = sourceContextController.position;
targetContext.SetSettingValue("m_SuitableContexts", (VFXBlockSubgraphContext.ContextType)m_SourceBlockControllers.Select(t => t.model.compatibleContexts).Aggregate((t, s) => t & s));
m_TargetBlocks = new List<VFXBlockController>();
CopyCustomAttributes<VFXBlockController>(sourceView, controllers);
VFXPaste.PasteBlocks(m_TargetController, copyData, targetContext, 0, m_TargetBlocks);
VFXPaste.PasteStickyNotes(m_TargetController, copyData);
Dictionary<VFXNodeController, VFXNodeController> targetControllers = new Dictionary<VFXNodeController, VFXNodeController>();
CopyPasteOperators(targetControllers);
m_SourceControllersWithBlocks = m_SourceControllers.Concat(m_SourceBlockControllers);
//Create lost links between nodes and blocks
foreach (var edge in m_SourceController.dataEdges.Where(t => m_SourceOperatorAndParameters.Contains(t.output.sourceNode) && m_SourceBlockControllers.Contains(t.input.sourceNode)))
{
var outputNode = targetControllers[edge.output.sourceNode];
var output = outputNode.outputPorts.First(t => t.path == edge.output.path);
var inputBlock = m_TargetBlocks[m_SourceBlockControllers.IndexOf(edge.input.sourceNode as VFXBlockController)];
var input = inputBlock.inputPorts.First(t => t.path == edge.input.path);
m_TargetController.CreateLink(input, output);
}
//Create lost links between nodes
foreach (var edge in m_SourceController.dataEdges.Where(t => m_SourceOperatorAndParameters.Contains(t.output.sourceNode) && m_SourceOperatorAndParameters.Contains(t.input.sourceNode)))
{
var outputNode = targetControllers[edge.output.sourceNode];
var output = outputNode.outputPorts.First(t => t.path == edge.output.path);
var inputNode = targetControllers[edge.input.sourceNode];
var input = inputNode.inputPorts.First(t => t.path == edge.input.path);
m_TargetController.CreateLink(input, output);
}
var sourceBlock = ScriptableObject.CreateInstance<VFXSubgraphBlock>();
m_SourceNode = sourceBlock;
sourceContextController.model.AddChild(m_SourceNode, m_SourceBlockControllers.Select(t => t.index).Min());
(m_SourceView.GetNodeByController(sourceContextController) as VFXContextUI).UpdateSelectionWithNewBlocks();
sourceContextController.ApplyChanges();
m_SourceNodeController = sourceContextController.blockControllers.First(t => t.model == m_SourceNode);
PostSetup();
m_SourceNode.SetSettingValue("m_Subgraph", m_TargetSubgraph);
m_SourceNodeController.ApplyChanges();
var targetContextController = m_TargetController.GetRootNodeController(targetContext, 0) as VFXContextController;
m_SourceControllersWithBlocks = m_SourceControllers.Concat(m_SourceBlockControllers);
m_SourceControllers = m_SourceOperatorAndParameters.Cast<Controller>().ToList();
m_TargetControllers = m_TargetOperatorAndParameters;
TransferEdges();
m_SourceControllers = m_SourceControllersWithBlocks.ToList();
UninitSmart();
}
bool CreateUniqueSubgraph(string typeName, string extension, Func<string, VisualEffectObject> createFunc)
{
string graphPath = AssetDatabase.GetAssetPath(m_SourceView.controller.model);
string graphName;
string graphDirPath;
if (string.IsNullOrEmpty(graphPath))
{
graphName = m_SourceView.controller.model.name;
if (string.IsNullOrEmpty(graphName))
graphName = "New VFX";
graphDirPath = "Assets";
}
else
{
graphName = Path.GetFileNameWithoutExtension(graphPath);
graphDirPath = Path.GetDirectoryName(graphPath).Replace('\\', '/');
}
string fileName = $"{graphName}_{typeName}";
var targetSubgraphPath = string.Format("{0}/{1}{2}", graphDirPath, fileName, extension);
int cpt = 1;
while (File.Exists(targetSubgraphPath))
{
fileName = $"{graphName}_{typeName}_{cpt++}";
targetSubgraphPath = string.Format("{0}/{1}{2}", graphDirPath, fileName, extension);
}
targetSubgraphPath = EditorUtility.SaveFilePanelInProject("Create Subgraph", fileName, extension.Substring(1), "Select where you want to save your subgraph.");
if (string.IsNullOrEmpty(targetSubgraphPath))
return false;
if (Path.GetExtension(targetSubgraphPath) != extension)
{
targetSubgraphPath += extension;
}
if (File.Exists(targetSubgraphPath))
{
Debug.LogError("Can't overwrite a subgraph");
return false;
}
m_TargetSubgraph = createFunc(targetSubgraphPath);
m_TargetController = VFXViewController.GetController(m_TargetSubgraph.GetResource());
m_TargetController.useCount++;
m_TargetControllers = new List<VFXNodeController>();
return true;
}
void PostSetupNode()
{
PostSetup();
m_SourceNode.position = m_Rect.center;
m_SourceView.UpdateSelectionWithNewNode();
m_SourceController.graph.AddChild(m_SourceNode);
m_SourceNode.SetSettingValue("m_Subgraph", m_TargetSubgraph);
m_SourceController.LightApplyChanges();
m_SourceNodeController = m_SourceController.GetRootNodeController(m_SourceNode, 0);
m_SourceNodeController.ApplyChanges();
}
void PostSetup()
{
SetupTargetParameters();
m_SourceSlotContainer = m_SourceNode as IVFXSlotContainer;
}
void TransferEdges()
{
for (int i = 0; i < m_TargetParameters.Count; ++i)
{
var input = m_SourceNodeController.inputPorts.First(t => t.model == m_SourceSlotContainer.inputSlots[i]);
var output = m_SourceParameters[m_TargetParameters[i].exposedName].outputPorts.First();
m_TargetController.CreateLink(input, output);
}
TransfertDataEdges();
}
IEnumerable<Controller> m_SourceControllersWithBlocks;
void TransfertDataEdges()
{
// Search for links between with inputs in the selected part and the output in other parts of the graph.
Dictionary<VFXDataAnchorController, List<VFXDataAnchorController>> traversingInEdges = new Dictionary<VFXDataAnchorController, List<VFXDataAnchorController>>();
foreach (var edge in m_SourceController.dataEdges.Where(
t =>
{
if (parameterNodeControllers.Contains(t.output.sourceNode))
return false;
var inputInControllers = m_SourceControllersWithBlocks.Contains(t.input.sourceNode);
var outputInControllers = m_SourceControllersWithBlocks.Contains(t.output.sourceNode);
return inputInControllers && !outputInControllers;
}
))
{
List<VFXDataAnchorController> outputs = null;
if (!traversingInEdges.TryGetValue(edge.input, out outputs))
{
outputs = new List<VFXDataAnchorController>();
traversingInEdges[edge.input] = outputs;
}
outputs.Add(edge.output);
}
var newSourceInputs = traversingInEdges.Keys.ToArray();
for (int i = 0; i < newSourceInputs.Length; ++i)
{
VFXParameter newTargetParameter = m_TargetController.AddVFXParameter(Vector2.zero, VFXLibrary.GetParameters().First(t => t.modelType == newSourceInputs[i].portType).variant);
m_TargetController.LightApplyChanges();
VFXParameterController newTargetParamController = m_TargetController.GetParameterController(newTargetParameter);
newTargetParamController.exposed = true;
var outputs = traversingInEdges[newSourceInputs[i]];
var linkedParameter = outputs.Select(t => t.sourceNode).OfType<VFXParameterNodeController>().FirstOrDefault();
if (linkedParameter != null &&
newTargetParameter.type == linkedParameter.parentController.model.type)
{
newTargetParamController.exposedName = ReplaceReservedName(linkedParameter.parentController.exposedName);
{
VFXParameter originalParameter = linkedParameter.parentController.model;
newTargetParameter.valueFilter = originalParameter.valueFilter;
if (originalParameter.valueFilter == VFXValueFilter.Range)
{
newTargetParameter.min = originalParameter.min;
newTargetParameter.max = originalParameter.max;
}
else if (originalParameter.valueFilter == VFXValueFilter.Enum)
{
newTargetParameter.enumValues = originalParameter.enumValues.ToList();
}
}
}
else
newTargetParamController.exposedName = ReplaceReservedName(newSourceInputs[i].name);
//first the equivalent of sourceInput in the target
VFXNodeController targetNode = null;
Vector2 position;
if (newSourceInputs[i].sourceNode is VFXBlockController)
{
var blockController = newSourceInputs[i].sourceNode as VFXBlockController;
if (m_TargetBlocks != null)
{
targetNode = m_TargetBlocks[m_SourceBlockControllers.IndexOf(blockController)];
position = blockController.contextController.position;
}
else
{
var targetContext = m_TargetControllers[m_SourceControllers.IndexOf(blockController.contextController)] as VFXContextController;
targetNode = targetContext.blockControllers[blockController.index];
position = blockController.contextController.position;
}
}
else
{
targetNode = m_TargetControllers[m_SourceControllers.IndexOf(newSourceInputs[i].sourceNode)];
position = targetNode.position;
}
VFXDataAnchorController targetAnchor = targetNode.inputPorts.First(t => t.path == newSourceInputs[i].path);
position.y += targetAnchor.model.owner.inputSlots.IndexOf(targetAnchor.model) * 32;
VFXNodeController parameterNode = m_TargetController.AddVFXParameter(position - new Vector2(200, 0), newTargetParamController, null);
// Link the parameternode and the input in the target
m_TargetController.CreateLink(targetAnchor, parameterNode.outputPorts[0]);
if (m_SourceSlotContainer is VFXOperator)
(m_SourceSlotContainer as VFXOperator).ResyncSlots(true);
else if (m_SourceSlotContainer is VFXSubgraphBlock)
{
VFXSubgraphBlock blk = (m_SourceSlotContainer as VFXSubgraphBlock);
blk.RecreateCopy();
blk.ResyncSlots(true);
}
else if (m_SourceSlotContainer is VFXSubgraphContext)
{
VFXSubgraphContext ctx = (m_SourceSlotContainer as VFXSubgraphContext);
ctx.RecreateCopy();
ctx.ResyncSlots(true);
}
m_SourceNodeController.model.Invalidate(VFXModel.InvalidationCause.kSettingChanged); // call to resync slots
m_SourceNodeController.ApplyChanges();
//Link all the outputs to the matching input of the subgraph
foreach (var output in outputs)
{
m_SourceController.CreateLink(m_SourceNodeController.inputPorts.First(t => t.model == m_SourceSlotContainer.inputSlots.Last()), output);
}
}
}
static string ReplaceReservedName(string name)
{
if (name == VFXBlock.activationSlotName)
return "enabled";
return name;
}
void TransfertOperatorOutputEdges()
{
var traversingOutEdges = new Dictionary<VFXDataAnchorController, List<VFXDataAnchorController>>();
foreach (var edge in m_SourceController.dataEdges.Where(
t =>
{
if (t.output.sourceNode is VFXParameterNodeController)
return false;
var inputInControllers = m_SourceControllersWithBlocks.Contains(t.input.sourceNode);
var outputInControllers = m_SourceControllersWithBlocks.Contains(t.output.sourceNode);
return !inputInControllers && outputInControllers;
}
))
{
List<VFXDataAnchorController> inputs = null;
if (!traversingOutEdges.TryGetValue(edge.output, out inputs))
{
inputs = new List<VFXDataAnchorController>();
traversingOutEdges[edge.output] = inputs;
}
inputs.Add(edge.input);
}
var newSourceOutputs = traversingOutEdges.Keys.ToArray();
for (int i = 0; i < newSourceOutputs.Length; ++i)
{
VFXParameter newTargetParameter = m_TargetController.AddVFXParameter(Vector2.zero, VFXLibrary.GetParameters().First(t => t.variant.modelType == newSourceOutputs[i].portType).variant);
m_TargetController.LightApplyChanges();
VFXParameterController newTargetParamController = m_TargetController.GetParameterController(newTargetParameter);
newTargetParamController.isOutput = true;
var inputs = traversingOutEdges[newSourceOutputs[i]];
var linkedParameter = inputs.FirstOrDefault(t => t.sourceNode is VFXParameterNodeController);
if (linkedParameter != null)
newTargetParamController.exposedName = ReplaceReservedName((linkedParameter.sourceNode as VFXParameterNodeController).parentController.exposedName);
else
newTargetParamController.exposedName = ReplaceReservedName(newSourceOutputs[i].name);
//first the equivalent of sourceInput in the target
VFXNodeController targetNode = null;
if (newSourceOutputs[i].sourceNode is VFXBlockController)
{
var blockController = newSourceOutputs[i].sourceNode as VFXBlockController;
if (m_TargetBlocks != null)
{
targetNode = m_TargetBlocks[m_SourceBlockControllers.IndexOf(blockController)];
}
else
{
var targetContext = m_TargetControllers[m_SourceControllers.IndexOf(blockController.contextController)] as VFXContextController;
targetNode = targetContext.blockControllers[blockController.index];
}
}
else
{
targetNode = m_TargetControllers[m_SourceControllers.IndexOf(newSourceOutputs[i].sourceNode)];
}
VFXDataAnchorController targetAnchor = targetNode.outputPorts.FirstOrDefault(t => t.path == newSourceOutputs[i].path);
if (targetAnchor != null)
{
VFXNodeController parameterNode = m_TargetController.AddVFXParameter(targetNode.position + new Vector2(400, 0), newTargetParamController, null);
// Link the parameternode and the input in the target
m_TargetController.CreateLink(parameterNode.inputPorts[0], targetAnchor);
if (m_SourceSlotContainer is VFXOperator)
(m_SourceSlotContainer as VFXOperator).ResyncSlots(true);
m_SourceNodeController.ApplyChanges();
}
//Link all the outputs to the matching input of the subgraph
foreach (var input in inputs)
{
var port = m_SourceNodeController.outputPorts.FirstOrDefault(t => t.model == m_SourceSlotContainer.outputSlots.Last());
if (port != null)
m_SourceController.CreateLink(input, port);
}
}
}
void TransferContextsFlowEdges()
{
var initializeContexts = m_SourceControllers.OfType<VFXContextController>().Where(t => t.model.contextType == VFXContextType.Init ||
t.model.contextType == VFXContextType.Spawner ||
t.model.contextType == VFXContextType.Subgraph).ToArray();
var outputSpawners = new Dictionary<VFXContextController, List<VFXFlowAnchorController>>();
var outputEvents = new Dictionary<string, List<VFXFlowAnchorController>>();
foreach (var initializeContext in initializeContexts)
{
for (int i = 0; i < initializeContext.flowInputAnchors.Count; ++i)
if (initializeContext.flowInputAnchors[i].connections.Count() > 0)
{
var outputContext = initializeContext.flowInputAnchors[i].connections.First().output.context; //output context must be linked through is it is linked with a spawner
if (!m_SourceControllers.Contains(outputContext))
{
if (outputContext.model.contextType == VFXContextType.Spawner /*||
((outputContext.model is VFXBasicEvent) &&
(new string[] { VisualEffectAsset.PlayEventName, VisualEffectAsset.StopEventName }.Contains((outputContext.model as VFXBasicEvent).eventName) ||
sourceController.model.isSubgraph && (outputContext.model as VFXBasicEvent).eventName == VFXSubgraphContext.triggerEventName))*/)
{
List<VFXFlowAnchorController> inputs = null;
if (!outputSpawners.TryGetValue(outputContext, out inputs))
{
inputs = new List<VFXFlowAnchorController>();
outputSpawners.Add(outputContext, inputs);
}
inputs.Add(initializeContext.flowInputAnchors[i]);
}
else if (outputContext.model is VFXBasicEvent)
{
List<VFXFlowAnchorController> inputs = null;
var eventName = (outputContext.model as VFXBasicEvent).eventName;
if (!outputEvents.TryGetValue(eventName, out inputs))
{
inputs = new List<VFXFlowAnchorController>();
outputEvents.Add(eventName, inputs);
}
inputs.Add(initializeContext.flowInputAnchors[i]);
}
}
}
}
{
if (outputSpawners.Count() > 1)
{
Debug.LogWarning("More than one spawner is linked to the content if the new subgraph, some links we not be kept");
}
}
{ //link named events as if
foreach (var kv in outputEvents)
{
CreateAndLinkEvent(m_SourceControllers, m_TargetController, m_TargetControllers, kv.Value, kv.Key);
}
}
}
private void CopyCustomAttributes<T>(VFXView sourceView, IEnumerable<Controller> controllers) where T : VFXNodeController
{
// Only copy custom attributes which are used by nodes to convert
var attributeManager = sourceView.controller.graph.attributesManager;
var usedCustomAttributes = new HashSet<VFXAttribute>();
foreach (var controller in controllers.OfType<T>())
{
if (controller.model is IVFXAttributeUsage attributeUsage)
{
foreach (var attribute in attributeUsage.usedAttributes)
{
if (attributeManager.IsCustom(attribute.name))
{
usedCustomAttributes.Add(attribute);
}
}
}
}
foreach (var customAttribute in usedCustomAttributes)
{
m_TargetController.graph.TryAddCustomAttribute(customAttribute.name, customAttribute.type, customAttribute.description, false, out _);
}
}
}
private static void CreateAndLinkEvent(List<Controller> sourceControllers, VFXViewController targetController, List<VFXNodeController> targetControllers, List<VFXFlowAnchorController> inputs, string eventName)
{
var triggerEvent = VFXBasicEvent.CreateInstance<VFXBasicEvent>();
triggerEvent.eventName = eventName;
targetController.graph.AddChild(triggerEvent);
float xMiddle = 0;
float yMin = Mathf.Infinity;
foreach (var edge in inputs)
{
var targetContext = targetControllers[sourceControllers.IndexOf(edge.context)] as VFXContextController;
var targetInputLink = edge.slotIndex;
triggerEvent.LinkTo(targetContext.model, 0, targetInputLink);
xMiddle += targetContext.position.x;
if (targetContext.position.y < yMin)
yMin = targetContext.position.y;
}
triggerEvent.position = new Vector2(xMiddle / inputs.Count, yMin) - new Vector2(0, 200); // place the event above the top center of the linked contexts.
}
}
}