mobile_vr_interface.cpp 17 KB

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