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.
Files changed (42) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +781 -728
  3. package/build/index.module.js +5223 -3956
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +5226 -3954
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +68 -60
  8. package/src/core/DynamicPathTracingSceneGenerator.js +119 -111
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +255 -255
  11. package/src/core/PathTracingSceneGenerator.js +68 -68
  12. package/src/index.js +33 -26
  13. package/src/materials/AlphaDisplayMaterial.js +48 -48
  14. package/src/materials/AmbientOcclusionMaterial.js +197 -197
  15. package/src/materials/BlendMaterial.js +67 -67
  16. package/src/materials/LambertPathTracingMaterial.js +285 -285
  17. package/src/materials/MaterialBase.js +56 -56
  18. package/src/materials/PhysicalPathTracingMaterial.js +922 -848
  19. package/src/{core → objects}/EquirectCamera.js +13 -13
  20. package/src/{core → objects}/PhysicalCamera.js +28 -28
  21. package/src/objects/PhysicalSpotLight.js +14 -0
  22. package/src/objects/ShapedAreaLight.js +12 -0
  23. package/src/shader/shaderEnvMapSampling.js +59 -59
  24. package/src/shader/shaderGGXFunctions.js +108 -108
  25. package/src/shader/shaderIridescenceFunctions.js +130 -0
  26. package/src/shader/shaderLightSampling.js +231 -87
  27. package/src/shader/shaderMaterialSampling.js +546 -501
  28. package/src/shader/shaderSheenFunctions.js +98 -0
  29. package/src/shader/shaderStructs.js +307 -191
  30. package/src/shader/shaderUtils.js +350 -287
  31. package/src/uniforms/EquirectHdrInfoUniform.js +259 -263
  32. package/src/uniforms/IESProfilesTexture.js +100 -0
  33. package/src/uniforms/LightsInfoUniformStruct.js +162 -0
  34. package/src/uniforms/MaterialsTexture.js +406 -319
  35. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  36. package/src/uniforms/RenderTarget2DArray.js +93 -93
  37. package/src/utils/BlurredEnvMapGenerator.js +113 -113
  38. package/src/utils/GeometryPreparationUtils.js +194 -194
  39. package/src/utils/IESLoader.js +325 -0
  40. package/src/utils/UVUnwrapper.js +101 -101
  41. package/src/workers/PathTracingSceneWorker.js +42 -41
  42. package/src/uniforms/LightsTexture.js +0 -83
