babylon.filesInput.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. module BABYLON {
  2. export class FilesInput {
  3. private _engine: Engine;
  4. private _currentScene: Scene;
  5. private _canvas: HTMLCanvasElement;
  6. private _sceneLoadedCallback;
  7. private _progressCallback;
  8. private _additionnalRenderLoopLogicCallback;
  9. private _textureLoadingCallback;
  10. private _startingProcessingFilesCallback;
  11. private _elementToMonitor: HTMLElement;
  12. public static FilesTextures: any[] = new Array();
  13. public static FilesToLoad: any[] = new Array();
  14. private _sceneFileToLoad: File;
  15. private _filesToLoad: File[];
  16. /// Register to core BabylonJS object: engine, scene, rendering canvas, callback function when the scene will be loaded,
  17. /// loading progress callback and optionnal addionnal logic to call in the rendering loop
  18. constructor(p_engine: Engine, p_scene: Scene, p_canvas: HTMLCanvasElement, p_sceneLoadedCallback,
  19. p_progressCallback, p_additionnalRenderLoopLogicCallback, p_textureLoadingCallback, p_startingProcessingFilesCallback) {
  20. this._engine = p_engine;
  21. this._canvas = p_canvas;
  22. this._currentScene = p_scene;
  23. this._sceneLoadedCallback = p_sceneLoadedCallback;
  24. this._progressCallback = p_progressCallback;
  25. this._additionnalRenderLoopLogicCallback = p_additionnalRenderLoopLogicCallback;
  26. this._textureLoadingCallback = p_textureLoadingCallback;
  27. this._startingProcessingFilesCallback = p_startingProcessingFilesCallback;
  28. }
  29. public monitorElementForDragNDrop(p_elementToMonitor: HTMLElement): void {
  30. if (p_elementToMonitor) {
  31. this._elementToMonitor = p_elementToMonitor;
  32. this._elementToMonitor.addEventListener("dragenter", (e) => { this.drag(e); }, false);
  33. this._elementToMonitor.addEventListener("dragover", (e) => { this.drag(e); }, false);
  34. this._elementToMonitor.addEventListener("drop", (e) => { this.drop(e); }, false);
  35. }
  36. }
  37. private renderFunction(): void {
  38. if (this._additionnalRenderLoopLogicCallback) {
  39. this._additionnalRenderLoopLogicCallback();
  40. }
  41. if (this._currentScene) {
  42. if (this._textureLoadingCallback) {
  43. var remaining = this._currentScene.getWaitingItemsCount();
  44. if (remaining > 0) {
  45. this._textureLoadingCallback(remaining);
  46. }
  47. }
  48. this._currentScene.render();
  49. }
  50. }
  51. private drag(e): void {
  52. e.stopPropagation();
  53. e.preventDefault();
  54. }
  55. private drop(eventDrop): void {
  56. eventDrop.stopPropagation();
  57. eventDrop.preventDefault();
  58. this.loadFiles(eventDrop);
  59. }
  60. public loadFiles(event): void {
  61. if (this._startingProcessingFilesCallback) this._startingProcessingFilesCallback();
  62. // Handling data transfer via drag'n'drop
  63. if (event && event.dataTransfer && event.dataTransfer.files) {
  64. this._filesToLoad = event.dataTransfer.files;
  65. }
  66. // Handling files from input files
  67. if (event && event.target && event.target.files) {
  68. this._filesToLoad = event.target.files;
  69. }
  70. if (this._filesToLoad && this._filesToLoad.length > 0) {
  71. for (var i = 0; i < this._filesToLoad.length; i++) {
  72. switch (this._filesToLoad[i].type) {
  73. case "image/jpeg":
  74. case "image/png":
  75. case "image/bmp":
  76. FilesInput.FilesTextures[this._filesToLoad[i].name.toLowerCase()] = this._filesToLoad[i];
  77. break;
  78. case "image/targa":
  79. case "image/vnd.ms-dds":
  80. case "audio/wav":
  81. case "audio/x-wav":
  82. case "audio/mp3":
  83. case "audio/mpeg":
  84. case "audio/mpeg3":
  85. case "audio/x-mpeg-3":
  86. case "audio/ogg":
  87. FilesInput.FilesToLoad[this._filesToLoad[i].name.toLowerCase()] = this._filesToLoad[i];
  88. break;
  89. default:
  90. if (this._filesToLoad[i].name.indexOf(".mtl") !== -1) {
  91. FilesInput.FilesToLoad[this._filesToLoad[i].name.toLowerCase()] = this._filesToLoad[i];
  92. }
  93. else if ((
  94. this._filesToLoad[i].name.indexOf(".babylon") !== -1 ||
  95. this._filesToLoad[i].name.indexOf(".stl") !== -1 ||
  96. this._filesToLoad[i].name.indexOf(".obj") !== -1
  97. )
  98. && this._filesToLoad[i].name.indexOf(".manifest") === -1
  99. && this._filesToLoad[i].name.indexOf(".incremental") === -1 && this._filesToLoad[i].name.indexOf(".babylonmeshdata") === -1
  100. && this._filesToLoad[i].name.indexOf(".babylongeometrydata") === -1 && this._filesToLoad[i].name.indexOf(".babylonbinarymeshdata") === -1 &&
  101. this._filesToLoad[i].name.indexOf(".binary.babylon") === -1) {
  102. this._sceneFileToLoad = this._filesToLoad[i];
  103. }
  104. break;
  105. }
  106. }
  107. this.reload();
  108. }
  109. }
  110. public reload() {
  111. var that = this;
  112. // If a ".babylon" file has been provided
  113. if (this._sceneFileToLoad) {
  114. if (this._currentScene) {
  115. if (Tools.errorsCount > 0) {
  116. Tools.ClearLogCache();
  117. Tools.Log("Babylon.js engine (v" + Engine.Version + ") launched");
  118. }
  119. this._engine.stopRenderLoop();
  120. this._currentScene.dispose();
  121. }
  122. SceneLoader.Load("file:", this._sceneFileToLoad, this._engine, (newScene) => {
  123. that._currentScene = newScene;
  124. // Wait for textures and shaders to be ready
  125. that._currentScene.executeWhenReady(() => {
  126. // Attach camera to canvas inputs
  127. if (!that._currentScene.activeCamera || that._currentScene.lights.length === 0) {
  128. that._currentScene.createDefaultCameraOrLight();
  129. }
  130. that._currentScene.activeCamera.attachControl(that._canvas);
  131. if (that._sceneLoadedCallback) {
  132. that._sceneLoadedCallback(this._sceneFileToLoad, that._currentScene);
  133. }
  134. that._engine.runRenderLoop(() => { that.renderFunction(); });
  135. });
  136. }, progress => {
  137. if (this._progressCallback) {
  138. this._progressCallback(progress);
  139. }
  140. });
  141. }
  142. else {
  143. Tools.Error("Please provide a valid .babylon file.");
  144. }
  145. }
  146. }
  147. }