babylon.assetContainer.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. module BABYLON {
  2. /**
  3. * Set of assets to keep when moving a scene into an asset container.
  4. */
  5. export class KeepAssets {
  6. /**
  7. * Cameras to keep.
  8. */
  9. public cameras = new Array<Camera>();
  10. /**
  11. * Lights to keep.
  12. */
  13. public lights = new Array<Light>();
  14. /**
  15. * Meshes to keep.
  16. */
  17. public meshes = new Array<AbstractMesh>();
  18. /**
  19. * Skeletons to keep.
  20. */
  21. public skeletons = new Array<Skeleton>();
  22. /**
  23. * ParticleSystems to keep.
  24. */
  25. public particleSystems = new Array<IParticleSystem>();
  26. /**
  27. * Animations to keep.
  28. */
  29. public animations = new Array<Animation>();
  30. /**
  31. * AnimationGroups to keep.
  32. */
  33. public animationGroups = new Array<AnimationGroup>();
  34. /**
  35. * MultiMaterials to keep.
  36. */
  37. public multiMaterials = new Array<MultiMaterial>();
  38. /**
  39. * Materials to keep.
  40. */
  41. public materials = new Array<Material>();
  42. /**
  43. * MorphTargetManagers to keep.
  44. */
  45. public morphTargetManagers = new Array<MorphTargetManager>();
  46. /**
  47. * Geometries to keep.
  48. */
  49. public geometries = new Array<Geometry>();
  50. /**
  51. * TransformNodes to keep.
  52. */
  53. public transformNodes = new Array<TransformNode>();
  54. /**
  55. * LensFlareSystems to keep.
  56. */
  57. public lensFlareSystems = new Array<LensFlareSystem>();
  58. /**
  59. * ShadowGenerators to keep.
  60. */
  61. public shadowGenerators = new Array<ShadowGenerator>();
  62. /**
  63. * ActionManagers to keep.
  64. */
  65. public actionManagers = new Array<ActionManager>();
  66. /**
  67. * Sounds to keep.
  68. */
  69. public sounds = new Array<Sound>();
  70. /**
  71. * Textures to keep.
  72. */
  73. public textures = new Array<Texture>();
  74. /**
  75. * Effect layers to keep.
  76. */
  77. public effectLayers = new Array<EffectLayer>();
  78. }
  79. /**
  80. * Container with a set of assets that can be added or removed from a scene.
  81. */
  82. export class AssetContainer {
  83. /**
  84. * The scene the AssetContainer belongs to.
  85. */
  86. public scene: Scene;
  87. // Objects
  88. /**
  89. * Cameras populated in the container.
  90. */
  91. public cameras = new Array<Camera>();
  92. /**
  93. * Lights populated in the container.
  94. */
  95. public lights = new Array<Light>();
  96. /**
  97. * Meshes populated in the container.
  98. */
  99. public meshes = new Array<AbstractMesh>();
  100. /**
  101. * Skeletons populated in the container.
  102. */
  103. public skeletons = new Array<Skeleton>();
  104. /**
  105. * ParticleSystems populated in the container.
  106. */
  107. public particleSystems = new Array<IParticleSystem>();
  108. /**
  109. * Animations populated in the container.
  110. */
  111. public animations = new Array<Animation>();
  112. /**
  113. * AnimationGroups populated in the container.
  114. */
  115. public animationGroups = new Array<AnimationGroup>();
  116. /**
  117. * MultiMaterials populated in the container.
  118. */
  119. public multiMaterials = new Array<MultiMaterial>();
  120. /**
  121. * Materials populated in the container.
  122. */
  123. public materials = new Array<Material>();
  124. /**
  125. * MorphTargetManagers populated in the container.
  126. */
  127. public morphTargetManagers = new Array<MorphTargetManager>();
  128. /**
  129. * Geometries populated in the container.
  130. */
  131. public geometries = new Array<Geometry>();
  132. /**
  133. * TransformNodes populated in the container.
  134. */
  135. public transformNodes = new Array<TransformNode>();
  136. /**
  137. * LensFlareSystems populated in the container.
  138. */
  139. public lensFlareSystems = new Array<LensFlareSystem>();
  140. /**
  141. * ShadowGenerators populated in the container.
  142. */
  143. public shadowGenerators = new Array<ShadowGenerator>();
  144. /**
  145. * ActionManagers populated in the container.
  146. */
  147. public actionManagers = new Array<ActionManager>();
  148. /**
  149. * Sounds populated in the container.
  150. */
  151. public sounds = new Array<Sound>();
  152. /**
  153. * Textures populated in the container.
  154. */
  155. public textures = new Array<Texture>();
  156. /**
  157. * Effect layers populated in the container.
  158. */
  159. public effectLayers = new Array<EffectLayer>();
  160. /**
  161. * Instantiates an AssetContainer.
  162. * @param scene The scene the AssetContainer belongs to.
  163. */
  164. constructor(scene: Scene) {
  165. this.scene = scene;
  166. }
  167. /**
  168. * Adds all the assets from the container to the scene.
  169. */
  170. public addAllToScene() {
  171. this.cameras.forEach((o) => {
  172. this.scene.addCamera(o);
  173. });
  174. this.lights.forEach((o) => {
  175. this.scene.addLight(o);
  176. });
  177. this.meshes.forEach((o) => {
  178. this.scene.addMesh(o);
  179. });
  180. this.skeletons.forEach((o) => {
  181. this.scene.addSkeleton(o);
  182. });
  183. this.particleSystems.forEach((o) => {
  184. this.scene.addParticleSystem(o);
  185. });
  186. this.animations.forEach((o) => {
  187. this.scene.addAnimation(o);
  188. });
  189. this.animationGroups.forEach((o) => {
  190. this.scene.addAnimationGroup(o);
  191. });
  192. this.multiMaterials.forEach((o) => {
  193. this.scene.addMultiMaterial(o);
  194. });
  195. this.materials.forEach((o) => {
  196. this.scene.addMaterial(o);
  197. });
  198. this.morphTargetManagers.forEach((o) => {
  199. this.scene.addMorphTargetManager(o);
  200. });
  201. this.geometries.forEach((o) => {
  202. this.scene.addGeometry(o);
  203. });
  204. this.transformNodes.forEach((o) => {
  205. this.scene.addTransformNode(o);
  206. });
  207. this.lensFlareSystems.forEach((o) => {
  208. this.scene.addLensFlareSystem(o);
  209. });
  210. this.actionManagers.forEach((o) => {
  211. this.scene.addActionManager(o);
  212. });
  213. this.sounds.forEach((o) => {
  214. o.play();
  215. o.autoplay = true;
  216. this.scene.mainSoundTrack.AddSound(o);
  217. });
  218. this.textures.forEach((o) => {
  219. this.scene.addTexture(o);
  220. });
  221. this.effectLayers.forEach((o) => {
  222. this.scene.addEffectLayer(o);
  223. });
  224. }
  225. /**
  226. * Removes all the assets in the container from the scene
  227. */
  228. public removeAllFromScene() {
  229. this.cameras.forEach((o) => {
  230. this.scene.removeCamera(o);
  231. });
  232. this.lights.forEach((o) => {
  233. this.scene.removeLight(o);
  234. });
  235. this.meshes.forEach((o) => {
  236. this.scene.removeMesh(o);
  237. });
  238. this.skeletons.forEach((o) => {
  239. this.scene.removeSkeleton(o);
  240. });
  241. this.particleSystems.forEach((o) => {
  242. this.scene.removeParticleSystem(o);
  243. });
  244. this.animations.forEach((o) => {
  245. this.scene.removeAnimation(o);
  246. });
  247. this.animationGroups.forEach((o) => {
  248. this.scene.removeAnimationGroup(o);
  249. });
  250. this.multiMaterials.forEach((o) => {
  251. this.scene.removeMultiMaterial(o);
  252. });
  253. this.materials.forEach((o) => {
  254. this.scene.removeMaterial(o);
  255. });
  256. this.morphTargetManagers.forEach((o) => {
  257. this.scene.removeMorphTargetManager(o);
  258. });
  259. this.geometries.forEach((o) => {
  260. this.scene.removeGeometry(o);
  261. });
  262. this.transformNodes.forEach((o) => {
  263. this.scene.removeTransformNode(o);
  264. });
  265. this.lensFlareSystems.forEach((o) => {
  266. this.scene.removeLensFlareSystem(o);
  267. });
  268. this.actionManagers.forEach((o) => {
  269. this.scene.removeActionManager(o);
  270. });
  271. this.sounds.forEach((o) => {
  272. o.stop();
  273. o.autoplay = false;
  274. this.scene.mainSoundTrack.RemoveSound(o);
  275. });
  276. this.textures.forEach((o) => {
  277. this.scene.removeTexture(o);
  278. });
  279. this.effectLayers.forEach((o) => {
  280. this.scene.removeEffectLayer(o);
  281. });
  282. }
  283. private _moveAssets<T>(sourceAssets: T[], targetAssets: T[], keepAssets: T[]): void {
  284. for (let asset of sourceAssets) {
  285. let move = true;
  286. for (let keepAsset of keepAssets) {
  287. if (asset === keepAsset) {
  288. move = false;
  289. break;
  290. }
  291. }
  292. if (move) {
  293. targetAssets.push(asset);
  294. }
  295. }
  296. }
  297. /**
  298. * Removes all the assets contained in the scene and adds them to the container.
  299. * @param keepAssets Set of assets to keep in the scene. (default: empty)
  300. */
  301. public moveAllFromScene(keepAssets?: KeepAssets): void {
  302. if (keepAssets === undefined) {
  303. keepAssets = new KeepAssets();
  304. }
  305. this._moveAssets(this.scene.cameras, this.cameras, keepAssets.cameras);
  306. this._moveAssets(this.scene.meshes, this.meshes, keepAssets.meshes);
  307. this._moveAssets(this.scene.getGeometries(), this.geometries, keepAssets.geometries);
  308. this._moveAssets(this.scene.materials, this.materials, keepAssets.materials);
  309. this._moveAssets(this.scene._actionManagers, this.actionManagers, keepAssets.actionManagers);
  310. this._moveAssets(this.scene.animations, this.animations, keepAssets.animations);
  311. this._moveAssets(this.scene.animationGroups, this.animationGroups, keepAssets.animationGroups);
  312. this._moveAssets(this.scene.lensFlareSystems, this.lensFlareSystems, keepAssets.lensFlareSystems);
  313. this._moveAssets(this.scene.lights, this.lights, keepAssets.lights);
  314. this._moveAssets(this.scene.morphTargetManagers, this.morphTargetManagers, keepAssets.morphTargetManagers);
  315. this._moveAssets(this.scene.multiMaterials, this.multiMaterials, keepAssets.multiMaterials);
  316. this._moveAssets(this.scene.skeletons, this.skeletons, keepAssets.skeletons);
  317. this._moveAssets(this.scene.particleSystems, this.particleSystems, keepAssets.particleSystems);
  318. this._moveAssets(this.scene.mainSoundTrack.soundCollection, this.sounds, keepAssets.sounds);
  319. this._moveAssets(this.scene.transformNodes, this.transformNodes, keepAssets.transformNodes);
  320. this._moveAssets(this.scene.textures, this.textures, keepAssets.textures);
  321. this._moveAssets(this.scene.effectLayers, this.effectLayers, keepAssets.effectLayers);
  322. this.removeAllFromScene();
  323. }
  324. }
  325. }