aboutsummaryrefslogtreecommitdiff
path: root/mayaPlug/shaveShadowNode.cpp
blob: 2ee0334071685f4c36c4284af4e7f23b15f70479 (plain) (blame)
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
// Shave and a Haircut
// (c) 2019 Epic Games
// US Patent 6720962

#include "shaveGlobals.h"
#include "shaveShadowNode.h"
#include "shaveUtil.h"

MTypeId shaveShadowNode::id(0x00106502);


void shaveShadowNode::postConstructor( )
{
	setMPSafe(true);
}
 

const MString shaveShadowNode::nodeTypeName("shaveShadowNode");

MObject shaveShadowNode::IntensityValue;

MObject shaveShadowNode::aPointWorld;
MObject shaveShadowNode::aPointWorldX;
MObject shaveShadowNode::aPointWorldY;
MObject shaveShadowNode::aPointWorldZ;
MObject shaveShadowNode::colorR;
MObject shaveShadowNode::colorG;
MObject shaveShadowNode::colorB;
MObject shaveShadowNode::shadColorR;
MObject shaveShadowNode::shadColorG;
MObject shaveShadowNode::shadColorB;
MObject shaveShadowNode::lightID;
MObject shaveShadowNode::aOutIntensity;
MObject shaveShadowNode::aOutColorR;
MObject shaveShadowNode::aOutColorG;
MObject shaveShadowNode::aOutColorB;
MObject shaveShadowNode::aOutColor;
MObject shaveShadowNode::color;
MObject shaveShadowNode::shadColor;

//
// DESCRIPTION:
///////////////////////////////////////////////////////
shaveShadowNode::shaveShadowNode()
{
}

//
// DESCRIPTION:
///////////////////////////////////////////////////////
shaveShadowNode::~shaveShadowNode()
{
}

//
// DESCRIPTION:
///////////////////////////////////////////////////////
void* shaveShadowNode::creator()
{
    return new shaveShadowNode();
}



MStatus shaveShadowNode::initialize()
{
     MFnNumericAttribute nAttr; 



// User defined input value

    IntensityValue = nAttr.create( "Intensity", "int", MFnNumericData::kFloat);
    nAttr.setDefault(1.0f);
    nAttr.setMin(0.0f);
    nAttr.setMax(30.0f);
    nAttr.setKeyable(true);
    nAttr.setStorable(true);
    nAttr.setReadable(true);
    nAttr.setWritable(true);

	colorB = nAttr.create( "colorB", "cB", MFnNumericData::kFloat);
    nAttr.setDefault(1.0f);
    nAttr.setMin(0.0f);
    nAttr.setMax(1.0f);
    nAttr.setKeyable(true);
    nAttr.setStorable(true);
    nAttr.setReadable(true);
    nAttr.setWritable(true);

	colorG = nAttr.create( "colorG", "cG", MFnNumericData::kFloat);
    nAttr.setDefault(1.0f);
    nAttr.setMin(0.0f);
    nAttr.setMax(1.0f);
    nAttr.setKeyable(true);
    nAttr.setStorable(true);
    nAttr.setReadable(true);
    nAttr.setWritable(true);

	colorR = nAttr.create( "colorR", "cR", MFnNumericData::kFloat);
    nAttr.setDefault(1.0f);
    nAttr.setMin(0.0f);
    nAttr.setMax(1.0f);
    nAttr.setKeyable(true);
    nAttr.setStorable(true);
    nAttr.setReadable(true);
    nAttr.setWritable(true);


	shadColorB = nAttr.create( "shadowColorB", "scB", MFnNumericData::kFloat);
    nAttr.setDefault(1.0f);
    nAttr.setMin(0.0f);
    nAttr.setMax(1.0f);
    nAttr.setKeyable(true);
    nAttr.setStorable(true);
    nAttr.setReadable(true);
    nAttr.setWritable(true);

	shadColorG = nAttr.create( "shadowColorG", "scG", MFnNumericData::kFloat);
    nAttr.setDefault(1.0f);
    nAttr.setMin(0.0f);
    nAttr.setMax(1.0f);
    nAttr.setKeyable(true);
    nAttr.setStorable(true);
    nAttr.setReadable(true);
    nAttr.setWritable(true);

	shadColorR = nAttr.create( "shadowColorR", "scR", MFnNumericData::kFloat);
    nAttr.setDefault(1.0f);
    nAttr.setMin(0.0f);
    nAttr.setMax(1.0f);
    nAttr.setKeyable(true);
    nAttr.setStorable(true);
    nAttr.setReadable(true);
    nAttr.setWritable(true);

	aOutColorB = nAttr.create( "outColorB", "ocB", MFnNumericData::kFloat);
    nAttr.setKeyable(false);
    nAttr.setStorable(false);
    nAttr.setReadable(true);
    nAttr.setWritable(false);

	aOutColorG = nAttr.create( "outColorG", "ocG", MFnNumericData::kFloat);
    nAttr.setKeyable(false);
    nAttr.setStorable(false);
    nAttr.setReadable(true);
    nAttr.setWritable(false);

	aOutColorR = nAttr.create( "outColorR", "ocR", MFnNumericData::kFloat);
    nAttr.setKeyable(false);
    nAttr.setStorable(false);
    nAttr.setReadable(true);
    nAttr.setWritable(false);

	aOutColor = nAttr.create( "outColor", "oc", aOutColorR, aOutColorG, aOutColorB);
	nAttr.setKeyable(false);
    nAttr.setStorable(false);
    nAttr.setReadable(true);
    nAttr.setWritable(false);

	shadColor = nAttr.create( "shadColor", "sc", shadColorR, shadColorG, shadColorB);
	nAttr.setStorable(true);
	nAttr.setUsedAsColor(true);
	nAttr.setKeyable(true);

	color = nAttr.create( "color", "col", colorR, colorG, colorB);
	nAttr.setStorable(true);
	nAttr.setUsedAsColor(true);
	nAttr.setKeyable(true);

	
    lightID = nAttr.create( "LightID", "lid", MFnNumericData::kShort);
    nAttr.setDefault((short)0);
    nAttr.setStorable(true);
    nAttr.setReadable(true);
    nAttr.setWritable(true);

// Point on surface in camera space, will be used to compute view vector

    aPointWorldX = nAttr.create( "pointWorldX", "px", MFnNumericData::kFloat);
    nAttr.setStorable(false);
	nAttr.setHidden(true);

    aPointWorldY = nAttr.create( "pointWorldY", "py", MFnNumericData::kFloat);
    nAttr.setStorable(false);
	nAttr.setHidden(true);

    aPointWorldZ = nAttr.create( "pointWorldZ", "pz", MFnNumericData::kFloat);
    nAttr.setStorable(false);
	nAttr.setHidden(true);

    aPointWorld = nAttr.create( "pointWorld","p", aPointWorldX, 
                                   aPointWorldY, aPointWorldZ);
    nAttr.setStorable(false);
    nAttr.setHidden(true);
    nAttr.setReadable(true);
    nAttr.setWritable(true);

// Outputs
//
// Always set your output attributes to be read-only
// You should also mark any internal attributes that your node
// computes to be read-only also, this will prevent any connections.

    aOutIntensity = nAttr.create( "outIntensity", "oi", MFnNumericData::kFloat);
    nAttr.setStorable(false);
    nAttr.setHidden(false);
    nAttr.setReadable(true);
    nAttr.setWritable(false);

	addAttribute(aOutColor);
	addAttribute(shadColor);
	addAttribute(color);

    addAttribute(IntensityValue);
    addAttribute(aPointWorld);
    addAttribute(aOutIntensity);
	addAttribute(lightID);

    attributeAffects (IntensityValue, aOutColor);

    attributeAffects (aPointWorldX, aOutColorB);
    attributeAffects (aPointWorldY, aOutColorB);
    attributeAffects (aPointWorldZ, aOutColorB);
    attributeAffects (aPointWorld,  aOutColorB);
	attributeAffects (aPointWorldX, aOutColorR);
    attributeAffects (aPointWorldY, aOutColorR);
    attributeAffects (aPointWorldZ, aOutColorR);
    attributeAffects (aPointWorld,  aOutColorR);
	attributeAffects (aPointWorldX, aOutColorG);
    attributeAffects (aPointWorldY, aOutColorG);
    attributeAffects (aPointWorldZ, aOutColorG);
    attributeAffects (aPointWorld,  aOutColorG);
	attributeAffects (shadColorR, aOutColor);
	attributeAffects (colorR, aOutColor);


    return MS::kSuccess;
}

