fix the how we determine the motion source inside the GetControllerOrientationAndPosition and GetControllerTrackingStatus for ue versions earlier than 5.3

This commit is contained in:
Mamadou Babaei
2024-08-16 16:37:24 +02:00
parent f9fb5c61ca
commit 003d3f9b26
@@ -1117,96 +1117,82 @@ bool FSGXRTracker::GetControllerOrientationAndPosition(const int32 ControllerInd
return false;
}
if (ControllerIndex != Pimpl->DeviceIndex)
{
return false;
}
bool bTracked = false;
if (ControllerIndex == Pimpl->DeviceIndex)
{
FTransform ControllerTransform{FTransform::Identity};
const bool bFallbackToHandTrackingIfNoGloveDetected = FImpl::ShouldFallbackToHandTrackingIfNoGloveDetected();
if (bFallbackToHandTrackingIfNoGloveDetected)
{
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3
const FImpl::FSGMotionSourceInfo* KeyPointInfoPtr{Pimpl->MotionSourceToKeypointMap.Find(MotionSource)};
if (KeyPointInfoPtr)
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
if (GetControllerTrackingStatus(ControllerIndex, DeviceHand) != ETrackingStatus::NotTracked)
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
{
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3
const EHandKeypoint KeyPoint = KeyPointInfoPtr->Key;
const bool bIsLeft = KeyPointInfoPtr->Value;
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
const bool bIsLeft = DeviceHand == EControllerHand::Left;
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
if (bIsLeft)
{
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3
ControllerTransform = GetLeftHandState().GetTransform(KeyPoint);
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
ControllerTransform = GetLeftHandState().GetTransform(EHandKeypoint::Wrist);
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
bTracked = !!GetLeftHandState().bReceivedJointPoses;
}
else
{
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3
ControllerTransform = GetRightHandState().GetTransform(KeyPoint);
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
ControllerTransform = GetRightHandState().GetTransform(EHandKeypoint::Wrist);
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
bTracked = !!GetRightHandState().bReceivedJointPoses;
}
}
}
if (bTracked)
{
OutPosition = ControllerTransform.GetLocation();
OutOrientation = ControllerTransform.GetRotation().Rotator();
}
else
{
static const FName OpenXRName{TEXT("OpenXR")};
static constexpr int32 CurrentPlayerControllerIndex = 0;
FTransform ControllerTransform{FTransform::Identity};
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION <= 2
const FName& MotionSource{FImpl::GetDefaultMotionSourceName(DeviceHand)};
const FName& MotionSource{StaticEnum<EControllerHand>()->GetNameByValue(static_cast<int64>(DeviceHand))};
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION <= 2 */
const TArray<IMotionController*> MotionControllers{
IModularFeatures::Get().GetModularFeatureImplementations<IMotionController>(
IMotionController::GetModularFeatureName())
};
for (const IMotionController* MotionController: MotionControllers)
const bool bFallbackToHandTrackingIfNoGloveDetected = FImpl::ShouldFallbackToHandTrackingIfNoGloveDetected();
if (bFallbackToHandTrackingIfNoGloveDetected)
{
const FImpl::FSGMotionSourceInfo* KeyPointInfoPtr{Pimpl->MotionSourceToKeypointMap.Find(MotionSource)};
if (KeyPointInfoPtr)
{
const EHandKeypoint KeyPoint = KeyPointInfoPtr->Key;
const bool bIsLeft = KeyPointInfoPtr->Value;
if (bIsLeft)
{
if (!MotionController)
{
continue;
}
ControllerTransform = GetLeftHandState().GetTransform(KeyPoint);
bTracked = !!GetLeftHandState().bReceivedJointPoses;
}
else
{
ControllerTransform = GetRightHandState().GetTransform(KeyPoint);
bTracked = !!GetRightHandState().bReceivedJointPoses;
}
}
}
const FName DeviceTypeName{MotionController->GetMotionControllerDeviceTypeName()};
if (DeviceTypeName != OpenXRName)
{
continue;
}
if (bTracked)
{
OutPosition = ControllerTransform.GetLocation();
OutOrientation = ControllerTransform.GetRotation().Rotator();
}
else
{
static const FName OpenXRName{TEXT("OpenXR")};
const ETrackingStatus TrackingStatus = MotionController->GetControllerTrackingStatus(
CurrentPlayerControllerIndex, MotionSource);
if (TrackingStatus == ETrackingStatus::NotTracked)
{
continue;
}
static constexpr int32 CurrentPlayerControllerIndex = 0;
const bool bGotTransform = MotionController->GetControllerOrientationAndPosition(
ControllerIndex, MotionSource, OutOrientation, OutPosition, WorldToMetersScale);
if (bGotTransform)
{
bTracked = true;
break;
}
const TArray<IMotionController*> MotionControllers{
IModularFeatures::Get().GetModularFeatureImplementations<IMotionController>(
IMotionController::GetModularFeatureName())
};
for (const IMotionController* MotionController: MotionControllers)
{
if (!MotionController)
{
continue;
}
const FName DeviceTypeName{MotionController->GetMotionControllerDeviceTypeName()};
if (DeviceTypeName != OpenXRName)
{
continue;
}
const ETrackingStatus TrackingStatus = MotionController->GetControllerTrackingStatus(
CurrentPlayerControllerIndex, MotionSource);
if (TrackingStatus == ETrackingStatus::NotTracked)
{
continue;
}
const bool bGotTransform = MotionController->GetControllerOrientationAndPosition(
ControllerIndex, MotionSource, OutOrientation, OutPosition, WorldToMetersScale);
if (bGotTransform)
{
bTracked = true;
break;
}
}
}
@@ -1226,14 +1212,14 @@ ETrackingStatus FSGXRTracker::GetControllerTrackingStatus(const int32 Controller
return ETrackingStatus::NotTracked;
}
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION <= 2
const FName& MotionSource{StaticEnum<EControllerHand>()->GetNameByValue(static_cast<int64>(DeviceHand))};
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION <= 2 */
const FImpl::FSGMotionSourceInfo* KeyPointInfoPtr{Pimpl->MotionSourceToKeypointMap.Find(MotionSource)};
if (KeyPointInfoPtr)
{
const bool bIsLeft = KeyPointInfoPtr->Value;
#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
const bool bIsLeft = DeviceHand == EControllerHand::Left;
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
if ((bIsLeft && FImpl::IsLeftGloveConnected()) || (!bIsLeft && FImpl::IsRightGloveConnected()))
{
@@ -1246,9 +1232,7 @@ ETrackingStatus FSGXRTracker::GetControllerTrackingStatus(const int32 Controller
const FSGXRHandState& HandState{bIsLeft ? GetLeftHandState() : GetRightHandState()};
return !!HandState.bReceivedJointPoses ? ETrackingStatus::Tracked : ETrackingStatus::NotTracked;
}
#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3
}
#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */
return ETrackingStatus::NotTracked;
}