assetContainer.ts 7.1 KB

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