babylon.filesInput.js 6.0 KB

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