openxr_interface.cpp 48 KB

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