openxr_interface.cpp 49 KB

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