4#include "Net/UnrealNetwork.h"
5#include "Engine/Engine.h"
7#include "IXRTrackingSystem.h"
9#include "Rendering/MotionVectorSimulation.h"
11#include "IHeadMountedDisplay.h"
19 if (!XRSystem->IsHeadTrackingAllowed())
24 if (World->WorldType != EWorldType::PIE)
30 const int32 MyPIEInstanceID = World->GetOutermost()->PIEInstanceID;
31 for (
const FWorldContext& WorldContext : GEngine->GetWorldContexts())
33 if (WorldContext.WorldType == EWorldType::PIE && WorldContext.RunAsDedicated ==
false && WorldContext.World())
35 return WorldContext.World()->GetOutermost()->PIEInstanceID == MyPIEInstanceID;
40 return XRSystem->IsHeadTrackingAllowedForWorld(*World);
45 : Super(ObjectInitializer)
47 PrimaryComponentTick.bCanEverTick =
true;
48 PrimaryComponentTick.bStartWithTickEnabled =
true;
51 SetIsReplicatedByDefault(
true);
52 SetRelativeScale3D(FVector(1.0f, 1.0f, 1.0f));
59 bUsePawnControlRotation =
false;
90void UReplicatedVRCameraComponent::GetLifetimeReplicatedProps(TArray< class FLifetimeProperty > & OutLifetimeProps)
const
96 Super::GetLifetimeReplicatedProps(OutLifetimeProps);
120void UReplicatedVRCameraComponent::Server_SendCameraTransform_Implementation(
FBPVRComponentPosRep NewTransform)
132bool UReplicatedVRCameraComponent::Server_SendCameraTransform_Validate(
FBPVRComponentPosRep NewTransform)
158 if (
AVRBaseCharacter* CharacterOwner = Cast<AVRBaseCharacter>(this->GetOwner()))
167 Super::OnAttachmentChanged();
179 OriginalPosition.X = 0;
180 OriginalPosition.Y = 0;
213 if (
bSetPositionDuringTick && bLockToHmd && GEngine->XRSystem.IsValid() && GEngine->XRSystem->IsHeadTrackingAllowedForWorld(*GetWorld()))
218 if (GEngine->XRSystem->GetCurrentPose(IXRTrackingSystem::HMDDeviceId, Orientation, Position))
225 SetRelativeTransform(FTransform(Orientation, Position));
269 SetRelativeLocationAndRotation(
286 FTransform NA = FTransform(GetRelativeRotation(), GetRelativeLocation(), FVector(1.0f));
288 NA.NormalizeRotation();
289 NB.NormalizeRotation();
291 NA.Blend(NA, NB, Alpha);
294 if (NA.EqualsNoScale(NB))
300 SetRelativeLocationAndRotation(NA.GetTranslation(), NA.Rotator());
309 Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
319 if (!CharMove || !CharMove->IsComponentTickEnabled() || !CharMove->IsActive() || (!CharMove->PrimaryComponentTick.bTickEvenWhenPaused && GetWorld()->IsPaused()))
329 if (this->GetIsReplicated())
331 FRotator RelativeRot = GetRelativeRotation();
332 FVector RelativeLoc = GetRelativeLocation();
346 if (GetNetMode() == NM_Client)
372 if (bIsLocallyControlled)
378 if (bIsLocallyControlled && GEngine && GEngine->XRSystem.IsValid() && GetWorld() && GetWorld()->WorldType != EWorldType::Editor)
380 IXRTrackingSystem* XRSystem = GEngine->XRSystem.Get();
381 auto XRCamera = XRSystem->GetXRCamera();
383 if (XRCamera.IsValid())
388 const FTransform ParentWorld = CalcNewComponentToWorld(FTransform());
389 XRCamera->SetupLateUpdate(ParentWorld,
this, bLockToHmd == 0);
395 if (XRCamera->UpdatePlayerCamera(Orientation, Position))
402 SetRelativeTransform(FTransform(Orientation, Position));
406 SetRelativeScale3D(FVector(1.0f));
414 XRCamera->OverrideFOV(this->FieldOfView);
419 if (bUsePawnControlRotation)
421 const APawn* OwningPawn = Cast<APawn>(GetOwner());
422 const AController* OwningController = OwningPawn ? OwningPawn->GetController() :
nullptr;
423 if (OwningController && OwningController->IsLocalPlayerController())
425 const FRotator PawnViewRotation = OwningPawn->GetViewRotation();
426 if (!PawnViewRotation.Equals(GetComponentRotation()))
428 SetWorldRotation(PawnViewRotation);
433 if (bUseAdditiveOffset)
435 FTransform OffsetCamToBaseCam = AdditiveOffset;
436 FTransform BaseCamToWorld = GetComponentToWorld();
437 FTransform OffsetCamToWorld = OffsetCamToBaseCam * BaseCamToWorld;
439 DesiredView.Location = OffsetCamToWorld.GetLocation();
440 DesiredView.Rotation = OffsetCamToWorld.Rotator();
444 DesiredView.Location = GetComponentLocation();
445 DesiredView.Rotation = GetComponentRotation();
448 DesiredView.FOV = bUseAdditiveOffset ? (FieldOfView + AdditiveFOVOffset) : FieldOfView;
449 DesiredView.AspectRatio = AspectRatio;
450 DesiredView.bConstrainAspectRatio = bConstrainAspectRatio;
451 DesiredView.bUseFieldOfViewForLOD = bUseFieldOfViewForLOD;
452 DesiredView.ProjectionMode = ProjectionMode;
453 DesiredView.OrthoWidth = OrthoWidth;
454 DesiredView.OrthoNearClipPlane = OrthoNearClipPlane;
455 DesiredView.OrthoFarClipPlane = OrthoFarClipPlane;
458 DesiredView.PostProcessBlendWeight = PostProcessBlendWeight;
459 if (PostProcessBlendWeight > 0.0f)
461 DesiredView.PostProcessSettings = PostProcessSettings;
465 DesiredView.PreviousViewTransform = FMotionVectorSimulation::Get().GetPreviousTransform(
this);
488 float NewDistance = OldToNewVector.SizeSquared();
bool TMP_IsHeadTrackingAllowedForWorld(IXRTrackingSystem *XRSystem, UWorld *World)
UCLASS(Blueprintable, meta = (BlueprintSpawnableComponent), ClassGroup = VRExpansionLibrary)
bool bSampleVelocityInWorldSpace
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|ComponentVelocity")
void UpdateTracking(float DeltaTime)
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override
FTransform LastRelativePosition
float NetworkMaxSmoothUpdateDistance
UPROPERTY(EditAnywhere, Category = "GripMotionController|Networking|Smoothing", meta = (editcondition...
bool bScaleTracking
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|Advanced|Tracking")
float NetworkNoSmoothUpdateDistance
UPROPERTY(EditAnywhere, Category = "GripMotionController|Networking|Smoothing", meta = (editcondition...
bool IsLocallyControlled() const
bool bOffsetByHMD
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera")
bool bLimitMaxHeight
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|Advanced|Tracking")
uint32 bAutoSetLockToHmd
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|Advanced|Tracking")
TWeakObjectPtr< AVRBaseCharacter > AttachChar
UPROPERTY()
FVector TrackingScaler
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|Advanced|Tracking",...
float NetUpdateRate
UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = "ReplicatedCamera|Networking")
virtual void OnAttachmentChanged() override
FVector LastUpdatesRelativePosition
FBPVRComponentPosRep ReplicatedCameraTransform
UPROPERTY(EditDefaultsOnly, ReplicatedUsing = OnRep_ReplicatedCameraTransform, Category = "Replicated...
bool bSetPositionDuringTick
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera")
VRBaseCharTransformRPC_Pointer OverrideSendTransform
UReplicatedVRCameraComponent(const FObjectInitializer &ObjectInitializer)
float MaximumTrackedBounds
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|Advanced|Tracking",...
float MinimumHeightAllowed
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|Advanced|Tracking",...
bool bUpdateInCharacterMovement
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VRExpansionLibrary")
virtual void OnRep_ReplicatedCameraTransform()
UFUNCTION()
float MaxHeightAllowed
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|Advanced|Tracking",...
virtual void GetCameraView(float DeltaTime, FMinimalViewInfo &DesiredView) override
void ApplyTrackingParameters(FVector &OriginalPosition)
bool bHadValidFirstVelocity
void Server_SendCameraTransform(FBPVRComponentPosRep NewTransform)
UFUNCTION(Unreliable, Server, WithValidation)
bool bSmoothReplicatedMotion
UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category = "ReplicatedCamera|Networking")
FRotator LastUpdatesRelativeRotation
bool HasTrackingParameters()
void RunNetworkedSmoothing(float DeltaTime)
bool bLimitMinHeight
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|Advanced|Tracking")
float InterpolationSpeed
UPROPERTY(EditAnywhere, Category = "GripMotionController|Networking|Smoothing", meta = (editcondition...
bool bUseExponentialSmoothing
UPROPERTY(EditAnywhere, Category = "GripMotionController|Networking|Smoothing", meta = (editcondition...
bool bLimitBounds
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ReplicatedCamera|Advanced|Tracking")
FVector Position
UPROPERTY(Transient)
FRotator Rotation
UPROPERTY(Transient)