three-gpu-pathtracer 0.0.7 → 0.0.9

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 (51) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +886 -815
  3. package/build/index.module.js +6374 -5613
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +6377 -5615
  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 +275 -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 +199 -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 +982 -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 +58 -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 +229 -231
  30. package/src/shader/shaderMaterialSampling.js +498 -542
  31. package/src/shader/shaderRandFunctions.js +57 -0
  32. package/src/shader/shaderSheenFunctions.js +98 -98
  33. package/src/shader/shaderSobolSampling.js +256 -0
  34. package/src/shader/shaderStructs.js +325 -321
  35. package/src/shader/shaderUtils.js +361 -364
  36. package/src/textures/GradientEquirectTexture.js +35 -0
  37. package/src/textures/ProceduralEquirectTexture.js +75 -0
  38. package/src/uniforms/AttributesTextureArray.js +35 -0
  39. package/src/uniforms/EquirectHdrInfoUniform.js +259 -259
  40. package/src/uniforms/FloatAttributeTextureArray.js +169 -0
  41. package/src/uniforms/IESProfilesTexture.js +100 -100
  42. package/src/uniforms/LightsInfoUniformStruct.js +207 -162
  43. package/src/uniforms/MaterialsTexture.js +426 -426
  44. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  45. package/src/uniforms/RenderTarget2DArray.js +97 -93
  46. package/src/uniforms/utils.js +30 -0
  47. package/src/utils/BlurredEnvMapGenerator.js +116 -113
  48. package/src/utils/IESLoader.js +325 -325
  49. package/src/utils/SobolNumberMapGenerator.js +80 -0
  50. package/src/utils/UVUnwrapper.js +101 -101
  51. package/src/workers/PathTracingSceneWorker.js +42 -42
@@ -0,0 +1,35 @@
1
+ import { Color, Vector3 } from 'three';
2
+ import { ProceduralEquirectTexture } from './ProceduralEquirectTexture.js';
3
+
4
+ const _direction = new Vector3();
5
+ export class GradientEquirectTexture extends ProceduralEquirectTexture {
6
+
7
+ constructor( resolution = 512 ) {
8
+
9
+ super( resolution, resolution );
10
+
11
+ this.topColor = new Color().set( 0xffffff );
12
+ this.bottomColor = new Color().set( 0x000000 );
13
+ this.exponent = 2;
14
+ this.generationCallback = ( polar, uv, coord, color ) => {
15
+
16
+ _direction.setFromSpherical( polar );
17
+
18
+ const t = _direction.y * 0.5 + 0.5;
19
+ color.lerpColors( this.bottomColor, this.topColor, t ** this.exponent );
20
+
21
+ };
22
+
23
+ }
24
+
25
+ copy( other ) {
26
+
27
+ super.copy( other );
28
+
29
+ this.topColor.copy( other.topColor );
30
+ this.bottomColor.copy( other.bottomColor );
31
+ return this;
32
+
33
+ }
34
+
35
+ }
@@ -0,0 +1,75 @@
1
+ import {
2
+ ClampToEdgeWrapping,
3
+ Color,
4
+ DataTexture,
5
+ EquirectangularReflectionMapping,
6
+ FloatType,
7
+ LinearFilter,
8
+ RepeatWrapping,
9
+ RGBAFormat,
10
+ Spherical,
11
+ Vector2,
12
+ } from 'three';
13
+
14
+ const _uv = new Vector2();
15
+ const _coord = new Vector2();
16
+ const _polar = new Spherical();
17
+ const _color = new Color();
18
+ export class ProceduralEquirectTexture extends DataTexture {
19
+
20
+ constructor( width, height ) {
21
+
22
+ super(
23
+ new Float32Array( width * height * 4 ),
24
+ width, height, RGBAFormat, FloatType, EquirectangularReflectionMapping,
25
+ RepeatWrapping, ClampToEdgeWrapping, LinearFilter, LinearFilter,
26
+ );
27
+
28
+ this.generationCallback = null;
29
+
30
+ }
31
+
32
+ update() {
33
+
34
+ this.dispose();
35
+ this.needsUpdate = true;
36
+
37
+ const { data, width, height } = this.image;
38
+ for ( let x = 0; x < width; x ++ ) {
39
+
40
+ for ( let y = 0; y < height; y ++ ) {
41
+
42
+ _coord.set( width, height );
43
+
44
+ _uv.set( x / width, y / height );
45
+ _uv.x -= 0.5;
46
+ _uv.y = 1.0 - _uv.y;
47
+
48
+ _polar.theta = _uv.x * 2.0 * Math.PI;
49
+ _polar.phi = _uv.y * Math.PI;
50
+ _polar.radius = 1.0;
51
+
52
+ this.generationCallback( _polar, _uv, _coord, _color );
53
+
54
+ const i = y * width + x;
55
+ const i4 = 4 * i;
56
+ data[ i4 + 0 ] = _color.r;
57
+ data[ i4 + 1 ] = _color.g;
58
+ data[ i4 + 2 ] = _color.b;
59
+ data[ i4 + 3 ] = 1.0;
60
+
61
+ }
62
+
63
+ }
64
+
65
+ }
66
+
67
+ copy( other ) {
68
+
69
+ super.copy( other );
70
+ this.generationCallback = other.generationCallback;
71
+ return this;
72
+
73
+ }
74
+
75
+ }
@@ -0,0 +1,35 @@
1
+ import { FloatAttributeTextureArray } from './FloatAttributeTextureArray.js';
2
+
3
+ export class AttributesTextureArray extends FloatAttributeTextureArray {
4
+
5
+ updateNormalAttribute( attr ) {
6
+
7
+ this.updateAttribute( 0, attr );
8
+
9
+ }
10
+
11
+ updateTangentAttribute( attr ) {
12
+
13
+ this.updateAttribute( 1, attr );
14
+
15
+ }
16
+
17
+ updateUvAttribute( attr ) {
18
+
19
+ this.updateAttribute( 2, attr );
20
+
21
+ }
22
+
23
+ updateColorAttribute( attr ) {
24
+
25
+ this.updateAttribute( 3, attr );
26
+
27
+ }
28
+
29
+ updateFrom( normal, tangent, uv, color ) {
30
+
31
+ this.setAttributes( [ normal, tangent, uv, color ] );
32
+
33
+ }
34
+
35
+ }
@@ -1,259 +1,259 @@
1
- import { DataTexture, FloatType, RedFormat, LinearFilter, DataUtils, HalfFloatType, Source, RepeatWrapping } from 'three';
2
-
3
- function binarySearchFindClosestIndexOf( array, targetValue, offset = 0, count = array.length ) {
4
-
5
- let lower = 0;
6
- let upper = count;
7
- while ( lower < upper ) {
8
-
9
- const mid = ~ ~ ( 0.5 * upper + 0.5 * lower );
10
-
11
-
12
- // check if the middle array value is above or below the target and shift
13
- // which half of the array we're looking at
14
- if ( array[ offset + mid ] < targetValue ) {
15
-
16
- lower = mid + 1;
17
-
18
- } else {
19
-
20
- upper = mid;
21
-
22
- }
23
-
24
- }
25
-
26
- return lower;
27
-
28
- }
29
-
30
- function colorToLuminance( r, g, b ) {
31
-
32
- // https://en.wikipedia.org/wiki/Relative_luminance
33
- return 0.2126 * r + 0.7152 * g + 0.0722 * b;
34
-
35
- }
36
-
37
- // ensures the data is all floating point values and flipY is false
38
- function preprocessEnvMap( envMap ) {
39
-
40
- const map = envMap.clone();
41
- map.source = new Source( { ...map.image } );
42
- const { width, height, data } = map.image;
43
-
44
- // TODO: is there a simple way to avoid cloning and adjusting the env map data here?
45
- // convert the data from half float uint 16 arrays to float arrays for cdf computation
46
- let newData = data;
47
- if ( map.type === HalfFloatType ) {
48
-
49
- newData = new Float32Array( data.length );
50
- for ( const i in data ) {
51
-
52
- newData[ i ] = DataUtils.fromHalfFloat( data[ i ] );
53
-
54
- }
55
-
56
- map.image.data = newData;
57
- map.type = FloatType;
58
-
59
- }
60
-
61
- // remove any y flipping for cdf computation
62
- if ( map.flipY ) {
63
-
64
- const ogData = newData;
65
- newData = newData.slice();
66
- for ( let y = 0; y < height; y ++ ) {
67
-
68
- for ( let x = 0; x < width; x ++ ) {
69
-
70
- const newY = height - y - 1;
71
- const ogIndex = 4 * ( y * width + x );
72
- const newIndex = 4 * ( newY * width + x );
73
-
74
- newData[ newIndex + 0 ] = ogData[ ogIndex + 0 ];
75
- newData[ newIndex + 1 ] = ogData[ ogIndex + 1 ];
76
- newData[ newIndex + 2 ] = ogData[ ogIndex + 2 ];
77
- newData[ newIndex + 3 ] = ogData[ ogIndex + 3 ];
78
-
79
- }
80
-
81
- }
82
-
83
- map.flipY = false;
84
- map.image.data = newData;
85
-
86
- }
87
-
88
- return map;
89
-
90
- }
91
-
92
- export class EquirectHdrInfoUniform {
93
-
94
- constructor() {
95
-
96
- // Stores a map of [0, 1] value -> cumulative importance row & pdf
97
- // used to sampling a random value to a relevant row to sample from
98
- const marginalWeights = new DataTexture();
99
- marginalWeights.type = FloatType;
100
- marginalWeights.format = RedFormat;
101
- marginalWeights.minFilter = LinearFilter;
102
- marginalWeights.magFilter = LinearFilter;
103
- marginalWeights.generateMipmaps = false;
104
-
105
- // Stores a map of [0, 1] value -> cumulative importance column & pdf
106
- // used to sampling a random value to a relevant pixel to sample from
107
- const conditionalWeights = new DataTexture();
108
- conditionalWeights.type = FloatType;
109
- conditionalWeights.format = RedFormat;
110
- conditionalWeights.minFilter = LinearFilter;
111
- conditionalWeights.magFilter = LinearFilter;
112
- conditionalWeights.generateMipmaps = false;
113
-
114
- this.marginalWeights = marginalWeights;
115
- this.conditionalWeights = conditionalWeights;
116
- this.map = null;
117
-
118
- // the total sum value is separated into two values to work around low precision
119
- // storage of floating values in structs
120
- this.totalSumWhole = 0;
121
- this.totalSumDecimal = 0;
122
-
123
- }
124
-
125
- dispose() {
126
-
127
- this.marginalWeights.dispose();
128
- this.conditionalWeights.dispose();
129
- if ( this.map ) this.map.dispose();
130
-
131
- }
132
-
133
- updateFrom( hdr ) {
134
-
135
- // https://github.com/knightcrawler25/GLSL-PathTracer/blob/3c6fd9b6b3da47cd50c527eeb45845eef06c55c3/src/loaders/hdrloader.cpp
136
- // https://pbr-book.org/3ed-2018/Light_Transport_I_Surface_Reflection/Sampling_Light_Sources#InfiniteAreaLights
137
- const map = preprocessEnvMap( hdr );
138
- map.wrapS = RepeatWrapping;
139
- map.wrapT = RepeatWrapping;
140
-
141
- const { width, height, data } = map.image;
142
-
143
- // "conditional" = "pixel relative to row pixels sum"
144
- // "marginal" = "row relative to row sum"
145
-
146
- // track the importance of any given pixel in the image by tracking its weight relative to other pixels in the image
147
- const pdfConditional = new Float32Array( width * height );
148
- const cdfConditional = new Float32Array( width * height );
149
-
150
- const pdfMarginal = new Float32Array( height );
151
- const cdfMarginal = new Float32Array( height );
152
-
153
- let totalSumValue = 0.0;
154
- let cumulativeWeightMarginal = 0.0;
155
- for ( let y = 0; y < height; y ++ ) {
156
-
157
- let cumulativeRowWeight = 0.0;
158
- for ( let x = 0; x < width; x ++ ) {
159
-
160
- const i = y * width + x;
161
- const r = data[ 4 * i + 0 ];
162
- const g = data[ 4 * i + 1 ];
163
- const b = data[ 4 * i + 2 ];
164
-
165
- // the probability of the pixel being selected in this row is the
166
- // scale of the luminance relative to the rest of the pixels.
167
- // TODO: this should also account for the solid angle of the pixel when sampling
168
- const weight = colorToLuminance( r, g, b );
169
- cumulativeRowWeight += weight;
170
- totalSumValue += weight;
171
-
172
- pdfConditional[ i ] = weight;
173
- cdfConditional[ i ] = cumulativeRowWeight;
174
-
175
- }
176
-
177
- // can happen if the row is all black
178
- if ( cumulativeRowWeight !== 0 ) {
179
-
180
- // scale the pdf and cdf to [0.0, 1.0]
181
- for ( let i = y * width, l = y * width + width; i < l; i ++ ) {
182
-
183
- pdfConditional[ i ] /= cumulativeRowWeight;
184
- cdfConditional[ i ] /= cumulativeRowWeight;
185
-
186
- }
187
-
188
- }
189
-
190
- cumulativeWeightMarginal += cumulativeRowWeight;
191
-
192
- // compute the marginal pdf and cdf along the height of the map.
193
- pdfMarginal[ y ] = cumulativeRowWeight;
194
- cdfMarginal[ y ] = cumulativeWeightMarginal;
195
-
196
- }
197
-
198
- // can happen if the texture is all black
199
- if ( cumulativeWeightMarginal !== 0 ) {
200
-
201
- // scale the marginal pdf and cdf to [0.0, 1.0]
202
- for ( let i = 0, l = pdfMarginal.length; i < l; i ++ ) {
203
-
204
- pdfMarginal[ i ] /= cumulativeWeightMarginal;
205
- cdfMarginal[ i ] /= cumulativeWeightMarginal;
206
-
207
- }
208
-
209
- }
210
-
211
- // compute a sorted index of distributions and the probabilities along them for both
212
- // the marginal and conditional data. These will be used to sample with a random number
213
- // to retrieve a uv value to sample in the environment map.
214
- // These values continually increase so it's okay to interpolate between them.
215
- const marginalDataArray = new Float32Array( height );
216
- const conditionalDataArray = new Float32Array( width * height );
217
-
218
- for ( let i = 0; i < height; i ++ ) {
219
-
220
- const dist = ( i + 1 ) / height;
221
- const row = binarySearchFindClosestIndexOf( cdfMarginal, dist );
222
-
223
- marginalDataArray[ i ] = row / height;
224
-
225
- }
226
-
227
- for ( let y = 0; y < height; y ++ ) {
228
-
229
- for ( let x = 0; x < width; x ++ ) {
230
-
231
- const i = y * width + x;
232
- const dist = ( x + 1 ) / width;
233
- const col = binarySearchFindClosestIndexOf( cdfConditional, dist, y * width, width );
234
-
235
- conditionalDataArray[ i ] = col / width;
236
-
237
- }
238
-
239
- }
240
-
241
- this.dispose();
242
-
243
- const { marginalWeights, conditionalWeights } = this;
244
- marginalWeights.image = { width: height, height: 1, data: marginalDataArray };
245
- marginalWeights.needsUpdate = true;
246
-
247
- conditionalWeights.image = { width, height, data: conditionalDataArray };
248
- conditionalWeights.needsUpdate = true;
249
-
250
- const totalSumWhole = ~ ~ totalSumValue;
251
- const totalSumDecimal = ( totalSumValue - totalSumWhole );
252
- this.totalSumWhole = totalSumWhole;
253
- this.totalSumDecimal = totalSumDecimal;
254
-
255
- this.map = map;
256
-
257
- }
258
-
259
- }
1
+ import { DataTexture, FloatType, RedFormat, LinearFilter, DataUtils, HalfFloatType, Source, RepeatWrapping } from 'three';
2
+
3
+ function binarySearchFindClosestIndexOf( array, targetValue, offset = 0, count = array.length ) {
4
+
5
+ let lower = 0;
6
+ let upper = count;
7
+ while ( lower < upper ) {
8
+
9
+ const mid = ~ ~ ( 0.5 * upper + 0.5 * lower );
10
+
11
+
12
+ // check if the middle array value is above or below the target and shift
13
+ // which half of the array we're looking at
14
+ if ( array[ offset + mid ] < targetValue ) {
15
+
16
+ lower = mid + 1;
17
+
18
+ } else {
19
+
20
+ upper = mid;
21
+
22
+ }
23
+
24
+ }
25
+
26
+ return lower;
27
+
28
+ }
29
+
30
+ function colorToLuminance( r, g, b ) {
31
+
32
+ // https://en.wikipedia.org/wiki/Relative_luminance
33
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
34
+
35
+ }
36
+
37
+ // ensures the data is all floating point values and flipY is false
38
+ function preprocessEnvMap( envMap ) {
39
+
40
+ const map = envMap.clone();
41
+ map.source = new Source( { ...map.image } );
42
+ const { width, height, data } = map.image;
43
+
44
+ // TODO: is there a simple way to avoid cloning and adjusting the env map data here?
45
+ // convert the data from half float uint 16 arrays to float arrays for cdf computation
46
+ let newData = data;
47
+ if ( map.type === HalfFloatType ) {
48
+
49
+ newData = new Float32Array( data.length );
50
+ for ( const i in data ) {
51
+
52
+ newData[ i ] = DataUtils.fromHalfFloat( data[ i ] );
53
+
54
+ }
55
+
56
+ map.image.data = newData;
57
+ map.type = FloatType;
58
+
59
+ }
60
+
61
+ // remove any y flipping for cdf computation
62
+ if ( map.flipY ) {
63
+
64
+ const ogData = newData;
65
+ newData = newData.slice();
66
+ for ( let y = 0; y < height; y ++ ) {
67
+
68
+ for ( let x = 0; x < width; x ++ ) {
69
+
70
+ const newY = height - y - 1;
71
+ const ogIndex = 4 * ( y * width + x );
72
+ const newIndex = 4 * ( newY * width + x );
73
+
74
+ newData[ newIndex + 0 ] = ogData[ ogIndex + 0 ];
75
+ newData[ newIndex + 1 ] = ogData[ ogIndex + 1 ];
76
+ newData[ newIndex + 2 ] = ogData[ ogIndex + 2 ];
77
+ newData[ newIndex + 3 ] = ogData[ ogIndex + 3 ];
78
+
79
+ }
80
+
81
+ }
82
+
83
+ map.flipY = false;
84
+ map.image.data = newData;
85
+
86
+ }
87
+
88
+ return map;
89
+
90
+ }
91
+
92
+ export class EquirectHdrInfoUniform {
93
+
94
+ constructor() {
95
+
96
+ // Stores a map of [0, 1] value -> cumulative importance row & pdf
97
+ // used to sampling a random value to a relevant row to sample from
98
+ const marginalWeights = new DataTexture();
99
+ marginalWeights.type = FloatType;
100
+ marginalWeights.format = RedFormat;
101
+ marginalWeights.minFilter = LinearFilter;
102
+ marginalWeights.magFilter = LinearFilter;
103
+ marginalWeights.generateMipmaps = false;
104
+
105
+ // Stores a map of [0, 1] value -> cumulative importance column & pdf
106
+ // used to sampling a random value to a relevant pixel to sample from
107
+ const conditionalWeights = new DataTexture();
108
+ conditionalWeights.type = FloatType;
109
+ conditionalWeights.format = RedFormat;
110
+ conditionalWeights.minFilter = LinearFilter;
111
+ conditionalWeights.magFilter = LinearFilter;
112
+ conditionalWeights.generateMipmaps = false;
113
+
114
+ this.marginalWeights = marginalWeights;
115
+ this.conditionalWeights = conditionalWeights;
116
+ this.map = null;
117
+
118
+ // the total sum value is separated into two values to work around low precision
119
+ // storage of floating values in structs
120
+ this.totalSumWhole = 0;
121
+ this.totalSumDecimal = 0;
122
+
123
+ }
124
+
125
+ dispose() {
126
+
127
+ this.marginalWeights.dispose();
128
+ this.conditionalWeights.dispose();
129
+ if ( this.map ) this.map.dispose();
130
+
131
+ }
132
+
133
+ updateFrom( hdr ) {
134
+
135
+ // https://github.com/knightcrawler25/GLSL-PathTracer/blob/3c6fd9b6b3da47cd50c527eeb45845eef06c55c3/src/loaders/hdrloader.cpp
136
+ // https://pbr-book.org/3ed-2018/Light_Transport_I_Surface_Reflection/Sampling_Light_Sources#InfiniteAreaLights
137
+ const map = preprocessEnvMap( hdr );
138
+ map.wrapS = RepeatWrapping;
139
+ map.wrapT = RepeatWrapping;
140
+
141
+ const { width, height, data } = map.image;
142
+
143
+ // "conditional" = "pixel relative to row pixels sum"
144
+ // "marginal" = "row relative to row sum"
145
+
146
+ // track the importance of any given pixel in the image by tracking its weight relative to other pixels in the image
147
+ const pdfConditional = new Float32Array( width * height );
148
+ const cdfConditional = new Float32Array( width * height );
149
+
150
+ const pdfMarginal = new Float32Array( height );
151
+ const cdfMarginal = new Float32Array( height );
152
+
153
+ let totalSumValue = 0.0;
154
+ let cumulativeWeightMarginal = 0.0;
155
+ for ( let y = 0; y < height; y ++ ) {
156
+
157
+ let cumulativeRowWeight = 0.0;
158
+ for ( let x = 0; x < width; x ++ ) {
159
+
160
+ const i = y * width + x;
161
+ const r = data[ 4 * i + 0 ];
162
+ const g = data[ 4 * i + 1 ];
163
+ const b = data[ 4 * i + 2 ];
164
+
165
+ // the probability of the pixel being selected in this row is the
166
+ // scale of the luminance relative to the rest of the pixels.
167
+ // TODO: this should also account for the solid angle of the pixel when sampling
168
+ const weight = colorToLuminance( r, g, b );
169
+ cumulativeRowWeight += weight;
170
+ totalSumValue += weight;
171
+
172
+ pdfConditional[ i ] = weight;
173
+ cdfConditional[ i ] = cumulativeRowWeight;
174
+
175
+ }
176
+
177
+ // can happen if the row is all black
178
+ if ( cumulativeRowWeight !== 0 ) {
179
+
180
+ // scale the pdf and cdf to [0.0, 1.0]
181
+ for ( let i = y * width, l = y * width + width; i < l; i ++ ) {
182
+
183
+ pdfConditional[ i ] /= cumulativeRowWeight;
184
+ cdfConditional[ i ] /= cumulativeRowWeight;
185
+
186
+ }
187
+
188
+ }
189
+
190
+ cumulativeWeightMarginal += cumulativeRowWeight;
191
+
192
+ // compute the marginal pdf and cdf along the height of the map.
193
+ pdfMarginal[ y ] = cumulativeRowWeight;
194
+ cdfMarginal[ y ] = cumulativeWeightMarginal;
195
+
196
+ }
197
+
198
+ // can happen if the texture is all black
199
+ if ( cumulativeWeightMarginal !== 0 ) {
200
+
201
+ // scale the marginal pdf and cdf to [0.0, 1.0]
202
+ for ( let i = 0, l = pdfMarginal.length; i < l; i ++ ) {
203
+
204
+ pdfMarginal[ i ] /= cumulativeWeightMarginal;
205
+ cdfMarginal[ i ] /= cumulativeWeightMarginal;
206
+
207
+ }
208
+
209
+ }
210
+
211
+ // compute a sorted index of distributions and the probabilities along them for both
212
+ // the marginal and conditional data. These will be used to sample with a random number
213
+ // to retrieve a uv value to sample in the environment map.
214
+ // These values continually increase so it's okay to interpolate between them.
215
+ const marginalDataArray = new Float32Array( height );
216
+ const conditionalDataArray = new Float32Array( width * height );
217
+
218
+ for ( let i = 0; i < height; i ++ ) {
219
+
220
+ const dist = ( i + 1 ) / height;
221
+ const row = binarySearchFindClosestIndexOf( cdfMarginal, dist );
222
+
223
+ marginalDataArray[ i ] = row / height;
224
+
225
+ }
226
+
227
+ for ( let y = 0; y < height; y ++ ) {
228
+
229
+ for ( let x = 0; x < width; x ++ ) {
230
+
231
+ const i = y * width + x;
232
+ const dist = ( x + 1 ) / width;
233
+ const col = binarySearchFindClosestIndexOf( cdfConditional, dist, y * width, width );
234
+
235
+ conditionalDataArray[ i ] = col / width;
236
+
237
+ }
238
+
239
+ }
240
+
241
+ this.dispose();
242
+
243
+ const { marginalWeights, conditionalWeights } = this;
244
+ marginalWeights.image = { width: height, height: 1, data: marginalDataArray };
245
+ marginalWeights.needsUpdate = true;
246
+
247
+ conditionalWeights.image = { width, height, data: conditionalDataArray };
248
+ conditionalWeights.needsUpdate = true;
249
+
250
+ const totalSumWhole = ~ ~ totalSumValue;
251
+ const totalSumDecimal = ( totalSumValue - totalSumWhole );
252
+ this.totalSumWhole = totalSumWhole;
253
+ this.totalSumDecimal = totalSumDecimal;
254
+
255
+ this.map = map;
256
+
257
+ }
258
+
259
+ }