babylon.filesInput.ts 6.6 KB

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