Files
UnrealEngineUWP/Engine/Source/Runtime/AnimGraphRuntime/Private/BoneControllers/AnimNode_BoneDrivenController.cpp
Marc Audy 4c41590c3d Copying //UE4/Dev-Framework to Dev-Main (//UE4/Dev-Main) @ 2879625
#lockdown Nick.Penwarden

==========================
MAJOR FEATURES + CHANGES
==========================
Change 2821607 on 2016/01/08 by Mieszko.Zielinski

	Added a way to limit amount of information logged by vlog by discarding logs from classes from outside of class whitelist #UE4

	This feature was followed by refactoring of functions taking FVisualLogEntry pointers to use references instead.

	#rb Lukasz.Furman

Change 2828384 on 2016/01/14 by Mieszko.Zielinski

	Back out of visual log refactor done as part of CL#2821607 #UE4

Change 2869215 on 2016/02/16 by Marc.Audy

	Store a WorldSettings pointer on ULevel instead of requiring it be index 0 in the Actors array. However, we will still generally attempt to keep it at index 0 for consistency with previous behavior
	#rb Bruce.Nesbit
	#jira UE-26417

Change 2869404 on 2016/02/16 by Ori.Cohen

	Improve UI for default collision. It now uses a single drop down and sets the appropriate flags under the hood.

	#rb Lina.Halper

Change 2870062 on 2016/02/17 by Jurre.deBaare

	Name parameter driven by bone controller
	#JIRA UE-25997
	#rb Thomas.Sarkanen

Change 2870280 on 2016/02/17 by Mieszko.Zielinski

	Vis log category handling fixes #UE4

	Also, a minor cleanup

	#rb Lukasz.Furman

Change 2871729 on 2016/02/18 by James.Golding

	UE-26663 Fix 'LOD For Collision' display name
	#rb thomas.sarkanen

Change 2871730 on 2016/02/18 by James.Golding

	UE-26580 Make ECollisionEnabled a BlueprintType
	UE-25373 Add a MakeHitResult node
	#rb thomas.sarkanen

Change 2871732 on 2016/02/18 by James.Golding

	UE-24397 Add 'test' option to async query API, and use it in places that made sense. Also removed deprecated (4.8) functions from API.
	#rb ori.cohen

Change 2872022 on 2016/02/18 by Lukasz.Furman

	gameplay debugger refactor
	#ue4

Change 2872082 on 2016/02/18 by Lukasz.Furman

	enabled old gameplay debugger as default one for now
	it will be deprecated with next version after testing in game projects
	#ue4

Change 2872390 on 2016/02/18 by Aaron.McLeran

	OR-15041 (CPU) Hitches due to audio decompression on Windows

	1) Moving ogg-vorbis file info parsing into a worker thread
	 - stat dumphitches now shows the vorbis stuff totally gone

	2) Moving async decoding tasks to be retrieved and started from OnBufferEnd callback

	#rb marc.audy

Change 2872418 on 2016/02/18 by Mieszko.Zielinski

	Fixed EQS debugger not storing data properly when subsequent Option is the one that produces result #UE4

	#rb Lukasz.Furman

Change 2872446 on 2016/02/18 by Aaron.McLeran

	Using cached value of ActualVolume in GetVolumeWeightedPriority

Change 2872770 on 2016/02/18 by Aaron.McLeran

	QAGame testing content for audio testing.

	Going to create a folder with specific sub-system testing maps for audio

Change 2873733 on 2016/02/19 by Jurre.deBaare

	- HLOD generated assets are now saved into a separate package instead of inside of the level asset
	#rb Ori.Cohen

Change 2873828 on 2016/02/19 by Ori.Cohen

	Distributions that bake out no longer load in cooked build.

	#JIRA UE-27126
	#rb Olaf.Piesche, Nick.Penwarden

Change 2874623 on 2016/02/19 by Aaron.McLeran

	UE-27131 Support for changing sound class volumes dynamically

	- new BP function to override a sound mix sound class adjuster
	- cleanup of AudioDevice.h and AudioDevice.cpp
	   - removing unnecessarily forward declares on various types
	   - removing unnecessary spaces and (void) params, etc

Change 2874922 on 2016/02/20 by Mieszko.Zielinski

	Fixed EQS tests being compiled out from Shipping and Test with WITH_DEV_AUTOMATION_TESTS macro #UE4

	#jira OR-15292
	#rb none

Change 2875838 on 2016/02/22 by Benn.Gallagher

[CL 2880055 by Marc Audy in Main branch]
2016-02-24 14:23:53 -05:00

