openxr_interface.cpp 57 KB

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