babylon.filesInput.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. 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 (_this.progressCallback) {
  103. _this.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