Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement