openxr_interface.cpp 42 KB

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