openxr_interface.cpp 53 KB

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