XRControllerComponent.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "XRControllerComponent.h"
  9. #include <AzCore/Component/TransformBus.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzFramework/Components/CameraBus.h>
  13. #include <OpenXRVk/OpenXRVkActionsInterface.h>
  14. #include <AzCore/Math/Color.h>
  15. namespace OpenXRVk
  16. {
  17. void XRControllerComponent::Reflect(AZ::ReflectContext* context)
  18. {
  19. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  20. {
  21. serializeContext->Class<XRControllerComponent, AZ::Component>()
  22. ->Version(1)
  23. ->Field("Label of Pose Label", &XRControllerComponent::m_controllerPoseActionLabel)
  24. ;
  25. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  26. {
  27. editContext->Class<XRControllerComponent>("XR Controller", "Provides movement/orientation of VR controller")
  28. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  29. ->Attribute(AZ::Edit::Attributes::Category, "XR")
  30. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  31. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  32. ->DataElement(AZ::Edit::UIHandlers::Default, &XRControllerComponent::m_controllerPoseActionLabel, "Action name of pose", "OpenXRActionsInterface ActionHandle label of pose")
  33. ;
  34. }
  35. }
  36. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  37. {
  38. behaviorContext->Class<XRControllerComponent>("XR Component Group")
  39. ->Attribute(AZ::Script::Attributes::Category, "OpenXRVk Gem Group")
  40. ;
  41. }
  42. }
  43. void XRControllerComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  44. {
  45. provided.push_back(AZ_CRC_CE("XRControllerMovementService"));
  46. }
  47. void XRControllerComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  48. {
  49. incompatible.push_back(AZ_CRC_CE("XRControllerMovementService"));
  50. }
  51. void XRControllerComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  52. {
  53. required.push_back(AZ_CRC_CE("TransformService"));
  54. }
  55. void XRControllerComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  56. {
  57. }
  58. void XRControllerComponent::Activate()
  59. {
  60. Camera::CameraNotificationBus::Handler::BusConnect();
  61. AZ::TickBus::Handler::BusConnect();
  62. }
  63. void XRControllerComponent::Deactivate()
  64. {
  65. if (AZ::TickBus::Handler::BusIsConnected())
  66. {
  67. AZ::TickBus::Handler::BusDisconnect();
  68. }
  69. Camera::CameraNotificationBus::Handler::BusDisconnect();
  70. }
  71. void XRControllerComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  72. {
  73. if (m_cameraEntity == AZ::EntityId())
  74. {
  75. return;
  76. }
  77. ProcessOpenXRActions();
  78. // Get the camera's transform
  79. AZ::Transform cameraTransform;
  80. AZ::TransformBus::EventResult(cameraTransform, m_cameraEntity, &AZ::TransformBus::Events::GetWorldTM);
  81. // Current transform of the controller (in local space relative to the camera)
  82. AZ::Transform controllerLocalTransform = m_movement;
  83. // Convert the controller's local transform to world space by multiplying with the camera's transform
  84. // Ensure correct order of multiplication: local transform first, then camera transform
  85. AZ::Transform controllerWorldTransform = cameraTransform * controllerLocalTransform;
  86. // Apply the new world transform to the controller's render object
  87. AZ::TransformBus::Event(GetEntityId(), &AZ::TransformBus::Events::SetWorldTM, controllerWorldTransform);
  88. }
  89. static AZ::Transform ReadActionHandlePose(IOpenXRActions* iface, IOpenXRActions::ActionHandle actionHandle)
  90. {
  91. auto outcome = iface->GetActionStatePose(actionHandle);
  92. if (!outcome.IsSuccess())
  93. {
  94. AZ::Transform value = AZ::Transform::CreateIdentity();
  95. value.SetTranslation(AZ::Vector3(-1000, -1000, -1000));
  96. // Most likely the controller went to sleep.
  97. return value;
  98. }
  99. AZ::Transform value = outcome.GetValue();
  100. if (value.GetTranslation().IsClose(AZ::Vector3::CreateZero()))
  101. {
  102. // To avoid rendering controllers when the camera is inside of them
  103. value.SetTranslation(AZ::Vector3(-1000, -1000, -1000));
  104. }
  105. return value;
  106. }
  107. void XRControllerComponent::ProcessOpenXRActions()
  108. {
  109. auto actionsIFace = OpenXRActionsInterface::Get();
  110. if (!actionsIFace)
  111. {
  112. return;
  113. }
  114. if (!m_controllerPoseHandle.IsValid())
  115. {
  116. // Try to cache all handles.
  117. m_controllerPoseHandle = actionsIFace->GetActionHandle("main_action_set", m_controllerPoseActionLabel);
  118. if (!m_controllerPoseHandle.IsValid())
  119. {
  120. // Most likely the Action System failed to load the ActionSets asset.
  121. return;
  122. }
  123. }
  124. m_movement = ReadActionHandlePose(actionsIFace, m_controllerPoseHandle);
  125. }
  126. // Camera::CameraNotificationBus::Handler overrides
  127. void XRControllerComponent::OnActiveViewChanged(const AZ::EntityId& activeEntityId)
  128. {
  129. m_cameraEntity = activeEntityId;
  130. }
  131. } // namespace OpenXRVk