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,194 +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 materialArray = new Uint8Array( vertCount );
9
- const totalCount = indexAttr ? indexAttr.count : vertCount;
10
- let groups = geometry.groups;
11
- if ( groups.length === 0 ) {
12
-
13
- groups = [ { count: totalCount, start: 0, materialIndex: 0 } ];
14
-
15
- }
16
-
17
- for ( let i = 0; i < groups.length; i ++ ) {
18
-
19
- const group = groups[ i ];
20
- const start = group.start;
21
- const count = group.count;
22
- const endCount = Math.min( count, totalCount - start );
23
-
24
- const mat = Array.isArray( materials ) ? materials[ group.materialIndex ] : materials;
25
- const materialIndex = allMaterials.indexOf( mat );
26
-
27
- for ( let j = 0; j < endCount; j ++ ) {
28
-
29
- let index = start + j;
30
- if ( indexAttr ) {
31
-
32
- index = indexAttr.getX( index );
33
-
34
- }
35
-
36
- materialArray[ index ] = materialIndex;
37
-
38
- }
39
-
40
- }
41
-
42
- return new BufferAttribute( materialArray, 1, false );
43
-
44
- }
45
-
46
- export function trimToAttributes( geometry, attributes ) {
47
-
48
- // trim any unneeded attributes
49
- if ( attributes ) {
50
-
51
- for ( const key in geometry.attributes ) {
52
-
53
- if ( ! attributes.includes( key ) ) {
54
-
55
- geometry.deleteAttribute( key );
56
-
57
- }
58
-
59
- }
60
-
61
- }
62
-
63
- }
64
-
65
- export function setCommonAttributes( geometry, options ) {
66
-
67
- const { attributes = [], normalMapRequired = false } = options;
68
-
69
- if ( ! geometry.attributes.normal && ( attributes && attributes.includes( 'normal' ) ) ) {
70
-
71
- geometry.computeVertexNormals();
72
-
73
- }
74
-
75
- if ( ! geometry.attributes.uv && ( attributes && attributes.includes( 'uv' ) ) ) {
76
-
77
- const vertCount = geometry.attributes.position.count;
78
- geometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( vertCount * 2 ), 2, false ) );
79
-
80
- }
81
-
82
- if ( ! geometry.attributes.tangent && ( attributes && attributes.includes( 'tangent' ) ) ) {
83
-
84
- if ( normalMapRequired ) {
85
-
86
- // computeTangents requires an index buffer
87
- if ( geometry.index === null ) {
88
-
89
- geometry = mergeVertices( geometry );
90
-
91
- }
92
-
93
- geometry.computeTangents();
94
-
95
- } else {
96
-
97
- const vertCount = geometry.attributes.position.count;
98
- geometry.setAttribute( 'tangent', new BufferAttribute( new Float32Array( vertCount * 4 ), 4, false ) );
99
-
100
- }
101
-
102
- }
103
-
104
- if ( ! geometry.index ) {
105
-
106
- // TODO: compute a typed array
107
- const indexCount = geometry.attributes.position.count;
108
- const array = new Array( indexCount );
109
- for ( let i = 0; i < indexCount; i ++ ) {
110
-
111
- array[ i ] = i;
112
-
113
- }
114
-
115
- geometry.setIndex( array );
116
-
117
- }
118
-
119
- }
120
-
121
- export function mergeMeshes( meshes, options = {} ) {
122
-
123
- options = { attributes: null, cloneGeometry: true, ...options };
124
-
125
- const transformedGeometry = [];
126
- const materialSet = new Set();
127
- for ( let i = 0, l = meshes.length; i < l; i ++ ) {
128
-
129
- // save any materials
130
- const mesh = meshes[ i ];
131
- if ( mesh.visible === false ) continue;
132
-
133
- if ( Array.isArray( mesh.material ) ) {
134
-
135
- mesh.material.forEach( m => materialSet.add( m ) );
136
-
137
- } else {
138
-
139
- materialSet.add( mesh.material );
140
-
141
- }
142
-
143
- }
144
-
145
- const materials = Array.from( materialSet );
146
- for ( let i = 0, l = meshes.length; i < l; i ++ ) {
147
-
148
- // ensure the matrix world is up to date
149
- const mesh = meshes[ i ];
150
- if ( mesh.visible === false ) continue;
151
-
152
- mesh.updateMatrixWorld();
153
-
154
- // apply the matrix world to the geometry
155
- const originalGeometry = meshes[ i ].geometry;
156
- const geometry = options.cloneGeometry ? originalGeometry.clone() : originalGeometry;
157
- geometry.applyMatrix4( mesh.matrixWorld );
158
-
159
- // ensure our geometry has common attributes
160
- setCommonAttributes( geometry, {
161
- attributes: options.attributes,
162
- normalMapRequired: ! ! mesh.material.normalMap,
163
- } );
164
- trimToAttributes( geometry, options.attributes );
165
-
166
- // create the material index attribute
167
- const materialIndexAttribute = getGroupMaterialIndicesAttribute( geometry, mesh.material, materials );
168
- geometry.setAttribute( 'materialIndex', materialIndexAttribute );
169
-
170
- transformedGeometry.push( geometry );
171
-
172
- }
173
-
174
- const textureSet = new Set();
175
- materials.forEach( material => {
176
-
177
- for ( const key in material ) {
178
-
179
- const value = material[ key ];
180
- if ( value && value.isTexture ) {
181
-
182
- textureSet.add( value );
183
-
184
- }
185
-
186
- }
187
-
188
- } );
189
-
190
- const geometry = mergeBufferGeometries( transformedGeometry, false );
191
- const textures = Array.from( textureSet );
192
- return { geometry, materials, textures };
193
-
194
- }
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
+ }