260 lines
8.7 KiB
C++
260 lines
8.7 KiB
C++
/**
|
|
* @file
|
|
*
|
|
* @author Mamadou Babaei <mamadou@senseglove.com>
|
|
*
|
|
* @section LICENSE
|
|
*
|
|
* (The MIT License)
|
|
*
|
|
* Copyright (c) 2020 - 2025 SenseGlove
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
* @section DESCRIPTION
|
|
*
|
|
*
|
|
*/
|
|
|
|
|
|
#include "SenseGlove/Animation/SGVirtualHandAnimInstanceProxy.h"
|
|
|
|
#include "Animation/AnimInstance.h"
|
|
#include "Animation/Skeleton.h"
|
|
#include "BoneContainer.h"
|
|
#include "BoneControllers/AnimNode_ModifyBone.h"
|
|
#include "BonePose.h"
|
|
#include "Containers/Array.h"
|
|
#include "Containers/Map.h"
|
|
#include "Containers/UnrealString.h"
|
|
#include "Engine/SkeletalMesh.h"
|
|
#include "Math/Rotator.h"
|
|
|
|
#include "SenseGlove/Components/SGVirtualHandComponent.h"
|
|
|
|
FSGVirtualHandAnimInstanceProxy::FSGVirtualHandAnimInstanceProxy()
|
|
: FAnimInstanceProxy{}
|
|
{
|
|
VirtualHand = nullptr;
|
|
HandMeshAsset = nullptr;
|
|
HandSkeleton = nullptr;
|
|
|
|
bVirtualHandVisible = false;
|
|
bMarkRenderStateDirty = false;
|
|
|
|
bShouldAnimationApplyBoneLocation = false;
|
|
}
|
|
|
|
FSGVirtualHandAnimInstanceProxy::FSGVirtualHandAnimInstanceProxy(UAnimInstance* Instance)
|
|
: FAnimInstanceProxy{Instance}
|
|
{
|
|
VirtualHand = nullptr;
|
|
HandMeshAsset = nullptr;
|
|
HandSkeleton = nullptr;
|
|
|
|
bVirtualHandVisible = false;
|
|
bMarkRenderStateDirty = false;
|
|
|
|
bShouldAnimationApplyBoneLocation = false;
|
|
}
|
|
|
|
void FSGVirtualHandAnimInstanceProxy::PreEvaluateAnimation(UAnimInstance* InAnimInstance)
|
|
{
|
|
FAnimInstanceProxy::PreEvaluateAnimation(InAnimInstance);
|
|
|
|
if (!IsValid(InAnimInstance))
|
|
{
|
|
return;
|
|
}
|
|
|
|
VirtualHand = Cast<USGVirtualHandComponent>(GetSkelMeshComponent());
|
|
if (!IsValid(VirtualHand))
|
|
{
|
|
return;
|
|
}
|
|
|
|
HandMeshAsset = VirtualHand->GetSkeletalMeshAsset();
|
|
if (!IsValid(HandMeshAsset))
|
|
{
|
|
return;
|
|
}
|
|
|
|
HandSkeleton = HandMeshAsset->GetSkeleton();
|
|
if (!IsValid(HandSkeleton))
|
|
{
|
|
return;
|
|
}
|
|
|
|
bVirtualHandVisible = VirtualHand->IsVisible() && VirtualHand->GetVisibleFlag();
|
|
if (!bVirtualHandVisible)
|
|
{
|
|
return;
|
|
}
|
|
|
|
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
|
|
HandTrackingState = VirtualHand->GetHandTrackingState();
|
|
if (!HandTrackingState.bValid)
|
|
{
|
|
return;
|
|
}
|
|
ensureAlwaysMsgf(HandTrackingState.HandKeyLocations.Num() == HandTrackingState.HandKeyRotations.Num(),
|
|
TEXT("Mismatched hand tracking state locations and rotations!"));
|
|
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
|
|
MotionControllerData = VirtualHand->GetMotionControllerData();
|
|
if (!MotionControllerData.bValid)
|
|
{
|
|
return;
|
|
}
|
|
ensureAlwaysMsgf(MotionControllerData.HandKeyPositions.Num() == MotionControllerData.HandKeyRotations.Num(),
|
|
TEXT("Mismatched motion controller data positions and rotations!"));
|
|
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
|
|
|
|
|
|
BoneNamesMap.Empty();
|
|
for (TArray<FQuat>::SizeType HandKeyIndex = 0;
|
|
HandKeyIndex <
|
|
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
|
|
HandTrackingState.HandKeyRotations.Num()
|
|
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
|
|
MotionControllerData.HandKeyRotations.Num()
|
|
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
|
|
; ++HandKeyIndex)
|
|
{
|
|
const FName& BoneName{VirtualHand->GetHandBoneName(HandKeyIndex)};
|
|
BoneNamesMap.Add(HandKeyIndex, BoneName);
|
|
}
|
|
|
|
const FSGVirtualHandAnimationSettings AnimationSettings{VirtualHand->GetVirtualHandSettings().AnimationSettings};
|
|
AnimationBoneCorrectionOffsetTransform = FTransform{AnimationSettings.AnimationBoneRotationCorrectionOffset};
|
|
bShouldAnimationApplyBoneLocation = AnimationSettings.bShouldAnimationApplyBoneLocation;
|
|
}
|
|
|
|
bool FSGVirtualHandAnimInstanceProxy::Evaluate(FPoseContext& Output)
|
|
{
|
|
if (!bVirtualHandVisible)
|
|
{
|
|
return FAnimInstanceProxy::Evaluate(Output);
|
|
}
|
|
|
|
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
|
|
if (!HandTrackingState.bValid)
|
|
{
|
|
return FAnimInstanceProxy::Evaluate(Output);
|
|
}
|
|
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
|
|
if (!MotionControllerData.bValid)
|
|
{
|
|
return FAnimInstanceProxy::Evaluate(Output);
|
|
}
|
|
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
|
|
|
|
FComponentSpacePoseContext PoseContext{Output.AnimInstanceProxy};
|
|
PoseContext.Pose.InitPose(Output.Pose);
|
|
|
|
for (const TTuple<TArray<FQuat>::SizeType, FName>& Bone: BoneNamesMap)
|
|
{
|
|
const TArray<FQuat>::SizeType HandKeyIndex = Bone.Key;
|
|
const FName& BoneName{Bone.Value};
|
|
if (BoneName == NAME_None)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
const int32 BoneIndex = GetRequiredBones().GetPoseBoneIndexForBoneName(BoneName);
|
|
if (BoneIndex == INDEX_NONE)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
FBoneReference BoneReference;
|
|
BoneReference.BoneName = BoneName;
|
|
BoneReference.BoneIndex = BoneIndex;
|
|
BoneReference.bUseSkeletonIndex = true;
|
|
|
|
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
|
|
const FVector& HandKeyLocation{HandTrackingState.HandKeyLocations[HandKeyIndex]};
|
|
const FQuat& HandKeyRotation{HandTrackingState.HandKeyRotations[HandKeyIndex]};
|
|
const FTransform HandKeyTransform{HandKeyRotation, HandKeyLocation};
|
|
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
|
|
const FVector& HandKeyPosition{MotionControllerData.HandKeyPositions[HandKeyIndex]};
|
|
const FQuat& HandKeyRotation{MotionControllerData.HandKeyRotations[HandKeyIndex]};
|
|
const FTransform HandKeyTransform{HandKeyRotation, HandKeyPosition};
|
|
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
|
|
|
|
const FTransform BoneTransform{AnimationBoneCorrectionOffsetTransform * HandKeyTransform};
|
|
FRotator BoneRotation{BoneTransform.GetRotation().Rotator()};
|
|
|
|
FAnimNode_ModifyBone BoneController;
|
|
BoneController.BoneToModify = MoveTemp(BoneReference);
|
|
BoneController.Rotation = MoveTemp(BoneRotation);
|
|
BoneController.RotationMode = EBoneModificationMode::BMM_Replace;
|
|
BoneController.RotationSpace = EBoneControlSpace::BCS_WorldSpace;
|
|
BoneController.ScaleMode = EBoneModificationMode::BMM_Ignore;
|
|
|
|
if (bShouldAnimationApplyBoneLocation)
|
|
{
|
|
FVector BoneTranslation{BoneTransform.GetLocation()};
|
|
BoneController.TranslationMode = EBoneModificationMode::BMM_Replace;
|
|
BoneController.TranslationSpace = EBoneControlSpace::BCS_WorldSpace;
|
|
BoneController.Translation = MoveTemp(BoneTranslation);
|
|
}
|
|
else
|
|
{
|
|
BoneController.TranslationMode = EBoneModificationMode::BMM_Ignore;
|
|
}
|
|
|
|
const bool bValidToEvaluate =
|
|
BoneController.IsValidToEvaluate(HandSkeleton, Output.Pose.GetBoneContainer());
|
|
if (!bValidToEvaluate)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
TArray<FBoneTransform> BoneTransforms;
|
|
BoneController.EvaluateSkeletalControl_AnyThread(PoseContext, BoneTransforms);
|
|
|
|
const TArray<FBoneTransform>::SizeType BoneTransformsNum = BoneTransforms.Num();
|
|
if (BoneTransformsNum > 0)
|
|
{
|
|
PoseContext.Pose.LocalBlendCSBoneTransforms(BoneTransforms, 1.0f);
|
|
PoseContext.Pose.ConvertComponentPosesToLocalPoses(PoseContext.Pose, Output.Pose);
|
|
|
|
bMarkRenderStateDirty = true;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void FSGVirtualHandAnimInstanceProxy::PostEvaluate(UAnimInstance* InAnimInstance)
|
|
{
|
|
FAnimInstanceProxy::PostEvaluate(InAnimInstance);
|
|
|
|
if (!bMarkRenderStateDirty)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!IsValid(VirtualHand))
|
|
{
|
|
return;
|
|
}
|
|
|
|
VirtualHand->MarkRenderStateDirty();
|
|
} |