Add 'Goto entity' support for the new Editor camera (#743)

* add find entity in viewport functionality to new camera

* fix AZ_CVAR usage

* updates following review feedback

- updated comment styles from /// to //!
- retrieve fov of camera (add test for fov access)

* update namespace naming, fix AZ_CVAR usage

* update missed namespace and use AZ::Transform::CreateLookAt

* add missing include

* move EditorViewportSettings to EditorLib

* update DLL import/export API and rename namespace usage
This commit is contained in:
Tom Hulton-Harrop
2021-05-17 18:21:38 +01:00
committed by GitHub
parent 1c1b34cf76
commit f4106fe73f
14 changed files with 233 additions and 104 deletions
@@ -74,7 +74,6 @@ namespace AzFramework
void SetCameraClippingVolumeFromPerspectiveFovMatrixRH(CameraState& cameraState, const AZ::Matrix4x4& clipFromView)
{
const float m11 = clipFromView(1, 1);
const float m22 = clipFromView(2, 2);
const float m23 = clipFromView(2, 3);
cameraState.m_nearClip = m23 / m22;
@@ -84,7 +83,12 @@ namespace AzFramework
{
AZStd::swap(cameraState.m_nearClip, cameraState.m_farClip);
}
cameraState.m_fovOrZoom = 2 * (AZ::Constants::HalfPi - atanf(m11));
cameraState.m_fovOrZoom = RetrieveFov(clipFromView);
}
float RetrieveFov(const AZ::Matrix4x4& clipFromView)
{
return 2.0f * (AZ::Constants::HalfPi - AZStd::atan(clipFromView(1, 1)));
}
void CameraState::Reflect(AZ::SerializeContext& serializeContext)
@@ -17,54 +17,57 @@
namespace AzFramework
{
/// Represents the camera state populated by the viewport camera.
//! Represents the camera state populated by the viewport camera.
struct CameraState
{
/// @cond
//! @cond
AZ_TYPE_INFO(CameraState, "{D309D934-044C-4BA8-91F1-EA3A45177A52}")
CameraState() = default;
/// @endcond
//! @endcond
static void Reflect(AZ::SerializeContext& context);
/// Return the vertical fov of the camera when the view is in perspective.
//! Return the vertical fov of the camera when the view is in perspective.
float VerticalFovRadian() const { return m_fovOrZoom; }
/// Return the zoom amount of the camera when the view is in orthographic.
//! Return the zoom amount of the camera when the view is in orthographic.
float Zoom() const { return m_fovOrZoom; }
AZ::Vector3 m_position = AZ::Vector3::CreateZero(); ///< World position of the camera.
AZ::Vector3 m_forward = AZ::Vector3::CreateAxisY(); ///< Forward look direction of the camera (world space).
AZ::Vector3 m_side = AZ::Vector3::CreateAxisX(); ///< Side vector of camera (orthogonal to forward and up).
AZ::Vector3 m_up = AZ::Vector3::CreateAxisZ(); ///< Up vector of the camera (cameras frame - world space).
AZ::Vector2 m_viewportSize = AZ::Vector2::CreateZero(); ///< Dimensions of the viewport.
float m_nearClip = 0.01f; ///< Near clip plane of the camera.
float m_farClip = 100.0f; ///< Far clip plane of the camera.
float m_fovOrZoom = 0.0f; ///< Fov or zoom of camera depending on if it is using orthographic projection or not.
bool m_orthographic = false; ///< Is the camera using orthographic projection or not.
AZ::Vector3 m_position = AZ::Vector3::CreateZero(); //!< World position of the camera.
AZ::Vector3 m_forward = AZ::Vector3::CreateAxisY(); //!< Forward look direction of the camera (world space).
AZ::Vector3 m_side = AZ::Vector3::CreateAxisX(); //!< Side vector of camera (orthogonal to forward and up).
AZ::Vector3 m_up = AZ::Vector3::CreateAxisZ(); //!< Up vector of the camera (cameras frame - world space).
AZ::Vector2 m_viewportSize = AZ::Vector2::CreateZero(); //!< Dimensions of the viewport.
float m_nearClip = 0.01f; //!< Near clip plane of the camera.
float m_farClip = 100.0f; //!< Far clip plane of the camera.
float m_fovOrZoom = 0.0f; //!< Fov or zoom of camera depending on if it is using orthographic projection or not.
bool m_orthographic = false; //!< Is the camera using orthographic projection or not.
};
/// Create a camera at the given transform with a specific viewport size.
/// @note The near/far clip planes and fov are sensible default values - please
/// use SetCameraClippingVolume to override them.
//! Create a camera at the given transform with a specific viewport size.
//! @note The near/far clip planes and fov are sensible default values - please
//! use SetCameraClippingVolume to override them.
CameraState CreateDefaultCamera(const AZ::Transform& transform, const AZ::Vector2& viewportSize);
/// Create a camera at the given position (no orientation) with a specific viewport size.
/// @note The near/far clip planes and fov are sensible default values - please
/// use SetCameraClippingVolume to override them.
//! Create a camera at the given position (no orientation) with a specific viewport size.
//! @note The near/far clip planes and fov are sensible default values - please
//! use SetCameraClippingVolume to override them.
CameraState CreateIdentityDefaultCamera(const AZ::Vector3& position, const AZ::Vector2& viewportSize);
/// Create a camera transformed by the given view to world matrix with a specific viewport size.
/// @note The near/far clip planes and fov are sensible default values - please
/// use SetCameraClippingVolume to override them.
//! Create a camera transformed by the given view to world matrix with a specific viewport size.
//! @note The near/far clip planes and fov are sensible default values - please
//! use SetCameraClippingVolume to override them.
CameraState CreateCameraFromWorldFromViewMatrix(const AZ::Matrix4x4& worldFromView, const AZ::Vector2& viewportSize);
/// Override the default near/far clipping planes and fov of the camera.
//! Override the default near/far clipping planes and fov of the camera.
void SetCameraClippingVolume(CameraState& cameraState, float nearPlane, float farPlane, float fovRad);
/// Override the default near/far clipping planes and fov of the camera by inferring them the specified right handed transform into clip space.
//! Override the default near/far clipping planes and fov of the camera by inferring them the specified right handed transform into clip space.
void SetCameraClippingVolumeFromPerspectiveFovMatrixRH(CameraState& cameraState, const AZ::Matrix4x4& clipFromView);
/// Set the transform for an existing camera.
//! Retrieve the field of view (Fov) from the perspective projection matrix (view space to clip space).
float RetrieveFov(const AZ::Matrix4x4& clipFromView);
//! Set the transform for an existing camera.
void SetCameraTransform(CameraState& cameraState, const AZ::Transform& transform);
} // namespace AzFramework
@@ -237,4 +237,23 @@ namespace UnitTest
EXPECT_THAT(cameraTransform, IsClose(cameraTransformFromView));
EXPECT_THAT(cameraView, IsClose(cameraViewFromTransform));
}
TEST(ViewportScreen, FovCanBeRetrievedFromProjectionMatrix)
{
using ::testing::FloatNear;
auto cameraState = AzFramework::CreateIdentityDefaultCamera(AZ::Vector3::CreateZero(), AZ::Vector2(800.0f, 600.0f));
{
const float fovRadians = AZ::DegToRad(45.0f);
AzFramework::SetCameraClippingVolume(cameraState, 0.1f, 100.0f, fovRadians);
EXPECT_THAT(AzFramework::RetrieveFov(AzFramework::CameraProjection(cameraState)), FloatNear(fovRadians, 0.001f));
}
{
const float fovRadians = AZ::DegToRad(90.0f);
AzFramework::SetCameraClippingVolume(cameraState, 0.1f, 100.0f, fovRadians);
EXPECT_THAT(AzFramework::RetrieveFov(AzFramework::CameraProjection(cameraState)), FloatNear(fovRadians, 0.001f));
}
}
} // namespace UnitTest