openxr_interface.cpp 49 KB

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