mobile_vr_interface.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*************************************************************************/
  2. /* mobile_vr_interface.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "mobile_vr_interface.h"
  31. #include "core/os/input.h"
  32. #include "core/os/os.h"
  33. #include "servers/visual/visual_server_global.h"
  34. StringName MobileVRInterface::get_name() const {
  35. return "Native mobile";
  36. };
  37. int MobileVRInterface::get_capabilities() const {
  38. return ARVRInterface::ARVR_STEREO;
  39. };
  40. Vector3 MobileVRInterface::scale_magneto(const Vector3 &p_magnetometer) {
  41. // Our magnetometer doesn't give us nice clean data.
  42. // Well it may on Mac OS X because we're getting a calibrated value in the current implementation but Android we're getting raw data.
  43. // This is a fairly simple adjustment we can do to correct for the magnetometer data being elliptical
  44. Vector3 mag_raw = p_magnetometer;
  45. Vector3 mag_scaled = p_magnetometer;
  46. // update our variables every x frames
  47. if (mag_count > 20) {
  48. mag_current_min = mag_next_min;
  49. mag_current_max = mag_next_max;
  50. mag_count = 0;
  51. } else {
  52. mag_count++;
  53. };
  54. // adjust our min and max
  55. if (mag_raw.x > mag_next_max.x) mag_next_max.x = mag_raw.x;
  56. if (mag_raw.y > mag_next_max.y) mag_next_max.y = mag_raw.y;
  57. if (mag_raw.z > mag_next_max.z) mag_next_max.z = mag_raw.z;
  58. if (mag_raw.x < mag_next_min.x) mag_next_min.x = mag_raw.x;
  59. if (mag_raw.y < mag_next_min.y) mag_next_min.y = mag_raw.y;
  60. if (mag_raw.z < mag_next_min.z) mag_next_min.z = mag_raw.z;
  61. // scale our x, y and z
  62. if (!(mag_current_max.x - mag_current_min.x)) {
  63. mag_raw.x -= (mag_current_min.x + mag_current_max.x) / 2.0;
  64. mag_scaled.x = (mag_raw.x - mag_current_min.x) / ((mag_current_max.x - mag_current_min.x) * 2.0 - 1.0);
  65. };
  66. if (!(mag_current_max.y - mag_current_min.y)) {
  67. mag_raw.y -= (mag_current_min.y + mag_current_max.y) / 2.0;
  68. mag_scaled.y = (mag_raw.y - mag_current_min.y) / ((mag_current_max.y - mag_current_min.y) * 2.0 - 1.0);
  69. };
  70. if (!(mag_current_max.z - mag_current_min.z)) {
  71. mag_raw.z -= (mag_current_min.z + mag_current_max.z) / 2.0;
  72. mag_scaled.z = (mag_raw.z - mag_current_min.z) / ((mag_current_max.z - mag_current_min.z) * 2.0 - 1.0);
  73. };
  74. return mag_scaled;
  75. };
  76. Basis MobileVRInterface::combine_acc_mag(const Vector3 &p_grav, const Vector3 &p_magneto) {
  77. // yup, stock standard cross product solution...
  78. Vector3 up = -p_grav.normalized();
  79. Vector3 magneto_east = up.cross(p_magneto.normalized()); // or is this west?, but should be horizon aligned now
  80. magneto_east.normalize();
  81. Vector3 magneto = up.cross(magneto_east); // and now we have a horizon aligned north
  82. magneto.normalize();
  83. // We use our gravity and magnetometer vectors to construct our matrix
  84. Basis acc_mag_m3;
  85. acc_mag_m3.elements[0] = -magneto_east;
  86. acc_mag_m3.elements[1] = up;
  87. acc_mag_m3.elements[2] = magneto;
  88. return acc_mag_m3;
  89. };
  90. void MobileVRInterface::set_position_from_sensors() {
  91. _THREAD_SAFE_METHOD_
  92. // this is a helper function that attempts to adjust our transform using our 9dof sensors
  93. // 9dof is a misleading marketing term coming from 3 accelerometer axis + 3 gyro axis + 3 magnetometer axis = 9 axis
  94. // but in reality this only offers 3 dof (yaw, pitch, roll) orientation
  95. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  96. uint64_t ticks_elapsed = ticks - last_ticks;
  97. float delta_time = (double)ticks_elapsed / 1000000.0;
  98. // few things we need
  99. Input *input = Input::get_singleton();
  100. Vector3 down(0.0, -1.0, 0.0); // Down is Y negative
  101. Vector3 north(0.0, 0.0, 1.0); // North is Z positive
  102. // make copies of our inputs
  103. bool has_grav = false;
  104. Vector3 acc = input->get_accelerometer();
  105. Vector3 gyro = input->get_gyroscope();
  106. Vector3 grav = input->get_gravity();
  107. Vector3 magneto = scale_magneto(input->get_magnetometer()); // this may be overkill on iOS because we're already getting a calibrated magnetometer reading
  108. if (sensor_first) {
  109. sensor_first = false;
  110. } else {
  111. acc = scrub(acc, last_accerometer_data, 2, 0.2);
  112. magneto = scrub(magneto, last_magnetometer_data, 3, 0.3);
  113. };
  114. last_accerometer_data = acc;
  115. last_magnetometer_data = magneto;
  116. if (grav.length() < 0.1) {
  117. // not ideal but use our accelerometer, this will contain shakey shakey user behaviour
  118. // maybe look into some math but I'm guessing that if this isn't available, its because we lack the gyro sensor to actually work out
  119. // what a stable gravity vector is
  120. grav = acc;
  121. if (grav.length() > 0.1) {
  122. has_grav = true;
  123. };
  124. } else {
  125. has_grav = true;
  126. };
  127. bool has_magneto = magneto.length() > 0.1;
  128. if (gyro.length() > 0.1) {
  129. /* this can return to 0.0 if the user doesn't move the phone, so once on, it's on */
  130. has_gyro = true;
  131. };
  132. if (has_gyro) {
  133. // start with applying our gyro (do NOT smooth our gyro!)
  134. Basis rotate;
  135. rotate.rotate(orientation.get_axis(0), gyro.x * delta_time);
  136. rotate.rotate(orientation.get_axis(1), gyro.y * delta_time);
  137. rotate.rotate(orientation.get_axis(2), gyro.z * delta_time);
  138. orientation = rotate * orientation;
  139. tracking_state = ARVRInterface::ARVR_NORMAL_TRACKING;
  140. };
  141. ///@TODO improve this, the magnetometer is very fidgity sometimes flipping the axis for no apparent reason (probably a bug on my part)
  142. // if you have a gyro + accelerometer that combo tends to be better then combining all three but without a gyro you need the magnetometer..
  143. if (has_magneto && has_grav && !has_gyro) {
  144. // convert to quaternions, easier to smooth those out
  145. Quat transform_quat(orientation);
  146. Quat acc_mag_quat(combine_acc_mag(grav, magneto));
  147. transform_quat = transform_quat.slerp(acc_mag_quat, 0.1);
  148. orientation = Basis(transform_quat);
  149. tracking_state = ARVRInterface::ARVR_NORMAL_TRACKING;
  150. } else if (has_grav) {
  151. // use gravity vector to make sure down is down...
  152. // transform gravity into our world space
  153. grav.normalize();
  154. Vector3 grav_adj = orientation.xform(grav);
  155. float dot = grav_adj.dot(down);
  156. if ((dot > -1.0) && (dot < 1.0)) {
  157. // axis around which we have this rotation
  158. Vector3 axis = grav_adj.cross(down);
  159. axis.normalize();
  160. Basis drift_compensation(axis, acos(dot) * delta_time * 10);
  161. orientation = drift_compensation * orientation;
  162. };
  163. };
  164. // JIC
  165. orientation.orthonormalize();
  166. last_ticks = ticks;
  167. };
  168. void MobileVRInterface::_bind_methods() {
  169. ClassDB::bind_method(D_METHOD("set_iod", "iod"), &MobileVRInterface::set_iod);
  170. ClassDB::bind_method(D_METHOD("get_iod"), &MobileVRInterface::get_iod);
  171. ClassDB::bind_method(D_METHOD("set_display_width", "display_width"), &MobileVRInterface::set_display_width);
  172. ClassDB::bind_method(D_METHOD("get_display_width"), &MobileVRInterface::get_display_width);
  173. ClassDB::bind_method(D_METHOD("set_display_to_lens", "display_to_lens"), &MobileVRInterface::set_display_to_lens);
  174. ClassDB::bind_method(D_METHOD("get_display_to_lens"), &MobileVRInterface::get_display_to_lens);
  175. ClassDB::bind_method(D_METHOD("set_oversample", "oversample"), &MobileVRInterface::set_oversample);
  176. ClassDB::bind_method(D_METHOD("get_oversample"), &MobileVRInterface::get_oversample);
  177. ClassDB::bind_method(D_METHOD("set_k1", "k"), &MobileVRInterface::set_k1);
  178. ClassDB::bind_method(D_METHOD("get_k1"), &MobileVRInterface::get_k1);
  179. ClassDB::bind_method(D_METHOD("set_k2", "k"), &MobileVRInterface::set_k2);
  180. ClassDB::bind_method(D_METHOD("get_k2"), &MobileVRInterface::get_k2);
  181. ADD_PROPERTY(PropertyInfo(Variant::REAL, "iod", PROPERTY_HINT_RANGE, "4.0,10.0,0.1"), "set_iod", "get_iod");
  182. ADD_PROPERTY(PropertyInfo(Variant::REAL, "display_width", PROPERTY_HINT_RANGE, "5.0,25.0,0.1"), "set_display_width", "get_display_width");
  183. ADD_PROPERTY(PropertyInfo(Variant::REAL, "display_to_lens", PROPERTY_HINT_RANGE, "5.0,25.0,0.1"), "set_display_to_lens", "get_display_to_lens");
  184. ADD_PROPERTY(PropertyInfo(Variant::REAL, "oversample", PROPERTY_HINT_RANGE, "1.0,2.0,0.1"), "set_oversample", "get_oversample");
  185. ADD_PROPERTY(PropertyInfo(Variant::REAL, "k1", PROPERTY_HINT_RANGE, "0.1,10.0,0.0001"), "set_k1", "get_k1");
  186. ADD_PROPERTY(PropertyInfo(Variant::REAL, "k2", PROPERTY_HINT_RANGE, "0.1,10.0,0.0001"), "set_k2", "get_k2");
  187. }
  188. void MobileVRInterface::set_iod(const real_t p_iod) {
  189. intraocular_dist = p_iod;
  190. };
  191. real_t MobileVRInterface::get_iod() const {
  192. return intraocular_dist;
  193. };
  194. void MobileVRInterface::set_display_width(const real_t p_display_width) {
  195. display_width = p_display_width;
  196. };
  197. real_t MobileVRInterface::get_display_width() const {
  198. return display_width;
  199. };
  200. void MobileVRInterface::set_display_to_lens(const real_t p_display_to_lens) {
  201. display_to_lens = p_display_to_lens;
  202. };
  203. real_t MobileVRInterface::get_display_to_lens() const {
  204. return display_to_lens;
  205. };
  206. void MobileVRInterface::set_oversample(const real_t p_oversample) {
  207. oversample = p_oversample;
  208. };
  209. real_t MobileVRInterface::get_oversample() const {
  210. return oversample;
  211. };
  212. void MobileVRInterface::set_k1(const real_t p_k1) {
  213. k1 = p_k1;
  214. };
  215. real_t MobileVRInterface::get_k1() const {
  216. return k1;
  217. };
  218. void MobileVRInterface::set_k2(const real_t p_k2) {
  219. k2 = p_k2;
  220. };
  221. real_t MobileVRInterface::get_k2() const {
  222. return k2;
  223. };
  224. bool MobileVRInterface::is_stereo() {
  225. // needs stereo...
  226. return true;
  227. };
  228. bool MobileVRInterface::is_initialized() const {
  229. return (initialized);
  230. };
  231. bool MobileVRInterface::initialize() {
  232. ARVRServer *arvr_server = ARVRServer::get_singleton();
  233. ERR_FAIL_NULL_V(arvr_server, false);
  234. if (!initialized) {
  235. // reset our sensor data and orientation
  236. mag_count = 0;
  237. has_gyro = false;
  238. sensor_first = true;
  239. mag_next_min = Vector3(10000, 10000, 10000);
  240. mag_next_max = Vector3(-10000, -10000, -10000);
  241. mag_current_min = Vector3(0, 0, 0);
  242. mag_current_max = Vector3(0, 0, 0);
  243. // reset our orientation
  244. orientation = Basis();
  245. // make this our primary interface
  246. arvr_server->set_primary_interface(this);
  247. last_ticks = OS::get_singleton()->get_ticks_usec();
  248. initialized = true;
  249. };
  250. return true;
  251. };
  252. void MobileVRInterface::uninitialize() {
  253. if (initialized) {
  254. ARVRServer *arvr_server = ARVRServer::get_singleton();
  255. if (arvr_server != NULL) {
  256. // no longer our primary interface
  257. arvr_server->clear_primary_interface_if(this);
  258. }
  259. initialized = false;
  260. };
  261. };
  262. Size2 MobileVRInterface::get_render_targetsize() {
  263. _THREAD_SAFE_METHOD_
  264. // we use half our window size
  265. Size2 target_size = OS::get_singleton()->get_window_size();
  266. target_size.x *= 0.5 * oversample;
  267. target_size.y *= oversample;
  268. return target_size;
  269. };
  270. Transform MobileVRInterface::get_transform_for_eye(ARVRInterface::Eyes p_eye, const Transform &p_cam_transform) {
  271. _THREAD_SAFE_METHOD_
  272. Transform transform_for_eye;
  273. ARVRServer *arvr_server = ARVRServer::get_singleton();
  274. ERR_FAIL_NULL_V(arvr_server, transform_for_eye);
  275. if (initialized) {
  276. float world_scale = arvr_server->get_world_scale();
  277. // we don't need to check for the existence of our HMD, doesn't effect our values...
  278. // note * 0.01 to convert cm to m and * 0.5 as we're moving half in each direction...
  279. if (p_eye == ARVRInterface::EYE_LEFT) {
  280. transform_for_eye.origin.x = -(intraocular_dist * 0.01 * 0.5 * world_scale);
  281. } else if (p_eye == ARVRInterface::EYE_RIGHT) {
  282. transform_for_eye.origin.x = intraocular_dist * 0.01 * 0.5 * world_scale;
  283. } else {
  284. // for mono we don't reposition, we want our center position.
  285. };
  286. // just scale our origin point of our transform
  287. Transform hmd_transform;
  288. hmd_transform.basis = orientation;
  289. hmd_transform.origin = Vector3(0.0, eye_height * world_scale, 0.0);
  290. transform_for_eye = p_cam_transform * (arvr_server->get_reference_frame()) * hmd_transform * transform_for_eye;
  291. } else {
  292. // huh? well just return what we got....
  293. transform_for_eye = p_cam_transform;
  294. };
  295. return transform_for_eye;
  296. };
  297. CameraMatrix MobileVRInterface::get_projection_for_eye(ARVRInterface::Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far) {
  298. _THREAD_SAFE_METHOD_
  299. CameraMatrix eye;
  300. if (p_eye == ARVRInterface::EYE_MONO) {
  301. ///@TODO for now hardcode some of this, what is really needed here is that this needs to be in sync with the real cameras properties
  302. // which probably means implementing a specific class for iOS and Android. For now this is purely here as an example.
  303. // Note also that if you use a normal viewport with AR/VR turned off you can still use the tracker output of this interface
  304. // to position a stock standard Godot camera and have control over this.
  305. // This will make more sense when we implement ARkit on iOS (probably a separate interface).
  306. eye.set_perspective(60.0, p_aspect, p_z_near, p_z_far, false);
  307. } else {
  308. eye.set_for_hmd(p_eye == ARVRInterface::EYE_LEFT ? 1 : 2, p_aspect, intraocular_dist, display_width, display_to_lens, oversample, p_z_near, p_z_far);
  309. };
  310. return eye;
  311. };
  312. void MobileVRInterface::commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) {
  313. _THREAD_SAFE_METHOD_
  314. // We must have a valid render target
  315. ERR_FAIL_COND(!p_render_target.is_valid());
  316. // Because we are rendering to our device we must use our main viewport!
  317. ERR_FAIL_COND(p_screen_rect == Rect2());
  318. Rect2 dest = p_screen_rect;
  319. Vector2 eye_center;
  320. // we output half a screen
  321. dest.size.x *= 0.5;
  322. if (p_eye == ARVRInterface::EYE_LEFT) {
  323. eye_center.x = ((-intraocular_dist / 2.0) + (display_width / 4.0)) / (display_width / 2.0);
  324. } else if (p_eye == ARVRInterface::EYE_RIGHT) {
  325. dest.position.x = dest.size.x;
  326. eye_center.x = ((intraocular_dist / 2.0) - (display_width / 4.0)) / (display_width / 2.0);
  327. }
  328. // we don't offset the eye center vertically (yet)
  329. eye_center.y = 0.0;
  330. // unset our render target so we are outputting to our main screen by making RasterizerStorageGLES3::system_fbo our current FBO
  331. VSG::rasterizer->set_current_render_target(RID());
  332. // and output
  333. VSG::rasterizer->output_lens_distorted_to_screen(p_render_target, dest, k1, k2, eye_center, oversample);
  334. };
  335. void MobileVRInterface::process() {
  336. _THREAD_SAFE_METHOD_
  337. if (initialized) {
  338. set_position_from_sensors();
  339. };
  340. };
  341. MobileVRInterface::MobileVRInterface() {
  342. initialized = false;
  343. // Just set some defaults for these. At some point we need to look at adding a lookup table for common device + headset combos and/or support reading cardboard QR codes
  344. eye_height = 1.85;
  345. intraocular_dist = 6.0;
  346. display_width = 14.5;
  347. display_to_lens = 4.0;
  348. oversample = 1.5;
  349. k1 = 0.215;
  350. k2 = 0.215;
  351. last_ticks = 0;
  352. };
  353. MobileVRInterface::~MobileVRInterface() {
  354. // and make sure we cleanup if we haven't already
  355. if (is_initialized()) {
  356. uninitialize();
  357. };
  358. };