mobile_vr_interface.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**************************************************************************/
  2. /* mobile_vr_interface.h */
  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. #ifndef MOBILE_VR_INTERFACE_H
  31. #define MOBILE_VR_INTERFACE_H
  32. #include "servers/xr/xr_interface.h"
  33. #include "servers/xr/xr_positional_tracker.h"
  34. #include "servers/xr/xr_vrs.h"
  35. /**
  36. The mobile interface is a native VR interface that can be used on Android and iOS phones.
  37. It contains a basic implementation supporting 3DOF tracking if a gyroscope and accelerometer are
  38. present and sets up the proper projection matrices based on the values provided.
  39. We're planning to eventually do separate interfaces towards mobile SDKs that have far more capabilities and
  40. do not rely on the user providing most of these settings (though enhancing this with auto detection features
  41. based on the device we're running on would be cool). I'm mostly adding this as an example or base plate for
  42. more advanced interfaces.
  43. */
  44. class MobileVRInterface : public XRInterface {
  45. GDCLASS(MobileVRInterface, XRInterface);
  46. private:
  47. bool initialized = false;
  48. XRInterface::TrackingStatus tracking_state;
  49. XRPose::TrackingConfidence tracking_confidence = XRPose::XR_TRACKING_CONFIDENCE_NONE;
  50. // 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
  51. double eye_height = 1.85;
  52. uint64_t last_ticks = 0;
  53. double intraocular_dist = 6.0;
  54. double display_width = 14.5;
  55. double display_to_lens = 4.0;
  56. double oversample = 1.5;
  57. Rect2 offset_rect = Rect2(0, 0, 1, 1); // Full screen rect.
  58. double k1 = 0.215;
  59. double k2 = 0.215;
  60. double aspect = 1.0;
  61. // at a minimum we need a tracker for our head
  62. Ref<XRPositionalTracker> head;
  63. Transform3D head_transform;
  64. XRVRS xr_vrs;
  65. /*
  66. logic for processing our sensor data, this was originally in our positional tracker logic but I think
  67. that doesn't make sense in hindsight. It only makes marginally more sense to park it here for now,
  68. this probably deserves an object of its own
  69. */
  70. Vector3 scale_magneto(const Vector3 &p_magnetometer);
  71. Basis combine_acc_mag(const Vector3 &p_grav, const Vector3 &p_magneto);
  72. int mag_count = 0;
  73. bool has_gyro = false;
  74. bool sensor_first = false;
  75. Vector3 last_accerometer_data;
  76. Vector3 last_magnetometer_data;
  77. Vector3 mag_current_min;
  78. Vector3 mag_current_max;
  79. Vector3 mag_next_min;
  80. Vector3 mag_next_max;
  81. ///@TODO a few support functions for trackers, most are math related and should likely be moved elsewhere
  82. float floor_decimals(const float p_value, const float p_decimals) {
  83. float power_of_10 = pow(10.0f, p_decimals);
  84. return floor(p_value * power_of_10) / power_of_10;
  85. }
  86. Vector3 floor_decimals(const Vector3 &p_vector, const float p_decimals) {
  87. return Vector3(floor_decimals(p_vector.x, p_decimals), floor_decimals(p_vector.y, p_decimals), floor_decimals(p_vector.z, p_decimals));
  88. }
  89. Vector3 low_pass(const Vector3 &p_vector, const Vector3 &p_last_vector, const float p_factor) {
  90. return p_vector + (p_factor * (p_last_vector - p_vector));
  91. }
  92. Vector3 scrub(const Vector3 &p_vector, const Vector3 &p_last_vector, const float p_decimals, const float p_factor) {
  93. return low_pass(floor_decimals(p_vector, p_decimals), p_last_vector, p_factor);
  94. }
  95. void set_position_from_sensors();
  96. protected:
  97. static void _bind_methods();
  98. public:
  99. void set_eye_height(const double p_eye_height);
  100. double get_eye_height() const;
  101. void set_iod(const double p_iod);
  102. double get_iod() const;
  103. void set_display_width(const double p_display_width);
  104. double get_display_width() const;
  105. void set_offset_rect(const Rect2 &p_offset_rect);
  106. Rect2 get_offset_rect() const;
  107. void set_display_to_lens(const double p_display_to_lens);
  108. double get_display_to_lens() const;
  109. void set_oversample(const double p_oversample);
  110. double get_oversample() const;
  111. void set_k1(const double p_k1);
  112. double get_k1() const;
  113. void set_k2(const double p_k2);
  114. double get_k2() const;
  115. float get_vrs_min_radius() const;
  116. void set_vrs_min_radius(float p_vrs_min_radius);
  117. float get_vrs_strength() const;
  118. void set_vrs_strength(float p_vrs_strength);
  119. virtual StringName get_name() const override;
  120. virtual uint32_t get_capabilities() const override;
  121. virtual TrackingStatus get_tracking_status() const override;
  122. virtual bool is_initialized() const override;
  123. virtual bool initialize() override;
  124. virtual void uninitialize() override;
  125. virtual Dictionary get_system_info() override;
  126. virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
  127. virtual XRInterface::PlayAreaMode get_play_area_mode() const override;
  128. virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
  129. virtual Size2 get_render_target_size() override;
  130. virtual uint32_t get_view_count() override;
  131. virtual Transform3D get_camera_transform() override;
  132. virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override;
  133. virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override;
  134. virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) override;
  135. virtual void process() override;
  136. virtual RID get_vrs_texture() override;
  137. MobileVRInterface();
  138. ~MobileVRInterface();
  139. };
  140. #endif // MOBILE_VR_INTERFACE_H