summaryrefslogtreecommitdiff
path: root/modules/mobile_vr
diff options
context:
space:
mode:
Diffstat (limited to 'modules/mobile_vr')
-rw-r--r--modules/mobile_vr/doc_classes/MobileVRInterface.xml5
-rw-r--r--modules/mobile_vr/mobile_vr_interface.cpp109
-rw-r--r--modules/mobile_vr/mobile_vr_interface.h55
-rw-r--r--modules/mobile_vr/register_types.cpp17
4 files changed, 121 insertions, 65 deletions
diff --git a/modules/mobile_vr/doc_classes/MobileVRInterface.xml b/modules/mobile_vr/doc_classes/MobileVRInterface.xml
index 120535bd41..df099f2c98 100644
--- a/modules/mobile_vr/doc_classes/MobileVRInterface.xml
+++ b/modules/mobile_vr/doc_classes/MobileVRInterface.xml
@@ -15,8 +15,6 @@
</description>
<tutorials>
</tutorials>
- <methods>
- </methods>
<members>
<member name="display_to_lens" type="float" setter="set_display_to_lens" getter="get_display_to_lens" default="4.0">
The distance between the display and the lenses inside of the device in centimeters.
@@ -39,7 +37,6 @@
<member name="oversample" type="float" setter="set_oversample" getter="get_oversample" default="1.5">
The oversample setting. Because of the lens distortion we have to render our buffers at a higher resolution then the screen can natively handle. A value between 1.5 and 2.0 often provides good results but at the cost of performance.
</member>
+ <member name="xr_play_area_mode" type="int" setter="set_play_area_mode" getter="get_play_area_mode" overrides="XRInterface" enum="XRInterface.PlayAreaMode" default="1" />
</members>
- <constants>
- </constants>
</class>
diff --git a/modules/mobile_vr/mobile_vr_interface.cpp b/modules/mobile_vr/mobile_vr_interface.cpp
index 12bb0e5b44..ba7353ace2 100644
--- a/modules/mobile_vr/mobile_vr_interface.cpp
+++ b/modules/mobile_vr/mobile_vr_interface.cpp
@@ -126,6 +126,8 @@ void MobileVRInterface::set_position_from_sensors() {
// 9dof is a misleading marketing term coming from 3 accelerometer axis + 3 gyro axis + 3 magnetometer axis = 9 axis
// but in reality this only offers 3 dof (yaw, pitch, roll) orientation
+ Basis orientation;
+
uint64_t ticks = OS::get_singleton()->get_ticks_usec();
uint64_t ticks_elapsed = ticks - last_ticks;
float delta_time = (double)ticks_elapsed / 1000000.0;
@@ -207,8 +209,8 @@ void MobileVRInterface::set_position_from_sensors() {
};
};
- // JIC
- orientation.orthonormalize();
+ // and copy to our head transform
+ head_transform.basis = orientation.orthonormalized();
last_ticks = ticks;
};
@@ -244,59 +246,59 @@ void MobileVRInterface::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "k2", PROPERTY_HINT_RANGE, "0.1,10.0,0.0001"), "set_k2", "get_k2");
}
-void MobileVRInterface::set_eye_height(const real_t p_eye_height) {
+void MobileVRInterface::set_eye_height(const double p_eye_height) {
eye_height = p_eye_height;
}
-real_t MobileVRInterface::get_eye_height() const {
+double MobileVRInterface::get_eye_height() const {
return eye_height;
}
-void MobileVRInterface::set_iod(const real_t p_iod) {
+void MobileVRInterface::set_iod(const double p_iod) {
intraocular_dist = p_iod;
};
-real_t MobileVRInterface::get_iod() const {
+double MobileVRInterface::get_iod() const {
return intraocular_dist;
};
-void MobileVRInterface::set_display_width(const real_t p_display_width) {
+void MobileVRInterface::set_display_width(const double p_display_width) {
display_width = p_display_width;
};
-real_t MobileVRInterface::get_display_width() const {
+double MobileVRInterface::get_display_width() const {
return display_width;
};
-void MobileVRInterface::set_display_to_lens(const real_t p_display_to_lens) {
+void MobileVRInterface::set_display_to_lens(const double p_display_to_lens) {
display_to_lens = p_display_to_lens;
};
-real_t MobileVRInterface::get_display_to_lens() const {
+double MobileVRInterface::get_display_to_lens() const {
return display_to_lens;
};
-void MobileVRInterface::set_oversample(const real_t p_oversample) {
+void MobileVRInterface::set_oversample(const double p_oversample) {
oversample = p_oversample;
};
-real_t MobileVRInterface::get_oversample() const {
+double MobileVRInterface::get_oversample() const {
return oversample;
};
-void MobileVRInterface::set_k1(const real_t p_k1) {
+void MobileVRInterface::set_k1(const double p_k1) {
k1 = p_k1;
};
-real_t MobileVRInterface::get_k1() const {
+double MobileVRInterface::get_k1() const {
return k1;
};
-void MobileVRInterface::set_k2(const real_t p_k2) {
+void MobileVRInterface::set_k2(const double p_k2) {
k2 = p_k2;
};
-real_t MobileVRInterface::get_k2() const {
+double MobileVRInterface::get_k2() const {
return k2;
};
@@ -318,7 +320,7 @@ bool MobileVRInterface::initialize() {
ERR_FAIL_NULL_V(xr_server, false);
if (!initialized) {
- // reset our sensor data and orientation
+ // reset our sensor data
mag_count = 0;
has_gyro = false;
sensor_first = true;
@@ -326,9 +328,15 @@ bool MobileVRInterface::initialize() {
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);
- // reset our orientation
- orientation = Basis();
+ // we must create a tracker for our head
+ 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);
// make this our primary interface
xr_server->set_primary_interface(this);
@@ -343,16 +351,38 @@ bool MobileVRInterface::initialize() {
void MobileVRInterface::uninitialize() {
if (initialized) {
+ // do any cleanup here...
XRServer *xr_server = XRServer::get_singleton();
- if (xr_server != nullptr && xr_server->get_primary_interface() == this) {
- // no longer our primary interface
- xr_server->set_primary_interface(nullptr);
+ if (xr_server != nullptr) {
+ if (head.is_valid()) {
+ xr_server->remove_tracker(head);
+
+ head.unref();
+ }
+
+ if (xr_server->get_primary_interface() == this) {
+ // no longer our primary interface
+ xr_server->set_primary_interface(nullptr);
+ }
}
initialized = false;
};
};
+bool MobileVRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
+ // This interface has no positional tracking so fix this to 3DOF
+ return p_mode == XR_PLAY_AREA_3DOF;
+}
+
+XRInterface::PlayAreaMode MobileVRInterface::get_play_area_mode() const {
+ return XR_PLAY_AREA_3DOF;
+}
+
+bool MobileVRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
+ return p_mode == XR_PLAY_AREA_3DOF;
+}
+
Size2 MobileVRInterface::get_render_target_size() {
_THREAD_SAFE_METHOD_
@@ -377,11 +407,10 @@ Transform3D MobileVRInterface::get_camera_transform() {
float world_scale = xr_server->get_world_scale();
// just scale our origin point of our transform
- Transform3D hmd_transform;
- hmd_transform.basis = orientation;
- hmd_transform.origin = Vector3(0.0, eye_height * world_scale, 0.0);
+ Transform3D _head_transform = head_transform;
+ _head_transform.origin *= world_scale;
- transform_for_eye = (xr_server->get_reference_frame()) * hmd_transform;
+ transform_for_eye = (xr_server->get_reference_frame()) * _head_transform;
}
return transform_for_eye;
@@ -409,11 +438,10 @@ Transform3D MobileVRInterface::get_transform_for_view(uint32_t p_view, const Tra
};
// just scale our origin point of our transform
- Transform3D hmd_transform;
- hmd_transform.basis = orientation;
- hmd_transform.origin = Vector3(0.0, eye_height * world_scale, 0.0);
+ Transform3D _head_transform = head_transform;
+ _head_transform.origin *= world_scale;
- transform_for_eye = p_cam_transform * (xr_server->get_reference_frame()) * hmd_transform * transform_for_eye;
+ transform_for_eye = p_cam_transform * (xr_server->get_reference_frame()) * _head_transform * transform_for_eye;
} else {
// huh? well just return what we got....
transform_for_eye = p_cam_transform;
@@ -422,7 +450,7 @@ Transform3D MobileVRInterface::get_transform_for_view(uint32_t p_view, const Tra
return transform_for_eye;
};
-CameraMatrix MobileVRInterface::get_projection_for_view(uint32_t p_view, real_t p_aspect, real_t p_z_near, real_t p_z_far) {
+CameraMatrix MobileVRInterface::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
_THREAD_SAFE_METHOD_
CameraMatrix eye;
@@ -455,16 +483,16 @@ Vector<BlitToScreen> MobileVRInterface::commit_views(RID p_render_target, const
blit.lens_distortion.aspect_ratio = aspect;
// left eye
- blit.rect = p_screen_rect;
- blit.rect.size.width *= 0.5;
+ blit.dst_rect = p_screen_rect;
+ blit.dst_rect.size.width *= 0.5;
blit.multi_view.layer = 0;
blit.lens_distortion.eye_center.x = ((-intraocular_dist / 2.0) + (display_width / 4.0)) / (display_width / 2.0);
blit_to_screen.push_back(blit);
// right eye
- blit.rect = p_screen_rect;
- blit.rect.size.width *= 0.5;
- blit.rect.position.x = blit.rect.size.width;
+ blit.dst_rect = p_screen_rect;
+ blit.dst_rect.size.width *= 0.5;
+ blit.dst_rect.position.x = blit.dst_rect.size.width;
blit.multi_view.layer = 1;
blit.lens_distortion.eye_center.x = ((intraocular_dist / 2.0) - (display_width / 4.0)) / (display_width / 2.0);
blit_to_screen.push_back(blit);
@@ -476,7 +504,16 @@ void MobileVRInterface::process() {
_THREAD_SAFE_METHOD_
if (initialized) {
+ // update our head transform orientation
set_position_from_sensors();
+
+ // update our head transform position (should be constant)
+ head_transform.origin = Vector3(0.0, eye_height, 0.0);
+
+ if (head.is_valid()) {
+ // Set our head position, note in real space, reference frame and world scale is applied later
+ head->set_pose("default", head_transform, Vector3(), Vector3());
+ }
};
};
diff --git a/modules/mobile_vr/mobile_vr_interface.h b/modules/mobile_vr/mobile_vr_interface.h
index 48b76ec187..b5bf966247 100644
--- a/modules/mobile_vr/mobile_vr_interface.h
+++ b/modules/mobile_vr/mobile_vr_interface.h
@@ -53,20 +53,23 @@ class MobileVRInterface : public XRInterface {
private:
bool initialized = false;
XRInterface::TrackingStatus tracking_state;
- Basis orientation;
// 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
- float eye_height = 1.85;
+ double eye_height = 1.85;
uint64_t last_ticks = 0;
- real_t intraocular_dist = 6.0;
- real_t display_width = 14.5;
- real_t display_to_lens = 4.0;
- real_t oversample = 1.5;
+ double intraocular_dist = 6.0;
+ double display_width = 14.5;
+ double display_to_lens = 4.0;
+ double oversample = 1.5;
- real_t k1 = 0.215;
- real_t k2 = 0.215;
- real_t aspect = 1.0;
+ double k1 = 0.215;
+ double k2 = 0.215;
+ double aspect = 1.0;
+
+ // at a minimum we need a tracker for our head
+ Ref<XRPositionalTracker> head;
+ Transform3D head_transform;
/*
logic for processing our sensor data, this was originally in our positional tracker logic but I think
@@ -110,26 +113,26 @@ protected:
static void _bind_methods();
public:
- void set_eye_height(const real_t p_eye_height);
- real_t get_eye_height() const;
+ void set_eye_height(const double p_eye_height);
+ double get_eye_height() const;
- void set_iod(const real_t p_iod);
- real_t get_iod() const;
+ void set_iod(const double p_iod);
+ double get_iod() const;
- void set_display_width(const real_t p_display_width);
- real_t get_display_width() const;
+ void set_display_width(const double p_display_width);
+ double get_display_width() const;
- void set_display_to_lens(const real_t p_display_to_lens);
- real_t get_display_to_lens() const;
+ void set_display_to_lens(const double p_display_to_lens);
+ double get_display_to_lens() const;
- void set_oversample(const real_t p_oversample);
- real_t get_oversample() const;
+ void set_oversample(const double p_oversample);
+ double get_oversample() const;
- void set_k1(const real_t p_k1);
- real_t get_k1() const;
+ void set_k1(const double p_k1);
+ double get_k1() const;
- void set_k2(const real_t p_k2);
- real_t get_k2() const;
+ void set_k2(const double p_k2);
+ double get_k2() const;
virtual StringName get_name() const override;
virtual uint32_t get_capabilities() const override;
@@ -140,11 +143,15 @@ public:
virtual bool initialize() override;
virtual void uninitialize() override;
+ virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
+ virtual XRInterface::PlayAreaMode get_play_area_mode() const override;
+ virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
+
virtual Size2 get_render_target_size() override;
virtual uint32_t get_view_count() override;
virtual Transform3D get_camera_transform() override;
virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override;
- virtual CameraMatrix get_projection_for_view(uint32_t p_view, real_t p_aspect, real_t p_z_near, real_t p_z_far) override;
+ virtual CameraMatrix get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override;
virtual Vector<BlitToScreen> commit_views(RID p_render_target, const Rect2 &p_screen_rect) override;
virtual void process() override;
diff --git a/modules/mobile_vr/register_types.cpp b/modules/mobile_vr/register_types.cpp
index 47d1fe482c..233c16531a 100644
--- a/modules/mobile_vr/register_types.cpp
+++ b/modules/mobile_vr/register_types.cpp
@@ -32,15 +32,30 @@
#include "mobile_vr_interface.h"
+Ref<MobileVRInterface> mobile_vr;
+
void register_mobile_vr_types() {
GDREGISTER_CLASS(MobileVRInterface);
if (XRServer::get_singleton()) {
- Ref<MobileVRInterface> mobile_vr;
mobile_vr.instantiate();
XRServer::get_singleton()->add_interface(mobile_vr);
}
}
void unregister_mobile_vr_types() {
+ if (mobile_vr.is_valid()) {
+ // uninitialise our interface if it is initialised
+ if (mobile_vr->is_initialized()) {
+ mobile_vr->uninitialize();
+ }
+
+ // unregister our interface from the XR server
+ if (XRServer::get_singleton()) {
+ XRServer::get_singleton()->remove_interface(mobile_vr);
+ }
+
+ // and release
+ mobile_vr.unref();
+ }
}