-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathltc.frag
443 lines (361 loc) · 12.4 KB
/
ltc.frag
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
// Real-Time Polygonal-Light Shading with Linearly Transformed Cosines
// by Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt
// code: https://github.com/selfshadow/ltc_code/
mat3 transposeMat3( const in mat3 m ) {
mat3 tmp;
tmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );
tmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );
tmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );
return tmp;
}
vec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {
const float LUT_SIZE = 64.0;
const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;
const float LUT_BIAS = 0.5 / LUT_SIZE;
float dotNV = saturate( dot( N, V ) );
// texture parameterized by sqrt( GGX alpha ) and sqrt( 1 - cos( theta ) )
vec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );
uv = uv * LUT_SCALE + LUT_BIAS;
return uv;
}
float LTC_ClippedSphereFormFactor( const in vec3 f ) {
// Real-Time Area Lighting: a Journey from Research to Production (p.102)
// An approximation of the form factor of a horizon-clipped rectangle.
float l = length( f );
return max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );
}
vec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {
float x = dot( v1, v2 );
float y = abs( x );
// rational polynomial approximation to theta / sin( theta ) / 2PI
float a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;
float b = 3.4175940 + ( 4.1616724 + y ) * y;
float v = a / b;
float theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;
return cross( v1, v2 ) * theta_sintheta;
}
struct Coords {
vec3 coord0;
vec3 coord1;
vec3 coord2;
vec3 coord3;
};
float LTC_EvaluateRect( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in Coords rectCoords) {
// bail if point is on back side of plane of light
// assumes ccw winding order of light vertices
vec3 v1 = rectCoords.coord1 - rectCoords.coord0;
vec3 v2 = rectCoords.coord3 - rectCoords.coord0;
vec3 lightNormal = cross( v1, v2 );
// if( dot( lightNormal, P - rectCoords.coord0 ) < 0.0 ) return 0.0;
float factor = sign(-dot( lightNormal, P - rectCoords.coord0 ));
// construct orthonormal basis around N
vec3 T1, T2;
T1 = normalize( V - N * dot( V, N ) );
T2 = factor * cross( N, T1 ); // negated from paper; possibly due to a different handedness of world coordinate system
// compute transform
mat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );
// transform rect
vec3 coords[ 4 ];
coords[ 0 ] = mat * ( rectCoords.coord0 - P );
coords[ 1 ] = mat * ( rectCoords.coord1 - P );
coords[ 2 ] = mat * ( rectCoords.coord2 - P );
coords[ 3 ] = mat * ( rectCoords.coord3 - P );
// project rect onto sphere
coords[ 0 ] = normalize( coords[ 0 ] );
coords[ 1 ] = normalize( coords[ 1 ] );
coords[ 2 ] = normalize( coords[ 2 ] );
coords[ 3 ] = normalize( coords[ 3 ] );
// calculate vector form factor
vec3 vectorFormFactor = vec3( 0.0 );
vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );
vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );
vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );
vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );
// adjust for horizon clipping
float result = LTC_ClippedSphereFormFactor( vectorFormFactor );
return result;
}
Coords dLTCCoords;
Coords getLTCLightCoords(vec3 lightPos, vec3 halfWidth, vec3 halfHeight){
Coords coords;
coords.coord0 = lightPos + halfWidth - halfHeight;
coords.coord1 = lightPos - halfWidth - halfHeight;
coords.coord2 = lightPos - halfWidth + halfHeight;
coords.coord3 = lightPos + halfWidth + halfHeight;
return coords;
}
//used for simple sphere light falloff
float dSphereRadius;
Coords getSphereLightCoords(vec3 lightPos, vec3 halfWidth, vec3 halfHeight){
Coords coords;
float radius = max(length(halfWidth), length(halfWidth));
dSphereRadius = radius;
vec3 f = normalize(lightPos-view_position);
vec3 w = normalize(cross(f, halfHeight));
vec3 h = normalize(cross(f, w));
coords.coord0 = lightPos + w * radius - h * radius;
coords.coord1 = lightPos - w * radius - h * radius;
coords.coord2 = lightPos - w * radius + h * radius;
coords.coord3 = lightPos + w * radius + h * radius;
return coords;
}
// used for LTC LUT texture lookup
vec2 dLTCUV;
#ifdef CLEARCOAT
vec2 ccLTCUV;
#endif
vec2 getLTCLightUV(float tGlossiness, vec3 tNormalW)
{
float roughness = max((1.0 - tGlossiness) * (1.0 - tGlossiness), 0.001);
return LTC_Uv( tNormalW, dViewDirW, roughness );
}
vec3 getLTCLightSpecFres(vec2 uv, vec3 tSpecularity)
{
vec4 t2 = texture2D( areaLightsLutTex2, uv );
#ifdef AREA_R8_G8_B8_A8_LUTS
t2 *= vec4(0.693103,1,1,1);
t2 += vec4(0.306897,0,0,0);
#endif
return tSpecularity * t2.x + ( vec3( 1.0 ) - tSpecularity) * t2.y;
}
void calcLTCLightValues()
{
dLTCUV = getLTCLightUV(dGlossiness, dNormalW);
dLightFresnel = getLTCLightSpecFres(dLTCUV, dSpecularity);
#ifdef CLEARCOAT
ccLTCUV = getLTCLightUV(ccGlossiness, ccNormalW);
ccLightFresnel = getLTCLightSpecFres(ccLTCUV, vec3(ccSpecularity));
#endif
}
void calcRectLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight)
{
dLTCCoords = getLTCLightCoords(lightPos, halfWidth, halfHeight);
calcLTCLightValues();
}
void calcDiskLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight)
{
calcRectLightValues(lightPos, halfWidth, halfHeight);
}
void calcSphereLightValues(vec3 lightPos, vec3 halfWidth, vec3 halfHeight)
{
dLTCCoords = getSphereLightCoords(lightPos, halfWidth, halfHeight);
calcLTCLightValues();
}
// An extended version of the implementation from
// "How to solve a cubic equation, revisited"
// http://momentsingraphics.de/?p=105
vec3 SolveCubic(vec4 Coefficient)
{
float pi = 3.14159;
// Normalize the polynomial
Coefficient.xyz /= Coefficient.w;
// Divide middle coefficients by three
Coefficient.yz /= 3.0;
float A = Coefficient.w;
float B = Coefficient.z;
float C = Coefficient.y;
float D = Coefficient.x;
// Compute the Hessian and the discriminant
vec3 Delta = vec3(
-Coefficient.z * Coefficient.z + Coefficient.y,
-Coefficient.y * Coefficient.z + Coefficient.x,
dot(vec2(Coefficient.z, -Coefficient.y), Coefficient.xy)
);
float Discriminant = dot(vec2(4.0 * Delta.x, -Delta.y), Delta.zy);
vec3 RootsA, RootsD;
vec2 xlc, xsc;
// Algorithm A
{
float A_a = 1.0;
float C_a = Delta.x;
float D_a = -2.0 * B * Delta.x + Delta.y;
// Take the cubic root of a normalized complex number
float Theta = atan(sqrt(Discriminant), -D_a) / 3.0;
float x_1a = 2.0 * sqrt(-C_a) * cos(Theta);
float x_3a = 2.0 * sqrt(-C_a) * cos(Theta + (2.0 / 3.0) * pi);
float xl;
if ((x_1a + x_3a) > 2.0 * B)
xl = x_1a;
else
xl = x_3a;
xlc = vec2(xl - B, A);
}
// Algorithm D
{
float A_d = D;
float C_d = Delta.z;
float D_d = -D * Delta.y + 2.0 * C * Delta.z;
// Take the cubic root of a normalized complex number
float Theta = atan(D * sqrt(Discriminant), -D_d) / 3.0;
float x_1d = 2.0 * sqrt(-C_d) * cos(Theta);
float x_3d = 2.0 * sqrt(-C_d) * cos(Theta + (2.0 / 3.0) * pi);
float xs;
if (x_1d + x_3d < 2.0 * C)
xs = x_1d;
else
xs = x_3d;
xsc = vec2(-D, xs + C);
}
float E = xlc.y * xsc.y;
float F = -xlc.x * xsc.y - xlc.y * xsc.x;
float G = xlc.x * xsc.x;
vec2 xmc = vec2(C * F - B * G, -B * F + C * E);
vec3 Root = vec3(xsc.x / xsc.y, xmc.x / xmc.y, xlc.x / xlc.y);
if (Root.x < Root.y && Root.x < Root.z)
Root.xyz = Root.yxz;
else if (Root.z < Root.x && Root.z < Root.y)
Root.xyz = Root.xzy;
return Root;
}
float LTC_EvaluateDisk(vec3 N, vec3 V, vec3 P, mat3 Minv, Coords points)
{
// construct orthonormal basis around N
vec3 T1, T2;
T1 = normalize(V - N * dot(V, N));
T2 = cross(N, T1);
// rotate area light in (T1, T2, N) basis
//mat3 R = transpose(mat3(T1, T2, N));
mat3 R = transposeMat3( mat3( T1, T2, N ) );
// polygon (allocate 5 vertices for clipping)
vec3 L_[ 3 ];
L_[ 0 ] = R * ( points.coord0 - P );
L_[ 1 ] = R * ( points.coord1 - P );
L_[ 2 ] = R * ( points.coord2 - P );
vec3 Lo_i = vec3(0);
// init ellipse
vec3 C = 0.5 * (L_[0] + L_[2]);
vec3 V1 = 0.5 * (L_[1] - L_[2]);
vec3 V2 = 0.5 * (L_[1] - L_[0]);
C = Minv * C;
V1 = Minv * V1;
V2 = Minv * V2;
//if(dot(cross(V1, V2), C) > 0.0)
// return 0.0;
// compute eigenvectors of ellipse
float a, b;
float d11 = dot(V1, V1);
float d22 = dot(V2, V2);
float d12 = dot(V1, V2);
if (abs(d12) / sqrt(d11 * d22) > 0.0001)
{
float tr = d11 + d22;
float det = -d12 * d12 + d11 * d22;
// use sqrt matrix to solve for eigenvalues
det = sqrt(det);
float u = 0.5 * sqrt(tr - 2.0 * det);
float v = 0.5 * sqrt(tr + 2.0 * det);
float e_max = (u + v) * (u + v);
float e_min = (u - v) * (u - v);
vec3 V1_, V2_;
if (d11 > d22)
{
V1_ = d12 * V1 + (e_max - d11) * V2;
V2_ = d12 * V1 + (e_min - d11) * V2;
}
else
{
V1_ = d12*V2 + (e_max - d22)*V1;
V2_ = d12*V2 + (e_min - d22)*V1;
}
a = 1.0 / e_max;
b = 1.0 / e_min;
V1 = normalize(V1_);
V2 = normalize(V2_);
}
else
{
a = 1.0 / dot(V1, V1);
b = 1.0 / dot(V2, V2);
V1 *= sqrt(a);
V2 *= sqrt(b);
}
vec3 V3 = cross(V1, V2);
if (dot(C, V3) < 0.0)
V3 *= -1.0;
float L = dot(V3, C);
float x0 = dot(V1, C) / L;
float y0 = dot(V2, C) / L;
float E1 = inversesqrt(a);
float E2 = inversesqrt(b);
a *= L * L;
b *= L * L;
float c0 = a * b;
float c1 = a * b * (1.0 + x0 * x0 + y0 * y0) - a - b;
float c2 = 1.0 - a * (1.0 + x0 * x0) - b * (1.0 + y0 * y0);
float c3 = 1.0;
vec3 roots = SolveCubic(vec4(c0, c1, c2, c3));
float e1 = roots.x;
float e2 = roots.y;
float e3 = roots.z;
vec3 avgDir = vec3(a * x0 / (a - e2), b * y0 / (b - e2), 1.0);
mat3 rotate = mat3(V1, V2, V3);
avgDir = rotate * avgDir;
avgDir = normalize(avgDir);
float L1 = sqrt(-e2 / e3);
float L2 = sqrt(-e2 / e1);
float formFactor = L1 * L2 * inversesqrt((1.0 + L1 * L1) * (1.0 + L2 * L2));
const float LUT_SIZE = 64.0;
const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;
const float LUT_BIAS = 0.5 / LUT_SIZE;
// use tabulated horizon-clipped sphere
vec2 uv = vec2(avgDir.z * 0.5 + 0.5, formFactor);
uv = uv*LUT_SCALE + LUT_BIAS;
float scale = texture2D( areaLightsLutTex2, uv ).w;
return formFactor*scale;
}
float getRectLightDiffuse() {
return LTC_EvaluateRect( dNormalW, dViewDirW, vPositionW, mat3( 1.0 ), dLTCCoords );
}
float getDiskLightDiffuse() {
return LTC_EvaluateDisk( dNormalW, dViewDirW, vPositionW, mat3( 1.0 ), dLTCCoords );
}
float getSphereLightDiffuse() {
// NB: this could be improved further with distance based wrap lighting
float falloff = dSphereRadius / (dot(dLightDirW, dLightDirW) + dSphereRadius);
return getLightDiffuse()*falloff;
}
mat3 getLTCLightInvMat(vec2 uv)
{
vec4 t1 = texture2D( areaLightsLutTex1, uv );
#ifdef AREA_R8_G8_B8_A8_LUTS
t1 *= vec4(1.001, 0.3239, 0.60437568, 1.0);
t1 += vec4(0.0, -0.2976, -0.01381, 0.0);
#endif
return mat3(
vec3( t1.x, 0, t1.y ),
vec3( 0, 1, 0 ),
vec3( t1.z, 0, t1.w )
);
}
float calcRectLightSpecular(vec3 tNormalW, vec2 uv) {
mat3 mInv = getLTCLightInvMat(uv);
return LTC_EvaluateRect( tNormalW, dViewDirW, vPositionW, mInv, dLTCCoords );
}
vec3 getRectLightSpecular() {
return dLightFresnel * calcRectLightSpecular(dNormalW, dLTCUV);
}
#ifdef CLEARCOAT
vec3 getRectLightSpecularCC() {
return ccLightFresnel * calcRectLightSpecular(ccNormalW, ccLTCUV);
}
#endif
float calcDiskLightSpecular(vec3 tNormalW, vec2 uv) {
mat3 mInv = getLTCLightInvMat(uv);
return LTC_EvaluateDisk( tNormalW, dViewDirW, vPositionW, mInv, dLTCCoords );
}
vec3 getDiskLightSpecular() {
return dLightFresnel * calcDiskLightSpecular(dNormalW, dLTCUV);
}
#ifdef CLEARCOAT
vec3 getDiskLightSpecularCC() {
return ccLightFresnel * calcDiskLightSpecular(ccNormalW, ccLTCUV);
}
#endif
vec3 getSphereLightSpecular() {
return dLightFresnel * calcDiskLightSpecular(dNormalW, dLTCUV);
}
#ifdef CLEARCOAT
vec3 getSphereLightSpecularCC() {
return ccLightFresnel * calcDiskLightSpecular(ccNormalW, ccLTCUV);
}
#endif