-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathUpdateRenderPackageAsyncTask.cs
612 lines (521 loc) · 25.1 KB
/
UpdateRenderPackageAsyncTask.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Autodesk.DesignScript.Geometry;
using Autodesk.DesignScript.Interfaces;
using Dynamo.Engine;
using Dynamo.Graph.Nodes;
using Dynamo.Visualization;
using ProtoCore.Mirror;
namespace Dynamo.Scheduler
{
class UpdateRenderPackageParams
{
internal IRenderPackageFactory RenderPackageFactory { get; set; }
internal string PreviewIdentifierName { get; set; }
internal NodeModel Node { get; set; }
internal EngineController EngineController { get; set; }
internal IEnumerable<KeyValuePair<Guid, string>> DrawableIdMap { get; set; }
internal bool ForceUpdate { get; set; }
}
/// <summary>
/// An asynchronous task to regenerate render packages for a given node.
/// During execution the task retrieves the corresponding IGraphicItem from
/// EngineController through a set of drawable identifiers supplied by the
/// node. These IGraphicItem objects then fill the IRenderPackage objects
/// with tessellated geometric data. Each of the resulting IRenderPackage
/// objects is then tagged with a label.
/// </summary>
///
class UpdateRenderPackageAsyncTask : AsyncTask
{
private const byte DefR = 0;
private const byte DefG = 0;
private const byte DefB = 0;
private const byte DefA = 255;
private const byte MidTone = 180;
#region Class Data Members and Properties
protected Guid nodeGuid;
private bool displayLabels;
private bool isNodeSelected;
private string previewIdentifierName;
private EngineController engineController;
private IEnumerable<KeyValuePair<Guid, string>> drawableIdMap;
private readonly RenderPackageCache renderPackageCache;
private IRenderPackageFactory factory;
internal RenderPackageCache RenderPackages
{
get { return renderPackageCache; }
}
public override TaskPriority Priority
{
get { return TaskPriority.Normal; }
}
#endregion
#region Public Class Operational Methods
internal UpdateRenderPackageAsyncTask(IScheduler scheduler)
: base(scheduler)
{
nodeGuid = Guid.Empty;
renderPackageCache = new RenderPackageCache();
}
internal bool Initialize(UpdateRenderPackageParams initParams)
{
if (initParams == null)
throw new ArgumentNullException("initParams");
if (initParams.Node == null)
throw new ArgumentNullException("initParams.Node");
if (initParams.EngineController == null)
throw new ArgumentNullException("initParams.EngineController");
if (initParams.DrawableIdMap == null)
throw new ArgumentNullException("initParams.DrawableIdMap");
var nodeModel = initParams.Node;
if (nodeModel.WasRenderPackageUpdatedAfterExecution && !initParams.ForceUpdate)
return false; // Not has not been updated at all.
// If a node is in either of the following states, then it will not
// produce any geometric output. Bail after clearing the render packages.
if (nodeModel.IsInErrorState || !nodeModel.IsVisible)
return false;
// Without AstIdentifierForPreview, a node cannot have MirrorData.
if (string.IsNullOrEmpty(nodeModel.AstIdentifierForPreview.Value))
return false;
drawableIdMap = initParams.DrawableIdMap;
if (!drawableIdMap.Any())
return false; // Nothing to be drawn.
displayLabels = nodeModel.DisplayLabels;
isNodeSelected = nodeModel.IsSelected;
factory = initParams.RenderPackageFactory;
engineController = initParams.EngineController;
previewIdentifierName = initParams.PreviewIdentifierName;
nodeGuid = nodeModel.GUID;
nodeModel.WasRenderPackageUpdatedAfterExecution = true;
return true;
}
#endregion
#region Protected Overridable Methods
protected override void HandleTaskExecutionCore()
{
if (nodeGuid == Guid.Empty)
{
throw new InvalidOperationException(
"UpdateRenderPackageAsyncTask.Initialize not called");
}
var idEnum = drawableIdMap.GetEnumerator();
while (idEnum.MoveNext())
{
var mirrorData = engineController.GetMirror(idEnum.Current.Value);
if (mirrorData == null)
continue;
GetRenderPackagesFromMirrorData(
idEnum.Current.Key,
mirrorData.GetData(),
previewIdentifierName,
displayLabels);
}
}
private void GetRenderPackagesFromMirrorData(
Guid outputPortId,
MirrorData mirrorData,
string tag,
bool displayLabels)
{
if (mirrorData.IsNull)
{
return;
}
var package = factory.CreateRenderPackage();
package.DisplayLabels = displayLabels;
package.Description = tag;
package.IsSelected = isNodeSelected;
//Initialize the package here assuming we will only generate a single renderPackage for this node
//We set the AllowLegacyColorOperations so that we can catch tessellation implementations which
//use the deprecated calls. At that point we will roll back the changes to the renderPackage
//and call tessellate with a new renderPackage object with AllowLegacyColorOperations set to true.
if (package is IRenderPackageSupplement packageSupplement)
{
packageSupplement.AllowLegacyColorOperations = false;
}
GetRenderPackagesFromMirrorDataImp(outputPortId, mirrorData, package, tag);
if (package.MeshVertexColors.Any())
{
package.RequiresPerVertexColoration = true;
}
if (package.HasRenderingData)
{
renderPackageCache.Add(package, outputPortId);
}
}
private void GetRenderPackagesFromMirrorDataImp(
Guid outputPortId,
MirrorData mirrorData,
IRenderPackage package,
string labelKey)
{
if (mirrorData.IsCollection)
{
int count = 0;
foreach (var el in mirrorData.GetElements())
{
if (el.IsCollection || el.Data is IGraphicItem)
{
string newLabel = labelKey + ":" + count;
GetRenderPackagesFromMirrorDataImp(outputPortId, el, package, newLabel);
}
count += 1;
}
}
else
{
if (!(mirrorData.Data is IGraphicItem graphicItem))
{
return;
}
GetTessellationDataFromGraphicItem(outputPortId, graphicItem, labelKey, ref package);
}
}
private void GetTessellationDataFromGraphicItem(Guid outputPortId, IGraphicItem graphicItem, string labelKey, ref IRenderPackage package)
{
var packageWithTransform = package as ITransformable;
try
{
var previousPointVertexCount = package.PointVertexCount;
var previousLineVertexCount = package.LineVertexCount;
var previousMeshVertexCount = package.MeshVertexCount;
//Todo Plane tessellation needs to be handled here vs in LibG currently
if (graphicItem is Plane plane)
{
CreatePlaneTessellation(package, plane);
}
else
{
try
{
graphicItem.Tessellate(package, factory.TessellationParameters);
}
catch (LegacyRenderPackageMethodException)
{
//At this point we have detected an implementation of Tessellate which is using legacy color methods
//We roll back the primary renderPackage to it previous state before calling tessellation again.
package = RollBackPackage(package, previousPointVertexCount, previousLineVertexCount,
previousMeshVertexCount);
//Now we create a renderPackage object that will allow legacy color methods
var legacyPackage = factory.CreateRenderPackage();
legacyPackage.DisplayLabels = displayLabels;
legacyPackage.Description = labelKey;
legacyPackage.IsSelected = isNodeSelected;
//Use it to get fill the tessellation data and add it to the render cache.
GetTessellationDataFromGraphicItem(outputPortId, graphicItem, labelKey, ref legacyPackage);
if (legacyPackage.MeshVertexColors.Any())
{
legacyPackage.RequiresPerVertexColoration = true;
}
renderPackageCache.Add(legacyPackage, outputPortId);
return;
}
//Now we validate that tessellation call has added colors for each new vertex.
//If any vertex colors previously existed, this will ensure the vertex color and vertex counts stays in sync.
EnsureColorExistsPerVertex(package, previousPointVertexCount, previousLineVertexCount,
previousMeshVertexCount);
if (factory.TessellationParameters.ShowEdges)
{
if (graphicItem is Topology topology)
{
if (graphicItem is Surface surf)
{
foreach (var curve in surf.PerimeterCurves())
{
curve.Tessellate(package, factory.TessellationParameters);
curve.Dispose();
}
}
else
{
var edges = topology.Edges;
foreach (var geom in edges.Select(edge => edge.CurveGeometry))
{
geom.Tessellate(package, factory.TessellationParameters);
geom.Dispose();
}
edges.ForEach(x => x.Dispose());
}
}
}
}
//If the package has a transform that is not the identity matrix
//then set requiresCustomTransform to true.
if (packageWithTransform != null && !packageWithTransform.RequiresCustomTransform &&
packageWithTransform.Transform.SequenceEqual(
new double[] {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}) == false)
{
(packageWithTransform).RequiresCustomTransform = true;
}
//Do not add replication labels if the tessellation call set DisplayLabels to true;
if (package is IRenderLabels packageLabels && packageLabels.AutoGenerateLabels)
{
if (package.MeshVertexCount > previousMeshVertexCount)
{
packageLabels.AddLabel(labelKey, VertexType.Mesh, package.MeshVertexCount);
}
else if (package.LineVertexCount > previousLineVertexCount)
{
packageLabels.AddLabel(labelKey, VertexType.Line, package.LineVertexCount);
}
else if (package.PointVertexCount > previousPointVertexCount)
{
packageLabels.AddLabel(labelKey, VertexType.Point, package.PointVertexCount);
}
}
}
catch (Exception e)
{
Debug.WriteLine(
"PushGraphicItemIntoPackage: " + e);
}
}
private static void CreatePlaneTessellation(IRenderPackage package, Plane plane)
{
package.RequiresPerVertexColoration = true;
var s = 2.5;
var cs = CoordinateSystem.ByPlane(plane);
var a = Point.ByCartesianCoordinates(cs, s, s, 0);
var b = Point.ByCartesianCoordinates(cs, -s, s, 0);
var c = Point.ByCartesianCoordinates(cs, -s, -s, 0);
var d = Point.ByCartesianCoordinates(cs, s, -s, 0);
//Add two triangles to represent the plane
package.AddTriangleVertex(a.X, a.Y, a.Z);
package.AddTriangleVertex(b.X, b.Y, b.Z);
package.AddTriangleVertex(c.X, c.Y, c.Z);
package.AddTriangleVertex(c.X, c.Y, c.Z);
package.AddTriangleVertex(d.X, d.Y, d.Z);
package.AddTriangleVertex(a.X, a.Y, a.Z);
//Add the mesh vertex UV, normal, and color data for the 6 triangle vertices
for (var i = 0; i < 6; i++)
{
package.AddTriangleVertexUV(0, 0);
package.AddTriangleVertexNormal(plane.Normal.X, plane.Normal.Y, plane.Normal.Z);
package.AddTriangleVertexColor(0, 0, 0, 10);
}
// Draw plane edges
package.AddLineStripVertex(a.X, a.Y, a.Z);
package.AddLineStripVertex(b.X, b.Y, b.Z);
package.AddLineStripVertex(b.X, b.Y, b.Z);
package.AddLineStripVertex(c.X, c.Y, c.Z);
package.AddLineStripVertex(c.X, c.Y, c.Z);
package.AddLineStripVertex(d.X, d.Y, d.Z);
package.AddLineStripVertex(d.X, d.Y, d.Z);
package.AddLineStripVertex(a.X, a.Y, a.Z);
// Draw normal
package.AddLineStripVertex(plane.Origin.X, plane.Origin.Y, plane.Origin.Z);
var nEnd = plane.Origin.Add(plane.Normal.Scale(2.5));
package.AddLineStripVertex(nEnd.X, nEnd.Y, nEnd.Z);
//Add the line vertex data for the plane line geometry (4 plane edges and 1 normal).
for (var i = 0; i < 5; i++)
{
package.AddLineStripVertexCount(2);
package.AddLineStripVertexColor(MidTone, MidTone, MidTone, 255);
package.AddLineStripVertexColor(MidTone, MidTone, MidTone, 255);
}
//dispose helper geometry
a.Dispose();
b.Dispose();
c.Dispose();
d.Dispose();
cs.Dispose();
}
private static void EnsureColorExistsPerVertex(IRenderPackage package, int previousPointVertexCount, int previousLineVertexCount, int previousMeshVertexCount)
{
var packageSupplement = package as IRenderPackageSupplement;
//Use legacy slow path if package does not implement IRenderPackageSupplement
if (packageSupplement == null)
{
LegacyPackageEnsureColorExistsPerVertex(package, previousPointVertexCount, previousLineVertexCount, previousMeshVertexCount);
return;
}
if (package.PointVertexCount > previousPointVertexCount)
{
var count = package.PointVertexCount - packageSupplement.PointVertexColorCount;
if (count > 0)
{
packageSupplement.AppendPointVertexColorRange(CreateColorByteArrayOfSize(count, DefR, DefG, DefB, DefA));
}
}
if (package.LineVertexCount > previousLineVertexCount)
{
var count = package.LineVertexCount - packageSupplement.LineVertexColorCount;
if (count > 0)
{
packageSupplement.AppendLineVertexColorRange(CreateColorByteArrayOfSize(count, DefR, DefG, DefB, DefA));
}
}
if (package.MeshVertexCount > previousMeshVertexCount)
{
var count = package.MeshVertexCount - packageSupplement.MeshVertexColorCount;
if (count > 0)
{
packageSupplement.AppendMeshVertexColorRange(CreateColorByteArrayOfSize(count, DefR, DefG, DefB, DefA));
}
}
}
private static void LegacyPackageEnsureColorExistsPerVertex(IRenderPackage pkg, int previousPointVertexCount, int previousLineVertexCount, int previousMeshVertexCount)
{
// We sequence the if statements here to avoid calling Count on PointVertexColors if we don't have to.
// For Helix IRenderPackage implementation we have to regenerate a new IEnumerable each time we access PointVertexColors.
// The data is stored in a Helix specific data structure for efficient transfer to the view later.
// The same applies for line and mesh
if (pkg.PointVertexCount > previousPointVertexCount)
{
var colorCount = pkg.PointVertexColors.Count() / 4;
if(colorCount < pkg.PointVertexCount)
{
for (var i = colorCount; i < pkg.PointVertexCount; i++)
{
pkg.AddPointVertexColor(DefR, DefG, DefB, DefA);
}
}
}
if (pkg.LineVertexCount > previousLineVertexCount)
{
var colorCount = pkg.LineStripVertexColors.Count() / 4;
if (colorCount < pkg.LineVertexCount)
{
for (var i = colorCount; i < pkg.LineVertexCount; i++)
{
pkg.AddLineStripVertexColor(DefR, DefG, DefB, DefA);
}
}
}
if (pkg.MeshVertexCount > previousMeshVertexCount)
{
var colorCount = pkg.MeshVertexColors.Count() / 4;
if (colorCount < pkg.MeshVertexCount)
{
for (var i = colorCount; i < pkg.MeshVertexCount; i++)
{
pkg.AddTriangleVertexColor(DefR, DefG, DefB, DefA);
}
}
}
}
private static byte[] CreateColorByteArrayOfSize(int size, byte red, byte green, byte blue, byte alpha)
{
var arr = new byte[size * 4];
for (var i = 0; i < arr.Count(); i += 4)
{
arr[i] = red;
arr[i + 1] = green;
arr[i + 2] = blue;
arr[i + 3] = alpha;
}
return arr;
}
private IRenderPackage RollBackPackage(IRenderPackage package, int previousPointVertexCount, int previousLineVertexCount, int previousMeshVertexCount)
{
var newPackage = factory.CreateRenderPackage();
newPackage.Description = package.Description;
newPackage.IsSelected = package.IsSelected;
newPackage.DisplayLabels = package.DisplayLabels;
var ptVertices = package.PointVertices.ToArray();
for (var i = 0; i < previousPointVertexCount * 3 && i < ptVertices.Length; i += 3)
{
newPackage.AddPointVertex(ptVertices[i], ptVertices[i+1],ptVertices[i+2]);
}
var ptColors = package.PointVertexColors.ToArray();
for (var i = 0; i < previousPointVertexCount * 4 && i < ptColors.Length; i += 4)
{
newPackage.AddPointVertexColor(ptColors[i], ptColors[i + 1], ptColors[i + 2], ptColors[i + 3]);
}
var lineVertices = package.LineStripVertices.ToArray();
for (var i = 0; i < previousLineVertexCount * 3 && i < lineVertices.Length; i += 3)
{
newPackage.AddLineStripVertex(lineVertices[i], lineVertices[i + 1], lineVertices[i + 2]);
}
var lineColors = package.LineStripVertexColors.ToArray();
for (var i = 0; i < previousLineVertexCount * 4 && i < lineColors.Length; i += 4)
{
newPackage.AddLineStripVertexColor(lineColors[i], lineColors[i + 1], lineColors[i + 2], lineColors[i + 3]);
}
var lineVerticesCounts = package.LineStripVertexCounts.ToArray();
var count = 0;
var index = 0;
while (count < lineVerticesCounts.Length && count < previousLineVertexCount)
{
newPackage.AddLineStripVertexCount(lineVerticesCounts[index]);
count += lineVerticesCounts[index];
index++;
}
var meshVertices = package.MeshVertices.ToArray();
for (var i = 0; i < previousMeshVertexCount * 3 && i < meshVertices.Length; i += 3)
{
newPackage.AddTriangleVertex(meshVertices[i], meshVertices[i + 1], meshVertices[i + 2]);
}
var meshNormals = package.MeshNormals.ToArray();
for (var i = 0; i < previousMeshVertexCount * 3 && i < meshNormals.Length; i += 3)
{
newPackage.AddTriangleVertexNormal(meshNormals[i], meshNormals[i + 1], meshNormals[i + 2]);
}
var meshUV = package.MeshTextureCoordinates.ToArray();
for (var i = 0; i < previousMeshVertexCount * 2 && i < meshUV.Length; i += 2)
{
newPackage.AddTriangleVertexUV(meshUV[i], meshUV[i + 1]);
}
var meshColors = package.MeshVertexColors.ToArray();
for (var i = 0; i < previousMeshVertexCount * 4 && i < meshColors.Length; i += 4)
{
newPackage.AddTriangleVertexColor(meshColors[i], meshColors[i + 1], meshColors[i + 2], meshColors[i + 3]);
}
//Todo Need to obsolete ITransformable
if(package is ITransformable transformable && newPackage is ITransformable newTransformable)
{
newTransformable.RequiresCustomTransform = transformable.RequiresCustomTransform;
var t = transformable.Transform;
if (t.Length == 16)
{
newTransformable.SetTransform(t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8], t[9], t[10], t[11], t[12], t[13], t[14], t[15]);
}
}
if(package is IRenderPackageSupplement packageSupplement && newPackage is IRenderPackageSupplement newPackageSupplement)
{
var textureMapsList = packageSupplement.TextureMapsList;
var textureMapsStrideList = packageSupplement.TextureMapsStrideList;
var meshVerticesRange = packageSupplement.MeshVerticesRangesAssociatedWithTextureMaps;
packageSupplement.AllowLegacyColorOperations = false;
for (var i = 0; i < textureMapsList.Count; i++)
{
newPackageSupplement.AddTextureMapForMeshVerticesRange(meshVerticesRange[i].Item1, meshVerticesRange[i].Item2, textureMapsList[i], textureMapsStrideList[i]);
}
}
if (package is IRenderLabels renderLabels && newPackage is IRenderLabels newRenderLabels)
{
var labelData = renderLabels.LabelData;
foreach (var (label, pt) in labelData)
{
newRenderLabels.AddLabel(label, pt[0], pt[1], pt[2]);
}
}
return newPackage;
}
protected override void HandleTaskCompletionCore()
{
}
protected override TaskMergeInstruction CanMergeWithCore(AsyncTask otherTask)
{
var theOtherTask = otherTask as UpdateRenderPackageAsyncTask;
if (theOtherTask == null)
return base.CanMergeWithCore(otherTask);
// If the two UpdateRenderPackageAsyncTask are for different nodes,
// then there is no comparison to be made, keep both the tasks.
//
if (nodeGuid != theOtherTask.nodeGuid)
return TaskMergeInstruction.KeepBoth;
// Comparing to another NotifyRenderPackagesReadyAsyncTask, the one
// that gets scheduled more recently stay, while the earlier one
// gets dropped. If this task has a higher tick count, keep this.
//
if (ScheduledTime.TickCount > theOtherTask.ScheduledTime.TickCount)
return TaskMergeInstruction.KeepThis;
return TaskMergeInstruction.KeepOther; // Otherwise, keep the other.
}
#endregion
}
}