babylon.filesInput.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // We're mainly based on the logic defined into the FreeCamera code
  9. export class FilesInput {
  10. private engine: BABYLON.Engine;
  11. private currentScene: BABYLON.Scene;
  12. private canvas: HTMLCanvasElement;
  13. private sceneLoadedCallback;
  14. private progressCallback;
  15. private additionnalRenderLoopLogicCallback;
  16. private textureLoadingCallback;
  17. private startingProcessingFilesCallback;
  18. private elementToMonitor: HTMLElement;
  19. public static FilesTextures: 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. if (filesToLoad[i].name.indexOf(".babylon") !== -1 && filesToLoad[i].name.indexOf(".manifest") === -1
  80. && filesToLoad[i].name.indexOf(".incremental") === -1 && filesToLoad[i].name.indexOf(".babylonmeshdata") === -1
  81. && filesToLoad[i].name.indexOf(".babylongeometrydata") === -1) {
  82. sceneFileToLoad = filesToLoad[i];
  83. }
  84. else {
  85. if (filesToLoad[i].type.indexOf("image/jpeg") == 0 || filesToLoad[i].type.indexOf("image/png") == 0) {
  86. BABYLON.FilesInput.FilesTextures[filesToLoad[i].name] = filesToLoad[i];
  87. }
  88. }
  89. }
  90. // If a ".babylon" file has been provided
  91. if (sceneFileToLoad) {
  92. if (this.currentScene) {
  93. this.engine.stopRenderLoop();
  94. this.currentScene.dispose();
  95. }
  96. BABYLON.SceneLoader.Load("file:", sceneFileToLoad, this.engine, (newScene) => {
  97. that.currentScene = newScene;
  98. // Wait for textures and shaders to be ready
  99. that.currentScene.executeWhenReady(function () {
  100. // Attach camera to canvas inputs
  101. if (that.currentScene.activeCamera) {
  102. that.currentScene.activeCamera.attachControl(that.canvas);
  103. }
  104. if (that.sceneLoadedCallback) {
  105. that.sceneLoadedCallback(sceneFileToLoad, that.currentScene);
  106. }
  107. that.engine.runRenderLoop(function () { that.renderFunction() });
  108. });
  109. }, function (progress) {
  110. if (that.progressCallback) {
  111. that.progressCallback(progress);
  112. }
  113. });
  114. }
  115. else {
  116. BABYLON.Tools.Error("Please provide a valid .babylon file.");
  117. }
  118. }
  119. }
  120. }
  121. }