-
Notifications
You must be signed in to change notification settings - Fork 1
/
proWater.cpp
438 lines (340 loc) · 12.8 KB
/
proWater.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
//-
// ==========================================================================
// Copyright 1995,2006,2008 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.
// ==========================================================================
//+
//
// File: proWater.cc
//
// Description:
// Example implementation of a deformer. This node
// offsets vertices according to the CV's weights.
// The weights are set using the set editor or the
// percent command.
//
#include <string.h>
#include <maya/MIOStream.h>
#include <math.h>
#include <cmath>
#include <maya/MPxDeformerNode.h>
#include <maya/MItGeometry.h>
#include <maya/MPxLocatorNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnMatrixAttribute.h>
#include <maya/MFnMatrixData.h>
#include <maya/MFnPlugin.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MArrayDataHandle.h>
#include <maya/MPoint.h>
#include <maya/MVector.h>
#include <maya/MMatrix.h>
#include <maya/MFnMesh.h>
#include <maya/MDagModifier.h>
#include <simplexNoise.cpp>
#include <complex>
class proWater : public MPxDeformerNode
{
public:
proWater();
virtual ~proWater();
static void* creator();
static MStatus initialize();
MStatus compute(const MPlug& plug, MDataBlock& dataBlock);
// when the accessory is deleted, this node will clean itself up
//
virtual MObject& accessoryAttribute() const;
// create accessory nodes when the node is created
//
virtual MStatus accessoryNodeSetup(MDagModifier& cmd);
public:
// local node attributes
static MObject offsetMatrix; // offset center and axis
static MTypeId id;
static MObject time; //time variable
static MObject bigFreq;
static MObject amplitude1;
static MObject amplitude2;
static MObject amplitude3;
static MObject frequency1;
static MObject frequency2;
static MObject frequency3;
static MObject dir;
private:
};
MTypeId proWater::id( 0x8000c );
// local attributes
//
MObject proWater::offsetMatrix;
MObject proWater::time;
MObject proWater::bigFreq;
MObject proWater::amplitude1;
MObject proWater::amplitude2;
MObject proWater::amplitude3;
MObject proWater::frequency1;
MObject proWater::frequency2;
MObject proWater::frequency3;
MObject proWater::dir;
proWater::proWater() {}
proWater::~proWater() {}
void* proWater::creator()
{
return new proWater();
}
MStatus proWater::initialize()
{
// local attribute initialization
//time parameter
MFnNumericAttribute nAttr;
time = nAttr.create("time", "t", MFnNumericData::kDouble);
nAttr.setDefault(0.0);
nAttr.setKeyable(true);
nAttr.setSoftMin(0.0);
nAttr.setSoftMax(1000);
nAttr.setMin(0.0);
nAttr.setMax(1000);
addAttribute(time);
attributeAffects(proWater::time, proWater::outputGeom);
//
//direction parameter
MFnNumericAttribute dirAttr;
dir = dirAttr.create("direction", "dirDeg", MFnNumericData::kDouble);
dirAttr.setDefault(45);
dirAttr.setKeyable(true);
dirAttr.setSoftMin(0.0);
dirAttr.setSoftMax(360);
dirAttr.setMin(0.0);
dirAttr.setMax(360);
addAttribute(dir);
attributeAffects(proWater::dir, proWater::outputGeom);
//
//bigAmp1 parameter
MFnNumericAttribute bigAttr;
bigFreq = bigAttr.create("largeWaveAmplitude", "bigAmp", MFnNumericData::kDouble);
bigAttr.setDefault(3);
bigAttr.setKeyable(true);
bigAttr.setSoftMin(0.0);
bigAttr.setSoftMax(100);
bigAttr.setMin(0.0);
bigAttr.setMax(100);
addAttribute(bigFreq);
attributeAffects(proWater::bigFreq, proWater::outputGeom);
//
//amplitude1 parameter
MFnNumericAttribute ampAttr1;
amplitude1 = ampAttr1.create("firstOctaveAmplitude", "amp1", MFnNumericData::kDouble);
ampAttr1.setDefault(0.5);
ampAttr1.setKeyable(true);
ampAttr1.setSoftMin(0.0);
ampAttr1.setSoftMax(100);
ampAttr1.setMin(0.0);
ampAttr1.setMax(100);
addAttribute(amplitude1);
attributeAffects(proWater::amplitude1, proWater::outputGeom);
//
//frequency1 parameter
MFnNumericAttribute freqAttr1;
frequency1 = freqAttr1.create("firstFrequency", "freq1", MFnNumericData::kDouble);
freqAttr1.setDefault(0.5);
freqAttr1.setKeyable(true);
freqAttr1.setSoftMin(0.0);
freqAttr1.setSoftMax(100);
freqAttr1.setMin(0.0);
freqAttr1.setMax(100);
addAttribute(frequency1);
attributeAffects(proWater::frequency1, proWater::outputGeom);
//
//amplitude2 parameter
MFnNumericAttribute ampAttr2;
amplitude2 = ampAttr2.create("secondOctaveAmplitude", "amp2", MFnNumericData::kDouble);
ampAttr2.setDefault(1.3);
ampAttr2.setKeyable(true);
ampAttr2.setSoftMin(0.0);
ampAttr2.setSoftMax(100);
ampAttr2.setMin(0.0);
ampAttr2.setMax(100);
addAttribute(amplitude2);
attributeAffects(proWater::amplitude2, proWater::outputGeom);
//
//frequency2 parameter
MFnNumericAttribute freqAttr2;
frequency2 = freqAttr2.create("secondFrequency", "freq2", MFnNumericData::kDouble);
freqAttr2.setDefault(0.7);
freqAttr2.setKeyable(true);
freqAttr2.setSoftMin(0.0);
freqAttr2.setSoftMax(100);
freqAttr2.setMin(0.0);
freqAttr2.setMax(100);
addAttribute(frequency2);
attributeAffects(proWater::frequency2, proWater::outputGeom);
//
MFnMatrixAttribute mAttr;
offsetMatrix=mAttr.create( "locateMatrix", "lm");
mAttr.setStorable(false);
mAttr.setConnectable(true);
// deformation attributes
addAttribute( offsetMatrix);
attributeAffects( proWater::offsetMatrix, proWater::outputGeom );
return MStatus::kSuccess;
}
MStatus proWater::compute(const MPlug& plug, MDataBlock& dataBlock)
{
MStatus status = MStatus::kUnknownParameter;
if (plug.attribute() == outputGeom) {
// get the input corresponding to this output
//
unsigned int index = plug.logicalIndex();
MObject thisNode = this->thisMObject();
MPlug inPlug(thisNode,input);
inPlug.selectAncestorLogicalIndex(index,input);
MDataHandle hInput = dataBlock.inputValue(inPlug);
// get the input geometry and input groupId
//
MDataHandle hGeom = hInput.child(inputGeom);
MDataHandle hGroup = hInput.child(groupId);
unsigned int groupId = hGroup.asLong();
MDataHandle hOutput = dataBlock.outputValue(plug);
hOutput.copy(hGeom);
MStatus returnStatus;
MDataHandle envData = dataBlock.inputValue(envelope, &returnStatus);
if (MS::kSuccess != returnStatus) return returnStatus;
float env = envData.asFloat();
MDataHandle timeData = dataBlock.inputValue(time, &returnStatus);
if(MS::kSuccess != returnStatus) return returnStatus;
double t = timeData.asDouble();
MDataHandle dirData = dataBlock.inputValue(dir, &returnStatus);
if(MS::kSuccess != returnStatus) return returnStatus;
double dirDeg = dirData.asDouble();
MDataHandle bigData = dataBlock.inputValue(bigFreq, &returnStatus);
if(MS::kSuccess != returnStatus) return returnStatus;
double bigFreqAmp = bigData.asDouble();
MDataHandle ampData = dataBlock.inputValue(amplitude1, &returnStatus);
if(MS::kSuccess != returnStatus) return returnStatus;
double amp1 = ampData.asDouble();
MDataHandle freqData = dataBlock.inputValue(frequency1, &returnStatus);
if(MS::kSuccess != returnStatus) return returnStatus;
double freq1 = freqData.asDouble();
MDataHandle ampData2 = dataBlock.inputValue(amplitude2, &returnStatus);
if(MS::kSuccess != returnStatus) return returnStatus;
double amp2 = ampData2.asDouble();
MDataHandle freqData2 = dataBlock.inputValue(frequency2, &returnStatus);
if(MS::kSuccess != returnStatus) return returnStatus;
double freq2 = freqData2.asDouble();
// Get the MFnMesh
MStatus stat;
MObject inputObj = hOutput.data();
MFnMesh * meshFn = new MFnMesh(inputObj, &stat);
// do the deformation
//
MItGeometry iter(hOutput,groupId,false);
for ( ; !iter.isDone(); iter.next()) {
MPoint pt = iter.position();
//float2 uvPoint;
//float u,v;
//uvPoint[0] = u;
//uvPoint[1] = v;
//meshFn->getUVAtPoint(pt, uvPoint, MSpace::kObject);
float u = pt.x; //uvPoint[0]*100;
float v = pt.z; //uvPoint[1]*100;
float degDir = dirDeg;
float dir = degDir* M_PI/180;
float dirX = cos(dir);
float dirY = sin(dir);
float bigFreq = 0.01;
float bigWaves = scaled_raw_noise_3d(0, 1, (u + 3*t*dirX)*bigFreq*dirX, (v + 3*t*dirY)*bigFreq*dirY*2, t*0.01);
float frequency1 = freq1/10;//0.2;
float amplitude1 = amp1;//1.3;
float firstOctave = -(std::abs(scaled_raw_noise_3d(-amplitude1, amplitude1, (float)(u + 0.7*t*dirX)*frequency1*0.4, (float)(v + 0.7*t*dirY)*frequency1*0.6, 0.05*t))-amplitude1);
float frequency2 = freq2/10;
float amplitude2 = amp2;
float secondOctave = - (std::abs(scaled_raw_noise_3d(-amplitude2, amplitude2, (float)(u + 0.7*t*dirX)*frequency2*0.35, (float)(v + 0.7*t*dirY)*frequency2*0.65, 0.005*t))-amplitude2);
float frequency3 = freq1/10;
float amplitude3 = amp1/1.5;
float thirdOctave = - (std::abs(scaled_raw_noise_3d(-amplitude3, amplitude3, (float)(u + t*0.5*dirX)*frequency3*0.4, (float)(v + t*0.5*dirY)*frequency3*0.6, 30))-amplitude3);
float frequency4 = freq2/10;
float amplitude4 = amp2/1.5;
float fourthOctave = scaled_raw_noise_3d(-amplitude4, amplitude4, (float)(u + t*0.5*dirX)*frequency4*0.4, (float)(v + t*0.5*dirY)*frequency4*0.6, 50);
float frequency5 = freq2;
float amplitude5 = amp2/2;
float fifthOctave = scaled_raw_noise_3d(-amplitude5, amplitude5, (float)(u + t*0.5*dirX)*frequency5*0.15, (float)(v + t*0.5*dirY)*frequency5*0.85, 0.001*t);
float disp = bigFreqAmp*bigWaves + 7*(bigWaves)*firstOctave + secondOctave + thirdOctave*thirdOctave + fourthOctave + std::abs(bigWaves-1)*fifthOctave;
pt = pt + iter.normal()*disp;
iter.setPosition(pt);
}
delete meshFn;
status = MStatus::kSuccess;
}
return status;
}
/* override */
MObject&
proWater::accessoryAttribute() const
//
// Description:
// This method returns a the attribute to which an accessory
// shape is connected. If the accessory shape is deleted, the deformer
// node will automatically be deleted.
//
// This method is optional.
//
{
return proWater::offsetMatrix;
}
/* override */
MStatus
proWater::accessoryNodeSetup(MDagModifier& cmd)
//
// Description:
// This method is called when the deformer is created by the
// "deformer" command. You can add to the cmds in the MDagModifier
// cmd in order to hook up any additional nodes that your node needs
// to operate.
//
// In this example, we create a locator and attach its matrix attribute
// to the matrix input on the offset node. The locator is used to
// set the direction and scale of the random field.
//
// Description:
// This method is optional.
//
{
MStatus result;
// hook up the accessory node
//
MObject objLoc = cmd.createNode(MString("locator"),
MObject::kNullObj,
&result);
if (MS::kSuccess == result) {
MFnDependencyNode fnLoc(objLoc);
MString attrName;
attrName.set("matrix");
MObject attrMat = fnLoc.attribute(attrName);
result = cmd.connect(objLoc,attrMat,this->thisMObject(),proWater::offsetMatrix);
}
return result;
}
// standard initialization procedures
//
MStatus initializePlugin( MObject obj )
{
MStatus result;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "3.0", "Any");
result = plugin.registerNode( "proWater", proWater::id, proWater::creator,
proWater::initialize, MPxNode::kDeformerNode );
return result;
}
MStatus uninitializePlugin( MObject obj)
{
MStatus result;
MFnPlugin plugin( obj );
result = plugin.deregisterNode( proWater::id );
return result;
}