xr_server.cpp 16 KB

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