remove support for FXRMotionControllerData on UE 5.7 as it has been obliterated by the upstream and apply necessary changes to make XR_EXT_hand_tracking work again on 5.7

This commit is contained in:
Mamadou Babaei
2025-11-18 03:23:24 +01:00
parent 2a51d58f05
commit 93e8e7878b
16 changed files with 39 additions and 372 deletions
@@ -650,6 +650,10 @@ struct FSGXRTracker::FImpl
void BuildMotionSourceToKeypointMap();
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7
void CreateHandTracker(XrSession InSession, XrHandEXT HandEXT, FSGXRHandState& HandState) const;
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7 */
bool CanOutputTrackingData(EControllerHand Hand) const;
bool UpdateXRHandTrackingData(FSGXRHandState& OutHandState, const XrHandJointsLocateInfoEXT& LocateInfo) const;
@@ -1009,231 +1013,6 @@ FTransform FSGXRTracker::FImpl::GetMeshFingerStartBoneRefTransform(
return Transform;
}
bool FSGXRTracker::GetMotionControllerData(
UObject* WorldContextObject,
const EControllerHand Hand,
FXRMotionControllerData& OutMotionControllerData)
{
OutMotionControllerData.DeviceName = NAME_None;
OutMotionControllerData.ApplicationInstanceID = FApp::GetInstanceId();
OutMotionControllerData.DeviceVisualType = EXRVisualType::Controller;
OutMotionControllerData.TrackingStatus = ETrackingStatus::NotTracked;
OutMotionControllerData.HandIndex = Hand;
OutMotionControllerData.bValid = false;
if (!IsValid(WorldContextObject))
{
return false;
}
IXRTrackingSystem* XRTrackingSystem{GEngine ? GEngine->XRSystem.Get() : nullptr};
if (!XRTrackingSystem)
{
return false;
}
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 4
IOpenXRHMD* OpenXRHMD{XRTrackingSystem->GetIOpenXRHMD()};
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 4 */
FOpenXRHMD* OpenXRHMD{static_cast<FOpenXRHMD*>(GEngine->XRSystem.Get())};
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 4 */
if (!OpenXRHMD)
{
return false;
}
TArray<int32> Devices;
const int32 HandIndex = static_cast<int32>(Hand);
const bool bGotDevices =
XRTrackingSystem->EnumerateTrackedDevices(Devices, EXRTrackedDeviceType::Controller);
if (bGotDevices && Devices.IsValidIndex(HandIndex))
{
const XrSession Session{OpenXRHMD->GetSession()};
if (Session)
{
XrInteractionProfileState Profile = {XR_TYPE_INTERACTION_PROFILE_STATE};
const XrPath TopLevelUserPath{OpenXRHMD->GetTrackedDevicePath(Devices[HandIndex])};
const XrResult CurrentInteractionProfile{
xrGetCurrentInteractionProfile(Session, TopLevelUserPath, &Profile)
};
if (XR_SUCCEEDED(CurrentInteractionProfile) && Profile.interactionProfile != XR_NULL_PATH)
{
OutMotionControllerData.DeviceName = FOpenXRPath(Profile.interactionProfile);
}
}
}
static const FName HandTrackerName{"OpenXRHandTracking"};
TArray<IHandTracker*> HandTrackers = IModularFeatures::Get().GetModularFeatureImplementations<IHandTracker>(
IHandTracker::GetModularFeatureName());
IHandTracker* HandTracker{nullptr};
for (IHandTracker* HandTrackerIterator: HandTrackers)
{
if (!HandTrackerIterator)
{
continue;
}
const FName DeviceTypeName{HandTrackerIterator->GetHandTrackerDeviceTypeName()};
if (DeviceTypeName != HandTrackerName)
{
continue;
}
FSGXRTracker* SGXRTracker{static_cast<FSGXRTracker*>(HandTrackerIterator)};
if (!SGXRTracker)
{
continue;
}
HandTracker = SGXRTracker;
break;
}
if (Hand == EControllerHand::Left || Hand == EControllerHand::Right)
{
static const FName MotionControllerName("OpenXR");
TArray<IMotionController*> MotionControllers{
IModularFeatures::Get().GetModularFeatureImplementations<IMotionController>(
IMotionController::GetModularFeatureName())
};
IMotionController* MotionController{nullptr};
for (IMotionController* MotionControllerIterator: MotionControllers)
{
if (!MotionControllerIterator)
{
continue;
}
const FName DeviceTypeName{MotionControllerIterator->GetMotionControllerDeviceTypeName()};
if (DeviceTypeName != MotionControllerName)
{
continue;
}
MotionController = MotionControllerIterator;
break;
}
const float WorldToMeters = XRTrackingSystem->GetWorldToMetersScale();
if (MotionController)
{
const FTransform TrackingToWorld{XRTrackingSystem->GetTrackingToWorldTransform()};
{
const FName AimSource{Hand == EControllerHand::Left ? FName{TEXT("LeftAim")} : FName{TEXT("RightAim")}};
FRotator AimSourceRotation;
FVector AimSourcePosition;
const bool bGotAimOrientationAndPosition =
MotionController->GetControllerOrientationAndPosition(
0, AimSource, AimSourceRotation, AimSourcePosition, WorldToMeters);
if (bGotAimOrientationAndPosition)
{
OutMotionControllerData.AimPosition = TrackingToWorld.TransformPosition(AimSourcePosition);
OutMotionControllerData.AimRotation =
TrackingToWorld.TransformRotation(AimSourceRotation.Quaternion());
}
OutMotionControllerData.bValid |= bGotAimOrientationAndPosition;
}
const FName GripSource{Hand == EControllerHand::Left ? FName{TEXT("LeftGrip")} : FName{TEXT("RightGrip")}};
{
FRotator GripRotation;
FVector GripPosition;
const bool bGotGripSourceOrientationAndPosition =
MotionController->GetControllerOrientationAndPosition(
0, GripSource, GripRotation, GripPosition, WorldToMeters);
if (bGotGripSourceOrientationAndPosition)
{
OutMotionControllerData.GripPosition = TrackingToWorld.TransformPosition(GripPosition);
OutMotionControllerData.GripRotation =
TrackingToWorld.TransformRotation(GripRotation.Quaternion());
}
OutMotionControllerData.bValid |= bGotGripSourceOrientationAndPosition;
}
{
const FName PalmSource{
Hand == EControllerHand::Left ? FName{TEXT("LeftPalm")} : FName{TEXT("RightPalm")}
};
FRotator PalmRotation;
FVector PalmPosition;
const bool bGotPalmOrientationAndPosition = MotionController->GetControllerOrientationAndPosition(
0, PalmSource, PalmRotation, PalmPosition, WorldToMeters);
if (bGotPalmOrientationAndPosition)
{
OutMotionControllerData.GripPosition = TrackingToWorld.TransformPosition(PalmPosition);
OutMotionControllerData.GripRotation =
TrackingToWorld.TransformRotation(PalmRotation.Quaternion());
}
OutMotionControllerData.bValid |= bGotPalmOrientationAndPosition;
}
OutMotionControllerData.TrackingStatus = MotionController->GetControllerTrackingStatus(0, GripSource);
}
const bool bHandTrackingStateValid = HandTracker && HandTracker->IsHandTrackingStateValid();
if (bHandTrackingStateValid)
{
OutMotionControllerData.DeviceVisualType = EXRVisualType::Hand;
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
bool bTracked = false;
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
OutMotionControllerData.bValid = HandTracker->GetAllKeypointStates(
Hand,
OutMotionControllerData.HandKeyPositions,
OutMotionControllerData.HandKeyRotations,
OutMotionControllerData.HandKeyRadii
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
, bTracked
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
);
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
// Unused!
(void) bTracked;
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
// TODO
// Begin backward compatibility with the deprecated function, remove this block along with the deprecated
// GetAllKeypointStates (though it should be time to remove this entire function as well)!
if (!OutMotionControllerData.bValid)
{
PRAGMA_DISABLE_DEPRECATION_WARNINGS
OutMotionControllerData.bValid = HandTracker->GetAllKeypointStates(
Hand,
OutMotionControllerData.HandKeyPositions,
OutMotionControllerData.HandKeyRotations,
OutMotionControllerData.HandKeyRadii);
PRAGMA_ENABLE_DEPRECATION_WARNINGS
}
// End backward compability.
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5 */
if (!ensureAlwaysMsgf(
!OutMotionControllerData.bValid
|| (OutMotionControllerData.HandKeyPositions.Num() == EHandKeypointCount
&& OutMotionControllerData.HandKeyRotations.Num() == EHandKeypointCount
&& OutMotionControllerData.HandKeyRadii.Num() == EHandKeypointCount),
TEXT("Corrupted OutMotionControllerData!")))
{
OutMotionControllerData.bValid = false;
return false;
}
}
}
if (OutMotionControllerData.bValid)
{
OutMotionControllerData.TrackingStatus = ETrackingStatus::Tracked;
}
return OutMotionControllerData.bValid;
}
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 5
bool FSGXRTracker::GetMotionControllerState(
@@ -1478,6 +1257,7 @@ bool FSGXRTracker::GetHandTrackingState(
OutHandTrackingState.HandKeyRadii,
bTracked);
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION < 7
/// TODO
// Begin backward compatibility with the deprecated function, remove this block along with the deprecated
// GetAllKeypointStates.
@@ -1494,6 +1274,7 @@ bool FSGXRTracker::GetHandTrackingState(
// The inability to distinguish this is the reason for the deprecation.
}
// End backward compability.
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION < 7 */
if (!ensureAlwaysMsgf(
!OutHandTrackingState.bValid
@@ -1589,7 +1370,7 @@ bool FSGXRTracker::GetRequiredExtensions(TArray<const ANSICHAR*>& OutExtensions)
}
}
OutExtensions.Add("XR_EXT_hand_tracking");
OutExtensions.Add(XR_EXT_HAND_TRACKING_EXTENSION_NAME);
return true;
}
@@ -1638,21 +1419,29 @@ const void* FSGXRTracker::OnBeginSession(const XrSession InSession, const void*
// Create a hand tracker for the left hand that tracks default set of hand joints.
FSGXRHandState& LeftHandState{Pimpl->GetLeftHandState()};
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7
Pimpl->CreateHandTracker(InSession, XR_HAND_LEFT_EXT, LeftHandState);
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7 */
{
XrHandTrackerCreateInfoEXT CreateInfo{XR_TYPE_HAND_TRACKER_CREATE_INFO_EXT};
CreateInfo.hand = XR_HAND_LEFT_EXT;
CreateInfo.handJointSet = XR_HAND_JOINT_SET_DEFAULT_EXT;
XR_ENSURE(Pimpl->XRCreateHandTrackerEXT(InSession, &CreateInfo, &LeftHandState.HandTracker));
}
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7 */
// Create a hand tracker for the right hand that tracks default set of hand joints.
FSGXRHandState& RightHandState{Pimpl->GetRightHandState()};
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7
Pimpl->CreateHandTracker(InSession, XR_HAND_RIGHT_EXT, RightHandState);
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7 */
{
XrHandTrackerCreateInfoEXT CreateInfo{XR_TYPE_HAND_TRACKER_CREATE_INFO_EXT};
CreateInfo.hand = XR_HAND_RIGHT_EXT;
CreateInfo.handJointSet = XR_HAND_JOINT_SET_DEFAULT_EXT;
XR_ENSURE(Pimpl->XRCreateHandTrackerEXT(InSession, &CreateInfo, &RightHandState.HandTracker));
}
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7 */
}
return InNext;
@@ -2030,6 +1819,27 @@ void FSGXRTracker::FImpl::BuildMotionSourceToKeypointMap()
RightHandSGMotionSourceName, FSGMotionSourceInfo(EHandKeypoint::Wrist, false));
}
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7
void FSGXRTracker::FImpl::CreateHandTracker(XrSession InSession, XrHandEXT HandEXT, FSGXRHandState& HandState) const
{
XrHandTrackerCreateInfoEXT CreateInfo{XR_TYPE_HAND_TRACKER_CREATE_INFO_EXT};
CreateInfo.hand = HandEXT;
CreateInfo.handJointSet = XR_HAND_JOINT_SET_DEFAULT_EXT;
IOpenXRHMD* OpenXRHMD{XRTrackingSystem ? XRTrackingSystem->GetIOpenXRHMD() : nullptr};
if (OpenXRHMD)
{
TArray<IOpenXRExtensionPlugin*> ExtensionPlugins{OpenXRHMD->GetExtensionPlugins()};
for (IOpenXRExtensionPlugin* Plugin : ExtensionPlugins)
{
CreateInfo.next = Plugin->OnCreateHandTracker(CreateInfo.next);
}
}
XR_ENSURE(XRCreateHandTrackerEXT(InSession, &CreateInfo, &HandState.HandTracker));
}
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 7 */
bool FSGXRTracker::FImpl::CanOutputTrackingData(const EControllerHand Hand) const
{
const bool bFallbackToHandTrackingIfNoGloveDetected = ShouldFallbackToHandTrackingIfNoGloveDetected();