babylon.filesInput.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var FilesInput = (function () {
  4. /// Register to core BabylonJS object: engine, scene, rendering canvas, callback function when the scene will be loaded,
  5. /// loading progress callback and optionnal addionnal logic to call in the rendering loop
  6. function FilesInput(p_engine, p_scene, p_canvas, p_sceneLoadedCallback, p_progressCallback, p_additionnalRenderLoopLogicCallback, p_textureLoadingCallback, p_startingProcessingFilesCallback) {
  7. this.engine = p_engine;
  8. this.canvas = p_canvas;
  9. this.currentScene = p_scene;
  10. this.sceneLoadedCallback = p_sceneLoadedCallback;
  11. this.progressCallback = p_progressCallback;
  12. this.additionnalRenderLoopLogicCallback = p_additionnalRenderLoopLogicCallback;
  13. this.textureLoadingCallback = p_textureLoadingCallback;
  14. this.startingProcessingFilesCallback = p_startingProcessingFilesCallback;
  15. }
  16. FilesInput.prototype.monitorElementForDragNDrop = function (p_elementToMonitor) {
  17. var _this = this;
  18. if (p_elementToMonitor) {
  19. this.elementToMonitor = p_elementToMonitor;
  20. this.elementToMonitor.addEventListener("dragenter", function (e) {
  21. _this.drag(e);
  22. }, false);
  23. this.elementToMonitor.addEventListener("dragover", function (e) {
  24. _this.drag(e);
  25. }, false);
  26. this.elementToMonitor.addEventListener("drop", function (e) {
  27. _this.drop(e);
  28. }, false);
  29. }
  30. };
  31. FilesInput.prototype.renderFunction = function () {
  32. if (this.additionnalRenderLoopLogicCallback) {
  33. this.additionnalRenderLoopLogicCallback();
  34. }
  35. if (this.currentScene) {
  36. if (this.textureLoadingCallback) {
  37. var remaining = this.currentScene.getWaitingItemsCount();
  38. if (remaining > 0) {
  39. this.textureLoadingCallback(remaining);
  40. }
  41. }
  42. this.currentScene.render();
  43. }
  44. };
  45. FilesInput.prototype.drag = function (e) {
  46. e.stopPropagation();
  47. e.preventDefault();
  48. };
  49. FilesInput.prototype.drop = function (eventDrop) {
  50. eventDrop.stopPropagation();
  51. eventDrop.preventDefault();
  52. this.loadFiles(eventDrop);
  53. };
  54. FilesInput.prototype.loadFiles = function (event) {
  55. var _this = this;
  56. var that = this;
  57. if (this.startingProcessingFilesCallback)
  58. this.startingProcessingFilesCallback();
  59. var sceneFileToLoad;
  60. var filesToLoad;
  61. // Handling data transfer via drag'n'drop
  62. if (event && event.dataTransfer && event.dataTransfer.files) {
  63. filesToLoad = event.dataTransfer.files;
  64. }
  65. // Handling files from input files
  66. if (event && event.target && event.target.files) {
  67. filesToLoad = event.target.files;
  68. }
  69. if (filesToLoad && filesToLoad.length > 0) {
  70. for (var i = 0; i < filesToLoad.length; i++) {
  71. switch (filesToLoad[i].type) {
  72. case "image/jpeg":
  73. case "image/png":
  74. case "image/bmp":
  75. FilesInput.FilesTextures[filesToLoad[i].name] = filesToLoad[i];
  76. break;
  77. case "image/targa":
  78. case "image/vnd.ms-dds":
  79. case "audio/wav":
  80. case "audio/x-wav":
  81. case "audio/mp3":
  82. case "audio/mpeg":
  83. case "audio/mpeg3":
  84. case "audio/x-mpeg-3":
  85. case "audio/ogg":
  86. 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 && filesToLoad[i].name.indexOf(".incremental") === -1 && filesToLoad[i].name.indexOf(".babylonmeshdata") === -1 && filesToLoad[i].name.indexOf(".babylongeometrydata") === -1) {
  90. sceneFileToLoad = filesToLoad[i];
  91. }
  92. break;
  93. }
  94. }
  95. // If a ".babylon" file has been provided
  96. if (sceneFileToLoad) {
  97. if (this.currentScene) {
  98. this.engine.stopRenderLoop();
  99. this.currentScene.dispose();
  100. }
  101. BABYLON.SceneLoader.Load("file:", sceneFileToLoad, this.engine, function (newScene) {
  102. that.currentScene = newScene;
  103. // Wait for textures and shaders to be ready
  104. that.currentScene.executeWhenReady(function () {
  105. // Attach camera to canvas inputs
  106. if (that.currentScene.activeCamera) {
  107. that.currentScene.activeCamera.attachControl(that.canvas);
  108. }
  109. if (that.sceneLoadedCallback) {
  110. that.sceneLoadedCallback(sceneFileToLoad, that.currentScene);
  111. }
  112. that.engine.runRenderLoop(function () {
  113. that.renderFunction();
  114. });
  115. });
  116. }, function (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. FilesInput.FilesTextures = new Array();
  128. FilesInput.FilesToLoad = new Array();
  129. return FilesInput;
  130. })();
  131. BABYLON.FilesInput = FilesInput;
  132. })(BABYLON || (BABYLON = {}));
  133. //# sourceMappingURL=babylon.filesInput.js.map