babylon.sceneLoader.js 3.2 KB

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