CameraSystemComponent.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "CameraSystemComponent.h"
  9. #include <AzCore/Math/MatrixUtils.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzFramework/Viewport/CameraState.h>
  12. #include <Atom/RPI.Public/View.h>
  13. #include <Atom/RPI.Public/ViewportContextBus.h>
  14. namespace Camera
  15. {
  16. void CameraSystemComponent::Reflect(AZ::ReflectContext* context)
  17. {
  18. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  19. {
  20. serializeContext->Class<CameraSystemComponent, AZ::Component>()
  21. ->Version(1)
  22. ;
  23. }
  24. }
  25. void CameraSystemComponent::Activate()
  26. {
  27. CameraSystemRequestBus::Handler::BusConnect();
  28. ActiveCameraRequestBus::Handler::BusConnect();
  29. CameraNotificationBus::Handler::BusConnect();
  30. }
  31. void CameraSystemComponent::Deactivate()
  32. {
  33. CameraNotificationBus::Handler::BusDisconnect();
  34. ActiveCameraRequestBus::Handler::BusDisconnect();
  35. CameraSystemRequestBus::Handler::BusDisconnect();
  36. }
  37. AZ::EntityId CameraSystemComponent::GetActiveCamera()
  38. {
  39. return m_activeView;
  40. }
  41. const AZ::Transform& CameraSystemComponent::GetActiveCameraTransform()
  42. {
  43. if (m_activeView.IsValid())
  44. {
  45. AZ::TransformBus::EventResult(m_activeViewProperties.transform, m_activeView, &AZ::TransformBus::Events::GetWorldTM);
  46. }
  47. else
  48. {
  49. // In editor, invalid entity ID for the active view denotes the "default editor camera"
  50. // In game, this is an impossible state and if we reached here, we'll likely fail somehow...
  51. m_activeViewProperties.transform = AZ::Transform::CreateIdentity();
  52. using namespace AZ::RPI;
  53. if (auto viewSystem = ViewportContextRequests::Get())
  54. {
  55. if (auto viewGroup = viewSystem->GetCurrentViewGroup(viewSystem->GetDefaultViewportContextName()))
  56. {
  57. m_activeViewProperties.transform = viewGroup->GetView()->GetCameraTransform();
  58. }
  59. }
  60. }
  61. return m_activeViewProperties.transform;
  62. }
  63. const Configuration& CameraSystemComponent::GetActiveCameraConfiguration()
  64. {
  65. if (m_activeView.IsValid())
  66. {
  67. CameraRequestBus::EventResult(m_activeViewProperties.configuration, m_activeView, &CameraRequestBus::Events::GetCameraConfiguration);
  68. }
  69. else
  70. {
  71. auto& cfg = m_activeViewProperties.configuration;
  72. cfg = Configuration();
  73. // In editor, invalid entity ID for the active view denotes the "default editor camera"
  74. // In game, this is an impossible state and if we reached here, we'll likely fail somehow...
  75. using namespace AZ::RPI;
  76. if (auto viewSystem = ViewportContextRequests::Get())
  77. {
  78. if (auto viewGroup = viewSystem->GetCurrentViewGroup(viewSystem->GetDefaultViewportContextName()))
  79. {
  80. AzFramework::CameraState cam;
  81. AzFramework::SetCameraClippingVolumeFromPerspectiveFovMatrixRH(cam, viewGroup->GetView()->GetViewToClipMatrix());
  82. cfg.m_fovRadians = cam.m_fovOrZoom;
  83. cfg.m_nearClipDistance = cam.m_nearClip;
  84. cfg.m_farClipDistance = cam.m_farClip;
  85. // No idea what to do here. Seems to be unused?
  86. cfg.m_frustumWidth = cfg.m_frustumHeight = 1.0f;
  87. }
  88. }
  89. }
  90. return m_activeViewProperties.configuration;
  91. }
  92. void CameraSystemComponent::OnActiveViewChanged(const AZ::EntityId& activeView)
  93. {
  94. m_activeView = activeView;
  95. }
  96. } // namespace Camera