Project world coordinate to screen coordinate while in editor mode

Hey,

I want to project a world coordinate to a screen coordinate (preferred in pixels) while the engine is in the editor mode and the game is not running. So I can’t use the APlayerController method ProjectWorldLocationToScreen, because I don’t have a PlayerController.

I tried to use the Projection Matrix of the Camera View from the UCameraComponent, but the results are wrong.

FVector WorldToCamera(FVector WorldPoint, UCameraComponent* CameraComponent)
{
    FMinimalViewInfo CameraView;
    CameraComponent->GetCameraView(0, CameraView);
    return CameraView.CalculateProjectionMatrix().TransformFVector4(FVector4(WorldPoint, 1));
}

A workaround would be to start and stop the engine from within the c++ code (to generate a player controller), but I can’t find any way to achive that.

Thanks for reading and I hope you can help me :slight_smile:

Hi UltimativLunzer,

That method in PlayerController just calls this - maybe you can call that directly?

UGameplayStatics::ProjectWorldToScreen(this, WorldLocation, ScreenLocation, bPlayerViewportRelative);

Sadly it still needs a PlayerController. I will try to recreate the function based on the source code. Thanks for your help!

Hey there! any luck or insights in doing so? I exported the projection matrix from a cinematic camera actor, as well as the 3d coordinates to a file, so I can play around with it in python.
I then rendered the scene from the cinematic camera. In principle, I should be able to use the projection, and the exact 3d coordinates, to calculate the 2d location in the cinematic camera, but I never get the correct results :confused: were you able to solve this?

thanks!!
tobi

for editor please using editor camera instead player controller camera:

#if WITH_EDITOR
    FMinimalViewInfo CameraView;
    FLevelEditorViewportClient* client = (FLevelEditorViewportClient*)GEditor->GetActiveViewport()->GetClient();
	client->GetCameraComponentForView()->GetCameraView(0,CameraView);
#endif
2 Likes