zero2geospatial3d-sdk 0.1.0__py3-none-any.whl

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 (89) hide show
  1. zero2geospatial3d/__init__.py +105 -0
  2. zero2geospatial3d/acoustics.py +284 -0
  3. zero2geospatial3d/classify.py +379 -0
  4. zero2geospatial3d/cli.py +367 -0
  5. zero2geospatial3d/comfort.py +687 -0
  6. zero2geospatial3d/data/epw_stations.json.gz +0 -0
  7. zero2geospatial3d/daylight.py +337 -0
  8. zero2geospatial3d/dispersion.py +249 -0
  9. zero2geospatial3d/epw.py +162 -0
  10. zero2geospatial3d/geo.py +138 -0
  11. zero2geospatial3d/grid.py +119 -0
  12. zero2geospatial3d/microclimate.py +388 -0
  13. zero2geospatial3d/morphology.py +283 -0
  14. zero2geospatial3d/nsga2.py +315 -0
  15. zero2geospatial3d/osm.py +450 -0
  16. zero2geospatial3d/osm_tags.py +180 -0
  17. zero2geospatial3d/py.typed +0 -0
  18. zero2geospatial3d/results.py +231 -0
  19. zero2geospatial3d/road_sources.py +141 -0
  20. zero2geospatial3d/scene.py +555 -0
  21. zero2geospatial3d/server.py +332 -0
  22. zero2geospatial3d/shading.py +443 -0
  23. zero2geospatial3d/solar.py +207 -0
  24. zero2geospatial3d/terrain.py +610 -0
  25. zero2geospatial3d/urban_form.py +326 -0
  26. zero2geospatial3d/walkability.py +380 -0
  27. zero2geospatial3d/weather.py +373 -0
  28. zero2geospatial3d/web/index.html +50 -0
  29. zero2geospatial3d/web/src/analysis/climate.js +278 -0
  30. zero2geospatial3d/web/src/analysis/compliance.js +345 -0
  31. zero2geospatial3d/web/src/analysis/gpu.js +431 -0
  32. zero2geospatial3d/web/src/analysis/optimize.js +208 -0
  33. zero2geospatial3d/web/src/analysis/runners.js +1644 -0
  34. zero2geospatial3d/web/src/analysis/terrain_results.js +145 -0
  35. zero2geospatial3d/web/src/app.js +630 -0
  36. zero2geospatial3d/web/src/core/acoustics.js +341 -0
  37. zero2geospatial3d/web/src/core/classify.js +158 -0
  38. zero2geospatial3d/web/src/core/comfort.js +686 -0
  39. zero2geospatial3d/web/src/core/daylight_rule.js +199 -0
  40. zero2geospatial3d/web/src/core/dispersion.js +264 -0
  41. zero2geospatial3d/web/src/core/microclimate.js +372 -0
  42. zero2geospatial3d/web/src/core/morphology.js +153 -0
  43. zero2geospatial3d/web/src/core/nsga2.js +181 -0
  44. zero2geospatial3d/web/src/core/party_walls.js +60 -0
  45. zero2geospatial3d/web/src/core/road_sources.js +127 -0
  46. zero2geospatial3d/web/src/core/solar.js +112 -0
  47. zero2geospatial3d/web/src/core/terrain_analysis.js +267 -0
  48. zero2geospatial3d/web/src/core/terrain_math.js +279 -0
  49. zero2geospatial3d/web/src/core/urban_form.js +405 -0
  50. zero2geospatial3d/web/src/core/walkability.js +316 -0
  51. zero2geospatial3d/web/src/core/weather.js +225 -0
  52. zero2geospatial3d/web/src/core/wind.js +131 -0
  53. zero2geospatial3d/web/src/export/check_pdf.js +74 -0
  54. zero2geospatial3d/web/src/export/exporters.js +556 -0
  55. zero2geospatial3d/web/src/export/pdf.js +149 -0
  56. zero2geospatial3d/web/src/export/snapshot.js +234 -0
  57. zero2geospatial3d/web/src/export/snapshot_scene.js +150 -0
  58. zero2geospatial3d/web/src/main.js +75 -0
  59. zero2geospatial3d/web/src/scene/objects.js +645 -0
  60. zero2geospatial3d/web/src/scene/overlay.js +145 -0
  61. zero2geospatial3d/web/src/scene/terrain.js +546 -0
  62. zero2geospatial3d/web/src/scene/viewer.js +275 -0
  63. zero2geospatial3d/web/src/scene/wind_streamlines.js +336 -0
  64. zero2geospatial3d/web/src/style.css +225 -0
  65. zero2geospatial3d/web/src/ui/building_report.js +404 -0
  66. zero2geospatial3d/web/src/ui/charts.js +441 -0
  67. zero2geospatial3d/web/src/ui/colorize.js +101 -0
  68. zero2geospatial3d/web/src/ui/compare.js +362 -0
  69. zero2geospatial3d/web/src/ui/dom.js +104 -0
  70. zero2geospatial3d/web/src/ui/panel_analyze.js +742 -0
  71. zero2geospatial3d/web/src/ui/panel_compare.js +294 -0
  72. zero2geospatial3d/web/src/ui/panel_layers.js +172 -0
  73. zero2geospatial3d/web/src/ui/panel_terrain.js +277 -0
  74. zero2geospatial3d/web/src/ui/qgis_link.js +127 -0
  75. zero2geospatial3d/web/src/ui/sunclock.js +45 -0
  76. zero2geospatial3d/web/src/ui/tools.js +197 -0
  77. zero2geospatial3d/web/src/ui/viewtools.js +173 -0
  78. zero2geospatial3d/web/vendor/three/addons/controls/OrbitControls.js +1417 -0
  79. zero2geospatial3d/web/vendor/three/addons/exporters/GLTFExporter.js +3309 -0
  80. zero2geospatial3d/web/vendor/three/addons/utils/TextureUtils.js +98 -0
  81. zero2geospatial3d/web/vendor/three/three.module.js +53044 -0
  82. zero2geospatial3d/wind.py +178 -0
  83. zero2geospatial3d_sdk-0.1.0.dist-info/METADATA +219 -0
  84. zero2geospatial3d_sdk-0.1.0.dist-info/RECORD +89 -0
  85. zero2geospatial3d_sdk-0.1.0.dist-info/WHEEL +5 -0
  86. zero2geospatial3d_sdk-0.1.0.dist-info/entry_points.txt +2 -0
  87. zero2geospatial3d_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
  88. zero2geospatial3d_sdk-0.1.0.dist-info/licenses/THIRD_PARTY_NOTICES.md +52 -0
  89. zero2geospatial3d_sdk-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,3309 @@
