From 07b92dd33f793e97658e0edb426b3376173cad9d Mon Sep 17 00:00:00 2001 From: Carolina Herbster Date: Tue, 24 Oct 2023 10:47:43 -0300 Subject: [PATCH 1/6] function to add all elements to assetcontainer --- packages/dev/core/src/assetContainer.ts | 96 ++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/packages/dev/core/src/assetContainer.ts b/packages/dev/core/src/assetContainer.ts index 56f2f527e8e..87ff6f03f34 100644 --- a/packages/dev/core/src/assetContainer.ts +++ b/packages/dev/core/src/assetContainer.ts @@ -5,7 +5,7 @@ import { TransformNode } from "./Meshes/transformNode"; import type { Skeleton } from "./Bones/skeleton"; import type { AnimationGroup } from "./Animations/animationGroup"; import type { Animatable } from "./Animations/animatable"; -import type { AbstractMesh } from "./Meshes/abstractMesh"; +import { AbstractMesh } from "./Meshes/abstractMesh"; import type { MultiMaterial } from "./Materials/multiMaterial"; import type { Material } from "./Materials/material"; import { Logger } from "./Misc/logger"; @@ -18,6 +18,7 @@ import { InstancedMesh } from "./Meshes/instancedMesh"; import { Light } from "./Lights/light"; import { Camera } from "./Cameras/camera"; import { Tools } from "./Misc/tools"; +import { PBRMaterial, StandardMaterial } from "./Materials"; /** * Set of assets to keep when moving a scene into an asset container. @@ -694,7 +695,7 @@ export class AssetContainer extends AbstractScene { if (predicate && !predicate(o)) { return; } - this.scene.removeMesh(o); + this.scene.removeMesh(o, true); }); this.skeletons.forEach((o) => { if (predicate && !predicate(o)) { @@ -1050,4 +1051,95 @@ export class AssetContainer extends AbstractScene { } }); } + + private _checkAndAddTextureStandardMaterial(material: StandardMaterial, textureName: keyof StandardMaterial) { + if (material[textureName]) { + this.textures.push(material[textureName]); + } + } + + private _checkAndAddTexturePBRMaterial(material: PBRMaterial, textureName: keyof PBRMaterial) { + if (material[textureName]) { + this.textures.push(material[textureName]); + } + } + + /** + * @since + * Given a root asset, this method will traverse its hierarchy and add it, its children and any materials/skeletons/animation groups to the container. + * @param root + */ + public addAllAssetsToContainer(root: Node) { + if (!root) { + return; + } + + const nodesToVisit = new Array(); + const visitedNodes = new Set(); + + nodesToVisit.push(root); + + while (nodesToVisit.length > 0) { + const nodeToVisit = nodesToVisit.pop()!; + visitedNodes.add(nodeToVisit); + + if (nodeToVisit instanceof Mesh) { + if (nodeToVisit.geometry) { + this.geometries.push(nodeToVisit.geometry); + } + this.meshes.push(nodeToVisit); + } else if (nodeToVisit instanceof TransformNode) { + this.transformNodes.push(nodeToVisit); + } else if (nodeToVisit instanceof Light) { + this.lights.push(nodeToVisit); + } else if (nodeToVisit instanceof Camera) { + this.cameras.push(nodeToVisit); + } + + if (nodeToVisit instanceof AbstractMesh) { + if (nodeToVisit.material) { + this.materials.push(nodeToVisit.material); + if (nodeToVisit.material instanceof StandardMaterial) { + this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "diffuseTexture"); + this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "bumpTexture"); + this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "ambientTexture"); + this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "opacityTexture"); + this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "reflectionTexture"); + this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "emissiveTexture"); + this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "specularTexture"); + this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "lightmapTexture"); + this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "refractionTexture"); + } else if (nodeToVisit.material instanceof PBRMaterial) { + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "albedoTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "ambientTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "opacityTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "reflectionTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "emissiveTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "metallicTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "reflectivityTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "bumpTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "lightmapTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "refractionTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "microSurfaceTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "metallicReflectanceTexture"); + this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "reflectanceTexture"); + } + } + + if (nodeToVisit.skeleton) { + this.skeletons.push(nodeToVisit.skeleton); + } + + if (nodeToVisit.morphTargetManager) { + this.morphTargetManagers.push(nodeToVisit.morphTargetManager); + } + } + + for (const child of nodeToVisit.getChildren()) { + if (!visitedNodes.has(child)) { + nodesToVisit.push(child); + } + } + } + } } From 0fa4d5009fea80db624e9125f482c7947ca41fc9 Mon Sep 17 00:00:00 2001 From: Carolina Herbster Date: Tue, 24 Oct 2023 11:35:23 -0300 Subject: [PATCH 2/6] add populate root nodes --- packages/dev/core/src/assetContainer.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/dev/core/src/assetContainer.ts b/packages/dev/core/src/assetContainer.ts index 87ff6f03f34..808a290c188 100644 --- a/packages/dev/core/src/assetContainer.ts +++ b/packages/dev/core/src/assetContainer.ts @@ -1141,5 +1141,7 @@ export class AssetContainer extends AbstractScene { } } } + + this.populateRootNodes(); } } From 3d30150adc1ef046bf847081ff6a2b669dcbaa92 Mon Sep 17 00:00:00 2001 From: Carolina Herbster Date: Tue, 24 Oct 2023 13:33:00 -0300 Subject: [PATCH 3/6] fix imports --- packages/dev/core/src/assetContainer.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/dev/core/src/assetContainer.ts b/packages/dev/core/src/assetContainer.ts index 808a290c188..a58527457ed 100644 --- a/packages/dev/core/src/assetContainer.ts +++ b/packages/dev/core/src/assetContainer.ts @@ -18,7 +18,8 @@ import { InstancedMesh } from "./Meshes/instancedMesh"; import { Light } from "./Lights/light"; import { Camera } from "./Cameras/camera"; import { Tools } from "./Misc/tools"; -import { PBRMaterial, StandardMaterial } from "./Materials"; +import { PBRMaterial } from "./Materials/PBR/pbrMaterial"; +import { StandardMaterial } from "./Materials/standardMaterial"; /** * Set of assets to keep when moving a scene into an asset container. From fa7b2460e73138ebe104d463a6c9c4e1e641fb7c Mon Sep 17 00:00:00 2001 From: Carolina Herbster Date: Tue, 24 Oct 2023 15:29:47 -0300 Subject: [PATCH 4/6] use getactivetextures --- packages/dev/core/src/assetContainer.ts | 40 +------------------------ 1 file changed, 1 insertion(+), 39 deletions(-) diff --git a/packages/dev/core/src/assetContainer.ts b/packages/dev/core/src/assetContainer.ts index a58527457ed..b3e700a7f86 100644 --- a/packages/dev/core/src/assetContainer.ts +++ b/packages/dev/core/src/assetContainer.ts @@ -18,8 +18,6 @@ import { InstancedMesh } from "./Meshes/instancedMesh"; import { Light } from "./Lights/light"; import { Camera } from "./Cameras/camera"; import { Tools } from "./Misc/tools"; -import { PBRMaterial } from "./Materials/PBR/pbrMaterial"; -import { StandardMaterial } from "./Materials/standardMaterial"; /** * Set of assets to keep when moving a scene into an asset container. @@ -1053,18 +1051,6 @@ export class AssetContainer extends AbstractScene { }); } - private _checkAndAddTextureStandardMaterial(material: StandardMaterial, textureName: keyof StandardMaterial) { - if (material[textureName]) { - this.textures.push(material[textureName]); - } - } - - private _checkAndAddTexturePBRMaterial(material: PBRMaterial, textureName: keyof PBRMaterial) { - if (material[textureName]) { - this.textures.push(material[textureName]); - } - } - /** * @since * Given a root asset, this method will traverse its hierarchy and add it, its children and any materials/skeletons/animation groups to the container. @@ -1100,31 +1086,7 @@ export class AssetContainer extends AbstractScene { if (nodeToVisit instanceof AbstractMesh) { if (nodeToVisit.material) { this.materials.push(nodeToVisit.material); - if (nodeToVisit.material instanceof StandardMaterial) { - this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "diffuseTexture"); - this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "bumpTexture"); - this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "ambientTexture"); - this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "opacityTexture"); - this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "reflectionTexture"); - this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "emissiveTexture"); - this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "specularTexture"); - this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "lightmapTexture"); - this._checkAndAddTextureStandardMaterial(nodeToVisit.material, "refractionTexture"); - } else if (nodeToVisit.material instanceof PBRMaterial) { - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "albedoTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "ambientTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "opacityTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "reflectionTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "emissiveTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "metallicTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "reflectivityTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "bumpTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "lightmapTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "refractionTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "microSurfaceTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "metallicReflectanceTexture"); - this._checkAndAddTexturePBRMaterial(nodeToVisit.material, "reflectanceTexture"); - } + this.textures.push(...nodeToVisit.material.getActiveTextures()); } if (nodeToVisit.skeleton) { From 5c8b05e8071c5a925243d979c17162a44f5ca6f9 Mon Sep 17 00:00:00 2001 From: Carolina Herbster Date: Wed, 25 Oct 2023 08:03:38 -0300 Subject: [PATCH 5/6] move to end of function --- packages/dev/core/src/assetContainer.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/dev/core/src/assetContainer.ts b/packages/dev/core/src/assetContainer.ts index b3e700a7f86..87a853442cd 100644 --- a/packages/dev/core/src/assetContainer.ts +++ b/packages/dev/core/src/assetContainer.ts @@ -1068,7 +1068,6 @@ export class AssetContainer extends AbstractScene { while (nodesToVisit.length > 0) { const nodeToVisit = nodesToVisit.pop()!; - visitedNodes.add(nodeToVisit); if (nodeToVisit instanceof Mesh) { if (nodeToVisit.geometry) { @@ -1103,6 +1102,8 @@ export class AssetContainer extends AbstractScene { nodesToVisit.push(child); } } + + visitedNodes.add(nodeToVisit); } this.populateRootNodes(); From 9d61a6c71cd9101b9166f340deec6b66e897c230 Mon Sep 17 00:00:00 2001 From: Carolina Herbster Date: Wed, 25 Oct 2023 10:16:56 -0300 Subject: [PATCH 6/6] check for repeated elements --- packages/dev/core/src/assetContainer.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/dev/core/src/assetContainer.ts b/packages/dev/core/src/assetContainer.ts index 87a853442cd..470304875ab 100644 --- a/packages/dev/core/src/assetContainer.ts +++ b/packages/dev/core/src/assetContainer.ts @@ -1070,7 +1070,7 @@ export class AssetContainer extends AbstractScene { const nodeToVisit = nodesToVisit.pop()!; if (nodeToVisit instanceof Mesh) { - if (nodeToVisit.geometry) { + if (nodeToVisit.geometry && this.geometries.indexOf(nodeToVisit.geometry) === -1) { this.geometries.push(nodeToVisit.geometry); } this.meshes.push(nodeToVisit); @@ -1083,16 +1083,20 @@ export class AssetContainer extends AbstractScene { } if (nodeToVisit instanceof AbstractMesh) { - if (nodeToVisit.material) { + if (nodeToVisit.material && this.materials.indexOf(nodeToVisit.material) === -1) { this.materials.push(nodeToVisit.material); - this.textures.push(...nodeToVisit.material.getActiveTextures()); + for (const texture of nodeToVisit.material.getActiveTextures()) { + if (this.textures.indexOf(texture) === -1) { + this.textures.push(texture); + } + } } - if (nodeToVisit.skeleton) { + if (nodeToVisit.skeleton && this.skeletons.indexOf(nodeToVisit.skeleton) === -1) { this.skeletons.push(nodeToVisit.skeleton); } - if (nodeToVisit.morphTargetManager) { + if (nodeToVisit.morphTargetManager && this.morphTargetManagers.indexOf(nodeToVisit.morphTargetManager) === -1) { this.morphTargetManagers.push(nodeToVisit.morphTargetManager); } }