openxr_interface.cpp 52 KB

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