From fc4eda4ec188c733e2687a260bab8075adfc5d9f Mon Sep 17 00:00:00 2001 From: Kris Helwig <helwig@vr.rwth-aachen.de> Date: Fri, 7 Mar 2025 14:52:47 +0100 Subject: [PATCH 1/6] feat(pawn): Adds VR scaling functionality --- .../RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp | 59 +++++++++++++++---- Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h | 4 ++ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp index 812e83ea..4eb03b04 100644 --- a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp +++ b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp @@ -41,8 +41,30 @@ ARWTHVRPawn::ARWTHVRPawn(const FObjectInitializer& ObjectInitializer) : Super(Ob LeftHand = CreateDefaultSubobject<UReplicatedMotionControllerComponent>(TEXT("Left Hand MCC")); LeftHand->SetupAttachment(RootComponent); + + GetRootComponent()->TransformUpdated.AddLambda([this](USceneComponent*, EUpdateTransformFlags, ETeleportType) + { + FVector CurrentScale = this->GetActorScale3D(); + if (CurrentScale.X == CurrentScale.Y && CurrentScale.Y == CurrentScale.Z) + { + float expectedScale = GetWorldSettings()->WorldToMeters / InitialWorldToMeters; + float ErrorPrecision = 1E-05; + if (FMath::IsNearlyEqual(CurrentScale.X, expectedScale, ErrorPrecision)) + { + return; + } + } + UE_LOGFMT(Toolkit, Warning, + "ARWTHVRPawn: Do not adjust the scale of the pawn directly. This will not work in VR. Use ARWTHVRPawn::SetScale(float) instead.") + ; + }); +} + +void ARWTHVRPawn::BeginPlay() +{ + Super::BeginPlay(); + InitialWorldToMeters = GetWorldSettings()->WorldToMeters; } -void ARWTHVRPawn::BeginPlay() { Super::BeginPlay(); } void ARWTHVRPawn::Tick(float DeltaSeconds) { @@ -56,6 +78,17 @@ void ARWTHVRPawn::Tick(float DeltaSeconds) EvaluateLivelink(); } +/* + * Scales the Pawn while also adjusting the WorldToMeters ratio to adjust for pupillary distance. + * Only supports uniform scaling. + */ +void ARWTHVRPawn::SetScale(float NewScale) +{ + FVector NewScaleVector = FVector(NewScale, NewScale, NewScale); + GetWorldSettings()->WorldToMeters = InitialWorldToMeters * NewScale; + SetActorScale3D(NewScaleVector); +} + /* * The alternative would be to do this only on the server on possess and check for player state/type, * as connections now send their playertype over. @@ -71,7 +104,7 @@ void ARWTHVRPawn::NotifyControllerChanged() if (HasAuthority()) { UE_LOG(Toolkit, Display, - TEXT("ARWTHVRPawn: Player Controller has changed, trying to change Cluster attachment if possible...")); + TEXT("ARWTHVRPawn: Player Controller has changed, trying to change Cluster attachment if possible...")); if (const ARWTHVRPlayerState* State = GetPlayerState<ARWTHVRPlayerState>()) { const EPlayerType Type = State->GetPlayerType(); @@ -101,7 +134,7 @@ void ARWTHVRPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponen } UE_LOGFMT(Toolkit, Display, "SetupPlayerInputComponent: Player Controller is valid, setting up input for {Pawn}", - GetName()); + GetName()); // Set the control rotation of the PC to zero again. There is a small period of 2 frames where, when the pawn gets @@ -173,7 +206,7 @@ void ARWTHVRPawn::AddInputMappingContext(const APlayerController* PC, const UInp else { UE_LOGFMT(Toolkit, Warning, - "ARWTHVRPawn::AddInputMappingContext: UEnhancedInputLocalPlayerSubsystem is nullptr!"); + "ARWTHVRPawn::AddInputMappingContext: UEnhancedInputLocalPlayerSubsystem is nullptr!"); } } else @@ -201,7 +234,7 @@ void ARWTHVRPawn::EvaluateLivelink() const IModularFeatures::Get().GetModularFeature<ILiveLinkClient>(ILiveLinkClient::ModularFeatureName); FLiveLinkSubjectFrameData SubjectData; const bool bHasValidData = LiveLinkClient.EvaluateFrame_AnyThread(HeadSubjectRepresentation.Subject, - HeadSubjectRepresentation.Role, SubjectData); + HeadSubjectRepresentation.Role, SubjectData); if (!bHasValidData) { @@ -278,12 +311,12 @@ void ARWTHVRPawn::AttachClustertoPawn() bool bAttached = ClusterActor->AttachToComponent(GetRootComponent(), AttachmentRules); // State->GetCorrespondingClusterActor()->OnAttached(); UE_LOGFMT(Toolkit, Display, - "ARWTHVRPawn: Attaching corresponding cluster actor to our pawn returned: {Attached}", bAttached); + "ARWTHVRPawn: Attaching corresponding cluster actor to our pawn returned: {Attached}", bAttached); } else { UE_LOGFMT(Toolkit, Error, - "ARWTHVRPawn::AttachClustertoPawn: No ARWTHVRPlayerState set! This won't work on the Cave."); + "ARWTHVRPawn::AttachClustertoPawn: No ARWTHVRPlayerState set! This won't work on the Cave."); } if (HasAuthority()) // Should always be the case here, but double check @@ -321,19 +354,19 @@ void ARWTHVRPawn::SetCameraOffset() const } void ARWTHVRPawn::ApplyLiveLinkTransform(const FTransform& Transform, - const FLiveLinkTransformStaticData& StaticData) const + const FLiveLinkTransformStaticData& StaticData) const { if (StaticData.bIsLocationSupported) { if (bWorldTransform) { HeadCameraComponent->SetWorldLocation(Transform.GetLocation(), false, nullptr, - ETeleportType::TeleportPhysics); + ETeleportType::TeleportPhysics); } else { HeadCameraComponent->SetRelativeLocation(Transform.GetLocation(), false, nullptr, - ETeleportType::TeleportPhysics); + ETeleportType::TeleportPhysics); } } @@ -342,12 +375,12 @@ void ARWTHVRPawn::ApplyLiveLinkTransform(const FTransform& Transform, if (bWorldTransform) { HeadCameraComponent->SetWorldRotation(Transform.GetRotation(), false, nullptr, - ETeleportType::TeleportPhysics); + ETeleportType::TeleportPhysics); } else { HeadCameraComponent->SetRelativeRotation(Transform.GetRotation(), false, nullptr, - ETeleportType::TeleportPhysics); + ETeleportType::TeleportPhysics); } } @@ -362,4 +395,4 @@ void ARWTHVRPawn::ApplyLiveLinkTransform(const FTransform& Transform, HeadCameraComponent->SetRelativeScale3D(Transform.GetScale3D()); } } -} +} \ No newline at end of file diff --git a/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h b/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h index cd81e497..eaf24745 100644 --- a/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h +++ b/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h @@ -31,6 +31,9 @@ public: virtual void NotifyControllerChanged() override; + UFUNCTION(BlueprintCallable) + void SetScale(float NewScale); + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Pawn|Input") TArray<UInputMappingContext*> InputMappingContexts; @@ -111,4 +114,5 @@ protected: private: UInputComponent* ActivePlayerInputComponent; + float InitialWorldToMeters; }; -- GitLab From 9b1eb745890b8e34f447ecc96eac826efbd57d7d Mon Sep 17 00:00:00 2001 From: Kris Helwig <helwig@vr.rwth-aachen.de> Date: Tue, 11 Mar 2025 15:42:58 +0100 Subject: [PATCH 2/6] feat(pawn): Adds OnScaleChanged callback --- .../RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp | 29 ++++++++++--------- Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h | 13 +++++++-- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp index 4eb03b04..7e32a90d 100644 --- a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp +++ b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp @@ -42,21 +42,16 @@ ARWTHVRPawn::ARWTHVRPawn(const FObjectInitializer& ObjectInitializer) : Super(Ob LeftHand = CreateDefaultSubobject<UReplicatedMotionControllerComponent>(TEXT("Left Hand MCC")); LeftHand->SetupAttachment(RootComponent); + UniformScale = GetActorScale3D().X; GetRootComponent()->TransformUpdated.AddLambda([this](USceneComponent*, EUpdateTransformFlags, ETeleportType) { FVector CurrentScale = this->GetActorScale3D(); - if (CurrentScale.X == CurrentScale.Y && CurrentScale.Y == CurrentScale.Z) + if (CurrentScale.X != UniformScale || CurrentScale.Y != UniformScale || CurrentScale.Z != UniformScale) { - float expectedScale = GetWorldSettings()->WorldToMeters / InitialWorldToMeters; - float ErrorPrecision = 1E-05; - if (FMath::IsNearlyEqual(CurrentScale.X, expectedScale, ErrorPrecision)) - { - return; - } + UE_LOGFMT(Toolkit, Warning, + "ARWTHVRPawn: Do not adjust the scale of the pawn directly. This will not work in VR. Use ARWTHVRPawn::SetScale(float) instead.") + ; } - UE_LOGFMT(Toolkit, Warning, - "ARWTHVRPawn: Do not adjust the scale of the pawn directly. This will not work in VR. Use ARWTHVRPawn::SetScale(float) instead.") - ; }); } @@ -84,9 +79,17 @@ void ARWTHVRPawn::Tick(float DeltaSeconds) */ void ARWTHVRPawn::SetScale(float NewScale) { - FVector NewScaleVector = FVector(NewScale, NewScale, NewScale); - GetWorldSettings()->WorldToMeters = InitialWorldToMeters * NewScale; - SetActorScale3D(NewScaleVector); + FVector OldScale = GetActorScale(); + UniformScale = NewScale; + FVector NewScaleVector = FVector(UniformScale, UniformScale, UniformScale); + GetWorldSettings()->WorldToMeters = InitialWorldToMeters * UniformScale; + SetActorRelativeScale3D(NewScaleVector); + OnScaleChanged.Broadcast(OldScale, NewScale); +} + +float ARWTHVRPawn::GetScale() +{ + return UniformScale; } /* diff --git a/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h b/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h index eaf24745..bad99461 100644 --- a/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h +++ b/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h @@ -14,6 +14,8 @@ class UCameraComponent; class UMotionControllerComponent; struct FLiveLinkTransformStaticData; +DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnScaleChangedDelegate, FVector, OldScale, float, NewUniformScale); + /** * Pawn implementation with additional VR functionality, can be used in the Cave, with an HMD and on desktop. */ @@ -30,10 +32,16 @@ public: virtual void Tick(float DeltaSeconds) override; virtual void NotifyControllerChanged() override; - + UFUNCTION(BlueprintCallable) void SetScale(float NewScale); - + + UFUNCTION(BlueprintCallable) + float GetScale(); + + UPROPERTY(BlueprintAssignable) + FOnScaleChangedDelegate OnScaleChanged; + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Pawn|Input") TArray<UInputMappingContext*> InputMappingContexts; @@ -115,4 +123,5 @@ protected: private: UInputComponent* ActivePlayerInputComponent; float InitialWorldToMeters; + float UniformScale; }; -- GitLab From e3e150ba6f616a0521ec7863450b392438dd995e Mon Sep 17 00:00:00 2001 From: Kris Helwig <helwig@vr.rwth-aachen.de> Date: Mon, 17 Mar 2025 15:20:17 +0100 Subject: [PATCH 3/6] fix(movement): Scales the CollisionHandlingMovement capsule to counteract pawn scaling --- .../Private/Pawn/Navigation/CollisionHandlingMovement.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/RWTHVRToolkit/Private/Pawn/Navigation/CollisionHandlingMovement.cpp b/Source/RWTHVRToolkit/Private/Pawn/Navigation/CollisionHandlingMovement.cpp index 62bfa460..0effb261 100644 --- a/Source/RWTHVRToolkit/Private/Pawn/Navigation/CollisionHandlingMovement.cpp +++ b/Source/RWTHVRToolkit/Private/Pawn/Navigation/CollisionHandlingMovement.cpp @@ -3,6 +3,7 @@ #include "Kismet/KismetSystemLibrary.h" #include "Logging/StructuredLog.h" #include "Utility/RWTHVRUtilities.h" +#include "DrawDebugHelpers.h" UCollisionHandlingMovement::UCollisionHandlingMovement(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) @@ -189,6 +190,9 @@ void UCollisionHandlingMovement::SetCapsuleColliderToUserSize() const } CapsuleColliderComponent->SetWorldRotation(FRotator::ZeroRotator); + + // Counteract Pawn Scaling + CapsuleColliderComponent->SetWorldScale3D(FVector::One()); } void UCollisionHandlingMovement::CheckAndRevertCollisionSinceLastTick() -- GitLab From 5fff5b7ee76a2026588cec9439674a21de7840e3 Mon Sep 17 00:00:00 2001 From: Kris Helwig <helwig@vr.rwth-aachen.de> Date: Mon, 17 Mar 2025 15:42:51 +0100 Subject: [PATCH 4/6] style(pawn): fixes clang format in RWTHVRPawn.cpp and RWTHVRPawn.h --- .../RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp | 46 +++++++++---------- Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h | 2 +- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp index 7e32a90d..f773fa9f 100644 --- a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp +++ b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp @@ -43,16 +43,17 @@ ARWTHVRPawn::ARWTHVRPawn(const FObjectInitializer& ObjectInitializer) : Super(Ob LeftHand->SetupAttachment(RootComponent); UniformScale = GetActorScale3D().X; - GetRootComponent()->TransformUpdated.AddLambda([this](USceneComponent*, EUpdateTransformFlags, ETeleportType) - { - FVector CurrentScale = this->GetActorScale3D(); - if (CurrentScale.X != UniformScale || CurrentScale.Y != UniformScale || CurrentScale.Z != UniformScale) + GetRootComponent()->TransformUpdated.AddLambda( + [this](USceneComponent*, EUpdateTransformFlags, ETeleportType) { - UE_LOGFMT(Toolkit, Warning, - "ARWTHVRPawn: Do not adjust the scale of the pawn directly. This will not work in VR. Use ARWTHVRPawn::SetScale(float) instead.") - ; - } - }); + FVector CurrentScale = this->GetActorScale3D(); + if (CurrentScale.X != UniformScale || CurrentScale.Y != UniformScale || CurrentScale.Z != UniformScale) + { + UE_LOGFMT(Toolkit, Warning, + "ARWTHVRPawn: Do not adjust the scale of the pawn directly. This will not work in VR. Use " + "ARWTHVRPawn::SetScale(float) instead."); + } + }); } void ARWTHVRPawn::BeginPlay() @@ -87,10 +88,7 @@ void ARWTHVRPawn::SetScale(float NewScale) OnScaleChanged.Broadcast(OldScale, NewScale); } -float ARWTHVRPawn::GetScale() -{ - return UniformScale; -} +float ARWTHVRPawn::GetScale() { return UniformScale; } /* * The alternative would be to do this only on the server on possess and check for player state/type, @@ -107,7 +105,7 @@ void ARWTHVRPawn::NotifyControllerChanged() if (HasAuthority()) { UE_LOG(Toolkit, Display, - TEXT("ARWTHVRPawn: Player Controller has changed, trying to change Cluster attachment if possible...")); + TEXT("ARWTHVRPawn: Player Controller has changed, trying to change Cluster attachment if possible...")); if (const ARWTHVRPlayerState* State = GetPlayerState<ARWTHVRPlayerState>()) { const EPlayerType Type = State->GetPlayerType(); @@ -137,7 +135,7 @@ void ARWTHVRPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponen } UE_LOGFMT(Toolkit, Display, "SetupPlayerInputComponent: Player Controller is valid, setting up input for {Pawn}", - GetName()); + GetName()); // Set the control rotation of the PC to zero again. There is a small period of 2 frames where, when the pawn gets @@ -209,7 +207,7 @@ void ARWTHVRPawn::AddInputMappingContext(const APlayerController* PC, const UInp else { UE_LOGFMT(Toolkit, Warning, - "ARWTHVRPawn::AddInputMappingContext: UEnhancedInputLocalPlayerSubsystem is nullptr!"); + "ARWTHVRPawn::AddInputMappingContext: UEnhancedInputLocalPlayerSubsystem is nullptr!"); } } else @@ -237,7 +235,7 @@ void ARWTHVRPawn::EvaluateLivelink() const IModularFeatures::Get().GetModularFeature<ILiveLinkClient>(ILiveLinkClient::ModularFeatureName); FLiveLinkSubjectFrameData SubjectData; const bool bHasValidData = LiveLinkClient.EvaluateFrame_AnyThread(HeadSubjectRepresentation.Subject, - HeadSubjectRepresentation.Role, SubjectData); + HeadSubjectRepresentation.Role, SubjectData); if (!bHasValidData) { @@ -314,12 +312,12 @@ void ARWTHVRPawn::AttachClustertoPawn() bool bAttached = ClusterActor->AttachToComponent(GetRootComponent(), AttachmentRules); // State->GetCorrespondingClusterActor()->OnAttached(); UE_LOGFMT(Toolkit, Display, - "ARWTHVRPawn: Attaching corresponding cluster actor to our pawn returned: {Attached}", bAttached); + "ARWTHVRPawn: Attaching corresponding cluster actor to our pawn returned: {Attached}", bAttached); } else { UE_LOGFMT(Toolkit, Error, - "ARWTHVRPawn::AttachClustertoPawn: No ARWTHVRPlayerState set! This won't work on the Cave."); + "ARWTHVRPawn::AttachClustertoPawn: No ARWTHVRPlayerState set! This won't work on the Cave."); } if (HasAuthority()) // Should always be the case here, but double check @@ -357,19 +355,19 @@ void ARWTHVRPawn::SetCameraOffset() const } void ARWTHVRPawn::ApplyLiveLinkTransform(const FTransform& Transform, - const FLiveLinkTransformStaticData& StaticData) const + const FLiveLinkTransformStaticData& StaticData) const { if (StaticData.bIsLocationSupported) { if (bWorldTransform) { HeadCameraComponent->SetWorldLocation(Transform.GetLocation(), false, nullptr, - ETeleportType::TeleportPhysics); + ETeleportType::TeleportPhysics); } else { HeadCameraComponent->SetRelativeLocation(Transform.GetLocation(), false, nullptr, - ETeleportType::TeleportPhysics); + ETeleportType::TeleportPhysics); } } @@ -378,12 +376,12 @@ void ARWTHVRPawn::ApplyLiveLinkTransform(const FTransform& Transform, if (bWorldTransform) { HeadCameraComponent->SetWorldRotation(Transform.GetRotation(), false, nullptr, - ETeleportType::TeleportPhysics); + ETeleportType::TeleportPhysics); } else { HeadCameraComponent->SetRelativeRotation(Transform.GetRotation(), false, nullptr, - ETeleportType::TeleportPhysics); + ETeleportType::TeleportPhysics); } } diff --git a/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h b/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h index bad99461..40eb174f 100644 --- a/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h +++ b/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h @@ -32,7 +32,7 @@ public: virtual void Tick(float DeltaSeconds) override; virtual void NotifyControllerChanged() override; - + UFUNCTION(BlueprintCallable) void SetScale(float NewScale); -- GitLab From 4e8d0770cd8145ca9e682e630798dfde0b36f9e1 Mon Sep 17 00:00:00 2001 From: Kris Helwig <helwig@vr.rwth-aachen.de> Date: Mon, 17 Mar 2025 15:45:30 +0100 Subject: [PATCH 5/6] style(pawn): fixes clang format in RWTHVRPawn.cpp and RWTHVRPawn.h again --- Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp index f773fa9f..dc9c9022 100644 --- a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp +++ b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp @@ -88,7 +88,7 @@ void ARWTHVRPawn::SetScale(float NewScale) OnScaleChanged.Broadcast(OldScale, NewScale); } -float ARWTHVRPawn::GetScale() { return UniformScale; } +float ARWTHVRPawn::GetScale() { return UniformScale; } /* * The alternative would be to do this only on the server on possess and check for player state/type, -- GitLab From b53030de656fc241154cd842753d02b7f2f16ff2 Mon Sep 17 00:00:00 2001 From: David Gilbert <gilbert@vr.rwth-aachen.de> Date: Thu, 20 Mar 2025 15:50:31 +0100 Subject: [PATCH 6/6] style(movement): removes unnecessary include --- .../Private/Pawn/Navigation/CollisionHandlingMovement.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/RWTHVRToolkit/Private/Pawn/Navigation/CollisionHandlingMovement.cpp b/Source/RWTHVRToolkit/Private/Pawn/Navigation/CollisionHandlingMovement.cpp index 0effb261..912fe0c1 100644 --- a/Source/RWTHVRToolkit/Private/Pawn/Navigation/CollisionHandlingMovement.cpp +++ b/Source/RWTHVRToolkit/Private/Pawn/Navigation/CollisionHandlingMovement.cpp @@ -3,7 +3,6 @@ #include "Kismet/KismetSystemLibrary.h" #include "Logging/StructuredLog.h" #include "Utility/RWTHVRUtilities.h" -#include "DrawDebugHelpers.h" UCollisionHandlingMovement::UCollisionHandlingMovement(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) -- GitLab