123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- import { AbstractScene } from "./abstractScene";
- import { Scene } from "./scene";
- import { Mesh } from "./Meshes/mesh";
- /**
- * Set of assets to keep when moving a scene into an asset container.
- */
- export class KeepAssets extends AbstractScene { }
- /**
- * Container with a set of assets that can be added or removed from a scene.
- */
- export class AssetContainer extends AbstractScene {
- /**
- * The scene the AssetContainer belongs to.
- */
- public scene: Scene;
- /**
- * Instantiates an AssetContainer.
- * @param scene The scene the AssetContainer belongs to.
- */
- constructor(scene: Scene) {
- super();
- this.scene = scene;
- this["sounds"] = [];
- this["effectLayers"] = [];
- this["layers"] = [];
- this["lensFlareSystems"] = [];
- this["proceduralTextures"] = [];
- this["reflectionProbes"] = [];
- }
- /**
- * Adds all the assets from the container to the scene.
- */
- public addAllToScene() {
- this.cameras.forEach((o) => {
- this.scene.addCamera(o);
- });
- this.lights.forEach((o) => {
- this.scene.addLight(o);
- });
- this.meshes.forEach((o) => {
- this.scene.addMesh(o);
- });
- this.skeletons.forEach((o) => {
- this.scene.addSkeleton(o);
- });
- this.animations.forEach((o) => {
- this.scene.addAnimation(o);
- });
- this.animationGroups.forEach((o) => {
- this.scene.addAnimationGroup(o);
- });
- this.multiMaterials.forEach((o) => {
- this.scene.addMultiMaterial(o);
- });
- this.materials.forEach((o) => {
- this.scene.addMaterial(o);
- });
- this.morphTargetManagers.forEach((o) => {
- this.scene.addMorphTargetManager(o);
- });
- this.geometries.forEach((o) => {
- this.scene.addGeometry(o);
- });
- this.transformNodes.forEach((o) => {
- this.scene.addTransformNode(o);
- });
- this.actionManagers.forEach((o) => {
- this.scene.addActionManager(o);
- });
- this.textures.forEach((o) => {
- this.scene.addTexture(o);
- });
- this.reflectionProbes.forEach((o) => {
- this.scene.addReflectionProbe(o);
- });
- for (let component of this.scene._serializableComponents) {
- component.addFromContainer(this);
- }
- }
- /**
- * Removes all the assets in the container from the scene
- */
- public removeAllFromScene() {
- this.cameras.forEach((o) => {
- this.scene.removeCamera(o);
- });
- this.lights.forEach((o) => {
- this.scene.removeLight(o);
- });
- this.meshes.forEach((o) => {
- this.scene.removeMesh(o);
- });
- this.skeletons.forEach((o) => {
- this.scene.removeSkeleton(o);
- });
- this.animations.forEach((o) => {
- this.scene.removeAnimation(o);
- });
- this.animationGroups.forEach((o) => {
- this.scene.removeAnimationGroup(o);
- });
- this.multiMaterials.forEach((o) => {
- this.scene.removeMultiMaterial(o);
- });
- this.materials.forEach((o) => {
- this.scene.removeMaterial(o);
- });
- this.morphTargetManagers.forEach((o) => {
- this.scene.removeMorphTargetManager(o);
- });
- this.geometries.forEach((o) => {
- this.scene.removeGeometry(o);
- });
- this.transformNodes.forEach((o) => {
- this.scene.removeTransformNode(o);
- });
- this.actionManagers.forEach((o) => {
- this.scene.removeActionManager(o);
- });
- this.textures.forEach((o) => {
- this.scene.removeTexture(o);
- });
- this.reflectionProbes.forEach((o) => {
- this.scene.removeReflectionProbe(o);
- });
- for (let component of this.scene._serializableComponents) {
- component.removeFromContainer(this);
- }
- }
- /**
- * Disposes all the assets in the container
- */
- public dispose() {
- this.cameras.forEach((o) => {
- o.dispose();
- });
- this.lights.forEach((o) => {
- o.dispose();
- });
- this.meshes.forEach((o) => {
- o.dispose();
- });
- this.skeletons.forEach((o) => {
- o.dispose();
- });
- this.animationGroups.forEach((o) => {
- o.dispose();
- });
- this.multiMaterials.forEach((o) => {
- o.dispose();
- });
- this.materials.forEach((o) => {
- o.dispose();
- });
- this.geometries.forEach((o) => {
- o.dispose();
- });
- this.transformNodes.forEach((o) => {
- o.dispose();
- });
- this.actionManagers.forEach((o) => {
- o.dispose();
- });
- this.textures.forEach((o) => {
- o.dispose();
- });
- this.reflectionProbes.forEach((o) => {
- o.dispose();
- });
- for (let component of this.scene._serializableComponents) {
- component.dispose();
- }
- }
- private _moveAssets<T>(sourceAssets: T[], targetAssets: T[], keepAssets: T[]): void {
- if (!sourceAssets) {
- return;
- }
- for (let asset of sourceAssets) {
- let move = true;
- if (keepAssets) {
- for (let keepAsset of keepAssets) {
- if (asset === keepAsset) {
- move = false;
- break;
- }
- }
- }
- if (move) {
- targetAssets.push(asset);
- }
- }
- }
- /**
- * Removes all the assets contained in the scene and adds them to the container.
- * @param keepAssets Set of assets to keep in the scene. (default: empty)
- */
- public moveAllFromScene(keepAssets?: KeepAssets): void {
- if (keepAssets === undefined) {
- keepAssets = new KeepAssets();
- }
- for (let key in this) {
- if (this.hasOwnProperty(key)) {
- (<any>this)[key] = (<any>this)[key] || [];
- this._moveAssets((<any>this.scene)[key], (<any>this)[key], (<any>keepAssets)[key]);
- }
- }
- this.removeAllFromScene();
- }
- /**
- * 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.
- * @returns the root mesh
- */
- public createRootMesh() {
- var rootMesh = new Mesh("assetContainerRootMesh", this.scene);
- this.meshes.forEach((m) => {
- if (!m.parent) {
- rootMesh.addChild(m);
- }
- });
- this.meshes.unshift(rootMesh);
- return rootMesh;
- }
- }
|