babylon.filesInput.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // ANY
  2. declare module BABYLON {
  3. export class SceneLoader {
  4. static Load: (param1: any, param2: any, param3: any, param4: any, param5: any) => void;
  5. }
  6. }
  7. module BABYLON {
  8. export class FilesInput {
  9. private engine: BABYLON.Engine;
  10. private currentScene: BABYLON.Scene;
  11. private canvas: HTMLCanvasElement;
  12. private sceneLoadedCallback;
  13. private progressCallback;
  14. private additionnalRenderLoopLogicCallback;
  15. private textureLoadingCallback;
  16. private startingProcessingFilesCallback;
  17. private elementToMonitor: HTMLElement;
  18. public static FilesTextures: any[] = new Array();
  19. public static FilesToLoad: any[] = new Array();
  20. /// Register to core BabylonJS object: engine, scene, rendering canvas, callback function when the scene will be loaded,
  21. /// loading progress callback and optionnal addionnal logic to call in the rendering loop
  22. constructor(p_engine: BABYLON.Engine, p_scene: BABYLON.Scene, p_canvas: HTMLCanvasElement, p_sceneLoadedCallback,
  23. p_progressCallback, p_additionnalRenderLoopLogicCallback, p_textureLoadingCallback, p_startingProcessingFilesCallback) {
  24. this.engine = p_engine;
  25. this.canvas = p_canvas;
  26. this.currentScene = p_scene;
  27. this.sceneLoadedCallback = p_sceneLoadedCallback;
  28. this.progressCallback = p_progressCallback;
  29. this.additionnalRenderLoopLogicCallback = p_additionnalRenderLoopLogicCallback;
  30. this.textureLoadingCallback = p_textureLoadingCallback;
  31. this.startingProcessingFilesCallback = p_startingProcessingFilesCallback;
  32. }
  33. public monitorElementForDragNDrop(p_elementToMonitor: HTMLElement): void {
  34. if (p_elementToMonitor) {
  35. this.elementToMonitor = p_elementToMonitor;
  36. this.elementToMonitor.addEventListener("dragenter", (e) => { this.drag(e); }, false);
  37. this.elementToMonitor.addEventListener("dragover", (e) => { this.drag(e); }, false);
  38. this.elementToMonitor.addEventListener("drop", (e) => { this.drop(e); }, false);
  39. }
  40. }
  41. private renderFunction(): void {
  42. if (this.additionnalRenderLoopLogicCallback) {
  43. this.additionnalRenderLoopLogicCallback();
  44. }
  45. if (this.currentScene) {
  46. if (this.textureLoadingCallback) {
  47. var remaining = this.currentScene.getWaitingItemsCount();
  48. if (remaining > 0) {
  49. this.textureLoadingCallback(remaining);
  50. }
  51. }
  52. this.currentScene.render();
  53. }
  54. }
  55. private drag(e): void {
  56. e.stopPropagation();
  57. e.preventDefault();
  58. }
  59. private drop(eventDrop): void {
  60. eventDrop.stopPropagation();
  61. eventDrop.preventDefault();
  62. this.loadFiles(eventDrop);
  63. }
  64. private loadFiles(event): void {
  65. var that = this;
  66. if (this.startingProcessingFilesCallback) this.startingProcessingFilesCallback();
  67. var sceneFileToLoad: File;
  68. var filesToLoad: File[];
  69. // Handling data transfer via drag'n'drop
  70. if (event && event.dataTransfer && event.dataTransfer.files) {
  71. filesToLoad = event.dataTransfer.files;
  72. }
  73. // Handling files from input files
  74. if (event && event.target && event.target.files) {
  75. filesToLoad = event.target.files;
  76. }
  77. if (filesToLoad && filesToLoad.length > 0) {
  78. for (var i = 0; i < filesToLoad.length; i++) {
  79. switch (filesToLoad[i].type) {
  80. case "image/jpeg":
  81. case "image/png":
  82. BABYLON.FilesInput.FilesTextures[filesToLoad[i].name] = filesToLoad[i];
  83. break;
  84. case "image/targa":
  85. case "image/vnd.ms-dds":
  86. BABYLON.FilesInput.FilesToLoad[filesToLoad[i].name] = filesToLoad[i];
  87. break;
  88. default:
  89. if (filesToLoad[i].name.indexOf(".babylon") !== -1 && filesToLoad[i].name.indexOf(".manifest") === -1
  90. && filesToLoad[i].name.indexOf(".incremental") === -1 && filesToLoad[i].name.indexOf(".babylonmeshdata") === -1
  91. && filesToLoad[i].name.indexOf(".babylongeometrydata") === -1) {
  92. sceneFileToLoad = filesToLoad[i];
  93. }
  94. break;
  95. }
  96. }
  97. // If a ".babylon" file has been provided
  98. if (sceneFileToLoad) {
  99. if (this.currentScene) {
  100. this.engine.stopRenderLoop();
  101. this.currentScene.dispose();
  102. }
  103. BABYLON.SceneLoader.Load("file:", sceneFileToLoad, this.engine, (newScene) => {
  104. that.currentScene = newScene;
  105. // Wait for textures and shaders to be ready
  106. that.currentScene.executeWhenReady(() => {
  107. // Attach camera to canvas inputs
  108. if (that.currentScene.activeCamera) {
  109. that.currentScene.activeCamera.attachControl(that.canvas);
  110. }
  111. if (that.sceneLoadedCallback) {
  112. that.sceneLoadedCallback(sceneFileToLoad, that.currentScene);
  113. }
  114. that.engine.runRenderLoop(() => { that.renderFunction(); });
  115. });
  116. }, progress => {
  117. if (this.progressCallback) {
  118. this.progressCallback(progress);
  119. }
  120. });
  121. }
  122. else {
  123. BABYLON.Tools.Error("Please provide a valid .babylon file.");
  124. }
  125. }
  126. }
  127. }
  128. }