babylon.sceneLoader.js 4.1 KB

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