Files
UnrealEngineUWP/Engine/Source/Runtime/AnimGraphRuntime/Private/AnimNodes/AnimNode_AimOffsetLookAt.cpp
Thomas Sarkanen 16eee0289d Anim node data/compiler refactor
Per-node constant data is now held on a generated struct as part of sparse class data.
Per-node mutable data (i.e. pin links/property access mappings) is now held on a generated 'mutable data' struct that is compiled as part of the generated class.

The anim BP compiler is now extended more conventionally using UAnimBlueprintExtension, derived from UBlueprintExtension. This directly replaces the older 'compiler handler' pattern that was added in an emergency fashion for 4.26. Anim graph nodes now request their required extensions and these are held on the UAnimBlueprint in the UBlueprint::Extensions array. The Extensions array is potentially refreshed with any node addition or removal. The Extensions array is force-refreshed each time an anim BP is compiled for the first time to deal with newly added or removed requirements.

Const-corrected a bunch of UAnimInstance/FAnimInstanceProxy APIs that rely on (now truly) const data.
Added a split state/constant version of FInputScaleBiasClamp to allow some of its data to be split into constants.
Tweaked alignment/ordering of FPoseLinkBase to save a few bytes per pose link.
Deprecated FAnimNode_Base::OverrideAsset in favor of a more UAnimGraphNode_Base-based approach. Individual nodes can still have runtime overrides via specific accessors. The new approach will also give us the oppurtunity to override multiple assets per node if required in the future.

Moved property access into Engine module & removed event support from it - this was never used.
Reworked property access compilation API a little - construction/lifetime was a bit confusing previously.

Optimized path used to create UK2Node_StructMemberSet nodes in per-node custom events. When using mutable data, the structure used is large and very sparsely connected (i.e. only a few properties are written) so we only create pins that are actually going to be used, rather than creating all of them and conly connecting a few.

Patched the following nodes to use the new data approach:

- Asset players (sequences, blendspaces, aim offsets)
- Blend lists
- Ref poses
- Roots

#rb Jurre.deBaare, Martin.Wilson, Keith.Yerex

[CL 16090510 by Thomas Sarkanen in ue5-main branch]
2021-04-22 04:57:09 -04:00

