Skip to content

Instantly share code, notes, and snippets.

@sfider
Last active July 18, 2021 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sfider/4eff650a4d325918940a9c75e6eeb0a7 to your computer and use it in GitHub Desktop.
Save sfider/4eff650a4d325918940a9c75e6eeb0a7 to your computer and use it in GitHub Desktop.
UE4 Pixel Perfect GameViewportClient
#include "DataGameViewportClient.h"
#include "Engine.h"
#include "Engine/LocalPlayer.h"
#include "SceneView.h"
constexpr int RENDER_WIDTH = 320;
constexpr int RENDER_HEIGHT = 180;
UDataGameViewportClient::UDataGameViewportClient(FVTableHelper& Helper)
: Super(Helper)
{
}
void UDataGameViewportClient::LayoutPlayers()
{
ULocalPlayer* LocalPlayer = GetMainPlayer();
if (LocalPlayer && Viewport)
{
FIntPoint ViewportSize = Viewport->GetSizeXY();
int RenderScale = FMath::Min(ViewportSize.X / RENDER_WIDTH, ViewportSize.Y / RENDER_HEIGHT);
FIntPoint RenderSize = FIntPoint(RENDER_WIDTH * RenderScale, RENDER_HEIGHT * RenderScale);
LocalPlayer->Size.X = float(RenderSize.X) / float(ViewportSize.X);
LocalPlayer->Size.Y = float(RenderSize.Y) / float(ViewportSize.Y);
LocalPlayer->Origin = (FVector2D(1.0f, 1.0f) - LocalPlayer->Size) * 0.5f;
}
}
void UDataGameViewportClient::FinalizeViews(class FSceneViewFamily* ViewFamily, const TMap<ULocalPlayer*, FSceneView*>& PlayerViewMap)
{
Super::FinalizeViews(ViewFamily, PlayerViewMap);
ULocalPlayer* LocalPlayer = GetMainPlayer();
if (FSceneView* SceneView = PlayerViewMap[LocalPlayer])
{
if (Viewport)
{
FIntPoint ViewportSize = Viewport->GetSizeXY();
int RenderScale = FMath::Min(ViewportSize.X / RENDER_WIDTH, ViewportSize.Y / RENDER_HEIGHT);
SceneView->FinalPostProcessSettings.ScreenPercentage = 100.0f / RenderScale;
}
}
}
ULocalPlayer* UDataGameViewportClient::GetMainPlayer()
{
const TArray<ULocalPlayer*>& PlayerList = GetOuterUEngine()->GetGamePlayers(this);
if (PlayerList.Num() > 0)
{
return PlayerList[0];
}
return nullptr;
}
#pragma once
#include "Engine/GameViewportClient.h"
#include "DataGameViewportClient.generated.h"
UCLASS()
class UDataGameViewportClient : public UGameViewportClient
{
GENERATED_BODY()
public:
UDataGameViewportClient(FVTableHelper& Helper);
virtual void LayoutPlayers() override;
virtual void FinalizeViews(class FSceneViewFamily* ViewFamily, const TMap<ULocalPlayer*, FSceneView*>& PlayerViewMap) override;
private:
ULocalPlayer* GetMainPlayer();
};
[PostProcessQuality@0]
r.Upscale.Quality=0
[PostProcessQuality@1]
r.Upscale.Quality=0
[PostProcessQuality@2]
r.Upscale.Quality=0
[PostProcessQuality@3]
r.Upscale.Quality=0
[PostProcessQuality@Cine]
r.Upscale.Quality=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment