three-gpu-pathtracer 0.0.14 → 0.0.16

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 (76) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +1004 -981
  3. package/build/index.module.js +7413 -6902
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +7446 -6933
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +73 -73
  8. package/src/core/DynamicPathTracingSceneGenerator.js +119 -119
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +346 -346
  11. package/src/core/PathTracingSceneGenerator.js +69 -69
  12. package/src/core/QuiltPathTracingRenderer.js +223 -223
  13. package/src/detectors/CompatibilityDetector.js +38 -0
  14. package/src/detectors/MaterialCompileDetector.js +50 -0
  15. package/src/detectors/PrecisionDetector.js +85 -0
  16. package/src/detectors/PrecisionMaterial.js +160 -0
  17. package/src/index.js +40 -36
  18. package/src/materials/MaterialBase.js +56 -56
  19. package/src/materials/debug/GraphMaterial.js +243 -243
  20. package/src/materials/fullscreen/AlphaDisplayMaterial.js +50 -48
  21. package/src/materials/fullscreen/BlendMaterial.js +67 -67
  22. package/src/materials/fullscreen/DenoiseMaterial.js +142 -142
  23. package/src/materials/fullscreen/GradientMapMaterial.js +82 -0
  24. package/src/materials/pathtracing/LambertPathTracingMaterial.js +296 -296
  25. package/src/materials/pathtracing/PhysicalPathTracingMaterial.js +118 -196
  26. package/src/materials/pathtracing/glsl/attenuateHit.glsl.js +177 -179
  27. package/src/materials/pathtracing/glsl/cameraUtils.glsl.js +84 -81
  28. package/src/materials/pathtracing/glsl/directLightContribution.glsl.js +93 -0
  29. package/src/materials/pathtracing/glsl/getSurfaceRecord.glsl.js +323 -317
  30. package/src/materials/pathtracing/glsl/renderStructs.glsl.js +50 -0
  31. package/src/materials/pathtracing/glsl/traceScene.glsl.js +52 -54
  32. package/src/materials/surface/AmbientOcclusionMaterial.js +207 -207
  33. package/src/materials/surface/FogVolumeMaterial.js +23 -23
  34. package/src/objects/EquirectCamera.js +13 -13
  35. package/src/objects/PhysicalCamera.js +42 -28
  36. package/src/objects/PhysicalSpotLight.js +25 -14
  37. package/src/objects/ShapedAreaLight.js +22 -12
  38. package/src/shader/bsdf/bsdfSampling.glsl.js +499 -490
  39. package/src/shader/bsdf/fog.glsl.js +22 -23
  40. package/src/shader/bsdf/ggx.glsl.js +102 -102
  41. package/src/shader/bsdf/iridescence.glsl.js +135 -135
  42. package/src/shader/bsdf/sheen.glsl.js +98 -98
  43. package/src/shader/common/arraySamplerTexelFetch.glsl.js +25 -25
  44. package/src/shader/common/bvhAnyHit.glsl.js +76 -76
  45. package/src/shader/common/fresnel.glsl.js +98 -98
  46. package/src/shader/common/intersectShapes.glsl.js +62 -62
  47. package/src/shader/common/math.glsl.js +81 -81
  48. package/src/shader/common/utils.glsl.js +116 -116
  49. package/src/shader/rand/pcg.glsl.js +57 -57
  50. package/src/shader/rand/sobol.glsl.js +256 -256
  51. package/src/shader/sampling/equirectSampling.glsl.js +62 -62
  52. package/src/shader/sampling/lightSampling.glsl.js +223 -223
  53. package/src/shader/sampling/shapeSampling.glsl.js +86 -86
  54. package/src/shader/structs/cameraStruct.glsl.js +13 -13
  55. package/src/shader/structs/equirectStruct.glsl.js +13 -14
  56. package/src/shader/structs/fogMaterialBvh.glsl.js +62 -62
  57. package/src/shader/structs/lightsStruct.glsl.js +78 -78
  58. package/src/shader/structs/materialStruct.glsl.js +207 -207
  59. package/src/textures/GradientEquirectTexture.js +35 -35
  60. package/src/textures/ProceduralEquirectTexture.js +75 -75
  61. package/src/uniforms/AttributesTextureArray.js +35 -35
  62. package/src/uniforms/EquirectHdrInfoUniform.js +269 -277
  63. package/src/uniforms/FloatAttributeTextureArray.js +169 -169
  64. package/src/uniforms/IESProfilesTexture.js +100 -100
  65. package/src/uniforms/LightsInfoUniformStruct.js +212 -212
  66. package/src/uniforms/MaterialsTexture.js +503 -503
  67. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  68. package/src/uniforms/RenderTarget2DArray.js +97 -97
  69. package/src/uniforms/utils.js +30 -30
  70. package/src/utils/BlurredEnvMapGenerator.js +116 -116
  71. package/src/utils/GeometryPreparationUtils.js +214 -214
  72. package/src/utils/IESLoader.js +325 -325
  73. package/src/utils/SobolNumberMapGenerator.js +80 -80
  74. package/src/utils/UVUnwrapper.js +101 -101
  75. package/src/utils/macroify.js +9 -9
  76. 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
+ }