UNPKG

@matematrolii/sketchbook

Version:

3D matematrolii playground built on three.js and cannon.js

74 lines (63 loc) 1.48 kB
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; import { LoadingTrackerEntry } from './LoadingTrackerEntry'; import { World } from '../world/World'; export class LoadingManager { public firstLoad: boolean = true; public onFinishedCallback: () => void; private world: World; private gltfLoader: GLTFLoader; private loadingTracker: LoadingTrackerEntry[] = []; constructor(world: World) { this.world = world; this.gltfLoader = new GLTFLoader(); this.world.setTimeScale(0); } public loadGLTF(path: string, onLoadingFinished: (gltf: any) => void): void { let trackerEntry = this.addLoadingEntry(path); this.gltfLoader.load(path, (gltf) => { onLoadingFinished(gltf); this.doneLoading(trackerEntry); }, (xhr) => { if ( xhr.lengthComputable ) { trackerEntry.progress = xhr.loaded / xhr.total; } }, (error) => { console.error(error); }); } public addLoadingEntry(path: string): LoadingTrackerEntry { let entry = new LoadingTrackerEntry(path); this.loadingTracker.push(entry); return entry; } public doneLoading(trackerEntry: LoadingTrackerEntry): void { trackerEntry.finished = true; trackerEntry.progress = 1; if (this.isLoadingDone()) { if (this.onFinishedCallback !== undefined) { this.onFinishedCallback(); } } } private isLoadingDone(): boolean { for (const entry of this.loadingTracker) { if (!entry.finished) return false; } return true; } }