openxr_interface.cpp 41 KB

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