CameraComponentController.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/Asset/AssetCommon.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/TransformBus.h>
  12. #include <AzFramework/Components/CameraBus.h>
  13. #include <AzFramework/Viewport/CameraState.h>
  14. #include <Atom/RPI.Public/AuxGeom/AuxGeomFeatureProcessorInterface.h>
  15. #include <Atom/RPI.Public/Base.h>
  16. #include <Atom/RPI.Public/RenderPipeline.h>
  17. #include <Atom/RPI.Public/ViewGroup.h>
  18. #include <Atom/RPI.Public/ViewportContextBus.h>
  19. #include <Atom/RPI.Public/ViewProviderBus.h>
  20. #include <Atom/RPI.Public/XR/XRRenderingInterface.h>
  21. #include <Atom/RPI.Public/XR/XRSpaceNotificationBus.h>
  22. #include <Atom/RPI.Reflect/Image/AttachmentImageAsset.h>
  23. namespace Camera
  24. {
  25. static constexpr float DefaultFoV = 75.0f;
  26. static constexpr float MinFoV = std::numeric_limits<float>::epsilon();
  27. static constexpr float MaxFoV = AZ::RadToDeg(AZ::Constants::Pi);
  28. static constexpr float MinimumNearPlaneDistance = 0.001f;
  29. static constexpr float DefaultNearPlaneDistance = 0.2f;
  30. static constexpr float DefaultFarClipPlaneDistance = 1024.0f;
  31. static constexpr float DefaultFrustumDimension = 256.f;
  32. struct CameraComponentConfig final
  33. : public AZ::ComponentConfig
  34. {
  35. AZ_CLASS_ALLOCATOR(CameraComponentConfig, AZ::SystemAllocator)
  36. AZ_RTTI(CameraComponentConfig, "{064A5D64-8688-4188-B3DE-C80CE4BB7558}", AZ::ComponentConfig);
  37. static void Reflect(AZ::ReflectContext* context);
  38. float GetFarClipDistance() const;
  39. float GetNearClipDistance() const;
  40. AZ::EntityId GetEditorEntityId() const;
  41. AZ::u32 GetPerspectiveParameterVisibility() const;
  42. AZ::u32 GetOrthographicParameterVisibility() const;
  43. // Reflected members
  44. float m_fov = DefaultFoV;
  45. float m_nearClipDistance = DefaultNearPlaneDistance;
  46. float m_farClipDistance = DefaultFarClipPlaneDistance;
  47. float m_frustumWidth = DefaultFrustumDimension;
  48. float m_frustumHeight = DefaultFrustumDimension;
  49. bool m_specifyFrustumDimensions = false;
  50. bool m_makeActiveViewOnActivation = true;
  51. bool m_orthographic = false;
  52. float m_orthographicHalfWidth = 5.f;
  53. AZ::u64 m_editorEntityId = AZ::EntityId::InvalidEntityId;
  54. // Members for render to texture
  55. // The texture assets which is used for render to texture feature. It defines the resolution, format etc.
  56. AZ::Data::Asset<AZ::RPI::AttachmentImageAsset> m_renderTextureAsset;
  57. // The pass template name used for render pipeline's root template
  58. AZStd::string m_pipelineTemplate = "CameraPipeline";
  59. };
  60. class CameraComponentController
  61. : public CameraBus::Handler
  62. , public CameraRequestBus::Handler
  63. , public AZ::TransformNotificationBus::Handler
  64. , public AZ::RPI::ViewportContextNotificationBus::Handler
  65. , public AZ::RPI::ViewProviderBus::Handler
  66. , public AZ::RPI::XRSpaceNotificationBus::Handler
  67. {
  68. public:
  69. AZ_TYPE_INFO(CameraComponentController, "{A27A0725-8C07-4BF2-BF95-B6CB0CBD01B8}");
  70. CameraComponentController() = default;
  71. explicit CameraComponentController(const CameraComponentConfig& config);
  72. //! Defines a callback for determining whether this camera should push itself to the top of the Atom camera stack.
  73. //! Used by the Editor to disable undesirable camera changes in edit mode.
  74. void SetShouldActivateFunction(AZStd::function<bool()> shouldActivateFunction);
  75. //! Defines a callback for determining whether this camera is currently locked by its transform.
  76. void SetIsLockedFunction(AZStd::function<bool()> isLockedFunction);
  77. // Controller interface
  78. static void Reflect(AZ::ReflectContext* context);
  79. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  80. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  81. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  82. void Init();
  83. void Activate(AZ::EntityId entityId);
  84. void Deactivate();
  85. void SetConfiguration(const CameraComponentConfig& config);
  86. const CameraComponentConfig& GetConfiguration() const;
  87. AZ::RPI::ViewportContextPtr GetViewportContext();
  88. // CameraBus::Handler interface
  89. AZ::EntityId GetCameras() override;
  90. // CameraRequestBus::Handler interface
  91. float GetFovDegrees() override;
  92. float GetFovRadians() override;
  93. float GetNearClipDistance() override;
  94. float GetFarClipDistance() override;
  95. float GetFrustumWidth() override;
  96. float GetFrustumHeight() override;
  97. bool IsOrthographic() override;
  98. float GetOrthographicHalfWidth() override;
  99. void SetFovDegrees(float fov) override;
  100. void SetFovRadians(float fov) override;
  101. void SetNearClipDistance(float nearClipDistance) override;
  102. void SetFarClipDistance(float farClipDistance) override;
  103. void SetFrustumWidth(float width) override;
  104. void SetFrustumHeight(float height) override;
  105. void SetOrthographic(bool orthographic) override;
  106. void SetOrthographicHalfWidth(float halfWidth) override;
  107. void SetXRViewQuaternion(const AZ::Quaternion& viewQuat, uint32_t xrViewIndex) override;
  108. void MakeActiveView() override;
  109. bool IsActiveView() override;
  110. AZ::Vector3 ScreenToWorld(const AZ::Vector2& screenPosition, float depth) override;
  111. AZ::Vector3 ScreenNdcToWorld(const AZ::Vector2& screenNdcPosition, float depth) override;
  112. AZ::Vector2 WorldToScreen(const AZ::Vector3& worldPosition) override;
  113. AZ::Vector2 WorldToScreenNdc(const AZ::Vector3& worldPosition) override;
  114. // AZ::TransformNotificationBus::Handler interface
  115. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  116. // AZ::RPI::ViewportContextNotificationBus::Handler interface
  117. void OnViewportSizeChanged(AzFramework::WindowSize size) override;
  118. void OnViewportDefaultViewChanged(AZ::RPI::ViewPtr view) override;
  119. // AZ::RPI::ViewProviderBus::Handler interface
  120. AZ::RPI::ViewPtr GetView() const override;
  121. AZ::RPI::ViewPtr GetStereoscopicView(AZ::RPI::ViewType viewType) const override;
  122. ///////////////////////////////////////////////////////////////////////
  123. // AZ::RPI::XRSpaceNotificationBus::Handler Overrides
  124. void OnXRSpaceLocationsChanged(
  125. const AZ::Transform& HeadRelativeToBaseSpaceTm,
  126. const AZ::Transform& LeftEyeRelativeToHeadTm,
  127. const AZ::Transform& RightEyeRelativeToHeadTm) override;
  128. ///////////////////////////////////////////////////////////////////////
  129. private:
  130. AZ_DISABLE_COPY(CameraComponentController);
  131. void CreateRenderPipelineForTexture();
  132. void ActivateAtomView();
  133. void DeactivateAtomView();
  134. void UpdateCamera();
  135. void SetupAtomAuxGeom(AZ::RPI::ViewportContextPtr viewportContext);
  136. AzFramework::CameraState GetCameraState();
  137. CameraComponentConfig m_config;
  138. AZ::EntityId m_entityId;
  139. // Atom integration
  140. AZ::RPI::ViewGroupPtr m_atomCameraViewGroup = nullptr;
  141. AZ::RPI::AuxGeomDrawPtr m_atomAuxGeom;
  142. bool m_updatingTransformFromEntity = false;
  143. bool m_isActiveView = false;
  144. AZStd::function<bool()> m_shouldActivateFn;
  145. AZStd::function<bool()> m_isLockedFn = []{ return false; };
  146. // for render to texture
  147. AZ::RPI::RenderPipelinePtr m_renderToTexturePipeline;
  148. //! From this point onwards the member variables are only applicable
  149. //! when the XRRenderingInterface is active.
  150. AZ::RPI::XRRenderingInterface* m_xrSystem = nullptr;
  151. AZ::u32 m_numSterescopicViews = 0;
  152. // Remarks, When using the XR Gem the world camera transform will be:
  153. // entityWorldTm = m_xrCameraToBaseSpaceTm * baseSpaceToHeadTm
  154. // And for each Eye it will be:
  155. // leftEyeWorldTm = m_xrCameraToBaseSpaceTm * baseSpaceToHeadTm * HeadToLeftEyeTm;
  156. // rightEyeWorldTm = m_xrCameraToBaseSpaceTm * baseSpaceToHeadTm * HeadToRightEyeTm;
  157. AZ::Transform m_xrCameraToBaseSpaceTm;
  158. AZ::Transform m_xrBaseSpaceToHeadTm; // Comes from the XR System
  159. AZ::Transform m_xrHeadToLeftEyeTm; // Comes from the XR System
  160. AZ::Transform m_xrHeadToRightEyeTm; // Comes from the XR System
  161. };
  162. } // namespace Camera