XRInteractableComponent.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 <XRInteractableComponent.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 <Atom/RPI.Public/ViewportContext.h>
  17. #include <Atom/RPI.Public/ViewportContextBus.h>
  18. #include <Atom/RPI.Public/Pass/PassFilter.h>
  19. #include <Atom/RPI.Public/ViewportContextManager.h>
  20. #include <Atom/RPI.Public/AuxGeom/AuxGeomDraw.h>
  21. #include <AzFramework/Components/CameraBus.h>
  22. #include <AzCore/Component/NonUniformScaleBus.h>
  23. #include <AzFramework/Physics/PhysicsScene.h>
  24. #include <AzFramework/Physics/PhysicsSystem.h>
  25. #include <AzFramework/Physics/Common/PhysicsSceneQueries.h>
  26. #include <AzFramework/Physics/Shape.h>
  27. #include <Source/RigidBodyComponent.h>
  28. #include <AzFramework/Physics/SimulatedBodies/RigidBody.h>
  29. namespace OpenXRVk
  30. {
  31. void XRInteractableComponent::Reflect(AZ::ReflectContext* context)
  32. {
  33. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  34. {
  35. serializeContext->Class<XRInteractableComponent, AZ::Component>()
  36. ->Version(1)
  37. ->Field("Type of the interactable", &XRInteractableComponent::m_XRInteractableType)
  38. ;
  39. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  40. {
  41. editContext->Class<XRInteractableComponent>("XR Interactable", "Reacts on Ray Interactor from the XR controller")
  42. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  43. ->Attribute(AZ::Edit::Attributes::Category, "XR")
  44. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  45. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  46. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &XRInteractableComponent::m_XRInteractableType, "Type of the interactable", "Type of the interactable")
  47. ->EnumAttribute(XRInteractableComponent::XRInteractableType::Simple, "Simple")
  48. ;
  49. }
  50. }
  51. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  52. {
  53. behaviorContext->Class<XRInteractableComponent>("XRInteractable Component Group")
  54. ->Attribute(AZ::Script::Attributes::Category, "OpenXRVk Gem Group")
  55. ;
  56. }
  57. }
  58. void XRInteractableComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  59. {
  60. provided.push_back(AZ_CRC_CE("XRInteractableService"));
  61. }
  62. void XRInteractableComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  63. {
  64. incompatible.push_back(AZ_CRC_CE("XRInteractableService"));
  65. }
  66. void XRInteractableComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  67. {
  68. required.push_back(AZ_CRC_CE("TransformService"));
  69. required.push_back(AZ_CRC_CE("PhysicsRigidBodyService"));
  70. }
  71. void XRInteractableComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  72. {
  73. }
  74. void XRInteractableComponent::Init()
  75. {
  76. }
  77. void XRInteractableComponent::Activate()
  78. {
  79. AZ::TickBus::Handler::BusConnect();
  80. Physics::RigidBodyNotificationBus::Handler::BusConnect(GetEntityId());
  81. m_cachedRigidBodyComponent = GetEntity()->FindComponent<PhysX::RigidBodyComponent>();
  82. }
  83. void XRInteractableComponent::Deactivate()
  84. {
  85. if (AZ::TickBus::Handler::BusIsConnected())
  86. {
  87. AZ::TickBus::Handler::BusDisconnect();
  88. }
  89. Physics::RigidBodyNotificationBus::Handler::BusDisconnect();
  90. }
  91. static float SmoothStep(float edge0, float edge1, float t)
  92. {
  93. t = AZ::GetClamp((t - edge0) / (edge1 - edge0), 0.0f, 1.0f);
  94. return t * t * (3.0f - 2.0f * t);
  95. }
  96. void XRInteractableComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  97. {
  98. if (!m_hovering)
  99. return;
  100. constexpr float animationDuration = 0.25f;
  101. constexpr float increaseScaleFactor = 0.3f;
  102. m_hoverTime += deltaTime;
  103. float t = AZ::GetClamp(m_hoverTime / animationDuration, 0.0f, 1.0f);
  104. float factor = m_scalingUp
  105. ? SmoothStep(0.0f, 1.0f, t) * increaseScaleFactor + 1.0f // 1.0 -> 1.increaseScaleFactor
  106. : 1 + increaseScaleFactor - SmoothStep(0.0f, 1.0f, t) * increaseScaleFactor; // 1.increaseScaleFactor -> 1.0
  107. AZ::Vector3 newScale = m_originalScale * factor;
  108. if (t >= 1.0f)
  109. {
  110. if (m_scalingUp)
  111. {
  112. m_scalingUp = false;
  113. m_hoverTime = 0.0f;
  114. }
  115. else
  116. {
  117. newScale = m_originalScale;
  118. m_hovering = false;
  119. }
  120. }
  121. if (m_hasNonUniform)
  122. {
  123. AZ::NonUniformScaleRequestBus::Event(GetEntityId(), &AZ::NonUniformScaleRequests::SetScale, newScale);
  124. }
  125. else
  126. {
  127. AZ::TransformBus::Event(GetEntityId(), &AZ::TransformInterface::SetLocalUniformScale, newScale.GetX());
  128. }
  129. }
  130. // Called when the pointer is hovering over this object
  131. void XRInteractableComponent::OnHoverStart()
  132. {
  133. if (m_hovering)
  134. {
  135. return;
  136. }
  137. m_hoverTime = 0.0f;
  138. m_hovering = true;
  139. m_scalingUp = true;
  140. if (!m_originalScaleCached)
  141. {
  142. CacheOriginalScale();
  143. }
  144. }
  145. // Called when the pointer stops hovering over this object
  146. void XRInteractableComponent::OnHoverEnd()
  147. {
  148. }
  149. void XRInteractableComponent::CacheOriginalScale()
  150. {
  151. m_originalScale = AZ::Vector3::CreateOne();
  152. m_hasNonUniform = false;
  153. AZ::Vector3 testScale = AZ::Vector3::CreateOne();
  154. AZ::NonUniformScaleRequestBus::EventResult(testScale, GetEntityId(), &AZ::NonUniformScaleRequests::GetScale);
  155. if (!testScale.IsClose(AZ::Vector3::CreateOne()))
  156. {
  157. m_hasNonUniform = true;
  158. m_originalScale = testScale;
  159. }
  160. else
  161. {
  162. float uniform = 1.0f;
  163. AZ::TransformBus::EventResult(uniform, GetEntityId(), &AZ::TransformInterface::GetLocalUniformScale);
  164. m_originalScale = AZ::Vector3(uniform);
  165. }
  166. m_originalScaleCached = true;
  167. }
  168. void XRInteractableComponent::OnGrab()
  169. {
  170. m_isGrabbing = true;
  171. m_cachedRigidBodyComponent->DisablePhysics();
  172. }
  173. void XRInteractableComponent::OnRelease(const AZ::Vector3& impulse)
  174. {
  175. m_cachedRigidBodyComponent->EnablePhysics();
  176. m_cachedRigidBodyComponent->ApplyLinearImpulse(impulse * m_cachedRigidBodyComponent->GetMass());
  177. m_isGrabbing = false;
  178. }
  179. } // namespace OpenXRVk