xr_nodes.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*************************************************************************/
  2. /* xr_nodes.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/input/input.h"
  32. #include "servers/xr/xr_interface.h"
  33. #include "servers/xr_server.h"
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////
  35. void XRCamera3D::_notification(int p_what) {
  36. switch (p_what) {
  37. case NOTIFICATION_ENTER_TREE: {
  38. // need to find our XROrigin3D parent and let it know we're its camera!
  39. XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
  40. if (origin != nullptr) {
  41. origin->set_tracked_camera(this);
  42. }
  43. }; break;
  44. case NOTIFICATION_EXIT_TREE: {
  45. // need to find our XROrigin3D parent and let it know we're no longer its camera!
  46. XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
  47. if (origin != nullptr) {
  48. origin->clear_tracked_camera_if(this);
  49. }
  50. }; break;
  51. };
  52. };
  53. String XRCamera3D::get_configuration_warning() const {
  54. if (!is_visible() || !is_inside_tree())
  55. return String();
  56. // must be child node of XROrigin3D!
  57. XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
  58. if (origin == nullptr) {
  59. return TTR("XRCamera3D must have an XROrigin3D node as its parent.");
  60. };
  61. return String();
  62. };
  63. Vector3 XRCamera3D::project_local_ray_normal(const Point2 &p_pos) const {
  64. // get our XRServer
  65. XRServer *xr_server = XRServer::get_singleton();
  66. ERR_FAIL_NULL_V(xr_server, Vector3());
  67. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  68. if (xr_interface.is_null()) {
  69. // we might be in the editor or have VR turned off, just call superclass
  70. return Camera3D::project_local_ray_normal(p_pos);
  71. }
  72. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  73. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  74. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  75. Vector3 ray;
  76. CameraMatrix cm = xr_interface->get_projection_for_eye(XRInterface::EYE_MONO, viewport_size.aspect(), get_znear(), get_zfar());
  77. Vector2 screen_he = cm.get_viewport_half_extents();
  78. 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_znear()).normalized();
  79. return ray;
  80. };
  81. Point2 XRCamera3D::unproject_position(const Vector3 &p_pos) const {
  82. // get our XRServer
  83. XRServer *xr_server = XRServer::get_singleton();
  84. ERR_FAIL_NULL_V(xr_server, Vector2());
  85. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  86. if (xr_interface.is_null()) {
  87. // we might be in the editor or have VR turned off, just call superclass
  88. return Camera3D::unproject_position(p_pos);
  89. }
  90. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector2(), "Camera is not inside scene.");
  91. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  92. CameraMatrix cm = xr_interface->get_projection_for_eye(XRInterface::EYE_MONO, viewport_size.aspect(), get_znear(), get_zfar());
  93. Plane p(get_camera_transform().xform_inv(p_pos), 1.0);
  94. p = cm.xform4(p);
  95. p.normal /= p.d;
  96. Point2 res;
  97. res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x;
  98. res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y;
  99. return res;
  100. };
  101. Vector3 XRCamera3D::project_position(const Point2 &p_point, float p_z_depth) const {
  102. // get our XRServer
  103. XRServer *xr_server = XRServer::get_singleton();
  104. ERR_FAIL_NULL_V(xr_server, Vector3());
  105. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  106. if (xr_interface.is_null()) {
  107. // we might be in the editor or have VR turned off, just call superclass
  108. return Camera3D::project_position(p_point, p_z_depth);
  109. }
  110. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  111. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  112. CameraMatrix cm = xr_interface->get_projection_for_eye(XRInterface::EYE_MONO, viewport_size.aspect(), get_znear(), get_zfar());
  113. Vector2 vp_he = cm.get_viewport_half_extents();
  114. Vector2 point;
  115. point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0;
  116. point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
  117. point *= vp_he;
  118. Vector3 p(point.x, point.y, -p_z_depth);
  119. return get_camera_transform().xform(p);
  120. };
  121. Vector<Plane> XRCamera3D::get_frustum() const {
  122. // get our XRServer
  123. XRServer *xr_server = XRServer::get_singleton();
  124. ERR_FAIL_NULL_V(xr_server, Vector<Plane>());
  125. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  126. if (xr_interface.is_null()) {
  127. // we might be in the editor or have VR turned off, just call superclass
  128. return Camera3D::get_frustum();
  129. }
  130. ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
  131. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  132. CameraMatrix cm = xr_interface->get_projection_for_eye(XRInterface::EYE_MONO, viewport_size.aspect(), get_znear(), get_zfar());
  133. return cm.get_projection_planes(get_camera_transform());
  134. };
  135. XRCamera3D::XRCamera3D(){
  136. // nothing to do here yet for now..
  137. };
  138. XRCamera3D::~XRCamera3D(){
  139. // nothing to do here yet for now..
  140. };
  141. ////////////////////////////////////////////////////////////////////////////////////////////////////
  142. void XRController3D::_notification(int p_what) {
  143. switch (p_what) {
  144. case NOTIFICATION_ENTER_TREE: {
  145. set_process_internal(true);
  146. }; break;
  147. case NOTIFICATION_EXIT_TREE: {
  148. set_process_internal(false);
  149. }; break;
  150. case NOTIFICATION_INTERNAL_PROCESS: {
  151. // get our XRServer
  152. XRServer *xr_server = XRServer::get_singleton();
  153. ERR_FAIL_NULL(xr_server);
  154. // find the tracker for our controller
  155. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
  156. if (tracker == nullptr) {
  157. // this controller is currently turned off
  158. is_active = false;
  159. button_states = 0;
  160. } else {
  161. is_active = true;
  162. set_transform(tracker->get_transform(true));
  163. int joy_id = tracker->get_joy_id();
  164. if (joy_id >= 0) {
  165. int mask = 1;
  166. // check button states
  167. for (int i = 0; i < 16; i++) {
  168. bool was_pressed = (button_states & mask) == mask;
  169. bool is_pressed = Input::get_singleton()->is_joy_button_pressed(joy_id, i);
  170. if (!was_pressed && is_pressed) {
  171. emit_signal("button_pressed", i);
  172. button_states += mask;
  173. } else if (was_pressed && !is_pressed) {
  174. emit_signal("button_release", i);
  175. button_states -= mask;
  176. };
  177. mask = mask << 1;
  178. };
  179. } else {
  180. button_states = 0;
  181. };
  182. // check for an updated mesh
  183. Ref<Mesh> trackerMesh = tracker->get_mesh();
  184. if (mesh != trackerMesh) {
  185. mesh = trackerMesh;
  186. emit_signal("mesh_updated", mesh);
  187. }
  188. };
  189. }; break;
  190. default:
  191. break;
  192. };
  193. };
  194. void XRController3D::_bind_methods() {
  195. ClassDB::bind_method(D_METHOD("set_controller_id", "controller_id"), &XRController3D::set_controller_id);
  196. ClassDB::bind_method(D_METHOD("get_controller_id"), &XRController3D::get_controller_id);
  197. ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_id", PROPERTY_HINT_RANGE, "0,32,1"), "set_controller_id", "get_controller_id");
  198. ClassDB::bind_method(D_METHOD("get_controller_name"), &XRController3D::get_controller_name);
  199. // passthroughs to information about our related joystick
  200. ClassDB::bind_method(D_METHOD("get_joystick_id"), &XRController3D::get_joystick_id);
  201. ClassDB::bind_method(D_METHOD("is_button_pressed", "button"), &XRController3D::is_button_pressed);
  202. ClassDB::bind_method(D_METHOD("get_joystick_axis", "axis"), &XRController3D::get_joystick_axis);
  203. ClassDB::bind_method(D_METHOD("get_is_active"), &XRController3D::get_is_active);
  204. ClassDB::bind_method(D_METHOD("get_hand"), &XRController3D::get_hand);
  205. ClassDB::bind_method(D_METHOD("get_rumble"), &XRController3D::get_rumble);
  206. ClassDB::bind_method(D_METHOD("set_rumble", "rumble"), &XRController3D::set_rumble);
  207. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rumble", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_rumble", "get_rumble");
  208. ADD_PROPERTY_DEFAULT("rumble", 0.0);
  209. ClassDB::bind_method(D_METHOD("get_mesh"), &XRController3D::get_mesh);
  210. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::INT, "button")));
  211. ADD_SIGNAL(MethodInfo("button_release", PropertyInfo(Variant::INT, "button")));
  212. ADD_SIGNAL(MethodInfo("mesh_updated", PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh")));
  213. };
  214. void XRController3D::set_controller_id(int p_controller_id) {
  215. // We don't check any bounds here, this controller may not yet be active and just be a place holder until it is.
  216. // Note that setting this to 0 means this node is not bound to a controller yet.
  217. controller_id = p_controller_id;
  218. update_configuration_warning();
  219. };
  220. int XRController3D::get_controller_id() const {
  221. return controller_id;
  222. };
  223. String XRController3D::get_controller_name() const {
  224. // get our XRServer
  225. XRServer *xr_server = XRServer::get_singleton();
  226. ERR_FAIL_NULL_V(xr_server, String());
  227. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
  228. if (tracker == nullptr) {
  229. return String("Not connected");
  230. };
  231. return tracker->get_name();
  232. };
  233. int XRController3D::get_joystick_id() const {
  234. // get our XRServer
  235. XRServer *xr_server = XRServer::get_singleton();
  236. ERR_FAIL_NULL_V(xr_server, 0);
  237. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
  238. if (tracker == nullptr) {
  239. // No tracker? no joystick id... (0 is our first joystick)
  240. return -1;
  241. };
  242. return tracker->get_joy_id();
  243. };
  244. bool XRController3D::is_button_pressed(int p_button) const {
  245. int joy_id = get_joystick_id();
  246. if (joy_id == -1) {
  247. return false;
  248. };
  249. return Input::get_singleton()->is_joy_button_pressed(joy_id, p_button);
  250. };
  251. float XRController3D::get_joystick_axis(int p_axis) const {
  252. int joy_id = get_joystick_id();
  253. if (joy_id == -1) {
  254. return 0.0;
  255. };
  256. return Input::get_singleton()->get_joy_axis(joy_id, p_axis);
  257. };
  258. real_t XRController3D::get_rumble() const {
  259. // get our XRServer
  260. XRServer *xr_server = XRServer::get_singleton();
  261. ERR_FAIL_NULL_V(xr_server, 0.0);
  262. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
  263. if (tracker == nullptr) {
  264. return 0.0;
  265. };
  266. return tracker->get_rumble();
  267. };
  268. void XRController3D::set_rumble(real_t p_rumble) {
  269. // get our XRServer
  270. XRServer *xr_server = XRServer::get_singleton();
  271. ERR_FAIL_NULL(xr_server);
  272. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
  273. if (tracker != nullptr) {
  274. tracker->set_rumble(p_rumble);
  275. };
  276. };
  277. Ref<Mesh> XRController3D::get_mesh() const {
  278. return mesh;
  279. }
  280. bool XRController3D::get_is_active() const {
  281. return is_active;
  282. };
  283. XRPositionalTracker::TrackerHand XRController3D::get_hand() const {
  284. // get our XRServer
  285. XRServer *xr_server = XRServer::get_singleton();
  286. ERR_FAIL_NULL_V(xr_server, XRPositionalTracker::TRACKER_HAND_UNKNOWN);
  287. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
  288. if (tracker == nullptr) {
  289. return XRPositionalTracker::TRACKER_HAND_UNKNOWN;
  290. };
  291. return tracker->get_hand();
  292. };
  293. String XRController3D::get_configuration_warning() const {
  294. if (!is_visible() || !is_inside_tree())
  295. return String();
  296. // must be child node of XROrigin!
  297. XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
  298. if (origin == nullptr) {
  299. return TTR("XRController3D must have an XROrigin3D node as its parent.");
  300. };
  301. if (controller_id == 0) {
  302. return TTR("The controller ID must not be 0 or this controller won't be bound to an actual controller.");
  303. };
  304. return String();
  305. };
  306. XRController3D::XRController3D() {
  307. controller_id = 1;
  308. is_active = true;
  309. button_states = 0;
  310. };
  311. XRController3D::~XRController3D(){
  312. // nothing to do here yet for now..
  313. };
  314. ////////////////////////////////////////////////////////////////////////////////////////////////////
  315. void XRAnchor3D::_notification(int p_what) {
  316. switch (p_what) {
  317. case NOTIFICATION_ENTER_TREE: {
  318. set_process_internal(true);
  319. }; break;
  320. case NOTIFICATION_EXIT_TREE: {
  321. set_process_internal(false);
  322. }; break;
  323. case NOTIFICATION_INTERNAL_PROCESS: {
  324. // get our XRServer
  325. XRServer *xr_server = XRServer::get_singleton();
  326. ERR_FAIL_NULL(xr_server);
  327. // find the tracker for our anchor
  328. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_ANCHOR, anchor_id);
  329. if (tracker == nullptr) {
  330. // this anchor is currently not available
  331. is_active = false;
  332. } else {
  333. is_active = true;
  334. Transform transform;
  335. // we'll need our world_scale
  336. real_t world_scale = xr_server->get_world_scale();
  337. // get our info from our tracker
  338. transform.basis = tracker->get_orientation();
  339. transform.origin = tracker->get_position(); // <-- already adjusted to world scale
  340. // our basis is scaled to the size of the plane the anchor is tracking
  341. // extract the size from our basis and reset the scale
  342. size = transform.basis.get_scale() * world_scale;
  343. transform.basis.orthonormalize();
  344. // apply our reference frame and set our transform
  345. set_transform(xr_server->get_reference_frame() * transform);
  346. // check for an updated mesh
  347. Ref<Mesh> trackerMesh = tracker->get_mesh();
  348. if (mesh != trackerMesh) {
  349. mesh = trackerMesh;
  350. emit_signal("mesh_updated", mesh);
  351. }
  352. };
  353. }; break;
  354. default:
  355. break;
  356. };
  357. };
  358. void XRAnchor3D::_bind_methods() {
  359. ClassDB::bind_method(D_METHOD("set_anchor_id", "anchor_id"), &XRAnchor3D::set_anchor_id);
  360. ClassDB::bind_method(D_METHOD("get_anchor_id"), &XRAnchor3D::get_anchor_id);
  361. ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_id", PROPERTY_HINT_RANGE, "0,32,1"), "set_anchor_id", "get_anchor_id");
  362. ClassDB::bind_method(D_METHOD("get_anchor_name"), &XRAnchor3D::get_anchor_name);
  363. ClassDB::bind_method(D_METHOD("get_is_active"), &XRAnchor3D::get_is_active);
  364. ClassDB::bind_method(D_METHOD("get_size"), &XRAnchor3D::get_size);
  365. ClassDB::bind_method(D_METHOD("get_plane"), &XRAnchor3D::get_plane);
  366. ClassDB::bind_method(D_METHOD("get_mesh"), &XRAnchor3D::get_mesh);
  367. ADD_SIGNAL(MethodInfo("mesh_updated", PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh")));
  368. };
  369. void XRAnchor3D::set_anchor_id(int p_anchor_id) {
  370. // We don't check any bounds here, this anchor may not yet be active and just be a place holder until it is.
  371. // Note that setting this to 0 means this node is not bound to an anchor yet.
  372. anchor_id = p_anchor_id;
  373. update_configuration_warning();
  374. };
  375. int XRAnchor3D::get_anchor_id() const {
  376. return anchor_id;
  377. };
  378. Vector3 XRAnchor3D::get_size() const {
  379. return size;
  380. };
  381. String XRAnchor3D::get_anchor_name() const {
  382. // get our XRServer
  383. XRServer *xr_server = XRServer::get_singleton();
  384. ERR_FAIL_NULL_V(xr_server, String());
  385. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_ANCHOR, anchor_id);
  386. if (tracker == nullptr) {
  387. return String("Not connected");
  388. };
  389. return tracker->get_name();
  390. };
  391. bool XRAnchor3D::get_is_active() const {
  392. return is_active;
  393. };
  394. String XRAnchor3D::get_configuration_warning() const {
  395. if (!is_visible() || !is_inside_tree())
  396. return String();
  397. // must be child node of XROrigin3D!
  398. XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
  399. if (origin == nullptr) {
  400. return TTR("XRAnchor3D must have an XROrigin3D node as its parent.");
  401. };
  402. if (anchor_id == 0) {
  403. return TTR("The anchor ID must not be 0 or this anchor won't be bound to an actual anchor.");
  404. };
  405. return String();
  406. };
  407. Plane XRAnchor3D::get_plane() const {
  408. Vector3 location = get_translation();
  409. Basis orientation = get_transform().basis;
  410. Plane plane(location, orientation.get_axis(1).normalized());
  411. return plane;
  412. };
  413. Ref<Mesh> XRAnchor3D::get_mesh() const {
  414. return mesh;
  415. }
  416. XRAnchor3D::XRAnchor3D() {
  417. anchor_id = 1;
  418. is_active = true;
  419. };
  420. XRAnchor3D::~XRAnchor3D(){
  421. // nothing to do here yet for now..
  422. };
  423. ////////////////////////////////////////////////////////////////////////////////////////////////////
  424. String XROrigin3D::get_configuration_warning() const {
  425. if (!is_visible() || !is_inside_tree())
  426. return String();
  427. if (tracked_camera == nullptr)
  428. return TTR("XROrigin3D requires an XRCamera3D child node.");
  429. return String();
  430. };
  431. void XROrigin3D::_bind_methods() {
  432. ClassDB::bind_method(D_METHOD("set_world_scale", "world_scale"), &XROrigin3D::set_world_scale);
  433. ClassDB::bind_method(D_METHOD("get_world_scale"), &XROrigin3D::get_world_scale);
  434. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "world_scale"), "set_world_scale", "get_world_scale");
  435. };
  436. void XROrigin3D::set_tracked_camera(XRCamera3D *p_tracked_camera) {
  437. tracked_camera = p_tracked_camera;
  438. };
  439. void XROrigin3D::clear_tracked_camera_if(XRCamera3D *p_tracked_camera) {
  440. if (tracked_camera == p_tracked_camera) {
  441. tracked_camera = nullptr;
  442. };
  443. };
  444. float XROrigin3D::get_world_scale() const {
  445. // get our XRServer
  446. XRServer *xr_server = XRServer::get_singleton();
  447. ERR_FAIL_NULL_V(xr_server, 1.0);
  448. return xr_server->get_world_scale();
  449. };
  450. void XROrigin3D::set_world_scale(float p_world_scale) {
  451. // get our XRServer
  452. XRServer *xr_server = XRServer::get_singleton();
  453. ERR_FAIL_NULL(xr_server);
  454. xr_server->set_world_scale(p_world_scale);
  455. };
  456. void XROrigin3D::_notification(int p_what) {
  457. // get our XRServer
  458. XRServer *xr_server = XRServer::get_singleton();
  459. ERR_FAIL_NULL(xr_server);
  460. switch (p_what) {
  461. case NOTIFICATION_ENTER_TREE: {
  462. set_process_internal(true);
  463. }; break;
  464. case NOTIFICATION_EXIT_TREE: {
  465. set_process_internal(false);
  466. }; break;
  467. case NOTIFICATION_INTERNAL_PROCESS: {
  468. // set our world origin to our node transform
  469. xr_server->set_world_origin(get_global_transform());
  470. // check if we have a primary interface
  471. Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
  472. if (xr_interface.is_valid() && tracked_camera != nullptr) {
  473. // get our positioning transform for our headset
  474. Transform t = xr_interface->get_transform_for_eye(XRInterface::EYE_MONO, Transform());
  475. // now apply this to our camera
  476. tracked_camera->set_transform(t);
  477. };
  478. }; break;
  479. default:
  480. break;
  481. };
  482. // send our notification to all active XE interfaces, they may need to react to it also
  483. for (int i = 0; i < xr_server->get_interface_count(); i++) {
  484. Ref<XRInterface> interface = xr_server->get_interface(i);
  485. if (interface.is_valid() && interface->is_initialized()) {
  486. interface->notification(p_what);
  487. }
  488. }
  489. };
  490. XROrigin3D::XROrigin3D() {
  491. tracked_camera = nullptr;
  492. };
  493. XROrigin3D::~XROrigin3D(){
  494. // nothing to do here yet for now..
  495. };