@@ -78,6 +78,7 @@ import { GreasedLineBaseMesh } from "core/Meshes/GreasedLine/greasedLineBaseMesh
78
78
import { Color3 , Color4 } from "core/Maths/math.color" ;
79
79
import { TargetCamera } from "core/Cameras/targetCamera" ;
80
80
import { Epsilon } from "core/Maths/math.constants" ;
81
+ import { DataWriter } from "./dataWriter" ;
81
82
82
83
class ExporterState {
83
84
// Babylon indices array, start, count, offset, flip -> glTF accessor index
@@ -521,9 +522,9 @@ export class GLTFExporter {
521
522
522
523
public async generateGLTFAsync ( glTFPrefix : string ) : Promise < GLTFData > {
523
524
const binaryBuffer = await this . _generateBinaryAsync ( ) ;
524
-
525
525
this . _extensionsOnExporting ( ) ;
526
526
const jsonText = this . _generateJSON ( binaryBuffer . byteLength , glTFPrefix , true ) ;
527
+
527
528
const bin = new Blob ( [ binaryBuffer ] , { type : "application/octet-stream" } ) ;
528
529
529
530
const glTFFileName = glTFPrefix + ".gltf" ;
@@ -564,15 +565,15 @@ export class GLTFExporter {
564
565
public async generateGLBAsync ( glTFPrefix : string ) : Promise < GLTFData > {
565
566
this . _shouldUseGlb = true ;
566
567
const binaryBuffer = await this . _generateBinaryAsync ( ) ;
567
-
568
568
this . _extensionsOnExporting ( ) ;
569
569
const jsonText = this . _generateJSON ( binaryBuffer . byteLength ) ;
570
+
570
571
const glbFileName = glTFPrefix + ".glb" ;
571
572
const headerLength = 12 ;
572
573
const chunkLengthPrefix = 8 ;
573
574
let jsonLength = jsonText . length ;
574
575
let encodedJsonText ;
575
- // make use of TextEncoder when available
576
+ // Make use of TextEncoder when available
576
577
if ( typeof TextEncoder !== "undefined" ) {
577
578
const encoder = new TextEncoder ( ) ;
578
579
encodedJsonText = encoder . encode ( jsonText ) ;
@@ -583,61 +584,53 @@ export class GLTFExporter {
583
584
584
585
const byteLength = headerLength + 2 * chunkLengthPrefix + jsonLength + jsonPadding + binaryBuffer . byteLength + binPadding ;
585
586
586
- // header
587
- const headerBuffer = new ArrayBuffer ( headerLength ) ;
588
- const headerBufferView = new DataView ( headerBuffer ) ;
589
- headerBufferView . setUint32 ( 0 , 0x46546c67 , true ) ; //glTF
590
- headerBufferView . setUint32 ( 4 , 2 , true ) ; // version
591
- headerBufferView . setUint32 ( 8 , byteLength , true ) ; // total bytes in file
592
-
593
- // json chunk
594
- const jsonChunkBuffer = new ArrayBuffer ( chunkLengthPrefix + jsonLength + jsonPadding ) ;
595
- const jsonChunkBufferView = new DataView ( jsonChunkBuffer ) ;
596
- jsonChunkBufferView . setUint32 ( 0 , jsonLength + jsonPadding , true ) ;
597
- jsonChunkBufferView . setUint32 ( 4 , 0x4e4f534a , true ) ;
598
-
599
- // json chunk bytes
600
- const jsonData = new Uint8Array ( jsonChunkBuffer , chunkLengthPrefix ) ;
601
- // if TextEncoder was available, we can simply copy the encoded array
587
+ const dataWriter = new DataWriter ( byteLength ) ;
588
+
589
+ // Header
590
+ dataWriter . writeUInt32 ( 0x46546c67 ) ; // "glTF"
591
+ dataWriter . writeUInt32 ( 2 ) ; // Version
592
+ dataWriter . writeUInt32 ( byteLength ) ; // Total bytes in file
593
+
594
+ // JSON chunk length prefix
595
+ dataWriter . writeUInt32 ( jsonLength + jsonPadding ) ;
596
+ dataWriter . writeUInt32 ( 0x4e4f534a ) ; // "JSON"
597
+
598
+ // JSON chunk bytes
602
599
if ( encodedJsonText ) {
603
- jsonData . set ( encodedJsonText ) ;
600
+ // If TextEncoder was available, we can simply copy the encoded array
601
+ dataWriter . writeTypedArray ( encodedJsonText ) ;
604
602
} else {
605
603
const blankCharCode = "_" . charCodeAt ( 0 ) ;
606
604
for ( let i = 0 ; i < jsonLength ; ++ i ) {
607
605
const charCode = jsonText . charCodeAt ( i ) ;
608
- // if the character doesn't fit into a single UTF-16 code unit, just put a blank character
606
+ // If the character doesn't fit into a single UTF-16 code unit, just put a blank character
609
607
if ( charCode != jsonText . codePointAt ( i ) ) {
610
- jsonData [ i ] = blankCharCode ;
608
+ dataWriter . writeUInt8 ( blankCharCode ) ;
611
609
} else {
612
- jsonData [ i ] = charCode ;
610
+ dataWriter . writeUInt8 ( charCode ) ;
613
611
}
614
612
}
615
613
}
616
614
617
- // json padding
618
- const jsonPaddingView = new Uint8Array ( jsonChunkBuffer , chunkLengthPrefix + jsonLength ) ;
615
+ // JSON padding
619
616
for ( let i = 0 ; i < jsonPadding ; ++ i ) {
620
- jsonPaddingView [ i ] = 0x20 ;
617
+ dataWriter . writeUInt8 ( 0x20 ) ;
621
618
}
622
619
623
- // binary chunk
624
- const binaryChunkBuffer = new ArrayBuffer ( chunkLengthPrefix ) ;
625
- const binaryChunkBufferView = new DataView ( binaryChunkBuffer ) ;
626
- binaryChunkBufferView . setUint32 ( 0 , binaryBuffer . byteLength + binPadding , true ) ;
627
- binaryChunkBufferView . setUint32 ( 4 , 0x004e4942 , true ) ;
620
+ // Binary chunk length prefix
621
+ dataWriter . writeUInt32 ( binaryBuffer . byteLength + binPadding ) ;
622
+ dataWriter . writeUInt32 ( 0x004e4942 ) ; // "BIN"
628
623
629
- // binary padding
630
- const binPaddingBuffer = new ArrayBuffer ( binPadding ) ;
631
- const binPaddingView = new Uint8Array ( binPaddingBuffer ) ;
624
+ // Binary chunk bytes
625
+ dataWriter . writeTypedArray ( binaryBuffer ) ;
626
+
627
+ // Binary padding
632
628
for ( let i = 0 ; i < binPadding ; ++ i ) {
633
- binPaddingView [ i ] = 0 ;
629
+ dataWriter . writeUInt8 ( 0 ) ;
634
630
}
635
631
636
- const glbData = [ headerBuffer , jsonChunkBuffer , binaryChunkBuffer , binaryBuffer , binPaddingBuffer ] ;
637
- const glbFile = new Blob ( glbData , { type : "application/octet-stream" } ) ;
638
-
639
632
const container = new GLTFData ( ) ;
640
- container . files [ glbFileName ] = glbFile ;
633
+ container . files [ glbFileName ] = new Blob ( [ dataWriter . getOutputData ( ) ] , { type : "application/octet-stream" } ) ;
641
634
642
635
return container ;
643
636
}
0 commit comments