babylon.filesInput.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. var elementToMonitor;
  5. var engine;
  6. var canvas;
  7. var currentScene;
  8. // elementToMonitor is the HTML element that will listen to drag'n'drop events
  9. // it could be the rendering canvas or whatever element on the page
  10. BABYLON.FilesInput = function (p_engine, p_canvas, p_elementToMonitor) {
  11. if (p_elementToMonitor) {
  12. elementToMonitor = p_elementToMonitor;
  13. elementToMonitor.addEventListener("dragenter", drag, false);
  14. elementToMonitor.addEventListener("dragover", drag, false);
  15. elementToMonitor.addEventListener("drop", drop, false);
  16. }
  17. engine = p_engine;
  18. canvas = p_canvas;
  19. engine.runRenderLoop(renderFunction);
  20. };
  21. function renderFunction() {
  22. if (currentScene) {
  23. currentScene.render();
  24. }
  25. };
  26. function drag(e) {
  27. e.stopPropagation();
  28. e.preventDefault();
  29. };
  30. function drop(eventDrop) {
  31. eventDrop.stopPropagation();
  32. eventDrop.preventDefault();
  33. BABYLON.FilesInput.loadFiles(eventDrop);
  34. };
  35. BABYLON.FilesInput.loadFiles = function (event) {
  36. var sceneFileToLoad;
  37. var filesToLoad;
  38. BABYLON.FilesTextures = {};
  39. // Handling data transfer via drag'n'drop
  40. if (event && event.dataTransfer && event.dataTransfer.files) {
  41. filesToLoad = event.dataTransfer.files;
  42. }
  43. // Handling files from input files
  44. if (event && event.target && event.target.files) {
  45. filesToLoad = event.target.files;
  46. }
  47. if (filesToLoad) {
  48. for (var i = 0; i < filesToLoad.length; i++) {
  49. if (filesToLoad[i].name.indexOf(".babylon") !== -1 && filesToLoad[i].name.indexOf(".manifest") === -1
  50. && filesToLoad[i].name.indexOf(".incremental") === -1 && filesToLoad[i].name.indexOf(".babylonmeshdata") === -1) {
  51. sceneFileToLoad = filesToLoad[i];
  52. }
  53. else {
  54. if (filesToLoad[i].type.indexOf("image/jpeg") == 0 || filesToLoad[i].type.indexOf("image/png") == 0) {
  55. BABYLON.FilesTextures[filesToLoad[i].name] = filesToLoad[i];
  56. }
  57. }
  58. }
  59. // If a ".babylon" file has been provided
  60. if (sceneFileToLoad) {
  61. BABYLON.SceneLoader.Load("file:", sceneFileToLoad, engine, function (newScene) {
  62. if (currentScene) currentScene.dispose();
  63. currentScene = newScene;
  64. // Wait for textures and shaders to be ready
  65. currentScene.executeWhenReady(function () {
  66. // Attach camera to canvas inputs
  67. currentScene.activeCamera.attachControl(canvas);
  68. });
  69. }, function (progress) {
  70. // To do: give progress feedback to user
  71. });
  72. }
  73. else {
  74. console.log("Please provide a valid .babylon file.");
  75. }
  76. }
  77. };
  78. })();