openxr_interface.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. /**************************************************************************/
  2. /* openxr_interface.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_interface.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/io/resource_saver.h"
  33. #include "extensions/openxr_eye_gaze_interaction.h"
  34. #include "extensions/openxr_hand_interaction_extension.h"
  35. #include "extensions/openxr_performance_settings_extension.h"
  36. #include "servers/rendering/renderer_compositor.h"
  37. #include <openxr/openxr.h>
  38. void OpenXRInterface::_bind_methods() {
  39. // lifecycle signals
  40. ADD_SIGNAL(MethodInfo("session_begun"));
  41. ADD_SIGNAL(MethodInfo("session_stopping"));
  42. ADD_SIGNAL(MethodInfo("session_focussed"));
  43. ADD_SIGNAL(MethodInfo("session_visible"));
  44. ADD_SIGNAL(MethodInfo("session_loss_pending"));
  45. ADD_SIGNAL(MethodInfo("instance_exiting"));
  46. ADD_SIGNAL(MethodInfo("pose_recentered"));
  47. ADD_SIGNAL(MethodInfo("refresh_rate_changed", PropertyInfo(Variant::FLOAT, "refresh_rate")));
  48. ADD_SIGNAL(MethodInfo("cpu_level_changed", PropertyInfo(Variant::INT, "sub_domain"), PropertyInfo(Variant::INT, "from_level"), PropertyInfo(Variant::INT, "to_level")));
  49. ADD_SIGNAL(MethodInfo("gpu_level_changed", PropertyInfo(Variant::INT, "sub_domain"), PropertyInfo(Variant::INT, "from_level"), PropertyInfo(Variant::INT, "to_level")));
  50. // Display refresh rate
  51. ClassDB::bind_method(D_METHOD("get_display_refresh_rate"), &OpenXRInterface::get_display_refresh_rate);
  52. ClassDB::bind_method(D_METHOD("set_display_refresh_rate", "refresh_rate"), &OpenXRInterface::set_display_refresh_rate);
  53. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "display_refresh_rate"), "set_display_refresh_rate", "get_display_refresh_rate");
  54. // Render Target size multiplier
  55. ClassDB::bind_method(D_METHOD("get_render_target_size_multiplier"), &OpenXRInterface::get_render_target_size_multiplier);
  56. ClassDB::bind_method(D_METHOD("set_render_target_size_multiplier", "multiplier"), &OpenXRInterface::set_render_target_size_multiplier);
  57. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "render_target_size_multiplier"), "set_render_target_size_multiplier", "get_render_target_size_multiplier");
  58. // Foveation level
  59. ClassDB::bind_method(D_METHOD("is_foveation_supported"), &OpenXRInterface::is_foveation_supported);
  60. ClassDB::bind_method(D_METHOD("get_foveation_level"), &OpenXRInterface::get_foveation_level);
  61. ClassDB::bind_method(D_METHOD("set_foveation_level", "foveation_level"), &OpenXRInterface::set_foveation_level);
  62. ADD_PROPERTY(PropertyInfo(Variant::INT, "foveation_level"), "set_foveation_level", "get_foveation_level");
  63. ClassDB::bind_method(D_METHOD("get_foveation_dynamic"), &OpenXRInterface::get_foveation_dynamic);
  64. ClassDB::bind_method(D_METHOD("set_foveation_dynamic", "foveation_dynamic"), &OpenXRInterface::set_foveation_dynamic);
  65. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "foveation_dynamic"), "set_foveation_dynamic", "get_foveation_dynamic");
  66. // Action sets
  67. ClassDB::bind_method(D_METHOD("is_action_set_active", "name"), &OpenXRInterface::is_action_set_active);
  68. ClassDB::bind_method(D_METHOD("set_action_set_active", "name", "active"), &OpenXRInterface::set_action_set_active);
  69. ClassDB::bind_method(D_METHOD("get_action_sets"), &OpenXRInterface::get_action_sets);
  70. // Refresh rates
  71. ClassDB::bind_method(D_METHOD("get_available_display_refresh_rates"), &OpenXRInterface::get_available_display_refresh_rates);
  72. // Hand tracking.
  73. ClassDB::bind_method(D_METHOD("set_motion_range", "hand", "motion_range"), &OpenXRInterface::set_motion_range);
  74. ClassDB::bind_method(D_METHOD("get_motion_range", "hand"), &OpenXRInterface::get_motion_range);
  75. ClassDB::bind_method(D_METHOD("get_hand_tracking_source", "hand"), &OpenXRInterface::get_hand_tracking_source);
  76. ClassDB::bind_method(D_METHOD("get_hand_joint_flags", "hand", "joint"), &OpenXRInterface::get_hand_joint_flags);
  77. ClassDB::bind_method(D_METHOD("get_hand_joint_rotation", "hand", "joint"), &OpenXRInterface::get_hand_joint_rotation);
  78. ClassDB::bind_method(D_METHOD("get_hand_joint_position", "hand", "joint"), &OpenXRInterface::get_hand_joint_position);
  79. ClassDB::bind_method(D_METHOD("get_hand_joint_radius", "hand", "joint"), &OpenXRInterface::get_hand_joint_radius);
  80. ClassDB::bind_method(D_METHOD("get_hand_joint_linear_velocity", "hand", "joint"), &OpenXRInterface::get_hand_joint_linear_velocity);
  81. ClassDB::bind_method(D_METHOD("get_hand_joint_angular_velocity", "hand", "joint"), &OpenXRInterface::get_hand_joint_angular_velocity);
  82. ClassDB::bind_method(D_METHOD("is_hand_tracking_supported"), &OpenXRInterface::is_hand_tracking_supported);
  83. ClassDB::bind_method(D_METHOD("is_hand_interaction_supported"), &OpenXRInterface::is_hand_interaction_supported);
  84. ClassDB::bind_method(D_METHOD("is_eye_gaze_interaction_supported"), &OpenXRInterface::is_eye_gaze_interaction_supported);
  85. // VRS
  86. ClassDB::bind_method(D_METHOD("get_vrs_min_radius"), &OpenXRInterface::get_vrs_min_radius);
  87. ClassDB::bind_method(D_METHOD("set_vrs_min_radius", "radius"), &OpenXRInterface::set_vrs_min_radius);
  88. ClassDB::bind_method(D_METHOD("get_vrs_strength"), &OpenXRInterface::get_vrs_strength);
  89. ClassDB::bind_method(D_METHOD("set_vrs_strength", "strength"), &OpenXRInterface::set_vrs_strength);
  90. // Performance settings.
  91. ClassDB::bind_method(D_METHOD("set_cpu_level", "level"), &OpenXRInterface::set_cpu_level);
  92. ClassDB::bind_method(D_METHOD("set_gpu_level", "level"), &OpenXRInterface::set_gpu_level);
  93. ADD_GROUP("Vulkan VRS", "vrs_");
  94. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "vrs_min_radius", PROPERTY_HINT_RANGE, "1.0,100.0,1.0"), "set_vrs_min_radius", "get_vrs_min_radius");
  95. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "vrs_strength", PROPERTY_HINT_RANGE, "0.1,10.0,0.1"), "set_vrs_strength", "get_vrs_strength");
  96. BIND_ENUM_CONSTANT(HAND_LEFT);
  97. BIND_ENUM_CONSTANT(HAND_RIGHT);
  98. BIND_ENUM_CONSTANT(HAND_MAX);
  99. BIND_ENUM_CONSTANT(HAND_MOTION_RANGE_UNOBSTRUCTED);
  100. BIND_ENUM_CONSTANT(HAND_MOTION_RANGE_CONFORM_TO_CONTROLLER);
  101. BIND_ENUM_CONSTANT(HAND_MOTION_RANGE_MAX);
  102. BIND_ENUM_CONSTANT(HAND_TRACKED_SOURCE_UNKNOWN);
  103. BIND_ENUM_CONSTANT(HAND_TRACKED_SOURCE_UNOBSTRUCTED);
  104. BIND_ENUM_CONSTANT(HAND_TRACKED_SOURCE_CONTROLLER);
  105. BIND_ENUM_CONSTANT(HAND_TRACKED_SOURCE_MAX);
  106. BIND_ENUM_CONSTANT(HAND_JOINT_PALM);
  107. BIND_ENUM_CONSTANT(HAND_JOINT_WRIST);
  108. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_METACARPAL);
  109. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_PROXIMAL);
  110. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_DISTAL);
  111. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_TIP);
  112. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_METACARPAL);
  113. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_PROXIMAL);
  114. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_INTERMEDIATE);
  115. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_DISTAL);
  116. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_TIP);
  117. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_METACARPAL);
  118. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_PROXIMAL);
  119. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_INTERMEDIATE);
  120. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_DISTAL);
  121. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_TIP);
  122. BIND_ENUM_CONSTANT(HAND_JOINT_RING_METACARPAL);
  123. BIND_ENUM_CONSTANT(HAND_JOINT_RING_PROXIMAL);
  124. BIND_ENUM_CONSTANT(HAND_JOINT_RING_INTERMEDIATE);
  125. BIND_ENUM_CONSTANT(HAND_JOINT_RING_DISTAL);
  126. BIND_ENUM_CONSTANT(HAND_JOINT_RING_TIP);
  127. BIND_ENUM_CONSTANT(HAND_JOINT_LITTLE_METACARPAL);
  128. BIND_ENUM_CONSTANT(HAND_JOINT_LITTLE_PROXIMAL);
  129. BIND_ENUM_CONSTANT(HAND_JOINT_LITTLE_INTERMEDIATE);
  130. BIND_ENUM_CONSTANT(HAND_JOINT_LITTLE_DISTAL);
  131. BIND_ENUM_CONSTANT(HAND_JOINT_LITTLE_TIP);
  132. BIND_ENUM_CONSTANT(HAND_JOINT_MAX);
  133. BIND_ENUM_CONSTANT(PERF_SETTINGS_LEVEL_POWER_SAVINGS);
  134. BIND_ENUM_CONSTANT(PERF_SETTINGS_LEVEL_SUSTAINED_LOW);
  135. BIND_ENUM_CONSTANT(PERF_SETTINGS_LEVEL_SUSTAINED_HIGH);
  136. BIND_ENUM_CONSTANT(PERF_SETTINGS_LEVEL_BOOST);
  137. BIND_ENUM_CONSTANT(PERF_SETTINGS_SUB_DOMAIN_COMPOSITING);
  138. BIND_ENUM_CONSTANT(PERF_SETTINGS_SUB_DOMAIN_RENDERING);
  139. BIND_ENUM_CONSTANT(PERF_SETTINGS_SUB_DOMAIN_THERMAL);
  140. BIND_ENUM_CONSTANT(PERF_SETTINGS_NOTIF_LEVEL_NORMAL);
  141. BIND_ENUM_CONSTANT(PERF_SETTINGS_NOTIF_LEVEL_WARNING);
  142. BIND_ENUM_CONSTANT(PERF_SETTINGS_NOTIF_LEVEL_IMPAIRED);
  143. BIND_BITFIELD_FLAG(HAND_JOINT_NONE);
  144. BIND_BITFIELD_FLAG(HAND_JOINT_ORIENTATION_VALID);
  145. BIND_BITFIELD_FLAG(HAND_JOINT_ORIENTATION_TRACKED);
  146. BIND_BITFIELD_FLAG(HAND_JOINT_POSITION_VALID);
  147. BIND_BITFIELD_FLAG(HAND_JOINT_POSITION_TRACKED);
  148. BIND_BITFIELD_FLAG(HAND_JOINT_LINEAR_VELOCITY_VALID);
  149. BIND_BITFIELD_FLAG(HAND_JOINT_ANGULAR_VELOCITY_VALID);
  150. }
  151. StringName OpenXRInterface::get_name() const {
  152. return StringName("OpenXR");
  153. }
  154. uint32_t OpenXRInterface::get_capabilities() const {
  155. return XRInterface::XR_VR + XRInterface::XR_STEREO;
  156. }
  157. PackedStringArray OpenXRInterface::get_suggested_tracker_names() const {
  158. // These are hardcoded in OpenXR, note that they will only be available if added to our action map
  159. PackedStringArray arr = {
  160. "head", // XRPositionalTracker for the users head (Mapped from OpenXR /user/head)
  161. "left_hand", // XRControllerTracker for the users left hand (Mapped from OpenXR /user/hand/left)
  162. "right_hand", // XRControllerTracker for the users right hand (Mapped from OpenXR /user/hand/right)
  163. "/user/hand_tracker/left", // XRHandTracker for the users left hand
  164. "/user/hand_tracker/right", // XRHandTracker for the users right hand
  165. "/user/body_tracker", // XRBodyTracker for the users body
  166. "/user/face_tracker", // XRFaceTracker for the users face
  167. "/user/treadmill"
  168. };
  169. for (OpenXRExtensionWrapper *wrapper : OpenXRAPI::get_singleton()->get_registered_extension_wrappers()) {
  170. arr.append_array(wrapper->get_suggested_tracker_names());
  171. }
  172. return arr;
  173. }
  174. XRInterface::TrackingStatus OpenXRInterface::get_tracking_status() const {
  175. return tracking_state;
  176. }
  177. void OpenXRInterface::_load_action_map() {
  178. ERR_FAIL_NULL(openxr_api);
  179. // This may seem a bit duplicitous to a little bit of background info here.
  180. // OpenXRActionMap (with all its sub resource classes) is a class that allows us to configure and store an action map in.
  181. // This gives the user the ability to edit the action map in a UI and customize the actions.
  182. // OpenXR however requires us to submit an action map and it takes over from that point and we can no longer change it.
  183. // This system does that push and we store the info needed to then work with this action map going forward.
  184. // Within our openxr device we maintain a number of classes that wrap the relevant OpenXR objects for this.
  185. // Within OpenXRInterface we have a few internal classes that keep track of what we've created.
  186. // This allow us to process the relevant actions each frame.
  187. // just in case clean up
  188. free_trackers();
  189. free_interaction_profiles();
  190. free_action_sets();
  191. Ref<OpenXRActionMap> action_map;
  192. if (Engine::get_singleton()->is_editor_hint()) {
  193. #ifdef TOOLS_ENABLED
  194. action_map.instantiate();
  195. action_map->create_editor_action_sets();
  196. #endif
  197. } else {
  198. String default_tres_name = openxr_api->get_default_action_map_resource_name();
  199. // Check if we can load our default
  200. if (ResourceLoader::exists(default_tres_name)) {
  201. action_map = ResourceLoader::load(default_tres_name);
  202. }
  203. // Check if we need to create default action set
  204. if (action_map.is_null()) {
  205. action_map.instantiate();
  206. action_map->create_default_action_sets();
  207. #ifdef TOOLS_ENABLED
  208. // Save our action sets so our user can
  209. action_map->set_path(default_tres_name, true);
  210. ResourceSaver::save(action_map, default_tres_name);
  211. #endif
  212. }
  213. }
  214. // process our action map
  215. if (action_map.is_valid()) {
  216. HashMap<Ref<OpenXRAction>, Action *> xr_actions;
  217. Array action_set_array = action_map->get_action_sets();
  218. for (int i = 0; i < action_set_array.size(); i++) {
  219. // Create our action set
  220. Ref<OpenXRActionSet> xr_action_set = action_set_array[i];
  221. ActionSet *action_set = create_action_set(xr_action_set->get_name(), xr_action_set->get_localized_name(), xr_action_set->get_priority());
  222. if (!action_set) {
  223. continue;
  224. }
  225. // Now create our actions for these
  226. Array actions = xr_action_set->get_actions();
  227. for (int j = 0; j < actions.size(); j++) {
  228. Ref<OpenXRAction> xr_action = actions[j];
  229. PackedStringArray toplevel_paths = xr_action->get_toplevel_paths();
  230. Vector<Tracker *> trackers_for_action;
  231. for (int k = 0; k < toplevel_paths.size(); k++) {
  232. // Only check for our tracker if our path is supported.
  233. if (openxr_api->is_top_level_path_supported(toplevel_paths[k])) {
  234. Tracker *tracker = find_tracker(toplevel_paths[k], true);
  235. if (tracker) {
  236. trackers_for_action.push_back(tracker);
  237. }
  238. }
  239. }
  240. // Only add our action if we have at least one valid toplevel path
  241. if (trackers_for_action.size() > 0) {
  242. Action *action = create_action(action_set, xr_action->get_name(), xr_action->get_localized_name(), xr_action->get_action_type(), trackers_for_action);
  243. if (action) {
  244. // add this to our map for creating our interaction profiles
  245. xr_actions[xr_action] = action;
  246. }
  247. }
  248. }
  249. }
  250. // now do our suggestions
  251. Array interaction_profile_array = action_map->get_interaction_profiles();
  252. for (int i = 0; i < interaction_profile_array.size(); i++) {
  253. Ref<OpenXRInteractionProfile> xr_interaction_profile = interaction_profile_array[i];
  254. // Note, we can only have one entry per interaction profile so if it already exists we clear it out
  255. RID ip = openxr_api->interaction_profile_create(xr_interaction_profile->get_interaction_profile_path());
  256. if (ip.is_valid()) {
  257. openxr_api->interaction_profile_clear_bindings(ip);
  258. for (Ref<OpenXRBindingModifier> xr_binding_modifier : xr_interaction_profile->get_binding_modifiers()) {
  259. PackedByteArray bm = xr_binding_modifier->get_ip_modification();
  260. if (!bm.is_empty()) {
  261. openxr_api->interaction_profile_add_modifier(ip, bm);
  262. }
  263. }
  264. Array xr_bindings = xr_interaction_profile->get_bindings();
  265. for (int j = 0; j < xr_bindings.size(); j++) {
  266. Ref<OpenXRIPBinding> xr_binding = xr_bindings[j];
  267. Ref<OpenXRAction> xr_action = xr_binding->get_action();
  268. Action *action = nullptr;
  269. if (xr_actions.has(xr_action)) {
  270. action = xr_actions[xr_action];
  271. } else {
  272. print_line("Action ", xr_action->get_name(), " isn't part of an action set!");
  273. continue;
  274. }
  275. int binding_no = openxr_api->interaction_profile_add_binding(ip, action->action_rid, xr_binding->get_binding_path());
  276. if (binding_no >= 0) {
  277. for (Ref<OpenXRBindingModifier> xr_binding_modifier : xr_binding->get_binding_modifiers()) {
  278. // Binding modifiers on bindings can be added to the interaction profile.
  279. PackedByteArray bm = xr_binding_modifier->get_ip_modification();
  280. if (!bm.is_empty()) {
  281. openxr_api->interaction_profile_add_modifier(ip, bm);
  282. }
  283. // And possibly in the future on the binding itself, we're just preparing for that eventuality.
  284. }
  285. }
  286. }
  287. // Now submit our suggestions
  288. openxr_api->interaction_profile_suggest_bindings(ip);
  289. // And record it in our array so we can clean it up later on
  290. if (interaction_profile_array.has(ip)) {
  291. interaction_profile_array.push_back(ip);
  292. }
  293. }
  294. }
  295. }
  296. }
  297. OpenXRInterface::ActionSet *OpenXRInterface::create_action_set(const String &p_action_set_name, const String &p_localized_name, const int p_priority) {
  298. ERR_FAIL_NULL_V(openxr_api, nullptr);
  299. // find if it already exists
  300. for (int i = 0; i < action_sets.size(); i++) {
  301. if (action_sets[i]->action_set_name == p_action_set_name) {
  302. // already exists in this set
  303. return nullptr;
  304. }
  305. }
  306. ActionSet *action_set = memnew(ActionSet);
  307. action_set->action_set_name = p_action_set_name;
  308. action_set->is_active = true;
  309. action_set->action_set_rid = openxr_api->action_set_create(p_action_set_name, p_localized_name, p_priority);
  310. action_sets.push_back(action_set);
  311. return action_set;
  312. }
  313. void OpenXRInterface::free_action_sets() {
  314. ERR_FAIL_NULL(openxr_api);
  315. for (int i = 0; i < action_sets.size(); i++) {
  316. ActionSet *action_set = action_sets[i];
  317. free_actions(action_set);
  318. openxr_api->action_set_free(action_set->action_set_rid);
  319. memfree(action_set);
  320. }
  321. action_sets.clear();
  322. }
  323. OpenXRInterface::Action *OpenXRInterface::create_action(ActionSet *p_action_set, const String &p_action_name, const String &p_localized_name, OpenXRAction::ActionType p_action_type, const Vector<Tracker *> p_trackers) {
  324. ERR_FAIL_NULL_V(openxr_api, nullptr);
  325. for (int i = 0; i < p_action_set->actions.size(); i++) {
  326. if (p_action_set->actions[i]->action_name == p_action_name) {
  327. // already exists in this set
  328. return nullptr;
  329. }
  330. }
  331. Vector<RID> tracker_rids;
  332. for (int i = 0; i < p_trackers.size(); i++) {
  333. tracker_rids.push_back(p_trackers[i]->tracker_rid);
  334. }
  335. Action *action = memnew(Action);
  336. if (p_action_type == OpenXRAction::OPENXR_ACTION_POSE) {
  337. // We can't have dual action names in OpenXR hence we added _pose,
  338. // but default, aim and grip and default pose action names in Godot so rename them on the tracker.
  339. // NOTE need to decide on whether we should keep the naming convention or rename it on Godots side
  340. if (p_action_name == "default_pose") {
  341. action->action_name = "default";
  342. } else if (p_action_name == "aim_pose") {
  343. action->action_name = "aim";
  344. } else if (p_action_name == "grip_pose") {
  345. action->action_name = "grip";
  346. } else {
  347. action->action_name = p_action_name;
  348. }
  349. } else {
  350. action->action_name = p_action_name;
  351. }
  352. action->action_type = p_action_type;
  353. action->action_rid = openxr_api->action_create(p_action_set->action_set_rid, p_action_name, p_localized_name, p_action_type, tracker_rids);
  354. p_action_set->actions.push_back(action);
  355. // we link our actions back to our trackers so we know which actions to check when we're processing our trackers
  356. for (int i = 0; i < p_trackers.size(); i++) {
  357. if (!p_trackers[i]->actions.has(action)) {
  358. p_trackers[i]->actions.push_back(action);
  359. }
  360. }
  361. return action;
  362. }
  363. OpenXRInterface::Action *OpenXRInterface::find_action(const String &p_action_name) {
  364. // We just find the first action by this name
  365. for (int i = 0; i < action_sets.size(); i++) {
  366. for (int j = 0; j < action_sets[i]->actions.size(); j++) {
  367. if (action_sets[i]->actions[j]->action_name == p_action_name) {
  368. return action_sets[i]->actions[j];
  369. }
  370. }
  371. }
  372. // not found
  373. return nullptr;
  374. }
  375. void OpenXRInterface::free_actions(ActionSet *p_action_set) {
  376. ERR_FAIL_NULL(openxr_api);
  377. for (int i = 0; i < p_action_set->actions.size(); i++) {
  378. Action *action = p_action_set->actions[i];
  379. openxr_api->action_free(action->action_rid);
  380. memdelete(action);
  381. }
  382. p_action_set->actions.clear();
  383. }
  384. OpenXRInterface::Tracker *OpenXRInterface::find_tracker(const String &p_tracker_name, bool p_create) {
  385. XRServer *xr_server = XRServer::get_singleton();
  386. ERR_FAIL_NULL_V(xr_server, nullptr);
  387. ERR_FAIL_NULL_V(openxr_api, nullptr);
  388. Tracker *tracker = nullptr;
  389. for (int i = 0; i < trackers.size(); i++) {
  390. tracker = trackers[i];
  391. if (tracker->tracker_name == p_tracker_name) {
  392. return tracker;
  393. }
  394. }
  395. if (!p_create) {
  396. return nullptr;
  397. }
  398. ERR_FAIL_COND_V(!openxr_api->is_top_level_path_supported(p_tracker_name), nullptr);
  399. // Create our RID
  400. RID tracker_rid = openxr_api->tracker_create(p_tracker_name);
  401. ERR_FAIL_COND_V(tracker_rid.is_null(), nullptr);
  402. // Create our controller tracker.
  403. Ref<XRControllerTracker> controller_tracker;
  404. controller_tracker.instantiate();
  405. // We have standardized some names to make things nicer to the user so lets recognize the toplevel paths related to these.
  406. if (p_tracker_name == "/user/hand/left") {
  407. controller_tracker->set_tracker_name("left_hand");
  408. controller_tracker->set_tracker_desc("Left hand controller");
  409. controller_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_LEFT);
  410. } else if (p_tracker_name == "/user/hand/right") {
  411. controller_tracker->set_tracker_name("right_hand");
  412. controller_tracker->set_tracker_desc("Right hand controller");
  413. controller_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_RIGHT);
  414. } else {
  415. controller_tracker->set_tracker_name(p_tracker_name);
  416. controller_tracker->set_tracker_desc(p_tracker_name);
  417. }
  418. controller_tracker->set_tracker_profile(INTERACTION_PROFILE_NONE);
  419. xr_server->add_tracker(controller_tracker);
  420. // create a new entry
  421. tracker = memnew(Tracker);
  422. tracker->tracker_name = p_tracker_name;
  423. tracker->tracker_rid = tracker_rid;
  424. tracker->controller_tracker = controller_tracker;
  425. tracker->interaction_profile = RID();
  426. trackers.push_back(tracker);
  427. return tracker;
  428. }
  429. void OpenXRInterface::tracker_profile_changed(RID p_tracker, RID p_interaction_profile) {
  430. Tracker *tracker = nullptr;
  431. for (int i = 0; i < trackers.size() && tracker == nullptr; i++) {
  432. if (trackers[i]->tracker_rid == p_tracker) {
  433. tracker = trackers[i];
  434. }
  435. }
  436. ERR_FAIL_NULL(tracker);
  437. tracker->interaction_profile = p_interaction_profile;
  438. if (p_interaction_profile.is_null()) {
  439. print_verbose("OpenXR: Interaction profile for " + tracker->tracker_name + " changed to " + INTERACTION_PROFILE_NONE);
  440. tracker->controller_tracker->set_tracker_profile(INTERACTION_PROFILE_NONE);
  441. } else {
  442. String name = openxr_api->interaction_profile_get_name(p_interaction_profile);
  443. print_verbose("OpenXR: Interaction profile for " + tracker->tracker_name + " changed to " + name);
  444. tracker->controller_tracker->set_tracker_profile(name);
  445. }
  446. }
  447. void OpenXRInterface::handle_tracker(Tracker *p_tracker) {
  448. ERR_FAIL_NULL(openxr_api);
  449. ERR_FAIL_COND(p_tracker->controller_tracker.is_null());
  450. // Note, which actions are actually bound to inputs are handled by our interaction profiles however interaction
  451. // profiles are suggested bindings for controller types we know about. OpenXR runtimes can stray away from these
  452. // and rebind them or even offer bindings to controllers that are not known to us.
  453. // We don't really have a consistent way to detect whether a controller is active however as long as it is
  454. // unbound it seems to be unavailable, so far unknown controller seem to mimic one of the profiles we've
  455. // supplied.
  456. if (p_tracker->interaction_profile.is_null()) {
  457. return;
  458. }
  459. // We check all actions that are related to our tracker.
  460. for (int i = 0; i < p_tracker->actions.size(); i++) {
  461. Action *action = p_tracker->actions[i];
  462. switch (action->action_type) {
  463. case OpenXRAction::OPENXR_ACTION_BOOL: {
  464. bool pressed = openxr_api->get_action_bool(action->action_rid, p_tracker->tracker_rid);
  465. p_tracker->controller_tracker->set_input(action->action_name, Variant(pressed));
  466. } break;
  467. case OpenXRAction::OPENXR_ACTION_FLOAT: {
  468. real_t value = openxr_api->get_action_float(action->action_rid, p_tracker->tracker_rid);
  469. p_tracker->controller_tracker->set_input(action->action_name, Variant(value));
  470. } break;
  471. case OpenXRAction::OPENXR_ACTION_VECTOR2: {
  472. Vector2 value = openxr_api->get_action_vector2(action->action_rid, p_tracker->tracker_rid);
  473. p_tracker->controller_tracker->set_input(action->action_name, Variant(value));
  474. } break;
  475. case OpenXRAction::OPENXR_ACTION_POSE: {
  476. Transform3D transform;
  477. Vector3 linear, angular;
  478. XRPose::TrackingConfidence confidence = openxr_api->get_action_pose(action->action_rid, p_tracker->tracker_rid, transform, linear, angular);
  479. if (confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  480. p_tracker->controller_tracker->set_pose(action->action_name, transform, linear, angular, confidence);
  481. } else {
  482. p_tracker->controller_tracker->invalidate_pose(action->action_name);
  483. }
  484. } break;
  485. default: {
  486. // not yet supported
  487. } break;
  488. }
  489. }
  490. }
  491. void OpenXRInterface::trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec) {
  492. ERR_FAIL_NULL(openxr_api);
  493. Action *action = find_action(p_action_name);
  494. ERR_FAIL_NULL(action);
  495. // We need to map our tracker name to our OpenXR name for our inbuild names.
  496. String tracker_name = p_tracker_name;
  497. if (tracker_name == "left_hand") {
  498. tracker_name = "/user/hand/left";
  499. } else if (tracker_name == "right_hand") {
  500. tracker_name = "/user/hand/right";
  501. }
  502. Tracker *tracker = find_tracker(tracker_name);
  503. ERR_FAIL_NULL(tracker);
  504. // TODO OpenXR does not support delay, so we may need to add support for that somehow...
  505. XrDuration duration = XrDuration(p_duration_sec * 1000000000.0); // seconds -> nanoseconds
  506. openxr_api->trigger_haptic_pulse(action->action_rid, tracker->tracker_rid, p_frequency, p_amplitude, duration);
  507. }
  508. void OpenXRInterface::free_trackers() {
  509. XRServer *xr_server = XRServer::get_singleton();
  510. ERR_FAIL_NULL(xr_server);
  511. ERR_FAIL_NULL(openxr_api);
  512. for (int i = 0; i < trackers.size(); i++) {
  513. Tracker *tracker = trackers[i];
  514. openxr_api->tracker_free(tracker->tracker_rid);
  515. xr_server->remove_tracker(tracker->controller_tracker);
  516. tracker->controller_tracker.unref();
  517. memdelete(tracker);
  518. }
  519. trackers.clear();
  520. }
  521. void OpenXRInterface::free_interaction_profiles() {
  522. ERR_FAIL_NULL(openxr_api);
  523. for (const RID &interaction_profile : interaction_profiles) {
  524. openxr_api->interaction_profile_free(interaction_profile);
  525. }
  526. interaction_profiles.clear();
  527. }
  528. bool OpenXRInterface::initialize_on_startup() const {
  529. if (openxr_api == nullptr) {
  530. return false;
  531. } else if (!openxr_api->is_initialized()) {
  532. return false;
  533. } else {
  534. return true;
  535. }
  536. }
  537. bool OpenXRInterface::is_initialized() const {
  538. return initialized;
  539. }
  540. bool OpenXRInterface::initialize() {
  541. XRServer *xr_server = XRServer::get_singleton();
  542. ERR_FAIL_NULL_V(xr_server, false);
  543. if (openxr_api == nullptr) {
  544. return false;
  545. } else if (!openxr_api->is_initialized()) {
  546. return false;
  547. } else if (initialized) {
  548. return true;
  549. }
  550. // load up our action sets before setting up our session, note that our profiles are suggestions, OpenXR takes ownership of (re)binding
  551. _load_action_map();
  552. if (!openxr_api->initialize_session()) {
  553. return false;
  554. }
  555. // we must create a tracker for our head
  556. head.instantiate();
  557. head->set_tracker_type(XRServer::TRACKER_HEAD);
  558. head->set_tracker_name("head");
  559. head->set_tracker_desc("Players head");
  560. xr_server->add_tracker(head);
  561. // attach action sets
  562. Vector<RID> loaded_action_sets;
  563. for (int i = 0; i < action_sets.size(); i++) {
  564. loaded_action_sets.append(action_sets[i]->action_set_rid);
  565. }
  566. openxr_api->attach_action_sets(loaded_action_sets);
  567. // make this our primary interface
  568. xr_server->set_primary_interface(this);
  569. // Register an additional output with the display server, so rendering won't
  570. // be skipped if no windows are visible.
  571. DisplayServer::get_singleton()->register_additional_output(this);
  572. initialized = true;
  573. return initialized;
  574. }
  575. void OpenXRInterface::uninitialize() {
  576. // Our OpenXR driver will clean itself up properly when Godot exits, so we just do some basic stuff here
  577. // end the session if we need to?
  578. // cleanup stuff
  579. free_trackers();
  580. free_interaction_profiles();
  581. free_action_sets();
  582. XRServer *xr_server = XRServer::get_singleton();
  583. if (xr_server) {
  584. if (head.is_valid()) {
  585. xr_server->remove_tracker(head);
  586. head.unref();
  587. }
  588. }
  589. DisplayServer::get_singleton()->unregister_additional_output(this);
  590. initialized = false;
  591. }
  592. Dictionary OpenXRInterface::get_system_info() {
  593. Dictionary dict;
  594. if (openxr_api) {
  595. dict[SNAME("XRRuntimeName")] = openxr_api->get_runtime_name();
  596. dict[SNAME("XRRuntimeVersion")] = openxr_api->get_runtime_version();
  597. dict[SNAME("OpenXRSystemName")] = openxr_api->get_system_name();
  598. dict[SNAME("OpenXRVendorID")] = openxr_api->get_vendor_id();
  599. }
  600. return dict;
  601. }
  602. bool OpenXRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  603. if (p_mode == XRInterface::XR_PLAY_AREA_3DOF) {
  604. return false;
  605. }
  606. return true;
  607. }
  608. XRInterface::PlayAreaMode OpenXRInterface::get_play_area_mode() const {
  609. if (!openxr_api || !initialized) {
  610. return XRInterface::XR_PLAY_AREA_UNKNOWN;
  611. }
  612. XrReferenceSpaceType reference_space = openxr_api->get_reference_space();
  613. if (reference_space == XR_REFERENCE_SPACE_TYPE_LOCAL) {
  614. return XRInterface::XR_PLAY_AREA_SITTING;
  615. } else if (reference_space == XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT) {
  616. return XRInterface::XR_PLAY_AREA_ROOMSCALE;
  617. } else if (reference_space == XR_REFERENCE_SPACE_TYPE_STAGE) {
  618. return XRInterface::XR_PLAY_AREA_STAGE;
  619. }
  620. return XRInterface::XR_PLAY_AREA_UNKNOWN;
  621. }
  622. bool OpenXRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  623. ERR_FAIL_NULL_V(openxr_api, false);
  624. XrReferenceSpaceType reference_space;
  625. if (p_mode == XRInterface::XR_PLAY_AREA_SITTING) {
  626. reference_space = XR_REFERENCE_SPACE_TYPE_LOCAL;
  627. } else if (p_mode == XRInterface::XR_PLAY_AREA_ROOMSCALE) {
  628. reference_space = XR_REFERENCE_SPACE_TYPE_LOCAL_FLOOR_EXT;
  629. } else if (p_mode == XRInterface::XR_PLAY_AREA_STAGE) {
  630. reference_space = XR_REFERENCE_SPACE_TYPE_STAGE;
  631. } else {
  632. return false;
  633. }
  634. if (openxr_api->set_requested_reference_space(reference_space)) {
  635. XRServer *xr_server = XRServer::get_singleton();
  636. if (xr_server) {
  637. xr_server->clear_reference_frame();
  638. }
  639. return true;
  640. }
  641. return false;
  642. }
  643. PackedVector3Array OpenXRInterface::get_play_area() const {
  644. XRServer *xr_server = XRServer::get_singleton();
  645. ERR_FAIL_NULL_V(xr_server, PackedVector3Array());
  646. PackedVector3Array arr;
  647. Vector3 sides[4] = {
  648. Vector3(-0.5f, 0.0f, -0.5f),
  649. Vector3(0.5f, 0.0f, -0.5f),
  650. Vector3(0.5f, 0.0f, 0.5f),
  651. Vector3(-0.5f, 0.0f, 0.5f),
  652. };
  653. if (openxr_api != nullptr && openxr_api->is_initialized()) {
  654. Size2 extents = openxr_api->get_play_space_bounds();
  655. if (extents.width != 0.0 && extents.height != 0.0) {
  656. Transform3D reference_frame = xr_server->get_reference_frame();
  657. for (int i = 0; i < 4; i++) {
  658. Vector3 coord = sides[i];
  659. // Scale it up.
  660. coord.x *= extents.width;
  661. coord.z *= extents.height;
  662. // Now apply our reference.
  663. Vector3 out = reference_frame.xform(coord);
  664. arr.push_back(out);
  665. }
  666. } else {
  667. WARN_PRINT_ONCE("OpenXR: No extents available.");
  668. }
  669. }
  670. return arr;
  671. }
  672. float OpenXRInterface::get_display_refresh_rate() const {
  673. if (openxr_api == nullptr) {
  674. return 0.0;
  675. } else if (!openxr_api->is_initialized()) {
  676. return 0.0;
  677. } else {
  678. return openxr_api->get_display_refresh_rate();
  679. }
  680. }
  681. void OpenXRInterface::set_display_refresh_rate(float p_refresh_rate) {
  682. if (openxr_api == nullptr) {
  683. return;
  684. } else if (!openxr_api->is_initialized()) {
  685. return;
  686. } else {
  687. openxr_api->set_display_refresh_rate(p_refresh_rate);
  688. }
  689. }
  690. Array OpenXRInterface::get_available_display_refresh_rates() const {
  691. if (openxr_api == nullptr) {
  692. return Array();
  693. } else if (!openxr_api->is_initialized()) {
  694. return Array();
  695. } else {
  696. return openxr_api->get_available_display_refresh_rates();
  697. }
  698. }
  699. bool OpenXRInterface::is_hand_tracking_supported() {
  700. if (openxr_api == nullptr) {
  701. return false;
  702. } else if (!openxr_api->is_initialized()) {
  703. return false;
  704. } else {
  705. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  706. if (hand_tracking_ext == nullptr) {
  707. return false;
  708. } else {
  709. return hand_tracking_ext->get_active();
  710. }
  711. }
  712. }
  713. bool OpenXRInterface::is_hand_interaction_supported() const {
  714. if (openxr_api == nullptr) {
  715. return false;
  716. } else if (!openxr_api->is_initialized()) {
  717. return false;
  718. } else {
  719. OpenXRHandInteractionExtension *hand_interaction_ext = OpenXRHandInteractionExtension::get_singleton();
  720. if (hand_interaction_ext == nullptr) {
  721. return false;
  722. } else {
  723. return hand_interaction_ext->is_available();
  724. }
  725. }
  726. }
  727. bool OpenXRInterface::is_eye_gaze_interaction_supported() {
  728. if (openxr_api == nullptr) {
  729. return false;
  730. } else if (!openxr_api->is_initialized()) {
  731. return false;
  732. } else {
  733. OpenXREyeGazeInteractionExtension *eye_gaze_ext = OpenXREyeGazeInteractionExtension::get_singleton();
  734. if (eye_gaze_ext == nullptr) {
  735. return false;
  736. } else {
  737. return eye_gaze_ext->supports_eye_gaze_interaction();
  738. }
  739. }
  740. }
  741. bool OpenXRInterface::is_action_set_active(const String &p_action_set) const {
  742. for (ActionSet *action_set : action_sets) {
  743. if (action_set->action_set_name == p_action_set) {
  744. return action_set->is_active;
  745. }
  746. }
  747. WARN_PRINT("OpenXR: Unknown action set " + p_action_set);
  748. return false;
  749. }
  750. void OpenXRInterface::set_action_set_active(const String &p_action_set, bool p_active) {
  751. for (ActionSet *action_set : action_sets) {
  752. if (action_set->action_set_name == p_action_set) {
  753. action_set->is_active = p_active;
  754. return;
  755. }
  756. }
  757. WARN_PRINT("OpenXR: Unknown action set " + p_action_set);
  758. }
  759. Array OpenXRInterface::get_action_sets() const {
  760. Array arr;
  761. for (ActionSet *action_set : action_sets) {
  762. arr.push_back(action_set->action_set_name);
  763. }
  764. return arr;
  765. }
  766. float OpenXRInterface::get_vrs_min_radius() const {
  767. return xr_vrs.get_vrs_min_radius();
  768. }
  769. void OpenXRInterface::set_vrs_min_radius(float p_vrs_min_radius) {
  770. xr_vrs.set_vrs_min_radius(p_vrs_min_radius);
  771. }
  772. float OpenXRInterface::get_vrs_strength() const {
  773. return xr_vrs.get_vrs_strength();
  774. }
  775. void OpenXRInterface::set_vrs_strength(float p_vrs_strength) {
  776. xr_vrs.set_vrs_strength(p_vrs_strength);
  777. }
  778. double OpenXRInterface::get_render_target_size_multiplier() const {
  779. if (openxr_api == nullptr) {
  780. return 1.0;
  781. } else {
  782. return openxr_api->get_render_target_size_multiplier();
  783. }
  784. }
  785. void OpenXRInterface::set_render_target_size_multiplier(double multiplier) {
  786. if (openxr_api == nullptr) {
  787. return;
  788. } else {
  789. openxr_api->set_render_target_size_multiplier(multiplier);
  790. }
  791. }
  792. bool OpenXRInterface::is_foveation_supported() const {
  793. if (openxr_api == nullptr) {
  794. return false;
  795. } else {
  796. return openxr_api->is_foveation_supported();
  797. }
  798. }
  799. int OpenXRInterface::get_foveation_level() const {
  800. if (openxr_api == nullptr) {
  801. return 0;
  802. } else {
  803. return openxr_api->get_foveation_level();
  804. }
  805. }
  806. void OpenXRInterface::set_foveation_level(int p_foveation_level) {
  807. if (openxr_api == nullptr) {
  808. return;
  809. } else {
  810. openxr_api->set_foveation_level(p_foveation_level);
  811. }
  812. }
  813. bool OpenXRInterface::get_foveation_dynamic() const {
  814. if (openxr_api == nullptr) {
  815. return false;
  816. } else {
  817. return openxr_api->get_foveation_dynamic();
  818. }
  819. }
  820. void OpenXRInterface::set_foveation_dynamic(bool p_foveation_dynamic) {
  821. if (openxr_api == nullptr) {
  822. return;
  823. } else {
  824. openxr_api->set_foveation_dynamic(p_foveation_dynamic);
  825. }
  826. }
  827. Size2 OpenXRInterface::get_render_target_size() {
  828. if (openxr_api == nullptr) {
  829. return Size2();
  830. } else {
  831. return openxr_api->get_recommended_target_size();
  832. }
  833. }
  834. uint32_t OpenXRInterface::get_view_count() {
  835. // TODO set this based on our configuration
  836. return 2;
  837. }
  838. void OpenXRInterface::_set_default_pos(Transform3D &p_transform, double p_world_scale, uint64_t p_eye) {
  839. p_transform = Transform3D();
  840. // if we're not tracking, don't put our head on the floor...
  841. p_transform.origin.y = 1.5 * p_world_scale;
  842. // overkill but..
  843. if (p_eye == 1) {
  844. p_transform.origin.x = 0.03 * p_world_scale;
  845. } else if (p_eye == 2) {
  846. p_transform.origin.x = -0.03 * p_world_scale;
  847. }
  848. }
  849. Transform3D OpenXRInterface::get_camera_transform() {
  850. XRServer *xr_server = XRServer::get_singleton();
  851. ERR_FAIL_NULL_V(xr_server, Transform3D());
  852. Transform3D hmd_transform;
  853. double world_scale = xr_server->get_world_scale();
  854. // head_transform should be updated in process
  855. hmd_transform.basis = head_transform.basis;
  856. hmd_transform.origin = head_transform.origin * world_scale;
  857. return hmd_transform;
  858. }
  859. Transform3D OpenXRInterface::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
  860. XRServer *xr_server = XRServer::get_singleton();
  861. ERR_FAIL_NULL_V(xr_server, Transform3D());
  862. ERR_FAIL_UNSIGNED_INDEX_V_MSG(p_view, get_view_count(), Transform3D(), "View index outside bounds.");
  863. Transform3D t;
  864. if (openxr_api && openxr_api->get_view_transform(p_view, t)) {
  865. // update our cached value if we have a valid transform
  866. transform_for_view[p_view] = t;
  867. } else {
  868. // reuse cached value
  869. t = transform_for_view[p_view];
  870. }
  871. // Apply our world scale
  872. double world_scale = xr_server->get_world_scale();
  873. t.origin *= world_scale;
  874. return p_cam_transform * xr_server->get_reference_frame() * t;
  875. }
  876. Projection OpenXRInterface::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
  877. Projection cm;
  878. ERR_FAIL_UNSIGNED_INDEX_V_MSG(p_view, get_view_count(), cm, "View index outside bounds.");
  879. if (openxr_api) {
  880. if (openxr_api->get_view_projection(p_view, p_z_near, p_z_far, cm)) {
  881. return cm;
  882. }
  883. }
  884. // Failed to get from our OpenXR device? Default to some sort of sensible camera matrix..
  885. cm.set_for_hmd(p_view + 1, 1.0, 6.0, 14.5, 4.0, 1.5, p_z_near, p_z_far);
  886. return cm;
  887. }
  888. Rect2i OpenXRInterface::get_render_region() {
  889. if (openxr_api) {
  890. return openxr_api->get_render_region();
  891. } else {
  892. return Rect2i();
  893. }
  894. }
  895. RID OpenXRInterface::get_color_texture() {
  896. if (openxr_api) {
  897. return openxr_api->get_color_texture();
  898. } else {
  899. return RID();
  900. }
  901. }
  902. RID OpenXRInterface::get_depth_texture() {
  903. if (openxr_api) {
  904. return openxr_api->get_depth_texture();
  905. } else {
  906. return RID();
  907. }
  908. }
  909. RID OpenXRInterface::get_velocity_texture() {
  910. if (openxr_api) {
  911. return openxr_api->get_velocity_texture();
  912. } else {
  913. return RID();
  914. }
  915. }
  916. RID OpenXRInterface::get_velocity_depth_texture() {
  917. if (openxr_api) {
  918. return openxr_api->get_velocity_depth_texture();
  919. } else {
  920. return RID();
  921. }
  922. }
  923. Size2i OpenXRInterface::get_velocity_target_size() {
  924. if (openxr_api) {
  925. return openxr_api->get_velocity_target_size();
  926. } else {
  927. return Size2i();
  928. }
  929. }
  930. void OpenXRInterface::handle_hand_tracking(const String &p_path, OpenXRHandTrackingExtension::HandTrackedHands p_hand) {
  931. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  932. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  933. OpenXRInterface::Tracker *tracker = find_tracker(p_path);
  934. if (tracker && tracker->controller_tracker.is_valid()) {
  935. XrSpaceLocationFlags location_flags = hand_tracking_ext->get_hand_joint_location_flags(p_hand, XR_HAND_JOINT_PALM_EXT);
  936. if (location_flags & (XR_SPACE_LOCATION_ORIENTATION_VALID_BIT + XR_SPACE_LOCATION_POSITION_VALID_BIT)) {
  937. static const XrSpaceLocationFlags all_location_flags = XR_SPACE_LOCATION_ORIENTATION_VALID_BIT + XR_SPACE_LOCATION_POSITION_VALID_BIT + XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT + XR_SPACE_LOCATION_POSITION_TRACKED_BIT;
  938. XRPose::TrackingConfidence confidence = XRPose::XR_TRACKING_CONFIDENCE_LOW;
  939. Transform3D transform;
  940. Vector3 linear_velocity;
  941. Vector3 angular_velocity;
  942. if ((location_flags & all_location_flags) == all_location_flags) {
  943. // All flags set? confidence is high!
  944. confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
  945. }
  946. if (location_flags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) {
  947. transform.basis = Basis(hand_tracking_ext->get_hand_joint_rotation(p_hand, XR_HAND_JOINT_PALM_EXT));
  948. }
  949. if (location_flags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
  950. transform.origin = hand_tracking_ext->get_hand_joint_position(p_hand, XR_HAND_JOINT_PALM_EXT);
  951. }
  952. XrSpaceVelocityFlags velocity_flags = hand_tracking_ext->get_hand_joint_location_flags(p_hand, XR_HAND_JOINT_PALM_EXT);
  953. if (velocity_flags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT) {
  954. linear_velocity = hand_tracking_ext->get_hand_joint_linear_velocity(p_hand, XR_HAND_JOINT_PALM_EXT);
  955. }
  956. if (velocity_flags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT) {
  957. angular_velocity = hand_tracking_ext->get_hand_joint_angular_velocity(p_hand, XR_HAND_JOINT_PALM_EXT);
  958. }
  959. tracker->controller_tracker->set_pose("skeleton", transform, linear_velocity, angular_velocity, confidence);
  960. } else {
  961. tracker->controller_tracker->invalidate_pose("skeleton");
  962. }
  963. }
  964. }
  965. }
  966. void OpenXRInterface::process() {
  967. if (openxr_api) {
  968. // do our normal process
  969. if (openxr_api->process()) {
  970. Transform3D t;
  971. Vector3 linear_velocity;
  972. Vector3 angular_velocity;
  973. head_confidence = openxr_api->get_head_center(t, linear_velocity, angular_velocity);
  974. if (head_confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  975. // Only update our transform if we have one to update it with
  976. // note that poses are stored without world scale and reference frame applied!
  977. head_transform = t;
  978. head_linear_velocity = linear_velocity;
  979. head_angular_velocity = angular_velocity;
  980. }
  981. }
  982. // handle our action sets....
  983. Vector<RID> active_sets;
  984. for (int i = 0; i < action_sets.size(); i++) {
  985. if (action_sets[i]->is_active) {
  986. active_sets.push_back(action_sets[i]->action_set_rid);
  987. }
  988. }
  989. if (openxr_api->sync_action_sets(active_sets)) {
  990. for (int i = 0; i < trackers.size(); i++) {
  991. handle_tracker(trackers[i]);
  992. }
  993. }
  994. // Handle hand tracking
  995. handle_hand_tracking("/user/hand/left", OpenXRHandTrackingExtension::OPENXR_TRACKED_LEFT_HAND);
  996. handle_hand_tracking("/user/hand/right", OpenXRHandTrackingExtension::OPENXR_TRACKED_RIGHT_HAND);
  997. }
  998. if (head.is_valid()) {
  999. head->set_pose("default", head_transform, head_linear_velocity, head_angular_velocity, head_confidence);
  1000. }
  1001. if (reference_stage_changing) {
  1002. // Now that we have updated tracking information in our updated reference space, trigger our pose recentered signal.
  1003. emit_signal(SNAME("pose_recentered"));
  1004. reference_stage_changing = false;
  1005. }
  1006. }
  1007. void OpenXRInterface::pre_render() {
  1008. if (openxr_api) {
  1009. openxr_api->pre_render();
  1010. }
  1011. }
  1012. bool OpenXRInterface::pre_draw_viewport(RID p_render_target) {
  1013. if (openxr_api) {
  1014. return openxr_api->pre_draw_viewport(p_render_target);
  1015. } else {
  1016. // don't render
  1017. return false;
  1018. }
  1019. }
  1020. Vector<BlitToScreen> OpenXRInterface::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
  1021. Vector<BlitToScreen> blit_to_screen;
  1022. #ifndef ANDROID_ENABLED
  1023. // If separate HMD we should output one eye to screen
  1024. if (p_screen_rect != Rect2()) {
  1025. BlitToScreen blit;
  1026. blit.render_target = p_render_target;
  1027. blit.multi_view.use_layer = true;
  1028. blit.multi_view.layer = 0;
  1029. blit.lens_distortion.apply = false;
  1030. Size2 render_size = get_render_target_size();
  1031. Rect2 dst_rect = p_screen_rect;
  1032. float new_height = dst_rect.size.x * (render_size.y / render_size.x);
  1033. if (new_height > dst_rect.size.y) {
  1034. dst_rect.position.y = (0.5 * dst_rect.size.y) - (0.5 * new_height);
  1035. dst_rect.size.y = new_height;
  1036. } else {
  1037. float new_width = dst_rect.size.y * (render_size.x / render_size.y);
  1038. dst_rect.position.x = (0.5 * dst_rect.size.x) - (0.5 * new_width);
  1039. dst_rect.size.x = new_width;
  1040. }
  1041. blit.dst_rect = dst_rect;
  1042. blit_to_screen.push_back(blit);
  1043. }
  1044. #endif
  1045. if (openxr_api) {
  1046. openxr_api->post_draw_viewport(p_render_target);
  1047. }
  1048. return blit_to_screen;
  1049. }
  1050. void OpenXRInterface::end_frame() {
  1051. if (openxr_api) {
  1052. openxr_api->end_frame();
  1053. }
  1054. }
  1055. bool OpenXRInterface::is_passthrough_supported() {
  1056. return get_supported_environment_blend_modes().find(XR_ENV_BLEND_MODE_ALPHA_BLEND);
  1057. }
  1058. bool OpenXRInterface::is_passthrough_enabled() {
  1059. return get_environment_blend_mode() == XR_ENV_BLEND_MODE_ALPHA_BLEND;
  1060. }
  1061. bool OpenXRInterface::start_passthrough() {
  1062. return set_environment_blend_mode(XR_ENV_BLEND_MODE_ALPHA_BLEND);
  1063. }
  1064. void OpenXRInterface::stop_passthrough() {
  1065. set_environment_blend_mode(XR_ENV_BLEND_MODE_OPAQUE);
  1066. }
  1067. Array OpenXRInterface::get_supported_environment_blend_modes() {
  1068. Array modes;
  1069. if (!openxr_api) {
  1070. return modes;
  1071. }
  1072. const Vector<XrEnvironmentBlendMode> env_blend_modes = openxr_api->get_supported_environment_blend_modes();
  1073. for (const XrEnvironmentBlendMode &env_blend_mode : env_blend_modes) {
  1074. switch (env_blend_mode) {
  1075. case XR_ENVIRONMENT_BLEND_MODE_OPAQUE:
  1076. modes.push_back(XR_ENV_BLEND_MODE_OPAQUE);
  1077. break;
  1078. case XR_ENVIRONMENT_BLEND_MODE_ADDITIVE:
  1079. modes.push_back(XR_ENV_BLEND_MODE_ADDITIVE);
  1080. break;
  1081. case XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND:
  1082. modes.push_back(XR_ENV_BLEND_MODE_ALPHA_BLEND);
  1083. break;
  1084. default:
  1085. WARN_PRINT(vformat("Unsupported blend mode found: %s.", String::num_int64(int64_t(env_blend_mode))));
  1086. }
  1087. }
  1088. if (openxr_api->is_environment_blend_mode_alpha_blend_supported() == OpenXRAPI::OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING) {
  1089. modes.push_back(XR_ENV_BLEND_MODE_ALPHA_BLEND);
  1090. }
  1091. return modes;
  1092. }
  1093. XRInterface::EnvironmentBlendMode OpenXRInterface::get_environment_blend_mode() const {
  1094. if (openxr_api) {
  1095. XrEnvironmentBlendMode oxr_blend_mode = openxr_api->get_environment_blend_mode();
  1096. switch (oxr_blend_mode) {
  1097. case XR_ENVIRONMENT_BLEND_MODE_OPAQUE: {
  1098. return XR_ENV_BLEND_MODE_OPAQUE;
  1099. } break;
  1100. case XR_ENVIRONMENT_BLEND_MODE_ADDITIVE: {
  1101. return XR_ENV_BLEND_MODE_ADDITIVE;
  1102. } break;
  1103. case XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND: {
  1104. return XR_ENV_BLEND_MODE_ALPHA_BLEND;
  1105. } break;
  1106. default:
  1107. break;
  1108. }
  1109. }
  1110. return XR_ENV_BLEND_MODE_OPAQUE;
  1111. }
  1112. bool OpenXRInterface::set_environment_blend_mode(XRInterface::EnvironmentBlendMode mode) {
  1113. if (openxr_api) {
  1114. XrEnvironmentBlendMode oxr_blend_mode;
  1115. switch (mode) {
  1116. case XR_ENV_BLEND_MODE_OPAQUE:
  1117. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
  1118. break;
  1119. case XR_ENV_BLEND_MODE_ADDITIVE:
  1120. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ADDITIVE;
  1121. break;
  1122. case XR_ENV_BLEND_MODE_ALPHA_BLEND:
  1123. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND;
  1124. break;
  1125. default:
  1126. WARN_PRINT("Unknown blend mode requested: " + String::num_int64(int64_t(mode)));
  1127. oxr_blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
  1128. }
  1129. return openxr_api->set_environment_blend_mode(oxr_blend_mode);
  1130. }
  1131. return false;
  1132. }
  1133. void OpenXRInterface::on_state_ready() {
  1134. emit_signal(SNAME("session_begun"));
  1135. }
  1136. void OpenXRInterface::on_state_visible() {
  1137. emit_signal(SNAME("session_visible"));
  1138. }
  1139. void OpenXRInterface::on_state_focused() {
  1140. emit_signal(SNAME("session_focussed"));
  1141. }
  1142. void OpenXRInterface::on_state_stopping() {
  1143. emit_signal(SNAME("session_stopping"));
  1144. }
  1145. void OpenXRInterface::on_state_loss_pending() {
  1146. emit_signal(SNAME("session_loss_pending"));
  1147. }
  1148. void OpenXRInterface::on_state_exiting() {
  1149. emit_signal(SNAME("instance_exiting"));
  1150. }
  1151. void OpenXRInterface::on_reference_space_change_pending() {
  1152. reference_stage_changing = true;
  1153. }
  1154. void OpenXRInterface::on_refresh_rate_changes(float p_new_rate) {
  1155. emit_signal(SNAME("refresh_rate_changed"), p_new_rate);
  1156. }
  1157. /** Hand tracking. */
  1158. void OpenXRInterface::set_motion_range(const Hand p_hand, const HandMotionRange p_motion_range) {
  1159. ERR_FAIL_INDEX(p_hand, HAND_MAX);
  1160. ERR_FAIL_INDEX(p_motion_range, HAND_MOTION_RANGE_MAX);
  1161. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  1162. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  1163. XrHandJointsMotionRangeEXT xr_motion_range;
  1164. switch (p_motion_range) {
  1165. case HAND_MOTION_RANGE_UNOBSTRUCTED:
  1166. xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_UNOBSTRUCTED_EXT;
  1167. break;
  1168. case HAND_MOTION_RANGE_CONFORM_TO_CONTROLLER:
  1169. xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_CONFORMING_TO_CONTROLLER_EXT;
  1170. break;
  1171. default:
  1172. // Shouldn't get here, ERR_FAIL_INDEX should have caught this...
  1173. xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_CONFORMING_TO_CONTROLLER_EXT;
  1174. break;
  1175. }
  1176. hand_tracking_ext->set_motion_range(OpenXRHandTrackingExtension::HandTrackedHands(p_hand), xr_motion_range);
  1177. }
  1178. }
  1179. OpenXRInterface::HandMotionRange OpenXRInterface::get_motion_range(const Hand p_hand) const {
  1180. ERR_FAIL_INDEX_V(p_hand, HAND_MAX, HAND_MOTION_RANGE_MAX);
  1181. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  1182. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  1183. XrHandJointsMotionRangeEXT xr_motion_range = hand_tracking_ext->get_motion_range(OpenXRHandTrackingExtension::HandTrackedHands(p_hand));
  1184. switch (xr_motion_range) {
  1185. case XR_HAND_JOINTS_MOTION_RANGE_UNOBSTRUCTED_EXT:
  1186. return HAND_MOTION_RANGE_UNOBSTRUCTED;
  1187. case XR_HAND_JOINTS_MOTION_RANGE_CONFORMING_TO_CONTROLLER_EXT:
  1188. return HAND_MOTION_RANGE_CONFORM_TO_CONTROLLER;
  1189. default:
  1190. ERR_FAIL_V_MSG(HAND_MOTION_RANGE_MAX, "Unknown motion range returned by OpenXR");
  1191. }
  1192. }
  1193. return HAND_MOTION_RANGE_MAX;
  1194. }
  1195. OpenXRInterface::HandTrackedSource OpenXRInterface::get_hand_tracking_source(const Hand p_hand) const {
  1196. ERR_FAIL_INDEX_V(p_hand, HAND_MAX, HAND_TRACKED_SOURCE_UNKNOWN);
  1197. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  1198. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  1199. OpenXRHandTrackingExtension::HandTrackedSource source = hand_tracking_ext->get_hand_tracking_source(OpenXRHandTrackingExtension::HandTrackedHands(p_hand));
  1200. switch (source) {
  1201. case OpenXRHandTrackingExtension::OPENXR_SOURCE_UNOBSTRUCTED:
  1202. return HAND_TRACKED_SOURCE_UNOBSTRUCTED;
  1203. case OpenXRHandTrackingExtension::OPENXR_SOURCE_CONTROLLER:
  1204. return HAND_TRACKED_SOURCE_CONTROLLER;
  1205. case OpenXRHandTrackingExtension::OPENXR_SOURCE_UNKNOWN:
  1206. return HAND_TRACKED_SOURCE_UNKNOWN;
  1207. default:
  1208. ERR_FAIL_V_MSG(HAND_TRACKED_SOURCE_UNKNOWN, "Unknown hand tracking source returned by OpenXR");
  1209. }
  1210. }
  1211. return HAND_TRACKED_SOURCE_UNKNOWN;
  1212. }
  1213. BitField<OpenXRInterface::HandJointFlags> OpenXRInterface::get_hand_joint_flags(Hand p_hand, HandJoints p_joint) const {
  1214. BitField<OpenXRInterface::HandJointFlags> bits;
  1215. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  1216. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  1217. XrSpaceLocationFlags location_flags = hand_tracking_ext->get_hand_joint_location_flags(OpenXRHandTrackingExtension::HandTrackedHands(p_hand), XrHandJointEXT(p_joint));
  1218. if (location_flags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) {
  1219. bits.set_flag(HAND_JOINT_ORIENTATION_VALID);
  1220. }
  1221. if (location_flags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) {
  1222. bits.set_flag(HAND_JOINT_ORIENTATION_TRACKED);
  1223. }
  1224. if (location_flags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
  1225. bits.set_flag(HAND_JOINT_POSITION_VALID);
  1226. }
  1227. if (location_flags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT) {
  1228. bits.set_flag(HAND_JOINT_POSITION_TRACKED);
  1229. }
  1230. XrSpaceVelocityFlags velocity_flags = hand_tracking_ext->get_hand_joint_velocity_flags(OpenXRHandTrackingExtension::HandTrackedHands(p_hand), XrHandJointEXT(p_joint));
  1231. if (velocity_flags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT) {
  1232. bits.set_flag(HAND_JOINT_LINEAR_VELOCITY_VALID);
  1233. }
  1234. if (velocity_flags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT) {
  1235. bits.set_flag(HAND_JOINT_ANGULAR_VELOCITY_VALID);
  1236. }
  1237. }
  1238. return bits;
  1239. }
  1240. Quaternion OpenXRInterface::get_hand_joint_rotation(Hand p_hand, HandJoints p_joint) const {
  1241. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  1242. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  1243. return hand_tracking_ext->get_hand_joint_rotation(OpenXRHandTrackingExtension::HandTrackedHands(p_hand), XrHandJointEXT(p_joint));
  1244. }
  1245. return Quaternion();
  1246. }
  1247. Vector3 OpenXRInterface::get_hand_joint_position(Hand p_hand, HandJoints p_joint) const {
  1248. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  1249. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  1250. return hand_tracking_ext->get_hand_joint_position(OpenXRHandTrackingExtension::HandTrackedHands(p_hand), XrHandJointEXT(p_joint));
  1251. }
  1252. return Vector3();
  1253. }
  1254. float OpenXRInterface::get_hand_joint_radius(Hand p_hand, HandJoints p_joint) const {
  1255. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  1256. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  1257. return hand_tracking_ext->get_hand_joint_radius(OpenXRHandTrackingExtension::HandTrackedHands(p_hand), XrHandJointEXT(p_joint));
  1258. }
  1259. return 0.0;
  1260. }
  1261. Vector3 OpenXRInterface::get_hand_joint_linear_velocity(Hand p_hand, HandJoints p_joint) const {
  1262. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  1263. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  1264. return hand_tracking_ext->get_hand_joint_linear_velocity(OpenXRHandTrackingExtension::HandTrackedHands(p_hand), XrHandJointEXT(p_joint));
  1265. }
  1266. return Vector3();
  1267. }
  1268. Vector3 OpenXRInterface::get_hand_joint_angular_velocity(Hand p_hand, HandJoints p_joint) const {
  1269. OpenXRHandTrackingExtension *hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  1270. if (hand_tracking_ext && hand_tracking_ext->get_active()) {
  1271. return hand_tracking_ext->get_hand_joint_angular_velocity(OpenXRHandTrackingExtension::HandTrackedHands(p_hand), XrHandJointEXT(p_joint));
  1272. }
  1273. return Vector3();
  1274. }
  1275. RID OpenXRInterface::get_vrs_texture() {
  1276. if (!openxr_api) {
  1277. return RID();
  1278. }
  1279. PackedVector2Array eye_foci;
  1280. Size2 target_size = get_render_target_size();
  1281. real_t aspect_ratio = target_size.x / target_size.y;
  1282. uint32_t view_count = get_view_count();
  1283. for (uint32_t v = 0; v < view_count; v++) {
  1284. eye_foci.push_back(openxr_api->get_eye_focus(v, aspect_ratio));
  1285. }
  1286. xr_vrs.set_vrs_render_region(get_render_region());
  1287. return xr_vrs.make_vrs_texture(target_size, eye_foci);
  1288. }
  1289. void OpenXRInterface::set_cpu_level(PerfSettingsLevel p_level) {
  1290. OpenXRPerformanceSettingsExtension *performance_settings_ext = OpenXRPerformanceSettingsExtension::get_singleton();
  1291. if (performance_settings_ext && performance_settings_ext->is_available()) {
  1292. performance_settings_ext->set_cpu_level(p_level);
  1293. }
  1294. }
  1295. void OpenXRInterface::set_gpu_level(PerfSettingsLevel p_level) {
  1296. OpenXRPerformanceSettingsExtension *performance_settings_ext = OpenXRPerformanceSettingsExtension::get_singleton();
  1297. if (performance_settings_ext && performance_settings_ext->is_available()) {
  1298. performance_settings_ext->set_gpu_level(p_level);
  1299. }
  1300. }
  1301. void OpenXRInterface::on_cpu_level_changed(PerfSettingsSubDomain p_sub_domain, PerfSettingsNotificationLevel p_from_level, PerfSettingsNotificationLevel p_to_level) {
  1302. emit_signal(SNAME("cpu_level_changed"), p_sub_domain, p_from_level, p_to_level);
  1303. }
  1304. void OpenXRInterface::on_gpu_level_changed(PerfSettingsSubDomain p_sub_domain, PerfSettingsNotificationLevel p_from_level, PerfSettingsNotificationLevel p_to_level) {
  1305. emit_signal(SNAME("gpu_level_changed"), p_sub_domain, p_from_level, p_to_level);
  1306. }
  1307. OpenXRInterface::OpenXRInterface() {
  1308. openxr_api = OpenXRAPI::get_singleton();
  1309. if (openxr_api) {
  1310. openxr_api->set_xr_interface(this);
  1311. }
  1312. // while we don't have head tracking, don't put the headset on the floor...
  1313. _set_default_pos(head_transform, 1.0, 0);
  1314. _set_default_pos(transform_for_view[0], 1.0, 1);
  1315. _set_default_pos(transform_for_view[1], 1.0, 2);
  1316. }
  1317. OpenXRInterface::~OpenXRInterface() {
  1318. if (is_initialized()) {
  1319. uninitialize();
  1320. }
  1321. if (openxr_api) {
  1322. openxr_api->set_xr_interface(nullptr);
  1323. openxr_api = nullptr;
  1324. }
  1325. }