|
基本原理
每个动画(AnimSequence)要指定一个重定向源(RetargetSource),然后实时的时候根据重定向源的TPose和当前角色的TPose进行重定向的计算。
实时重定向的计算方法又分为五种: Animation, Skeleton, AnimationScale, AnimationRelative, OrientAndScale
骨骼重定向方法的设置粒度是骨骼级别,具体使用方法参见官方文档
官方文档:
https://docs.unrealengine.com/en-US/Engine/Animation/AnimationRetargeting/index.html
我印象中的常用操作,通常有问题都是用以下操作解决:
某个动画重定向结果不对:在动画编辑器里指定下重定向源
某根骨骼的效果结果不对:在骨骼树窗口里更改此骨骼的重定向设置
新角色的动画,需要在最大化骨架编辑器里新增一下新角色的重定向源
双手武器重定向后,挂接效果不对。这时候可能就需要ik了,ik和重定向是一个搭档。
实时重定向源码
UE中获取任何的骨骼动画数据,都会调用 UAnimSequence::GetBonePose
GetBonePose就像古代的关隘,连接动画逻辑(动画树、Montage等)与动画数据
压缩动画的重定向
压缩动画的重定向核心代码就直接在GetBonePose函数的末尾
由于压缩动画重定向和非压缩动画的重定向的原理是一样的,只是读取动画数据的方式不同,所以重定向源码分析放到下节
非压缩动画的重定向
GetBonePose函数并没有直接处理非压缩动画的重定向,而是通过如下调用栈:
BuildPoseFromRawData -> RetargetBoneTransform -> FAnimationRuntime::RetargetBoneTransform
所以非压缩动画的重定向核心逻辑如下
void FAnimationRuntime::RetargetBoneTransform(const USkeleton* MySkeleton, const FName& RetargetSource, FTransform& BoneTransform, const int32 SkeletonBoneIndex, const FCompactPoseBoneIndex& BoneIndex, const FBoneContainer& RequiredBones, const bool bIsBakedAdditive){ if (MySkeleton) { switch (MySkeleton->GetBoneTranslationRetargetingMode(SkeletonBoneIndex)) { case EBoneTranslationRetargetingMode::AnimationScaled: { // @todo - precache that in FBoneContainer when we have SkeletonIndex->TrackIndex mapping. So we can just apply scale right away. const TArray<FTransform>& SkeletonRefPoseArray = MySkeleton->GetRefLocalPoses(RetargetSource); const float SourceTranslationLength = SkeletonRefPoseArray[SkeletonBoneIndex].GetTranslation().Size(); if (SourceTranslationLength > KINDA_SMALL_NUMBER) { const float TargetTranslationLength = RequiredBones.GetRefPoseTransform(BoneIndex).GetTranslation().Size(); BoneTransform.ScaleTranslation(TargetTranslationLength / SourceTranslationLength); } break; } case EBoneTranslationRetargetingMode::Skeleton: { BoneTransform.SetTranslation(bIsBakedAdditive ? FVector::ZeroVector : RequiredBones.GetRefPoseTransform(BoneIndex).GetTranslation()); break; } case EBoneTranslationRetargetingMode::AnimationRelative: { // With baked additive animations, Animation Relative delta gets canceled out, so we can skip it. // (A1 + Rel) - (A2 + Rel) = A1 - A2. if (!bIsBakedAdditive) { const TArray<FTransform>& AuthoredOnRefSkeleton = MySkeleton->GetRefLocalPoses(RetargetSource); const TArray<FTransform>& PlayingOnRefSkeleton = RequiredBones.GetRefPoseCompactArray(); const FTransform& RefPoseTransform = RequiredBones.GetRefPoseTransform(BoneIndex); // Apply the retargeting as if it were an additive difference between the current skeleton and the retarget skeleton. BoneTransform.SetRotation(BoneTransform.GetRotation() * AuthoredOnRefSkeleton[SkeletonBoneIndex].GetRotation().Inverse() * RefPoseTransform.GetRotation()); BoneTransform.SetTranslation(BoneTransform.GetTranslation() + (RefPoseTransform.GetTranslation() - AuthoredOnRefSkeleton[SkeletonBoneIndex].GetTranslation())); BoneTransform.SetScale3D(BoneTransform.GetScale3D() * (RefPoseTransform.GetScale3D() * AuthoredOnRefSkeleton[SkeletonBoneIndex].GetSafeScaleReciprocal(AuthoredOnRefSkeleton[SkeletonBoneIndex].GetScale3D()))); BoneTransform.NormalizeRotation(); } break; } case EBoneTranslationRetargetingMode::OrientAndScale: { if (!bIsBakedAdditive) { const FRetargetSourceCachedData& RetargetSourceCachedData = RequiredBones.GetRetargetSourceCachedData(RetargetSource); const TArray<FOrientAndScaleRetargetingCachedData>& OrientAndScaleDataArray = RetargetSourceCachedData.OrientAndScaleData; const TArray<int32>& CompactPoseIndexToOrientAndScaleIndex = RetargetSourceCachedData.CompactPoseIndexToOrientAndScaleIndex; // If we have any cached retargeting data. if ((OrientAndScaleDataArray.Num() > 0) && (CompactPoseIndexToOrientAndScaleIndex.Num() == RequiredBones.GetCompactPoseNumBones())) { const int32 OrientAndScaleIndex = CompactPoseIndexToOrientAndScaleIndex[BoneIndex.GetInt()]; if (OrientAndScaleIndex != INDEX_NONE) { const FOrientAndScaleRetargetingCachedData& OrientAndScaleData = OrientAndScaleDataArray[OrientAndScaleIndex]; const FVector AnimatedTranslation = BoneTransform.GetTranslation(); // If Translation is not animated, we can just copy the TargetTranslation. No retargeting needs to be done. const FVector NewTranslation = (AnimatedTranslation - OrientAndScaleData.SourceTranslation).IsNearlyZero(BONE_TRANS_RT_ORIENT_AND_SCALE_PRECISION) ? OrientAndScaleData.TargetTranslation : OrientAndScaleData.TranslationDeltaOrient.RotateVector(AnimatedTranslation) * OrientAndScaleData.TranslationScale; BoneTransform.SetTranslation(NewTranslation); } } } break; } } }}
这个函数只是一根骨骼的重定向计算。
当前角色的TPose就存在RequiredBones里。
重定向源的TPose都是存在最大化骨架MySkeleton中的,根据AnimSequence.RetargetSource来获取当前的重定向源数据
大部分的实时重定向的方法主要改的就是骨骼的translation,scale和rotation不会动,AnimationRelative模式例外
Animation: 纯动画,不做任何重定向Skeleton: 直接用角色TPose的translationAnimationScale: 用动画的Translation,但是会根据当前角色的TPose和RetargetSource的TPose的比例对Translation进行缩放。 和AnimationRelative相比,delta是用除法算出来的,且只改TranslationAnimationRelative: 用动画的Translation,scale,rotation,但是会根据当前角色的TPose和RetargetSource的TPose的差值对动画的prs进行增减。和AnimationScale相比,delta是用减法算出来的。OrientAndScale: 用动画的Translation,实时会根据OrientAndScaleData的信息对Translation进行变换
OrientAndScale模式下的OrientAndScaleData的计算
OrientAndScaleData.TranslationDeltaOrient是一个四元数旋转: 是从重定向源的骨骼translation(SourceSkelTransDir)到当前角色TPose的骨骼Translation(TargetSkelTransDir)的一个旋转OrientAndScaleData.TranslationScale是一个scale的除法: 当前角色TPose的骨骼Translation的长度(TargetSkelTransLength) / 重定向源的骨骼translation的长度(SourceSkelTransLength)
const FRetargetSourceCachedData& FBoneContainer::GetRetargetSourceCachedData(const FName& InRetargetSourceName) const{ FRetargetSourceCachedData* RetargetSourceCachedData = RetargetSourceCachedDataLUT.Find(InRetargetSourceName); if (!RetargetSourceCachedData) { RetargetSourceCachedData = &RetargetSourceCachedDataLUT.Add(InRetargetSourceName); // Build Cached Data for OrientAndScale retargeting. const TArray<FTransform>& AuthoredOnRefSkeleton = AssetSkeleton->GetRefLocalPoses(InRetargetSourceName); const TArray<FTransform>& PlayingOnRefSkeleton = GetRefPoseCompactArray(); const int32 CompactPoseNumBones = GetCompactPoseNumBones(); RetargetSourceCachedData->CompactPoseIndexToOrientAndScaleIndex.Reset(); for (int32 CompactBoneIndex = 0; CompactBoneIndex < CompactPoseNumBones; CompactBoneIndex++) { const int32& SkeletonBoneIndex = CompactPoseToSkeletonIndex[CompactBoneIndex]; if (AssetSkeleton->GetBoneTranslationRetargetingMode(SkeletonBoneIndex) == EBoneTranslationRetargetingMode::OrientAndScale) { const FVector SourceSkelTrans = AuthoredOnRefSkeleton[SkeletonBoneIndex].GetTranslation(); const FVector TargetSkelTrans = PlayingOnRefSkeleton[CompactBoneIndex].GetTranslation(); // If translations are identical, we don't need to do any retargeting if (!SourceSkelTrans.Equals(TargetSkelTrans, BONE_TRANS_RT_ORIENT_AND_SCALE_PRECISION)) { const float SourceSkelTransLength = SourceSkelTrans.Size(); const float TargetSkelTransLength = TargetSkelTrans.Size(); // this only works on non zero vectors. if (!FMath::IsNearlyZero(SourceSkelTransLength * TargetSkelTransLength)) { const FVector SourceSkelTransDir = SourceSkelTrans / SourceSkelTransLength; const FVector TargetSkelTransDir = TargetSkelTrans / TargetSkelTransLength; const FQuat DeltaRotation = FQuat::FindBetweenNormals(SourceSkelTransDir, TargetSkelTransDir); const float Scale = TargetSkelTransLength / SourceSkelTransLength; const int32 OrientAndScaleIndex = RetargetSourceCachedData->OrientAndScaleData.Add(FOrientAndScaleRetargetingCachedData(DeltaRotation, Scale, SourceSkelTrans, TargetSkelTrans)); // initialize CompactPoseBoneIndex to OrientAndScale Index LUT on demand if (RetargetSourceCachedData->CompactPoseIndexToOrientAndScaleIndex.Num() == 0) { RetargetSourceCachedData->CompactPoseIndexToOrientAndScaleIndex.Init(INDEX_NONE, CompactPoseNumBones); } RetargetSourceCachedData->CompactPoseIndexToOrientAndScaleIndex[CompactBoneIndex] = OrientAndScaleIndex; } } } } } return *RetargetSourceCachedData;} |
|