XRRayInteractorComponent.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <XRRayInteractorComponent.h>
  9. #include <XRInteractableComponent.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/RTTI/BehaviorContext.h>
  14. #include <OpenXRVk/OpenXRVkActionsInterface.h>
  15. #include <Atom/RPI.Public/ViewProviderBus.h>
  16. #include <Atom/RPI.Public/View.h>
  17. #include <Atom/RPI.Public/ViewportContext.h>
  18. #include <Atom/RPI.Public/ViewportContextBus.h>
  19. #include <Atom/RPI.Public/Pass/PassFilter.h>
  20. #include <Atom/RPI.Public/ViewportContextManager.h>
  21. #include <Atom/RPI.Public/AuxGeom/AuxGeomDraw.h>
  22. #include <AzFramework/Components/CameraBus.h>
  23. #include <AzCore/Component/NonUniformScaleBus.h>
  24. #include <AzFramework/Physics/PhysicsScene.h>
  25. #include <AzFramework/Physics/PhysicsSystem.h>
  26. #include <AzFramework/Physics/Common/PhysicsSceneQueries.h>
  27. #include <AzFramework/Physics/Shape.h>
  28. #include <Atom/RPI.Public/Material/Material.h>
  29. #include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentBus.h>
  30. #include <AzCore/Name/Name.h>
  31. #include <AzFramework/Physics/RigidBodyBus.h>
  32. #include <AzFramework/Physics/SimulatedBodies/RigidBody.h>
  33. namespace OpenXRVk
  34. {
  35. const AZStd::string baseColorPropertyName = "baseColor.color";
  36. const float translationToYScaleCoefficent = 100;
  37. const float hoveringXZScaleCoefficient = 4;
  38. void XRRayInteractorComponent::Reflect(AZ::ReflectContext* context)
  39. {
  40. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  41. {
  42. serializeContext->Class<XRRayInteractorComponent, AZ::Component>()
  43. ->Version(1)
  44. ->Field("Max length of the ray", &XRRayInteractorComponent::m_maxLength)
  45. ->Field("The default color of the ray", &XRRayInteractorComponent::m_initialRayColor)
  46. ->Field("The color when the ray hovers over some interactable object", &XRRayInteractorComponent::m_hoveringRayColor)
  47. ->Field("Label of the grip button", &XRRayInteractorComponent::m_gripControlActionLabel)
  48. ;
  49. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  50. {
  51. editContext->Class<XRRayInteractorComponent>("XR Ray Interactor", "Draws ray interactor from the XR controller")
  52. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  53. ->Attribute(AZ::Edit::Attributes::Category, "XR")
  54. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  55. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  56. ->DataElement(AZ::Edit::UIHandlers::Default, &XRRayInteractorComponent::m_maxLength, "Max length of the ray", "Max length of the ray in meters")
  57. ->DataElement(AZ::Edit::UIHandlers::Default, &XRRayInteractorComponent::m_initialRayColor, "Default color", "The default color of the ray")
  58. ->DataElement(AZ::Edit::UIHandlers::Default, &XRRayInteractorComponent::m_hoveringRayColor, "Hovering color", "The color when the ray hovers over some interactable object")
  59. ->DataElement(AZ::Edit::UIHandlers::Default, &XRRayInteractorComponent::m_gripControlActionLabel, "Action name of the grip", "Action Handle Label of OpenXRActionsInterface")
  60. ;
  61. }
  62. }
  63. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  64. {
  65. behaviorContext->Class<XRRayInteractorComponent>("XRRayInteractor Component Group")
  66. ->Attribute(AZ::Script::Attributes::Category, "OpenXRVk Gem Group")
  67. ;
  68. }
  69. }
  70. void XRRayInteractorComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  71. {
  72. provided.push_back(AZ_CRC_CE("XRRayInteractorService"));
  73. }
  74. void XRRayInteractorComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  75. {
  76. incompatible.push_back(AZ_CRC_CE("XRRayInteractorService"));
  77. }
  78. void XRRayInteractorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  79. {
  80. required.push_back(AZ_CRC_CE("TransformService"));
  81. }
  82. void XRRayInteractorComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  83. {
  84. }
  85. void XRRayInteractorComponent::Init()
  86. {
  87. }
  88. void XRRayInteractorComponent::Activate()
  89. {
  90. AZ::TickBus::Handler::BusConnect();
  91. }
  92. void XRRayInteractorComponent::Deactivate()
  93. {
  94. if (AZ::TickBus::Handler::BusIsConnected())
  95. {
  96. AZ::TickBus::Handler::BusDisconnect();
  97. }
  98. }
  99. void XRRayInteractorComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  100. {
  101. if (!m_colorDefined)
  102. {
  103. AZ::Render::MaterialAssignmentMap originalMaterials;
  104. AZ::Render::MaterialComponentRequestBus::EventResult(
  105. originalMaterials, GetEntityId(), &AZ::Render::MaterialComponentRequestBus::Events::GetMaterialMap);
  106. if (!originalMaterials.empty())
  107. {
  108. for (const auto& [materialId, assignment] : originalMaterials)
  109. {
  110. auto materialAsset = assignment.m_materialAsset;
  111. if (!materialAsset.IsReady())
  112. continue;
  113. // Waiting when the asset is ready
  114. auto const pIndex = assignment.m_materialInstance->FindPropertyIndex(AZ::Name("baseColor.color"));
  115. if (!pIndex.IsValid())
  116. continue;
  117. // Let's find the right material
  118. assignmentId = AZ::Render::MaterialAssignmentId::CreateDefault();
  119. AZ::Render::MaterialComponentRequestBus::Event(
  120. GetEntityId(),
  121. &AZ::Render::MaterialComponentRequestBus::Events::SetPropertyValueT<AZ::Color>,
  122. assignmentId, baseColorPropertyName, m_initialRayColor);
  123. m_colorDefined = true;
  124. }
  125. }
  126. }
  127. ProcessOpenXRActions();
  128. if (m_heldEntity.IsValid())
  129. {
  130. // Update held object's position relative to ray
  131. AZ::Transform rayTransform;
  132. AZ::TransformBus::EventResult(rayTransform, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  133. AZ::Transform targetTransform = rayTransform * m_grabOffset;
  134. AZ::TransformBus::Event(m_heldEntity, &AZ::TransformBus::Events::SetWorldTM, targetTransform);
  135. // Track velocity
  136. AZ::Vector3 currentPosition = targetTransform.GetTranslation();
  137. m_currentVelocity = (currentPosition - m_lastFramePosition) / deltaTime;
  138. m_lastFramePosition = currentPosition;
  139. // Release object when button is released
  140. if (m_currentSqueezeValue < 0.1f)
  141. {
  142. ReleaseHeldEntity();
  143. }
  144. return;
  145. }
  146. else if (m_currentSqueezeValue > 0.9f && m_currentlyHoveredEntity.IsValid())
  147. {
  148. GrabHoveredEntity();
  149. return;
  150. }
  151. else {
  152. m_heldEntity.SetInvalid();
  153. }
  154. // Store initial nonUniform scale X and Z
  155. AZ::Vector3 nonUniformScale;
  156. AZ::NonUniformScaleRequestBus::EventResult(nonUniformScale, GetEntityId(), &AZ::NonUniformScaleRequestBus::Events::GetScale);
  157. if (m_XZnonUniformScale == 0 && nonUniformScale.GetX() > 0)
  158. {
  159. m_XZnonUniformScale = nonUniformScale.GetX();
  160. }
  161. // Get the world transform of the current entity (typically the controller)
  162. AZ::Transform rayOriginTransform;
  163. AZ::TransformBus::EventResult(rayOriginTransform, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  164. // Define the ray start position and normalized direction
  165. AZ::Vector3 start = rayOriginTransform.GetTranslation();
  166. AZ::Vector3 direction = rayOriginTransform.GetBasisY().GetNormalized();
  167. // Maximum ray length
  168. float maxDistance = m_maxLength;
  169. // Get the default physics scene
  170. auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
  171. AzPhysics::SceneHandle sceneHandle = sceneInterface->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
  172. if (sceneHandle == AzPhysics::InvalidSceneHandle)
  173. {
  174. AZ_Warning("XRRayInteractorComponent", false, "Invalid physics scene.");
  175. return;
  176. }
  177. // Prepare the raycast request
  178. AzPhysics::RayCastRequest request;
  179. request.m_start = start;
  180. request.m_direction = direction;
  181. request.m_distance = maxDistance;
  182. request.m_reportMultipleHits = false;
  183. // Perform the raycast query
  184. AzPhysics::SceneQueryHits hitResult;
  185. sceneInterface->QueryScene(sceneHandle, &request, hitResult);
  186. // Handle the result of the raycast
  187. if (!hitResult.m_hits.empty())
  188. {
  189. AzPhysics::SceneQueryHit &hit = hitResult.m_hits[0];
  190. // You can use hitResult.m_position or hitResult.m_entityId for further logic
  191. if (hit.m_entityId.IsValid())
  192. {
  193. if (m_currentlyHoveredEntity != hit.m_entityId)
  194. {
  195. CheckEndHovering();
  196. XRInteractableComponent* xrInteractableComponent = GetXRInterctableComponent(hit.m_entityId);
  197. if (xrInteractableComponent) // Interactable component has changed
  198. {
  199. AZ::Render::MaterialComponentRequestBus::Event(
  200. GetEntityId(),
  201. &AZ::Render::MaterialComponentRequestBus::Events::SetPropertyValueT<AZ::Color>,
  202. assignmentId, baseColorPropertyName, m_hoveringRayColor);
  203. xrInteractableComponent->OnHoverStart();
  204. m_currentlyHoveredEntity = hit.m_entityId;
  205. }
  206. }
  207. }
  208. else
  209. {
  210. CheckEndHovering();
  211. }
  212. float xzScale = m_currentlyHoveredEntity.IsValid() ? m_XZnonUniformScale * hoveringXZScaleCoefficient : m_XZnonUniformScale;
  213. nonUniformScale.Set(xzScale, hit.m_distance * translationToYScaleCoefficent, xzScale);
  214. AZ::NonUniformScaleRequestBus::Event(GetEntityId(), &AZ::NonUniformScaleRequestBus::Events::SetScale, nonUniformScale);
  215. }
  216. else
  217. {
  218. // Restore initial scale of the ray
  219. nonUniformScale.Set(m_XZnonUniformScale, m_maxLength* translationToYScaleCoefficent, m_XZnonUniformScale);
  220. AZ::NonUniformScaleRequestBus::Event(GetEntityId(), &AZ::NonUniformScaleRequestBus::Events::SetScale, nonUniformScale);
  221. CheckEndHovering();
  222. }
  223. }
  224. void XRRayInteractorComponent::CheckEndHovering()
  225. {
  226. if (m_currentlyHoveredEntity.IsValid())
  227. {
  228. XRInteractableComponent* oldCRInteractableComponent = GetXRInterctableComponent(m_currentlyHoveredEntity);
  229. if (oldCRInteractableComponent)
  230. {
  231. oldCRInteractableComponent->OnHoverEnd();
  232. m_currentlyHoveredEntity.SetInvalid();
  233. AZ::Render::MaterialComponentRequestBus::Event(
  234. GetEntityId(),
  235. &AZ::Render::MaterialComponentRequestBus::Events::SetPropertyValueT<AZ::Color>,
  236. assignmentId, baseColorPropertyName, m_initialRayColor);
  237. }
  238. }
  239. }
  240. static float ReadActionHandleFloat(IOpenXRActions* iface, IOpenXRActions::ActionHandle actionHandle, float deadZone = 0.05f)
  241. {
  242. auto outcome = iface->GetActionStateFloat(actionHandle);
  243. if (!outcome.IsSuccess())
  244. {
  245. // Most likely the controller went to sleep.
  246. return 0.0f;
  247. }
  248. float value = outcome.GetValue();
  249. if (fabsf(value) < deadZone)
  250. {
  251. return 0.0f;
  252. }
  253. return value;
  254. }
  255. void XRRayInteractorComponent::ProcessOpenXRActions()
  256. {
  257. m_currentSqueezeValue = 0;
  258. auto actionsIFace = OpenXRActionsInterface::Get();
  259. if (!actionsIFace)
  260. {
  261. return;
  262. }
  263. if (!m_controllerSqueezeHandle.IsValid())
  264. {
  265. // Try to cache all handles.
  266. m_controllerSqueezeHandle = actionsIFace->GetActionHandle("main_action_set", m_gripControlActionLabel);
  267. if (!m_controllerSqueezeHandle.IsValid())
  268. {
  269. // Most likely the Action System failed to load the ActionSets asset.
  270. return;
  271. }
  272. }
  273. m_currentSqueezeValue = ReadActionHandleFloat(actionsIFace, m_controllerSqueezeHandle);
  274. }
  275. void XRRayInteractorComponent::GrabHoveredEntity()
  276. {
  277. m_heldEntity = m_currentlyHoveredEntity;
  278. AZ::Transform controllerWorldTM;
  279. AZ::Transform objectWorldTM;
  280. AZ::TransformBus::EventResult(controllerWorldTM, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  281. AZ::TransformBus::EventResult(objectWorldTM, m_heldEntity, &AZ::TransformBus::Events::GetWorldTM);
  282. m_grabOffset = controllerWorldTM.GetInverse() * objectWorldTM;
  283. m_lastFramePosition = objectWorldTM.GetTranslation();
  284. m_currentVelocity = AZ::Vector3::CreateZero();
  285. XRInteractableComponent* xrInteractableComponent = GetXRInterctableComponent(m_heldEntity);
  286. if (xrInteractableComponent)
  287. {
  288. xrInteractableComponent->OnGrab();
  289. }
  290. }
  291. void XRRayInteractorComponent::ReleaseHeldEntity()
  292. {
  293. // Call OnRelease on the XRInteractableComponent if available
  294. XRInteractableComponent* xrInteractableComponent = GetXRInterctableComponent(m_heldEntity);
  295. if (xrInteractableComponent)
  296. {
  297. xrInteractableComponent->OnRelease(m_currentVelocity);
  298. }
  299. m_heldEntity.SetInvalid();
  300. }
  301. XRInteractableComponent* XRRayInteractorComponent::GetXRInterctableComponent(AZ::EntityId entityId)
  302. {
  303. AZ::Entity* entity = nullptr;
  304. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, entityId);
  305. if (entity)
  306. {
  307. return entity->FindComponent<XRInteractableComponent>();
  308. }
  309. return nullptr;
  310. }
  311. } // namespace OpenXRVk