openxr_interface.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*************************************************************************/
  2. /* openxr_interface.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. void OpenXRInterface::_bind_methods() {
  35. // lifecycle signals
  36. ADD_SIGNAL(MethodInfo("session_begun"));
  37. ADD_SIGNAL(MethodInfo("session_stopping"));
  38. ADD_SIGNAL(MethodInfo("session_focussed"));
  39. ADD_SIGNAL(MethodInfo("session_visible"));
  40. ADD_SIGNAL(MethodInfo("pose_recentered"));
  41. }
  42. StringName OpenXRInterface::get_name() const {
  43. return StringName("OpenXR");
  44. };
  45. uint32_t OpenXRInterface::get_capabilities() const {
  46. return XRInterface::XR_VR + XRInterface::XR_STEREO;
  47. };
  48. PackedStringArray OpenXRInterface::get_suggested_tracker_names() const {
  49. // These are hardcoded in OpenXR, note that they will only be available if added to our action map
  50. PackedStringArray arr = {
  51. "left_hand", // /user/hand/left is mapped to our defaults
  52. "right_hand", // /user/hand/right is mapped to our defaults
  53. "/user/treadmill",
  54. // Even though these are only available if you have the tracker extension,
  55. // we add these as we may be deploying on a different platform than our
  56. // editor is running on.
  57. "/user/vive_tracker_htcx/role/handheld_object",
  58. "/user/vive_tracker_htcx/role/left_foot",
  59. "/user/vive_tracker_htcx/role/right_foot",
  60. "/user/vive_tracker_htcx/role/left_shoulder",
  61. "/user/vive_tracker_htcx/role/right_shoulder",
  62. "/user/vive_tracker_htcx/role/left_elbow",
  63. "/user/vive_tracker_htcx/role/right_elbow",
  64. "/user/vive_tracker_htcx/role/left_knee",
  65. "/user/vive_tracker_htcx/role/right_knee",
  66. "/user/vive_tracker_htcx/role/waist",
  67. "/user/vive_tracker_htcx/role/chest",
  68. "/user/vive_tracker_htcx/role/camera",
  69. "/user/vive_tracker_htcx/role/keyboard"
  70. };
  71. return arr;
  72. }
  73. XRInterface::TrackingStatus OpenXRInterface::get_tracking_status() const {
  74. return tracking_state;
  75. }
  76. void OpenXRInterface::_load_action_map() {
  77. ERR_FAIL_NULL(openxr_api);
  78. // This may seem a bit duplicitous to a little bit of background info here.
  79. // OpenXRActionMap (with all its sub resource classes) is a class that allows us to configure and store an action map in.
  80. // This gives the user the ability to edit the action map in a UI and customise the actions.
  81. // OpenXR however requires us to submit an action map and it takes over from that point and we can no longer change it.
  82. // This system does that push and we store the info needed to then work with this action map going forward.
  83. // Within our openxr device we maintain a number of classes that wrap the relevant OpenXR objects for this.
  84. // Within OpenXRInterface we have a few internal classes that keep track of what we've created.
  85. // This allow us to process the relevant actions each frame.
  86. // just in case clean up
  87. free_trackers();
  88. free_interaction_profiles();
  89. free_action_sets();
  90. Ref<OpenXRActionMap> action_map;
  91. if (Engine::get_singleton()->is_editor_hint()) {
  92. #ifdef TOOLS_ENABLED
  93. action_map.instantiate();
  94. action_map->create_editor_action_sets();
  95. #endif
  96. } else {
  97. String default_tres_name = openxr_api->get_default_action_map_resource_name();
  98. // Check if we can load our default
  99. if (ResourceLoader::exists(default_tres_name)) {
  100. action_map = ResourceLoader::load(default_tres_name);
  101. }
  102. // Check if we need to create default action set
  103. if (action_map.is_null()) {
  104. action_map.instantiate();
  105. action_map->create_default_action_sets();
  106. #ifdef TOOLS_ENABLED
  107. // Save our action sets so our user can
  108. action_map->set_path(default_tres_name, true);
  109. ResourceSaver::save(action_map, default_tres_name);
  110. #endif
  111. }
  112. }
  113. // process our action map
  114. if (action_map.is_valid()) {
  115. HashMap<Ref<OpenXRAction>, Action *> xr_actions;
  116. Array action_set_array = action_map->get_action_sets();
  117. for (int i = 0; i < action_set_array.size(); i++) {
  118. // Create our action set
  119. Ref<OpenXRActionSet> xr_action_set = action_set_array[i];
  120. ActionSet *action_set = create_action_set(xr_action_set->get_name(), xr_action_set->get_localized_name(), xr_action_set->get_priority());
  121. if (!action_set) {
  122. continue;
  123. }
  124. // Now create our actions for these
  125. Array actions = xr_action_set->get_actions();
  126. for (int j = 0; j < actions.size(); j++) {
  127. Ref<OpenXRAction> xr_action = actions[j];
  128. PackedStringArray toplevel_paths = xr_action->get_toplevel_paths();
  129. Vector<Tracker *> trackers_new;
  130. for (int k = 0; k < toplevel_paths.size(); k++) {
  131. Tracker *tracker = find_tracker(toplevel_paths[k], true);
  132. if (tracker) {
  133. trackers_new.push_back(tracker);
  134. }
  135. }
  136. Action *action = create_action(action_set, xr_action->get_name(), xr_action->get_localized_name(), xr_action->get_action_type(), trackers);
  137. if (action) {
  138. // we link our actions back to our trackers so we know which actions to check when we're processing our trackers
  139. for (int t = 0; t < trackers_new.size(); t++) {
  140. link_action_to_tracker(trackers_new[t], action);
  141. }
  142. // add this to our map for creating our interaction profiles
  143. xr_actions[xr_action] = action;
  144. }
  145. }
  146. }
  147. // now do our suggestions
  148. Array interaction_profile_array = action_map->get_interaction_profiles();
  149. for (int i = 0; i < interaction_profile_array.size(); i++) {
  150. Ref<OpenXRInteractionProfile> xr_interaction_profile = interaction_profile_array[i];
  151. // Note, we can only have one entry per interaction profile so if it already exists we clear it out
  152. RID ip = openxr_api->interaction_profile_create(xr_interaction_profile->get_interaction_profile_path());
  153. if (ip.is_valid()) {
  154. openxr_api->interaction_profile_clear_bindings(ip);
  155. Array xr_bindings = xr_interaction_profile->get_bindings();
  156. for (int j = 0; j < xr_bindings.size(); j++) {
  157. Ref<OpenXRIPBinding> xr_binding = xr_bindings[j];
  158. Ref<OpenXRAction> xr_action = xr_binding->get_action();
  159. Action *action = nullptr;
  160. if (xr_actions.has(xr_action)) {
  161. action = xr_actions[xr_action];
  162. } else {
  163. print_line("Action ", xr_action->get_name(), " isn't part of an action set!");
  164. continue;
  165. }
  166. PackedStringArray paths = xr_binding->get_paths();
  167. for (int k = 0; k < paths.size(); k++) {
  168. openxr_api->interaction_profile_add_binding(ip, action->action_rid, paths[k]);
  169. }
  170. }
  171. // Now submit our suggestions
  172. openxr_api->interaction_profile_suggest_bindings(ip);
  173. // And record it in our array so we can clean it up later on
  174. if (interaction_profile_array.has(ip)) {
  175. interaction_profile_array.push_back(ip);
  176. }
  177. }
  178. }
  179. }
  180. }
  181. OpenXRInterface::ActionSet *OpenXRInterface::create_action_set(const String &p_action_set_name, const String &p_localized_name, const int p_priority) {
  182. ERR_FAIL_NULL_V(openxr_api, nullptr);
  183. // find if it already exists
  184. for (int i = 0; i < action_sets.size(); i++) {
  185. if (action_sets[i]->action_set_name == p_action_set_name) {
  186. // already exists in this set
  187. return nullptr;
  188. }
  189. }
  190. ActionSet *action_set = memnew(ActionSet);
  191. action_set->action_set_name = p_action_set_name;
  192. action_set->is_active = true;
  193. action_set->action_set_rid = openxr_api->action_set_create(p_action_set_name, p_localized_name, p_priority);
  194. action_sets.push_back(action_set);
  195. return action_set;
  196. }
  197. void OpenXRInterface::free_action_sets() {
  198. ERR_FAIL_NULL(openxr_api);
  199. for (int i = 0; i < action_sets.size(); i++) {
  200. ActionSet *action_set = action_sets[i];
  201. free_actions(action_set);
  202. openxr_api->action_set_free(action_set->action_set_rid);
  203. memfree(action_set);
  204. }
  205. action_sets.clear();
  206. }
  207. 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) {
  208. ERR_FAIL_NULL_V(openxr_api, nullptr);
  209. for (int i = 0; i < p_action_set->actions.size(); i++) {
  210. if (p_action_set->actions[i]->action_name == p_action_name) {
  211. // already exists in this set
  212. return nullptr;
  213. }
  214. }
  215. Vector<RID> tracker_rids;
  216. for (int i = 0; i < p_trackers.size(); i++) {
  217. tracker_rids.push_back(p_trackers[i]->tracker_rid);
  218. }
  219. Action *action = memnew(Action);
  220. if (p_action_type == OpenXRAction::OPENXR_ACTION_POSE) {
  221. // We can't have dual action names in OpenXR hence we added _pose,
  222. // but default, aim and grip and default pose action names in Godot so rename them on the tracker.
  223. // NOTE need to decide on whether we should keep the naming convention or rename it on Godots side
  224. if (p_action_name == "default_pose") {
  225. action->action_name = "default";
  226. } else if (p_action_name == "aim_pose") {
  227. action->action_name = "aim";
  228. } else if (p_action_name == "grip_pose") {
  229. action->action_name = "grip";
  230. } else {
  231. action->action_name = p_action_name;
  232. }
  233. } else {
  234. action->action_name = p_action_name;
  235. }
  236. action->action_type = p_action_type;
  237. action->action_rid = openxr_api->action_create(p_action_set->action_set_rid, p_action_name, p_localized_name, p_action_type, tracker_rids);
  238. p_action_set->actions.push_back(action);
  239. return action;
  240. }
  241. OpenXRInterface::Action *OpenXRInterface::find_action(const String &p_action_name) {
  242. // We just find the first action by this name
  243. for (int i = 0; i < action_sets.size(); i++) {
  244. for (int j = 0; j < action_sets[i]->actions.size(); j++) {
  245. if (action_sets[i]->actions[j]->action_name == p_action_name) {
  246. return action_sets[i]->actions[j];
  247. }
  248. }
  249. }
  250. // not found
  251. return nullptr;
  252. }
  253. void OpenXRInterface::free_actions(ActionSet *p_action_set) {
  254. ERR_FAIL_NULL(openxr_api);
  255. for (int i = 0; i < p_action_set->actions.size(); i++) {
  256. Action *action = p_action_set->actions[i];
  257. openxr_api->action_free(action->action_rid);
  258. memdelete(action);
  259. }
  260. p_action_set->actions.clear();
  261. }
  262. OpenXRInterface::Tracker *OpenXRInterface::find_tracker(const String &p_tracker_name, bool p_create) {
  263. XRServer *xr_server = XRServer::get_singleton();
  264. ERR_FAIL_NULL_V(xr_server, nullptr);
  265. ERR_FAIL_NULL_V(openxr_api, nullptr);
  266. Tracker *tracker = nullptr;
  267. for (int i = 0; i < trackers.size(); i++) {
  268. tracker = trackers[i];
  269. if (tracker->tracker_name == p_tracker_name) {
  270. return tracker;
  271. }
  272. }
  273. if (!p_create) {
  274. return nullptr;
  275. }
  276. // Create our RID
  277. RID tracker_rid = openxr_api->tracker_create(p_tracker_name);
  278. ERR_FAIL_COND_V(tracker_rid.is_null(), nullptr);
  279. // create our positional tracker
  280. Ref<XRPositionalTracker> positional_tracker;
  281. positional_tracker.instantiate();
  282. // We have standardised some names to make things nicer to the user so lets recognise the toplevel paths related to these.
  283. if (p_tracker_name == "/user/hand/left") {
  284. positional_tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  285. positional_tracker->set_tracker_name("left_hand");
  286. positional_tracker->set_tracker_desc("Left hand controller");
  287. positional_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_LEFT);
  288. } else if (p_tracker_name == "/user/hand/right") {
  289. positional_tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  290. positional_tracker->set_tracker_name("right_hand");
  291. positional_tracker->set_tracker_desc("Right hand controller");
  292. positional_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_RIGHT);
  293. } else {
  294. positional_tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  295. positional_tracker->set_tracker_name(p_tracker_name);
  296. positional_tracker->set_tracker_desc(p_tracker_name);
  297. }
  298. positional_tracker->set_tracker_profile(INTERACTION_PROFILE_NONE);
  299. xr_server->add_tracker(positional_tracker);
  300. // create a new entry
  301. tracker = memnew(Tracker);
  302. tracker->tracker_name = p_tracker_name;
  303. tracker->tracker_rid = tracker_rid;
  304. tracker->positional_tracker = positional_tracker;
  305. tracker->interaction_profile = RID();
  306. trackers.push_back(tracker);
  307. return tracker;
  308. }
  309. void OpenXRInterface::tracker_profile_changed(RID p_tracker, RID p_interaction_profile) {
  310. Tracker *tracker = nullptr;
  311. for (int i = 0; i < trackers.size() && tracker == nullptr; i++) {
  312. if (trackers[i]->tracker_rid == p_tracker) {
  313. tracker = trackers[i];
  314. }
  315. }
  316. ERR_FAIL_NULL(tracker);
  317. tracker->interaction_profile = p_interaction_profile;
  318. if (p_interaction_profile.is_null()) {
  319. print_verbose("OpenXR: Interaction profile for " + tracker->tracker_name + " changed to " + INTERACTION_PROFILE_NONE);
  320. tracker->positional_tracker->set_tracker_profile(INTERACTION_PROFILE_NONE);
  321. } else {
  322. String name = openxr_api->interaction_profile_get_name(p_interaction_profile);
  323. print_verbose("OpenXR: Interaction profile for " + tracker->tracker_name + " changed to " + name);
  324. tracker->positional_tracker->set_tracker_profile(name);
  325. }
  326. }
  327. void OpenXRInterface::link_action_to_tracker(Tracker *p_tracker, Action *p_action) {
  328. if (p_tracker->actions.find(p_action) == -1) {
  329. p_tracker->actions.push_back(p_action);
  330. }
  331. }
  332. void OpenXRInterface::handle_tracker(Tracker *p_tracker) {
  333. ERR_FAIL_NULL(openxr_api);
  334. ERR_FAIL_COND(p_tracker->positional_tracker.is_null());
  335. // Note, which actions are actually bound to inputs are handled by our interaction profiles however interaction
  336. // profiles are suggested bindings for controller types we know about. OpenXR runtimes can stray away from these
  337. // and rebind them or even offer bindings to controllers that are not known to us.
  338. // We don't really have a consistent way to detect whether a controller is active however as long as it is
  339. // unbound it seems to be unavailable, so far unknown controller seem to mimic one of the profiles we've
  340. // supplied.
  341. if (p_tracker->interaction_profile.is_null()) {
  342. return;
  343. }
  344. // We check all actions that are related to our tracker.
  345. for (int i = 0; i < p_tracker->actions.size(); i++) {
  346. Action *action = p_tracker->actions[i];
  347. switch (action->action_type) {
  348. case OpenXRAction::OPENXR_ACTION_BOOL: {
  349. bool pressed = openxr_api->get_action_bool(action->action_rid, p_tracker->tracker_rid);
  350. p_tracker->positional_tracker->set_input(action->action_name, Variant(pressed));
  351. } break;
  352. case OpenXRAction::OPENXR_ACTION_FLOAT: {
  353. real_t value = openxr_api->get_action_float(action->action_rid, p_tracker->tracker_rid);
  354. p_tracker->positional_tracker->set_input(action->action_name, Variant(value));
  355. } break;
  356. case OpenXRAction::OPENXR_ACTION_VECTOR2: {
  357. Vector2 value = openxr_api->get_action_vector2(action->action_rid, p_tracker->tracker_rid);
  358. p_tracker->positional_tracker->set_input(action->action_name, Variant(value));
  359. } break;
  360. case OpenXRAction::OPENXR_ACTION_POSE: {
  361. Transform3D transform;
  362. Vector3 linear, angular;
  363. XRPose::TrackingConfidence confidence = openxr_api->get_action_pose(action->action_rid, p_tracker->tracker_rid, transform, linear, angular);
  364. if (confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  365. p_tracker->positional_tracker->set_pose(action->action_name, transform, linear, angular, confidence);
  366. } else {
  367. p_tracker->positional_tracker->invalidate_pose(action->action_name);
  368. }
  369. } break;
  370. default: {
  371. // not yet supported
  372. } break;
  373. }
  374. }
  375. }
  376. 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) {
  377. ERR_FAIL_NULL(openxr_api);
  378. Action *action = find_action(p_action_name);
  379. ERR_FAIL_NULL(action);
  380. // We need to map our tracker name to our OpenXR name for our inbuild names.
  381. String tracker_name = p_tracker_name;
  382. if (tracker_name == "left_hand") {
  383. tracker_name = "/user/hand/left";
  384. } else if (tracker_name == "right_hand") {
  385. tracker_name = "/user/hand/right";
  386. }
  387. Tracker *tracker = find_tracker(tracker_name);
  388. ERR_FAIL_NULL(tracker);
  389. // TODO OpenXR does not support delay, so we may need to add support for that somehow...
  390. XrDuration duration = XrDuration(p_duration_sec * 1000000000.0); // seconds -> nanoseconds
  391. openxr_api->trigger_haptic_pulse(action->action_rid, tracker->tracker_rid, p_frequency, p_amplitude, duration);
  392. }
  393. void OpenXRInterface::free_trackers() {
  394. XRServer *xr_server = XRServer::get_singleton();
  395. ERR_FAIL_NULL(xr_server);
  396. ERR_FAIL_NULL(openxr_api);
  397. for (int i = 0; i < trackers.size(); i++) {
  398. Tracker *tracker = trackers[i];
  399. openxr_api->tracker_free(tracker->tracker_rid);
  400. xr_server->remove_tracker(tracker->positional_tracker);
  401. tracker->positional_tracker.unref();
  402. memdelete(tracker);
  403. }
  404. trackers.clear();
  405. }
  406. void OpenXRInterface::free_interaction_profiles() {
  407. ERR_FAIL_NULL(openxr_api);
  408. for (int i = 0; i < interaction_profiles.size(); i++) {
  409. openxr_api->interaction_profile_free(interaction_profiles[i]);
  410. }
  411. interaction_profiles.clear();
  412. }
  413. bool OpenXRInterface::initialize_on_startup() const {
  414. if (openxr_api == nullptr) {
  415. return false;
  416. } else if (!openxr_api->is_initialized()) {
  417. return false;
  418. } else {
  419. return true;
  420. }
  421. }
  422. bool OpenXRInterface::is_initialized() const {
  423. return initialized;
  424. };
  425. bool OpenXRInterface::initialize() {
  426. XRServer *xr_server = XRServer::get_singleton();
  427. ERR_FAIL_NULL_V(xr_server, false);
  428. if (openxr_api == nullptr) {
  429. return false;
  430. } else if (!openxr_api->is_initialized()) {
  431. return false;
  432. } else if (initialized) {
  433. return true;
  434. }
  435. // load up our action sets before setting up our session, note that our profiles are suggestions, OpenXR takes ownership of (re)binding
  436. _load_action_map();
  437. if (!openxr_api->initialize_session()) {
  438. return false;
  439. }
  440. // we must create a tracker for our head
  441. head.instantiate();
  442. head->set_tracker_type(XRServer::TRACKER_HEAD);
  443. head->set_tracker_name("head");
  444. head->set_tracker_desc("Players head");
  445. xr_server->add_tracker(head);
  446. // attach action sets
  447. for (int i = 0; i < action_sets.size(); i++) {
  448. openxr_api->action_set_attach(action_sets[i]->action_set_rid);
  449. }
  450. // make this our primary interface
  451. xr_server->set_primary_interface(this);
  452. initialized = true;
  453. return initialized;
  454. }
  455. void OpenXRInterface::uninitialize() {
  456. // Our OpenXR driver will clean itself up properly when Godot exits, so we just do some basic stuff here
  457. // end the session if we need to?
  458. // cleanup stuff
  459. free_trackers();
  460. free_interaction_profiles();
  461. free_action_sets();
  462. XRServer *xr_server = XRServer::get_singleton();
  463. if (xr_server) {
  464. if (head.is_valid()) {
  465. xr_server->remove_tracker(head);
  466. head.unref();
  467. }
  468. }
  469. initialized = false;
  470. }
  471. bool OpenXRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  472. return false;
  473. }
  474. XRInterface::PlayAreaMode OpenXRInterface::get_play_area_mode() const {
  475. return XRInterface::XR_PLAY_AREA_UNKNOWN;
  476. }
  477. bool OpenXRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  478. return false;
  479. }
  480. Size2 OpenXRInterface::get_render_target_size() {
  481. if (openxr_api == nullptr) {
  482. return Size2();
  483. } else {
  484. return openxr_api->get_recommended_target_size();
  485. }
  486. }
  487. uint32_t OpenXRInterface::get_view_count() {
  488. // TODO set this based on our configuration
  489. return 2;
  490. }
  491. void OpenXRInterface::_set_default_pos(Transform3D &p_transform, double p_world_scale, uint64_t p_eye) {
  492. p_transform = Transform3D();
  493. // if we're not tracking, don't put our head on the floor...
  494. p_transform.origin.y = 1.5 * p_world_scale;
  495. // overkill but..
  496. if (p_eye == 1) {
  497. p_transform.origin.x = 0.03 * p_world_scale;
  498. } else if (p_eye == 2) {
  499. p_transform.origin.x = -0.03 * p_world_scale;
  500. }
  501. }
  502. Transform3D OpenXRInterface::get_camera_transform() {
  503. XRServer *xr_server = XRServer::get_singleton();
  504. ERR_FAIL_NULL_V(xr_server, Transform3D());
  505. Transform3D hmd_transform;
  506. double world_scale = xr_server->get_world_scale();
  507. // head_transform should be updated in process
  508. hmd_transform.basis = head_transform.basis;
  509. hmd_transform.origin = head_transform.origin * world_scale;
  510. return hmd_transform;
  511. }
  512. Transform3D OpenXRInterface::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
  513. XRServer *xr_server = XRServer::get_singleton();
  514. ERR_FAIL_NULL_V(xr_server, Transform3D());
  515. Transform3D t;
  516. if (openxr_api && openxr_api->get_view_transform(p_view, t)) {
  517. // update our cached value if we have a valid transform
  518. transform_for_view[p_view] = t;
  519. } else {
  520. // reuse cached value
  521. t = transform_for_view[p_view];
  522. }
  523. // Apply our world scale
  524. double world_scale = xr_server->get_world_scale();
  525. t.origin *= world_scale;
  526. return p_cam_transform * xr_server->get_reference_frame() * t;
  527. }
  528. Projection OpenXRInterface::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
  529. Projection cm;
  530. if (openxr_api) {
  531. if (openxr_api->get_view_projection(p_view, p_z_near, p_z_far, cm)) {
  532. return cm;
  533. }
  534. }
  535. // Failed to get from our OpenXR device? Default to some sort of sensible camera matrix..
  536. cm.set_for_hmd(p_view + 1, 1.0, 6.0, 14.5, 4.0, 1.5, p_z_near, p_z_far);
  537. return cm;
  538. }
  539. RID OpenXRInterface::get_color_texture() {
  540. if (openxr_api) {
  541. return openxr_api->get_color_texture();
  542. } else {
  543. return RID();
  544. }
  545. }
  546. RID OpenXRInterface::get_depth_texture() {
  547. if (openxr_api) {
  548. return openxr_api->get_depth_texture();
  549. } else {
  550. return RID();
  551. }
  552. }
  553. void OpenXRInterface::process() {
  554. if (openxr_api) {
  555. // do our normal process
  556. if (openxr_api->process()) {
  557. Transform3D t;
  558. Vector3 linear_velocity;
  559. Vector3 angular_velocity;
  560. XRPose::TrackingConfidence confidence = openxr_api->get_head_center(t, linear_velocity, angular_velocity);
  561. if (confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  562. // Only update our transform if we have one to update it with
  563. // note that poses are stored without world scale and reference frame applied!
  564. head_transform = t;
  565. head_linear_velocity = linear_velocity;
  566. head_angular_velocity = angular_velocity;
  567. }
  568. }
  569. // handle our action sets....
  570. Vector<RID> active_sets;
  571. for (int i = 0; i < action_sets.size(); i++) {
  572. if (action_sets[i]->is_active) {
  573. active_sets.push_back(action_sets[i]->action_set_rid);
  574. }
  575. }
  576. if (openxr_api->sync_action_sets(active_sets)) {
  577. for (int i = 0; i < trackers.size(); i++) {
  578. handle_tracker(trackers[i]);
  579. }
  580. }
  581. }
  582. if (head.is_valid()) {
  583. // TODO figure out how to get our velocities
  584. head->set_pose("default", head_transform, head_linear_velocity, head_angular_velocity);
  585. // TODO set confidence on pose once we support tracking this..
  586. }
  587. }
  588. void OpenXRInterface::pre_render() {
  589. if (openxr_api) {
  590. openxr_api->pre_render();
  591. }
  592. }
  593. bool OpenXRInterface::pre_draw_viewport(RID p_render_target) {
  594. if (openxr_api) {
  595. return openxr_api->pre_draw_viewport(p_render_target);
  596. } else {
  597. // don't render
  598. return false;
  599. }
  600. }
  601. Vector<BlitToScreen> OpenXRInterface::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
  602. Vector<BlitToScreen> blit_to_screen;
  603. #ifndef ANDROID_ENABLED
  604. // If separate HMD we should output one eye to screen
  605. if (p_screen_rect != Rect2()) {
  606. BlitToScreen blit;
  607. blit.render_target = p_render_target;
  608. blit.multi_view.use_layer = true;
  609. blit.multi_view.layer = 0;
  610. blit.lens_distortion.apply = false;
  611. Size2 render_size = get_render_target_size();
  612. Rect2 dst_rect = p_screen_rect;
  613. float new_height = dst_rect.size.x * (render_size.y / render_size.x);
  614. if (new_height > dst_rect.size.y) {
  615. dst_rect.position.y = (0.5 * dst_rect.size.y) - (0.5 * new_height);
  616. dst_rect.size.y = new_height;
  617. } else {
  618. float new_width = dst_rect.size.y * (render_size.x / render_size.y);
  619. dst_rect.position.x = (0.5 * dst_rect.size.x) - (0.5 * new_width);
  620. dst_rect.size.x = new_width;
  621. }
  622. blit.dst_rect = dst_rect;
  623. blit_to_screen.push_back(blit);
  624. }
  625. #endif
  626. if (openxr_api) {
  627. openxr_api->post_draw_viewport(p_render_target);
  628. }
  629. return blit_to_screen;
  630. }
  631. void OpenXRInterface::end_frame() {
  632. if (openxr_api) {
  633. openxr_api->end_frame();
  634. }
  635. }
  636. bool OpenXRInterface::is_passthrough_supported() {
  637. return passthrough_wrapper != nullptr && passthrough_wrapper->is_passthrough_supported();
  638. }
  639. bool OpenXRInterface::is_passthrough_enabled() {
  640. return passthrough_wrapper != nullptr && passthrough_wrapper->is_passthrough_enabled();
  641. }
  642. bool OpenXRInterface::start_passthrough() {
  643. return passthrough_wrapper != nullptr && passthrough_wrapper->start_passthrough();
  644. }
  645. void OpenXRInterface::stop_passthrough() {
  646. if (passthrough_wrapper) {
  647. passthrough_wrapper->stop_passthrough();
  648. }
  649. }
  650. void OpenXRInterface::on_state_ready() {
  651. emit_signal(SNAME("session_begun"));
  652. }
  653. void OpenXRInterface::on_state_visible() {
  654. emit_signal(SNAME("session_visible"));
  655. }
  656. void OpenXRInterface::on_state_focused() {
  657. emit_signal(SNAME("session_focussed"));
  658. }
  659. void OpenXRInterface::on_state_stopping() {
  660. emit_signal(SNAME("session_stopping"));
  661. }
  662. void OpenXRInterface::on_pose_recentered() {
  663. emit_signal(SNAME("pose_recentered"));
  664. }
  665. OpenXRInterface::OpenXRInterface() {
  666. openxr_api = OpenXRAPI::get_singleton();
  667. if (openxr_api) {
  668. openxr_api->set_xr_interface(this);
  669. }
  670. // while we don't have head tracking, don't put the headset on the floor...
  671. _set_default_pos(head_transform, 1.0, 0);
  672. _set_default_pos(transform_for_view[0], 1.0, 1);
  673. _set_default_pos(transform_for_view[1], 1.0, 2);
  674. passthrough_wrapper = OpenXRFbPassthroughExtensionWrapper::get_singleton();
  675. }
  676. OpenXRInterface::~OpenXRInterface() {
  677. if (is_initialized()) {
  678. uninitialize();
  679. }
  680. if (openxr_api) {
  681. openxr_api->set_xr_interface(nullptr);
  682. openxr_api = nullptr;
  683. }
  684. }