@@ -72,7 +72,7 @@ export function exportGlb(meshByColor: Map<Color, Mesh>) {
72
72
const primitives : Record < string , any > [ ] = [ ] ;
73
73
const accessors : Record < string , any > [ ] = [ ] ;
74
74
const bufferViews : Record < string , any > [ ] = [ ] ;
75
- const buffers : Buffer [ ] = [ ] ;
75
+ const binaryBuffer : ArrayBuffer [ ] = [ ] ;
76
76
const materials : Record < string , any > [ ] = [ ] ;
77
77
let byteOffset = 0 ;
78
78
@@ -87,10 +87,10 @@ export function exportGlb(meshByColor: Map<Color, Mesh>) {
87
87
meshByColor . forEach ( ( mesh , color ) => {
88
88
const { positions, normals, faces } = mesh ;
89
89
const accessorOffset = accessors . length ;
90
- const faceBuffer = Buffer . from ( new Uint32Array ( faces ) . buffer ) ;
91
- const positionBuffer = Buffer . from ( flattenVec3s ( positions ) . buffer ) ;
92
- const normalBuffer = Buffer . from ( flattenVec3s ( normals ) . buffer ) ;
93
- buffers . push ( faceBuffer , positionBuffer , normalBuffer ) ;
90
+ const faceBuffer = new Uint32Array ( faces ) . buffer ;
91
+ const positionBuffer = flattenVec3s ( positions ) . buffer ;
92
+ const normalBuffer = flattenVec3s ( normals ) . buffer ;
93
+ binaryBuffer . push ( faceBuffer , positionBuffer , normalBuffer ) ;
94
94
const materialOffset = materials . length ;
95
95
const { min : positionMin , max : positionMax } = computeBounding ( positions ) ;
96
96
@@ -106,36 +106,36 @@ export function exportGlb(meshByColor: Map<Color, Mesh>) {
106
106
bufferViews . push ( {
107
107
'buffer' : 0 ,
108
108
'byteOffset' : byteOffset ,
109
- 'byteLength' : faceBuffer . length ,
110
- 'target' : 34963
109
+ 'byteLength' : faceBuffer . byteLength ,
110
+ 'target' : 34963 // ELEMENT_ARRAY_BUFFER
111
111
} ) ;
112
- byteOffset += faceBuffer . length ;
112
+ byteOffset += faceBuffer . byteLength ;
113
113
bufferViews . push ( {
114
114
'buffer' : 0 ,
115
115
'byteOffset' : byteOffset ,
116
- 'byteLength' : positionBuffer . length ,
117
- 'target' : 34962
116
+ 'byteLength' : positionBuffer . byteLength ,
117
+ 'target' : 34962 // ARRAY_BUFFER
118
118
} ) ;
119
- byteOffset += positionBuffer . length ;
119
+ byteOffset += positionBuffer . byteLength ;
120
120
bufferViews . push ( {
121
121
'buffer' : 0 ,
122
122
'byteOffset' : byteOffset ,
123
- 'byteLength' : normalBuffer . length ,
124
- 'target' : 34962
123
+ 'byteLength' : normalBuffer . byteLength ,
124
+ 'target' : 34962 // ARRAY_BUFFER
125
125
} ) ;
126
- byteOffset += normalBuffer . length ;
126
+ byteOffset += normalBuffer . byteLength ;
127
127
128
128
accessors . push ( {
129
129
'bufferView' : accessorOffset ,
130
130
'byteOffset' : 0 ,
131
- 'componentType' : 5125 , // unsigned int
131
+ 'componentType' : 5125 , // UNSIGNED_INT
132
132
'count' : faces . length ,
133
133
'type' : 'SCALAR'
134
134
} ) ;
135
135
accessors . push ( {
136
136
'bufferView' : accessorOffset + 1 ,
137
137
'byteOffset' : 0 ,
138
- 'componentType' : 5126 , // float
138
+ 'componentType' : 5126 , // FLOAT
139
139
'count' : positions . length ,
140
140
'type' : 'VEC3' ,
141
141
'max' : positionMax ,
@@ -144,7 +144,7 @@ export function exportGlb(meshByColor: Map<Color, Mesh>) {
144
144
accessors . push ( {
145
145
'bufferView' : accessorOffset + 2 ,
146
146
'byteOffset' : 0 ,
147
- 'componentType' : 5126 , // float
147
+ 'componentType' : 5126 , // FLOAT
148
148
'count' : normals . length ,
149
149
'type' : 'VEC3'
150
150
} ) ;
@@ -158,7 +158,7 @@ export function exportGlb(meshByColor: Map<Color, Mesh>) {
158
158
}
159
159
} ) ;
160
160
} ) ;
161
- const binaryBuffer = Buffer . concat ( buffers ) ;
161
+ const binaryBufferLength = byteOffset ;
162
162
163
163
const gltf = {
164
164
'asset' : {
@@ -174,38 +174,52 @@ export function exportGlb(meshByColor: Map<Color, Mesh>) {
174
174
'primitives' : primitives
175
175
} ] ,
176
176
'buffers' : [ {
177
- 'byteLength' : binaryBuffer . length ,
177
+ 'byteLength' : binaryBufferLength ,
178
178
} ] ,
179
179
'bufferViews' : bufferViews ,
180
180
'accessors' : accessors ,
181
181
'materials' : materials
182
182
} ;
183
183
184
- const createChunk = ( chunkType : number , buf : Buffer , padChar : number ) => {
185
- let bufBytes = buf . length ;
184
+ const createChunk = ( chunkType : number , buf : ArrayBuffer [ ] , byteLength : number , padChar : number ) : [ ArrayBuffer [ ] , number ] => {
186
185
let padding = null ;
187
- if ( bufBytes % 4 !== 0 ) {
188
- const pad = 4 - ( bufBytes % 4 ) ;
189
- bufBytes += pad ;
190
- padding = Buffer . alloc ( pad , padChar ) ;
186
+ if ( byteLength % 4 !== 0 ) {
187
+ const pad = 4 - ( byteLength % 4 ) ;
188
+ byteLength += pad ;
189
+ padding = new Uint8Array ( pad ) ;
190
+ padding . fill ( padChar ) ;
191
191
}
192
- const tmp = [ Buffer . from ( new Uint32Array ( [ bufBytes , chunkType ] ) . buffer ) , buf ] ;
192
+ const tmp = [ new Uint32Array ( [ byteLength , chunkType ] ) . buffer , ... buf ] ;
193
193
if ( padding ) {
194
- tmp . push ( padding ) ;
194
+ tmp . push ( padding . buffer ) ;
195
195
}
196
- return Buffer . concat ( tmp ) ;
196
+ return [ tmp , byteLength + 8 ] ;
197
197
} ;
198
- const jsonChunk = createChunk ( 0x4E4F534A , Buffer . from ( JSON . stringify ( gltf ) ) , 0x20 ) ;
199
- const binaryChunk = createChunk ( 0x004E4942 , binaryBuffer , 0x00 ) ;
200
- const glb = Buffer . concat ( [
201
- Buffer . from ( new Uint32Array ( [
198
+ const jsonString = JSON . stringify ( gltf ) ;
199
+ const jsonBuffer = new Uint8Array ( jsonString . length ) ;
200
+ for ( let i = 0 , il = jsonString . length ; i < il ; ++ i ) {
201
+ jsonBuffer [ i ] = jsonString . charCodeAt ( i ) ;
202
+ }
203
+
204
+ const [ jsonChunk , jsonChunkLength ] = createChunk ( 0x4E4F534A , [ jsonBuffer . buffer ] , jsonBuffer . length , 0x20 ) ;
205
+ const [ binaryChunk , binaryChunkLength ] = createChunk ( 0x004E4942 , binaryBuffer , binaryBufferLength , 0x00 ) ;
206
+ const glbBufferLength = 12 + jsonChunkLength + binaryChunkLength ;
207
+ const glbBuffer = [
208
+ new Uint32Array ( [
202
209
0x46546C67 , // magic number "gltf"
203
210
0x00000002 , // version
204
- 12 + jsonChunk . length + binaryChunk . length // total length
205
- ] ) . buffer ) ,
206
- jsonChunk ,
207
- binaryChunk
208
- ] ) ;
211
+ glbBufferLength
212
+ ] ) . buffer ,
213
+ ...jsonChunk ,
214
+ ...binaryChunk
215
+ ] ;
216
+
217
+ const glb = new Uint8Array ( glbBufferLength ) ;
218
+ let offset = 0 ;
219
+ for ( const buffer of glbBuffer ) {
220
+ glb . set ( new Uint8Array ( buffer ) , offset ) ;
221
+ offset += buffer . byteLength ;
222
+ }
209
223
return glb ;
210
224
}
211
225
@@ -269,5 +283,5 @@ export function exportUsdz(meshByColor: Map<Color, Mesh>) {
269
283
const crateFile = new CrateFile ( ) ;
270
284
crateFile . writeUsd ( root ) ;
271
285
const usdz = crateFile . getUsdz ( ) ;
272
- return Buffer . from ( usdz . buffer ) ;
286
+ return usdz ;
273
287
}
0 commit comments