158 lines
5.1 KiB
C++
158 lines
5.1 KiB
C++
/**
|
|
* @file
|
|
*
|
|
* @author Mamadou Babaei <mamadou@senseglove.com>
|
|
*
|
|
* @section LICENSE
|
|
*
|
|
* (The MIT License)
|
|
*
|
|
* Copyright (c) 2020 - 2023 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 "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 "Runtime/Launch/Resources/Version.h"
|
|
#include "SGLog/SGLog.h"
|
|
|
|
#include "SenseGlove/Components/SGVirtualHandComponent.h"
|
|
|
|
FSGVirtualHandAnimInstanceProxy::FSGVirtualHandAnimInstanceProxy()
|
|
: FAnimInstanceProxy()
|
|
{
|
|
}
|
|
|
|
FSGVirtualHandAnimInstanceProxy::FSGVirtualHandAnimInstanceProxy(UAnimInstance* Instance)
|
|
: FAnimInstanceProxy(Instance)
|
|
{
|
|
}
|
|
|
|
void FSGVirtualHandAnimInstanceProxy::PreEvaluateAnimation(UAnimInstance* InAnimInstance)
|
|
{
|
|
FAnimInstanceProxy::PreEvaluateAnimation(InAnimInstance);
|
|
|
|
if (!IsValid(InAnimInstance))
|
|
{
|
|
return;
|
|
}
|
|
|
|
USGVirtualHandComponent* VirtualHand{Cast<USGVirtualHandComponent>(GetSkelMeshComponent())};
|
|
if (!IsValid(VirtualHand))
|
|
{
|
|
return;
|
|
}
|
|
|
|
BonesRotations.Empty();
|
|
|
|
if (!VirtualHand->IsGloveConnected())
|
|
{
|
|
return;
|
|
}
|
|
|
|
BonesRotations = VirtualHand->GetFingersBonesRotations();
|
|
}
|
|
|
|
bool FSGVirtualHandAnimInstanceProxy::Evaluate(FPoseContext& Output)
|
|
{
|
|
USGVirtualHandComponent* VirtualHand{Cast<USGVirtualHandComponent>(GetSkelMeshComponent())};
|
|
if (!IsValid(VirtualHand) && !VirtualHand->IsGloveConnected())
|
|
{
|
|
return FAnimInstanceProxy::Evaluate(Output);
|
|
}
|
|
|
|
const USkeletalMesh* HandMesh{VirtualHand->
|
|
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1
|
|
GetSkeletalMeshAsset()
|
|
#else /* ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 1 */
|
|
SkeletalMesh
|
|
#endif /* ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 1 */
|
|
};
|
|
if (!IsValid(HandMesh))
|
|
{
|
|
return FAnimInstanceProxy::Evaluate(Output);
|
|
}
|
|
|
|
const USkeleton* HandSkeleton{HandMesh->GetSkeleton()};
|
|
if (!IsValid(HandSkeleton))
|
|
{
|
|
return FAnimInstanceProxy::Evaluate(Output);
|
|
}
|
|
|
|
FComponentSpacePoseContext PoseContext{Output.AnimInstanceProxy};
|
|
PoseContext.Pose.InitPose(Output.Pose);
|
|
|
|
for (const TTuple<FName, FRotator>& Bone: BonesRotations)
|
|
{
|
|
FName BoneName{Bone.Key};
|
|
FRotator BoneRotation{Bone.Value};
|
|
|
|
const int32 BoneIndex = GetRequiredBones().GetPoseBoneIndexForBoneName(BoneName);
|
|
if (BoneIndex == INDEX_NONE)
|
|
{
|
|
SGLOG_WARNING(FString::Printf(TEXT("Could not find a bone index for '%s' finger bone!"),
|
|
*BoneName.ToString()));
|
|
continue;
|
|
}
|
|
|
|
FBoneReference BoneReference;
|
|
BoneReference.BoneName = MoveTemp(BoneName);
|
|
BoneReference.BoneIndex = BoneIndex;
|
|
BoneReference.bUseSkeletonIndex = true;
|
|
|
|
FAnimNode_ModifyBone BoneController;
|
|
BoneController.BoneToModify = MoveTemp(BoneReference);
|
|
BoneController.Rotation = MoveTemp(BoneRotation);
|
|
BoneController.RotationMode = EBoneModificationMode::BMM_Replace;
|
|
BoneController.RotationSpace = EBoneControlSpace::BCS_ParentBoneSpace;
|
|
BoneController.ScaleMode = EBoneModificationMode::BMM_Ignore;
|
|
BoneController.TranslationMode = EBoneModificationMode::BMM_Ignore;
|
|
|
|
if (BoneController.IsValidToEvaluate(HandSkeleton, Output.Pose.GetBoneContainer()))
|
|
{
|
|
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);
|
|
|
|
VirtualHand->MarkRenderStateDirty();
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
} |