openxr_eye_gaze_interaction.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**************************************************************************/
  2. /* openxr_eye_gaze_interaction.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "openxr_eye_gaze_interaction.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "../action_map/openxr_interaction_profile_metadata.h"
  34. #include "../openxr_api.h"
  35. OpenXREyeGazeInteractionExtension *OpenXREyeGazeInteractionExtension::singleton = nullptr;
  36. OpenXREyeGazeInteractionExtension *OpenXREyeGazeInteractionExtension::get_singleton() {
  37. ERR_FAIL_NULL_V(singleton, nullptr);
  38. return singleton;
  39. }
  40. OpenXREyeGazeInteractionExtension::OpenXREyeGazeInteractionExtension() {
  41. singleton = this;
  42. }
  43. OpenXREyeGazeInteractionExtension::~OpenXREyeGazeInteractionExtension() {
  44. singleton = nullptr;
  45. }
  46. HashMap<String, bool *> OpenXREyeGazeInteractionExtension::get_requested_extensions() {
  47. HashMap<String, bool *> request_extensions;
  48. // Only enable this extension when requested.
  49. // We still register our meta data or the action map editor will fail.
  50. if (GLOBAL_GET("xr/openxr/extensions/eye_gaze_interaction") && (!OS::get_singleton()->has_feature("mobile") || OS::get_singleton()->has_feature(XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME))) {
  51. request_extensions[XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME] = &available;
  52. }
  53. return request_extensions;
  54. }
  55. void *OpenXREyeGazeInteractionExtension::set_system_properties_and_get_next_pointer(void *p_next_pointer) {
  56. if (!available) {
  57. return p_next_pointer;
  58. }
  59. properties.type = XR_TYPE_SYSTEM_EYE_GAZE_INTERACTION_PROPERTIES_EXT;
  60. properties.next = p_next_pointer;
  61. properties.supportsEyeGazeInteraction = false;
  62. return &properties;
  63. }
  64. PackedStringArray OpenXREyeGazeInteractionExtension::get_suggested_tracker_names() {
  65. PackedStringArray arr = { "/user/eyes_ext" };
  66. return arr;
  67. }
  68. bool OpenXREyeGazeInteractionExtension::is_available() {
  69. return available;
  70. }
  71. bool OpenXREyeGazeInteractionExtension::supports_eye_gaze_interaction() {
  72. // The extension being available only means that the OpenXR Runtime supports the extension.
  73. // The `supportsEyeGazeInteraction` is set to true if the device also supports this.
  74. // Thus both need to be true.
  75. // In addition, on mobile runtimes, the proper permission needs to be granted.
  76. if (available && properties.supportsEyeGazeInteraction) {
  77. return !OS::get_singleton()->has_feature("mobile") || OS::get_singleton()->has_feature("PERMISSION_XR_EXT_eye_gaze_interaction");
  78. }
  79. return false;
  80. }
  81. void OpenXREyeGazeInteractionExtension::on_register_metadata() {
  82. OpenXRInteractionProfileMetadata *metadata = OpenXRInteractionProfileMetadata::get_singleton();
  83. ERR_FAIL_NULL(metadata);
  84. // Eyes top path
  85. metadata->register_top_level_path("Eye gaze tracker", "/user/eyes_ext", XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME);
  86. // Eye gaze interaction
  87. metadata->register_interaction_profile("Eye gaze", "/interaction_profiles/ext/eye_gaze_interaction", XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME);
  88. metadata->register_io_path("/interaction_profiles/ext/eye_gaze_interaction", "Gaze pose", "/user/eyes_ext", "/user/eyes_ext/input/gaze_ext/pose", "", OpenXRAction::OPENXR_ACTION_POSE);
  89. }
  90. bool OpenXREyeGazeInteractionExtension::get_eye_gaze_pose(double p_dist, Vector3 &r_eye_pose) {
  91. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  92. ERR_FAIL_NULL_V(openxr_api, false);
  93. if (!init_eye_gaze_pose) {
  94. init_eye_gaze_pose = true;
  95. eye_tracker = openxr_api->find_tracker("/user/eyes_ext");
  96. if (eye_tracker.is_null()) {
  97. WARN_PRINT("Couldn't obtain eye tracker");
  98. }
  99. eye_action = openxr_api->find_action("eye_gaze_pose");
  100. if (eye_action.is_null()) {
  101. WARN_PRINT("Couldn't obtain pose action for `eye_gaze_pose`, make sure to add this to your action map.");
  102. }
  103. }
  104. if (eye_tracker.is_null() || eye_action.is_null()) {
  105. return false;
  106. }
  107. Transform3D eye_transform;
  108. Vector3 linear_velocity;
  109. Vector3 angular_velocity;
  110. XRPose::TrackingConfidence confidence = openxr_api->get_action_pose(eye_action, eye_tracker, eye_transform, linear_velocity, angular_velocity);
  111. if (confidence == XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  112. return false;
  113. }
  114. r_eye_pose = eye_transform.origin + eye_transform.basis[2] * p_dist;
  115. return true;
  116. }