-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmicrosurface_transformations.h
More file actions
517 lines (431 loc) · 14.9 KB
/
microsurface_transformations.h
File metadata and controls
517 lines (431 loc) · 14.9 KB
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#pragma once
// A numerical validation for the paper "Microsurface Transformations"
// by Asen Atanasov, Vladimir Koylazov, Rossen Dimov and Alexander Wilkie, EGSR'22
// The basic technique to transform microsurfaces is implemented in the base class Microsurface.
// A list of implementations for specific distributions are provided afterwards:
// 1. GTR - "Physically Based Shading at Disney" by Burley (2012) and "Deriving the Smith shadowing function G_1 for \gamma \in (0,4]" by Dimov (2015)
// 2. GGX - Anisotropic GGX, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs" by Heitz (2014)
// 3. Beckmann - Anisotropic Beckmann, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs" by Heitz (2014)
// 4. Phong - "Microfacet Models for Refraction through Rough Surfaces" by Walter et al. (2007)
// 5. Sheen - "Production Friendly Microfacet Sheen BRDF", Estevez and Kulla (2017)
// 6. STD - "STD: Student's t-Distribution of Slopes for Microfacet Based BSDFs" by Ribardiere et al. (2017)
// 7. Discrete - discrete GGX with classical Smith shadowing, "Discrete Stochastic Microfacet Models", Jakob et al. (2014)
// Author: Asen Atanasov
// The code for GTR D and G1 is copied from V-Ray SDK
#include "math_utils.h"
class Microsurface {
Matrix m, im;
float det;
virtual float getD(Vector h) const = 0;
virtual float getG1(Vector dir) const = 0;
public:
virtual const char* getName() const = 0;
// Inilialize a matrix of the form given in Equation (9)
void initTransform(Matrix m) {
this->m=m;
this->im=m;
im.makeInverse();
det=fabs(mixed(m[0], m[1], m[2]));
}
// The transformed distribution, Equation (22)
float getMicrofacetDistribution(Vector h) const {
Vector hTransformed=h*m; // multiply with the transpose of m
const float normalization=1.0f/hTransformed.lengthSqr();
hTransformed*=sqrtf(normalization);
return det*sqr(normalization)*getD(hTransformed);
}
// The transformed shadowing function, Equation (12)
float getSmithG1(Vector dir) const {
const Vector dirTransformed=normalize(im*dir);
return getG1(dirTransformed);
}
// Normalization constraint, Equation (3)
float integrateDistribution(int numSamples) const {
double sum=0.0;
for(int i=0; i<numSamples; i++) {
const float u = float(i)/float(numSamples);
const float v = radicalInverse2(i);
const Vector h=getSphereDir(u, v);
const float hz=h.z;
sum += double(getMicrofacetDistribution(h)*hz);
}
const float res = 2.0f*PI*float(sum)/float(numSamples);
return res;
}
// Shadowing function constraint, Equation (2)
float shadowingConstraint(const Vector &outgoing, int numSamples) const {
double sum=0.0f;
for(int i=0; i<numSamples; i++) {
const float u = float(i)/float(numSamples);
const float v = radicalInverse2(i);
const Vector h=getSphereDir(u, v);
sum += double(std::max(0.0f, dotf(h, outgoing)*getMicrofacetDistribution(h)*getSmithG1(outgoing)));
}
const float res = 2.0f*PI*float(sum)/float(numSamples);
return res/outgoing.z;
}
};
template <int KNOTS>
static float evalEquidistantNaturalSpline(float knots[KNOTS], float t) {
float f[KNOTS-2];
float m[KNOTS];
m[0] = 0.0f;
m[KNOTS-1] = 0.0f;
// Initialize moments m and result vector f.
for(int i=0; i<KNOTS-2; i++) {
m[i+1] = 4.0f;
f[i] = knots[i+2]-2.0f*knots[i+1]+knots[i];
}
// Solve tridiagonal system. The solution is in m.
for(int i=0; i<KNOTS-3; i++) {
const float k = 1.0f/m[i+1];
m[i+2] -= k;
f[i+1] -= k*f[i];
}
for(int i=KNOTS-3; i>=0; i--) {
m[i+1] = (f[i]-m[i+2])/m[i+1];
}
const int i=clamp(int(floorf(t)), 0, KNOTS-2);
// Calculate polynomial coefficients.
const float a = m[i+1]-m[i];
const float b = 3.0f*m[i];
const float c = knots[i+1]-knots[i]-2.0f*m[i]-m[i+1];
const float d = knots[i];
const float x=t-float(i);
const float res=((a*x+b)*x+c)*x+d;
return res;
}
class GTR: public Microsurface {
float alpha;
float gamma;
float getD(Vector h) const {
float res=0.0f;
const float cosTheta=h.z;
if(cosTheta<=1e-3f)
return res;
const float cosTheta2=sqr(cosTheta);
const float tanTheta2=(1.0f/cosTheta2)-1.0f;
const float roughness2=sqr(alpha);
if(fabsf(gamma-1.0f)<1e-6f) {
const float div=PI*logf(roughness2)*cosTheta2*(roughness2+tanTheta2);
res=(div<(roughness2-1.0f)*1e-6f)? (roughness2-1.0f)/div : 0.0f;
}
else {
const float divisor=PI*(1.0f-powf(roughness2, 1.0f-gamma))*powf(cosTheta2*(roughness2+tanTheta2), gamma);
const float dividend=(gamma-1.0f)*(roughness2-1.0f);
res=(fabsf(divisor)>fabsf(dividend)*1e-6f)? dividend/divisor : 0.0f;
}
return res;
}
float getG1Gamma1(float alpha, float cotTheta) const {
const float cotTheta2=sqr(cotTheta);
const float alpha2=sqr(alpha);
const float a=sqrtf(cotTheta2+alpha2);
const float b=sqrtf(cotTheta2+1.0f);
return cotTheta*logf(alpha2)/(a-b+cotTheta*logf(alpha2*(cotTheta+b)/(cotTheta+a)));
}
float getG1Gamma2(float alpha, float cotTheta) const {
return 2.f/(1.f+sqrtf(1.f+sqr(alpha/cotTheta)));
}
float getG1Gamma3(float alpha, float cotTheta) const {
const float cotTheta2=sqr(cotTheta);
const float alpha2=sqr(alpha);
const float a=sqrtf(cotTheta2+alpha2);
const float b=alpha2+1.0f;
return 4.0f*cotTheta*a*b/(2.0f*cotTheta*b*(cotTheta+a)+alpha2*(3.0f*alpha2+1.0f));
}
float getG1Gamma4(float alpha, float cotTheta) const {
const float cotTheta2=sqr(cotTheta);
const float alpha2=sqr(alpha);
const float alpha4=sqr(alpha2);
const float a=8.f*(alpha4+alpha2+1.f);
const float b=sqrtf(cotTheta2+alpha2);
const float b3=b*(cotTheta2+alpha2);
return 2.f*cotTheta*a*b3/(a*cotTheta*(b3+cotTheta*cotTheta2)+3.f*alpha2*(4.f*cotTheta2*(2.f*alpha4+alpha2+1.f)+alpha2*(5.f*alpha4+2.f*alpha2+1.f)));
}
float getG1(Vector dir) const {
const float cosTheta = dir.z;
if(cosTheta<=1e-3f) return 0.0f;
if(cosTheta>=1.0f-1e-6f) return 1.0f;
const float cotTheta = cosTheta/sqrtf(1.f-sqr(cosTheta));
float res=0.0f;
// when gamma is any of the integer values 0, 1, 2, 3, 4 apply analytical solution
if(gamma<=0.01f)
res=getG1Gamma2(1.0f, cotTheta);
else if(fabsf(gamma-1.0f)<=1e-2f)
res=getG1Gamma1(alpha, cotTheta);
else if(fabsf(gamma-2.0f)<=1e-2f)
res=getG1Gamma2(alpha, cotTheta);
else if(fabsf(gamma-3.0f)<=1e-2f)
res=getG1Gamma3(alpha, cotTheta);
else if(gamma>=4.0f-1e-2f)
res=getG1Gamma4(alpha, cotTheta);
else {
float knots[5];
knots[0]=getG1Gamma2(1.0f, cotTheta);
knots[1]=getG1Gamma1(alpha, cotTheta);
knots[2]=getG1Gamma2(alpha, cotTheta);
knots[3]=getG1Gamma3(alpha, cotTheta);
knots[4]=getG1Gamma4(alpha, cotTheta);
res=evalEquidistantNaturalSpline<5>(knots, gamma);
}
return clamp(res, 0.0f, 1.0f);
}
public:
GTR(float alpha=0.1f, float gamma=2.0f, Matrix m=Matrix(1.0f)): alpha(alpha), gamma(gamma) {
initTransform(m);
}
const char* getName() const {
return "GTR ";
}
};
inline float getAlpha(Vector dir, float alphaX, float alphaY) {
const float cosTheta2 = sqr(dir.z);
const float invSinTheta2 = 1.0f/(1.0f-cosTheta2);
if(alphaX==alphaY || invSinTheta2<=0) {
return alphaX;
}
const float cosPhi2 = sqr(dir.x)*invSinTheta2;
const float sinPhi2 = sqr(dir.y)*invSinTheta2;
return sqrt(cosPhi2*sqr(alphaX) + sinPhi2*sqr(alphaY));
}
class GGX: public Microsurface {
float alphaX;
float alphaY;
float getD(Vector h) const {
if(h.z<=1e-3f) return 0.0f;
const float denominator=PI*alphaX*alphaY*sqr(sqr(h.x/alphaX)+sqr(h.y/alphaY)+sqr(h.z));
return 1.0f/denominator;
}
float getG1(Vector dir) const {
const float cosTheta = dir.z;
if(cosTheta<=1e-3f) return 0.0f;
if(cosTheta>=1.0f-1e-6f) return 1.0f;
const float alpha=getAlpha(dir, alphaX, alphaY);
const float tanTheta2=(1.0f/sqr(dir.z))-1.0f;
const float denominator=1.0f+sqrtf(1.0f+tanTheta2*sqr(alpha));
return 2.0f/denominator;
}
public:
GGX(float alphaX, float alphaY, Matrix m=Matrix(1.0f)): alphaX(alphaX), alphaY(alphaY) {
initTransform(m);
}
const char* getName() const {
return "GGX ";
}
};
class Beckmann: public Microsurface {
float alphaX;
float alphaY;
float getD(Vector h) const {
const float cosTheta=h.z;
if(cosTheta<=1e-3f) return 0.0f;
const float cosTheta2=sqr(cosTheta);
const float sinTheta2=1.0f-cosTheta2;
float numerator=1.0f;
if(sinTheta2>1e-6f) {
const float tanTheta2=sinTheta2/cosTheta2;
const float cosPhi2=sqr(h.x)/sinTheta2;
const float sinPhi2=sqr(h.y)/sinTheta2;
numerator=expf(-tanTheta2*(cosPhi2/sqr(alphaX)+sinPhi2/sqr(alphaY)));
}
const float denominator=PI*alphaX*alphaY*sqr(cosTheta2);
return numerator/denominator;
}
float getG1(Vector dir) const {
const float cosTheta = dir.z;
if(cosTheta<=1e-3f) return 0.0f;
if(cosTheta>=1.0f-1e-6f) return 1.0f;
const float alpha=getAlpha(dir, alphaX, alphaY);
const float tanTheta=sqrtf((1.0f/sqr(cosTheta))-1.0f);
const float a=1.0f/(alpha*tanTheta);
if(a<1.6f) {
return (3.535f*a+2.181f*sqr(a))/(1.0f+2.276f*a+2.577f*sqr(a));
}
return 1.0f;
}
public:
Beckmann(float alphaX, float alphaY, Matrix m=Matrix(1.0f)): alphaX(alphaX), alphaY(alphaY) {
initTransform(m);
}
const char* getName() const {
return "Beckmann";
}
};
class Phong: public Microsurface {
float alpha;
float getD(Vector h) const {
const float cosTheta=h.z;
if(cosTheta<=1e-3f) return 0.0f;
return (alpha+2.0f)*powf(cosTheta, alpha)/(2.0f*PI);
}
float getG1(Vector dir) const {
const float cosTheta = dir.z;
if(cosTheta<=1e-3f) return 0.0f;
if(cosTheta>=1.0f-1e-6f) return 1.0f;
const float tanTheta=sqrtf((1.0f/sqr(cosTheta))-1.0f);
const float a=sqrtf(0.5f*alpha+1.0f)/tanTheta;
if(a<1.6f) {
return (3.535f*a+2.181f*sqr(a))/(1.0f+2.276f*a+2.577f*sqr(a));
}
return 1.0f;
}
public:
Phong(float alpha, Matrix m=Matrix(1.0f)): alpha(alpha) {
initTransform(m);
}
const char* getName() const {
return "Phong ";
}
};
class Sheen: public Microsurface {
float alpha;
float a, b, c, d, e;
float getD(Vector h) const {
const float cosTheta=h.z;
if(cosTheta<=1e-3f) return 0.0f;
const float sinTheta=sqrtf(1.0f-sqr(cosTheta));
const float power=alpha>1e-6f?1.0f/alpha:1e18f;
return (2.0f+power)*powf(sinTheta, power)/(2.0f*PI);
}
void initG1() {
const float t=sqr(1.0f-alpha);
a=t*25.3245f+(1-t)*21.5473f;
b=t*3.32435f+(1-t)*3.82987f;
c=t*0.16801f+(1-t)*0.19823f;
d=t*-1.27393f+(1-t)*-1.97760f;
e=t*-4.85967f+(1-t)*-4.32054f;
}
float getL(float x) const {
return a/(1.0f+b*powf(x, c))+d*x+e;
}
float getG1(Vector dir) const {
const float cosTheta = dir.z;
if(cosTheta<=1e-3f) return 0.0f;
if(cosTheta>=1.0f-1e-6f) return 1.0f;
if(dir.z<0.5f) {
const float lambda=expf(getL(cosTheta));
return 1.0f/(1.0f+lambda);
}
const float lambda=expf(2.0f*getL(0.5)-getL(1.0f-cosTheta));
return 1.0f/(1.0f+lambda);
}
public:
Sheen(float alpha, Matrix m=Matrix(1.0f)): alpha(alpha) {
initTransform(m);
this->alpha=clamp(alpha, 1e-6f, 1.0f);
initG1();
}
const char* getName() const {
return "Sheen ";
}
};
class STD: public Microsurface {
float alphaX;
float alphaY;
float gamma;
float getD(Vector h) const {
const float cosTheta=h.z;
if(cosTheta<=1e-3f) return 0.0f;
const float cosTheta2=sqr(cosTheta);
const float sinTheta2=1.0f-cosTheta2;
float denominator=PI*alphaX*alphaY;
if(sinTheta2>1e-6f) {
const float tanTheta2=sinTheta2/cosTheta2;
const float cosPhi2=sqr(h.x)/sinTheta2;
const float sinPhi2=sqr(h.y)/sinTheta2;
denominator*=sqr(cosTheta2)*powf(1.0f+tanTheta2*(cosPhi2/sqr(alphaX)+sinPhi2/sqr(alphaY))/(gamma-1.0f), gamma);
}
return 1.0f/denominator;
}
float getS1(float alpha, float mu) const {
return alpha*powf(gamma-1.0f+sqr(mu/alpha), 1.5f-gamma)/mu;
}
float getF21(float z) const {
return z*(1.066f+z*(2.655f+4.892f*z))/(1.038f+z*(2.969f+z*(4.305f+4.418f*z)));
}
float getF22() const {
return (-14.402f+gamma*(-27.145f+gamma*(20.574f-2.745f*gamma)))/(-30.612f+gamma*(86.567f+gamma*(-84.341f+29.938f*gamma)));
}
float getF23() const {
return (-129.404f+gamma*(324.987f+gamma*(-299.305f+93.268f*gamma)))/(-92.609f+gamma*(256.006f+gamma*(-245.663f+86.064f*gamma)));
}
float getF24(float z) const {
return (6.537f+z*(6.074f+z*(-0.623f+5.223f*z)))/(6.538f+z*(6.103f+z*(-3.218f+6.347f*z)));
}
float getS2(float alpha, float mu) const {
const float z=mu/alpha;
return getF21(z)*(getF22()+getF23()*getF24(z));
}
float getLambda(float alpha, float mu) const {
const float invSqrtPi=0.56418958354775628694807945156077f;
return invSqrtPi*tgammaf(gamma-0.5f)*(powf(gamma-1.0f, gamma)*getS1(alpha, mu)/(2.0f*gamma-3.0f)+sqrtf(gamma-1.0f)*getS2(alpha, mu))/tgammaf(gamma)-0.5f;
}
float getG1(Vector dir) const {
const float cosTheta = dir.z;
if(cosTheta<=1e-3f) return 0.0f;
if(cosTheta>=1.0f-1e-6f) return 1.0f;
const float alpha=getAlpha(dir, alphaX, alphaY);
const float cosTheta2=sqr(cosTheta);
const float cotTheta=sqrtf(cosTheta2/(1.0f-cosTheta2));
const float lambda=getLambda(alpha, cotTheta);
return 1.0f/(1.0f+lambda);
}
public:
STD(float alphaX, float alphaY, float gamma, Matrix m=Matrix(1.0f)): alphaX(alphaX), alphaY(alphaY), gamma(gamma) {
initTransform(m);
}
const char* getName() const {
return "STD ";
}
};
class Discrete: public Microsurface {
float alpha;
float gamma;
float cosGamma;
int count;
Vector *normals;
float getD(Vector h) const {
if (h.z<=1e-3f) return 0.0f;
int inside=0;
for (int i=0; i<count; i++) {
if (dotf(normals[i], h)>cosGamma) {
inside++;
}
}
const float coneSolidAngle=2.0f*PI*(1.0f-cosGamma);
return float(inside)/(float(count)*coneSolidAngle*h.z);
}
float getG1(Vector dir) const {
const float cosTheta = dir.z;
if(cosTheta<=1e-3f) return 0.0f;
if(cosTheta>=1.0f-1e-6f) return 1.0f;
const float tanTheta2=(1.0f/sqr(cosTheta))-1.0f;
const float denominator=1.0f+sqrtf(1.0f+tanTheta2*sqr(alpha));
return 2.0f/denominator;
}
Vector sampleGGXNormal(float alpha, float u, float v) {
float cosTheta2 = (1.0f-u)/(1.0f+(sqr(alpha)-1.0f)*u);
float cosTheta = sqrtf(std::max(cosTheta2, 0.0f));
float sinTheta = sqrtf(std::max(1.0f - cosTheta2, 0.0f));
float phi = 2.f*PI*v;
return Vector(cosf(phi)*sinTheta, sinf(phi)*sinTheta, cosTheta);
}
public:
Discrete(float alpha, float gamma, int count, Matrix m=Matrix(1.0f)): alpha(alpha), gamma(gamma), count(count) {
initTransform(m);
cosGamma=cosf(PI*gamma/180.0f);
normals=new Vector[count];
for (int i=0; i<count; i++) {
const float u=rnd();
const float v=rnd();
normals[i]=sampleGGXNormal(alpha, u, v);
}
}
~Discrete() {
delete[] normals;
}
const char* getName() const {
return "Discrete";
}
};