You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
296 lines
8.3 KiB
C++
296 lines
8.3 KiB
C++
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "HeadMountedDisplayFunctionLibrary.h"
|
|
#include "EngineGlobals.h"
|
|
#include "Engine/Engine.h"
|
|
#include "GameFramework/WorldSettings.h"
|
|
#include "IHeadMountedDisplay.h"
|
|
#include "ISpectatorScreenController.h"
|
|
|
|
DEFINE_LOG_CATEGORY_STATIC(LogUHeadMountedDisplay, Log, All);
|
|
|
|
UHeadMountedDisplayFunctionLibrary::UHeadMountedDisplayFunctionLibrary(const FObjectInitializer& ObjectInitializer)
|
|
: Super(ObjectInitializer)
|
|
{
|
|
}
|
|
|
|
bool UHeadMountedDisplayFunctionLibrary::IsHeadMountedDisplayEnabled()
|
|
{
|
|
return GEngine->HMDDevice.IsValid() && GEngine->HMDDevice->IsHeadTrackingAllowed();
|
|
}
|
|
|
|
bool UHeadMountedDisplayFunctionLibrary::IsHeadMountedDisplayConnected()
|
|
{
|
|
return GEngine->HMDDevice.IsValid() && GEngine->HMDDevice->IsHMDConnected();
|
|
}
|
|
|
|
bool UHeadMountedDisplayFunctionLibrary::EnableHMD(bool bEnable)
|
|
{
|
|
if (GEngine->HMDDevice.IsValid())
|
|
{
|
|
GEngine->HMDDevice->EnableHMD(bEnable);
|
|
if (bEnable)
|
|
{
|
|
return GEngine->HMDDevice->EnableStereo(true);
|
|
}
|
|
else
|
|
{
|
|
GEngine->HMDDevice->EnableStereo(false);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
FName UHeadMountedDisplayFunctionLibrary::GetHMDDeviceName()
|
|
{
|
|
FName DeviceName(NAME_None);
|
|
|
|
if (GEngine->HMDDevice.IsValid())
|
|
{
|
|
DeviceName = GEngine->HMDDevice->GetDeviceName();
|
|
}
|
|
|
|
return DeviceName;
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::GetOrientationAndPosition(FRotator& DeviceRotation, FVector& DevicePosition)
|
|
{
|
|
if(GEngine->HMDDevice.IsValid() && GEngine->HMDDevice->IsHeadTrackingAllowed())
|
|
{
|
|
FQuat OrientationAsQuat;
|
|
FVector Position(0.f);
|
|
|
|
GEngine->HMDDevice->GetCurrentOrientationAndPosition(OrientationAsQuat, Position);
|
|
|
|
DeviceRotation = OrientationAsQuat.Rotator();
|
|
DevicePosition = Position;
|
|
}
|
|
else
|
|
{
|
|
DeviceRotation = FRotator::ZeroRotator;
|
|
DevicePosition = FVector::ZeroVector;
|
|
}
|
|
}
|
|
|
|
bool UHeadMountedDisplayFunctionLibrary::HasValidTrackingPosition()
|
|
{
|
|
if(GEngine->HMDDevice.IsValid() && GEngine->HMDDevice->IsHeadTrackingAllowed())
|
|
{
|
|
return GEngine->HMDDevice->HasValidTrackingPosition();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
int32 UHeadMountedDisplayFunctionLibrary::GetNumOfTrackingSensors()
|
|
{
|
|
if (GEngine->HMDDevice.IsValid())
|
|
{
|
|
return GEngine->HMDDevice->GetNumOfTrackingSensors();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::GetPositionalTrackingCameraParameters(FVector& CameraOrigin, FRotator& CameraRotation, float& HFOV, float& VFOV, float& CameraDistance, float& NearPlane, float& FarPlane)
|
|
{
|
|
bool isActive;
|
|
float LeftFOV;
|
|
float RightFOV;
|
|
float TopFOV;
|
|
float BottomFOV;
|
|
GetTrackingSensorParameters(CameraOrigin, CameraRotation, LeftFOV, RightFOV, TopFOV, BottomFOV, CameraDistance, NearPlane, FarPlane, isActive, 0);
|
|
HFOV = LeftFOV + RightFOV;
|
|
VFOV = TopFOV + BottomFOV;
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::GetTrackingSensorParameters(FVector& Origin, FRotator& Rotation, float& LeftFOV, float& RightFOV, float& TopFOV, float& BottomFOV, float& Distance, float& NearPlane, float& FarPlane, bool& IsActive, int32 Index)
|
|
{
|
|
IsActive = false;
|
|
if (Index >= 0 && GEngine->HMDDevice.IsValid() && GEngine->HMDDevice->IsHeadTrackingAllowed() && GEngine->HMDDevice->DoesSupportPositionalTracking())
|
|
{
|
|
FQuat Orientation;
|
|
IsActive = GEngine->HMDDevice->GetTrackingSensorProperties((uint8)Index, Origin, Orientation, LeftFOV, RightFOV, TopFOV, BottomFOV, Distance, NearPlane, FarPlane);
|
|
Rotation = Orientation.Rotator();
|
|
}
|
|
else
|
|
{
|
|
// No HMD, zero the values
|
|
Origin = FVector::ZeroVector;
|
|
Rotation = FRotator::ZeroRotator;
|
|
LeftFOV = RightFOV = TopFOV = BottomFOV = 0.f;
|
|
NearPlane = 0.f;
|
|
FarPlane = 0.f;
|
|
Distance = 0.f;
|
|
}
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition(float Yaw, EOrientPositionSelector::Type Options)
|
|
{
|
|
if (GEngine->HMDDevice.IsValid() && GEngine->HMDDevice->IsHeadTrackingAllowed())
|
|
{
|
|
switch (Options)
|
|
{
|
|
case EOrientPositionSelector::Orientation:
|
|
GEngine->HMDDevice->ResetOrientation(Yaw);
|
|
break;
|
|
case EOrientPositionSelector::Position:
|
|
GEngine->HMDDevice->ResetPosition();
|
|
break;
|
|
default:
|
|
GEngine->HMDDevice->ResetOrientationAndPosition(Yaw);
|
|
}
|
|
}
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::SetClippingPlanes(float Near, float Far)
|
|
{
|
|
if (GEngine->HMDDevice.IsValid())
|
|
{
|
|
GEngine->HMDDevice->SetClippingPlanes(Near, Far);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets screen percentage to be used in VR mode.
|
|
*
|
|
* @param ScreenPercentage (in) Specifies the screen percentage to be used in VR mode. Use 0.0f value to reset to default value.
|
|
*/
|
|
void UHeadMountedDisplayFunctionLibrary::SetScreenPercentage(float ScreenPercentage)
|
|
{
|
|
static const auto ScreenPercentageCVar = IConsoleManager::Get().FindConsoleVariable(TEXT("r.ScreenPercentage"));
|
|
static float SavedValue = 0.f; // TODO: Add a way to ask HMD devices for the "ideal" screen percentage value and use that when resetting
|
|
if (ScreenPercentage > 0.f)
|
|
{
|
|
if (SavedValue <= 0.f)
|
|
{
|
|
SavedValue = ScreenPercentageCVar->GetFloat();
|
|
}
|
|
ScreenPercentageCVar->Set(ScreenPercentage);
|
|
}
|
|
else if (SavedValue > 0.f)
|
|
{
|
|
ScreenPercentageCVar->Set(SavedValue);
|
|
SavedValue = 0.f;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns screen percentage to be used in VR mode.
|
|
*
|
|
* @return (float) The screen percentage to be used in VR mode.
|
|
*/
|
|
float UHeadMountedDisplayFunctionLibrary::GetScreenPercentage()
|
|
{
|
|
static const auto ScreenPercentageTCVar = IConsoleManager::Get().FindTConsoleVariableDataFloat(TEXT("r.ScreenPercentage"));
|
|
return ScreenPercentageTCVar->GetValueOnGameThread();
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::SetWorldToMetersScale(UObject* WorldContext, float NewScale)
|
|
{
|
|
if (WorldContext)
|
|
{
|
|
WorldContext->GetWorld()->GetWorldSettings()->WorldToMeters = NewScale;
|
|
}
|
|
}
|
|
|
|
float UHeadMountedDisplayFunctionLibrary::GetWorldToMetersScale(UObject* WorldContext)
|
|
{
|
|
return WorldContext ? WorldContext->GetWorld()->GetWorldSettings()->WorldToMeters : 0.f;
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::SetTrackingOrigin(TEnumAsByte<EHMDTrackingOrigin::Type> InOrigin)
|
|
{
|
|
if (GEngine->HMDDevice.IsValid())
|
|
{
|
|
EHMDTrackingOrigin::Type Origin = EHMDTrackingOrigin::Eye;
|
|
switch (InOrigin)
|
|
{
|
|
case EHMDTrackingOrigin::Eye:
|
|
Origin = EHMDTrackingOrigin::Eye;
|
|
break;
|
|
case EHMDTrackingOrigin::Floor:
|
|
Origin = EHMDTrackingOrigin::Floor;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
GEngine->HMDDevice->SetTrackingOrigin(Origin);
|
|
}
|
|
}
|
|
|
|
TEnumAsByte<EHMDTrackingOrigin::Type> UHeadMountedDisplayFunctionLibrary::GetTrackingOrigin()
|
|
{
|
|
EHMDTrackingOrigin::Type Origin = EHMDTrackingOrigin::Eye;
|
|
|
|
if (GEngine->HMDDevice.IsValid())
|
|
{
|
|
Origin = GEngine->HMDDevice->GetTrackingOrigin();
|
|
}
|
|
|
|
return Origin;
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::GetVRFocusState(bool& bUseFocus, bool& bHasFocus)
|
|
{
|
|
if (GEngine->HMDDevice.IsValid())
|
|
{
|
|
bUseFocus = GEngine->HMDDevice->DoesAppUseVRFocus();
|
|
bHasFocus = GEngine->HMDDevice->DoesAppHaveVRFocus();
|
|
}
|
|
else
|
|
{
|
|
bUseFocus = bHasFocus = false;
|
|
}
|
|
}
|
|
|
|
namespace HMDFunctionLibraryHelpers
|
|
{
|
|
ISpectatorScreenController* GetSpectatorScreenController()
|
|
{
|
|
if (GEngine->HMDDevice.IsValid())
|
|
{
|
|
return GEngine->HMDDevice->GetSpectatorScreenController();
|
|
}
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
bool UHeadMountedDisplayFunctionLibrary::IsSpectatorScreenModeControllable()
|
|
{
|
|
return HMDFunctionLibraryHelpers::GetSpectatorScreenController() != nullptr;
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::SetSpectatorScreenMode(ESpectatorScreenMode Mode)
|
|
{
|
|
ISpectatorScreenController* const Controller = HMDFunctionLibraryHelpers::GetSpectatorScreenController();
|
|
if (Controller)
|
|
{
|
|
Controller->SetSpectatorScreenMode(Mode);
|
|
}
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::SetSpectatorScreenTexture(UTexture* InTexture)
|
|
{
|
|
ISpectatorScreenController* const Controller = HMDFunctionLibraryHelpers::GetSpectatorScreenController();
|
|
if (Controller)
|
|
{
|
|
if (!InTexture)
|
|
{
|
|
UE_LOG(LogHMD, Warning, TEXT("SetSpectatorScreenTexture blueprint function called with null Texture!"));
|
|
}
|
|
|
|
Controller->SetSpectatorScreenTexture(InTexture);
|
|
}
|
|
}
|
|
|
|
void UHeadMountedDisplayFunctionLibrary::SetSpectatorScreenModeTexturePlusEyeLayout(FVector2D EyeRectMin, FVector2D EyeRectMax, FVector2D TextureRectMin, FVector2D TextureRectMax, bool bDrawEyeFirst /* = true */, bool bClearBlack /* = false */)
|
|
{
|
|
ISpectatorScreenController* const Controller = HMDFunctionLibraryHelpers::GetSpectatorScreenController();
|
|
if (Controller)
|
|
{
|
|
Controller->SetSpectatorScreenModeTexturePlusEyeLayout(FSpectatorScreenModeTexturePlusEyeLayout(EyeRectMin, EyeRectMax, TextureRectMin, TextureRectMax, bDrawEyeFirst, bClearBlack));
|
|
}
|
|
}
|
|
|