openxr_interface.cpp 50 KB

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