I wanted to freeze the meshes to improve performance, but

I want to freeze the mesh to improve performance, but I don’t know how to tell if the current mesh has an animation group, if so, its child and parent nodes should not be frozen, is there a corresponding api?

there is my code:

export function freezeNonAnimationNodes(loadedParams: IFreezeNonAnimationNodesParams) {
  const now = performance.now();
  const { loadedAnimationGroups, loadedTransformNodes, loadedMeshes } = loadedParams;
  const checkedNodes = new Map<AbstractMesh | TransformNode, boolean>();
  const nodesToFreeze: Array<AbstractMesh | TransformNode> = [];

  function isNodeOrChildrenInAnimationGroup(node: any) {
    if (checkedNodes.has(node)) {
      return checkedNodes.has(node);
    }

    let inAnimationGroup = _isNodeInAnimationGroup(node);
    if (!inAnimationGroup) {
      for (const child of node.getChildren()) {
        if (isNodeOrChildrenInAnimationGroup(child)) {
          inAnimationGroup = true;
          break;
        }
      }
    }
    checkedNodes.set(node, inAnimationGroup);
    return inAnimationGroup;
  }

  function _isNodeInAnimationGroup(node: any) {
    return loadedAnimationGroups.some((group: AnimationGroup) => group.targetedAnimations.some((animation) => animation.target === node));
  }

  loadedMeshes.forEach((mesh) => {
    if (mesh.animations.length === 0 && !isNodeOrChildrenInAnimationGroup(mesh)) {
      nodesToFreeze.push(mesh);
    }
  });

  loadedTransformNodes.forEach((node) => {
    if (node.animations.length === 0 && !isNodeOrChildrenInAnimationGroup(node)) {
      nodesToFreeze.push(node);
    }
  });

and use it :

freezeNonAnimationNodes({ loadedAnimationGroups, loadedTransformNodes, loadedMeshes });

boys, could you give me some advice?

to my knowledge, there’s no direct way to check if a mesh belongs to an animation group.
you need to check the targetedAnimations and find the targetMesh.

maybe this can help

function getUniqueTargetMeshes(animationGroup) {
    const uniqueMeshes = new Set();

    animationGroup.targetedAnimations.forEach(targetedAnimation => {
        uniqueMeshes.add(targetedAnimation.target);
    });

    return Array.from(uniqueMeshes);
}

should add its children or parent ?
such as:

function getUniqueTargetMeshes(animationGroup) {
    const uniqueMeshes = new Set();

    animationGroup.targetedAnimations.forEach(targetedAnimation => {
        uniqueMeshes.add(targetedAnimation.target);
        if(targetedAnimation.target.parent){
             uniqueMeshes.add(targetedAnimation.target.parent)
        }  
        if(targetedAnimation.target.children){
            uniqueMeshes.add(targetedAnimation.target.children)
         }

    });

    return Array.from(uniqueMeshes);
}

Note that for skeletal animations, animations won’t be applied on meshes but either on bones or transform nodes (in case the animations are loaded from a gltf file).

1 Like

ok ,thank u;