babylon.filesInput.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 
  2. var BABYLON;
  3. (function (BABYLON) {
  4. // We're mainly based on the logic defined into the FreeCamera code
  5. var FilesInput = (function () {
  6. /// Register to core BabylonJS object: engine, scene, rendering canvas, callback function when the scene will be loaded,
  7. /// loading progress callback and optionnal addionnal logic to call in the rendering loop
  8. function FilesInput(p_engine, p_scene, p_canvas, p_sceneLoadedCallback, p_progressCallback, p_additionnalRenderLoopLogicCallback, p_textureLoadingCallback, p_startingProcessingFilesCallback) {
  9. this.engine = p_engine;
  10. this.canvas = p_canvas;
  11. this.currentScene = p_scene;
  12. this.sceneLoadedCallback = p_sceneLoadedCallback;
  13. this.progressCallback = p_progressCallback;
  14. this.additionnalRenderLoopLogicCallback = p_additionnalRenderLoopLogicCallback;
  15. this.textureLoadingCallback = p_textureLoadingCallback;
  16. this.startingProcessingFilesCallback = p_startingProcessingFilesCallback;
  17. }
  18. FilesInput.prototype.monitorElementForDragNDrop = function (p_elementToMonitor) {
  19. var _this = this;
  20. if (p_elementToMonitor) {
  21. this.elementToMonitor = p_elementToMonitor;
  22. this.elementToMonitor.addEventListener("dragenter", function (e) {
  23. _this.drag(e);
  24. }, false);
  25. this.elementToMonitor.addEventListener("dragover", function (e) {
  26. _this.drag(e);
  27. }, false);
  28. this.elementToMonitor.addEventListener("drop", function (e) {
  29. _this.drop(e);
  30. }, false);
  31. }
  32. };
  33. FilesInput.prototype.renderFunction = function () {
  34. if (this.additionnalRenderLoopLogicCallback) {
  35. this.additionnalRenderLoopLogicCallback();
  36. }
  37. if (this.currentScene) {
  38. if (this.textureLoadingCallback) {
  39. var remaining = this.currentScene.getWaitingItemsCount();
  40. if (remaining > 0) {
  41. this.textureLoadingCallback(remaining);
  42. }
  43. }
  44. this.currentScene.render();
  45. }
  46. };
  47. FilesInput.prototype.drag = function (e) {
  48. e.stopPropagation();
  49. e.preventDefault();
  50. };
  51. FilesInput.prototype.drop = function (eventDrop) {
  52. eventDrop.stopPropagation();
  53. eventDrop.preventDefault();
  54. this.loadFiles(eventDrop);
  55. };
  56. FilesInput.prototype.loadFiles = function (event) {
  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. 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) {
  73. sceneFileToLoad = filesToLoad[i];
  74. } else {
  75. if (filesToLoad[i].type.indexOf("image/jpeg") == 0 || filesToLoad[i].type.indexOf("image/png") == 0) {
  76. BABYLON.FilesInput.FilesTextures[filesToLoad[i].name] = filesToLoad[i];
  77. }
  78. }
  79. }
  80. // If a ".babylon" file has been provided
  81. if (sceneFileToLoad) {
  82. if (this.currentScene) {
  83. this.engine.stopRenderLoop();
  84. this.currentScene.dispose();
  85. }
  86. BABYLON.SceneLoader.Load("file:", sceneFileToLoad, this.engine, function (newScene) {
  87. that.currentScene = newScene;
  88. // Wait for textures and shaders to be ready
  89. that.currentScene.executeWhenReady(function () {
  90. // Attach camera to canvas inputs
  91. if (that.currentScene.activeCamera) {
  92. that.currentScene.activeCamera.attachControl(that.canvas);
  93. }
  94. if (that.sceneLoadedCallback) {
  95. that.sceneLoadedCallback(sceneFileToLoad, that.currentScene);
  96. }
  97. that.engine.runRenderLoop(function () {
  98. that.renderFunction();
  99. });
  100. });
  101. }, function (progress) {
  102. if (that.progressCallback) {
  103. that.progressCallback(progress);
  104. }
  105. });
  106. } else {
  107. BABYLON.Tools.Error("Please provide a valid .babylon file.");
  108. }
  109. }
  110. };
  111. FilesInput.FilesTextures = new Array();
  112. return FilesInput;
  113. })();
  114. BABYLON.FilesInput = FilesInput;
  115. })(BABYLON || (BABYLON = {}));
  116. //# sourceMappingURL=babylon.filesInput.js.map