Created
December 6, 2016 10:07
-
-
Save hideyukisaito/e3e9fb1d4762585cffb90f4619166b9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as THREE from 'three'; | |
import { EventEmitter } from 'events'; | |
import 'three/examples/js/loaders/OBJLoader'; | |
export default class MultipleOBJLoader extends EventEmitter { | |
pathsToLoad = {}; | |
modelsLoaded = {}; | |
numLoaded = 0; | |
constructor () { | |
super(); | |
} | |
registerPath (path, key) { | |
if (!path) { | |
throw new Error('required argument: path'); | |
} | |
let _key; | |
if (key) { | |
_key = key; | |
} else { | |
_key = path; | |
} | |
// already loaded | |
if (this.modelsLoaded[_key]) { | |
return; | |
} | |
this.pathsToLoad[_key] = path; | |
} | |
load () { | |
const self = this; | |
const numPaths = Object.keys(self.pathsToLoad).length; | |
return new Promise(function (resolve, reject) { | |
Object.keys(self.pathsToLoad).forEach(function (key) { | |
(function (key) { | |
var ldr = new THREE.OBJLoader(); | |
ldr.load(self.pathsToLoad[key], function (obj) { | |
self.modelsLoaded[key] = obj; | |
++self.numLoaded; | |
if (numPaths === self.numLoaded) { | |
resolve(self.modelsLoaded); | |
} | |
}, function (xhr) { | |
var parcent = xhr.loaded / xhr.total; | |
var ratio = parcent / numPaths; | |
self.emit('progress', ratio + ratio * self.numLoaded); | |
}); | |
})(key); | |
}); | |
}); | |
} | |
getModelsLoaded () { | |
return this.modelsLoaded; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment