three-gpu-pathtracer 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +981 -961
  3. package/build/index.module.js +6965 -6508
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +6959 -6505
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +73 -73
  8. package/src/core/DynamicPathTracingSceneGenerator.js +119 -119
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +346 -346
  11. package/src/core/PathTracingSceneGenerator.js +69 -69
  12. package/src/core/QuiltPathTracingRenderer.js +223 -223
  13. package/src/index.js +36 -40
  14. package/src/materials/MaterialBase.js +56 -56
  15. package/src/materials/{GraphMaterial.js → debug/GraphMaterial.js} +243 -243
  16. package/src/materials/{AlphaDisplayMaterial.js → fullscreen/AlphaDisplayMaterial.js} +48 -48
  17. package/src/materials/{BlendMaterial.js → fullscreen/BlendMaterial.js} +67 -67
  18. package/src/materials/{DenoiseMaterial.js → fullscreen/DenoiseMaterial.js} +142 -142
  19. package/src/materials/{LambertPathTracingMaterial.js → pathtracing/LambertPathTracingMaterial.js} +296 -285
  20. package/src/materials/pathtracing/PhysicalPathTracingMaterial.js +635 -0
  21. package/src/materials/pathtracing/glsl/attenuateHit.glsl.js +179 -0
  22. package/src/materials/pathtracing/glsl/cameraUtils.glsl.js +81 -0
  23. package/src/materials/pathtracing/glsl/getSurfaceRecord.glsl.js +317 -0
  24. package/src/materials/pathtracing/glsl/traceScene.glsl.js +54 -0
  25. package/src/materials/{AmbientOcclusionMaterial.js → surface/AmbientOcclusionMaterial.js} +207 -199
  26. package/src/materials/surface/FogVolumeMaterial.js +23 -0
  27. package/src/objects/EquirectCamera.js +13 -13
  28. package/src/objects/PhysicalCamera.js +28 -28
  29. package/src/objects/PhysicalSpotLight.js +14 -14
  30. package/src/objects/ShapedAreaLight.js +12 -12
  31. package/src/shader/bsdf/bsdfSampling.glsl.js +490 -0
  32. package/src/shader/bsdf/fog.glsl.js +23 -0
  33. package/src/shader/bsdf/ggx.glsl.js +102 -0
  34. package/src/shader/bsdf/iridescence.glsl.js +135 -0
  35. package/src/shader/bsdf/sheen.glsl.js +98 -0
  36. package/src/shader/{shaderLayerTexelFetchFunctions.js → common/arraySamplerTexelFetch.glsl.js} +25 -25
  37. package/src/shader/common/bvhAnyHit.glsl.js +76 -0
  38. package/src/shader/common/fresnel.glsl.js +98 -0
  39. package/src/shader/common/intersectShapes.glsl.js +62 -0
  40. package/src/shader/common/math.glsl.js +81 -0
  41. package/src/shader/common/utils.glsl.js +116 -0
  42. package/src/shader/{shaderRandFunctions.js → rand/pcg.glsl.js} +57 -57
  43. package/src/shader/{shaderSobolSampling.js → rand/sobol.glsl.js} +256 -256
  44. package/src/shader/sampling/equirectSampling.glsl.js +62 -0
  45. package/src/shader/sampling/lightSampling.glsl.js +223 -0
  46. package/src/shader/sampling/shapeSampling.glsl.js +86 -0
  47. package/src/shader/structs/cameraStruct.glsl.js +13 -0
  48. package/src/shader/structs/equirectStruct.glsl.js +14 -0
  49. package/src/shader/structs/fogMaterialBvh.glsl.js +62 -0
  50. package/src/shader/structs/lightsStruct.glsl.js +78 -0
  51. package/src/shader/{shaderStructs.js → structs/materialStruct.glsl.js} +207 -327
  52. package/src/textures/GradientEquirectTexture.js +35 -35
  53. package/src/textures/ProceduralEquirectTexture.js +75 -75
  54. package/src/uniforms/AttributesTextureArray.js +35 -35
  55. package/src/uniforms/EquirectHdrInfoUniform.js +277 -273
  56. package/src/uniforms/FloatAttributeTextureArray.js +169 -169
  57. package/src/uniforms/IESProfilesTexture.js +100 -100
  58. package/src/uniforms/LightsInfoUniformStruct.js +212 -212
  59. package/src/uniforms/MaterialsTexture.js +503 -426
  60. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  61. package/src/uniforms/RenderTarget2DArray.js +97 -97
  62. package/src/uniforms/utils.js +30 -30
  63. package/src/utils/BlurredEnvMapGenerator.js +116 -116
  64. package/src/utils/GeometryPreparationUtils.js +214 -214
  65. package/src/utils/IESLoader.js +325 -325
  66. package/src/utils/SobolNumberMapGenerator.js +80 -80
  67. package/src/utils/UVUnwrapper.js +101 -101
  68. package/src/utils/macroify.js +9 -0
  69. package/src/workers/PathTracingSceneWorker.js +42 -42
  70. package/src/materials/PhysicalPathTracingMaterial.js +0 -1013
  71. package/src/shader/shaderBvhAnyHit.js +0 -76
  72. package/src/shader/shaderEnvMapSampling.js +0 -58
  73. package/src/shader/shaderGGXFunctions.js +0 -100
  74. package/src/shader/shaderIridescenceFunctions.js +0 -135
  75. package/src/shader/shaderLightSampling.js +0 -229
  76. package/src/shader/shaderMaterialSampling.js +0 -510
  77. package/src/shader/shaderSheenFunctions.js +0 -98
  78. package/src/shader/shaderUtils.js +0 -377
