mobile_vr_interface.cpp 17 KB

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