-
Notifications
You must be signed in to change notification settings - Fork 3
/
apiMeshGeometryOverride.cpp
3425 lines (3095 loc) · 105 KB
/
apiMeshGeometryOverride.cpp
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-
// ==========================================================================
// Copyright 2015 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk
// license agreement provided at the time of installation or download,
// or which otherwise accompanies this software in either electronic
// or hard copy form.
// ==========================================================================
//+
///////////////////////////////////////////////////////////////////////////////
//
// apiMeshGeometryOverride.cpp
//
////////////////////////////////////////////////////////////////////////////////
#include "apiMeshGeometryOverride.h"
#include "apiMeshShape.h"
#include "apiMeshGeom.h"
#include <maya/MGlobal.h>
#include <maya/MUserData.h>
#include <maya/MSelectionList.h>
#include <maya/MPxComponentConverter.h>
#include <maya/MSelectionContext.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MHWGeometry.h>
#include <maya/MShaderManager.h>
#include <maya/MViewport2Renderer.h>
#include <maya/MDrawContext.h>
#include <maya/MHWGeometryUtilities.h>
#include <maya/MObjectArray.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MFnDagNode.h>
#include <maya/MDrawRegistry.h>
#include <set>
#include <vector>
// Custom user data class to attach to render items
class apiMeshUserData : public MUserData
{
public:
apiMeshUserData()
: MUserData(true) // let Maya clean up
, fMessage("")
, fNumModifications(0)
{
}
virtual ~apiMeshUserData()
{
}
MString fMessage;
int fNumModifications;
};
// Custom user data class to attach to render items
// to help with viewport 2.0 selection
class apiMeshHWSelectionUserData : public MUserData
{
public:
apiMeshHWSelectionUserData()
: MUserData(true) // let Maya clean up
, fMeshGeom(NULL)
{
}
virtual ~apiMeshHWSelectionUserData()
{
}
apiMeshGeom* fMeshGeom;
};
// Pre/Post callback helper
static void callbackDataPrint(
MHWRender::MDrawContext& context,
const MHWRender::MRenderItemList& renderItemList)
{
int numItems = renderItemList.length();
const MHWRender::MRenderItem* item = NULL;
for (int i=0; i<numItems; i++)
{
item = renderItemList.itemAt(i);
if (item)
{
MDagPath path = item->sourceDagPath();
printf("\tITEM: '%s' -- SOURCE: '%s'\n", item->name().asChar(), path.fullPathName().asChar());
}
}
const MHWRender::MPassContext & passCtx = context.getPassContext();
const MString & passId = passCtx.passIdentifier();
const MStringArray & passSem = passCtx.passSemantics();
printf("tAPI mesh drawing in pass[%s], semantic[", passId.asChar());
for (unsigned int i=0; i<passSem.length(); i++)
printf(" %s", passSem[i].asChar());
printf("\n");
}
// Custom pre-draw callback
static void apiMeshPreDrawCallback(
MHWRender::MDrawContext& context,
const MHWRender::MRenderItemList& renderItemList,
MHWRender::MShaderInstance *shaderInstance)
{
printf("PRE-draw callback triggered for render item list with data:\n");
callbackDataPrint(context, renderItemList);
printf("\n");
}
// Custom post-draw callback
static void apiMeshPostDrawCallback(
MHWRender::MDrawContext& context,
const MHWRender::MRenderItemList& renderItemList,
MHWRender::MShaderInstance *shaderInstance)
{
printf("POST-draw callback triggered for render item list with data:\n");
callbackDataPrint(context, renderItemList);
printf("\n");
}
// Custom component converter to select vertices
// Attached to the dormant vertices render item (sVertexItemName)
class meshVertComponentConverter : public MHWRender::MPxComponentConverter
{
public:
meshVertComponentConverter() : MHWRender::MPxComponentConverter() {}
virtual ~meshVertComponentConverter() {}
virtual void initialize(const MHWRender::MRenderItem& renderItem)
{
// Create the component selection object .. here we are selecting vertex component
fComponentObject = fComponent.create( MFn::kMeshVertComponent );
// Build a lookup table to match each primitive position in the index buffer of the render item geometry
// to the correponding vertex component of the object
// Use same algorithm as in updateIndexingForDormantVertices
apiMeshHWSelectionUserData* selectionData = dynamic_cast<apiMeshHWSelectionUserData*>(renderItem.customData());
if(selectionData)
{
apiMeshGeom* meshGeom = selectionData->fMeshGeom;
// Allocate vertices lookup table
{
unsigned int numTriangles = 0;
for (int i=0; i<meshGeom->faceCount; i++)
{
int numVerts = meshGeom->face_counts[i];
if (numVerts > 2)
{
numTriangles += numVerts - 2;
}
}
fVertices.resize(3*numTriangles);
}
// Fill vertices lookup table
unsigned int base = 0;
unsigned int idx = 0;
for (int faceIdx=0; faceIdx<meshGeom->faceCount; faceIdx++)
{
// ignore degenerate faces
int numVerts = meshGeom->face_counts[faceIdx];
if (numVerts > 2)
{
for (int v=1; v<numVerts-1; v++)
{
fVertices[idx++] = meshGeom->face_connects[base];
fVertices[idx++] = meshGeom->face_connects[base+v];
fVertices[idx++] = meshGeom->face_connects[base+v+1];
}
base += numVerts;
}
}
}
}
virtual void addIntersection(MHWRender::MIntersection& intersection)
{
// Convert the intersection index, which represent the primitive position in the
// index buffer, to the correct vertex component
const int rawIdx = intersection.index();
int idx = 0;
if(rawIdx >= 0 && rawIdx < (int)fVertices.size())
idx = fVertices[rawIdx];
fComponent.addElement(idx);
}
virtual MObject component()
{
// Return the component object that contains the ids of the selected vertices
return fComponentObject;
}
virtual MSelectionMask selectionMask() const
{
// This converter is only valid for vertex selection or snapping
MSelectionMask retVal;
retVal.setMask(MSelectionMask::kSelectMeshVerts);
retVal.addMask(MSelectionMask::kSelectPointsForGravity);
return retVal;
}
static MPxComponentConverter* creator()
{
return new meshVertComponentConverter();
}
private:
MFnSingleIndexedComponent fComponent;
MObject fComponentObject;
std::vector<int> fVertices;
};
// Custom component converter to select edges
// Attached to the edge selection render item (sEdgeSelectionItemName)
class meshEdgeComponentConverter : public MHWRender::MPxComponentConverter
{
public:
meshEdgeComponentConverter() : MHWRender::MPxComponentConverter() {}
virtual ~meshEdgeComponentConverter() {}
virtual void initialize(const MHWRender::MRenderItem& renderItem)
{
// Create the component selection object .. here we are selecting edge component
fComponentObject = fComponent.create( MFn::kMeshEdgeComponent );
// Build a lookup table to match each primitive position in the index buffer of the render item geometry
// to the correponding edge component of the object
// Use same algorithm as in updateIndexingForEdges
// in updateIndexingForEdges the index buffer is allocated with "totalEdges = 2*totalVerts"
// but since we are drawing lines, we'll get only half of the data as primitive positions :
// indices 0 & 1 : primitive #0
// indices 2 & 3 : primitive #1
// ...
apiMeshHWSelectionUserData* selectionData = dynamic_cast<apiMeshHWSelectionUserData*>(renderItem.customData());
if(selectionData)
{
apiMeshGeom* meshGeom = selectionData->fMeshGeom;
// Allocate edges lookup table
{
unsigned int totalVerts = 0;
for (int i=0; i<meshGeom->faceCount; i++)
{
int numVerts = meshGeom->face_counts[i];
if (numVerts > 2)
{
totalVerts += numVerts;
}
}
fEdges.resize(totalVerts);
}
// Fill edges lookup table
unsigned int idx = 0;
int edgeId = 0;
for (int faceIdx=0; faceIdx<meshGeom->faceCount; faceIdx++)
{
// ignore degenerate faces
int numVerts = meshGeom->face_counts[faceIdx];
if (numVerts > 2)
{
for (int v=0; v<numVerts; v++)
{
fEdges[idx++] = edgeId;
++edgeId;
}
}
}
}
}
virtual void addIntersection(MHWRender::MIntersection& intersection)
{
// Convert the intersection index, which represent the primitive position in the
// index buffer, to the correct edge component
const int rawIdx = intersection.index();
int idx = 0;
if(rawIdx >= 0 && rawIdx < (int)fEdges.size())
idx = fEdges[rawIdx];
fComponent.addElement(idx);
}
virtual MObject component()
{
// Return the component object that contains the ids of the selected edges
return fComponentObject;
}
virtual MSelectionMask selectionMask() const
{
// This converter is only valid for edge selection
return MSelectionMask::kSelectMeshEdges;
}
static MPxComponentConverter* creator()
{
return new meshEdgeComponentConverter();
}
private:
MFnSingleIndexedComponent fComponent;
MObject fComponentObject;
std::vector<int> fEdges;
};
// Custom component converter to select faces
// Attached to the face selection render item (sFaceSelectionItemName)
class meshFaceComponentConverter : public MHWRender::MPxComponentConverter
{
public:
meshFaceComponentConverter() : MHWRender::MPxComponentConverter() {}
virtual ~meshFaceComponentConverter() {}
virtual void initialize(const MHWRender::MRenderItem& renderItem)
{
// Create the component selection object .. here we are selecting face component
fComponentObject = fComponent.create( MFn::kMeshPolygonComponent );
// Build a lookup table to match each primitive position in the index buffer of the render item geometry
// to the correponding face component of the object
// Use same algorithm as in updateIndexingForFaces
// in updateIndexingForFaces the index buffer is allocated with "numTriangleVertices = 3*numTriangles"
// but since we are drawing triangles, we'll get only a third of the data as primitive positions :
// indices 0, 1 & 2 : primitive #0
// indices 3, 4 & 5 : primitive #1
// ...
apiMeshHWSelectionUserData* selectionData = dynamic_cast<apiMeshHWSelectionUserData*>(renderItem.customData());
if(selectionData)
{
apiMeshGeom* meshGeom = selectionData->fMeshGeom;
// Allocate faces lookup table
{
unsigned int numTriangles = 0;
for (int i=0; i<meshGeom->faceCount; i++)
{
int numVerts = meshGeom->face_counts[i];
if (numVerts > 2)
{
numTriangles += numVerts - 2;
}
}
fFaces.resize(numTriangles);
}
// Fill faces lookup table
unsigned int idx = 0;
for (int faceIdx=0; faceIdx<meshGeom->faceCount; faceIdx++)
{
// ignore degenerate faces
int numVerts = meshGeom->face_counts[faceIdx];
if (numVerts > 2)
{
for (int v=1; v<numVerts-1; v++)
{
fFaces[idx++] = faceIdx;
}
}
}
}
}
virtual void addIntersection(MHWRender::MIntersection& intersection)
{
// Convert the intersection index, which represent the primitive position in the
// index buffer, to the correct face component
const int rawIdx = intersection.index();
int idx = 0;
if(rawIdx >= 0 && rawIdx < (int)fFaces.size())
idx = fFaces[rawIdx];
fComponent.addElement(idx);
}
virtual MObject component()
{
// Return the component object that contains the ids of the selected faces
return fComponentObject;
}
virtual MSelectionMask selectionMask() const
{
// This converter is only valid for face selection
return MSelectionMask::kSelectMeshFaces;
}
static MPxComponentConverter* creator()
{
return new meshFaceComponentConverter();
}
private:
MFnSingleIndexedComponent fComponent;
MObject fComponentObject;
std::vector<int> fFaces;
};
const MString apiMeshGeometryOverride::sWireframeItemName = "apiMeshWire";
const MString apiMeshGeometryOverride::sShadedTemplateItemName = "apiMeshShadedTemplateWire";
const MString apiMeshGeometryOverride::sSelectedWireframeItemName = "apiMeshSelectedWireFrame";
const MString apiMeshGeometryOverride::sVertexItemName = "apiMeshVertices";
const MString apiMeshGeometryOverride::sActiveVertexItemName = "apiMeshActiveVertices";
const MString apiMeshGeometryOverride::sVertexIdItemName = "apiMeshVertexIds";
const MString apiMeshGeometryOverride::sVertexPositionItemName = "apiMeshVertexPositions";
const MString apiMeshGeometryOverride::sShadedModeFaceCenterItemName = "apiMeshFaceCenterInShadedMode";
const MString apiMeshGeometryOverride::sWireframeModeFaceCenterItemName = "apiMeshFaceCenterInWireframeMode";
const MString apiMeshGeometryOverride::sShadedProxyItemName = "apiShadedProxy";
const MString apiMeshGeometryOverride::sAffectedEdgeItemName = "apiMeshAffectedEdges";
const MString apiMeshGeometryOverride::sAffectedFaceItemName = "apiMeshAffectedFaces";
const MString apiMeshGeometryOverride::sEdgeSelectionItemName = "apiMeshEdgeSelection";
const MString apiMeshGeometryOverride::sFaceSelectionItemName = "apiMeshFaceSelection";
const MString apiMeshGeometryOverride::sActiveVertexStreamName = "apiMeshSharedVertexStream";
const MString apiMeshGeometryOverride::sFaceCenterStreamName = "apiMeshFaceCenterStream";
apiMeshGeometryOverride::apiMeshGeometryOverride(const MObject& obj)
: MPxGeometryOverride(obj)
, fMesh(NULL)
, fMeshGeom(NULL)
, fColorRemapTexture(NULL)
, fLinearSampler(NULL)
{
// get the real apiMesh object from the MObject
MStatus status;
MFnDependencyNode node(obj, &status);
if (status)
{
fMesh = dynamic_cast<apiMesh*>(node.userNode());
}
fDrawSharedActiveVertices = true; // false;
fDrawFaceCenters = true; // false;
// Turn on to view active vertices with colours lookedup from a 1D texture.
fDrawActiveVerticesWithRamp = false;
if (fDrawActiveVerticesWithRamp)
{
fDrawFaceCenters = false; // Too cluttered showing the face centers at the same time.
}
// Can set the following to true, but then the logic to
// determine what color to set is the responsibility of the plugin.
//
fUseCustomColors = false;
// Can change this to choose a different shader to use when
// no shader node is assigned to the object.
fProxyShader =
// Uncommenting one of the following will result in a different line
// shader to be used. Note that color-per-vertex (CPV) is provided in populateGeometry()
// to handle shaders which have this geometry requirement.
//
// -1 // Use this to indicate to later code that we want to use a built in fragment shader
// MHWRender::MShaderManager::k3dSolidShader // - Basic line shader
// MHWRender::MShaderManager::k3dStippleShader // - For filled stipple faces (triangles)
// MHWRender::MShaderManager::k3dThickLineShader // For thick solid colored lines
// MHWRender::MShaderManager::k3dCPVThickLineShader // For thick colored lines. Black if no CPV
// MHWRender::MShaderManager::k3dDashLineShader // - For dashed solid color lines
// MHWRender::MShaderManager::k3dCPVDashLineShader //- For dashed coloured lines. Black if no CPV
// MHWRender::MShaderManager::k3dThickDashLineShader // For thick dashed solid color lines. black if no cpv
MHWRender::MShaderManager::k3dCPVThickDashLineShader //- For thick, dashed and coloured lines
;
// Set to true to test that overriding internal items has no effect
// for shadows and effects overrides
fInternalItems_NoShadowCast = false;
fInternalItems_NoShadowReceive = false;
fInternalItems_NoPostEffects = false;
// Use the existing shadow casts / receives flags on the shape
// to drive settings for applicable render items.
fExternalItems_NoShadowCast = false;
fExternalItems_NoShadowReceive = false;
fExternalItemsNonTri_NoShadowCast = false;
fExternalItemsNonTri_NoShadowReceive = false;
// Set to true to ignore post-effects for wireframe items.
// Shaded items will still have effects applied.
fExternalItems_NoPostEffects = true;
fExternalItemsNonTri_NoPostEffects = true;
}
apiMeshGeometryOverride::~apiMeshGeometryOverride()
{
fMesh = NULL;
fMeshGeom = NULL;
if (fColorRemapTexture)
{
MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
MHWRender::MTextureManager* textureMgr = renderer ? renderer->getTextureManager() : NULL;
if (textureMgr)
{
textureMgr->releaseTexture(fColorRemapTexture);
fColorRemapTexture = NULL;
}
}
if (fLinearSampler)
{
MHWRender::MStateManager::releaseSamplerState(fLinearSampler);
fLinearSampler = NULL;
}
}
MHWRender::DrawAPI apiMeshGeometryOverride::supportedDrawAPIs() const
{
// this plugin supports both GL and DX
return (MHWRender::kOpenGL | MHWRender::kDirectX11 | MHWRender::kOpenGLCoreProfile);
}
void apiMeshGeometryOverride::updateDG()
{
// Pull the actual outMesh from the shape, as well
// as any active components
fActiveVertices.clear();
fActiveVerticesSet.clear();
fActiveEdgesSet.clear();
fActiveFacesSet.clear();
if (fMesh)
{
fMeshGeom = fMesh->meshGeom();
if (fMesh->hasActiveComponents())
{
MObjectArray activeComponents = fMesh->activeComponents();
if (activeComponents.length())
{
MFnSingleIndexedComponent fnComponent( activeComponents[0] );
if (fnComponent.elementCount())
{
MIntArray activeIds;
fnComponent.getElements( activeIds );
if(fnComponent.componentType() == MFn::kMeshVertComponent)
{
fActiveVertices = activeIds;
for (unsigned int i=0; i<activeIds.length(); ++i)
fActiveVerticesSet.insert( activeIds[i] );
}
else if(fnComponent.componentType() == MFn::kMeshEdgeComponent)
{
for (unsigned int i=0; i<activeIds.length(); ++i)
fActiveEdgesSet.insert( activeIds[i] );
}
else if(fnComponent.componentType() == MFn::kMeshPolygonComponent)
{
for (unsigned int i=0; i<activeIds.length(); ++i)
fActiveFacesSet.insert( activeIds[i] );
}
}
}
}
}
}
/*
Some example code to print out shader parameters
*/
void apiMeshGeometryOverride::printShader(MHWRender::MShaderInstance* shader)
{
if (!shader)
return;
MStringArray params;
shader->parameterList(params);
unsigned int numParams = params.length();
printf("DEBUGGING SHADER, BEGIN PARAM LIST OF LENGTH %d\n", numParams);
for (unsigned int i=0; i<numParams; i++)
{
printf("ParamName='%s', ParamType=", params[i].asChar());
switch (shader->parameterType(params[i]))
{
case MHWRender::MShaderInstance::kInvalid:
printf("'Invalid', ");
break;
case MHWRender::MShaderInstance::kBoolean:
printf("'Boolean', ");
break;
case MHWRender::MShaderInstance::kInteger:
printf("'Integer', ");
break;
case MHWRender::MShaderInstance::kFloat:
printf("'Float', ");
break;
case MHWRender::MShaderInstance::kFloat2:
printf("'Float2', ");
break;
case MHWRender::MShaderInstance::kFloat3:
printf("'Float3', ");
break;
case MHWRender::MShaderInstance::kFloat4:
printf("'Float4', ");
break;
case MHWRender::MShaderInstance::kFloat4x4Row:
printf("'Float4x4Row', ");
break;
case MHWRender::MShaderInstance::kFloat4x4Col:
printf("'Float4x4Col', ");
break;
case MHWRender::MShaderInstance::kTexture1:
printf("'1D Texture', ");
break;
case MHWRender::MShaderInstance::kTexture2:
printf("'2D Texture', ");
break;
case MHWRender::MShaderInstance::kTexture3:
printf("'3D Texture', ");
break;
case MHWRender::MShaderInstance::kTextureCube:
printf("'Cube Texture', ");
break;
case MHWRender::MShaderInstance::kSampler:
printf("'Sampler', ");
break;
default:
printf("'Unknown', ");
break;
}
printf("IsArrayParameter='%s'\n", shader->isArrayParameter(params[i]) ? "YES" : "NO");
}
printf("END PARAM LIST\n");
}
/*
Set the solid color for solid color shaders
*/
void apiMeshGeometryOverride::setSolidColor(MHWRender::MShaderInstance* shaderInstance, const float *value)
{
if (!shaderInstance)
return;
const MString colorParameterName = "solidColor";
shaderInstance->setParameter(colorParameterName, value);
}
/*
Set the point size for solid color shaders
*/
void apiMeshGeometryOverride::setSolidPointSize(MHWRender::MShaderInstance* shaderInstance, float pointSize)
{
if (!shaderInstance)
return;
const float pointSizeArray[2] = {pointSize, pointSize};
const MString pointSizeParameterName = "pointSize";
shaderInstance->setParameter( pointSizeParameterName, pointSizeArray );
}
/*
Set the line width for solid color shaders
*/
void apiMeshGeometryOverride::setLineWidth(MHWRender::MShaderInstance* shaderInstance, float lineWidth)
{
if (!shaderInstance)
return;
const float lineWidthArray[2] = {lineWidth, lineWidth};
const MString lineWidthParameterName = "lineWidth";
shaderInstance->setParameter( lineWidthParameterName, lineWidthArray );
}
/*
Update render items for dormant and template wireframe drawing.
1) If the object is dormant and not templated then we require
a render item to display when wireframe drawing is required (display modes
is wire or wire-on-shaded)
2a) If the object is templated then we use the same render item as in 1)
when in wireframe drawing is required.
2b) However we also require a render item to display when in shaded mode.
*/
void apiMeshGeometryOverride::updateDormantAndTemplateWireframeItems(
const MDagPath& path,
MHWRender::MRenderItemList& list,
const MHWRender::MShaderManager* shaderMgr)
{
// Stock colors
static const float dormantColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };
static const float templateColor[] = { 0.45f, 0.45f, 0.45f, 1.0f };
static const float activeTemplateColor[] = { 1.0f, 0.5f, 0.5f, 1.0f };
// Some local options to show debug interface
//
static const bool debugShader = false;
static const unsigned int shadedDrawMode = MHWRender::MGeometry::kAll;
MHWRender::MGeometry::Primitive primitive = MHWRender::MGeometry::kLines;
// Get render item used for draw in wireframe mode
// (Mode to draw in is MHWRender::MGeometry::kWireframe)
//
MHWRender::MRenderItem* wireframeItem = NULL;
int index = list.indexOf(sWireframeItemName);
if (index < 0)
{
wireframeItem = MHWRender::MRenderItem::Create(
sWireframeItemName,
MHWRender::MRenderItem::DecorationItem,
primitive);
wireframeItem->setDrawMode(MHWRender::MGeometry::kWireframe);
// Set dormant wireframe with appropriate priority to not clash with
// any active wireframe which may overlap in depth.
wireframeItem->depthPriority( MHWRender::MRenderItem::sDormantWireDepthPriority );
list.append(wireframeItem);
MHWRender::MShaderInstance* shader = shaderMgr->getStockShader(
MHWRender::MShaderManager::k3dSolidShader,
debugShader ? apiMeshPreDrawCallback : NULL,
debugShader ? apiMeshPostDrawCallback : NULL);
if (shader)
{
// assign shader
wireframeItem->setShader(shader);
// sample debug code
if (debugShader)
{
printShader( shader );
}
// once assigned, no need to hold on to shader instance
shaderMgr->releaseShader(shader);
}
}
else
{
wireframeItem = list.itemAt(index);
}
// Get render item for handling mode shaded template drawing
//
MHWRender::MRenderItem* shadedTemplateItem = NULL;
int index2 = list.indexOf(sShadedTemplateItemName);
if (index2 < 0)
{
shadedTemplateItem = MHWRender::MRenderItem::Create(
sShadedTemplateItemName,
MHWRender::MRenderItem::DecorationItem,
primitive);
shadedTemplateItem->setDrawMode((MHWRender::MGeometry::DrawMode) shadedDrawMode);
// Set shaded item as being dormant wire since it should still be raised
// above any shaded items, but not as high as active items.
shadedTemplateItem->depthPriority( MHWRender::MRenderItem::sDormantWireDepthPriority );
list.append(shadedTemplateItem);
MHWRender::MShaderInstance* shader = shaderMgr->getStockShader(
MHWRender::MShaderManager::k3dSolidShader,
NULL, NULL);
if (shader)
{
// assign shader
shadedTemplateItem->setShader(shader);
// sample debug code
if (debugShader)
{
printShader( shader );
}
// once assigned, no need to hold on to shader instance
shaderMgr->releaseShader(shader);
}
}
else
{
shadedTemplateItem = list.itemAt(index2);
}
// Sample code to disable cast, receives shadows, and post effects.
if (fExternalItemsNonTri_NoShadowCast)
shadedTemplateItem->castsShadows( false );
if (fExternalItemsNonTri_NoShadowReceive)
shadedTemplateItem->receivesShadows( false );
if (fExternalItemsNonTri_NoPostEffects)
shadedTemplateItem->setExcludedFromPostEffects( true );
MHWRender::DisplayStatus displayStatus =
MHWRender::MGeometryUtilities::displayStatus(path);
MColor wireColor = MHWRender::MGeometryUtilities::wireframeColor(path);
// Enable / disable wireframe item and update the shader parameters
//
if (wireframeItem)
{
MHWRender::MShaderInstance* shader = wireframeItem->getShader();
switch (displayStatus) {
case MHWRender::kTemplate:
if (shader)
{
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : templateColor);
}
wireframeItem->enable(true);
break;
case MHWRender::kActiveTemplate:
if (shader)
{
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : activeTemplateColor );
}
wireframeItem->enable(true);
break;
case MHWRender::kDormant:
if (shader)
{
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : dormantColor);
}
wireframeItem->enable(true);
break;
case MHWRender::kActiveAffected:
if (shader)
{
static const float theColor[] = { 0.5f, 0.0f, 1.0f, 1.0f };
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : theColor );
}
wireframeItem->enable(true);
break;
default:
wireframeItem->enable(false);
break;
}
}
// Enable / disable shaded/template item and update the shader parameters
//
bool isTemplate = path.isTemplated();
if (shadedTemplateItem)
{
MHWRender::MShaderInstance* shader = shadedTemplateItem->getShader();
switch (displayStatus) {
case MHWRender::kTemplate:
if (shader)
{
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : templateColor );
}
shadedTemplateItem->enable(isTemplate);
break;
case MHWRender::kActiveTemplate:
if (shader)
{
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : activeTemplateColor );
}
shadedTemplateItem->enable(isTemplate);
break;
case MHWRender::kDormant:
if (shader)
{
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : dormantColor);
}
shadedTemplateItem->enable(isTemplate);
break;
default:
shadedTemplateItem->enable(false);
break;
}
}
}
/*
Create a render item for active wireframe if it does not exist. Updating
shading parameters as necessary.
*/
void apiMeshGeometryOverride::updateActiveWireframeItem(const MDagPath& path,
MHWRender::MRenderItemList& list,
const MHWRender::MShaderManager* shaderMgr)
{
MHWRender::MRenderItem* selectItem = NULL;
int index = list.indexOf(sSelectedWireframeItemName);
if (index < 0)
{
selectItem = MHWRender::MRenderItem::Create(
sSelectedWireframeItemName,
MHWRender::MRenderItem::DecorationItem,
MHWRender::MGeometry::kLines);
selectItem->setDrawMode(MHWRender::MGeometry::kAll);
// This is the same as setting the argument raiseAboveShaded = true,
// since it sets the priority value to be the same. This line is just
// an example of another way to do the same thing after creation of
// the render item.
selectItem->depthPriority( MHWRender::MRenderItem::sActiveWireDepthPriority);
list.append(selectItem);
// For active wireframe we will use a shader which allows us to draw thick lines
//
static const bool drawThick = false;
MHWRender::MShaderInstance* shader =
shaderMgr->getStockShader( drawThick ? MHWRender::MShaderManager::k3dThickLineShader : MHWRender::MShaderManager::k3dSolidShader );
if (shader)
{
// assign shader
selectItem->setShader(shader);
// once assigned, no need to hold on to shader instance
shaderMgr->releaseShader(shader);
}
}
else
{
selectItem = list.itemAt(index);
}
MHWRender::MShaderInstance* shader = NULL;
if (selectItem)
{
shader = selectItem->getShader();
}
MHWRender::DisplayStatus displayStatus =
MHWRender::MGeometryUtilities::displayStatus(path);
MColor wireColor = MHWRender::MGeometryUtilities::wireframeColor(path);
switch (displayStatus) {
case MHWRender::kLead:
selectItem->enable(true);
if (shader)
{
static const float theColor[] = { 0.0f, 0.8f, 0.0f, 1.0f };
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : theColor );
}
break;
case MHWRender::kActive:
if (shader)
{
static const float theColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : theColor );
}
selectItem->enable(true);
break;
case MHWRender::kHilite:
case MHWRender::kActiveComponent:
if (shader)
{
static const float theColor[] = { 0.0f, 0.5f, 0.7f, 1.0f };
setSolidColor( shader, !fUseCustomColors ? &(wireColor.r) : theColor );
}
selectItem->enable(true);
break;
default:
selectItem->enable(false);
break;
};
// Add custom user data to selection item
apiMeshUserData* myCustomData = dynamic_cast<apiMeshUserData*>(selectItem->customData());
if (!myCustomData)
{
// create the custom data
myCustomData = new apiMeshUserData();
myCustomData->fMessage = "I'm custom data!";
selectItem->setCustomData(myCustomData);
}
else
{
// modify the custom data
myCustomData->fNumModifications++;
}
}
/*
Create render items for numeric display, and update shaders as necessary
*/
void apiMeshGeometryOverride::updateVertexNumericItems(const MDagPath& path, MHWRender::MRenderItemList& list, const MHWRender::MShaderManager* shaderMgr)
{
// Enable to show numeric render items
bool enableNumiercDisplay = false;
// Vertex id item
//
MHWRender::MRenderItem* vertexItem = NULL;
int index = list.indexOf(sVertexIdItemName);
if (index < 0)
{
vertexItem = MHWRender::MRenderItem::Create(
sVertexIdItemName,
MHWRender::MRenderItem::DecorationItem,
MHWRender::MGeometry::kPoints);
vertexItem->setDrawMode(MHWRender::MGeometry::kAll);
vertexItem->depthPriority( MHWRender::MRenderItem::sDormantPointDepthPriority );
list.append(vertexItem);
// Use single integer numeric shader
MHWRender::MShaderInstance* shader = shaderMgr->getStockShader(