xr_interface.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**************************************************************************/
  2. /* xr_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 XR_INTERFACE_H
  31. #define XR_INTERFACE_H
  32. #include "core/math/projection.h"
  33. #include "core/os/thread_safe.h"
  34. #include "servers/xr_server.h"
  35. // forward declaration
  36. struct BlitToScreen;
  37. /**
  38. The XR interface is a template class on top of which we build interface to different AR, VR and tracking SDKs.
  39. The idea is that we subclass this class, implement the logic, and then instantiate a singleton of each interface
  40. when Godot starts. These instances do not initialize themselves but register themselves with the AR/VR server.
  41. If the user wants to enable AR/VR the choose the interface they want to use and initialize it.
  42. Note that we may make this into a fully instantiable class for GDExtension support.
  43. */
  44. class XRInterface : public RefCounted {
  45. GDCLASS(XRInterface, RefCounted);
  46. public:
  47. enum Capabilities { /* purely meta data, provides some info about what this interface supports */
  48. XR_NONE = 0, /* no capabilities */
  49. XR_MONO = 1, /* can be used with mono output */
  50. XR_STEREO = 2, /* can be used with stereo output */
  51. XR_QUAD = 4, /* can be used with quad output (not currently supported) */
  52. XR_VR = 8, /* offers VR support */
  53. XR_AR = 16, /* offers AR support */
  54. XR_EXTERNAL = 32 /* renders to external device */
  55. };
  56. enum TrackingStatus { /* tracking status currently based on AR but we can start doing more with this for VR as well */
  57. XR_NORMAL_TRACKING,
  58. XR_EXCESSIVE_MOTION,
  59. XR_INSUFFICIENT_FEATURES,
  60. XR_UNKNOWN_TRACKING,
  61. XR_NOT_TRACKING
  62. };
  63. enum PlayAreaMode { /* defines the mode used by the XR interface for tracking */
  64. XR_PLAY_AREA_UNKNOWN, /* Area mode not set or not available */
  65. XR_PLAY_AREA_3DOF, /* Only support orientation tracking, no positional tracking, area will center around player */
  66. XR_PLAY_AREA_SITTING, /* Player is in seated position, limited positional tracking, fixed guardian around player */
  67. XR_PLAY_AREA_ROOMSCALE, /* Player is free to move around, full positional tracking */
  68. XR_PLAY_AREA_STAGE, /* Same as roomscale but origin point is fixed to the center of the physical space, XRServer.center_on_hmd disabled */
  69. };
  70. enum EnvironmentBlendMode {
  71. XR_ENV_BLEND_MODE_OPAQUE, /* You cannot see the real world, VR like */
  72. XR_ENV_BLEND_MODE_ADDITIVE, /* You can see the real world, AR like */
  73. XR_ENV_BLEND_MODE_ALPHA_BLEND, /* Real world is passed through where alpha channel is 0.0 and gradually blends to opaque for value 1.0. */
  74. };
  75. protected:
  76. _THREAD_SAFE_CLASS_
  77. static void _bind_methods();
  78. public:
  79. /** general interface information **/
  80. virtual StringName get_name() const = 0;
  81. virtual uint32_t get_capabilities() const = 0;
  82. bool is_primary();
  83. void set_primary(bool p_is_primary);
  84. virtual bool is_initialized() const = 0; /* returns true if we've initialized this interface */
  85. virtual bool initialize() = 0; /* initialize this interface, if this has an HMD it becomes the primary interface */
  86. virtual void uninitialize() = 0; /* deinitialize this interface */
  87. /** input and output **/
  88. virtual PackedStringArray get_suggested_tracker_names() const; /* return a list of likely/suggested tracker names */
  89. virtual PackedStringArray get_suggested_pose_names(const StringName &p_tracker_name) const; /* return a list of likely/suggested action names for this tracker */
  90. virtual TrackingStatus get_tracking_status() const; /* get the status of our current tracking */
  91. virtual void trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec = 0); /* trigger a haptic pulse */
  92. /** specific to VR **/
  93. virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode); /* query if this interface supports this play area mode */
  94. virtual XRInterface::PlayAreaMode get_play_area_mode() const; /* get the current play area mode */
  95. virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode); /* change the play area mode, note that this should return false if the mode is not available */
  96. virtual PackedVector3Array get_play_area() const; /* if available, returns an array of vectors denoting the play area the player can move around in */
  97. /** specific to AR **/
  98. virtual bool get_anchor_detection_is_enabled() const;
  99. virtual void set_anchor_detection_is_enabled(bool p_enable);
  100. virtual int get_camera_feed_id();
  101. /** rendering and internal **/
  102. virtual Size2 get_render_target_size() = 0; /* returns the recommended render target size per eye for this device */
  103. virtual uint32_t get_view_count() = 0; /* returns the view count we need (1 is monoscopic, 2 is stereoscopic but can be more) */
  104. virtual Transform3D get_camera_transform() = 0; /* returns the position of our camera for updating our camera node. For monoscopic this is equal to the views transform, for stereoscopic this should be an average */
  105. virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) = 0; /* get each views transform */
  106. virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) = 0; /* get each view projection matrix */
  107. virtual RID get_vrs_texture(); /* obtain VRS texture */
  108. virtual RID get_color_texture(); /* obtain color output texture (if applicable) */
  109. virtual RID get_depth_texture(); /* obtain depth output texture (if applicable, used for reprojection) */
  110. virtual RID get_velocity_texture(); /* obtain velocity output texture (if applicable, used for spacewarp) */
  111. virtual void process() = 0;
  112. virtual void pre_render(){};
  113. virtual bool pre_draw_viewport(RID p_render_target) { return true; }; /* inform XR interface we are about to start our viewport draw process */
  114. virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) = 0; /* inform XR interface we finished our viewport draw process */
  115. virtual void end_frame(){};
  116. /** passthrough **/
  117. virtual bool is_passthrough_supported() { return false; }
  118. virtual bool is_passthrough_enabled() { return false; }
  119. virtual bool start_passthrough() { return false; }
  120. virtual void stop_passthrough() {}
  121. /** environment blend mode. */
  122. virtual Array get_supported_environment_blend_modes();
  123. virtual bool set_environment_blend_mode(EnvironmentBlendMode mode) { return false; }
  124. XRInterface();
  125. ~XRInterface();
  126. private:
  127. struct VRSData {
  128. RID vrs_texture;
  129. Size2i size;
  130. } vrs;
  131. };
  132. VARIANT_ENUM_CAST(XRInterface::Capabilities);
  133. VARIANT_ENUM_CAST(XRInterface::TrackingStatus);
  134. VARIANT_ENUM_CAST(XRInterface::PlayAreaMode);
  135. VARIANT_ENUM_CAST(XRInterface::EnvironmentBlendMode);
  136. #endif // XR_INTERFACE_H