openxr_interface.cpp 56 KB

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