xr_nodes.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /**************************************************************************/
  2. /* xr_nodes.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_nodes.h"
  31. #include "core/config/project_settings.h"
  32. #include "scene/main/viewport.h"
  33. #include "servers/xr/xr_interface.h"
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////
  35. void XRCamera3D::_bind_tracker() {
  36. XRServer *xr_server = XRServer::get_singleton();
  37. ERR_FAIL_NULL(xr_server);
  38. tracker = xr_server->get_tracker(tracker_name);
  39. if (tracker.is_valid()) {
  40. tracker->connect("pose_changed", callable_mp(this, &XRCamera3D::_pose_changed));
  41. Ref<XRPose> pose = tracker->get_pose(pose_name);
  42. if (pose.is_valid()) {
  43. set_transform(pose->get_adjusted_transform());
  44. }
  45. }
  46. }
  47. void XRCamera3D::_unbind_tracker() {
  48. if (tracker.is_valid()) {
  49. tracker->disconnect("pose_changed", callable_mp(this, &XRCamera3D::_pose_changed));
  50. }
  51. tracker.unref();
  52. }
  53. void XRCamera3D::_changed_tracker(const StringName p_tracker_name, int p_tracker_type) {
  54. if (p_tracker_name == tracker_name) {
  55. _bind_tracker();
  56. }
  57. }
  58. void XRCamera3D::_removed_tracker(const StringName p_tracker_name, int p_tracker_type) {
  59. if (p_tracker_name == tracker_name) {
  60. _unbind_tracker();
  61. }
  62. }
  63. void XRCamera3D::_pose_changed(const Ref<XRPose> &p_pose) {
  64. if (p_pose->get_name() == pose_name) {
  65. set_transform(p_pose->get_adjusted_transform());
  66. }
  67. }
  68. PackedStringArray XRCamera3D::get_configuration_warnings() const {
  69. PackedStringArray warnings = Node::get_configuration_warnings();
  70. if (is_visible() && is_inside_tree()) {
  71. // must be child node of XROrigin3D!
  72. XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
  73. if (origin == nullptr) {
  74. warnings.push_back(RTR("XRCamera3D must have an XROrigin3D node as its parent."));
  75. };
  76. }
  77. return warnings;
  78. };
  79. Vector3 XRCamera3D::project_local_ray_normal(const Point2 &p_pos) const {
  80. // get our XRServer
  81. XRServer *xr_server = XRServer::get_singleton();
  82. ERR_FAIL_NULL_V(xr_server, Vector3());
  83. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  84. if (xr_interface.is_null()) {
  85. // we might be in the editor or have VR turned off, just call superclass
  86. return Camera3D::project_local_ray_normal(p_pos);
  87. }
  88. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  89. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  90. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  91. Vector3 ray;
  92. // Just use the first view, if multiple views are supported this function has no good result
  93. Projection cm = xr_interface->get_projection_for_view(0, viewport_size.aspect(), get_near(), get_far());
  94. Vector2 screen_he = cm.get_viewport_half_extents();
  95. ray = Vector3(((cpos.x / viewport_size.width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (cpos.y / viewport_size.height)) * 2.0 - 1.0) * screen_he.y, -get_near()).normalized();
  96. return ray;
  97. };
  98. Point2 XRCamera3D::unproject_position(const Vector3 &p_pos) const {
  99. // get our XRServer
  100. XRServer *xr_server = XRServer::get_singleton();
  101. ERR_FAIL_NULL_V(xr_server, Vector2());
  102. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  103. if (xr_interface.is_null()) {
  104. // we might be in the editor or have VR turned off, just call superclass
  105. return Camera3D::unproject_position(p_pos);
  106. }
  107. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector2(), "Camera is not inside scene.");
  108. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  109. // Just use the first view, if multiple views are supported this function has no good result
  110. Projection cm = xr_interface->get_projection_for_view(0, viewport_size.aspect(), get_near(), get_far());
  111. Plane p(get_camera_transform().xform_inv(p_pos), 1.0);
  112. p = cm.xform4(p);
  113. p.normal /= p.d;
  114. Point2 res;
  115. res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x;
  116. res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y;
  117. return res;
  118. };
  119. Vector3 XRCamera3D::project_position(const Point2 &p_point, real_t p_z_depth) const {
  120. // get our XRServer
  121. XRServer *xr_server = XRServer::get_singleton();
  122. ERR_FAIL_NULL_V(xr_server, Vector3());
  123. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  124. if (xr_interface.is_null()) {
  125. // we might be in the editor or have VR turned off, just call superclass
  126. return Camera3D::project_position(p_point, p_z_depth);
  127. }
  128. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  129. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  130. // Just use the first view, if multiple views are supported this function has no good result
  131. Projection cm = xr_interface->get_projection_for_view(0, viewport_size.aspect(), get_near(), get_far());
  132. Vector2 vp_he = cm.get_viewport_half_extents();
  133. Vector2 point;
  134. point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0;
  135. point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
  136. point *= vp_he;
  137. Vector3 p(point.x, point.y, -p_z_depth);
  138. return get_camera_transform().xform(p);
  139. };
  140. Vector<Plane> XRCamera3D::get_frustum() const {
  141. // get our XRServer
  142. XRServer *xr_server = XRServer::get_singleton();
  143. ERR_FAIL_NULL_V(xr_server, Vector<Plane>());
  144. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  145. if (xr_interface.is_null()) {
  146. // we might be in the editor or have VR turned off, just call superclass
  147. return Camera3D::get_frustum();
  148. }
  149. ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
  150. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  151. // TODO Just use the first view for now, this is mostly for debugging so we may look into using our combined projection here.
  152. Projection cm = xr_interface->get_projection_for_view(0, viewport_size.aspect(), get_near(), get_far());
  153. return cm.get_projection_planes(get_camera_transform());
  154. };
  155. XRCamera3D::XRCamera3D() {
  156. XRServer *xr_server = XRServer::get_singleton();
  157. ERR_FAIL_NULL(xr_server);
  158. xr_server->connect("tracker_added", callable_mp(this, &XRCamera3D::_changed_tracker));
  159. xr_server->connect("tracker_updated", callable_mp(this, &XRCamera3D::_changed_tracker));
  160. xr_server->connect("tracker_removed", callable_mp(this, &XRCamera3D::_removed_tracker));
  161. // check if our tracker already exists and if so, bind it...
  162. _bind_tracker();
  163. }
  164. XRCamera3D::~XRCamera3D() {
  165. XRServer *xr_server = XRServer::get_singleton();
  166. ERR_FAIL_NULL(xr_server);
  167. xr_server->disconnect("tracker_added", callable_mp(this, &XRCamera3D::_changed_tracker));
  168. xr_server->disconnect("tracker_updated", callable_mp(this, &XRCamera3D::_changed_tracker));
  169. xr_server->disconnect("tracker_removed", callable_mp(this, &XRCamera3D::_removed_tracker));
  170. }
  171. ////////////////////////////////////////////////////////////////////////////////////////////////////
  172. // XRNode3D is a node that has it's transform updated by an XRPositionalTracker.
  173. // Note that trackers are only available in runtime and only after an XRInterface registers one.
  174. // So we bind by name and as long as a tracker isn't available, our node remains inactive.
  175. void XRNode3D::_bind_methods() {
  176. ClassDB::bind_method(D_METHOD("set_tracker", "tracker_name"), &XRNode3D::set_tracker);
  177. ClassDB::bind_method(D_METHOD("get_tracker"), &XRNode3D::get_tracker);
  178. ADD_PROPERTY(PropertyInfo(Variant::STRING, "tracker", PROPERTY_HINT_ENUM_SUGGESTION), "set_tracker", "get_tracker");
  179. ClassDB::bind_method(D_METHOD("set_pose_name", "pose"), &XRNode3D::set_pose_name);
  180. ClassDB::bind_method(D_METHOD("get_pose_name"), &XRNode3D::get_pose_name);
  181. ADD_PROPERTY(PropertyInfo(Variant::STRING, "pose", PROPERTY_HINT_ENUM_SUGGESTION), "set_pose_name", "get_pose_name");
  182. ClassDB::bind_method(D_METHOD("get_is_active"), &XRNode3D::get_is_active);
  183. ClassDB::bind_method(D_METHOD("get_has_tracking_data"), &XRNode3D::get_has_tracking_data);
  184. ClassDB::bind_method(D_METHOD("get_pose"), &XRNode3D::get_pose);
  185. ClassDB::bind_method(D_METHOD("trigger_haptic_pulse", "action_name", "frequency", "amplitude", "duration_sec", "delay_sec"), &XRNode3D::trigger_haptic_pulse);
  186. ADD_SIGNAL(MethodInfo("tracking_changed", PropertyInfo(Variant::BOOL, "tracking")));
  187. };
  188. void XRNode3D::_validate_property(PropertyInfo &p_property) const {
  189. XRServer *xr_server = XRServer::get_singleton();
  190. ERR_FAIL_NULL(xr_server);
  191. if (p_property.name == "tracker") {
  192. PackedStringArray names = xr_server->get_suggested_tracker_names();
  193. String hint_string;
  194. for (const String &name : names) {
  195. hint_string += name + ",";
  196. }
  197. p_property.hint_string = hint_string;
  198. } else if (p_property.name == "pose") {
  199. PackedStringArray names = xr_server->get_suggested_pose_names(tracker_name);
  200. String hint_string;
  201. for (const String &name : names) {
  202. hint_string += name + ",";
  203. }
  204. p_property.hint_string = hint_string;
  205. }
  206. }
  207. void XRNode3D::set_tracker(const StringName p_tracker_name) {
  208. if (tracker.is_valid() && tracker->get_tracker_name() == p_tracker_name) {
  209. // didn't change
  210. return;
  211. }
  212. // just in case
  213. _unbind_tracker();
  214. // copy the name
  215. tracker_name = p_tracker_name;
  216. pose_name = "default";
  217. // see if it's already available
  218. _bind_tracker();
  219. update_configuration_warnings();
  220. notify_property_list_changed();
  221. }
  222. StringName XRNode3D::get_tracker() const {
  223. return tracker_name;
  224. }
  225. void XRNode3D::set_pose_name(const StringName p_pose_name) {
  226. pose_name = p_pose_name;
  227. // Update pose if we are bound to a tracker with a valid pose
  228. Ref<XRPose> pose = get_pose();
  229. if (pose.is_valid()) {
  230. set_transform(pose->get_adjusted_transform());
  231. }
  232. }
  233. StringName XRNode3D::get_pose_name() const {
  234. return pose_name;
  235. }
  236. bool XRNode3D::get_is_active() const {
  237. if (tracker.is_null()) {
  238. return false;
  239. } else if (!tracker->has_pose(pose_name)) {
  240. return false;
  241. } else {
  242. return true;
  243. }
  244. }
  245. bool XRNode3D::get_has_tracking_data() const {
  246. return has_tracking_data;
  247. }
  248. void XRNode3D::trigger_haptic_pulse(const String &p_action_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec) {
  249. // TODO need to link trackers to the interface that registered them so we can call this on the correct interface.
  250. // For now this works fine as in 99% of the cases we only have our primary interface active
  251. XRServer *xr_server = XRServer::get_singleton();
  252. if (xr_server != nullptr) {
  253. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  254. if (xr_interface.is_valid()) {
  255. xr_interface->trigger_haptic_pulse(p_action_name, tracker_name, p_frequency, p_amplitude, p_duration_sec, p_delay_sec);
  256. }
  257. }
  258. }
  259. Ref<XRPose> XRNode3D::get_pose() {
  260. if (tracker.is_valid()) {
  261. return tracker->get_pose(pose_name);
  262. } else {
  263. return Ref<XRPose>();
  264. }
  265. }
  266. void XRNode3D::_bind_tracker() {
  267. ERR_FAIL_COND_MSG(tracker.is_valid(), "Unbind the current tracker first");
  268. XRServer *xr_server = XRServer::get_singleton();
  269. if (xr_server != nullptr) {
  270. tracker = xr_server->get_tracker(tracker_name);
  271. if (tracker.is_null()) {
  272. // It is possible and valid if the tracker isn't available (yet), in this case we just exit
  273. return;
  274. }
  275. tracker->connect("pose_changed", callable_mp(this, &XRNode3D::_pose_changed));
  276. tracker->connect("pose_lost_tracking", callable_mp(this, &XRNode3D::_pose_lost_tracking));
  277. Ref<XRPose> pose = get_pose();
  278. if (pose.is_valid()) {
  279. set_transform(pose->get_adjusted_transform());
  280. _set_has_tracking_data(pose->get_has_tracking_data());
  281. }
  282. }
  283. }
  284. void XRNode3D::_unbind_tracker() {
  285. if (tracker.is_valid()) {
  286. tracker->disconnect("pose_changed", callable_mp(this, &XRNode3D::_pose_changed));
  287. tracker->disconnect("pose_lost_tracking", callable_mp(this, &XRNode3D::_pose_lost_tracking));
  288. tracker.unref();
  289. _set_has_tracking_data(false);
  290. }
  291. }
  292. void XRNode3D::_changed_tracker(const StringName p_tracker_name, int p_tracker_type) {
  293. if (tracker_name == p_tracker_name) {
  294. // just in case unref our current tracker
  295. _unbind_tracker();
  296. // get our new tracker
  297. _bind_tracker();
  298. }
  299. }
  300. void XRNode3D::_removed_tracker(const StringName p_tracker_name, int p_tracker_type) {
  301. if (tracker_name == p_tracker_name) {
  302. // unref our tracker, it's no longer available
  303. _unbind_tracker();
  304. }
  305. }
  306. void XRNode3D::_pose_changed(const Ref<XRPose> &p_pose) {
  307. if (p_pose.is_valid() && p_pose->get_name() == pose_name) {
  308. set_transform(p_pose->get_adjusted_transform());
  309. _set_has_tracking_data(p_pose->get_has_tracking_data());
  310. }
  311. }
  312. void XRNode3D::_pose_lost_tracking(const Ref<XRPose> &p_pose) {
  313. if (p_pose.is_valid() && p_pose->get_name() == pose_name) {
  314. _set_has_tracking_data(false);
  315. }
  316. }
  317. void XRNode3D::_set_has_tracking_data(bool p_has_tracking_data) {
  318. // Ignore if the has_tracking_data state isn't changing.
  319. if (p_has_tracking_data == has_tracking_data) {
  320. return;
  321. }
  322. // Handle change of has_tracking_data.
  323. has_tracking_data = p_has_tracking_data;
  324. emit_signal(SNAME("tracking_changed"), has_tracking_data);
  325. }
  326. XRNode3D::XRNode3D() {
  327. XRServer *xr_server = XRServer::get_singleton();
  328. ERR_FAIL_NULL(xr_server);
  329. xr_server->connect("tracker_added", callable_mp(this, &XRNode3D::_changed_tracker));
  330. xr_server->connect("tracker_updated", callable_mp(this, &XRNode3D::_changed_tracker));
  331. xr_server->connect("tracker_removed", callable_mp(this, &XRNode3D::_removed_tracker));
  332. }
  333. XRNode3D::~XRNode3D() {
  334. _unbind_tracker();
  335. XRServer *xr_server = XRServer::get_singleton();
  336. ERR_FAIL_NULL(xr_server);
  337. xr_server->disconnect("tracker_added", callable_mp(this, &XRNode3D::_changed_tracker));
  338. xr_server->disconnect("tracker_updated", callable_mp(this, &XRNode3D::_changed_tracker));
  339. xr_server->disconnect("tracker_removed", callable_mp(this, &XRNode3D::_removed_tracker));
  340. }
  341. PackedStringArray XRNode3D::get_configuration_warnings() const {
  342. PackedStringArray warnings = Node::get_configuration_warnings();
  343. if (is_visible() && is_inside_tree()) {
  344. // must be child node of XROrigin!
  345. XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
  346. if (origin == nullptr) {
  347. warnings.push_back(RTR("XRController3D must have an XROrigin3D node as its parent."));
  348. }
  349. if (tracker_name == "") {
  350. warnings.push_back(RTR("No tracker name is set."));
  351. }
  352. if (pose_name == "") {
  353. warnings.push_back(RTR("No pose is set."));
  354. }
  355. }
  356. return warnings;
  357. }
  358. ////////////////////////////////////////////////////////////////////////////////////////////////////
  359. void XRController3D::_bind_methods() {
  360. // passthroughs to information about our related joystick
  361. ClassDB::bind_method(D_METHOD("is_button_pressed", "name"), &XRController3D::is_button_pressed);
  362. ClassDB::bind_method(D_METHOD("get_input", "name"), &XRController3D::get_input);
  363. ClassDB::bind_method(D_METHOD("get_float", "name"), &XRController3D::get_float);
  364. ClassDB::bind_method(D_METHOD("get_vector2", "name"), &XRController3D::get_vector2);
  365. ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRController3D::get_tracker_hand);
  366. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::STRING, "name")));
  367. ADD_SIGNAL(MethodInfo("button_released", PropertyInfo(Variant::STRING, "name")));
  368. ADD_SIGNAL(MethodInfo("input_float_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::FLOAT, "value")));
  369. ADD_SIGNAL(MethodInfo("input_vector2_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::VECTOR2, "value")));
  370. };
  371. void XRController3D::_bind_tracker() {
  372. XRNode3D::_bind_tracker();
  373. if (tracker.is_valid()) {
  374. // bind to input signals
  375. tracker->connect("button_pressed", callable_mp(this, &XRController3D::_button_pressed));
  376. tracker->connect("button_released", callable_mp(this, &XRController3D::_button_released));
  377. tracker->connect("input_float_changed", callable_mp(this, &XRController3D::_input_float_changed));
  378. tracker->connect("input_vector2_changed", callable_mp(this, &XRController3D::_input_vector2_changed));
  379. }
  380. }
  381. void XRController3D::_unbind_tracker() {
  382. if (tracker.is_valid()) {
  383. // unbind input signals
  384. tracker->disconnect("button_pressed", callable_mp(this, &XRController3D::_button_pressed));
  385. tracker->disconnect("button_released", callable_mp(this, &XRController3D::_button_released));
  386. tracker->disconnect("input_float_changed", callable_mp(this, &XRController3D::_input_float_changed));
  387. tracker->disconnect("input_vector2_changed", callable_mp(this, &XRController3D::_input_vector2_changed));
  388. }
  389. XRNode3D::_unbind_tracker();
  390. }
  391. void XRController3D::_button_pressed(const String &p_name) {
  392. // just pass it on...
  393. emit_signal(SNAME("button_pressed"), p_name);
  394. }
  395. void XRController3D::_button_released(const String &p_name) {
  396. // just pass it on...
  397. emit_signal(SNAME("button_released"), p_name);
  398. }
  399. void XRController3D::_input_float_changed(const String &p_name, float p_value) {
  400. // just pass it on...
  401. emit_signal(SNAME("input_float_changed"), p_name, p_value);
  402. }
  403. void XRController3D::_input_vector2_changed(const String &p_name, Vector2 p_value) {
  404. // just pass it on...
  405. emit_signal(SNAME("input_vector2_changed"), p_name, p_value);
  406. }
  407. bool XRController3D::is_button_pressed(const StringName &p_name) const {
  408. if (tracker.is_valid()) {
  409. // Inputs should already be of the correct type, our XR runtime handles conversions between raw input and the desired type
  410. bool pressed = tracker->get_input(p_name);
  411. return pressed;
  412. } else {
  413. return false;
  414. }
  415. }
  416. Variant XRController3D::get_input(const StringName &p_name) const {
  417. if (tracker.is_valid()) {
  418. return tracker->get_input(p_name);
  419. } else {
  420. return Variant();
  421. }
  422. }
  423. float XRController3D::get_float(const StringName &p_name) const {
  424. if (tracker.is_valid()) {
  425. // Inputs should already be of the correct type, our XR runtime handles conversions between raw input and the desired type, but just in case we convert
  426. Variant input = tracker->get_input(p_name);
  427. switch (input.get_type()) {
  428. case Variant::BOOL: {
  429. bool value = input;
  430. return value ? 1.0 : 0.0;
  431. } break;
  432. case Variant::FLOAT: {
  433. float value = input;
  434. return value;
  435. } break;
  436. default:
  437. return 0.0;
  438. };
  439. } else {
  440. return 0.0;
  441. }
  442. }
  443. Vector2 XRController3D::get_vector2(const StringName &p_name) const {
  444. if (tracker.is_valid()) {
  445. // Inputs should already be of the correct type, our XR runtime handles conversions between raw input and the desired type, but just in case we convert
  446. Variant input = tracker->get_input(p_name);
  447. switch (input.get_type()) {
  448. case Variant::BOOL: {
  449. bool value = input;
  450. return Vector2(value ? 1.0 : 0.0, 0.0);
  451. } break;
  452. case Variant::FLOAT: {
  453. float value = input;
  454. return Vector2(value, 0.0);
  455. } break;
  456. case Variant::VECTOR2: {
  457. Vector2 axis = input;
  458. return axis;
  459. }
  460. default:
  461. return Vector2();
  462. }
  463. } else {
  464. return Vector2();
  465. }
  466. }
  467. XRPositionalTracker::TrackerHand XRController3D::get_tracker_hand() const {
  468. // get our XRServer
  469. if (!tracker.is_valid()) {
  470. return XRPositionalTracker::TRACKER_HAND_UNKNOWN;
  471. }
  472. return tracker->get_tracker_hand();
  473. }
  474. ////////////////////////////////////////////////////////////////////////////////////////////////////
  475. void XRAnchor3D::_bind_methods() {
  476. ClassDB::bind_method(D_METHOD("get_size"), &XRAnchor3D::get_size);
  477. ClassDB::bind_method(D_METHOD("get_plane"), &XRAnchor3D::get_plane);
  478. }
  479. Vector3 XRAnchor3D::get_size() const {
  480. return size;
  481. }
  482. Plane XRAnchor3D::get_plane() const {
  483. Vector3 location = get_position();
  484. Basis orientation = get_transform().basis;
  485. Plane plane(orientation.get_column(1).normalized(), location);
  486. return plane;
  487. }
  488. ////////////////////////////////////////////////////////////////////////////////////////////////////
  489. Vector<XROrigin3D *> XROrigin3D::origin_nodes;
  490. PackedStringArray XROrigin3D::get_configuration_warnings() const {
  491. PackedStringArray warnings = Node::get_configuration_warnings();
  492. if (is_visible() && is_inside_tree()) {
  493. bool has_camera = false;
  494. for (int i = 0; !has_camera && i < get_child_count(); i++) {
  495. XRCamera3D *camera = Object::cast_to<XRCamera3D>(get_child(i));
  496. if (camera) {
  497. // found it!
  498. has_camera = true;
  499. }
  500. }
  501. if (!has_camera) {
  502. warnings.push_back(RTR("XROrigin3D requires an XRCamera3D child node."));
  503. }
  504. }
  505. bool xr_enabled = GLOBAL_GET("xr/shaders/enabled");
  506. if (!xr_enabled) {
  507. warnings.push_back(RTR("XR shaders are not enabled in project settings. Stereoscopic output is not supported unless they are enabled. Please enable `xr/shaders/enabled` to use stereoscopic output."));
  508. }
  509. return warnings;
  510. }
  511. void XROrigin3D::_bind_methods() {
  512. ClassDB::bind_method(D_METHOD("set_world_scale", "world_scale"), &XROrigin3D::set_world_scale);
  513. ClassDB::bind_method(D_METHOD("get_world_scale"), &XROrigin3D::get_world_scale);
  514. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "world_scale"), "set_world_scale", "get_world_scale");
  515. ClassDB::bind_method(D_METHOD("set_current", "enabled"), &XROrigin3D::set_current);
  516. ClassDB::bind_method(D_METHOD("is_current"), &XROrigin3D::is_current);
  517. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "set_current", "is_current");
  518. }
  519. real_t XROrigin3D::get_world_scale() const {
  520. // get our XRServer
  521. XRServer *xr_server = XRServer::get_singleton();
  522. ERR_FAIL_NULL_V(xr_server, 1.0);
  523. return xr_server->get_world_scale();
  524. }
  525. void XROrigin3D::set_world_scale(real_t p_world_scale) {
  526. // get our XRServer
  527. XRServer *xr_server = XRServer::get_singleton();
  528. ERR_FAIL_NULL(xr_server);
  529. xr_server->set_world_scale(p_world_scale);
  530. }
  531. void XROrigin3D::_set_current(bool p_enabled, bool p_update_others) {
  532. // We run this logic even if current already equals p_enabled as we may have set this previously before we entered our tree.
  533. // This is then called a second time on NOTIFICATION_ENTER_TREE where we actually process activating this origin node.
  534. current = p_enabled;
  535. if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
  536. return;
  537. }
  538. // Notify us of any transform changes
  539. set_notify_local_transform(current);
  540. set_notify_transform(current);
  541. // update XRServer with our current position
  542. if (current) {
  543. XRServer *xr_server = XRServer::get_singleton();
  544. ERR_FAIL_NULL(xr_server);
  545. xr_server->set_world_origin(get_global_transform());
  546. }
  547. // Check if we need to update our other origin nodes accordingly
  548. if (p_update_others) {
  549. if (current) {
  550. for (int i = 0; i < origin_nodes.size(); i++) {
  551. if (origin_nodes[i] != this && origin_nodes[i]->current) {
  552. origin_nodes[i]->_set_current(false, false);
  553. }
  554. }
  555. } else {
  556. // We no longer have a current origin so find the first one we can make current
  557. for (int i = 0; i < origin_nodes.size(); i++) {
  558. if (origin_nodes[i] != this) {
  559. origin_nodes[i]->_set_current(true, false);
  560. return; // we are done.
  561. }
  562. }
  563. }
  564. }
  565. }
  566. void XROrigin3D::set_current(bool p_enabled) {
  567. _set_current(p_enabled, true);
  568. }
  569. bool XROrigin3D::is_current() const {
  570. if (Engine::get_singleton()->is_editor_hint()) {
  571. // return as is
  572. return current;
  573. } else {
  574. return current && is_inside_tree();
  575. }
  576. }
  577. void XROrigin3D::_notification(int p_what) {
  578. // get our XRServer
  579. XRServer *xr_server = XRServer::get_singleton();
  580. ERR_FAIL_NULL(xr_server);
  581. switch (p_what) {
  582. case NOTIFICATION_ENTER_TREE: {
  583. if (!Engine::get_singleton()->is_editor_hint()) {
  584. if (origin_nodes.is_empty()) {
  585. // first entry always becomes current
  586. current = true;
  587. }
  588. origin_nodes.push_back(this);
  589. if (current) {
  590. // set this again so we do whatever setup is needed.
  591. set_current(true);
  592. }
  593. }
  594. } break;
  595. case NOTIFICATION_EXIT_TREE: {
  596. if (!Engine::get_singleton()->is_editor_hint()) {
  597. origin_nodes.erase(this);
  598. if (current) {
  599. // We are no longer current
  600. set_current(false);
  601. }
  602. }
  603. } break;
  604. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED:
  605. case NOTIFICATION_TRANSFORM_CHANGED: {
  606. if (current && !Engine::get_singleton()->is_editor_hint()) {
  607. xr_server->set_world_origin(get_global_transform());
  608. }
  609. } break;
  610. }
  611. if (current) {
  612. // send our notification to all active XE interfaces, they may need to react to it also
  613. for (int i = 0; i < xr_server->get_interface_count(); i++) {
  614. Ref<XRInterface> interface = xr_server->get_interface(i);
  615. if (interface.is_valid() && interface->is_initialized()) {
  616. interface->notification(p_what);
  617. }
  618. }
  619. }
  620. }