import { BufferGeometry , Group , Mesh } from 'three' ; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader' ; class ModelHelper { requestedModels : Map < string , ( ( value : BufferGeometry | PromiseLike < BufferGeometry > ) => void ) [ ] > = new Map ( ) ; loadedModels : Map < string , BufferGeometry > = new Map ( ) ; loader = new GLTFLoader ( ) ; loadModel ( path ) { return new Promise < BufferGeometry > ( ( resolve , reject ) => { if ( this . loadedModels . has ( path ) ) { resolve ( this . loadedModels . get ( path ) ! ) ; } else { if ( this . requestedModels . has ( path ) ) { this . requestedModels . get ( path ) ! . push ( resolve ) ; } else { this . requestedModels . set ( path , [ resolve ] ) ; this . loadRequestedModel ( path ) ; } } } ) ; } loadGroup ( path ) { return new Promise ( ( resolve , reject ) => { this . loader . load ( path , gltf => { resolve ( gltf . scene ) ; } , undefined , error => { console . error ( error ) ; } ) ; } ) ; } private loadRequestedModel ( path ) { this . loader . load ( path , gltf => { const geometry = ( gltf . scene . children [ 0 ] as Mesh ) . geometry as BufferGeometry ; this . loadedModels . set ( path , geometry ) ; for ( const resolveX of this . requestedModels . get ( path ) ! ) { resolveX ( geometry ) ; } this . requestedModels . delete ( path ) ; } , undefined , error => { console . error ( error ) ; } ) ; } } const modelHelper = new ModelHelper ( ) ; export { modelHelper } ;