three-gpu-pathtracer 0.0.3 → 0.0.6

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 (32) hide show
  1. package/README.md +136 -15
  2. package/build/index.module.js +2706 -529
  3. package/build/index.module.js.map +1 -1
  4. package/build/index.umd.cjs +2713 -529
  5. package/build/index.umd.cjs.map +1 -1
  6. package/package.json +68 -60
  7. package/src/core/DynamicPathTracingSceneGenerator.js +24 -11
  8. package/src/core/PathTracingRenderer.js +22 -0
  9. package/src/core/PathTracingSceneGenerator.js +36 -20
  10. package/src/index.js +9 -1
  11. package/src/materials/PhysicalPathTracingMaterial.js +350 -60
  12. package/src/objects/EquirectCamera.js +13 -0
  13. package/src/{core → objects}/PhysicalCamera.js +0 -0
  14. package/src/objects/PhysicalSpotLight.js +14 -0
  15. package/src/objects/ShapedAreaLight.js +12 -0
  16. package/src/shader/shaderEnvMapSampling.js +59 -67
  17. package/src/shader/shaderGGXFunctions.js +3 -2
  18. package/src/shader/shaderIridescenceFunctions.js +130 -0
  19. package/src/shader/shaderLightSampling.js +231 -0
  20. package/src/shader/shaderMaterialSampling.js +259 -53
  21. package/src/shader/shaderSheenFunctions.js +98 -0
  22. package/src/shader/shaderStructs.js +307 -92
  23. package/src/shader/shaderUtils.js +122 -0
  24. package/src/uniforms/EquirectHdrInfoUniform.js +10 -14
  25. package/src/uniforms/IESProfilesTexture.js +100 -0
  26. package/src/uniforms/LightsInfoUniformStruct.js +162 -0
  27. package/src/uniforms/MaterialsTexture.js +266 -33
  28. package/src/uniforms/PhysicalCameraUniform.js +1 -1
  29. package/src/uniforms/RenderTarget2DArray.js +93 -80
  30. package/src/utils/GeometryPreparationUtils.js +1 -1
  31. package/src/utils/IESLoader.js +325 -0
  32. package/src/workers/PathTracingSceneWorker.js +3 -1
