three-gpu-pathtracer 0.0.6 → 0.0.8

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 (49) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +887 -781
  3. package/build/index.module.js +6070 -5224
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +6071 -5221
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +69 -68
  8. package/src/core/DynamicPathTracingSceneGenerator.js +119 -119
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +270 -255
  11. package/src/core/PathTracingSceneGenerator.js +69 -68
  12. package/src/index.js +39 -33
  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/DenoiseMaterial.js +142 -0
  17. package/src/materials/GraphMaterial.js +243 -0
  18. package/src/materials/LambertPathTracingMaterial.js +285 -285
  19. package/src/materials/MaterialBase.js +56 -56
  20. package/src/materials/PhysicalPathTracingMaterial.js +973 -922
  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/shaderEnvMapSampling.js +59 -59
  26. package/src/shader/shaderGGXFunctions.js +100 -108
  27. package/src/shader/shaderIridescenceFunctions.js +130 -130
  28. package/src/shader/shaderLayerTexelFetchFunctions.js +25 -0
  29. package/src/shader/shaderLightSampling.js +231 -231
  30. package/src/shader/shaderMaterialSampling.js +504 -546
  31. package/src/shader/shaderSheenFunctions.js +98 -98
  32. package/src/shader/shaderStructs.js +321 -307
  33. package/src/shader/shaderUtils.js +403 -350
  34. package/src/textures/GradientEquirectTexture.js +35 -0
  35. package/src/textures/ProceduralEquirectTexture.js +75 -0
  36. package/src/uniforms/AttributesTextureArray.js +35 -0
  37. package/src/uniforms/EquirectHdrInfoUniform.js +259 -259
  38. package/src/uniforms/FloatAttributeTextureArray.js +169 -0
  39. package/src/uniforms/IESProfilesTexture.js +100 -100
  40. package/src/uniforms/LightsInfoUniformStruct.js +162 -162
  41. package/src/uniforms/MaterialsTexture.js +420 -406
  42. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  43. package/src/uniforms/RenderTarget2DArray.js +97 -93
  44. package/src/uniforms/utils.js +21 -0
  45. package/src/utils/BlurredEnvMapGenerator.js +116 -113
  46. package/src/utils/GeometryPreparationUtils.js +214 -194
  47. package/src/utils/IESLoader.js +325 -325
  48. package/src/utils/UVUnwrapper.js +101 -101
  49. package/src/workers/PathTracingSceneWorker.js +42 -42
