zincjs 1.0.13 → 1.0.15

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 (45) hide show
  1. package/build/zinc.frontend.js +1 -1
  2. package/build/zinc.js +43 -35
  3. package/build/zinc.js.map +1 -1
  4. package/package.json +3 -3
  5. package/src/assets/disc.png +0 -0
  6. package/src/assets/mapMarker.svg +11 -0
  7. package/src/controls.js +1594 -0
  8. package/src/geometryCSG.js +148 -0
  9. package/src/glyphsetCSG.js +84 -0
  10. package/src/loaders/GLTFToZincJSLoader.js +85 -0
  11. package/src/loaders/JSONLoader.js +697 -0
  12. package/src/loaders/OBJLoader.js +911 -0
  13. package/src/loaders/STLLoader.js +399 -0
  14. package/src/loaders/primitivesLoader.js +46 -0
  15. package/src/minimap.js +82 -0
  16. package/src/primitives/augmentShader.js +22 -0
  17. package/src/primitives/geometry.js +109 -0
  18. package/src/primitives/glyph.js +150 -0
  19. package/src/primitives/glyphset.js +657 -0
  20. package/src/primitives/label.js +51 -0
  21. package/src/primitives/lines.js +35 -0
  22. package/src/primitives/marker.js +88 -0
  23. package/src/primitives/pointset.js +53 -0
  24. package/src/primitives/texturePrimitive.js +16 -0
  25. package/src/primitives/textureSlides.js +118 -0
  26. package/src/primitives/zincObject.js +573 -0
  27. package/src/region.js +554 -0
  28. package/src/renderer.js +612 -0
  29. package/src/scene.js +963 -0
  30. package/src/sceneExporter.js +32 -0
  31. package/src/sceneLoader.js +842 -0
  32. package/src/texture/texture.js +57 -0
  33. package/src/texture/textureArray.js +85 -0
  34. package/src/three/GLTFExporter.js +2448 -0
  35. package/src/three/Geometry.js +2084 -0
  36. package/src/three/Loader.js +344 -0
  37. package/src/three/Points.js +223 -0
  38. package/src/three/line/Line.js +293 -0
  39. package/src/three/line/LineSegments.js +65 -0
  40. package/src/three-js-csg.js +564 -0
  41. package/src/utilities.js +321 -0
  42. package/src/videoHandler.js +92 -0
  43. package/src/workers/geometryCSG.worker.js +73 -0
  44. package/src/workers/geometryCSGInternal.js +58 -0
  45. package/src/zinc.js +38 -0
