babylon.filesInput.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. };
  19. // elementToMonitor is the HTML element that will listen to drag'n'drop events
  20. // it could be the rendering canvas or whatever element on the page
  21. BABYLON.FilesInput.prototype.monitorElementForDragNDrop = function (p_elementToMonitor) {
  22. if (p_elementToMonitor) {
  23. this.elementToMonitor = p_elementToMonitor;
  24. this.elementToMonitor.addEventListener("dragenter", drag, false);
  25. this.elementToMonitor.addEventListener("dragover", drag, false);
  26. this.elementToMonitor.addEventListener("drop", drop, false);
  27. }
  28. };
  29. function renderFunction() {
  30. if (that.additionnalRenderLoopLogicCallback) {
  31. that.additionnalRenderLoopLogicCallback();
  32. }
  33. if (that.currentScene) {
  34. if (that.textureLoadingCallback) {
  35. var remaining = that.currentScene.getWaitingItemsCount();
  36. if (remaining > 0) {
  37. that.textureLoadingCallback(remaining);
  38. }
  39. }
  40. that.currentScene.render();
  41. }
  42. };
  43. function drag(e) {
  44. e.stopPropagation();
  45. e.preventDefault();
  46. };
  47. function drop(eventDrop) {
  48. eventDrop.stopPropagation();
  49. eventDrop.preventDefault();
  50. that.loadFiles(eventDrop);
  51. };
  52. BABYLON.FilesInput.prototype.loadFiles = function (event) {
  53. if (that.startingProcessingFilesCallback) that.startingProcessingFilesCallback();
  54. var sceneFileToLoad;
  55. var filesToLoad;
  56. BABYLON.FilesTextures = {};
  57. // Handling data transfer via drag'n'drop
  58. if (event && event.dataTransfer && event.dataTransfer.files) {
  59. filesToLoad = event.dataTransfer.files;
  60. }
  61. // Handling files from input files
  62. if (event && event.target && event.target.files) {
  63. filesToLoad = event.target.files;
  64. }
  65. if (filesToLoad && filesToLoad.length > 0) {
  66. for (var i = 0; i < filesToLoad.length; i++) {
  67. if (filesToLoad[i].name.indexOf(".babylon") !== -1 && filesToLoad[i].name.indexOf(".manifest") === -1
  68. && filesToLoad[i].name.indexOf(".incremental") === -1 && filesToLoad[i].name.indexOf(".babylonmeshdata") === -1) {
  69. sceneFileToLoad = filesToLoad[i];
  70. }
  71. else {
  72. if (filesToLoad[i].type.indexOf("image/jpeg") == 0 || filesToLoad[i].type.indexOf("image/png") == 0) {
  73. BABYLON.FilesTextures[filesToLoad[i].name] = filesToLoad[i];
  74. }
  75. }
  76. }
  77. // If a ".babylon" file has been provided
  78. if (sceneFileToLoad) {
  79. if (that.currentScene) {
  80. that.engine.stopRenderLoop();
  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. if (that.currentScene.activeCamera) {
  89. that.currentScene.activeCamera.attachControl(that.canvas);
  90. }
  91. if (that.sceneLoadedCallback) {
  92. that.sceneLoadedCallback(sceneFileToLoad, that.currentScene);
  93. }
  94. that.engine.runRenderLoop(renderFunction);
  95. });
  96. }, function (progress) {
  97. if (that.progressCallback) {
  98. that.progressCallback(progress);
  99. }
  100. });
  101. }
  102. else {
  103. console.log("Please provide a valid .babylon file.");
  104. }
  105. }
  106. };
  107. })();