Skip to content

Commit 892c6a2

Browse files
Merge branch 'feature/scaling' into '5.4'
Feature/scaling See merge request vr-vis/VR-Group/unreal-development/plugins/rwth-vr-toolkit!114
2 parents 1536d61 + b53030d commit 892c6a2

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

Source/RWTHVRToolkit/Private/Pawn/Navigation/CollisionHandlingMovement.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ void UCollisionHandlingMovement::SetCapsuleColliderToUserSize() const
189189
}
190190

191191
CapsuleColliderComponent->SetWorldRotation(FRotator::ZeroRotator);
192+
193+
// Counteract Pawn Scaling
194+
CapsuleColliderComponent->SetWorldScale3D(FVector::One());
192195
}
193196

194197
void UCollisionHandlingMovement::CheckAndRevertCollisionSinceLastTick()

Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,26 @@ ARWTHVRPawn::ARWTHVRPawn(const FObjectInitializer& ObjectInitializer) : Super(Ob
4141

4242
LeftHand = CreateDefaultSubobject<UReplicatedMotionControllerComponent>(TEXT("Left Hand MCC"));
4343
LeftHand->SetupAttachment(RootComponent);
44+
45+
UniformScale = GetActorScale3D().X;
46+
GetRootComponent()->TransformUpdated.AddLambda(
47+
[this](USceneComponent*, EUpdateTransformFlags, ETeleportType)
48+
{
49+
FVector CurrentScale = this->GetActorScale3D();
50+
if (CurrentScale.X != UniformScale || CurrentScale.Y != UniformScale || CurrentScale.Z != UniformScale)
51+
{
52+
UE_LOGFMT(Toolkit, Warning,
53+
"ARWTHVRPawn: Do not adjust the scale of the pawn directly. This will not work in VR. Use "
54+
"ARWTHVRPawn::SetScale(float) instead.");
55+
}
56+
});
57+
}
58+
59+
void ARWTHVRPawn::BeginPlay()
60+
{
61+
Super::BeginPlay();
62+
InitialWorldToMeters = GetWorldSettings()->WorldToMeters;
4463
}
45-
void ARWTHVRPawn::BeginPlay() { Super::BeginPlay(); }
4664

4765
void ARWTHVRPawn::Tick(float DeltaSeconds)
4866
{
@@ -56,6 +74,22 @@ void ARWTHVRPawn::Tick(float DeltaSeconds)
5674
EvaluateLivelink();
5775
}
5876

77+
/*
78+
* Scales the Pawn while also adjusting the WorldToMeters ratio to adjust for pupillary distance.
79+
* Only supports uniform scaling.
80+
*/
81+
void ARWTHVRPawn::SetScale(float NewScale)
82+
{
83+
FVector OldScale = GetActorScale();
84+
UniformScale = NewScale;
85+
FVector NewScaleVector = FVector(UniformScale, UniformScale, UniformScale);
86+
GetWorldSettings()->WorldToMeters = InitialWorldToMeters * UniformScale;
87+
SetActorRelativeScale3D(NewScaleVector);
88+
OnScaleChanged.Broadcast(OldScale, NewScale);
89+
}
90+
91+
float ARWTHVRPawn::GetScale() { return UniformScale; }
92+
5993
/*
6094
* The alternative would be to do this only on the server on possess and check for player state/type,
6195
* as connections now send their playertype over.
@@ -362,4 +396,4 @@ void ARWTHVRPawn::ApplyLiveLinkTransform(const FTransform& Transform,
362396
HeadCameraComponent->SetRelativeScale3D(Transform.GetScale3D());
363397
}
364398
}
365-
}
399+
}

Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class UCameraComponent;
1414
class UMotionControllerComponent;
1515
struct FLiveLinkTransformStaticData;
1616

17+
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnScaleChangedDelegate, FVector, OldScale, float, NewUniformScale);
18+
1719
/**
1820
* Pawn implementation with additional VR functionality, can be used in the Cave, with an HMD and on desktop.
1921
*/
@@ -31,6 +33,15 @@ class RWTHVRTOOLKIT_API ARWTHVRPawn : public APawn
3133

3234
virtual void NotifyControllerChanged() override;
3335

36+
UFUNCTION(BlueprintCallable)
37+
void SetScale(float NewScale);
38+
39+
UFUNCTION(BlueprintCallable)
40+
float GetScale();
41+
42+
UPROPERTY(BlueprintAssignable)
43+
FOnScaleChangedDelegate OnScaleChanged;
44+
3445
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Pawn|Input")
3546
TArray<UInputMappingContext*> InputMappingContexts;
3647

@@ -111,4 +122,6 @@ class RWTHVRTOOLKIT_API ARWTHVRPawn : public APawn
111122

112123
private:
113124
UInputComponent* ActivePlayerInputComponent;
125+
float InitialWorldToMeters;
126+
float UniformScale;
114127
};

0 commit comments

Comments
 (0)