CameraComponentController.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. bool m_allowPipelineChanges = false;
  53. float m_orthographicHalfWidth = 5.f;
  54. AZ::u64 m_editorEntityId = AZ::EntityId::InvalidEntityId;
  55. // Members for render to texture
  56. // The texture assets which is used for render to texture feature. It defines the resolution, format etc.
  57. AZ::Data::Asset<AZ::RPI::AttachmentImageAsset> m_renderTextureAsset;
  58. // The pass template name used for render pipeline's root template
  59. AZStd::string m_pipelineTemplate = "CameraPipeline";
  60. private:
  61. //! Check if experimental features are enabled
  62. AZ::u32 GetAllowPipelineChangesVisibility() const;
  63. };
  64. class CameraComponentController
  65. : public CameraBus::Handler
  66. , public CameraRequestBus::Handler
  67. , public AZ::TransformNotificationBus::Handler
  68. , public AZ::RPI::ViewportContextNotificationBus::Handler
  69. , public AZ::RPI::ViewProviderBus::Handler
  70. , public AZ::RPI::XRSpaceNotificationBus::Handler
  71. {
  72. public:
  73. AZ_TYPE_INFO(CameraComponentController, "{A27A0725-8C07-4BF2-BF95-B6CB0CBD01B8}");
  74. CameraComponentController() = default;
  75. explicit CameraComponentController(const CameraComponentConfig& config);
  76. //! Defines a callback for determining whether this camera should push itself to the top of the Atom camera stack.
  77. //! Used by the Editor to disable undesirable camera changes in edit mode.
  78. void SetShouldActivateFunction(AZStd::function<bool()> shouldActivateFunction);
  79. //! Defines a callback for determining whether this camera is currently locked by its transform.
  80. void SetIsLockedFunction(AZStd::function<bool()> isLockedFunction);
  81. // Controller interface
  82. static void Reflect(AZ::ReflectContext* context);
  83. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  84. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  85. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  86. void Init();
  87. void Activate(AZ::EntityId entityId);
  88. void Deactivate();
  89. void SetConfiguration(const CameraComponentConfig& config);
  90. const CameraComponentConfig& GetConfiguration() const;
  91. AZ::RPI::ViewportContextPtr GetViewportContext();
  92. // CameraBus::Handler interface
  93. AZ::EntityId GetCameras() override;
  94. // CameraRequestBus::Handler interface
  95. float GetFovDegrees() override;
  96. float GetFovRadians() override;
  97. float GetNearClipDistance() override;
  98. float GetFarClipDistance() override;
  99. float GetFrustumWidth() override;
  100. float GetFrustumHeight() override;
  101. bool IsOrthographic() override;
  102. float GetOrthographicHalfWidth() override;
  103. void SetFovDegrees(float fov) override;
  104. void SetFovRadians(float fov) override;
  105. void SetNearClipDistance(float nearClipDistance) override;
  106. void SetFarClipDistance(float farClipDistance) override;
  107. void SetFrustumWidth(float width) override;
  108. void SetFrustumHeight(float height) override;
  109. void SetOrthographic(bool orthographic) override;
  110. void SetOrthographicHalfWidth(float halfWidth) override;
  111. void SetXRViewQuaternion(const AZ::Quaternion& viewQuat, uint32_t xrViewIndex) override;
  112. void MakeActiveView() override;
  113. bool IsActiveView() override;
  114. AZ::Vector3 ScreenToWorld(const AZ::Vector2& screenPosition, float depth) override;
  115. AZ::Vector3 ScreenNdcToWorld(const AZ::Vector2& screenNdcPosition, float depth) override;
  116. AZ::Vector2 WorldToScreen(const AZ::Vector3& worldPosition) override;
  117. AZ::Vector2 WorldToScreenNdc(const AZ::Vector3& worldPosition) override;
  118. // AZ::TransformNotificationBus::Handler interface
  119. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  120. // AZ::RPI::ViewportContextNotificationBus::Handler interface
  121. void OnViewportSizeChanged(AzFramework::WindowSize size) override;
  122. void OnViewportDefaultViewChanged(AZ::RPI::ViewPtr view) override;
  123. // AZ::RPI::ViewProviderBus::Handler interface
  124. AZ::RPI::ViewPtr GetView() const override;
  125. AZ::RPI::ViewPtr GetStereoscopicView(AZ::RPI::ViewType viewType) const override;
  126. ///////////////////////////////////////////////////////////////////////
  127. // AZ::RPI::XRSpaceNotificationBus::Handler Overrides
  128. void OnXRSpaceLocationsChanged(
  129. const AZ::Transform& HeadRelativeToBaseSpaceTm,
  130. const AZ::Transform& LeftEyeRelativeToHeadTm,
  131. const AZ::Transform& RightEyeRelativeToHeadTm) override;
  132. ///////////////////////////////////////////////////////////////////////
  133. private:
  134. AZ_DISABLE_COPY(CameraComponentController);
  135. void CreateRenderPipelineForTexture();
  136. void ActivateAtomView();
  137. void DeactivateAtomView();
  138. void UpdateCamera();
  139. void SetupAtomAuxGeom(AZ::RPI::ViewportContextPtr viewportContext);
  140. AzFramework::CameraState GetCameraState();
  141. CameraComponentConfig m_config;
  142. AZ::EntityId m_entityId;
  143. // Atom integration
  144. AZ::RPI::ViewGroupPtr m_atomCameraViewGroup = nullptr;
  145. AZ::RPI::AuxGeomDrawPtr m_atomAuxGeom;
  146. bool m_updatingTransformFromEntity = false;
  147. bool m_isActiveView = false;
  148. AZStd::function<bool()> m_shouldActivateFn;
  149. AZStd::function<bool()> m_isLockedFn = []{ return false; };
  150. // for render to texture
  151. AZ::RPI::RenderPipelinePtr m_renderToTexturePipeline;
  152. //! From this point onwards the member variables are only applicable
  153. //! when the XRRenderingInterface is active.
  154. AZ::RPI::XRRenderingInterface* m_xrSystem = nullptr;
  155. AZ::u32 m_numSterescopicViews = 0;
  156. // Remarks, When using the XR Gem the world camera transform will be:
  157. // entityWorldTm = m_xrCameraToBaseSpaceTm * baseSpaceToHeadTm
  158. // And for each Eye it will be:
  159. // leftEyeWorldTm = m_xrCameraToBaseSpaceTm * baseSpaceToHeadTm * HeadToLeftEyeTm;
  160. // rightEyeWorldTm = m_xrCameraToBaseSpaceTm * baseSpaceToHeadTm * HeadToRightEyeTm;
  161. AZ::Transform m_xrCameraToBaseSpaceTm;
  162. AZ::Transform m_xrBaseSpaceToHeadTm; // Comes from the XR System
  163. AZ::Transform m_xrHeadToLeftEyeTm; // Comes from the XR System
  164. AZ::Transform m_xrHeadToRightEyeTm; // Comes from the XR System
  165. };
  166. } // namespace Camera