three-gpu-pathtracer 0.0.11 → 0.0.12

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 (53) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +886 -886
  3. package/build/index.module.js +6478 -6470
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +6473 -6465
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +72 -69
  8. package/src/core/DynamicPathTracingSceneGenerator.js +119 -119
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +275 -275
  11. package/src/core/PathTracingSceneGenerator.js +69 -69
  12. package/src/index.js +39 -39
  13. package/src/materials/AlphaDisplayMaterial.js +48 -48
  14. package/src/materials/AmbientOcclusionMaterial.js +199 -199
  15. package/src/materials/BlendMaterial.js +67 -67
  16. package/src/materials/DenoiseMaterial.js +142 -142
  17. package/src/materials/GraphMaterial.js +243 -243
  18. package/src/materials/LambertPathTracingMaterial.js +285 -285
  19. package/src/materials/MaterialBase.js +56 -56
  20. package/src/materials/PhysicalPathTracingMaterial.js +982 -982
  21. package/src/objects/EquirectCamera.js +13 -13
  22. package/src/objects/PhysicalCamera.js +28 -28
  23. package/src/objects/PhysicalSpotLight.js +14 -14
  24. package/src/objects/ShapedAreaLight.js +12 -12
  25. package/src/shader/shaderBvhAnyHit.js +76 -0
  26. package/src/shader/shaderEnvMapSampling.js +58 -58
  27. package/src/shader/shaderGGXFunctions.js +100 -100
  28. package/src/shader/shaderIridescenceFunctions.js +130 -130
  29. package/src/shader/shaderLayerTexelFetchFunctions.js +25 -25
  30. package/src/shader/shaderLightSampling.js +229 -229
  31. package/src/shader/shaderMaterialSampling.js +506 -498
  32. package/src/shader/shaderRandFunctions.js +57 -57
  33. package/src/shader/shaderSheenFunctions.js +98 -98
  34. package/src/shader/shaderSobolSampling.js +256 -256
  35. package/src/shader/shaderStructs.js +325 -325
  36. package/src/shader/shaderUtils.js +361 -361
  37. package/src/textures/GradientEquirectTexture.js +35 -35
  38. package/src/textures/ProceduralEquirectTexture.js +75 -75
  39. package/src/uniforms/AttributesTextureArray.js +35 -35
  40. package/src/uniforms/EquirectHdrInfoUniform.js +259 -259
  41. package/src/uniforms/FloatAttributeTextureArray.js +169 -169
  42. package/src/uniforms/IESProfilesTexture.js +100 -100
  43. package/src/uniforms/LightsInfoUniformStruct.js +207 -207
  44. package/src/uniforms/MaterialsTexture.js +426 -426
  45. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  46. package/src/uniforms/RenderTarget2DArray.js +97 -97
  47. package/src/uniforms/utils.js +30 -30
  48. package/src/utils/BlurredEnvMapGenerator.js +116 -116
  49. package/src/utils/GeometryPreparationUtils.js +214 -214
  50. package/src/utils/IESLoader.js +325 -325
  51. package/src/utils/SobolNumberMapGenerator.js +80 -80
  52. package/src/utils/UVUnwrapper.js +101 -101
  53. package/src/workers/PathTracingSceneWorker.js +42 -42
