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 all 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
66 changes: 64 additions & 2 deletions packages/dev/core/src/assetContainer.ts
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -694,7 +694,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 +1050,66 @@ export class AssetContainer extends AbstractScene {
}
});
}

/**
* @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()!;

if (nodeToVisit instanceof Mesh) {
if (nodeToVisit.geometry && this.geometries.indexOf(nodeToVisit.geometry) === -1) {
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.indexOf(nodeToVisit.material) === -1) {
this.materials.push(nodeToVisit.material);
for (const texture of nodeToVisit.material.getActiveTextures()) {
if (this.textures.indexOf(texture) === -1) {
this.textures.push(texture);
}
}
}

if (nodeToVisit.skeleton && this.skeletons.indexOf(nodeToVisit.skeleton) === -1) {
this.skeletons.push(nodeToVisit.skeleton);
}

if (nodeToVisit.morphTargetManager && this.morphTargetManagers.indexOf(nodeToVisit.morphTargetManager) === -1) {
this.morphTargetManagers.push(nodeToVisit.morphTargetManager);
}
}

for (const child of nodeToVisit.getChildren()) {
if (!visitedNodes.has(child)) {
nodesToVisit.push(child);
}
}

visitedNodes.add(nodeToVisit);
}

this.populateRootNodes();
}
}