Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import * as THREE from 'three';
  2. import { EventEmitter } from 'events';
  3.  
  4. import 'three/examples/js/loaders/OBJLoader';
  5.  
  6. export default class MultipleOBJLoader extends EventEmitter {
  7.  
  8. pathsToLoad = {};
  9. modelsLoaded = {};
  10. numLoaded = 0;
  11.  
  12. constructor () {
  13.  
  14. super();
  15. }
  16.  
  17. registerPath (path, key) {
  18.  
  19. if (!path) {
  20.  
  21. throw new Error('required argument: path');
  22. }
  23.  
  24. let _key;
  25.  
  26. if (key) {
  27. _key = key;
  28. } else {
  29. _key = path;
  30. }
  31.  
  32. // already loaded
  33. if (this.modelsLoaded[_key]) {
  34. return;
  35. }
  36.  
  37. this.pathsToLoad[_key] = path;
  38. }
  39.  
  40. load () {
  41.  
  42. const self = this;
  43. const numPaths = Object.keys(self.pathsToLoad).length;
  44.  
  45. return new Promise(function (resolve, reject) {
  46.  
  47. Object.keys(self.pathsToLoad).forEach(function (key) {
  48.  
  49. (function (key) {
  50.  
  51. var ldr = new THREE.OBJLoader();
  52. ldr.load(self.pathsToLoad[key], function (obj) {
  53.  
  54. self.modelsLoaded[key] = obj;
  55.  
  56. ++self.numLoaded;
  57.  
  58. if (numPaths === self.numLoaded) {
  59. resolve(self.modelsLoaded);
  60. }
  61. }, function (xhr) {
  62.  
  63. var parcent = xhr.loaded / xhr.total;
  64. var ratio = parcent / numPaths;
  65. self.emit('progress', ratio + ratio * self.numLoaded);
  66. });
  67. })(key);
  68. });
  69. });
  70. }
  71.  
  72. getModelsLoaded () {
  73.  
  74. return this.modelsLoaded;
  75. }
  76. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement