XRCameraMovementComponent.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <XRCameraMovementComponent.h>
  9. #include <AzCore/Component/TransformBus.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/RTTI/BehaviorContext.h>
  13. #include <OpenXRVk/OpenXRVkActionsInterface.h>
  14. #include <Atom/RPI.Public/ViewProviderBus.h>
  15. #include <Atom/RPI.Public/View.h>
  16. #include <AzFramework/Components/CameraBus.h>
  17. #include <Atom/RPI.Public/ViewportContext.h>
  18. #include <Atom/RPI.Public/ViewportContextBus.h>
  19. namespace OpenXRVk
  20. {
  21. void XRCameraMovementComponent::Reflect(AZ::ReflectContext* context)
  22. {
  23. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  24. {
  25. serializeContext->Class<XRCameraMovementComponent, AZ::Component>()
  26. ->Version(1)
  27. ->Field("Move Speed", &XRCameraMovementComponent::m_moveSpeed)
  28. ->Field("Movement Sensitivity", &XRCameraMovementComponent::m_movementSensitivity)
  29. ;
  30. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  31. {
  32. editContext->Class<XRCameraMovementComponent>("XR Camera Movement", "Provides XR controller input to control the camera")
  33. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  34. ->Attribute(AZ::Edit::Attributes::Category, "XR")
  35. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  36. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  37. ->DataElement(AZ::Edit::UIHandlers::Default, &XRCameraMovementComponent::m_moveSpeed, "Move Speed", "Speed of camera movement")
  38. ->Attribute(AZ::Edit::Attributes::Min, 1.f)
  39. ->Attribute(AZ::Edit::Attributes::Max, 50.f)
  40. ->DataElement(AZ::Edit::UIHandlers::Default, &XRCameraMovementComponent::m_movementSensitivity, "Move Sensitivity", "Fine movement sensitivity factor")
  41. ->Attribute(AZ::Edit::Attributes::Min, 0.f)
  42. ->Attribute(AZ::Edit::Attributes::Max, 1.f)
  43. ;
  44. }
  45. }
  46. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  47. {
  48. behaviorContext->Class<XRCameraMovementComponent>("XRCameraMovement Component Group")
  49. ->Attribute(AZ::Script::Attributes::Category, "OpenXRVk Gem Group")
  50. ;
  51. }
  52. }
  53. void XRCameraMovementComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  54. {
  55. provided.push_back(AZ_CRC_CE("CameraMovementService"));
  56. }
  57. void XRCameraMovementComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  58. {
  59. incompatible.push_back(AZ_CRC_CE("CameraMovementService"));
  60. }
  61. void XRCameraMovementComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  62. {
  63. required.push_back(AZ_CRC_CE("TransformService"));
  64. }
  65. void XRCameraMovementComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  66. {
  67. }
  68. void XRCameraMovementComponent::Activate()
  69. {
  70. Camera::CameraNotificationBus::Handler::BusConnect();
  71. if (m_isActive)
  72. {
  73. AZ::TickBus::Handler::BusConnect();
  74. }
  75. }
  76. void XRCameraMovementComponent::Deactivate()
  77. {
  78. if (AZ::TickBus::Handler::BusIsConnected())
  79. {
  80. AZ::TickBus::Handler::BusDisconnect();
  81. }
  82. Camera::CameraNotificationBus::Handler::BusDisconnect();
  83. }
  84. void XRCameraMovementComponent::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  85. {
  86. ProcessOpenXRActions();
  87. AZ::Transform cameraTransform;
  88. AZ::TransformBus::EventResult(cameraTransform, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  89. // Update movement...
  90. const float moveSpeed = m_moveSpeed * deltaTime;
  91. const AZ::Vector3 movementVec =
  92. (cameraTransform.GetBasisX() * m_movement.GetX())
  93. + (cameraTransform.GetBasisY() * m_movement.GetY())
  94. + (cameraTransform.GetBasisZ() * m_movement.GetZ());
  95. const AZ::Vector3 newPosition{ (cameraTransform.GetTranslation() + (movementVec * moveSpeed)) };
  96. cameraTransform.SetTranslation(newPosition);
  97. AZ::TransformBus::Event(GetEntityId(), &AZ::TransformBus::Events::SetWorldTM, cameraTransform);
  98. }
  99. static float ReadActionHandleFloat(IOpenXRActions* iface, IOpenXRActions::ActionHandle actionHandle, float deadZone = 0.05f)
  100. {
  101. auto outcome = iface->GetActionStateFloat(actionHandle);
  102. if (!outcome.IsSuccess())
  103. {
  104. // Most likely the controller went to sleep.
  105. return 0.0f;
  106. }
  107. float value = outcome.GetValue();
  108. if (fabsf(value) < deadZone)
  109. {
  110. return 0.0f;
  111. }
  112. return value;
  113. }
  114. void XRCameraMovementComponent::ProcessOpenXRActions()
  115. {
  116. auto actionsIFace = OpenXRActionsInterface::Get();
  117. if (!actionsIFace)
  118. {
  119. return;
  120. }
  121. if (!m_moveFrontwaysHandle.IsValid())
  122. {
  123. // Try to cache all handles.
  124. m_moveFrontwaysHandle = actionsIFace->GetActionHandle("main_action_set", "move_frontways");
  125. if (!m_moveFrontwaysHandle.IsValid())
  126. {
  127. // Most likely the Action System failed to load the ActionSets asset.
  128. return;
  129. }
  130. m_moveSidewaysHandle = actionsIFace->GetActionHandle("main_action_set", "move_sideways");
  131. AZ_Assert(m_moveSidewaysHandle.IsValid(), "Invalid action handle");
  132. m_moveUpHandle = actionsIFace->GetActionHandle("main_action_set", "move_up");
  133. AZ_Assert(m_moveUpHandle.IsValid(), "Invalid action handle");
  134. m_moveDownHandle = actionsIFace->GetActionHandle("main_action_set", "move_down");
  135. AZ_Assert(m_moveDownHandle.IsValid(), "Invalid action handle");
  136. }
  137. m_movement.Set(0.0f);
  138. m_movement.SetY(ReadActionHandleFloat(actionsIFace, m_moveFrontwaysHandle) * m_movementSensitivity);
  139. m_movement.SetX(ReadActionHandleFloat(actionsIFace, m_moveSidewaysHandle) * m_movementSensitivity);
  140. {
  141. auto outcome = actionsIFace->GetActionStateBoolean(m_moveUpHandle);
  142. if (outcome.IsSuccess() && outcome.GetValue())
  143. {
  144. m_movement.SetZ(m_movementSensitivity);
  145. }
  146. }
  147. {
  148. auto outcome = actionsIFace->GetActionStateBoolean(m_moveDownHandle);
  149. if (outcome.IsSuccess() && outcome.GetValue())
  150. {
  151. m_movement.SetZ(-m_movementSensitivity);
  152. }
  153. }
  154. }
  155. // Camera::CameraNotificationBus::Handler overrides
  156. void XRCameraMovementComponent::OnActiveViewChanged(const AZ::EntityId& activeEntityId)
  157. {
  158. m_isActive = activeEntityId == GetEntityId();
  159. if (m_isActive)
  160. {
  161. if (!AZ::TickBus::Handler::BusIsConnected())
  162. {
  163. AZ::TickBus::Handler::BusConnect();
  164. }
  165. }
  166. else
  167. {
  168. if (AZ::TickBus::Handler::BusIsConnected())
  169. {
  170. AZ::TickBus::Handler::BusDisconnect();
  171. }
  172. }
  173. }
  174. } // namespace OpenXRVk