three-gpu-pathtracer 0.0.12 → 0.0.14

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.
Files changed (58) hide show
  1. package/README.md +102 -7
  2. package/build/index.module.js +2891 -2065
  3. package/build/index.module.js.map +1 -1
  4. package/build/index.umd.cjs +2886 -2062
  5. package/build/index.umd.cjs.map +1 -1
  6. package/package.json +2 -1
  7. package/src/core/PathTracingRenderer.js +87 -16
  8. package/src/core/PathTracingSceneGenerator.js +1 -1
  9. package/src/core/QuiltPathTracingRenderer.js +223 -0
  10. package/src/index.js +5 -8
  11. package/src/materials/{GraphMaterial.js → debug/GraphMaterial.js} +1 -1
  12. package/src/materials/{AlphaDisplayMaterial.js → fullscreen/AlphaDisplayMaterial.js} +1 -1
  13. package/src/materials/{BlendMaterial.js → fullscreen/BlendMaterial.js} +1 -1
  14. package/src/materials/{DenoiseMaterial.js → fullscreen/DenoiseMaterial.js} +1 -1
  15. package/src/materials/{LambertPathTracingMaterial.js → pathtracing/LambertPathTracingMaterial.js} +18 -7
  16. package/src/materials/pathtracing/PhysicalPathTracingMaterial.js +635 -0
  17. package/src/materials/pathtracing/glsl/attenuateHit.glsl.js +179 -0
  18. package/src/materials/pathtracing/glsl/cameraUtils.glsl.js +81 -0
  19. package/src/materials/pathtracing/glsl/getSurfaceRecord.glsl.js +317 -0
  20. package/src/materials/pathtracing/glsl/traceScene.glsl.js +54 -0
  21. package/src/materials/{AmbientOcclusionMaterial.js → surface/AmbientOcclusionMaterial.js} +16 -8
  22. package/src/materials/surface/FogVolumeMaterial.js +23 -0
  23. package/src/shader/bsdf/bsdfSampling.glsl.js +490 -0
  24. package/src/shader/bsdf/fog.glsl.js +23 -0
  25. package/src/shader/bsdf/ggx.glsl.js +102 -0
  26. package/src/shader/bsdf/iridescence.glsl.js +135 -0
  27. package/src/shader/bsdf/sheen.glsl.js +98 -0
  28. package/src/shader/{shaderLayerTexelFetchFunctions.js → common/arraySamplerTexelFetch.glsl.js} +1 -1
  29. package/src/shader/common/bvhAnyHit.glsl.js +76 -0
  30. package/src/shader/common/fresnel.glsl.js +98 -0
  31. package/src/shader/common/intersectShapes.glsl.js +62 -0
  32. package/src/shader/common/math.glsl.js +81 -0
  33. package/src/shader/common/utils.glsl.js +116 -0
  34. package/src/shader/{shaderRandFunctions.js → rand/pcg.glsl.js} +1 -1
  35. package/src/shader/{shaderSobolSampling.js → rand/sobol.glsl.js} +3 -3
  36. package/src/shader/sampling/equirectSampling.glsl.js +62 -0
  37. package/src/shader/sampling/lightSampling.glsl.js +223 -0
  38. package/src/shader/sampling/shapeSampling.glsl.js +86 -0
  39. package/src/shader/structs/cameraStruct.glsl.js +13 -0
  40. package/src/shader/structs/equirectStruct.glsl.js +14 -0
  41. package/src/shader/structs/fogMaterialBvh.glsl.js +62 -0
  42. package/src/shader/structs/lightsStruct.glsl.js +78 -0
  43. package/src/shader/{shaderStructs.js → structs/materialStruct.glsl.js} +5 -123
  44. package/src/uniforms/EquirectHdrInfoUniform.js +29 -11
  45. package/src/uniforms/LightsInfoUniformStruct.js +9 -4
  46. package/src/uniforms/MaterialsTexture.js +80 -3
  47. package/src/utils/BlurredEnvMapGenerator.js +2 -2
  48. package/src/utils/SobolNumberMapGenerator.js +3 -3
  49. package/src/utils/macroify.js +9 -0
  50. package/src/materials/PhysicalPathTracingMaterial.js +0 -982
  51. package/src/shader/shaderBvhAnyHit.js +0 -76
  52. package/src/shader/shaderEnvMapSampling.js +0 -58
  53. package/src/shader/shaderGGXFunctions.js +0 -100
  54. package/src/shader/shaderIridescenceFunctions.js +0 -130
  55. package/src/shader/shaderLightSampling.js +0 -229
  56. package/src/shader/shaderMaterialSampling.js +0 -506
  57. package/src/shader/shaderSheenFunctions.js +0 -98
  58. package/src/shader/shaderUtils.js +0 -361
