Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to add element and all its children to AssetContainer #14457

Merged
merged 6 commits into from Oct 25, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 96 additions & 2 deletions packages/dev/core/src/assetContainer.ts
Expand Up @@ -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";
Expand All @@ -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";
carolhmj marked this conversation as resolved.
Show resolved Hide resolved

/**
* Set of assets to keep when moving a scene into an asset container.
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -1050,4 +1051,97 @@ 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<Node>();
const visitedNodes = new Set<Node>();

nodesToVisit.push(root);

while (nodesToVisit.length > 0) {
const nodeToVisit = nodesToVisit.pop()!;
visitedNodes.add(nodeToVisit);
carolhmj marked this conversation as resolved.
Show resolved Hide resolved

if (nodeToVisit instanceof Mesh) {
if (nodeToVisit.geometry) {
this.geometries.push(nodeToVisit.geometry);
carolhmj marked this conversation as resolved.
Show resolved Hide resolved
}
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");
carolhmj marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}

this.populateRootNodes();
}
}