xr_server.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /**************************************************************************/
  2. /* xr_server.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 "xr_server.h"
  31. #include "core/config/project_settings.h"
  32. #include "xr/xr_interface.h"
  33. #include "xr/xr_positional_tracker.h"
  34. XRServer::XRMode XRServer::xr_mode = XRMODE_DEFAULT;
  35. XRServer::XRMode XRServer::get_xr_mode() {
  36. return xr_mode;
  37. }
  38. void XRServer::set_xr_mode(XRServer::XRMode p_mode) {
  39. xr_mode = p_mode;
  40. }
  41. XRServer *XRServer::singleton = nullptr;
  42. XRServer *XRServer::get_singleton() {
  43. return singleton;
  44. };
  45. void XRServer::_bind_methods() {
  46. ClassDB::bind_method(D_METHOD("get_world_scale"), &XRServer::get_world_scale);
  47. ClassDB::bind_method(D_METHOD("set_world_scale", "scale"), &XRServer::set_world_scale);
  48. ClassDB::bind_method(D_METHOD("get_world_origin"), &XRServer::get_world_origin);
  49. ClassDB::bind_method(D_METHOD("set_world_origin", "world_origin"), &XRServer::set_world_origin);
  50. ClassDB::bind_method(D_METHOD("get_reference_frame"), &XRServer::get_reference_frame);
  51. ClassDB::bind_method(D_METHOD("center_on_hmd", "rotation_mode", "keep_height"), &XRServer::center_on_hmd);
  52. ClassDB::bind_method(D_METHOD("get_hmd_transform"), &XRServer::get_hmd_transform);
  53. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "world_scale"), "set_world_scale", "get_world_scale");
  54. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "world_origin"), "set_world_origin", "get_world_origin");
  55. ClassDB::bind_method(D_METHOD("add_interface", "interface"), &XRServer::add_interface);
  56. ClassDB::bind_method(D_METHOD("get_interface_count"), &XRServer::get_interface_count);
  57. ClassDB::bind_method(D_METHOD("remove_interface", "interface"), &XRServer::remove_interface);
  58. ClassDB::bind_method(D_METHOD("get_interface", "idx"), &XRServer::get_interface);
  59. ClassDB::bind_method(D_METHOD("get_interfaces"), &XRServer::get_interfaces);
  60. ClassDB::bind_method(D_METHOD("find_interface", "name"), &XRServer::find_interface);
  61. ClassDB::bind_method(D_METHOD("add_tracker", "tracker"), &XRServer::add_tracker);
  62. ClassDB::bind_method(D_METHOD("remove_tracker", "tracker"), &XRServer::remove_tracker);
  63. ClassDB::bind_method(D_METHOD("get_trackers", "tracker_types"), &XRServer::get_trackers);
  64. ClassDB::bind_method(D_METHOD("get_tracker", "tracker_name"), &XRServer::get_tracker);
  65. ClassDB::bind_method(D_METHOD("get_primary_interface"), &XRServer::get_primary_interface);
  66. ClassDB::bind_method(D_METHOD("set_primary_interface", "interface"), &XRServer::set_primary_interface);
  67. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "primary_interface"), "set_primary_interface", "get_primary_interface");
  68. BIND_ENUM_CONSTANT(TRACKER_HEAD);
  69. BIND_ENUM_CONSTANT(TRACKER_CONTROLLER);
  70. BIND_ENUM_CONSTANT(TRACKER_BASESTATION);
  71. BIND_ENUM_CONSTANT(TRACKER_ANCHOR);
  72. BIND_ENUM_CONSTANT(TRACKER_ANY_KNOWN);
  73. BIND_ENUM_CONSTANT(TRACKER_UNKNOWN);
  74. BIND_ENUM_CONSTANT(TRACKER_ANY);
  75. BIND_ENUM_CONSTANT(RESET_FULL_ROTATION);
  76. BIND_ENUM_CONSTANT(RESET_BUT_KEEP_TILT);
  77. BIND_ENUM_CONSTANT(DONT_RESET_ROTATION);
  78. ADD_SIGNAL(MethodInfo("interface_added", PropertyInfo(Variant::STRING_NAME, "interface_name")));
  79. ADD_SIGNAL(MethodInfo("interface_removed", PropertyInfo(Variant::STRING_NAME, "interface_name")));
  80. ADD_SIGNAL(MethodInfo("tracker_added", PropertyInfo(Variant::STRING_NAME, "tracker_name"), PropertyInfo(Variant::INT, "type")));
  81. ADD_SIGNAL(MethodInfo("tracker_updated", PropertyInfo(Variant::STRING_NAME, "tracker_name"), PropertyInfo(Variant::INT, "type")));
  82. ADD_SIGNAL(MethodInfo("tracker_removed", PropertyInfo(Variant::STRING_NAME, "tracker_name"), PropertyInfo(Variant::INT, "type")));
  83. };
  84. double XRServer::get_world_scale() const {
  85. return world_scale;
  86. };
  87. void XRServer::set_world_scale(double p_world_scale) {
  88. if (p_world_scale < 0.01) {
  89. p_world_scale = 0.01;
  90. } else if (p_world_scale > 1000.0) {
  91. p_world_scale = 1000.0;
  92. }
  93. world_scale = p_world_scale;
  94. };
  95. Transform3D XRServer::get_world_origin() const {
  96. return world_origin;
  97. };
  98. void XRServer::set_world_origin(const Transform3D &p_world_origin) {
  99. world_origin = p_world_origin;
  100. };
  101. Transform3D XRServer::get_reference_frame() const {
  102. return reference_frame;
  103. };
  104. void XRServer::center_on_hmd(RotationMode p_rotation_mode, bool p_keep_height) {
  105. if (primary_interface == nullptr) {
  106. return;
  107. }
  108. if (primary_interface->get_play_area_mode() == XRInterface::XR_PLAY_AREA_STAGE) {
  109. // center_on_hmd is not available in this mode
  110. reference_frame = Transform3D();
  111. return;
  112. }
  113. // clear our current reference frame or we'll end up double adjusting it
  114. reference_frame = Transform3D();
  115. // requesting our EYE_MONO transform should return our current HMD position
  116. Transform3D new_reference_frame = primary_interface->get_camera_transform();
  117. // remove our tilt
  118. if (p_rotation_mode == 1) {
  119. // take the Y out of our Z
  120. new_reference_frame.basis.set_column(2, Vector3(new_reference_frame.basis.rows[0][2], 0.0, new_reference_frame.basis.rows[2][2]).normalized());
  121. // Y is straight up
  122. new_reference_frame.basis.set_column(1, Vector3(0.0, 1.0, 0.0));
  123. // and X is our cross reference
  124. new_reference_frame.basis.set_column(0, new_reference_frame.basis.get_column(1).cross(new_reference_frame.basis.get_column(2)).normalized());
  125. } else if (p_rotation_mode == 2) {
  126. // remove our rotation, we're only interesting in centering on position
  127. new_reference_frame.basis = Basis();
  128. };
  129. // don't negate our height
  130. if (p_keep_height) {
  131. new_reference_frame.origin.y = 0.0;
  132. };
  133. reference_frame = new_reference_frame.inverse();
  134. };
  135. Transform3D XRServer::get_hmd_transform() {
  136. Transform3D hmd_transform;
  137. if (primary_interface != nullptr) {
  138. hmd_transform = primary_interface->get_camera_transform();
  139. };
  140. return hmd_transform;
  141. };
  142. void XRServer::add_interface(const Ref<XRInterface> &p_interface) {
  143. ERR_FAIL_COND(p_interface.is_null());
  144. for (int i = 0; i < interfaces.size(); i++) {
  145. if (interfaces[i] == p_interface) {
  146. ERR_PRINT("Interface was already added");
  147. return;
  148. };
  149. };
  150. interfaces.push_back(p_interface);
  151. emit_signal(SNAME("interface_added"), p_interface->get_name());
  152. };
  153. void XRServer::remove_interface(const Ref<XRInterface> &p_interface) {
  154. ERR_FAIL_COND(p_interface.is_null());
  155. int idx = -1;
  156. for (int i = 0; i < interfaces.size(); i++) {
  157. if (interfaces[i] == p_interface) {
  158. idx = i;
  159. break;
  160. };
  161. };
  162. ERR_FAIL_COND_MSG(idx == -1, "Interface not found.");
  163. print_verbose("XR: Removed interface" + p_interface->get_name());
  164. emit_signal(SNAME("interface_removed"), p_interface->get_name());
  165. interfaces.remove_at(idx);
  166. };
  167. int XRServer::get_interface_count() const {
  168. return interfaces.size();
  169. };
  170. Ref<XRInterface> XRServer::get_interface(int p_index) const {
  171. ERR_FAIL_INDEX_V(p_index, interfaces.size(), nullptr);
  172. return interfaces[p_index];
  173. };
  174. Ref<XRInterface> XRServer::find_interface(const String &p_name) const {
  175. int idx = -1;
  176. for (int i = 0; i < interfaces.size(); i++) {
  177. if (interfaces[i]->get_name() == p_name) {
  178. idx = i;
  179. break;
  180. };
  181. };
  182. ERR_FAIL_COND_V_MSG(idx == -1, nullptr, "Interface not found.");
  183. return interfaces[idx];
  184. };
  185. TypedArray<Dictionary> XRServer::get_interfaces() const {
  186. Array ret;
  187. for (int i = 0; i < interfaces.size(); i++) {
  188. Dictionary iface_info;
  189. iface_info["id"] = i;
  190. iface_info["name"] = interfaces[i]->get_name();
  191. ret.push_back(iface_info);
  192. };
  193. return ret;
  194. };
  195. Ref<XRInterface> XRServer::get_primary_interface() const {
  196. return primary_interface;
  197. };
  198. void XRServer::set_primary_interface(const Ref<XRInterface> &p_primary_interface) {
  199. if (p_primary_interface.is_null()) {
  200. print_verbose("XR: Clearing primary interface");
  201. primary_interface.unref();
  202. } else {
  203. primary_interface = p_primary_interface;
  204. print_verbose("XR: Primary interface set to: " + primary_interface->get_name());
  205. }
  206. };
  207. void XRServer::add_tracker(Ref<XRPositionalTracker> p_tracker) {
  208. ERR_FAIL_COND(p_tracker.is_null());
  209. StringName tracker_name = p_tracker->get_tracker_name();
  210. if (trackers.has(tracker_name)) {
  211. if (trackers[tracker_name] != p_tracker) {
  212. // We already have a tracker with this name, we're going to replace it
  213. trackers[tracker_name] = p_tracker;
  214. emit_signal(SNAME("tracker_updated"), tracker_name, p_tracker->get_tracker_type());
  215. }
  216. } else {
  217. trackers[tracker_name] = p_tracker;
  218. emit_signal(SNAME("tracker_added"), tracker_name, p_tracker->get_tracker_type());
  219. }
  220. };
  221. void XRServer::remove_tracker(Ref<XRPositionalTracker> p_tracker) {
  222. ERR_FAIL_COND(p_tracker.is_null());
  223. StringName tracker_name = p_tracker->get_tracker_name();
  224. if (trackers.has(tracker_name)) {
  225. // we send the signal right before removing it
  226. emit_signal(SNAME("tracker_removed"), p_tracker->get_tracker_name(), p_tracker->get_tracker_type());
  227. // and remove it
  228. trackers.erase(tracker_name);
  229. }
  230. };
  231. Dictionary XRServer::get_trackers(int p_tracker_types) {
  232. Dictionary res;
  233. for (int i = 0; i < trackers.size(); i++) {
  234. Ref<XRPositionalTracker> tracker = trackers.get_value_at_index(i);
  235. if (tracker.is_valid() && (tracker->get_tracker_type() & p_tracker_types) != 0) {
  236. res[tracker->get_tracker_name()] = tracker;
  237. }
  238. }
  239. return res;
  240. }
  241. Ref<XRPositionalTracker> XRServer::get_tracker(const StringName &p_name) const {
  242. if (trackers.has(p_name)) {
  243. return trackers[p_name];
  244. } else {
  245. // tracker hasn't been registered yet, which is fine, no need to spam the error log...
  246. return Ref<XRPositionalTracker>();
  247. }
  248. };
  249. PackedStringArray XRServer::get_suggested_tracker_names() const {
  250. PackedStringArray arr;
  251. for (int i = 0; i < interfaces.size(); i++) {
  252. Ref<XRInterface> interface = interfaces[i];
  253. PackedStringArray interface_arr = interface->get_suggested_tracker_names();
  254. for (int a = 0; a < interface_arr.size(); a++) {
  255. if (!arr.has(interface_arr[a])) {
  256. arr.push_back(interface_arr[a]);
  257. }
  258. }
  259. }
  260. if (arr.size() == 0) {
  261. // no suggestions from our tracker? include our defaults
  262. arr.push_back(String("head"));
  263. arr.push_back(String("left_hand"));
  264. arr.push_back(String("right_hand"));
  265. }
  266. return arr;
  267. }
  268. PackedStringArray XRServer::get_suggested_pose_names(const StringName &p_tracker_name) const {
  269. PackedStringArray arr;
  270. for (int i = 0; i < interfaces.size(); i++) {
  271. Ref<XRInterface> interface = interfaces[i];
  272. PackedStringArray interface_arr = interface->get_suggested_pose_names(p_tracker_name);
  273. for (int a = 0; a < interface_arr.size(); a++) {
  274. if (!arr.has(interface_arr[a])) {
  275. arr.push_back(interface_arr[a]);
  276. }
  277. }
  278. }
  279. if (arr.size() == 0) {
  280. // no suggestions from our tracker? include our defaults
  281. arr.push_back(String("default"));
  282. if ((p_tracker_name == "left_hand") || (p_tracker_name == "right_hand")) {
  283. arr.push_back(String("aim"));
  284. arr.push_back(String("grip"));
  285. arr.push_back(String("skeleton"));
  286. }
  287. }
  288. return arr;
  289. }
  290. void XRServer::_process() {
  291. // called from our main game loop before we handle physics and game logic
  292. // note that we can have multiple interfaces active if we have interfaces that purely handle tracking
  293. // process all active interfaces
  294. for (int i = 0; i < interfaces.size(); i++) {
  295. if (!interfaces[i].is_valid()) {
  296. // ignore, not a valid reference
  297. } else if (interfaces[i]->is_initialized()) {
  298. interfaces.write[i]->process();
  299. };
  300. };
  301. };
  302. void XRServer::pre_render() {
  303. // called from RendererViewport.draw_viewports right before we start drawing our viewports
  304. // note that we can have multiple interfaces active if we have interfaces that purely handle tracking
  305. // process all active interfaces
  306. for (int i = 0; i < interfaces.size(); i++) {
  307. if (!interfaces[i].is_valid()) {
  308. // ignore, not a valid reference
  309. } else if (interfaces[i]->is_initialized()) {
  310. interfaces.write[i]->pre_render();
  311. };
  312. };
  313. }
  314. void XRServer::end_frame() {
  315. // called from RenderingServerDefault after Vulkan queues have been submitted
  316. // process all active interfaces
  317. for (int i = 0; i < interfaces.size(); i++) {
  318. if (!interfaces[i].is_valid()) {
  319. // ignore, not a valid reference
  320. } else if (interfaces[i]->is_initialized()) {
  321. interfaces.write[i]->end_frame();
  322. };
  323. };
  324. }
  325. XRServer::XRServer() {
  326. singleton = this;
  327. world_scale = 1.0;
  328. };
  329. XRServer::~XRServer() {
  330. primary_interface.unref();
  331. while (interfaces.size() > 0) {
  332. interfaces.remove_at(0);
  333. }
  334. // TODO pretty sure there is a clear function or something...
  335. while (trackers.size() > 0) {
  336. trackers.erase(trackers.get_key_at_index(0));
  337. }
  338. singleton = nullptr;
  339. };