babylon.filesInput.js 6.4 KB

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