babylon.filesInput.ts 5.8 KB

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