babylon.filesInput.js 7.0 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) { _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] = 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] = this._filesToLoad[i];
  77. break;
  78. default:
  79. if ((this._filesToLoad[i].name.indexOf(".babylon") !== -1 || this._filesToLoad[i].name.indexOf(".stl") !== -1 ||
  80. this._filesToLoad[i].name.indexOf(".obj") !== -1 || this._filesToLoad[i].name.indexOf(".mtl") !== -1)
  81. && this._filesToLoad[i].name.indexOf(".manifest") === -1
  82. && this._filesToLoad[i].name.indexOf(".incremental") === -1 && this._filesToLoad[i].name.indexOf(".babylonmeshdata") === -1
  83. && this._filesToLoad[i].name.indexOf(".babylongeometrydata") === -1 && this._filesToLoad[i].name.indexOf(".babylonbinarymeshdata") === -1 &&
  84. this._filesToLoad[i].name.indexOf(".binary.babylon") === -1) {
  85. this._sceneFileToLoad = this._filesToLoad[i];
  86. }
  87. break;
  88. }
  89. }
  90. this.reload();
  91. }
  92. };
  93. FilesInput.prototype.reload = function () {
  94. var _this = this;
  95. var that = this;
  96. // If a ".babylon" file has been provided
  97. if (this._sceneFileToLoad) {
  98. if (this._currentScene) {
  99. if (BABYLON.Tools.errorsCount > 0) {
  100. BABYLON.Tools.ClearLogCache();
  101. BABYLON.Tools.Log("Babylon.js engine (v" + BABYLON.Engine.Version + ") launched");
  102. }
  103. this._engine.stopRenderLoop();
  104. this._currentScene.dispose();
  105. }
  106. BABYLON.SceneLoader.Load("file:", this._sceneFileToLoad, this._engine, function (newScene) {
  107. that._currentScene = newScene;
  108. // Wait for textures and shaders to be ready
  109. that._currentScene.executeWhenReady(function () {
  110. // Attach camera to canvas inputs
  111. if (!that._currentScene.activeCamera || that._currentScene.lights.length === 0) {
  112. that._currentScene.createDefaultCameraOrLight();
  113. }
  114. that._currentScene.activeCamera.attachControl(that._canvas);
  115. if (that._sceneLoadedCallback) {
  116. that._sceneLoadedCallback(_this._sceneFileToLoad, that._currentScene);
  117. }
  118. that._engine.runRenderLoop(function () { that.renderFunction(); });
  119. });
  120. }, function (progress) {
  121. if (_this._progressCallback) {
  122. _this._progressCallback(progress);
  123. }
  124. });
  125. }
  126. else {
  127. BABYLON.Tools.Error("Please provide a valid .babylon file.");
  128. }
  129. };
  130. FilesInput.FilesTextures = new Array();
  131. FilesInput.FilesToLoad = new Array();
  132. return FilesInput;
  133. })();
  134. BABYLON.FilesInput = FilesInput;
  135. })(BABYLON || (BABYLON = {}));