babylon.filesInput.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. var that;
  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. BABYLON.FilesInput = function (p_engine, p_scene, p_canvas, p_sceneLoadedCallback,
  8. p_progressCallback, p_additionnalRenderLoopLogicCallback, p_textureLoadingCallback, p_startingProcessingFilesCallback) {
  9. that = this;
  10. this.engine = p_engine;
  11. this.canvas = p_canvas;
  12. this.currentScene = p_scene;
  13. this.sceneLoadedCallback = p_sceneLoadedCallback;
  14. this.progressCallback = p_progressCallback;
  15. this.additionnalRenderLoopLogicCallback = p_additionnalRenderLoopLogicCallback;
  16. this.textureLoadingCallback = p_textureLoadingCallback;
  17. this.startingProcessingFilesCallback = p_startingProcessingFilesCallback;
  18. this.engine.runRenderLoop(renderFunction);
  19. };
  20. // elementToMonitor is the HTML element that will listen to drag'n'drop events
  21. // it could be the rendering canvas or whatever element on the page
  22. BABYLON.FilesInput.prototype.monitorElementForDragNDrop = function (p_elementToMonitor) {
  23. if (p_elementToMonitor) {
  24. this.elementToMonitor = p_elementToMonitor;
  25. this.elementToMonitor.addEventListener("dragenter", drag, false);
  26. this.elementToMonitor.addEventListener("dragover", drag, false);
  27. this.elementToMonitor.addEventListener("drop", drop, false);
  28. }
  29. };
  30. function renderFunction() {
  31. if (that.additionnalRenderLoopLogicCallback) {
  32. that.additionnalRenderLoopLogicCallback();
  33. }
  34. if (that.currentScene) {
  35. if (that.textureLoadingCallback) {
  36. var remaining = that.currentScene.getWaitingItemsCount();
  37. if (remaining > 0) {
  38. that.textureLoadingCallback(remaining);
  39. }
  40. }
  41. that.currentScene.render();
  42. }
  43. };
  44. function drag(e) {
  45. e.stopPropagation();
  46. e.preventDefault();
  47. };
  48. function drop(eventDrop) {
  49. eventDrop.stopPropagation();
  50. eventDrop.preventDefault();
  51. that.loadFiles(eventDrop);
  52. };
  53. BABYLON.FilesInput.prototype.loadFiles = function (event) {
  54. if (that.startingProcessingFilesCallback) that.startingProcessingFilesCallback();
  55. var sceneFileToLoad;
  56. var filesToLoad;
  57. BABYLON.FilesTextures = {};
  58. // Handling data transfer via drag'n'drop
  59. if (event && event.dataTransfer && event.dataTransfer.files) {
  60. filesToLoad = event.dataTransfer.files;
  61. }
  62. // Handling files from input files
  63. if (event && event.target && event.target.files) {
  64. filesToLoad = event.target.files;
  65. }
  66. if (filesToLoad && filesToLoad.length > 0) {
  67. for (var i = 0; i < filesToLoad.length; i++) {
  68. if (filesToLoad[i].name.indexOf(".babylon") !== -1 && filesToLoad[i].name.indexOf(".manifest") === -1
  69. && filesToLoad[i].name.indexOf(".incremental") === -1 && filesToLoad[i].name.indexOf(".babylonmeshdata") === -1) {
  70. sceneFileToLoad = filesToLoad[i];
  71. }
  72. else {
  73. if (filesToLoad[i].type.indexOf("image/jpeg") == 0 || filesToLoad[i].type.indexOf("image/png") == 0) {
  74. BABYLON.FilesTextures[filesToLoad[i].name] = filesToLoad[i];
  75. }
  76. }
  77. }
  78. // If a ".babylon" file has been provided
  79. if (sceneFileToLoad) {
  80. if (that.currentScene) {
  81. that.currentScene.dispose();
  82. }
  83. BABYLON.SceneLoader.Load("file:", sceneFileToLoad, that.engine, function (newScene) {
  84. that.currentScene = newScene;
  85. // Wait for textures and shaders to be ready
  86. that.currentScene.executeWhenReady(function () {
  87. // Attach camera to canvas inputs
  88. that.currentScene.activeCamera.attachControl(that.canvas);
  89. if (that.sceneLoadedCallback) {
  90. that.sceneLoadedCallback(sceneFileToLoad, that.currentScene);
  91. }
  92. });
  93. }, function (progress) {
  94. if (that.progressCallback) {
  95. that.progressCallback(progress);
  96. }
  97. });
  98. }
  99. else {
  100. console.log("Please provide a valid .babylon file.");
  101. }
  102. }
  103. };
  104. })();