babylon.sceneLoader.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 manifestChecked = success => {
  51. scene.database = database;
  52. var plugin = SceneLoader._getPluginForFilename(sceneFilename);
  53. var importMeshFromData = data => {
  54. var meshes = [];
  55. var particleSystems = [];
  56. var skeletons = [];
  57. try {
  58. if (!plugin.importMesh(meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons)) {
  59. if (onerror) {
  60. onerror(scene, 'unable to load the scene');
  61. }
  62. return;
  63. }
  64. } catch (e) {
  65. if (onerror) {
  66. onerror(scene, e);
  67. }
  68. return;
  69. }
  70. if (onsuccess) {
  71. scene.importedMeshesFiles.push(rootUrl + sceneFilename);
  72. onsuccess(meshes, particleSystems, skeletons);
  73. }
  74. };
  75. if (sceneFilename.substr && sceneFilename.substr(0, 5) === "data:") {
  76. // Direct load
  77. importMeshFromData(sceneFilename.substr(5));
  78. return;
  79. }
  80. Tools.LoadFile(rootUrl + sceneFilename, data => {
  81. importMeshFromData(data);
  82. }, progressCallBack, database);
  83. };
  84. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  85. var database = new Database(rootUrl + sceneFilename, manifestChecked);
  86. }
  87. /**
  88. * Load a scene
  89. * @param rootUrl a string that defines the root url for scene and resources
  90. * @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
  91. * @param engine is the instance of BABYLON.Engine to use to create the scene
  92. */
  93. public static Load(rootUrl: string, sceneFilename: any, engine: Engine, onsuccess?: (scene: Scene) => void, progressCallBack?: any, onerror?: (scene: Scene) => void): void {
  94. SceneLoader.Append(rootUrl, sceneFilename, new Scene(engine), onsuccess, progressCallBack, onerror);
  95. }
  96. /**
  97. * Append a scene
  98. * @param rootUrl a string that defines the root url for scene and resources
  99. * @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
  100. * @param scene is the instance of BABYLON.Scene to append to
  101. */
  102. public static Append(rootUrl: string, sceneFilename: any, scene: Scene, onsuccess?: (scene: Scene) => void, progressCallBack?: any, onerror?: (scene: Scene) => void): void {
  103. if (sceneFilename.substr && sceneFilename.substr(0, 1) === "/") {
  104. Tools.Error("Wrong sceneFilename parameter");
  105. return;
  106. }
  107. var plugin = this._getPluginForFilename(sceneFilename.name || sceneFilename);
  108. var database;
  109. if (SceneLoader.ShowLoadingScreen) {
  110. scene.getEngine().displayLoadingUI();
  111. }
  112. var loadSceneFromData = data => {
  113. scene.database = database;
  114. if (!plugin.load(scene, data, rootUrl)) {
  115. if (onerror) {
  116. onerror(scene);
  117. }
  118. scene.getEngine().hideLoadingUI();
  119. return;
  120. }
  121. if (onsuccess) {
  122. onsuccess(scene);
  123. }
  124. if (SceneLoader.ShowLoadingScreen) {
  125. scene.executeWhenReady(() => {
  126. scene.getEngine().hideLoadingUI();
  127. });
  128. }
  129. };
  130. var manifestChecked = success => {
  131. Tools.LoadFile(rootUrl + sceneFilename, loadSceneFromData, progressCallBack, database);
  132. };
  133. if (sceneFilename.substr && sceneFilename.substr(0, 5) === "data:") {
  134. // Direct load
  135. loadSceneFromData(sceneFilename.substr(5));
  136. return;
  137. }
  138. if (rootUrl.indexOf("file:") === -1) {
  139. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  140. database = new Database(rootUrl + sceneFilename, manifestChecked);
  141. }
  142. // Loading file from disk via input file or drag'n'drop
  143. else {
  144. Tools.ReadFile(sceneFilename, loadSceneFromData, progressCallBack);
  145. }
  146. }
  147. };
  148. }