Resizable VR Spectator view

This post is based on Unreal Engine 4.27, but I would be surprised if Unreal Engine 5 changed any of this.

By default the spectator window keeps the size it had when starting the game, ignoring any resizing done to that window afterwards. This also results in bugs where the mouse coordinate conversion functions are incorrect.

To fix this, you need to tell the game viewport size to be driven by the window. It is not clear to me why this isn’t default, but perhaps the stereo rendering was originally implemented by setting the window size to the HMD resolution, and maybe it was never updated.

First you need to find the spectator window. In a packaged build that’s simply*:

UGameEngine* GameEngine = Cast<UGameEngine>(GEngine);
OutInputWindow = GameEngine->GameViewportWindow;

Then you need to call SWindow::SetViewportSizeDrivenByWindow

TWeakPtr<SWindow> GameViewportWindow;
FindGameWindow(GameViewportWindow);
if (TSharedPtr<SWindow> Window = GameViewportWindow.Pin())
{
	Window->SetViewportSizeDrivenByWindow(true);
}

Note that you may need to delay this slightly after the start of the game, to make sure it takes.

*grabbing the game window in editor is slightly more tricky, so the most straight forward way to do so is to steal some code from VirtualCameraActor.cpp::FindSceneViewport, and edit out the bits about the viewport, which you don’t need.

Excuse me.
Let me ask you a question.

I would like to try the above content, but in which folder should I rewrite the file?

I tried searching under the folder after the package, but I couldn’t find it, so I’m asking a question.

This code can be used in C++ functions you make, such as C++ BlueprintFunctionLibraries

Luckily you don’t need to rewrite any files, it’s just that VirtualCameraActor.cpp (UE Engine Source Code Folder/Engine/Plugins/Experimental/VirtualCamera/Source/VirtualCamera/Private) contains a function that’s pretty close to what you need for the FindGameWindow function. So you can copy it to your own C++ BlueprintFunctionLibrary and modify it to strip out the bits you don’t need (the bits about OutSceneViewport)

In case you’re not familiar with C++ BlueprintFunctionLibraries, you can create one from the Unreal Editor, under File > New C++ Class > BlueprintFunctionLibrary (this requires scrolling down a bit). I believe it’s slightly different, but similar, in UE5. If you want, you could also do this as part of a plugin.

For example, in my project I made a blueprint node with the following signature for this:

UFUNCTION(BlueprintCallable, Category = "Window")
static void LinkViewportToWindowDimensions();

If all of this is still confusing, it might be helpful to learn a bit more about C++ BlueprintFunctionLibraries, or mixing C++ and blueprints in general.

Thank you for your reply!

I don’t know much about C++, but I’ll give it a try.