openxr_interface.cpp 25 KB

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