283 lines
11 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "AnimGraphRuntimePrivatePCH.h"
#include "BoneControllers/AnimNode_BoneDrivenController.h"
#include "AnimInstanceProxy.h"
/////////////////////////////////////////////////////
// FAnimNode_BoneDrivenController
FAnimNode_BoneDrivenController::FAnimNode_BoneDrivenController()
: SourceComponent(EComponentType::None)
, Multiplier(1.0f)
, bUseRange(false)
, RangeMin(-1.0f)
, RangeMax(1.0f)
, RemappedMin(0.0f)
, RemappedMax(1.0f)
, DestinationMode(EDrivenDestinationMode::Bone)
, TargetComponent_DEPRECATED(EComponentType::None)
, bAffectTargetTranslationX(false)
, bAffectTargetTranslationY(false)
, bAffectTargetTranslationZ(false)
, bAffectTargetRotationX(false)
, bAffectTargetRotationY(false)
, bAffectTargetRotationZ(false)
, bAffectTargetScaleX(false)
, bAffectTargetScaleY(false)
, bAffectTargetScaleZ(false)
, ModificationMode(EDrivenBoneModificationMode::AddToInput)
{
}
void FAnimNode_BoneDrivenController::GatherDebugData(FNodeDebugData& DebugData)
{
FString DebugLine = DebugData.GetNodeName(this);
DebugLine += "(";
AddDebugNodeData(DebugLine);
if (DestinationMode == EDrivenDestinationMode::Bone)
{
DebugLine += FString::Printf(TEXT(" DrivingBone: %s\nDrivenBone: %s"), *SourceBone.BoneName.ToString(), *TargetBone.BoneName.ToString());
}
else
{
DebugLine += FString::Printf(TEXT(" DrivingBone: %s\nDrivenParameter: %s"), *SourceBone.BoneName.ToString(), *ParameterName.ToString());
}
DebugData.AddDebugItem(DebugLine);
ComponentPose.GatherDebugData(DebugData);
}
void FAnimNode_BoneDrivenController::EvaluateBoneTransforms(USkeletalMeshComponent* SkelComp, FCSPose<FCompactPose>& MeshBases, TArray<FBoneTransform>& OutCSBoneTransforms)
{
check(OutCSBoneTransforms.Num() == 0);
// Early out if we're not driving from or to anything
if (SourceComponent == EComponentType::None || DestinationMode != EDrivenDestinationMode::Bone)
{
return;
}
// Get the Local space transform and the ref pose transform to see how the transform for the source bone has changed
const FBoneContainer& BoneContainer = MeshBases.GetPose().GetBoneContainer();
const FTransform& SourceRefPoseBoneTransform = BoneContainer.GetRefPoseArray()[SourceBone.BoneIndex];
const FTransform SourceCurrentBoneTransform = MeshBases.GetLocalSpaceTransform(SourceBone.GetCompactPoseIndex(BoneContainer));
const float FinalDriverValue = ExtractSourceValue(SourceCurrentBoneTransform, SourceRefPoseBoneTransform);
// Calculate a new local-space bone position by adding or replacing target components in the current local space position
const FCompactPoseBoneIndex TargetBoneIndex = TargetBone.GetCompactPoseIndex(BoneContainer);
const FTransform OriginalLocalTM = MeshBases.GetLocalSpaceTransform(TargetBoneIndex);
FVector NewTrans(OriginalLocalTM.GetTranslation());
FVector NewScale(OriginalLocalTM.GetScale3D());
FQuat NewRot(OriginalLocalTM.GetRotation());
if (ModificationMode == EDrivenBoneModificationMode::AddToInput)
{
// Add the mapped value to the target components
if (bAffectTargetTranslationX) { NewTrans.X += FinalDriverValue; }
if (bAffectTargetTranslationY) { NewTrans.Y += FinalDriverValue; }
if (bAffectTargetTranslationZ) { NewTrans.Z += FinalDriverValue; }
if (bAffectTargetRotationX || bAffectTargetRotationY || bAffectTargetRotationZ)
{
FVector NewRotDeltaEuler(ForceInitToZero);
if (bAffectTargetRotationX) { NewRotDeltaEuler.X = FinalDriverValue; }
if (bAffectTargetRotationY) { NewRotDeltaEuler.Y = FinalDriverValue; }
if (bAffectTargetRotationZ) { NewRotDeltaEuler.Z = FinalDriverValue; }
NewRot = NewRot * FQuat::MakeFromEuler(NewRotDeltaEuler);
}
if (bAffectTargetScaleX) { NewScale.X += FinalDriverValue; }
if (bAffectTargetScaleY) { NewScale.Y += FinalDriverValue; }
if (bAffectTargetScaleZ) { NewScale.Z += FinalDriverValue; }
}
else if (ModificationMode == EDrivenBoneModificationMode::ReplaceComponent)
{
// Replace the target components with the mapped value
if (bAffectTargetTranslationX) { NewTrans.X = FinalDriverValue; }
if (bAffectTargetTranslationY) { NewTrans.Y = FinalDriverValue; }
if (bAffectTargetTranslationZ) { NewTrans.Z = FinalDriverValue; }
if (bAffectTargetRotationX || bAffectTargetRotationY || bAffectTargetRotationZ)
{
FVector NewRotEuler(NewRot.Euler());
if (bAffectTargetRotationX) { NewRotEuler.X = FinalDriverValue; }
if (bAffectTargetRotationY) { NewRotEuler.Y = FinalDriverValue; }
if (bAffectTargetRotationZ) { NewRotEuler.Z = FinalDriverValue; }
NewRot = FQuat::MakeFromEuler(NewRotEuler);
}
if (bAffectTargetScaleX) { NewScale.X = FinalDriverValue; }
if (bAffectTargetScaleY) { NewScale.Y = FinalDriverValue; }
if (bAffectTargetScaleZ) { NewScale.Z = FinalDriverValue; }
}
else if (ModificationMode == EDrivenBoneModificationMode::AddToRefPose)
{
const FTransform RefPoseTransform = MeshBases.GetPose().GetRefPose(TargetBoneIndex);
// Add the mapped value to the ref pose components
if (bAffectTargetTranslationX) { NewTrans.X = RefPoseTransform.GetTranslation().X + FinalDriverValue; }
if (bAffectTargetTranslationY) { NewTrans.Y = RefPoseTransform.GetTranslation().Y + FinalDriverValue; }
if (bAffectTargetTranslationZ) { NewTrans.Z = RefPoseTransform.GetTranslation().Z + FinalDriverValue; }
if (bAffectTargetRotationX || bAffectTargetRotationY || bAffectTargetRotationZ)
{
const FVector RefPoseRotEuler(RefPoseTransform.GetRotation().Euler());
// Replace any components that are being driven with their ref pose value first and create a delta rotation as well
FVector SourceRotationEuler(NewRot.Euler());
FVector NewRotDeltaEuler(ForceInitToZero);
if (bAffectTargetRotationX) { SourceRotationEuler.X = RefPoseRotEuler.X; NewRotDeltaEuler.X = FinalDriverValue; }
if (bAffectTargetRotationY) { SourceRotationEuler.Y = RefPoseRotEuler.Y; NewRotDeltaEuler.Y = FinalDriverValue; }
if (bAffectTargetRotationZ) { SourceRotationEuler.Z = RefPoseRotEuler.Z; NewRotDeltaEuler.Z = FinalDriverValue; }
// Combine the (modified) source and the delta rotation
NewRot = FQuat::MakeFromEuler(SourceRotationEuler) * FQuat::MakeFromEuler(NewRotDeltaEuler);
}
if (bAffectTargetScaleX) { NewScale.X = RefPoseTransform.GetScale3D().X + FinalDriverValue; }
if (bAffectTargetScaleY) { NewScale.Y = RefPoseTransform.GetScale3D().Y + FinalDriverValue; }
if (bAffectTargetScaleZ) { NewScale.Z = RefPoseTransform.GetScale3D().Z + FinalDriverValue; }
}
else
{
ensureMsgf(false, TEXT("Unknown entry in EDrivenBoneModificationMode"));
}
const FTransform ModifiedLocalTM(NewRot, NewTrans, NewScale);
// If we have a parent, concatenate the transform, otherwise just take the new transform
const FCompactPoseBoneIndex ParentIndex = MeshBases.GetPose().GetParentBoneIndex(TargetBoneIndex);
if (ParentIndex != INDEX_NONE)
{
const FTransform& ParentTM = MeshBases.GetComponentSpaceTransform(ParentIndex);
OutCSBoneTransforms.Add(FBoneTransform(TargetBoneIndex, ModifiedLocalTM * ParentTM));
}
else
{
OutCSBoneTransforms.Add(FBoneTransform(TargetBoneIndex, ModifiedLocalTM));
}
}
void FAnimNode_BoneDrivenController::EvaluateComponentSpaceInternal(FComponentSpacePoseContext& Context)
{
// Early out if we're not driving from or to anything
if (SourceComponent == EComponentType::None || DestinationMode == EDrivenDestinationMode::Bone)
{
return;
}
// Get the Local space transform and the ref pose transform to see how the transform for the source bone has changed
const FBoneContainer& BoneContainer = Context.Pose.GetPose().GetBoneContainer();
const FTransform& SourceRefPoseBoneTransform = BoneContainer.GetRefPoseArray()[SourceBone.BoneIndex];
const FTransform SourceCurrentBoneTransform = Context.Pose.GetLocalSpaceTransform(SourceBone.GetCompactPoseIndex(BoneContainer));
const float FinalDriverValue = ExtractSourceValue(SourceCurrentBoneTransform, SourceRefPoseBoneTransform);
if (DestinationMode == EDrivenDestinationMode::MorphTarget || DestinationMode == EDrivenDestinationMode::MaterialParameter)
{
// Morph target and Material parameter curves
USkeleton* Skeleton = Context.AnimInstanceProxy->GetSkeleton();
FSmartNameMapping::UID NameUID;
Skeleton->AddSmartNameAndModify(USkeleton::AnimCurveMappingName, ParameterName, NameUID);
Context.Curve.Set(NameUID, FinalDriverValue, (DestinationMode == EDrivenDestinationMode::MorphTarget) ? EAnimCurveFlags::ACF_DrivesMorphTarget : EAnimCurveFlags::ACF_DrivesMaterial);
}
}
const float FAnimNode_BoneDrivenController::ExtractSourceValue(const FTransform &InCurrentBoneTransform, const FTransform &InRefPoseBoneTransform)
{
// Resolve source value
float SourceValue = 0.0f;
if (SourceComponent < EComponentType::RotationX)
{
const FVector TranslationDiff = InCurrentBoneTransform.GetLocation() - InRefPoseBoneTransform.GetLocation();
SourceValue = TranslationDiff[(int32)(SourceComponent - EComponentType::TranslationX)];
}
else if (SourceComponent < EComponentType::Scale)
{
const FVector RotationDiff = (InCurrentBoneTransform.GetRotation() * InRefPoseBoneTransform.GetRotation().Inverse()).Euler();
SourceValue = RotationDiff[(int32)(SourceComponent - EComponentType::RotationX)];
}
else if (SourceComponent == EComponentType::Scale)
{
const FVector CurrentScale = InCurrentBoneTransform.GetScale3D();
const FVector RefScale = InRefPoseBoneTransform.GetScale3D();
const float ScaleDiff = FMath::Max3(CurrentScale[0], CurrentScale[1], CurrentScale[2]) - FMath::Max3(RefScale[0], RefScale[1], RefScale[2]);
SourceValue = ScaleDiff;
}
else
{
const FVector ScaleDiff = InCurrentBoneTransform.GetScale3D() - InRefPoseBoneTransform.GetScale3D();
SourceValue = ScaleDiff[(int32)(SourceComponent - EComponentType::ScaleX)];
}
// Determine the resulting value
float FinalDriverValue = SourceValue;
if (DrivingCurve != nullptr)
{
// Remap thru the curve if set
FinalDriverValue = DrivingCurve->GetFloatValue(FinalDriverValue);
}
else
{
// Apply the fixed function remapping/clamping
if (bUseRange)
{
const float Alpha = FMath::Clamp(FMath::GetRangePct(RangeMin, RangeMax, FinalDriverValue), 0.0f, 1.0f);
FinalDriverValue = FMath::Lerp(RemappedMin, RemappedMax, Alpha);
}
FinalDriverValue *= Multiplier;
}
return FinalDriverValue;
}
bool FAnimNode_BoneDrivenController::IsValidToEvaluate(const USkeleton* Skeleton, const FBoneContainer& RequiredBones)
{
return SourceBone.IsValid(RequiredBones) && ( TargetBone.IsValid(RequiredBones) || DestinationMode != EDrivenDestinationMode::Bone );
}
void FAnimNode_BoneDrivenController::ConvertTargetComponentToBits()
{
switch (TargetComponent_DEPRECATED)
{
case EComponentType::TranslationX:
bAffectTargetTranslationX = true;
break;
case EComponentType::TranslationY:
bAffectTargetTranslationY = true;
break;
case EComponentType::TranslationZ:
bAffectTargetTranslationZ = true;
break;
case EComponentType::RotationX:
bAffectTargetRotationX = true;
break;
case EComponentType::RotationY:
bAffectTargetRotationY = true;
break;
case EComponentType::RotationZ:
bAffectTargetRotationZ = true;
break;
case EComponentType::Scale:
bAffectTargetScaleX = true;
bAffectTargetScaleY = true;
bAffectTargetScaleZ = true;
break;
}
}
void FAnimNode_BoneDrivenController::InitializeBoneReferences(const FBoneContainer& RequiredBones)
{
SourceBone.Initialize(RequiredBones);
TargetBone.Initialize(RequiredBones);
}