babylon.filesInput.ts 6.0 KB

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