Skip to content

Instantly share code, notes, and snippets.

@hideyukisaito
Created December 6, 2016 10:07
Show Gist options
  • Save hideyukisaito/e3e9fb1d4762585cffb90f4619166b9a to your computer and use it in GitHub Desktop.
Save hideyukisaito/e3e9fb1d4762585cffb90f4619166b9a to your computer and use it in GitHub Desktop.
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