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,325 +1,325 @@
1
- import {
2
- DataTexture,
3
- FileLoader,
4
- FloatType,
5
- LinearFilter,
6
- RedFormat,
7
- MathUtils,
8
- Loader,
9
- } from 'three';
10
-
11
- function IESLamp( text ) {
12
-
13
- const _self = this;
14
-
15
- const textArray = text.split( '\n' );
16
-
17
- let lineNumber = 0;
18
- let line;
19
-
20
- _self.verAngles = [ ];
21
- _self.horAngles = [ ];
22
-
23
- _self.candelaValues = [ ];
24
-
25
- _self.tiltData = { };
26
- _self.tiltData.angles = [ ];
27
- _self.tiltData.mulFactors = [ ];
28
-
29
- function textToArray( text ) {
30
-
31
- text = text.replace( /^\s+|\s+$/g, '' ); // remove leading or trailing spaces
32
- text = text.replace( /,/g, ' ' ); // replace commas with spaces
33
- text = text.replace( /\s\s+/g, ' ' ); // replace white space/tabs etc by single whitespace
34
-
35
- const array = text.split( ' ' );
36
-
37
- return array;
38
-
39
- }
40
-
41
- function readArray( count, array ) {
42
-
43
- while ( true ) {
44
-
45
- const line = textArray[ lineNumber ++ ];
46
- const lineData = textToArray( line );
47
-
48
- for ( let i = 0; i < lineData.length; ++ i ) {
49
-
50
- array.push( Number( lineData[ i ] ) );
51
-
52
- }
53
-
54
- if ( array.length === count )
55
- break;
56
-
57
- }
58
-
59
- }
60
-
61
- function readTilt() {
62
-
63
- let line = textArray[ lineNumber ++ ];
64
- let lineData = textToArray( line );
65
-
66
- _self.tiltData.lampToLumGeometry = Number( lineData[ 0 ] );
67
-
68
- line = textArray[ lineNumber ++ ];
69
- lineData = textToArray( line );
70
-
71
- _self.tiltData.numAngles = Number( lineData[ 0 ] );
72
-
73
- readArray( _self.tiltData.numAngles, _self.tiltData.angles );
74
- readArray( _self.tiltData.numAngles, _self.tiltData.mulFactors );
75
-
76
- }
77
-
78
- function readLampValues() {
79
-
80
- const values = [ ];
81
- readArray( 10, values );
82
-
83
- _self.count = Number( values[ 0 ] );
84
- _self.lumens = Number( values[ 1 ] );
85
- _self.multiplier = Number( values[ 2 ] );
86
- _self.numVerAngles = Number( values[ 3 ] );
87
- _self.numHorAngles = Number( values[ 4 ] );
88
- _self.gonioType = Number( values[ 5 ] );
89
- _self.units = Number( values[ 6 ] );
90
- _self.width = Number( values[ 7 ] );
91
- _self.length = Number( values[ 8 ] );
92
- _self.height = Number( values[ 9 ] );
93
-
94
- }
95
-
96
- function readLampFactors() {
97
-
98
- const values = [ ];
99
- readArray( 3, values );
100
-
101
- _self.ballFactor = Number( values[ 0 ] );
102
- _self.blpFactor = Number( values[ 1 ] );
103
- _self.inputWatts = Number( values[ 2 ] );
104
-
105
- }
106
-
107
- while ( true ) {
108
-
109
- line = textArray[ lineNumber ++ ];
110
-
111
- if ( line.includes( 'TILT' ) ) {
112
-
113
- break;
114
-
115
- }
116
-
117
- }
118
-
119
- if ( ! line.includes( 'NONE' ) ) {
120
-
121
- if ( line.includes( 'INCLUDE' ) ) {
122
-
123
- readTilt();
124
-
125
- } else {
126
-
127
- // TODO:: Read tilt data from a file
128
-
129
- }
130
-
131
- }
132
-
133
- readLampValues();
134
-
135
- readLampFactors();
136
-
137
- // Initialize candela value array
138
- for ( let i = 0; i < _self.numHorAngles; ++ i ) {
139
-
140
- _self.candelaValues.push( [ ] );
141
-
142
- }
143
-
144
- // Parse Angles
145
- readArray( _self.numVerAngles, _self.verAngles );
146
- readArray( _self.numHorAngles, _self.horAngles );
147
-
148
- // Parse Candela values
149
- for ( let i = 0; i < _self.numHorAngles; ++ i ) {
150
-
151
- readArray( _self.numVerAngles, _self.candelaValues[ i ] );
152
-
153
- }
154
-
155
- // Calculate actual candela values, and normalize.
156
- for ( let i = 0; i < _self.numHorAngles; ++ i ) {
157
-
158
- for ( let j = 0; j < _self.numVerAngles; ++ j ) {
159
-
160
- _self.candelaValues[ i ][ j ] *= _self.candelaValues[ i ][ j ] * _self.multiplier
161
- * _self.ballFactor * _self.blpFactor;
162
-
163
- }
164
-
165
- }
166
-
167
- let maxVal = - 1;
168
- for ( let i = 0; i < _self.numHorAngles; ++ i ) {
169
-
170
- for ( let j = 0; j < _self.numVerAngles; ++ j ) {
171
-
172
- const value = _self.candelaValues[ i ][ j ];
173
- maxVal = maxVal < value ? value : maxVal;
174
-
175
- }
176
-
177
- }
178
-
179
- const bNormalize = true;
180
- if ( bNormalize && maxVal > 0 ) {
181
-
182
- for ( let i = 0; i < _self.numHorAngles; ++ i ) {
183
-
184
- for ( let j = 0; j < _self.numVerAngles; ++ j ) {
185
-
186
- _self.candelaValues[ i ][ j ] /= maxVal;
187
-
188
- }
189
-
190
- }
191
-
192
- }
193
-
194
- }
195
-
196
- export class IESLoader extends Loader {
197
-
198
- _getIESValues( iesLamp ) {
199
-
200
- const width = 360;
201
- const height = 180;
202
- const size = width * height;
203
-
204
- const data = new Float32Array( size );
205
-
206
- function interpolateCandelaValues( phi, theta ) {
207
-
208
- let phiIndex = 0, thetaIndex = 0;
209
- let startTheta = 0, endTheta = 0, startPhi = 0, endPhi = 0;
210
-
211
- for ( let i = 0; i < iesLamp.numHorAngles - 1; ++ i ) { // numHorAngles = horAngles.length-1 because of extra padding, so this wont cause an out of bounds error
212
-
213
- if ( theta < iesLamp.horAngles[ i + 1 ] || i == iesLamp.numHorAngles - 2 ) {
214
-
215
- thetaIndex = i;
216
- startTheta = iesLamp.horAngles[ i ];
217
- endTheta = iesLamp.horAngles[ i + 1 ];
218
-
219
- break;
220
-
221
- }
222
-
223
- }
224
-
225
- for ( let i = 0; i < iesLamp.numVerAngles - 1; ++ i ) {
226
-
227
- if ( phi < iesLamp.verAngles[ i + 1 ] || i == iesLamp.numVerAngles - 2 ) {
228
-
229
- phiIndex = i;
230
- startPhi = iesLamp.verAngles[ i ];
231
- endPhi = iesLamp.verAngles[ i + 1 ];
232
-
233
- break;
234
-
235
- }
236
-
237
- }
238
-
239
- const deltaTheta = endTheta - startTheta;
240
- const deltaPhi = endPhi - startPhi;
241
-
242
- if ( deltaPhi === 0 ) // Outside range
243
- return 0;
244
-
245
- const t1 = deltaTheta === 0 ? 0 : ( theta - startTheta ) / deltaTheta;
246
- const t2 = ( phi - startPhi ) / deltaPhi;
247
-
248
- const nextThetaIndex = deltaTheta === 0 ? thetaIndex : thetaIndex + 1;
249
-
250
- const v1 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex ], t1 );
251
- const v2 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex + 1 ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex + 1 ], t1 );
252
- const v = MathUtils.lerp( v1, v2, t2 );
253
-
254
- return v;
255
-
256
- }
257
-
258
- const startTheta = iesLamp.horAngles[ 0 ], endTheta = iesLamp.horAngles[ iesLamp.numHorAngles - 1 ];
259
- for ( let i = 0; i < size; ++ i ) {
260
-
261
- let theta = i % width;
262
- const phi = Math.floor( i / width );
263
-
264
- if ( endTheta - startTheta !== 0 && ( theta < startTheta || theta >= endTheta ) ) { // Handle symmetry for hor angles
265
-
266
- theta %= endTheta * 2;
267
- if ( theta > endTheta )
268
- theta = endTheta * 2 - theta;
269
-
270
- }
271
-
272
- data[ i ] = interpolateCandelaValues( phi, theta );
273
-
274
- }
275
-
276
- return data;
277
-
278
- }
279
-
280
- load( url, onLoad, onProgress, onError ) {
281
-
282
- const loader = new FileLoader( this.manager );
283
- loader.setResponseType( 'text' );
284
- loader.setCrossOrigin( this.crossOrigin );
285
- loader.setWithCredentials( this.withCredentials );
286
- loader.setPath( this.path );
287
- loader.setRequestHeader( this.requestHeader );
288
-
289
- const texture = new DataTexture( null, 360, 180, RedFormat, FloatType );
290
- texture.minFilter = LinearFilter;
291
- texture.magFilter = LinearFilter;
292
-
293
- loader.load( url, text => {
294
-
295
- const iesLamp = new IESLamp( text );
296
-
297
- texture.image.data = this._getIESValues( iesLamp );
298
- texture.needsUpdate = true;
299
-
300
- if ( onLoad !== undefined ) {
301
-
302
- onLoad( texture );
303
-
304
- }
305
-
306
- }, onProgress, onError );
307
-
308
- return texture;
309
-
310
- }
311
-
312
- parse( text ) {
313
-
314
- const iesLamp = new IESLamp( text );
315
- const texture = new DataTexture( null, 360, 180, RedFormat, FloatType );
316
- texture.minFilter = LinearFilter;
317
- texture.magFilter = LinearFilter;
318
- texture.image.data = this._getIESValues( iesLamp );
319
- texture.needsUpdate = true;
320
-
321
- return texture;
322
-
323
- }
324
-
325
- }
1
+ import {
2
+ DataTexture,
3
+ FileLoader,
4
+ FloatType,
5
+ LinearFilter,
6
+ RedFormat,
7
+ MathUtils,
8
+ Loader,
9
+ } from 'three';
10
+
11
+ function IESLamp( text ) {
12
+
13
+ const _self = this;
14
+
15
+ const textArray = text.split( '\n' );
16
+
17
+ let lineNumber = 0;
18
+ let line;
19
+
20
+ _self.verAngles = [ ];
21
+ _self.horAngles = [ ];
22
+
23
+ _self.candelaValues = [ ];
24
+
25
+ _self.tiltData = { };
26
+ _self.tiltData.angles = [ ];
27
+ _self.tiltData.mulFactors = [ ];
28
+
29
+ function textToArray( text ) {
30
+
31
+ text = text.trim(); // remove leading or trailing spaces
32
+ text = text.replace( /,/g, ' ' ); // replace commas with spaces
33
+ text = text.replace( /\s\s+/g, ' ' ); // replace white space/tabs etc by single whitespace
34
+
35
+ const array = text.split( ' ' );
36
+
37
+ return array;
38
+
39
+ }
40
+
41
+ function readArray( count, array ) {
42
+
43
+ while ( true ) {
44
+
45
+ const line = textArray[ lineNumber ++ ];
46
+ const lineData = textToArray( line );
47
+
48
+ for ( let i = 0; i < lineData.length; ++ i ) {
49
+
50
+ array.push( Number( lineData[ i ] ) );
51
+
52
+ }
53
+
54
+ if ( array.length === count )
55
+ break;
56
+
57
+ }
58
+
59
+ }
60
+
61
+ function readTilt() {
62
+
63
+ let line = textArray[ lineNumber ++ ];
64
+ let lineData = textToArray( line );
65
+
66
+ _self.tiltData.lampToLumGeometry = Number( lineData[ 0 ] );
67
+
68
+ line = textArray[ lineNumber ++ ];
69
+ lineData = textToArray( line );
70
+
71
+ _self.tiltData.numAngles = Number( lineData[ 0 ] );
72
+
73
+ readArray( _self.tiltData.numAngles, _self.tiltData.angles );
74
+ readArray( _self.tiltData.numAngles, _self.tiltData.mulFactors );
75
+
76
+ }
77
+
78
+ function readLampValues() {
79
+
80
+ const values = [ ];
81
+ readArray( 10, values );
82
+
83
+ _self.count = Number( values[ 0 ] );
84
+ _self.lumens = Number( values[ 1 ] );
85
+ _self.multiplier = Number( values[ 2 ] );
86
+ _self.numVerAngles = Number( values[ 3 ] );
87
+ _self.numHorAngles = Number( values[ 4 ] );
88
+ _self.gonioType = Number( values[ 5 ] );
89
+ _self.units = Number( values[ 6 ] );
90
+ _self.width = Number( values[ 7 ] );
91
+ _self.length = Number( values[ 8 ] );
92
+ _self.height = Number( values[ 9 ] );
93
+
94
+ }
95
+
96
+ function readLampFactors() {
97
+
98
+ const values = [ ];
99
+ readArray( 3, values );
100
+
101
+ _self.ballFactor = Number( values[ 0 ] );
102
+ _self.blpFactor = Number( values[ 1 ] );
103
+ _self.inputWatts = Number( values[ 2 ] );
104
+
105
+ }
106
+
107
+ while ( true ) {
108
+
109
+ line = textArray[ lineNumber ++ ];
110
+
111
+ if ( line.includes( 'TILT' ) ) {
112
+
113
+ break;
114
+
115
+ }
116
+
117
+ }
118
+
119
+ if ( ! line.includes( 'NONE' ) ) {
120
+
121
+ if ( line.includes( 'INCLUDE' ) ) {
122
+
123
+ readTilt();
124
+
125
+ } else {
126
+
127
+ // TODO:: Read tilt data from a file
128
+
129
+ }
130
+
131
+ }
132
+
133
+ readLampValues();
134
+
135
+ readLampFactors();
136
+
137
+ // Initialize candela value array
138
+ for ( let i = 0; i < _self.numHorAngles; ++ i ) {
139
+
140
+ _self.candelaValues.push( [ ] );
141
+
142
+ }
143
+
144
+ // Parse Angles
145
+ readArray( _self.numVerAngles, _self.verAngles );
146
+ readArray( _self.numHorAngles, _self.horAngles );
147
+
148
+ // Parse Candela values
149
+ for ( let i = 0; i < _self.numHorAngles; ++ i ) {
150
+
151
+ readArray( _self.numVerAngles, _self.candelaValues[ i ] );
152
+
153
+ }
154
+
155
+ // Calculate actual candela values, and normalize.
156
+ for ( let i = 0; i < _self.numHorAngles; ++ i ) {
157
+
158
+ for ( let j = 0; j < _self.numVerAngles; ++ j ) {
159
+
160
+ _self.candelaValues[ i ][ j ] *= _self.candelaValues[ i ][ j ] * _self.multiplier
161
+ * _self.ballFactor * _self.blpFactor;
162
+
163
+ }
164
+
165
+ }
166
+
167
+ let maxVal = - 1;
168
+ for ( let i = 0; i < _self.numHorAngles; ++ i ) {
169
+
170
+ for ( let j = 0; j < _self.numVerAngles; ++ j ) {
171
+
172
+ const value = _self.candelaValues[ i ][ j ];
173
+ maxVal = maxVal < value ? value : maxVal;
174
+
175
+ }
176
+
177
+ }
178
+
179
+ const bNormalize = true;
180
+ if ( bNormalize && maxVal > 0 ) {
181
+
182
+ for ( let i = 0; i < _self.numHorAngles; ++ i ) {
183
+
184
+ for ( let j = 0; j < _self.numVerAngles; ++ j ) {
185
+
186
+ _self.candelaValues[ i ][ j ] /= maxVal;
187
+
188
+ }
189
+
190
+ }
191
+
192
+ }
193
+
194
+ }
195
+
196
+ export class IESLoader extends Loader {
197
+
198
+ _getIESValues( iesLamp ) {
199
+
200
+ const width = 360;
201
+ const height = 180;
202
+ const size = width * height;
203
+
204
+ const data = new Float32Array( size );
205
+
206
+ function interpolateCandelaValues( phi, theta ) {
207
+
208
+ let phiIndex = 0, thetaIndex = 0;
209
+ let startTheta = 0, endTheta = 0, startPhi = 0, endPhi = 0;
210
+
211
+ for ( let i = 0; i < iesLamp.numHorAngles - 1; ++ i ) { // numHorAngles = horAngles.length-1 because of extra padding, so this wont cause an out of bounds error
212
+
213
+ if ( theta < iesLamp.horAngles[ i + 1 ] || i == iesLamp.numHorAngles - 2 ) {
214
+
215
+ thetaIndex = i;
216
+ startTheta = iesLamp.horAngles[ i ];
217
+ endTheta = iesLamp.horAngles[ i + 1 ];
218
+
219
+ break;
220
+
221
+ }
222
+
223
+ }
224
+
225
+ for ( let i = 0; i < iesLamp.numVerAngles - 1; ++ i ) {
226
+
227
+ if ( phi < iesLamp.verAngles[ i + 1 ] || i == iesLamp.numVerAngles - 2 ) {
228
+
229
+ phiIndex = i;
230
+ startPhi = iesLamp.verAngles[ i ];
231
+ endPhi = iesLamp.verAngles[ i + 1 ];
232
+
233
+ break;
234
+
235
+ }
236
+
237
+ }
238
+
239
+ const deltaTheta = endTheta - startTheta;
240
+ const deltaPhi = endPhi - startPhi;
241
+
242
+ if ( deltaPhi === 0 ) // Outside range
243
+ return 0;
244
+
245
+ const t1 = deltaTheta === 0 ? 0 : ( theta - startTheta ) / deltaTheta;
246
+ const t2 = ( phi - startPhi ) / deltaPhi;
247
+
248
+ const nextThetaIndex = deltaTheta === 0 ? thetaIndex : thetaIndex + 1;
249
+
250
+ const v1 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex ], t1 );
251
+ const v2 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex + 1 ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex + 1 ], t1 );
252
+ const v = MathUtils.lerp( v1, v2, t2 );
253
+
254
+ return v;
255
+
256
+ }
257
+
258
+ const startTheta = iesLamp.horAngles[ 0 ], endTheta = iesLamp.horAngles[ iesLamp.numHorAngles - 1 ];
259
+ for ( let i = 0; i < size; ++ i ) {
260
+
261
+ let theta = i % width;
262
+ const phi = Math.floor( i / width );
263
+
264
+ if ( endTheta - startTheta !== 0 && ( theta < startTheta || theta >= endTheta ) ) { // Handle symmetry for hor angles
265
+
266
+ theta %= endTheta * 2;
267
+ if ( theta > endTheta )
268
+ theta = endTheta * 2 - theta;
269
+
270
+ }
271
+
272
+ data[ i ] = interpolateCandelaValues( phi, theta );
273
+
274
+ }
275
+
276
+ return data;
277
+
278
+ }
279
+
280
+ load( url, onLoad, onProgress, onError ) {
281
+
282
+ const loader = new FileLoader( this.manager );
283
+ loader.setResponseType( 'text' );
284
+ loader.setCrossOrigin( this.crossOrigin );
285
+ loader.setWithCredentials( this.withCredentials );
286
+ loader.setPath( this.path );
287
+ loader.setRequestHeader( this.requestHeader );
288
+
289
+ const texture = new DataTexture( null, 360, 180, RedFormat, FloatType );
290
+ texture.minFilter = LinearFilter;
291
+ texture.magFilter = LinearFilter;
292
+
293
+ loader.load( url, text => {
294
+
295
+ const iesLamp = new IESLamp( text );
296
+
297
+ texture.image.data = this._getIESValues( iesLamp );
298
+ texture.needsUpdate = true;
299
+
300
+ if ( onLoad !== undefined ) {
301
+
302
+ onLoad( texture );
303
+
304
+ }
305
+
306
+ }, onProgress, onError );
307
+
308
+ return texture;
309
+
310
+ }
311
+
312
+ parse( text ) {
313
+
314
+ const iesLamp = new IESLamp( text );
315
+ const texture = new DataTexture( null, 360, 180, RedFormat, FloatType );
316
+ texture.minFilter = LinearFilter;
317
+ texture.magFilter = LinearFilter;
318
+ texture.image.data = this._getIESValues( iesLamp );
319
+ texture.needsUpdate = true;
320
+
321
+ return texture;
322
+
323
+ }
324
+
325
+ }