assetContainer.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. if (this.environmentTexture) {
  77. this.scene.environmentTexture = this.environmentTexture;
  78. }
  79. for (let component of this.scene._serializableComponents) {
  80. component.addFromContainer(this);
  81. }
  82. }
  83. /**
  84. * Removes all the assets in the container from the scene
  85. */
  86. public removeAllFromScene() {
  87. this.cameras.forEach((o) => {
  88. this.scene.removeCamera(o);
  89. });
  90. this.lights.forEach((o) => {
  91. this.scene.removeLight(o);
  92. });
  93. this.meshes.forEach((o) => {
  94. this.scene.removeMesh(o);
  95. });
  96. this.skeletons.forEach((o) => {
  97. this.scene.removeSkeleton(o);
  98. });
  99. this.animations.forEach((o) => {
  100. this.scene.removeAnimation(o);
  101. });
  102. this.animationGroups.forEach((o) => {
  103. this.scene.removeAnimationGroup(o);
  104. });
  105. this.multiMaterials.forEach((o) => {
  106. this.scene.removeMultiMaterial(o);
  107. });
  108. this.materials.forEach((o) => {
  109. this.scene.removeMaterial(o);
  110. });
  111. this.morphTargetManagers.forEach((o) => {
  112. this.scene.removeMorphTargetManager(o);
  113. });
  114. this.geometries.forEach((o) => {
  115. this.scene.removeGeometry(o);
  116. });
  117. this.transformNodes.forEach((o) => {
  118. this.scene.removeTransformNode(o);
  119. });
  120. this.actionManagers.forEach((o) => {
  121. this.scene.removeActionManager(o);
  122. });
  123. this.textures.forEach((o) => {
  124. this.scene.removeTexture(o);
  125. });
  126. this.reflectionProbes.forEach((o) => {
  127. this.scene.removeReflectionProbe(o);
  128. });
  129. if (this.environmentTexture === this.scene.environmentTexture) {
  130. this.scene.environmentTexture = null;
  131. }
  132. for (let component of this.scene._serializableComponents) {
  133. component.removeFromContainer(this);
  134. }
  135. }
  136. /**
  137. * Disposes all the assets in the container
  138. */
  139. public dispose() {
  140. this.cameras.forEach((o) => {
  141. o.dispose();
  142. });
  143. this.cameras = [];
  144. this.lights.forEach((o) => {
  145. o.dispose();
  146. });
  147. this.lights = [];
  148. this.meshes.forEach((o) => {
  149. o.dispose();
  150. });
  151. this.meshes = [];
  152. this.skeletons.forEach((o) => {
  153. o.dispose();
  154. });
  155. this.skeletons = [];
  156. this.animationGroups.forEach((o) => {
  157. o.dispose();
  158. });
  159. this.animationGroups = [];
  160. this.multiMaterials.forEach((o) => {
  161. o.dispose();
  162. });
  163. this.multiMaterials = [];
  164. this.materials.forEach((o) => {
  165. o.dispose();
  166. });
  167. this.materials = [];
  168. this.geometries.forEach((o) => {
  169. o.dispose();
  170. });
  171. this.geometries = [];
  172. this.transformNodes.forEach((o) => {
  173. o.dispose();
  174. });
  175. this.transformNodes = [];
  176. this.actionManagers.forEach((o) => {
  177. o.dispose();
  178. });
  179. this.actionManagers = [];
  180. this.textures.forEach((o) => {
  181. o.dispose();
  182. });
  183. this.textures = [];
  184. this.reflectionProbes.forEach((o) => {
  185. o.dispose();
  186. });
  187. this.reflectionProbes = [];
  188. if (this.environmentTexture) {
  189. this.environmentTexture.dispose();
  190. this.environmentTexture = null;
  191. }
  192. for (let component of this.scene._serializableComponents) {
  193. component.removeFromContainer(this, true);
  194. }
  195. }
  196. private _moveAssets<T>(sourceAssets: T[], targetAssets: T[], keepAssets: T[]): void {
  197. if (!sourceAssets) {
  198. return;
  199. }
  200. for (let asset of sourceAssets) {
  201. let move = true;
  202. if (keepAssets) {
  203. for (let keepAsset of keepAssets) {
  204. if (asset === keepAsset) {
  205. move = false;
  206. break;
  207. }
  208. }
  209. }
  210. if (move) {
  211. targetAssets.push(asset);
  212. }
  213. }
  214. }
  215. /**
  216. * Removes all the assets contained in the scene and adds them to the container.
  217. * @param keepAssets Set of assets to keep in the scene. (default: empty)
  218. */
  219. public moveAllFromScene(keepAssets?: KeepAssets): void {
  220. if (keepAssets === undefined) {
  221. keepAssets = new KeepAssets();
  222. }
  223. for (let key in this) {
  224. if (this.hasOwnProperty(key)) {
  225. (<any>this)[key] = (<any>this)[key] || [];
  226. this._moveAssets((<any>this.scene)[key], (<any>this)[key], (<any>keepAssets)[key]);
  227. }
  228. }
  229. this.removeAllFromScene();
  230. }
  231. /**
  232. * 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.
  233. * @returns the root mesh
  234. */
  235. public createRootMesh() {
  236. var rootMesh = new Mesh("assetContainerRootMesh", this.scene);
  237. this.meshes.forEach((m) => {
  238. if (!m.parent) {
  239. rootMesh.addChild(m);
  240. }
  241. });
  242. this.meshes.unshift(rootMesh);
  243. return rootMesh;
  244. }
  245. }