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
|
// Shave and a Haircut
// (c) 2019 Epic Games
// US Patent 6720962
/**********************************************************************
*<
FILE: shavePack2TexCmd.cpp
DESCRIPTION: command for packing hair data to image file
HISTORY: created 23-04-2013
*>
**********************************************************************/
#include "shavePack2TexCmd.h"
#include "shaveHairShape.h"
#include "shaveAPI.h"
#include "shaveRender.h"
#include <maya/MDagPath.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MDGModifier.h>
#include <maya/MArgDatabase.h>
#include <maya/MPlugArray.h>
#include <maya/MDagModifier.h>
#include <maya/MSelectionList.h>
#include <maya/MFnRenderLayer.h>
#include <maya/MItDependencyNodes.h>
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
| Command |
/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
MString shavePack2TexCmd::cmd = "shavePack2Tex";
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
| Flags |
/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
char* shavePack2TexCmd::sf_file = "f";
char* shavePack2TexCmd::lf_file = "file";
char* shavePack2TexCmd::sf_hair = "h";
char* shavePack2TexCmd::lf_hair = "hair";
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
| Methods |
/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
MStatus shavePack2TexCmd::doIt(const MArgList &maList)
{
MStatus stat;
MArgDatabase parser(syntax(),maList,&stat);
if(stat != MStatus::kSuccess)
{
MGlobal::displayError("shavePack2TexCmd: can not parse arguments.");
return stat;
}
if(parser.isFlagSet(sf_file) &&
parser.isFlagSet(sf_hair))
{
MString file;
MString hair;
if(parser.getFlagArgument(sf_file,0,file) != MStatus::kSuccess)
{
MGlobal::displayError("shavePack2TexCmd: can not get 'file' argument.");
return MStatus::kFailure;
}
MStringArray fparts;
if(file.split('.',fparts) != MStatus::kSuccess || fparts.length() < 2)
{
MGlobal::displayError(MString("shavePack2TexCmd: can not get file extention from ") + file);
return MStatus::kFailure;
}
if(fparts[1] != "tga")
{
MGlobal::displayWarning("shavePack2TexCmd: you need 32bpp format without compression .tga");
return MStatus::kFailure;
}
if(parser.getFlagArgument(sf_hair,0,hair) != MStatus::kSuccess)
{
MGlobal::displayError("shavePack2TexCmd: can not get 'hair' argument.");
return MStatus::kFailure;
}
MObject node;
if(!findNodeByName(hair,node))
{
MGlobal::displayError(MString("shavePack2TexCmd: can not find ") + hair + " node");
return MStatus::kFailure;
}
MFnDependencyNode dFn(node);
if(dFn.typeId() != shaveHairShape::id)
{
MGlobal::displayError(MString("shavePack2TexCmd: the node ") + hair + " is not a shaveHairShape");
return MStatus::kFailure;
}
MImage img;
if(!copyToImage(node,img))
{
MGlobal::displayError("shavePack2TexCmd: can not pack to image.");
return MStatus::kFailure;
}
MString mdir;
MGlobal::executeCommand("internalVar -userWorkspaceDir",mdir);
MString out = mdir+file;
MGlobal::displayInfo(MString("shavePack2TexCmd: saving file ") + out );
if(img.writeToFile(out,fparts[1]) != MStatus::kSuccess)
{
MGlobal::displayError(MString("shavePack2TexCmd: can not write ") + out );
return MStatus::kFailure;
}
return MStatus::kSuccess;
}
else
{
MGlobal::displayError("shavePack2TexCmd: you should specify 'file' and 'hair' flags.");
return MStatus::kFailure;
}
}
MSyntax shavePack2TexCmd::newSyntax()
{
MSyntax syn;
syn.addFlag(sf_file, lf_file, MSyntax::kString);
syn.addFlag(sf_hair, lf_hair, MSyntax::kString);
return syn;
}
#define SIMPLE_PACK
static void i2c(unsigned char* c, unsigned int i)
{
#ifdef SIMPLE_PACK
memcpy(c,&i,4);
#else
char b[4] = {0,0,0,0};
b[3] = i/1000000;
i -= b[3]*1000000;
b[2] = i/10000;
i -= b[2]*10000;
b[1] = i/100;
i -= b[1]*100;
b[0] = i;
memcpy(c,b,4);
#endif
}
static void f2c(unsigned char* c, float f)
{
#ifdef SIMPLE_PACK
memcpy(c,&f,4);
#else
char sign = f >= 0.0f ? 1 : 0;
f = fabs(f);
float af = floor(f);
float df = f - af;
if(df < 0.01f)
df = 0.0f;
//int d = f * pow(10.0f,7) - a * pow(10.0f,7);
//while(d >= 100)
// d /= 10;
int a = (int)af;
int d = (int)(df*100.0f);
char b[4] = {0,0,0,0};
b[3]=(char)(a/256);
b[2]=a%256;
b[1]=d;
b[0]=sign;
memcpy(c,b,4);
#endif
}
bool shavePack2TexCmd::copyToImage(MObject node, MImage& image) const
{
//MObjectArray nodes;
//nodes.append(node);
//crap, unresolved!!
//shaveAPI::HairInfo h;
//shaveAPI::exportHair(nodes, &h);
//MFnDependencyNode dFn(node);
//shaveHairShape* hairShape = (shaveHairShape*)dFn.userNode();
//SHAVENODE* hairNode = hairShape->getHairNode();
//hairShape->setStackIndex(0);
//SHAVEclear_stack();
//SHAVEadd_hairOPEN2(hairNode, 0);
MObjectArray nodes;
nodes.append(node);
shaveRender::buildHairStack(nodes, shaveConstant::kShutterBoth);
HAIRTYPE h;
SHAVEinit_hairtype(&h);
SHAVEexport_hairtype(&h);
// if(h.numHairs == 0 || h.numVertices == 0 || h.numHairVertices == 0)
// return false;
if(h.totalfaces == 0 || h.totalverts == 0 || h.totalfverts == 0)
{
MGlobal::displayError("shavePack2TexCmd: hairtype is empty.");
return false;
}
struct header
{
unsigned char num_hairs[4];
unsigned char num_knots[4];
};
struct hair
{
unsigned char start_vert[4];
unsigned char tip_radi[4];
unsigned char root_radi[4];
unsigned char root_color_r[4];
unsigned char root_color_g[4];
unsigned char root_color_b[4];
unsigned char tip_color_r[4];
unsigned char tip_color_g[4];
unsigned char tip_color_b[4];
};
struct vertex
{
unsigned char x[4];
unsigned char y[4];
unsigned char z[4];
};
//[header][hairs array][verts arra]
//int numknots = h.hairEndIndices[0] - h.hairStartIndices[0];
int numknots = h.face_end[0] - h.face_start[0];
int numhairs = h.totalfaces;
header he;
//i2c(he.num_hairs,h.numHairs);
i2c(he.num_hairs,h.totalfaces);
i2c(he.num_knots,numknots);
hair* har = (hair*) malloc(numhairs*sizeof(hair));
vertex* var = (vertex*) malloc(numhairs*numknots*sizeof(vertex));
for(int i = 0; i < numhairs; i++)
{
hair& hh = har[i];
i2c(hh.start_vert,i*numknots);
//f2c(hh.tip_radi,h.tipRadii[i]);
//f2c(hh.root_radi,h.rootRadii[i]);
f2c(hh.tip_radi,h.radiustip[i]);
f2c(hh.root_radi,h.radiusroot[i]);
//f2c(hh.root_color_r,h.rootColors[i].r);
//f2c(hh.root_color_g,h.rootColors[i].g);
//f2c(hh.root_color_b,h.rootColors[i].b);
f2c(hh.root_color_r,h.colorroot[i].x);
f2c(hh.root_color_g,h.colorroot[i].y);
f2c(hh.root_color_b,h.colorroot[i].z);
//f2c(hh.tip_color_r,h.tipColors[i].r);
//f2c(hh.tip_color_g,h.tipColors[i].g);
//f2c(hh.tip_color_b,h.tipColors[i].b);
f2c(hh.tip_color_r,h.colortip[i].x);
f2c(hh.tip_color_g,h.colortip[i].y);
f2c(hh.tip_color_b,h.colortip[i].z);
for(int k = 0; k < numknots; k++)
{
//int vi = h.hairStartIndices[i] + k;
int vi = h.face_start[i] + k;
vertex& vv = var[i*numknots + k];
//f2c(vv.x, h.vertices[vi].x);
//f2c(vv.y, h.vertices[vi].y);
//f2c(vv.z, h.vertices[vi].z);
f2c(vv.x, h.v[vi].x);
f2c(vv.y, h.v[vi].y);
f2c(vv.z, h.v[vi].z);
}
}
int npix = (sizeof(header) + numhairs*sizeof(hair) +numhairs*numknots*sizeof(vertex))/4;
int W = 1024;
int H = 1 + npix/W;
image.create(W,H,4);
unsigned char* pix = image.pixels();
memcpy(pix,&he,sizeof(header));
memcpy(&pix[sizeof(header)],har,numhairs*sizeof(hair));
memcpy(&pix[sizeof(header)+ numhairs*sizeof(hair)],var,numhairs*numknots*sizeof(vertex));
free(har);
free(var);
return true;
}
bool shavePack2TexCmd::findNodeByName(const MString& name, MObject& thenode) const
{
if(name == "")
return false;
MItDependencyNodes iter;
for (; !iter.isDone(); iter.next())
{
MObject node = iter.item();
MFnDependencyNode dFn(node);
if(dFn.name() == name)
{
thenode = node;
return true;
}
}
return false;
}
|