babylon.sceneLoader.js 3.7 KB

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