arvr_nodes.cpp 21 KB

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