CameraComponent.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <AzCore/Serialization/EditContext.h>
  9. #include <AzCore/Component/TransformBus.h>
  10. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  11. #include <Atom/RPI.Public/View.h>
  12. #include <Atom/RPI.Public/ViewportContextManager.h>
  13. #include <Atom/RPI.Public/ViewportContext.h>
  14. #include "CameraComponent.h"
  15. #include <AzCore/Math/MatrixUtils.h>
  16. #include <AzCore/RTTI/BehaviorContext.h>
  17. namespace Camera
  18. {
  19. namespace ClassConverters
  20. {
  21. extern bool DeprecateCameraComponentWithoutEditor(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement);
  22. extern bool UpdateCameraComponentToUseController(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement);
  23. }
  24. CameraComponent::CameraComponent(const CameraComponentConfig& properties)
  25. : CameraComponentBase(properties)
  26. {
  27. }
  28. void CameraComponent::Activate()
  29. {
  30. CameraComponentBase::Activate();
  31. }
  32. void CameraComponent::Deactivate()
  33. {
  34. CameraComponentBase::Deactivate();
  35. }
  36. static bool UpdateGameCameraComponentToUseController(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  37. {
  38. if (!ClassConverters::UpdateCameraComponentToUseController(context, classElement))
  39. {
  40. return false;
  41. }
  42. classElement.Convert<CameraComponent>(context);
  43. return true;
  44. }
  45. class CameraNotificationBus_BehaviorHandler : public CameraNotificationBus::Handler, public AZ::BehaviorEBusHandler
  46. {
  47. public:
  48. AZ_EBUS_BEHAVIOR_BINDER(CameraNotificationBus_BehaviorHandler, "{91E442A0-37E7-4E03-AB59-FEC11A06741D}", AZ::SystemAllocator,
  49. OnCameraAdded, OnCameraRemoved, OnActiveViewChanged);
  50. void OnCameraAdded(const AZ::EntityId& cameraId) override
  51. {
  52. Call(FN_OnCameraAdded, cameraId);
  53. }
  54. void OnCameraRemoved(const AZ::EntityId& cameraId) override
  55. {
  56. Call(FN_OnCameraRemoved, cameraId);
  57. }
  58. void OnActiveViewChanged(const AZ::EntityId& cameraId) override
  59. {
  60. Call(FN_OnCameraRemoved, cameraId);
  61. };
  62. };
  63. void CameraComponent::Reflect(AZ::ReflectContext* reflection)
  64. {
  65. CameraComponentBase::Reflect(reflection);
  66. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  67. if (serializeContext)
  68. {
  69. serializeContext->ClassDeprecate("CameraComponent", AZ::Uuid("{A0C21E18-F759-4E72-AF26-7A36FC59E477}"), &ClassConverters::DeprecateCameraComponentWithoutEditor);
  70. serializeContext->ClassDeprecate("CameraComponent", AZ::Uuid("{E409F5C0-9919-4CA5-9488-1FE8A041768E}"), &UpdateGameCameraComponentToUseController);
  71. serializeContext->Class<CameraComponent, CameraComponentBase>()
  72. ->Version(0)
  73. ;
  74. }
  75. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(reflection))
  76. {
  77. behaviorContext->EBus<CameraRequestBus>("CameraRequestBus")
  78. ->Event("GetNearClipDistance", &CameraRequestBus::Events::GetNearClipDistance)
  79. ->Event("GetFarClipDistance", &CameraRequestBus::Events::GetFarClipDistance)
  80. ->Event("GetFovDegrees", &CameraRequestBus::Events::GetFovDegrees)
  81. ->Event("SetFovDegrees", &CameraRequestBus::Events::SetFovDegrees)
  82. ->Event("GetFovRadians", &CameraRequestBus::Events::GetFovRadians)
  83. ->Event("SetFovRadians", &CameraRequestBus::Events::SetFovRadians)
  84. ->Event("GetFov", &CameraRequestBus::Events::GetFov) // Deprecated in 1.13
  85. ->Event("SetFov", &CameraRequestBus::Events::SetFov) // Deprecated in 1.13
  86. ->Event("SetNearClipDistance", &CameraRequestBus::Events::SetNearClipDistance)
  87. ->Event("SetFarClipDistance", &CameraRequestBus::Events::SetFarClipDistance)
  88. ->Event("MakeActiveView", &CameraRequestBus::Events::MakeActiveView)
  89. ->Event("IsActiveView", &CameraRequestBus::Events::IsActiveView)
  90. ->Event("IsOrthographic", &CameraRequestBus::Events::IsOrthographic)
  91. ->Event("SetOrthographic", &CameraRequestBus::Events::SetOrthographic)
  92. ->Event("GetOrthographicHalfWidth", &CameraRequestBus::Events::GetOrthographicHalfWidth)
  93. ->Event("SetOrthographicHalfWidth", &CameraRequestBus::Events::SetOrthographicHalfWidth)
  94. ->Event("SetXRViewQuaternion", &CameraRequestBus::Events::SetXRViewQuaternion)
  95. ->Event("ScreenToWorld", &CameraRequestBus::Events::ScreenToWorld)
  96. ->Event("ScreenNdcToWorld", &CameraRequestBus::Events::ScreenNdcToWorld)
  97. ->Event("WorldToScreen", &CameraRequestBus::Events::WorldToScreen)
  98. ->Event("WorldToScreenNdc", &CameraRequestBus::Events::WorldToScreenNdc)
  99. ->VirtualProperty("FieldOfView","GetFovDegrees","SetFovDegrees")
  100. ->VirtualProperty("NearClipDistance", "GetNearClipDistance", "SetNearClipDistance")
  101. ->VirtualProperty("FarClipDistance", "GetFarClipDistance", "SetFarClipDistance")
  102. ->VirtualProperty("Orthographic", "IsOrthographic", "SetOrthographic")
  103. ->VirtualProperty("OrthographicHalfWidth", "GetOrthographicHalfWidth", "SetOrthographicHalfWidth")
  104. ;
  105. behaviorContext->Class<CameraComponent>()->RequestBus("CameraRequestBus");
  106. behaviorContext->EBus<CameraSystemRequestBus>("CameraSystemRequestBus")
  107. ->Attribute(AZ::Script::Attributes::Category, "Camera")
  108. ->Event("GetActiveCamera", &CameraSystemRequestBus::Events::GetActiveCamera)
  109. ;
  110. behaviorContext->EBus<CameraNotificationBus>("CameraNotificationBus")
  111. ->Attribute(AZ::Script::Attributes::Category, "Camera")
  112. ->Handler<CameraNotificationBus_BehaviorHandler>()
  113. ;
  114. }
  115. }
  116. } //namespace Camera