babylon.filesInput.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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] = 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] = this._filesToLoad[i];
  88. break;
  89. default:
  90. if ((
  91. this._filesToLoad[i].name.indexOf(".babylon") !== -1 || this._filesToLoad[i].name.indexOf(".stl") !== -1 ||
  92. this._filesToLoad[i].name.indexOf(".obj") !== -1 || this._filesToLoad[i].name.indexOf(".mtl") !== -1
  93. )
  94. && this._filesToLoad[i].name.indexOf(".manifest") === -1
  95. && this._filesToLoad[i].name.indexOf(".incremental") === -1 && this._filesToLoad[i].name.indexOf(".babylonmeshdata") === -1
  96. && this._filesToLoad[i].name.indexOf(".babylongeometrydata") === -1 && this._filesToLoad[i].name.indexOf(".babylonbinarymeshdata") === -1 &&
  97. this._filesToLoad[i].name.indexOf(".binary.babylon") === -1) {
  98. this._sceneFileToLoad = this._filesToLoad[i];
  99. }
  100. break;
  101. }
  102. }
  103. this.reload();
  104. }
  105. }
  106. public reload() {
  107. var that = this;
  108. // If a ".babylon" file has been provided
  109. if (this._sceneFileToLoad) {
  110. if (this._currentScene) {
  111. if (Tools.errorsCount > 0) {
  112. Tools.ClearLogCache();
  113. Tools.Log("Babylon.js engine (v" + Engine.Version + ") launched");
  114. }
  115. this._engine.stopRenderLoop();
  116. this._currentScene.dispose();
  117. }
  118. SceneLoader.Load("file:", this._sceneFileToLoad, this._engine, (newScene) => {
  119. that._currentScene = newScene;
  120. // Wait for textures and shaders to be ready
  121. that._currentScene.executeWhenReady(() => {
  122. // Attach camera to canvas inputs
  123. if (!that._currentScene.activeCamera || that._currentScene.lights.length === 0) {
  124. that._currentScene.createDefaultCameraOrLight();
  125. }
  126. that._currentScene.activeCamera.attachControl(that._canvas);
  127. if (that._sceneLoadedCallback) {
  128. that._sceneLoadedCallback(this._sceneFileToLoad, that._currentScene);
  129. }
  130. that._engine.runRenderLoop(() => { that.renderFunction(); });
  131. });
  132. }, progress => {
  133. if (this._progressCallback) {
  134. this._progressCallback(progress);
  135. }
  136. });
  137. }
  138. else {
  139. Tools.Error("Please provide a valid .babylon file.");
  140. }
  141. }
  142. }
  143. }