babylon.sceneLoader.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, then, progressCallBack) {
  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. plugin.importMesh(meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons);
  32. if (then) {
  33. then(meshes, particleSystems, skeletons);
  34. }
  35. }, progressCallBack, database);
  36. },
  37. Load: function (rootUrl, sceneFilename, engine, then, progressCallBack) {
  38. var plugin = this._getPluginForFilename(sceneFilename);
  39. var database;
  40. var loadSceneFromData = function (data) {
  41. var scene = new BABYLON.Scene(engine);
  42. scene.database = database;
  43. plugin.load(scene, data, rootUrl);
  44. if (then) {
  45. then(scene);
  46. }
  47. };
  48. if (rootUrl.indexOf("file:") === -1) {
  49. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  50. database = new BABYLON.Database(rootUrl + sceneFilename);
  51. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, loadSceneFromData, progressCallBack, database);
  52. }
  53. // Loading file from disk via input file or drag'n'drop
  54. else {
  55. BABYLON.Tools.ReadFile(sceneFilename, loadSceneFromData, progressCallBack);
  56. }
  57. }
  58. };
  59. })();