@@ -1,80 +1,93 @@
1
- import {
2
- WebGLArrayRenderTarget,
3
- RGBAFormat,
4
- UnsignedByteType,
5
- MeshBasicMaterial,
6
- Color,
7
- RepeatWrapping,
8
- LinearFilter,
9
- NoToneMapping,
10
- } from 'three';
11
- import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
12
-
13
- const prevColor = new Color();
14
- export class RenderTarget2DArray extends WebGLArrayRenderTarget {
15
-
16
- constructor( ...args ) {
17
-
18
- super( ...args );
19
-
20
- const tex = this.texture;
21
- tex.format = RGBAFormat;
22
- tex.type = UnsignedByteType;
23
- tex.minFilter = LinearFilter;
24
- tex.magFilter = LinearFilter;
25
- tex.wrapS = RepeatWrapping;
26
- tex.wrapT = RepeatWrapping;
27
- tex.setTextures = ( ...args ) => {
28
-
29
- this.setTextures( ...args );
30
-
31
- };
32
-
33
- const fsQuad = new FullScreenQuad( new MeshBasicMaterial() );
34
- this.fsQuad = fsQuad;
35
-
36
- }
37
-
38
- setTextures( renderer, width, height, textures ) {
39
-
40
- // save previous renderer state
41
- const prevRenderTarget = renderer.getRenderTarget();
42
- const prevToneMapping = renderer.toneMapping;
43
- const prevAlpha = renderer.getClearAlpha();
44
- renderer.getClearColor( prevColor );
45
-
46
- // resize the render target and ensure we don't have an empty texture
47
- const depth = textures.length || 1;
48
- this.setSize( width, height, depth );
49
- renderer.setClearColor( 0, 0 );
50
- renderer.toneMapping = NoToneMapping;
51
-
52
- // render each texture into each layer of the target
53
- const fsQuad = this.fsQuad;
54
- for ( let i = 0, l = depth; i < l; i ++ ) {
55
-
56
- const texture = textures[ i ];
57
- fsQuad.material.map = texture;
58
- fsQuad.material.transparent = true;
59
-
60
- renderer.setRenderTarget( this, i );
61
- fsQuad.render( renderer );
62
-
63
- }
64
-
65
- // reset the renderer
66
- fsQuad.material.map = null;
67
- renderer.setClearColor( prevColor, prevAlpha );
68
- renderer.setRenderTarget( prevRenderTarget );
69
- renderer.toneMapping = prevToneMapping;
70
-
71
- }
72
-
73
- dispose() {
74
-
75
- super.dispose();
76
- this.fsQuad.dispose();
77
-
78
- }
79
-
80
- }
1
+ import {
2
+ WebGLArrayRenderTarget,
3
+ RGBAFormat,
4
+ UnsignedByteType,
5
+ MeshBasicMaterial,
6
+ Color,
7
+ RepeatWrapping,
8
+ LinearFilter,
9
+ NoToneMapping,
10
+ } from 'three';
11
+ import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
12
+
13
+ const prevColor = new Color();
14
+ export class RenderTarget2DArray extends WebGLArrayRenderTarget {
15
+
16
+ constructor( ...args ) {
17
+
18
+ super( ...args );
19
+
20
+ const tex = this.texture;
21
+ tex.format = RGBAFormat;
22
+ tex.type = UnsignedByteType;
23
+ tex.minFilter = LinearFilter;
24
+ tex.magFilter = LinearFilter;
25
+ tex.wrapS = RepeatWrapping;
26
+ tex.wrapT = RepeatWrapping;
27
+ tex.setTextures = ( ...args ) => {
28
+
29
+ this.setTextures( ...args );
30
+
31
+ };
32
+
33
+ const fsQuad = new FullScreenQuad( new MeshBasicMaterial() );
34
+ this.fsQuad = fsQuad;
35
+
36
+ }
37
+
38
+ setTextures( renderer, width, height, textures ) {
39
+
40
+ // save previous renderer state
41
+ const prevRenderTarget = renderer.getRenderTarget();
42
+ const prevToneMapping = renderer.toneMapping;
43
+ const prevAlpha = renderer.getClearAlpha();
44
+ renderer.getClearColor( prevColor );
45
+
46
+ // resize the render target and ensure we don't have an empty texture
47
+ // render target depth must be >= 1 to avoid unbound texture error on android devices
48
+ const depth = textures.length || 1;
49
+ this.setSize( width, height, depth );
50
+ renderer.setClearColor( 0, 0 );
51
+ renderer.toneMapping = NoToneMapping;
52
+
53
+ // render each texture into each layer of the target
54
+ const fsQuad = this.fsQuad;
55
+ for ( let i = 0, l = depth; i < l; i ++ ) {
56
+
57
+ const texture = textures[ i ];
58
+ if ( texture ) {
59
+
60
+ // revert to default texture transform before rendering
61
+ texture.matrixAutoUpdate = false;
62
+ texture.matrix.identity();
63
+
64
+ fsQuad.material.map = texture;
65
+ fsQuad.material.transparent = true;
66
+
67
+ renderer.setRenderTarget( this, i );
68
+ fsQuad.render( renderer );
69
+
70
+ // restore custom texture transform
71
+ texture.updateMatrix();
72
+ texture.matrixAutoUpdate = true;
73
+
74
+ }
75
+
76
+ }
77
+
78
+ // reset the renderer
79
+ fsQuad.material.map = null;
80
+ renderer.setClearColor( prevColor, prevAlpha );
81
+ renderer.setRenderTarget( prevRenderTarget );
82
+ renderer.toneMapping = prevToneMapping;
83
+
84
+ }
85
+
86
+ dispose() {
87
+
88
+ super.dispose();
89
+ this.fsQuad.dispose();
90
+
91
+ }
92
+
93
+ }
@@ -153,7 +153,7 @@ export function mergeMeshes( meshes, options = {} ) {
153
153
 
154
154
  // apply the matrix world to the geometry
155
155
  const originalGeometry = meshes[ i ].geometry;
156
- let geometry = options.cloneGeometry ? originalGeometry.clone() : originalGeometry;
156
+ const geometry = options.cloneGeometry ? originalGeometry.clone() : originalGeometry;
157
157
  geometry.applyMatrix4( mesh.matrixWorld );
158
158
 
159
159
  // ensure our geometry has common attributes
@@ -0,0 +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
+ }
@@ -14,7 +14,7 @@ export class PathTracingSceneWorker extends PathTracingSceneGenerator {
14
14
  generate( scene, options = {} ) {
15
15
 
16
16
  const { bvhGenerator } = this;
17
- const { geometry, materials, textures } = this.prepScene( scene );
17
+ const { geometry, materials, textures, lights, spotLights } = this.prepScene( scene );
18
18
 
19
19
  const bvhOptions = { strategy: SAH, ...options, maxLeafTris: 1 };
20
20
  const bvhPromise = bvhGenerator.generate( geometry, bvhOptions );
@@ -24,6 +24,8 @@ export class PathTracingSceneWorker extends PathTracingSceneGenerator {
24
24
  scene,
25
25
  materials,
26
26
  textures,
27
+ lights,
28
+ spotLights,
27
29
  bvh,
28
30
  };
29
31