Uses the Physics Asset to compute an accurate reference-pose bounding box for a skeletal mesh asset. This typically has more utility than the default skeletal mesh bounds which can often be arbitrarily inflated or scaled.

bool UMyFunctionLibrary::GetAccurateReferencePoseBounds(FBox& AABB, const USkeletalMesh* Mesh)
{
	AABB = FBox(EForceInit::ForceInitToZero);

	if (!Mesh) { return false; }
	if (!Mesh->GetPhysicsAsset()) { return false; }

	for (const USkeletalBodySetup* BodySetups : Mesh->GetPhysicsAsset()->SkeletalBodySetups)
	{
		const FReferenceSkeleton& RefSkeleton = Mesh->GetSkeleton()->GetReferenceSkeleton();
		FTransform ComponentSpaceBoneTransform = FTransform::Identity;

		int32 BoneIndex = RefSkeleton.FindBoneIndex(BodySetups->BoneName);
		while (BoneIndex != INDEX_NONE)
		{
			const FTransform& BoneLocalTM = RefSkeleton.GetRefBonePose()[BoneIndex];
			ComponentSpaceBoneTransform = ComponentSpaceBoneTransform * BoneLocalTM;

			BoneIndex = RefSkeleton.GetParentIndex(BoneIndex);
		}

		AABB += BodySetups->AggGeom.CalcAABB(ComponentSpaceBoneTransform);
	}

	return true;
}
Author James
Published
Categories Snippet
Views 1111
1