@@ -1,377 +0,0 @@
1
- export const shaderUtils = /* glsl */`
2
-
3
- #ifndef RAY_OFFSET
4
- #define RAY_OFFSET 1e-4
5
- #endif
6
-
7
- // adjust the hit point by the surface normal by a factor of some offset and the
8
- // maximum component-wise value of the current point to accommodate floating point
9
- // error as values increase.
10
- vec3 stepRayOrigin( vec3 rayOrigin, vec3 rayDirection, vec3 offset, float dist ) {
11
-
12
- vec3 point = rayOrigin + rayDirection * dist;
13
- vec3 absPoint = abs( point );
14
- float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
15
- return point + offset * ( maxPoint + 1.0 ) * RAY_OFFSET;
16
-
17
- }
18
-
19
- // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_volume/README.md#attenuation
20
- vec3 transmissionAttenuation( float dist, vec3 attColor, float attDist ) {
21
-
22
- vec3 ot = - log( attColor ) / attDist;
23
- return exp( - ot * dist );
24
-
25
- }
26
-
27
- // https://google.github.io/filament/Filament.md.html#materialsystem/diffusebrdf
28
- float schlickFresnel( float cosine, float f0 ) {
29
-
30
- return f0 + ( 1.0 - f0 ) * pow( 1.0 - cosine, 5.0 );
31
-
32
- }
33
-
34
- vec3 schlickFresnel( float cosine, vec3 f0 ) {
35
-
36
- return f0 + ( 1.0 - f0 ) * pow( 1.0 - cosine, 5.0 );
37
-
38
- }
39
-
40
- float dielectricFresnel( float cosThetaI, float eta ) {
41
-
42
- // https://schuttejoe.github.io/post/disneybsdf/
43
- float ni = eta;
44
- float nt = 1.0;
45
-
46
- // Check for total internal reflection
47
- float sinThetaISq = 1.0f - cosThetaI * cosThetaI;
48
- float sinThetaTSq = eta * eta * sinThetaISq;
49
- if( sinThetaTSq >= 1.0 ) {
50
-
51
- return 1.0;
52
-
53
- }
54
-
55
- float sinThetaT = sqrt( sinThetaTSq );
56
-
57
- float cosThetaT = sqrt( max( 0.0, 1.0f - sinThetaT * sinThetaT ) );
58
- float rParallel = ( ( nt * cosThetaI ) - ( ni * cosThetaT ) ) / ( ( nt * cosThetaI ) + ( ni * cosThetaT ) );
59
- float rPerpendicular = ( ( ni * cosThetaI ) - ( nt * cosThetaT ) ) / ( ( ni * cosThetaI ) + ( nt * cosThetaT ) );
60
- return ( rParallel * rParallel + rPerpendicular * rPerpendicular ) / 2.0;
61
-
62
- }
63
-
64
- // https://raytracing.github.io/books/RayTracingInOneWeekend.html#dielectrics/schlickapproximation
65
- float iorRatioToF0( float eta ) {
66
-
67
- return pow( ( 1.0 - eta ) / ( 1.0 + eta ), 2.0 );
68
-
69
- }
70
-
71
- // forms a basis with the normal vector as Z
72
- mat3 getBasisFromNormal( vec3 normal ) {
73
-
74
- vec3 other;
75
- if ( abs( normal.x ) > 0.5 ) {
76
-
77
- other = vec3( 0.0, 1.0, 0.0 );
78
-
79
- } else {
80
-
81
- other = vec3( 1.0, 0.0, 0.0 );
82
-
83
- }
84
-
85
- vec3 ortho = normalize( cross( normal, other ) );
86
- vec3 ortho2 = normalize( cross( normal, ortho ) );
87
- return mat3( ortho2, ortho, normal );
88
-
89
- }
90
-
91
- vec3 getHalfVector( vec3 wi, vec3 wo, float eta ) {
92
-
93
- // get the half vector - assuming if the light incident vector is on the other side
94
- // of the that it's transmissive.
95
- vec3 h;
96
- if ( wi.z > 0.0 ) {
97
-
98
- h = normalize( wi + wo );
99
-
100
- } else {
101
-
102
- // Scale by the ior ratio to retrieve the appropriate half vector
103
- // From Section 2.2 on computing the transmission half vector:
104
- // https://blog.selfshadow.com/publications/s2015-shading-course/burley/s2015_pbs_disney_bsdf_notes.pdf
105
- h = normalize( wi + wo * eta );
106
-
107
- }
108
-
109
- h *= sign( h.z );
110
- return h;
111
-
112
- }
113
-
114
- vec3 getHalfVector( vec3 a, vec3 b ) {
115
-
116
- return normalize( a + b );
117
-
118
- }
119
-
120
- // The discrepancy between interpolated surface normal and geometry normal can cause issues when a ray
121
- // is cast that is on the top side of the geometry normal plane but below the surface normal plane. If
122
- // we find a ray like that we ignore it to avoid artifacts.
123
- // This function returns if the direction is on the same side of both planes.
124
- bool isDirectionValid( vec3 direction, vec3 surfaceNormal, vec3 geometryNormal ) {
125
-
126
- bool aboveSurfaceNormal = dot( direction, surfaceNormal ) > 0.0;
127
- bool aboveGeometryNormal = dot( direction, geometryNormal ) > 0.0;
128
- return aboveSurfaceNormal == aboveGeometryNormal;
129
-
130
- }
131
-
132
- vec3 getHemisphereSample( vec3 n, vec2 uv ) {
133
-
134
- // https://www.rorydriscoll.com/2009/01/07/better-sampling/
135
- // https://graphics.pixar.com/library/OrthonormalB/paper.pdf
136
- float sign = n.z == 0.0 ? 1.0 : sign( n.z );
137
- float a = - 1.0 / ( sign + n.z );
138
- float b = n.x * n.y * a;
139
- vec3 b1 = vec3( 1.0 + sign * n.x * n.x * a, sign * b, - sign * n.x );
140
- vec3 b2 = vec3( b, sign + n.y * n.y * a, - n.y );
141
-
142
- float r = sqrt( uv.x );
143
- float theta = 2.0 * PI * uv.y;
144
- float x = r * cos( theta );
145
- float y = r * sin( theta );
146
- return x * b1 + y * b2 + sqrt( 1.0 - uv.x ) * n;
147
-
148
- }
149
-
150
- vec2 sampleTriangle( vec2 a, vec2 b, vec2 c, vec2 r ) {
151
-
152
- // get the edges of the triangle and the diagonal across the
153
- // center of the parallelogram
154
- vec2 e1 = a - b;
155
- vec2 e2 = c - b;
156
- vec2 diag = normalize( e1 + e2 );
157
-
158
- // pick the point in the parallelogram
159
- if ( r.x + r.y > 1.0 ) {
160
-
161
- r = vec2( 1.0 ) - r;
162
-
163
- }
164
-
165
- return e1 * r.x + e2 * r.y;
166
-
167
- }
168
-
169
- vec2 sampleCircle( vec2 uv ) {
170
-
171
- float angle = 2.0 * PI * uv.x;
172
- float radius = sqrt( uv.y );
173
- return vec2( cos( angle ), sin( angle ) ) * radius;
174
-
175
- }
176
-
177
- vec3 sampleSphere( vec2 uv ) {
178
-
179
- float u = ( uv.x - 0.5 ) * 2.0;
180
- float t = uv.y * PI * 2.0;
181
- float f = sqrt( 1.0 - u * u );
182
-
183
- return vec3( f * cos( t ), f * sin( t ), u );
184
-
185
- }
186
-
187
- vec2 sampleRegularNGon( int sides, vec3 uvw ) {
188
-
189
- sides = max( sides, 3 );
190
-
191
- vec3 r = uvw;
192
- float anglePerSegment = 2.0 * PI / float( sides );
193
- float segment = floor( float( sides ) * r.x );
194
-
195
- float angle1 = anglePerSegment * segment;
196
- float angle2 = angle1 + anglePerSegment;
197
- vec2 a = vec2( sin( angle1 ), cos( angle1 ) );
198
- vec2 b = vec2( 0.0, 0.0 );
199
- vec2 c = vec2( sin( angle2 ), cos( angle2 ) );
200
-
201
- return sampleTriangle( a, b, c, r.yz );
202
-
203
- }
204
-
205
- // samples an aperture shape with the given number of sides. 0 means circle
206
- vec2 sampleAperture( int blades, vec3 uvw ) {
207
-
208
- return blades == 0 ?
209
- sampleCircle( uvw.xy ) :
210
- sampleRegularNGon( blades, uvw );
211
-
212
- }
213
-
214
- // ray sampling x and z are swapped to align with expected background view
215
- vec2 equirectDirectionToUv( vec3 direction ) {
216
-
217
- // from Spherical.setFromCartesianCoords
218
- vec2 uv = vec2( atan( direction.z, direction.x ), acos( direction.y ) );
219
- uv /= vec2( 2.0 * PI, PI );
220
-
221
- // apply adjustments to get values in range [0, 1] and y right side up
222
- uv.x += 0.5;
223
- uv.y = 1.0 - uv.y;
224
- return uv;
225
-
226
- }
227
-
228
- vec3 equirectUvToDirection( vec2 uv ) {
229
-
230
- // undo above adjustments
231
- uv.x -= 0.5;
232
- uv.y = 1.0 - uv.y;
233
-
234
- // from Vector3.setFromSphericalCoords
235
- float theta = uv.x * 2.0 * PI;
236
- float phi = uv.y * PI;
237
-
238
- float sinPhi = sin( phi );
239
-
240
- return vec3( sinPhi * cos( theta ), cos( phi ), sinPhi * sin( theta ) );
241
-
242
- }
243
-
244
- // Fast arccos approximation used to remove banding artifacts caused by numerical errors in acos.
245
- // This is a cubic Lagrange interpolating polynomial for x = [-1, -1/2, 0, 1/2, 1].
246
- // For more information see: https://github.com/gkjohnson/three-gpu-pathtracer/pull/171#issuecomment-1152275248
247
- float acosApprox( float x ) {
248
-
249
- x = clamp( x, -1.0, 1.0 );
250
- return ( - 0.69813170079773212 * x * x - 0.87266462599716477 ) * x + 1.5707963267948966;
251
-
252
- }
253
-
254
- // An acos with input values bound to the range [-1, 1].
255
- float acosSafe( float x ) {
256
-
257
- return acos( clamp( x, -1.0, 1.0 ) );
258
-
259
- }
260
-
261
- float saturateCos( float val ) {
262
-
263
- return clamp( val, 0.001, 1.0 );
264
-
265
- }
266
-
267
- float square( float t ) {
268
-
269
- return t * t;
270
-
271
- }
272
-
273
- vec2 square( vec2 t ) {
274
-
275
- return t * t;
276
-
277
- }
278
-
279
- vec3 square( vec3 t ) {
280
-
281
- return t * t;
282
-
283
- }
284
-
285
- vec4 square( vec4 t ) {
286
-
287
- return t * t;
288
-
289
- }
290
-
291
- vec2 rotateVector( vec2 v, float t ) {
292
-
293
- float ac = cos( t );
294
- float as = sin( t );
295
- return vec2(
296
- v.x * ac - v.y * as,
297
- v.x * as + v.y * ac
298
- );
299
-
300
- }
301
-
302
- // Finds the point where the ray intersects the plane defined by u and v and checks if this point
303
- // falls in the bounds of the rectangle on that same plane.
304
- // Plane intersection: https://lousodrome.net/blog/light/2020/07/03/intersection-of-a-ray-and-a-plane/
305
- bool intersectsRectangle( vec3 center, vec3 normal, vec3 u, vec3 v, vec3 rayOrigin, vec3 rayDirection, out float dist ) {
306
-
307
- float t = dot( center - rayOrigin, normal ) / dot( rayDirection, normal );
308
-
309
- if ( t > EPSILON ) {
310
-
311
- vec3 p = rayOrigin + rayDirection * t;
312
- vec3 vi = p - center;
313
-
314
- // check if p falls inside the rectangle
315
- float a1 = dot( u, vi );
316
- if ( abs( a1 ) <= 0.5 ) {
317
-
318
- float a2 = dot( v, vi );
319
- if ( abs( a2 ) <= 0.5 ) {
320
-
321
- dist = t;
322
- return true;
323
-
324
- }
325
-
326
- }
327
-
328
- }
329
-
330
- return false;
331
-
332
- }
333
-
334
- // Finds the point where the ray intersects the plane defined by u and v and checks if this point
335
- // falls in the bounds of the circle on that same plane. See above URL for a description of the plane intersection algorithm.
336
- bool intersectsCircle( vec3 position, vec3 normal, vec3 u, vec3 v, vec3 rayOrigin, vec3 rayDirection, out float dist ) {
337
-
338
- float t = dot( position - rayOrigin, normal ) / dot( rayDirection, normal );
339
-
340
- if ( t > EPSILON ) {
341
-
342
- vec3 hit = rayOrigin + rayDirection * t;
343
- vec3 vi = hit - position;
344
-
345
- float a1 = dot( u, vi );
346
- float a2 = dot( v, vi );
347
-
348
- if( length( vec2( a1, a2 ) ) <= 0.5 ) {
349
-
350
- dist = t;
351
- return true;
352
-
353
- }
354
-
355
- }
356
-
357
- return false;
358
-
359
- }
360
-
361
- // power heuristic for multiple importance sampling
362
- float misHeuristic( float a, float b ) {
363
-
364
- float aa = a * a;
365
- float bb = b * b;
366
- return aa / ( aa + bb );
367
-
368
- }
369
-
370
- // tentFilter from Peter Shirley's 'Realistic Ray Tracing (2nd Edition)' book, pg. 60
371
- // erichlof/THREE.js-PathTracing-Renderer/
372
- float tentFilter( float x ) {
373
-
374
- return x < 0.5 ? sqrt( 2.0 * x ) - 1.0 : 1.0 - sqrt( 2.0 - ( 2.0 * x ) );
375
-
376
- }
377
- `;