//
// DESCRIPTION:
///////////////////////////////////////////////////////
MStatus shaveShadowNode::compute(
const MPlug&      plug,
      MDataBlock& block ) 
{ 
    bool k = false;
	k |= (plug == aOutColor);
	k |= (plug == aOutColorR);
	k |= (plug == aOutColorG);
	k |= (plug == aOutColorB);
    if( !k ) return MS::kUnknownParameter;

	short lightIndex = block.inputValue ( lightID ).asShort();
	VERT wpoint;
	wpoint.x = block.inputValue( aPointWorldX ).asFloat();
	wpoint.y = block.inputValue( aPointWorldY ).asFloat();
	wpoint.z = block.inputValue( aPointWorldZ ).asFloat();
	float cR = block.inputValue( colorR ).asFloat();
	float cG = block.inputValue( colorG ).asFloat();
	float cB = block.inputValue( colorB ).asFloat();
	float sCR = block.inputValue( shadColorR ).asFloat();
	float sCG = block.inputValue( shadColorG ).asFloat();
	float sCB = block.inputValue( shadColorB ).asFloat();
	float shadow;

	//
	// We need to get the shadow density from the shaveGlobals node.
	// However, it would be too expensive to call
	// shaveGlobals::getGlobals() on every iteration of compute, so
	// shaveRender::frameStart() does it for us, so we can just use the
	// result global vars.
	//
	if (shaveShadowDensityGlob > 0.0)
	{
		float	illum = 1255.0f;
		int		id;
		int		minId = shaveUtil::globalLightList[lightIndex].minId;
		int		maxId = shaveUtil::globalLightList[lightIndex].maxId;
		float	temp=10000.0;

		for (id = minId; id <= maxId; id++)
		{
			temp = SHAVEilluminate_point(wpoint, id);
			if (temp < illum) illum = temp; // changed max to min
		}

		shadow = (1.0f - illum) * shaveShadowDensityGlob;
	}
	else
		shadow = shaveShadowDensityGlob;

	MDataHandle outColorRHandle = block.outputValue( aOutColorR );
	float& oCR = outColorRHandle.asFloat();
	MDataHandle outColorGHandle = block.outputValue( aOutColorG );
	float& oCG = outColorGHandle.asFloat();
	MDataHandle outColorBHandle = block.outputValue( aOutColorB );
	float& oCB = outColorBHandle.asFloat();
	oCR = cR + (sCR - cR) * shadow;
	oCG = cG + (sCG - cG) * shadow;
	oCB = cB + (sCB - cB) * shadow;
	outColorRHandle.setClean();
	outColorGHandle.setClean();
	outColorBHandle.setClean();
	return MS::kSuccess;
}