openxr_interface.cpp 48 KB

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