Files
UnrealEngineUWP/Engine/Source/Runtime/AnimGraphRuntime/Private/BoneControllers/AnimNode_SpringBone.cpp
Marc Audy ba3ad4c356 Copying //UE4/Dev-Framework to Dev-Main (//UE4/Dev-Main) @ 2855699
#lockdown Nick.Penwarden

==========================
MAJOR FEATURES + CHANGES
==========================

Change 2839897 on 2016/01/22 by Ori.Cohen

	Allow static mesh editor to specify a default collision profile.

	#rb Lina.Halper
	#UE-2836

Change 2840489 on 2016/01/22 by Ori.Cohen

	Fix collision customization so that it respects const editing property

	#rb Marc.Audy

Change 2840528 on 2016/01/22 by Ori.Cohen

	Fix compile error and actually get value from attribute

Change 2840672 on 2016/01/22 by Zak.Middleton

	#ue4 - Include data from USkinnedMeshComponent in USkeletalMeshComponent::GetResourceSize().

	#rb Michael.Noland

Change 2841314 on 2016/01/24 by Marc.Audy

	Fix depressingly frequent misspellings of 'suppress'

Change 2841323 on 2016/01/24 by Marc.Audy

	Reserve worst case memory for TSet Intersect, Union, and Difference to avoid memory allocations during iteration
	Ensure that TSet Intersect considers the least number of elements possible
	Early out from TSet Contains if Other is larger than this
	Clarify comment on TSet Difference
	#rb Steve.Robb

Change 2841380 on 2016/01/24 by Aaron.McLeran

	UE-25586 Audio assets not correctly reporting resource memory usage

	Tested on PC/PS4 and with Editor builds. Memory reporting is working for all cases now.

Change 2841385 on 2016/01/24 by Aaron.McLeran

	UE-21210 Adding subtitle priority to USoundWave

Change 2841386 on 2016/01/24 by Marc.Audy

	Return null for GameNetDriver if World is null instead of crashing

Change 2841409 on 2016/01/24 by Aaron.McLeran

	UE-25514 Removing load for default objects for every sound wave

Change 2841858 on 2016/01/25 by Ori.Cohen

	Make sure that PIE face index results are consistent with runtime

	#rb Benn.Gallagher

Change 2841977 on 2016/01/25 by Ori.Cohen

	Fix object type customization so that it's only enabled when custom is selected. (Accidently broke this in recent change)

Change 2841982 on 2016/01/25 by Marc.Audy

	Minor optimization by avoiding recreating FNames repeatedly in constructor

Change 2842169 on 2016/01/25 by Benn.Gallagher

	Fixes to animBP compiler and instance to store and double buffer internal machine state weights on the instance. So they can be queried cross-machine without issue.
	#rb Lina.Halper

Change 2842390 on 2016/01/25 by Ori.Cohen

	Fix in world editing of BodyInstance not working.

	No longer serializing Scale3D as this is allways initialized in InitBody.
	No longer overwriting MassInKg and renamed to to MassInKgOverride which better reflects what this variable does.

	#JIRA UE-25518
	#rb Lina.Halper

Change 2843579 on 2016/01/26 by Marc.Audy

	Only update replication when it actually changes
	Don't check calling SetIsReplicated if the class cannot replicate, instead output an error message
	Fix spelling in comment
	#rb Ori.Cohen

Change 2843627 on 2016/01/26 by Marc.Audy

	Add \\ as a default console key for Italian keyboard layouts
	#jira UE-25198
	#rb James.Golding

Change 2843628 on 2016/01/26 by Marc.Audy

	Don't reconstruct FName on each call to GetHitResultAtScreenPosition
	#rb James.Golding

Change 2843671 on 2016/01/26 by Martin.Wilson

	Fix incorrect bone transforms being pushed to the renderer during SetSkeletalMesh. This presented as motion blur artifacts in editor

	#rb Thomas.Sarkanen

Change 2843768 on 2016/01/26 by Marc.Audy

	Inline Get Component functions in TriggerBase

Change 2844003 on 2016/01/26 by Zak.Middleton

	#ue4 - Fix FMath::Fmod(X, Y) sometimes returning small negative values for positive X and Y due to float imprecision. Added tests to math tests at startup to check this, and also to better handle results close to Y. Wrap the ensure on Y=0 within a conditional so a breakpoint can be used during debugging (to distinguish between zero and very small input).

	#codereview Laurent.Delayen

Change 2844005 on 2016/01/26 by Zak.Middleton

	#ue4 - Convert uses of fmod() and fmodf() to use FMath::Fmod() instead.

	Also see CL 2844003

