CameraRigComponent.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "CameraRigComponent.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Math/Transform.h>
  11. #include <AzCore/Component/TransformBus.h>
  12. #include <AzCore/Component/Entity.h>
  13. #include "CameraFramework/ICameraTargetAcquirer.h"
  14. #include "CameraFramework/ICameraLookAtBehavior.h"
  15. #include "CameraFramework/ICameraTransformBehavior.h"
  16. namespace Camera
  17. {
  18. // Clean up the behaviours instantiated for us by the editor/serialization system
  19. CameraRigComponent::~CameraRigComponent()
  20. {
  21. for (ICameraTargetAcquirer* targetAcquirer : m_targetAcquirers)
  22. {
  23. delete targetAcquirer;
  24. }
  25. for (ICameraLookAtBehavior* lookAtBehavior : m_lookAtBehaviors)
  26. {
  27. delete lookAtBehavior;
  28. }
  29. for (ICameraTransformBehavior* transformBehavior : m_transformBehaviors)
  30. {
  31. delete transformBehavior;
  32. }
  33. }
  34. void CameraRigComponent::Init()
  35. {
  36. for (ICameraTargetAcquirer* targetAcquirer : m_targetAcquirers)
  37. {
  38. targetAcquirer->Init();
  39. }
  40. for (ICameraLookAtBehavior* lookAtBehavior : m_lookAtBehaviors)
  41. {
  42. lookAtBehavior->Init();
  43. }
  44. for (ICameraTransformBehavior* transformBehavior : m_transformBehaviors)
  45. {
  46. transformBehavior->Init();
  47. }
  48. }
  49. void CameraRigComponent::Activate()
  50. {
  51. #ifdef AZ_ENABLE_TRACING
  52. bool isStaticTransform = false;
  53. AZ::TransformBus::EventResult(isStaticTransform, GetEntityId(), &AZ::TransformBus::Events::IsStaticTransform);
  54. AZ_Warning("Camera Rig Component", !isStaticTransform,
  55. "Camera Rig needs to move, but entity '%s' %s has a static transform.", GetEntity()->GetName().c_str(), GetEntityId().ToString().c_str());
  56. #endif
  57. for (ICameraTargetAcquirer* targetAcquirer : m_targetAcquirers)
  58. {
  59. targetAcquirer->Activate(GetEntityId());
  60. }
  61. for (ICameraLookAtBehavior* lookAtBehavior : m_lookAtBehaviors)
  62. {
  63. lookAtBehavior->Activate(GetEntityId());
  64. }
  65. for (ICameraTransformBehavior* transformBehavior : m_transformBehaviors)
  66. {
  67. transformBehavior->Activate(GetEntityId());
  68. }
  69. m_initialTransform = AZ::Transform::CreateIdentity();
  70. AZ::TransformBus::EventResult(m_initialTransform, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  71. AZ::TickBus::Handler::BusConnect();
  72. }
  73. void CameraRigComponent::Deactivate()
  74. {
  75. AZ::TickBus::Handler::BusDisconnect();
  76. for (ICameraTargetAcquirer* targetAcquirer : m_targetAcquirers)
  77. {
  78. targetAcquirer->Deactivate();
  79. }
  80. for (ICameraLookAtBehavior* lookAtBehavior : m_lookAtBehaviors)
  81. {
  82. lookAtBehavior->Deactivate();
  83. }
  84. for (ICameraTransformBehavior* transformBehavior : m_transformBehaviors)
  85. {
  86. transformBehavior->Deactivate();
  87. }
  88. }
  89. void CameraRigComponent::Reflect(AZ::ReflectContext* reflection)
  90. {
  91. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  92. if (serializeContext)
  93. {
  94. serializeContext->Class<ICameraTargetAcquirer>()
  95. ->Version(1);
  96. serializeContext->Class<ICameraLookAtBehavior>()
  97. ->Version(1);
  98. serializeContext->Class<ICameraTransformBehavior>()
  99. ->Version(1);
  100. serializeContext->Class<CameraRigComponent, AZ::Component>()
  101. ->Version(1)
  102. ->Field("Target Acquirers", &CameraRigComponent::m_targetAcquirers)
  103. ->Field("Look-at Behaviors", &CameraRigComponent::m_lookAtBehaviors)
  104. ->Field("Camera Transform Behaviors", &CameraRigComponent::m_transformBehaviors);
  105. AZ::EditContext* editContext = serializeContext->GetEditContext();
  106. if (editContext)
  107. {
  108. editContext->Class<ICameraTargetAcquirer>("ICameraTargetAcquirer", "Base class for all target acquirers. Implementations can be found in other gems");
  109. editContext->Class<ICameraLookAtBehavior>("ICameraLookAtBehavior", "Base class for all look at behaviors. Implementations can be found in other gems");
  110. editContext->Class<ICameraTransformBehavior>("ICameraTransformBehavior", "Base class for all transform behaviors. Implementations can be found in other gems");
  111. editContext->Class<CameraRigComponent>( "Camera Rig", "The Camera Rig component can be used to add and remove behaviors to drive your camera entity")
  112. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  113. ->Attribute(AZ::Edit::Attributes::Category, "Camera")
  114. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/CameraRig.svg")
  115. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/CameraRig.png")
  116. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  117. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/camera/camera-rig/")
  118. ->DataElement(0, &CameraRigComponent::m_targetAcquirers, "Target acquirers",
  119. "A list of behaviors that define how a camera will select a target. They are executed in order until one succeeds")
  120. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  121. ->DataElement(0, &CameraRigComponent::m_lookAtBehaviors, "Look-at behaviors",
  122. "A list of look-at behaviors. They are run in order, each having the chance to sequentially modify the look-at target transform")
  123. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  124. ->DataElement(0, &CameraRigComponent::m_transformBehaviors, "Transform behaviors",
  125. "A list of behaviors that run in order, each having the chance to sequentially modify the camera's transform based on the look-at transform")
  126. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  127. }
  128. }
  129. }
  130. void CameraRigComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  131. {
  132. provided.push_back(AZ_CRC_CE("CameraRigService"));
  133. }
  134. void CameraRigComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  135. {
  136. required.push_back(AZ_CRC_CE("TransformService"));
  137. required.push_back(AZ_CRC_CE("CameraService"));
  138. }
  139. void CameraRigComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  140. {
  141. }
  142. void CameraRigComponent::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  143. {
  144. // Step 1 Acquire a target
  145. AZ::Transform initialCameraTransform = AZ::Transform::Identity();
  146. AZ::TransformBus::EventResult(initialCameraTransform, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  147. AZ::Transform targetTransform(m_initialTransform);
  148. for (ICameraTargetAcquirer* targetAcquirer : m_targetAcquirers)
  149. {
  150. if (targetAcquirer->AcquireTarget(targetTransform))
  151. {
  152. break;
  153. }
  154. }
  155. // Step 2 modify the target look at position
  156. AZ::Transform lookAtTargetTransform(targetTransform);
  157. for (ICameraLookAtBehavior* CameraLookAtBehavior : m_lookAtBehaviors)
  158. {
  159. CameraLookAtBehavior->AdjustLookAtTarget(deltaTime, targetTransform, lookAtTargetTransform);
  160. }
  161. // Step 3 modify the camera's position
  162. AZ::Transform finalTransform(initialCameraTransform);
  163. for (ICameraTransformBehavior* cameraTransformBehavior : m_transformBehaviors)
  164. {
  165. cameraTransformBehavior->AdjustCameraTransform(deltaTime, initialCameraTransform, lookAtTargetTransform, finalTransform);
  166. }
  167. // Step 4 Alert the camera component of the new desired transform
  168. AZ::TransformBus::Event(GetEntityId(), &AZ::TransformBus::Events::SetWorldTM, finalTransform);
  169. }
  170. } //namespace Camera