@@ -0,0 +1,2448 @@
1
+ import {
2
+ BufferAttribute,
3
+ ClampToEdgeWrapping,
4
+ DoubleSide,
5
+ InterpolateDiscrete,
6
+ InterpolateLinear,
7
+ LinearFilter,
8
+ LinearMipmapLinearFilter,
9
+ LinearMipmapNearestFilter,
10
+ MathUtils,
11
+ Matrix4,
12
+ MirroredRepeatWrapping,
13
+ NearestFilter,
14
+ NearestMipmapLinearFilter,
15
+ NearestMipmapNearestFilter,
16
+ PropertyBinding,
17
+ RGBAFormat,
18
+ RGBFormat,
19
+ RepeatWrapping,
20
+ Scene,
21
+ Vector3
22
+ } from 'three';
23
+
24
+
25
+ class GLTFExporter {
26
+
27
+ constructor() {
28
+
29
+ this.pluginCallbacks = [];
30
+
31
+ this.register( function ( writer ) {
32
+
33
+ return new GLTFLightExtension( writer );
34
+
35
+ } );
36
+
37
+ this.register( function ( writer ) {
38
+
39
+ return new GLTFMaterialsUnlitExtension( writer );
40
+
41
+ } );
42
+
43
+ this.register( function ( writer ) {
44
+
45
+ return new GLTFMaterialsPBRSpecularGlossiness( writer );
46
+
47
+ } );
48
+
49
+ }
50
+
51
+ register( callback ) {
52
+
53
+ if ( this.pluginCallbacks.indexOf( callback ) === - 1 ) {
54
+
55
+ this.pluginCallbacks.push( callback );
56
+
57
+ }
58
+
59
+ return this;
60
+
61
+ }
62
+
63
+ unregister( callback ) {
64
+
65
+ if ( this.pluginCallbacks.indexOf( callback ) !== - 1 ) {
66
+
67
+ this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 );
68
+
69
+ }
70
+
71
+ return this;
72
+
73
+ }
74
+
75
+ /**
76
+ * Parse scenes and generate GLTF output
77
+ * @param {Scene or [THREE.Scenes]} input Scene or Array of THREE.Scenes
78
+ * @param {Function} onDone Callback on completed
79
+ * @param {Object} options options
80
+ */
81
+ parse( input, onDone, options ) {
82
+
83
+ const writer = new GLTFWriter();
84
+ const plugins = [];
85
+
86
+ for ( let i = 0, il = this.pluginCallbacks.length; i < il; i ++ ) {
87
+
88
+ plugins.push( this.pluginCallbacks[ i ]( writer ) );
89
+
90
+ }
91
+
92
+ writer.setPlugins( plugins );
93
+ writer.write( input, onDone, options );
94
+
95
+ }
96
+
97
+ }
98
+
99
+ //------------------------------------------------------------------------------
100
+ // Constants
101
+ //------------------------------------------------------------------------------
102
+
103
+ const WEBGL_CONSTANTS = {
104
+ POINTS: 0x0000,
105
+ LINES: 0x0001,
106
+ LINE_LOOP: 0x0002,
107
+ LINE_STRIP: 0x0003,
108
+ TRIANGLES: 0x0004,
109
+ TRIANGLE_STRIP: 0x0005,
110
+ TRIANGLE_FAN: 0x0006,
111
+
112
+ UNSIGNED_BYTE: 0x1401,
113
+ UNSIGNED_SHORT: 0x1403,
114
+ FLOAT: 0x1406,
115
+ UNSIGNED_INT: 0x1405,
116
+ ARRAY_BUFFER: 0x8892,
117
+ ELEMENT_ARRAY_BUFFER: 0x8893,
118
+
119
+ NEAREST: 0x2600,
120
+ LINEAR: 0x2601,
121
+ NEAREST_MIPMAP_NEAREST: 0x2700,
122
+ LINEAR_MIPMAP_NEAREST: 0x2701,
123
+ NEAREST_MIPMAP_LINEAR: 0x2702,
124
+ LINEAR_MIPMAP_LINEAR: 0x2703,
125
+
126
+ CLAMP_TO_EDGE: 33071,
127
+ MIRRORED_REPEAT: 33648,
128
+ REPEAT: 10497
129
+ };
130
+
131
+ const THREE_TO_WEBGL = {};
132
+
133
+ THREE_TO_WEBGL[ NearestFilter ] = WEBGL_CONSTANTS.NEAREST;
134
+ THREE_TO_WEBGL[ NearestMipmapNearestFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_NEAREST;
135
+ THREE_TO_WEBGL[ NearestMipmapLinearFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_LINEAR;
136
+ THREE_TO_WEBGL[ LinearFilter ] = WEBGL_CONSTANTS.LINEAR;
137
+ THREE_TO_WEBGL[ LinearMipmapNearestFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_NEAREST;
138
+ THREE_TO_WEBGL[ LinearMipmapLinearFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_LINEAR;
139
+
140
+ THREE_TO_WEBGL[ ClampToEdgeWrapping ] = WEBGL_CONSTANTS.CLAMP_TO_EDGE;
141
+ THREE_TO_WEBGL[ RepeatWrapping ] = WEBGL_CONSTANTS.REPEAT;
142
+ THREE_TO_WEBGL[ MirroredRepeatWrapping ] = WEBGL_CONSTANTS.MIRRORED_REPEAT;
143
+
144
+ const PATH_PROPERTIES = {
145
+ scale: 'scale',
146
+ position: 'translation',
147
+ quaternion: 'rotation',
148
+ morphTargetInfluences: 'weights'
149
+ };
150
+
151
+ // GLB constants
152
+ // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
153
+
154
+ const GLB_HEADER_BYTES = 12;
155
+ const GLB_HEADER_MAGIC = 0x46546C67;
156
+ const GLB_VERSION = 2;
157
+
158
+ const GLB_CHUNK_PREFIX_BYTES = 8;
159
+ const GLB_CHUNK_TYPE_JSON = 0x4E4F534A;
160
+ const GLB_CHUNK_TYPE_BIN = 0x004E4942;
161
+
162
+ //------------------------------------------------------------------------------
163
+ // Utility functions
164
+ //------------------------------------------------------------------------------
165
+
166
+ /**
167
+ * Compare two arrays
168
+ * @param {Array} array1 Array 1 to compare
169
+ * @param {Array} array2 Array 2 to compare
170
+ * @return {Boolean} Returns true if both arrays are equal
171
+ */
172
+ function equalArray( array1, array2 ) {
173
+
174
+ return ( array1.length === array2.length ) && array1.every( function ( element, index ) {
175
+
176
+ return element === array2[ index ];
177
+
178
+ } );
179
+
180
+ }
181
+
182
+ /**
183
+ * Converts a string to an ArrayBuffer.
184
+ * @param {string} text
185
+ * @return {ArrayBuffer}
186
+ */
187
+ function stringToArrayBuffer( text ) {
188
+
189
+ if ( window.TextEncoder !== undefined ) {
190
+
191
+ return new TextEncoder().encode( text ).buffer;
192
+
193
+ }
194
+
195
+ const array = new Uint8Array( new ArrayBuffer( text.length ) );
196
+
197
+ for ( let i = 0, il = text.length; i < il; i ++ ) {
198
+
199
+ const value = text.charCodeAt( i );
200
+
201
+ // Replacing multi-byte character with space(0x20).
202
+ array[ i ] = value > 0xFF ? 0x20 : value;
203
+
204
+ }
205
+
206
+ return array.buffer;
207
+
208
+ }
209
+
210
+ /**
211
+ * Is identity matrix
212
+ *
213
+ * @param {Matrix4} matrix
214
+ * @returns {Boolean} Returns true, if parameter is identity matrix
215
+ */
216
+ function isIdentityMatrix( matrix ) {
217
+
218
+ return equalArray( matrix.elements, [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] );
219
+
220
+ }
221
+
222
+ /**
223
+ * Get the min and max vectors from the given attribute
224
+ * @param {BufferAttribute} attribute Attribute to find the min/max in range from start to start + count
225
+ * @param {Integer} start
226
+ * @param {Integer} count
227
+ * @return {Object} Object containing the `min` and `max` values (As an array of attribute.itemSize components)
228
+ */
229
+ function getMinMax( attribute, start, count ) {
230
+
231
+ const output = {
232
+
233
+ min: new Array( attribute.itemSize ).fill( Number.POSITIVE_INFINITY ),
234
+ max: new Array( attribute.itemSize ).fill( Number.NEGATIVE_INFINITY )
235
+
236
+ };
237
+
238
+ for ( let i = start; i < start + count; i ++ ) {
239
+
240
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
241
+
242
+ let value;
243
+
244
+ if ( attribute.itemSize > 4 ) {
245
+
246
+ // no support for interleaved data for itemSize > 4
247
+
248
+ value = attribute.array[ i * attribute.itemSize + a ];
249
+
250
+ } else {
251
+
252
+ if ( a === 0 ) value = attribute.getX( i );
253
+ else if ( a === 1 ) value = attribute.getY( i );
254
+ else if ( a === 2 ) value = attribute.getZ( i );
255
+ else if ( a === 3 ) value = attribute.getW( i );
256
+
257
+ }
258
+
259
+ output.min[ a ] = Math.min( output.min[ a ], value );
260
+ output.max[ a ] = Math.max( output.max[ a ], value );
261
+
262
+ }
263
+
264
+ }
265
+
266
+ return output;
267
+
268
+ }
269
+
270
+ /**
271
+ * Get the required size + padding for a buffer, rounded to the next 4-byte boundary.
272
+ * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment
273
+ *
274
+ * @param {Integer} bufferSize The size the original buffer.
275
+ * @returns {Integer} new buffer size with required padding.
276
+ *
277
+ */
278
+ function getPaddedBufferSize( bufferSize ) {
279
+
280
+ return Math.ceil( bufferSize / 4 ) * 4;
281
+
282
+ }
283
+
284
+ /**
285
+ * Returns a buffer aligned to 4-byte boundary.
286
+ *
287
+ * @param {ArrayBuffer} arrayBuffer Buffer to pad
288
+ * @param {Integer} paddingByte (Optional)
289
+ * @returns {ArrayBuffer} The same buffer if it's already aligned to 4-byte boundary or a new buffer
290
+ */
291
+ function getPaddedArrayBuffer( arrayBuffer, paddingByte = 0 ) {
292
+
293
+ const paddedLength = getPaddedBufferSize( arrayBuffer.byteLength );
294
+
295
+ if ( paddedLength !== arrayBuffer.byteLength ) {
296
+
297
+ const array = new Uint8Array( paddedLength );
298
+ array.set( new Uint8Array( arrayBuffer ) );
299
+
300
+ if ( paddingByte !== 0 ) {
301
+
302
+ for ( let i = arrayBuffer.byteLength; i < paddedLength; i ++ ) {
303
+
304
+ array[ i ] = paddingByte;
305
+
306
+ }
307
+
308
+ }
309
+
310
+ return array.buffer;
311
+
312
+ }
313
+
314
+ return arrayBuffer;
315
+
316
+ }
317
+
318
+ let cachedCanvas = null;
319
+
320
+ /**
321
+ * Writer
322
+ */
323
+ class GLTFWriter {
324
+
325
+ constructor() {
326
+
327
+ this.plugins = [];
328
+
329
+ this.options = {};
330
+ this.pending = [];
331
+ this.buffers = [];
332
+
333
+ this.byteOffset = 0;
334
+ this.buffers = [];
335
+ this.nodeMap = new Map();
336
+ this.skins = [];
337
+ this.extensionsUsed = {};
338
+
339
+ this.uids = new Map();
340
+ this.uid = 0;
341
+
342
+ this.json = {
343
+ asset: {
344
+ version: '2.0',
345
+ generator: 'THREE.GLTFExporter'
346
+ }
347
+ };
348
+
349
+ this.cache = {
350
+ meshes: new Map(),
351
+ attributes: new Map(),
352
+ attributesNormalized: new Map(),
353
+ materials: new Map(),
354
+ textures: new Map(),
355
+ images: new Map()
356
+ };
357
+
358
+ }
359
+
360
+ setPlugins( plugins ) {
361
+
362
+ this.plugins = plugins;
363
+
364
+ }
365
+
366
+ /**
367
+ * Parse scenes and generate GLTF output
368
+ * @param {Scene or [THREE.Scenes]} input Scene or Array of THREE.Scenes
369
+ * @param {Function} onDone Callback on completed
370
+ * @param {Object} options options
371
+ */
372
+ write( input, onDone, options ) {
373
+
374
+ this.options = Object.assign( {}, {
375
+ // default options
376
+ binary: false,
377
+ trs: false,
378
+ onlyVisible: true,
379
+ truncateDrawRange: true,
380
+ embedImages: true,
381
+ maxTextureSize: Infinity,
382
+ animations: [],
383
+ includeCustomExtensions: false
384
+ }, options );
385
+
386
+ if ( this.options.animations.length > 0 ) {
387
+
388
+ // Only TRS properties, and not matrices, may be targeted by animation.
389
+ this.options.trs = true;
390
+
391
+ }
392
+
393
+ this.processInput( input );
394
+
395
+ const writer = this;
396
+
397
+ Promise.all( this.pending ).then( function () {
398
+
399
+ const buffers = writer.buffers;
400
+ const json = writer.json;
401
+ const options = writer.options;
402
+ const extensionsUsed = writer.extensionsUsed;
403
+
404
+ // Merge buffers.
405
+ const blob = new Blob( buffers, { type: 'application/octet-stream' } );
406
+
407
+ // Declare extensions.
408
+ const extensionsUsedList = Object.keys( extensionsUsed );
409
+
410
+ if ( extensionsUsedList.length > 0 ) json.extensionsUsed = extensionsUsedList;
411
+
412
+ // Update bytelength of the single buffer.
413
+ if ( json.buffers && json.buffers.length > 0 ) json.buffers[ 0 ].byteLength = blob.size;
414
+
415
+ if ( options.binary === true ) {
416
+
417
+ // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
418
+
419
+ const reader = new window.FileReader();
420
+ reader.readAsArrayBuffer( blob );
421
+ reader.onloadend = function () {
422
+
423
+ // Binary chunk.
424
+ const binaryChunk = getPaddedArrayBuffer( reader.result );
425
+ const binaryChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
426
+ binaryChunkPrefix.setUint32( 0, binaryChunk.byteLength, true );
427
+ binaryChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_BIN, true );
428
+
429
+ // JSON chunk.
430
+ const jsonChunk = getPaddedArrayBuffer( stringToArrayBuffer( JSON.stringify( json ) ), 0x20 );
431
+ const jsonChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
432
+ jsonChunkPrefix.setUint32( 0, jsonChunk.byteLength, true );
433
+ jsonChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_JSON, true );
434
+
435
+ // GLB header.
436
+ const header = new ArrayBuffer( GLB_HEADER_BYTES );
437
+ const headerView = new DataView( header );
438
+ headerView.setUint32( 0, GLB_HEADER_MAGIC, true );
439
+ headerView.setUint32( 4, GLB_VERSION, true );
440
+ const totalByteLength = GLB_HEADER_BYTES
441
+ + jsonChunkPrefix.byteLength + jsonChunk.byteLength
442
+ + binaryChunkPrefix.byteLength + binaryChunk.byteLength;
443
+ headerView.setUint32( 8, totalByteLength, true );
444
+
445
+ const glbBlob = new Blob( [
446
+ header,
447
+ jsonChunkPrefix,
448
+ jsonChunk,
449
+ binaryChunkPrefix,
450
+ binaryChunk
451
+ ], { type: 'application/octet-stream' } );
452
+
453
+ const glbReader = new window.FileReader();
454
+ glbReader.readAsArrayBuffer( glbBlob );
455
+ glbReader.onloadend = function () {
456
+
457
+ onDone( glbReader.result );
458
+
459
+ };
460
+
461
+ };
462
+
463
+ } else {
464
+
465
+ if ( json.buffers && json.buffers.length > 0 ) {
466
+
467
+ const reader = new window.FileReader();
468
+ reader.readAsDataURL( blob );
469
+ reader.onloadend = function () {
470
+
471
+ const base64data = reader.result;
472
+ json.buffers[ 0 ].uri = base64data;
473
+ onDone( json );
474
+
475
+ };
476
+
477
+ } else {
478
+
479
+ onDone( json );
480
+
481
+ }
482
+
483
+ }
484
+
485
+ } );
486
+
487
+ }
488
+
489
+ /**
490
+ * Serializes a userData.
491
+ *
492
+ * @param {THREE.Object3D|THREE.Material} object
493
+ * @param {Object} objectDef
494
+ */
495
+ serializeUserData( object, objectDef ) {
496
+
497
+ if (object.userData && object.userData.isZincObject) return;
498
+
499
+ if ( Object.keys( object.userData ).length === 0 ) return;
500
+
501
+ const options = this.options;
502
+ const extensionsUsed = this.extensionsUsed;
503
+
504
+ try {
505
+
506
+ const json = JSON.parse( JSON.stringify( object.userData ) );
507
+
508
+ if ( options.includeCustomExtensions && json.gltfExtensions ) {
509
+
510
+ if ( objectDef.extensions === undefined ) objectDef.extensions = {};
511
+
512
+ for ( const extensionName in json.gltfExtensions ) {
513
+
514
+ objectDef.extensions[ extensionName ] = json.gltfExtensions[ extensionName ];
515
+ extensionsUsed[ extensionName ] = true;
516
+
517
+ }
518
+
519
+ delete json.gltfExtensions;
520
+
521
+ }
522
+
523
+ if ( Object.keys( json ).length > 0 ) objectDef.extras = json;
524
+
525
+ } catch ( error ) {
526
+
527
+ console.warn( 'THREE.GLTFExporter: userData of \'' + object.name + '\' ' +
528
+ 'won\'t be serialized because of JSON.stringify error - ' + error.message );
529
+
530
+ }
531
+
532
+ }
533
+
534
+ /**
535
+ * Assign and return a temporal unique id for an object
536
+ * especially which doesn't have .uuid
537
+ * @param {Object} object
538
+ * @return {Integer}
539
+ */
540
+ getUID( object ) {
541
+
542
+ if ( ! this.uids.has( object ) ) this.uids.set( object, this.uid ++ );
543
+
544
+ return this.uids.get( object );
545
+
546
+ }
547
+
548
+ /**
549
+ * Checks if normal attribute values are normalized.
550
+ *
551
+ * @param {BufferAttribute} normal
552
+ * @returns {Boolean}
553
+ */
554
+ isNormalizedNormalAttribute( normal ) {
555
+
556
+ const cache = this.cache;
557
+
558
+ if ( cache.attributesNormalized.has( normal ) ) return false;
559
+
560
+ const v = new Vector3();
561
+
562
+ for ( let i = 0, il = normal.count; i < il; i ++ ) {
563
+
564
+ // 0.0005 is from glTF-validator
565
+ if ( Math.abs( v.fromBufferAttribute( normal, i ).length() - 1.0 ) > 0.0005 ) return false;
566
+
567
+ }
568
+
569
+ return true;
570
+
571
+ }
572
+
573
+ /**
574
+ * Creates normalized normal buffer attribute.
575
+ *
576
+ * @param {BufferAttribute} normal
577
+ * @returns {BufferAttribute}
578
+ *
579
+ */
580
+ createNormalizedNormalAttribute( normal ) {
581
+
582
+ const cache = this.cache;
583
+
584
+ if ( cache.attributesNormalized.has( normal ) ) return cache.attributesNormalized.get( normal );
585
+
586
+ const attribute = normal.clone();
587
+ const v = new Vector3();
588
+
589
+ for ( let i = 0, il = attribute.count; i < il; i ++ ) {
590
+
591
+ v.fromBufferAttribute( attribute, i );
592
+
593
+ if ( v.x === 0 && v.y === 0 && v.z === 0 ) {
594
+
595
+ // if values can't be normalized set (1, 0, 0)
596
+ v.setX( 1.0 );
597
+
598
+ } else {
599
+
600
+ v.normalize();
601
+
602
+ }
603
+
604
+ attribute.setXYZ( i, v.x, v.y, v.z );
605
+
606
+ }
607
+
608
+ cache.attributesNormalized.set( normal, attribute );
609
+
610
+ return attribute;
611
+
612
+ }
613
+
614
+ /**
615
+ * Applies a texture transform, if present, to the map definition. Requires
616
+ * the KHR_texture_transform extension.
617
+ *
618
+ * @param {Object} mapDef
619
+ * @param {THREE.Texture} texture
620
+ */
621
+ applyTextureTransform( mapDef, texture ) {
622
+
623
+ let didTransform = false;
624
+ const transformDef = {};
625
+
626
+ if ( texture.offset.x !== 0 || texture.offset.y !== 0 ) {
627
+
628
+ transformDef.offset = texture.offset.toArray();
629
+ didTransform = true;
630
+
631
+ }
632
+
633
+ if ( texture.rotation !== 0 ) {
634
+
635
+ transformDef.rotation = texture.rotation;
636
+ didTransform = true;
637
+
638
+ }
639
+
640
+ if ( texture.repeat.x !== 1 || texture.repeat.y !== 1 ) {
641
+
642
+ transformDef.scale = texture.repeat.toArray();
643
+ didTransform = true;
644
+
645
+ }
646
+
647
+ if ( didTransform ) {
648
+
649
+ mapDef.extensions = mapDef.extensions || {};
650
+ mapDef.extensions[ 'KHR_texture_transform' ] = transformDef;
651
+ this.extensionsUsed[ 'KHR_texture_transform' ] = true;
652
+
653
+ }
654
+
655
+ }
656
+
657
+ /**
658
+ * Process a buffer to append to the default one.
659
+ * @param {ArrayBuffer} buffer
660
+ * @return {Integer}
661
+ */
662
+ processBuffer( buffer ) {
663
+
664
+ const json = this.json;
665
+ const buffers = this.buffers;
666
+
667
+ if ( ! json.buffers ) json.buffers = [ { byteLength: 0 } ];
668
+
669
+ // All buffers are merged before export.
670
+ buffers.push( buffer );
671
+
672
+ return 0;
673
+
674
+ }
675
+
676
+ /**
677
+ * Process and generate a BufferView
678
+ * @param {BufferAttribute} attribute
679
+ * @param {number} componentType
680
+ * @param {number} start
681
+ * @param {number} count
682
+ * @param {number} target (Optional) Target usage of the BufferView
683
+ * @return {Object}
684
+ */
685
+ processBufferView( attribute, componentType, start, count, target ) {
686
+
687
+ const json = this.json;
688
+
689
+ if ( ! json.bufferViews ) json.bufferViews = [];
690
+
691
+ // Create a new dataview and dump the attribute's array into it
692
+
693
+ let componentSize;
694
+
695
+ if ( componentType === WEBGL_CONSTANTS.UNSIGNED_BYTE ) {
696
+
697
+ componentSize = 1;
698
+
699
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
700
+
701
+ componentSize = 2;
702
+
703
+ } else {
704
+
705
+ componentSize = 4;
706
+
707
+ }
708
+
709
+ const byteLength = getPaddedBufferSize( count * attribute.itemSize * componentSize );
710
+ const dataView = new DataView( new ArrayBuffer( byteLength ) );
711
+ let offset = 0;
712
+
713
+ for ( let i = start; i < start + count; i ++ ) {
714
+
715
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
716
+
717
+ let value;
718
+
719
+ if ( attribute.itemSize > 4 ) {
720
+
721
+ // no support for interleaved data for itemSize > 4
722
+
723
+ value = attribute.array[ i * attribute.itemSize + a ];
724
+
725
+ } else {
726
+
727
+ if ( a === 0 ) value = attribute.getX( i );
728
+ else if ( a === 1 ) value = attribute.getY( i );
729
+ else if ( a === 2 ) value = attribute.getZ( i );
730
+ else if ( a === 3 ) value = attribute.getW( i );
731
+
732
+ }
733
+
734
+ if ( componentType === WEBGL_CONSTANTS.FLOAT ) {
735
+
736
+ dataView.setFloat32( offset, value, true );
737
+
738
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_INT ) {
739
+
740
+ dataView.setUint32( offset, value, true );
741
+
742
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
743
+
744
+ dataView.setUint16( offset, value, true );
745
+
746
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_BYTE ) {
747
+
748
+ dataView.setUint8( offset, value );
749
+
750
+ }
751
+
752
+ offset += componentSize;
753
+
754
+ }
755
+
756
+ }
757
+
758
+ const bufferViewDef = {
759
+
760
+ buffer: this.processBuffer( dataView.buffer ),
761
+ byteOffset: this.byteOffset,
762
+ byteLength: byteLength
763
+
764
+ };
765
+
766
+ if ( target !== undefined ) bufferViewDef.target = target;
767
+
768
+ if ( target === WEBGL_CONSTANTS.ARRAY_BUFFER ) {
769
+
770
+ // Only define byteStride for vertex attributes.
771
+ bufferViewDef.byteStride = attribute.itemSize * componentSize;
772
+
773
+ }
774
+
775
+ this.byteOffset += byteLength;
776
+
777
+ json.bufferViews.push( bufferViewDef );
778
+
779
+ // @TODO Merge bufferViews where possible.
780
+ const output = {
781
+
782
+ id: json.bufferViews.length - 1,
783
+ byteLength: 0
784
+
785
+ };
786
+
787
+ return output;
788
+
789
+ }
790
+
791
+ /**
792
+ * Process and generate a BufferView from an image Blob.
793
+ * @param {Blob} blob
794
+ * @return {Promise<Integer>}
795
+ */
796
+ processBufferViewImage( blob ) {
797
+
798
+ const writer = this;
799
+ const json = writer.json;
800
+
801
+ if ( ! json.bufferViews ) json.bufferViews = [];
802
+
803
+ return new Promise( function ( resolve ) {
804
+
805
+ const reader = new window.FileReader();
806
+ reader.readAsArrayBuffer( blob );
807
+ reader.onloadend = function () {
808
+
809
+ const buffer = getPaddedArrayBuffer( reader.result );
810
+
811
+ const bufferViewDef = {
812
+ buffer: writer.processBuffer( buffer ),
813
+ byteOffset: writer.byteOffset,
814
+ byteLength: buffer.byteLength
815
+ };
816
+
817
+ writer.byteOffset += buffer.byteLength;
818
+ resolve( json.bufferViews.push( bufferViewDef ) - 1 );
819
+
820
+ };
821
+
822
+ } );
823
+
824
+ }
825
+
826
+ /**
827
+ * Process attribute to generate an accessor
828
+ * @param {BufferAttribute} attribute Attribute to process
829
+ * @param {THREE.BufferGeometry} geometry (Optional) Geometry used for truncated draw range
830
+ * @param {Integer} start (Optional)
831
+ * @param {Integer} count (Optional)
832
+ * @return {Integer|null} Index of the processed accessor on the "accessors" array
833
+ */
834
+ processAccessor( attribute, geometry, start, count ) {
835
+
836
+ const options = this.options;
837
+ const json = this.json;
838
+
839
+ const types = {
840
+
841
+ 1: 'SCALAR',
842
+ 2: 'VEC2',
843
+ 3: 'VEC3',
844
+ 4: 'VEC4',
845
+ 16: 'MAT4'
846
+
847
+ };
848
+
849
+ let componentType;
850
+
851
+ // Detect the component type of the attribute array (float, uint or ushort)
852
+ if ( attribute.array.constructor === Float32Array ) {
853
+
854
+ componentType = WEBGL_CONSTANTS.FLOAT;
855
+
856
+ } else if ( attribute.array.constructor === Uint32Array ) {
857
+
858
+ componentType = WEBGL_CONSTANTS.UNSIGNED_INT;
859
+
860
+ } else if ( attribute.array.constructor === Uint16Array ) {
861
+
862
+ componentType = WEBGL_CONSTANTS.UNSIGNED_SHORT;
863
+
864
+ } else if ( attribute.array.constructor === Uint8Array ) {
865
+
866
+ componentType = WEBGL_CONSTANTS.UNSIGNED_BYTE;
867
+
868
+ } else {
869
+
870
+ throw new Error( 'THREE.GLTFExporter: Unsupported bufferAttribute component type.' );
871
+
872
+ }
873
+
874
+ if ( start === undefined ) start = 0;
875
+ if ( count === undefined ) count = attribute.count;
876
+
877
+ // @TODO Indexed buffer geometry with drawRange not supported yet
878
+ if ( options.truncateDrawRange && geometry !== undefined && geometry.index === null ) {
879
+
880
+ const end = start + count;
881
+ const end2 = geometry.drawRange.count === Infinity
882
+ ? attribute.count
883
+ : geometry.drawRange.start + geometry.drawRange.count;
884
+
885
+ start = Math.max( start, geometry.drawRange.start );
886
+ count = Math.min( end, end2 ) - start;
887
+
888
+ if ( count < 0 ) count = 0;
889
+
890
+ }
891
+
892
+ // Skip creating an accessor if the attribute doesn't have data to export
893
+ if ( count === 0 ) return null;
894
+
895
+ const minMax = getMinMax( attribute, start, count );
896
+ let bufferViewTarget;
897
+
898
+ // If geometry isn't provided, don't infer the target usage of the bufferView. For
899
+ // animation samplers, target must not be set.
900
+ if ( geometry !== undefined ) {
901
+
902
+ bufferViewTarget = attribute === geometry.index ? WEBGL_CONSTANTS.ELEMENT_ARRAY_BUFFER : WEBGL_CONSTANTS.ARRAY_BUFFER;
903
+
904
+ }
905
+
906
+ const bufferView = this.processBufferView( attribute, componentType, start, count, bufferViewTarget );
907
+
908
+ const accessorDef = {
909
+
910
+ bufferView: bufferView.id,
911
+ byteOffset: bufferView.byteOffset,
912
+ componentType: componentType,
913
+ count: count,
914
+ max: minMax.max,
915
+ min: minMax.min,
916
+ type: types[ attribute.itemSize ]
917
+
918
+ };
919
+
920
+ if ( attribute.normalized === true ) accessorDef.normalized = true;
921
+ if ( ! json.accessors ) json.accessors = [];
922
+
923
+ return json.accessors.push( accessorDef ) - 1;
924
+
925
+ }
926
+
927
+ /**
928
+ * Process image
929
+ * @param {Image} image to process
930
+ * @param {Integer} format of the image (e.g. RGBFormat, RGBAFormat etc)
931
+ * @param {Boolean} flipY before writing out the image
932
+ * @return {Integer} Index of the processed texture in the "images" array
933
+ */
934
+ processImage( image, format, flipY ) {
935
+
936
+ const writer = this;
937
+ const cache = writer.cache;
938
+ const json = writer.json;
939
+ const options = writer.options;
940
+ const pending = writer.pending;
941
+
942
+ if ( ! cache.images.has( image ) ) cache.images.set( image, {} );
943
+
944
+ const cachedImages = cache.images.get( image );
945
+ const mimeType = format === RGBAFormat ? 'image/png' : 'image/jpeg';
946
+ const key = mimeType + ':flipY/' + flipY.toString();
947
+
948
+ if ( cachedImages[ key ] !== undefined ) return cachedImages[ key ];
949
+
950
+ if ( ! json.images ) json.images = [];
951
+
952
+ const imageDef = { mimeType: mimeType };
953
+
954
+ if ( options.embedImages ) {
955
+
956
+ const canvas = cachedCanvas = cachedCanvas || document.createElement( 'canvas' );
957
+
958
+ canvas.width = Math.min( image.width, options.maxTextureSize );
959
+ canvas.height = Math.min( image.height, options.maxTextureSize );
960
+
961
+ const ctx = canvas.getContext( '2d' );
962
+
963
+ if ( flipY === true ) {
964
+
965
+ ctx.translate( 0, canvas.height );
966
+ ctx.scale( 1, - 1 );
967
+
968
+ }
969
+
970
+ if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
971
+ ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
972
+ ( typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas ) ||
973
+ ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
974
+
975
+ ctx.drawImage( image, 0, 0, canvas.width, canvas.height );
976
+
977
+ } else {
978
+
979
+ if ( format !== RGBAFormat && format !== RGBFormat ) {
980
+
981
+ console.error( 'GLTFExporter: Only RGB and RGBA formats are supported.' );
982
+
983
+ }
984
+
985
+ if ( image.width > options.maxTextureSize || image.height > options.maxTextureSize ) {
986
+
987
+ console.warn( 'GLTFExporter: Image size is bigger than maxTextureSize', image );
988
+
989
+ }
990
+
991
+ const data = new Uint8ClampedArray( image.height * image.width * 4 );
992
+
993
+ if ( format === RGBAFormat ) {
994
+
995
+ for ( let i = 0; i < data.length; i += 4 ) {
996
+
997
+ data[ i + 0 ] = image.data[ i + 0 ];
998
+ data[ i + 1 ] = image.data[ i + 1 ];
999
+ data[ i + 2 ] = image.data[ i + 2 ];
1000
+ data[ i + 3 ] = image.data[ i + 3 ];
1001
+
1002
+ }
1003
+
1004
+ } else {
1005
+
1006
+ for ( let i = 0, j = 0; i < data.length; i += 4, j += 3 ) {
1007
+
1008
+ data[ i + 0 ] = image.data[ j + 0 ];
1009
+ data[ i + 1 ] = image.data[ j + 1 ];
1010
+ data[ i + 2 ] = image.data[ j + 2 ];
1011
+ data[ i + 3 ] = 255;
1012
+
1013
+ }
1014
+
1015
+ }
1016
+
1017
+ ctx.putImageData( new ImageData( data, image.width, image.height ), 0, 0 );
1018
+
1019
+ }
1020
+
1021
+ if ( options.binary === true ) {
1022
+
1023
+ pending.push( new Promise( function ( resolve ) {
1024
+
1025
+ canvas.toBlob( function ( blob ) {
1026
+
1027
+ writer.processBufferViewImage( blob ).then( function ( bufferViewIndex ) {
1028
+
1029
+ imageDef.bufferView = bufferViewIndex;
1030
+ resolve();
1031
+
1032
+ } );
1033
+
1034
+ }, mimeType );
1035
+
1036
+ } ) );
1037
+
1038
+ } else {
1039
+
1040
+ imageDef.uri = canvas.toDataURL( mimeType );
1041
+
1042
+ }
1043
+
1044
+ } else {
1045
+
1046
+ imageDef.uri = image.src;
1047
+
1048
+ }
1049
+
1050
+ const index = json.images.push( imageDef ) - 1;
1051
+ cachedImages[ key ] = index;
1052
+ return index;
1053
+
1054
+ }
1055
+
1056
+ /**
1057
+ * Process sampler
1058
+ * @param {Texture} map Texture to process
1059
+ * @return {Integer} Index of the processed texture in the "samplers" array
1060
+ */
1061
+ processSampler( map ) {
1062
+
1063
+ const json = this.json;
1064
+
1065
+ if ( ! json.samplers ) json.samplers = [];
1066
+
1067
+ const samplerDef = {
1068
+ magFilter: THREE_TO_WEBGL[ map.magFilter ],
1069
+ minFilter: THREE_TO_WEBGL[ map.minFilter ],
1070
+ wrapS: THREE_TO_WEBGL[ map.wrapS ],
1071
+ wrapT: THREE_TO_WEBGL[ map.wrapT ]
1072
+ };
1073
+
1074
+ return json.samplers.push( samplerDef ) - 1;
1075
+
1076
+ }
1077
+
1078
+ /**
1079
+ * Process texture
1080
+ * @param {Texture} map Map to process
1081
+ * @return {Integer} Index of the processed texture in the "textures" array
1082
+ */
1083
+ processTexture( map ) {
1084
+
1085
+ const cache = this.cache;
1086
+ const json = this.json;
1087
+
1088
+ if ( cache.textures.has( map ) ) return cache.textures.get( map );
1089
+
1090
+ if ( ! json.textures ) json.textures = [];
1091
+
1092
+ const textureDef = {
1093
+ sampler: this.processSampler( map ),
1094
+ source: this.processImage( map.image, map.format, map.flipY )
1095
+ };
1096
+
1097
+ if ( map.name ) textureDef.name = map.name;
1098
+
1099
+ this._invokeAll( function ( ext ) {
1100
+
1101
+ ext.writeTexture && ext.writeTexture( map, textureDef );
1102
+
1103
+ } );
1104
+
1105
+ const index = json.textures.push( textureDef ) - 1;
1106
+ cache.textures.set( map, index );
1107
+ return index;
1108
+
1109
+ }
1110
+
1111
+ /**
1112
+ * Process material
1113
+ * @param {THREE.Material} material Material to process
1114
+ * @return {Integer|null} Index of the processed material in the "materials" array
1115
+ */
1116
+ processMaterial( material ) {
1117
+
1118
+ const cache = this.cache;
1119
+ const json = this.json;
1120
+
1121
+ if ( cache.materials.has( material ) ) return cache.materials.get( material );
1122
+
1123
+ if ( material.isShaderMaterial ) {
1124
+
1125
+ console.warn( 'GLTFExporter: THREE.ShaderMaterial not supported.' );
1126
+ return null;
1127
+
1128
+ }
1129
+
1130
+ if ( ! json.materials ) json.materials = [];
1131
+
1132
+ // @QUESTION Should we avoid including any attribute that has the default value?
1133
+ const materialDef = { pbrMetallicRoughness: {} };
1134
+
1135
+ if ( material.isMeshStandardMaterial !== true && material.isMeshBasicMaterial !== true ) {
1136
+
1137
+ console.warn( 'GLTFExporter: Use MeshStandardMaterial or MeshBasicMaterial for best results.' );
1138
+
1139
+ }
1140
+
1141
+ // pbrMetallicRoughness.baseColorFactor
1142
+ const color = material.color.toArray().concat( [ material.opacity ] );
1143
+
1144
+ if ( ! equalArray( color, [ 1, 1, 1, 1 ] ) ) {
1145
+
1146
+ materialDef.pbrMetallicRoughness.baseColorFactor = color;
1147
+
1148
+ }
1149
+
1150
+ if ( material.isMeshStandardMaterial ) {
1151
+
1152
+ materialDef.pbrMetallicRoughness.metallicFactor = material.metalness;
1153
+ materialDef.pbrMetallicRoughness.roughnessFactor = material.roughness;
1154
+
1155
+ } else {
1156
+
1157
+ materialDef.pbrMetallicRoughness.metallicFactor = 0.5;
1158
+ materialDef.pbrMetallicRoughness.roughnessFactor = 0.5;
1159
+
1160
+ }
1161
+
1162
+ // pbrMetallicRoughness.metallicRoughnessTexture
1163
+ if ( material.metalnessMap || material.roughnessMap ) {
1164
+
1165
+ if ( material.metalnessMap === material.roughnessMap ) {
1166
+
1167
+ const metalRoughMapDef = { index: this.processTexture( material.metalnessMap ) };
1168
+ this.applyTextureTransform( metalRoughMapDef, material.metalnessMap );
1169
+ materialDef.pbrMetallicRoughness.metallicRoughnessTexture = metalRoughMapDef;
1170
+
1171
+ } else {
1172
+
1173
+ console.warn( 'THREE.GLTFExporter: Ignoring metalnessMap and roughnessMap because they are not the same Texture.' );
1174
+
1175
+ }
1176
+
1177
+ }
1178
+
1179
+ // pbrMetallicRoughness.baseColorTexture or pbrSpecularGlossiness diffuseTexture
1180
+ if ( material.map ) {
1181
+
1182
+ const baseColorMapDef = { index: this.processTexture( material.map ) };
1183
+ this.applyTextureTransform( baseColorMapDef, material.map );
1184
+ materialDef.pbrMetallicRoughness.baseColorTexture = baseColorMapDef;
1185
+
1186
+ }
1187
+
1188
+ if ( material.emissive ) {
1189
+
1190
+ // note: emissive components are limited to stay within the 0 - 1 range to accommodate glTF spec. see #21849 and #22000.
1191
+ const emissive = material.emissive.clone().multiplyScalar( material.emissiveIntensity );
1192
+ const maxEmissiveComponent = Math.max( emissive.r, emissive.g, emissive.b );
1193
+
1194
+ if ( maxEmissiveComponent > 1 ) {
1195
+
1196
+ emissive.multiplyScalar( 1 / maxEmissiveComponent );
1197
+
1198
+ console.warn( 'THREE.GLTFExporter: Some emissive components exceed 1; emissive has been limited' );
1199
+
1200
+ }
1201
+
1202
+ if ( maxEmissiveComponent > 0 ) {
1203
+
1204
+ materialDef.emissiveFactor = emissive.toArray();
1205
+
1206
+ }
1207
+
1208
+ // emissiveTexture
1209
+ if ( material.emissiveMap ) {
1210
+
1211
+ const emissiveMapDef = { index: this.processTexture( material.emissiveMap ) };
1212
+ this.applyTextureTransform( emissiveMapDef, material.emissiveMap );
1213
+ materialDef.emissiveTexture = emissiveMapDef;
1214
+
1215
+ }
1216
+
1217
+ }
1218
+
1219
+ // normalTexture
1220
+ if ( material.normalMap ) {
1221
+
1222
+ const normalMapDef = { index: this.processTexture( material.normalMap ) };
1223
+
1224
+ if ( material.normalScale && material.normalScale.x !== - 1 ) {
1225
+
1226
+ if ( material.normalScale.x !== material.normalScale.y ) {
1227
+
1228
+ console.warn( 'THREE.GLTFExporter: Normal scale components are different, ignoring Y and exporting X.' );
1229
+
1230
+ }
1231
+
1232
+ normalMapDef.scale = material.normalScale.x;
1233
+
1234
+ }
1235
+
1236
+ this.applyTextureTransform( normalMapDef, material.normalMap );
1237
+ materialDef.normalTexture = normalMapDef;
1238
+
1239
+ }
1240
+
1241
+ // occlusionTexture
1242
+ if ( material.aoMap ) {
1243
+
1244
+ const occlusionMapDef = {
1245
+ index: this.processTexture( material.aoMap ),
1246
+ texCoord: 1
1247
+ };
1248
+
1249
+ if ( material.aoMapIntensity !== 1.0 ) {
1250
+
1251
+ occlusionMapDef.strength = material.aoMapIntensity;
1252
+
1253
+ }
1254
+
1255
+ this.applyTextureTransform( occlusionMapDef, material.aoMap );
1256
+ materialDef.occlusionTexture = occlusionMapDef;
1257
+
1258
+ }
1259
+
1260
+ // alphaMode
1261
+ if ( material.transparent ) {
1262
+
1263
+ materialDef.alphaMode = 'BLEND';
1264
+
1265
+ } else {
1266
+
1267
+ if ( material.alphaTest > 0.0 ) {
1268
+
1269
+ materialDef.alphaMode = 'MASK';
1270
+ materialDef.alphaCutoff = material.alphaTest;
1271
+
1272
+ }
1273
+
1274
+ }
1275
+
1276
+ // doubleSided
1277
+ if ( material.side === DoubleSide ) materialDef.doubleSided = true;
1278
+ if ( material.name !== '' ) materialDef.name = material.name;
1279
+
1280
+ this.serializeUserData( material, materialDef );
1281
+
1282
+ this._invokeAll( function ( ext ) {
1283
+
1284
+ ext.writeMaterial && ext.writeMaterial( material, materialDef );
1285
+
1286
+ } );
1287
+
1288
+ const index = json.materials.push( materialDef ) - 1;
1289
+ cache.materials.set( material, index );
1290
+ return index;
1291
+
1292
+ }
1293
+
1294
+ /**
1295
+ * Process mesh
1296
+ * @param {THREE.Mesh} mesh Mesh to process
1297
+ * @return {Integer|null} Index of the processed mesh in the "meshes" array
1298
+ */
1299
+ processMesh( mesh ) {
1300
+
1301
+ const cache = this.cache;
1302
+ const json = this.json;
1303
+
1304
+ const meshCacheKeyParts = [ mesh.geometry.uuid ];
1305
+
1306
+ if ( Array.isArray( mesh.material ) ) {
1307
+
1308
+ for ( let i = 0, l = mesh.material.length; i < l; i ++ ) {
1309
+
1310
+ meshCacheKeyParts.push( mesh.material[ i ].uuid );
1311
+
1312
+ }
1313
+
1314
+ } else {
1315
+
1316
+ meshCacheKeyParts.push( mesh.material.uuid );
1317
+
1318
+ }
1319
+
1320
+ const meshCacheKey = meshCacheKeyParts.join( ':' );
1321
+
1322
+ if ( cache.meshes.has( meshCacheKey ) ) return cache.meshes.get( meshCacheKey );
1323
+
1324
+ const geometry = mesh.geometry;
1325
+ let mode;
1326
+
1327
+ // Use the correct mode
1328
+ if ( mesh.isLineSegments ) {
1329
+
1330
+ mode = WEBGL_CONSTANTS.LINES;
1331
+
1332
+ } else if ( mesh.isLineLoop ) {
1333
+
1334
+ mode = WEBGL_CONSTANTS.LINE_LOOP;
1335
+
1336
+ } else if ( mesh.isLine ) {
1337
+
1338
+ mode = WEBGL_CONSTANTS.LINE_STRIP;
1339
+
1340
+ } else if ( mesh.isPoints ) {
1341
+
1342
+ mode = WEBGL_CONSTANTS.POINTS;
1343
+
1344
+ } else {
1345
+
1346
+ mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINES : WEBGL_CONSTANTS.TRIANGLES;
1347
+
1348
+ }
1349
+
1350
+ if ( geometry.isBufferGeometry !== true ) {
1351
+
1352
+ throw new Error( 'THREE.GLTFExporter: Geometry is not of type THREE.BufferGeometry.' );
1353
+
1354
+ }
1355
+
1356
+ const meshDef = {};
1357
+ const attributes = {};
1358
+ const primitives = [];
1359
+ const targets = [];
1360
+
1361
+ // Conversion between attributes names in threejs and gltf spec
1362
+ const nameConversion = {
1363
+ uv: 'TEXCOORD_0',
1364
+ uv2: 'TEXCOORD_1',
1365
+ color: 'COLOR_0',
1366
+ skinWeight: 'WEIGHTS_0',
1367
+ skinIndex: 'JOINTS_0'
1368
+ };
1369
+
1370
+ const originalNormal = geometry.getAttribute( 'normal' );
1371
+
1372
+ if ( originalNormal !== undefined && ! this.isNormalizedNormalAttribute( originalNormal ) ) {
1373
+
1374
+ console.warn( 'THREE.GLTFExporter: Creating normalized normal attribute from the non-normalized one.' );
1375
+
1376
+ geometry.setAttribute( 'normal', this.createNormalizedNormalAttribute( originalNormal ) );
1377
+
1378
+ }
1379
+
1380
+ // @QUESTION Detect if .vertexColors = true?
1381
+ // For every attribute create an accessor
1382
+ let modifiedAttribute = null;
1383
+
1384
+ for ( let attributeName in geometry.attributes ) {
1385
+
1386
+ // Ignore morph target attributes, which are exported later.
1387
+ if ( attributeName.substr( 0, 5 ) === 'morph' ) continue;
1388
+
1389
+ const attribute = geometry.attributes[ attributeName ];
1390
+ attributeName = nameConversion[ attributeName ] || attributeName.toUpperCase();
1391
+
1392
+ // Prefix all geometry attributes except the ones specifically
1393
+ // listed in the spec; non-spec attributes are considered custom.
1394
+ const validVertexAttributes =
1395
+ /^(POSITION|NORMAL|TANGENT|TEXCOORD_\d+|COLOR_\d+|JOINTS_\d+|WEIGHTS_\d+)$/;
1396
+
1397
+ if ( ! validVertexAttributes.test( attributeName ) ) attributeName = '_' + attributeName;
1398
+
1399
+ if ( cache.attributes.has( this.getUID( attribute ) ) ) {
1400
+
1401
+ attributes[ attributeName ] = cache.attributes.get( this.getUID( attribute ) );
1402
+ continue;
1403
+
1404
+ }
1405
+
1406
+ // JOINTS_0 must be UNSIGNED_BYTE or UNSIGNED_SHORT.
1407
+ modifiedAttribute = null;
1408
+ const array = attribute.array;
1409
+
1410
+ if ( attributeName === 'JOINTS_0' &&
1411
+ ! ( array instanceof Uint16Array ) &&
1412
+ ! ( array instanceof Uint8Array ) ) {
1413
+
1414
+ console.warn( 'GLTFExporter: Attribute "skinIndex" converted to type UNSIGNED_SHORT.' );
1415
+ modifiedAttribute = new BufferAttribute( new Uint16Array( array ), attribute.itemSize, attribute.normalized );
1416
+
1417
+ }
1418
+
1419
+ const accessor = this.processAccessor( modifiedAttribute || attribute, geometry );
1420
+
1421
+ if ( accessor !== null ) {
1422
+
1423
+ attributes[ attributeName ] = accessor;
1424
+ cache.attributes.set( this.getUID( attribute ), accessor );
1425
+
1426
+ }
1427
+
1428
+ }
1429
+
1430
+ if ( originalNormal !== undefined ) geometry.setAttribute( 'normal', originalNormal );
1431
+
1432
+ // Skip if no exportable attributes found
1433
+ if ( Object.keys( attributes ).length === 0 ) return null;
1434
+
1435
+ // Morph targets
1436
+ if ( mesh.morphTargetInfluences !== undefined && mesh.morphTargetInfluences.length > 0 ) {
1437
+
1438
+ const weights = [];
1439
+ const targetNames = [];
1440
+ const reverseDictionary = {};
1441
+
1442
+ if ( mesh.morphTargetDictionary !== undefined ) {
1443
+
1444
+ for ( const key in mesh.morphTargetDictionary ) {
1445
+
1446
+ reverseDictionary[ mesh.morphTargetDictionary[ key ] ] = key;
1447
+
1448
+ }
1449
+
1450
+ }
1451
+
1452
+ for ( let i = 0; i < mesh.morphTargetInfluences.length; ++ i ) {
1453
+
1454
+ const target = {};
1455
+ let warned = false;
1456
+
1457
+ for ( const attributeName in geometry.morphAttributes ) {
1458
+
1459
+
1460
+ // glTF 2.0 morph supports only POSITION/NORMAL/TANGENT.
1461
+ // Three.js doesn't support TANGENT yet.
1462
+
1463
+ if ( attributeName !== 'position' && attributeName !== 'normal' && attributeName !== 'color' ) {
1464
+
1465
+ if ( ! warned ) {
1466
+
1467
+ console.warn( 'GLTFExporter: Only POSITION and NORMAL morph are supported.' );
1468
+ warned = true;
1469
+
1470
+ }
1471
+
1472
+ continue;
1473
+
1474
+ }
1475
+
1476
+ const attribute = geometry.morphAttributes[ attributeName ][ i ];
1477
+ let gltfAttributeName = attributeName.toUpperCase();
1478
+
1479
+ if ( nameConversion[ attributeName ] ) {
1480
+
1481
+ gltfAttributeName = nameConversion[ attributeName ];
1482
+
1483
+ }
1484
+
1485
+ // Three.js morph attribute has absolute values while the one of glTF has relative values.
1486
+ //
1487
+ // glTF 2.0 Specification:
1488
+ // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#morph-targets
1489
+
1490
+ const baseAttribute = geometry.attributes[ attributeName ];
1491
+
1492
+ if ( cache.attributes.has( this.getUID( attribute ) ) ) {
1493
+
1494
+ target[ gltfAttributeName ] = cache.attributes.get( this.getUID( attribute ) );
1495
+ continue;
1496
+
1497
+ }
1498
+
1499
+ // Clones attribute not to override
1500
+ const relativeAttribute = baseAttribute.clone();
1501
+
1502
+ if ( ! geometry.morphTargetsRelative ) {
1503
+
1504
+ if (baseAttribute) {
1505
+
1506
+ for ( let j = 0, jl = attribute.count; j < jl; j ++ ) {
1507
+
1508
+ if (baseAttribute.count > j) {
1509
+
1510
+ relativeAttribute.setXYZ(
1511
+ j,
1512
+ attribute.getX( j ) - baseAttribute.getX( j ),
1513
+ attribute.getY( j ) - baseAttribute.getY( j ),
1514
+ attribute.getZ( j ) - baseAttribute.getZ( j )
1515
+ );
1516
+
1517
+ }
1518
+
1519
+ }
1520
+
1521
+ } else {
1522
+
1523
+ for ( let j = 0, jl = attribute.count; j < jl; j ++ ) {
1524
+
1525
+ relativeAttribute.setXYZ(
1526
+ j,
1527
+ 0,
1528
+ 0,
1529
+ 0
1530
+ );
1531
+
1532
+ }
1533
+
1534
+ }
1535
+
1536
+ }
1537
+
1538
+ target[ gltfAttributeName ] = this.processAccessor( relativeAttribute, geometry );
1539
+ cache.attributes.set( this.getUID( baseAttribute ), target[ gltfAttributeName ] );
1540
+
1541
+ }
1542
+
1543
+ targets.push( target );
1544
+
1545
+ weights.push( mesh.morphTargetInfluences[ i ] );
1546
+
1547
+ if ( mesh.morphTargetDictionary !== undefined ) targetNames.push( reverseDictionary[ i ] );
1548
+
1549
+ }
1550
+
1551
+ meshDef.weights = weights;
1552
+
1553
+ if ( targetNames.length > 0 ) {
1554
+
1555
+ meshDef.extras = {};
1556
+ meshDef.extras.targetNames = targetNames;
1557
+
1558
+ }
1559
+
1560
+ }
1561
+
1562
+ const isMultiMaterial = Array.isArray( mesh.material );
1563
+
1564
+ if ( isMultiMaterial && geometry.groups.length === 0 ) return null;
1565
+
1566
+ const materials = isMultiMaterial ? mesh.material : [ mesh.material ];
1567
+ const groups = isMultiMaterial ? geometry.groups : [ { materialIndex: 0, start: undefined, count: undefined } ];
1568
+
1569
+ for ( let i = 0, il = groups.length; i < il; i ++ ) {
1570
+
1571
+ const primitive = {
1572
+ mode: mode,
1573
+ attributes: attributes,
1574
+ };
1575
+
1576
+ this.serializeUserData( geometry, primitive );
1577
+
1578
+ if ( targets.length > 0 ) primitive.targets = targets;
1579
+
1580
+ if ( geometry.index !== null ) {
1581
+
1582
+ let cacheKey = this.getUID( geometry.index );
1583
+
1584
+ if ( groups[ i ].start !== undefined || groups[ i ].count !== undefined ) {
1585
+
1586
+ cacheKey += ':' + groups[ i ].start + ':' + groups[ i ].count;
1587
+
1588
+ }
1589
+
1590
+ if ( cache.attributes.has( cacheKey ) ) {
1591
+
1592
+ primitive.indices = cache.attributes.get( cacheKey );
1593
+
1594
+ } else {
1595
+
1596
+ primitive.indices = this.processAccessor( geometry.index, geometry, groups[ i ].start, groups[ i ].count );
1597
+ cache.attributes.set( cacheKey, primitive.indices );
1598
+
1599
+ }
1600
+
1601
+ if ( primitive.indices === null ) delete primitive.indices;
1602
+
1603
+ }
1604
+
1605
+ const material = this.processMaterial( materials[ groups[ i ].materialIndex ] );
1606
+
1607
+ if ( material !== null ) primitive.material = material;
1608
+
1609
+ primitives.push( primitive );
1610
+
1611
+ }
1612
+
1613
+ meshDef.primitives = primitives;
1614
+
1615
+ if ( ! json.meshes ) json.meshes = [];
1616
+
1617
+ this._invokeAll( function ( ext ) {
1618
+
1619
+ ext.writeMesh && ext.writeMesh( mesh, meshDef );
1620
+
1621
+ } );
1622
+
1623
+ const index = json.meshes.push( meshDef ) - 1;
1624
+ cache.meshes.set( meshCacheKey, index );
1625
+ return index;
1626
+
1627
+ }
1628
+
1629
+ /**
1630
+ * Process camera
1631
+ * @param {THREE.Camera} camera Camera to process
1632
+ * @return {Integer} Index of the processed mesh in the "camera" array
1633
+ */
1634
+ processCamera( camera ) {
1635
+
1636
+ const json = this.json;
1637
+
1638
+ if ( ! json.cameras ) json.cameras = [];
1639
+
1640
+ const isOrtho = camera.isOrthographicCamera;
1641
+
1642
+ const cameraDef = {
1643
+ type: isOrtho ? 'orthographic' : 'perspective'
1644
+ };
1645
+
1646
+ if ( isOrtho ) {
1647
+
1648
+ cameraDef.orthographic = {
1649
+ xmag: camera.right * 2,
1650
+ ymag: camera.top * 2,
1651
+ zfar: camera.far <= 0 ? 0.001 : camera.far,
1652
+ znear: camera.near < 0 ? 0 : camera.near
1653
+ };
1654
+
1655
+ } else {
1656
+
1657
+ cameraDef.perspective = {
1658
+ aspectRatio: camera.aspect,
1659
+ yfov: MathUtils.degToRad( camera.fov ),
1660
+ zfar: camera.far <= 0 ? 0.001 : camera.far,
1661
+ znear: camera.near < 0 ? 0 : camera.near
1662
+ };
1663
+
1664
+ }
1665
+
1666
+ // Question: Is saving "type" as name intentional?
1667
+ if ( camera.name !== '' ) cameraDef.name = camera.type;
1668
+
1669
+ return json.cameras.push( cameraDef ) - 1;
1670
+
1671
+ }
1672
+
1673
+ /**
1674
+ * Creates glTF animation entry from AnimationClip object.
1675
+ *
1676
+ * Status:
1677
+ * - Only properties listed in PATH_PROPERTIES may be animated.
1678
+ *
1679
+ * @param {THREE.AnimationClip} clip
1680
+ * @param {THREE.Object3D} root
1681
+ * @return {number|null}
1682
+ */
1683
+ processAnimation( clip, root ) {
1684
+
1685
+ const json = this.json;
1686
+ const nodeMap = this.nodeMap;
1687
+
1688
+ if ( ! json.animations ) json.animations = [];
1689
+
1690
+ clip = GLTFExporter.Utils.mergeMorphTargetTracks( clip.clone(), root );
1691
+
1692
+ const tracks = clip.tracks;
1693
+ const channels = [];
1694
+ const samplers = [];
1695
+
1696
+ for ( let i = 0; i < tracks.length; ++ i ) {
1697
+
1698
+ const track = tracks[ i ];
1699
+ const trackBinding = PropertyBinding.parseTrackName( track.name );
1700
+ let trackNode = PropertyBinding.findNode( root, trackBinding.nodeName );
1701
+ const trackProperty = PATH_PROPERTIES[ trackBinding.propertyName ];
1702
+
1703
+ if ( trackBinding.objectName === 'bones' ) {
1704
+
1705
+ if ( trackNode.isSkinnedMesh === true ) {
1706
+
1707
+ trackNode = trackNode.skeleton.getBoneByName( trackBinding.objectIndex );
1708
+
1709
+ } else {
1710
+
1711
+ trackNode = undefined;
1712
+
1713
+ }
1714
+
1715
+ }
1716
+
1717
+ if ( ! trackNode || ! trackProperty ) {
1718
+
1719
+ console.warn( 'THREE.GLTFExporter: Could not export animation track "%s".', track.name );
1720
+ return null;
1721
+
1722
+ }
1723
+
1724
+ const inputItemSize = 1;
1725
+ let outputItemSize = track.values.length / track.times.length;
1726
+
1727
+ if ( trackProperty === PATH_PROPERTIES.morphTargetInfluences ) {
1728
+
1729
+ outputItemSize /= trackNode.morphTargetInfluences.length;
1730
+
1731
+ }
1732
+
1733
+ let interpolation;
1734
+
1735
+ // @TODO export CubicInterpolant(InterpolateSmooth) as CUBICSPLINE
1736
+
1737
+ // Detecting glTF cubic spline interpolant by checking factory method's special property
1738
+ // GLTFCubicSplineInterpolant is a custom interpolant and track doesn't return
1739
+ // valid value from .getInterpolation().
1740
+ if ( track.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline === true ) {
1741
+
1742
+ interpolation = 'CUBICSPLINE';
1743
+
1744
+ // itemSize of CUBICSPLINE keyframe is 9
1745
+ // (VEC3 * 3: inTangent, splineVertex, and outTangent)
1746
+ // but needs to be stored as VEC3 so dividing by 3 here.
1747
+ outputItemSize /= 3;
1748
+
1749
+ } else if ( track.getInterpolation() === InterpolateDiscrete ) {
1750
+
1751
+ interpolation = 'STEP';
1752
+
1753
+ } else {
1754
+
1755
+ interpolation = 'LINEAR';
1756
+
1757
+ }
1758
+
1759
+ samplers.push( {
1760
+ input: this.processAccessor( new BufferAttribute( track.times, inputItemSize ) ),
1761
+ output: this.processAccessor( new BufferAttribute( track.values, outputItemSize ) ),
1762
+ interpolation: interpolation
1763
+ } );
1764
+
1765
+ channels.push( {
1766
+ sampler: samplers.length - 1,
1767
+ target: {
1768
+ node: nodeMap.get( trackNode ),
1769
+ path: trackProperty
1770
+ }
1771
+ } );
1772
+
1773
+ }
1774
+
1775
+ json.animations.push( {
1776
+ name: clip.name || 'clip_' + json.animations.length,
1777
+ samplers: samplers,
1778
+ channels: channels
1779
+ } );
1780
+
1781
+ return json.animations.length - 1;
1782
+
1783
+ }
1784
+
1785
+ /**
1786
+ * @param {THREE.Object3D} object
1787
+ * @return {number|null}
1788
+ */
1789
+ processSkin( object ) {
1790
+
1791
+ const json = this.json;
1792
+ const nodeMap = this.nodeMap;
1793
+
1794
+ const node = json.nodes[ nodeMap.get( object ) ];
1795
+
1796
+ const skeleton = object.skeleton;
1797
+
1798
+ if ( skeleton === undefined ) return null;
1799
+
1800
+ const rootJoint = object.skeleton.bones[ 0 ];
1801
+
1802
+ if ( rootJoint === undefined ) return null;
1803
+
1804
+ const joints = [];
1805
+ const inverseBindMatrices = new Float32Array( skeleton.bones.length * 16 );
1806
+ const temporaryBoneInverse = new Matrix4();
1807
+
1808
+ for ( let i = 0; i < skeleton.bones.length; ++ i ) {
1809
+
1810
+ joints.push( nodeMap.get( skeleton.bones[ i ] ) );
1811
+ temporaryBoneInverse.copy( skeleton.boneInverses[ i ] );
1812
+ temporaryBoneInverse.multiply( object.bindMatrix ).toArray( inverseBindMatrices, i * 16 );
1813
+
1814
+ }
1815
+
1816
+ if ( json.skins === undefined ) json.skins = [];
1817
+
1818
+ json.skins.push( {
1819
+ inverseBindMatrices: this.processAccessor( new BufferAttribute( inverseBindMatrices, 16 ) ),
1820
+ joints: joints,
1821
+ skeleton: nodeMap.get( rootJoint )
1822
+ } );
1823
+
1824
+ const skinIndex = node.skin = json.skins.length - 1;
1825
+
1826
+ return skinIndex;
1827
+
1828
+ }
1829
+
1830
+ /**
1831
+ * Process Object3D node
1832
+ * @param {THREE.Object3D} node Object3D to processNode
1833
+ * @return {Integer} Index of the node in the nodes list
1834
+ */
1835
+ processNode( object ) {
1836
+
1837
+ const json = this.json;
1838
+ const options = this.options;
1839
+ const nodeMap = this.nodeMap;
1840
+
1841
+ if ( ! json.nodes ) json.nodes = [];
1842
+
1843
+ const nodeDef = {};
1844
+
1845
+ if ( options.trs ) {
1846
+
1847
+ const rotation = object.quaternion.toArray();
1848
+ const position = object.position.toArray();
1849
+ const scale = object.scale.toArray();
1850
+
1851
+ if ( ! equalArray( rotation, [ 0, 0, 0, 1 ] ) ) {
1852
+
1853
+ nodeDef.rotation = rotation;
1854
+
1855
+ }
1856
+
1857
+ if ( ! equalArray( position, [ 0, 0, 0 ] ) ) {
1858
+
1859
+ nodeDef.translation = position;
1860
+
1861
+ }
1862
+
1863
+ if ( ! equalArray( scale, [ 1, 1, 1 ] ) ) {
1864
+
1865
+ nodeDef.scale = scale;
1866
+
1867
+ }
1868
+
1869
+ } else {
1870
+
1871
+ if ( object.matrixAutoUpdate ) {
1872
+
1873
+ object.updateMatrix();
1874
+
1875
+ }
1876
+
1877
+ if ( isIdentityMatrix( object.matrix ) === false ) {
1878
+
1879
+ nodeDef.matrix = object.matrix.elements;
1880
+
1881
+ }
1882
+
1883
+ }
1884
+
1885
+ // We don't export empty strings name because it represents no-name in Three.js.
1886
+ if ( object.name !== '' ) nodeDef.name = String( object.name );
1887
+
1888
+ this.serializeUserData( object, nodeDef );
1889
+
1890
+ if ( object.isMesh || object.isLine || object.isPoints ) {
1891
+
1892
+ const meshIndex = this.processMesh( object );
1893
+
1894
+ if ( meshIndex !== null ) nodeDef.mesh = meshIndex;
1895
+
1896
+ } else if ( object.isCamera ) {
1897
+
1898
+ nodeDef.camera = this.processCamera( object );
1899
+
1900
+ }
1901
+
1902
+ if ( object.isSkinnedMesh ) this.skins.push( object );
1903
+
1904
+ if ( object.children.length > 0 ) {
1905
+
1906
+ const children = [];
1907
+
1908
+ for ( let i = 0, l = object.children.length; i < l; i ++ ) {
1909
+
1910
+ const child = object.children[ i ];
1911
+
1912
+ if ( child.visible || options.onlyVisible === false ) {
1913
+
1914
+ const nodeIndex = this.processNode( child );
1915
+
1916
+ if ( nodeIndex !== null ) children.push( nodeIndex );
1917
+
1918
+ }
1919
+
1920
+ }
1921
+
1922
+ if ( children.length > 0 ) nodeDef.children = children;
1923
+
1924
+ }
1925
+
1926
+ this._invokeAll( function ( ext ) {
1927
+
1928
+ ext.writeNode && ext.writeNode( object, nodeDef );
1929
+
1930
+ } );
1931
+
1932
+ const nodeIndex = json.nodes.push( nodeDef ) - 1;
1933
+ nodeMap.set( object, nodeIndex );
1934
+ return nodeIndex;
1935
+
1936
+ }
1937
+
1938
+ /**
1939
+ * Process Scene
1940
+ * @param {Scene} node Scene to process
1941
+ */
1942
+ processScene( scene ) {
1943
+
1944
+ const json = this.json;
1945
+ const options = this.options;
1946
+
1947
+ if ( ! json.scenes ) {
1948
+
1949
+ json.scenes = [];
1950
+ json.scene = 0;
1951
+
1952
+ }
1953
+
1954
+ const sceneDef = {};
1955
+
1956
+ if ( scene.name !== '' ) sceneDef.name = scene.name;
1957
+
1958
+ json.scenes.push( sceneDef );
1959
+
1960
+ const nodes = [];
1961
+
1962
+ for ( let i = 0, l = scene.children.length; i < l; i ++ ) {
1963
+
1964
+ const child = scene.children[ i ];
1965
+
1966
+ if ( child.visible || options.onlyVisible === false ) {
1967
+
1968
+ const nodeIndex = this.processNode( child );
1969
+
1970
+ if ( nodeIndex !== null ) nodes.push( nodeIndex );
1971
+
1972
+ }
1973
+
1974
+ }
1975
+
1976
+ if ( nodes.length > 0 ) sceneDef.nodes = nodes;
1977
+
1978
+ this.serializeUserData( scene, sceneDef );
1979
+
1980
+ }
1981
+
1982
+ /**
1983
+ * Creates a Scene to hold a list of objects and parse it
1984
+ * @param {Array} objects List of objects to process
1985
+ */
1986
+ processObjects( objects ) {
1987
+
1988
+ const scene = new Scene();
1989
+ scene.name = 'AuxScene';
1990
+
1991
+ for ( let i = 0; i < objects.length; i ++ ) {
1992
+
1993
+ // We push directly to children instead of calling `add` to prevent
1994
+ // modify the .parent and break its original scene and hierarchy
1995
+ scene.children.push( objects[ i ] );
1996
+
1997
+ }
1998
+
1999
+ this.processScene( scene );
2000
+
2001
+ }
2002
+
2003
+ /**
2004
+ * @param {THREE.Object3D|Array<THREE.Object3D>} input
2005
+ */
2006
+ processInput( input ) {
2007
+
2008
+ const options = this.options;
2009
+
2010
+ input = input instanceof Array ? input : [ input ];
2011
+
2012
+ this._invokeAll( function ( ext ) {
2013
+
2014
+ ext.beforeParse && ext.beforeParse( input );
2015
+
2016
+ } );
2017
+
2018
+ const objectsWithoutScene = [];
2019
+
2020
+ for ( let i = 0; i < input.length; i ++ ) {
2021
+
2022
+ if ( input[ i ] instanceof Scene ) {
2023
+
2024
+ this.processScene( input[ i ] );
2025
+
2026
+ } else {
2027
+
2028
+ objectsWithoutScene.push( input[ i ] );
2029
+
2030
+ }
2031
+
2032
+ }
2033
+
2034
+ if ( objectsWithoutScene.length > 0 ) this.processObjects( objectsWithoutScene );
2035
+
2036
+ for ( let i = 0; i < this.skins.length; ++ i ) {
2037
+
2038
+ this.processSkin( this.skins[ i ] );
2039
+
2040
+ }
2041
+
2042
+ for ( let i = 0; i < options.animations.length; ++ i ) {
2043
+
2044
+ this.processAnimation( options.animations[ i ].clip, options.animations[i].mesh);
2045
+
2046
+ }
2047
+
2048
+ this._invokeAll( function ( ext ) {
2049
+
2050
+ ext.afterParse && ext.afterParse( input );
2051
+
2052
+ } );
2053
+
2054
+ }
2055
+
2056
+ _invokeAll( func ) {
2057
+
2058
+ for ( let i = 0, il = this.plugins.length; i < il; i ++ ) {
2059
+
2060
+ func( this.plugins[ i ] );
2061
+
2062
+ }
2063
+
2064
+ }
2065
+
2066
+ }
2067
+
2068
+ /**
2069
+ * Punctual Lights Extension
2070
+ *
2071
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
2072
+ */
2073
+ class GLTFLightExtension {
2074
+
2075
+ constructor( writer ) {
2076
+
2077
+ this.writer = writer;
2078
+ this.name = 'KHR_lights_punctual';
2079
+
2080
+ }
2081
+
2082
+ writeNode( light, nodeDef ) {
2083
+
2084
+ if ( ! light.isLight ) return;
2085
+
2086
+ if ( ! light.isDirectionalLight && ! light.isPointLight && ! light.isSpotLight ) {
2087
+
2088
+ console.warn( 'THREE.GLTFExporter: Only directional, point, and spot lights are supported.', light );
2089
+ return;
2090
+
2091
+ }
2092
+
2093
+ const writer = this.writer;
2094
+ const json = writer.json;
2095
+ const extensionsUsed = writer.extensionsUsed;
2096
+
2097
+ const lightDef = {};
2098
+
2099
+ if ( light.name ) lightDef.name = light.name;
2100
+
2101
+ lightDef.color = light.color.toArray();
2102
+
2103
+ lightDef.intensity = light.intensity;
2104
+
2105
+ if ( light.isDirectionalLight ) {
2106
+
2107
+ lightDef.type = 'directional';
2108
+
2109
+ } else if ( light.isPointLight ) {
2110
+
2111
+ lightDef.type = 'point';
2112
+
2113
+ if ( light.distance > 0 ) lightDef.range = light.distance;
2114
+
2115
+ } else if ( light.isSpotLight ) {
2116
+
2117
+ lightDef.type = 'spot';
2118
+
2119
+ if ( light.distance > 0 ) lightDef.range = light.distance;
2120
+
2121
+ lightDef.spot = {};
2122
+ lightDef.spot.innerConeAngle = ( light.penumbra - 1.0 ) * light.angle * - 1.0;
2123
+ lightDef.spot.outerConeAngle = light.angle;
2124
+
2125
+ }
2126
+
2127
+ if ( light.decay !== undefined && light.decay !== 2 ) {
2128
+
2129
+ console.warn( 'THREE.GLTFExporter: Light decay may be lost. glTF is physically-based, '
2130
+ + 'and expects light.decay=2.' );
2131
+
2132
+ }
2133
+
2134
+ if ( light.target
2135
+ && ( light.target.parent !== light
2136
+ || light.target.position.x !== 0
2137
+ || light.target.position.y !== 0
2138
+ || light.target.position.z !== - 1 ) ) {
2139
+
2140
+ console.warn( 'THREE.GLTFExporter: Light direction may be lost. For best results, '
2141
+ + 'make light.target a child of the light with position 0,0,-1.' );
2142
+
2143
+ }
2144
+
2145
+ if ( ! extensionsUsed[ this.name ] ) {
2146
+
2147
+ json.extensions = json.extensions || {};
2148
+ json.extensions[ this.name ] = { lights: [] };
2149
+ extensionsUsed[ this.name ] = true;
2150
+
2151
+ }
2152
+
2153
+ const lights = json.extensions[ this.name ].lights;
2154
+ lights.push( lightDef );
2155
+
2156
+ nodeDef.extensions = nodeDef.extensions || {};
2157
+ nodeDef.extensions[ this.name ] = { light: lights.length - 1 };
2158
+
2159
+ }
2160
+
2161
+ }
2162
+
2163
+ /**
2164
+ * Unlit Materials Extension
2165
+ *
2166
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit
2167
+ */
2168
+ class GLTFMaterialsUnlitExtension {
2169
+
2170
+ constructor( writer ) {
2171
+
2172
+ this.writer = writer;
2173
+ this.name = 'KHR_materials_unlit';
2174
+
2175
+ }
2176
+
2177
+ writeMaterial( material, materialDef ) {
2178
+
2179
+ if ( ! material.isMeshBasicMaterial ) return;
2180
+
2181
+ const writer = this.writer;
2182
+ const extensionsUsed = writer.extensionsUsed;
2183
+
2184
+ materialDef.extensions = materialDef.extensions || {};
2185
+ materialDef.extensions[ this.name ] = {};
2186
+
2187
+ extensionsUsed[ this.name ] = true;
2188
+
2189
+ materialDef.pbrMetallicRoughness.metallicFactor = 0.0;
2190
+ materialDef.pbrMetallicRoughness.roughnessFactor = 0.9;
2191
+
2192
+ }
2193
+
2194
+ }
2195
+
2196
+ /**
2197
+ * Specular-Glossiness Extension
2198
+ *
2199
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness
2200
+ */
2201
+ class GLTFMaterialsPBRSpecularGlossiness {
2202
+
2203
+ constructor( writer ) {
2204
+
2205
+ this.writer = writer;
2206
+ this.name = 'KHR_materials_pbrSpecularGlossiness';
2207
+
2208
+ }
2209
+
2210
+ writeMaterial( material, materialDef ) {
2211
+
2212
+ if ( ! material.isGLTFSpecularGlossinessMaterial ) return;
2213
+
2214
+ const writer = this.writer;
2215
+ const extensionsUsed = writer.extensionsUsed;
2216
+
2217
+ const extensionDef = {};
2218
+
2219
+ if ( materialDef.pbrMetallicRoughness.baseColorFactor ) {
2220
+
2221
+ extensionDef.diffuseFactor = materialDef.pbrMetallicRoughness.baseColorFactor;
2222
+
2223
+ }
2224
+
2225
+ const specularFactor = [ 1, 1, 1 ];
2226
+ material.specular.toArray( specularFactor, 0 );
2227
+ extensionDef.specularFactor = specularFactor;
2228
+ extensionDef.glossinessFactor = material.glossiness;
2229
+
2230
+ if ( materialDef.pbrMetallicRoughness.baseColorTexture ) {
2231
+
2232
+ extensionDef.diffuseTexture = materialDef.pbrMetallicRoughness.baseColorTexture;
2233
+
2234
+ }
2235
+
2236
+ if ( material.specularMap ) {
2237
+
2238
+ const specularMapDef = { index: writer.processTexture( material.specularMap ) };
2239
+ writer.applyTextureTransform( specularMapDef, material.specularMap );
2240
+ extensionDef.specularGlossinessTexture = specularMapDef;
2241
+
2242
+ }
2243
+
2244
+ materialDef.extensions = materialDef.extensions || {};
2245
+ materialDef.extensions[ this.name ] = extensionDef;
2246
+ extensionsUsed[ this.name ] = true;
2247
+
2248
+ }
2249
+
2250
+ }
2251
+
2252
+ /**
2253
+ * Static utility functions
2254
+ */
2255
+ GLTFExporter.Utils = {
2256
+
2257
+ insertKeyframe: function ( track, time ) {
2258
+
2259
+ const tolerance = 0.001; // 1ms
2260
+ const valueSize = track.getValueSize();
2261
+
2262
+ const times = new track.TimeBufferType( track.times.length + 1 );
2263
+ const values = new track.ValueBufferType( track.values.length + valueSize );
2264
+ const interpolant = track.createInterpolant( new track.ValueBufferType( valueSize ) );
2265
+
2266
+ let index;
2267
+
2268
+ if ( track.times.length === 0 ) {
2269
+
2270
+ times[ 0 ] = time;
2271
+
2272
+ for ( let i = 0; i < valueSize; i ++ ) {
2273
+
2274
+ values[ i ] = 0;
2275
+
2276
+ }
2277
+
2278
+ index = 0;
2279
+
2280
+ } else if ( time < track.times[ 0 ] ) {
2281
+
2282
+ if ( Math.abs( track.times[ 0 ] - time ) < tolerance ) return 0;
2283
+
2284
+ times[ 0 ] = time;
2285
+ times.set( track.times, 1 );
2286
+
2287
+ values.set( interpolant.evaluate( time ), 0 );
2288
+ values.set( track.values, valueSize );
2289
+
2290
+ index = 0;
2291
+
2292
+ } else if ( time > track.times[ track.times.length - 1 ] ) {
2293
+
2294
+ if ( Math.abs( track.times[ track.times.length - 1 ] - time ) < tolerance ) {
2295
+
2296
+ return track.times.length - 1;
2297
+
2298
+ }
2299
+
2300
+ times[ times.length - 1 ] = time;
2301
+ times.set( track.times, 0 );
2302
+
2303
+ values.set( track.values, 0 );
2304
+ values.set( interpolant.evaluate( time ), track.values.length );
2305
+
2306
+ index = times.length - 1;
2307
+
2308
+ } else {
2309
+
2310
+ for ( let i = 0; i < track.times.length; i ++ ) {
2311
+
2312
+ if ( Math.abs( track.times[ i ] - time ) < tolerance ) return i;
2313
+
2314
+ if ( track.times[ i ] < time && track.times[ i + 1 ] > time ) {
2315
+
2316
+ times.set( track.times.slice( 0, i + 1 ), 0 );
2317
+ times[ i + 1 ] = time;
2318
+ times.set( track.times.slice( i + 1 ), i + 2 );
2319
+
2320
+ values.set( track.values.slice( 0, ( i + 1 ) * valueSize ), 0 );
2321
+ values.set( interpolant.evaluate( time ), ( i + 1 ) * valueSize );
2322
+ values.set( track.values.slice( ( i + 1 ) * valueSize ), ( i + 2 ) * valueSize );
2323
+
2324
+ index = i + 1;
2325
+
2326
+ break;
2327
+
2328
+ }
2329
+
2330
+ }
2331
+
2332
+ }
2333
+
2334
+ track.times = times;
2335
+ track.values = values;
2336
+
2337
+ return index;
2338
+
2339
+ },
2340
+
2341
+ mergeMorphTargetTracks: function ( clip, root ) {
2342
+
2343
+ const tracks = [];
2344
+ const mergedTracks = {};
2345
+ const sourceTracks = clip.tracks;
2346
+
2347
+ for ( let i = 0; i < sourceTracks.length; ++ i ) {
2348
+
2349
+ let sourceTrack = sourceTracks[ i ];
2350
+ const sourceTrackBinding = PropertyBinding.parseTrackName( sourceTrack.name );
2351
+ const sourceTrackNode = PropertyBinding.findNode( root, sourceTrackBinding.nodeName );
2352
+
2353
+ if ( sourceTrackBinding.propertyName !== 'morphTargetInfluences' || sourceTrackBinding.propertyIndex === undefined ) {
2354
+
2355
+ // Tracks that don't affect morph targets, or that affect all morph targets together, can be left as-is.
2356
+ tracks.push( sourceTrack );
2357
+ continue;
2358
+
2359
+ }
2360
+
2361
+ if ( sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodDiscrete
2362
+ && sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodLinear ) {
2363
+
2364
+ if ( sourceTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) {
2365
+
2366
+ // This should never happen, because glTF morph target animations
2367
+ // affect all targets already.
2368
+ throw new Error( 'THREE.GLTFExporter: Cannot merge tracks with glTF CUBICSPLINE interpolation.' );
2369
+
2370
+ }
2371
+
2372
+ console.warn( 'THREE.GLTFExporter: Morph target interpolation mode not yet supported. Using LINEAR instead.' );
2373
+
2374
+ sourceTrack = sourceTrack.clone();
2375
+ sourceTrack.setInterpolation( InterpolateLinear );
2376
+
2377
+ }
2378
+
2379
+ const targetCount = sourceTrackNode.morphTargetInfluences.length;
2380
+ const targetIndex = sourceTrackNode.morphTargetDictionary[ sourceTrackBinding.propertyIndex ];
2381
+
2382
+ if ( targetIndex === undefined ) {
2383
+
2384
+ throw new Error( 'THREE.GLTFExporter: Morph target name not found: ' + sourceTrackBinding.propertyIndex );
2385
+
2386
+ }
2387
+
2388
+ let mergedTrack;
2389
+
2390
+ // If this is the first time we've seen this object, create a new
2391
+ // track to store merged keyframe data for each morph target.
2392
+ if ( mergedTracks[ sourceTrackNode.uuid ] === undefined ) {
2393
+
2394
+ mergedTrack = sourceTrack.clone();
2395
+
2396
+ const values = new mergedTrack.ValueBufferType( targetCount * mergedTrack.times.length );
2397
+
2398
+ for ( let j = 0; j < mergedTrack.times.length; j ++ ) {
2399
+
2400
+ values[ j * targetCount + targetIndex ] = mergedTrack.values[ j ];
2401
+
2402
+ }
2403
+
2404
+ // We need to take into consideration the intended target node
2405
+ // of our original un-merged morphTarget animation.
2406
+ mergedTrack.name = ( sourceTrackBinding.nodeName || '' ) + '.morphTargetInfluences';
2407
+ mergedTrack.values = values;
2408
+
2409
+ mergedTracks[ sourceTrackNode.uuid ] = mergedTrack;
2410
+ tracks.push( mergedTrack );
2411
+
2412
+ continue;
2413
+
2414
+ }
2415
+
2416
+ const sourceInterpolant = sourceTrack.createInterpolant( new sourceTrack.ValueBufferType( 1 ) );
2417
+
2418
+ mergedTrack = mergedTracks[ sourceTrackNode.uuid ];
2419
+
2420
+ // For every existing keyframe of the merged track, write a (possibly
2421
+ // interpolated) value from the source track.
2422
+ for ( let j = 0; j < mergedTrack.times.length; j ++ ) {
2423
+
2424
+ mergedTrack.values[ j * targetCount + targetIndex ] = sourceInterpolant.evaluate( mergedTrack.times[ j ] );
2425
+
2426
+ }
2427
+
2428
+ // For every existing keyframe of the source track, write a (possibly
2429
+ // new) keyframe to the merged track. Values from the previous loop may
2430
+ // be written again, but keyframes are de-duplicated.
2431
+ for ( let j = 0; j < sourceTrack.times.length; j ++ ) {
2432
+
2433
+ const keyframeIndex = this.insertKeyframe( mergedTrack, sourceTrack.times[ j ] );
2434
+ mergedTrack.values[ keyframeIndex * targetCount + targetIndex ] = sourceTrack.values[ j ];
2435
+
2436
+ }
2437
+
2438
+ }
2439
+
2440
+ clip.tracks = tracks;
2441
+
2442
+ return clip;
2443
+
2444
+ }
2445
+
2446
+ };
2447
+
2448
+ export { GLTFExporter };