three-gpu-pathtracer 0.0.10 → 0.0.12

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 (53) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +886 -886
  3. package/build/index.module.js +6479 -6472
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +6478 -6470
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +72 -69
  8. package/src/core/DynamicPathTracingSceneGenerator.js +119 -119
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +275 -275
  11. package/src/core/PathTracingSceneGenerator.js +69 -69
  12. package/src/index.js +39 -39
  13. package/src/materials/AlphaDisplayMaterial.js +48 -48
  14. package/src/materials/AmbientOcclusionMaterial.js +199 -199
  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 -982
  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/shaderBvhAnyHit.js +76 -0
  26. package/src/shader/shaderEnvMapSampling.js +58 -58
  27. package/src/shader/shaderGGXFunctions.js +100 -100
  28. package/src/shader/shaderIridescenceFunctions.js +130 -130
  29. package/src/shader/shaderLayerTexelFetchFunctions.js +25 -25
  30. package/src/shader/shaderLightSampling.js +229 -229
  31. package/src/shader/shaderMaterialSampling.js +506 -498
  32. package/src/shader/shaderRandFunctions.js +57 -57
  33. package/src/shader/shaderSheenFunctions.js +98 -98
  34. package/src/shader/shaderSobolSampling.js +256 -256
  35. package/src/shader/shaderStructs.js +325 -325
  36. package/src/shader/shaderUtils.js +361 -361
  37. package/src/textures/GradientEquirectTexture.js +35 -35
  38. package/src/textures/ProceduralEquirectTexture.js +75 -75
  39. package/src/uniforms/AttributesTextureArray.js +35 -35
  40. package/src/uniforms/EquirectHdrInfoUniform.js +259 -259
  41. package/src/uniforms/FloatAttributeTextureArray.js +169 -169
  42. package/src/uniforms/IESProfilesTexture.js +100 -100
  43. package/src/uniforms/LightsInfoUniformStruct.js +207 -207
  44. package/src/uniforms/MaterialsTexture.js +426 -426
  45. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  46. package/src/uniforms/RenderTarget2DArray.js +97 -97
  47. package/src/uniforms/utils.js +30 -30
  48. package/src/utils/BlurredEnvMapGenerator.js +116 -116
  49. package/src/utils/GeometryPreparationUtils.js +214 -214
  50. package/src/utils/IESLoader.js +325 -325
  51. package/src/utils/SobolNumberMapGenerator.js +80 -80
  52. package/src/utils/UVUnwrapper.js +101 -101
  53. package/src/workers/PathTracingSceneWorker.js +42 -42
