Get screen aspect ratio

Hi,

How can I get viewport aspect ratio from Lua or C++ code? I’ve tried:


AzToolsFramework::ViewportInteraction::CameraState result;
AzToolsFramework::ViewportInteractionRequestBus::BroadcastResult(result, &AzToolsFramework::ViewportInteractionRequests::GetCameraState);
return result.m_viewportSize.GetX() / result.m_viewportSize.GetY();

but it only works for editor. I’ve also tried:


AZ::EntityId cameraId;
float width;
float height;
Camera::CameraSystemRequestBus::BroadcastResult(cameraId,
&Camera::CameraSystemRequests::GetActiveCamera);
Camera::CameraRequestBus::EventResult(width, cameraId, &Camera::CameraComponentRequests::GetFrustumWidth);
Camera::CameraRequestBus::EventResult(height, cameraId, &Camera::CameraComponentRequests::GetFrustumHeight);
return width / height;

but it always returns 1 (regardless of actual size).

Thank you for advice,

Sasha

Hi @Sasha,

There are a few different ways to do this depending on your context.

The way that the LyShine UI system gets the viewport width and height is using:

gEnv->pRenderer->GetOverlayWidth() and gEnv->pRenderer->GetOverlayHeight()

If you have a UI canvas already you can call GetCanvasSize on the canvas, this will give you the viewport size.

Hope that helps,

Rob

Thank you, works perfectly.