@@ -356,13 +356,11 @@ function resolveUrl(url: string, baseUrl?: string) {
356356 return ( new URL ( url , baseUrl ) ) . href ;
357357}
358358
359- function loadBuffer ( buffer : { uri : string ; byteLength : number } , gltf : GLTF , index : number , baseUrl ?: string ) {
360- return fetch ( resolveUrl ( buffer . uri , baseUrl ) )
361- . then ( response => response . arrayBuffer ( ) )
362- . then ( arrayBuffer => {
363- assert ( arrayBuffer . byteLength >= buffer . byteLength ) ;
364- gltf . buffers [ index ] = arrayBuffer ;
365- } ) ;
359+ async function loadBuffer ( buffer : { uri : string ; byteLength : number } , gltf : GLTF , index : number , baseUrl ?: string ) : Promise < void > {
360+ const response = await fetch ( resolveUrl ( buffer . uri , baseUrl ) ) ;
361+ const arrayBuffer = await response . arrayBuffer ( ) ;
362+ assert ( arrayBuffer . byteLength >= buffer . byteLength ) ;
363+ gltf . buffers [ index ] = arrayBuffer ;
366364}
367365
368366function getGLTFBytes ( gltf : GLTF , bufferViewIndex : number ) : Uint8Array < ArrayBuffer > {
@@ -371,26 +369,22 @@ function getGLTFBytes(gltf: GLTF, bufferViewIndex: number): Uint8Array<ArrayBuff
371369 return new Uint8Array < ArrayBuffer > ( buffer , bufferView . byteOffset || 0 , bufferView . byteLength ) ;
372370}
373371
374- function loadImage ( img : { uri ?: string ; bufferView ?: number ; mimeType : string } , gltf : GLTF , index : number , baseUrl ?: string ) {
372+ async function loadImage ( img : { uri ?: string ; bufferView ?: number ; mimeType : string } , gltf : GLTF , index : number , baseUrl ?: string ) : Promise < void > {
375373 if ( img . uri ) {
376374 const uri = resolveUrl ( img . uri , baseUrl ) ;
377- return fetch ( uri )
378- . then ( response => response . blob ( ) )
379- . then ( blob => createImageBitmap ( blob ) )
380- . then ( imageBitmap => {
381- gltf . images [ index ] = imageBitmap ;
382- } ) ;
375+ const response = await fetch ( uri ) ;
376+ const blob = await response . blob ( ) ;
377+ const imageBitmap = await createImageBitmap ( blob ) ;
378+ gltf . images [ index ] = imageBitmap ;
383379 } else if ( img . bufferView !== undefined ) {
384380 const bytes = getGLTFBytes ( gltf , img . bufferView ) ;
385381 const blob = new Blob ( [ bytes ] , { type : img . mimeType } ) ;
386- return createImageBitmap ( blob )
387- . then ( imageBitmap => {
388- gltf . images [ index ] = imageBitmap ;
389- } ) ;
382+ const imageBitmap = await createImageBitmap ( blob ) ;
383+ gltf . images [ index ] = imageBitmap ;
390384 }
391385}
392386
393- export function decodeGLTF ( arrayBuffer : ArrayBuffer , byteOffset : number = 0 , baseUrl ?: string ) : Promise < GLTF | void > {
387+ export async function decodeGLTF ( arrayBuffer : ArrayBuffer , byteOffset : number = 0 , baseUrl ?: string ) : Promise < GLTF | void > {
394388 const startTime = PerformanceUtils . now ( ) ;
395389
396390 const gltf : GLTF = { json : null , images : [ ] , buffers : [ ] } ;
@@ -420,9 +414,9 @@ export function decodeGLTF(arrayBuffer: ArrayBuffer, byteOffset: number = 0, bas
420414 }
421415
422416 const { buffers, images, meshes, extensionsUsed, bufferViews} = gltf . json ;
423- let bufferLoadsPromise : Promise < unknown > = Promise . resolve ( ) ;
417+
424418 if ( buffers ) {
425- const bufferLoads = [ ] ;
419+ const bufferLoads : Promise < void > [ ] = [ ] ;
426420 for ( let i = 0 ; i < buffers . length ; i ++ ) {
427421 const buffer = buffers [ i ] ;
428422 if ( buffer . uri ) {
@@ -431,58 +425,52 @@ export function decodeGLTF(arrayBuffer: ArrayBuffer, byteOffset: number = 0, bas
431425 gltf . buffers [ i ] = null ;
432426 }
433427 }
434- bufferLoadsPromise = Promise . all ( bufferLoads ) ;
428+ await Promise . all ( bufferLoads ) ;
435429 }
436430
437- return bufferLoadsPromise . then ( ( ) => {
438- const assetLoads = [ ] ;
431+ const assetLoads : Promise < unknown > [ ] = [ ] ;
432+ const dracoUsed = extensionsUsed && extensionsUsed . includes ( DRACO_EXT ) ;
433+ const meshoptUsed = extensionsUsed && extensionsUsed . includes ( MESHOPT_EXT ) ;
439434
440- const dracoUsed = extensionsUsed && extensionsUsed . includes ( DRACO_EXT ) ;
441- const meshoptUsed = extensionsUsed && extensionsUsed . includes ( MESHOPT_EXT ) ;
442- if ( dracoUsed ) {
443- assetLoads . push ( waitForDraco ( ) ) ;
435+ if ( dracoUsed ) {
436+ assetLoads . push ( waitForDraco ( ) ) ;
437+ }
438+ if ( meshoptUsed ) {
439+ assetLoads . push ( waitForMeshopt ( ) ) ;
440+ }
441+ if ( images ) {
442+ for ( let i = 0 ; i < images . length ; i ++ ) {
443+ assetLoads . push ( loadImage ( images [ i ] , gltf , i , baseUrl ) ) ;
444444 }
445+ }
445446
446- if ( meshoptUsed ) {
447- assetLoads . push ( waitForMeshopt ( ) ) ;
448- }
447+ if ( assetLoads . length ) {
448+ await Promise . all ( assetLoads ) ;
449+ }
449450
450- if ( images ) {
451- for ( let i = 0 ; i < images . length ; i ++ ) {
452- assetLoads . push ( loadImage ( images [ i ] , gltf , i , baseUrl ) ) ;
451+ if ( dracoUsed && meshes ) {
452+ for ( const { primitives} of meshes ) {
453+ for ( const primitive of primitives ) {
454+ loadDracoMesh ( primitive , gltf ) ;
453455 }
454456 }
457+ }
455458
456- const assetLoadsPromise = assetLoads . length ?
457- Promise . all ( assetLoads ) :
458- Promise . resolve ( ) ;
459-
460- return assetLoadsPromise . then ( ( ) => {
461- if ( dracoUsed && meshes ) {
462- for ( const { primitives} of meshes ) {
463- for ( const primitive of primitives ) {
464- loadDracoMesh ( primitive , gltf ) ;
465- }
466- }
467- }
468-
469- if ( meshoptUsed && meshes && bufferViews ) {
470- for ( const bufferView of bufferViews ) {
471- loadMeshoptBuffer ( bufferView , gltf ) ;
472- }
473- }
459+ if ( meshoptUsed && meshes && bufferViews ) {
460+ for ( const bufferView of bufferViews ) {
461+ loadMeshoptBuffer ( bufferView , gltf ) ;
462+ }
463+ }
474464
475- PerformanceUtils . measureWithDetails ( PerformanceUtils . GROUP_COMMON , "decodeGLTF" , "Models" , startTime ) ;
465+ PerformanceUtils . measureWithDetails ( PerformanceUtils . GROUP_COMMON , "decodeGLTF" , "Models" , startTime ) ;
476466
477- return gltf ;
478- } ) ;
479- } ) ;
467+ return gltf ;
480468}
481469
482- export function loadGLTF ( url : string ) : Promise < GLTF | void > {
483- return fetch ( url )
484- . then ( response => response . arrayBuffer ( ) )
485- . then ( buffer => decodeGLTF ( buffer , 0 , url ) ) ;
470+ export async function loadGLTF ( url : string ) : Promise < GLTF | void > {
471+ const response = await fetch ( url ) ;
472+ const buffer = await response . arrayBuffer ( ) ;
473+ return decodeGLTF ( buffer , 0 , url ) ;
486474}
487475
488476export function load3DTile ( data : ArrayBuffer ) : Promise < GLTF | void > {
0 commit comments