【UE4】在 Dedicate Server 上刷新一帧骨骼 Mesh Pose

问题描述

  在 UE4 中,为了性能优化,常会在 Dedicate Server 上把 Mesh 的 Tick 关掉(即 OnlyTickMontagesWhenNotRendered),使其在在服务器上保持 TPose,只在客户端上实时刷新 Mesh Pose(即 AlwaysTickPoseAndRefreshBones)。

if (GetNetMode() == NM_DedicatedServer && GetMesh())
{
    
    
	GetMesh()->VisibilityBasedAnimTickOption = EVisibilityBasedAnimTickOption::OnlyTickPoseWhenRendered;
}

  这样会导致一个问题:当需要在服务器上知道 Mesh Pose 的时候(比如计算 Socket 位置的时候),如在服务器上释放一个法球,法球出生 Transform 为法师特定时刻手的 Transform,那就会计算出错(因为服务器上是 TPose)。
  简单的解决方案就是根据 Root 点配置偏移,通过偏移达到从手的位置释放法球的效果,但是这样的实现方式下,无论什么姿势,什么动作,法球 Spawn 位置固定,表现会不好。
  最好的方法还是在服务器是 Spawn 法球之前,Tick 一次 Mesh Pose,也就是从 TPose 变成正确的(和客户端一致)Pose。参考 UE4 AnswerHub,Tick 方法如下:

if (GetMesh())
{
    
    
	GetMesh()->TickPose(0.f, false);
	GetMesh()->RefreshBoneTransforms();
}

  从表现上看,确实可以达到计算服务器当前帧的 Pose 的目的,但是也发现了新的问题(TriggerAnimNotifies 递归调用问题):

TriggerAnimNotifies 递归调用问题

  参考 【UE4】TriggerAnimNotifies 递归调用问题,即 在 NotifyBegin 或者 NotifyEnd 的流程中,调用会触发 TriggerAnimNotifies 函数的方法,则会出现这一帧内这个 NotifyState 触发了一次 Begin,两次 End。
  重新看 Tick Pose 的调用:

GetMesh()->TickPose(0.f, false);
GetMesh()->RefreshBoneTransforms();

  TickPose 部分源码:

void USkeletalMeshComponent::TickPose(float DeltaTime, bool bNeedsValidRootMotion)
{
    
    
	Super::TickPose(DeltaTime, bNeedsValidRootMotion);

	if (ShouldTickAnimation())
	{
    
    
		// ...
		// 计算 DeltaTimeForTick;
		TickAnimation(DeltaTimeForTick, bNeedsValidRootMotion);
		// ...
	}
	// ...
}

  TickAnimation 部分源码:

void USkeletalMeshComponent::TickAnimation(float DeltaTime, bool bNeedsValidRootMotion)
{
    
    
	//...

	if (SkeletalMesh != nullptr)
	{
    
    
		// We're about to UpdateAnimation, this will potentially queue events that we'll need to dispatch.
		bNeedsQueuedAnimEventsDispatched = true;

		// We update linked instances first incase we're using either root motion or non-threaded update.
		// This ensures that we go through the pre update process and initialize the proxies correctly.
		for (UAnimInstance* LinkedInstance : LinkedInstances)
		{
    
    
			// Sub anim instances are always forced to do a parallel update 
			LinkedInstance->UpdateAnimation(DeltaTime * GlobalAnimRateScale, false, UAnimInstance::EUpdateAnimationFlag::ForceParallelUpdate);
		}

		if (AnimScriptInstance != nullptr)
		{
    
    
			// Tick the animation
			AnimScriptInstance->UpdateAnimation(DeltaTime * GlobalAnimRateScale, bNeedsValidRootMotion);
		}

		if(ShouldUpdatePostProcessInstance())
		{
    
    
			PostProcessAnimInstance->UpdateAnimation(DeltaTime * GlobalAnimRateScale, false);
		}

		/**
			If we're called directly for autonomous proxies, TickComponent is not guaranteed to get called.
			So dispatch all queued events here if we're doing MontageOnly ticking.
		*/
		if (ShouldOnlyTickMontages(DeltaTime))
		{
    
    
			ConditionallyDispatchQueuedAnimEvents();
		}
	}
}

  ConditionallyDispatchQueuedAnimEvents 会触发动画通知,所以如果我们不希望触发,DeltaTime 应该传 0,且本身如果希望当前帧,也应该传入 0。
  RefreshBoneTransforms 部分源码:

void USkeletalMeshComponent::RefreshBoneTransforms(FActorComponentTickFunction* TickFunction)
{
    
    
	// ...
	
	if (TickFunction == nullptr && ShouldBlendPhysicsBones())
	{
    
    
		FinalizeBoneTransform();
	}
}

  FinalizeBoneTransform 会调用 ConditionallyDispatchQueuedAnimEvents()(会判断 bNeedsQueuedAnimEventsDispatched,这个变量在 TickAnimation 里被设为 true),最终调用 TriggerAnimNotifies,造成还没有 MoveTemp,就又触发了一次 End,即一次 Begin,两次 End。

解决方案

  为了避免这个问题,可以如下调用,也可达到刷新一帧 Mesh Pose 的效果:

if (SkelMeshComponent->AnimScriptInstance)
{
    
    
	// Tick the animation
	SkelMeshComponent->AnimScriptInstance->UpdateAnimation(0.f, false);
}
SkelMeshComponent->RefreshBoneTransforms();

  不过 UE 自身源码中也存在如下代码(不确定是没有考虑这个问题,还是觉得两次 End 没有什么问题):

void USkeletalMeshComponent::InitAnim(bool bForceReinit)
{
    
    
	// ...
	
	//...
	{
    
    
		TickAnimation(0.f, false);
		RefreshBoneTransforms();
	}
	
	// ...
}

相关参考

  1. 【UE4】TriggerAnimNotifies 递归调用问题
  2. Dedicated server tick mesh pose for a single frame

猜你喜欢

转载自blog.csdn.net/Bob__yuan/article/details/113988174