| 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 | |
| 31 | #include "openxr_eye_gaze_interaction.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/os/os.h" |
| 35 | |
| 36 | #include "../action_map/openxr_interaction_profile_metadata.h" |
| 37 | #include "../openxr_api.h" |
| 38 | |
| 39 | OpenXREyeGazeInteractionExtension *OpenXREyeGazeInteractionExtension::singleton = nullptr; |
| 40 | |
| 41 | OpenXREyeGazeInteractionExtension *OpenXREyeGazeInteractionExtension::get_singleton() { |
| 42 | ERR_FAIL_NULL_V(singleton, nullptr); |
| 43 | return singleton; |
| 44 | } |
| 45 | |
| 46 | OpenXREyeGazeInteractionExtension::OpenXREyeGazeInteractionExtension() { |
| 47 | singleton = this; |
| 48 | } |
| 49 | |
| 50 | OpenXREyeGazeInteractionExtension::~OpenXREyeGazeInteractionExtension() { |
| 51 | singleton = nullptr; |
| 52 | } |
| 53 | |
| 54 | HashMap<String, bool *> OpenXREyeGazeInteractionExtension::get_requested_extensions(XrVersion p_version) { |
| 55 | HashMap<String, bool *> request_extensions; |
| 56 | |
| 57 | // Only enable this extension when requested. |
| 58 | // We still register our meta data or the action map editor will fail. |
| 59 | if (GLOBAL_GET_CACHED(bool, "xr/openxr/extensions/eye_gaze_interaction") && (!OS::get_singleton()->has_feature("mobile") || OS::get_singleton()->has_feature(XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME))) { |
| 60 | request_extensions[XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME] = &available; |
| 61 | } |
| 62 | |
| 63 | return request_extensions; |
| 64 | } |
| 65 | |
| 66 | void *OpenXREyeGazeInteractionExtension::set_system_properties_and_get_next_pointer(void *p_next_pointer) { |
| 67 | if (!available) { |
| 68 | return p_next_pointer; |
| 69 | } |
| 70 | |
| 71 | properties.type = XR_TYPE_SYSTEM_EYE_GAZE_INTERACTION_PROPERTIES_EXT; |
| 72 | properties.next = p_next_pointer; |
| 73 | properties.supportsEyeGazeInteraction = false; |
| 74 | |
| 75 | return &properties; |
| 76 | } |
| 77 | |
| 78 | PackedStringArray OpenXREyeGazeInteractionExtension::get_suggested_tracker_names() { |
| 79 | PackedStringArray arr = { "/user/eyes_ext" }; |
| 80 | return arr; |
| 81 | } |
| 82 | |
| 83 | bool OpenXREyeGazeInteractionExtension::is_available() { |
| 84 | return available; |
| 85 | } |
| 86 | |
| 87 | bool OpenXREyeGazeInteractionExtension::supports_eye_gaze_interaction() { |
| 88 | // The extension being available only means that the OpenXR Runtime supports the extension. |
| 89 | // The `supportsEyeGazeInteraction` is set to true if the device also supports this. |
| 90 | // Thus both need to be true. |
| 91 | // In addition, on mobile runtimes, the proper permission needs to be granted. |
| 92 | if (available && properties.supportsEyeGazeInteraction) { |
| 93 | return !OS::get_singleton()->has_feature("mobile") || OS::get_singleton()->has_feature("PERMISSION_XR_EXT_eye_gaze_interaction"); |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | void OpenXREyeGazeInteractionExtension::on_register_metadata() { |
| 100 | OpenXRInteractionProfileMetadata *openxr_metadata = OpenXRInteractionProfileMetadata::get_singleton(); |
| 101 | ERR_FAIL_NULL(openxr_metadata); |
| 102 | |
| 103 | // Eyes top path |
| 104 | openxr_metadata->register_top_level_path("Eye gaze tracker", "/user/eyes_ext", XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME); |
| 105 | |
| 106 | // Eye gaze interaction |
| 107 | openxr_metadata->register_interaction_profile("Eye gaze", "/interaction_profiles/ext/eye_gaze_interaction", XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME); |
| 108 | openxr_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); |
| 109 | } |
| 110 | |
| 111 | bool OpenXREyeGazeInteractionExtension::get_eye_gaze_pose(double p_dist, Vector3 &r_eye_pose) { |
| 112 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 113 | ERR_FAIL_NULL_V(openxr_api, false); |
| 114 | |
| 115 | if (!init_eye_gaze_pose) { |
| 116 | init_eye_gaze_pose = true; |
| 117 | |
| 118 | eye_tracker = openxr_api->find_tracker("/user/eyes_ext"); |
| 119 | if (eye_tracker.is_null()) { |
| 120 | WARN_PRINT("Couldn't obtain eye tracker"); |
| 121 | } |
| 122 | |
| 123 | eye_action = openxr_api->find_action("eye_gaze_pose"); |
| 124 | if (eye_action.is_null()) { |
| 125 | WARN_PRINT("Couldn't obtain pose action for `eye_gaze_pose`, make sure to add this to your action map."); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (eye_tracker.is_null() || eye_action.is_null()) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | Transform3D eye_transform; |
| 134 | Vector3 linear_velocity; |
| 135 | Vector3 angular_velocity; |
| 136 | XRPose::TrackingConfidence confidence = openxr_api->get_action_pose(eye_action, eye_tracker, eye_transform, linear_velocity, angular_velocity); |
| 137 | if (confidence == XRPose::XR_TRACKING_CONFIDENCE_NONE) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | r_eye_pose = eye_transform.origin + eye_transform.basis[2] * p_dist; |
| 142 | |
| 143 | return true; |
| 144 | } |