#include "mobile_vr_interface.h"
#include "core/input/input.h"
#include "core/os/os.h"
#include "servers/display_server.h"
#include "servers/rendering/rendering_server_globals.h"
StringName MobileVRInterface::get_name() const {
return "Native mobile";
};
uint32_t MobileVRInterface::get_capabilities() const {
return XRInterface::XR_STEREO;
};
Vector3 MobileVRInterface::scale_magneto(const Vector3 &p_magnetometer) {
Vector3 mag_raw = p_magnetometer;
Vector3 mag_scaled = p_magnetometer;
if (mag_count > 20) {
mag_current_min = mag_next_min;
mag_current_max = mag_next_max;
mag_count = 0;
} else {
mag_count++;
};
if (mag_raw.x > mag_next_max.x) {
mag_next_max.x = mag_raw.x;
}
if (mag_raw.y > mag_next_max.y) {
mag_next_max.y = mag_raw.y;
}
if (mag_raw.z > mag_next_max.z) {
mag_next_max.z = mag_raw.z;
}
if (mag_raw.x < mag_next_min.x) {
mag_next_min.x = mag_raw.x;
}
if (mag_raw.y < mag_next_min.y) {
mag_next_min.y = mag_raw.y;
}
if (mag_raw.z < mag_next_min.z) {
mag_next_min.z = mag_raw.z;
}
if (!(mag_current_max.x - mag_current_min.x)) {
mag_raw.x -= (mag_current_min.x + mag_current_max.x) / 2.0;
mag_scaled.x = (mag_raw.x - mag_current_min.x) / ((mag_current_max.x - mag_current_min.x) * 2.0 - 1.0);
};
if (!(mag_current_max.y - mag_current_min.y)) {
mag_raw.y -= (mag_current_min.y + mag_current_max.y) / 2.0;
mag_scaled.y = (mag_raw.y - mag_current_min.y) / ((mag_current_max.y - mag_current_min.y) * 2.0 - 1.0);
};
if (!(mag_current_max.z - mag_current_min.z)) {
mag_raw.z -= (mag_current_min.z + mag_current_max.z) / 2.0;
mag_scaled.z = (mag_raw.z - mag_current_min.z) / ((mag_current_max.z - mag_current_min.z) * 2.0 - 1.0);
};
return mag_scaled;
};
Basis MobileVRInterface::combine_acc_mag(const Vector3 &p_grav, const Vector3 &p_magneto) {
Vector3 up = -p_grav.normalized();
Vector3 magneto_east = up.cross(p_magneto.normalized());
magneto_east.normalize();
Vector3 magneto = up.cross(magneto_east);
magneto.normalize();
Basis acc_mag_m3;
acc_mag_m3.rows[0] = -magneto_east;
acc_mag_m3.rows[1] = up;
acc_mag_m3.rows[2] = magneto;
return acc_mag_m3;
};
void MobileVRInterface::set_position_from_sensors() {
_THREAD_SAFE_METHOD_
Basis orientation = head_transform.basis;
uint64_t ticks = OS::get_singleton()->get_ticks_usec();
uint64_t ticks_elapsed = ticks - last_ticks;
float delta_time = (double)ticks_elapsed / 1000000.0;
Input *input = Input::get_singleton();
Vector3 down(0.0, -1.0, 0.0);
Vector3 north(0.0, 0.0, 1.0);
bool has_grav = false;
Vector3 acc = input->get_accelerometer();
Vector3 gyro = input->get_gyroscope();
Vector3 grav = input->get_gravity();
Vector3 magneto = scale_magneto(input->get_magnetometer());
if (sensor_first) {
sensor_first = false;
} else {
acc = scrub(acc, last_accerometer_data, 2, 0.2);
magneto = scrub(magneto, last_magnetometer_data, 3, 0.3);
};
last_accerometer_data = acc;
last_magnetometer_data = magneto;
if (grav.length() < 0.1) {
grav = acc;
if (grav.length() > 0.1) {
has_grav = true;
};
} else {
has_grav = true;
};
bool has_magneto = magneto.length() > 0.1;
if (gyro.length() > 0.1) {
has_gyro = true;
};
if (has_gyro) {
Basis rotate;
rotate.rotate(orientation.get_column(0), gyro.x * delta_time);
rotate.rotate(orientation.get_column(1), gyro.y * delta_time);
rotate.rotate(orientation.get_column(2), gyro.z * delta_time);
orientation = rotate * orientation;
tracking_state = XRInterface::XR_NORMAL_TRACKING;
tracking_confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
};
if (has_magneto && has_grav && !has_gyro) {
Quaternion transform_quat(orientation);
Quaternion acc_mag_quat(combine_acc_mag(grav, magneto));
transform_quat = transform_quat.slerp(acc_mag_quat, 0.1);
orientation = Basis(transform_quat);
tracking_state = XRInterface::XR_NORMAL_TRACKING;
tracking_confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
} else if (has_grav) {
grav.normalize();
Vector3 grav_adj = orientation.xform(grav);
float dot = grav_adj.dot(down);
if ((dot > -1.0) && (dot < 1.0)) {
Vector3 axis = grav_adj.cross(down);
axis.normalize();
Basis drift_compensation(axis, acos(dot) * delta_time * 10);
orientation = drift_compensation * orientation;
};
};
head_transform.basis = orientation.orthonormalized();
last_ticks = ticks;
};
void MobileVRInterface::_bind_methods() { … }
void MobileVRInterface::set_eye_height(const double p_eye_height) { … }
double MobileVRInterface::get_eye_height() const { … }
void MobileVRInterface::set_offset_rect(const Rect2 &p_offset_rect) { … }
Rect2 MobileVRInterface::get_offset_rect() const { … }
void MobileVRInterface::set_iod(const double p_iod) {
intraocular_dist = p_iod;
};
double MobileVRInterface::get_iod() const {
return intraocular_dist;
};
void MobileVRInterface::set_display_width(const double p_display_width) {
display_width = p_display_width;
};
double MobileVRInterface::get_display_width() const {
return display_width;
};
void MobileVRInterface::set_display_to_lens(const double p_display_to_lens) {
display_to_lens = p_display_to_lens;
};
double MobileVRInterface::get_display_to_lens() const {
return display_to_lens;
};
void MobileVRInterface::set_oversample(const double p_oversample) {
oversample = p_oversample;
};
double MobileVRInterface::get_oversample() const {
return oversample;
};
void MobileVRInterface::set_k1(const double p_k1) {
k1 = p_k1;
};
double MobileVRInterface::get_k1() const {
return k1;
};
void MobileVRInterface::set_k2(const double p_k2) {
k2 = p_k2;
};
double MobileVRInterface::get_k2() const {
return k2;
};
float MobileVRInterface::get_vrs_min_radius() const { … }
void MobileVRInterface::set_vrs_min_radius(float p_vrs_min_radius) { … }
float MobileVRInterface::get_vrs_strength() const { … }
void MobileVRInterface::set_vrs_strength(float p_vrs_strength) { … }
uint32_t MobileVRInterface::get_view_count() {
return 2;
};
XRInterface::TrackingStatus MobileVRInterface::get_tracking_status() const { … }
bool MobileVRInterface::is_initialized() const {
return (initialized);
};
bool MobileVRInterface::initialize() {
XRServer *xr_server = XRServer::get_singleton();
ERR_FAIL_NULL_V(xr_server, false);
if (!initialized) {
mag_count = 0;
has_gyro = false;
sensor_first = true;
mag_next_min = Vector3(10000, 10000, 10000);
mag_next_max = Vector3(-10000, -10000, -10000);
mag_current_min = Vector3(0, 0, 0);
mag_current_max = Vector3(0, 0, 0);
head_transform.basis = Basis();
head_transform.origin = Vector3(0.0, eye_height, 0.0);
head.instantiate();
head->set_tracker_type(XRServer::TRACKER_HEAD);
head->set_tracker_name("head");
head->set_tracker_desc("Players head");
xr_server->add_tracker(head);
xr_server->set_primary_interface(this);
last_ticks = OS::get_singleton()->get_ticks_usec();
initialized = true;
};
return true;
};
void MobileVRInterface::uninitialize() {
if (initialized) {
XRServer *xr_server = XRServer::get_singleton();
if (xr_server != nullptr) {
if (head.is_valid()) {
xr_server->remove_tracker(head);
head.unref();
}
if (xr_server->get_primary_interface() == this) {
xr_server->set_primary_interface(nullptr);
}
}
initialized = false;
};
};
Dictionary MobileVRInterface::get_system_info() { … }
bool MobileVRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) { … }
XRInterface::PlayAreaMode MobileVRInterface::get_play_area_mode() const { … }
bool MobileVRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) { … }
Size2 MobileVRInterface::get_render_target_size() {
_THREAD_SAFE_METHOD_
Size2 target_size = DisplayServer::get_singleton()->window_get_size();
target_size.x *= 0.5 * oversample;
target_size.y *= oversample;
return target_size;
};
Transform3D MobileVRInterface::get_camera_transform() {
_THREAD_SAFE_METHOD_
Transform3D transform_for_eye;
XRServer *xr_server = XRServer::get_singleton();
ERR_FAIL_NULL_V(xr_server, transform_for_eye);
if (initialized) {
float world_scale = xr_server->get_world_scale();
Transform3D _head_transform = head_transform;
_head_transform.origin *= world_scale;
transform_for_eye = (xr_server->get_reference_frame()) * _head_transform;
}
return transform_for_eye;
};
Transform3D MobileVRInterface::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
_THREAD_SAFE_METHOD_
Transform3D transform_for_eye;
XRServer *xr_server = XRServer::get_singleton();
ERR_FAIL_NULL_V(xr_server, transform_for_eye);
if (initialized) {
float world_scale = xr_server->get_world_scale();
if (p_view == 0) {
transform_for_eye.origin.x = -(intraocular_dist * 0.01 * 0.5 * world_scale);
} else if (p_view == 1) {
transform_for_eye.origin.x = intraocular_dist * 0.01 * 0.5 * world_scale;
} else {
};
Transform3D _head_transform = head_transform;
_head_transform.origin *= world_scale;
transform_for_eye = p_cam_transform * (xr_server->get_reference_frame()) * _head_transform * transform_for_eye;
} else {
transform_for_eye = p_cam_transform;
};
return transform_for_eye;
};
Projection MobileVRInterface::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
_THREAD_SAFE_METHOD_
Projection eye;
aspect = p_aspect;
eye.set_for_hmd(p_view + 1, p_aspect, intraocular_dist, display_width, display_to_lens, oversample, p_z_near, p_z_far);
return eye;
};
Vector<BlitToScreen> MobileVRInterface::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) { … }
void MobileVRInterface::process() {
_THREAD_SAFE_METHOD_
if (initialized) {
set_position_from_sensors();
head_transform.origin = Vector3(0.0, eye_height, 0.0);
if (head.is_valid()) {
head->set_pose("default", head_transform, Vector3(), Vector3(), tracking_confidence);
}
};
};
RID MobileVRInterface::get_vrs_texture() { … }
MobileVRInterface::MobileVRInterface() { … }
MobileVRInterface::~MobileVRInterface() {
if (is_initialized()) {
uninitialize();
};
};