babylon.assetContainer.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. module BABYLON {
  2. /**
  3. * Set of assets to keep when moving a scene into an asset container.
  4. */
  5. export class KeepAssets extends AbstractScene { }
  6. /**
  7. * Container with a set of assets that can be added or removed from a scene.
  8. */
  9. export class AssetContainer extends AbstractScene {
  10. /**
  11. * The scene the AssetContainer belongs to.
  12. */
  13. public scene: Scene;
  14. /**
  15. * Instantiates an AssetContainer.
  16. * @param scene The scene the AssetContainer belongs to.
  17. */
  18. constructor(scene: Scene) {
  19. super();
  20. this.scene = scene;
  21. }
  22. /**
  23. * Adds all the assets from the container to the scene.
  24. */
  25. public addAllToScene() {
  26. this.cameras.forEach((o) => {
  27. this.scene.addCamera(o);
  28. });
  29. this.lights.forEach((o) => {
  30. this.scene.addLight(o);
  31. });
  32. this.meshes.forEach((o) => {
  33. this.scene.addMesh(o);
  34. });
  35. this.skeletons.forEach((o) => {
  36. this.scene.addSkeleton(o);
  37. });
  38. this.animations.forEach((o) => {
  39. this.scene.addAnimation(o);
  40. });
  41. this.animationGroups.forEach((o) => {
  42. this.scene.addAnimationGroup(o);
  43. });
  44. this.multiMaterials.forEach((o) => {
  45. this.scene.addMultiMaterial(o);
  46. });
  47. this.materials.forEach((o) => {
  48. this.scene.addMaterial(o);
  49. });
  50. this.morphTargetManagers.forEach((o) => {
  51. this.scene.addMorphTargetManager(o);
  52. });
  53. this.geometries.forEach((o) => {
  54. this.scene.addGeometry(o);
  55. });
  56. this.transformNodes.forEach((o) => {
  57. this.scene.addTransformNode(o);
  58. });
  59. this.actionManagers.forEach((o) => {
  60. this.scene.addActionManager(o);
  61. });
  62. this.sounds.forEach((o) => {
  63. o.play();
  64. o.autoplay = true;
  65. this.scene.mainSoundTrack.AddSound(o);
  66. });
  67. this.textures.forEach((o) => {
  68. this.scene.addTexture(o);
  69. });
  70. for (let component of this.scene._serializableComponents) {
  71. component.addFromContainer(this.scene);
  72. }
  73. }
  74. /**
  75. * Removes all the assets in the container from the scene
  76. */
  77. public removeAllFromScene() {
  78. this.cameras.forEach((o) => {
  79. this.scene.removeCamera(o);
  80. });
  81. this.lights.forEach((o) => {
  82. this.scene.removeLight(o);
  83. });
  84. this.meshes.forEach((o) => {
  85. this.scene.removeMesh(o);
  86. });
  87. this.skeletons.forEach((o) => {
  88. this.scene.removeSkeleton(o);
  89. });
  90. this.animations.forEach((o) => {
  91. this.scene.removeAnimation(o);
  92. });
  93. this.animationGroups.forEach((o) => {
  94. this.scene.removeAnimationGroup(o);
  95. });
  96. this.multiMaterials.forEach((o) => {
  97. this.scene.removeMultiMaterial(o);
  98. });
  99. this.materials.forEach((o) => {
  100. this.scene.removeMaterial(o);
  101. });
  102. this.morphTargetManagers.forEach((o) => {
  103. this.scene.removeMorphTargetManager(o);
  104. });
  105. this.geometries.forEach((o) => {
  106. this.scene.removeGeometry(o);
  107. });
  108. this.transformNodes.forEach((o) => {
  109. this.scene.removeTransformNode(o);
  110. });
  111. this.actionManagers.forEach((o) => {
  112. this.scene.removeActionManager(o);
  113. });
  114. this.sounds.forEach((o) => {
  115. o.stop();
  116. o.autoplay = false;
  117. this.scene.mainSoundTrack.RemoveSound(o);
  118. });
  119. this.textures.forEach((o) => {
  120. this.scene.removeTexture(o);
  121. });
  122. for (let component of this.scene._serializableComponents) {
  123. component.removeFromContainer(this.scene);
  124. }
  125. }
  126. private _moveAssets<T>(sourceAssets: T[], targetAssets: T[], keepAssets: T[]): void {
  127. if (!sourceAssets) {
  128. return;
  129. }
  130. for (let asset of sourceAssets) {
  131. let move = true;
  132. if (keepAssets) {
  133. for (let keepAsset of keepAssets) {
  134. if (asset === keepAsset) {
  135. move = false;
  136. break;
  137. }
  138. }
  139. }
  140. if (move) {
  141. targetAssets.push(asset);
  142. }
  143. }
  144. }
  145. /**
  146. * Removes all the assets contained in the scene and adds them to the container.
  147. * @param keepAssets Set of assets to keep in the scene. (default: empty)
  148. */
  149. public moveAllFromScene(keepAssets?: KeepAssets): void {
  150. if (keepAssets === undefined) {
  151. keepAssets = new KeepAssets();
  152. }
  153. for (let key in this) {
  154. if (this.hasOwnProperty(key)) {
  155. (<any>this)[key] = (<any>this)[key] || [];
  156. this._moveAssets((<any>this.scene)[key], (<any>this)[key], (<any>keepAssets)[key]);
  157. }
  158. }
  159. this.removeAllFromScene();
  160. }
  161. /**
  162. * Adds all meshes in the asset container to a root mesh that can be used to position all the contained meshes. The root mesh is then added to the front of the meshes in the assetContainer.
  163. * @returns the root mesh
  164. */
  165. public createRootMesh(){
  166. var rootMesh = new BABYLON.Mesh("assetContainerRootMesh", this.scene);
  167. this.meshes.forEach((m)=>{
  168. if(!m.parent){
  169. rootMesh.addChild(m);
  170. }
  171. })
  172. this.meshes.unshift(rootMesh);
  173. return rootMesh;
  174. }
  175. }
  176. }