xr_server.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. for (int i = 0; i < interfaces.size(); i++) {
  176. if (interfaces[i]->get_name() == p_name) {
  177. return interfaces[i];
  178. };
  179. };
  180. return Ref<XRInterface>();
  181. };
  182. TypedArray<Dictionary> XRServer::get_interfaces() const {
  183. Array ret;
  184. for (int i = 0; i < interfaces.size(); i++) {
  185. Dictionary iface_info;
  186. iface_info["id"] = i;
  187. iface_info["name"] = interfaces[i]->get_name();
  188. ret.push_back(iface_info);
  189. };
  190. return ret;
  191. };
  192. Ref<XRInterface> XRServer::get_primary_interface() const {
  193. return primary_interface;
  194. };
  195. void XRServer::set_primary_interface(const Ref<XRInterface> &p_primary_interface) {
  196. if (p_primary_interface.is_null()) {
  197. print_verbose("XR: Clearing primary interface");
  198. primary_interface.unref();
  199. } else {
  200. primary_interface = p_primary_interface;
  201. print_verbose("XR: Primary interface set to: " + primary_interface->get_name());
  202. }
  203. };
  204. void XRServer::add_tracker(Ref<XRPositionalTracker> p_tracker) {
  205. ERR_FAIL_COND(p_tracker.is_null());
  206. StringName tracker_name = p_tracker->get_tracker_name();
  207. if (trackers.has(tracker_name)) {
  208. if (trackers[tracker_name] != p_tracker) {
  209. // We already have a tracker with this name, we're going to replace it
  210. trackers[tracker_name] = p_tracker;
  211. emit_signal(SNAME("tracker_updated"), tracker_name, p_tracker->get_tracker_type());
  212. }
  213. } else {
  214. trackers[tracker_name] = p_tracker;
  215. emit_signal(SNAME("tracker_added"), tracker_name, p_tracker->get_tracker_type());
  216. }
  217. };
  218. void XRServer::remove_tracker(Ref<XRPositionalTracker> p_tracker) {
  219. ERR_FAIL_COND(p_tracker.is_null());
  220. StringName tracker_name = p_tracker->get_tracker_name();
  221. if (trackers.has(tracker_name)) {
  222. // we send the signal right before removing it
  223. emit_signal(SNAME("tracker_removed"), p_tracker->get_tracker_name(), p_tracker->get_tracker_type());
  224. // and remove it
  225. trackers.erase(tracker_name);
  226. }
  227. };
  228. Dictionary XRServer::get_trackers(int p_tracker_types) {
  229. Dictionary res;
  230. for (int i = 0; i < trackers.size(); i++) {
  231. Ref<XRPositionalTracker> tracker = trackers.get_value_at_index(i);
  232. if (tracker.is_valid() && (tracker->get_tracker_type() & p_tracker_types) != 0) {
  233. res[tracker->get_tracker_name()] = tracker;
  234. }
  235. }
  236. return res;
  237. }
  238. Ref<XRPositionalTracker> XRServer::get_tracker(const StringName &p_name) const {
  239. if (trackers.has(p_name)) {
  240. return trackers[p_name];
  241. } else {
  242. // tracker hasn't been registered yet, which is fine, no need to spam the error log...
  243. return Ref<XRPositionalTracker>();
  244. }
  245. };
  246. PackedStringArray XRServer::get_suggested_tracker_names() const {
  247. PackedStringArray arr;
  248. for (int i = 0; i < interfaces.size(); i++) {
  249. Ref<XRInterface> interface = interfaces[i];
  250. PackedStringArray interface_arr = interface->get_suggested_tracker_names();
  251. for (int a = 0; a < interface_arr.size(); a++) {
  252. if (!arr.has(interface_arr[a])) {
  253. arr.push_back(interface_arr[a]);
  254. }
  255. }
  256. }
  257. if (arr.size() == 0) {
  258. // no suggestions from our tracker? include our defaults
  259. arr.push_back(String("head"));
  260. arr.push_back(String("left_hand"));
  261. arr.push_back(String("right_hand"));
  262. }
  263. return arr;
  264. }
  265. PackedStringArray XRServer::get_suggested_pose_names(const StringName &p_tracker_name) const {
  266. PackedStringArray arr;
  267. for (int i = 0; i < interfaces.size(); i++) {
  268. Ref<XRInterface> interface = interfaces[i];
  269. PackedStringArray interface_arr = interface->get_suggested_pose_names(p_tracker_name);
  270. for (int a = 0; a < interface_arr.size(); a++) {
  271. if (!arr.has(interface_arr[a])) {
  272. arr.push_back(interface_arr[a]);
  273. }
  274. }
  275. }
  276. if (arr.size() == 0) {
  277. // no suggestions from our tracker? include our defaults
  278. arr.push_back(String("default"));
  279. if ((p_tracker_name == "left_hand") || (p_tracker_name == "right_hand")) {
  280. arr.push_back(String("aim"));
  281. arr.push_back(String("grip"));
  282. arr.push_back(String("skeleton"));
  283. }
  284. }
  285. return arr;
  286. }
  287. void XRServer::_process() {
  288. // called from our main game loop before we handle physics and game logic
  289. // note that we can have multiple interfaces active if we have interfaces that purely handle tracking
  290. // process all active interfaces
  291. for (int i = 0; i < interfaces.size(); i++) {
  292. if (!interfaces[i].is_valid()) {
  293. // ignore, not a valid reference
  294. } else if (interfaces[i]->is_initialized()) {
  295. interfaces.write[i]->process();
  296. };
  297. };
  298. };
  299. void XRServer::pre_render() {
  300. // called from RendererViewport.draw_viewports right before we start drawing our viewports
  301. // note that we can have multiple interfaces active if we have interfaces that purely handle tracking
  302. // process all active interfaces
  303. for (int i = 0; i < interfaces.size(); i++) {
  304. if (!interfaces[i].is_valid()) {
  305. // ignore, not a valid reference
  306. } else if (interfaces[i]->is_initialized()) {
  307. interfaces.write[i]->pre_render();
  308. };
  309. };
  310. }
  311. void XRServer::end_frame() {
  312. // called from RenderingServerDefault after Vulkan queues have been submitted
  313. // process all active interfaces
  314. for (int i = 0; i < interfaces.size(); i++) {
  315. if (!interfaces[i].is_valid()) {
  316. // ignore, not a valid reference
  317. } else if (interfaces[i]->is_initialized()) {
  318. interfaces.write[i]->end_frame();
  319. };
  320. };
  321. }
  322. XRServer::XRServer() {
  323. singleton = this;
  324. world_scale = 1.0;
  325. };
  326. XRServer::~XRServer() {
  327. primary_interface.unref();
  328. while (interfaces.size() > 0) {
  329. interfaces.remove_at(0);
  330. }
  331. // TODO pretty sure there is a clear function or something...
  332. while (trackers.size() > 0) {
  333. trackers.erase(trackers.get_key_at_index(0));
  334. }
  335. singleton = nullptr;
  336. };