openxr_interface.cpp 38 KB

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