babylon.sceneLoader.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var SceneLoader = (function () {
  4. function SceneLoader() {
  5. }
  6. Object.defineProperty(SceneLoader, "NO_LOGGING", {
  7. get: function () {
  8. return 0;
  9. },
  10. enumerable: true,
  11. configurable: true
  12. });
  13. Object.defineProperty(SceneLoader, "MINIMAL_LOGGING", {
  14. get: function () {
  15. return 1;
  16. },
  17. enumerable: true,
  18. configurable: true
  19. });
  20. Object.defineProperty(SceneLoader, "SUMMARY_LOGGING", {
  21. get: function () {
  22. return 2;
  23. },
  24. enumerable: true,
  25. configurable: true
  26. });
  27. Object.defineProperty(SceneLoader, "DETAILED_LOGGING", {
  28. get: function () {
  29. return 3;
  30. },
  31. enumerable: true,
  32. configurable: true
  33. });
  34. Object.defineProperty(SceneLoader, "ForceFullSceneLoadingForIncremental", {
  35. get: function () {
  36. return SceneLoader._ForceFullSceneLoadingForIncremental;
  37. },
  38. set: function (value) {
  39. SceneLoader._ForceFullSceneLoadingForIncremental = value;
  40. },
  41. enumerable: true,
  42. configurable: true
  43. });
  44. Object.defineProperty(SceneLoader, "ShowLoadingScreen", {
  45. get: function () {
  46. return SceneLoader._ShowLoadingScreen;
  47. },
  48. set: function (value) {
  49. SceneLoader._ShowLoadingScreen = value;
  50. },
  51. enumerable: true,
  52. configurable: true
  53. });
  54. Object.defineProperty(SceneLoader, "loggingLevel", {
  55. get: function () {
  56. return SceneLoader._loggingLevel;
  57. },
  58. set: function (value) {
  59. SceneLoader._loggingLevel = value;
  60. },
  61. enumerable: true,
  62. configurable: true
  63. });
  64. SceneLoader._getPluginForFilename = function (sceneFilename) {
  65. var dotPosition = sceneFilename.lastIndexOf(".");
  66. var queryStringPosition = sceneFilename.indexOf("?");
  67. if (queryStringPosition === -1) {
  68. queryStringPosition = sceneFilename.length;
  69. }
  70. var extension = sceneFilename.substring(dotPosition, queryStringPosition).toLowerCase();
  71. for (var index = 0; index < this._registeredPlugins.length; index++) {
  72. var plugin = this._registeredPlugins[index];
  73. if (plugin.extensions.indexOf(extension) !== -1) {
  74. return plugin;
  75. }
  76. }
  77. return this._registeredPlugins[this._registeredPlugins.length - 1];
  78. };
  79. // Public functions
  80. SceneLoader.GetPluginForExtension = function (extension) {
  81. for (var index = 0; index < this._registeredPlugins.length; index++) {
  82. var plugin = this._registeredPlugins[index];
  83. if (plugin.extensions.indexOf(extension) !== -1) {
  84. return plugin;
  85. }
  86. }
  87. return null;
  88. };
  89. SceneLoader.RegisterPlugin = function (plugin) {
  90. plugin.extensions = plugin.extensions.toLowerCase();
  91. SceneLoader._registeredPlugins.push(plugin);
  92. };
  93. SceneLoader.ImportMesh = function (meshesNames, rootUrl, sceneFilename, scene, onsuccess, progressCallBack, onerror) {
  94. if (sceneFilename.substr && sceneFilename.substr(0, 1) === "/") {
  95. BABYLON.Tools.Error("Wrong sceneFilename parameter");
  96. return;
  97. }
  98. var loadingToken = {};
  99. scene._addPendingData(loadingToken);
  100. var manifestChecked = function (success) {
  101. scene.database = database;
  102. var plugin = SceneLoader._getPluginForFilename(sceneFilename);
  103. var importMeshFromData = function (data) {
  104. var meshes = [];
  105. var particleSystems = [];
  106. var skeletons = [];
  107. try {
  108. if (!plugin.importMesh(meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons)) {
  109. if (onerror) {
  110. onerror(scene, 'Unable to import meshes from ' + rootUrl + sceneFilename);
  111. }
  112. scene._removePendingData(loadingToken);
  113. return;
  114. }
  115. }
  116. catch (e) {
  117. if (onerror) {
  118. onerror(scene, 'Unable to import meshes from ' + rootUrl + sceneFilename + ' (Exception: ' + e + ')');
  119. }
  120. scene._removePendingData(loadingToken);
  121. return;
  122. }
  123. if (onsuccess) {
  124. scene.importedMeshesFiles.push(rootUrl + sceneFilename);
  125. onsuccess(meshes, particleSystems, skeletons);
  126. scene._removePendingData(loadingToken);
  127. }
  128. };
  129. if (sceneFilename.substr && sceneFilename.substr(0, 5) === "data:") {
  130. // Direct load
  131. importMeshFromData(sceneFilename.substr(5));
  132. return;
  133. }
  134. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, function (data) {
  135. importMeshFromData(data);
  136. }, progressCallBack, database);
  137. };
  138. if (scene.getEngine().enableOfflineSupport && !(sceneFilename.substr && sceneFilename.substr(0, 5) === "data:")) {
  139. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  140. var database = new BABYLON.Database(rootUrl + sceneFilename, manifestChecked);
  141. }
  142. else {
  143. // If the scene is a data stream or offline support is not enabled, it's a direct load
  144. manifestChecked(true);
  145. }
  146. };
  147. /**
  148. * Load a scene
  149. * @param rootUrl a string that defines the root url for scene and resources
  150. * @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
  151. * @param engine is the instance of BABYLON.Engine to use to create the scene
  152. */
  153. SceneLoader.Load = function (rootUrl, sceneFilename, engine, onsuccess, progressCallBack, onerror) {
  154. if (!SceneLoader._warned) {
  155. BABYLON.Tools.Warn("SceneLoader.Load deprecated since 2.4. Use SceneLoader.Append.");
  156. SceneLoader._warned = true;
  157. }
  158. SceneLoader.Append(rootUrl, sceneFilename, new BABYLON.Scene(engine), onsuccess, progressCallBack, onerror);
  159. };
  160. /**
  161. * Append a scene
  162. * @param rootUrl a string that defines the root url for scene and resources
  163. * @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
  164. * @param scene is the instance of BABYLON.Scene to append to
  165. */
  166. SceneLoader.Append = function (rootUrl, sceneFilename, scene, onsuccess, progressCallBack, onerror) {
  167. if (sceneFilename.substr && sceneFilename.substr(0, 1) === "/") {
  168. BABYLON.Tools.Error("Wrong sceneFilename parameter");
  169. return;
  170. }
  171. var plugin = this._getPluginForFilename(sceneFilename.name || sceneFilename);
  172. var database;
  173. var loadingToken = {};
  174. scene._addPendingData(loadingToken);
  175. if (SceneLoader.ShowLoadingScreen) {
  176. scene.getEngine().displayLoadingUI();
  177. }
  178. var loadSceneFromData = function (data) {
  179. scene.database = database;
  180. if (!plugin.load(scene, data, rootUrl)) {
  181. if (onerror) {
  182. onerror(scene);
  183. }
  184. scene._removePendingData(loadingToken);
  185. scene.getEngine().hideLoadingUI();
  186. return;
  187. }
  188. if (onsuccess) {
  189. onsuccess(scene);
  190. }
  191. scene._removePendingData(loadingToken);
  192. if (SceneLoader.ShowLoadingScreen) {
  193. scene.executeWhenReady(function () {
  194. scene.getEngine().hideLoadingUI();
  195. });
  196. }
  197. };
  198. var manifestChecked = function (success) {
  199. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, loadSceneFromData, progressCallBack, database);
  200. };
  201. if (sceneFilename.substr && sceneFilename.substr(0, 5) === "data:") {
  202. // Direct load
  203. loadSceneFromData(sceneFilename.substr(5));
  204. return;
  205. }
  206. if (rootUrl.indexOf("file:") === -1) {
  207. if (scene.getEngine().enableOfflineSupport) {
  208. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  209. database = new BABYLON.Database(rootUrl + sceneFilename, manifestChecked);
  210. }
  211. else {
  212. manifestChecked(true);
  213. }
  214. }
  215. else {
  216. BABYLON.Tools.ReadFile(sceneFilename, loadSceneFromData, progressCallBack);
  217. }
  218. };
  219. // Flags
  220. SceneLoader._ForceFullSceneLoadingForIncremental = false;
  221. SceneLoader._ShowLoadingScreen = true;
  222. SceneLoader._loggingLevel = SceneLoader.NO_LOGGING;
  223. // Members
  224. SceneLoader._registeredPlugins = new Array();
  225. return SceneLoader;
  226. })();
  227. BABYLON.SceneLoader = SceneLoader;
  228. ;
  229. })(BABYLON || (BABYLON = {}));