[CL 2855709 by Marc Audy in Main branch]
2016-02-04 10:55:30 -05:00

226 lines
7.1 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "AnimGraphRuntimePrivatePCH.h"
#include "BoneControllers/AnimNode_SpringBone.h"
#include "Animation/AnimInstanceProxy.h"
/////////////////////////////////////////////////////
// FAnimNode_SpringBone
FAnimNode_SpringBone::FAnimNode_SpringBone()
: bLimitDisplacement(false)
, MaxDisplacement(0.0f)
, SpringStiffness(50.0f)
, SpringDamping(4.0f)
, ErrorResetThresh(256.0f)
, bNoZSpring_DEPRECATED(false)
, bTranslateX(true)
, bTranslateY(true)
, bTranslateZ(true)
, bRotateX(false)
, bRotateY(false)
, bRotateZ(false)
, RemainingTime(0.f)
, bHadValidStrength(false)
, BoneLocation(FVector::ZeroVector)
, BoneVelocity(FVector::ZeroVector)
{
}
void FAnimNode_SpringBone::Initialize(const FAnimationInitializeContext& Context)
{
FAnimNode_SkeletalControlBase::Initialize(Context);
RemainingTime = 0.0f;
}
void FAnimNode_SpringBone::CacheBones(const FAnimationCacheBonesContext& Context)
{
FAnimNode_SkeletalControlBase::CacheBones(Context);
}
void FAnimNode_SpringBone::UpdateInternal(const FAnimationUpdateContext& Context)
{
FAnimNode_SkeletalControlBase::UpdateInternal(Context);
RemainingTime += Context.GetDeltaTime();
// Fixed step simulation at 120hz
FixedTimeStep = (1.f / 120.f) * TimeDilation;
}
void FAnimNode_SpringBone::GatherDebugData(FNodeDebugData& DebugData)
{
const float ActualAlpha = AlphaScaleBias.ApplyTo(Alpha);
//MDW_TODO Add more output info?
FString DebugLine = DebugData.GetNodeName(this);
DebugLine += FString::Printf(TEXT("(Alpha: %.1f%% RemainingTime: %.3f)"), ActualAlpha*100.f, RemainingTime);
DebugData.AddDebugItem(DebugLine);
ComponentPose.GatherDebugData(DebugData);
}
FORCEINLINE void CopyToVectorByFlags(FVector& DestVec, const FVector& SrcVec, bool bX, bool bY, bool bZ)
{
if (bX)
{
DestVec.X = SrcVec.X;
}
if (bY)
{
DestVec.Y = SrcVec.Y;
}
if (bZ)
{
DestVec.Z = SrcVec.Z;
}
}
void FAnimNode_SpringBone::EvaluateBoneTransforms(USkeletalMeshComponent* SkelComp, FCSPose<FCompactPose>& MeshBases, TArray<FBoneTransform>& OutBoneTransforms)
{
check(OutBoneTransforms.Num() == 0);
const bool bNoOffset = !bTranslateX && !bTranslateY && !bTranslateZ;
if (bNoOffset)
{
return;
}
// Location of our bone in world space
const FBoneContainer& BoneContainer = MeshBases.GetPose().GetBoneContainer();
const FCompactPoseBoneIndex SpringBoneIndex = SpringBone.GetCompactPoseIndex(BoneContainer);
const FTransform& SpaceBase = MeshBases.GetComponentSpaceTransform(SpringBoneIndex);
FTransform BoneTransformInWorldSpace = (SkelComp != NULL) ? SpaceBase * SkelComp->GetComponentToWorld() : SpaceBase;
FVector const TargetPos = BoneTransformInWorldSpace.GetLocation();
AActor* SkelOwner = (SkelComp != NULL) ? SkelComp->GetOwner() : NULL;
if ((SkelComp != NULL) && (SkelComp->GetAttachParent() != NULL) && (SkelOwner == NULL))
{
SkelOwner = SkelComp->GetAttachParent()->GetOwner();
}
// Init values first time
if (RemainingTime == 0.0f)
{
BoneLocation = TargetPos;
BoneVelocity = FVector::ZeroVector;
}
while (RemainingTime > FixedTimeStep)
{
// Update location of our base by how much our base moved this frame.
FVector const BaseTranslation = SkelOwner ? (SkelOwner->GetVelocity() * FixedTimeStep) : FVector::ZeroVector;
BoneLocation += BaseTranslation;
// Reinit values if outside reset threshold
if (((TargetPos - BoneLocation).SizeSquared() > (ErrorResetThresh*ErrorResetThresh)))
{
BoneLocation = TargetPos;
BoneVelocity = FVector::ZeroVector;
}
// Calculate error vector.
FVector const Error = (TargetPos - BoneLocation);
FVector const DampingForce = SpringDamping * BoneVelocity;
FVector const SpringForce = SpringStiffness * Error;
// Calculate force based on error and vel
FVector const Acceleration = SpringForce - DampingForce;
// Integrate velocity
// Make sure damping with variable frame rate actually dampens velocity. Otherwise Spring will go nuts.
float const CutOffDampingValue = 1.f/FixedTimeStep;
if (SpringDamping > CutOffDampingValue)
{
float const SafetyScale = CutOffDampingValue / SpringDamping;
BoneVelocity += SafetyScale * (Acceleration * FixedTimeStep);
}
else
{
BoneVelocity += (Acceleration * FixedTimeStep);
}
// Clamp velocity to something sane (|dX/dt| <= ErrorResetThresh)
float const BoneVelocityMagnitude = BoneVelocity.Size();
if (BoneVelocityMagnitude * FixedTimeStep > ErrorResetThresh)
{
BoneVelocity *= (ErrorResetThresh / (BoneVelocityMagnitude * FixedTimeStep));
}
// Integrate position
FVector const OldBoneLocation = BoneLocation;
FVector const DeltaMove = (BoneVelocity * FixedTimeStep);
BoneLocation += DeltaMove;
// Filter out spring translation based on our filter properties
CopyToVectorByFlags(BoneLocation, TargetPos, !bTranslateX, !bTranslateY, !bTranslateZ);
// If desired, limit error
if (bLimitDisplacement)
{
FVector CurrentDisp = BoneLocation - TargetPos;
// Too far away - project back onto sphere around target.
if (CurrentDisp.SizeSquared() > FMath::Square(MaxDisplacement))
{
FVector DispDir = CurrentDisp.GetSafeNormal();
BoneLocation = TargetPos + (MaxDisplacement * DispDir);
}
}
// Update velocity to reflect post processing done to bone location.
BoneVelocity = (BoneLocation - OldBoneLocation) / FixedTimeStep;
check( !BoneLocation.ContainsNaN() );
check( !BoneVelocity.ContainsNaN() );
RemainingTime -= FixedTimeStep;
}
// Now convert back into component space and output - rotation is unchanged.
FTransform OutBoneTM = SpaceBase;
OutBoneTM.SetLocation( SkelComp->GetComponentToWorld().InverseTransformPosition(BoneLocation) );
const bool bUseRotation = bRotateX || bRotateY || bRotateZ;
if (bUseRotation)
{
FCompactPoseBoneIndex ParentBoneIndex = MeshBases.GetPose().GetParentBoneIndex(SpringBoneIndex);
const FTransform& ParentSpaceBase = MeshBases.GetComponentSpaceTransform(ParentBoneIndex);
FVector ParentToTarget = (TargetPos - ParentSpaceBase.GetLocation()).GetSafeNormal();
FVector ParentToCurrent = (BoneLocation - ParentSpaceBase.GetLocation()).GetSafeNormal();
FQuat AdditionalRotation = FQuat::FindBetweenNormals(ParentToTarget, ParentToCurrent);
// Filter rotation based on our filter properties
FVector EularRot = AdditionalRotation.Euler();
CopyToVectorByFlags(EularRot, FVector::ZeroVector, !bRotateX, !bRotateY, !bRotateZ);
OutBoneTM.SetRotation(FQuat::MakeFromEuler(EularRot) * OutBoneTM.GetRotation());
}
// Output new transform for current bone.
OutBoneTransforms.Add(FBoneTransform(SpringBoneIndex, OutBoneTM));
}
bool FAnimNode_SpringBone::IsValidToEvaluate(const USkeleton* Skeleton, const FBoneContainer& RequiredBones)
{
return (SpringBone.IsValid(RequiredBones));
}
void FAnimNode_SpringBone::InitializeBoneReferences(const FBoneContainer& RequiredBones)
{
SpringBone.Initialize(RequiredBones);
}
void FAnimNode_SpringBone::PreUpdate(const UAnimInstance* InAnimInstance)
{
const USkeletalMeshComponent* SkelComp = InAnimInstance->GetSkelMeshComponent();
const UWorld* World = SkelComp->GetWorld();
check(World->GetWorldSettings());
TimeDilation = World->GetWorldSettings()->GetEffectiveTimeDilation();
}