-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.fxh
460 lines (396 loc) · 11.2 KB
/
common.fxh
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
// Header file for dx11 HLSL
#define PI (3.14159265)
#define PHASE (2*PI)
bool IsTextureAssigned( Texture2D tex )
{
uint w, h;
tex.GetDimensions( w, h );
return w != 0;
}
bool IsTextureCubeAssigned( TextureCube tex )
{
uint w, h;
tex.GetDimensions( w, h );
return w != 0;
}
float3 SampleTexture( Texture2D tex, SamplerState state, float2 uv )
{
// dx11 style texture sampling
return tex.Sample( state, uv );
}
float3 SampleTexture(Texture2D tex, SamplerState state, float2 uv, float3 returnColor)
{
if( !IsTextureAssigned( tex ) )
return returnColor;
return tex.Sample( state, uv );
}
float4 SampleTexture( Texture2D tex, SamplerState state, float2 uv, float4 returnColor )
{
// If Texture is not assigned return input color instead of dx11 default black
if( !IsTextureAssigned( tex ) )
return returnColor;
return tex.Sample( state, uv );
}
float3 DecodeNormalTexture( float2 tex )
{
float2 xy = tex.xy * 2 - 1;
return float3( xy, sqrt ( max( 0, 1 - dot( xy, xy ) ) ) );
}
//------------------------------------
// Light parameters
//------------------------------------
// dx11 autmatically binds and accepts up to 3 lights in the Maya scene
cbuffer UpdateLights : register(b2)
{
// ---------------------------------------------
// Light 0 GROUP
// ---------------------------------------------
// This value is controlled by Maya to tell us if a light should be calculated
// For example the artist may disable a light in the scene, or choose to see only the selected light
// This flag allows Maya to tell our shader not to contribute this light into the lighting
bool light0Enable : LIGHTENABLE
<
string Object = "Light 0"; // UI Group for lights, auto-closed
string UIName = "Enable Light 0";
int UIOrder = 1020;
> = false; // maya manages lights itself and defaults to no lights
// follows LightParameterInfo::ELightType
// spot = 2, point = 3, directional = 4, ambient = 5,
int light0Type : LIGHTTYPE
<
string Object = "Light 0";
string UIName = "Light 0 Type";
string UIFieldNames ="None:Default:Spot:Point:Directional:Ambient";
int UIOrder = 1021;
float UIMin = 0;
float UIMax = 5;
float UIStep = 1;
> = 2; // default to spot so the cone angle etc work when "Use Shader Settings" option is used
float3 light0Pos : POSITION
<
string Object = "Light 0";
string UIName = "Light 0 Position";
string Space = "World";
int UIOrder = 1022;
> = {100.0f, 100.0f, 100.0f};
float3 light0Color : LIGHTCOLOR
<
string Object = "Light 0";
string UIName = "Light 0 Color";
string UIWidget = "Color";
int UIOrder = 1023;
> = { 1.0f, 1.0f, 1.0f};
float light0Intensity : LIGHTINTENSITY
<
string Object = "Light 0";
string UIName = "Light 0 Intensity";
float UIMin = 0.0;
float UIMax = 1000;
float UIStep = 0.01;
int UIOrder = 1024;
> = { 1.0f };
float3 light0Dir : DIRECTION
<
string Object = "Light 0";
string UIName = "Light 0 Direction";
string Space = "World";
int UIOrder = 1025;
> = {100.0f, 100.0f, 100.0f};
float light0ConeAngle : HOTSPOT // In radians
<
string Object = "Light 0";
string UIName = "Light 0 Cone Angle";
float UIMin = 0;
float UIMax = PI/2;
int UIOrder = 1026;
> = { 0.46f };
float light0FallOff : FALLOFF // In radians. Sould be HIGHER then cone angle or lighted area will invert
<
string Object = "Light 0";
string UIName = "Light 0 Penumbra Angle";
float UIMin = 0;
float UIMax = PI/2;
int UIOrder = 1027;
> = { 0.7f };
float light0AttenScale : DECAYRATE
<
string Object = "Light 0";
string UIName = "Light 0 Decay";
float UIMin = 0.0;
float UIMax = 1000;
float UIStep = 0.01;
int UIOrder = 1028;
> = {0.0};
bool light0ShadowOn : SHADOWFLAG
<
string Object = "Light 0";
string UIName = "Light 0 Casts Shadow";
string UIWidget = "None";
int UIOrder = 1029;
> = true;
float4x4 light0Matrix : SHADOWMAPMATRIX
<
string Object = "Light 0";
string UIWidget = "None";
int UIOrder = 1129;
>;
// ---------------------------------------------
// Light 1 GROUP
// ---------------------------------------------
bool light1Enable : LIGHTENABLE
<
string Object = "Light 1";
string UIName = "Enable Light 1";
int UIOrder = 1030;
> = false;
int light1Type : LIGHTTYPE
<
string Object = "Light 1";
string UIName = "Light 1 Type";
string UIFieldNames ="None:Default:Spot:Point:Directional:Ambient";
float UIMin = 0;
float UIMax = 5;
int UIOrder = 1031;
> = 2;
float3 light1Pos : POSITION
<
string Object = "Light 1";
string UIName = "Light 1 Position";
string Space = "World";
int UIOrder = 1032;
> = {-100.0f, 100.0f, 100.0f};
float3 light1Color : LIGHTCOLOR
<
string Object = "Light 1";
string UIName = "Light 1 Color";
string UIWidget = "Color";
int UIOrder = 1033;
> = { 1.0f, 1.0f, 1.0f};
float light1Intensity : LIGHTINTENSITY
<
string Object = "Light 1";
string UIName = "Light 1 Intensity";
float UIMin = 0.0;
float UIMax = 1000;
float UIStep = 0.01;
int UIOrder = 1034;
> = { 1.0f };
float3 light1Dir : DIRECTION
<
string Object = "Light 1";
string UIName = "Light 1 Direction";
string Space = "World";
int UIOrder = 1035;
> = {100.0f, 100.0f, 100.0f};
float light1ConeAngle : HOTSPOT // In radians
<
string Object = "Light 1";
string UIName = "Light 1 Cone Angle";
float UIMin = 0;
float UIMax = PI/2;
int UIOrder = 1036;
> = { 45.0f };
float light1FallOff : FALLOFF // In radians. Sould be HIGHER then cone angle or lighted area will invert
<
string Object = "Light 1";
string UIName = "Light 1 Penumbra Angle";
float UIMin = 0;
float UIMax = PI/2;
int UIOrder = 1037;
> = { 0.0f };
float light1AttenScale : DECAYRATE
<
string Object = "Light 1";
string UIName = "Light 1 Decay";
float UIMin = 0.0;
float UIMax = 1000;
float UIStep = 0.01;
int UIOrder = 1038;
> = {0.0};
bool light1ShadowOn : SHADOWFLAG
<
string Object = "Light 1";
string UIName = "Light 1 Casts Shadow";
string UIWidget = "None";
int UIOrder = 1039;
> = true;
float4x4 light1Matrix : SHADOWMAPMATRIX
<
string Object = "Light 1";
string UIWidget = "None";
int UIOrder = 1139;
>;
// ---------------------------------------------
// Light 2 GROUP
// ---------------------------------------------
bool light2Enable : LIGHTENABLE
<
string Object = "Light 2";
string UIName = "Enable Light 2";
int UIOrder = 1040;
> = false;
int light2Type : LIGHTTYPE
<
string Object = "Light 2";
string UIName = "Light 2 Type";
string UIFieldNames ="None:Default:Spot:Point:Directional:Ambient";
float UIMin = 0;
float UIMax = 5;
int UIOrder = 1041;
> = 2;
float3 light2Pos : POSITION
<
string Object = "Light 2";
string UIName = "Light 2 Position";
string Space = "World";
int UIOrder = 1042;
> = {100.0f, 100.0f, -100.0f};
float3 light2Color : LIGHTCOLOR
<
string Object = "Light 2";
string UIName = "Light 2 Color";
string UIWidget = "Color";
int UIOrder = 1043;
> = { 1.0f, 1.0f, 1.0f};
float light2Intensity : LIGHTINTENSITY
<
string Object = "Light 2";
string UIName = "Light 2 Intensity";
float UIMin = 0.0;
float UIMax = 1000;
float UIStep = 0.01;
int UIOrder = 1044;
> = { 1.0f };
float3 light2Dir : DIRECTION
<
string Object = "Light 2";
string UIName = "Light 2 Direction";
string Space = "World";
int UIOrder = 1045;
> = {100.0f, 100.0f, 100.0f};
float light2ConeAngle : HOTSPOT // In radians
<
string Object = "Light 2";
string UIName = "Light 2 Cone Angle";
float UIMin = 0;
float UIMax = PI/2;
int UIOrder = 1046;
> = { 45.0f };
float light2FallOff : FALLOFF // In radians. Sould be HIGHER then cone angle or lighted area will invert
<
string Object = "Light 2";
string UIName = "Light 2 Penumbra Angle";
float UIMin = 0;
float UIMax = PI/2;
int UIOrder = 1047;
> = { 0.0f };
float light2AttenScale : DECAYRATE
<
string Object = "Light 2";
string UIName = "Light 2 Decay";
float UIMin = 0.0;
float UIMax = 1000;
float UIStep = 0.01;
int UIOrder = 1048;
> = {0.0};
bool light2ShadowOn : SHADOWFLAG
<
string Object = "Light 2";
string UIName = "Light 2 Casts Shadow";
string UIWidget = "None";
int UIOrder = 1049;
> = true;
float4x4 light2Matrix : SHADOWMAPMATRIX
<
string Object = "Light 2";
string UIWidget = "None";
int UIOrder = 1149;
>;
} //end lights cbuffer
// Spot light cone
float LightConeangle(float coneAngle, float coneFalloff, float3 lightVec, float3 lightDir)
{
// the cone falloff should be equal or bigger then the coneAngle or the light inverts
// this is added to make manually tweaking the spot settings easier.
if (coneFalloff < coneAngle)
coneFalloff = coneAngle;
float LdotDir = dot(normalize(lightVec), lightDir);
// cheaper cone, no fall-off control would be:
// float cone = pow(saturate(LdotDir), 1 / coneAngle);
// higher quality cone (more expensive):
float cone = smoothstep( cos(coneFalloff), cos(coneAngle), LdotDir);
return cone;
}
// Calculate a light:
struct LightData
{
float3 dir;
float3 color;
float3 lightVec;
};
LightData CalculateLight ( int lightType, float lightAtten, float3 lightPos, float3 vertWorldPos,
float3 lightColor, float lightIntensity, float3 lightDir, float lightConeAngle, float lightFallOff )
{
// For Maya, flip the lightDir:
lightDir = -lightDir;
// directional light has no position, so we use lightDir instead
bool isDirectionalLight = (lightType == 4);
float3 lightVec = lerp(lightPos - vertWorldPos, lightDir, isDirectionalLight);
float3 L = normalize(lightVec);
// Light Attenuation:
float attenuation = 1.0f;
if (!isDirectionalLight) // directional lights do not support attenuation, skip calculation
{
bool enableAttenuation = lightAtten > 0.0001f;
attenuation = lerp(1.0, 1 / pow(length(lightVec), lightAtten), enableAttenuation);
}
// Spot light Cone Angle:
if (lightType == 2)
{
float angle = LightConeangle(lightConeAngle, lightFallOff, lightVec, lightDir);
attenuation *= angle;
}
LightData ld;
ld.dir = L;
ld.color = lightColor * lightIntensity * attenuation;
ld.lightVec = lightVec;
return ld;
}
float3 ToLinear(float3 c)
{
return pow(c,float3(2.2,2.2,2.2));
}
float3 gerstnerWave(float3 position, float multiplier, float2 direction, float amplitude, float waveLength, float crestFactor, float speed, float globalTimer, float outType)
{
// Returns either position, binormal, tangent or normal
float amp = amplitude*0.025;
amp *= multiplier;
float WL = waveLength;
WL *= multiplier;
float2 D = direction;
float w = 2*PI/WL;
float Q = crestFactor;
float3 P0 = position.xyz;
float myPhase = speed * 2*PI/WL;
float dotD = dot(D, P0.xz);
float C = cos(w*dotD + (globalTimer*myPhase));
float S = sin(w*dotD + (globalTimer*myPhase));
float dotX = dot(D, P0.x);
float dotY = dot(D, P0.y);
float dotP = dot(D, P0);
float powDotX = dot(D, pow(P0.x, 2));
float powDotY = dot(D, pow(P0.y, 2));
float WA = w*amp;
if(outType == 0)
float3 P = float3(Q*amp*D.x*C, amp * S, Q*amp*D.y*C);
return P;
if(outType == 1)
float3 B = float3(Q*powDotX*WA*S, dotX*WA*C, Q*dotX*dotY*WA*S);
return B;
if(outType == 2)
float3 T = float3(Q*dotX*dotY*WA*S, dotY*WA*C, Q*powDotY*WA*S);
return T;
if(outType == 3)
float3 N = float3(dotX*WA*C, Q*WA*S, dotY*WA*C);
return N;
}