babylon.sceneLoader.js 2.4 KB

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