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
|
// Shave and a Haircut
// (c) 2019 Epic Games
// US Patent 6720962
#include <maya/MColor.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MFloatVector.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnMesh.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MGlobal.h>
#include <maya/MItDependencyGraph.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MItMeshVertex.h>
#include <maya/MPlug.h>
#include <maya/MPointArray.h>
#include <maya/MSelectionList.h>
#include <maya/MString.h>
#include <maya/MTypeId.h>
#include <math.h>
#include "shaveVertexShader.h"
// Static data
MTypeId shaveVertexShader::id(0x00102998);
const MString shaveVertexShader::nodeTypeName("shaveVertexShader");
// Attributes
MObject shaveVertexShader::pointAttr;
MObject shaveVertexShader::surfaceIndexAttr;
MObject shaveVertexShader::objectIdAttr;
MObject shaveVertexShader::primitiveIdAttr;
MObject shaveVertexShader::outColorAttr;
MObject shaveVertexShader::outAlphaAttr;
void shaveVertexShader::postConstructor()
{
setMPSafe(true);
}
shaveVertexShader::shaveVertexShader()
{
}
shaveVertexShader::~shaveVertexShader()
{
}
void * shaveVertexShader::creator()
{
return new shaveVertexShader();
}
MStatus shaveVertexShader::initialize()
{
MFnNumericAttribute nAttr;
pointAttr = nAttr.create("pointObj", "po", MFnNumericData::k3Float);
nAttr.setStorable(false);
nAttr.setHidden(true);
surfaceIndexAttr = nAttr.create("surfaceIndex", "si", MFnNumericData::kFloat);
nAttr.setHidden(true);
objectIdAttr = nAttr.create("objectId", "oi", MFnNumericData::kLong);
nAttr.setHidden(true);
primitiveIdAttr = nAttr.create("primitiveId", "pi", MFnNumericData::kLong);
nAttr.setHidden(true);
outColorAttr = nAttr.create("outColor", "oc", MFnNumericData::k3Float);
nAttr.setStorable(false);
nAttr.setReadable(true);
nAttr.setWritable(false);
outAlphaAttr = nAttr.create( "outAlpha", "oa", MFnNumericData::kFloat);
nAttr.setDisconnectBehavior(MFnAttribute::kReset);
nAttr.setStorable(false);
nAttr.setReadable(true);
nAttr.setWritable(false);
addAttribute(pointAttr);
addAttribute(outColorAttr);
addAttribute(outAlphaAttr);
addAttribute(surfaceIndexAttr);
addAttribute(objectIdAttr);
addAttribute(primitiveIdAttr);
attributeAffects(pointAttr, outColorAttr);
attributeAffects(surfaceIndexAttr, outColorAttr);
attributeAffects(objectIdAttr, outColorAttr);
attributeAffects(primitiveIdAttr, outColorAttr);
attributeAffects(pointAttr, outAlphaAttr);
attributeAffects(surfaceIndexAttr, outAlphaAttr);
attributeAffects(objectIdAttr, outAlphaAttr);
attributeAffects(primitiveIdAttr, outAlphaAttr);
return MS::kSuccess;
}
MStatus shaveVertexShader::compute( const MPlug& plug, MDataBlock& block )
{
if ((plug != outColorAttr) && (plug.parent() != outColorAttr) &&
(plug != outAlphaAttr))
{
return MS::kUnknownParameter;
}
MStatus status;
MObject thisNode = thisMObject();
MColor resultColour(0.5, 0.5, 0.5, 1.0);
long surfaceIndex = (long)(block.inputValue(surfaceIndexAttr).asFloat() + 0.5);
long triangleID = block.inputValue(primitiveIdAttr).asLong();
// Location of the point we are shading
float3& samplePtIn = block.inputValue(pointAttr).asFloat3();
MFloatVector samplePt(samplePtIn);
// Find the Mesh object
MPlug outColorPlug = MFnDependencyNode(thisNode).findPlug("outColor",
&status);
MItDependencyGraph depIt( outColorPlug, MFn::kShadingEngine,
MItDependencyGraph::kDownstream,
MItDependencyGraph::kBreadthFirst,
MItDependencyGraph::kNodeLevel,
&status);
depIt.enablePruningOnFilter();
MObject shadingEngineNode = depIt.thisNode();
MPlug dagSetMembersPlug = MFnDependencyNode(shadingEngineNode).findPlug(
"dagSetMembers", &status
);
if (surfaceIndex < (long)dagSetMembersPlug.numElements())
{
MPlug dagSetMembersElementPlug;
dagSetMembersElementPlug = dagSetMembersPlug.elementByLogicalIndex(
surfaceIndex, &status
);
MPlugArray meshPlugArray;
dagSetMembersElementPlug.connectedTo(
meshPlugArray, true, false, &status
);
if (meshPlugArray.length() > 0)
{
MObject meshNode = meshPlugArray[0].node();
int polygonID = 0;
int prevIndex = 0;
int i;
MFnMesh meshFn(meshNode);
MItMeshPolygon iter(meshNode);
int numPolygons = meshFn.numPolygons();
//
// If the mesh is empty, then there's nothing to do.
//
if (numPolygons > 0)
{
// We used to cache the face and triangle IDs for the
// current object, under the assumption that we would get
// many consecutive hits on the same object, thereby
// speeding things up. But the caching makes this node MP
// unsafe and disabling MP slows things down considerably.
// Also, it appears that the caching alone is slower than
// just handling each sample on the fly. So we no longer
// cache
for (i = 0; i < numPolygons; i++)
{
int nTri;
iter.setIndex(i, prevIndex);
iter.numTriangles(nTri);
if (triangleID < nTri)
{
polygonID = i;
break;
}
triangleID -= nTri;
}
iter.setIndex(polygonID, prevIndex);
MPointArray v;
MIntArray vertIndices;
//
// Get the triangle's vertices.
//
status = iter.getTriangle(
triangleID, v, vertIndices, MSpace::kObject
);
MFloatVector p1((float)v[0].x, (float)v[0].y, (float)v[0].z);
MFloatVector p2((float)v[1].x, (float)v[1].y, (float)v[1].z);
MFloatVector p3((float)v[2].x, (float)v[2].y, (float)v[2].z);
MColor colour1;
MColor colour2;
MColor colour3;
MItMeshVertex vertIter(meshNode);
prevIndex = 0;
vertIter.setIndex(vertIndices[0], prevIndex);
vertIter.getColor(colour1, polygonID);
vertIter.setIndex(vertIndices[1], prevIndex);
vertIter.getColor(colour2, polygonID);
vertIter.setIndex(vertIndices[2], prevIndex);
vertIter.getColor(colour3, polygonID);
//
// Calculate the point's barycentric coordinates.
//
samplePt = samplePt - p3;
p1 = p1 - p3;
p2 = p2 - p3;
MFloatVector norm = p1 ^ p2;
float lenSquared = norm * norm;
lenSquared = (norm * samplePt) / lenSquared;
//
// The point may not be exactly on the triangle, so move it
// there.
//
samplePt = samplePt - (lenSquared * norm);
float aa = p1 * p1;
float bb = p2 * p2;
float ab = p1 * p2;
float am = p1 * samplePt;
float bm = p2 * samplePt;
float det = aa*bb - ab*ab;
MFloatVector abc;
abc.x = (am*bb - bm*ab) / det;
abc.y = (bm*aa - am*ab) / det;
abc.z = 1 - abc.x - abc.y;
resultColour = (abc.x*colour1)
+ (abc.y*colour2)
+ (abc.z*colour3);
}
}
}
MDataHandle outColorHandle = block.outputValue(outColorAttr);
MFloatVector& outColor = outColorHandle.asFloatVector();
outColor.x = resultColour.r;
outColor.y = resultColour.g;
outColor.z = resultColour.b;
outColorHandle.setClean();
MDataHandle outAlphaHandle = block.outputValue(outAlphaAttr);
float& outAlpha = outAlphaHandle.asFloat();
outAlpha = resultColour.a;
outAlphaHandle.setClean();
return MS::kSuccess;
}
|