Files
Plugin/Source/SenseGlove/Private/SenseGlove/Components/SGVirtualHandComponent.cpp
T

640 lines
19 KiB
C++

/**
* @file
*
* @author Mamadou Babaei <mamadou@senseglove.com>
*
* @section LICENSE
*
* (The MIT License)
*
* Copyright (c) 2020 - 2024 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/Components/SGVirtualHandComponent.h"
#include "HeadMountedDisplayTypes.h"
#include "Animation/Skeleton.h"
#include "Components/SkeletalMeshComponent.h"
#include "Engine/Engine.h"
#include "Engine/SkeletalMesh.h"
#include "InputCoreTypes.h"
#include "IXRTrackingSystem.h"
#include "Templates/UnrealTypeTraits.h"
#include "UObject/ConstructorHelpers.h"
#include "SenseGlove/Animation/SGVirtualHandAnimInstance.h"
#include "SenseGlove/Components/SGWristTrackerComponent.h"
#include "SGCore/SGHapticGlove.h"
#include "SGDebug/SGDebugVirtualHand.h"
#include "SGLog/SGLog.h"
#include "SGSettings/SGSettings.h"
#include "SGTracking/SGGloveTracker.h"
struct USGVirtualHandComponent::FImpl
{
/************************
* Static methods
************************/
FORCEINLINE static const FString& GetDefaultLeftHandMeshPath()
{
static const FString Path{TEXT("SkeletalMesh'/SenseGlove/Meshes/"
"SK_SenseGlove_VirtualHand_Left.SK_SenseGlove_VirtualHand_Left'")};
return Path;
}
FORCEINLINE static const FString& GetDefaultLeftHandMeshPathOnly()
{
static const FString Path{TEXT("/SenseGlove/Meshes/"
"SK_SenseGlove_VirtualHand_Left.SK_SenseGlove_VirtualHand_Left")};
return Path;
}
FORCEINLINE static const FString& GetDefaultRightHandMeshPath()
{
static const FString Path{TEXT("SkeletalMesh'/SenseGlove/Meshes/"
"SK_SenseGlove_VirtualHand_Right.SK_SenseGlove_VirtualHand_Right'")};
return Path;
}
FORCEINLINE static const FString& GetDefaultRightHandMeshPathOnly()
{
static const FString Path{TEXT("/SenseGlove/Meshes/"
"SK_SenseGlove_VirtualHand_Right.SK_SenseGlove_VirtualHand_Right")};
return Path;
}
static const FSGVirtualHandTrackingSettings& GetVirtualHandTrackingSettings()
{
const USGSettings* Settings{USGSettings::GetInstance()};
ensureAlwaysMsgf(Settings, TEXT("The settings subsystem has not been initialized!"));
return Settings->GetTrackingSettings().VirtualHandTrackingSettings;
}
/************************
* Owner object
************************/
USGVirtualHandComponent* Owner;
/************************
* Constructor / Destructor
************************/
explicit FImpl(USGVirtualHandComponent* InOwner);
~FImpl();
/************************
* Default copy constructor & copy assignment operator
************************/
FImpl(const FImpl& Rhs) = default;
FImpl& operator=(const FImpl& Rhs) = default;
/************************
* Methods
************************/
bool IsEditor() const;
FString GetDefaultMeshPath() const;
bool IsUsingDefaultMesh() const;
bool IsUsingUserMesh() const;
USkeletalMesh* LoadDefaultMesh() const;
void SetDefaultMesh() const;
USkeletalMesh* GetMesh() const;
USkeleton* GetSkeleton() const;
const FReferenceSkeleton& GetRefSkeleton() const;
void UpdateGloveVisibility() const;
FORCEINLINE void SetVisibility(const bool bInVisible) const
{
Owner->SetVisibility(bInVisible, true);
}
void DrawDebugVirtualHand() const;
};
const TArray<FName>& USGVirtualHandComponent::GetLeftHandBoneNames()
{
return FImpl::GetVirtualHandTrackingSettings().LeftHandBoneNames;
}
const TArray<FName>& USGVirtualHandComponent::GetRightHandBoneNames()
{
return FImpl::GetVirtualHandTrackingSettings().RightHandBoneNames;
}
const FName& USGVirtualHandComponent::GetLeftHandBoneName(const int32 Joint)
{
const TArray<FName>& BoneNames{GetLeftHandBoneNames()};
ensureAlwaysMsgf(Joint < BoneNames.Num(), TEXT("Joint index is out of bound!"));
const FName& BoneName{BoneNames[Joint]};
return BoneName;
}
const FName& USGVirtualHandComponent::GetRightHandBoneName(const int32 Joint)
{
const TArray<FName>& BoneNames{GetRightHandBoneNames()};
ensureAlwaysMsgf(Joint < BoneNames.Num(), TEXT("Joint index is out of bound!"));
const FName& BoneName{BoneNames[Joint]};
return BoneName;
}
USGVirtualHandComponent::USGVirtualHandComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer),
Pimpl(TUniquePtr<FImpl, FImplDeleter>(new FImpl{this}, PimplDeleter))
{
PrimaryComponentTick.bTickEvenWhenPaused = false;
PrimaryComponentTick.bCanEverTick = true;
PrimaryComponentTick.bStartWithTickEnabled = true;
bTickInEditor = true;
bNeverNeedsRenderUpdate = false;
bAllowConcurrentTick = true;
SetUpdateAnimationInEditor(true);
SetCastShadow(true);
SetCastHiddenShadow(false);
bCastDynamicShadow = true;
SetOnlyOwnerSee(false);
SetReceivesDecals(true);
SetCanEverAffectNavigation(false);
Super::SetCollisionObjectType(ECC_WorldDynamic);
Super::SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
Super::SetCollisionResponseToAllChannels(ECR_Overlap);
Super::SetCollisionResponseToChannel(ECC_WorldStatic, ECR_Block);
Super::SetCollisionResponseToChannel(ECC_WorldDynamic, ECR_Block);
Super::SetCollisionResponseToChannel(ECC_Pawn, ECR_Block);
Super::SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
Super::SetCollisionResponseToChannel(ECC_PhysicsBody, ECR_Block);
Super::SetCollisionResponseToChannel(ECC_Vehicle, ECR_Block);
Super::SetCollisionResponseToChannel(ECC_Destructible, ECR_Block);
Super::SetAnimClass(USGVirtualHandAnimInstance::StaticClass());
bRight = true;
bHiddenInGameIfNoHandDataAvailable = false;
AnimationBoneRotationCorrectionOffset = FRotator{0.0f, 90.0f, 90.0f};
bAnimationShouldApplyBoneLocation = true;
bAutoStopAllHapticsOnEndPlay = true;
GrabSocketName = FName{TEXT("GenericGrabPoint")};
DefaultFingertipsGrabColliderSize = FVector{0.08f, 0.08f, 0.08f};
ThumbFingertipGrabSocketName = FName{TEXT("GrabThumbFingertip")};
IndexFingertipGrabSocketName = FName{TEXT("GrabIndexFingertip")};
MiddleFingertipGrabSocketName = FName{TEXT("GrabMiddleFingertip")};
DefaultFingertipsTouchColliderSize = FVector{0.08f, 0.08f, 0.08f};
ThumbFingertipTouchSocketName = FName{TEXT("TouchThumbFingertip")};
IndexFingertipTouchSocketName = FName{TEXT("TouchIndexFingertip")};
MiddleFingertipTouchSocketName = FName{TEXT("TouchMiddleFingertip")};
RingFingertipTouchSocketName = FName{TEXT("TouchRingFingertip")};
PinkyFingertipTouchSocketName = FName{TEXT("TouchPinkyFingertip")};
DebugVirtualHandSettings = FSGDebugVirtualHandSettings{};
Super::SetSkeletalMesh(nullptr, true);
WristTracker = nullptr;
}
void USGVirtualHandComponent::SetRight(const bool bInRight)
{
bRight = bInRight;
if (!Pimpl->IsUsingUserMesh())
{
Pimpl->SetDefaultMesh();
}
}
void USGVirtualHandComponent::SetAnimClass(UClass* NewClass)
{
ensureAlwaysMsgf((TIsDerivedFrom<decltype(NewClass), USGVirtualHandAnimInstance>::IsDerived),
TEXT("The AnimInstance class must be a subclass of USGVirtualHandAnimInstance!"));
Super::SetAnimClass(NewClass);
}
void USGVirtualHandComponent::PostInitProperties()
{
Super::PostInitProperties();
if (!Pimpl->IsUsingUserMesh())
{
Pimpl->SetDefaultMesh();
}
}
#if WITH_EDITOR
void USGVirtualHandComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
const FName PropertyName{
PropertyChangedEvent.Property
? PropertyChangedEvent.Property->GetFName()
: NAME_None
};
if (PropertyName == GET_MEMBER_NAME_CHECKED(USGVirtualHandComponent, bRight))
{
if (!Pimpl->IsUsingUserMesh())
{
Pimpl->SetDefaultMesh();
}
}
}
#endif /* WITH_EDITOR */
void USGVirtualHandComponent::InitializeComponent()
{
Super::InitializeComponent();
if (!Pimpl->IsUsingUserMesh())
{
Pimpl->SetDefaultMesh();
}
Pimpl->UpdateGloveVisibility();
}
void USGVirtualHandComponent::UninitializeComponent()
{
Super::UninitializeComponent();
const USGGloveTracker* GloveTracker{USGGloveTracker::GetInstance()};
USGHapticGlove* Glove{IsValid(GloveTracker) ? GloveTracker->GetGlove(IsRight()) : nullptr};
if (IsValid(Glove) && Glove->IsConnected() && AutoStopsAllHapticsOnEndPlay())
{
Glove->StopHaptics();
}
}
void USGVirtualHandComponent::BeginPlay()
{
Super::BeginPlay();
Pimpl->UpdateGloveVisibility();
}
void USGVirtualHandComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
const USGGloveTracker* GloveTracker{USGGloveTracker::GetInstance()};
USGHapticGlove* Glove{IsValid(GloveTracker) ? GloveTracker->GetGlove(IsRight()) : nullptr};
if (IsValid(Glove) && Glove->IsConnected() && AutoStopsAllHapticsOnEndPlay())
{
Glove->StopHaptics();
}
}
void USGVirtualHandComponent::TickComponent(
const float DeltaTime, const ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
#if WITH_EDITOR
if (Pimpl->IsEditor())
{
EditorTick(DeltaTime, TickType, ThisTickFunction);
return;
}
#endif /* WITH_EDITOR */
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
Pimpl->UpdateGloveVisibility();
if (DebugVirtualHandSettings.DrawingMode != ESGDebugVirtualHandDrawingMode::None)
{
Pimpl->DrawDebugVirtualHand();
}
}
void USGVirtualHandComponent::SetSkeletalMesh(USkeletalMesh* NewMesh, const bool bReinitPose)
{
if (IsValid(NewMesh))
{
Super::SetSkeletalMesh(NewMesh, bReinitPose);
}
else
{
Super::SetSkeletalMesh(Pimpl->LoadDefaultMesh(), bReinitPose);
}
}
void USGVirtualHandComponent::EditorTick(
const float DeltaTime, const ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Pimpl->UpdateGloveVisibility();
if (DebugVirtualHandSettings.DrawingMode != ESGDebugVirtualHandDrawingMode::None)
{
Pimpl->DrawDebugVirtualHand();
}
}
bool USGVirtualHandComponent::IsGloveConnected() const
{
const USGGloveTracker* GloveTracker{USGGloveTracker::GetInstance()};
const USGHapticGlove* Glove{IsValid(GloveTracker) ? GloveTracker->GetGlove(IsRight()) : nullptr};
return IsValid(Glove);
}
USGHapticGlove* USGVirtualHandComponent::GetConnectedGlove() const
{
const USGGloveTracker* GloveTracker{USGGloveTracker::GetInstance()};
USGHapticGlove* Glove{IsValid(GloveTracker) ? GloveTracker->GetGlove(IsRight()) : nullptr};
return Glove;
}
bool USGVirtualHandComponent::GetMotionControllerData(FXRMotionControllerData& OutMotionControllerData)
{
OutMotionControllerData.bValid = false;
if (GEngine)
{
IXRTrackingSystem* XrtTrackingSystem{GEngine->XRSystem.Get()};
if (XrtTrackingSystem)
{
const EControllerHand Hand = IsRight() ? EControllerHand::Right : EControllerHand::Left;
XrtTrackingSystem->GetMotionControllerData(this, Hand, OutMotionControllerData);
}
}
return OutMotionControllerData.bValid;
}
const TArray<FName>& USGVirtualHandComponent::GetHandBoneNames() const
{
const TArray<FName>& Names{
IsRight()
? GetRightHandBoneNames()
: GetLeftHandBoneNames()
};
return Names;
}
const FName& USGVirtualHandComponent::GetHandBoneName(const int32 Joint) const
{
const FName& Name{
IsRight()
? GetRightHandBoneName(Joint)
: GetLeftHandBoneName(Joint)
};
return Name;
}
const FTransform& USGVirtualHandComponent::GetHandBoneRefTransform(const FName& BoneName) const
{
const FReferenceSkeleton& ReferenceSkeleton{Pimpl->GetRefSkeleton()};
const int32 BoneIndex = ReferenceSkeleton.FindBoneIndex(BoneName);
if (BoneIndex == INDEX_NONE)
{
SGLOG_WARNING(FString::Printf(TEXT("Could not find a bone index for the '%s' joint!"),
*BoneName.ToString()));
return FTransform::Identity;
}
const FTransform& BoneTransform{ReferenceSkeleton.GetRefBonePose()[BoneIndex]};
return BoneTransform;
}
const FTransform& USGVirtualHandComponent::GetHandRootBoneRefTransform() const
{
static constexpr int32_t WristKeypointIndex = static_cast<int32>(EHandKeypoint::Wrist);
const FTransform& BoneTransform{GetHandBoneRefTransform(WristKeypointIndex)};
return BoneTransform;
}
const FTransform& USGVirtualHandComponent::GetHandBoneRefTransform(const int32 Joint) const
{
const FName& BoneName{GetHandBoneName(Joint)};
return GetHandBoneRefTransform(BoneName);
}
FRotator USGVirtualHandComponent::GetHandBoneRefRotation(const FName& BoneName) const
{
const FTransform BoneTransform{GetHandBoneRefTransform(BoneName)};
const FRotator BoneRotation{BoneTransform.GetRotation().Rotator()};
return BoneRotation;
}
FRotator USGVirtualHandComponent::GetHandBoneRefRotation(const int32 Joint) const
{
const FTransform BoneTransform{GetHandBoneRefTransform(Joint)};
const FRotator BoneRotation{BoneTransform.GetRotation().Rotator()};
return BoneRotation;
}
USGVirtualHandComponent::FImpl::FImpl(USGVirtualHandComponent* InOwner)
: Owner(InOwner)
{
}
USGVirtualHandComponent::FImpl::~FImpl() = default;
bool USGVirtualHandComponent::FImpl::IsEditor() const
{
const UWorld* World{Owner->GetWorld()};
if (World && (World->WorldType == EWorldType::Editor || World->WorldType == EWorldType::EditorPreview))
{
return true;
}
return false;
}
FString USGVirtualHandComponent::FImpl::GetDefaultMeshPath() const
{
FString SkeletalMeshPath;
if (Owner->IsRight())
{
SkeletalMeshPath = GetDefaultRightHandMeshPath();
}
else
{
SkeletalMeshPath = GetDefaultLeftHandMeshPath();
}
return SkeletalMeshPath;
}
bool USGVirtualHandComponent::FImpl::IsUsingDefaultMesh() const
{
const USkeletalMesh* CurrentMesh{GetMesh()};
if (!IsValid(CurrentMesh))
{
return false;
}
const FString CurrentMeshPath{CurrentMesh->GetPathName()};
const FString LeftHandDefaultMeshPath{GetDefaultLeftHandMeshPath()};
const FString LeftHandDefaultMeshPathOnly{GetDefaultLeftHandMeshPathOnly()};
const FString RightHandDefaultMeshPath{GetDefaultRightHandMeshPath()};
const FString RightHandDefaultMeshPathOnly{GetDefaultRightHandMeshPathOnly()};
if (CurrentMeshPath == LeftHandDefaultMeshPath
|| CurrentMeshPath == LeftHandDefaultMeshPathOnly
|| CurrentMeshPath == RightHandDefaultMeshPath
|| CurrentMeshPath == RightHandDefaultMeshPathOnly)
{
return true;
}
return false;
}
bool USGVirtualHandComponent::FImpl::IsUsingUserMesh() const
{
const USkeletalMesh* CurrentMesh{GetMesh()};
if (!IsValid(CurrentMesh))
{
return false;
}
const FString CurrentMeshPath{CurrentMesh->GetPathName()};
const FString LeftHandDefaultMeshPath{GetDefaultLeftHandMeshPath()};
const FString LeftHandDefaultMeshPathOnly{GetDefaultLeftHandMeshPathOnly()};
const FString RightHandDefaultMeshPath{GetDefaultRightHandMeshPath()};
const FString RightHandDefaultMeshPathOnly{GetDefaultRightHandMeshPathOnly()};
if (CurrentMeshPath == LeftHandDefaultMeshPath
|| CurrentMeshPath == LeftHandDefaultMeshPathOnly
|| CurrentMeshPath == RightHandDefaultMeshPath
|| CurrentMeshPath == RightHandDefaultMeshPathOnly)
{
return false;
}
return true;
}
USkeletalMesh* USGVirtualHandComponent::FImpl::LoadDefaultMesh() const
{
const FString DefaultMeshPath{GetDefaultMeshPath()};
USkeletalMesh* DefaultMesh{
Cast<USkeletalMesh>(StaticLoadObject(USkeletalMesh::StaticClass(), nullptr, *DefaultMeshPath))
};
if (!IsValid(DefaultMesh))
{
SGLOG_WARNING(FString::Printf(TEXT("Failed to load the default skeletal mesh: '%s'"), *DefaultMeshPath));
}
return DefaultMesh;
}
void USGVirtualHandComponent::FImpl::SetDefaultMesh() const
{
Owner->SetSkeletalMesh(LoadDefaultMesh(), true);
}
USkeletalMesh* USGVirtualHandComponent::FImpl::GetMesh() const
{
return Owner->GetSkeletalMeshAsset();
}
USkeleton* USGVirtualHandComponent::FImpl::GetSkeleton() const
{
USkeletalMesh* Mesh{GetMesh()};
ensureAlwaysMsgf(IsValid(Mesh), TEXT("Invalid SkeletalMesh object!"));
return Mesh->GetSkeleton();
}
const FReferenceSkeleton& USGVirtualHandComponent::FImpl::GetRefSkeleton() const
{
const USkeletalMesh* Mesh{GetMesh()};
ensureAlwaysMsgf(IsValid(Mesh), TEXT("Invalid SkeletalMesh object!"));
return Mesh->GetRefSkeleton();
}
void USGVirtualHandComponent::FImpl::UpdateGloveVisibility() const
{
FXRMotionControllerData MotionControllerData;
const bool bGotMotionControllerData = Owner->GetMotionControllerData(MotionControllerData);
const bool bIsHandDataAvailable = bGotMotionControllerData && MotionControllerData.bValid;
const bool bIsVisible = Owner->IsVisible();
if (Owner->bHiddenInGameIfNoHandDataAvailable)
{
if (bIsHandDataAvailable)
{
if (!bIsVisible)
{
SetVisibility(true);
}
}
else
{
if (bIsVisible)
{
SetVisibility(false);
}
}
}
else
{
if (!bIsVisible)
{
SetVisibility(true);
}
}
}
void USGVirtualHandComponent::FImpl::DrawDebugVirtualHand() const
{
const UWorld* World{Owner->GetWorld()};
if (!IsValid(Owner->WristTracker))
{
return;
}
FXRMotionControllerData MotionControllerData;
const bool bGotMotionControllerData{Owner->GetMotionControllerData(MotionControllerData)};
if (!bGotMotionControllerData || !MotionControllerData.bValid)
{
return;
}
FSGDebugVirtualHand::Draw(World, MotionControllerData, Owner->DebugVirtualHandSettings);
}
void USGVirtualHandComponent::FImplDeleter::operator()(const FImpl* P) const
{
delete P;
}