babylon.sceneLoader.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. onsuccess(meshes, particleSystems, skeletons);
  39. }
  40. }, progressCallBack, database);
  41. },
  42. Load: function (rootUrl, sceneFilename, engine, onsuccess, progressCallBack, onerror) {
  43. var plugin = this._getPluginForFilename(sceneFilename.name || sceneFilename);
  44. var database;
  45. var loadSceneFromData = function (data) {
  46. var scene = new BABYLON.Scene(engine);
  47. scene.database = database;
  48. if (!plugin.load(scene, data, rootUrl)) {
  49. if (onerror) {
  50. onerror(scene);
  51. }
  52. return;
  53. }
  54. if (onsuccess) {
  55. onsuccess(scene);
  56. }
  57. };
  58. if (rootUrl.indexOf("file:") === -1) {
  59. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  60. database = new BABYLON.Database(rootUrl + sceneFilename);
  61. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, loadSceneFromData, progressCallBack, database);
  62. }
  63. // Loading file from disk via input file or drag'n'drop
  64. else {
  65. BABYLON.Tools.ReadFile(sceneFilename, loadSceneFromData, progressCallBack);
  66. }
  67. }
  68. };
  69. })();