@@ -1,350 +1,403 @@
1
- export const shaderUtils = /* glsl */`
2
-
3
- // https://google.github.io/filament/Filament.md.html#materialsystem/diffusebrdf
4
- float schlickFresnel( float cosine, float f0 ) {
5
-
6
- return f0 + ( 1.0 - f0 ) * pow( 1.0 - cosine, 5.0 );
7
-
8
- }
9
-
10
- vec3 schlickFresnel( float cosine, vec3 f0 ) {
11
-
12
- return f0 + ( 1.0 - f0 ) * pow( 1.0 - cosine, 5.0 );
13
-
14
- }
15
-
16
- // https://raytracing.github.io/books/RayTracingInOneWeekend.html#dielectrics/schlickapproximation
17
- float schlickFresnelFromIor( float cosine, float iorRatio ) {
18
-
19
- // Schlick approximation
20
- float r_0 = pow( ( 1.0 - iorRatio ) / ( 1.0 + iorRatio ), 2.0 );
21
- return schlickFresnel( cosine, r_0 );
22
-
23
- }
24
-
25
- // forms a basis with the normal vector as Z
26
- mat3 getBasisFromNormal( vec3 normal ) {
27
-
28
- vec3 other;
29
- if ( abs( normal.x ) > 0.5 ) {
30
-
31
- other = vec3( 0.0, 1.0, 0.0 );
32
-
33
- } else {
34
-
35
- other = vec3( 1.0, 0.0, 0.0 );
36
-
37
- }
38
-
39
- vec3 ortho = normalize( cross( normal, other ) );
40
- vec3 ortho2 = normalize( cross( normal, ortho ) );
41
- return mat3( ortho2, ortho, normal );
42
-
43
- }
44
-
45
- vec3 getHalfVector( vec3 a, vec3 b ) {
46
-
47
- return normalize( a + b );
48
-
49
- }
50
-
51
- // The discrepancy between interpolated surface normal and geometry normal can cause issues when a ray
52
- // is cast that is on the top side of the geometry normal plane but below the surface normal plane. If
53
- // we find a ray like that we ignore it to avoid artifacts.
54
- // This function returns if the direction is on the same side of both planes.
55
- bool isDirectionValid( vec3 direction, vec3 surfaceNormal, vec3 geometryNormal ) {
56
-
57
- bool aboveSurfaceNormal = dot( direction, surfaceNormal ) > 0.0;
58
- bool aboveGeometryNormal = dot( direction, geometryNormal ) > 0.0;
59
- return aboveSurfaceNormal == aboveGeometryNormal;
60
-
61
- }
62
-
63
- vec3 getHemisphereSample( vec3 n, vec2 uv ) {
64
-
65
- // https://www.rorydriscoll.com/2009/01/07/better-sampling/
66
- // https://graphics.pixar.com/library/OrthonormalB/paper.pdf
67
- float sign = n.z == 0.0 ? 1.0 : sign( n.z );
68
- float a = - 1.0 / ( sign + n.z );
69
- float b = n.x * n.y * a;
70
- vec3 b1 = vec3( 1.0 + sign * n.x * n.x * a, sign * b, - sign * n.x );
71
- vec3 b2 = vec3( b, sign + n.y * n.y * a, - n.y );
72
-
73
- float r = sqrt( uv.x );
74
- float theta = 2.0 * PI * uv.y;
75
- float x = r * cos( theta );
76
- float y = r * sin( theta );
77
- return x * b1 + y * b2 + sqrt( 1.0 - uv.x ) * n;
78
-
79
- }
80
-
81
- // https://www.shadertoy.com/view/wltcRS
82
- uvec4 s0;
83
-
84
- void rng_initialize(vec2 p, int frame) {
85
-
86
- // white noise seed
87
- s0 = uvec4( p, uint( frame ), uint( p.x ) + uint( p.y ) );
88
-
89
- }
90
-
91
- // https://www.pcg-random.org/
92
- void pcg4d( inout uvec4 v ) {
93
-
94
- v = v * 1664525u + 1013904223u;
95
- v.x += v.y * v.w;
96
- v.y += v.z * v.x;
97
- v.z += v.x * v.y;
98
- v.w += v.y * v.z;
99
- v = v ^ ( v >> 16u );
100
- v.x += v.y*v.w;
101
- v.y += v.z*v.x;
102
- v.z += v.x*v.y;
103
- v.w += v.y*v.z;
104
-
105
- }
106
-
107
- // returns [ 0, 1 ]
108
- float rand() {
109
-
110
- pcg4d(s0);
111
- return float( s0.x ) / float( 0xffffffffu );
112
-
113
- }
114
-
115
- vec2 rand2() {
116
-
117
- pcg4d( s0 );
118
- return vec2( s0.xy ) / float(0xffffffffu);
119
-
120
- }
121
-
122
- vec3 rand3() {
123
-
124
- pcg4d(s0);
125
- return vec3( s0.xyz ) / float( 0xffffffffu );
126
-
127
- }
128
-
129
- vec4 rand4() {
130
-
131
- pcg4d(s0);
132
- return vec4(s0)/float(0xffffffffu);
133
-
134
- }
135
-
136
- // https://github.com/mrdoob/three.js/blob/dev/src/math/Vector3.js#L724
137
- vec3 randDirection() {
138
-
139
- vec2 r = rand2();
140
- float u = ( r.x - 0.5 ) * 2.0;
141
- float t = r.y * PI * 2.0;
142
- float f = sqrt( 1.0 - u * u );
143
-
144
- return vec3( f * cos( t ), f * sin( t ), u );
145
-
146
- }
147
-
148
- vec2 triangleSample( vec2 a, vec2 b, vec2 c ) {
149
-
150
- // get the edges of the triangle and the diagonal across the
151
- // center of the parallelogram
152
- vec2 e1 = a - b;
153
- vec2 e2 = c - b;
154
- vec2 diag = normalize( e1 + e2 );
155
-
156
- // pick a random point in the parallelogram
157
- vec2 r = rand2();
158
- if ( r.x + r.y > 1.0 ) {
159
-
160
- r = vec2( 1.0 ) - r;
161
-
162
- }
163
-
164
- return e1 * r.x + e2 * r.y;
165
-
166
- }
167
-
168
- // samples an aperture shape with the given number of sides. 0 means circle
169
- vec2 sampleAperture( int blades ) {
170
-
171
- if ( blades == 0 ) {
172
-
173
- vec2 r = rand2();
174
- float angle = 2.0 * PI * r.x;
175
- float radius = sqrt( rand() );
176
- return vec2( cos( angle ), sin( angle ) ) * radius;
177
-
178
- } else {
179
-
180
- blades = max( blades, 3 );
181
-
182
- vec3 r = rand3();
183
- float anglePerSegment = 2.0 * PI / float( blades );
184
- float segment = floor( float( blades ) * r.x );
185
-
186
- float angle1 = anglePerSegment * segment;
187
- float angle2 = angle1 + anglePerSegment;
188
- vec2 a = vec2( sin( angle1 ), cos( angle1 ) );
189
- vec2 b = vec2( 0.0, 0.0 );
190
- vec2 c = vec2( sin( angle2 ), cos( angle2 ) );
191
-
192
- return triangleSample( a, b, c );
193
-
194
- }
195
-
196
- }
197
-
198
- float colorToLuminance( vec3 color ) {
199
-
200
- // https://en.wikipedia.org/wiki/Relative_luminance
201
- return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
202
-
203
- }
204
-
205
- // ray sampling x and z are swapped to align with expected background view
206
- vec2 equirectDirectionToUv( vec3 direction ) {
207
-
208
- // from Spherical.setFromCartesianCoords
209
- vec2 uv = vec2( atan( direction.z, direction.x ), acos( direction.y ) );
210
- uv /= vec2( 2.0 * PI, PI );
211
-
212
- // apply adjustments to get values in range [0, 1] and y right side up
213
- uv.x += 0.5;
214
- uv.y = 1.0 - uv.y;
215
- return uv;
216
-
217
- }
218
-
219
- vec3 equirectUvToDirection( vec2 uv ) {
220
-
221
- // undo above adjustments
222
- uv.x -= 0.5;
223
- uv.y = 1.0 - uv.y;
224
-
225
- // from Vector3.setFromSphericalCoords
226
- float theta = uv.x * 2.0 * PI;
227
- float phi = uv.y * PI;
228
-
229
- float sinPhi = sin( phi );
230
-
231
- return vec3( sinPhi * cos( theta ), cos( phi ), sinPhi * sin( theta ) );
232
-
233
- }
234
-
235
- // Fast arccos approximation used to remove banding artifacts caused by numerical errors in acos.
236
- // This is a cubic Lagrange interpolating polynomial for x = [-1, -1/2, 0, 1/2, 1].
237
- // For more information see: https://github.com/gkjohnson/three-gpu-pathtracer/pull/171#issuecomment-1152275248
238
- float acosApprox( float x ) {
239
-
240
- x = clamp( x, -1.0, 1.0 );
241
- return ( - 0.69813170079773212 * x * x - 0.87266462599716477 ) * x + 1.5707963267948966;
242
-
243
- }
244
-
245
- // An acos with input values bound to the range [-1, 1].
246
- float acosSafe( float x ) {
247
-
248
- return acos( clamp( x, -1.0, 1.0 ) );
249
-
250
- }
251
-
252
- float saturateCos( float val ) {
253
-
254
- return clamp( val, 0.001, 1.0 );
255
-
256
- }
257
-
258
- float square( float t ) {
259
-
260
- return t * t;
261
-
262
- }
263
-
264
- vec2 square( vec2 t) {
265
-
266
- return t * t;
267
-
268
- }
269
-
270
- vec3 square( vec3 t ) {
271
-
272
- return t * t;
273
-
274
- }
275
-
276
- vec4 square( vec4 t ) {
277
-
278
- return t * t;
279
-
280
- }
281
-
282
- // Finds the point where the ray intersects the plane defined by u and v and checks if this point
283
- // falls in the bounds of the rectangle on that same plane.
284
- // Plane intersection: https://lousodrome.net/blog/light/2020/07/03/intersection-of-a-ray-and-a-plane/
285
- bool intersectsRectangle( vec3 center, vec3 normal, vec3 u, vec3 v, vec3 rayOrigin, vec3 rayDirection, out float dist ) {
286
-
287
- float t = dot( center - rayOrigin, normal ) / dot( rayDirection, normal );
288
-
289
- if ( t > EPSILON ) {
290
-
291
- vec3 p = rayOrigin + rayDirection * t;
292
- vec3 vi = p - center;
293
-
294
- // check if p falls inside the rectangle
295
- float a1 = dot( u, vi );
296
- if ( abs( a1 ) <= 0.5 ) {
297
-
298
- float a2 = dot( v, vi );
299
- if ( abs( a2 ) <= 0.5 ) {
300
-
301
- dist = t;
302
- return true;
303
-
304
- }
305
-
306
- }
307
-
308
- }
309
-
310
- return false;
311
-
312
- }
313
-
314
- // Finds the point where the ray intersects the plane defined by u and v and checks if this point
315
- // falls in the bounds of the circle on that same plane. See above URL for a description of the plane intersection algorithm.
316
- bool intersectsCircle( vec3 position, vec3 normal, vec3 u, vec3 v, vec3 rayOrigin, vec3 rayDirection, out float dist ) {
317
-
318
- float t = dot( position - rayOrigin, normal ) / dot( rayDirection, normal );
319
-
320
- if ( t > EPSILON ) {
321
-
322
- vec3 hit = rayOrigin + rayDirection * t;
323
- vec3 vi = hit - position;
324
-
325
- float a1 = dot( u, vi );
326
- float a2 = dot( v, vi );
327
-
328
- if( length( vec2( a1, a2 ) ) <= 0.5 ) {
329
-
330
- dist = t;
331
- return true;
332
-
333
- }
334
-
335
- }
336
-
337
- return false;
338
-
339
- }
340
-
341
- // power heuristic for multiple importance sampling
342
- float misHeuristic( float a, float b ) {
343
-
344
- float aa = a * a;
345
- float bb = b * b;
346
- return aa / ( aa + bb );
347
-
348
- }
349
-
350
- `;
1
+ export const shaderUtils = /* glsl */`
2
+
3
+ // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_volume/README.md#attenuation
4
+ vec3 transmissionAttenuation( float dist, vec3 attColor, float attDist ) {
5
+
6
+ vec3 ot = - log( attColor ) / attDist;
7
+ return exp( - ot * dist );
8
+
9
+ }
10
+
11
+ // https://google.github.io/filament/Filament.md.html#materialsystem/diffusebrdf
12
+ float schlickFresnel( float cosine, float f0 ) {
13
+
14
+ return f0 + ( 1.0 - f0 ) * pow( 1.0 - cosine, 5.0 );
15
+
16
+ }
17
+
18
+ vec3 schlickFresnel( float cosine, vec3 f0 ) {
19
+
20
+ return f0 + ( 1.0 - f0 ) * pow( 1.0 - cosine, 5.0 );
21
+
22
+ }
23
+
24
+ float dielectricFresnel( float cosThetaI, float eta ) {
25
+
26
+ // https://schuttejoe.github.io/post/disneybsdf/
27
+ float ni = eta;
28
+ float nt = 1.0;
29
+
30
+ // Check for total internal reflection
31
+ float sinThetaISq = 1.0f - cosThetaI * cosThetaI;
32
+ float sinThetaTSq = eta * eta * sinThetaISq;
33
+ if( sinThetaTSq >= 1.0 ) {
34
+
35
+ return 1.0;
36
+
37
+ }
38
+
39
+ float sinThetaT = sqrt( sinThetaTSq );
40
+
41
+ float cosThetaT = sqrt( max( 0.0, 1.0f - sinThetaT * sinThetaT ) );
42
+ float rParallel = ( ( nt * cosThetaI ) - ( ni * cosThetaT ) ) / ( ( nt * cosThetaI ) + ( ni * cosThetaT ) );
43
+ float rPerpendicular = ( ( ni * cosThetaI ) - ( nt * cosThetaT ) ) / ( ( ni * cosThetaI ) + ( nt * cosThetaT ) );
44
+ return ( rParallel * rParallel + rPerpendicular * rPerpendicular ) / 2.0;
45
+
46
+ }
47
+
48
+ // https://raytracing.github.io/books/RayTracingInOneWeekend.html#dielectrics/schlickapproximation
49
+ float iorRatioToF0( float eta ) {
50
+
51
+ return pow( ( 1.0 - eta ) / ( 1.0 + eta ), 2.0 );
52
+
53
+ }
54
+
55
+ // forms a basis with the normal vector as Z
56
+ mat3 getBasisFromNormal( vec3 normal ) {
57
+
58
+ vec3 other;
59
+ if ( abs( normal.x ) > 0.5 ) {
60
+
61
+ other = vec3( 0.0, 1.0, 0.0 );
62
+
63
+ } else {
64
+
65
+ other = vec3( 1.0, 0.0, 0.0 );
66
+
67
+ }
68
+
69
+ vec3 ortho = normalize( cross( normal, other ) );
70
+ vec3 ortho2 = normalize( cross( normal, ortho ) );
71
+ return mat3( ortho2, ortho, normal );
72
+
73
+ }
74
+
75
+ vec3 getHalfVector( vec3 wi, vec3 wo, float eta ) {
76
+
77
+ // get the half vector - assuming if the light incident vector is on the other side
78
+ // of the that it's transmissive.
79
+ vec3 h;
80
+ if ( wi.z > 0.0 ) {
81
+
82
+ h = normalize( wi + wo );
83
+
84
+ } else {
85
+
86
+ // Scale by the ior ratio to retrieve the appropriate half vector
87
+ // From Section 2.2 on computing the transmission half vector:
88
+ // https://blog.selfshadow.com/publications/s2015-shading-course/burley/s2015_pbs_disney_bsdf_notes.pdf
89
+ h = normalize( wi + wo * eta );
90
+
91
+ }
92
+
93
+ h *= sign( h.z );
94
+ return h;
95
+
96
+ }
97
+
98
+ vec3 getHalfVector( vec3 a, vec3 b ) {
99
+
100
+ return normalize( a + b );
101
+
102
+ }
103
+
104
+ // The discrepancy between interpolated surface normal and geometry normal can cause issues when a ray
105
+ // is cast that is on the top side of the geometry normal plane but below the surface normal plane. If
106
+ // we find a ray like that we ignore it to avoid artifacts.
107
+ // This function returns if the direction is on the same side of both planes.
108
+ bool isDirectionValid( vec3 direction, vec3 surfaceNormal, vec3 geometryNormal ) {
109
+
110
+ bool aboveSurfaceNormal = dot( direction, surfaceNormal ) > 0.0;
111
+ bool aboveGeometryNormal = dot( direction, geometryNormal ) > 0.0;
112
+ return aboveSurfaceNormal == aboveGeometryNormal;
113
+
114
+ }
115
+
116
+ vec3 getHemisphereSample( vec3 n, vec2 uv ) {
117
+
118
+ // https://www.rorydriscoll.com/2009/01/07/better-sampling/
119
+ // https://graphics.pixar.com/library/OrthonormalB/paper.pdf
120
+ float sign = n.z == 0.0 ? 1.0 : sign( n.z );
121
+ float a = - 1.0 / ( sign + n.z );
122
+ float b = n.x * n.y * a;
123
+ vec3 b1 = vec3( 1.0 + sign * n.x * n.x * a, sign * b, - sign * n.x );
124
+ vec3 b2 = vec3( b, sign + n.y * n.y * a, - n.y );
125
+
126
+ float r = sqrt( uv.x );
127
+ float theta = 2.0 * PI * uv.y;
128
+ float x = r * cos( theta );
129
+ float y = r * sin( theta );
130
+ return x * b1 + y * b2 + sqrt( 1.0 - uv.x ) * n;
131
+
132
+ }
133
+
134
+ // https://www.shadertoy.com/view/wltcRS
135
+ uvec4 s0;
136
+
137
+ void rng_initialize(vec2 p, int frame) {
138
+
139
+ // white noise seed
140
+ s0 = uvec4( p, uint( frame ), uint( p.x ) + uint( p.y ) );
141
+
142
+ }
143
+
144
+ // https://www.pcg-random.org/
145
+ void pcg4d( inout uvec4 v ) {
146
+
147
+ v = v * 1664525u + 1013904223u;
148
+ v.x += v.y * v.w;
149
+ v.y += v.z * v.x;
150
+ v.z += v.x * v.y;
151
+ v.w += v.y * v.z;
152
+ v = v ^ ( v >> 16u );
153
+ v.x += v.y*v.w;
154
+ v.y += v.z*v.x;
155
+ v.z += v.x*v.y;
156
+ v.w += v.y*v.z;
157
+
158
+ }
159
+
160
+ // returns [ 0, 1 ]
161
+ float rand() {
162
+
163
+ pcg4d(s0);
164
+ return float( s0.x ) / float( 0xffffffffu );
165
+
166
+ }
167
+
168
+ vec2 rand2() {
169
+
170
+ pcg4d( s0 );
171
+ return vec2( s0.xy ) / float(0xffffffffu);
172
+
173
+ }
174
+
175
+ vec3 rand3() {
176
+
177
+ pcg4d(s0);
178
+ return vec3( s0.xyz ) / float( 0xffffffffu );
179
+
180
+ }
181
+
182
+ vec4 rand4() {
183
+
184
+ pcg4d(s0);
185
+ return vec4(s0)/float(0xffffffffu);
186
+
187
+ }
188
+
189
+ // https://github.com/mrdoob/three.js/blob/dev/src/math/Vector3.js#L724
190
+ vec3 randDirection() {
191
+
192
+ vec2 r = rand2();
193
+ float u = ( r.x - 0.5 ) * 2.0;
194
+ float t = r.y * PI * 2.0;
195
+ float f = sqrt( 1.0 - u * u );
196
+
197
+ return vec3( f * cos( t ), f * sin( t ), u );
198
+
199
+ }
200
+
201
+ vec2 triangleSample( vec2 a, vec2 b, vec2 c ) {
202
+
203
+ // get the edges of the triangle and the diagonal across the
204
+ // center of the parallelogram
205
+ vec2 e1 = a - b;
206
+ vec2 e2 = c - b;
207
+ vec2 diag = normalize( e1 + e2 );
208
+
209
+ // pick a random point in the parallelogram
210
+ vec2 r = rand2();
211
+ if ( r.x + r.y > 1.0 ) {
212
+
213
+ r = vec2( 1.0 ) - r;
214
+
215
+ }
216
+
217
+ return e1 * r.x + e2 * r.y;
218
+
219
+ }
220
+
221
+ // samples an aperture shape with the given number of sides. 0 means circle
222
+ vec2 sampleAperture( int blades ) {
223
+
224
+ if ( blades == 0 ) {
225
+
226
+ vec2 r = rand2();
227
+ float angle = 2.0 * PI * r.x;
228
+ float radius = sqrt( rand() );
229
+ return vec2( cos( angle ), sin( angle ) ) * radius;
230
+
231
+ } else {
232
+
233
+ blades = max( blades, 3 );
234
+
235
+ vec3 r = rand3();
236
+ float anglePerSegment = 2.0 * PI / float( blades );
237
+ float segment = floor( float( blades ) * r.x );
238
+
239
+ float angle1 = anglePerSegment * segment;
240
+ float angle2 = angle1 + anglePerSegment;
241
+ vec2 a = vec2( sin( angle1 ), cos( angle1 ) );
242
+ vec2 b = vec2( 0.0, 0.0 );
243
+ vec2 c = vec2( sin( angle2 ), cos( angle2 ) );
244
+
245
+ return triangleSample( a, b, c );
246
+
247
+ }
248
+
249
+ }
250
+
251
+ float colorToLuminance( vec3 color ) {
252
+
253
+ // https://en.wikipedia.org/wiki/Relative_luminance
254
+ return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
255
+
256
+ }
257
+
258
+ // ray sampling x and z are swapped to align with expected background view
259
+ vec2 equirectDirectionToUv( vec3 direction ) {
260
+
261
+ // from Spherical.setFromCartesianCoords
262
+ vec2 uv = vec2( atan( direction.z, direction.x ), acos( direction.y ) );
263
+ uv /= vec2( 2.0 * PI, PI );
264
+
265
+ // apply adjustments to get values in range [0, 1] and y right side up
266
+ uv.x += 0.5;
267
+ uv.y = 1.0 - uv.y;
268
+ return uv;
269
+
270
+ }
271
+
272
+ vec3 equirectUvToDirection( vec2 uv ) {
273
+
274
+ // undo above adjustments
275
+ uv.x -= 0.5;
276
+ uv.y = 1.0 - uv.y;
277
+
278
+ // from Vector3.setFromSphericalCoords
279
+ float theta = uv.x * 2.0 * PI;
280
+ float phi = uv.y * PI;
281
+
282
+ float sinPhi = sin( phi );
283
+
284
+ return vec3( sinPhi * cos( theta ), cos( phi ), sinPhi * sin( theta ) );
285
+
286
+ }
287
+
288
+ // Fast arccos approximation used to remove banding artifacts caused by numerical errors in acos.
289
+ // This is a cubic Lagrange interpolating polynomial for x = [-1, -1/2, 0, 1/2, 1].
290
+ // For more information see: https://github.com/gkjohnson/three-gpu-pathtracer/pull/171#issuecomment-1152275248
291
+ float acosApprox( float x ) {
292
+
293
+ x = clamp( x, -1.0, 1.0 );
294
+ return ( - 0.69813170079773212 * x * x - 0.87266462599716477 ) * x + 1.5707963267948966;
295
+
296
+ }
297
+
298
+ // An acos with input values bound to the range [-1, 1].
299
+ float acosSafe( float x ) {
300
+
301
+ return acos( clamp( x, -1.0, 1.0 ) );
302
+
303
+ }
304
+
305
+ float saturateCos( float val ) {
306
+
307
+ return clamp( val, 0.001, 1.0 );
308
+
309
+ }
310
+
311
+ float square( float t ) {
312
+
313
+ return t * t;
314
+
315
+ }
316
+
317
+ vec2 square( vec2 t ) {
318
+
319
+ return t * t;
320
+
321
+ }
322
+
323
+ vec3 square( vec3 t ) {
324
+
325
+ return t * t;
326
+
327
+ }
328
+
329
+ vec4 square( vec4 t ) {
330
+
331
+ return t * t;
332
+
333
+ }
334
+
335
+ // Finds the point where the ray intersects the plane defined by u and v and checks if this point
336
+ // falls in the bounds of the rectangle on that same plane.
337
+ // Plane intersection: https://lousodrome.net/blog/light/2020/07/03/intersection-of-a-ray-and-a-plane/
338
+ bool intersectsRectangle( vec3 center, vec3 normal, vec3 u, vec3 v, vec3 rayOrigin, vec3 rayDirection, out float dist ) {
339
+
340
+ float t = dot( center - rayOrigin, normal ) / dot( rayDirection, normal );
341
+
342
+ if ( t > EPSILON ) {
343
+
344
+ vec3 p = rayOrigin + rayDirection * t;
345
+ vec3 vi = p - center;
346
+
347
+ // check if p falls inside the rectangle
348
+ float a1 = dot( u, vi );
349
+ if ( abs( a1 ) <= 0.5 ) {
350
+
351
+ float a2 = dot( v, vi );
352
+ if ( abs( a2 ) <= 0.5 ) {
353
+
354
+ dist = t;
355
+ return true;
356
+
357
+ }
358
+
359
+ }
360
+
361
+ }
362
+
363
+ return false;
364
+
365
+ }
366
+
367
+ // Finds the point where the ray intersects the plane defined by u and v and checks if this point
368
+ // falls in the bounds of the circle on that same plane. See above URL for a description of the plane intersection algorithm.
369
+ bool intersectsCircle( vec3 position, vec3 normal, vec3 u, vec3 v, vec3 rayOrigin, vec3 rayDirection, out float dist ) {
370
+
371
+ float t = dot( position - rayOrigin, normal ) / dot( rayDirection, normal );
372
+
373
+ if ( t > EPSILON ) {
374
+
375
+ vec3 hit = rayOrigin + rayDirection * t;
376
+ vec3 vi = hit - position;
377
+
378
+ float a1 = dot( u, vi );
379
+ float a2 = dot( v, vi );
380
+
381
+ if( length( vec2( a1, a2 ) ) <= 0.5 ) {
382
+
383
+ dist = t;
384
+ return true;
385
+
386
+ }
387
+
388
+ }
389
+
390
+ return false;
391
+
392
+ }
393
+
394
+ // power heuristic for multiple importance sampling
395
+ float misHeuristic( float a, float b ) {
396
+
397
+ float aa = a * a;
398
+ float bb = b * b;
399
+ return aa / ( aa + bb );
400
+
401
+ }
402
+
403
+ `;