load-controller.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {THREE, FBXLoader, GLTFLoader, SkeletonUtils} from './three-defs.js';
  2. import {entity} from "./entity.js";
  3. export const load_controller = (() => {
  4. class LoadController extends entity.Component {
  5. constructor() {
  6. super();
  7. this.textures_ = {};
  8. this.models_ = {};
  9. this.sounds_ = {};
  10. this.playing_ = [];
  11. }
  12. LoadTexture(path, name) {
  13. if (!(name in this.textures_)) {
  14. const loader = new THREE.TextureLoader();
  15. loader.setPath(path);
  16. this.textures_[name] = {loader: loader, texture: loader.load(name)};
  17. this.textures_[name].encoding = THREE.sRGBEncoding;
  18. }
  19. return this.textures_[name].texture;
  20. }
  21. LoadSound(path, name, onLoad) {
  22. if (!(name in this.sounds_)) {
  23. const loader = new THREE.AudioLoader();
  24. loader.setPath(path);
  25. loader.load(name, (buf) => {
  26. this.sounds_[name] = {
  27. buffer: buf
  28. };
  29. const threejs = this.FindEntity('threejs').GetComponent('ThreeJSController');
  30. const s = new THREE.PositionalAudio(threejs.listener_);
  31. s.setBuffer(buf);
  32. s.setRefDistance(10);
  33. s.setMaxDistance(500);
  34. onLoad(s);
  35. this.playing_.push(s);
  36. });
  37. } else {
  38. const threejs = this.FindEntity('threejs').GetComponent('ThreeJSController');
  39. const s = new THREE.PositionalAudio(threejs.listener_);
  40. s.setBuffer(this.sounds_[name].buffer);
  41. s.setRefDistance(25);
  42. s.setMaxDistance(1000);
  43. onLoad(s);
  44. this.playing_.push(s);
  45. }
  46. }
  47. Load(path, name, onLoad) {
  48. if (name.endsWith('glb') || name.endsWith('gltf')) {
  49. this.LoadGLB(path, name, onLoad);
  50. } else if (name.endsWith('fbx')) {
  51. this.LoadFBX(path, name, onLoad);
  52. } else {
  53. // Silently fail, because screw you future me.
  54. }
  55. }
  56. LoadFBX(path, name, onLoad) {
  57. if (!(name in this.models_)) {
  58. const loader = new FBXLoader();
  59. loader.setPath(path);
  60. this.models_[name] = {loader: loader, asset: null, queue: [onLoad]};
  61. this.models_[name].loader.load(name, (fbx) => {
  62. this.models_[name].asset = fbx;
  63. const queue = this.models_[name].queue;
  64. this.models_[name].queue = null;
  65. for (let q of queue) {
  66. const clone = this.models_[name].asset.clone();
  67. q(clone);
  68. }
  69. });
  70. } else if (this.models_[name].asset == null) {
  71. this.models_[name].queue.push(onLoad);
  72. } else {
  73. const clone = this.models_[name].asset.clone();
  74. onLoad(clone);
  75. }
  76. }
  77. LoadGLB(path, name, onLoad) {
  78. const fullName = path + name;
  79. if (!(fullName in this.models_)) {
  80. const loader = new GLTFLoader();
  81. loader.setPath(path);
  82. this.models_[fullName] = {loader: loader, asset: null, queue: [onLoad]};
  83. this.models_[fullName].loader.load(name, (glb) => {
  84. this.models_[fullName].asset = glb;
  85. const queue = this.models_[fullName].queue;
  86. this.models_[fullName].queue = null;
  87. for (let q of queue) {
  88. const clone = {...glb};
  89. clone.scene = SkeletonUtils.clone(clone.scene);
  90. q(clone.scene);
  91. }
  92. });
  93. } else if (this.models_[fullName].asset == null) {
  94. this.models_[fullName].queue.push(onLoad);
  95. } else {
  96. const clone = {...this.models_[fullName].asset};
  97. clone.scene = SkeletonUtils.clone(clone.scene);
  98. onLoad(clone.scene);
  99. }
  100. }
  101. Update(timeElapsed) {
  102. for (let i = 0; i < this.playing_.length; ++i) {
  103. if (!this.playing_[i].isPlaying) {
  104. this.playing_[i].parent.remove(this.playing_[i]);
  105. }
  106. }
  107. this.playing_ = this.playing_.filter(s => s.isPlaying);
  108. }
  109. }
  110. return {
  111. LoadController: LoadController,
  112. };
  113. })();