babylon.sceneLoader.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. module BABYLON {
  2. export interface ISceneLoaderPlugin {
  3. extensions: string;
  4. importMesh: (meshesNames: any, scene: Scene, data: any, rootUrl: string, meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => boolean;
  5. load: (scene: Scene, data: string, rootUrl: string) => boolean;
  6. }
  7. export class SceneLoader {
  8. // Flags
  9. private static _ForceFullSceneLoadingForIncremental = false;
  10. private static _ShowLoadingScreen = true;
  11. public static get ForceFullSceneLoadingForIncremental() {
  12. return SceneLoader._ForceFullSceneLoadingForIncremental;
  13. }
  14. public static set ForceFullSceneLoadingForIncremental(value: boolean) {
  15. SceneLoader._ForceFullSceneLoadingForIncremental = value;
  16. }
  17. public static get ShowLoadingScreen() {
  18. return SceneLoader._ShowLoadingScreen;
  19. }
  20. public static set ShowLoadingScreen(value: boolean) {
  21. SceneLoader._ShowLoadingScreen = value;
  22. }
  23. // Members
  24. private static _registeredPlugins = new Array<ISceneLoaderPlugin>();
  25. private static _getPluginForFilename(sceneFilename): ISceneLoaderPlugin {
  26. var dotPosition = sceneFilename.lastIndexOf(".");
  27. var queryStringPosition = sceneFilename.indexOf("?");
  28. if (queryStringPosition === -1) {
  29. queryStringPosition = sceneFilename.length;
  30. }
  31. var extension = sceneFilename.substring(dotPosition, queryStringPosition).toLowerCase();
  32. for (var index = 0; index < this._registeredPlugins.length; index++) {
  33. var plugin = this._registeredPlugins[index];
  34. if (plugin.extensions.indexOf(extension) !== -1) {
  35. return plugin;
  36. }
  37. }
  38. return this._registeredPlugins[this._registeredPlugins.length - 1];
  39. }
  40. // Public functions
  41. public static RegisterPlugin(plugin: ISceneLoaderPlugin): void {
  42. plugin.extensions = plugin.extensions.toLowerCase();
  43. SceneLoader._registeredPlugins.push(plugin);
  44. }
  45. public static ImportMesh(meshesNames: any, rootUrl: string, sceneFilename: string, scene: Scene, onsuccess?: (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => void, progressCallBack?: () => void, onerror?: (scene: Scene, e: any) => void): void {
  46. if (sceneFilename.substr && sceneFilename.substr(0, 1) === "/") {
  47. Tools.Error("Wrong sceneFilename parameter");
  48. return;
  49. }
  50. var loadingToken = {};
  51. scene._addPendingData(loadingToken);
  52. var manifestChecked = success => {
  53. scene.database = database;
  54. var plugin = SceneLoader._getPluginForFilename(sceneFilename);
  55. var importMeshFromData = data => {
  56. var meshes = [];
  57. var particleSystems = [];
  58. var skeletons = [];
  59. try {
  60. if (!plugin.importMesh(meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons)) {
  61. if (onerror) {
  62. onerror(scene, 'unable to load the scene');
  63. }
  64. scene._removePendingData(loadingToken);
  65. return;
  66. }
  67. } catch (e) {
  68. if (onerror) {
  69. onerror(scene, e);
  70. }
  71. scene._removePendingData(loadingToken);
  72. return;
  73. }
  74. if (onsuccess) {
  75. scene.importedMeshesFiles.push(rootUrl + sceneFilename);
  76. onsuccess(meshes, particleSystems, skeletons);
  77. scene._removePendingData(loadingToken);
  78. }
  79. };
  80. if (sceneFilename.substr && sceneFilename.substr(0, 5) === "data:") {
  81. // Direct load
  82. importMeshFromData(sceneFilename.substr(5));
  83. return;
  84. }
  85. Tools.LoadFile(rootUrl + sceneFilename, data => {
  86. importMeshFromData(data);
  87. }, progressCallBack, database);
  88. };
  89. if (scene.getEngine().enableOfflineSupport) {
  90. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  91. var database = new Database(rootUrl + sceneFilename, manifestChecked);
  92. }
  93. else {
  94. manifestChecked(true);
  95. }
  96. }
  97. /**
  98. * Load a scene
  99. * @param rootUrl a string that defines the root url for scene and resources
  100. * @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
  101. * @param engine is the instance of BABYLON.Engine to use to create the scene
  102. */
  103. public static Load(rootUrl: string, sceneFilename: any, engine: Engine, onsuccess?: (scene: Scene) => void, progressCallBack?: any, onerror?: (scene: Scene) => void): void {
  104. SceneLoader.Append(rootUrl, sceneFilename, new Scene(engine), onsuccess, progressCallBack, onerror);
  105. }
  106. /**
  107. * Append a scene
  108. * @param rootUrl a string that defines the root url for scene and resources
  109. * @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
  110. * @param scene is the instance of BABYLON.Scene to append to
  111. */
  112. public static Append(rootUrl: string, sceneFilename: any, scene: Scene, onsuccess?: (scene: Scene) => void, progressCallBack?: any, onerror?: (scene: Scene) => void): void {
  113. if (sceneFilename.substr && sceneFilename.substr(0, 1) === "/") {
  114. Tools.Error("Wrong sceneFilename parameter");
  115. return;
  116. }
  117. var plugin = this._getPluginForFilename(sceneFilename.name || sceneFilename);
  118. var database;
  119. var loadingToken = {};
  120. scene._addPendingData(loadingToken);
  121. if (SceneLoader.ShowLoadingScreen) {
  122. scene.getEngine().displayLoadingUI();
  123. }
  124. var loadSceneFromData = data => {
  125. scene.database = database;
  126. if (!plugin.load(scene, data, rootUrl)) {
  127. if (onerror) {
  128. onerror(scene);
  129. }
  130. scene._removePendingData(loadingToken);
  131. scene.getEngine().hideLoadingUI();
  132. return;
  133. }
  134. if (onsuccess) {
  135. onsuccess(scene);
  136. }
  137. scene._removePendingData(loadingToken);
  138. if (SceneLoader.ShowLoadingScreen) {
  139. scene.executeWhenReady(() => {
  140. scene.getEngine().hideLoadingUI();
  141. });
  142. }
  143. };
  144. var manifestChecked = success => {
  145. Tools.LoadFile(rootUrl + sceneFilename, loadSceneFromData, progressCallBack, database);
  146. };
  147. if (sceneFilename.substr && sceneFilename.substr(0, 5) === "data:") {
  148. // Direct load
  149. loadSceneFromData(sceneFilename.substr(5));
  150. return;
  151. }
  152. if (rootUrl.indexOf("file:") === -1) {
  153. if (scene.getEngine().enableOfflineSupport) {
  154. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  155. database = new Database(rootUrl + sceneFilename, manifestChecked);
  156. }
  157. else {
  158. manifestChecked(true);
  159. }
  160. }
  161. // Loading file from disk via input file or drag'n'drop
  162. else {
  163. Tools.ReadFile(sceneFilename, loadSceneFromData, progressCallBack);
  164. }
  165. }
  166. };
  167. }