mobile_vr_interface.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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/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. uint32_t 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 macOS 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. }
  59. if (mag_raw.y > mag_next_max.y) {
  60. mag_next_max.y = mag_raw.y;
  61. }
  62. if (mag_raw.z > mag_next_max.z) {
  63. mag_next_max.z = mag_raw.z;
  64. }
  65. if (mag_raw.x < mag_next_min.x) {
  66. mag_next_min.x = mag_raw.x;
  67. }
  68. if (mag_raw.y < mag_next_min.y) {
  69. mag_next_min.y = mag_raw.y;
  70. }
  71. if (mag_raw.z < mag_next_min.z) {
  72. mag_next_min.z = mag_raw.z;
  73. }
  74. // scale our x, y and z
  75. if (!(mag_current_max.x - mag_current_min.x)) {
  76. mag_raw.x -= (mag_current_min.x + mag_current_max.x) / 2.0;
  77. mag_scaled.x = (mag_raw.x - mag_current_min.x) / ((mag_current_max.x - mag_current_min.x) * 2.0 - 1.0);
  78. };
  79. if (!(mag_current_max.y - mag_current_min.y)) {
  80. mag_raw.y -= (mag_current_min.y + mag_current_max.y) / 2.0;
  81. mag_scaled.y = (mag_raw.y - mag_current_min.y) / ((mag_current_max.y - mag_current_min.y) * 2.0 - 1.0);
  82. };
  83. if (!(mag_current_max.z - mag_current_min.z)) {
  84. mag_raw.z -= (mag_current_min.z + mag_current_max.z) / 2.0;
  85. mag_scaled.z = (mag_raw.z - mag_current_min.z) / ((mag_current_max.z - mag_current_min.z) * 2.0 - 1.0);
  86. };
  87. return mag_scaled;
  88. }
  89. Basis MobileVRInterface::combine_acc_mag(const Vector3 &p_grav, const Vector3 &p_magneto) {
  90. // yup, stock standard cross product solution...
  91. Vector3 up = -p_grav.normalized();
  92. Vector3 magneto_east = up.cross(p_magneto.normalized()); // or is this west?, but should be horizon aligned now
  93. magneto_east.normalize();
  94. Vector3 magneto = up.cross(magneto_east); // and now we have a horizon aligned north
  95. magneto.normalize();
  96. // We use our gravity and magnetometer vectors to construct our matrix
  97. Basis acc_mag_m3;
  98. acc_mag_m3.rows[0] = -magneto_east;
  99. acc_mag_m3.rows[1] = up;
  100. acc_mag_m3.rows[2] = magneto;
  101. return acc_mag_m3;
  102. }
  103. void MobileVRInterface::set_position_from_sensors() {
  104. _THREAD_SAFE_METHOD_
  105. // this is a helper function that attempts to adjust our transform using our 9dof sensors
  106. // 9dof is a misleading marketing term coming from 3 accelerometer axis + 3 gyro axis + 3 magnetometer axis = 9 axis
  107. // but in reality this only offers 3 dof (yaw, pitch, roll) orientation
  108. Basis orientation = head_transform.basis;
  109. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  110. uint64_t ticks_elapsed = ticks - last_ticks;
  111. float delta_time = (double)ticks_elapsed / 1000000.0;
  112. // few things we need
  113. Input *input = Input::get_singleton();
  114. Vector3 down(0.0, -1.0, 0.0); // Down is Y negative
  115. Vector3 north(0.0, 0.0, 1.0); // North is Z positive
  116. // make copies of our inputs
  117. bool has_grav = false;
  118. Vector3 acc = input->get_accelerometer();
  119. Vector3 gyro = input->get_gyroscope();
  120. Vector3 grav = input->get_gravity();
  121. Vector3 magneto = scale_magneto(input->get_magnetometer()); // this may be overkill on iOS because we're already getting a calibrated magnetometer reading
  122. if (sensor_first) {
  123. sensor_first = false;
  124. } else {
  125. acc = scrub(acc, last_accerometer_data, 2, 0.2);
  126. magneto = scrub(magneto, last_magnetometer_data, 3, 0.3);
  127. };
  128. last_accerometer_data = acc;
  129. last_magnetometer_data = magneto;
  130. if (grav.length() < 0.1) {
  131. // not ideal but use our accelerometer, this will contain shaky user behavior
  132. // maybe look into some math but I'm guessing that if this isn't available, it's because we lack the gyro sensor to actually work out
  133. // what a stable gravity vector is
  134. grav = acc;
  135. if (grav.length() > 0.1) {
  136. has_grav = true;
  137. };
  138. } else {
  139. has_grav = true;
  140. };
  141. bool has_magneto = magneto.length() > 0.1;
  142. if (gyro.length() > 0.1) {
  143. /* this can return to 0.0 if the user doesn't move the phone, so once on, it's on */
  144. has_gyro = true;
  145. };
  146. if (has_gyro) {
  147. // start with applying our gyro (do NOT smooth our gyro!)
  148. Basis rotate;
  149. rotate.rotate(orientation.get_column(0), gyro.x * delta_time);
  150. rotate.rotate(orientation.get_column(1), gyro.y * delta_time);
  151. rotate.rotate(orientation.get_column(2), gyro.z * delta_time);
  152. orientation = rotate * orientation;
  153. tracking_state = XRInterface::XR_NORMAL_TRACKING;
  154. tracking_confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
  155. };
  156. ///@TODO improve this, the magnetometer is very fidgety sometimes flipping the axis for no apparent reason (probably a bug on my part)
  157. // if you have a gyro + accelerometer that combo tends to be better than combining all three but without a gyro you need the magnetometer..
  158. if (has_magneto && has_grav && !has_gyro) {
  159. // convert to quaternions, easier to smooth those out
  160. Quaternion transform_quat(orientation);
  161. Quaternion acc_mag_quat(combine_acc_mag(grav, magneto));
  162. transform_quat = transform_quat.slerp(acc_mag_quat, 0.1);
  163. orientation = Basis(transform_quat);
  164. tracking_state = XRInterface::XR_NORMAL_TRACKING;
  165. tracking_confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
  166. } else if (has_grav) {
  167. // use gravity vector to make sure down is down...
  168. // transform gravity into our world space
  169. grav.normalize();
  170. Vector3 grav_adj = orientation.xform(grav);
  171. float dot = grav_adj.dot(down);
  172. if ((dot > -1.0) && (dot < 1.0)) {
  173. // axis around which we have this rotation
  174. Vector3 axis = grav_adj.cross(down);
  175. axis.normalize();
  176. Basis drift_compensation(axis, acos(dot) * delta_time * 10);
  177. orientation = drift_compensation * orientation;
  178. };
  179. };
  180. // and copy to our head transform
  181. head_transform.basis = orientation.orthonormalized();
  182. last_ticks = ticks;
  183. }
  184. void MobileVRInterface::_bind_methods() {
  185. ClassDB::bind_method(D_METHOD("set_eye_height", "eye_height"), &MobileVRInterface::set_eye_height);
  186. ClassDB::bind_method(D_METHOD("get_eye_height"), &MobileVRInterface::get_eye_height);
  187. ClassDB::bind_method(D_METHOD("set_iod", "iod"), &MobileVRInterface::set_iod);
  188. ClassDB::bind_method(D_METHOD("get_iod"), &MobileVRInterface::get_iod);
  189. ClassDB::bind_method(D_METHOD("set_display_width", "display_width"), &MobileVRInterface::set_display_width);
  190. ClassDB::bind_method(D_METHOD("get_display_width"), &MobileVRInterface::get_display_width);
  191. ClassDB::bind_method(D_METHOD("set_display_to_lens", "display_to_lens"), &MobileVRInterface::set_display_to_lens);
  192. ClassDB::bind_method(D_METHOD("get_display_to_lens"), &MobileVRInterface::get_display_to_lens);
  193. ClassDB::bind_method(D_METHOD("set_offset_rect", "offset_rect"), &MobileVRInterface::set_offset_rect);
  194. ClassDB::bind_method(D_METHOD("get_offset_rect"), &MobileVRInterface::get_offset_rect);
  195. ClassDB::bind_method(D_METHOD("set_oversample", "oversample"), &MobileVRInterface::set_oversample);
  196. ClassDB::bind_method(D_METHOD("get_oversample"), &MobileVRInterface::get_oversample);
  197. ClassDB::bind_method(D_METHOD("set_k1", "k"), &MobileVRInterface::set_k1);
  198. ClassDB::bind_method(D_METHOD("get_k1"), &MobileVRInterface::get_k1);
  199. ClassDB::bind_method(D_METHOD("set_k2", "k"), &MobileVRInterface::set_k2);
  200. ClassDB::bind_method(D_METHOD("get_k2"), &MobileVRInterface::get_k2);
  201. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "eye_height", PROPERTY_HINT_RANGE, "0.0,3.0,0.1"), "set_eye_height", "get_eye_height");
  202. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "iod", PROPERTY_HINT_RANGE, "4.0,10.0,0.1"), "set_iod", "get_iod");
  203. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "display_width", PROPERTY_HINT_RANGE, "5.0,25.0,0.1"), "set_display_width", "get_display_width");
  204. 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");
  205. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "offset_rect"), "set_offset_rect", "get_offset_rect");
  206. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "oversample", PROPERTY_HINT_RANGE, "1.0,2.0,0.1"), "set_oversample", "get_oversample");
  207. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "k1", PROPERTY_HINT_RANGE, "0.1,10.0,0.0001"), "set_k1", "get_k1");
  208. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "k2", PROPERTY_HINT_RANGE, "0.1,10.0,0.0001"), "set_k2", "get_k2");
  209. ClassDB::bind_method(D_METHOD("get_vrs_min_radius"), &MobileVRInterface::get_vrs_min_radius);
  210. ClassDB::bind_method(D_METHOD("set_vrs_min_radius", "radius"), &MobileVRInterface::set_vrs_min_radius);
  211. ClassDB::bind_method(D_METHOD("get_vrs_strength"), &MobileVRInterface::get_vrs_strength);
  212. ClassDB::bind_method(D_METHOD("set_vrs_strength", "strength"), &MobileVRInterface::set_vrs_strength);
  213. ADD_GROUP("Vulkan VRS", "vrs_");
  214. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "vrs_min_radius", PROPERTY_HINT_RANGE, "1.0,100.0,1.0"), "set_vrs_min_radius", "get_vrs_min_radius");
  215. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "vrs_strength", PROPERTY_HINT_RANGE, "0.1,10.0,0.1"), "set_vrs_strength", "get_vrs_strength");
  216. }
  217. void MobileVRInterface::set_eye_height(const double p_eye_height) {
  218. eye_height = p_eye_height;
  219. }
  220. double MobileVRInterface::get_eye_height() const {
  221. return eye_height;
  222. }
  223. void MobileVRInterface::set_offset_rect(const Rect2 &p_offset_rect) {
  224. offset_rect = p_offset_rect;
  225. }
  226. Rect2 MobileVRInterface::get_offset_rect() const {
  227. return offset_rect;
  228. }
  229. void MobileVRInterface::set_iod(const double p_iod) {
  230. intraocular_dist = p_iod;
  231. }
  232. double MobileVRInterface::get_iod() const {
  233. return intraocular_dist;
  234. }
  235. void MobileVRInterface::set_display_width(const double p_display_width) {
  236. display_width = p_display_width;
  237. }
  238. double MobileVRInterface::get_display_width() const {
  239. return display_width;
  240. }
  241. void MobileVRInterface::set_display_to_lens(const double p_display_to_lens) {
  242. display_to_lens = p_display_to_lens;
  243. }
  244. double MobileVRInterface::get_display_to_lens() const {
  245. return display_to_lens;
  246. }
  247. void MobileVRInterface::set_oversample(const double p_oversample) {
  248. oversample = p_oversample;
  249. }
  250. double MobileVRInterface::get_oversample() const {
  251. return oversample;
  252. }
  253. void MobileVRInterface::set_k1(const double p_k1) {
  254. k1 = p_k1;
  255. }
  256. double MobileVRInterface::get_k1() const {
  257. return k1;
  258. }
  259. void MobileVRInterface::set_k2(const double p_k2) {
  260. k2 = p_k2;
  261. }
  262. double MobileVRInterface::get_k2() const {
  263. return k2;
  264. }
  265. float MobileVRInterface::get_vrs_min_radius() const {
  266. return xr_vrs.get_vrs_min_radius();
  267. }
  268. void MobileVRInterface::set_vrs_min_radius(float p_vrs_min_radius) {
  269. xr_vrs.set_vrs_min_radius(p_vrs_min_radius);
  270. }
  271. float MobileVRInterface::get_vrs_strength() const {
  272. return xr_vrs.get_vrs_strength();
  273. }
  274. void MobileVRInterface::set_vrs_strength(float p_vrs_strength) {
  275. xr_vrs.set_vrs_strength(p_vrs_strength);
  276. }
  277. uint32_t MobileVRInterface::get_view_count() {
  278. // needs stereo...
  279. return 2;
  280. }
  281. XRInterface::TrackingStatus MobileVRInterface::get_tracking_status() const {
  282. return tracking_state;
  283. }
  284. bool MobileVRInterface::is_initialized() const {
  285. return (initialized);
  286. }
  287. bool MobileVRInterface::initialize() {
  288. XRServer *xr_server = XRServer::get_singleton();
  289. ERR_FAIL_NULL_V(xr_server, false);
  290. if (!initialized) {
  291. // reset our sensor data
  292. mag_count = 0;
  293. has_gyro = false;
  294. sensor_first = true;
  295. mag_next_min = Vector3(10000, 10000, 10000);
  296. mag_next_max = Vector3(-10000, -10000, -10000);
  297. mag_current_min = Vector3(0, 0, 0);
  298. mag_current_max = Vector3(0, 0, 0);
  299. head_transform.basis = Basis();
  300. head_transform.origin = Vector3(0.0, eye_height, 0.0);
  301. // we must create a tracker for our head
  302. head.instantiate();
  303. head->set_tracker_type(XRServer::TRACKER_HEAD);
  304. head->set_tracker_name("head");
  305. head->set_tracker_desc("Players head");
  306. xr_server->add_tracker(head);
  307. // make this our primary interface
  308. xr_server->set_primary_interface(this);
  309. last_ticks = OS::get_singleton()->get_ticks_usec();
  310. initialized = true;
  311. };
  312. return true;
  313. }
  314. void MobileVRInterface::uninitialize() {
  315. if (initialized) {
  316. // do any cleanup here...
  317. XRServer *xr_server = XRServer::get_singleton();
  318. if (xr_server != nullptr) {
  319. if (head.is_valid()) {
  320. xr_server->remove_tracker(head);
  321. head.unref();
  322. }
  323. if (xr_server->get_primary_interface() == this) {
  324. // no longer our primary interface
  325. xr_server->set_primary_interface(nullptr);
  326. }
  327. }
  328. initialized = false;
  329. };
  330. }
  331. Dictionary MobileVRInterface::get_system_info() {
  332. Dictionary dict;
  333. dict[SNAME("XRRuntimeName")] = String("Godot mobile VR interface");
  334. dict[SNAME("XRRuntimeVersion")] = String("");
  335. return dict;
  336. }
  337. bool MobileVRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  338. // This interface has no positional tracking so fix this to 3DOF
  339. return p_mode == XR_PLAY_AREA_3DOF;
  340. }
  341. XRInterface::PlayAreaMode MobileVRInterface::get_play_area_mode() const {
  342. return XR_PLAY_AREA_3DOF;
  343. }
  344. bool MobileVRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  345. return p_mode == XR_PLAY_AREA_3DOF;
  346. }
  347. Size2 MobileVRInterface::get_render_target_size() {
  348. _THREAD_SAFE_METHOD_
  349. // we use half our window size
  350. Size2 target_size = DisplayServer::get_singleton()->window_get_size();
  351. target_size.x *= 0.5 * oversample;
  352. target_size.y *= oversample;
  353. return target_size;
  354. }
  355. Transform3D MobileVRInterface::get_camera_transform() {
  356. _THREAD_SAFE_METHOD_
  357. Transform3D transform_for_eye;
  358. XRServer *xr_server = XRServer::get_singleton();
  359. ERR_FAIL_NULL_V(xr_server, transform_for_eye);
  360. if (initialized) {
  361. float world_scale = xr_server->get_world_scale();
  362. // just scale our origin point of our transform
  363. Transform3D _head_transform = head_transform;
  364. _head_transform.origin *= world_scale;
  365. transform_for_eye = (xr_server->get_reference_frame()) * _head_transform;
  366. }
  367. return transform_for_eye;
  368. }
  369. Transform3D MobileVRInterface::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
  370. _THREAD_SAFE_METHOD_
  371. Transform3D transform_for_eye;
  372. XRServer *xr_server = XRServer::get_singleton();
  373. ERR_FAIL_NULL_V(xr_server, transform_for_eye);
  374. if (initialized) {
  375. float world_scale = xr_server->get_world_scale();
  376. // we don't need to check for the existence of our HMD, doesn't affect our values...
  377. // note * 0.01 to convert cm to m and * 0.5 as we're moving half in each direction...
  378. if (p_view == 0) {
  379. transform_for_eye.origin.x = -(intraocular_dist * 0.01 * 0.5 * world_scale);
  380. } else if (p_view == 1) {
  381. transform_for_eye.origin.x = intraocular_dist * 0.01 * 0.5 * world_scale;
  382. } else {
  383. // should not have any other values..
  384. };
  385. // just scale our origin point of our transform
  386. Transform3D _head_transform = head_transform;
  387. _head_transform.origin *= world_scale;
  388. transform_for_eye = p_cam_transform * (xr_server->get_reference_frame()) * _head_transform * transform_for_eye;
  389. } else {
  390. // huh? well just return what we got....
  391. transform_for_eye = p_cam_transform;
  392. };
  393. return transform_for_eye;
  394. }
  395. Projection MobileVRInterface::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
  396. _THREAD_SAFE_METHOD_
  397. Projection eye;
  398. aspect = p_aspect;
  399. eye.set_for_hmd(p_view + 1, p_aspect, intraocular_dist, display_width, display_to_lens, oversample, p_z_near, p_z_far);
  400. return eye;
  401. }
  402. Vector<BlitToScreen> MobileVRInterface::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
  403. _THREAD_SAFE_METHOD_
  404. Vector<BlitToScreen> blit_to_screen;
  405. // We must have a valid render target.
  406. ERR_FAIL_COND_V(!p_render_target.is_valid(), blit_to_screen);
  407. // We will only output to screen if this is our main viewport.
  408. if (p_screen_rect == Rect2()) {
  409. // Warn the developer once, it's up to the developer to output to screen.
  410. WARN_PRINT_ONCE("SubViewport used with MobileVRInterface, no output to screen");
  411. return blit_to_screen;
  412. }
  413. Rect2 modified_screen_rect = Rect2(p_screen_rect.position + offset_rect.position * p_screen_rect.size, p_screen_rect.size * offset_rect.size);
  414. // and add our blits
  415. BlitToScreen blit;
  416. blit.render_target = p_render_target;
  417. blit.multi_view.use_layer = true;
  418. blit.lens_distortion.apply = true;
  419. blit.lens_distortion.k1 = k1;
  420. blit.lens_distortion.k2 = k2;
  421. blit.lens_distortion.upscale = oversample;
  422. blit.lens_distortion.aspect_ratio = aspect;
  423. // left eye
  424. blit.dst_rect = modified_screen_rect;
  425. blit.dst_rect.size.width *= 0.5;
  426. blit.multi_view.layer = 0;
  427. blit.lens_distortion.eye_center.x = ((-intraocular_dist / 2.0) + (display_width / 4.0)) / (display_width / 2.0);
  428. blit_to_screen.push_back(blit);
  429. // right eye
  430. blit.dst_rect = modified_screen_rect;
  431. blit.dst_rect.size.width *= 0.5;
  432. blit.dst_rect.position.x += blit.dst_rect.size.width;
  433. blit.multi_view.layer = 1;
  434. blit.lens_distortion.eye_center.x = ((intraocular_dist / 2.0) - (display_width / 4.0)) / (display_width / 2.0);
  435. blit_to_screen.push_back(blit);
  436. return blit_to_screen;
  437. }
  438. void MobileVRInterface::process() {
  439. _THREAD_SAFE_METHOD_
  440. if (initialized) {
  441. // update our head transform orientation
  442. set_position_from_sensors();
  443. // update our head transform position (should be constant)
  444. head_transform.origin = Vector3(0.0, eye_height, 0.0);
  445. if (head.is_valid()) {
  446. // Set our head position, note in real space, reference frame and world scale is applied later
  447. head->set_pose("default", head_transform, Vector3(), Vector3(), tracking_confidence);
  448. }
  449. };
  450. }
  451. RID MobileVRInterface::get_vrs_texture() {
  452. PackedVector2Array eye_foci;
  453. Size2 target_size = get_render_target_size();
  454. real_t aspect_ratio = target_size.x / target_size.y;
  455. uint32_t view_count = get_view_count();
  456. for (uint32_t v = 0; v < view_count; v++) {
  457. Projection cm = get_projection_for_view(v, aspect_ratio, 0.1, 1000.0);
  458. Vector3 center = cm.xform(Vector3(0.0, 0.0, 999.0));
  459. eye_foci.push_back(Vector2(center.x, center.y));
  460. }
  461. return xr_vrs.make_vrs_texture(target_size, eye_foci);
  462. }
  463. MobileVRInterface::MobileVRInterface() {}
  464. MobileVRInterface::~MobileVRInterface() {
  465. // and make sure we cleanup if we haven't already
  466. if (is_initialized()) {
  467. uninitialize();
  468. };
  469. }