EditorViewportCamera.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Math/Quaternion.h>
  10. #include <AzCore/Math/Transform.h>
  11. #include <AzCore/Math/Vector3.h>
  12. #include <AzFramework/Viewport/ViewportId.h>
  13. #include <SandboxAPI.h>
  14. namespace SandboxEditor
  15. {
  16. //! Sets the specified viewport camera translation/position.
  17. //! @param viewportId The id of the viewport to modify.
  18. //! @param position The new position of the camera in world space.
  19. SANDBOX_API void SetViewportCameraPosition(AzFramework::ViewportId viewportId, const AZ::Vector3& position);
  20. //! Sets the default viewport camera translation/position.
  21. //! @param position The new position of the camera in world space.
  22. SANDBOX_API void SetDefaultViewportCameraPosition(const AZ::Vector3& position);
  23. //! Sets the specified viewport camera orientation/rotation.
  24. //! @param pitch Amount of pitch in radians.
  25. //! @param yaw Amount of yaw in radians.
  26. SANDBOX_API void SetViewportCameraRotation(AzFramework::ViewportId viewportId, float pitch, float yaw);
  27. //! Sets the default viewport camera orientation/rotation.
  28. //! @param pitch Amount of pitch in radians.
  29. //! @param yaw Amount of yaw in radians.
  30. SANDBOX_API void SetDefaultViewportCameraRotation(float pitch, float yaw);
  31. //! Sets the specified viewport camera translation/position and orientation/rotation.
  32. //! @param position The new position of the camera in world space.
  33. //! @param pitch Amount of pitch in radians.
  34. //! @param yaw Amount of yaw in radians.
  35. SANDBOX_API void SetViewportCameraTransform(AzFramework::ViewportId viewportId, const AZ::Vector3& position, float pitch, float yaw);
  36. //! Sets the default viewport camera translation/position and orientation/rotation.
  37. //! @param position The new position of the camera in world space.
  38. //! @param pitch Amount of pitch in radians.
  39. //! @param yaw Amount of yaw in radians.
  40. SANDBOX_API void SetDefaultViewportCameraTransform(const AZ::Vector3& position, float pitch, float yaw);
  41. //! Sets the specified viewport camera transform.
  42. //! @param transform The transform of the camera in world space.
  43. SANDBOX_API void SetViewportCameraTransform(AzFramework::ViewportId viewportId, const AZ::Transform& transform);
  44. //! Sets the default viewport camera transform.
  45. //! @param transform The transform of the camera in world space.
  46. SANDBOX_API void SetDefaultViewportCameraTransform(const AZ::Transform& transform);
  47. //! Sets the specified viewport camera to interpolate to the given position and orientation.
  48. //! @param position The new position of the camera in world space.
  49. //! @param pitch Amount of pitch in radians.
  50. //! @param yaw Amount of yaw in radians.
  51. //! @param duration Time (in seconds) the interpolation should last.
  52. SANDBOX_API void InterpolateViewportCameraToTransform(
  53. AzFramework::ViewportId viewportId, const AZ::Vector3& position, float pitch, float yaw, float duration);
  54. //! Sets the default viewport camera to interpolate to the given position and orientation.
  55. //! @param position The new position of the camera in world space.
  56. //! @param pitch Amount of pitch in radians.
  57. //! @param yaw Amount of yaw in radians.
  58. //! @param duration Time (in seconds) the interpolation should last.
  59. SANDBOX_API void InterpolateDefaultViewportCameraToTransform(const AZ::Vector3& position, float pitch, float yaw, float duration);
  60. //! Sets the specified viewport camera to interpolate to the given transform.
  61. //! @param transform The transform of the camera in world space.
  62. //! @param duration Time (in seconds) the interpolation should last.
  63. SANDBOX_API void InterpolateViewportCameraToTransform(
  64. AzFramework::ViewportId viewportId, const AZ::Transform& transform, float duration);
  65. //! Sets the default viewport camera to interpolate to the given transform.
  66. //! @param transform The transform of the camera in world space.
  67. //! @param duration Time (in seconds) the interpolation should last.
  68. SANDBOX_API void InterpolateDefaultViewportCameraToTransform(const AZ::Transform& transform, float duration);
  69. //! Gets the specified viewport camera transform in world space.
  70. SANDBOX_API AZ::Transform GetViewportCameraTransform(AzFramework::ViewportId viewportId);
  71. //! Gets the default viewport camera transform in world space.
  72. SANDBOX_API AZ::Transform GetDefaultViewportCameraTransform();
  73. //! Will call either Set or Interpolate camera transform depending on user setting.
  74. //! @param position The new position of the camera in world space.
  75. //! @param pitch Amount of pitch in radians.
  76. //! @param yaw Amount of yaw in radians.
  77. SANDBOX_API void HandleViewportCameraTransitionFromSetting(
  78. AzFramework::ViewportId viewportId, const AZ::Vector3& position, float pitch, float yaw);
  79. //! Will call either Set or Interpolate camera transform depending on user setting.
  80. //! @param position The new position of the camera in world space.
  81. //! @param pitch Amount of pitch in radians.
  82. //! @param yaw Amount of yaw in radians.
  83. SANDBOX_API void HandleDefaultViewportCameraTransitionFromSetting(const AZ::Vector3& position, float pitch, float yaw);
  84. //! Will call either Set or Interpolate camera transform depending on user setting.
  85. //! @param transform The transform of the camera in world space.
  86. SANDBOX_API void HandleViewportCameraTransitionFromSetting(
  87. AzFramework::ViewportId viewportId, const AZ::Transform& transform);
  88. //! Will call either Set or Interpolate camera transform depending on user setting.
  89. //! @param transform The transform of the camera in world space.
  90. SANDBOX_API void HandleDefaultViewportCameraTransitionFromSetting(const AZ::Transform& transform);
  91. //! Returns a transform that will aim to have the entity fill the screen (determined by the current
  92. //! camera transform, field of view and position and radius of the entity).
  93. //! @param cameraTransform The current transform of the camera.
  94. //! @param fovRadians The field of view of the camera (in radians).
  95. //! @param center The entity position in world space.
  96. //! @param radius The radius of the bounding sphere of the entity (usually calculated from the entity Aabb).
  97. //! @return Returns the new transform for the camera to fill the screen with the entity. If the cameraTransform
  98. //! and center match, an empty optional is returned.
  99. SANDBOX_API AZStd::optional<AZ::Transform> CalculateGoToEntityTransform(
  100. const AZ::Transform& cameraTransform, float fovRadians, const AZ::Vector3& center, float radius);
  101. //! Returns a quaternion representing a pitch/yaw rotation for a camera.
  102. //! @param pitch Amount of pitch in radians.
  103. //! @param yaw Amount of yaw in radians.
  104. inline AZ::Quaternion CameraRotation(const float pitch, const float yaw)
  105. {
  106. return AZ::Quaternion::CreateRotationZ(yaw) * AZ::Quaternion::CreateRotationX(pitch);
  107. }
  108. //! Returns a transform representing a position and pitch/yaw rotation.
  109. //! @param position Position for the transform.
  110. //! @param pitch Amount of pitch in radians.
  111. //! @param yaw Amount of yaw in radians.
  112. inline AZ::Transform TransformFromPositionPitchYaw(const AZ::Vector3& position, const float pitch, const float yaw)
  113. {
  114. return AZ::Transform::CreateFromQuaternionAndTranslation(CameraRotation(pitch, yaw), position);
  115. }
  116. } // namespace SandboxEditor