babylon.sceneLoader.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.SceneLoader = {
  5. _registeredPlugins: [],
  6. _getPluginForFilename: function (sceneFilename) {
  7. var dotPosition = sceneFilename.lastIndexOf(".");
  8. var extension = sceneFilename.substring(dotPosition).toLowerCase();
  9. for (var index = 0; index < this._registeredPlugins.length; index++) {
  10. var plugin = this._registeredPlugins[index];
  11. if (plugin.extensions.indexOf(extension) !== -1) {
  12. return plugin;
  13. }
  14. }
  15. throw new Error("No plugin found to load this file: " + sceneFilename);
  16. },
  17. // Public functions
  18. RegisterPlugin: function (plugin) {
  19. plugin.extensions = plugin.extensions.toLowerCase();
  20. this._registeredPlugins.push(plugin);
  21. },
  22. ImportMesh: function (meshesNames, rootUrl, sceneFilename, scene, onsuccess, progressCallBack, onerror) {
  23. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  24. var database = new BABYLON.Database(rootUrl + sceneFilename);
  25. scene.database = database;
  26. var plugin = this._getPluginForFilename(sceneFilename);
  27. var importMeshFromData = function(data) {
  28. var meshes = [];
  29. var particleSystems = [];
  30. var skeletons = [];
  31. if (!plugin.importMesh(meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons)) {
  32. if (onerror) {
  33. onerror(scene);
  34. }
  35. return;
  36. }
  37. if (onsuccess) {
  38. scene.importedMeshesFiles.push(rootUrl + sceneFilename);
  39. onsuccess(meshes, particleSystems, skeletons);
  40. }
  41. };
  42. if (sceneFilename.substr(0, 5) === "data:") {
  43. // Direct load
  44. importMeshFromData(sceneFilename.substr(5));
  45. return;
  46. }
  47. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, function (data) {
  48. importMeshFromData(data);
  49. }, progressCallBack, database);
  50. },
  51. /**
  52. * Load a scene
  53. * @param rootUrl a string that defines the root url for scene and resources
  54. * @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
  55. * @param engine is the instance of BABYLON.Engine to use to create the scene
  56. */
  57. Load: function (rootUrl, sceneFilename, engine, onsuccess, progressCallBack, onerror) {
  58. var plugin = this._getPluginForFilename(sceneFilename.name || sceneFilename);
  59. var database;
  60. var loadSceneFromData = function (data) {
  61. var scene = new BABYLON.Scene(engine);
  62. scene.database = database;
  63. if (!plugin.load(scene, data, rootUrl)) {
  64. if (onerror) {
  65. onerror(scene);
  66. }
  67. return;
  68. }
  69. if (onsuccess) {
  70. onsuccess(scene);
  71. }
  72. };
  73. if (sceneFilename.substr(0, 5) === "data:") {
  74. // Direct load
  75. loadSceneFromData(sceneFilename.substr(5));
  76. return;
  77. }
  78. if (rootUrl.indexOf("file:") === -1) {
  79. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  80. database = new BABYLON.Database(rootUrl + sceneFilename);
  81. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, loadSceneFromData, progressCallBack, database);
  82. }
  83. // Loading file from disk via input file or drag'n'drop
  84. else {
  85. BABYLON.Tools.ReadFile(sceneFilename, loadSceneFromData, progressCallBack);
  86. }
  87. }
  88. };
  89. })();