@@ -1,498 +1,506 @@
1
- import { shaderGGXFunctions } from './shaderGGXFunctions.js';
2
- import { shaderSheenFunctions } from './shaderSheenFunctions.js';
3
- import { shaderIridescenceFunctions } from './shaderIridescenceFunctions.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
- */
14
-
15
- export const shaderMaterialSampling = /* glsl */`
16
-
17
- struct SurfaceRec {
18
- vec3 normal;
19
- vec3 faceNormal;
20
- bool frontFace;
21
- float roughness;
22
- float filteredRoughness;
23
- float metalness;
24
- vec3 color;
25
- vec3 emission;
26
- float transmission;
27
- bool thinFilm;
28
- float ior;
29
- float eta;
30
- float f0;
31
- float clearcoat;
32
- float clearcoatRoughness;
33
- float filteredClearcoatRoughness;
34
- vec3 sheenColor;
35
- float sheenRoughness;
36
- float iridescence;
37
- float iridescenceIor;
38
- float iridescenceThickness;
39
- vec3 specularColor;
40
- float specularIntensity;
41
- vec3 attenuationColor;
42
- float attenuationDistance;
43
- };
44
-
45
- struct SampleRec {
46
- float specularPdf;
47
- float pdf;
48
- vec3 direction;
49
- vec3 clearcoatDirection;
50
- vec3 color;
51
- };
52
-
53
- ${ shaderGGXFunctions }
54
- ${ shaderSheenFunctions }
55
- ${ shaderIridescenceFunctions }
56
-
57
- float disneyFresnel( SurfaceRec surf, vec3 wo, vec3 wi, vec3 wh ) {
58
-
59
- float dotHV = dot( wo, wh );
60
- float dotHL = dot( wi, wh );
61
-
62
- // TODO: some model-viewer test models look better when surf.eta is set to a non 1.5 eta here here?
63
- // and the furnace test seems to pass when it === 1.0
64
- // float dielectricFresnel = dielectricFresnel( abs( dotHV ), surf.eta );
65
- float dielectricFresnel = dielectricFresnel( abs( dotHV ), 1.0 / 1.1 );
66
- float metallicFresnel = schlickFresnel( dotHL, surf.f0 );
67
-
68
- return mix( dielectricFresnel, metallicFresnel, surf.metalness );
69
-
70
- }
71
-
72
- // diffuse
73
- float diffuseEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, out vec3 color ) {
74
-
75
- // https://schuttejoe.github.io/post/disneybsdf/
76
- float fl = schlickFresnel( wi.z, 0.0 );
77
- float fv = schlickFresnel( wo.z, 0.0 );
78
-
79
- float metalFactor = ( 1.0 - surf.metalness );
80
- float transFactor = ( 1.0 - surf.transmission );
81
- float rr = 0.5 + 2.0 * surf.roughness * fl * fl;
82
- float retro = rr * ( fl + fv + fl * fv * ( rr - 1.0f ) );
83
- float lambert = ( 1.0f - 0.5f * fl ) * ( 1.0f - 0.5f * fv );
84
-
85
- // TODO: subsurface approx?
86
-
87
- float FM = disneyFresnel( surf, wo, wi, wh );
88
-
89
- color = ( 1.0 - FM ) * transFactor * metalFactor * wi.z * surf.color * ( retro + lambert ) / PI;
90
- return wi.z / PI;
91
-
92
- }
93
-
94
- vec3 diffuseDirection( vec3 wo, SurfaceRec surf ) {
95
-
96
- vec3 lightDirection = sampleSphere( sobol2( 11 ) );
97
- lightDirection.z += 1.0;
98
- lightDirection = normalize( lightDirection );
99
-
100
- return lightDirection;
101
-
102
- }
103
-
104
- // specular
105
- float specularEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, out vec3 color ) {
106
-
107
- // if roughness is set to 0 then D === NaN which results in black pixels
108
- float metalness = surf.metalness;
109
- float filteredRoughness = surf.filteredRoughness;
110
-
111
- float eta = surf.eta;
112
- float f0 = surf.f0;
113
- float G = ggxShadowMaskG2( wi, wo, filteredRoughness );
114
- float D = ggxDistribution( wh, filteredRoughness );
115
- float FM = disneyFresnel( surf, wo, wi, wh );
116
- float cosTheta = min( wo.z, 1.0 );
117
- float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
118
- bool cannotRefract = eta * sinTheta > 1.0;
119
- if ( cannotRefract ) {
120
-
121
- FM = 1.0;
122
-
123
- }
124
-
125
- vec3 metalColor = surf.color;
126
- vec3 dielectricColor = f0 * surf.specularColor;
127
- vec3 specColor = mix( dielectricColor, metalColor, surf.metalness );
128
-
129
- vec3 iridescenceF = evalIridescence( 1.0, surf.iridescenceIor, dot( wi, wh ), surf.iridescenceThickness, vec3( f0 ) );
130
- vec3 iridescenceMix = mix( vec3( FM ), iridescenceF, surf.iridescence );
131
- vec3 F = mix( specColor, vec3( 1.0 ), iridescenceMix );
132
-
133
- color = mix( surf.specularIntensity, 1.0, surf.metalness ) * wi.z * F * G * D / ( 4.0 * abs( wi.z * wo.z ) );
134
-
135
- // PDF
136
- // See 14.1.1 Microfacet BxDFs in https://www.pbr-book.org/
137
- float incidentTheta = acos( wo.z );
138
- float G1 = ggxShadowMaskG1( incidentTheta, filteredRoughness );
139
- float ggxPdf = D * G1 * max( 0.0, abs( dot( wo, wh ) ) ) / abs ( wo.z );
140
- return ggxPdf / ( 4.0 * dot( wo, wh ) );
141
-
142
- }
143
-
144
- vec3 specularDirection( vec3 wo, SurfaceRec surf ) {
145
-
146
- // sample ggx vndf distribution which gives a new normal
147
- float filteredRoughness = surf.filteredRoughness;
148
- vec3 halfVector = ggxDirection(
149
- wo,
150
- vec2( filteredRoughness ),
151
- sobol2( 12 )
152
- );
153
-
154
- // apply to new ray by reflecting off the new normal
155
- return - reflect( wo, halfVector );
156
-
157
- }
158
-
159
-
160
- // transmission
161
- /*
162
- float transmissionEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, out vec3 color ) {
163
-
164
- // See section 4.2 in https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf
165
-
166
- float filteredRoughness = surf.filteredRoughness;
167
- float eta = surf.eta;
168
- bool frontFace = surf.frontFace;
169
- bool thinFilm = surf.thinFilm;
170
-
171
- vec3 col = thinFilm || frontFace ? surf.color : vec3( 1.0 );
172
- color = surf.transmission * col;
173
-
174
- float denom = pow( eta * dot( wi, wh ) + dot( wo, wh ), 2.0 );
175
- return ggxPDF( wo, wh, filteredRoughness ) / denom;
176
-
177
- }
178
-
179
- vec3 transmissionDirection( vec3 wo, SurfaceRec surf ) {
180
-
181
- float filteredRoughness = surf.filteredRoughness;
182
- float eta = surf.eta;
183
- bool frontFace = surf.frontFace;
184
-
185
- // sample ggx vndf distribution which gives a new normal
186
- vec3 halfVector = ggxDirection(
187
- wo,
188
- vec2( filteredRoughness ),
189
- sobol2( 13 )
190
- );
191
-
192
-
193
- // TODO: support thin film
194
- vec3 lightDirection = refract( normalize( - wo ), halfVector, eta );
195
- return normalize( lightDirection );
196
-
197
- }
198
- */
199
-
200
- // TODO: This is just using a basic cosine-weighted specular distribution with an
201
- // incorrect PDF value at the moment. Update it to correctly use a GGX distribution
202
- float transmissionEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, out vec3 color ) {
203
-
204
- // only attenuate the color if it's on the way in
205
- vec3 col = surf.thinFilm || surf.frontFace ? surf.color : vec3( 1.0 );
206
- color = surf.transmission * col;
207
-
208
- // PDF
209
- float eta = surf.eta;
210
- float f0 = surf.f0;
211
- float cosTheta = min( wo.z, 1.0 );
212
- float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
213
- float reflectance = schlickFresnel( cosTheta, f0 );
214
- bool cannotRefract = eta * sinTheta > 1.0;
215
- if ( cannotRefract ) {
216
-
217
- return 0.0;
218
-
219
- }
220
-
221
- return 1.0 / ( 1.0 - reflectance );
222
-
223
- }
224
-
225
- vec3 transmissionDirection( vec3 wo, SurfaceRec surf ) {
226
-
227
- float roughness = surf.roughness;
228
- float eta = surf.eta;
229
- vec3 halfVector = normalize( vec3( 0.0, 0.0, 1.0 ) + sampleSphere( sobol2( 13 ) ) * roughness );
230
- vec3 lightDirection = refract( normalize( - wo ), halfVector, eta );
231
-
232
- if ( surf.thinFilm ) {
233
-
234
- lightDirection = - refract( normalize( - lightDirection ), - vec3( 0.0, 0.0, 1.0 ), 1.0 / eta );
235
-
236
- }
237
- return normalize( lightDirection );
238
-
239
- }
240
-
241
- // clearcoat
242
- float clearcoatEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, inout vec3 color ) {
243
-
244
- float ior = 1.5;
245
- float f0 = iorRatioToF0( ior );
246
- bool frontFace = surf.frontFace;
247
- float filteredClearcoatRoughness = surf.filteredClearcoatRoughness;
248
-
249
- float eta = frontFace ? 1.0 / ior : ior;
250
- float G = ggxShadowMaskG2( wi, wo, filteredClearcoatRoughness );
251
- float D = ggxDistribution( wh, filteredClearcoatRoughness );
252
- float F = schlickFresnel( dot( wi, wh ), f0 );
253
- float cosTheta = min( wo.z, 1.0 );
254
- float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
255
- bool cannotRefract = eta * sinTheta > 1.0;
256
- if ( cannotRefract ) {
257
-
258
- F = 1.0;
259
-
260
- }
261
-
262
- float fClearcoat = F * D * G / ( 4.0 * abs( wi.z * wo.z ) );
263
- color = color * ( 1.0 - surf.clearcoat * F ) + fClearcoat * surf.clearcoat * wi.z;
264
-
265
- // PDF
266
- // See equation (27) in http://jcgt.org/published/0003/02/03/
267
- return ggxPDF( wo, wh, filteredClearcoatRoughness ) / ( 4.0 * dot( wi, wh ) );
268
-
269
- }
270
-
271
- vec3 clearcoatDirection( vec3 wo, SurfaceRec surf ) {
272
-
273
- // sample ggx vndf distribution which gives a new normal
274
- float filteredClearcoatRoughness = surf.filteredClearcoatRoughness;
275
- vec3 halfVector = ggxDirection(
276
- wo,
277
- vec2( filteredClearcoatRoughness ),
278
- sobol2( 14 )
279
- );
280
-
281
- // apply to new ray by reflecting off the new normal
282
- return - reflect( wo, halfVector );
283
-
284
- }
285
-
286
- // sheen
287
- vec3 sheenColor( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf ) {
288
-
289
- float cosThetaO = saturateCos( wo.z );
290
- float cosThetaI = saturateCos( wi.z );
291
- float cosThetaH = wh.z;
292
-
293
- float D = velvetD( cosThetaH, surf.sheenRoughness );
294
- float G = velvetG( cosThetaO, cosThetaI, surf.sheenRoughness );
295
-
296
- // See equation (1) in http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf
297
- vec3 color = surf.sheenColor;
298
- color *= D * G / ( 4.0 * abs( cosThetaO * cosThetaI ) );
299
- color *= wi.z;
300
-
301
- return color;
302
-
303
- }
304
-
305
- // bsdf
306
- #define DIFF_WEIGHT 0
307
- #define SPEC_WEIGHT 1
308
- #define TRANS_WEIGHT 2
309
- #define CC_WEIGHT 3
310
- void getLobeWeights( vec3 wo, vec3 wi, vec3 wh, vec3 clearcoatWo, SurfaceRec surf, out float[ 4 ] weights ) {
311
-
312
- float metalness = surf.metalness;
313
- float transmission = surf.transmission;
314
-
315
- float eta = surf.eta;
316
- float f0 = surf.f0;
317
- float cosTheta = min( wo.z, 1.0 );
318
- float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
319
-
320
- // TODO: does "cannot refract" belong in disney fresnel?
321
- float reflectance = disneyFresnel( surf, wo, wi, wh );
322
- bool cannotRefract = eta * sinTheta > 1.0;
323
- if ( cannotRefract ) {
324
-
325
- reflectance = 1.0;
326
-
327
- }
328
-
329
- float transSpecularProb = mix( max( 0.25, reflectance ), 1.0, metalness );
330
- float diffSpecularProb = 0.5 + 0.5 * metalness;
331
-
332
- float diffuseWeight = ( 1.0 - transmission ) * ( 1.0 - diffSpecularProb );
333
- float specularWeight = transmission * transSpecularProb + ( 1.0 - transmission ) * diffSpecularProb;
334
- float transmissionWeight = transmission * ( 1.0 - transSpecularProb );
335
- float clearcoatWeight = surf.clearcoat * schlickFresnel( clearcoatWo.z, 0.04 );
336
-
337
- float totalWeight = diffuseWeight + specularWeight + transmissionWeight + clearcoatWeight;
338
- weights[ DIFF_WEIGHT ] = diffuseWeight / totalWeight;
339
- weights[ SPEC_WEIGHT ] = specularWeight / totalWeight;
340
- weights[ TRANS_WEIGHT ] = transmissionWeight / totalWeight;
341
- weights[ CC_WEIGHT ] = clearcoatWeight / totalWeight;
342
-
343
- }
344
-
345
- float bsdfEval( vec3 wo, vec3 clearcoatWo, vec3 wi, vec3 clearcoatWi, SurfaceRec surf, float[ 4 ] weights, out float specularPdf, out vec3 color ) {
346
-
347
- float diffuseWeight = weights[ DIFF_WEIGHT ];
348
- float specularWeight = weights[ SPEC_WEIGHT ];
349
- float transmissionWeight = weights[ TRANS_WEIGHT ];
350
- float clearcoatWeight = weights[ CC_WEIGHT ];
351
-
352
- float metalness = surf.metalness;
353
- float transmission = surf.transmission;
354
-
355
- float eta = surf.eta;
356
- float f0 = surf.f0;
357
- float cosTheta = min( wo.z, 1.0 );
358
- float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
359
- float reflectance = schlickFresnel( cosTheta, f0 );
360
- bool cannotRefract = eta * sinTheta > 1.0;
361
- if ( cannotRefract ) {
362
-
363
- reflectance = 1.0;
364
-
365
- }
366
-
367
- float spdf = 0.0;
368
- float dpdf = 0.0;
369
- float tpdf = 0.0;
370
- float cpdf = 0.0;
371
- color = vec3( 0.0 );
372
-
373
- vec3 halfVector = getHalfVector( wi, wo, surf.eta );
374
-
375
- // diffuse
376
- if ( diffuseWeight > 0.0 && wi.z > 0.0 ) {
377
-
378
- dpdf = diffuseEval( wo, wi, halfVector, surf, color );
379
- color *= 1.0 - surf.transmission;
380
-
381
- }
382
-
383
- // ggx specular
384
- if ( specularWeight > 0.0 && wi.z > 0.0 ) {
385
-
386
- vec3 outColor;
387
- spdf = specularEval( wo, wi, getHalfVector( wi, wo ), surf, outColor );
388
- color += outColor;
389
-
390
- }
391
-
392
- // transmission
393
- if ( transmissionWeight > 0.0 && wi.z < 0.0 ) {
394
-
395
- tpdf = transmissionEval( wo, wi, halfVector, surf, color );
396
-
397
- }
398
-
399
- // sheen
400
- color *= sheenAlbedoScaling( wo, wi, surf );
401
- color += sheenColor( wo, wi, halfVector, surf );
402
-
403
- // clearcoat
404
- if ( clearcoatWi.z >= 0.0 && clearcoatWeight > 0.0 ) {
405
-
406
- vec3 clearcoatHalfVector = getHalfVector( clearcoatWo, clearcoatWi );
407
- cpdf = clearcoatEval( clearcoatWo, clearcoatWi, clearcoatHalfVector, surf, color );
408
-
409
- }
410
-
411
- float pdf =
412
- dpdf * diffuseWeight
413
- + spdf * specularWeight
414
- + tpdf * transmissionWeight
415
- + cpdf * clearcoatWeight;
416
-
417
- // retrieve specular rays for the shadows flag
418
- specularPdf = spdf * specularWeight + cpdf * clearcoatWeight;
419
-
420
- return pdf;
421
-
422
- }
423
-
424
- float bsdfResult( vec3 wo, vec3 clearcoatWo, vec3 wi, vec3 clearcoatWi, SurfaceRec surf, out vec3 color ) {
425
-
426
- float[ 4 ] pdf;
427
- vec3 wh = getHalfVector( wo, wi, surf.eta );
428
- getLobeWeights( wo, wi, wh, clearcoatWo, surf, pdf );
429
-
430
- float specularPdf;
431
- return bsdfEval( wo, clearcoatWo, wi, clearcoatWi, surf, pdf, specularPdf, color );
432
-
433
- }
434
-
435
- SampleRec bsdfSample( vec3 wo, vec3 clearcoatWo, mat3 normalBasis, mat3 invBasis, mat3 clearcoatNormalBasis, mat3 clearcoatInvBasis, SurfaceRec surf ) {
436
-
437
- // using normal and basically-reflected ray since we don't have proper half vector here
438
- float pdf[4];
439
- getLobeWeights( wo, wo, vec3( 0, 0, 1 ), clearcoatWo, surf, pdf );
440
-
441
- float cdf[4];
442
- cdf[0] = pdf[0];
443
- cdf[1] = pdf[1] + cdf[0];
444
- cdf[2] = pdf[2] + cdf[1];
445
- cdf[3] = pdf[3] + cdf[2];
446
-
447
- if( cdf[3] != 0.0 ) {
448
-
449
- float invMaxCdf = 1.0 / cdf[3];
450
- cdf[0] *= invMaxCdf;
451
- cdf[1] *= invMaxCdf;
452
- cdf[2] *= invMaxCdf;
453
- cdf[3] *= invMaxCdf;
454
-
455
- } else {
456
-
457
- cdf[0] = 1.0;
458
- cdf[1] = 0.0;
459
- cdf[2] = 0.0;
460
- cdf[3] = 0.0;
461
-
462
- }
463
-
464
- vec3 wi;
465
- vec3 clearcoatWi;
466
-
467
- float r = sobol( 15 );
468
- if ( r <= cdf[0] ) { // diffuse
469
-
470
- wi = diffuseDirection( wo, surf );
471
- clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
472
-
473
- } else if ( r <= cdf[1] ) { // specular
474
-
475
- wi = specularDirection( wo, surf );
476
- clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
477
-
478
- } else if ( r <= cdf[2] ) { // transmission / refraction
479
-
480
- wi = transmissionDirection( wo, surf );
481
- clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
482
-
483
- } else if ( r <= cdf[3] ) { // clearcoat
484
-
485
- clearcoatWi = clearcoatDirection( clearcoatWo, surf );
486
- wi = normalize( invBasis * normalize( clearcoatNormalBasis * clearcoatWi ) );
487
-
488
- }
489
-
490
- SampleRec result;
491
- result.pdf = bsdfEval( wo, clearcoatWo, wi, clearcoatWi, surf, pdf, result.specularPdf, result.color );
492
- result.direction = wi;
493
- result.clearcoatDirection = clearcoatWi;
494
-
495
- return result;
496
-
497
- }
498
- `;
1
+ import { shaderGGXFunctions } from './shaderGGXFunctions.js';
2
+ import { shaderSheenFunctions } from './shaderSheenFunctions.js';
3
+ import { shaderIridescenceFunctions } from './shaderIridescenceFunctions.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
+ */
14
+
15
+ export const shaderMaterialSampling = /* glsl */`
16
+
17
+ struct SurfaceRec {
18
+ vec3 normal;
19
+ vec3 faceNormal;
20
+ bool frontFace;
21
+ float roughness;
22
+ float filteredRoughness;
23
+ float metalness;
24
+ vec3 color;
25
+ vec3 emission;
26
+ float transmission;
27
+ bool thinFilm;
28
+ float ior;
29
+ float eta;
30
+ float f0;
31
+ float clearcoat;
32
+ float clearcoatRoughness;
33
+ float filteredClearcoatRoughness;
34
+ vec3 sheenColor;
35
+ float sheenRoughness;
36
+ float iridescence;
37
+ float iridescenceIor;
38
+ float iridescenceThickness;
39
+ vec3 specularColor;
40
+ float specularIntensity;
41
+ vec3 attenuationColor;
42
+ float attenuationDistance;
43
+ };
44
+
45
+ struct SampleRec {
46
+ float specularPdf;
47
+ float pdf;
48
+ vec3 direction;
49
+ vec3 clearcoatDirection;
50
+ vec3 color;
51
+ };
52
+
53
+ ${ shaderGGXFunctions }
54
+ ${ shaderSheenFunctions }
55
+ ${ shaderIridescenceFunctions }
56
+
57
+ float disneyFresnel( SurfaceRec surf, vec3 wo, vec3 wi, vec3 wh ) {
58
+
59
+ float dotHV = dot( wo, wh );
60
+ float dotHL = dot( wi, wh );
61
+
62
+ // TODO: some model-viewer test models look better when surf.eta is set to a non 1.5 eta here here?
63
+ // and the furnace test seems to pass when it === 1.0
64
+ // float dielectricFresnel = dielectricFresnel( abs( dotHV ), surf.eta );
65
+ float dielectricFresnel = dielectricFresnel( abs( dotHV ), 1.0 / 1.1 );
66
+ float metallicFresnel = schlickFresnel( dotHL, surf.f0 );
67
+
68
+ return mix( dielectricFresnel, metallicFresnel, surf.metalness );
69
+
70
+ }
71
+
72
+ // diffuse
73
+ float diffuseEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, out vec3 color ) {
74
+
75
+ // https://schuttejoe.github.io/post/disneybsdf/
76
+ float fl = schlickFresnel( wi.z, 0.0 );
77
+ float fv = schlickFresnel( wo.z, 0.0 );
78
+
79
+ float metalFactor = ( 1.0 - surf.metalness );
80
+ float transFactor = ( 1.0 - surf.transmission );
81
+ float rr = 0.5 + 2.0 * surf.roughness * fl * fl;
82
+ float retro = rr * ( fl + fv + fl * fv * ( rr - 1.0f ) );
83
+ float lambert = ( 1.0f - 0.5f * fl ) * ( 1.0f - 0.5f * fv );
84
+
85
+ // TODO: subsurface approx?
86
+
87
+ float FM = disneyFresnel( surf, wo, wi, wh );
88
+
89
+ color = ( 1.0 - FM ) * transFactor * metalFactor * wi.z * surf.color * ( retro + lambert ) / PI;
90
+ return wi.z / PI;
91
+
92
+ }
93
+
94
+ vec3 diffuseDirection( vec3 wo, SurfaceRec surf ) {
95
+
96
+ vec3 lightDirection = sampleSphere( sobol2( 11 ) );
97
+ lightDirection.z += 1.0;
98
+ lightDirection = normalize( lightDirection );
99
+
100
+ return lightDirection;
101
+
102
+ }
103
+
104
+ // specular
105
+ float specularEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, out vec3 color ) {
106
+
107
+ // if roughness is set to 0 then D === NaN which results in black pixels
108
+ float metalness = surf.metalness;
109
+ float filteredRoughness = surf.filteredRoughness;
110
+
111
+ float eta = surf.eta;
112
+ float f0 = surf.f0;
113
+ float G = ggxShadowMaskG2( wi, wo, filteredRoughness );
114
+ float D = ggxDistribution( wh, filteredRoughness );
115
+ float FM = disneyFresnel( surf, wo, wi, wh );
116
+ float cosTheta = min( wo.z, 1.0 );
117
+ float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
118
+ bool cannotRefract = eta * sinTheta > 1.0;
119
+ if ( cannotRefract ) {
120
+
121
+ FM = 1.0;
122
+
123
+ }
124
+
125
+ vec3 metalColor = surf.color;
126
+ vec3 dielectricColor = f0 * surf.specularColor;
127
+ vec3 specColor = mix( dielectricColor, metalColor, surf.metalness );
128
+
129
+ vec3 iridescenceF = evalIridescence( 1.0, surf.iridescenceIor, dot( wi, wh ), surf.iridescenceThickness, vec3( f0 ) );
130
+ vec3 iridescenceMix = mix( vec3( FM ), iridescenceF, surf.iridescence );
131
+ vec3 F = mix( specColor, vec3( 1.0 ), iridescenceMix );
132
+
133
+ color = mix( surf.specularIntensity, 1.0, surf.metalness ) * wi.z * F * G * D / ( 4.0 * abs( wi.z * wo.z ) );
134
+
135
+ // PDF
136
+ // See 14.1.1 Microfacet BxDFs in https://www.pbr-book.org/
137
+ float incidentTheta = acos( wo.z );
138
+ float G1 = ggxShadowMaskG1( incidentTheta, filteredRoughness );
139
+ float ggxPdf = D * G1 * max( 0.0, abs( dot( wo, wh ) ) ) / abs ( wo.z );
140
+ return ggxPdf / ( 4.0 * dot( wo, wh ) );
141
+
142
+ }
143
+
144
+ vec3 specularDirection( vec3 wo, SurfaceRec surf ) {
145
+
146
+ // sample ggx vndf distribution which gives a new normal
147
+ float filteredRoughness = surf.filteredRoughness;
148
+ vec3 halfVector = ggxDirection(
149
+ wo,
150
+ vec2( filteredRoughness ),
151
+ sobol2( 12 )
152
+ );
153
+
154
+ // apply to new ray by reflecting off the new normal
155
+ return - reflect( wo, halfVector );
156
+
157
+ }
158
+
159
+
160
+ // transmission
161
+ /*
162
+ float transmissionEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, out vec3 color ) {
163
+
164
+ // See section 4.2 in https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf
165
+
166
+ float filteredRoughness = surf.filteredRoughness;
167
+ float eta = surf.eta;
168
+ bool frontFace = surf.frontFace;
169
+ bool thinFilm = surf.thinFilm;
170
+
171
+ vec3 col = thinFilm || frontFace ? surf.color : vec3( 1.0 );
172
+ color = surf.transmission * col;
173
+
174
+ float denom = pow( eta * dot( wi, wh ) + dot( wo, wh ), 2.0 );
175
+ return ggxPDF( wo, wh, filteredRoughness ) / denom;
176
+
177
+ }
178
+
179
+ vec3 transmissionDirection( vec3 wo, SurfaceRec surf ) {
180
+
181
+ float filteredRoughness = surf.filteredRoughness;
182
+ float eta = surf.eta;
183
+ bool frontFace = surf.frontFace;
184
+
185
+ // sample ggx vndf distribution which gives a new normal
186
+ vec3 halfVector = ggxDirection(
187
+ wo,
188
+ vec2( filteredRoughness ),
189
+ sobol2( 13 )
190
+ );
191
+
192
+
193
+ // TODO: support thin film
194
+ vec3 lightDirection = refract( normalize( - wo ), halfVector, eta );
195
+ return normalize( lightDirection );
196
+
197
+ }
198
+ */
199
+
200
+ // TODO: This is just using a basic cosine-weighted specular distribution with an
201
+ // incorrect PDF value at the moment. Update it to correctly use a GGX distribution
202
+ float transmissionEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, out vec3 color ) {
203
+
204
+ // only attenuate the color if it's on the way in
205
+ vec3 col = surf.thinFilm || surf.frontFace ? surf.color : vec3( 1.0 );
206
+ color = surf.transmission * col;
207
+
208
+ // PDF
209
+ float eta = surf.eta;
210
+ float f0 = surf.f0;
211
+ float cosTheta = min( wo.z, 1.0 );
212
+ float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
213
+ float reflectance = schlickFresnel( cosTheta, f0 );
214
+ bool cannotRefract = eta * sinTheta > 1.0;
215
+ if ( cannotRefract ) {
216
+
217
+ return 0.0;
218
+
219
+ }
220
+
221
+ return 1.0 / ( 1.0 - reflectance );
222
+
223
+ }
224
+
225
+ vec3 transmissionDirection( vec3 wo, SurfaceRec surf ) {
226
+
227
+ float roughness = surf.roughness;
228
+ float eta = surf.eta;
229
+ vec3 halfVector = normalize( vec3( 0.0, 0.0, 1.0 ) + sampleSphere( sobol2( 13 ) ) * roughness );
230
+ vec3 lightDirection = refract( normalize( - wo ), halfVector, eta );
231
+
232
+ if ( surf.thinFilm ) {
233
+
234
+ lightDirection = - refract( normalize( - lightDirection ), - vec3( 0.0, 0.0, 1.0 ), 1.0 / eta );
235
+
236
+ }
237
+ return normalize( lightDirection );
238
+
239
+ }
240
+
241
+ // clearcoat
242
+ float clearcoatEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf, inout vec3 color ) {
243
+
244
+ float ior = 1.5;
245
+ float f0 = iorRatioToF0( ior );
246
+ bool frontFace = surf.frontFace;
247
+ float filteredClearcoatRoughness = surf.filteredClearcoatRoughness;
248
+
249
+ float eta = frontFace ? 1.0 / ior : ior;
250
+ float G = ggxShadowMaskG2( wi, wo, filteredClearcoatRoughness );
251
+ float D = ggxDistribution( wh, filteredClearcoatRoughness );
252
+ float F = schlickFresnel( dot( wi, wh ), f0 );
253
+ float cosTheta = min( wo.z, 1.0 );
254
+ float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
255
+ bool cannotRefract = eta * sinTheta > 1.0;
256
+ if ( cannotRefract ) {
257
+
258
+ F = 1.0;
259
+
260
+ }
261
+
262
+ float fClearcoat = F * D * G / ( 4.0 * abs( wi.z * wo.z ) );
263
+ color = color * ( 1.0 - surf.clearcoat * F ) + fClearcoat * surf.clearcoat * wi.z;
264
+
265
+ // PDF
266
+ // See equation (27) in http://jcgt.org/published/0003/02/03/
267
+ return ggxPDF( wo, wh, filteredClearcoatRoughness ) / ( 4.0 * dot( wi, wh ) );
268
+
269
+ }
270
+
271
+ vec3 clearcoatDirection( vec3 wo, SurfaceRec surf ) {
272
+
273
+ // sample ggx vndf distribution which gives a new normal
274
+ float filteredClearcoatRoughness = surf.filteredClearcoatRoughness;
275
+ vec3 halfVector = ggxDirection(
276
+ wo,
277
+ vec2( filteredClearcoatRoughness ),
278
+ sobol2( 14 )
279
+ );
280
+
281
+ // apply to new ray by reflecting off the new normal
282
+ return - reflect( wo, halfVector );
283
+
284
+ }
285
+
286
+ // sheen
287
+ vec3 sheenColor( vec3 wo, vec3 wi, vec3 wh, SurfaceRec surf ) {
288
+
289
+ float cosThetaO = saturateCos( wo.z );
290
+ float cosThetaI = saturateCos( wi.z );
291
+ float cosThetaH = wh.z;
292
+
293
+ float D = velvetD( cosThetaH, surf.sheenRoughness );
294
+ float G = velvetG( cosThetaO, cosThetaI, surf.sheenRoughness );
295
+
296
+ // See equation (1) in http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf
297
+ vec3 color = surf.sheenColor;
298
+ color *= D * G / ( 4.0 * abs( cosThetaO * cosThetaI ) );
299
+ color *= wi.z;
300
+
301
+ return color;
302
+
303
+ }
304
+
305
+ // bsdf
306
+ void getLobeWeights(
307
+ vec3 wo, vec3 wi, vec3 wh, vec3 clearcoatWo, SurfaceRec surf,
308
+ out float diffuseWeight, out float specularWeight, out float transmissionWeight, out float clearcoatWeight
309
+ ) {
310
+
311
+ float metalness = surf.metalness;
312
+ float transmission = surf.transmission;
313
+
314
+ float eta = surf.eta;
315
+ float f0 = surf.f0;
316
+ float cosTheta = min( wo.z, 1.0 );
317
+ float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
318
+
319
+ // TODO: does "cannot refract" belong in disney fresnel?
320
+ float reflectance = disneyFresnel( surf, wo, wi, wh );
321
+ bool cannotRefract = eta * sinTheta > 1.0;
322
+ if ( cannotRefract ) {
323
+
324
+ reflectance = 1.0;
325
+
326
+ }
327
+
328
+ float transSpecularProb = mix( max( 0.25, reflectance ), 1.0, metalness );
329
+ float diffSpecularProb = 0.5 + 0.5 * metalness;
330
+
331
+ diffuseWeight = ( 1.0 - transmission ) * ( 1.0 - diffSpecularProb );
332
+ specularWeight = transmission * transSpecularProb + ( 1.0 - transmission ) * diffSpecularProb;
333
+ transmissionWeight = transmission * ( 1.0 - transSpecularProb );
334
+ clearcoatWeight = surf.clearcoat * schlickFresnel( clearcoatWo.z, 0.04 );
335
+
336
+ float totalWeight = diffuseWeight + specularWeight + transmissionWeight + clearcoatWeight;
337
+ diffuseWeight /= totalWeight;
338
+ specularWeight /= totalWeight;
339
+ transmissionWeight /= totalWeight;
340
+ clearcoatWeight /= totalWeight;
341
+ }
342
+
343
+ float bsdfEval(
344
+ vec3 wo, vec3 clearcoatWo, vec3 wi, vec3 clearcoatWi, SurfaceRec surf,
345
+ float diffuseWeight, float specularWeight, float transmissionWeight, float clearcoatWeight, out float specularPdf, out vec3 color
346
+ ) {
347
+
348
+ float metalness = surf.metalness;
349
+ float transmission = surf.transmission;
350
+
351
+ float eta = surf.eta;
352
+ float f0 = surf.f0;
353
+ float cosTheta = min( wo.z, 1.0 );
354
+ float sinTheta = sqrt( 1.0 - cosTheta * cosTheta );
355
+ float reflectance = schlickFresnel( cosTheta, f0 );
356
+ bool cannotRefract = eta * sinTheta > 1.0;
357
+ if ( cannotRefract ) {
358
+
359
+ reflectance = 1.0;
360
+
361
+ }
362
+
363
+ float spdf = 0.0;
364
+ float dpdf = 0.0;
365
+ float tpdf = 0.0;
366
+ float cpdf = 0.0;
367
+ color = vec3( 0.0 );
368
+
369
+ vec3 halfVector = getHalfVector( wi, wo, surf.eta );
370
+
371
+ // diffuse
372
+ if ( diffuseWeight > 0.0 && wi.z > 0.0 ) {
373
+
374
+ dpdf = diffuseEval( wo, wi, halfVector, surf, color );
375
+ color *= 1.0 - surf.transmission;
376
+
377
+ }
378
+
379
+ // ggx specular
380
+ if ( specularWeight > 0.0 && wi.z > 0.0 ) {
381
+
382
+ vec3 outColor;
383
+ spdf = specularEval( wo, wi, getHalfVector( wi, wo ), surf, outColor );
384
+ color += outColor;
385
+
386
+ }
387
+
388
+ // transmission
389
+ if ( transmissionWeight > 0.0 && wi.z < 0.0 ) {
390
+
391
+ tpdf = transmissionEval( wo, wi, halfVector, surf, color );
392
+
393
+ }
394
+
395
+ // sheen
396
+ color *= sheenAlbedoScaling( wo, wi, surf );
397
+ color += sheenColor( wo, wi, halfVector, surf );
398
+
399
+ // clearcoat
400
+ if ( clearcoatWi.z >= 0.0 && clearcoatWeight > 0.0 ) {
401
+
402
+ vec3 clearcoatHalfVector = getHalfVector( clearcoatWo, clearcoatWi );
403
+ cpdf = clearcoatEval( clearcoatWo, clearcoatWi, clearcoatHalfVector, surf, color );
404
+
405
+ }
406
+
407
+ float pdf =
408
+ dpdf * diffuseWeight
409
+ + spdf * specularWeight
410
+ + tpdf * transmissionWeight
411
+ + cpdf * clearcoatWeight;
412
+
413
+ // retrieve specular rays for the shadows flag
414
+ specularPdf = spdf * specularWeight + cpdf * clearcoatWeight;
415
+
416
+ return pdf;
417
+
418
+ }
419
+
420
+ float bsdfResult( vec3 wo, vec3 clearcoatWo, vec3 wi, vec3 clearcoatWi, SurfaceRec surf, out vec3 color ) {
421
+
422
+ vec3 wh = getHalfVector( wo, wi, surf.eta );
423
+ float diffuseWeight;
424
+ float specularWeight;
425
+ float transmissionWeight;
426
+ float clearcoatWeight;
427
+ getLobeWeights( wo, wi, wh, clearcoatWo, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
428
+
429
+ float specularPdf;
430
+ return bsdfEval( wo, clearcoatWo, wi, clearcoatWi, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight, specularPdf, color );
431
+
432
+ }
433
+
434
+ SampleRec bsdfSample( vec3 wo, vec3 clearcoatWo, mat3 normalBasis, mat3 invBasis, mat3 clearcoatNormalBasis, mat3 clearcoatInvBasis, SurfaceRec surf ) {
435
+
436
+ float diffuseWeight;
437
+ float specularWeight;
438
+ float transmissionWeight;
439
+ float clearcoatWeight;
440
+ // using normal and basically-reflected ray since we don't have proper half vector here
441
+ getLobeWeights( wo, wo, vec3( 0, 0, 1 ), clearcoatWo, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight );
442
+
443
+ float pdf[4];
444
+ pdf[0] = diffuseWeight;
445
+ pdf[1] = specularWeight;
446
+ pdf[2] = transmissionWeight;
447
+ pdf[3] = clearcoatWeight;
448
+
449
+ float cdf[4];
450
+ cdf[0] = pdf[0];
451
+ cdf[1] = pdf[1] + cdf[0];
452
+ cdf[2] = pdf[2] + cdf[1];
453
+ cdf[3] = pdf[3] + cdf[2];
454
+
455
+ if( cdf[3] != 0.0 ) {
456
+
457
+ float invMaxCdf = 1.0 / cdf[3];
458
+ cdf[0] *= invMaxCdf;
459
+ cdf[1] *= invMaxCdf;
460
+ cdf[2] *= invMaxCdf;
461
+ cdf[3] *= invMaxCdf;
462
+
463
+ } else {
464
+
465
+ cdf[0] = 1.0;
466
+ cdf[1] = 0.0;
467
+ cdf[2] = 0.0;
468
+ cdf[3] = 0.0;
469
+
470
+ }
471
+
472
+ vec3 wi;
473
+ vec3 clearcoatWi;
474
+
475
+ float r = sobol( 15 );
476
+ if ( r <= cdf[0] ) { // diffuse
477
+
478
+ wi = diffuseDirection( wo, surf );
479
+ clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
480
+
481
+ } else if ( r <= cdf[1] ) { // specular
482
+
483
+ wi = specularDirection( wo, surf );
484
+ clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
485
+
486
+ } else if ( r <= cdf[2] ) { // transmission / refraction
487
+
488
+ wi = transmissionDirection( wo, surf );
489
+ clearcoatWi = normalize( clearcoatInvBasis * normalize( normalBasis * wi ) );
490
+
491
+ } else if ( r <= cdf[3] ) { // clearcoat
492
+
493
+ clearcoatWi = clearcoatDirection( clearcoatWo, surf );
494
+ wi = normalize( invBasis * normalize( clearcoatNormalBasis * clearcoatWi ) );
495
+
496
+ }
497
+
498
+ SampleRec result;
499
+ result.pdf = bsdfEval( wo, clearcoatWo, wi, clearcoatWi, surf, diffuseWeight, specularWeight, transmissionWeight, clearcoatWeight, result.specularPdf, result.color );
500
+ result.direction = wi;
501
+ result.clearcoatDirection = clearcoatWi;
502
+
503
+ return result;
504
+
505
+ }
506
+ `;