@@ -1,214 +1,214 @@
1
- import { BufferAttribute } from 'three';
2
- import { mergeBufferGeometries, mergeVertices } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
3
- export function getGroupMaterialIndicesAttribute( geometry, materials, allMaterials ) {
4
-
5
- const indexAttr = geometry.index;
6
- const posAttr = geometry.attributes.position;
7
- const vertCount = posAttr.count;
8
- const totalCount = indexAttr ? indexAttr.count : vertCount;
9
- let groups = geometry.groups;
10
- if ( groups.length === 0 ) {
11
-
12
- groups = [ { count: totalCount, start: 0, materialIndex: 0 } ];
13
-
14
- }
15
-
16
- // use an array with the minimum precision required to store all material id references.
17
- let materialArray;
18
- if ( allMaterials.length <= 255 ) {
19
-
20
- materialArray = new Uint8Array( vertCount );
21
-
22
- } else {
23
-
24
- materialArray = new Uint16Array( vertCount );
25
-
26
- }
27
-
28
- for ( let i = 0; i < groups.length; i ++ ) {
29
-
30
- const group = groups[ i ];
31
- const start = group.start;
32
- const count = group.count;
33
- const endCount = Math.min( count, totalCount - start );
34
-
35
- const mat = Array.isArray( materials ) ? materials[ group.materialIndex ] : materials;
36
- const materialIndex = allMaterials.indexOf( mat );
37
-
38
- for ( let j = 0; j < endCount; j ++ ) {
39
-
40
- let index = start + j;
41
- if ( indexAttr ) {
42
-
43
- index = indexAttr.getX( index );
44
-
45
- }
46
-
47
- materialArray[ index ] = materialIndex;
48
-
49
- }
50
-
51
- }
52
-
53
- return new BufferAttribute( materialArray, 1, false );
54
-
55
- }
56
-
57
- export function trimToAttributes( geometry, attributes ) {
58
-
59
- // trim any unneeded attributes
60
- if ( attributes ) {
61
-
62
- for ( const key in geometry.attributes ) {
63
-
64
- if ( ! attributes.includes( key ) ) {
65
-
66
- geometry.deleteAttribute( key );
67
-
68
- }
69
-
70
- }
71
-
72
- }
73
-
74
- }
75
-
76
- export function setCommonAttributes( geometry, options ) {
77
-
78
- const { attributes = [], normalMapRequired = false } = options;
79
-
80
- if ( ! geometry.attributes.normal && ( attributes && attributes.includes( 'normal' ) ) ) {
81
-
82
- geometry.computeVertexNormals();
83
-
84
- }
85
-
86
- if ( ! geometry.attributes.uv && ( attributes && attributes.includes( 'uv' ) ) ) {
87
-
88
- const vertCount = geometry.attributes.position.count;
89
- geometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( vertCount * 2 ), 2, false ) );
90
-
91
- }
92
-
93
- if ( ! geometry.attributes.tangent && ( attributes && attributes.includes( 'tangent' ) ) ) {
94
-
95
- if ( normalMapRequired ) {
96
-
97
- // computeTangents requires an index buffer
98
- if ( geometry.index === null ) {
99
-
100
- geometry = mergeVertices( geometry );
101
-
102
- }
103
-
104
- geometry.computeTangents();
105
-
106
- } else {
107
-
108
- const vertCount = geometry.attributes.position.count;
109
- geometry.setAttribute( 'tangent', new BufferAttribute( new Float32Array( vertCount * 4 ), 4, false ) );
110
-
111
- }
112
-
113
- }
114
-
115
- if ( ! geometry.attributes.color && ( attributes && attributes.includes( 'color' ) ) ) {
116
-
117
- const vertCount = geometry.attributes.position.count;
118
- const array = new Float32Array( vertCount * 4 );
119
- array.fill( 1.0 );
120
- geometry.setAttribute( 'color', new BufferAttribute( array, 4 ) );
121
-
122
- }
123
-
124
- if ( ! geometry.index ) {
125
-
126
- // TODO: compute a typed array
127
- const indexCount = geometry.attributes.position.count;
128
- const array = new Array( indexCount );
129
- for ( let i = 0; i < indexCount; i ++ ) {
130
-
131
- array[ i ] = i;
132
-
133
- }
134
-
135
- geometry.setIndex( array );
136
-
137
- }
138
-
139
- }
140
-
141
- export function mergeMeshes( meshes, options = {} ) {
142
-
143
- options = { attributes: null, cloneGeometry: true, ...options };
144
-
145
- const transformedGeometry = [];
146
- const materialSet = new Set();
147
- for ( let i = 0, l = meshes.length; i < l; i ++ ) {
148
-
149
- // save any materials
150
- const mesh = meshes[ i ];
151
- if ( mesh.visible === false ) continue;
152
-
153
- if ( Array.isArray( mesh.material ) ) {
154
-
155
- mesh.material.forEach( m => materialSet.add( m ) );
156
-
157
- } else {
158
-
159
- materialSet.add( mesh.material );
160
-
161
- }
162
-
163
- }
164
-
165
- const materials = Array.from( materialSet );
166
- for ( let i = 0, l = meshes.length; i < l; i ++ ) {
167
-
168
- // ensure the matrix world is up to date
169
- const mesh = meshes[ i ];
170
- if ( mesh.visible === false ) continue;
171
-
172
- mesh.updateMatrixWorld();
173
-
174
- // apply the matrix world to the geometry
175
- const originalGeometry = meshes[ i ].geometry;
176
- const geometry = options.cloneGeometry ? originalGeometry.clone() : originalGeometry;
177
- geometry.applyMatrix4( mesh.matrixWorld );
178
-
179
- // ensure our geometry has common attributes
180
- setCommonAttributes( geometry, {
181
- attributes: options.attributes,
182
- normalMapRequired: ! ! mesh.material.normalMap,
183
- } );
184
- trimToAttributes( geometry, options.attributes );
185
-
186
- // create the material index attribute
187
- const materialIndexAttribute = getGroupMaterialIndicesAttribute( geometry, mesh.material, materials );
188
- geometry.setAttribute( 'materialIndex', materialIndexAttribute );
189
-
190
- transformedGeometry.push( geometry );
191
-
192
- }
193
-
194
- const textureSet = new Set();
195
- materials.forEach( material => {
196
-
197
- for ( const key in material ) {
198
-
199
- const value = material[ key ];
200
- if ( value && value.isTexture ) {
201
-
202
- textureSet.add( value );
203
-
204
- }
205
-
206
- }
207
-
208
- } );
209
-
210
- const geometry = mergeBufferGeometries( transformedGeometry, false );
211
- const textures = Array.from( textureSet );
212
- return { geometry, materials, textures };
213
-
214
- }
1
+ import { BufferAttribute } from 'three';
2
+ import { mergeBufferGeometries, mergeVertices } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
3
+ export function getGroupMaterialIndicesAttribute( geometry, materials, allMaterials ) {
4
+
5
+ const indexAttr = geometry.index;
6
+ const posAttr = geometry.attributes.position;
7
+ const vertCount = posAttr.count;
8
+ const totalCount = indexAttr ? indexAttr.count : vertCount;
9
+ let groups = geometry.groups;
10
+ if ( groups.length === 0 ) {
11
+
12
+ groups = [ { count: totalCount, start: 0, materialIndex: 0 } ];
13
+
14
+ }
15
+
16
+ // use an array with the minimum precision required to store all material id references.
17
+ let materialArray;
18
+ if ( allMaterials.length <= 255 ) {
19
+
20
+ materialArray = new Uint8Array( vertCount );
21
+
22
+ } else {
23
+
24
+ materialArray = new Uint16Array( vertCount );
25
+
26
+ }
27
+
28
+ for ( let i = 0; i < groups.length; i ++ ) {
29
+
30
+ const group = groups[ i ];
31
+ const start = group.start;
32
+ const count = group.count;
33
+ const endCount = Math.min( count, totalCount - start );
34
+
35
+ const mat = Array.isArray( materials ) ? materials[ group.materialIndex ] : materials;
36
+ const materialIndex = allMaterials.indexOf( mat );
37
+
38
+ for ( let j = 0; j < endCount; j ++ ) {
39
+
40
+ let index = start + j;
41
+ if ( indexAttr ) {
42
+
43
+ index = indexAttr.getX( index );
44
+
45
+ }
46
+
47
+ materialArray[ index ] = materialIndex;
48
+
49
+ }
50
+
51
+ }
52
+
53
+ return new BufferAttribute( materialArray, 1, false );
54
+
55
+ }
56
+
57
+ export function trimToAttributes( geometry, attributes ) {
58
+
59
+ // trim any unneeded attributes
60
+ if ( attributes ) {
61
+
62
+ for ( const key in geometry.attributes ) {
63
+
64
+ if ( ! attributes.includes( key ) ) {
65
+
66
+ geometry.deleteAttribute( key );
67
+
68
+ }
69
+
70
+ }
71
+
72
+ }
73
+
74
+ }
75
+
76
+ export function setCommonAttributes( geometry, options ) {
77
+
78
+ const { attributes = [], normalMapRequired = false } = options;
79
+
80
+ if ( ! geometry.attributes.normal && ( attributes && attributes.includes( 'normal' ) ) ) {
81
+
82
+ geometry.computeVertexNormals();
83
+
84
+ }
85
+
86
+ if ( ! geometry.attributes.uv && ( attributes && attributes.includes( 'uv' ) ) ) {
87
+
88
+ const vertCount = geometry.attributes.position.count;
89
+ geometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( vertCount * 2 ), 2, false ) );
90
+
91
+ }
92
+
93
+ if ( ! geometry.attributes.tangent && ( attributes && attributes.includes( 'tangent' ) ) ) {
94
+
95
+ if ( normalMapRequired ) {
96
+
97
+ // computeTangents requires an index buffer
98
+ if ( geometry.index === null ) {
99
+
100
+ geometry = mergeVertices( geometry );
101
+
102
+ }
103
+
104
+ geometry.computeTangents();
105
+
106
+ } else {
107
+
108
+ const vertCount = geometry.attributes.position.count;
109
+ geometry.setAttribute( 'tangent', new BufferAttribute( new Float32Array( vertCount * 4 ), 4, false ) );
110
+
111
+ }
112
+
113
+ }
114
+
115
+ if ( ! geometry.attributes.color && ( attributes && attributes.includes( 'color' ) ) ) {
116
+
117
+ const vertCount = geometry.attributes.position.count;
118
+ const array = new Float32Array( vertCount * 4 );
119
+ array.fill( 1.0 );
120
+ geometry.setAttribute( 'color', new BufferAttribute( array, 4 ) );
121
+
122
+ }
123
+
124
+ if ( ! geometry.index ) {
125
+
126
+ // TODO: compute a typed array
127
+ const indexCount = geometry.attributes.position.count;
128
+ const array = new Array( indexCount );
129
+ for ( let i = 0; i < indexCount; i ++ ) {
130
+
131
+ array[ i ] = i;
132
+
133
+ }
134
+
135
+ geometry.setIndex( array );
136
+
137
+ }
138
+
139
+ }
140
+
141
+ export function mergeMeshes( meshes, options = {} ) {
142
+
143
+ options = { attributes: null, cloneGeometry: true, ...options };
144
+
145
+ const transformedGeometry = [];
146
+ const materialSet = new Set();
147
+ for ( let i = 0, l = meshes.length; i < l; i ++ ) {
148
+
149
+ // save any materials
150
+ const mesh = meshes[ i ];
151
+ if ( mesh.visible === false ) continue;
152
+
153
+ if ( Array.isArray( mesh.material ) ) {
154
+
155
+ mesh.material.forEach( m => materialSet.add( m ) );
156
+
157
+ } else {
158
+
159
+ materialSet.add( mesh.material );
160
+
161
+ }
162
+
163
+ }
164
+
165
+ const materials = Array.from( materialSet );
166
+ for ( let i = 0, l = meshes.length; i < l; i ++ ) {
167
+
168
+ // ensure the matrix world is up to date
169
+ const mesh = meshes[ i ];
170
+ if ( mesh.visible === false ) continue;
171
+
172
+ mesh.updateMatrixWorld();
173
+
174
+ // apply the matrix world to the geometry
175
+ const originalGeometry = meshes[ i ].geometry;
176
+ const geometry = options.cloneGeometry ? originalGeometry.clone() : originalGeometry;
177
+ geometry.applyMatrix4( mesh.matrixWorld );
178
+
179
+ // ensure our geometry has common attributes
180
+ setCommonAttributes( geometry, {
181
+ attributes: options.attributes,
182
+ normalMapRequired: ! ! mesh.material.normalMap,
183
+ } );
184
+ trimToAttributes( geometry, options.attributes );
185
+
186
+ // create the material index attribute
187
+ const materialIndexAttribute = getGroupMaterialIndicesAttribute( geometry, mesh.material, materials );
188
+ geometry.setAttribute( 'materialIndex', materialIndexAttribute );
189
+
190
+ transformedGeometry.push( geometry );
191
+
192
+ }
193
+
194
+ const textureSet = new Set();
195
+ materials.forEach( material => {
196
+
197
+ for ( const key in material ) {
198
+
199
+ const value = material[ key ];
200
+ if ( value && value.isTexture ) {
201
+
202
+ textureSet.add( value );
203
+
204
+ }
205
+
206
+ }
207
+
208
+ } );
209
+
210
+ const geometry = mergeBufferGeometries( transformedGeometry, false );
211
+ const textures = Array.from( textureSet );
212
+ return { geometry, materials, textures };
213
+
214
+ }