-
Notifications
You must be signed in to change notification settings - Fork 3
/
apiMeshSubSceneOverride.cpp
1728 lines (1561 loc) · 55.3 KB
/
apiMeshSubSceneOverride.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.
// ==========================================================================
//+
//////////////////////////////////////////////////////////////////////////////
//
// apiMeshSubSceneOverride.cpp
//
//////////////////////////////////////////////////////////////////////////////
#include "apiMeshSubSceneOverride.h"
#include "apiMeshShape.h"
#include "apiMeshGeom.h"
#include <maya/MDagPathArray.h>
#include <maya/MDrawRegistry.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MGLFunctionTable.h>
#include <maya/MGlobal.h>
#include <maya/MHWGeometry.h>
#include <maya/MHWGeometryUtilities.h>
#include <maya/MHardwareRenderer.h>
#include <maya/MObject.h>
#include <maya/MObjectArray.h>
#include <maya/MPlugArray.h>
#include <maya/MPxComponentConverter.h>
#include <maya/MSelectionContext.h>
#include <maya/MSelectionList.h>
#include <maya/MShaderManager.h>
#include <maya/MUserData.h>
#include <maya/MViewport2Renderer.h>
#include <vector>
#ifdef _WINDOWS
#include <d3d11.h>
#endif
namespace apiMeshSubSceneOverrideHelpers
{
// Helper class for link lost callback
class ShadedItemUserData : public MUserData
{
public:
ShadedItemUserData(apiMeshSubSceneOverride* ov)
: MUserData(false), fOverride(ov) {}
~ShadedItemUserData() { fOverride = NULL; }
apiMeshSubSceneOverride* fOverride;
};
// 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;
};
// Custom component converter to select components
// Attached to the vertex, edge and face selection render items
// respectively apiMeshSubSceneOverride::sVertexSelectionName, apiMeshSubSceneOverride::sEdgeSelectionName
// and apiMeshSubSceneOverride::sFaceSelectionName
class simpleComponentConverter : public MHWRender::MPxComponentConverter
{
public:
simpleComponentConverter(MFn::Type componentType, MSelectionMask selectionType)
: MHWRender::MPxComponentConverter()
, fComponentType(componentType)
, fSelectionType(selectionType) {}
virtual ~simpleComponentConverter() {}
virtual void initialize(const MHWRender::MRenderItem& renderItem)
{
// Create the component selection object
fComponentObject = fComponent.create( fComponentType );
// For face selection,
// create a lookup table to match triangle intersection with face id :
// One face may contains more than one triangle
if (fComponentType == MFn::kMeshPolygonComponent) {
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;
}
}
fLookupTable.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) {
fLookupTable[idx++] = faceIdx;
}
}
}
}
}
}
virtual void addIntersection(MHWRender::MIntersection& intersection)
{
// Convert the intersection index, which represent the primitive position in the
// index buffer, to the correct component id
// For vertex and edge: the primitive index value is the same as the component id
// For face: get the face id that matches the triangle index from the lookup table
if (fComponentType == MFn::kMeshEdgeComponent) {
// Only accept edge selection intersection on draw instance #2 -- scaled by 2
// and instance #-1 (when useDrawInstancingOnEdgeSelectionItem is false)
if (intersection.instanceID() == 1 || intersection.instanceID() == 3)
return;
}
int idx = intersection.index();
if (fComponentType == MFn::kMeshPolygonComponent) {
if (idx >= 0 && idx < (int)fLookupTable.size()) {
idx = fLookupTable[idx];
}
}
fComponent.addElement(idx);
}
virtual MObject component()
{
// Return the component object that contains the ids of the selected components
return fComponentObject;
}
virtual MSelectionMask selectionMask() const
{
// This converter is only valid for specified selection type
return fSelectionType;
}
static MPxComponentConverter* creatorVertexSelection()
{
// creator function to instanciate a component converter for vertex selection
MSelectionMask mask;
mask.setMask(MSelectionMask::kSelectMeshVerts);
mask.addMask(MSelectionMask::kSelectPointsForGravity);
return new simpleComponentConverter(MFn::kMeshVertComponent, mask);
}
static MPxComponentConverter* creatorEdgeSelection()
{
// creator function to instanciate a component converter for edge selection
return new simpleComponentConverter(MFn::kMeshEdgeComponent, MSelectionMask::kSelectMeshEdges);
}
static MPxComponentConverter* creatorFaceSelection()
{
// creator function to instanciate a component converter for face selection
return new simpleComponentConverter(MFn::kMeshPolygonComponent, MSelectionMask::kSelectMeshFaces);
}
private:
const MFn::Type fComponentType;
const MSelectionMask fSelectionType;
MFnSingleIndexedComponent fComponent;
MObject fComponentObject;
std::vector<int> fLookupTable;
};
}
using namespace apiMeshSubSceneOverrideHelpers;
// Compile time switching for regular MVertexBuffer management vs. custom
// user buffer management. Just an example to show
// how custom buffers can be used. See the bounding box code below.
// Note this now works for both OpenGL and DirectX11 Viewport 2.0
static const bool sUseCustomUserBuffersForBoundingBox = true;
static MHWRender::DrawAPI sDrawAPI = MHWRender::kNone;
static bool useSelectHighlight(const MSelectionList& selectedList, const MDagPath& path);
static bool floatApproxEqual(float left, float right);
const MString apiMeshSubSceneOverride::sWireName("apiMeshWire");
const MString apiMeshSubSceneOverride::sSelectName("apiMeshSelection");
const MString apiMeshSubSceneOverride::sBoxName("apiMeshBox");
const MString apiMeshSubSceneOverride::sSelectedBoxName("apiMeshBoxSelection");
const MString apiMeshSubSceneOverride::sShadedName("apiMeshShaded");
const MString apiMeshSubSceneOverride::sTexturedName("apiMeshTextured");
const MString apiMeshSubSceneOverride::sVertexSelectionName("apiMeshVertexSelection");
const MString apiMeshSubSceneOverride::sEdgeSelectionName("apiMeshEdgeSelection");
const MString apiMeshSubSceneOverride::sFaceSelectionName("apiMeshFaceSelection");
const MString apiMeshSubSceneOverride::sActiveVertexName("apiMeshActiveVertex");
const MString apiMeshSubSceneOverride::sActiveEdgeName("apiMeshActiveEdge");
const MString apiMeshSubSceneOverride::sActiveFaceName("apiMeshActiveFace");
apiMeshSubSceneOverride::apiMeshSubSceneOverride(const MObject& obj)
: MPxSubSceneOverride(obj)
, fObject(obj)
, fMesh(NULL)
, fWireShader(NULL)
, fThickWireShader(NULL)
, fSelectShader(NULL)
, fThickSelectShader(NULL)
, fShadedShader(NULL)
, fVertexComponentShader(NULL)
, fEdgeComponentShader(NULL)
, fFaceComponentShader(NULL)
, fPositionBuffer(NULL)
, fNormalBuffer(NULL)
, fBoxPositionBuffer(NULL)
, fBoxPositionBufferId(0)
, fBoxPositionBufferDX(NULL)
, fBoxIndexBufferId(0)
, fBoxIndexBufferDX(NULL)
, fWireIndexBuffer(NULL)
, fBoxIndexBuffer(NULL)
, fShadedIndexBuffer(NULL)
, fActiveVerticesIndexBuffer(NULL)
, fActiveEdgesIndexBuffer(NULL)
, fActiveFacesIndexBuffer(NULL)
, fThickLineWidth(-1.0)
, fQueuedLineWidth(-1.0)
, fNumInstances(0)
, fIsInstanceMode(false)
, fQueueUpdate(false)
, fUseQueuedLineUpdate(false) // Set to true to run sample line width update code
{
// get the real apiMesh object from the MObject
MStatus status;
MFnDependencyNode node(obj, &status);
if (status)
{
fMesh = dynamic_cast<apiMesh*>(node.userNode());
}
}
apiMeshSubSceneOverride::~apiMeshSubSceneOverride()
{
fMesh = NULL;
// Clear out LinkLost callbacks still in flight:
// They might get triggered if the shader are destroyed before
// the render items, and at this point the fOverride member is clearly invalid.
std::vector<ShadedItemUserData*>::iterator it;
for (it = fLinkLostCallbackData.begin(); it != fLinkLostCallbackData.end(); ++it)
{
ShadedItemUserData* cbData = *it;
cbData->fOverride = 0;
}
fLinkLostCallbackData.clear();
MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
if (renderer)
{
const MHWRender::MShaderManager* shaderMgr =
renderer->getShaderManager();
if (shaderMgr)
{
if (fWireShader) {
shaderMgr->releaseShader(fWireShader);
fWireShader = NULL;
}
if (fThickWireShader) {
shaderMgr->releaseShader(fThickWireShader);
fThickWireShader = NULL;
}
if (fSelectShader) {
shaderMgr->releaseShader(fSelectShader);
fSelectShader = NULL;
}
if (fThickSelectShader) {
shaderMgr->releaseShader(fThickSelectShader);
fThickSelectShader = NULL;
}
if (fShadedShader) {
shaderMgr->releaseShader(fShadedShader);
fShadedShader = NULL;
}
if (fVertexComponentShader) {
shaderMgr->releaseShader(fVertexComponentShader);
fVertexComponentShader = NULL;
}
if (fEdgeComponentShader) {
shaderMgr->releaseShader(fEdgeComponentShader);
fEdgeComponentShader = NULL;
}
if (fFaceComponentShader) {
shaderMgr->releaseShader(fFaceComponentShader);
fFaceComponentShader = NULL;
}
}
}
deleteBuffers();
}
void apiMeshSubSceneOverride::untrackLinkLostData(ShadedItemUserData* data)
{
for (size_t i = 0; i < fLinkLostCallbackData.size(); ++i)
{
if (fLinkLostCallbackData[i] == data)
{
fLinkLostCallbackData[i] = fLinkLostCallbackData.back();
fLinkLostCallbackData.pop_back();
break;
}
}
}
MHWRender::DrawAPI apiMeshSubSceneOverride::supportedDrawAPIs() const
{
// this plugin supports both GL and DX
return (MHWRender::kOpenGL | MHWRender::kDirectX11 | MHWRender::kOpenGLCoreProfile);
}
bool apiMeshSubSceneOverride::requiresUpdate(
const MHWRender::MSubSceneContainer& container,
const MHWRender::MFrameContext& frameContext) const
{
// Nothing in the container, definitely need to update
if (container.count() == 0)
{
return true;
}
// Update always. This could be optimized to only update when the
// actual shape node detects a change.
return true;
}
void apiMeshSubSceneOverride::update(
MHWRender::MSubSceneContainer& container,
const MHWRender::MFrameContext& frameContext)
{
// Update render items based on current set of instances
manageRenderItems(container, frameContext,
fMesh->shapeDirty() || container.count() == 0);
// Always reset shape dirty flag
fMesh->resetShapeDirty();
}
bool apiMeshSubSceneOverride::furtherUpdateRequired(
const MHWRender::MFrameContext& frameContext)
{
if (fUseQueuedLineUpdate)
{
if (!frameContext.inUserInteraction()
&& !frameContext.userChangingViewContext())
{
return fQueueUpdate;
}
}
return false;
}
void apiMeshSubSceneOverride::manageRenderItems(
MHWRender::MSubSceneContainer& container,
const MHWRender::MFrameContext& frameContext,
bool updateGeometry)
{
// Preamble
using namespace MHWRender;
if (!fMesh || fObject.isNull()) return;
MRenderer* renderer = MRenderer::theRenderer();
if (!renderer) return;
const MShaderManager* shaderMgr = renderer->getShaderManager();
if (!shaderMgr) return;
MStatus status;
MFnDagNode node(fObject, &status);
if (!status) return;
MDagPathArray instances;
if (!node.getAllPaths(instances) || instances.length() == 0) return;
// Constants
static const float sRed[] = {1.0f, 0.0f, 0.0f, 1.0f};
static const float sGreen[] = {0.0f, 1.0f, 0.0f, 1.0f};
static const float sWhite[] = {1.0f, 1.0f, 1.0f, 1.0f};
// Set up shared shaders if needed
if (!fWireShader)
{
fWireShader = shaderMgr->getStockShader(
MShaderManager::k3dSolidShader);
fWireShader->setParameter("solidColor", sRed);
}
if (!fThickWireShader)
{
fThickWireShader = shaderMgr->getStockShader(
MShaderManager::k3dThickLineShader);
fThickWireShader->setParameter("solidColor", sRed);
}
if (!fSelectShader)
{
fSelectShader = shaderMgr->getStockShader(
MShaderManager::k3dSolidShader);
fSelectShader->setParameter("solidColor", sGreen);
}
if (!fThickSelectShader)
{
fThickSelectShader = shaderMgr->getStockShader(
MShaderManager::k3dThickLineShader);
fThickSelectShader->setParameter("solidColor", sGreen);
}
if (!fVertexComponentShader)
{
fVertexComponentShader = shaderMgr->getStockShader(
MShaderManager::k3dFatPointShader);
fVertexComponentShader->setParameter("solidColor", sWhite);
const float pointSize[2] = {5.0, 5.0};
fVertexComponentShader->setParameter("pointSize", pointSize);
}
if (!fEdgeComponentShader)
{
fEdgeComponentShader = shaderMgr->getStockShader(
MShaderManager::k3dThickLineShader);
fEdgeComponentShader->setParameter("solidColor", sWhite);
const float lineWidth[2] = {2.0, 2.0};
fEdgeComponentShader->setParameter("lineWidth", lineWidth);
}
if (!fFaceComponentShader)
{
fFaceComponentShader = shaderMgr->getStockShader(
MShaderManager::k3dSolidShader);
fFaceComponentShader->setParameter("solidColor", sWhite);
}
if (!fShadedShader)
{
fShadedShader = shaderMgr->getStockShader(MShaderManager::k3dBlinnShader);
}
// Set up shared geometry if necessary
if (updateGeometry) rebuildGeometryBuffers();
if (!fPositionBuffer || !fNormalBuffer || !fBoxPositionBuffer ||
!fWireIndexBuffer || !fBoxIndexBuffer || !fShadedIndexBuffer)
{
return;
}
MSelectionList selectedList;
MGlobal::getActiveSelectionList(selectedList);
bool anyMatrixChanged = false;
bool itemsChanged = false;
unsigned int instanceArrayLength = instances.length();
unsigned int numInstanceSelected = 0;
unsigned int numInstanceUnselected = 0;
unsigned int numInstances = 0;
MMatrixArray instanceMatrixArray(instanceArrayLength);
MMatrixArray selectedInstanceMatrixArray(instanceArrayLength);
MMatrixArray unselectedInstanceMatrixArray(instanceArrayLength);
for (unsigned int instIdx=0; instIdx<instanceArrayLength; instIdx++)
{
// If expecting large numbers of instances, then walking through the whole
// list of instances every time to look for changes is not efficient
// enough. Watching for change events and changing only the required
// instances should be done instead. This method of checking for selection
// status is also not fast.
if (!instances[instIdx].isValid() || !instances[instIdx].isVisible())
continue;
unsigned int instanceNum = instances[instIdx].instanceNumber();
InstanceInfo instanceInfo(instances[instIdx].inclusiveMatrix(),
useSelectHighlight(selectedList, instances[instIdx]));
InstanceInfoMap::iterator iter = fInstanceInfoCache.find(instanceNum);
if (iter == fInstanceInfoCache.end() ||
iter->second.fIsSelected != instanceInfo.fIsSelected ||
!iter->second.fTransform.isEquivalent(instanceInfo.fTransform))
{
fInstanceInfoCache[instanceNum] = instanceInfo;
anyMatrixChanged = true;
}
instanceMatrixArray[numInstances++] = instanceInfo.fTransform;
if (instanceInfo.fIsSelected)
{
selectedInstanceMatrixArray[numInstanceSelected++] = instanceInfo.fTransform;
}
else
{
unselectedInstanceMatrixArray[numInstanceUnselected++] = instanceInfo.fTransform;
}
}
instanceMatrixArray.setLength(numInstances); //collapse to correct length
selectedInstanceMatrixArray.setLength(numInstanceSelected);
unselectedInstanceMatrixArray.setLength(numInstanceUnselected);
if (fNumInstances != numInstances)
{
anyMatrixChanged = true;
fNumInstances = numInstances;
}
const bool anyInstanceSelected = numInstanceSelected > 0;
const bool anyInstanceUnselected = numInstanceUnselected > 0;
std::set<int> activeVerticesSet;
std::set<int> activeEdgesSet;
std::set<int> activeFacesSet;
if (fMesh->hasActiveComponents())
{
MObjectArray activeComponents = fMesh->activeComponents();
if (activeComponents.length() > 0)
{
MFnSingleIndexedComponent fnComponent( activeComponents[0] );
if (fnComponent.elementCount())
{
MIntArray activeIds;
fnComponent.getElements( activeIds );
if(fnComponent.componentType() == MFn::kMeshVertComponent)
{
for (unsigned int i=0; i<activeIds.length(); ++i)
activeVerticesSet.insert( activeIds[i] );
}
else if(fnComponent.componentType() == MFn::kMeshEdgeComponent)
{
for (unsigned int i=0; i<activeIds.length(); ++i)
activeEdgesSet.insert( activeIds[i] );
}
else if(fnComponent.componentType() == MFn::kMeshPolygonComponent)
{
for (unsigned int i=0; i<activeIds.length(); ++i)
activeFacesSet.insert( activeIds[i] );
}
}
}
}
// Update index buffer of active items if necessary
const bool updateActiveItems =
updateGeometry ||
fActiveVerticesSet != activeVerticesSet ||
fActiveEdgesSet != activeEdgesSet ||
fActiveFacesSet != activeFacesSet;
fActiveVerticesSet = activeVerticesSet;
fActiveEdgesSet = activeEdgesSet;
fActiveFacesSet = activeFacesSet;
if (updateActiveItems)
rebuildActiveComponentIndexBuffers();
const bool anyVertexSelected = !fActiveVerticesSet.empty();
const bool anyEdgeSelected = !fActiveEdgesSet.empty();
const bool anyFaceSelected = !fActiveFacesSet.empty();
if( (anyVertexSelected && fActiveVerticesIndexBuffer == NULL) ||
(anyEdgeSelected && fActiveEdgesIndexBuffer == NULL) ||
(anyFaceSelected && fActiveFacesIndexBuffer == NULL) )
return;
// Add render items if necessary. Remove any pre-existing render items
// that are no longer needed.
MRenderItem* wireItem = container.find(sWireName);
if (!wireItem && anyInstanceUnselected)
{
wireItem = MRenderItem::Create(
sWireName,
MRenderItem::DecorationItem,
MGeometry::kLines);
wireItem->setDrawMode(MGeometry::kWireframe);
wireItem->depthPriority(MRenderItem::sActiveWireDepthPriority);
wireItem->setShader(fWireShader);
container.add(wireItem);
itemsChanged = true;
}
else if (wireItem && !anyInstanceUnselected)
{
container.remove(sWireName);
wireItem = NULL;
itemsChanged = true;
}
MRenderItem* selectItem = container.find(sSelectName);
if (!selectItem && anyInstanceSelected)
{
selectItem = MRenderItem::Create(
sSelectName,
MRenderItem::DecorationItem,
MGeometry::kLines);
selectItem->setDrawMode((MGeometry::DrawMode)(MGeometry::kWireframe | MGeometry::kShaded | MGeometry::kTextured));
selectItem->depthPriority(MRenderItem::sActiveWireDepthPriority);
selectItem->setShader(fSelectShader);
container.add(selectItem);
itemsChanged = true;
}
else if (selectItem && !anyInstanceSelected)
{
container.remove(sSelectName);
selectItem = NULL;
itemsChanged = true;
}
MRenderItem* boxItem = container.find(sBoxName);
if (!boxItem && anyInstanceUnselected)
{
boxItem = MRenderItem::Create(
sBoxName,
MRenderItem::NonMaterialSceneItem,
MGeometry::kLines);
boxItem->setDrawMode(MGeometry::kBoundingBox);
boxItem->setShader(fWireShader);
container.add(boxItem);
itemsChanged = true;
}
else if (boxItem && !anyInstanceUnselected)
{
container.remove(sBoxName);
boxItem = NULL;
itemsChanged = true;
}
MRenderItem* selectedBoxItem = container.find(sSelectedBoxName);
if (!selectedBoxItem && anyInstanceSelected)
{
selectedBoxItem = MRenderItem::Create(
sSelectedBoxName,
MRenderItem::NonMaterialSceneItem,
MGeometry::kLines);
selectedBoxItem->setDrawMode(MGeometry::kBoundingBox);
selectedBoxItem->setShader(fSelectShader);
container.add(selectedBoxItem);
itemsChanged = true;
}
else if (selectedBoxItem && !anyInstanceSelected)
{
container.remove(sSelectedBoxName);
selectedBoxItem = NULL;
itemsChanged = true;
}
MRenderItem* shadedItem = container.find(sShadedName);
if (!shadedItem)
{
// We always want a shaded item
shadedItem = MRenderItem::Create(
sShadedName,
MRenderItem::MaterialSceneItem,
MGeometry::kTriangles);
shadedItem->setDrawMode(MGeometry::kShaded);
shadedItem->setExcludedFromPostEffects(false);
shadedItem->castsShadows(true);
shadedItem->receivesShadows(true);
container.add(shadedItem);
itemsChanged = true;
}
MRenderItem* texturedItem = container.find(sTexturedName);
if (!texturedItem)
{
// We always want a textured item
texturedItem = MRenderItem::Create(
sTexturedName,
MRenderItem::MaterialSceneItem,
MGeometry::kTriangles);
texturedItem->setDrawMode(MGeometry::kTextured);
texturedItem->setExcludedFromPostEffects(false);
texturedItem->castsShadows(true);
texturedItem->receivesShadows(true);
container.add(texturedItem);
itemsChanged = true;
}
// Grab shading node from first component of first instance of the
// object and use it to get an MShaderInstance. This could be expanded
// to support full instancing and components if necessary.
MPlugArray connectedPlugs;
MObjectArray sets, comps;
if (node.getConnectedSetsAndMembers(0, sets, comps, true))
{
for (unsigned int i=0; i<sets.length(); i++)
{
MFnDependencyNode fnSet(sets[i], &status);
if (status)
{
MPlug shaderPlug = fnSet.findPlug("surfaceShader");
if (!shaderPlug.isNull())
{
shaderPlug.connectedTo(connectedPlugs, true, false);
if (connectedPlugs.length() > 0) break;
}
}
}
}
// Update shader for shaded item
if (fMesh->materialDirty() || !shadedItem->isShaderFromNode())
{
ShadedItemUserData *userData = new ShadedItemUserData(this);
if (connectedPlugs.length() > 0 &&
shadedItem->setShaderFromNode(connectedPlugs[0].node(),
instances[0],
shadedItemLinkLost,
userData,
true))
{
fLinkLostCallbackData.push_back(userData);
}
else
{
shadedItem->setShader(fShadedShader);
delete userData;
}
}
// Update shader for textured item
if (fMesh->materialDirty() || !texturedItem->isShaderFromNode())
{
ShadedItemUserData *userData = new ShadedItemUserData(this);
if (connectedPlugs.length() > 0 &&
texturedItem->setShaderFromNode(connectedPlugs[0].node(),
instances[0],
shadedItemLinkLost,
userData,
false))
{
fLinkLostCallbackData.push_back(userData);
}
else
{
texturedItem->setShader(fShadedShader);
delete userData;
}
}
// Reset the dirty flag since shader has been set for both shaded item and
// textured item.
fMesh->setMaterialDirty(false);
// render item for vertex selection
MRenderItem* vertexSelectionItem = container.find(sVertexSelectionName);
if (!vertexSelectionItem)
{
vertexSelectionItem = MRenderItem::Create(
sVertexSelectionName,
MRenderItem::DecorationItem,
MGeometry::kPoints);
// use for selection only : not visible in viewport
vertexSelectionItem->setDrawMode(MHWRender::MGeometry::kSelectionOnly);
// set the selection mask to kSelectMeshVerts : we want the render item to be used for Vertex Components selection
MSelectionMask mask(MSelectionMask::kSelectMeshVerts);
mask.addMask(MSelectionMask::kSelectPointsForGravity);
vertexSelectionItem->setSelectionMask( mask );
// set selection priority : on top
vertexSelectionItem->depthPriority(MRenderItem::sSelectionDepthPriority);
vertexSelectionItem->setShader(fVertexComponentShader);
container.add(vertexSelectionItem);
itemsChanged = true;
}
// change this value to enable item instancing : same item will be rendered multiple times
// the edge selection item will be visible in the viewport and rendered 3 times (with different scaling)
// only the second instance (scale 1.5) will be selectable (see simpleComponentConverter)
static bool useDrawInstancingOnEdgeSelectionItem = false;
// render item for edge selection
MRenderItem* edgeSelectionItem = container.find(sEdgeSelectionName);
if (!edgeSelectionItem)
{
// use for selection only : not visible in viewport
unsigned int drawMode = MHWRender::MGeometry::kSelectionOnly;
unsigned int depthPriority = MRenderItem::sSelectionDepthPriority;
if (useDrawInstancingOnEdgeSelectionItem) {
// display in viewport and in selection
drawMode = MHWRender::MGeometry::kAll;
// reduce priority so we can see the active item
depthPriority = MRenderItem::sActiveWireDepthPriority-1;
}
edgeSelectionItem = MRenderItem::Create(
sEdgeSelectionName,
MRenderItem::DecorationItem,
MGeometry::kLines);
edgeSelectionItem->setDrawMode( (MGeometry::DrawMode)drawMode);
// set the selection mask to kSelectMeshEdges : we want the render item to be used for Edge Components selection
edgeSelectionItem->setSelectionMask( MSelectionMask::kSelectMeshEdges );
edgeSelectionItem->depthPriority(depthPriority);
edgeSelectionItem->setShader(fWireShader);
container.add(edgeSelectionItem);
itemsChanged = true;
}
// render item for face selection
MRenderItem* faceSelectionItem = container.find(sFaceSelectionName);
if (!faceSelectionItem)
{
faceSelectionItem = MRenderItem::Create(
sFaceSelectionName,
MRenderItem::DecorationItem,
MGeometry::kTriangles);
// use for selection only : not visible in viewport
faceSelectionItem->setDrawMode(MHWRender::MGeometry::kSelectionOnly);
// set the selection mask to kSelectMeshFaces : we want the render item to be used for Face Components selection
faceSelectionItem->setSelectionMask( MSelectionMask::kSelectMeshFaces );
// set selection priority : on top
faceSelectionItem->depthPriority(MRenderItem::sSelectionDepthPriority);
faceSelectionItem->setShader(fFaceComponentShader);
container.add(faceSelectionItem);
itemsChanged = true;
}
// create and add a custom data to help the face component converter
if (updateGeometry)
{
apiMeshHWSelectionUserData* mySelectionData = new apiMeshHWSelectionUserData();
mySelectionData->fMeshGeom = fMesh->meshGeom();
faceSelectionItem->setCustomData(mySelectionData);
}
// render item to display active (selected) vertices
MRenderItem* activeVertexItem = container.find(sActiveVertexName);
if (!activeVertexItem && anyVertexSelected)
{
activeVertexItem = MRenderItem::Create(
sActiveVertexName,
MRenderItem::DecorationItem,
MGeometry::kPoints);
activeVertexItem->setDrawMode(MGeometry::kAll);
activeVertexItem->depthPriority(MRenderItem::sActivePointDepthPriority);
activeVertexItem->setShader(fVertexComponentShader);
container.add(activeVertexItem);
itemsChanged = true;
}
else if (activeVertexItem && !anyVertexSelected)
{
container.remove(sActiveVertexName);
activeVertexItem = NULL;
itemsChanged = true;
}
// render item to display active (selected) edges
MRenderItem* activeEdgeItem = container.find(sActiveEdgeName);
if (!activeEdgeItem && anyEdgeSelected)
{
activeEdgeItem = MRenderItem::Create(
sActiveEdgeName,
MRenderItem::DecorationItem,
MGeometry::kLines);
activeEdgeItem->setDrawMode(MGeometry::kAll);
activeEdgeItem->depthPriority(MRenderItem::sActiveLineDepthPriority);
activeEdgeItem->setShader(fEdgeComponentShader);
container.add(activeEdgeItem);
itemsChanged = true;
}
else if (activeEdgeItem && !anyEdgeSelected)
{
container.remove(sActiveEdgeName);
activeEdgeItem = NULL;
itemsChanged = true;
}
// render item to display active (selected) faces
MRenderItem* activeFaceItem = container.find(sActiveFaceName);
if (!activeFaceItem && anyFaceSelected)
{
activeFaceItem = MRenderItem::Create(
sActiveFaceName,
MRenderItem::DecorationItem,
MGeometry::kTriangles);
activeFaceItem->setDrawMode(MGeometry::kAll);
activeFaceItem->depthPriority(MRenderItem::sActiveLineDepthPriority);
activeFaceItem->setShader(fFaceComponentShader);
container.add(activeFaceItem);
itemsChanged = true;
}
else if (activeFaceItem && !anyFaceSelected)
{
container.remove(sActiveFaceName);
activeFaceItem = NULL;
itemsChanged = true;
}
// update the line width on the shader for our wire items if it changed
float lineWidth = frameContext.getGlobalLineWidth();
bool userWidthChange = !floatApproxEqual(lineWidth, fThickLineWidth);
bool doUpdate = false;
const float targetRefinedLineWidth = 50.0f;
if (userWidthChange)
{
fThickLineWidth = lineWidth;
doUpdate = true;
// First user change will trigger an update requirement
if (fUseQueuedLineUpdate)
{
fQueuedLineWidth = lineWidth;
if (fQueuedLineWidth < targetRefinedLineWidth)
{
fQueueUpdate = true;
}
}
}
else
{
// Increment by 1 until we reach the target width.
// If we haven't reached it yet then queue another update
// so we can increment and retest against the target width.
if (fUseQueuedLineUpdate && fQueueUpdate)
{
if (fQueuedLineWidth < targetRefinedLineWidth)
{
lineWidth = fQueuedLineWidth;
fQueuedLineWidth++;
fQueueUpdate = true;
doUpdate = true;
}
else
{
// Reached target. Stop asking for more refinement
fQueueUpdate = false;
}
}
}
if (doUpdate)
{
if (!floatApproxEqual(lineWidth, 1.0f))
{
// Only set the shader if the line width changes (or the first time)
float lineWidthArray[2] = {0};
lineWidthArray[0] = lineWidth;
lineWidthArray[1] = lineWidth;
fThickWireShader->setParameter("lineWidth", lineWidthArray);
fThickSelectShader->setParameter("lineWidth", lineWidthArray);
if (wireItem)
wireItem->setShader(fThickWireShader);
if (selectItem)
selectItem->setShader(fThickSelectShader);
}
else
{
if (wireItem)
wireItem->setShader(fWireShader);
if (selectItem)
selectItem->setShader(fSelectShader);
}
}
// Update geometry if required
if (itemsChanged || updateGeometry)
{
MBoundingBox bounds = fMesh->boundingBox();
MVertexBufferArray wireBuffers;
wireBuffers.addBuffer("positions", fPositionBuffer);
if (wireItem)
setGeometryForRenderItem(*wireItem, wireBuffers, *fWireIndexBuffer, &bounds);
if (selectItem)
setGeometryForRenderItem(*selectItem, wireBuffers, *fWireIndexBuffer, &bounds);
setGeometryForRenderItem(*edgeSelectionItem, wireBuffers, *fWireIndexBuffer, &bounds);
MVertexBufferArray boxBuffers;
boxBuffers.addBuffer("positions", fBoxPositionBuffer);
if (boxItem)
setGeometryForRenderItem(*boxItem, boxBuffers, *fBoxIndexBuffer, &bounds);
if (selectedBoxItem)
setGeometryForRenderItem(*selectedBoxItem, boxBuffers, *fBoxIndexBuffer, &bounds);
MVertexBufferArray shadedBuffers;
shadedBuffers.addBuffer("positions", fPositionBuffer);
shadedBuffers.addBuffer("normals", fNormalBuffer);
setGeometryForRenderItem(*shadedItem, shadedBuffers, *fShadedIndexBuffer, &bounds);
setGeometryForRenderItem(*texturedItem, shadedBuffers, *fShadedIndexBuffer, &bounds);