Path: blob/master/modules/mobile_vr/mobile_vr_interface.h
10277 views
/**************************************************************************/1/* mobile_vr_interface.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "servers/xr/xr_interface.h"33#include "servers/xr/xr_positional_tracker.h"34#include "servers/xr/xr_vrs.h"3536/**37The mobile interface is a native VR interface that can be used on Android and iOS phones.38It contains a basic implementation supporting 3DOF tracking if a gyroscope and accelerometer are39present and sets up the proper projection matrices based on the values provided.4041We're planning to eventually do separate interfaces towards mobile SDKs that have far more capabilities and42do not rely on the user providing most of these settings (though enhancing this with auto detection features43based on the device we're running on would be cool). I'm mostly adding this as an example or base plate for44more advanced interfaces.45*/4647class MobileVRInterface : public XRInterface {48GDCLASS(MobileVRInterface, XRInterface);4950private:51bool initialized = false;52XRInterface::TrackingStatus tracking_state;53XRPose::TrackingConfidence tracking_confidence = XRPose::XR_TRACKING_CONFIDENCE_NONE;5455// 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 codes56double eye_height = 1.85;57uint64_t last_ticks = 0;5859double intraocular_dist = 6.0;60double display_width = 14.5;61double display_to_lens = 4.0;62double oversample = 1.5;6364Rect2 offset_rect = Rect2(0, 0, 1, 1); // Full screen rect.6566double k1 = 0.215;67double k2 = 0.215;68double aspect = 1.0;6970// at a minimum we need a tracker for our head71Ref<XRPositionalTracker> head;72Transform3D head_transform;7374XRVRS xr_vrs;7576/*77logic for processing our sensor data, this was originally in our positional tracker logic but I think78that doesn't make sense in hindsight. It only makes marginally more sense to park it here for now,79this probably deserves an object of its own80*/81Vector3 scale_magneto(const Vector3 &p_magnetometer);82Basis combine_acc_mag(const Vector3 &p_grav, const Vector3 &p_magneto);8384int mag_count = 0;85bool has_gyro = false;86bool sensor_first = false;87Vector3 last_accerometer_data;88Vector3 last_magnetometer_data;89Vector3 mag_current_min;90Vector3 mag_current_max;91Vector3 mag_next_min;92Vector3 mag_next_max;9394///@TODO a few support functions for trackers, most are math related and should likely be moved elsewhere95float floor_decimals(const float p_value, const float p_decimals) {96float power_of_10 = std::pow(10.0f, p_decimals);97return std::floor(p_value * power_of_10) / power_of_10;98}99100Vector3 floor_decimals(const Vector3 &p_vector, const float p_decimals) {101return Vector3(floor_decimals(p_vector.x, p_decimals), floor_decimals(p_vector.y, p_decimals), floor_decimals(p_vector.z, p_decimals));102}103104Vector3 low_pass(const Vector3 &p_vector, const Vector3 &p_last_vector, const float p_factor) {105return p_vector + (p_factor * (p_last_vector - p_vector));106}107108Vector3 scrub(const Vector3 &p_vector, const Vector3 &p_last_vector, const float p_decimals, const float p_factor) {109return low_pass(floor_decimals(p_vector, p_decimals), p_last_vector, p_factor);110}111112void set_position_from_sensors();113114protected:115static void _bind_methods();116117public:118void set_eye_height(const double p_eye_height);119double get_eye_height() const;120121void set_iod(const double p_iod);122double get_iod() const;123124void set_display_width(const double p_display_width);125double get_display_width() const;126127void set_offset_rect(const Rect2 &p_offset_rect);128Rect2 get_offset_rect() const;129130void set_display_to_lens(const double p_display_to_lens);131double get_display_to_lens() const;132133void set_oversample(const double p_oversample);134double get_oversample() const;135136void set_k1(const double p_k1);137double get_k1() const;138139void set_k2(const double p_k2);140double get_k2() const;141142float get_vrs_min_radius() const;143void set_vrs_min_radius(float p_vrs_min_radius);144145float get_vrs_strength() const;146void set_vrs_strength(float p_vrs_strength);147148virtual StringName get_name() const override;149virtual uint32_t get_capabilities() const override;150151virtual TrackingStatus get_tracking_status() const override;152153virtual bool is_initialized() const override;154virtual bool initialize() override;155virtual void uninitialize() override;156virtual Dictionary get_system_info() override;157158virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode) override;159virtual XRInterface::PlayAreaMode get_play_area_mode() const override;160virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode) override;161162virtual Size2 get_render_target_size() override;163virtual uint32_t get_view_count() override;164virtual Transform3D get_camera_transform() override;165virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override;166virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override;167virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) override;168169virtual void process() override;170171virtual RID get_vrs_texture() override;172173MobileVRInterface();174~MobileVRInterface();175};176177178