forked from blurstudio/TwistSpline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twistSplineNode.cpp
507 lines (407 loc) · 16.6 KB
/
twistSplineNode.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
/*
MIT License
Copyright (c) 2018 Blur Studio
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <maya/M3dView.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MGlobal.h>
#include <maya/MFnCompoundAttribute.h>
#include <maya/MFnMatrixAttribute.h>
#include <maya/MFnUnitAttribute.h>
#include <maya/MMatrix.h>
#include <maya/MFnPluginData.h>
#include <maya/MTransformationMatrix.h>
#include <maya/MTypes.h>
#include <maya/MVector.h>
#include <maya/MPoint.h>
#include <maya/MVectorArray.h>
#include <maya/MPointArray.h>
#include <maya/MBoundingBox.h>
#include <maya/MPxGeometryOverride.h>
#include <maya/MQuaternion.h>
#include <string>
#include <iostream>
#include "twistSpline.h"
#include "twistSplineData.h"
#include "twistSplineNode.h"
#define MCHECKERROR(STAT) \
if (MS::kSuccess != STAT) { \
return MS::kFailure; \
}
MTypeId TwistSplineNode::id(0x001226F7);
MString TwistSplineNode::drawDbClassification("drawdb/geometry/twistSpline");
MString TwistSplineNode::drawRegistrantId("TwistSplineNodePlugin");
MObject TwistSplineNode::aOutputSpline;
MObject TwistSplineNode::aSplineLength;
MObject TwistSplineNode::aVertexData;
MObject TwistSplineNode::aInTangent;
MObject TwistSplineNode::aOutTangent;
MObject TwistSplineNode::aControlVertex;
MObject TwistSplineNode::aParamValue;
MObject TwistSplineNode::aParamWeight;
MObject TwistSplineNode::aTwistValue;
MObject TwistSplineNode::aTwistWeight;
MObject TwistSplineNode::aUseOrient;
MObject TwistSplineNode::aDebugDisplay;
MObject TwistSplineNode::aDebugScale;
MObject TwistSplineNode::aMaxVertices;
TwistSplineNode::TwistSplineNode() {}
TwistSplineNode::~TwistSplineNode() {}
MStatus TwistSplineNode::initialize() {
MFnCompoundAttribute cAttr;
MFnMatrixAttribute mAttr;
MFnTypedAttribute tAttr;
MFnNumericAttribute nAttr;
MFnUnitAttribute uAttr;
//---------------- Output ------------------
aOutputSpline = tAttr.create("outputSpline", "os", TwistSplineData::id);
tAttr.setWritable(false);
addAttribute(aOutputSpline);
aSplineLength = uAttr.create("splineLength", "sl", MFnUnitAttribute::kDistance);
nAttr.setWritable(false);
addAttribute(aSplineLength);
//--------------- Input -------------------
aDebugDisplay = nAttr.create("debugDisplay", "dd", MFnNumericData::kBoolean, false);
addAttribute(aDebugDisplay);
aDebugScale = nAttr.create("debugScale", "ds", MFnNumericData::kDouble, 1.0);
addAttribute(aDebugScale);
aMaxVertices = nAttr.create("maxVertices", "mv", MFnNumericData::kInt, 1000);
nAttr.setMin(2);
addAttribute(aMaxVertices);
//--------------- Array -------------------
aInTangent = mAttr.create("inTangent", "int");
mAttr.setHidden(true);
mAttr.setDefault(MMatrix::identity);
aOutTangent = mAttr.create("outTangent", "ot");
mAttr.setHidden(true);
mAttr.setDefault(MMatrix::identity);
aControlVertex = mAttr.create("controlVertex", "cv");
mAttr.setHidden(true);
mAttr.setDefault(MMatrix::identity);
aParamValue = nAttr.create("paramValue", "pv", MFnNumericData::kDouble, 0.0);
aParamWeight = nAttr.create("paramWeight", "pw", MFnNumericData::kDouble, 0.0);
nAttr.setMin(0.0);
nAttr.setMax(1.0);
aTwistValue = uAttr.create("twistValue", "tv", MFnUnitAttribute::kAngle, 0.0);
aTwistWeight = nAttr.create("twistWeight", "tw", MFnNumericData::kDouble, 0.0);
nAttr.setMin(0.0);
nAttr.setMax(1.0);
aUseOrient = nAttr.create("useOrient", "uo", MFnNumericData::kDouble, 0.0);
nAttr.setMin(0.0);
nAttr.setMax(1.0);
aVertexData = cAttr.create("vertexData", "vd");
cAttr.setArray(true);
cAttr.addChild(aInTangent); // matrix
cAttr.addChild(aControlVertex); // matrix
cAttr.addChild(aOutTangent); // matrix
cAttr.addChild(aParamValue); // float unbounded
cAttr.addChild(aParamWeight); // float 0-1
cAttr.addChild(aTwistWeight); // float 0-1
cAttr.addChild(aUseOrient); // float 0-1
cAttr.addChild(aTwistValue); // float unbounded
addAttribute(aVertexData);
attributeAffects(aInTangent, aOutputSpline);
attributeAffects(aControlVertex, aOutputSpline);
attributeAffects(aOutTangent, aOutputSpline);
attributeAffects(aParamValue, aOutputSpline);
attributeAffects(aParamWeight, aOutputSpline);
attributeAffects(aTwistWeight, aOutputSpline);
attributeAffects(aTwistValue, aOutputSpline);
attributeAffects(aUseOrient, aOutputSpline);
attributeAffects(aMaxVertices, aOutputSpline);
attributeAffects(aInTangent, aSplineLength);
attributeAffects(aControlVertex, aSplineLength);
attributeAffects(aOutTangent, aSplineLength);
attributeAffects(aMaxVertices, aSplineLength);
return MS::kSuccess;
}
TwistSplineT* TwistSplineNode::getSplineData() const {
MStatus stat;
MObject output;
// Getting this plug causes an update
MPlug tPlug(thisMObject(), aOutputSpline);
tPlug.getValue(output);
MFnPluginData outData(output);
auto tsd = dynamic_cast<TwistSplineData *>(outData.data());
return tsd->getSpline();
}
MBoundingBox TwistSplineNode::boundingBox() const {
TwistSplineT *ts = getSplineData();
double minx, miny, minz, maxx, maxy, maxz;
minx = miny = minz = std::numeric_limits<double>::max();
maxx = maxy = maxz = std::numeric_limits<double>::min();
MPointArray pts = ts->getVerts();
for (unsigned i = 0; i < pts.length(); ++i) {
MPoint &pt = pts[i];
minx = pt[0] < minx ? pt[0] : minx;
miny = pt[1] < miny ? pt[1] : miny;
minz = pt[2] < minz ? pt[2] : minz;
maxx = pt[0] > maxx ? pt[0] : maxx;
maxy = pt[1] > maxy ? pt[1] : maxy;
maxz = pt[2] > maxz ? pt[2] : maxz;
}
return MBoundingBox(MPoint(minx, miny, minz), MPoint(maxx, maxy, maxz));
}
void TwistSplineNode::getDebugDraw(bool &oDraw, double &oScale) const {
MStatus stat;
MObject mobj = thisMObject();
oDraw = false;
oScale = 1.0;
if (!mobj.isNull()) {
MPlug drawPlug(mobj, aDebugDisplay);
if (!drawPlug.isNull()) {
drawPlug.getValue(oDraw);
}
MPlug scalePlug(mobj, aDebugScale);
if (!scalePlug.isNull()) {
scalePlug.getValue(oScale);
}
}
}
MStatus TwistSplineNode::compute(const MPlug& plug, MDataBlock& data) {
if (plug == aOutputSpline) {
MStatus status;
// Get all input Data
MPointArray points;
MPointArray scales;
std::vector<MQuaternion> quats;
std::vector<double> lockPositions;
std::vector<double> lockVals;
std::vector<double> twistLock;
std::vector<double> userTwist;
std::vector<double> orientLock;
MDataHandle hMaxVertices = data.inputValue(aMaxVertices);
int maxVertices = hMaxVertices.asInt();
// loop over the input matrices
MArrayDataHandle inputs = data.inputArrayValue(aVertexData);
unsigned ecount = inputs.elementCount();
// Trim the number of vertices to the maximum provided by the user
if (maxVertices < ecount) { ecount = maxVertices; }
// I'm OK with just looping over the physical indices here
// because if it's unconnected, then I don't really care
double sGet[4];
sGet[3] = 1.0;
bool gotLocks = false, gotOris = false;
for (unsigned i = 0; i < ecount; ++i) {
auto group = inputs.inputValue();
lockPositions.push_back(group.child(aParamValue).asDouble());
double lockIt = group.child(aParamWeight).asDouble();
lockVals.push_back(lockIt);
if (lockIt > 0.0) gotLocks = true;
twistLock.push_back(group.child(aTwistWeight).asDouble());
userTwist.push_back(group.child(aTwistValue).asDouble());
double oriIt = group.child(aUseOrient).asDouble();
orientLock.push_back(oriIt);
if (oriIt > 0.0) gotOris = true;
if (i > 0) {
// Ignore the tangent data for the first vertex
// because they come *before* it. I'm just using the same interface
// for conveninece here
auto itMat = MTransformationMatrix(group.child(aInTangent).asMatrix());
points.append(itMat.getTranslation(MSpace::kWorld));
itMat.getScale(sGet, MSpace::kObject);
scales.append(MPoint(sGet));
quats.push_back(itMat.rotation());
}
auto cvMat = MTransformationMatrix(group.child(aControlVertex).asMatrix());
points.append(cvMat.getTranslation(MSpace::kWorld));
cvMat.getScale(sGet, MSpace::kObject);
scales.append(MPoint(sGet));
quats.push_back(cvMat.rotation());
auto otMat = MTransformationMatrix(group.child(aOutTangent).asMatrix());
points.append(otMat.getTranslation(MSpace::kWorld));
otMat.getScale(sGet, MSpace::kObject);
scales.append(MPoint(sGet));
quats.push_back(otMat.rotation());
inputs.next();
}
// Remove the last item from the group. It's the out tangent past the end
if (!quats.empty())
quats.pop_back();
if (points.length() > 0)
points.remove(points.length() - 1);
if (scales.length() > 0)
scales.remove(scales.length() - 1);
if (!gotLocks && !lockVals.empty())
lockVals[0] = 1.0;
if (!gotOris && !orientLock.empty())
orientLock[0] = 1.0;
// Output Data Handles
MDataHandle storageH = data.outputValue(aOutputSpline, &status);
MCHECKERROR(status);
// Build output data object
TwistSplineData *outSplineData;
MFnPluginData fnDataCreator;
MTypeId tmpid(TwistSplineData::id);
fnDataCreator.create(tmpid, &status);
MCHECKERROR(status);
outSplineData = dynamic_cast<TwistSplineData*>(fnDataCreator.data(&status));
MCHECKERROR(status);
TwistSplineT *outSpline = outSplineData->getSpline();
//if (points.length() != 0)
outSpline->setVerts(points, scales, quats, lockPositions, lockVals, userTwist, twistLock, orientLock);
// Testing closest point
//outSpline->buildKDTree();
//auto cp = outSpline->getClosestPoint(MPoint());
storageH.setMPxData(outSplineData);
data.setClean(aOutputSpline);
}
else if (plug == aSplineLength) {
// First, get the splines that need computing, and their weight.
MDataHandle inHandle = data.inputValue(aOutputSpline);
MDataHandle outHandle = data.outputValue(aSplineLength);
MPxData* pd = inHandle.asPluginData();
if (pd == nullptr) {
outHandle.setDouble(0.0);
return MS::kSuccess;
}
auto inSplineData = (TwistSplineData *)pd;
TwistSplineT *spline = inSplineData->getSpline();
if (spline == nullptr) {
outHandle.setDouble(0.0);
return MS::kSuccess;
}
outHandle.setDouble(spline->getTotalLength());
data.setClean(aSplineLength);
}
else {
return MS::kUnknownParameter;
}
return MS::kSuccess;
}
void* TwistSplineNode::creator() {
return new TwistSplineNode();
}
void TwistSplineNode::draw(M3dView &view, const MDagPath &path, M3dView::DisplayStyle style, M3dView::DisplayStatus dstat) {
// TODO: build the legacy viewport draw mechanism
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Viewport 2.0 override implementation
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// By setting isAlwaysDirty to false in MPxDrawOverride constructor, the
// draw override will be updated (via prepareForDraw()) only when the node
// is marked dirty via DG evaluation or dirty propagation. Additional
// callback is also added to explicitly mark the node as being dirty (via
// MRenderer::setGeometryDrawDirty()) for certain circumstances. Note that
// the draw callback in MPxDrawOverride constructor is set to NULL in order
// to achieve better performance.
TwistSplineDrawOverride::TwistSplineDrawOverride(const MObject& obj)
: MHWRender::MPxDrawOverride(obj, NULL, false) {
fModelEditorChangedCbId = MEventMessage::addEventCallback("modelEditorChanged", OnModelEditorChanged, this);
MStatus status;
MFnDependencyNode node(obj, &status);
tsn = status ? dynamic_cast<TwistSplineNode*>(node.userNode()) : NULL;
}
TwistSplineDrawOverride::~TwistSplineDrawOverride() {
tsn = NULL;
if (fModelEditorChangedCbId != 0) {
MMessage::removeCallback(fModelEditorChangedCbId);
fModelEditorChangedCbId = 0;
}
}
void TwistSplineDrawOverride::OnModelEditorChanged(void *clientData) {
// Mark the node as being dirty so that it can update on display appearance
// switch among wireframe and shaded.
TwistSplineDrawOverride *ovr = static_cast<TwistSplineDrawOverride*>(clientData);
if (ovr && ovr->tsn) {
MHWRender::MRenderer::setGeometryDrawDirty(ovr->tsn->thisMObject());
}
}
MHWRender::DrawAPI TwistSplineDrawOverride::supportedDrawAPIs() const {
// this plugin supports both GL and DX
return (MHWRender::kOpenGL | MHWRender::kDirectX11 | MHWRender::kOpenGLCoreProfile);
}
bool TwistSplineDrawOverride::isBounded(const MDagPath& /*objPath*/, const MDagPath& /*cameraPath*/) const {
return true;
}
MBoundingBox TwistSplineDrawOverride::boundingBox(
const MDagPath& objPath,
const MDagPath& cameraPath) const {
return tsn->boundingBox();
}
// Called by Maya each time the object needs to be drawn.
MUserData* TwistSplineDrawOverride::prepareForDraw(
const MDagPath& objPath,
const MDagPath& cameraPath,
const MHWRender::MFrameContext& frameContext,
MUserData* oldData) {
MStatus status;
// Any data needed from the Maya dependency graph must be retrieved and cached in this stage.
TwistSplineDrawData* data = dynamic_cast<TwistSplineDrawData*>(oldData);
if (!data) data = new TwistSplineDrawData();
TwistSplineT* ts = tsn->getSplineData();
data->splinePoints = ts->getPoints();
tsn->getDebugDraw(data->debugDraw, data->debugScale);
if (data->debugDraw){
double scale = data->debugScale;
MVectorArray tans = ts->getTangents();
data->tangents.setLength(tans.length() * 2);
for (size_t i = 0; i < tans.length(); ++i) {
MPoint &spi = data->splinePoints[i];
data->tangents[2*i] = spi;
data->tangents[(2*i) +1] = scale * tans[i] + spi;
}
MVectorArray norms = ts->getNormals();
data->normals.setLength(norms.length() * 2);
for (size_t i = 0; i < norms.length(); ++i) {
MPoint &spi = data->splinePoints[i];
MVector &nn = norms[i];
data->normals[2 * i] = spi;
data->normals[(2 * i) + 1] = scale * nn + spi;
}
MVectorArray binorms = ts->getBinormals();
data->binormals.setLength(binorms.length() * 2);
for (size_t i = 0; i < binorms.length(); ++i) {
MPoint &spi = data->splinePoints[i];
data->binormals[2 * i] = spi;
data->binormals[(2 * i) + 1] = scale * binorms[i] + spi;
}
}
// get correct color based on the state of object, e.g. active or dormant
data->color = MHWRender::MGeometryUtilities::wireframeColor(objPath);
return data;
}
// addUIDrawables() provides access to the MUIDrawManager, which can be used
// to queue up operations for drawing simple UI elements such as lines, circles and
// text. To enable addUIDrawables(), override hasUIDrawables() and make it return true.
void TwistSplineDrawOverride::addUIDrawables(
const MDagPath& objPath,
MHWRender::MUIDrawManager& drawManager,
const MHWRender::MFrameContext& frameContext,
const MUserData* userData) {
TwistSplineDrawData* data = (TwistSplineDrawData*)userData;
if (!data) return;
bool draw2D = false; // ALWAYS false
drawManager.beginDrawable();
drawManager.setColor(data->color);
drawManager.setDepthPriority(MHWRender::MRenderItem::sActiveLineDepthPriority);
drawManager.lineStrip(data->splinePoints, draw2D);
if (data->debugDraw){
drawManager.setColor(MColor(.5, 0, 0));
drawManager.lineList(data->tangents, draw2D);
drawManager.setColor(MColor(0, 0, .5));
drawManager.lineList(data->binormals, draw2D);
drawManager.setColor(MColor(0, .5, 0));
drawManager.lineList(data->normals, draw2D);
}
drawManager.endDrawable();
}