three-gpu-pathtracer 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +781 -728
- package/build/index.module.js +5223 -3956
- package/build/index.module.js.map +1 -1
- package/build/index.umd.cjs +5226 -3954
- package/build/index.umd.cjs.map +1 -1
- package/package.json +68 -60
- package/src/core/DynamicPathTracingSceneGenerator.js +119 -111
- package/src/core/MaterialReducer.js +256 -256
- package/src/core/PathTracingRenderer.js +255 -255
- package/src/core/PathTracingSceneGenerator.js +68 -68
- package/src/index.js +33 -26
- package/src/materials/AlphaDisplayMaterial.js +48 -48
- package/src/materials/AmbientOcclusionMaterial.js +197 -197
- package/src/materials/BlendMaterial.js +67 -67
- package/src/materials/LambertPathTracingMaterial.js +285 -285
- package/src/materials/MaterialBase.js +56 -56
- package/src/materials/PhysicalPathTracingMaterial.js +922 -848
- package/src/{core → objects}/EquirectCamera.js +13 -13
- package/src/{core → objects}/PhysicalCamera.js +28 -28
- package/src/objects/PhysicalSpotLight.js +14 -0
- package/src/objects/ShapedAreaLight.js +12 -0
- package/src/shader/shaderEnvMapSampling.js +59 -59
- package/src/shader/shaderGGXFunctions.js +108 -108
- package/src/shader/shaderIridescenceFunctions.js +130 -0
- package/src/shader/shaderLightSampling.js +231 -87
- package/src/shader/shaderMaterialSampling.js +546 -501
- package/src/shader/shaderSheenFunctions.js +98 -0
- package/src/shader/shaderStructs.js +307 -191
- package/src/shader/shaderUtils.js +350 -287
- package/src/uniforms/EquirectHdrInfoUniform.js +259 -263
- package/src/uniforms/IESProfilesTexture.js +100 -0
- package/src/uniforms/LightsInfoUniformStruct.js +162 -0
- package/src/uniforms/MaterialsTexture.js +406 -319
- package/src/uniforms/PhysicalCameraUniform.js +36 -36
- package/src/uniforms/RenderTarget2DArray.js +93 -93
- package/src/utils/BlurredEnvMapGenerator.js +113 -113
- package/src/utils/GeometryPreparationUtils.js +194 -194
- package/src/utils/IESLoader.js +325 -0
- package/src/utils/UVUnwrapper.js +101 -101
- package/src/workers/PathTracingSceneWorker.js +42 -41
- package/src/uniforms/LightsTexture.js +0 -83
|
@@ -1,501 +1,546 @@
|
|
|
1
|
-
import { shaderGGXFunctions } from './shaderGGXFunctions.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
float
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
float
|
|
17
|
-
float
|
|
18
|
-
float
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
float
|
|
23
|
-
float
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
vec3
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
float
|
|
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
|
-
return
|
|
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
|
-
const
|
|
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
|
-
float
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
float ratio = frontFace ? 1.0 / ior : ior;
|
|
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
|
-
float
|
|
278
|
-
|
|
279
|
-
float
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
float
|
|
302
|
-
float
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
float
|
|
320
|
-
float
|
|
321
|
-
float
|
|
322
|
-
bool
|
|
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
|
-
|
|
1
|
+
import { shaderGGXFunctions } from './shaderGGXFunctions.js';
|
|
2
|
+
import { shaderSheenFunctions } from './shaderSheenFunctions.js';
|
|
3
|
+
import { shaderIridescenceFunctions } from './shaderIridescenceFunctions.js';
|
|
4
|
+
|
|
5
|
+
export const shaderMaterialSampling = /* glsl */`
|
|
6
|
+
|
|
7
|
+
struct SurfaceRec {
|
|
8
|
+
vec3 normal;
|
|
9
|
+
vec3 faceNormal;
|
|
10
|
+
bool frontFace;
|
|
11
|
+
float roughness;
|
|
12
|
+
float filteredRoughness;
|
|
13
|
+
float metalness;
|
|
14
|
+
vec3 color;
|
|
15
|
+
vec3 emission;
|
|
16
|
+
float transmission;
|
|
17
|
+
float ior;
|
|
18
|
+
float clearcoat;
|
|
19
|
+
float clearcoatRoughness;
|
|
20
|
+
float filteredClearcoatRoughness;
|
|
21
|
+
vec3 sheenColor;
|
|
22
|
+
float sheenRoughness;
|
|
23
|
+
float iridescence;
|
|
24
|
+
float iridescenceIor;
|
|
25
|
+
float iridescenceThickness;
|
|
26
|
+
vec3 specularColor;
|
|
27
|
+
float specularIntensity;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
struct SampleRec {
|
|
31
|
+
float specularPdf;
|
|
32
|
+
float pdf;
|
|
33
|
+
vec3 direction;
|
|
34
|
+
vec3 clearcoatDirection;
|
|
35
|
+
vec3 color;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
${ shaderGGXFunctions }
|
|
39
|
+
${ shaderSheenFunctions }
|
|
40
|
+
${ shaderIridescenceFunctions }
|
|
41
|
+
|
|
42
|
+
// diffuse
|
|
43
|
+
float diffusePDF( vec3 wo, vec3 wi, SurfaceRec surf ) {
|
|
44
|
+
|
|
45
|
+
// https://raytracing.github.io/books/RayTracingTheRestOfYourLife.html#lightscattering/thescatteringpdf
|
|
46
|
+
float cosValue = wi.z;
|
|
47
|
+
return cosValue / PI;
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
vec3 diffuseDirection( vec3 wo, SurfaceRec surf ) {
|
|
52
|
+
|
|
53
|
+
vec3 lightDirection = randDirection();
|
|
54
|
+
lightDirection.z += 1.0;
|
|
55
|
+
lightDirection = normalize( lightDirection );
|
|
56
|
+
|
|
57
|
+
return lightDirection;
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
vec3 diffuseColor( vec3 wo, vec3 wi, SurfaceRec surf ) {
|
|
62
|
+
|
|
63
|
+
// TODO: scale by 1 - F here
|
|
64
|
+
// note on division by PI
|
|
65
|
+
// https://seblagarde.wordpress.com/2012/01/08/pi-or-not-to-pi-in-game-lighting-equation/
|
|
66
|
+
float metalFactor = ( 1.0 - surf.metalness ) * wi.z / ( PI * PI );
|
|
67
|
+
float transmissionFactor = 1.0 - surf.transmission;
|
|
68
|
+
return surf.color * metalFactor * transmissionFactor;
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// specular
|
|
73
|
+
float specularPDF( vec3 wo, vec3 wi, SurfaceRec surf ) {
|
|
74
|
+
|
|
75
|
+
// See 14.1.1 Microfacet BxDFs in https://www.pbr-book.org/
|
|
76
|
+
float filteredRoughness = surf.filteredRoughness;
|
|
77
|
+
vec3 halfVector = getHalfVector( wi, wo );
|
|
78
|
+
|
|
79
|
+
float incidentTheta = acos( wo.z );
|
|
80
|
+
float D = ggxDistribution( halfVector, filteredRoughness );
|
|
81
|
+
float G1 = ggxShadowMaskG1( incidentTheta, filteredRoughness );
|
|
82
|
+
float ggxPdf = D * G1 * max( 0.0, abs( dot( wo, halfVector ) ) ) / abs ( wo.z );
|
|
83
|
+
return ggxPdf / ( 4.0 * dot( wo, halfVector ) );
|
|
84
|
+
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
vec3 specularDirection( vec3 wo, SurfaceRec surf ) {
|
|
88
|
+
|
|
89
|
+
// sample ggx vndf distribution which gives a new normal
|
|
90
|
+
float filteredRoughness = surf.filteredRoughness;
|
|
91
|
+
vec3 halfVector = ggxDirection(
|
|
92
|
+
wo,
|
|
93
|
+
filteredRoughness,
|
|
94
|
+
filteredRoughness,
|
|
95
|
+
rand(),
|
|
96
|
+
rand()
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// apply to new ray by reflecting off the new normal
|
|
100
|
+
return - reflect( wo, halfVector );
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
vec3 specularColor( vec3 wo, vec3 wi, SurfaceRec surf ) {
|
|
105
|
+
|
|
106
|
+
// if roughness is set to 0 then D === NaN which results in black pixels
|
|
107
|
+
float metalness = surf.metalness;
|
|
108
|
+
float ior = surf.ior;
|
|
109
|
+
bool frontFace = surf.frontFace;
|
|
110
|
+
float filteredRoughness = surf.filteredRoughness;
|
|
111
|
+
|
|
112
|
+
vec3 halfVector = getHalfVector( wo, wi );
|
|
113
|
+
float iorRatio = frontFace ? 1.0 / ior : ior;
|
|
114
|
+
float G = ggxShadowMaskG2( wi, wo, filteredRoughness );
|
|
115
|
+
float D = ggxDistribution( halfVector, filteredRoughness );
|
|
116
|
+
vec3 F = vec3( schlickFresnelFromIor( dot( wi, halfVector ), iorRatio ) ) * surf.specularColor * surf.specularIntensity;
|
|
117
|
+
|
|
118
|
+
float cosTheta = min( wo.z, 1.0 );
|
|
119
|
+
float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
|
|
120
|
+
bool cannotRefract = iorRatio * sinTheta > 1.0;
|
|
121
|
+
if ( cannotRefract ) {
|
|
122
|
+
|
|
123
|
+
F = vec3( 1.0 );
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
float f0 = pow( ( iorRatio - 1.0 ) / ( iorRatio + 1.0 ), 2.0 );
|
|
128
|
+
vec3 iridescenceFresnel = evalIridescence( 1.0, surf.iridescenceIor, dot( wi, halfVector ), surf.iridescenceThickness, vec3( f0 ) );
|
|
129
|
+
F = mix( F, iridescenceFresnel, surf.iridescence );
|
|
130
|
+
|
|
131
|
+
vec3 color = mix( vec3( 1.0 ), surf.color, metalness );
|
|
132
|
+
color = mix( color, vec3( 1.0 ), F );
|
|
133
|
+
color *= G * D / ( 4.0 * abs( wi.z * wo.z ) );
|
|
134
|
+
color *= mix( F, vec3( 1.0 ), metalness );
|
|
135
|
+
color *= wi.z; // scale the light by the direction the light is coming in from
|
|
136
|
+
|
|
137
|
+
return color;
|
|
138
|
+
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/*
|
|
142
|
+
// transmission
|
|
143
|
+
function transmissionPDF( wo, wi, material, surf ) {
|
|
144
|
+
|
|
145
|
+
// See section 4.2 in https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf
|
|
146
|
+
|
|
147
|
+
const { roughness, ior } = material;
|
|
148
|
+
const { frontFace } = hit;
|
|
149
|
+
const ratio = frontFace ? ior : 1 / ior;
|
|
150
|
+
const minRoughness = Math.max( roughness, MIN_ROUGHNESS );
|
|
151
|
+
|
|
152
|
+
halfVector.set( 0, 0, 0 ).addScaledVector( wi, ratio ).addScaledVector( wo, 1.0 ).normalize().multiplyScalar( - 1 );
|
|
153
|
+
|
|
154
|
+
const denom = Math.pow( ratio * halfVector.dot( wi ) + 1.0 * halfVector.dot( wo ), 2.0 );
|
|
155
|
+
return ggxPDF( wo, halfVector, minRoughness ) / denom;
|
|
156
|
+
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function transmissionDirection( wo, hit, material, lightDirection ) {
|
|
160
|
+
|
|
161
|
+
const { roughness, ior } = material;
|
|
162
|
+
const { frontFace } = hit;
|
|
163
|
+
const ratio = frontFace ? 1 / ior : ior;
|
|
164
|
+
const minRoughness = Math.max( roughness, MIN_ROUGHNESS );
|
|
165
|
+
|
|
166
|
+
// sample ggx vndf distribution which gives a new normal
|
|
167
|
+
ggxDirection(
|
|
168
|
+
wo,
|
|
169
|
+
minRoughness,
|
|
170
|
+
minRoughness,
|
|
171
|
+
Math.random(),
|
|
172
|
+
Math.random(),
|
|
173
|
+
halfVector,
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
// apply to new ray by reflecting off the new normal
|
|
177
|
+
tempDir.copy( wo ).multiplyScalar( - 1 );
|
|
178
|
+
refract( tempDir, halfVector, ratio, lightDirection );
|
|
179
|
+
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function transmissionColor( wo, wi, material, hit, colorTarget ) {
|
|
183
|
+
|
|
184
|
+
const { metalness, transmission } = material;
|
|
185
|
+
colorTarget
|
|
186
|
+
.copy( material.color )
|
|
187
|
+
.multiplyScalar( ( 1.0 - metalness ) * wo.z )
|
|
188
|
+
.multiplyScalar( transmission );
|
|
189
|
+
|
|
190
|
+
}
|
|
191
|
+
*/
|
|
192
|
+
|
|
193
|
+
// TODO: This is just using a basic cosine-weighted specular distribution with an
|
|
194
|
+
// incorrect PDF value at the moment. Update it to correctly use a GGX distribution
|
|
195
|
+
float transmissionPDF( vec3 wo, vec3 wi, SurfaceRec surf ) {
|
|
196
|
+
|
|
197
|
+
float ior = surf.ior;
|
|
198
|
+
bool frontFace = surf.frontFace;
|
|
199
|
+
|
|
200
|
+
float ratio = frontFace ? 1.0 / ior : ior;
|
|
201
|
+
float cosTheta = min( wo.z, 1.0 );
|
|
202
|
+
float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
|
|
203
|
+
float reflectance = schlickFresnelFromIor( cosTheta, ratio );
|
|
204
|
+
bool cannotRefract = ratio * sinTheta > 1.0;
|
|
205
|
+
if ( cannotRefract ) {
|
|
206
|
+
|
|
207
|
+
return 0.0;
|
|
208
|
+
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return 1.0 / ( 1.0 - reflectance );
|
|
212
|
+
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
vec3 transmissionDirection( vec3 wo, SurfaceRec surf ) {
|
|
216
|
+
|
|
217
|
+
float roughness = surf.roughness;
|
|
218
|
+
float ior = surf.ior;
|
|
219
|
+
bool frontFace = surf.frontFace;
|
|
220
|
+
float ratio = frontFace ? 1.0 / ior : ior;
|
|
221
|
+
|
|
222
|
+
vec3 lightDirection = refract( - wo, vec3( 0.0, 0.0, 1.0 ), ratio );
|
|
223
|
+
lightDirection += randDirection() * roughness;
|
|
224
|
+
return normalize( lightDirection );
|
|
225
|
+
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
vec3 transmissionColor( vec3 wo, vec3 wi, SurfaceRec surf ) {
|
|
229
|
+
|
|
230
|
+
float metalness = surf.metalness;
|
|
231
|
+
float transmission = surf.transmission;
|
|
232
|
+
|
|
233
|
+
vec3 color = surf.color;
|
|
234
|
+
color *= ( 1.0 - metalness );
|
|
235
|
+
color *= transmission;
|
|
236
|
+
|
|
237
|
+
return color;
|
|
238
|
+
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// clearcoat
|
|
242
|
+
float clearcoatPDF( vec3 wo, vec3 wi, SurfaceRec surf ) {
|
|
243
|
+
|
|
244
|
+
// See equation (27) in http://jcgt.org/published/0003/02/03/
|
|
245
|
+
float filteredClearcoatRoughness = surf.filteredClearcoatRoughness;
|
|
246
|
+
vec3 halfVector = getHalfVector( wi, wo );
|
|
247
|
+
return ggxPDF( wo, halfVector, filteredClearcoatRoughness ) / ( 4.0 * dot( wi, halfVector ) );
|
|
248
|
+
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
vec3 clearcoatDirection( vec3 wo, SurfaceRec surf ) {
|
|
252
|
+
|
|
253
|
+
// sample ggx vndf distribution which gives a new normal
|
|
254
|
+
float filteredClearcoatRoughness = surf.filteredClearcoatRoughness;
|
|
255
|
+
vec3 halfVector = ggxDirection(
|
|
256
|
+
wo,
|
|
257
|
+
filteredClearcoatRoughness,
|
|
258
|
+
filteredClearcoatRoughness,
|
|
259
|
+
rand(),
|
|
260
|
+
rand()
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
// apply to new ray by reflecting off the new normal
|
|
264
|
+
return - reflect( wo, halfVector );
|
|
265
|
+
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
void clearcoatColor( inout vec3 color, vec3 wo, vec3 wi, SurfaceRec surf ) {
|
|
269
|
+
|
|
270
|
+
float ior = 1.5;
|
|
271
|
+
bool frontFace = surf.frontFace;
|
|
272
|
+
float filteredClearcoatRoughness = surf.filteredClearcoatRoughness;
|
|
273
|
+
|
|
274
|
+
vec3 halfVector = getHalfVector( wo, wi );
|
|
275
|
+
float iorRatio = frontFace ? 1.0 / ior : ior;
|
|
276
|
+
float G = ggxShadowMaskG2( wi, wo, filteredClearcoatRoughness );
|
|
277
|
+
float D = ggxDistribution( halfVector, filteredClearcoatRoughness );
|
|
278
|
+
|
|
279
|
+
float F = schlickFresnelFromIor( dot( wi, halfVector ), ior );
|
|
280
|
+
float cosTheta = min( wo.z, 1.0 );
|
|
281
|
+
float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
|
|
282
|
+
bool cannotRefract = iorRatio * sinTheta > 1.0;
|
|
283
|
+
if ( cannotRefract ) {
|
|
284
|
+
|
|
285
|
+
F = 1.0;
|
|
286
|
+
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
float fClearcoat = F * D * G / ( 4.0 * abs( wi.z * wo.z ) );
|
|
290
|
+
|
|
291
|
+
color = color * ( 1.0 - surf.clearcoat * F ) + fClearcoat * surf.clearcoat * wi.z;
|
|
292
|
+
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// sheen
|
|
296
|
+
vec3 sheenColor( vec3 wo, vec3 wi, SurfaceRec surf ) {
|
|
297
|
+
|
|
298
|
+
vec3 halfVector = getHalfVector( wo, wi );
|
|
299
|
+
|
|
300
|
+
float cosThetaO = saturateCos( wo.z );
|
|
301
|
+
float cosThetaI = saturateCos( wi.z );
|
|
302
|
+
float cosThetaH = halfVector.z;
|
|
303
|
+
|
|
304
|
+
float D = velvetD( cosThetaH, surf.sheenRoughness );
|
|
305
|
+
float G = velvetG( cosThetaO, cosThetaI, surf.sheenRoughness );
|
|
306
|
+
|
|
307
|
+
// See equation (1) in http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf
|
|
308
|
+
vec3 color = surf.sheenColor;
|
|
309
|
+
color *= D * G / ( 4.0 * abs( cosThetaO * cosThetaI ) );
|
|
310
|
+
color *= wi.z;
|
|
311
|
+
|
|
312
|
+
return color;
|
|
313
|
+
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// bsdf
|
|
317
|
+
void getLobeWeights( vec3 wo, vec3 clearcoatWo, SurfaceRec surf, out float diffuseWeight, out float specularWeight, out float transmissionWeight, out float clearcoatWeight ) {
|
|
318
|
+
|
|
319
|
+
float ior = surf.ior;
|
|
320
|
+
float metalness = surf.metalness;
|
|
321
|
+
float transmission = surf.transmission;
|
|
322
|
+
bool frontFace = surf.frontFace;
|
|
323
|
+
|
|
324
|
+
float ratio = frontFace ? 1.0 / ior : ior;
|
|
325
|
+
float cosTheta = min( wo.z, 1.0 );
|
|
326
|
+
float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
|
|
327
|
+
float reflectance = schlickFresnelFromIor( cosTheta, ratio );
|
|
328
|
+
bool cannotRefract = ratio * sinTheta > 1.0;
|
|
329
|
+
if ( cannotRefract ) {
|
|
330
|
+
|
|
331
|
+
reflectance = 1.0;
|
|
332
|
+
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
float transSpecularProb = mix( reflectance, 1.0, metalness );
|
|
336
|
+
float diffSpecularProb = 0.5 + 0.5 * metalness;
|
|
337
|
+
|
|
338
|
+
clearcoatWeight = surf.clearcoat * schlickFresnel( clearcoatWo.z, 0.04 );
|
|
339
|
+
diffuseWeight = ( 1.0 - transmission ) * ( 1.0 - diffSpecularProb ) * ( 1.0 - clearcoatWeight );
|
|
340
|
+
specularWeight = transmission * transSpecularProb + ( 1.0 - transmission ) * diffSpecularProb * ( 1.0 - clearcoatWeight );
|
|
341
|
+
transmissionWeight = transmission * ( 1.0 - transSpecularProb ) * ( 1.0 - clearcoatWeight );
|
|
342
|
+
|
|
343
|
+
float totalWeight = diffuseWeight + specularWeight + transmissionWeight + clearcoatWeight;
|
|
344
|
+
float invTotalWeight = 1.0 / totalWeight;
|
|
345
|
+
|
|
346
|
+
diffuseWeight *= invTotalWeight;
|
|
347
|
+
specularWeight *= invTotalWeight;
|
|
348
|
+
transmissionWeight *= invTotalWeight;
|
|
349
|
+
clearcoatWeight *= invTotalWeight;
|
|
350
|
+
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
float bsdfPdf( vec3 wo, vec3 clearcoatWo, vec3 wi, vec3 clearcoatWi, SurfaceRec surf, out float specularPdf, float diffuseWeight, float specularWeight, float transmissionWeight, float clearcoatWeight ) {
|
|
354
|
+
|
|
355
|
+
float ior = surf.ior;
|
|
356
|
+
float metalness = surf.metalness;
|
|
357
|
+
float transmission = surf.transmission;
|
|
358
|
+
bool frontFace = surf.frontFace;
|
|
359
|
+
|
|
360
|
+
float ratio = frontFace ? 1.0 / ior : ior;
|
|
361
|
+
float cosTheta = min( wo.z, 1.0 );
|
|
362
|
+
float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
|
|
363
|
+
float reflectance = schlickFresnelFromIor( cosTheta, ratio );
|
|
364
|
+
bool cannotRefract = ratio * sinTheta > 1.0;
|
|
365
|
+
if ( cannotRefract ) {
|
|
366
|
+
|
|
367
|
+
reflectance = 1.0;
|
|
368
|
+
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
float spdf = 0.0;
|
|
372
|
+
float dpdf = 0.0;
|
|
373
|
+
float tpdf = 0.0;
|
|
374
|
+
float cpdf = 0.0;
|
|
375
|
+
|
|
376
|
+
if ( wi.z < 0.0 ) {
|
|
377
|
+
|
|
378
|
+
if( transmissionWeight > 0.0 ) {
|
|
379
|
+
|
|
380
|
+
tpdf = transmissionPDF( wo, wi, surf );
|
|
381
|
+
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
} else {
|
|
385
|
+
|
|
386
|
+
if( diffuseWeight > 0.0 ) {
|
|
387
|
+
|
|
388
|
+
dpdf = diffusePDF( wo, wi, surf );
|
|
389
|
+
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if( specularWeight > 0.0 ) {
|
|
393
|
+
|
|
394
|
+
spdf = specularPDF( wo, wi, surf );
|
|
395
|
+
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if( clearcoatWi.z >= 0.0 && clearcoatWeight > 0.0 ) {
|
|
401
|
+
|
|
402
|
+
cpdf = clearcoatPDF( clearcoatWo, clearcoatWi, surf );
|
|
403
|
+
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
float pdf =
|
|
407
|
+
dpdf * diffuseWeight
|
|
408
|
+
+ spdf * specularWeight
|
|
409
|
+
+ tpdf * transmissionWeight
|
|
410
|
+
+ cpdf * clearcoatWeight;
|
|
411
|
+
|
|
412
|
+
// retrieve specular rays for the shadows flag
|
|
413
|
+
specularPdf = spdf * specularWeight + cpdf * clearcoatWeight;
|
|
414
|
+
|
|
415
|
+
return pdf;
|
|
416
|
+
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
vec3 bsdfColor( vec3 wo, vec3 clearcoatWo, vec3 wi, vec3 clearcoatWi, SurfaceRec surf, float diffuseWeight, float specularWeight, float transmissionWeight, float clearcoatWeight ) {
|
|
420
|
+
|
|
421
|
+
vec3 color = vec3( 0.0 );
|
|
422
|
+
if ( wi.z < 0.0 ) {
|
|
423
|
+
|
|
424
|
+
if( transmissionWeight > 0.0 ) {
|
|
425
|
+
|
|
426
|
+
color = transmissionColor( wo, wi, surf );
|
|
427
|
+
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
} else {
|
|
431
|
+
|
|
432
|
+
if( diffuseWeight > 0.0 ) {
|
|
433
|
+
|
|
434
|
+
color = diffuseColor( wo, wi, surf );
|
|
435
|
+
color *= 1.0 - surf.transmission;
|
|
436
|
+
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if( specularWeight > 0.0 ) {
|
|
440
|
+
|
|
441
|
+
color += specularColor( wo, wi, surf );
|
|
442
|
+
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
color *= sheenAlbedoScaling( wo, wi, surf );
|
|
446
|
+
color += sheenColor( wo, wi, surf );
|
|
447
|
+
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if( clearcoatWi.z >= 0.0 && clearcoatWeight > 0.0 ) {
|
|
451
|
+
|
|
452
|
+
clearcoatColor( color, clearcoatWo, clearcoatWi, surf );
|
|
453
|
+
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return color;
|
|
457
|
+
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
float bsdfResult( vec3 wo, vec3 clearcoatWo, vec3 wi, vec3 clearcoatWi, SurfaceRec surf, out vec3 color ) {
|
|
461
|
+
|
|
462
|
+
float diffuseWeight;
|
|
463
|
+
float specularWeight;
|
|
464
|
+
float transmissionWeight;
|
|
465
|
+
float clearcoatWeight;
|
|
466
|
+
getLobeWeights( wo, clearcoatWo, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
|
|
467
|
+
|
|
468
|
+
float specularPdf;
|
|
469
|
+
color = bsdfColor( wo, clearcoatWo, wi, clearcoatWi, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
|
|
470
|
+
return bsdfPdf( wo, clearcoatWo, wi, clearcoatWi, surf, specularPdf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
|
|
471
|
+
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
SampleRec bsdfSample( vec3 wo, vec3 clearcoatWo, mat3 normalBasis, mat3 invBasis, mat3 clearcoatNormalBasis, mat3 clearcoatInvBasis, SurfaceRec surf ) {
|
|
475
|
+
|
|
476
|
+
float diffuseWeight;
|
|
477
|
+
float specularWeight;
|
|
478
|
+
float transmissionWeight;
|
|
479
|
+
float clearcoatWeight;
|
|
480
|
+
getLobeWeights( wo, clearcoatWo, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
|
|
481
|
+
|
|
482
|
+
float pdf[4];
|
|
483
|
+
pdf[0] = diffuseWeight;
|
|
484
|
+
pdf[1] = specularWeight;
|
|
485
|
+
pdf[2] = transmissionWeight;
|
|
486
|
+
pdf[3] = clearcoatWeight;
|
|
487
|
+
|
|
488
|
+
float cdf[4];
|
|
489
|
+
cdf[0] = pdf[0];
|
|
490
|
+
cdf[1] = pdf[1] + cdf[0];
|
|
491
|
+
cdf[2] = pdf[2] + cdf[1];
|
|
492
|
+
cdf[3] = pdf[3] + cdf[2];
|
|
493
|
+
|
|
494
|
+
if( cdf[3] != 0.0 ) {
|
|
495
|
+
|
|
496
|
+
float invMaxCdf = 1.0 / cdf[3];
|
|
497
|
+
cdf[0] *= invMaxCdf;
|
|
498
|
+
cdf[1] *= invMaxCdf;
|
|
499
|
+
cdf[2] *= invMaxCdf;
|
|
500
|
+
cdf[3] *= invMaxCdf;
|
|
501
|
+
|
|
502
|
+
} else {
|
|
503
|
+
|
|
504
|
+
cdf[0] = 1.0;
|
|
505
|
+
cdf[1] = 0.0;
|
|
506
|
+
cdf[2] = 0.0;
|
|
507
|
+
cdf[3] = 0.0;
|
|
508
|
+
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
vec3 wi;
|
|
512
|
+
vec3 clearcoatWi;
|
|
513
|
+
|
|
514
|
+
float r = rand();
|
|
515
|
+
if ( r <= cdf[0] ) {
|
|
516
|
+
|
|
517
|
+
wi = diffuseDirection( wo, surf );
|
|
518
|
+
clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
|
|
519
|
+
|
|
520
|
+
} else if ( r <= cdf[1] ) {
|
|
521
|
+
|
|
522
|
+
wi = specularDirection( wo, surf );
|
|
523
|
+
clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
|
|
524
|
+
|
|
525
|
+
} else if ( r <= cdf[2] ) {
|
|
526
|
+
|
|
527
|
+
wi = transmissionDirection( wo, surf );
|
|
528
|
+
clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
|
|
529
|
+
|
|
530
|
+
} else if ( r <= cdf[3] ) {
|
|
531
|
+
|
|
532
|
+
clearcoatWi = clearcoatDirection( clearcoatWo, surf );
|
|
533
|
+
wi = normalize( invBasis * normalize( clearcoatNormalBasis * clearcoatWi ) );
|
|
534
|
+
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
SampleRec result;
|
|
538
|
+
result.pdf = bsdfPdf( wo, clearcoatWo, wi, clearcoatWi, surf, result.specularPdf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
|
|
539
|
+
result.color = bsdfColor( wo, clearcoatWo, wi, clearcoatWi, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
|
|
540
|
+
result.direction = wi;
|
|
541
|
+
result.clearcoatDirection = clearcoatWi;
|
|
542
|
+
|
|
543
|
+
return result;
|
|
544
|
+
|
|
545
|
+
}
|
|
546
|
+
`;
|