202 lines
8.1 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "AnimNodes/AnimNode_AimOffsetLookAt.h"
#include "Animation/AnimInstanceProxy.h"
#include "AnimationRuntime.h"
#include "Animation/BlendSpace.h"
#include "Animation/BlendSpace1D.h"
#include "Engine/SkeletalMeshSocket.h"
#include "DrawDebugHelpers.h"
#include "EngineGlobals.h"
#include "Engine/Engine.h"
#include "Animation/AnimTrace.h"
TAutoConsoleVariable<int32> CVarAimOffsetLookAtEnable(TEXT("a.AnimNode.AimOffsetLookAt.Enable"), 1, TEXT("Enable/Disable LookAt AimOffset"));
TAutoConsoleVariable<int32> CVarAimOffsetLookAtDebug(TEXT("a.AnimNode.AimOffsetLookAt.Debug"), 0, TEXT("Toggle LookAt AimOffset debug"));
/////////////////////////////////////////////////////
// FAnimNode_AimOffsetLookAt
void FAnimNode_AimOffsetLookAt::Initialize_AnyThread(const FAnimationInitializeContext& Context)
{
DECLARE_SCOPE_HIERARCHICAL_COUNTER_ANIMNODE(Initialize_AnyThread)
FAnimNode_BlendSpacePlayer::Initialize_AnyThread(Context);
BasePose.Initialize(Context);
}
void FAnimNode_AimOffsetLookAt::OnInitializeAnimInstance(const FAnimInstanceProxy* InProxy, const UAnimInstance* InAnimInstance)
{
FAnimNode_BlendSpacePlayer::OnInitializeAnimInstance(InProxy, InAnimInstance);
SocketBoneReference.BoneName = NAME_None;
if (USkeletalMeshComponent* SkelMeshComp = InAnimInstance->GetSkelMeshComponent())
{
if (USkeletalMesh* SkelMesh = SkelMeshComp->SkeletalMesh)
{
if (const USkeletalMeshSocket* Socket = SkelMesh->FindSocket(SourceSocketName))
{
SocketLocalTransform = Socket->GetSocketLocalTransform();
SocketBoneReference.BoneName = Socket->BoneName;
}
if (const USkeletalMeshSocket* Socket = SkelMesh->FindSocket(PivotSocketName))
{
PivotSocketLocalTransform = Socket->GetSocketLocalTransform();
PivotSocketBoneReference.BoneName = Socket->BoneName;
}
}
}
}
void FAnimNode_AimOffsetLookAt::UpdateAssetPlayer(const FAnimationUpdateContext& Context)
{
GetEvaluateGraphExposedInputs().Execute(Context);
bIsLODEnabled = IsLODEnabled(Context.AnimInstanceProxy);
// We don't support ticking and advancing time, because Inputs are determined during Evaluate.
// it may be possible to advance time there (is it a problem with notifies?)
// But typically AimOffsets contain single frame poses, so time doesn't matter.
// if (bIsLODEnabled)
// {
// FAnimNode_BlendSpacePlayer::UpdateAssetPlayer(Context);
// }
BasePose.Update(Context);
TRACE_ANIM_NODE_VALUE(Context, TEXT("Playback Time"), InternalTimeAccumulator);
}
void FAnimNode_AimOffsetLookAt::CacheBones_AnyThread(const FAnimationCacheBonesContext& Context)
{
DECLARE_SCOPE_HIERARCHICAL_COUNTER_ANIMNODE(CacheBones_AnyThread)
FAnimNode_BlendSpacePlayer::CacheBones_AnyThread(Context);
BasePose.CacheBones(Context);
SocketBoneReference.Initialize(Context.AnimInstanceProxy->GetRequiredBones());
PivotSocketBoneReference.Initialize(Context.AnimInstanceProxy->GetRequiredBones());
}
void FAnimNode_AimOffsetLookAt::Evaluate_AnyThread(FPoseContext& Context)
{
// Evaluate base pose
BasePose.Evaluate(Context);
if (bIsLODEnabled && FAnimWeight::IsRelevant(Alpha) && (CVarAimOffsetLookAtEnable.GetValueOnAnyThread() == 1))
{
UpdateFromLookAtTarget(Context);
// Evaluate MeshSpaceRotation additive blendspace
FPoseContext MeshSpaceRotationAdditivePoseContext(Context);
FAnimNode_BlendSpacePlayer::Evaluate_AnyThread(MeshSpaceRotationAdditivePoseContext);
// Accumulate poses together
FAnimationPoseData BaseAnimationPoseData(Context);
const FAnimationPoseData AdditiveAnimationPoseData(MeshSpaceRotationAdditivePoseContext);
FAnimationRuntime::AccumulateMeshSpaceRotationAdditiveToLocalPose(BaseAnimationPoseData, AdditiveAnimationPoseData, Alpha);
// Resulting rotations are not normalized, so normalize here.
Context.Pose.NormalizeRotations();
}
}
void FAnimNode_AimOffsetLookAt::UpdateFromLookAtTarget(FPoseContext& LocalPoseContext)
{
UBlendSpace* CurrentBlendSpace = GetBlendSpace();
const FBoneContainer& RequiredBones = LocalPoseContext.Pose.GetBoneContainer();
if (CurrentBlendSpace && SocketBoneReference.IsValidToEvaluate(RequiredBones))
{
FCSPose<FCompactPose> GlobalPose;
GlobalPose.InitPose(LocalPoseContext.Pose);
const FCompactPoseBoneIndex SocketBoneIndex = SocketBoneReference.GetCompactPoseIndex(RequiredBones);
const FTransform BoneTransform = GlobalPose.GetComponentSpaceTransform(SocketBoneIndex);
FTransform SourceComponentTransform = SocketLocalTransform * BoneTransform;
if (PivotSocketBoneReference.IsValidToEvaluate(RequiredBones))
{
const FCompactPoseBoneIndex PivotSocketBoneIndex = PivotSocketBoneReference.GetCompactPoseIndex(RequiredBones);
const FTransform PivotBoneComponentTransform = GlobalPose.GetComponentSpaceTransform(PivotSocketBoneIndex);
SourceComponentTransform.SetTranslation(PivotBoneComponentTransform.GetTranslation());
}
FAnimInstanceProxy* AnimProxy = LocalPoseContext.AnimInstanceProxy;
check(AnimProxy);
const FTransform SourceWorldTransform = SourceComponentTransform * AnimProxy->GetSkelMeshCompLocalToWorld();
const FTransform ActorTransform = AnimProxy->GetSkelMeshCompOwnerTransform();
// Convert Target to Actor Space
const FTransform TargetWorldTransform(LookAtLocation);
const FVector DirectionToTarget = ActorTransform.InverseTransformVectorNoScale(TargetWorldTransform.GetLocation() - SourceWorldTransform.GetLocation()).GetSafeNormal();
const FVector CurrentDirection = ActorTransform.InverseTransformVectorNoScale(SourceWorldTransform.TransformVector(SocketAxis).GetSafeNormal());
const FVector AxisX = FVector::ForwardVector;
const FVector AxisY = FVector::RightVector;
const FVector AxisZ = FVector::UpVector;
const FVector2D CurrentCoords = FMath::GetAzimuthAndElevation(CurrentDirection, AxisX, AxisY, AxisZ);
const FVector2D TargetCoords = FMath::GetAzimuthAndElevation(DirectionToTarget, AxisX, AxisY, AxisZ);
CurrentBlendInput.X = FRotator::NormalizeAxis(FMath::RadiansToDegrees(TargetCoords.X - CurrentCoords.X));
CurrentBlendInput.Y = FRotator::NormalizeAxis(FMath::RadiansToDegrees(TargetCoords.Y - CurrentCoords.Y));
#if ENABLE_DRAW_DEBUG
if (CVarAimOffsetLookAtDebug.GetValueOnAnyThread() == 1)
{
AnimProxy->AnimDrawDebugLine(SourceWorldTransform.GetLocation(), TargetWorldTransform.GetLocation(), FColor::Green);
AnimProxy->AnimDrawDebugLine(SourceWorldTransform.GetLocation(), SourceWorldTransform.GetLocation() + SourceWorldTransform.TransformVector(SocketAxis).GetSafeNormal() * (TargetWorldTransform.GetLocation() - SourceWorldTransform.GetLocation()).Size(), FColor::Red);
AnimProxy->AnimDrawDebugCoordinateSystem(ActorTransform.GetLocation(), ActorTransform.GetRotation().Rotator(), 100.f);
FString DebugString = FString::Printf(TEXT("Socket (X:%f, Y:%f), Target (X:%f, Y:%f), Result (X:%f, Y:%f)")
, FMath::RadiansToDegrees(CurrentCoords.X)
, FMath::RadiansToDegrees(CurrentCoords.Y)
, FMath::RadiansToDegrees(TargetCoords.X)
, FMath::RadiansToDegrees(TargetCoords.Y)
, CurrentBlendInput.X
, CurrentBlendInput.Y);
AnimProxy->AnimDrawDebugOnScreenMessage(DebugString, FColor::Red);
}
#endif // ENABLE_DRAW_DEBUG
}
// Generate BlendSampleDataCache from inputs.
if (CurrentBlendSpace)
{
CurrentBlendSpace->GetSamplesFromBlendInput(CurrentBlendInput, BlendSampleDataCache, CachedTriangulationIndex, true);
}
}
void FAnimNode_AimOffsetLookAt::GatherDebugData(FNodeDebugData& DebugData)
{
DECLARE_SCOPE_HIERARCHICAL_COUNTER_ANIMNODE(GatherDebugData)
FString DebugLine = DebugData.GetNodeName(this);
DebugLine += FString::Printf(TEXT("(Play Time: %.3f)"), InternalTimeAccumulator);
DebugData.AddDebugItem(DebugLine);
BasePose.GatherDebugData(DebugData);
}
FVector FAnimNode_AimOffsetLookAt::GetPosition() const
{
// Use our calculated coordinates rather than the folded values
return CurrentBlendInput;
}
FAnimNode_AimOffsetLookAt::FAnimNode_AimOffsetLookAt()
: SocketLocalTransform(FTransform::Identity)
, PivotSocketLocalTransform(FTransform::Identity)
, LODThreshold(INDEX_NONE)
, SourceSocketName(NAME_None)
, PivotSocketName(NAME_None)
, LookAtLocation(ForceInitToZero)
, SocketAxis(1.0f, 0.0f, 0.0f)
, Alpha(1.f)
, CurrentBlendInput(FVector::ZeroVector)
, bIsLODEnabled(false)
{
}