three-gpu-pathtracer 0.0.7 → 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 (48) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +887 -815
  3. package/build/index.module.js +5796 -5451
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +5795 -5448
  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 -270
  11. package/src/core/PathTracingSceneGenerator.js +1 -1
  12. package/src/index.js +39 -35
  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 -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 +973 -970
  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 -100
  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 -542
  31. package/src/shader/shaderSheenFunctions.js +98 -98
  32. package/src/shader/shaderStructs.js +321 -321
  33. package/src/shader/shaderUtils.js +403 -364
  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 -426
  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/IESLoader.js +325 -325
  47. package/src/utils/UVUnwrapper.js +101 -101
  48. package/src/workers/PathTracingSceneWorker.js +42 -42
@@ -1,426 +1,420 @@
1
- import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, FrontSide, BackSide, DoubleSide } from 'three';
2
-
3
- const MATERIAL_PIXELS = 45;
4
- const MATERIAL_STRIDE = MATERIAL_PIXELS * 4;
5
-
6
- const SIDE_OFFSET = 13 * 4 + 3; // s12.a
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
- setSide( materialIndex, side ) {
43
-
44
- const array = this.image.data;
45
- const index = materialIndex * MATERIAL_STRIDE + SIDE_OFFSET;
46
- switch ( side ) {
47
-
48
- case FrontSide:
49
- array[ index ] = 1;
50
- break;
51
- case BackSide:
52
- array[ index ] = - 1;
53
- break;
54
- case DoubleSide:
55
- array[ index ] = 0;
56
- break;
57
-
58
- }
59
-
60
- }
61
-
62
- getSide( materialIndex ) {
63
-
64
- const array = this.image.data;
65
- const index = materialIndex * MATERIAL_STRIDE + SIDE_OFFSET;
66
- switch ( array[ index ] ) {
67
-
68
- case 0:
69
- return DoubleSide;
70
- case 1:
71
- return FrontSide;
72
- case - 1:
73
- return BackSide;
74
-
75
- }
76
-
77
- return 0;
78
-
79
- }
80
-
81
- setMatte( materialIndex, matte ) {
82
-
83
- const array = this.image.data;
84
- const index = materialIndex * MATERIAL_STRIDE + MATTE_OFFSET;
85
- array[ index ] = matte ? 1 : 0;
86
-
87
- }
88
-
89
- getMatte( materialIndex ) {
90
-
91
- const array = this.image.data;
92
- const index = materialIndex * MATERIAL_STRIDE + MATTE_OFFSET;
93
- return Boolean( array[ index ] );
94
-
95
- }
96
-
97
- updateFrom( materials, textures ) {
98
-
99
- function getTexture( material, key, def = - 1 ) {
100
-
101
- return key in material ? textures.indexOf( material[ key ] ) : def;
102
-
103
- }
104
-
105
- function getField( material, key, def ) {
106
-
107
- return key in material ? material[ key ] : def;
108
-
109
- }
110
-
111
- function getUVTransformTexture( material ) {
112
-
113
- // https://github.com/mrdoob/three.js/blob/f3a832e637c98a404c64dae8174625958455e038/src/renderers/webgl/WebGLMaterials.js#L204-L306
114
- // https://threejs.org/docs/#api/en/textures/Texture.offset
115
- // fallback order of textures to use as a common uv transform
116
- return material.map ||
117
- material.specularMap ||
118
- material.displacementMap ||
119
- material.normalMap ||
120
- material.bumpMap ||
121
- material.roughnessMap ||
122
- material.metalnessMap ||
123
- material.alphaMap ||
124
- material.emissiveMap ||
125
- material.clearcoatMap ||
126
- material.clearcoatNormalMap ||
127
- material.clearcoatRoughnessMap ||
128
- material.iridescenceMap ||
129
- material.iridescenceThicknessMap ||
130
- material.specularIntensityMap ||
131
- material.specularColorMap ||
132
- material.transmissionMap ||
133
- material.thicknessMap ||
134
- material.sheenColorMap ||
135
- material.sheenRoughnessMap ||
136
- null;
137
-
138
- }
139
-
140
- function writeTextureMatrixToArray( material, textureKey, array, offset ) {
141
-
142
- let texture;
143
- if ( threeCompatibilityTransforms ) {
144
-
145
- texture = getUVTransformTexture( material );
146
-
147
- } else {
148
-
149
- texture = material[ textureKey ] && material[ textureKey ].isTexture ? material[ textureKey ] : null;
150
-
151
- }
152
-
153
- // check if texture exists
154
- if ( texture ) {
155
-
156
- const elements = texture.matrix.elements;
157
-
158
- let i = 0;
159
-
160
- // first row
161
- array[ offset + i ++ ] = elements[ 0 ];
162
- array[ offset + i ++ ] = elements[ 3 ];
163
- array[ offset + i ++ ] = elements[ 6 ];
164
- i ++;
165
-
166
- // second row
167
- array[ offset + i ++ ] = elements[ 1 ];
168
- array[ offset + i ++ ] = elements[ 4 ];
169
- array[ offset + i ++ ] = elements[ 7 ];
170
- i ++;
171
-
172
- }
173
-
174
- return 8;
175
-
176
- }
177
-
178
- let index = 0;
179
- const pixelCount = materials.length * MATERIAL_PIXELS;
180
- const dimension = Math.ceil( Math.sqrt( pixelCount ) );
181
- const { threeCompatibilityTransforms, image } = this;
182
-
183
- if ( image.width !== dimension ) {
184
-
185
- this.dispose();
186
-
187
- image.data = new Float32Array( dimension * dimension * 4 );
188
- image.width = dimension;
189
- image.height = dimension;
190
-
191
- }
192
-
193
- const floatArray = image.data;
194
-
195
- // on some devices (Google Pixel 6) the "floatBitsToInt" function does not work correctly so we
196
- // can't encode texture ids that way.
197
- // const intArray = new Int32Array( floatArray.buffer );
198
-
199
- for ( let i = 0, l = materials.length; i < l; i ++ ) {
200
-
201
- const m = materials[ i ];
202
-
203
- // sample 0
204
- // color
205
- floatArray[ index ++ ] = m.color.r;
206
- floatArray[ index ++ ] = m.color.g;
207
- floatArray[ index ++ ] = m.color.b;
208
- floatArray[ index ++ ] = getTexture( m, 'map' );
209
-
210
- // sample 1
211
- // metalness & roughness
212
- floatArray[ index ++ ] = getField( m, 'metalness', 0.0 );
213
- floatArray[ index ++ ] = textures.indexOf( m.metalnessMap );
214
- floatArray[ index ++ ] = getField( m, 'roughness', 0.0 );
215
- floatArray[ index ++ ] = textures.indexOf( m.roughnessMap );
216
-
217
- // sample 2
218
- // transmission & emissiveIntensity
219
- // three.js assumes a default f0 of 0.04 if no ior is provided which equates to an ior of 1.5
220
- floatArray[ index ++ ] = getField( m, 'ior', 1.5 );
221
- floatArray[ index ++ ] = getField( m, 'transmission', 0.0 );
222
- floatArray[ index ++ ] = getTexture( m, 'transmissionMap' );
223
- floatArray[ index ++ ] = getField( m, 'emissiveIntensity', 0.0 );
224
-
225
- // sample 3
226
- // emission
227
- if ( 'emissive' in m ) {
228
-
229
- floatArray[ index ++ ] = m.emissive.r;
230
- floatArray[ index ++ ] = m.emissive.g;
231
- floatArray[ index ++ ] = m.emissive.b;
232
-
233
- } else {
234
-
235
- floatArray[ index ++ ] = 0.0;
236
- floatArray[ index ++ ] = 0.0;
237
- floatArray[ index ++ ] = 0.0;
238
-
239
- }
240
-
241
- floatArray[ index ++ ] = getTexture( m, 'emissiveMap' );
242
-
243
- // sample 4
244
- // normals
245
- floatArray[ index ++ ] = getTexture( m, 'normalMap' );
246
- if ( 'normalScale' in m ) {
247
-
248
- floatArray[ index ++ ] = m.normalScale.x;
249
- floatArray[ index ++ ] = m.normalScale.y;
250
-
251
- } else {
252
-
253
- floatArray[ index ++ ] = 1;
254
- floatArray[ index ++ ] = 1;
255
-
256
- }
257
-
258
- // clearcoat
259
- floatArray[ index ++ ] = getField( m, 'clearcoat', 0.0 );
260
- floatArray[ index ++ ] = getTexture( m, 'clearcoatMap' ); // sample 5
261
-
262
- floatArray[ index ++ ] = getField( m, 'clearcoatRoughness', 0.0 );
263
- floatArray[ index ++ ] = getTexture( m, 'clearcoatRoughnessMap' );
264
-
265
- floatArray[ index ++ ] = getTexture( m, 'clearcoatNormalMap' );
266
-
267
- // sample 6
268
- if ( 'clearcoatNormalScale' in m ) {
269
-
270
- floatArray[ index ++ ] = m.clearcoatNormalScale.x;
271
- floatArray[ index ++ ] = m.clearcoatNormalScale.y;
272
-
273
- } else {
274
-
275
- floatArray[ index ++ ] = 1;
276
- floatArray[ index ++ ] = 1;
277
-
278
- }
279
-
280
- index ++;
281
- index ++;
282
-
283
- // sample 7
284
- // sheen
285
- if ( 'sheenColor' in m ) {
286
-
287
- floatArray[ index ++ ] = m.sheenColor.r;
288
- floatArray[ index ++ ] = m.sheenColor.g;
289
- floatArray[ index ++ ] = m.sheenColor.b;
290
-
291
- } else {
292
-
293
- floatArray[ index ++ ] = 0.0;
294
- floatArray[ index ++ ] = 0.0;
295
- floatArray[ index ++ ] = 0.0;
296
-
297
- }
298
-
299
- floatArray[ index ++ ] = getTexture( m, 'sheenColorMap' );
300
-
301
- // sample 8
302
- floatArray[ index ++ ] = getField( m, 'sheenRoughness', 0.0 );
303
- floatArray[ index ++ ] = getTexture( m, 'sheenRoughnessMap' );
304
-
305
- // iridescence
306
- floatArray[ index ++ ] = getTexture( m, 'iridescenceMap' );
307
- floatArray[ index ++ ] = getTexture( m, 'iridescenceThicknessMap' );
308
-
309
- floatArray[ index ++ ] = getField( m, 'iridescence', 0.0 ); // sample 9
310
- floatArray[ index ++ ] = getField( m, 'iridescenceIOR', 1.3 );
311
-
312
- const iridescenceThicknessRange = getField( m, 'iridescenceThicknessRange', [ 100, 400 ] );
313
- floatArray[ index ++ ] = iridescenceThicknessRange[ 0 ];
314
- floatArray[ index ++ ] = iridescenceThicknessRange[ 1 ];
315
-
316
- // sample 10
317
- // specular color
318
- if ( 'specularColor' in m ) {
319
-
320
- floatArray[ index ++ ] = m.specularColor.r;
321
- floatArray[ index ++ ] = m.specularColor.g;
322
- floatArray[ index ++ ] = m.specularColor.b;
323
-
324
- } else {
325
-
326
- floatArray[ index ++ ] = 1.0;
327
- floatArray[ index ++ ] = 1.0;
328
- floatArray[ index ++ ] = 1.0;
329
-
330
- }
331
-
332
- floatArray[ index ++ ] = getTexture( m, 'specularColorMap' );
333
-
334
- // sample 11
335
- // specular intensity
336
- floatArray[ index ++ ] = getField( m, 'specularIntensity', 1.0 );
337
- floatArray[ index ++ ] = getTexture( m, 'specularIntensityMap' );
338
-
339
- // thickness
340
- floatArray[ index ++ ] = getField( m, 'thickness', 0.0 ) === 0.0 && getField( m, 'attenuationDistance', Infinity ) === Infinity;
341
- index ++;
342
-
343
- // sample 12
344
- if ( 'attenuationColor' in m ) {
345
-
346
- floatArray[ index ++ ] = m.attenuationColor.r;
347
- floatArray[ index ++ ] = m.attenuationColor.g;
348
- floatArray[ index ++ ] = m.attenuationColor.b;
349
-
350
- } else {
351
-
352
- floatArray[ index ++ ] = 1.0;
353
- floatArray[ index ++ ] = 1.0;
354
- floatArray[ index ++ ] = 1.0;
355
-
356
- }
357
-
358
- floatArray[ index ++ ] = getField( m, 'attenuationDistance', Infinity );
359
-
360
- // sample 13
361
- // alphaMap
362
- floatArray[ index ++ ] = getTexture( m, 'alphaMap' );
363
-
364
- // side & matte
365
- floatArray[ index ++ ] = m.opacity;
366
- floatArray[ index ++ ] = m.alphaTest;
367
- index ++; // side
368
-
369
- // sample 14
370
- index ++; // matte
371
- index ++; // shadow
372
- floatArray[ index ++ ] = Number( m.vertexColors ); // vertexColors
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 } 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 source = material[ key ].source;
65
+ return uniqueTextures.findIndex( tex => tex.source === source );
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
+
156
+ if ( image.width !== dimension ) {
157
+
158
+ this.dispose();
159
+
160
+ image.data = new Float32Array( dimension * dimension * 4 );
161
+ image.width = dimension;
162
+ image.height = dimension;
163
+
164
+ }
165
+
166
+ const floatArray = image.data;
167
+
168
+ // on some devices (Google Pixel 6) the "floatBitsToInt" function does not work correctly so we
169
+ // can't encode texture ids that way.
170
+ // const intArray = new Int32Array( floatArray.buffer );
171
+
172
+ for ( let i = 0, l = materials.length; i < l; i ++ ) {
173
+
174
+ const m = materials[ i ];
175
+
176
+ // sample 0
177
+ // color
178
+ floatArray[ index ++ ] = m.color.r;
179
+ floatArray[ index ++ ] = m.color.g;
180
+ floatArray[ index ++ ] = m.color.b;
181
+ floatArray[ index ++ ] = getTexture( m, 'map' );
182
+
183
+ // sample 1
184
+ // metalness & roughness
185
+ floatArray[ index ++ ] = getField( m, 'metalness', 0.0 );
186
+ floatArray[ index ++ ] = uniqueTextures.indexOf( m.metalnessMap );
187
+ floatArray[ index ++ ] = getField( m, 'roughness', 0.0 );
188
+ floatArray[ index ++ ] = uniqueTextures.indexOf( m.roughnessMap );
189
+
190
+ // sample 2
191
+ // transmission & emissiveIntensity
192
+ // three.js assumes a default f0 of 0.04 if no ior is provided which equates to an ior of 1.5
193
+ floatArray[ index ++ ] = getField( m, 'ior', 1.5 );
194
+ floatArray[ index ++ ] = getField( m, 'transmission', 0.0 );
195
+ floatArray[ index ++ ] = getTexture( m, 'transmissionMap' );
196
+ floatArray[ index ++ ] = getField( m, 'emissiveIntensity', 0.0 );
197
+
198
+ // sample 3
199
+ // emission
200
+ if ( 'emissive' in m ) {
201
+
202
+ floatArray[ index ++ ] = m.emissive.r;
203
+ floatArray[ index ++ ] = m.emissive.g;
204
+ floatArray[ index ++ ] = m.emissive.b;
205
+
206
+ } else {
207
+
208
+ floatArray[ index ++ ] = 0.0;
209
+ floatArray[ index ++ ] = 0.0;
210
+ floatArray[ index ++ ] = 0.0;
211
+
212
+ }
213
+
214
+ floatArray[ index ++ ] = getTexture( m, 'emissiveMap' );
215
+
216
+ // sample 4
217
+ // normals
218
+ floatArray[ index ++ ] = getTexture( m, 'normalMap' );
219
+ if ( 'normalScale' in m ) {
220
+
221
+ floatArray[ index ++ ] = m.normalScale.x;
222
+ floatArray[ index ++ ] = m.normalScale.y;
223
+
224
+ } else {
225
+
226
+ floatArray[ index ++ ] = 1;
227
+ floatArray[ index ++ ] = 1;
228
+
229
+ }
230
+
231
+ // clearcoat
232
+ floatArray[ index ++ ] = getField( m, 'clearcoat', 0.0 );
233
+ floatArray[ index ++ ] = getTexture( m, 'clearcoatMap' ); // sample 5
234
+
235
+ floatArray[ index ++ ] = getField( m, 'clearcoatRoughness', 0.0 );
236
+ floatArray[ index ++ ] = getTexture( m, 'clearcoatRoughnessMap' );
237
+
238
+ floatArray[ index ++ ] = getTexture( m, 'clearcoatNormalMap' );
239
+
240
+ // sample 6
241
+ if ( 'clearcoatNormalScale' in m ) {
242
+
243
+ floatArray[ index ++ ] = m.clearcoatNormalScale.x;
244
+ floatArray[ index ++ ] = m.clearcoatNormalScale.y;
245
+
246
+ } else {
247
+
248
+ floatArray[ index ++ ] = 1;
249
+ floatArray[ index ++ ] = 1;
250
+
251
+ }
252
+
253
+ index ++;
254
+ index ++;
255
+
256
+ // sample 7
257
+ // sheen
258
+ if ( 'sheenColor' in m ) {
259
+
260
+ floatArray[ index ++ ] = m.sheenColor.r;
261
+ floatArray[ index ++ ] = m.sheenColor.g;
262
+ floatArray[ index ++ ] = m.sheenColor.b;
263
+
264
+ } else {
265
+
266
+ floatArray[ index ++ ] = 0.0;
267
+ floatArray[ index ++ ] = 0.0;
268
+ floatArray[ index ++ ] = 0.0;
269
+
270
+ }
271
+
272
+ floatArray[ index ++ ] = getTexture( m, 'sheenColorMap' );
273
+
274
+ // sample 8
275
+ floatArray[ index ++ ] = getField( m, 'sheenRoughness', 0.0 );
276
+ floatArray[ index ++ ] = getTexture( m, 'sheenRoughnessMap' );
277
+
278
+ // iridescence
279
+ floatArray[ index ++ ] = getTexture( m, 'iridescenceMap' );
280
+ floatArray[ index ++ ] = getTexture( m, 'iridescenceThicknessMap' );
281
+
282
+ floatArray[ index ++ ] = getField( m, 'iridescence', 0.0 ); // sample 9
283
+ floatArray[ index ++ ] = getField( m, 'iridescenceIOR', 1.3 );
284
+
285
+ const iridescenceThicknessRange = getField( m, 'iridescenceThicknessRange', [ 100, 400 ] );
286
+ floatArray[ index ++ ] = iridescenceThicknessRange[ 0 ];
287
+ floatArray[ index ++ ] = iridescenceThicknessRange[ 1 ];
288
+
289
+ // sample 10
290
+ // specular color
291
+ if ( 'specularColor' in m ) {
292
+
293
+ floatArray[ index ++ ] = m.specularColor.r;
294
+ floatArray[ index ++ ] = m.specularColor.g;
295
+ floatArray[ index ++ ] = m.specularColor.b;
296
+
297
+ } else {
298
+
299
+ floatArray[ index ++ ] = 1.0;
300
+ floatArray[ index ++ ] = 1.0;
301
+ floatArray[ index ++ ] = 1.0;
302
+
303
+ }
304
+
305
+ floatArray[ index ++ ] = getTexture( m, 'specularColorMap' );
306
+
307
+ // sample 11
308
+ // specular intensity
309
+ floatArray[ index ++ ] = getField( m, 'specularIntensity', 1.0 );
310
+ floatArray[ index ++ ] = getTexture( m, 'specularIntensityMap' );
311
+
312
+ // isThinFilm
313
+ const isThinFilm = getField( m, 'thickness', 0.0 ) === 0.0 && getField( m, 'attenuationDistance', Infinity ) === Infinity;
314
+ floatArray[ index ++ ] = Number( isThinFilm );
315
+ index ++;
316
+
317
+ // sample 12
318
+ if ( 'attenuationColor' in m ) {
319
+
320
+ floatArray[ index ++ ] = m.attenuationColor.r;
321
+ floatArray[ index ++ ] = m.attenuationColor.g;
322
+ floatArray[ index ++ ] = m.attenuationColor.b;
323
+
324
+ } else {
325
+
326
+ floatArray[ index ++ ] = 1.0;
327
+ floatArray[ index ++ ] = 1.0;
328
+ floatArray[ index ++ ] = 1.0;
329
+
330
+ }
331
+
332
+ floatArray[ index ++ ] = getField( m, 'attenuationDistance', Infinity );
333
+
334
+ // sample 13
335
+ // alphaMap
336
+ floatArray[ index ++ ] = getTexture( m, 'alphaMap' );
337
+
338
+ // side & matte
339
+ floatArray[ index ++ ] = m.opacity;
340
+ floatArray[ index ++ ] = m.alphaTest;
341
+ if ( ! isThinFilm && m.transmission > 0.0 ) {
342
+
343
+ floatArray[ index ++ ] = 0;
344
+
345
+ } else {
346
+
347
+ switch ( m.side ) {
348
+
349
+ case FrontSide:
350
+ floatArray[ index ++ ] = 1;
351
+ break;
352
+ case BackSide:
353
+ floatArray[ index ++ ] = - 1;
354
+ break;
355
+ case DoubleSide:
356
+ floatArray[ index ++ ] = 0;
357
+ break;
358
+
359
+ }
360
+
361
+ }
362
+
363
+ // sample 14
364
+ index ++; // matte
365
+ index ++; // shadow
366
+ floatArray[ index ++ ] = Number( m.vertexColors ); // vertexColors
367
+ floatArray[ index ++ ] = Number( m.transparent ); // transparent
368
+
369
+ // map transform 15
370
+ index += writeTextureMatrixToArray( m, 'map', floatArray, index );
371
+
372
+ // metalnessMap transform 17
373
+ index += writeTextureMatrixToArray( m, 'metalnessMap', floatArray, index );
374
+
375
+ // roughnessMap transform 19
376
+ index += writeTextureMatrixToArray( m, 'roughnessMap', floatArray, index );
377
+
378
+ // transmissionMap transform 21
379
+ index += writeTextureMatrixToArray( m, 'transmissionMap', floatArray, index );
380
+
381
+ // emissiveMap transform 22
382
+ index += writeTextureMatrixToArray( m, 'emissiveMap', floatArray, index );
383
+
384
+ // normalMap transform 25
385
+ index += writeTextureMatrixToArray( m, 'normalMap', floatArray, index );
386
+
387
+ // clearcoatMap transform 27
388
+ index += writeTextureMatrixToArray( m, 'clearcoatMap', floatArray, index );
389
+
390
+ // clearcoatNormalMap transform 29
391
+ index += writeTextureMatrixToArray( m, 'clearcoatNormalMap', floatArray, index );
392
+
393
+ // clearcoatRoughnessMap transform 31
394
+ index += writeTextureMatrixToArray( m, 'clearcoatRoughnessMap', floatArray, index );
395
+
396
+ // sheenColorMap transform 33
397
+ index += writeTextureMatrixToArray( m, 'sheenColorMap', floatArray, index );
398
+
399
+ // sheenRoughnessMap transform 35
400
+ index += writeTextureMatrixToArray( m, 'sheenRoughnessMap', floatArray, index );
401
+
402
+ // iridescenceMap transform 37
403
+ index += writeTextureMatrixToArray( m, 'iridescenceMap', floatArray, index );
404
+
405
+ // iridescenceThicknessMap transform 39
406
+ index += writeTextureMatrixToArray( m, 'iridescenceThicknessMap', floatArray, index );
407
+
408
+ // specularColorMap transform 41
409
+ index += writeTextureMatrixToArray( m, 'specularColorMap', floatArray, index );
410
+
411
+ // specularIntensityMap transform 43
412
+ index += writeTextureMatrixToArray( m, 'specularIntensityMap', floatArray, index );
413
+
414
+ }
415
+
416
+ this.needsUpdate = true;
417
+
418
+ }
419
+
420
+ }