xr_server.cpp 16 KB

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