mobile_vr_interface.cpp 17 KB

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