three-gpu-pathtracer 0.0.13 → 0.0.14

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 (78) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +981 -961
  3. package/build/index.module.js +6965 -6508
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +6959 -6505
  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/index.js +36 -40
  14. package/src/materials/MaterialBase.js +56 -56
  15. package/src/materials/{GraphMaterial.js → debug/GraphMaterial.js} +243 -243
  16. package/src/materials/{AlphaDisplayMaterial.js → fullscreen/AlphaDisplayMaterial.js} +48 -48
  17. package/src/materials/{BlendMaterial.js → fullscreen/BlendMaterial.js} +67 -67
  18. package/src/materials/{DenoiseMaterial.js → fullscreen/DenoiseMaterial.js} +142 -142
  19. package/src/materials/{LambertPathTracingMaterial.js → pathtracing/LambertPathTracingMaterial.js} +296 -285
  20. package/src/materials/pathtracing/PhysicalPathTracingMaterial.js +635 -0
  21. package/src/materials/pathtracing/glsl/attenuateHit.glsl.js +179 -0
  22. package/src/materials/pathtracing/glsl/cameraUtils.glsl.js +81 -0
  23. package/src/materials/pathtracing/glsl/getSurfaceRecord.glsl.js +317 -0
  24. package/src/materials/pathtracing/glsl/traceScene.glsl.js +54 -0
  25. package/src/materials/{AmbientOcclusionMaterial.js → surface/AmbientOcclusionMaterial.js} +207 -199
  26. package/src/materials/surface/FogVolumeMaterial.js +23 -0
  27. package/src/objects/EquirectCamera.js +13 -13
  28. package/src/objects/PhysicalCamera.js +28 -28
  29. package/src/objects/PhysicalSpotLight.js +14 -14
  30. package/src/objects/ShapedAreaLight.js +12 -12
  31. package/src/shader/bsdf/bsdfSampling.glsl.js +490 -0
  32. package/src/shader/bsdf/fog.glsl.js +23 -0
  33. package/src/shader/bsdf/ggx.glsl.js +102 -0
  34. package/src/shader/bsdf/iridescence.glsl.js +135 -0
  35. package/src/shader/bsdf/sheen.glsl.js +98 -0
  36. package/src/shader/{shaderLayerTexelFetchFunctions.js → common/arraySamplerTexelFetch.glsl.js} +25 -25
  37. package/src/shader/common/bvhAnyHit.glsl.js +76 -0
  38. package/src/shader/common/fresnel.glsl.js +98 -0
  39. package/src/shader/common/intersectShapes.glsl.js +62 -0
  40. package/src/shader/common/math.glsl.js +81 -0
  41. package/src/shader/common/utils.glsl.js +116 -0
  42. package/src/shader/{shaderRandFunctions.js → rand/pcg.glsl.js} +57 -57
  43. package/src/shader/{shaderSobolSampling.js → rand/sobol.glsl.js} +256 -256
  44. package/src/shader/sampling/equirectSampling.glsl.js +62 -0
  45. package/src/shader/sampling/lightSampling.glsl.js +223 -0
  46. package/src/shader/sampling/shapeSampling.glsl.js +86 -0
  47. package/src/shader/structs/cameraStruct.glsl.js +13 -0
  48. package/src/shader/structs/equirectStruct.glsl.js +14 -0
  49. package/src/shader/structs/fogMaterialBvh.glsl.js +62 -0
  50. package/src/shader/structs/lightsStruct.glsl.js +78 -0
  51. package/src/shader/{shaderStructs.js → structs/materialStruct.glsl.js} +207 -327
  52. package/src/textures/GradientEquirectTexture.js +35 -35
  53. package/src/textures/ProceduralEquirectTexture.js +75 -75
  54. package/src/uniforms/AttributesTextureArray.js +35 -35
  55. package/src/uniforms/EquirectHdrInfoUniform.js +277 -273
  56. package/src/uniforms/FloatAttributeTextureArray.js +169 -169
  57. package/src/uniforms/IESProfilesTexture.js +100 -100
  58. package/src/uniforms/LightsInfoUniformStruct.js +212 -212
  59. package/src/uniforms/MaterialsTexture.js +503 -426
  60. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  61. package/src/uniforms/RenderTarget2DArray.js +97 -97
  62. package/src/uniforms/utils.js +30 -30
  63. package/src/utils/BlurredEnvMapGenerator.js +116 -116
  64. package/src/utils/GeometryPreparationUtils.js +214 -214
  65. package/src/utils/IESLoader.js +325 -325
  66. package/src/utils/SobolNumberMapGenerator.js +80 -80
  67. package/src/utils/UVUnwrapper.js +101 -101
  68. package/src/utils/macroify.js +9 -0
  69. package/src/workers/PathTracingSceneWorker.js +42 -42
  70. package/src/materials/PhysicalPathTracingMaterial.js +0 -1013
  71. package/src/shader/shaderBvhAnyHit.js +0 -76
  72. package/src/shader/shaderEnvMapSampling.js +0 -58
  73. package/src/shader/shaderGGXFunctions.js +0 -100
  74. package/src/shader/shaderIridescenceFunctions.js +0 -135
  75. package/src/shader/shaderLightSampling.js +0 -229
  76. package/src/shader/shaderMaterialSampling.js +0 -510
  77. package/src/shader/shaderSheenFunctions.js +0 -98
  78. package/src/shader/shaderUtils.js +0 -377
