xr_interface.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* xr_interface.cpp */
  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. #include "xr_interface.h"
  31. void XRInterface::_bind_methods() {
  32. ADD_SIGNAL(MethodInfo("play_area_changed", PropertyInfo(Variant::INT, "mode")));
  33. ClassDB::bind_method(D_METHOD("get_name"), &XRInterface::get_name);
  34. ClassDB::bind_method(D_METHOD("get_capabilities"), &XRInterface::get_capabilities);
  35. ClassDB::bind_method(D_METHOD("is_primary"), &XRInterface::is_primary);
  36. ClassDB::bind_method(D_METHOD("set_primary", "primary"), &XRInterface::set_primary);
  37. ClassDB::bind_method(D_METHOD("is_initialized"), &XRInterface::is_initialized);
  38. ClassDB::bind_method(D_METHOD("initialize"), &XRInterface::initialize);
  39. ClassDB::bind_method(D_METHOD("uninitialize"), &XRInterface::uninitialize);
  40. ClassDB::bind_method(D_METHOD("get_system_info"), &XRInterface::get_system_info);
  41. ClassDB::bind_method(D_METHOD("get_tracking_status"), &XRInterface::get_tracking_status);
  42. ClassDB::bind_method(D_METHOD("get_render_target_size"), &XRInterface::get_render_target_size);
  43. ClassDB::bind_method(D_METHOD("get_view_count"), &XRInterface::get_view_count);
  44. ClassDB::bind_method(D_METHOD("trigger_haptic_pulse", "action_name", "tracker_name", "frequency", "amplitude", "duration_sec", "delay_sec"), &XRInterface::trigger_haptic_pulse);
  45. ADD_GROUP("Interface", "interface_");
  46. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interface_is_primary"), "set_primary", "is_primary");
  47. // methods and properties specific to VR...
  48. ClassDB::bind_method(D_METHOD("supports_play_area_mode", "mode"), &XRInterface::supports_play_area_mode);
  49. ClassDB::bind_method(D_METHOD("get_play_area_mode"), &XRInterface::get_play_area_mode);
  50. ClassDB::bind_method(D_METHOD("set_play_area_mode", "mode"), &XRInterface::set_play_area_mode);
  51. ClassDB::bind_method(D_METHOD("get_play_area"), &XRInterface::get_play_area);
  52. ADD_GROUP("XR", "xr_");
  53. ADD_PROPERTY(PropertyInfo(Variant::INT, "xr_play_area_mode", PROPERTY_HINT_ENUM, "Unknown,3DOF,Sitting,Roomscale,Stage"), "set_play_area_mode", "get_play_area_mode");
  54. // methods and properties specific to AR....
  55. ClassDB::bind_method(D_METHOD("get_anchor_detection_is_enabled"), &XRInterface::get_anchor_detection_is_enabled);
  56. ClassDB::bind_method(D_METHOD("set_anchor_detection_is_enabled", "enable"), &XRInterface::set_anchor_detection_is_enabled);
  57. ClassDB::bind_method(D_METHOD("get_camera_feed_id"), &XRInterface::get_camera_feed_id);
  58. ClassDB::bind_method(D_METHOD("is_passthrough_supported"), &XRInterface::is_passthrough_supported);
  59. ClassDB::bind_method(D_METHOD("is_passthrough_enabled"), &XRInterface::is_passthrough_enabled);
  60. ClassDB::bind_method(D_METHOD("start_passthrough"), &XRInterface::start_passthrough);
  61. ClassDB::bind_method(D_METHOD("stop_passthrough"), &XRInterface::stop_passthrough);
  62. ClassDB::bind_method(D_METHOD("get_transform_for_view", "view", "cam_transform"), &XRInterface::get_transform_for_view);
  63. ClassDB::bind_method(D_METHOD("get_projection_for_view", "view", "aspect", "near", "far"), &XRInterface::get_projection_for_view);
  64. /** environment blend mode. */
  65. ClassDB::bind_method(D_METHOD("get_supported_environment_blend_modes"), &XRInterface::get_supported_environment_blend_modes);
  66. ClassDB::bind_method(D_METHOD("set_environment_blend_mode", "mode"), &XRInterface::set_environment_blend_mode);
  67. ClassDB::bind_method(D_METHOD("get_environment_blend_mode"), &XRInterface::get_environment_blend_mode);
  68. ADD_PROPERTY(PropertyInfo(Variant::INT, "environment_blend_mode"), "set_environment_blend_mode", "get_environment_blend_mode");
  69. ADD_GROUP("AR", "ar_");
  70. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ar_is_anchor_detection_enabled"), "set_anchor_detection_is_enabled", "get_anchor_detection_is_enabled");
  71. BIND_ENUM_CONSTANT(XR_NONE);
  72. BIND_ENUM_CONSTANT(XR_MONO);
  73. BIND_ENUM_CONSTANT(XR_STEREO);
  74. BIND_ENUM_CONSTANT(XR_QUAD);
  75. BIND_ENUM_CONSTANT(XR_VR);
  76. BIND_ENUM_CONSTANT(XR_AR);
  77. BIND_ENUM_CONSTANT(XR_EXTERNAL);
  78. BIND_ENUM_CONSTANT(XR_NORMAL_TRACKING);
  79. BIND_ENUM_CONSTANT(XR_EXCESSIVE_MOTION);
  80. BIND_ENUM_CONSTANT(XR_INSUFFICIENT_FEATURES);
  81. BIND_ENUM_CONSTANT(XR_UNKNOWN_TRACKING);
  82. BIND_ENUM_CONSTANT(XR_NOT_TRACKING);
  83. BIND_ENUM_CONSTANT(XR_PLAY_AREA_UNKNOWN);
  84. BIND_ENUM_CONSTANT(XR_PLAY_AREA_3DOF);
  85. BIND_ENUM_CONSTANT(XR_PLAY_AREA_SITTING);
  86. BIND_ENUM_CONSTANT(XR_PLAY_AREA_ROOMSCALE);
  87. BIND_ENUM_CONSTANT(XR_PLAY_AREA_STAGE);
  88. BIND_ENUM_CONSTANT(XR_PLAY_AREA_CUSTOM);
  89. BIND_ENUM_CONSTANT(XR_ENV_BLEND_MODE_OPAQUE);
  90. BIND_ENUM_CONSTANT(XR_ENV_BLEND_MODE_ADDITIVE);
  91. BIND_ENUM_CONSTANT(XR_ENV_BLEND_MODE_ALPHA_BLEND);
  92. BIND_ENUM_CONSTANT(XR_VRS_TEXTURE_FORMAT_UNIFIED);
  93. BIND_ENUM_CONSTANT(XR_VRS_TEXTURE_FORMAT_FRAGMENT_SHADING_RATE);
  94. BIND_ENUM_CONSTANT(XR_VRS_TEXTURE_FORMAT_FRAGMENT_DENSITY_MAP);
  95. }
  96. bool XRInterface::is_primary() {
  97. XRServer *xr_server = XRServer::get_singleton();
  98. ERR_FAIL_NULL_V(xr_server, false);
  99. return xr_server->get_primary_interface() == this;
  100. }
  101. void XRInterface::set_primary(bool p_primary) {
  102. XRServer *xr_server = XRServer::get_singleton();
  103. ERR_FAIL_NULL(xr_server);
  104. if (p_primary) {
  105. ERR_FAIL_COND(!is_initialized());
  106. xr_server->set_primary_interface(this);
  107. } else if (xr_server->get_primary_interface() == this) {
  108. xr_server->set_primary_interface(nullptr);
  109. }
  110. }
  111. XRInterface::XRInterface() {}
  112. XRInterface::~XRInterface() {}
  113. // query if this interface supports this play area mode
  114. bool XRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  115. return p_mode == XR_PLAY_AREA_UNKNOWN;
  116. }
  117. // get the current play area mode
  118. XRInterface::PlayAreaMode XRInterface::get_play_area_mode() const {
  119. return XR_PLAY_AREA_UNKNOWN;
  120. }
  121. // change the play area mode, note that this should return false if the mode is not available
  122. bool XRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  123. return p_mode == XR_PLAY_AREA_UNKNOWN;
  124. }
  125. // if available, returns an array of vectors denoting the play area the player can move around in
  126. PackedVector3Array XRInterface::get_play_area() const {
  127. // Return an empty array by default.
  128. // Note implementation is responsible for applying our reference frame and world scale to the raw data.
  129. // `play_area_changed` should be emitted if play area data is available and either the reference frame or world scale changes.
  130. return PackedVector3Array();
  131. }
  132. /** these will only be implemented on AR interfaces, so we want dummies for VR **/
  133. bool XRInterface::get_anchor_detection_is_enabled() const {
  134. return false;
  135. }
  136. void XRInterface::set_anchor_detection_is_enabled(bool p_enable) {
  137. }
  138. int XRInterface::get_camera_feed_id() {
  139. return 0;
  140. }
  141. RID XRInterface::get_vrs_texture() {
  142. return RID();
  143. }
  144. /** these are optional, so we want dummies **/
  145. RID XRInterface::get_color_texture() {
  146. return RID();
  147. }
  148. RID XRInterface::get_depth_texture() {
  149. return RID();
  150. }
  151. RID XRInterface::get_velocity_texture() {
  152. return RID();
  153. }
  154. RID XRInterface::get_velocity_depth_texture() {
  155. return RID();
  156. }
  157. Size2i XRInterface::get_velocity_target_size() {
  158. return Size2i();
  159. }
  160. Rect2i XRInterface::get_render_region() {
  161. return Rect2i();
  162. }
  163. PackedStringArray XRInterface::get_suggested_tracker_names() const {
  164. PackedStringArray arr;
  165. return arr;
  166. }
  167. PackedStringArray XRInterface::get_suggested_pose_names(const StringName &p_tracker_name) const {
  168. PackedStringArray arr;
  169. return arr;
  170. }
  171. XRInterface::TrackingStatus XRInterface::get_tracking_status() const {
  172. return XR_UNKNOWN_TRACKING;
  173. }
  174. void XRInterface::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) {
  175. }
  176. Array XRInterface::get_supported_environment_blend_modes() {
  177. Array default_blend_modes = { XR_ENV_BLEND_MODE_OPAQUE };
  178. return default_blend_modes;
  179. }