@@ -1,848 +1,922 @@
1
- import { Matrix4, Matrix3, Color, Vector2 } from 'three';
2
- import { MaterialBase } from './MaterialBase.js';
3
- import {
4
- MeshBVHUniformStruct, FloatVertexAttributeTexture, UIntVertexAttributeTexture,
5
- shaderStructs, shaderIntersectFunction,
6
- } from 'three-mesh-bvh';
7
- import { shaderMaterialStructs, shaderLightStruct } from '../shader/shaderStructs.js';
8
- import { MaterialsTexture } from '../uniforms/MaterialsTexture.js';
9
- import { RenderTarget2DArray } from '../uniforms/RenderTarget2DArray.js';
10
- import { shaderMaterialSampling } from '../shader/shaderMaterialSampling.js';
11
- import { shaderEnvMapSampling } from '../shader/shaderEnvMapSampling.js';
12
- import { shaderLightSampling } from '../shader/shaderLightSampling.js';
13
- import { shaderUtils } from '../shader/shaderUtils.js';
14
- import { PhysicalCameraUniform } from '../uniforms/PhysicalCameraUniform.js';
15
- import { EquirectHdrInfoUniform } from '../uniforms/EquirectHdrInfoUniform.js';
16
- import { LightsTexture } from '../uniforms/LightsTexture.js';
17
-
18
- export class PhysicalPathTracingMaterial extends MaterialBase {
19
-
20
- onBeforeRender() {
21
-
22
- this.setDefine( 'FEATURE_DOF', this.physicalCamera.bokehSize === 0 ? 0 : 1 );
23
-
24
- }
25
-
26
- constructor( parameters ) {
27
-
28
- super( {
29
-
30
- transparent: true,
31
- depthWrite: false,
32
-
33
- defines: {
34
- FEATURE_MIS: 1,
35
- FEATURE_DOF: 1,
36
- FEATURE_GRADIENT_BG: 0,
37
- TRANSPARENT_TRAVERSALS: 5,
38
- // 0 = Perspective
39
- // 1 = Orthographic
40
- // 2 = Equirectangular
41
- CAMERA_TYPE: 0,
42
- },
43
-
44
- uniforms: {
45
- resolution: { value: new Vector2() },
46
-
47
- bounces: { value: 3 },
48
- physicalCamera: { value: new PhysicalCameraUniform() },
49
-
50
- bvh: { value: new MeshBVHUniformStruct() },
51
- normalAttribute: { value: new FloatVertexAttributeTexture() },
52
- tangentAttribute: { value: new FloatVertexAttributeTexture() },
53
- uvAttribute: { value: new FloatVertexAttributeTexture() },
54
- materialIndexAttribute: { value: new UIntVertexAttributeTexture() },
55
- materials: { value: new MaterialsTexture() },
56
- textures: { value: new RenderTarget2DArray().texture },
57
- lights: { value: new LightsTexture() },
58
- lightCount: { value: 0 },
59
- cameraWorldMatrix: { value: new Matrix4() },
60
- invProjectionMatrix: { value: new Matrix4() },
61
- backgroundBlur: { value: 0.0 },
62
- environmentIntensity: { value: 2.0 },
63
- environmentRotation: { value: new Matrix3() },
64
- envMapInfo: { value: new EquirectHdrInfoUniform() },
65
-
66
- seed: { value: 0 },
67
- opacity: { value: 1 },
68
- filterGlossyFactor: { value: 0.0 },
69
-
70
- bgGradientTop: { value: new Color( 0x111111 ) },
71
- bgGradientBottom: { value: new Color( 0x000000 ) },
72
- backgroundAlpha: { value: 1.0 },
73
- },
74
-
75
- vertexShader: /* glsl */`
76
-
77
- varying vec2 vUv;
78
- void main() {
79
-
80
- vec4 mvPosition = vec4( position, 1.0 );
81
- mvPosition = modelViewMatrix * mvPosition;
82
- gl_Position = projectionMatrix * mvPosition;
83
-
84
- vUv = uv;
85
-
86
- }
87
-
88
- `,
89
-
90
- fragmentShader: /* glsl */`
91
- #define RAY_OFFSET 1e-4
92
-
93
- precision highp isampler2D;
94
- precision highp usampler2D;
95
- precision highp sampler2DArray;
96
- vec4 envMapTexelToLinear( vec4 a ) { return a; }
97
- #include <common>
98
-
99
- ${ shaderStructs }
100
- ${ shaderIntersectFunction }
101
- ${ shaderMaterialStructs }
102
- ${ shaderLightStruct }
103
-
104
- ${ shaderUtils }
105
- ${ shaderMaterialSampling }
106
- ${ shaderEnvMapSampling }
107
- ${ shaderLightSampling }
108
-
109
- uniform mat3 environmentRotation;
110
- uniform float backgroundBlur;
111
- uniform float backgroundAlpha;
112
-
113
- #if FEATURE_GRADIENT_BG
114
-
115
- uniform vec3 bgGradientTop;
116
- uniform vec3 bgGradientBottom;
117
-
118
- #endif
119
-
120
- #if FEATURE_DOF
121
-
122
- uniform PhysicalCamera physicalCamera;
123
-
124
- #endif
125
-
126
- uniform vec2 resolution;
127
- uniform int bounces;
128
- uniform mat4 cameraWorldMatrix;
129
- uniform mat4 invProjectionMatrix;
130
- uniform sampler2D normalAttribute;
131
- uniform sampler2D tangentAttribute;
132
- uniform sampler2D uvAttribute;
133
- uniform usampler2D materialIndexAttribute;
134
- uniform BVH bvh;
135
- uniform float environmentIntensity;
136
- uniform float filterGlossyFactor;
137
- uniform int seed;
138
- uniform float opacity;
139
- uniform sampler2D materials;
140
- uniform sampler2D lights;
141
- uniform uint lightCount;
142
-
143
- uniform EquirectHdrInfo envMapInfo;
144
-
145
- uniform sampler2DArray textures;
146
- varying vec2 vUv;
147
-
148
- vec3 sampleBackground( vec3 direction ) {
149
-
150
- #if FEATURE_GRADIENT_BG
151
-
152
- direction = normalize( direction + randDirection() * 0.05 );
153
-
154
- float value = ( direction.y + 1.0 ) / 2.0;
155
- value = pow( value, 2.0 );
156
-
157
- return mix( bgGradientBottom, bgGradientTop, value );
158
-
159
- #else
160
-
161
- vec3 sampleDir = normalize( direction + getHemisphereSample( direction, rand2() ) * 0.5 * backgroundBlur );
162
- return environmentIntensity * sampleEquirectEnvMapColor( sampleDir, envMapInfo.map );
163
-
164
- #endif
165
-
166
- }
167
-
168
- // step through multiple surface hits and accumulate color attenuation based on transmissive surfaces
169
- bool attenuateHit( BVH bvh, vec3 rayOrigin, vec3 rayDirection, int traversals, bool isShadowRay, out vec3 color ) {
170
-
171
- // hit results
172
- uvec4 faceIndices = uvec4( 0u );
173
- vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
174
- vec3 barycoord = vec3( 0.0 );
175
- float side = 1.0;
176
- float dist = 0.0;
177
-
178
- color = vec3( 1.0 );
179
-
180
- for ( int i = 0; i < traversals; i ++ ) {
181
-
182
- if ( bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist ) ) {
183
-
184
- // TODO: attenuate the contribution based on the PDF of the resulting ray including refraction values
185
- // Should be able to work using the material BSDF functions which will take into account specularity, etc.
186
- // TODO: should we account for emissive surfaces here?
187
-
188
- vec2 uv = textureSampleBarycoord( uvAttribute, barycoord, faceIndices.xyz ).xy;
189
- uint materialIndex = uTexelFetch1D( materialIndexAttribute, faceIndices.x ).r;
190
- Material material = readMaterialInfo( materials, materialIndex );
191
-
192
- // adjust the ray to the new surface
193
- bool isBelowSurface = dot( rayDirection, faceNormal ) < 0.0;
194
- vec3 point = rayOrigin + rayDirection * dist;
195
- vec3 absPoint = abs( point );
196
- float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
197
- rayOrigin = point + faceNormal * ( maxPoint + 1.0 ) * ( isBelowSurface ? - RAY_OFFSET : RAY_OFFSET );
198
-
199
- if ( ! material.castShadow && isShadowRay ) {
200
-
201
- continue;
202
-
203
- }
204
-
205
- // Opacity Test
206
-
207
- // albedo
208
- vec4 albedo = vec4( material.color, material.opacity );
209
- if ( material.map != - 1 ) {
210
-
211
- vec3 uvPrime = material.mapTransform * vec3( uv, 1 );
212
- albedo *= texture2D( textures, vec3( uvPrime.xy, material.map ) );
213
-
214
- }
215
-
216
- // alphaMap
217
- if ( material.alphaMap != -1 ) {
218
-
219
- albedo.a *= texture2D( textures, vec3( uv, material.alphaMap ) ).x;
220
-
221
- }
222
-
223
- // transmission
224
- float transmission = material.transmission;
225
- if ( material.transmissionMap != - 1 ) {
226
-
227
- vec3 uvPrime = material.transmissionMapTransform * vec3( uv, 1 );
228
- transmission *= texture2D( textures, vec3( uvPrime.xy, material.transmissionMap ) ).r;
229
-
230
- }
231
-
232
- // metalness
233
- float metalness = material.metalness;
234
- if ( material.metalnessMap != - 1 ) {
235
-
236
- vec3 uvPrime = material.metalnessMapTransform * vec3( uv, 1 );
237
- metalness *= texture2D( textures, vec3( uvPrime.xy, material.metalnessMap ) ).b;
238
-
239
- }
240
-
241
- float alphaTest = material.alphaTest;
242
- bool useAlphaTest = alphaTest != 0.0;
243
- float transmissionFactor = ( 1.0 - metalness ) * transmission;
244
- if (
245
- transmissionFactor < rand() && ! (
246
- // material sidedness
247
- material.side != 0.0 && side == material.side
248
-
249
- // alpha test
250
- || useAlphaTest && albedo.a < alphaTest
251
-
252
- // opacity
253
- || ! useAlphaTest && albedo.a < rand()
254
- )
255
- ) {
256
-
257
- return true;
258
-
259
- }
260
-
261
- // only attenuate on the way in
262
- if ( isBelowSurface ) {
263
-
264
- color *= mix( vec3( 1.0 ), albedo.rgb, transmissionFactor );
265
-
266
- }
267
-
268
- } else {
269
-
270
- return false;
271
-
272
- }
273
-
274
- }
275
-
276
- return true;
277
-
278
- }
279
-
280
- // returns whether the ray hit anything before a certain distance, not just the first surface. Could be optimized to not check the full hierarchy.
281
- bool anyCloserHit( BVH bvh, vec3 rayOrigin, vec3 rayDirection, float maxDist ) {
282
-
283
- uvec4 faceIndices = uvec4( 0u );
284
- vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
285
- vec3 barycoord = vec3( 0.0 );
286
- float side = 1.0;
287
- float dist = 0.0;
288
- bool hit = bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist );
289
- return hit && dist < maxDist;
290
-
291
- }
292
-
293
- // tentFilter from Peter Shirley's 'Realistic Ray Tracing (2nd Edition)' book, pg. 60
294
- // erichlof/THREE.js-PathTracing-Renderer/
295
- float tentFilter( float x ) {
296
-
297
- return x < 0.5 ? sqrt( 2.0 * x ) - 1.0 : 1.0 - sqrt( 2.0 - ( 2.0 * x ) );
298
-
299
- }
300
-
301
- vec3 ndcToRayOrigin( vec2 coord ) {
302
-
303
- vec4 rayOrigin4 = cameraWorldMatrix * invProjectionMatrix * vec4( coord, - 1.0, 1.0 );
304
- return rayOrigin4.xyz / rayOrigin4.w;
305
- }
306
-
307
- void getCameraRay( out vec3 rayDirection, out vec3 rayOrigin ) {
308
-
309
- vec2 ssd = vec2( 1.0 ) / resolution;
310
-
311
- // Jitter the camera ray by finding a uv coordinate at a random sample
312
- // around this pixel's UV coordinate
313
- vec2 jitteredUv = vUv + vec2( tentFilter( rand() ) * ssd.x, tentFilter( rand() ) * ssd.y );
314
-
315
- #if CAMERA_TYPE == 2
316
-
317
- // Equirectangular projection
318
-
319
- vec4 rayDirection4 = vec4( equirectUvToDirection( jitteredUv ), 0.0 );
320
- vec4 rayOrigin4 = vec4( 0.0, 0.0, 0.0, 1.0 );
321
-
322
- rayDirection4 = cameraWorldMatrix * rayDirection4;
323
- rayOrigin4 = cameraWorldMatrix * rayOrigin4;
324
-
325
- rayDirection = normalize( rayDirection4.xyz );
326
- rayOrigin = rayOrigin4.xyz / rayOrigin4.w;
327
-
328
- #else
329
-
330
- // get [-1, 1] normalized device coordinates
331
- vec2 ndc = 2.0 * jitteredUv - vec2( 1.0 );
332
-
333
- rayOrigin = ndcToRayOrigin( ndc );
334
-
335
- #if CAMERA_TYPE == 1
336
-
337
- // Orthographic projection
338
-
339
- rayDirection = ( cameraWorldMatrix * vec4( 0.0, 0.0, -1.0, 0.0 ) ).xyz;
340
- rayDirection = normalize( rayDirection );
341
-
342
- #else
343
-
344
- // Perspective projection
345
-
346
- rayDirection = normalize( mat3(cameraWorldMatrix) * ( invProjectionMatrix * vec4( ndc, 0.0, 1.0 ) ).xyz );
347
-
348
- #endif
349
-
350
- #endif
351
-
352
- #if FEATURE_DOF
353
- {
354
-
355
- // depth of field
356
- vec3 focalPoint = rayOrigin + normalize( rayDirection ) * physicalCamera.focusDistance;
357
-
358
- // get the aperture sample
359
- vec2 apertureSample = sampleAperture( physicalCamera.apertureBlades ) * physicalCamera.bokehSize * 0.5 * 1e-3;
360
-
361
- // rotate the aperture shape
362
- float ac = cos( physicalCamera.apertureRotation );
363
- float as = sin( physicalCamera.apertureRotation );
364
- apertureSample = vec2(
365
- apertureSample.x * ac - apertureSample.y * as,
366
- apertureSample.x * as + apertureSample.y * ac
367
- );
368
- apertureSample.x *= saturate( physicalCamera.anamorphicRatio );
369
- apertureSample.y *= saturate( 1.0 / physicalCamera.anamorphicRatio );
370
-
371
- // create the new ray
372
- rayOrigin += ( cameraWorldMatrix * vec4( apertureSample, 0.0, 0.0 ) ).xyz;
373
- rayDirection = focalPoint - rayOrigin;
374
-
375
- }
376
- #endif
377
-
378
- rayDirection = normalize( rayDirection );
379
-
380
- }
381
-
382
- void main() {
383
-
384
- rng_initialize( gl_FragCoord.xy, seed );
385
-
386
- vec3 rayDirection;
387
- vec3 rayOrigin;
388
-
389
- getCameraRay( rayDirection, rayOrigin );
390
-
391
- // inverse environment rotation
392
- mat3 invEnvironmentRotation = inverse( environmentRotation );
393
-
394
- // final color
395
- gl_FragColor = vec4( 0.0 );
396
- gl_FragColor.a = 1.0;
397
-
398
- // hit results
399
- uvec4 faceIndices = uvec4( 0u );
400
- vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
401
- vec3 barycoord = vec3( 0.0 );
402
- float side = 1.0;
403
- float dist = 0.0;
404
-
405
- // path tracing state
406
- float accumulatedRoughness = 0.0;
407
- float accumulatedClearcoatRoughness = 0.0;
408
- bool transmissiveRay = true;
409
- int transparentTraversals = TRANSPARENT_TRAVERSALS;
410
- vec3 throughputColor = vec3( 1.0 );
411
- SampleRec sampleRec;
412
- int i;
413
- bool isShadowRay = false;
414
-
415
- for ( i = 0; i < bounces; i ++ ) {
416
-
417
- bool hit = bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist );
418
-
419
- LightSampleRec lightHit = lightsClosestHit( lights, lightCount, rayOrigin, rayDirection );
420
-
421
- if ( lightHit.hit && ( lightHit.dist < dist || !hit ) ) {
422
-
423
- if ( i == 0 || transmissiveRay ) {
424
-
425
- gl_FragColor.rgb += lightHit.emission * throughputColor;
426
-
427
- } else {
428
-
429
- #if FEATURE_MIS
430
-
431
- // weight the contribution
432
- float misWeight = misHeuristic( sampleRec.pdf, lightHit.pdf / float( lightCount + 1u ) );
433
- gl_FragColor.rgb += lightHit.emission * throughputColor * misWeight;
434
-
435
- #else
436
-
437
- gl_FragColor.rgb +=
438
- lightHit.emission *
439
- throughputColor;
440
-
441
- #endif
442
-
443
- }
444
- break;
445
-
446
- }
447
-
448
- if ( ! hit ) {
449
-
450
- if ( i == 0 || transmissiveRay ) {
451
-
452
- gl_FragColor.rgb += sampleBackground( environmentRotation * rayDirection ) * throughputColor;
453
- gl_FragColor.a = backgroundAlpha;
454
-
455
- } else {
456
-
457
- #if FEATURE_MIS
458
-
459
- // get the PDF of the hit envmap point
460
- vec3 envColor;
461
- float envPdf = envMapSample( environmentRotation * rayDirection, envMapInfo, envColor );
462
- envPdf /= float( lightCount + 1u );
463
-
464
- // and weight the contribution
465
- float misWeight = misHeuristic( sampleRec.pdf, envPdf );
466
- gl_FragColor.rgb += environmentIntensity * envColor * throughputColor * misWeight;
467
-
468
- #else
469
-
470
- gl_FragColor.rgb +=
471
- environmentIntensity *
472
- sampleEquirectEnvMapColor( environmentRotation * rayDirection, envMapInfo.map ) *
473
- throughputColor;
474
-
475
- #endif
476
-
477
- }
478
- break;
479
-
480
- }
481
-
482
- uint materialIndex = uTexelFetch1D( materialIndexAttribute, faceIndices.x ).r;
483
- Material material = readMaterialInfo( materials, materialIndex );
484
-
485
- if ( material.matte && i == 0 ) {
486
-
487
- gl_FragColor = vec4( 0.0 );
488
- break;
489
-
490
- }
491
-
492
- // if we've determined that this is a shadow ray and we've hit an item with no shadow casting
493
- // then skip it
494
- if ( ! material.castShadow && isShadowRay ) {
495
-
496
- vec3 point = rayOrigin + rayDirection * dist;
497
- vec3 absPoint = abs( point );
498
- float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
499
- rayOrigin = point - ( maxPoint + 1.0 ) * faceNormal * RAY_OFFSET;
500
-
501
- continue;
502
-
503
- }
504
-
505
- vec2 uv = textureSampleBarycoord( uvAttribute, barycoord, faceIndices.xyz ).xy;
506
- // albedo
507
- vec4 albedo = vec4( material.color, material.opacity );
508
- if ( material.map != - 1 ) {
509
-
510
- vec3 uvPrime = material.mapTransform * vec3( uv, 1 );
511
- albedo *= texture2D( textures, vec3( uvPrime.xy, material.map ) );
512
- }
513
-
514
- // alphaMap
515
- if ( material.alphaMap != -1 ) {
516
-
517
- albedo.a *= texture2D( textures, vec3( uv, material.alphaMap ) ).x;
518
-
519
- }
520
-
521
- // possibly skip this sample if it's transparent, alpha test is enabled, or we hit the wrong material side
522
- // and it's single sided.
523
- // - alpha test is disabled when it === 0
524
- // - the material sidedness test is complicated because we want light to pass through the back side but still
525
- // be able to see the front side. This boolean checks if the side we hit is the front side on the first ray
526
- // and we're rendering the other then we skip it. Do the opposite on subsequent bounces to get incoming light.
527
- float alphaTest = material.alphaTest;
528
- bool useAlphaTest = alphaTest != 0.0;
529
- bool isFirstHit = i == 0;
530
- if (
531
- // material sidedness
532
- material.side != 0.0 && ( side != material.side ) == isFirstHit
533
-
534
- // alpha test
535
- || useAlphaTest && albedo.a < alphaTest
536
-
537
- // opacity
538
- || ! useAlphaTest && albedo.a < rand()
539
- ) {
540
-
541
- vec3 point = rayOrigin + rayDirection * dist;
542
- vec3 absPoint = abs( point );
543
- float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
544
- rayOrigin = point - ( maxPoint + 1.0 ) * faceNormal * RAY_OFFSET;
545
-
546
- // only allow a limited number of transparency discards otherwise we could
547
- // crash the context with too long a loop.
548
- i -= sign( transparentTraversals );
549
- transparentTraversals -= sign( transparentTraversals );
550
- continue;
551
-
552
- }
553
-
554
- // fetch the interpolated smooth normal
555
- vec3 normal = normalize( textureSampleBarycoord(
556
- normalAttribute,
557
- barycoord,
558
- faceIndices.xyz
559
- ).xyz );
560
-
561
- // roughness
562
- float roughness = material.roughness;
563
- if ( material.roughnessMap != - 1 ) {
564
-
565
- vec3 uvPrime = material.roughnessMapTransform * vec3( uv, 1 );
566
- roughness *= texture2D( textures, vec3( uvPrime.xy, material.roughnessMap ) ).g;
567
-
568
- }
569
-
570
- // metalness
571
- float metalness = material.metalness;
572
- if ( material.metalnessMap != - 1 ) {
573
-
574
- vec3 uvPrime = material.metalnessMapTransform * vec3( uv, 1 );
575
- metalness *= texture2D( textures, vec3( uvPrime.xy, material.metalnessMap ) ).b;
576
-
577
- }
578
-
579
- // emission
580
- vec3 emission = material.emissiveIntensity * material.emissive;
581
- if ( material.emissiveMap != - 1 ) {
582
-
583
- vec3 uvPrime = material.emissiveMapTransform * vec3( uv, 1 );
584
- emission *= texture2D( textures, vec3( uvPrime.xy, material.emissiveMap ) ).xyz;
585
-
586
- }
587
-
588
- // transmission
589
- float transmission = material.transmission;
590
- if ( material.transmissionMap != - 1 ) {
591
-
592
- vec3 uvPrime = material.transmissionMapTransform * vec3( uv, 1 );
593
- transmission *= texture2D( textures, vec3( uvPrime.xy, material.transmissionMap ) ).r;
594
-
595
- }
596
-
597
- // normal
598
- vec3 baseNormal = normal;
599
- if ( material.normalMap != - 1 ) {
600
-
601
- vec4 tangentSample = textureSampleBarycoord(
602
- tangentAttribute,
603
- barycoord,
604
- faceIndices.xyz
605
- );
606
-
607
- // some provided tangents can be malformed (0, 0, 0) causing the normal to be degenerate
608
- // resulting in NaNs and slow path tracing.
609
- if ( length( tangentSample.xyz ) > 0.0 ) {
610
-
611
- vec3 tangent = normalize( tangentSample.xyz );
612
- vec3 bitangent = normalize( cross( normal, tangent ) * tangentSample.w );
613
- mat3 vTBN = mat3( tangent, bitangent, normal );
614
-
615
- vec3 uvPrime = material.normalMapTransform * vec3( uv, 1 );
616
- vec3 texNormal = texture2D( textures, vec3( uvPrime.xy, material.normalMap ) ).xyz * 2.0 - 1.0;
617
- texNormal.xy *= material.normalScale;
618
- normal = vTBN * texNormal;
619
-
620
- }
621
-
622
- }
623
-
624
- normal *= side;
625
-
626
- // clearcoat
627
- float clearcoat = material.clearcoat;
628
- if ( material.clearcoatMap != - 1 ) {
629
-
630
- vec3 uvPrime = material.clearcoatMapTransform * vec3( uv, 1 );
631
- clearcoat *= texture2D( textures, vec3( uvPrime.xy, material.clearcoatMap ) ).r;
632
-
633
- }
634
-
635
- // clearcoat
636
- float clearcoatRoughness = material.clearcoatRoughness;
637
- if ( material.clearcoatRoughnessMap != - 1 ) {
638
-
639
- vec3 uvPrime = material.clearcoatRoughnessMapTransform * vec3( uv, 1 );
640
- clearcoat *= texture2D( textures, vec3( uvPrime.xy, material.clearcoatRoughnessMap ) ).g;
641
-
642
- }
643
-
644
- // clearcoatNormal
645
- vec3 clearcoatNormal = baseNormal;
646
- if ( material.clearcoatNormalMap != - 1 ) {
647
-
648
- vec4 tangentSample = textureSampleBarycoord(
649
- tangentAttribute,
650
- barycoord,
651
- faceIndices.xyz
652
- );
653
-
654
- // some provided tangents can be malformed (0, 0, 0) causing the normal to be degenerate
655
- // resulting in NaNs and slow path tracing.
656
- if ( length( tangentSample.xyz ) > 0.0 ) {
657
-
658
- vec3 tangent = normalize( tangentSample.xyz );
659
- vec3 bitangent = normalize( cross( clearcoatNormal, tangent ) * tangentSample.w );
660
- mat3 vTBN = mat3( tangent, bitangent, clearcoatNormal );
661
-
662
- vec3 uvPrime = material.clearcoatNormalMapTransform * vec3( uv, 1 );
663
- vec3 texNormal = texture2D( textures, vec3( uvPrime.xy, material.clearcoatNormalMap ) ).xyz * 2.0 - 1.0;
664
- texNormal.xy *= material.clearcoatNormalScale;
665
- clearcoatNormal = vTBN * texNormal;
666
-
667
- }
668
-
669
- }
670
-
671
- clearcoatNormal *= side;
672
-
673
- SurfaceRec surfaceRec;
674
- surfaceRec.normal = normal;
675
- surfaceRec.faceNormal = faceNormal;
676
- surfaceRec.transmission = transmission;
677
- surfaceRec.ior = material.ior;
678
- surfaceRec.emission = emission;
679
- surfaceRec.metalness = metalness;
680
- surfaceRec.color = albedo.rgb;
681
- surfaceRec.roughness = roughness;
682
- surfaceRec.clearcoat = clearcoat;
683
- surfaceRec.clearcoatRoughness = clearcoatRoughness;
684
-
685
- // frontFace is used to determine transmissive properties and PDF. If no transmission is used
686
- // then we can just always assume this is a front face.
687
- surfaceRec.frontFace = side == 1.0 || transmission == 0.0;
688
-
689
- // Compute the filtered roughness value to use during specular reflection computations.
690
- // The accumulated roughness value is scaled by a user setting and a "magic value" of 5.0.
691
- // If we're exiting something transmissive then scale the factor down significantly so we can retain
692
- // sharp internal reflections
693
- surfaceRec.filteredRoughness = clamp( max( surfaceRec.roughness, accumulatedRoughness * filterGlossyFactor * 5.0 ), 0.0, 1.0 );
694
- surfaceRec.filteredClearcoatRoughness = clamp( max( surfaceRec.clearcoatRoughness, accumulatedClearcoatRoughness * filterGlossyFactor * 5.0 ), 0.0, 1.0 );
695
-
696
- mat3 normalBasis = getBasisFromNormal( surfaceRec.normal );
697
- mat3 invBasis = inverse( normalBasis );
698
-
699
- mat3 clearcoatNormalBasis = getBasisFromNormal( clearcoatNormal );
700
- mat3 clearcoatInvBasis = inverse( clearcoatNormalBasis );
701
-
702
- vec3 outgoing = - normalize( invBasis * rayDirection );
703
- vec3 clearcoatOutgoing = - normalize( clearcoatInvBasis * rayDirection );
704
- sampleRec = bsdfSample( outgoing, clearcoatOutgoing, normalBasis, invBasis, clearcoatNormalBasis, clearcoatInvBasis, surfaceRec );
705
-
706
- isShadowRay = sampleRec.specularPdf < rand();
707
-
708
- // adjust the hit point by the surface normal by a factor of some offset and the
709
- // maximum component-wise value of the current point to accommodate floating point
710
- // error as values increase.
711
- vec3 point = rayOrigin + rayDirection * dist;
712
- vec3 absPoint = abs( point );
713
- float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
714
- rayDirection = normalize( normalBasis * sampleRec.direction );
715
-
716
- bool isBelowSurface = dot( rayDirection, faceNormal ) < 0.0;
717
- rayOrigin = point + faceNormal * ( maxPoint + 1.0 ) * ( isBelowSurface ? - RAY_OFFSET : RAY_OFFSET );
718
-
719
- // direct env map sampling
720
- #if FEATURE_MIS
721
-
722
- // uniformly pick a light or environment map
723
- if( rand() > 1.0 / float( lightCount + 1u ) ) {
724
-
725
- // sample a light or environment
726
- LightSampleRec lightSampleRec = randomLightSample( lights, lightCount, rayOrigin );
727
-
728
- bool isSampleBelowSurface = dot( faceNormal, lightSampleRec.direction ) < 0.0;
729
- if ( isSampleBelowSurface ) {
730
-
731
- lightSampleRec.pdf = 0.0;
732
-
733
- }
734
-
735
- // check if a ray could even reach the light area
736
- if (
737
- lightSampleRec.pdf > 0.0 &&
738
- isDirectionValid( lightSampleRec.direction, normal, faceNormal ) &&
739
- ! anyCloserHit( bvh, rayOrigin, lightSampleRec.direction, lightSampleRec.dist )
740
- ) {
741
-
742
- // get the material pdf
743
- vec3 sampleColor;
744
- float lightMaterialPdf = bsdfResult( outgoing, clearcoatOutgoing, normalize( invBasis * lightSampleRec.direction ), normalize( clearcoatInvBasis * lightSampleRec.direction ), surfaceRec, sampleColor );
745
- bool isValidSampleColor = all( greaterThanEqual( sampleColor, vec3( 0.0 ) ) );
746
- if ( lightMaterialPdf > 0.0 && isValidSampleColor ) {
747
-
748
- // weight the direct light contribution
749
- float lightPdf = lightSampleRec.pdf / float( lightCount + 1u );
750
- float misWeight = misHeuristic( lightPdf, lightMaterialPdf );
751
- gl_FragColor.rgb += lightSampleRec.emission * throughputColor * sampleColor * misWeight / lightPdf;
752
-
753
- }
754
-
755
- }
756
-
757
- } else {
758
-
759
- // find a sample in the environment map to include in the contribution
760
- vec3 envColor, envDirection;
761
- float envPdf = randomEnvMapSample( envMapInfo, envColor, envDirection );
762
- envDirection = invEnvironmentRotation * envDirection;
763
-
764
- // this env sampling is not set up for transmissive sampling and yields overly bright
765
- // results so we ignore the sample in this case.
766
- // TODO: this should be improved but how? The env samples could traverse a few layers?
767
- bool isSampleBelowSurface = dot( faceNormal, envDirection ) < 0.0;
768
- if ( isSampleBelowSurface ) {
769
-
770
- envPdf = 0.0;
771
-
772
- }
773
-
774
- // check if a ray could even reach the surface
775
- vec3 attenuatedColor;
776
- if (
777
- envPdf > 0.0 &&
778
- isDirectionValid( envDirection, normal, faceNormal ) &&
779
- ! attenuateHit( bvh, rayOrigin, envDirection, bounces - i, isShadowRay, attenuatedColor )
780
- ) {
781
-
782
- // get the material pdf
783
- vec3 sampleColor;
784
- float envMaterialPdf = bsdfResult( outgoing, clearcoatOutgoing, normalize( invBasis * envDirection ), normalize( clearcoatInvBasis * envDirection ), surfaceRec, sampleColor );
785
- bool isValidSampleColor = all( greaterThanEqual( sampleColor, vec3( 0.0 ) ) );
786
- if ( envMaterialPdf > 0.0 && isValidSampleColor ) {
787
-
788
- // weight the direct light contribution
789
- envPdf /= float( lightCount + 1u );
790
- float misWeight = misHeuristic( envPdf, envMaterialPdf );
791
- gl_FragColor.rgb += attenuatedColor * environmentIntensity * envColor * throughputColor * sampleColor * misWeight / envPdf;
792
-
793
- }
794
-
795
- }
796
-
797
- }
798
- #endif
799
-
800
- // accumulate a roughness value to offset diffuse, specular, diffuse rays that have high contribution
801
- // to a single pixel resulting in fireflies
802
- if ( ! isBelowSurface ) {
803
-
804
- // determine if this is a rough normal or not by checking how far off straight up it is
805
- vec3 halfVector = normalize( outgoing + sampleRec.direction );
806
- accumulatedRoughness += sin( acosApprox( halfVector.z ) );
807
-
808
- vec3 clearcoatHalfVector = normalize( clearcoatOutgoing + sampleRec.clearcoatDirection );
809
- accumulatedClearcoatRoughness += sin( acosApprox( clearcoatHalfVector.z ) );
810
-
811
- transmissiveRay = false;
812
-
813
- }
814
-
815
- // accumulate color
816
- gl_FragColor.rgb += ( emission * throughputColor );
817
-
818
- // skip the sample if our PDF or ray is impossible
819
- if ( sampleRec.pdf <= 0.0 || ! isDirectionValid( rayDirection, normal, faceNormal) ) {
820
-
821
- break;
822
-
823
- }
824
-
825
- throughputColor *= sampleRec.color / sampleRec.pdf;
826
-
827
- // discard the sample if there are any NaNs
828
- if ( any( isnan( throughputColor ) ) || any( isinf( throughputColor ) ) ) {
829
-
830
- break;
831
-
832
- }
833
-
834
- }
835
-
836
- gl_FragColor.a *= opacity;
837
-
838
- }
839
-
840
- `
841
-
842
- } );
843
-
844
- this.setValues( parameters );
845
-
846
- }
847
-
848
- }
1
+ import { Matrix4, Matrix3, Color, Vector2 } from 'three';
2
+ import { MaterialBase } from './MaterialBase.js';
3
+ import {
4
+ MeshBVHUniformStruct, FloatVertexAttributeTexture, UIntVertexAttributeTexture,
5
+ shaderStructs, shaderIntersectFunction,
6
+ } from 'three-mesh-bvh';
7
+ import { shaderMaterialStructs, shaderLightStruct } from '../shader/shaderStructs.js';
8
+ import { MaterialsTexture } from '../uniforms/MaterialsTexture.js';
9
+ import { RenderTarget2DArray } from '../uniforms/RenderTarget2DArray.js';
10
+ import { shaderMaterialSampling } from '../shader/shaderMaterialSampling.js';
11
+ import { shaderEnvMapSampling } from '../shader/shaderEnvMapSampling.js';
12
+ import { shaderLightSampling } from '../shader/shaderLightSampling.js';
13
+ import { shaderUtils } from '../shader/shaderUtils.js';
14
+ import { PhysicalCameraUniform } from '../uniforms/PhysicalCameraUniform.js';
15
+ import { EquirectHdrInfoUniform } from '../uniforms/EquirectHdrInfoUniform.js';
16
+ import { LightsInfoUniformStruct } from '../uniforms/LightsInfoUniformStruct.js';
17
+ import { IESProfilesTexture } from '../uniforms/IESProfilesTexture.js';
18
+
19
+ export class PhysicalPathTracingMaterial extends MaterialBase {
20
+
21
+ onBeforeRender() {
22
+
23
+ this.setDefine( 'FEATURE_DOF', this.physicalCamera.bokehSize === 0 ? 0 : 1 );
24
+
25
+ }
26
+
27
+ constructor( parameters ) {
28
+
29
+ super( {
30
+
31
+ transparent: true,
32
+ depthWrite: false,
33
+
34
+ defines: {
35
+ FEATURE_MIS: 1,
36
+ FEATURE_DOF: 1,
37
+ FEATURE_GRADIENT_BG: 0,
38
+ TRANSPARENT_TRAVERSALS: 5,
39
+ // 0 = Perspective
40
+ // 1 = Orthographic
41
+ // 2 = Equirectangular
42
+ CAMERA_TYPE: 0,
43
+ },
44
+
45
+ uniforms: {
46
+ resolution: { value: new Vector2() },
47
+
48
+ bounces: { value: 3 },
49
+ physicalCamera: { value: new PhysicalCameraUniform() },
50
+
51
+ bvh: { value: new MeshBVHUniformStruct() },
52
+ normalAttribute: { value: new FloatVertexAttributeTexture() },
53
+ tangentAttribute: { value: new FloatVertexAttributeTexture() },
54
+ uvAttribute: { value: new FloatVertexAttributeTexture() },
55
+ materialIndexAttribute: { value: new UIntVertexAttributeTexture() },
56
+ materials: { value: new MaterialsTexture() },
57
+ textures: { value: new RenderTarget2DArray().texture },
58
+ lights: { value: new LightsInfoUniformStruct() },
59
+ iesProfiles: { value: new IESProfilesTexture().texture },
60
+ cameraWorldMatrix: { value: new Matrix4() },
61
+ invProjectionMatrix: { value: new Matrix4() },
62
+ backgroundBlur: { value: 0.0 },
63
+ environmentIntensity: { value: 1.0 },
64
+ environmentRotation: { value: new Matrix3() },
65
+ envMapInfo: { value: new EquirectHdrInfoUniform() },
66
+
67
+ seed: { value: 0 },
68
+ opacity: { value: 1 },
69
+ filterGlossyFactor: { value: 0.0 },
70
+
71
+ bgGradientTop: { value: new Color( 0x111111 ) },
72
+ bgGradientBottom: { value: new Color( 0x000000 ) },
73
+ backgroundAlpha: { value: 1.0 },
74
+ },
75
+
76
+ vertexShader: /* glsl */`
77
+
78
+ varying vec2 vUv;
79
+ void main() {
80
+
81
+ vec4 mvPosition = vec4( position, 1.0 );
82
+ mvPosition = modelViewMatrix * mvPosition;
83
+ gl_Position = projectionMatrix * mvPosition;
84
+
85
+ vUv = uv;
86
+
87
+ }
88
+
89
+ `,
90
+
91
+ fragmentShader: /* glsl */`
92
+ #define RAY_OFFSET 1e-4
93
+
94
+ precision highp isampler2D;
95
+ precision highp usampler2D;
96
+ precision highp sampler2DArray;
97
+ vec4 envMapTexelToLinear( vec4 a ) { return a; }
98
+ #include <common>
99
+
100
+ ${ shaderStructs }
101
+ ${ shaderIntersectFunction }
102
+ ${ shaderMaterialStructs }
103
+ ${ shaderLightStruct }
104
+
105
+ ${ shaderUtils }
106
+ ${ shaderMaterialSampling }
107
+ ${ shaderEnvMapSampling }
108
+
109
+ uniform mat3 environmentRotation;
110
+ uniform float backgroundBlur;
111
+ uniform float backgroundAlpha;
112
+
113
+ #if FEATURE_GRADIENT_BG
114
+
115
+ uniform vec3 bgGradientTop;
116
+ uniform vec3 bgGradientBottom;
117
+
118
+ #endif
119
+
120
+ #if FEATURE_DOF
121
+
122
+ uniform PhysicalCamera physicalCamera;
123
+
124
+ #endif
125
+
126
+ uniform vec2 resolution;
127
+ uniform int bounces;
128
+ uniform mat4 cameraWorldMatrix;
129
+ uniform mat4 invProjectionMatrix;
130
+ uniform sampler2D normalAttribute;
131
+ uniform sampler2D tangentAttribute;
132
+ uniform sampler2D uvAttribute;
133
+ uniform usampler2D materialIndexAttribute;
134
+ uniform BVH bvh;
135
+ uniform float environmentIntensity;
136
+ uniform float filterGlossyFactor;
137
+ uniform int seed;
138
+ uniform float opacity;
139
+ uniform sampler2D materials;
140
+ uniform LightsInfo lights;
141
+ uniform sampler2DArray iesProfiles;
142
+
143
+ ${ shaderLightSampling }
144
+
145
+ uniform EquirectHdrInfo envMapInfo;
146
+
147
+ uniform sampler2DArray textures;
148
+ varying vec2 vUv;
149
+
150
+ vec3 sampleBackground( vec3 direction ) {
151
+
152
+ #if FEATURE_GRADIENT_BG
153
+
154
+ direction = normalize( direction + randDirection() * 0.05 );
155
+
156
+ float value = ( direction.y + 1.0 ) / 2.0;
157
+ value = pow( value, 2.0 );
158
+
159
+ return mix( bgGradientBottom, bgGradientTop, value );
160
+
161
+ #else
162
+
163
+ vec3 sampleDir = normalize( direction + getHemisphereSample( direction, rand2() ) * 0.5 * backgroundBlur );
164
+ return environmentIntensity * sampleEquirectEnvMapColor( sampleDir, envMapInfo.map );
165
+
166
+ #endif
167
+
168
+ }
169
+
170
+ // step through multiple surface hits and accumulate color attenuation based on transmissive surfaces
171
+ bool attenuateHit( BVH bvh, vec3 rayOrigin, vec3 rayDirection, int traversals, bool isShadowRay, out vec3 color ) {
172
+
173
+ // hit results
174
+ uvec4 faceIndices = uvec4( 0u );
175
+ vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
176
+ vec3 barycoord = vec3( 0.0 );
177
+ float side = 1.0;
178
+ float dist = 0.0;
179
+
180
+ color = vec3( 1.0 );
181
+
182
+ for ( int i = 0; i < traversals; i ++ ) {
183
+
184
+ if ( bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist ) ) {
185
+
186
+ // TODO: attenuate the contribution based on the PDF of the resulting ray including refraction values
187
+ // Should be able to work using the material BSDF functions which will take into account specularity, etc.
188
+ // TODO: should we account for emissive surfaces here?
189
+
190
+ vec2 uv = textureSampleBarycoord( uvAttribute, barycoord, faceIndices.xyz ).xy;
191
+ uint materialIndex = uTexelFetch1D( materialIndexAttribute, faceIndices.x ).r;
192
+ Material material = readMaterialInfo( materials, materialIndex );
193
+
194
+ // adjust the ray to the new surface
195
+ bool isBelowSurface = dot( rayDirection, faceNormal ) < 0.0;
196
+ vec3 point = rayOrigin + rayDirection * dist;
197
+ vec3 absPoint = abs( point );
198
+ float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
199
+ rayOrigin = point + faceNormal * ( maxPoint + 1.0 ) * ( isBelowSurface ? - RAY_OFFSET : RAY_OFFSET );
200
+
201
+ if ( ! material.castShadow && isShadowRay ) {
202
+
203
+ continue;
204
+
205
+ }
206
+
207
+ // Opacity Test
208
+
209
+ // albedo
210
+ vec4 albedo = vec4( material.color, material.opacity );
211
+ if ( material.map != - 1 ) {
212
+
213
+ vec3 uvPrime = material.mapTransform * vec3( uv, 1 );
214
+ albedo *= texture2D( textures, vec3( uvPrime.xy, material.map ) );
215
+
216
+ }
217
+
218
+ // alphaMap
219
+ if ( material.alphaMap != -1 ) {
220
+
221
+ albedo.a *= texture2D( textures, vec3( uv, material.alphaMap ) ).x;
222
+
223
+ }
224
+
225
+ // transmission
226
+ float transmission = material.transmission;
227
+ if ( material.transmissionMap != - 1 ) {
228
+
229
+ vec3 uvPrime = material.transmissionMapTransform * vec3( uv, 1 );
230
+ transmission *= texture2D( textures, vec3( uvPrime.xy, material.transmissionMap ) ).r;
231
+
232
+ }
233
+
234
+ // metalness
235
+ float metalness = material.metalness;
236
+ if ( material.metalnessMap != - 1 ) {
237
+
238
+ vec3 uvPrime = material.metalnessMapTransform * vec3( uv, 1 );
239
+ metalness *= texture2D( textures, vec3( uvPrime.xy, material.metalnessMap ) ).b;
240
+
241
+ }
242
+
243
+ float alphaTest = material.alphaTest;
244
+ bool useAlphaTest = alphaTest != 0.0;
245
+ float transmissionFactor = ( 1.0 - metalness ) * transmission;
246
+ if (
247
+ transmissionFactor < rand() && ! (
248
+ // material sidedness
249
+ material.side != 0.0 && side == material.side
250
+
251
+ // alpha test
252
+ || useAlphaTest && albedo.a < alphaTest
253
+
254
+ // opacity
255
+ || ! useAlphaTest && albedo.a < rand()
256
+ )
257
+ ) {
258
+
259
+ return true;
260
+
261
+ }
262
+
263
+ // only attenuate on the way in
264
+ if ( isBelowSurface ) {
265
+
266
+ color *= mix( vec3( 1.0 ), albedo.rgb, transmissionFactor );
267
+
268
+ }
269
+
270
+ } else {
271
+
272
+ return false;
273
+
274
+ }
275
+
276
+ }
277
+
278
+ return true;
279
+
280
+ }
281
+
282
+ // returns whether the ray hit anything before a certain distance, not just the first surface. Could be optimized to not check the full hierarchy.
283
+ bool anyCloserHit( BVH bvh, vec3 rayOrigin, vec3 rayDirection, float maxDist ) {
284
+
285
+ uvec4 faceIndices = uvec4( 0u );
286
+ vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
287
+ vec3 barycoord = vec3( 0.0 );
288
+ float side = 1.0;
289
+ float dist = 0.0;
290
+ bool hit = bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist );
291
+ return hit && dist < maxDist;
292
+
293
+ }
294
+
295
+ // tentFilter from Peter Shirley's 'Realistic Ray Tracing (2nd Edition)' book, pg. 60
296
+ // erichlof/THREE.js-PathTracing-Renderer/
297
+ float tentFilter( float x ) {
298
+
299
+ return x < 0.5 ? sqrt( 2.0 * x ) - 1.0 : 1.0 - sqrt( 2.0 - ( 2.0 * x ) );
300
+
301
+ }
302
+
303
+ vec3 ndcToRayOrigin( vec2 coord ) {
304
+
305
+ vec4 rayOrigin4 = cameraWorldMatrix * invProjectionMatrix * vec4( coord, - 1.0, 1.0 );
306
+ return rayOrigin4.xyz / rayOrigin4.w;
307
+ }
308
+
309
+ void getCameraRay( out vec3 rayDirection, out vec3 rayOrigin ) {
310
+
311
+ vec2 ssd = vec2( 1.0 ) / resolution;
312
+
313
+ // Jitter the camera ray by finding a uv coordinate at a random sample
314
+ // around this pixel's UV coordinate
315
+ vec2 jitteredUv = vUv + vec2( tentFilter( rand() ) * ssd.x, tentFilter( rand() ) * ssd.y );
316
+
317
+ #if CAMERA_TYPE == 2
318
+
319
+ // Equirectangular projection
320
+
321
+ vec4 rayDirection4 = vec4( equirectUvToDirection( jitteredUv ), 0.0 );
322
+ vec4 rayOrigin4 = vec4( 0.0, 0.0, 0.0, 1.0 );
323
+
324
+ rayDirection4 = cameraWorldMatrix * rayDirection4;
325
+ rayOrigin4 = cameraWorldMatrix * rayOrigin4;
326
+
327
+ rayDirection = normalize( rayDirection4.xyz );
328
+ rayOrigin = rayOrigin4.xyz / rayOrigin4.w;
329
+
330
+ #else
331
+
332
+ // get [-1, 1] normalized device coordinates
333
+ vec2 ndc = 2.0 * jitteredUv - vec2( 1.0 );
334
+
335
+ rayOrigin = ndcToRayOrigin( ndc );
336
+
337
+ #if CAMERA_TYPE == 1
338
+
339
+ // Orthographic projection
340
+
341
+ rayDirection = ( cameraWorldMatrix * vec4( 0.0, 0.0, -1.0, 0.0 ) ).xyz;
342
+ rayDirection = normalize( rayDirection );
343
+
344
+ #else
345
+
346
+ // Perspective projection
347
+
348
+ rayDirection = normalize( mat3(cameraWorldMatrix) * ( invProjectionMatrix * vec4( ndc, 0.0, 1.0 ) ).xyz );
349
+
350
+ #endif
351
+
352
+ #endif
353
+
354
+ #if FEATURE_DOF
355
+ {
356
+
357
+ // depth of field
358
+ vec3 focalPoint = rayOrigin + normalize( rayDirection ) * physicalCamera.focusDistance;
359
+
360
+ // get the aperture sample
361
+ vec2 apertureSample = sampleAperture( physicalCamera.apertureBlades ) * physicalCamera.bokehSize * 0.5 * 1e-3;
362
+
363
+ // rotate the aperture shape
364
+ float ac = cos( physicalCamera.apertureRotation );
365
+ float as = sin( physicalCamera.apertureRotation );
366
+ apertureSample = vec2(
367
+ apertureSample.x * ac - apertureSample.y * as,
368
+ apertureSample.x * as + apertureSample.y * ac
369
+ );
370
+ apertureSample.x *= saturate( physicalCamera.anamorphicRatio );
371
+ apertureSample.y *= saturate( 1.0 / physicalCamera.anamorphicRatio );
372
+
373
+ // create the new ray
374
+ rayOrigin += ( cameraWorldMatrix * vec4( apertureSample, 0.0, 0.0 ) ).xyz;
375
+ rayDirection = focalPoint - rayOrigin;
376
+
377
+ }
378
+ #endif
379
+
380
+ rayDirection = normalize( rayDirection );
381
+
382
+ }
383
+
384
+ void main() {
385
+
386
+ rng_initialize( gl_FragCoord.xy, seed );
387
+
388
+ vec3 rayDirection;
389
+ vec3 rayOrigin;
390
+
391
+ getCameraRay( rayDirection, rayOrigin );
392
+
393
+ // inverse environment rotation
394
+ mat3 invEnvironmentRotation = inverse( environmentRotation );
395
+
396
+ // final color
397
+ gl_FragColor = vec4( 0.0 );
398
+ gl_FragColor.a = 1.0;
399
+
400
+ // hit results
401
+ uvec4 faceIndices = uvec4( 0u );
402
+ vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
403
+ vec3 barycoord = vec3( 0.0 );
404
+ float side = 1.0;
405
+ float dist = 0.0;
406
+
407
+ // path tracing state
408
+ float accumulatedRoughness = 0.0;
409
+ float accumulatedClearcoatRoughness = 0.0;
410
+ bool transmissiveRay = true;
411
+ int transparentTraversals = TRANSPARENT_TRAVERSALS;
412
+ vec3 throughputColor = vec3( 1.0 );
413
+ SampleRec sampleRec;
414
+ int i;
415
+ bool isShadowRay = false;
416
+
417
+ for ( i = 0; i < bounces; i ++ ) {
418
+
419
+ bool hit = bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist );
420
+
421
+ LightSampleRec lightHit = lightsClosestHit( lights.tex, lights.count, rayOrigin, rayDirection );
422
+
423
+ if ( lightHit.hit && ( lightHit.dist < dist || !hit ) ) {
424
+
425
+ if ( i == 0 || transmissiveRay ) {
426
+
427
+ gl_FragColor.rgb += lightHit.emission * throughputColor;
428
+
429
+ } else {
430
+
431
+ #if FEATURE_MIS
432
+
433
+ // NOTE: we skip MIS for spotlights since we haven't fixed the forward
434
+ // path tracing code path, yet
435
+ if ( lightHit.type == SPOT_LIGHT_TYPE ) {
436
+
437
+ gl_FragColor.rgb += lightHit.emission * throughputColor;
438
+
439
+ } else {
440
+
441
+ // weight the contribution
442
+ float misWeight = misHeuristic( sampleRec.pdf, lightHit.pdf / float( lights.count + 1u ) );
443
+ gl_FragColor.rgb += lightHit.emission * throughputColor * misWeight;
444
+
445
+ }
446
+
447
+ #else
448
+
449
+ gl_FragColor.rgb += lightHit.emission * throughputColor;
450
+
451
+ #endif
452
+
453
+ }
454
+ break;
455
+
456
+ }
457
+
458
+ if ( ! hit ) {
459
+
460
+ if ( i == 0 || transmissiveRay ) {
461
+
462
+ gl_FragColor.rgb += sampleBackground( environmentRotation * rayDirection ) * throughputColor;
463
+ gl_FragColor.a = backgroundAlpha;
464
+
465
+ } else {
466
+
467
+ #if FEATURE_MIS
468
+
469
+ // get the PDF of the hit envmap point
470
+ vec3 envColor;
471
+ float envPdf = envMapSample( environmentRotation * rayDirection, envMapInfo, envColor );
472
+ envPdf /= float( lights.count + 1u );
473
+
474
+ // and weight the contribution
475
+ float misWeight = misHeuristic( sampleRec.pdf, envPdf );
476
+ gl_FragColor.rgb += environmentIntensity * envColor * throughputColor * misWeight;
477
+
478
+ #else
479
+
480
+ gl_FragColor.rgb +=
481
+ environmentIntensity *
482
+ sampleEquirectEnvMapColor( environmentRotation * rayDirection, envMapInfo.map ) *
483
+ throughputColor;
484
+
485
+ #endif
486
+
487
+ }
488
+ break;
489
+
490
+ }
491
+
492
+ uint materialIndex = uTexelFetch1D( materialIndexAttribute, faceIndices.x ).r;
493
+ Material material = readMaterialInfo( materials, materialIndex );
494
+
495
+ if ( material.matte && i == 0 ) {
496
+
497
+ gl_FragColor = vec4( 0.0 );
498
+ break;
499
+
500
+ }
501
+
502
+ // if we've determined that this is a shadow ray and we've hit an item with no shadow casting
503
+ // then skip it
504
+ if ( ! material.castShadow && isShadowRay ) {
505
+
506
+ vec3 point = rayOrigin + rayDirection * dist;
507
+ vec3 absPoint = abs( point );
508
+ float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
509
+ rayOrigin = point - ( maxPoint + 1.0 ) * faceNormal * RAY_OFFSET;
510
+
511
+ continue;
512
+
513
+ }
514
+
515
+ vec2 uv = textureSampleBarycoord( uvAttribute, barycoord, faceIndices.xyz ).xy;
516
+ // albedo
517
+ vec4 albedo = vec4( material.color, material.opacity );
518
+ if ( material.map != - 1 ) {
519
+
520
+ vec3 uvPrime = material.mapTransform * vec3( uv, 1 );
521
+ albedo *= texture2D( textures, vec3( uvPrime.xy, material.map ) );
522
+ }
523
+
524
+ // alphaMap
525
+ if ( material.alphaMap != -1 ) {
526
+
527
+ albedo.a *= texture2D( textures, vec3( uv, material.alphaMap ) ).x;
528
+
529
+ }
530
+
531
+ // possibly skip this sample if it's transparent, alpha test is enabled, or we hit the wrong material side
532
+ // and it's single sided.
533
+ // - alpha test is disabled when it === 0
534
+ // - the material sidedness test is complicated because we want light to pass through the back side but still
535
+ // be able to see the front side. This boolean checks if the side we hit is the front side on the first ray
536
+ // and we're rendering the other then we skip it. Do the opposite on subsequent bounces to get incoming light.
537
+ float alphaTest = material.alphaTest;
538
+ bool useAlphaTest = alphaTest != 0.0;
539
+ bool isFirstHit = i == 0;
540
+ if (
541
+ // material sidedness
542
+ material.side != 0.0 && ( side != material.side ) == isFirstHit
543
+
544
+ // alpha test
545
+ || useAlphaTest && albedo.a < alphaTest
546
+
547
+ // opacity
548
+ || ! useAlphaTest && albedo.a < rand()
549
+ ) {
550
+
551
+ vec3 point = rayOrigin + rayDirection * dist;
552
+ vec3 absPoint = abs( point );
553
+ float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
554
+ rayOrigin = point - ( maxPoint + 1.0 ) * faceNormal * RAY_OFFSET;
555
+
556
+ // only allow a limited number of transparency discards otherwise we could
557
+ // crash the context with too long a loop.
558
+ i -= sign( transparentTraversals );
559
+ transparentTraversals -= sign( transparentTraversals );
560
+ continue;
561
+
562
+ }
563
+
564
+ // fetch the interpolated smooth normal
565
+ vec3 normal = normalize( textureSampleBarycoord(
566
+ normalAttribute,
567
+ barycoord,
568
+ faceIndices.xyz
569
+ ).xyz );
570
+
571
+ // roughness
572
+ float roughness = material.roughness;
573
+ if ( material.roughnessMap != - 1 ) {
574
+
575
+ vec3 uvPrime = material.roughnessMapTransform * vec3( uv, 1 );
576
+ roughness *= texture2D( textures, vec3( uvPrime.xy, material.roughnessMap ) ).g;
577
+
578
+ }
579
+
580
+ // metalness
581
+ float metalness = material.metalness;
582
+ if ( material.metalnessMap != - 1 ) {
583
+
584
+ vec3 uvPrime = material.metalnessMapTransform * vec3( uv, 1 );
585
+ metalness *= texture2D( textures, vec3( uvPrime.xy, material.metalnessMap ) ).b;
586
+
587
+ }
588
+
589
+ // emission
590
+ vec3 emission = material.emissiveIntensity * material.emissive;
591
+ if ( material.emissiveMap != - 1 ) {
592
+
593
+ vec3 uvPrime = material.emissiveMapTransform * vec3( uv, 1 );
594
+ emission *= texture2D( textures, vec3( uvPrime.xy, material.emissiveMap ) ).xyz;
595
+
596
+ }
597
+
598
+ // transmission
599
+ float transmission = material.transmission;
600
+ if ( material.transmissionMap != - 1 ) {
601
+
602
+ vec3 uvPrime = material.transmissionMapTransform * vec3( uv, 1 );
603
+ transmission *= texture2D( textures, vec3( uvPrime.xy, material.transmissionMap ) ).r;
604
+
605
+ }
606
+
607
+ // normal
608
+ vec3 baseNormal = normal;
609
+ if ( material.normalMap != - 1 ) {
610
+
611
+ vec4 tangentSample = textureSampleBarycoord(
612
+ tangentAttribute,
613
+ barycoord,
614
+ faceIndices.xyz
615
+ );
616
+
617
+ // some provided tangents can be malformed (0, 0, 0) causing the normal to be degenerate
618
+ // resulting in NaNs and slow path tracing.
619
+ if ( length( tangentSample.xyz ) > 0.0 ) {
620
+
621
+ vec3 tangent = normalize( tangentSample.xyz );
622
+ vec3 bitangent = normalize( cross( normal, tangent ) * tangentSample.w );
623
+ mat3 vTBN = mat3( tangent, bitangent, normal );
624
+
625
+ vec3 uvPrime = material.normalMapTransform * vec3( uv, 1 );
626
+ vec3 texNormal = texture2D( textures, vec3( uvPrime.xy, material.normalMap ) ).xyz * 2.0 - 1.0;
627
+ texNormal.xy *= material.normalScale;
628
+ normal = vTBN * texNormal;
629
+
630
+ }
631
+
632
+ }
633
+
634
+ normal *= side;
635
+
636
+ // clearcoat
637
+ float clearcoat = material.clearcoat;
638
+ if ( material.clearcoatMap != - 1 ) {
639
+
640
+ vec3 uvPrime = material.clearcoatMapTransform * vec3( uv, 1 );
641
+ clearcoat *= texture2D( textures, vec3( uvPrime.xy, material.clearcoatMap ) ).r;
642
+
643
+ }
644
+
645
+ // clearcoatRoughness
646
+ float clearcoatRoughness = material.clearcoatRoughness;
647
+ if ( material.clearcoatRoughnessMap != - 1 ) {
648
+
649
+ vec3 uvPrime = material.clearcoatRoughnessMapTransform * vec3( uv, 1 );
650
+ clearcoat *= texture2D( textures, vec3( uvPrime.xy, material.clearcoatRoughnessMap ) ).g;
651
+
652
+ }
653
+
654
+ // clearcoatNormal
655
+ vec3 clearcoatNormal = baseNormal;
656
+ if ( material.clearcoatNormalMap != - 1 ) {
657
+
658
+ vec4 tangentSample = textureSampleBarycoord(
659
+ tangentAttribute,
660
+ barycoord,
661
+ faceIndices.xyz
662
+ );
663
+
664
+ // some provided tangents can be malformed (0, 0, 0) causing the normal to be degenerate
665
+ // resulting in NaNs and slow path tracing.
666
+ if ( length( tangentSample.xyz ) > 0.0 ) {
667
+
668
+ vec3 tangent = normalize( tangentSample.xyz );
669
+ vec3 bitangent = normalize( cross( clearcoatNormal, tangent ) * tangentSample.w );
670
+ mat3 vTBN = mat3( tangent, bitangent, clearcoatNormal );
671
+
672
+ vec3 uvPrime = material.clearcoatNormalMapTransform * vec3( uv, 1 );
673
+ vec3 texNormal = texture2D( textures, vec3( uvPrime.xy, material.clearcoatNormalMap ) ).xyz * 2.0 - 1.0;
674
+ texNormal.xy *= material.clearcoatNormalScale;
675
+ clearcoatNormal = vTBN * texNormal;
676
+
677
+ }
678
+
679
+ }
680
+
681
+ clearcoatNormal *= side;
682
+
683
+ // sheenColor
684
+ vec3 sheenColor = material.sheenColor;
685
+ if ( material.sheenColorMap != - 1 ) {
686
+
687
+ vec3 uvPrime = material.sheenColorMapTransform * vec3( uv, 1 );
688
+ sheenColor *= texture2D( textures, vec3( uvPrime.xy, material.sheenColorMap ) ).rgb;
689
+
690
+ }
691
+
692
+ // sheenRoughness
693
+ float sheenRoughness = material.sheenRoughness;
694
+ if ( material.sheenRoughnessMap != - 1 ) {
695
+
696
+ vec3 uvPrime = material.sheenRoughnessMapTransform * vec3( uv, 1 );
697
+ sheenRoughness *= texture2D( textures, vec3( uvPrime.xy, material.sheenRoughnessMap ) ).a;
698
+
699
+ }
700
+
701
+ // iridescence
702
+ float iridescence = material.iridescence;
703
+ if ( material.iridescenceMap != - 1 ) {
704
+
705
+ vec3 uvPrime = material.iridescenceMapTransform * vec3( uv, 1 );
706
+ iridescence *= texture2D( textures, vec3( uvPrime.xy, material.iridescenceMap ) ).r;
707
+
708
+ }
709
+
710
+ // iridescence thickness
711
+ float iridescenceThickness = material.iridescenceThicknessMaximum;
712
+ if ( material.iridescenceThicknessMap != - 1 ) {
713
+
714
+ vec3 uvPrime = material.iridescenceThicknessMapTransform * vec3( uv, 1 );
715
+ float iridescenceThicknessSampled = texture2D( textures, vec3( uvPrime.xy, material.iridescenceThicknessMap ) ).g;
716
+ iridescenceThickness = mix( material.iridescenceThicknessMinimum, material.iridescenceThicknessMaximum, iridescenceThicknessSampled );
717
+
718
+ }
719
+
720
+ iridescence = iridescenceThickness == 0.0 ? 0.0 : iridescence;
721
+
722
+ // specular color
723
+ vec3 specularColor = material.specularColor;
724
+ if ( material.specularColorMap != - 1 ) {
725
+
726
+ vec3 uvPrime = material.specularColorMapTransform * vec3( uv, 1 );
727
+ specularColor *= texture2D( textures, vec3( uvPrime.xy, material.specularColorMap ) ).rgb;
728
+
729
+ }
730
+
731
+ // specular intensity
732
+ float specularIntensity = material.specularIntensity;
733
+ if ( material.specularIntensityMap != - 1 ) {
734
+
735
+ vec3 uvPrime = material.specularIntensityMapTransform * vec3( uv, 1 );
736
+ specularIntensity *= texture2D( textures, vec3( uvPrime.xy, material.specularIntensityMap ) ).a;
737
+
738
+ }
739
+
740
+ SurfaceRec surfaceRec;
741
+ surfaceRec.normal = normal;
742
+ surfaceRec.faceNormal = faceNormal;
743
+ surfaceRec.transmission = transmission;
744
+ surfaceRec.ior = material.ior;
745
+ surfaceRec.emission = emission;
746
+ surfaceRec.metalness = metalness;
747
+ surfaceRec.color = albedo.rgb;
748
+ surfaceRec.roughness = roughness;
749
+ surfaceRec.clearcoat = clearcoat;
750
+ surfaceRec.clearcoatRoughness = clearcoatRoughness;
751
+ surfaceRec.sheenColor = sheenColor;
752
+ surfaceRec.sheenRoughness = sheenRoughness;
753
+ surfaceRec.iridescence = iridescence;
754
+ surfaceRec.iridescenceIor = material.iridescenceIor;
755
+ surfaceRec.iridescenceThickness = iridescenceThickness;
756
+ surfaceRec.specularColor = specularColor;
757
+ surfaceRec.specularIntensity = specularIntensity;
758
+
759
+ // frontFace is used to determine transmissive properties and PDF. If no transmission is used
760
+ // then we can just always assume this is a front face.
761
+ surfaceRec.frontFace = side == 1.0 || transmission == 0.0;
762
+
763
+ // Compute the filtered roughness value to use during specular reflection computations.
764
+ // The accumulated roughness value is scaled by a user setting and a "magic value" of 5.0.
765
+ // If we're exiting something transmissive then scale the factor down significantly so we can retain
766
+ // sharp internal reflections
767
+ surfaceRec.filteredRoughness = clamp( max( surfaceRec.roughness, accumulatedRoughness * filterGlossyFactor * 5.0 ), 0.0, 1.0 );
768
+ surfaceRec.filteredClearcoatRoughness = clamp( max( surfaceRec.clearcoatRoughness, accumulatedClearcoatRoughness * filterGlossyFactor * 5.0 ), 0.0, 1.0 );
769
+
770
+ mat3 normalBasis = getBasisFromNormal( surfaceRec.normal );
771
+ mat3 invBasis = inverse( normalBasis );
772
+
773
+ mat3 clearcoatNormalBasis = getBasisFromNormal( clearcoatNormal );
774
+ mat3 clearcoatInvBasis = inverse( clearcoatNormalBasis );
775
+
776
+ vec3 outgoing = - normalize( invBasis * rayDirection );
777
+ vec3 clearcoatOutgoing = - normalize( clearcoatInvBasis * rayDirection );
778
+ sampleRec = bsdfSample( outgoing, clearcoatOutgoing, normalBasis, invBasis, clearcoatNormalBasis, clearcoatInvBasis, surfaceRec );
779
+
780
+ isShadowRay = sampleRec.specularPdf < rand();
781
+
782
+ // adjust the hit point by the surface normal by a factor of some offset and the
783
+ // maximum component-wise value of the current point to accommodate floating point
784
+ // error as values increase.
785
+ vec3 point = rayOrigin + rayDirection * dist;
786
+ vec3 absPoint = abs( point );
787
+ float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
788
+ rayDirection = normalize( normalBasis * sampleRec.direction );
789
+
790
+ bool isBelowSurface = dot( rayDirection, faceNormal ) < 0.0;
791
+ rayOrigin = point + faceNormal * ( maxPoint + 1.0 ) * ( isBelowSurface ? - RAY_OFFSET : RAY_OFFSET );
792
+
793
+ // direct env map sampling
794
+ #if FEATURE_MIS
795
+
796
+ // uniformly pick a light or environment map
797
+ if( rand() > 1.0 / float( lights.count + 1u ) ) {
798
+
799
+ // sample a light or environment
800
+ LightSampleRec lightSampleRec = randomLightSample( lights.tex, iesProfiles, lights.count, rayOrigin );
801
+
802
+ bool isSampleBelowSurface = dot( faceNormal, lightSampleRec.direction ) < 0.0;
803
+ if ( isSampleBelowSurface ) {
804
+
805
+ lightSampleRec.pdf = 0.0;
806
+
807
+ }
808
+
809
+ // check if a ray could even reach the light area
810
+ if (
811
+ lightSampleRec.pdf > 0.0 &&
812
+ isDirectionValid( lightSampleRec.direction, normal, faceNormal ) &&
813
+ ! anyCloserHit( bvh, rayOrigin, lightSampleRec.direction, lightSampleRec.dist )
814
+ ) {
815
+
816
+ // get the material pdf
817
+ vec3 sampleColor;
818
+ float lightMaterialPdf = bsdfResult( outgoing, clearcoatOutgoing, normalize( invBasis * lightSampleRec.direction ), normalize( clearcoatInvBasis * lightSampleRec.direction ), surfaceRec, sampleColor );
819
+ bool isValidSampleColor = all( greaterThanEqual( sampleColor, vec3( 0.0 ) ) );
820
+ if ( lightMaterialPdf > 0.0 && isValidSampleColor ) {
821
+
822
+ // weight the direct light contribution
823
+ float lightPdf = lightSampleRec.pdf / float( lights.count + 1u );
824
+ float misWeight = misHeuristic( lightPdf, lightMaterialPdf );
825
+ gl_FragColor.rgb += lightSampleRec.emission * throughputColor * sampleColor * misWeight / lightPdf;
826
+
827
+ }
828
+
829
+ }
830
+
831
+ } else {
832
+
833
+ // find a sample in the environment map to include in the contribution
834
+ vec3 envColor, envDirection;
835
+ float envPdf = randomEnvMapSample( envMapInfo, envColor, envDirection );
836
+ envDirection = invEnvironmentRotation * envDirection;
837
+
838
+ // this env sampling is not set up for transmissive sampling and yields overly bright
839
+ // results so we ignore the sample in this case.
840
+ // TODO: this should be improved but how? The env samples could traverse a few layers?
841
+ bool isSampleBelowSurface = dot( faceNormal, envDirection ) < 0.0;
842
+ if ( isSampleBelowSurface ) {
843
+
844
+ envPdf = 0.0;
845
+
846
+ }
847
+
848
+ // check if a ray could even reach the surface
849
+ vec3 attenuatedColor;
850
+ if (
851
+ envPdf > 0.0 &&
852
+ isDirectionValid( envDirection, normal, faceNormal ) &&
853
+ ! attenuateHit( bvh, rayOrigin, envDirection, bounces - i, isShadowRay, attenuatedColor )
854
+ ) {
855
+
856
+ // get the material pdf
857
+ vec3 sampleColor;
858
+ float envMaterialPdf = bsdfResult( outgoing, clearcoatOutgoing, normalize( invBasis * envDirection ), normalize( clearcoatInvBasis * envDirection ), surfaceRec, sampleColor );
859
+ bool isValidSampleColor = all( greaterThanEqual( sampleColor, vec3( 0.0 ) ) );
860
+ if ( envMaterialPdf > 0.0 && isValidSampleColor ) {
861
+
862
+ // weight the direct light contribution
863
+ envPdf /= float( lights.count + 1u );
864
+ float misWeight = misHeuristic( envPdf, envMaterialPdf );
865
+ gl_FragColor.rgb += attenuatedColor * environmentIntensity * envColor * throughputColor * sampleColor * misWeight / envPdf;
866
+
867
+ }
868
+
869
+ }
870
+
871
+ }
872
+ #endif
873
+
874
+ // accumulate a roughness value to offset diffuse, specular, diffuse rays that have high contribution
875
+ // to a single pixel resulting in fireflies
876
+ if ( ! isBelowSurface ) {
877
+
878
+ // determine if this is a rough normal or not by checking how far off straight up it is
879
+ vec3 halfVector = normalize( outgoing + sampleRec.direction );
880
+ accumulatedRoughness += sin( acosApprox( halfVector.z ) );
881
+
882
+ vec3 clearcoatHalfVector = normalize( clearcoatOutgoing + sampleRec.clearcoatDirection );
883
+ accumulatedClearcoatRoughness += sin( acosApprox( clearcoatHalfVector.z ) );
884
+
885
+ transmissiveRay = false;
886
+
887
+ }
888
+
889
+ // accumulate color
890
+ gl_FragColor.rgb += ( emission * throughputColor );
891
+
892
+ // skip the sample if our PDF or ray is impossible
893
+ if ( sampleRec.pdf <= 0.0 || ! isDirectionValid( rayDirection, normal, faceNormal) ) {
894
+
895
+ break;
896
+
897
+ }
898
+
899
+ throughputColor *= sampleRec.color / sampleRec.pdf;
900
+
901
+ // discard the sample if there are any NaNs
902
+ if ( any( isnan( throughputColor ) ) || any( isinf( throughputColor ) ) ) {
903
+
904
+ break;
905
+
906
+ }
907
+
908
+ }
909
+
910
+ gl_FragColor.a *= opacity;
911
+
912
+ }
913
+
914
+ `
915
+
916
+ } );
917
+
918
+ this.setValues( parameters );
919
+
920
+ }
921
+
922
+ }