@@ -1,426 +1,503 @@
1
- import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, FrontSide, BackSide, DoubleSide } from 'three';
2
- import { reduceTexturesToUniqueSources, getTextureHash } from './utils.js';
3
-
4
- const MATERIAL_PIXELS = 45;
5
- const MATERIAL_STRIDE = MATERIAL_PIXELS * 4;
6
-
7
- const MATTE_OFFSET = 14 * 4 + 0; // s14.r
8
- const SHADOW_OFFSET = 14 * 4 + 1; // s14.g
9
-
10
- export class MaterialsTexture extends DataTexture {
11
-
12
- constructor() {
13
-
14
- super( new Float32Array( 4 ), 1, 1 );
15
-
16
- this.format = RGBAFormat;
17
- this.type = FloatType;
18
- this.wrapS = ClampToEdgeWrapping;
19
- this.wrapT = ClampToEdgeWrapping;
20
- this.generateMipmaps = false;
21
- this.threeCompatibilityTransforms = false;
22
-
23
- }
24
-
25
- setCastShadow( materialIndex, cast ) {
26
-
27
- // invert the shadow value so we default to "true" when initializing a material
28
- const array = this.image.data;
29
- const index = materialIndex * MATERIAL_STRIDE + SHADOW_OFFSET;
30
- array[ index ] = ! cast ? 1 : 0;
31
-
32
- }
33
-
34
- getCastShadow( materialIndex ) {
35
-
36
- const array = this.image.data;
37
- const index = materialIndex * MATERIAL_STRIDE + SHADOW_OFFSET;
38
- return ! Boolean( array[ index ] );
39
-
40
- }
41
-
42
- setMatte( materialIndex, matte ) {
43
-
44
- const array = this.image.data;
45
- const index = materialIndex * MATERIAL_STRIDE + MATTE_OFFSET;
46
- array[ index ] = matte ? 1 : 0;
47
-
48
- }
49
-
50
- getMatte( materialIndex ) {
51
-
52
- const array = this.image.data;
53
- const index = materialIndex * MATERIAL_STRIDE + MATTE_OFFSET;
54
- return Boolean( array[ index ] );
55
-
56
- }
57
-
58
- updateFrom( materials, textures ) {
59
-
60
- function getTexture( material, key, def = - 1 ) {
61
-
62
- if ( key in material && material[ key ] ) {
63
-
64
- const hash = getTextureHash( material[ key ] );
65
- return uniqueTextureLookup[ hash ];
66
-
67
- } else {
68
-
69
- return def;
70
-
71
- }
72
-
73
- }
74
-
75
- function getField( material, key, def ) {
76
-
77
- return key in material ? material[ key ] : def;
78
-
79
- }
80
-
81
- function getUVTransformTexture( material ) {
82
-
83
- // https://github.com/mrdoob/three.js/blob/f3a832e637c98a404c64dae8174625958455e038/src/renderers/webgl/WebGLMaterials.js#L204-L306
84
- // https://threejs.org/docs/#api/en/textures/Texture.offset
85
- // fallback order of textures to use as a common uv transform
86
- return material.map ||
87
- material.specularMap ||
88
- material.displacementMap ||
89
- material.normalMap ||
90
- material.bumpMap ||
91
- material.roughnessMap ||
92
- material.metalnessMap ||
93
- material.alphaMap ||
94
- material.emissiveMap ||
95
- material.clearcoatMap ||
96
- material.clearcoatNormalMap ||
97
- material.clearcoatRoughnessMap ||
98
- material.iridescenceMap ||
99
- material.iridescenceThicknessMap ||
100
- material.specularIntensityMap ||
101
- material.specularColorMap ||
102
- material.transmissionMap ||
103
- material.thicknessMap ||
104
- material.sheenColorMap ||
105
- material.sheenRoughnessMap ||
106
- null;
107
-
108
- }
109
-
110
- function writeTextureMatrixToArray( material, textureKey, array, offset ) {
111
-
112
- let texture;
113
- if ( threeCompatibilityTransforms ) {
114
-
115
- texture = getUVTransformTexture( material );
116
-
117
- } else {
118
-
119
- texture = material[ textureKey ] && material[ textureKey ].isTexture ? material[ textureKey ] : null;
120
-
121
- }
122
-
123
- // check if texture exists
124
- if ( texture ) {
125
-
126
- const elements = texture.matrix.elements;
127
-
128
- let i = 0;
129
-
130
- // first row
131
- array[ offset + i ++ ] = elements[ 0 ];
132
- array[ offset + i ++ ] = elements[ 3 ];
133
- array[ offset + i ++ ] = elements[ 6 ];
134
- i ++;
135
-
136
- // second row
137
- array[ offset + i ++ ] = elements[ 1 ];
138
- array[ offset + i ++ ] = elements[ 4 ];
139
- array[ offset + i ++ ] = elements[ 7 ];
140
- i ++;
141
-
142
- }
143
-
144
- return 8;
145
-
146
- }
147
-
148
- let index = 0;
149
- const pixelCount = materials.length * MATERIAL_PIXELS;
150
- const dimension = Math.ceil( Math.sqrt( pixelCount ) );
151
- const { threeCompatibilityTransforms, image } = this;
152
-
153
- // get the list of textures with unique sources
154
- const uniqueTextures = reduceTexturesToUniqueSources( textures );
155
- const uniqueTextureLookup = {};
156
- for ( let i = 0, l = uniqueTextures.length; i < l; i ++ ) {
157
-
158
- uniqueTextureLookup[ getTextureHash( uniqueTextures[ i ] ) ] = i;
159
-
160
- }
161
-
162
- if ( image.width !== dimension ) {
163
-
164
- this.dispose();
165
-
166
- image.data = new Float32Array( dimension * dimension * 4 );
167
- image.width = dimension;
168
- image.height = dimension;
169
-
170
- }
171
-
172
- const floatArray = image.data;
173
-
174
- // on some devices (Google Pixel 6) the "floatBitsToInt" function does not work correctly so we
175
- // can't encode texture ids that way.
176
- // const intArray = new Int32Array( floatArray.buffer );
177
-
178
- for ( let i = 0, l = materials.length; i < l; i ++ ) {
179
-
180
- const m = materials[ i ];
181
-
182
- // sample 0
183
- // color
184
- floatArray[ index ++ ] = m.color.r;
185
- floatArray[ index ++ ] = m.color.g;
186
- floatArray[ index ++ ] = m.color.b;
187
- floatArray[ index ++ ] = getTexture( m, 'map' );
188
-
189
- // sample 1
190
- // metalness & roughness
191
- floatArray[ index ++ ] = getField( m, 'metalness', 0.0 );
192
- floatArray[ index ++ ] = getTexture( m, 'metalnessMap' );
193
- floatArray[ index ++ ] = getField( m, 'roughness', 0.0 );
194
- floatArray[ index ++ ] = getTexture( m, 'roughnessMap' );
195
-
196
- // sample 2
197
- // transmission & emissiveIntensity
198
- // three.js assumes a default f0 of 0.04 if no ior is provided which equates to an ior of 1.5
199
- floatArray[ index ++ ] = getField( m, 'ior', 1.5 );
200
- floatArray[ index ++ ] = getField( m, 'transmission', 0.0 );
201
- floatArray[ index ++ ] = getTexture( m, 'transmissionMap' );
202
- floatArray[ index ++ ] = getField( m, 'emissiveIntensity', 0.0 );
203
-
204
- // sample 3
205
- // emission
206
- if ( 'emissive' in m ) {
207
-
208
- floatArray[ index ++ ] = m.emissive.r;
209
- floatArray[ index ++ ] = m.emissive.g;
210
- floatArray[ index ++ ] = m.emissive.b;
211
-
212
- } else {
213
-
214
- floatArray[ index ++ ] = 0.0;
215
- floatArray[ index ++ ] = 0.0;
216
- floatArray[ index ++ ] = 0.0;
217
-
218
- }
219
-
220
- floatArray[ index ++ ] = getTexture( m, 'emissiveMap' );
221
-
222
- // sample 4
223
- // normals
224
- floatArray[ index ++ ] = getTexture( m, 'normalMap' );
225
- if ( 'normalScale' in m ) {
226
-
227
- floatArray[ index ++ ] = m.normalScale.x;
228
- floatArray[ index ++ ] = m.normalScale.y;
229
-
230
- } else {
231
-
232
- floatArray[ index ++ ] = 1;
233
- floatArray[ index ++ ] = 1;
234
-
235
- }
236
-
237
- // clearcoat
238
- floatArray[ index ++ ] = getField( m, 'clearcoat', 0.0 );
239
- floatArray[ index ++ ] = getTexture( m, 'clearcoatMap' ); // sample 5
240
-
241
- floatArray[ index ++ ] = getField( m, 'clearcoatRoughness', 0.0 );
242
- floatArray[ index ++ ] = getTexture( m, 'clearcoatRoughnessMap' );
243
-
244
- floatArray[ index ++ ] = getTexture( m, 'clearcoatNormalMap' );
245
-
246
- // sample 6
247
- if ( 'clearcoatNormalScale' in m ) {
248
-
249
- floatArray[ index ++ ] = m.clearcoatNormalScale.x;
250
- floatArray[ index ++ ] = m.clearcoatNormalScale.y;
251
-
252
- } else {
253
-
254
- floatArray[ index ++ ] = 1;
255
- floatArray[ index ++ ] = 1;
256
-
257
- }
258
-
259
- index ++;
260
- floatArray[ index ++ ] = getField( m, 'sheen', 0.0 );
261
-
262
- // sample 7
263
- // sheen
264
- if ( 'sheenColor' in m ) {
265
-
266
- floatArray[ index ++ ] = m.sheenColor.r;
267
- floatArray[ index ++ ] = m.sheenColor.g;
268
- floatArray[ index ++ ] = m.sheenColor.b;
269
-
270
- } else {
271
-
272
- floatArray[ index ++ ] = 0.0;
273
- floatArray[ index ++ ] = 0.0;
274
- floatArray[ index ++ ] = 0.0;
275
-
276
- }
277
-
278
- floatArray[ index ++ ] = getTexture( m, 'sheenColorMap' );
279
-
280
- // sample 8
281
- floatArray[ index ++ ] = getField( m, 'sheenRoughness', 0.0 );
282
- floatArray[ index ++ ] = getTexture( m, 'sheenRoughnessMap' );
283
-
284
- // iridescence
285
- floatArray[ index ++ ] = getTexture( m, 'iridescenceMap' );
286
- floatArray[ index ++ ] = getTexture( m, 'iridescenceThicknessMap' );
287
-
288
- floatArray[ index ++ ] = getField( m, 'iridescence', 0.0 ); // sample 9
289
- floatArray[ index ++ ] = getField( m, 'iridescenceIOR', 1.3 );
290
-
291
- const iridescenceThicknessRange = getField( m, 'iridescenceThicknessRange', [ 100, 400 ] );
292
- floatArray[ index ++ ] = iridescenceThicknessRange[ 0 ];
293
- floatArray[ index ++ ] = iridescenceThicknessRange[ 1 ];
294
-
295
- // sample 10
296
- // specular color
297
- if ( 'specularColor' in m ) {
298
-
299
- floatArray[ index ++ ] = m.specularColor.r;
300
- floatArray[ index ++ ] = m.specularColor.g;
301
- floatArray[ index ++ ] = m.specularColor.b;
302
-
303
- } else {
304
-
305
- floatArray[ index ++ ] = 1.0;
306
- floatArray[ index ++ ] = 1.0;
307
- floatArray[ index ++ ] = 1.0;
308
-
309
- }
310
-
311
- floatArray[ index ++ ] = getTexture( m, 'specularColorMap' );
312
-
313
- // sample 11
314
- // specular intensity
315
- floatArray[ index ++ ] = getField( m, 'specularIntensity', 1.0 );
316
- floatArray[ index ++ ] = getTexture( m, 'specularIntensityMap' );
317
-
318
- // isThinFilm
319
- const isThinFilm = getField( m, 'thickness', 0.0 ) === 0.0 && getField( m, 'attenuationDistance', Infinity ) === Infinity;
320
- floatArray[ index ++ ] = Number( isThinFilm );
321
- index ++;
322
-
323
- // sample 12
324
- if ( 'attenuationColor' in m ) {
325
-
326
- floatArray[ index ++ ] = m.attenuationColor.r;
327
- floatArray[ index ++ ] = m.attenuationColor.g;
328
- floatArray[ index ++ ] = m.attenuationColor.b;
329
-
330
- } else {
331
-
332
- floatArray[ index ++ ] = 1.0;
333
- floatArray[ index ++ ] = 1.0;
334
- floatArray[ index ++ ] = 1.0;
335
-
336
- }
337
-
338
- floatArray[ index ++ ] = getField( m, 'attenuationDistance', Infinity );
339
-
340
- // sample 13
341
- // alphaMap
342
- floatArray[ index ++ ] = getTexture( m, 'alphaMap' );
343
-
344
- // side & matte
345
- floatArray[ index ++ ] = m.opacity;
346
- floatArray[ index ++ ] = m.alphaTest;
347
- if ( ! isThinFilm && m.transmission > 0.0 ) {
348
-
349
- floatArray[ index ++ ] = 0;
350
-
351
- } else {
352
-
353
- switch ( m.side ) {
354
-
355
- case FrontSide:
356
- floatArray[ index ++ ] = 1;
357
- break;
358
- case BackSide:
359
- floatArray[ index ++ ] = - 1;
360
- break;
361
- case DoubleSide:
362
- floatArray[ index ++ ] = 0;
363
- break;
364
-
365
- }
366
-
367
- }
368
-
369
- // sample 14
370
- index ++; // matte
371
- index ++; // shadow
372
- floatArray[ index ++ ] = Number( m.vertexColors ) | ( Number( m.flatShading ) << 1 ); // vertexColors & flatShading
373
- floatArray[ index ++ ] = Number( m.transparent ); // transparent
374
-
375
- // map transform 15
376
- index += writeTextureMatrixToArray( m, 'map', floatArray, index );
377
-
378
- // metalnessMap transform 17
379
- index += writeTextureMatrixToArray( m, 'metalnessMap', floatArray, index );
380
-
381
- // roughnessMap transform 19
382
- index += writeTextureMatrixToArray( m, 'roughnessMap', floatArray, index );
383
-
384
- // transmissionMap transform 21
385
- index += writeTextureMatrixToArray( m, 'transmissionMap', floatArray, index );
386
-
387
- // emissiveMap transform 22
388
- index += writeTextureMatrixToArray( m, 'emissiveMap', floatArray, index );
389
-
390
- // normalMap transform 25
391
- index += writeTextureMatrixToArray( m, 'normalMap', floatArray, index );
392
-
393
- // clearcoatMap transform 27
394
- index += writeTextureMatrixToArray( m, 'clearcoatMap', floatArray, index );
395
-
396
- // clearcoatNormalMap transform 29
397
- index += writeTextureMatrixToArray( m, 'clearcoatNormalMap', floatArray, index );
398
-
399
- // clearcoatRoughnessMap transform 31
400
- index += writeTextureMatrixToArray( m, 'clearcoatRoughnessMap', floatArray, index );
401
-
402
- // sheenColorMap transform 33
403
- index += writeTextureMatrixToArray( m, 'sheenColorMap', floatArray, index );
404
-
405
- // sheenRoughnessMap transform 35
406
- index += writeTextureMatrixToArray( m, 'sheenRoughnessMap', floatArray, index );
407
-
408
- // iridescenceMap transform 37
409
- index += writeTextureMatrixToArray( m, 'iridescenceMap', floatArray, index );
410
-
411
- // iridescenceThicknessMap transform 39
412
- index += writeTextureMatrixToArray( m, 'iridescenceThicknessMap', floatArray, index );
413
-
414
- // specularColorMap transform 41
415
- index += writeTextureMatrixToArray( m, 'specularColorMap', floatArray, index );
416
-
417
- // specularIntensityMap transform 43
418
- index += writeTextureMatrixToArray( m, 'specularIntensityMap', floatArray, index );
419
-
420
- }
421
-
422
- this.needsUpdate = true;
423
-
424
- }
425
-
426
- }
1
+ import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, FrontSide, BackSide, DoubleSide } from 'three';
2
+ import { reduceTexturesToUniqueSources, getTextureHash } from './utils.js';
3
+
4
+ const MATERIAL_PIXELS = 45;
5
+ const MATERIAL_STRIDE = MATERIAL_PIXELS * 4;
6
+
7
+ const MATTE_OFFSET = 14 * 4 + 0; // s14.r
8
+ const SHADOW_OFFSET = 14 * 4 + 1; // s14.g
9
+
10
+ class MaterialFeatures {
11
+
12
+ constructor() {
13
+
14
+ this._features = {};
15
+
16
+ }
17
+
18
+ isUsed( feature ) {
19
+
20
+ return feature in this._features;
21
+
22
+ }
23
+
24
+ setUsed( feature, used = true ) {
25
+
26
+ if ( used === false ) {
27
+
28
+ delete this._features[ feature ];
29
+
30
+ } else {
31
+
32
+ this._features[ feature ] = true;
33
+
34
+ }
35
+
36
+ }
37
+
38
+ reset() {
39
+
40
+ this._features = {};
41
+
42
+ }
43
+
44
+ }
45
+
46
+ export class MaterialsTexture extends DataTexture {
47
+
48
+ constructor() {
49
+
50
+ super( new Float32Array( 4 ), 1, 1 );
51
+
52
+ this.format = RGBAFormat;
53
+ this.type = FloatType;
54
+ this.wrapS = ClampToEdgeWrapping;
55
+ this.wrapT = ClampToEdgeWrapping;
56
+ this.generateMipmaps = false;
57
+ this.threeCompatibilityTransforms = false;
58
+ this.features = new MaterialFeatures();
59
+
60
+ }
61
+
62
+ setCastShadow( materialIndex, cast ) {
63
+
64
+ // invert the shadow value so we default to "true" when initializing a material
65
+ const array = this.image.data;
66
+ const index = materialIndex * MATERIAL_STRIDE + SHADOW_OFFSET;
67
+ array[ index ] = ! cast ? 1 : 0;
68
+
69
+ }
70
+
71
+ getCastShadow( materialIndex ) {
72
+
73
+ const array = this.image.data;
74
+ const index = materialIndex * MATERIAL_STRIDE + SHADOW_OFFSET;
75
+ return ! Boolean( array[ index ] );
76
+
77
+ }
78
+
79
+ setMatte( materialIndex, matte ) {
80
+
81
+ const array = this.image.data;
82
+ const index = materialIndex * MATERIAL_STRIDE + MATTE_OFFSET;
83
+ array[ index ] = matte ? 1 : 0;
84
+
85
+ }
86
+
87
+ getMatte( materialIndex ) {
88
+
89
+ const array = this.image.data;
90
+ const index = materialIndex * MATERIAL_STRIDE + MATTE_OFFSET;
91
+ return Boolean( array[ index ] );
92
+
93
+ }
94
+
95
+ updateFrom( materials, textures ) {
96
+
97
+ function getTexture( material, key, def = - 1 ) {
98
+
99
+ if ( key in material && material[ key ] ) {
100
+
101
+ const hash = getTextureHash( material[ key ] );
102
+ return uniqueTextureLookup[ hash ];
103
+
104
+ } else {
105
+
106
+ return def;
107
+
108
+ }
109
+
110
+ }
111
+
112
+ function getField( material, key, def ) {
113
+
114
+ return key in material ? material[ key ] : def;
115
+
116
+ }
117
+
118
+ function getUVTransformTexture( material ) {
119
+
120
+ // https://github.com/mrdoob/three.js/blob/f3a832e637c98a404c64dae8174625958455e038/src/renderers/webgl/WebGLMaterials.js#L204-L306
121
+ // https://threejs.org/docs/#api/en/textures/Texture.offset
122
+ // fallback order of textures to use as a common uv transform
123
+ return material.map ||
124
+ material.specularMap ||
125
+ material.displacementMap ||
126
+ material.normalMap ||
127
+ material.bumpMap ||
128
+ material.roughnessMap ||
129
+ material.metalnessMap ||
130
+ material.alphaMap ||
131
+ material.emissiveMap ||
132
+ material.clearcoatMap ||
133
+ material.clearcoatNormalMap ||
134
+ material.clearcoatRoughnessMap ||
135
+ material.iridescenceMap ||
136
+ material.iridescenceThicknessMap ||
137
+ material.specularIntensityMap ||
138
+ material.specularColorMap ||
139
+ material.transmissionMap ||
140
+ material.thicknessMap ||
141
+ material.sheenColorMap ||
142
+ material.sheenRoughnessMap ||
143
+ null;
144
+
145
+ }
146
+
147
+ function writeTextureMatrixToArray( material, textureKey, array, offset ) {
148
+
149
+ let texture;
150
+ if ( threeCompatibilityTransforms ) {
151
+
152
+ texture = getUVTransformTexture( material );
153
+
154
+ } else {
155
+
156
+ texture = material[ textureKey ] && material[ textureKey ].isTexture ? material[ textureKey ] : null;
157
+
158
+ }
159
+
160
+ // check if texture exists
161
+ if ( texture ) {
162
+
163
+ const elements = texture.matrix.elements;
164
+
165
+ let i = 0;
166
+
167
+ // first row
168
+ array[ offset + i ++ ] = elements[ 0 ];
169
+ array[ offset + i ++ ] = elements[ 3 ];
170
+ array[ offset + i ++ ] = elements[ 6 ];
171
+ i ++;
172
+
173
+ // second row
174
+ array[ offset + i ++ ] = elements[ 1 ];
175
+ array[ offset + i ++ ] = elements[ 4 ];
176
+ array[ offset + i ++ ] = elements[ 7 ];
177
+ i ++;
178
+
179
+ }
180
+
181
+ return 8;
182
+
183
+ }
184
+
185
+ let index = 0;
186
+ const pixelCount = materials.length * MATERIAL_PIXELS;
187
+ const dimension = Math.ceil( Math.sqrt( pixelCount ) );
188
+ const { threeCompatibilityTransforms, image, features } = this;
189
+
190
+ // get the list of textures with unique sources
191
+ const uniqueTextures = reduceTexturesToUniqueSources( textures );
192
+ const uniqueTextureLookup = {};
193
+ for ( let i = 0, l = uniqueTextures.length; i < l; i ++ ) {
194
+
195
+ uniqueTextureLookup[ getTextureHash( uniqueTextures[ i ] ) ] = i;
196
+
197
+ }
198
+
199
+ if ( image.width !== dimension ) {
200
+
201
+ this.dispose();
202
+
203
+ image.data = new Float32Array( dimension * dimension * 4 );
204
+ image.width = dimension;
205
+ image.height = dimension;
206
+
207
+ }
208
+
209
+ const floatArray = image.data;
210
+
211
+ // on some devices (Google Pixel 6) the "floatBitsToInt" function does not work correctly so we
212
+ // can't encode texture ids that way.
213
+ // const intArray = new Int32Array( floatArray.buffer );
214
+
215
+ features.reset();
216
+ for ( let i = 0, l = materials.length; i < l; i ++ ) {
217
+
218
+ const m = materials[ i ];
219
+
220
+ if ( m.isFogVolumeMaterial ) {
221
+
222
+ features.setUsed( 'FOG' );
223
+
224
+ for ( let j = 0; j < MATERIAL_STRIDE; j ++ ) {
225
+
226
+ floatArray[ index + j ] = 0;
227
+
228
+ }
229
+
230
+ // sample 0 .rgb
231
+ floatArray[ index + 0 * 4 + 0 ] = m.color.r;
232
+ floatArray[ index + 0 * 4 + 1 ] = m.color.g;
233
+ floatArray[ index + 0 * 4 + 2 ] = m.color.b;
234
+
235
+ // sample 2 .a
236
+ floatArray[ index + 2 * 4 + 3 ] = getField( m, 'emissiveIntensity', 0.0 );
237
+
238
+ // sample 3 .rgb
239
+ floatArray[ index + 3 * 4 + 0 ] = m.emissive.r;
240
+ floatArray[ index + 3 * 4 + 1 ] = m.emissive.g;
241
+ floatArray[ index + 3 * 4 + 2 ] = m.emissive.b;
242
+
243
+ // sample 13 .g
244
+ // reusing opacity field
245
+ floatArray[ index + 13 * 4 + 1 ] = m.density;
246
+
247
+ // side
248
+ floatArray[ index + 13 * 4 + 3 ] = 0.0;
249
+
250
+ // sample 14 .b
251
+ floatArray[ index + 14 * 4 + 2 ] = 1 << 2;
252
+
253
+ index += MATERIAL_STRIDE;
254
+ continue;
255
+
256
+ }
257
+
258
+ // sample 0
259
+ // color
260
+ floatArray[ index ++ ] = m.color.r;
261
+ floatArray[ index ++ ] = m.color.g;
262
+ floatArray[ index ++ ] = m.color.b;
263
+ floatArray[ index ++ ] = getTexture( m, 'map' );
264
+
265
+ // sample 1
266
+ // metalness & roughness
267
+ floatArray[ index ++ ] = getField( m, 'metalness', 0.0 );
268
+ floatArray[ index ++ ] = getTexture( m, 'metalnessMap' );
269
+ floatArray[ index ++ ] = getField( m, 'roughness', 0.0 );
270
+ floatArray[ index ++ ] = getTexture( m, 'roughnessMap' );
271
+
272
+ // sample 2
273
+ // transmission & emissiveIntensity
274
+ // three.js assumes a default f0 of 0.04 if no ior is provided which equates to an ior of 1.5
275
+ floatArray[ index ++ ] = getField( m, 'ior', 1.5 );
276
+ floatArray[ index ++ ] = getField( m, 'transmission', 0.0 );
277
+ floatArray[ index ++ ] = getTexture( m, 'transmissionMap' );
278
+ floatArray[ index ++ ] = getField( m, 'emissiveIntensity', 0.0 );
279
+
280
+ // sample 3
281
+ // emission
282
+ if ( 'emissive' in m ) {
283
+
284
+ floatArray[ index ++ ] = m.emissive.r;
285
+ floatArray[ index ++ ] = m.emissive.g;
286
+ floatArray[ index ++ ] = m.emissive.b;
287
+
288
+ } else {
289
+
290
+ floatArray[ index ++ ] = 0.0;
291
+ floatArray[ index ++ ] = 0.0;
292
+ floatArray[ index ++ ] = 0.0;
293
+
294
+ }
295
+
296
+ floatArray[ index ++ ] = getTexture( m, 'emissiveMap' );
297
+
298
+ // sample 4
299
+ // normals
300
+ floatArray[ index ++ ] = getTexture( m, 'normalMap' );
301
+ if ( 'normalScale' in m ) {
302
+
303
+ floatArray[ index ++ ] = m.normalScale.x;
304
+ floatArray[ index ++ ] = m.normalScale.y;
305
+
306
+ } else {
307
+
308
+ floatArray[ index ++ ] = 1;
309
+ floatArray[ index ++ ] = 1;
310
+
311
+ }
312
+
313
+ // clearcoat
314
+ floatArray[ index ++ ] = getField( m, 'clearcoat', 0.0 );
315
+ floatArray[ index ++ ] = getTexture( m, 'clearcoatMap' ); // sample 5
316
+
317
+ floatArray[ index ++ ] = getField( m, 'clearcoatRoughness', 0.0 );
318
+ floatArray[ index ++ ] = getTexture( m, 'clearcoatRoughnessMap' );
319
+
320
+ floatArray[ index ++ ] = getTexture( m, 'clearcoatNormalMap' );
321
+
322
+ // sample 6
323
+ if ( 'clearcoatNormalScale' in m ) {
324
+
325
+ floatArray[ index ++ ] = m.clearcoatNormalScale.x;
326
+ floatArray[ index ++ ] = m.clearcoatNormalScale.y;
327
+
328
+ } else {
329
+
330
+ floatArray[ index ++ ] = 1;
331
+ floatArray[ index ++ ] = 1;
332
+
333
+ }
334
+
335
+ index ++;
336
+ floatArray[ index ++ ] = getField( m, 'sheen', 0.0 );
337
+
338
+ // sample 7
339
+ // sheen
340
+ if ( 'sheenColor' in m ) {
341
+
342
+ floatArray[ index ++ ] = m.sheenColor.r;
343
+ floatArray[ index ++ ] = m.sheenColor.g;
344
+ floatArray[ index ++ ] = m.sheenColor.b;
345
+
346
+ } else {
347
+
348
+ floatArray[ index ++ ] = 0.0;
349
+ floatArray[ index ++ ] = 0.0;
350
+ floatArray[ index ++ ] = 0.0;
351
+
352
+ }
353
+
354
+ floatArray[ index ++ ] = getTexture( m, 'sheenColorMap' );
355
+
356
+ // sample 8
357
+ floatArray[ index ++ ] = getField( m, 'sheenRoughness', 0.0 );
358
+ floatArray[ index ++ ] = getTexture( m, 'sheenRoughnessMap' );
359
+
360
+ // iridescence
361
+ floatArray[ index ++ ] = getTexture( m, 'iridescenceMap' );
362
+ floatArray[ index ++ ] = getTexture( m, 'iridescenceThicknessMap' );
363
+
364
+ // sample 9
365
+ floatArray[ index ++ ] = getField( m, 'iridescence', 0.0 );
366
+ floatArray[ index ++ ] = getField( m, 'iridescenceIOR', 1.3 );
367
+
368
+ const iridescenceThicknessRange = getField( m, 'iridescenceThicknessRange', [ 100, 400 ] );
369
+ floatArray[ index ++ ] = iridescenceThicknessRange[ 0 ];
370
+ floatArray[ index ++ ] = iridescenceThicknessRange[ 1 ];
371
+
372
+ // sample 10
373
+ // specular color
374
+ if ( 'specularColor' in m ) {
375
+
376
+ floatArray[ index ++ ] = m.specularColor.r;
377
+ floatArray[ index ++ ] = m.specularColor.g;
378
+ floatArray[ index ++ ] = m.specularColor.b;
379
+
380
+ } else {
381
+
382
+ floatArray[ index ++ ] = 1.0;
383
+ floatArray[ index ++ ] = 1.0;
384
+ floatArray[ index ++ ] = 1.0;
385
+
386
+ }
387
+
388
+ floatArray[ index ++ ] = getTexture( m, 'specularColorMap' );
389
+
390
+ // sample 11
391
+ // specular intensity
392
+ floatArray[ index ++ ] = getField( m, 'specularIntensity', 1.0 );
393
+ floatArray[ index ++ ] = getTexture( m, 'specularIntensityMap' );
394
+
395
+ // isThinFilm
396
+ const isThinFilm = getField( m, 'thickness', 0.0 ) === 0.0 && getField( m, 'attenuationDistance', Infinity ) === Infinity;
397
+ floatArray[ index ++ ] = Number( isThinFilm );
398
+ index ++;
399
+
400
+ // sample 12
401
+ if ( 'attenuationColor' in m ) {
402
+
403
+ floatArray[ index ++ ] = m.attenuationColor.r;
404
+ floatArray[ index ++ ] = m.attenuationColor.g;
405
+ floatArray[ index ++ ] = m.attenuationColor.b;
406
+
407
+ } else {
408
+
409
+ floatArray[ index ++ ] = 1.0;
410
+ floatArray[ index ++ ] = 1.0;
411
+ floatArray[ index ++ ] = 1.0;
412
+
413
+ }
414
+
415
+ floatArray[ index ++ ] = getField( m, 'attenuationDistance', Infinity );
416
+
417
+ // sample 13
418
+ // alphaMap
419
+ floatArray[ index ++ ] = getTexture( m, 'alphaMap' );
420
+
421
+ // side & matte
422
+ floatArray[ index ++ ] = m.opacity;
423
+ floatArray[ index ++ ] = m.alphaTest;
424
+ if ( ! isThinFilm && m.transmission > 0.0 ) {
425
+
426
+ floatArray[ index ++ ] = 0;
427
+
428
+ } else {
429
+
430
+ switch ( m.side ) {
431
+
432
+ case FrontSide:
433
+ floatArray[ index ++ ] = 1;
434
+ break;
435
+ case BackSide:
436
+ floatArray[ index ++ ] = - 1;
437
+ break;
438
+ case DoubleSide:
439
+ floatArray[ index ++ ] = 0;
440
+ break;
441
+
442
+ }
443
+
444
+ }
445
+
446
+ // sample 14
447
+ index ++; // matte
448
+ index ++; // shadow
449
+ floatArray[ index ++ ] = Number( m.vertexColors ) | ( Number( m.flatShading ) << 1 ); // vertexColors & flatShading
450
+ floatArray[ index ++ ] = Number( m.transparent ); // transparent
451
+
452
+ // map transform 15
453
+ index += writeTextureMatrixToArray( m, 'map', floatArray, index );
454
+
455
+ // metalnessMap transform 17
456
+ index += writeTextureMatrixToArray( m, 'metalnessMap', floatArray, index );
457
+
458
+ // roughnessMap transform 19
459
+ index += writeTextureMatrixToArray( m, 'roughnessMap', floatArray, index );
460
+
461
+ // transmissionMap transform 21
462
+ index += writeTextureMatrixToArray( m, 'transmissionMap', floatArray, index );
463
+
464
+ // emissiveMap transform 22
465
+ index += writeTextureMatrixToArray( m, 'emissiveMap', floatArray, index );
466
+
467
+ // normalMap transform 25
468
+ index += writeTextureMatrixToArray( m, 'normalMap', floatArray, index );
469
+
470
+ // clearcoatMap transform 27
471
+ index += writeTextureMatrixToArray( m, 'clearcoatMap', floatArray, index );
472
+
473
+ // clearcoatNormalMap transform 29
474
+ index += writeTextureMatrixToArray( m, 'clearcoatNormalMap', floatArray, index );
475
+
476
+ // clearcoatRoughnessMap transform 31
477
+ index += writeTextureMatrixToArray( m, 'clearcoatRoughnessMap', floatArray, index );
478
+
479
+ // sheenColorMap transform 33
480
+ index += writeTextureMatrixToArray( m, 'sheenColorMap', floatArray, index );
481
+
482
+ // sheenRoughnessMap transform 35
483
+ index += writeTextureMatrixToArray( m, 'sheenRoughnessMap', floatArray, index );
484
+
485
+ // iridescenceMap transform 37
486
+ index += writeTextureMatrixToArray( m, 'iridescenceMap', floatArray, index );
487
+
488
+ // iridescenceThicknessMap transform 39
489
+ index += writeTextureMatrixToArray( m, 'iridescenceThicknessMap', floatArray, index );
490
+
491
+ // specularColorMap transform 41
492
+ index += writeTextureMatrixToArray( m, 'specularColorMap', floatArray, index );
493
+
494
+ // specularIntensityMap transform 43
495
+ index += writeTextureMatrixToArray( m, 'specularIntensityMap', floatArray, index );
496
+
497
+ }
498
+
499
+ this.needsUpdate = true;
500
+
501
+ }
502
+
503
+ }