babylon.assetContainer.ts 10 KB

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