babylon.filesInput.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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) { _this.drag(e); }, false);
  21. this._elementToMonitor.addEventListener("dragover", function (e) { _this.drag(e); }, false);
  22. this._elementToMonitor.addEventListener("drop", function (e) { _this.drop(e); }, false);
  23. }
  24. };
  25. FilesInput.prototype.renderFunction = function () {
  26. if (this._additionnalRenderLoopLogicCallback) {
  27. this._additionnalRenderLoopLogicCallback();
  28. }
  29. if (this._currentScene) {
  30. if (this._textureLoadingCallback) {
  31. var remaining = this._currentScene.getWaitingItemsCount();
  32. if (remaining > 0) {
  33. this._textureLoadingCallback(remaining);
  34. }
  35. }
  36. this._currentScene.render();
  37. }
  38. };
  39. FilesInput.prototype.drag = function (e) {
  40. e.stopPropagation();
  41. e.preventDefault();
  42. };
  43. FilesInput.prototype.drop = function (eventDrop) {
  44. eventDrop.stopPropagation();
  45. eventDrop.preventDefault();
  46. this.loadFiles(eventDrop);
  47. };
  48. FilesInput.prototype.loadFiles = function (event) {
  49. if (this._startingProcessingFilesCallback)
  50. this._startingProcessingFilesCallback();
  51. // Handling data transfer via drag'n'drop
  52. if (event && event.dataTransfer && event.dataTransfer.files) {
  53. this._filesToLoad = event.dataTransfer.files;
  54. }
  55. // Handling files from input files
  56. if (event && event.target && event.target.files) {
  57. this._filesToLoad = event.target.files;
  58. }
  59. if (this._filesToLoad && this._filesToLoad.length > 0) {
  60. for (var i = 0; i < this._filesToLoad.length; i++) {
  61. switch (this._filesToLoad[i].type) {
  62. case "image/jpeg":
  63. case "image/png":
  64. case "image/bmp":
  65. FilesInput.FilesTextures[this._filesToLoad[i].name.toLowerCase()] = this._filesToLoad[i];
  66. break;
  67. case "image/targa":
  68. case "image/vnd.ms-dds":
  69. case "audio/wav":
  70. case "audio/x-wav":
  71. case "audio/mp3":
  72. case "audio/mpeg":
  73. case "audio/mpeg3":
  74. case "audio/x-mpeg-3":
  75. case "audio/ogg":
  76. FilesInput.FilesToLoad[this._filesToLoad[i].name.toLowerCase()] = this._filesToLoad[i];
  77. break;
  78. default:
  79. if (this._filesToLoad[i].name.indexOf(".mtl") !== -1) {
  80. FilesInput.FilesToLoad[this._filesToLoad[i].name.toLowerCase()] = this._filesToLoad[i];
  81. }
  82. else if ((this._filesToLoad[i].name.indexOf(".babylon") !== -1 ||
  83. this._filesToLoad[i].name.indexOf(".stl") !== -1 ||
  84. this._filesToLoad[i].name.indexOf(".obj") !== -1)
  85. && this._filesToLoad[i].name.indexOf(".manifest") === -1
  86. && this._filesToLoad[i].name.indexOf(".incremental") === -1 && this._filesToLoad[i].name.indexOf(".babylonmeshdata") === -1
  87. && this._filesToLoad[i].name.indexOf(".babylongeometrydata") === -1 && this._filesToLoad[i].name.indexOf(".babylonbinarymeshdata") === -1 &&
  88. this._filesToLoad[i].name.indexOf(".binary.babylon") === -1) {
  89. this._sceneFileToLoad = this._filesToLoad[i];
  90. }
  91. break;
  92. }
  93. }
  94. this.reload();
  95. }
  96. };
  97. FilesInput.prototype.reload = function () {
  98. var _this = this;
  99. var that = this;
  100. // If a ".babylon" file has been provided
  101. if (this._sceneFileToLoad) {
  102. if (this._currentScene) {
  103. if (BABYLON.Tools.errorsCount > 0) {
  104. BABYLON.Tools.ClearLogCache();
  105. BABYLON.Tools.Log("Babylon.js engine (v" + BABYLON.Engine.Version + ") launched");
  106. }
  107. this._engine.stopRenderLoop();
  108. this._currentScene.dispose();
  109. }
  110. BABYLON.SceneLoader.Load("file:", this._sceneFileToLoad, this._engine, function (newScene) {
  111. that._currentScene = newScene;
  112. // Wait for textures and shaders to be ready
  113. that._currentScene.executeWhenReady(function () {
  114. // Attach camera to canvas inputs
  115. if (!that._currentScene.activeCamera || that._currentScene.lights.length === 0) {
  116. that._currentScene.createDefaultCameraOrLight();
  117. }
  118. that._currentScene.activeCamera.attachControl(that._canvas);
  119. if (that._sceneLoadedCallback) {
  120. that._sceneLoadedCallback(_this._sceneFileToLoad, that._currentScene);
  121. }
  122. that._engine.runRenderLoop(function () { that.renderFunction(); });
  123. });
  124. }, function (progress) {
  125. if (_this._progressCallback) {
  126. _this._progressCallback(progress);
  127. }
  128. });
  129. }
  130. else {
  131. BABYLON.Tools.Error("Please provide a valid .babylon file.");
  132. }
  133. };
  134. FilesInput.FilesTextures = new Array();
  135. FilesInput.FilesToLoad = new Array();
  136. return FilesInput;
  137. }());
  138. BABYLON.FilesInput = FilesInput;
  139. })(BABYLON || (BABYLON = {}));