@@ -0,0 +1,490 @@
1
+ import { ggxGLSL } from './ggx.glsl.js';
2
+ import { sheenGLSL } from './sheen.glsl.js';
3
+ import { iridescenceGLSL } from './iridescence.glsl.js';
4
+
5
+ /*
6
+ wi : incident vector or light vector (pointing toward the light)
7
+ wo : outgoing vector or view vector (pointing towards the camera)
8
+ wh : computed half vector from wo and wi
9
+ Eval : Get the color and pdf for a direction
10
+ Sample : Get the direction, color, and pdf for a sample
11
+ eta : Greek character used to denote the "ratio of ior"
12
+ f0 : Amount of light reflected when looking at a surface head on - "fresnel 0"
13
+ f90 : Amount of light reflected at grazing angles
14
+ */
15
+
16
+ export const bsdfSamplingGLSL = /* glsl */`
17
+
18
+ struct SurfaceRecord {
19
+
20
+ // surface type
21
+ bool volumeParticle;
22
+
23
+ // geometry
24
+ vec3 faceNormal;
25
+ bool frontFace;
26
+ vec3 normal;
27
+
28
+ // cached properties
29
+ float eta;
30
+ float f0;
31
+
32
+ // material
33
+ float roughness;
34
+ float filteredRoughness;
35
+ float metalness;
36
+ vec3 color;
37
+ vec3 emission;
38
+
39
+ // transmission
40
+ float ior;
41
+ float transmission;
42
+ bool thinFilm;
43
+ vec3 attenuationColor;
44
+ float attenuationDistance;
45
+
46
+ // clearcoat
47
+ vec3 clearcoatNormal;
48
+ float clearcoat;
49
+ float clearcoatRoughness;
50
+ float filteredClearcoatRoughness;
51
+
52
+ // sheen
53
+ float sheen;
54
+ vec3 sheenColor;
55
+ float sheenRoughness;
56
+
57
+ // iridescence
58
+ float iridescence;
59
+ float iridescenceIor;
60
+ float iridescenceThickness;
61
+
62
+ // specular
63
+ vec3 specularColor;
64
+ float specularIntensity;
65
+ };
66
+
67
+ struct ScatterRecord {
68
+ float specularPdf;
69
+ float pdf;
70
+
71
+ vec3 direction;
72
+ vec3 clearcoatDirection;
73
+
74
+ vec3 color;
75
+ };
76
+
77
+ ${ ggxGLSL }
78
+ ${ sheenGLSL }
79
+ ${ iridescenceGLSL }
80
+
81
+ // diffuse
82
+ float diffuseEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRecord surf, out vec3 color ) {
83
+
84
+ // https://schuttejoe.github.io/post/disneybsdf/
85
+ float fl = schlickFresnel( wi.z, 0.0 );
86
+ float fv = schlickFresnel( wo.z, 0.0 );
87
+
88
+ float metalFactor = ( 1.0 - surf.metalness );
89
+ float transFactor = ( 1.0 - surf.transmission );
90
+ float rr = 0.5 + 2.0 * surf.roughness * fl * fl;
91
+ float retro = rr * ( fl + fv + fl * fv * ( rr - 1.0f ) );
92
+ float lambert = ( 1.0f - 0.5f * fl ) * ( 1.0f - 0.5f * fv );
93
+
94
+ // TODO: subsurface approx?
95
+
96
+ float F = evaluateFresnelWeight( dot( wo, wh ), surf.eta, surf.f0 );
97
+ color = ( 1.0 - F ) * transFactor * metalFactor * wi.z * surf.color * ( retro + lambert ) / PI;
98
+ return wi.z / PI;
99
+
100
+ }
101
+
102
+ vec3 diffuseDirection( vec3 wo, SurfaceRecord surf ) {
103
+
104
+ vec3 lightDirection = sampleSphere( sobol2( 11 ) );
105
+ lightDirection.z += 1.0;
106
+ lightDirection = normalize( lightDirection );
107
+
108
+ return lightDirection;
109
+
110
+ }
111
+
112
+ // specular
113
+ float specularEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRecord surf, out vec3 color ) {
114
+
115
+ // if roughness is set to 0 then D === NaN which results in black pixels
116
+ float metalness = surf.metalness;
117
+ float roughness = surf.filteredRoughness;
118
+
119
+ float eta = surf.eta;
120
+ float f0 = surf.f0;
121
+
122
+ vec3 f0Color = mix( f0 * surf.specularColor * surf.specularIntensity, surf.color, surf.metalness );
123
+ vec3 f90Color = vec3( mix( surf.specularIntensity, 1.0, surf.metalness ) );
124
+ vec3 F = evaluateFresnel( dot( wo, wh ), eta, f0Color, f90Color );
125
+
126
+ vec3 iridescenceF = evalIridescence( 1.0, surf.iridescenceIor, dot( wi, wh ), surf.iridescenceThickness, f0Color );
127
+ F = mix( F, iridescenceF, surf.iridescence );
128
+
129
+ // PDF
130
+ // See 14.1.1 Microfacet BxDFs in https://www.pbr-book.org/
131
+ float incidentTheta = acos( wo.z );
132
+ float G = ggxShadowMaskG2( wi, wo, roughness );
133
+ float D = ggxDistribution( wh, roughness );
134
+ float G1 = ggxShadowMaskG1( incidentTheta, roughness );
135
+ float ggxPdf = D * G1 * max( 0.0, abs( dot( wo, wh ) ) ) / abs ( wo.z );
136
+
137
+ color = wi.z * F * G * D / ( 4.0 * abs( wi.z * wo.z ) );
138
+ return ggxPdf / ( 4.0 * dot( wo, wh ) );
139
+
140
+ }
141
+
142
+ vec3 specularDirection( vec3 wo, SurfaceRecord surf ) {
143
+
144
+ // sample ggx vndf distribution which gives a new normal
145
+ float roughness = surf.filteredRoughness;
146
+ vec3 halfVector = ggxDirection(
147
+ wo,
148
+ vec2( roughness ),
149
+ sobol2( 12 )
150
+ );
151
+
152
+ // apply to new ray by reflecting off the new normal
153
+ return - reflect( wo, halfVector );
154
+
155
+ }
156
+
157
+
158
+ // transmission
159
+ /*
160
+ float transmissionEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRecord surf, out vec3 color ) {
161
+
162
+ // See section 4.2 in https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf
163
+
164
+ float filteredRoughness = surf.filteredRoughness;
165
+ float eta = surf.eta;
166
+ bool frontFace = surf.frontFace;
167
+ bool thinFilm = surf.thinFilm;
168
+
169
+ color = surf.transmission * surf.color;
170
+
171
+ float denom = pow( eta * dot( wi, wh ) + dot( wo, wh ), 2.0 );
172
+ return ggxPDF( wo, wh, filteredRoughness ) / denom;
173
+
174
+ }
175
+
176
+ vec3 transmissionDirection( vec3 wo, SurfaceRecord surf ) {
177
+
178
+ float filteredRoughness = surf.filteredRoughness;
179
+ float eta = surf.eta;
180
+ bool frontFace = surf.frontFace;
181
+
182
+ // sample ggx vndf distribution which gives a new normal
183
+ vec3 halfVector = ggxDirection(
184
+ wo,
185
+ vec2( filteredRoughness ),
186
+ sobol2( 13 )
187
+ );
188
+
189
+ vec3 lightDirection = refract( normalize( - wo ), halfVector, eta );
190
+ if ( surf.thinFilm ) {
191
+
192
+ lightDirection = - refract( normalize( - lightDirection ), - vec3( 0.0, 0.0, 1.0 ), 1.0 / eta );
193
+
194
+ }
195
+
196
+ return normalize( lightDirection );
197
+
198
+ }
199
+ */
200
+
201
+ // TODO: This is just using a basic cosine-weighted specular distribution with an
202
+ // incorrect PDF value at the moment. Update it to correctly use a GGX distribution
203
+ float transmissionEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRecord surf, out vec3 color ) {
204
+
205
+ color = surf.transmission * surf.color;
206
+
207
+ // PDF
208
+ float F = evaluateFresnelWeight( dot( wo, wh ), surf.eta, surf.f0 );
209
+ if ( F >= 1.0 ) {
210
+
211
+ return 0.0;
212
+
213
+ }
214
+
215
+ return 1.0 / ( 1.0 - F );
216
+
217
+ }
218
+
219
+ vec3 transmissionDirection( vec3 wo, SurfaceRecord surf ) {
220
+
221
+ float roughness = surf.filteredRoughness;
222
+ float eta = surf.eta;
223
+ vec3 halfVector = normalize( vec3( 0.0, 0.0, 1.0 ) + sampleSphere( sobol2( 13 ) ) * roughness );
224
+ vec3 lightDirection = refract( normalize( - wo ), halfVector, eta );
225
+
226
+ if ( surf.thinFilm ) {
227
+
228
+ lightDirection = - refract( normalize( - lightDirection ), - vec3( 0.0, 0.0, 1.0 ), 1.0 / eta );
229
+
230
+ }
231
+ return normalize( lightDirection );
232
+
233
+ }
234
+
235
+ // clearcoat
236
+ float clearcoatEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRecord surf, inout vec3 color ) {
237
+
238
+ float ior = 1.5;
239
+ float f0 = iorRatioToF0( ior );
240
+ bool frontFace = surf.frontFace;
241
+ float roughness = surf.filteredClearcoatRoughness;
242
+
243
+ float eta = frontFace ? 1.0 / ior : ior;
244
+ float G = ggxShadowMaskG2( wi, wo, roughness );
245
+ float D = ggxDistribution( wh, roughness );
246
+ float F = schlickFresnel( dot( wi, wh ), f0 );
247
+
248
+ float fClearcoat = F * D * G / ( 4.0 * abs( wi.z * wo.z ) );
249
+ color = color * ( 1.0 - surf.clearcoat * F ) + fClearcoat * surf.clearcoat * wi.z;
250
+
251
+ // PDF
252
+ // See equation (27) in http://jcgt.org/published/0003/02/03/
253
+ return ggxPDF( wo, wh, roughness ) / ( 4.0 * dot( wi, wh ) );
254
+
255
+ }
256
+
257
+ vec3 clearcoatDirection( vec3 wo, SurfaceRecord surf ) {
258
+
259
+ // sample ggx vndf distribution which gives a new normal
260
+ float roughness = surf.filteredClearcoatRoughness;
261
+ vec3 halfVector = ggxDirection(
262
+ wo,
263
+ vec2( roughness ),
264
+ sobol2( 14 )
265
+ );
266
+
267
+ // apply to new ray by reflecting off the new normal
268
+ return - reflect( wo, halfVector );
269
+
270
+ }
271
+
272
+ // sheen
273
+ vec3 sheenColor( vec3 wo, vec3 wi, vec3 wh, SurfaceRecord surf ) {
274
+
275
+ float cosThetaO = saturateCos( wo.z );
276
+ float cosThetaI = saturateCos( wi.z );
277
+ float cosThetaH = wh.z;
278
+
279
+ float D = velvetD( cosThetaH, surf.sheenRoughness );
280
+ float G = velvetG( cosThetaO, cosThetaI, surf.sheenRoughness );
281
+
282
+ // See equation (1) in http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf
283
+ vec3 color = surf.sheenColor;
284
+ color *= D * G / ( 4.0 * abs( cosThetaO * cosThetaI ) );
285
+ color *= wi.z;
286
+
287
+ return color;
288
+
289
+ }
290
+
291
+ // bsdf
292
+ void getLobeWeights(
293
+ vec3 wo, vec3 wi, vec3 wh, vec3 clearcoatWo, SurfaceRecord surf,
294
+ out float diffuseWeight, out float specularWeight, out float transmissionWeight, out float clearcoatWeight
295
+ ) {
296
+
297
+ float metalness = surf.metalness;
298
+ float transmission = surf.transmission;
299
+ float fEstimate = evaluateFresnelWeight( dot( wo, wh ), surf.eta, surf.f0 );
300
+
301
+ float transSpecularProb = mix( max( 0.25, fEstimate ), 1.0, metalness );
302
+ float diffSpecularProb = 0.5 + 0.5 * metalness;
303
+
304
+ diffuseWeight = ( 1.0 - transmission ) * ( 1.0 - diffSpecularProb );
305
+ specularWeight = transmission * transSpecularProb + ( 1.0 - transmission ) * diffSpecularProb;
306
+ transmissionWeight = transmission * ( 1.0 - transSpecularProb );
307
+ clearcoatWeight = surf.clearcoat * schlickFresnel( clearcoatWo.z, 0.04 );
308
+
309
+ float totalWeight = diffuseWeight + specularWeight + transmissionWeight + clearcoatWeight;
310
+ diffuseWeight /= totalWeight;
311
+ specularWeight /= totalWeight;
312
+ transmissionWeight /= totalWeight;
313
+ clearcoatWeight /= totalWeight;
314
+ }
315
+
316
+ float bsdfEval(
317
+ vec3 wo, vec3 clearcoatWo, vec3 wi, vec3 clearcoatWi, SurfaceRecord surf,
318
+ float diffuseWeight, float specularWeight, float transmissionWeight, float clearcoatWeight, out float specularPdf, out vec3 color
319
+ ) {
320
+
321
+ float metalness = surf.metalness;
322
+ float transmission = surf.transmission;
323
+
324
+ float spdf = 0.0;
325
+ float dpdf = 0.0;
326
+ float tpdf = 0.0;
327
+ float cpdf = 0.0;
328
+ color = vec3( 0.0 );
329
+
330
+ vec3 halfVector = getHalfVector( wi, wo, surf.eta );
331
+
332
+ // diffuse
333
+ if ( diffuseWeight > 0.0 && wi.z > 0.0 ) {
334
+
335
+ dpdf = diffuseEval( wo, wi, halfVector, surf, color );
336
+ color *= 1.0 - surf.transmission;
337
+
338
+ }
339
+
340
+ // ggx specular
341
+ if ( specularWeight > 0.0 && wi.z > 0.0 ) {
342
+
343
+ vec3 outColor;
344
+ spdf = specularEval( wo, wi, getHalfVector( wi, wo ), surf, outColor );
345
+ color += outColor;
346
+
347
+ }
348
+
349
+ // transmission
350
+ if ( transmissionWeight > 0.0 && wi.z < 0.0 ) {
351
+
352
+ tpdf = transmissionEval( wo, wi, halfVector, surf, color );
353
+
354
+ }
355
+
356
+ // sheen
357
+ color *= mix( 1.0, sheenAlbedoScaling( wo, wi, surf ), surf.sheen );
358
+ color += sheenColor( wo, wi, halfVector, surf ) * surf.sheen;
359
+
360
+ // clearcoat
361
+ if ( clearcoatWi.z >= 0.0 && clearcoatWeight > 0.0 ) {
362
+
363
+ vec3 clearcoatHalfVector = getHalfVector( clearcoatWo, clearcoatWi );
364
+ cpdf = clearcoatEval( clearcoatWo, clearcoatWi, clearcoatHalfVector, surf, color );
365
+
366
+ }
367
+
368
+ float pdf =
369
+ dpdf * diffuseWeight
370
+ + spdf * specularWeight
371
+ + tpdf * transmissionWeight
372
+ + cpdf * clearcoatWeight;
373
+
374
+ // retrieve specular rays for the shadows flag
375
+ specularPdf = spdf * specularWeight + cpdf * clearcoatWeight;
376
+
377
+ return pdf;
378
+
379
+ }
380
+
381
+ float bsdfResult( vec3 wo, vec3 clearcoatWo, vec3 wi, vec3 clearcoatWi, SurfaceRecord surf, out vec3 color ) {
382
+
383
+ if ( surf.volumeParticle ) {
384
+
385
+ color = surf.color / ( 4.0 * PI );
386
+ return 1.0 / ( 4.0 * PI );
387
+
388
+ }
389
+
390
+ vec3 wh = getHalfVector( wo, wi, surf.eta );
391
+ float diffuseWeight;
392
+ float specularWeight;
393
+ float transmissionWeight;
394
+ float clearcoatWeight;
395
+ getLobeWeights( wo, wi, wh, clearcoatWo, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
396
+
397
+ float specularPdf;
398
+ return bsdfEval( wo, clearcoatWo, wi, clearcoatWi, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight, specularPdf, color );
399
+
400
+ }
401
+
402
+ ScatterRecord bsdfSample( vec3 wo, vec3 clearcoatWo, mat3 normalBasis, mat3 invBasis, mat3 clearcoatNormalBasis, mat3 clearcoatInvBasis, SurfaceRecord surf ) {
403
+
404
+ if ( surf.volumeParticle ) {
405
+
406
+ vec3 wi = sampleSphere( sobol2( 16 ) );
407
+ vec3 wh = normalize( wo + wi );
408
+
409
+ ScatterRecord sampleRec;
410
+ sampleRec.specularPdf = 0.0;
411
+ sampleRec.pdf = 1.0 / ( 4.0 * PI );
412
+ sampleRec.direction = wi;
413
+ sampleRec.clearcoatDirection = wi;
414
+ sampleRec.color = surf.color / ( 4.0 * PI );
415
+ return sampleRec;
416
+
417
+ }
418
+
419
+ float diffuseWeight;
420
+ float specularWeight;
421
+ float transmissionWeight;
422
+ float clearcoatWeight;
423
+ // using normal and basically-reflected ray since we don't have proper half vector here
424
+ getLobeWeights( wo, wo, vec3( 0, 0, 1 ), clearcoatWo, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
425
+
426
+ float pdf[4];
427
+ pdf[0] = diffuseWeight;
428
+ pdf[1] = specularWeight;
429
+ pdf[2] = transmissionWeight;
430
+ pdf[3] = clearcoatWeight;
431
+
432
+ float cdf[4];
433
+ cdf[0] = pdf[0];
434
+ cdf[1] = pdf[1] + cdf[0];
435
+ cdf[2] = pdf[2] + cdf[1];
436
+ cdf[3] = pdf[3] + cdf[2];
437
+
438
+ if( cdf[3] != 0.0 ) {
439
+
440
+ float invMaxCdf = 1.0 / cdf[3];
441
+ cdf[0] *= invMaxCdf;
442
+ cdf[1] *= invMaxCdf;
443
+ cdf[2] *= invMaxCdf;
444
+ cdf[3] *= invMaxCdf;
445
+
446
+ } else {
447
+
448
+ cdf[0] = 1.0;
449
+ cdf[1] = 0.0;
450
+ cdf[2] = 0.0;
451
+ cdf[3] = 0.0;
452
+
453
+ }
454
+
455
+ vec3 wi;
456
+ vec3 clearcoatWi;
457
+
458
+ float r = sobol( 15 );
459
+ if ( r <= cdf[0] ) { // diffuse
460
+
461
+ wi = diffuseDirection( wo, surf );
462
+ clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
463
+
464
+ } else if ( r <= cdf[1] ) { // specular
465
+
466
+ wi = specularDirection( wo, surf );
467
+ clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
468
+
469
+ } else if ( r <= cdf[2] ) { // transmission / refraction
470
+
471
+ wi = transmissionDirection( wo, surf );
472
+ clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
473
+
474
+ } else if ( r <= cdf[3] ) { // clearcoat
475
+
476
+ clearcoatWi = clearcoatDirection( clearcoatWo, surf );
477
+ wi = normalize( invBasis * normalize( clearcoatNormalBasis * clearcoatWi ) );
478
+
479
+ }
480
+
481
+ ScatterRecord result;
482
+ result.pdf = bsdfEval( wo, clearcoatWo, wi, clearcoatWi, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight, result.specularPdf, result.color );
483
+ result.direction = wi;
484
+ result.clearcoatDirection = clearcoatWi;
485
+
486
+ return result;
487
+
488
+ }
489
+
490
+ `;
@@ -0,0 +1,23 @@
1
+ export const fogGLSL = /* glsl */`
2
+
3
+ // returns the hit distance given the material density
4
+ float intersectFogVolume( Material material, float u ) {
5
+
6
+ // https://raytracing.github.io/books/RayTracingTheNextWeek.html#volumes/constantdensitymediums
7
+ return material.opacity == 0.0 ? INFINITY : ( - 1.0 / material.opacity ) * log( u );
8
+
9
+ }
10
+
11
+ ScatterRecord sampleFogVolume( SurfaceRecord surf, vec2 uv ) {
12
+
13
+ ScatterRecord sampleRec;
14
+ sampleRec.specularPdf = 0.0;
15
+ sampleRec.pdf = 1.0 / ( 2.0 * PI );
16
+ sampleRec.direction = sampleSphere( uv );
17
+ sampleRec.clearcoatDirection = sampleRec.direction;
18
+ sampleRec.color = surf.color;
19
+ return sampleRec;
20
+
21
+ }
22
+
23
+ `;
@@ -0,0 +1,102 @@
1
+ export const ggxGLSL = /* glsl */`
2
+
3
+ // The GGX functions provide sampling and distribution information for normals as output so
4
+ // in order to get probability of scatter direction the half vector must be computed and provided.
5
+ // [0] https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf
6
+ // [1] https://hal.archives-ouvertes.fr/hal-01509746/document
7
+ // [2] http://jcgt.org/published/0007/04/01/
8
+ // [4] http://jcgt.org/published/0003/02/03/
9
+
10
+ // trowbridge-reitz === GGX === GTR
11
+
12
+ vec3 ggxDirection( vec3 incidentDir, vec2 roughness, vec2 uv ) {
13
+
14
+ // TODO: try GGXVNDF implementation from reference [2], here. Needs to update ggxDistribution
15
+ // function below, as well
16
+
17
+ // Implementation from reference [1]
18
+ // stretch view
19
+ vec3 V = normalize( vec3( roughness * incidentDir.xy, incidentDir.z ) );
20
+
21
+ // orthonormal basis
22
+ vec3 T1 = ( V.z < 0.9999 ) ? normalize( cross( V, vec3( 0.0, 0.0, 1.0 ) ) ) : vec3( 1.0, 0.0, 0.0 );
23
+ vec3 T2 = cross( T1, V );
24
+
25
+ // sample point with polar coordinates (r, phi)
26
+ float a = 1.0 / ( 1.0 + V.z );
27
+ float r = sqrt( uv.x );
28
+ float phi = ( uv.y < a ) ? uv.y / a * PI : PI + ( uv.y - a ) / ( 1.0 - a ) * PI;
29
+ float P1 = r * cos( phi );
30
+ float P2 = r * sin( phi ) * ( ( uv.y < a ) ? 1.0 : V.z );
31
+
32
+ // compute normal
33
+ vec3 N = P1 * T1 + P2 * T2 + V * sqrt( max( 0.0, 1.0 - P1 * P1 - P2 * P2 ) );
34
+
35
+ // unstretch
36
+ N = normalize( vec3( roughness * N.xy, max( 0.0, N.z ) ) );
37
+
38
+ return N;
39
+
40
+ }
41
+
42
+ // Below are PDF and related functions for use in a Monte Carlo path tracer
43
+ // as specified in Appendix B of the following paper
44
+ // See equation (34) from reference [0]
45
+ float ggxLamda( float theta, float roughness ) {
46
+
47
+ float tanTheta = tan( theta );
48
+ float tanTheta2 = tanTheta * tanTheta;
49
+ float alpha2 = roughness * roughness;
50
+
51
+ float numerator = - 1.0 + sqrt( 1.0 + alpha2 * tanTheta2 );
52
+ return numerator / 2.0;
53
+
54
+ }
55
+
56
+ // See equation (34) from reference [0]
57
+ float ggxShadowMaskG1( float theta, float roughness ) {
58
+
59
+ return 1.0 / ( 1.0 + ggxLamda( theta, roughness ) );
60
+
61
+ }
62
+
63
+ // See equation (125) from reference [4]
64
+ float ggxShadowMaskG2( vec3 wi, vec3 wo, float roughness ) {
65
+
66
+ float incidentTheta = acos( wi.z );
67
+ float scatterTheta = acos( wo.z );
68
+ return 1.0 / ( 1.0 + ggxLamda( incidentTheta, roughness ) + ggxLamda( scatterTheta, roughness ) );
69
+
70
+ }
71
+
72
+ // See equation (33) from reference [0]
73
+ float ggxDistribution( vec3 halfVector, float roughness ) {
74
+
75
+ float a2 = roughness * roughness;
76
+ a2 = max( EPSILON, a2 );
77
+ float cosTheta = halfVector.z;
78
+ float cosTheta4 = pow( cosTheta, 4.0 );
79
+
80
+ if ( cosTheta == 0.0 ) return 0.0;
81
+
82
+ float theta = acosSafe( halfVector.z );
83
+ float tanTheta = tan( theta );
84
+ float tanTheta2 = pow( tanTheta, 2.0 );
85
+
86
+ float denom = PI * cosTheta4 * pow( a2 + tanTheta2, 2.0 );
87
+ return ( a2 / denom );
88
+
89
+ }
90
+
91
+ // See equation (3) from reference [2]
92
+ float ggxPDF( vec3 wi, vec3 halfVector, float roughness ) {
93
+
94
+ float incidentTheta = acos( wi.z );
95
+ float D = ggxDistribution( halfVector, roughness );
96
+ float G1 = ggxShadowMaskG1( incidentTheta, roughness );
97
+
98
+ return D * G1 * max( 0.0, dot( wi, halfVector ) ) / wi.z;
99
+
100
+ }
101
+
102
+ `;