babylon.filesInput.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. if (this._startingProcessingFilesCallback)
  56. this._startingProcessingFilesCallback();
  57. // Handling data transfer via drag'n'drop
  58. if (event && event.dataTransfer && event.dataTransfer.files) {
  59. this._filesToLoad = event.dataTransfer.files;
  60. }
  61. // Handling files from input files
  62. if (event && event.target && event.target.files) {
  63. this._filesToLoad = event.target.files;
  64. }
  65. if (this._filesToLoad && this._filesToLoad.length > 0) {
  66. for (var i = 0; i < this._filesToLoad.length; i++) {
  67. switch (this._filesToLoad[i].type) {
  68. case "image/jpeg":
  69. case "image/png":
  70. case "image/bmp":
  71. FilesInput.FilesTextures[this._filesToLoad[i].name] = this._filesToLoad[i];
  72. break;
  73. case "image/targa":
  74. case "image/vnd.ms-dds":
  75. case "audio/wav":
  76. case "audio/x-wav":
  77. case "audio/mp3":
  78. case "audio/mpeg":
  79. case "audio/mpeg3":
  80. case "audio/x-mpeg-3":
  81. case "audio/ogg":
  82. FilesInput.FilesToLoad[this._filesToLoad[i].name] = this._filesToLoad[i];
  83. break;
  84. default:
  85. if (this._filesToLoad[i].name.indexOf(".babylon") !== -1 && this._filesToLoad[i].name.indexOf(".manifest") === -1 && this._filesToLoad[i].name.indexOf(".incremental") === -1 && this._filesToLoad[i].name.indexOf(".babylonmeshdata") === -1 && this._filesToLoad[i].name.indexOf(".babylongeometrydata") === -1) {
  86. this._sceneFileToLoad = this._filesToLoad[i];
  87. }
  88. break;
  89. }
  90. }
  91. this.reload();
  92. }
  93. };
  94. FilesInput.prototype.reload = function () {
  95. var _this = this;
  96. var that = this;
  97. // If a ".babylon" file has been provided
  98. if (this._sceneFileToLoad) {
  99. if (this._currentScene) {
  100. this._engine.stopRenderLoop();
  101. this._currentScene.dispose();
  102. }
  103. BABYLON.SceneLoader.Load("file:", this._sceneFileToLoad, this._engine, function (newScene) {
  104. that._currentScene = newScene;
  105. // Wait for textures and shaders to be ready
  106. that._currentScene.executeWhenReady(function () {
  107. // Attach camera to canvas inputs
  108. if (!that._currentScene.activeCamera || that._currentScene.lights.length === 0) {
  109. that._currentScene.createDefaultCameraOrLight();
  110. }
  111. that._currentScene.activeCamera.attachControl(that._canvas);
  112. if (that._sceneLoadedCallback) {
  113. that._sceneLoadedCallback(_this._sceneFileToLoad, that._currentScene);
  114. }
  115. that._engine.runRenderLoop(function () {
  116. that.renderFunction();
  117. });
  118. });
  119. }, function (progress) {
  120. if (_this._progressCallback) {
  121. _this._progressCallback(progress);
  122. }
  123. });
  124. }
  125. else {
  126. BABYLON.Tools.Error("Please provide a valid .babylon file.");
  127. }
  128. };
  129. FilesInput.FilesTextures = new Array();
  130. FilesInput.FilesToLoad = new Array();
  131. return FilesInput;
  132. })();
  133. BABYLON.FilesInput = FilesInput;
  134. })(BABYLON || (BABYLON = {}));
  135. //# sourceMappingURL=babylon.filesInput.js.map