babylon.sceneLoader.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var SceneLoader = (function () {
  4. function SceneLoader() {
  5. }
  6. Object.defineProperty(SceneLoader, "ForceFullSceneLoadingForIncremental", {
  7. get: function () {
  8. return SceneLoader._ForceFullSceneLoadingForIncremental;
  9. },
  10. set: function (value) {
  11. SceneLoader._ForceFullSceneLoadingForIncremental = value;
  12. },
  13. enumerable: true,
  14. configurable: true
  15. });
  16. SceneLoader._getPluginForFilename = function (sceneFilename) {
  17. var dotPosition = sceneFilename.lastIndexOf(".");
  18. var extension = sceneFilename.substring(dotPosition).toLowerCase();
  19. for (var index = 0; index < this._registeredPlugins.length; index++) {
  20. var plugin = this._registeredPlugins[index];
  21. if (plugin.extensions.indexOf(extension) !== -1) {
  22. return plugin;
  23. }
  24. }
  25. return this._registeredPlugins[this._registeredPlugins.length - 1];
  26. };
  27. // Public functions
  28. SceneLoader.RegisterPlugin = function (plugin) {
  29. plugin.extensions = plugin.extensions.toLowerCase();
  30. SceneLoader._registeredPlugins.push(plugin);
  31. };
  32. SceneLoader.ImportMesh = function (meshesNames, rootUrl, sceneFilename, scene, onsuccess, progressCallBack, onerror) {
  33. var _this = this;
  34. var manifestChecked = function (success) {
  35. scene.database = database;
  36. var plugin = _this._getPluginForFilename(sceneFilename);
  37. var importMeshFromData = function (data) {
  38. var meshes = [];
  39. var particleSystems = [];
  40. var skeletons = [];
  41. try {
  42. if (!plugin.importMesh(meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons)) {
  43. if (onerror) {
  44. onerror(scene);
  45. }
  46. return;
  47. }
  48. } catch (e) {
  49. if (onerror) {
  50. onerror(scene);
  51. }
  52. return;
  53. }
  54. if (onsuccess) {
  55. scene.importedMeshesFiles.push(rootUrl + sceneFilename);
  56. onsuccess(meshes, particleSystems, skeletons);
  57. }
  58. };
  59. if (sceneFilename.substr && sceneFilename.substr(0, 5) === "data:") {
  60. // Direct load
  61. importMeshFromData(sceneFilename.substr(5));
  62. return;
  63. }
  64. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, function (data) {
  65. importMeshFromData(data);
  66. }, progressCallBack, database);
  67. };
  68. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  69. var database = new BABYLON.Database(rootUrl + sceneFilename, manifestChecked);
  70. };
  71. /**
  72. * Load a scene
  73. * @param rootUrl a string that defines the root url for scene and resources
  74. * @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
  75. * @param engine is the instance of BABYLON.Engine to use to create the scene
  76. */
  77. SceneLoader.Load = function (rootUrl, sceneFilename, engine, onsuccess, progressCallBack, onerror) {
  78. SceneLoader.Append(rootUrl, sceneFilename, new BABYLON.Scene(engine), onsuccess, progressCallBack, onerror);
  79. };
  80. /**
  81. * Append a scene
  82. * @param rootUrl a string that defines the root url for scene and resources
  83. * @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
  84. * @param scene is the instance of BABYLON.Scene to append to
  85. */
  86. SceneLoader.Append = function (rootUrl, sceneFilename, scene, onsuccess, progressCallBack, onerror) {
  87. var plugin = this._getPluginForFilename(sceneFilename.name || sceneFilename);
  88. var database;
  89. var loadSceneFromData = function (data) {
  90. scene.database = database;
  91. if (!plugin.load(scene, data, rootUrl)) {
  92. if (onerror) {
  93. onerror(scene);
  94. }
  95. return;
  96. }
  97. if (onsuccess) {
  98. onsuccess(scene);
  99. }
  100. };
  101. var manifestChecked = function (success) {
  102. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, loadSceneFromData, progressCallBack, database);
  103. };
  104. if (sceneFilename.substr && sceneFilename.substr(0, 5) === "data:") {
  105. // Direct load
  106. loadSceneFromData(sceneFilename.substr(5));
  107. return;
  108. }
  109. if (rootUrl.indexOf("file:") === -1) {
  110. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  111. database = new BABYLON.Database(rootUrl + sceneFilename, manifestChecked);
  112. } else {
  113. BABYLON.Tools.ReadFile(sceneFilename, loadSceneFromData, progressCallBack);
  114. }
  115. };
  116. SceneLoader._ForceFullSceneLoadingForIncremental = false;
  117. SceneLoader._registeredPlugins = new Array();
  118. return SceneLoader;
  119. })();
  120. BABYLON.SceneLoader = SceneLoader;
  121. ;
  122. })(BABYLON || (BABYLON = {}));
  123. //# sourceMappingURL=babylon.sceneLoader.js.map