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