1
+ import {
2
+ BufferAttribute,
3
+ ClampToEdgeWrapping,
4
+ Color,
5
+ DoubleSide,
6
+ InterpolateDiscrete,
7
+ InterpolateLinear,
8
+ NoColorSpace,
9
+ LinearFilter,
10
+ LinearMipmapLinearFilter,
11
+ LinearMipmapNearestFilter,
12
+ MathUtils,
13
+ Matrix4,
14
+ MirroredRepeatWrapping,
15
+ NearestFilter,
16
+ NearestMipmapLinearFilter,
17
+ NearestMipmapNearestFilter,
18
+ PropertyBinding,
19
+ RGBAFormat,
20
+ RepeatWrapping,
21
+ Scene,
22
+ Source,
23
+ SRGBColorSpace,
24
+ CompressedTexture,
25
+ Vector3,
26
+ Quaternion,
27
+ } from 'three';
28
+ import { decompress } from './../utils/TextureUtils.js';
29
+
30
+
31
+ /**
32
+ * The KHR_mesh_quantization extension allows these extra attribute component types
33
+ *
34
+ * @see https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_mesh_quantization/README.md#extending-mesh-attributes
35
+ */
36
+ const KHR_mesh_quantization_ExtraAttrTypes = {
37
+ POSITION: [
38
+ 'byte',
39
+ 'byte normalized',
40
+ 'unsigned byte',
41
+ 'unsigned byte normalized',
42
+ 'short',
43
+ 'short normalized',
44
+ 'unsigned short',
45
+ 'unsigned short normalized',
46
+ ],
47
+ NORMAL: [
48
+ 'byte normalized',
49
+ 'short normalized',
50
+ ],
51
+ TANGENT: [
52
+ 'byte normalized',
53
+ 'short normalized',
54
+ ],
55
+ TEXCOORD: [
56
+ 'byte',
57
+ 'byte normalized',
58
+ 'unsigned byte',
59
+ 'short',
60
+ 'short normalized',
61
+ 'unsigned short',
62
+ ],
63
+ };
64
+
65
+
66
+ class GLTFExporter {
67
+
68
+ constructor() {
69
+
70
+ this.pluginCallbacks = [];
71
+
72
+ this.register( function ( writer ) {
73
+
74
+ return new GLTFLightExtension( writer );
75
+
76
+ } );
77
+
78
+ this.register( function ( writer ) {
79
+
80
+ return new GLTFMaterialsUnlitExtension( writer );
81
+
82
+ } );
83
+
84
+ this.register( function ( writer ) {
85
+
86
+ return new GLTFMaterialsTransmissionExtension( writer );
87
+
88
+ } );
89
+
90
+ this.register( function ( writer ) {
91
+
92
+ return new GLTFMaterialsVolumeExtension( writer );
93
+
94
+ } );
95
+
96
+ this.register( function ( writer ) {
97
+
98
+ return new GLTFMaterialsIorExtension( writer );
99
+
100
+ } );
101
+
102
+ this.register( function ( writer ) {
103
+
104
+ return new GLTFMaterialsSpecularExtension( writer );
105
+
106
+ } );
107
+
108
+ this.register( function ( writer ) {
109
+
110
+ return new GLTFMaterialsClearcoatExtension( writer );
111
+
112
+ } );
113
+
114
+ this.register( function ( writer ) {
115
+
116
+ return new GLTFMaterialsIridescenceExtension( writer );
117
+
118
+ } );
119
+
120
+ this.register( function ( writer ) {
121
+
122
+ return new GLTFMaterialsSheenExtension( writer );
123
+
124
+ } );
125
+
126
+ this.register( function ( writer ) {
127
+
128
+ return new GLTFMaterialsAnisotropyExtension( writer );
129
+
130
+ } );
131
+
132
+ this.register( function ( writer ) {
133
+
134
+ return new GLTFMaterialsEmissiveStrengthExtension( writer );
135
+
136
+ } );
137
+
138
+ this.register( function ( writer ) {
139
+
140
+ return new GLTFMaterialsBumpExtension( writer );
141
+
142
+ } );
143
+
144
+ this.register( function ( writer ) {
145
+
146
+ return new GLTFMeshGpuInstancing( writer );
147
+
148
+ } );
149
+
150
+ }
151
+
152
+ register( callback ) {
153
+
154
+ if ( this.pluginCallbacks.indexOf( callback ) === - 1 ) {
155
+
156
+ this.pluginCallbacks.push( callback );
157
+
158
+ }
159
+
160
+ return this;
161
+
162
+ }
163
+
164
+ unregister( callback ) {
165
+
166
+ if ( this.pluginCallbacks.indexOf( callback ) !== - 1 ) {
167
+
168
+ this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 );
169
+
170
+ }
171
+
172
+ return this;
173
+
174
+ }
175
+
176
+ /**
177
+ * Parse scenes and generate GLTF output
178
+ * @param {Scene or [THREE.Scenes]} input Scene or Array of THREE.Scenes
179
+ * @param {Function} onDone Callback on completed
180
+ * @param {Function} onError Callback on errors
181
+ * @param {Object} options options
182
+ */
183
+ parse( input, onDone, onError, options ) {
184
+
185
+ const writer = new GLTFWriter();
186
+ const plugins = [];
187
+
188
+ for ( let i = 0, il = this.pluginCallbacks.length; i < il; i ++ ) {
189
+
190
+ plugins.push( this.pluginCallbacks[ i ]( writer ) );
191
+
192
+ }
193
+
194
+ writer.setPlugins( plugins );
195
+ writer.write( input, onDone, options ).catch( onError );
196
+
197
+ }
198
+
199
+ parseAsync( input, options ) {
200
+
201
+ const scope = this;
202
+
203
+ return new Promise( function ( resolve, reject ) {
204
+
205
+ scope.parse( input, resolve, reject, options );
206
+
207
+ } );
208
+
209
+ }
210
+
211
+ }
212
+
213
+ //------------------------------------------------------------------------------
214
+ // Constants
215
+ //------------------------------------------------------------------------------
216
+
217
+ const WEBGL_CONSTANTS = {
218
+ POINTS: 0x0000,
219
+ LINES: 0x0001,
220
+ LINE_LOOP: 0x0002,
221
+ LINE_STRIP: 0x0003,
222
+ TRIANGLES: 0x0004,
223
+ TRIANGLE_STRIP: 0x0005,
224
+ TRIANGLE_FAN: 0x0006,
225
+
226
+ BYTE: 0x1400,
227
+ UNSIGNED_BYTE: 0x1401,
228
+ SHORT: 0x1402,
229
+ UNSIGNED_SHORT: 0x1403,
230
+ INT: 0x1404,
231
+ UNSIGNED_INT: 0x1405,
232
+ FLOAT: 0x1406,
233
+
234
+ ARRAY_BUFFER: 0x8892,
235
+ ELEMENT_ARRAY_BUFFER: 0x8893,
236
+
237
+ NEAREST: 0x2600,
238
+ LINEAR: 0x2601,
239
+ NEAREST_MIPMAP_NEAREST: 0x2700,
240
+ LINEAR_MIPMAP_NEAREST: 0x2701,
241
+ NEAREST_MIPMAP_LINEAR: 0x2702,
242
+ LINEAR_MIPMAP_LINEAR: 0x2703,
243
+
244
+ CLAMP_TO_EDGE: 33071,
245
+ MIRRORED_REPEAT: 33648,
246
+ REPEAT: 10497
247
+ };
248
+
249
+ const KHR_MESH_QUANTIZATION = 'KHR_mesh_quantization';
250
+
251
+ const THREE_TO_WEBGL = {};
252
+
253
+ THREE_TO_WEBGL[ NearestFilter ] = WEBGL_CONSTANTS.NEAREST;
254
+ THREE_TO_WEBGL[ NearestMipmapNearestFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_NEAREST;
255
+ THREE_TO_WEBGL[ NearestMipmapLinearFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_LINEAR;
256
+ THREE_TO_WEBGL[ LinearFilter ] = WEBGL_CONSTANTS.LINEAR;
257
+ THREE_TO_WEBGL[ LinearMipmapNearestFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_NEAREST;
258
+ THREE_TO_WEBGL[ LinearMipmapLinearFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_LINEAR;
259
+
260
+ THREE_TO_WEBGL[ ClampToEdgeWrapping ] = WEBGL_CONSTANTS.CLAMP_TO_EDGE;
261
+ THREE_TO_WEBGL[ RepeatWrapping ] = WEBGL_CONSTANTS.REPEAT;
262
+ THREE_TO_WEBGL[ MirroredRepeatWrapping ] = WEBGL_CONSTANTS.MIRRORED_REPEAT;
263
+
264
+ const PATH_PROPERTIES = {
265
+ scale: 'scale',
266
+ position: 'translation',
267
+ quaternion: 'rotation',
268
+ morphTargetInfluences: 'weights'
269
+ };
270
+
271
+ const DEFAULT_SPECULAR_COLOR = new Color();
272
+
273
+ // GLB constants
274
+ // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
275
+
276
+ const GLB_HEADER_BYTES = 12;
277
+ const GLB_HEADER_MAGIC = 0x46546C67;
278
+ const GLB_VERSION = 2;
279
+
280
+ const GLB_CHUNK_PREFIX_BYTES = 8;
281
+ const GLB_CHUNK_TYPE_JSON = 0x4E4F534A;
282
+ const GLB_CHUNK_TYPE_BIN = 0x004E4942;
283
+
284
+ //------------------------------------------------------------------------------
285
+ // Utility functions
286
+ //------------------------------------------------------------------------------
287
+
288
+ /**
289
+ * Compare two arrays
290
+ * @param {Array} array1 Array 1 to compare
291
+ * @param {Array} array2 Array 2 to compare
292
+ * @return {Boolean} Returns true if both arrays are equal
293
+ */
294
+ function equalArray( array1, array2 ) {
295
+
296
+ return ( array1.length === array2.length ) && array1.every( function ( element, index ) {
297
+
298
+ return element === array2[ index ];
299
+
300
+ } );
301
+
302
+ }
303
+
304
+ /**
305
+ * Converts a string to an ArrayBuffer.
306
+ * @param {string} text
307
+ * @return {ArrayBuffer}
308
+ */
309
+ function stringToArrayBuffer( text ) {
310
+
311
+ return new TextEncoder().encode( text ).buffer;
312
+
313
+ }
314
+
315
+ /**
316
+ * Is identity matrix
317
+ *
318
+ * @param {Matrix4} matrix
319
+ * @returns {Boolean} Returns true, if parameter is identity matrix
320
+ */
321
+ function isIdentityMatrix( matrix ) {
322
+
323
+ return equalArray( matrix.elements, [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] );
324
+
325
+ }
326
+
327
+ /**
328
+ * Get the min and max vectors from the given attribute
329
+ * @param {BufferAttribute} attribute Attribute to find the min/max in range from start to start + count
330
+ * @param {Integer} start
331
+ * @param {Integer} count
332
+ * @return {Object} Object containing the `min` and `max` values (As an array of attribute.itemSize components)
333
+ */
334
+ function getMinMax( attribute, start, count ) {
335
+
336
+ const output = {
337
+
338
+ min: new Array( attribute.itemSize ).fill( Number.POSITIVE_INFINITY ),
339
+ max: new Array( attribute.itemSize ).fill( Number.NEGATIVE_INFINITY )
340
+
341
+ };
342
+
343
+ for ( let i = start; i < start + count; i ++ ) {
344
+
345
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
346
+
347
+ let value;
348
+
349
+ if ( attribute.itemSize > 4 ) {
350
+
351
+ // no support for interleaved data for itemSize > 4
352
+
353
+ value = attribute.array[ i * attribute.itemSize + a ];
354
+
355
+ } else {
356
+
357
+ if ( a === 0 ) value = attribute.getX( i );
358
+ else if ( a === 1 ) value = attribute.getY( i );
359
+ else if ( a === 2 ) value = attribute.getZ( i );
360
+ else if ( a === 3 ) value = attribute.getW( i );
361
+
362
+ if ( attribute.normalized === true ) {
363
+
364
+ value = MathUtils.normalize( value, attribute.array );
365
+
366
+ }
367
+
368
+ }
369
+
370
+ output.min[ a ] = Math.min( output.min[ a ], value );
371
+ output.max[ a ] = Math.max( output.max[ a ], value );
372
+
373
+ }
374
+
375
+ }
376
+
377
+ return output;
378
+
379
+ }
380
+
381
+ /**
382
+ * Get the required size + padding for a buffer, rounded to the next 4-byte boundary.
383
+ * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment
384
+ *
385
+ * @param {Integer} bufferSize The size the original buffer.
386
+ * @returns {Integer} new buffer size with required padding.
387
+ *
388
+ */
389
+ function getPaddedBufferSize( bufferSize ) {
390
+
391
+ return Math.ceil( bufferSize / 4 ) * 4;
392
+
393
+ }
394
+
395
+ /**
396
+ * Returns a buffer aligned to 4-byte boundary.
397
+ *
398
+ * @param {ArrayBuffer} arrayBuffer Buffer to pad
399
+ * @param {Integer} paddingByte (Optional)
400
+ * @returns {ArrayBuffer} The same buffer if it's already aligned to 4-byte boundary or a new buffer
401
+ */
402
+ function getPaddedArrayBuffer( arrayBuffer, paddingByte = 0 ) {
403
+
404
+ const paddedLength = getPaddedBufferSize( arrayBuffer.byteLength );
405
+
406
+ if ( paddedLength !== arrayBuffer.byteLength ) {
407
+
408
+ const array = new Uint8Array( paddedLength );
409
+ array.set( new Uint8Array( arrayBuffer ) );
410
+
411
+ if ( paddingByte !== 0 ) {
412
+
413
+ for ( let i = arrayBuffer.byteLength; i < paddedLength; i ++ ) {
414
+
415
+ array[ i ] = paddingByte;
416
+
417
+ }
418
+
419
+ }
420
+
421
+ return array.buffer;
422
+
423
+ }
424
+
425
+ return arrayBuffer;
426
+
427
+ }
428
+
429
+ function getCanvas() {
430
+
431
+ if ( typeof document === 'undefined' && typeof OffscreenCanvas !== 'undefined' ) {
432
+
433
+ return new OffscreenCanvas( 1, 1 );
434
+
435
+ }
436
+
437
+ return document.createElement( 'canvas' );
438
+
439
+ }
440
+
441
+ function getToBlobPromise( canvas, mimeType ) {
442
+
443
+ if ( canvas.toBlob !== undefined ) {
444
+
445
+ return new Promise( ( resolve ) => canvas.toBlob( resolve, mimeType ) );
446
+
447
+ }
448
+
449
+ let quality;
450
+
451
+ // Blink's implementation of convertToBlob seems to default to a quality level of 100%
452
+ // Use the Blink default quality levels of toBlob instead so that file sizes are comparable.
453
+ if ( mimeType === 'image/jpeg' ) {
454
+
455
+ quality = 0.92;
456
+
457
+ } else if ( mimeType === 'image/webp' ) {
458
+
459
+ quality = 0.8;
460
+
461
+ }
462
+
463
+ return canvas.convertToBlob( {
464
+
465
+ type: mimeType,
466
+ quality: quality
467
+
468
+ } );
469
+
470
+ }
471
+
472
+ /**
473
+ * Writer
474
+ */
475
+ class GLTFWriter {
476
+
477
+ constructor() {
478
+
479
+ this.plugins = [];
480
+
481
+ this.options = {};
482
+ this.pending = [];
483
+ this.buffers = [];
484
+
485
+ this.byteOffset = 0;
486
+ this.buffers = [];
487
+ this.nodeMap = new Map();
488
+ this.skins = [];
489
+
490
+ this.extensionsUsed = {};
491
+ this.extensionsRequired = {};
492
+
493
+ this.uids = new Map();
494
+ this.uid = 0;
495
+
496
+ this.json = {
497
+ asset: {
498
+ version: '2.0',
499
+ generator: 'THREE.GLTFExporter'
500
+ }
501
+ };
502
+
503
+ this.cache = {
504
+ meshes: new Map(),
505
+ attributes: new Map(),
506
+ attributesNormalized: new Map(),
507
+ materials: new Map(),
508
+ textures: new Map(),
509
+ images: new Map()
510
+ };
511
+
512
+ }
513
+
514
+ setPlugins( plugins ) {
515
+
516
+ this.plugins = plugins;
517
+
518
+ }
519
+
520
+ /**
521
+ * Parse scenes and generate GLTF output
522
+ * @param {Scene or [THREE.Scenes]} input Scene or Array of THREE.Scenes
523
+ * @param {Function} onDone Callback on completed
524
+ * @param {Object} options options
525
+ */
526
+ async write( input, onDone, options = {} ) {
527
+
528
+ this.options = Object.assign( {
529
+ // default options
530
+ binary: false,
531
+ trs: false,
532
+ onlyVisible: true,
533
+ maxTextureSize: Infinity,
534
+ animations: [],
535
+ includeCustomExtensions: false
536
+ }, options );
537
+
538
+ if ( this.options.animations.length > 0 ) {
539
+
540
+ // Only TRS properties, and not matrices, may be targeted by animation.
541
+ this.options.trs = true;
542
+
543
+ }
544
+
545
+ this.processInput( input );
546
+
547
+ await Promise.all( this.pending );
548
+
549
+ const writer = this;
550
+ const buffers = writer.buffers;
551
+ const json = writer.json;
552
+ options = writer.options;
553
+
554
+ const extensionsUsed = writer.extensionsUsed;
555
+ const extensionsRequired = writer.extensionsRequired;
556
+
557
+ // Merge buffers.
558
+ const blob = new Blob( buffers, { type: 'application/octet-stream' } );
559
+
560
+ // Declare extensions.
561
+ const extensionsUsedList = Object.keys( extensionsUsed );
562
+ const extensionsRequiredList = Object.keys( extensionsRequired );
563
+
564
+ if ( extensionsUsedList.length > 0 ) json.extensionsUsed = extensionsUsedList;
565
+ if ( extensionsRequiredList.length > 0 ) json.extensionsRequired = extensionsRequiredList;
566
+
567
+ // Update bytelength of the single buffer.
568
+ if ( json.buffers && json.buffers.length > 0 ) json.buffers[ 0 ].byteLength = blob.size;
569
+
570
+ if ( options.binary === true ) {
571
+
572
+ // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
573
+
574
+ const reader = new FileReader();
575
+ reader.readAsArrayBuffer( blob );
576
+ reader.onloadend = function () {
577
+
578
+ // Binary chunk.
579
+ const binaryChunk = getPaddedArrayBuffer( reader.result );
580
+ const binaryChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
581
+ binaryChunkPrefix.setUint32( 0, binaryChunk.byteLength, true );
582
+ binaryChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_BIN, true );
583
+
584
+ // JSON chunk.
585
+ const jsonChunk = getPaddedArrayBuffer( stringToArrayBuffer( JSON.stringify( json ) ), 0x20 );
586
+ const jsonChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
587
+ jsonChunkPrefix.setUint32( 0, jsonChunk.byteLength, true );
588
+ jsonChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_JSON, true );
589
+
590
+ // GLB header.
591
+ const header = new ArrayBuffer( GLB_HEADER_BYTES );
592
+ const headerView = new DataView( header );
593
+ headerView.setUint32( 0, GLB_HEADER_MAGIC, true );
594
+ headerView.setUint32( 4, GLB_VERSION, true );
595
+ const totalByteLength = GLB_HEADER_BYTES
596
+ + jsonChunkPrefix.byteLength + jsonChunk.byteLength
597
+ + binaryChunkPrefix.byteLength + binaryChunk.byteLength;
598
+ headerView.setUint32( 8, totalByteLength, true );
599
+
600
+ const glbBlob = new Blob( [
601
+ header,
602
+ jsonChunkPrefix,
603
+ jsonChunk,
604
+ binaryChunkPrefix,
605
+ binaryChunk
606
+ ], { type: 'application/octet-stream' } );
607
+
608
+ const glbReader = new FileReader();
609
+ glbReader.readAsArrayBuffer( glbBlob );
610
+ glbReader.onloadend = function () {
611
+
612
+ onDone( glbReader.result );
613
+
614
+ };
615
+
616
+ };
617
+
618
+ } else {
619
+
620
+ if ( json.buffers && json.buffers.length > 0 ) {
621
+
622
+ const reader = new FileReader();
623
+ reader.readAsDataURL( blob );
624
+ reader.onloadend = function () {
625
+
626
+ const base64data = reader.result;
627
+ json.buffers[ 0 ].uri = base64data;
628
+ onDone( json );
629
+
630
+ };
631
+
632
+ } else {
633
+
634
+ onDone( json );
635
+
636
+ }
637
+
638
+ }
639
+
640
+
641
+ }
642
+
643
+ /**
644
+ * Serializes a userData.
645
+ *
646
+ * @param {THREE.Object3D|THREE.Material} object
647
+ * @param {Object} objectDef
648
+ */
649
+ serializeUserData( object, objectDef ) {
650
+
651
+ if ( Object.keys( object.userData ).length === 0 ) return;
652
+
653
+ const options = this.options;
654
+ const extensionsUsed = this.extensionsUsed;
655
+
656
+ try {
657
+
658
+ const json = JSON.parse( JSON.stringify( object.userData ) );
659
+
660
+ if ( options.includeCustomExtensions && json.gltfExtensions ) {
661
+
662
+ if ( objectDef.extensions === undefined ) objectDef.extensions = {};
663
+
664
+ for ( const extensionName in json.gltfExtensions ) {
665
+
666
+ objectDef.extensions[ extensionName ] = json.gltfExtensions[ extensionName ];
667
+ extensionsUsed[ extensionName ] = true;
668
+
669
+ }
670
+
671
+ delete json.gltfExtensions;
672
+
673
+ }
674
+
675
+ if ( Object.keys( json ).length > 0 ) objectDef.extras = json;
676
+
677
+ } catch ( error ) {
678
+
679
+ console.warn( 'THREE.GLTFExporter: userData of \'' + object.name + '\' ' +
680
+ 'won\'t be serialized because of JSON.stringify error - ' + error.message );
681
+
682
+ }
683
+
684
+ }
685
+
686
+ /**
687
+ * Returns ids for buffer attributes.
688
+ * @param {Object} object
689
+ * @return {Integer}
690
+ */
691
+ getUID( attribute, isRelativeCopy = false ) {
692
+
693
+ if ( this.uids.has( attribute ) === false ) {
694
+
695
+ const uids = new Map();
696
+
697
+ uids.set( true, this.uid ++ );
698
+ uids.set( false, this.uid ++ );
699
+
700
+ this.uids.set( attribute, uids );
701
+
702
+ }
703
+
704
+ const uids = this.uids.get( attribute );
705
+
706
+ return uids.get( isRelativeCopy );
707
+
708
+ }
709
+
710
+ /**
711
+ * Checks if normal attribute values are normalized.
712
+ *
713
+ * @param {BufferAttribute} normal
714
+ * @returns {Boolean}
715
+ */
716
+ isNormalizedNormalAttribute( normal ) {
717
+
718
+ const cache = this.cache;
719
+
720
+ if ( cache.attributesNormalized.has( normal ) ) return false;
721
+
722
+ const v = new Vector3();
723
+
724
+ for ( let i = 0, il = normal.count; i < il; i ++ ) {
725
+
726
+ // 0.0005 is from glTF-validator
727
+ if ( Math.abs( v.fromBufferAttribute( normal, i ).length() - 1.0 ) > 0.0005 ) return false;
728
+
729
+ }
730
+
731
+ return true;
732
+
733
+ }
734
+
735
+ /**
736
+ * Creates normalized normal buffer attribute.
737
+ *
738
+ * @param {BufferAttribute} normal
739
+ * @returns {BufferAttribute}
740
+ *
741
+ */
742
+ createNormalizedNormalAttribute( normal ) {
743
+
744
+ const cache = this.cache;
745
+
746
+ if ( cache.attributesNormalized.has( normal ) ) return cache.attributesNormalized.get( normal );
747
+
748
+ const attribute = normal.clone();
749
+ const v = new Vector3();
750
+
751
+ for ( let i = 0, il = attribute.count; i < il; i ++ ) {
752
+
753
+ v.fromBufferAttribute( attribute, i );
754
+
755
+ if ( v.x === 0 && v.y === 0 && v.z === 0 ) {
756
+
757
+ // if values can't be normalized set (1, 0, 0)
758
+ v.setX( 1.0 );
759
+
760
+ } else {
761
+
762
+ v.normalize();
763
+
764
+ }
765
+
766
+ attribute.setXYZ( i, v.x, v.y, v.z );
767
+
768
+ }
769
+
770
+ cache.attributesNormalized.set( normal, attribute );
771
+
772
+ return attribute;
773
+
774
+ }
775
+
776
+ /**
777
+ * Applies a texture transform, if present, to the map definition. Requires
778
+ * the KHR_texture_transform extension.
779
+ *
780
+ * @param {Object} mapDef
781
+ * @param {THREE.Texture} texture
782
+ */
783
+ applyTextureTransform( mapDef, texture ) {
784
+
785
+ let didTransform = false;
786
+ const transformDef = {};
787
+
788
+ if ( texture.offset.x !== 0 || texture.offset.y !== 0 ) {
789
+
790
+ transformDef.offset = texture.offset.toArray();
791
+ didTransform = true;
792
+
793
+ }
794
+
795
+ if ( texture.rotation !== 0 ) {
796
+
797
+ transformDef.rotation = texture.rotation;
798
+ didTransform = true;
799
+
800
+ }
801
+
802
+ if ( texture.repeat.x !== 1 || texture.repeat.y !== 1 ) {
803
+
804
+ transformDef.scale = texture.repeat.toArray();
805
+ didTransform = true;
806
+
807
+ }
808
+
809
+ if ( didTransform ) {
810
+
811
+ mapDef.extensions = mapDef.extensions || {};
812
+ mapDef.extensions[ 'KHR_texture_transform' ] = transformDef;
813
+ this.extensionsUsed[ 'KHR_texture_transform' ] = true;
814
+
815
+ }
816
+
817
+ }
818
+
819
+ buildMetalRoughTexture( metalnessMap, roughnessMap ) {
820
+
821
+ if ( metalnessMap === roughnessMap ) return metalnessMap;
822
+
823
+ function getEncodingConversion( map ) {
824
+
825
+ if ( map.colorSpace === SRGBColorSpace ) {
826
+
827
+ return function SRGBToLinear( c ) {
828
+
829
+ return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 );
830
+
831
+ };
832
+
833
+ }
834
+
835
+ return function LinearToLinear( c ) {
836
+
837
+ return c;
838
+
839
+ };
840
+
841
+ }
842
+
843
+ console.warn( 'THREE.GLTFExporter: Merged metalnessMap and roughnessMap textures.' );
844
+
845
+ if ( metalnessMap instanceof CompressedTexture ) {
846
+
847
+ metalnessMap = decompress( metalnessMap );
848
+
849
+ }
850
+
851
+ if ( roughnessMap instanceof CompressedTexture ) {
852
+
853
+ roughnessMap = decompress( roughnessMap );
854
+
855
+ }
856
+
857
+ const metalness = metalnessMap ? metalnessMap.image : null;
858
+ const roughness = roughnessMap ? roughnessMap.image : null;
859
+
860
+ const width = Math.max( metalness ? metalness.width : 0, roughness ? roughness.width : 0 );
861
+ const height = Math.max( metalness ? metalness.height : 0, roughness ? roughness.height : 0 );
862
+
863
+ const canvas = getCanvas();
864
+ canvas.width = width;
865
+ canvas.height = height;
866
+
867
+ const context = canvas.getContext( '2d' );
868
+ context.fillStyle = '#00ffff';
869
+ context.fillRect( 0, 0, width, height );
870
+
871
+ const composite = context.getImageData( 0, 0, width, height );
872
+
873
+ if ( metalness ) {
874
+
875
+ context.drawImage( metalness, 0, 0, width, height );
876
+
877
+ const convert = getEncodingConversion( metalnessMap );
878
+ const data = context.getImageData( 0, 0, width, height ).data;
879
+
880
+ for ( let i = 2; i < data.length; i += 4 ) {
881
+
882
+ composite.data[ i ] = convert( data[ i ] / 256 ) * 256;
883
+
884
+ }
885
+
886
+ }
887
+
888
+ if ( roughness ) {
889
+
890
+ context.drawImage( roughness, 0, 0, width, height );
891
+
892
+ const convert = getEncodingConversion( roughnessMap );
893
+ const data = context.getImageData( 0, 0, width, height ).data;
894
+
895
+ for ( let i = 1; i < data.length; i += 4 ) {
896
+
897
+ composite.data[ i ] = convert( data[ i ] / 256 ) * 256;
898
+
899
+ }
900
+
901
+ }
902
+
903
+ context.putImageData( composite, 0, 0 );
904
+
905
+ //
906
+
907
+ const reference = metalnessMap || roughnessMap;
908
+
909
+ const texture = reference.clone();
910
+
911
+ texture.source = new Source( canvas );
912
+ texture.colorSpace = NoColorSpace;
913
+ texture.channel = ( metalnessMap || roughnessMap ).channel;
914
+
915
+ if ( metalnessMap && roughnessMap && metalnessMap.channel !== roughnessMap.channel ) {
916
+
917
+ console.warn( 'THREE.GLTFExporter: UV channels for metalnessMap and roughnessMap textures must match.' );
918
+
919
+ }
920
+
921
+ return texture;
922
+
923
+ }
924
+
925
+ /**
926
+ * Process a buffer to append to the default one.
927
+ * @param {ArrayBuffer} buffer
928
+ * @return {Integer}
929
+ */
930
+ processBuffer( buffer ) {
931
+
932
+ const json = this.json;
933
+ const buffers = this.buffers;
934
+
935
+ if ( ! json.buffers ) json.buffers = [ { byteLength: 0 } ];
936
+
937
+ // All buffers are merged before export.
938
+ buffers.push( buffer );
939
+
940
+ return 0;
941
+
942
+ }
943
+
944
+ /**
945
+ * Process and generate a BufferView
946
+ * @param {BufferAttribute} attribute
947
+ * @param {number} componentType
948
+ * @param {number} start
949
+ * @param {number} count
950
+ * @param {number} target (Optional) Target usage of the BufferView
951
+ * @return {Object}
952
+ */
953
+ processBufferView( attribute, componentType, start, count, target ) {
954
+
955
+ const json = this.json;
956
+
957
+ if ( ! json.bufferViews ) json.bufferViews = [];
958
+
959
+ // Create a new dataview and dump the attribute's array into it
960
+
961
+ let componentSize;
962
+
963
+ switch ( componentType ) {
964
+
965
+ case WEBGL_CONSTANTS.BYTE:
966
+ case WEBGL_CONSTANTS.UNSIGNED_BYTE:
967
+
968
+ componentSize = 1;
969
+
970
+ break;
971
+
972
+ case WEBGL_CONSTANTS.SHORT:
973
+ case WEBGL_CONSTANTS.UNSIGNED_SHORT:
974
+
975
+ componentSize = 2;
976
+
977
+ break;
978
+
979
+ default:
980
+
981
+ componentSize = 4;
982
+
983
+ }
984
+
985
+ const byteLength = getPaddedBufferSize( count * attribute.itemSize * componentSize );
986
+ const dataView = new DataView( new ArrayBuffer( byteLength ) );
987
+ let offset = 0;
988
+
989
+ for ( let i = start; i < start + count; i ++ ) {
990
+
991
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
992
+
993
+ let value;
994
+
995
+ if ( attribute.itemSize > 4 ) {
996
+
997
+ // no support for interleaved data for itemSize > 4
998
+
999
+ value = attribute.array[ i * attribute.itemSize + a ];
1000
+
1001
+ } else {
1002
+
1003
+ if ( a === 0 ) value = attribute.getX( i );
1004
+ else if ( a === 1 ) value = attribute.getY( i );
1005
+ else if ( a === 2 ) value = attribute.getZ( i );
1006
+ else if ( a === 3 ) value = attribute.getW( i );
1007
+
1008
+ if ( attribute.normalized === true ) {
1009
+
1010
+ value = MathUtils.normalize( value, attribute.array );
1011
+
1012
+ }
1013
+
1014
+ }
1015
+
1016
+ if ( componentType === WEBGL_CONSTANTS.FLOAT ) {
1017
+
1018
+ dataView.setFloat32( offset, value, true );
1019
+
1020
+ } else if ( componentType === WEBGL_CONSTANTS.INT ) {
1021
+
1022
+ dataView.setInt32( offset, value, true );
1023
+
1024
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_INT ) {
1025
+
1026
+ dataView.setUint32( offset, value, true );
1027
+
1028
+ } else if ( componentType === WEBGL_CONSTANTS.SHORT ) {
1029
+
1030
+ dataView.setInt16( offset, value, true );
1031
+
1032
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
1033
+
1034
+ dataView.setUint16( offset, value, true );
1035
+
1036
+ } else if ( componentType === WEBGL_CONSTANTS.BYTE ) {
1037
+
1038
+ dataView.setInt8( offset, value );
1039
+
1040
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_BYTE ) {
1041
+
1042
+ dataView.setUint8( offset, value );
1043
+
1044
+ }
1045
+
1046
+ offset += componentSize;
1047
+
1048
+ }
1049
+
1050
+ }
1051
+
1052
+ const bufferViewDef = {
1053
+
1054
+ buffer: this.processBuffer( dataView.buffer ),
1055
+ byteOffset: this.byteOffset,
1056
+ byteLength: byteLength
1057
+
1058
+ };
1059
+
1060
+ if ( target !== undefined ) bufferViewDef.target = target;
1061
+
1062
+ if ( target === WEBGL_CONSTANTS.ARRAY_BUFFER ) {
1063
+
1064
+ // Only define byteStride for vertex attributes.
1065
+ bufferViewDef.byteStride = attribute.itemSize * componentSize;
1066
+
1067
+ }
1068
+
1069
+ this.byteOffset += byteLength;
1070
+
1071
+ json.bufferViews.push( bufferViewDef );
1072
+
1073
+ // @TODO Merge bufferViews where possible.
1074
+ const output = {
1075
+
1076
+ id: json.bufferViews.length - 1,
1077
+ byteLength: 0
1078
+
1079
+ };
1080
+
1081
+ return output;
1082
+
1083
+ }
1084
+
1085
+ /**
1086
+ * Process and generate a BufferView from an image Blob.
1087
+ * @param {Blob} blob
1088
+ * @return {Promise<Integer>}
1089
+ */
1090
+ processBufferViewImage( blob ) {
1091
+
1092
+ const writer = this;
1093
+ const json = writer.json;
1094
+
1095
+ if ( ! json.bufferViews ) json.bufferViews = [];
1096
+
1097
+ return new Promise( function ( resolve ) {
1098
+
1099
+ const reader = new FileReader();
1100
+ reader.readAsArrayBuffer( blob );
1101
+ reader.onloadend = function () {
1102
+
1103
+ const buffer = getPaddedArrayBuffer( reader.result );
1104
+
1105
+ const bufferViewDef = {
1106
+ buffer: writer.processBuffer( buffer ),
1107
+ byteOffset: writer.byteOffset,
1108
+ byteLength: buffer.byteLength
1109
+ };
1110
+
1111
+ writer.byteOffset += buffer.byteLength;
1112
+ resolve( json.bufferViews.push( bufferViewDef ) - 1 );
1113
+
1114
+ };
1115
+
1116
+ } );
1117
+
1118
+ }
1119
+
1120
+ /**
1121
+ * Process attribute to generate an accessor
1122
+ * @param {BufferAttribute} attribute Attribute to process
1123
+ * @param {THREE.BufferGeometry} geometry (Optional) Geometry used for truncated draw range
1124
+ * @param {Integer} start (Optional)
1125
+ * @param {Integer} count (Optional)
1126
+ * @return {Integer|null} Index of the processed accessor on the "accessors" array
1127
+ */
1128
+ processAccessor( attribute, geometry, start, count ) {
1129
+
1130
+ const json = this.json;
1131
+
1132
+ const types = {
1133
+
1134
+ 1: 'SCALAR',
1135
+ 2: 'VEC2',
1136
+ 3: 'VEC3',
1137
+ 4: 'VEC4',
1138
+ 9: 'MAT3',
1139
+ 16: 'MAT4'
1140
+
1141
+ };
1142
+
1143
+ let componentType;
1144
+
1145
+ // Detect the component type of the attribute array
1146
+ if ( attribute.array.constructor === Float32Array ) {
1147
+
1148
+ componentType = WEBGL_CONSTANTS.FLOAT;
1149
+
1150
+ } else if ( attribute.array.constructor === Int32Array ) {
1151
+
1152
+ componentType = WEBGL_CONSTANTS.INT;
1153
+
1154
+ } else if ( attribute.array.constructor === Uint32Array ) {
1155
+
1156
+ componentType = WEBGL_CONSTANTS.UNSIGNED_INT;
1157
+
1158
+ } else if ( attribute.array.constructor === Int16Array ) {
1159
+
1160
+ componentType = WEBGL_CONSTANTS.SHORT;
1161
+
1162
+ } else if ( attribute.array.constructor === Uint16Array ) {
1163
+
1164
+ componentType = WEBGL_CONSTANTS.UNSIGNED_SHORT;
1165
+
1166
+ } else if ( attribute.array.constructor === Int8Array ) {
1167
+
1168
+ componentType = WEBGL_CONSTANTS.BYTE;
1169
+
1170
+ } else if ( attribute.array.constructor === Uint8Array ) {
1171
+
1172
+ componentType = WEBGL_CONSTANTS.UNSIGNED_BYTE;
1173
+
1174
+ } else {
1175
+
1176
+ throw new Error( 'THREE.GLTFExporter: Unsupported bufferAttribute component type: ' + attribute.array.constructor.name );
1177
+
1178
+ }
1179
+
1180
+ if ( start === undefined ) start = 0;
1181
+ if ( count === undefined || count === Infinity ) count = attribute.count;
1182
+
1183
+ // Skip creating an accessor if the attribute doesn't have data to export
1184
+ if ( count === 0 ) return null;
1185
+
1186
+ const minMax = getMinMax( attribute, start, count );
1187
+ let bufferViewTarget;
1188
+
1189
+ // If geometry isn't provided, don't infer the target usage of the bufferView. For
1190
+ // animation samplers, target must not be set.
1191
+ if ( geometry !== undefined ) {
1192
+
1193
+ bufferViewTarget = attribute === geometry.index ? WEBGL_CONSTANTS.ELEMENT_ARRAY_BUFFER : WEBGL_CONSTANTS.ARRAY_BUFFER;
1194
+
1195
+ }
1196
+
1197
+ const bufferView = this.processBufferView( attribute, componentType, start, count, bufferViewTarget );
1198
+
1199
+ const accessorDef = {
1200
+
1201
+ bufferView: bufferView.id,
1202
+ byteOffset: bufferView.byteOffset,
1203
+ componentType: componentType,
1204
+ count: count,
1205
+ max: minMax.max,
1206
+ min: minMax.min,
1207
+ type: types[ attribute.itemSize ]
1208
+
1209
+ };
1210
+
1211
+ if ( attribute.normalized === true ) accessorDef.normalized = true;
1212
+ if ( ! json.accessors ) json.accessors = [];
1213
+
1214
+ return json.accessors.push( accessorDef ) - 1;
1215
+
1216
+ }
1217
+
1218
+ /**
1219
+ * Process image
1220
+ * @param {Image} image to process
1221
+ * @param {Integer} format of the image (RGBAFormat)
1222
+ * @param {Boolean} flipY before writing out the image
1223
+ * @param {String} mimeType export format
1224
+ * @return {Integer} Index of the processed texture in the "images" array
1225
+ */
1226
+ processImage( image, format, flipY, mimeType = 'image/png' ) {
1227
+
1228
+ if ( image !== null ) {
1229
+
1230
+ const writer = this;
1231
+ const cache = writer.cache;
1232
+ const json = writer.json;
1233
+ const options = writer.options;
1234
+ const pending = writer.pending;
1235
+
1236
+ if ( ! cache.images.has( image ) ) cache.images.set( image, {} );
1237
+
1238
+ const cachedImages = cache.images.get( image );
1239
+
1240
+ const key = mimeType + ':flipY/' + flipY.toString();
1241
+
1242
+ if ( cachedImages[ key ] !== undefined ) return cachedImages[ key ];
1243
+
1244
+ if ( ! json.images ) json.images = [];
1245
+
1246
+ const imageDef = { mimeType: mimeType };
1247
+
1248
+ const canvas = getCanvas();
1249
+
1250
+ canvas.width = Math.min( image.width, options.maxTextureSize );
1251
+ canvas.height = Math.min( image.height, options.maxTextureSize );
1252
+
1253
+ const ctx = canvas.getContext( '2d' );
1254
+
1255
+ if ( flipY === true ) {
1256
+
1257
+ ctx.translate( 0, canvas.height );
1258
+ ctx.scale( 1, - 1 );
1259
+
1260
+ }
1261
+
1262
+ if ( image.data !== undefined ) { // THREE.DataTexture
1263
+
1264
+ if ( format !== RGBAFormat ) {
1265
+
1266
+ console.error( 'GLTFExporter: Only RGBAFormat is supported.', format );
1267
+
1268
+ }
1269
+
1270
+ if ( image.width > options.maxTextureSize || image.height > options.maxTextureSize ) {
1271
+
1272
+ console.warn( 'GLTFExporter: Image size is bigger than maxTextureSize', image );
1273
+
1274
+ }
1275
+
1276
+ const data = new Uint8ClampedArray( image.height * image.width * 4 );
1277
+
1278
+ for ( let i = 0; i < data.length; i += 4 ) {
1279
+
1280
+ data[ i + 0 ] = image.data[ i + 0 ];
1281
+ data[ i + 1 ] = image.data[ i + 1 ];
1282
+ data[ i + 2 ] = image.data[ i + 2 ];
1283
+ data[ i + 3 ] = image.data[ i + 3 ];
1284
+
1285
+ }
1286
+
1287
+ ctx.putImageData( new ImageData( data, image.width, image.height ), 0, 0 );
1288
+
1289
+ } else {
1290
+
1291
+ ctx.drawImage( image, 0, 0, canvas.width, canvas.height );
1292
+
1293
+ }
1294
+
1295
+ if ( options.binary === true ) {
1296
+
1297
+ pending.push(
1298
+
1299
+ getToBlobPromise( canvas, mimeType )
1300
+ .then( blob => writer.processBufferViewImage( blob ) )
1301
+ .then( bufferViewIndex => {
1302
+
1303
+ imageDef.bufferView = bufferViewIndex;
1304
+
1305
+ } )
1306
+
1307
+ );
1308
+
1309
+ } else {
1310
+
1311
+ if ( canvas.toDataURL !== undefined ) {
1312
+
1313
+ imageDef.uri = canvas.toDataURL( mimeType );
1314
+
1315
+ } else {
1316
+
1317
+ pending.push(
1318
+
1319
+ getToBlobPromise( canvas, mimeType )
1320
+ .then( blob => new FileReader().readAsDataURL( blob ) )
1321
+ .then( dataURL => {
1322
+
1323
+ imageDef.uri = dataURL;
1324
+
1325
+ } )
1326
+
1327
+ );
1328
+
1329
+ }
1330
+
1331
+ }
1332
+
1333
+ const index = json.images.push( imageDef ) - 1;
1334
+ cachedImages[ key ] = index;
1335
+ return index;
1336
+
1337
+ } else {
1338
+
1339
+ throw new Error( 'THREE.GLTFExporter: No valid image data found. Unable to process texture.' );
1340
+
1341
+ }
1342
+
1343
+ }
1344
+
1345
+ /**
1346
+ * Process sampler
1347
+ * @param {Texture} map Texture to process
1348
+ * @return {Integer} Index of the processed texture in the "samplers" array
1349
+ */
1350
+ processSampler( map ) {
1351
+
1352
+ const json = this.json;
1353
+
1354
+ if ( ! json.samplers ) json.samplers = [];
1355
+
1356
+ const samplerDef = {
1357
+ magFilter: THREE_TO_WEBGL[ map.magFilter ],
1358
+ minFilter: THREE_TO_WEBGL[ map.minFilter ],
1359
+ wrapS: THREE_TO_WEBGL[ map.wrapS ],
1360
+ wrapT: THREE_TO_WEBGL[ map.wrapT ]
1361
+ };
1362
+
1363
+ return json.samplers.push( samplerDef ) - 1;
1364
+
1365
+ }
1366
+
1367
+ /**
1368
+ * Process texture
1369
+ * @param {Texture} map Map to process
1370
+ * @return {Integer} Index of the processed texture in the "textures" array
1371
+ */
1372
+ processTexture( map ) {
1373
+
1374
+ const writer = this;
1375
+ const options = writer.options;
1376
+ const cache = this.cache;
1377
+ const json = this.json;
1378
+
1379
+ if ( cache.textures.has( map ) ) return cache.textures.get( map );
1380
+
1381
+ if ( ! json.textures ) json.textures = [];
1382
+
1383
+ // make non-readable textures (e.g. CompressedTexture) readable by blitting them into a new texture
1384
+ if ( map instanceof CompressedTexture ) {
1385
+
1386
+ map = decompress( map, options.maxTextureSize );
1387
+
1388
+ }
1389
+
1390
+ let mimeType = map.userData.mimeType;
1391
+
1392
+ if ( mimeType === 'image/webp' ) mimeType = 'image/png';
1393
+
1394
+ const textureDef = {
1395
+ sampler: this.processSampler( map ),
1396
+ source: this.processImage( map.image, map.format, map.flipY, mimeType )
1397
+ };
1398
+
1399
+ if ( map.name ) textureDef.name = map.name;
1400
+
1401
+ this._invokeAll( function ( ext ) {
1402
+
1403
+ ext.writeTexture && ext.writeTexture( map, textureDef );
1404
+
1405
+ } );
1406
+
1407
+ const index = json.textures.push( textureDef ) - 1;
1408
+ cache.textures.set( map, index );
1409
+ return index;
1410
+
1411
+ }
1412
+
1413
+ /**
1414
+ * Process material
1415
+ * @param {THREE.Material} material Material to process
1416
+ * @return {Integer|null} Index of the processed material in the "materials" array
1417
+ */
1418
+ processMaterial( material ) {
1419
+
1420
+ const cache = this.cache;
1421
+ const json = this.json;
1422
+
1423
+ if ( cache.materials.has( material ) ) return cache.materials.get( material );
1424
+
1425
+ if ( material.isShaderMaterial ) {
1426
+
1427
+ console.warn( 'GLTFExporter: THREE.ShaderMaterial not supported.' );
1428
+ return null;
1429
+
1430
+ }
1431
+
1432
+ if ( ! json.materials ) json.materials = [];
1433
+
1434
+ // @QUESTION Should we avoid including any attribute that has the default value?
1435
+ const materialDef = { pbrMetallicRoughness: {} };
1436
+
1437
+ if ( material.isMeshStandardMaterial !== true && material.isMeshBasicMaterial !== true ) {
1438
+
1439
+ console.warn( 'GLTFExporter: Use MeshStandardMaterial or MeshBasicMaterial for best results.' );
1440
+
1441
+ }
1442
+
1443
+ // pbrMetallicRoughness.baseColorFactor
1444
+ const color = material.color.toArray().concat( [ material.opacity ] );
1445
+
1446
+ if ( ! equalArray( color, [ 1, 1, 1, 1 ] ) ) {
1447
+
1448
+ materialDef.pbrMetallicRoughness.baseColorFactor = color;
1449
+
1450
+ }
1451
+
1452
+ if ( material.isMeshStandardMaterial ) {
1453
+
1454
+ materialDef.pbrMetallicRoughness.metallicFactor = material.metalness;
1455
+ materialDef.pbrMetallicRoughness.roughnessFactor = material.roughness;
1456
+
1457
+ } else {
1458
+
1459
+ materialDef.pbrMetallicRoughness.metallicFactor = 0.5;
1460
+ materialDef.pbrMetallicRoughness.roughnessFactor = 0.5;
1461
+
1462
+ }
1463
+
1464
+ // pbrMetallicRoughness.metallicRoughnessTexture
1465
+ if ( material.metalnessMap || material.roughnessMap ) {
1466
+
1467
+ const metalRoughTexture = this.buildMetalRoughTexture( material.metalnessMap, material.roughnessMap );
1468
+
1469
+ const metalRoughMapDef = {
1470
+ index: this.processTexture( metalRoughTexture ),
1471
+ channel: metalRoughTexture.channel
1472
+ };
1473
+ this.applyTextureTransform( metalRoughMapDef, metalRoughTexture );
1474
+ materialDef.pbrMetallicRoughness.metallicRoughnessTexture = metalRoughMapDef;
1475
+
1476
+ }
1477
+
1478
+ // pbrMetallicRoughness.baseColorTexture
1479
+ if ( material.map ) {
1480
+
1481
+ const baseColorMapDef = {
1482
+ index: this.processTexture( material.map ),
1483
+ texCoord: material.map.channel
1484
+ };
1485
+ this.applyTextureTransform( baseColorMapDef, material.map );
1486
+ materialDef.pbrMetallicRoughness.baseColorTexture = baseColorMapDef;
1487
+
1488
+ }
1489
+
1490
+ if ( material.emissive ) {
1491
+
1492
+ const emissive = material.emissive;
1493
+ const maxEmissiveComponent = Math.max( emissive.r, emissive.g, emissive.b );
1494
+
1495
+ if ( maxEmissiveComponent > 0 ) {
1496
+
1497
+ materialDef.emissiveFactor = material.emissive.toArray();
1498
+
1499
+ }
1500
+
1501
+ // emissiveTexture
1502
+ if ( material.emissiveMap ) {
1503
+
1504
+ const emissiveMapDef = {
1505
+ index: this.processTexture( material.emissiveMap ),
1506
+ texCoord: material.emissiveMap.channel
1507
+ };
1508
+ this.applyTextureTransform( emissiveMapDef, material.emissiveMap );
1509
+ materialDef.emissiveTexture = emissiveMapDef;
1510
+
1511
+ }
1512
+
1513
+ }
1514
+
1515
+ // normalTexture
1516
+ if ( material.normalMap ) {
1517
+
1518
+ const normalMapDef = {
1519
+ index: this.processTexture( material.normalMap ),
1520
+ texCoord: material.normalMap.channel
1521
+ };
1522
+
1523
+ if ( material.normalScale && material.normalScale.x !== 1 ) {
1524
+
1525
+ // glTF normal scale is univariate. Ignore `y`, which may be flipped.
1526
+ // Context: https://github.com/mrdoob/three.js/issues/11438#issuecomment-507003995
1527
+ normalMapDef.scale = material.normalScale.x;
1528
+
1529
+ }
1530
+
1531
+ this.applyTextureTransform( normalMapDef, material.normalMap );
1532
+ materialDef.normalTexture = normalMapDef;
1533
+
1534
+ }
1535
+
1536
+ // occlusionTexture
1537
+ if ( material.aoMap ) {
1538
+
1539
+ const occlusionMapDef = {
1540
+ index: this.processTexture( material.aoMap ),
1541
+ texCoord: material.aoMap.channel
1542
+ };
1543
+
1544
+ if ( material.aoMapIntensity !== 1.0 ) {
1545
+
1546
+ occlusionMapDef.strength = material.aoMapIntensity;
1547
+
1548
+ }
1549
+
1550
+ this.applyTextureTransform( occlusionMapDef, material.aoMap );
1551
+ materialDef.occlusionTexture = occlusionMapDef;
1552
+
1553
+ }
1554
+
1555
+ // alphaMode
1556
+ if ( material.transparent ) {
1557
+
1558
+ materialDef.alphaMode = 'BLEND';
1559
+
1560
+ } else {
1561
+
1562
+ if ( material.alphaTest > 0.0 ) {
1563
+
1564
+ materialDef.alphaMode = 'MASK';
1565
+ materialDef.alphaCutoff = material.alphaTest;
1566
+
1567
+ }
1568
+
1569
+ }
1570
+
1571
+ // doubleSided
1572
+ if ( material.side === DoubleSide ) materialDef.doubleSided = true;
1573
+ if ( material.name !== '' ) materialDef.name = material.name;
1574
+
1575
+ this.serializeUserData( material, materialDef );
1576
+
1577
+ this._invokeAll( function ( ext ) {
1578
+
1579
+ ext.writeMaterial && ext.writeMaterial( material, materialDef );
1580
+
1581
+ } );
1582
+
1583
+ const index = json.materials.push( materialDef ) - 1;
1584
+ cache.materials.set( material, index );
1585
+ return index;
1586
+
1587
+ }
1588
+
1589
+ /**
1590
+ * Process mesh
1591
+ * @param {THREE.Mesh} mesh Mesh to process
1592
+ * @return {Integer|null} Index of the processed mesh in the "meshes" array
1593
+ */
1594
+ processMesh( mesh ) {
1595
+
1596
+ const cache = this.cache;
1597
+ const json = this.json;
1598
+
1599
+ const meshCacheKeyParts = [ mesh.geometry.uuid ];
1600
+
1601
+ if ( Array.isArray( mesh.material ) ) {
1602
+
1603
+ for ( let i = 0, l = mesh.material.length; i < l; i ++ ) {
1604
+
1605
+ meshCacheKeyParts.push( mesh.material[ i ].uuid );
1606
+
1607
+ }
1608
+
1609
+ } else {
1610
+
1611
+ meshCacheKeyParts.push( mesh.material.uuid );
1612
+
1613
+ }
1614
+
1615
+ const meshCacheKey = meshCacheKeyParts.join( ':' );
1616
+
1617
+ if ( cache.meshes.has( meshCacheKey ) ) return cache.meshes.get( meshCacheKey );
1618
+
1619
+ const geometry = mesh.geometry;
1620
+
1621
+ let mode;
1622
+
1623
+ // Use the correct mode
1624
+ if ( mesh.isLineSegments ) {
1625
+
1626
+ mode = WEBGL_CONSTANTS.LINES;
1627
+
1628
+ } else if ( mesh.isLineLoop ) {
1629
+
1630
+ mode = WEBGL_CONSTANTS.LINE_LOOP;
1631
+
1632
+ } else if ( mesh.isLine ) {
1633
+
1634
+ mode = WEBGL_CONSTANTS.LINE_STRIP;
1635
+
1636
+ } else if ( mesh.isPoints ) {
1637
+
1638
+ mode = WEBGL_CONSTANTS.POINTS;
1639
+
1640
+ } else {
1641
+
1642
+ mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINES : WEBGL_CONSTANTS.TRIANGLES;
1643
+
1644
+ }
1645
+
1646
+ const meshDef = {};
1647
+ const attributes = {};
1648
+ const primitives = [];
1649
+ const targets = [];
1650
+
1651
+ // Conversion between attributes names in threejs and gltf spec
1652
+ const nameConversion = {
1653
+ uv: 'TEXCOORD_0',
1654
+ uv1: 'TEXCOORD_1',
1655
+ uv2: 'TEXCOORD_2',
1656
+ uv3: 'TEXCOORD_3',
1657
+ color: 'COLOR_0',
1658
+ skinWeight: 'WEIGHTS_0',
1659
+ skinIndex: 'JOINTS_0'
1660
+ };
1661
+
1662
+ const originalNormal = geometry.getAttribute( 'normal' );
1663
+
1664
+ if ( originalNormal !== undefined && ! this.isNormalizedNormalAttribute( originalNormal ) ) {
1665
+
1666
+ console.warn( 'THREE.GLTFExporter: Creating normalized normal attribute from the non-normalized one.' );
1667
+
1668
+ geometry.setAttribute( 'normal', this.createNormalizedNormalAttribute( originalNormal ) );
1669
+
1670
+ }
1671
+
1672
+ // @QUESTION Detect if .vertexColors = true?
1673
+ // For every attribute create an accessor
1674
+ let modifiedAttribute = null;
1675
+
1676
+ for ( let attributeName in geometry.attributes ) {
1677
+
1678
+ // Ignore morph target attributes, which are exported later.
1679
+ if ( attributeName.slice( 0, 5 ) === 'morph' ) continue;
1680
+
1681
+ const attribute = geometry.attributes[ attributeName ];
1682
+ attributeName = nameConversion[ attributeName ] || attributeName.toUpperCase();
1683
+
1684
+ // Prefix all geometry attributes except the ones specifically
1685
+ // listed in the spec; non-spec attributes are considered custom.
1686
+ const validVertexAttributes =
1687
+ /^(POSITION|NORMAL|TANGENT|TEXCOORD_\d+|COLOR_\d+|JOINTS_\d+|WEIGHTS_\d+)$/;
1688
+
1689
+ if ( ! validVertexAttributes.test( attributeName ) ) attributeName = '_' + attributeName;
1690
+
1691
+ if ( cache.attributes.has( this.getUID( attribute ) ) ) {
1692
+
1693
+ attributes[ attributeName ] = cache.attributes.get( this.getUID( attribute ) );
1694
+ continue;
1695
+
1696
+ }
1697
+
1698
+ // JOINTS_0 must be UNSIGNED_BYTE or UNSIGNED_SHORT.
1699
+ modifiedAttribute = null;
1700
+ const array = attribute.array;
1701
+
1702
+ if ( attributeName === 'JOINTS_0' &&
1703
+ ! ( array instanceof Uint16Array ) &&
1704
+ ! ( array instanceof Uint8Array ) ) {
1705
+
1706
+ console.warn( 'GLTFExporter: Attribute "skinIndex" converted to type UNSIGNED_SHORT.' );
1707
+ modifiedAttribute = new BufferAttribute( new Uint16Array( array ), attribute.itemSize, attribute.normalized );
1708
+
1709
+ }
1710
+
1711
+ const accessor = this.processAccessor( modifiedAttribute || attribute, geometry );
1712
+
1713
+ if ( accessor !== null ) {
1714
+
1715
+ if ( ! attributeName.startsWith( '_' ) ) {
1716
+
1717
+ this.detectMeshQuantization( attributeName, attribute );
1718
+
1719
+ }
1720
+
1721
+ attributes[ attributeName ] = accessor;
1722
+ cache.attributes.set( this.getUID( attribute ), accessor );
1723
+
1724
+ }
1725
+
1726
+ }
1727
+
1728
+ if ( originalNormal !== undefined ) geometry.setAttribute( 'normal', originalNormal );
1729
+
1730
+ // Skip if no exportable attributes found
1731
+ if ( Object.keys( attributes ).length === 0 ) return null;
1732
+
1733
+ // Morph targets
1734
+ if ( mesh.morphTargetInfluences !== undefined && mesh.morphTargetInfluences.length > 0 ) {
1735
+
1736
+ const weights = [];
1737
+ const targetNames = [];
1738
+ const reverseDictionary = {};
1739
+
1740
+ if ( mesh.morphTargetDictionary !== undefined ) {
1741
+
1742
+ for ( const key in mesh.morphTargetDictionary ) {
1743
+
1744
+ reverseDictionary[ mesh.morphTargetDictionary[ key ] ] = key;
1745
+
1746
+ }
1747
+
1748
+ }
1749
+
1750
+ for ( let i = 0; i < mesh.morphTargetInfluences.length; ++ i ) {
1751
+
1752
+ const target = {};
1753
+ let warned = false;
1754
+
1755
+ for ( const attributeName in geometry.morphAttributes ) {
1756
+
1757
+ // glTF 2.0 morph supports only POSITION/NORMAL/TANGENT.
1758
+ // Three.js doesn't support TANGENT yet.
1759
+
1760
+ if ( attributeName !== 'position' && attributeName !== 'normal' ) {
1761
+
1762
+ if ( ! warned ) {
1763
+
1764
+ console.warn( 'GLTFExporter: Only POSITION and NORMAL morph are supported.' );
1765
+ warned = true;
1766
+
1767
+ }
1768
+
1769
+ continue;
1770
+
1771
+ }
1772
+
1773
+ const attribute = geometry.morphAttributes[ attributeName ][ i ];
1774
+ const gltfAttributeName = attributeName.toUpperCase();
1775
+
1776
+ // Three.js morph attribute has absolute values while the one of glTF has relative values.
1777
+ //
1778
+ // glTF 2.0 Specification:
1779
+ // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#morph-targets
1780
+
1781
+ const baseAttribute = geometry.attributes[ attributeName ];
1782
+
1783
+ if ( cache.attributes.has( this.getUID( attribute, true ) ) ) {
1784
+
1785
+ target[ gltfAttributeName ] = cache.attributes.get( this.getUID( attribute, true ) );
1786
+ continue;
1787
+
1788
+ }
1789
+
1790
+ // Clones attribute not to override
1791
+ const relativeAttribute = attribute.clone();
1792
+
1793
+ if ( ! geometry.morphTargetsRelative ) {
1794
+
1795
+ for ( let j = 0, jl = attribute.count; j < jl; j ++ ) {
1796
+
1797
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
1798
+
1799
+ if ( a === 0 ) relativeAttribute.setX( j, attribute.getX( j ) - baseAttribute.getX( j ) );
1800
+ if ( a === 1 ) relativeAttribute.setY( j, attribute.getY( j ) - baseAttribute.getY( j ) );
1801
+ if ( a === 2 ) relativeAttribute.setZ( j, attribute.getZ( j ) - baseAttribute.getZ( j ) );
1802
+ if ( a === 3 ) relativeAttribute.setW( j, attribute.getW( j ) - baseAttribute.getW( j ) );
1803
+
1804
+ }
1805
+
1806
+ }
1807
+
1808
+ }
1809
+
1810
+ target[ gltfAttributeName ] = this.processAccessor( relativeAttribute, geometry );
1811
+ cache.attributes.set( this.getUID( baseAttribute, true ), target[ gltfAttributeName ] );
1812
+
1813
+ }
1814
+
1815
+ targets.push( target );
1816
+
1817
+ weights.push( mesh.morphTargetInfluences[ i ] );
1818
+
1819
+ if ( mesh.morphTargetDictionary !== undefined ) targetNames.push( reverseDictionary[ i ] );
1820
+
1821
+ }
1822
+
1823
+ meshDef.weights = weights;
1824
+
1825
+ if ( targetNames.length > 0 ) {
1826
+
1827
+ meshDef.extras = {};
1828
+ meshDef.extras.targetNames = targetNames;
1829
+
1830
+ }
1831
+
1832
+ }
1833
+
1834
+ const isMultiMaterial = Array.isArray( mesh.material );
1835
+
1836
+ if ( isMultiMaterial && geometry.groups.length === 0 ) return null;
1837
+
1838
+ let didForceIndices = false;
1839
+
1840
+ if ( isMultiMaterial && geometry.index === null ) {
1841
+
1842
+ const indices = [];
1843
+
1844
+ for ( let i = 0, il = geometry.attributes.position.count; i < il; i ++ ) {
1845
+
1846
+ indices[ i ] = i;
1847
+
1848
+ }
1849
+
1850
+ geometry.setIndex( indices );
1851
+
1852
+ didForceIndices = true;
1853
+
1854
+ }
1855
+
1856
+ const materials = isMultiMaterial ? mesh.material : [ mesh.material ];
1857
+ const groups = isMultiMaterial ? geometry.groups : [ { materialIndex: 0, start: undefined, count: undefined } ];
1858
+
1859
+ for ( let i = 0, il = groups.length; i < il; i ++ ) {
1860
+
1861
+ const primitive = {
1862
+ mode: mode,
1863
+ attributes: attributes,
1864
+ };
1865
+
1866
+ this.serializeUserData( geometry, primitive );
1867
+
1868
+ if ( targets.length > 0 ) primitive.targets = targets;
1869
+
1870
+ if ( geometry.index !== null ) {
1871
+
1872
+ let cacheKey = this.getUID( geometry.index );
1873
+
1874
+ if ( groups[ i ].start !== undefined || groups[ i ].count !== undefined ) {
1875
+
1876
+ cacheKey += ':' + groups[ i ].start + ':' + groups[ i ].count;
1877
+
1878
+ }
1879
+
1880
+ if ( cache.attributes.has( cacheKey ) ) {
1881
+
1882
+ primitive.indices = cache.attributes.get( cacheKey );
1883
+
1884
+ } else {
1885
+
1886
+ primitive.indices = this.processAccessor( geometry.index, geometry, groups[ i ].start, groups[ i ].count );
1887
+ cache.attributes.set( cacheKey, primitive.indices );
1888
+
1889
+ }
1890
+
1891
+ if ( primitive.indices === null ) delete primitive.indices;
1892
+
1893
+ }
1894
+
1895
+ const material = this.processMaterial( materials[ groups[ i ].materialIndex ] );
1896
+
1897
+ if ( material !== null ) primitive.material = material;
1898
+
1899
+ primitives.push( primitive );
1900
+
1901
+ }
1902
+
1903
+ if ( didForceIndices === true ) {
1904
+
1905
+ geometry.setIndex( null );
1906
+
1907
+ }
1908
+
1909
+ meshDef.primitives = primitives;
1910
+
1911
+ if ( ! json.meshes ) json.meshes = [];
1912
+
1913
+ this._invokeAll( function ( ext ) {
1914
+
1915
+ ext.writeMesh && ext.writeMesh( mesh, meshDef );
1916
+
1917
+ } );
1918
+
1919
+ const index = json.meshes.push( meshDef ) - 1;
1920
+ cache.meshes.set( meshCacheKey, index );
1921
+ return index;
1922
+
1923
+ }
1924
+
1925
+ /**
1926
+ * If a vertex attribute with a
1927
+ * [non-standard data type](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview)
1928
+ * is used, it is checked whether it is a valid data type according to the
1929
+ * [KHR_mesh_quantization](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_mesh_quantization/README.md)
1930
+ * extension.
1931
+ * In this case the extension is automatically added to the list of used extensions.
1932
+ *
1933
+ * @param {string} attributeName
1934
+ * @param {THREE.BufferAttribute} attribute
1935
+ */
1936
+ detectMeshQuantization( attributeName, attribute ) {
1937
+
1938
+ if ( this.extensionsUsed[ KHR_MESH_QUANTIZATION ] ) return;
1939
+
1940
+ let attrType = undefined;
1941
+
1942
+ switch ( attribute.array.constructor ) {
1943
+
1944
+ case Int8Array:
1945
+
1946
+ attrType = 'byte';
1947
+
1948
+ break;
1949
+
1950
+ case Uint8Array:
1951
+
1952
+ attrType = 'unsigned byte';
1953
+
1954
+ break;
1955
+
1956
+ case Int16Array:
1957
+
1958
+ attrType = 'short';
1959
+
1960
+ break;
1961
+
1962
+ case Uint16Array:
1963
+
1964
+ attrType = 'unsigned short';
1965
+
1966
+ break;
1967
+
1968
+ default:
1969
+
1970
+ return;
1971
+
1972
+ }
1973
+
1974
+ if ( attribute.normalized ) attrType += ' normalized';
1975
+
1976
+ const attrNamePrefix = attributeName.split( '_', 1 )[ 0 ];
1977
+
1978
+ if ( KHR_mesh_quantization_ExtraAttrTypes[ attrNamePrefix ] && KHR_mesh_quantization_ExtraAttrTypes[ attrNamePrefix ].includes( attrType ) ) {
1979
+
1980
+ this.extensionsUsed[ KHR_MESH_QUANTIZATION ] = true;
1981
+ this.extensionsRequired[ KHR_MESH_QUANTIZATION ] = true;
1982
+
1983
+ }
1984
+
1985
+ }
1986
+
1987
+ /**
1988
+ * Process camera
1989
+ * @param {THREE.Camera} camera Camera to process
1990
+ * @return {Integer} Index of the processed mesh in the "camera" array
1991
+ */
1992
+ processCamera( camera ) {
1993
+
1994
+ const json = this.json;
1995
+
1996
+ if ( ! json.cameras ) json.cameras = [];
1997
+
1998
+ const isOrtho = camera.isOrthographicCamera;
1999
+
2000
+ const cameraDef = {
2001
+ type: isOrtho ? 'orthographic' : 'perspective'
2002
+ };
2003
+
2004
+ if ( isOrtho ) {
2005
+
2006
+ cameraDef.orthographic = {
2007
+ xmag: camera.right * 2,
2008
+ ymag: camera.top * 2,
2009
+ zfar: camera.far <= 0 ? 0.001 : camera.far,
2010
+ znear: camera.near < 0 ? 0 : camera.near
2011
+ };
2012
+
2013
+ } else {
2014
+
2015
+ cameraDef.perspective = {
2016
+ aspectRatio: camera.aspect,
2017
+ yfov: MathUtils.degToRad( camera.fov ),
2018
+ zfar: camera.far <= 0 ? 0.001 : camera.far,
2019
+ znear: camera.near < 0 ? 0 : camera.near
2020
+ };
2021
+
2022
+ }
2023
+
2024
+ // Question: Is saving "type" as name intentional?
2025
+ if ( camera.name !== '' ) cameraDef.name = camera.type;
2026
+
2027
+ return json.cameras.push( cameraDef ) - 1;
2028
+
2029
+ }
2030
+
2031
+ /**
2032
+ * Creates glTF animation entry from AnimationClip object.
2033
+ *
2034
+ * Status:
2035
+ * - Only properties listed in PATH_PROPERTIES may be animated.
2036
+ *
2037
+ * @param {THREE.AnimationClip} clip
2038
+ * @param {THREE.Object3D} root
2039
+ * @return {number|null}
2040
+ */
2041
+ processAnimation( clip, root ) {
2042
+
2043
+ const json = this.json;
2044
+ const nodeMap = this.nodeMap;
2045
+
2046
+ if ( ! json.animations ) json.animations = [];
2047
+
2048
+ clip = GLTFExporter.Utils.mergeMorphTargetTracks( clip.clone(), root );
2049
+
2050
+ const tracks = clip.tracks;
2051
+ const channels = [];
2052
+ const samplers = [];
2053
+
2054
+ for ( let i = 0; i < tracks.length; ++ i ) {
2055
+
2056
+ const track = tracks[ i ];
2057
+ const trackBinding = PropertyBinding.parseTrackName( track.name );
2058
+ let trackNode = PropertyBinding.findNode( root, trackBinding.nodeName );
2059
+ const trackProperty = PATH_PROPERTIES[ trackBinding.propertyName ];
2060
+
2061
+ if ( trackBinding.objectName === 'bones' ) {
2062
+
2063
+ if ( trackNode.isSkinnedMesh === true ) {
2064
+
2065
+ trackNode = trackNode.skeleton.getBoneByName( trackBinding.objectIndex );
2066
+
2067
+ } else {
2068
+
2069
+ trackNode = undefined;
2070
+
2071
+ }
2072
+
2073
+ }
2074
+
2075
+ if ( ! trackNode || ! trackProperty ) {
2076
+
2077
+ console.warn( 'THREE.GLTFExporter: Could not export animation track "%s".', track.name );
2078
+ return null;
2079
+
2080
+ }
2081
+
2082
+ const inputItemSize = 1;
2083
+ let outputItemSize = track.values.length / track.times.length;
2084
+
2085
+ if ( trackProperty === PATH_PROPERTIES.morphTargetInfluences ) {
2086
+
2087
+ outputItemSize /= trackNode.morphTargetInfluences.length;
2088
+
2089
+ }
2090
+
2091
+ let interpolation;
2092
+
2093
+ // @TODO export CubicInterpolant(InterpolateSmooth) as CUBICSPLINE
2094
+
2095
+ // Detecting glTF cubic spline interpolant by checking factory method's special property
2096
+ // GLTFCubicSplineInterpolant is a custom interpolant and track doesn't return
2097
+ // valid value from .getInterpolation().
2098
+ if ( track.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline === true ) {
2099
+
2100
+ interpolation = 'CUBICSPLINE';
2101
+
2102
+ // itemSize of CUBICSPLINE keyframe is 9
2103
+ // (VEC3 * 3: inTangent, splineVertex, and outTangent)
2104
+ // but needs to be stored as VEC3 so dividing by 3 here.
2105
+ outputItemSize /= 3;
2106
+
2107
+ } else if ( track.getInterpolation() === InterpolateDiscrete ) {
2108
+
2109
+ interpolation = 'STEP';
2110
+
2111
+ } else {
2112
+
2113
+ interpolation = 'LINEAR';
2114
+
2115
+ }
2116
+
2117
+ samplers.push( {
2118
+ input: this.processAccessor( new BufferAttribute( track.times, inputItemSize ) ),
2119
+ output: this.processAccessor( new BufferAttribute( track.values, outputItemSize ) ),
2120
+ interpolation: interpolation
2121
+ } );
2122
+
2123
+ channels.push( {
2124
+ sampler: samplers.length - 1,
2125
+ target: {
2126
+ node: nodeMap.get( trackNode ),
2127
+ path: trackProperty
2128
+ }
2129
+ } );
2130
+
2131
+ }
2132
+
2133
+ json.animations.push( {
2134
+ name: clip.name || 'clip_' + json.animations.length,
2135
+ samplers: samplers,
2136
+ channels: channels
2137
+ } );
2138
+
2139
+ return json.animations.length - 1;
2140
+
2141
+ }
2142
+
2143
+ /**
2144
+ * @param {THREE.Object3D} object
2145
+ * @return {number|null}
2146
+ */
2147
+ processSkin( object ) {
2148
+
2149
+ const json = this.json;
2150
+ const nodeMap = this.nodeMap;
2151
+
2152
+ const node = json.nodes[ nodeMap.get( object ) ];
2153
+
2154
+ const skeleton = object.skeleton;
2155
+
2156
+ if ( skeleton === undefined ) return null;
2157
+
2158
+ const rootJoint = object.skeleton.bones[ 0 ];
2159
+
2160
+ if ( rootJoint === undefined ) return null;
2161
+
2162
+ const joints = [];
2163
+ const inverseBindMatrices = new Float32Array( skeleton.bones.length * 16 );
2164
+ const temporaryBoneInverse = new Matrix4();
2165
+
2166
+ for ( let i = 0; i < skeleton.bones.length; ++ i ) {
2167
+
2168
+ joints.push( nodeMap.get( skeleton.bones[ i ] ) );
2169
+ temporaryBoneInverse.copy( skeleton.boneInverses[ i ] );
2170
+ temporaryBoneInverse.multiply( object.bindMatrix ).toArray( inverseBindMatrices, i * 16 );
2171
+
2172
+ }
2173
+
2174
+ if ( json.skins === undefined ) json.skins = [];
2175
+
2176
+ json.skins.push( {
2177
+ inverseBindMatrices: this.processAccessor( new BufferAttribute( inverseBindMatrices, 16 ) ),
2178
+ joints: joints,
2179
+ skeleton: nodeMap.get( rootJoint )
2180
+ } );
2181
+
2182
+ const skinIndex = node.skin = json.skins.length - 1;
2183
+
2184
+ return skinIndex;
2185
+
2186
+ }
2187
+
2188
+ /**
2189
+ * Process Object3D node
2190
+ * @param {THREE.Object3D} node Object3D to processNode
2191
+ * @return {Integer} Index of the node in the nodes list
2192
+ */
2193
+ processNode( object ) {
2194
+
2195
+ const json = this.json;
2196
+ const options = this.options;
2197
+ const nodeMap = this.nodeMap;
2198
+
2199
+ if ( ! json.nodes ) json.nodes = [];
2200
+
2201
+ const nodeDef = {};
2202
+
2203
+ if ( options.trs ) {
2204
+
2205
+ const rotation = object.quaternion.toArray();
2206
+ const position = object.position.toArray();
2207
+ const scale = object.scale.toArray();
2208
+
2209
+ if ( ! equalArray( rotation, [ 0, 0, 0, 1 ] ) ) {
2210
+
2211
+ nodeDef.rotation = rotation;
2212
+
2213
+ }
2214
+
2215
+ if ( ! equalArray( position, [ 0, 0, 0 ] ) ) {
2216
+
2217
+ nodeDef.translation = position;
2218
+
2219
+ }
2220
+
2221
+ if ( ! equalArray( scale, [ 1, 1, 1 ] ) ) {
2222
+
2223
+ nodeDef.scale = scale;
2224
+
2225
+ }
2226
+
2227
+ } else {
2228
+
2229
+ if ( object.matrixAutoUpdate ) {
2230
+
2231
+ object.updateMatrix();
2232
+
2233
+ }
2234
+
2235
+ if ( isIdentityMatrix( object.matrix ) === false ) {
2236
+
2237
+ nodeDef.matrix = object.matrix.elements;
2238
+
2239
+ }
2240
+
2241
+ }
2242
+
2243
+ // We don't export empty strings name because it represents no-name in Three.js.
2244
+ if ( object.name !== '' ) nodeDef.name = String( object.name );
2245
+
2246
+ this.serializeUserData( object, nodeDef );
2247
+
2248
+ if ( object.isMesh || object.isLine || object.isPoints ) {
2249
+
2250
+ const meshIndex = this.processMesh( object );
2251
+
2252
+ if ( meshIndex !== null ) nodeDef.mesh = meshIndex;
2253
+
2254
+ } else if ( object.isCamera ) {
2255
+
2256
+ nodeDef.camera = this.processCamera( object );
2257
+
2258
+ }
2259
+
2260
+ if ( object.isSkinnedMesh ) this.skins.push( object );
2261
+
2262
+ if ( object.children.length > 0 ) {
2263
+
2264
+ const children = [];
2265
+
2266
+ for ( let i = 0, l = object.children.length; i < l; i ++ ) {
2267
+
2268
+ const child = object.children[ i ];
2269
+
2270
+ if ( child.visible || options.onlyVisible === false ) {
2271
+
2272
+ const nodeIndex = this.processNode( child );
2273
+
2274
+ if ( nodeIndex !== null ) children.push( nodeIndex );
2275
+
2276
+ }
2277
+
2278
+ }
2279
+
2280
+ if ( children.length > 0 ) nodeDef.children = children;
2281
+
2282
+ }
2283
+
2284
+ this._invokeAll( function ( ext ) {
2285
+
2286
+ ext.writeNode && ext.writeNode( object, nodeDef );
2287
+
2288
+ } );
2289
+
2290
+ const nodeIndex = json.nodes.push( nodeDef ) - 1;
2291
+ nodeMap.set( object, nodeIndex );
2292
+ return nodeIndex;
2293
+
2294
+ }
2295
+
2296
+ /**
2297
+ * Process Scene
2298
+ * @param {Scene} node Scene to process
2299
+ */
2300
+ processScene( scene ) {
2301
+
2302
+ const json = this.json;
2303
+ const options = this.options;
2304
+
2305
+ if ( ! json.scenes ) {
2306
+
2307
+ json.scenes = [];
2308
+ json.scene = 0;
2309
+
2310
+ }
2311
+
2312
+ const sceneDef = {};
2313
+
2314
+ if ( scene.name !== '' ) sceneDef.name = scene.name;
2315
+
2316
+ json.scenes.push( sceneDef );
2317
+
2318
+ const nodes = [];
2319
+
2320
+ for ( let i = 0, l = scene.children.length; i < l; i ++ ) {
2321
+
2322
+ const child = scene.children[ i ];
2323
+
2324
+ if ( child.visible || options.onlyVisible === false ) {
2325
+
2326
+ const nodeIndex = this.processNode( child );
2327
+
2328
+ if ( nodeIndex !== null ) nodes.push( nodeIndex );
2329
+
2330
+ }
2331
+
2332
+ }
2333
+
2334
+ if ( nodes.length > 0 ) sceneDef.nodes = nodes;
2335
+
2336
+ this.serializeUserData( scene, sceneDef );
2337
+
2338
+ }
2339
+
2340
+ /**
2341
+ * Creates a Scene to hold a list of objects and parse it
2342
+ * @param {Array} objects List of objects to process
2343
+ */
2344
+ processObjects( objects ) {
2345
+
2346
+ const scene = new Scene();
2347
+ scene.name = 'AuxScene';
2348
+
2349
+ for ( let i = 0; i < objects.length; i ++ ) {
2350
+
2351
+ // We push directly to children instead of calling `add` to prevent
2352
+ // modify the .parent and break its original scene and hierarchy
2353
+ scene.children.push( objects[ i ] );
2354
+
2355
+ }
2356
+
2357
+ this.processScene( scene );
2358
+
2359
+ }
2360
+
2361
+ /**
2362
+ * @param {THREE.Object3D|Array<THREE.Object3D>} input
2363
+ */
2364
+ processInput( input ) {
2365
+
2366
+ const options = this.options;
2367
+
2368
+ input = input instanceof Array ? input : [ input ];
2369
+
2370
+ this._invokeAll( function ( ext ) {
2371
+
2372
+ ext.beforeParse && ext.beforeParse( input );
2373
+
2374
+ } );
2375
+
2376
+ const objectsWithoutScene = [];
2377
+
2378
+ for ( let i = 0; i < input.length; i ++ ) {
2379
+
2380
+ if ( input[ i ] instanceof Scene ) {
2381
+
2382
+ this.processScene( input[ i ] );
2383
+
2384
+ } else {
2385
+
2386
+ objectsWithoutScene.push( input[ i ] );
2387
+
2388
+ }
2389
+
2390
+ }
2391
+
2392
+ if ( objectsWithoutScene.length > 0 ) this.processObjects( objectsWithoutScene );
2393
+
2394
+ for ( let i = 0; i < this.skins.length; ++ i ) {
2395
+
2396
+ this.processSkin( this.skins[ i ] );
2397
+
2398
+ }
2399
+
2400
+ for ( let i = 0; i < options.animations.length; ++ i ) {
2401
+
2402
+ this.processAnimation( options.animations[ i ], input[ 0 ] );
2403
+
2404
+ }
2405
+
2406
+ this._invokeAll( function ( ext ) {
2407
+
2408
+ ext.afterParse && ext.afterParse( input );
2409
+
2410
+ } );
2411
+
2412
+ }
2413
+
2414
+ _invokeAll( func ) {
2415
+
2416
+ for ( let i = 0, il = this.plugins.length; i < il; i ++ ) {
2417
+
2418
+ func( this.plugins[ i ] );
2419
+
2420
+ }
2421
+
2422
+ }
2423
+
2424
+ }
2425
+
2426
+ /**
2427
+ * Punctual Lights Extension
2428
+ *
2429
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
2430
+ */
2431
+ class GLTFLightExtension {
2432
+
2433
+ constructor( writer ) {
2434
+
2435
+ this.writer = writer;
2436
+ this.name = 'KHR_lights_punctual';
2437
+
2438
+ }
2439
+
2440
+ writeNode( light, nodeDef ) {
2441
+
2442
+ if ( ! light.isLight ) return;
2443
+
2444
+ if ( ! light.isDirectionalLight && ! light.isPointLight && ! light.isSpotLight ) {
2445
+
2446
+ console.warn( 'THREE.GLTFExporter: Only directional, point, and spot lights are supported.', light );
2447
+ return;
2448
+
2449
+ }
2450
+
2451
+ const writer = this.writer;
2452
+ const json = writer.json;
2453
+ const extensionsUsed = writer.extensionsUsed;
2454
+
2455
+ const lightDef = {};
2456
+
2457
+ if ( light.name ) lightDef.name = light.name;
2458
+
2459
+ lightDef.color = light.color.toArray();
2460
+
2461
+ lightDef.intensity = light.intensity;
2462
+
2463
+ if ( light.isDirectionalLight ) {
2464
+
2465
+ lightDef.type = 'directional';
2466
+
2467
+ } else if ( light.isPointLight ) {
2468
+
2469
+ lightDef.type = 'point';
2470
+
2471
+ if ( light.distance > 0 ) lightDef.range = light.distance;
2472
+
2473
+ } else if ( light.isSpotLight ) {
2474
+
2475
+ lightDef.type = 'spot';
2476
+
2477
+ if ( light.distance > 0 ) lightDef.range = light.distance;
2478
+
2479
+ lightDef.spot = {};
2480
+ lightDef.spot.innerConeAngle = ( 1.0 - light.penumbra ) * light.angle;
2481
+ lightDef.spot.outerConeAngle = light.angle;
2482
+
2483
+ }
2484
+
2485
+ if ( light.decay !== undefined && light.decay !== 2 ) {
2486
+
2487
+ console.warn( 'THREE.GLTFExporter: Light decay may be lost. glTF is physically-based, '
2488
+ + 'and expects light.decay=2.' );
2489
+
2490
+ }
2491
+
2492
+ if ( light.target
2493
+ && ( light.target.parent !== light
2494
+ || light.target.position.x !== 0
2495
+ || light.target.position.y !== 0
2496
+ || light.target.position.z !== - 1 ) ) {
2497
+
2498
+ console.warn( 'THREE.GLTFExporter: Light direction may be lost. For best results, '
2499
+ + 'make light.target a child of the light with position 0,0,-1.' );
2500
+
2501
+ }
2502
+
2503
+ if ( ! extensionsUsed[ this.name ] ) {
2504
+
2505
+ json.extensions = json.extensions || {};
2506
+ json.extensions[ this.name ] = { lights: [] };
2507
+ extensionsUsed[ this.name ] = true;
2508
+
2509
+ }
2510
+
2511
+ const lights = json.extensions[ this.name ].lights;
2512
+ lights.push( lightDef );
2513
+
2514
+ nodeDef.extensions = nodeDef.extensions || {};
2515
+ nodeDef.extensions[ this.name ] = { light: lights.length - 1 };
2516
+
2517
+ }
2518
+
2519
+ }
2520
+
2521
+ /**
2522
+ * Unlit Materials Extension
2523
+ *
2524
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit
2525
+ */
2526
+ class GLTFMaterialsUnlitExtension {
2527
+
2528
+ constructor( writer ) {
2529
+
2530
+ this.writer = writer;
2531
+ this.name = 'KHR_materials_unlit';
2532
+
2533
+ }
2534
+
2535
+ writeMaterial( material, materialDef ) {
2536
+
2537
+ if ( ! material.isMeshBasicMaterial ) return;
2538
+
2539
+ const writer = this.writer;
2540
+ const extensionsUsed = writer.extensionsUsed;
2541
+
2542
+ materialDef.extensions = materialDef.extensions || {};
2543
+ materialDef.extensions[ this.name ] = {};
2544
+
2545
+ extensionsUsed[ this.name ] = true;
2546
+
2547
+ materialDef.pbrMetallicRoughness.metallicFactor = 0.0;
2548
+ materialDef.pbrMetallicRoughness.roughnessFactor = 0.9;
2549
+
2550
+ }
2551
+
2552
+ }
2553
+
2554
+ /**
2555
+ * Clearcoat Materials Extension
2556
+ *
2557
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_clearcoat
2558
+ */
2559
+ class GLTFMaterialsClearcoatExtension {
2560
+
2561
+ constructor( writer ) {
2562
+
2563
+ this.writer = writer;
2564
+ this.name = 'KHR_materials_clearcoat';
2565
+
2566
+ }
2567
+
2568
+ writeMaterial( material, materialDef ) {
2569
+
2570
+ if ( ! material.isMeshPhysicalMaterial || material.clearcoat === 0 ) return;
2571
+
2572
+ const writer = this.writer;
2573
+ const extensionsUsed = writer.extensionsUsed;
2574
+
2575
+ const extensionDef = {};
2576
+
2577
+ extensionDef.clearcoatFactor = material.clearcoat;
2578
+
2579
+ if ( material.clearcoatMap ) {
2580
+
2581
+ const clearcoatMapDef = {
2582
+ index: writer.processTexture( material.clearcoatMap ),
2583
+ texCoord: material.clearcoatMap.channel
2584
+ };
2585
+ writer.applyTextureTransform( clearcoatMapDef, material.clearcoatMap );
2586
+ extensionDef.clearcoatTexture = clearcoatMapDef;
2587
+
2588
+ }
2589
+
2590
+ extensionDef.clearcoatRoughnessFactor = material.clearcoatRoughness;
2591
+
2592
+ if ( material.clearcoatRoughnessMap ) {
2593
+
2594
+ const clearcoatRoughnessMapDef = {
2595
+ index: writer.processTexture( material.clearcoatRoughnessMap ),
2596
+ texCoord: material.clearcoatRoughnessMap.channel
2597
+ };
2598
+ writer.applyTextureTransform( clearcoatRoughnessMapDef, material.clearcoatRoughnessMap );
2599
+ extensionDef.clearcoatRoughnessTexture = clearcoatRoughnessMapDef;
2600
+
2601
+ }
2602
+
2603
+ if ( material.clearcoatNormalMap ) {
2604
+
2605
+ const clearcoatNormalMapDef = {
2606
+ index: writer.processTexture( material.clearcoatNormalMap ),
2607
+ texCoord: material.clearcoatNormalMap.channel
2608
+ };
2609
+ writer.applyTextureTransform( clearcoatNormalMapDef, material.clearcoatNormalMap );
2610
+ extensionDef.clearcoatNormalTexture = clearcoatNormalMapDef;
2611
+
2612
+ }
2613
+
2614
+ materialDef.extensions = materialDef.extensions || {};
2615
+ materialDef.extensions[ this.name ] = extensionDef;
2616
+
2617
+ extensionsUsed[ this.name ] = true;
2618
+
2619
+
2620
+ }
2621
+
2622
+ }
2623
+
2624
+ /**
2625
+ * Iridescence Materials Extension
2626
+ *
2627
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_iridescence
2628
+ */
2629
+ class GLTFMaterialsIridescenceExtension {
2630
+
2631
+ constructor( writer ) {
2632
+
2633
+ this.writer = writer;
2634
+ this.name = 'KHR_materials_iridescence';
2635
+
2636
+ }
2637
+
2638
+ writeMaterial( material, materialDef ) {
2639
+
2640
+ if ( ! material.isMeshPhysicalMaterial || material.iridescence === 0 ) return;
2641
+
2642
+ const writer = this.writer;
2643
+ const extensionsUsed = writer.extensionsUsed;
2644
+
2645
+ const extensionDef = {};
2646
+
2647
+ extensionDef.iridescenceFactor = material.iridescence;
2648
+
2649
+ if ( material.iridescenceMap ) {
2650
+
2651
+ const iridescenceMapDef = {
2652
+ index: writer.processTexture( material.iridescenceMap ),
2653
+ texCoord: material.iridescenceMap.channel
2654
+ };
2655
+ writer.applyTextureTransform( iridescenceMapDef, material.iridescenceMap );
2656
+ extensionDef.iridescenceTexture = iridescenceMapDef;
2657
+
2658
+ }
2659
+
2660
+ extensionDef.iridescenceIor = material.iridescenceIOR;
2661
+ extensionDef.iridescenceThicknessMinimum = material.iridescenceThicknessRange[ 0 ];
2662
+ extensionDef.iridescenceThicknessMaximum = material.iridescenceThicknessRange[ 1 ];
2663
+
2664
+ if ( material.iridescenceThicknessMap ) {
2665
+
2666
+ const iridescenceThicknessMapDef = {
2667
+ index: writer.processTexture( material.iridescenceThicknessMap ),
2668
+ texCoord: material.iridescenceThicknessMap.channel
2669
+ };
2670
+ writer.applyTextureTransform( iridescenceThicknessMapDef, material.iridescenceThicknessMap );
2671
+ extensionDef.iridescenceThicknessTexture = iridescenceThicknessMapDef;
2672
+
2673
+ }
2674
+
2675
+ materialDef.extensions = materialDef.extensions || {};
2676
+ materialDef.extensions[ this.name ] = extensionDef;
2677
+
2678
+ extensionsUsed[ this.name ] = true;
2679
+
2680
+ }
2681
+
2682
+ }
2683
+
2684
+ /**
2685
+ * Transmission Materials Extension
2686
+ *
2687
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_transmission
2688
+ */
2689
+ class GLTFMaterialsTransmissionExtension {
2690
+
2691
+ constructor( writer ) {
2692
+
2693
+ this.writer = writer;
2694
+ this.name = 'KHR_materials_transmission';
2695
+
2696
+ }
2697
+
2698
+ writeMaterial( material, materialDef ) {
2699
+
2700
+ if ( ! material.isMeshPhysicalMaterial || material.transmission === 0 ) return;
2701
+
2702
+ const writer = this.writer;
2703
+ const extensionsUsed = writer.extensionsUsed;
2704
+
2705
+ const extensionDef = {};
2706
+
2707
+ extensionDef.transmissionFactor = material.transmission;
2708
+
2709
+ if ( material.transmissionMap ) {
2710
+
2711
+ const transmissionMapDef = {
2712
+ index: writer.processTexture( material.transmissionMap ),
2713
+ texCoord: material.transmissionMap.channel
2714
+ };
2715
+ writer.applyTextureTransform( transmissionMapDef, material.transmissionMap );
2716
+ extensionDef.transmissionTexture = transmissionMapDef;
2717
+
2718
+ }
2719
+
2720
+ materialDef.extensions = materialDef.extensions || {};
2721
+ materialDef.extensions[ this.name ] = extensionDef;
2722
+
2723
+ extensionsUsed[ this.name ] = true;
2724
+
2725
+ }
2726
+
2727
+ }
2728
+
2729
+ /**
2730
+ * Materials Volume Extension
2731
+ *
2732
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_volume
2733
+ */
2734
+ class GLTFMaterialsVolumeExtension {
2735
+
2736
+ constructor( writer ) {
2737
+
2738
+ this.writer = writer;
2739
+ this.name = 'KHR_materials_volume';
2740
+
2741
+ }
2742
+
2743
+ writeMaterial( material, materialDef ) {
2744
+
2745
+ if ( ! material.isMeshPhysicalMaterial || material.transmission === 0 ) return;
2746
+
2747
+ const writer = this.writer;
2748
+ const extensionsUsed = writer.extensionsUsed;
2749
+
2750
+ const extensionDef = {};
2751
+
2752
+ extensionDef.thicknessFactor = material.thickness;
2753
+
2754
+ if ( material.thicknessMap ) {
2755
+
2756
+ const thicknessMapDef = {
2757
+ index: writer.processTexture( material.thicknessMap ),
2758
+ texCoord: material.thicknessMap.channel
2759
+ };
2760
+ writer.applyTextureTransform( thicknessMapDef, material.thicknessMap );
2761
+ extensionDef.thicknessTexture = thicknessMapDef;
2762
+
2763
+ }
2764
+
2765
+ extensionDef.attenuationDistance = material.attenuationDistance;
2766
+ extensionDef.attenuationColor = material.attenuationColor.toArray();
2767
+
2768
+ materialDef.extensions = materialDef.extensions || {};
2769
+ materialDef.extensions[ this.name ] = extensionDef;
2770
+
2771
+ extensionsUsed[ this.name ] = true;
2772
+
2773
+ }
2774
+
2775
+ }
2776
+
2777
+ /**
2778
+ * Materials ior Extension
2779
+ *
2780
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_ior
2781
+ */
2782
+ class GLTFMaterialsIorExtension {
2783
+
2784
+ constructor( writer ) {
2785
+
2786
+ this.writer = writer;
2787
+ this.name = 'KHR_materials_ior';
2788
+
2789
+ }
2790
+
2791
+ writeMaterial( material, materialDef ) {
2792
+
2793
+ if ( ! material.isMeshPhysicalMaterial || material.ior === 1.5 ) return;
2794
+
2795
+ const writer = this.writer;
2796
+ const extensionsUsed = writer.extensionsUsed;
2797
+
2798
+ const extensionDef = {};
2799
+
2800
+ extensionDef.ior = material.ior;
2801
+
2802
+ materialDef.extensions = materialDef.extensions || {};
2803
+ materialDef.extensions[ this.name ] = extensionDef;
2804
+
2805
+ extensionsUsed[ this.name ] = true;
2806
+
2807
+ }
2808
+
2809
+ }
2810
+
2811
+ /**
2812
+ * Materials specular Extension
2813
+ *
2814
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_specular
2815
+ */
2816
+ class GLTFMaterialsSpecularExtension {
2817
+
2818
+ constructor( writer ) {
2819
+
2820
+ this.writer = writer;
2821
+ this.name = 'KHR_materials_specular';
2822
+
2823
+ }
2824
+
2825
+ writeMaterial( material, materialDef ) {
2826
+
2827
+ if ( ! material.isMeshPhysicalMaterial || ( material.specularIntensity === 1.0 &&
2828
+ material.specularColor.equals( DEFAULT_SPECULAR_COLOR ) &&
2829
+ ! material.specularIntensityMap && ! material.specularColorMap ) ) return;
2830
+
2831
+ const writer = this.writer;
2832
+ const extensionsUsed = writer.extensionsUsed;
2833
+
2834
+ const extensionDef = {};
2835
+
2836
+ if ( material.specularIntensityMap ) {
2837
+
2838
+ const specularIntensityMapDef = {
2839
+ index: writer.processTexture( material.specularIntensityMap ),
2840
+ texCoord: material.specularIntensityMap.channel
2841
+ };
2842
+ writer.applyTextureTransform( specularIntensityMapDef, material.specularIntensityMap );
2843
+ extensionDef.specularTexture = specularIntensityMapDef;
2844
+
2845
+ }
2846
+
2847
+ if ( material.specularColorMap ) {
2848
+
2849
+ const specularColorMapDef = {
2850
+ index: writer.processTexture( material.specularColorMap ),
2851
+ texCoord: material.specularColorMap.channel
2852
+ };
2853
+ writer.applyTextureTransform( specularColorMapDef, material.specularColorMap );
2854
+ extensionDef.specularColorTexture = specularColorMapDef;
2855
+
2856
+ }
2857
+
2858
+ extensionDef.specularFactor = material.specularIntensity;
2859
+ extensionDef.specularColorFactor = material.specularColor.toArray();
2860
+
2861
+ materialDef.extensions = materialDef.extensions || {};
2862
+ materialDef.extensions[ this.name ] = extensionDef;
2863
+
2864
+ extensionsUsed[ this.name ] = true;
2865
+
2866
+ }
2867
+
2868
+ }
2869
+
2870
+ /**
2871
+ * Sheen Materials Extension
2872
+ *
2873
+ * Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_sheen
2874
+ */
2875
+ class GLTFMaterialsSheenExtension {
2876
+
2877
+ constructor( writer ) {
2878
+
2879
+ this.writer = writer;
2880
+ this.name = 'KHR_materials_sheen';
2881
+
2882
+ }
2883
+
2884
+ writeMaterial( material, materialDef ) {
2885
+
2886
+ if ( ! material.isMeshPhysicalMaterial || material.sheen == 0.0 ) return;
2887
+
2888
+ const writer = this.writer;
2889
+ const extensionsUsed = writer.extensionsUsed;
2890
+
2891
+ const extensionDef = {};
2892
+
2893
+ if ( material.sheenRoughnessMap ) {
2894
+
2895
+ const sheenRoughnessMapDef = {
2896
+ index: writer.processTexture( material.sheenRoughnessMap ),
2897
+ texCoord: material.sheenRoughnessMap.channel
2898
+ };
2899
+ writer.applyTextureTransform( sheenRoughnessMapDef, material.sheenRoughnessMap );
2900
+ extensionDef.sheenRoughnessTexture = sheenRoughnessMapDef;
2901
+
2902
+ }
2903
+
2904
+ if ( material.sheenColorMap ) {
2905
+
2906
+ const sheenColorMapDef = {
2907
+ index: writer.processTexture( material.sheenColorMap ),
2908
+ texCoord: material.sheenColorMap.channel
2909
+ };
2910
+ writer.applyTextureTransform( sheenColorMapDef, material.sheenColorMap );
2911
+ extensionDef.sheenColorTexture = sheenColorMapDef;
2912
+
2913
+ }
2914
+
2915
+ extensionDef.sheenRoughnessFactor = material.sheenRoughness;
2916
+ extensionDef.sheenColorFactor = material.sheenColor.toArray();
2917
+
2918
+ materialDef.extensions = materialDef.extensions || {};
2919
+ materialDef.extensions[ this.name ] = extensionDef;
2920
+
2921
+ extensionsUsed[ this.name ] = true;
2922
+
2923
+ }
2924
+
2925
+ }
2926
+
2927
+ /**
2928
+ * Anisotropy Materials Extension
2929
+ *
2930
+ * Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_anisotropy
2931
+ */
2932
+ class GLTFMaterialsAnisotropyExtension {
2933
+
2934
+ constructor( writer ) {
2935
+
2936
+ this.writer = writer;
2937
+ this.name = 'KHR_materials_anisotropy';
2938
+
2939
+ }
2940
+
2941
+ writeMaterial( material, materialDef ) {
2942
+
2943
+ if ( ! material.isMeshPhysicalMaterial || material.anisotropy == 0.0 ) return;
2944
+
2945
+ const writer = this.writer;
2946
+ const extensionsUsed = writer.extensionsUsed;
2947
+
2948
+ const extensionDef = {};
2949
+
2950
+ if ( material.anisotropyMap ) {
2951
+
2952
+ const anisotropyMapDef = { index: writer.processTexture( material.anisotropyMap ) };
2953
+ writer.applyTextureTransform( anisotropyMapDef, material.anisotropyMap );
2954
+ extensionDef.anisotropyTexture = anisotropyMapDef;
2955
+
2956
+ }
2957
+
2958
+ extensionDef.anisotropyStrength = material.anisotropy;
2959
+ extensionDef.anisotropyRotation = material.anisotropyRotation;
2960
+
2961
+ materialDef.extensions = materialDef.extensions || {};
2962
+ materialDef.extensions[ this.name ] = extensionDef;
2963
+
2964
+ extensionsUsed[ this.name ] = true;
2965
+
2966
+ }
2967
+
2968
+ }
2969
+
2970
+ /**
2971
+ * Materials Emissive Strength Extension
2972
+ *
2973
+ * Specification: https://github.com/KhronosGroup/glTF/blob/5768b3ce0ef32bc39cdf1bef10b948586635ead3/extensions/2.0/Khronos/KHR_materials_emissive_strength/README.md
2974
+ */
2975
+ class GLTFMaterialsEmissiveStrengthExtension {
2976
+
2977
+ constructor( writer ) {
2978
+
2979
+ this.writer = writer;
2980
+ this.name = 'KHR_materials_emissive_strength';
2981
+
2982
+ }
2983
+
2984
+ writeMaterial( material, materialDef ) {
2985
+
2986
+ if ( ! material.isMeshStandardMaterial || material.emissiveIntensity === 1.0 ) return;
2987
+
2988
+ const writer = this.writer;
2989
+ const extensionsUsed = writer.extensionsUsed;
2990
+
2991
+ const extensionDef = {};
2992
+
2993
+ extensionDef.emissiveStrength = material.emissiveIntensity;
2994
+
2995
+ materialDef.extensions = materialDef.extensions || {};
2996
+ materialDef.extensions[ this.name ] = extensionDef;
2997
+
2998
+ extensionsUsed[ this.name ] = true;
2999
+
3000
+ }
3001
+
3002
+ }
3003
+
3004
+
3005
+ /**
3006
+ * Materials bump Extension
3007
+ *
3008
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/EXT_materials_bump
3009
+ */
3010
+ class GLTFMaterialsBumpExtension {
3011
+
3012
+ constructor( writer ) {
3013
+
3014
+ this.writer = writer;
3015
+ this.name = 'EXT_materials_bump';
3016
+
3017
+ }
3018
+
3019
+ writeMaterial( material, materialDef ) {
3020
+
3021
+ if ( ! material.isMeshStandardMaterial || (
3022
+ material.bumpScale === 1 &&
3023
+ ! material.bumpMap ) ) return;
3024
+
3025
+ const writer = this.writer;
3026
+ const extensionsUsed = writer.extensionsUsed;
3027
+
3028
+ const extensionDef = {};
3029
+
3030
+ if ( material.bumpMap ) {
3031
+
3032
+ const bumpMapDef = {
3033
+ index: writer.processTexture( material.bumpMap ),
3034
+ texCoord: material.bumpMap.channel
3035
+ };
3036
+ writer.applyTextureTransform( bumpMapDef, material.bumpMap );
3037
+ extensionDef.bumpTexture = bumpMapDef;
3038
+
3039
+ }
3040
+
3041
+ extensionDef.bumpFactor = material.bumpScale;
3042
+
3043
+ materialDef.extensions = materialDef.extensions || {};
3044
+ materialDef.extensions[ this.name ] = extensionDef;
3045
+
3046
+ extensionsUsed[ this.name ] = true;
3047
+
3048
+ }
3049
+
3050
+ }
3051
+
3052
+ /**
3053
+ * GPU Instancing Extension
3054
+ *
3055
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_mesh_gpu_instancing
3056
+ */
3057
+ class GLTFMeshGpuInstancing {
3058
+
3059
+ constructor( writer ) {
3060
+
3061
+ this.writer = writer;
3062
+ this.name = 'EXT_mesh_gpu_instancing';
3063
+
3064
+ }
3065
+
3066
+ writeNode( object, nodeDef ) {
3067
+
3068
+ if ( ! object.isInstancedMesh ) return;
3069
+
3070
+ const writer = this.writer;
3071
+
3072
+ const mesh = object;
3073
+
3074
+ const translationAttr = new Float32Array( mesh.count * 3 );
3075
+ const rotationAttr = new Float32Array( mesh.count * 4 );
3076
+ const scaleAttr = new Float32Array( mesh.count * 3 );
3077
+
3078
+ const matrix = new Matrix4();
3079
+ const position = new Vector3();
3080
+ const quaternion = new Quaternion();
3081
+ const scale = new Vector3();
3082
+
3083
+ for ( let i = 0; i < mesh.count; i ++ ) {
3084
+
3085
+ mesh.getMatrixAt( i, matrix );
3086
+ matrix.decompose( position, quaternion, scale );
3087
+
3088
+ position.toArray( translationAttr, i * 3 );
3089
+ quaternion.toArray( rotationAttr, i * 4 );
3090
+ scale.toArray( scaleAttr, i * 3 );
3091
+
3092
+ }
3093
+
3094
+ const attributes = {
3095
+ TRANSLATION: writer.processAccessor( new BufferAttribute( translationAttr, 3 ) ),
3096
+ ROTATION: writer.processAccessor( new BufferAttribute( rotationAttr, 4 ) ),
3097
+ SCALE: writer.processAccessor( new BufferAttribute( scaleAttr, 3 ) ),
3098
+ };
3099
+
3100
+ if ( mesh.instanceColor )
3101
+ attributes._COLOR_0 = writer.processAccessor( mesh.instanceColor );
3102
+
3103
+ nodeDef.extensions = nodeDef.extensions || {};
3104
+ nodeDef.extensions[ this.name ] = { attributes };
3105
+
3106
+ writer.extensionsUsed[ this.name ] = true;
3107
+ writer.extensionsRequired[ this.name ] = true;
3108
+
3109
+ }
3110
+
3111
+ }
3112
+
3113
+ /**
3114
+ * Static utility functions
3115
+ */
3116
+ GLTFExporter.Utils = {
3117
+
3118
+ insertKeyframe: function ( track, time ) {
3119
+
3120
+ const tolerance = 0.001; // 1ms
3121
+ const valueSize = track.getValueSize();
3122
+
3123
+ const times = new track.TimeBufferType( track.times.length + 1 );
3124
+ const values = new track.ValueBufferType( track.values.length + valueSize );
3125
+ const interpolant = track.createInterpolant( new track.ValueBufferType( valueSize ) );
3126
+
3127
+ let index;
3128
+
3129
+ if ( track.times.length === 0 ) {
3130
+
3131
+ times[ 0 ] = time;
3132
+
3133
+ for ( let i = 0; i < valueSize; i ++ ) {
3134
+
3135
+ values[ i ] = 0;
3136
+
3137
+ }
3138
+
3139
+ index = 0;
3140
+
3141
+ } else if ( time < track.times[ 0 ] ) {
3142
+
3143
+ if ( Math.abs( track.times[ 0 ] - time ) < tolerance ) return 0;
3144
+
3145
+ times[ 0 ] = time;
3146
+ times.set( track.times, 1 );
3147
+
3148
+ values.set( interpolant.evaluate( time ), 0 );
3149
+ values.set( track.values, valueSize );
3150
+
3151
+ index = 0;
3152
+
3153
+ } else if ( time > track.times[ track.times.length - 1 ] ) {
3154
+
3155
+ if ( Math.abs( track.times[ track.times.length - 1 ] - time ) < tolerance ) {
3156
+
3157
+ return track.times.length - 1;
3158
+
3159
+ }
3160
+
3161
+ times[ times.length - 1 ] = time;
3162
+ times.set( track.times, 0 );
3163
+
3164
+ values.set( track.values, 0 );
3165
+ values.set( interpolant.evaluate( time ), track.values.length );
3166
+
3167
+ index = times.length - 1;
3168
+
3169
+ } else {
3170
+
3171
+ for ( let i = 0; i < track.times.length; i ++ ) {
3172
+
3173
+ if ( Math.abs( track.times[ i ] - time ) < tolerance ) return i;
3174
+
3175
+ if ( track.times[ i ] < time && track.times[ i + 1 ] > time ) {
3176
+
3177
+ times.set( track.times.slice( 0, i + 1 ), 0 );
3178
+ times[ i + 1 ] = time;
3179
+ times.set( track.times.slice( i + 1 ), i + 2 );
3180
+
3181
+ values.set( track.values.slice( 0, ( i + 1 ) * valueSize ), 0 );
3182
+ values.set( interpolant.evaluate( time ), ( i + 1 ) * valueSize );
3183
+ values.set( track.values.slice( ( i + 1 ) * valueSize ), ( i + 2 ) * valueSize );
3184
+
3185
+ index = i + 1;
3186
+
3187
+ break;
3188
+
3189
+ }
3190
+
3191
+ }
3192
+
3193
+ }
3194
+
3195
+ track.times = times;
3196
+ track.values = values;
3197
+
3198
+ return index;
3199
+
3200
+ },
3201
+
3202
+ mergeMorphTargetTracks: function ( clip, root ) {
3203
+
3204
+ const tracks = [];
3205
+ const mergedTracks = {};
3206
+ const sourceTracks = clip.tracks;
3207
+
3208
+ for ( let i = 0; i < sourceTracks.length; ++ i ) {
3209
+
3210
+ let sourceTrack = sourceTracks[ i ];
3211
+ const sourceTrackBinding = PropertyBinding.parseTrackName( sourceTrack.name );
3212
+ const sourceTrackNode = PropertyBinding.findNode( root, sourceTrackBinding.nodeName );
3213
+
3214
+ if ( sourceTrackBinding.propertyName !== 'morphTargetInfluences' || sourceTrackBinding.propertyIndex === undefined ) {
3215
+
3216
+ // Tracks that don't affect morph targets, or that affect all morph targets together, can be left as-is.
3217
+ tracks.push( sourceTrack );
3218
+ continue;
3219
+
3220
+ }
3221
+
3222
+ if ( sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodDiscrete
3223
+ && sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodLinear ) {
3224
+
3225
+ if ( sourceTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) {
3226
+
3227
+ // This should never happen, because glTF morph target animations
3228
+ // affect all targets already.
3229
+ throw new Error( 'THREE.GLTFExporter: Cannot merge tracks with glTF CUBICSPLINE interpolation.' );
3230
+
3231
+ }
3232
+
3233
+ console.warn( 'THREE.GLTFExporter: Morph target interpolation mode not yet supported. Using LINEAR instead.' );
3234
+
3235
+ sourceTrack = sourceTrack.clone();
3236
+ sourceTrack.setInterpolation( InterpolateLinear );
3237
+
3238
+ }
3239
+
3240
+ const targetCount = sourceTrackNode.morphTargetInfluences.length;
3241
+ const targetIndex = sourceTrackNode.morphTargetDictionary[ sourceTrackBinding.propertyIndex ];
3242
+
3243
+ if ( targetIndex === undefined ) {
3244
+
3245
+ throw new Error( 'THREE.GLTFExporter: Morph target name not found: ' + sourceTrackBinding.propertyIndex );
3246
+
3247
+ }
3248
+
3249
+ let mergedTrack;
3250
+
3251
+ // If this is the first time we've seen this object, create a new
3252
+ // track to store merged keyframe data for each morph target.
3253
+ if ( mergedTracks[ sourceTrackNode.uuid ] === undefined ) {
3254
+
3255
+ mergedTrack = sourceTrack.clone();
3256
+
3257
+ const values = new mergedTrack.ValueBufferType( targetCount * mergedTrack.times.length );
3258
+
3259
+ for ( let j = 0; j < mergedTrack.times.length; j ++ ) {
3260
+
3261
+ values[ j * targetCount + targetIndex ] = mergedTrack.values[ j ];
3262
+
3263
+ }
3264
+
3265
+ // We need to take into consideration the intended target node
3266
+ // of our original un-merged morphTarget animation.
3267
+ mergedTrack.name = ( sourceTrackBinding.nodeName || '' ) + '.morphTargetInfluences';
3268
+ mergedTrack.values = values;
3269
+
3270
+ mergedTracks[ sourceTrackNode.uuid ] = mergedTrack;
3271
+ tracks.push( mergedTrack );
3272
+
3273
+ continue;
3274
+
3275
+ }
3276
+
3277
+ const sourceInterpolant = sourceTrack.createInterpolant( new sourceTrack.ValueBufferType( 1 ) );
3278
+
3279
+ mergedTrack = mergedTracks[ sourceTrackNode.uuid ];
3280
+
3281
+ // For every existing keyframe of the merged track, write a (possibly
3282
+ // interpolated) value from the source track.
3283
+ for ( let j = 0; j < mergedTrack.times.length; j ++ ) {
3284
+
3285
+ mergedTrack.values[ j * targetCount + targetIndex ] = sourceInterpolant.evaluate( mergedTrack.times[ j ] );
3286
+
3287
+ }
3288
+
3289
+ // For every existing keyframe of the source track, write a (possibly
3290
+ // new) keyframe to the merged track. Values from the previous loop may
3291
+ // be written again, but keyframes are de-duplicated.
3292
+ for ( let j = 0; j < sourceTrack.times.length; j ++ ) {
3293
+
3294
+ const keyframeIndex = this.insertKeyframe( mergedTrack, sourceTrack.times[ j ] );
3295
+ mergedTrack.values[ keyframeIndex * targetCount + targetIndex ] = sourceTrack.values[ j ];
3296
+
3297
+ }
3298
+
3299
+ }
3300
+
3301
+ clip.tracks = tracks;
3302
+
3303
+ return clip;
3304
+
3305
+ }
3306
+
3307
+ };
3308
+
3309
+ export { GLTFExporter };