xr_interface.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*************************************************************************/
  2. /* xr_interface.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "servers/rendering/renderer_compositor.h"
  32. void XRInterface::_bind_methods() {
  33. ADD_SIGNAL(MethodInfo("play_area_changed", PropertyInfo(Variant::INT, "mode")));
  34. ClassDB::bind_method(D_METHOD("get_name"), &XRInterface::get_name);
  35. ClassDB::bind_method(D_METHOD("get_capabilities"), &XRInterface::get_capabilities);
  36. ClassDB::bind_method(D_METHOD("is_primary"), &XRInterface::is_primary);
  37. ClassDB::bind_method(D_METHOD("set_primary", "primary"), &XRInterface::set_primary);
  38. ClassDB::bind_method(D_METHOD("is_initialized"), &XRInterface::is_initialized);
  39. ClassDB::bind_method(D_METHOD("initialize"), &XRInterface::initialize);
  40. ClassDB::bind_method(D_METHOD("uninitialize"), &XRInterface::uninitialize);
  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. ADD_GROUP("AR", "ar_");
  63. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ar_is_anchor_detection_enabled"), "set_anchor_detection_is_enabled", "get_anchor_detection_is_enabled");
  64. BIND_ENUM_CONSTANT(XR_NONE);
  65. BIND_ENUM_CONSTANT(XR_MONO);
  66. BIND_ENUM_CONSTANT(XR_STEREO);
  67. BIND_ENUM_CONSTANT(XR_QUAD);
  68. BIND_ENUM_CONSTANT(XR_VR);
  69. BIND_ENUM_CONSTANT(XR_AR);
  70. BIND_ENUM_CONSTANT(XR_EXTERNAL);
  71. BIND_ENUM_CONSTANT(XR_NORMAL_TRACKING);
  72. BIND_ENUM_CONSTANT(XR_EXCESSIVE_MOTION);
  73. BIND_ENUM_CONSTANT(XR_INSUFFICIENT_FEATURES);
  74. BIND_ENUM_CONSTANT(XR_UNKNOWN_TRACKING);
  75. BIND_ENUM_CONSTANT(XR_NOT_TRACKING);
  76. BIND_ENUM_CONSTANT(XR_PLAY_AREA_UNKNOWN);
  77. BIND_ENUM_CONSTANT(XR_PLAY_AREA_3DOF);
  78. BIND_ENUM_CONSTANT(XR_PLAY_AREA_SITTING);
  79. BIND_ENUM_CONSTANT(XR_PLAY_AREA_ROOMSCALE);
  80. BIND_ENUM_CONSTANT(XR_PLAY_AREA_STAGE);
  81. };
  82. bool XRInterface::is_primary() {
  83. XRServer *xr_server = XRServer::get_singleton();
  84. ERR_FAIL_NULL_V(xr_server, false);
  85. return xr_server->get_primary_interface() == this;
  86. }
  87. void XRInterface::set_primary(bool p_primary) {
  88. XRServer *xr_server = XRServer::get_singleton();
  89. ERR_FAIL_NULL(xr_server);
  90. if (p_primary) {
  91. ERR_FAIL_COND(!is_initialized());
  92. xr_server->set_primary_interface(this);
  93. } else if (xr_server->get_primary_interface() == this) {
  94. xr_server->set_primary_interface(nullptr);
  95. }
  96. }
  97. XRInterface::XRInterface() {}
  98. XRInterface::~XRInterface() {
  99. if (vrs.vrs_texture.is_valid()) {
  100. RS::get_singleton()->free(vrs.vrs_texture);
  101. vrs.vrs_texture = RID();
  102. }
  103. }
  104. // query if this interface supports this play area mode
  105. bool XRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  106. return p_mode == XR_PLAY_AREA_UNKNOWN;
  107. }
  108. // get the current play area mode
  109. XRInterface::PlayAreaMode XRInterface::get_play_area_mode() const {
  110. return XR_PLAY_AREA_UNKNOWN;
  111. }
  112. // change the play area mode, note that this should return false if the mode is not available
  113. bool XRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  114. return p_mode == XR_PLAY_AREA_UNKNOWN;
  115. }
  116. // if available, returns an array of vectors denoting the play area the player can move around in
  117. PackedVector3Array XRInterface::get_play_area() const {
  118. // Return an empty array by default.
  119. // Note implementation is responsible for applying our reference frame and world scale to the raw data.
  120. // `play_area_changed` should be emitted if play area data is available and either the reference frame or world scale changes.
  121. return PackedVector3Array();
  122. };
  123. /** these will only be implemented on AR interfaces, so we want dummies for VR **/
  124. bool XRInterface::get_anchor_detection_is_enabled() const {
  125. return false;
  126. }
  127. void XRInterface::set_anchor_detection_is_enabled(bool p_enable) {
  128. }
  129. int XRInterface::get_camera_feed_id() {
  130. return 0;
  131. }
  132. RID XRInterface::get_vrs_texture() {
  133. // Default logic will return a standard VRS image based on our target size and default projections.
  134. // Note that this only gets called if VRS is supported on the hardware.
  135. Size2 texel_size = Size2(16.0, 16.0); // For now we assume we always use 16x16 texels, seems to be the standard.
  136. int view_count = get_view_count();
  137. Size2 target_size = get_render_target_size();
  138. real_t aspect = target_size.x / target_size.y; // is this y/x ?
  139. Size2 vrs_size = Size2(round(0.5 + target_size.x / texel_size.x), round(0.5 + target_size.y / texel_size.y));
  140. real_t radius = vrs_size.length() * 0.5;
  141. Size2 vrs_sizei = vrs_size;
  142. if (vrs.size != vrs_sizei) {
  143. const uint8_t densities[] = {
  144. 0, // 1x1
  145. 1, // 1x2
  146. // 4, // 2x1
  147. 5, // 2x2
  148. 6, // 2x4
  149. // 9, // 4x2
  150. 10, // 4x4
  151. };
  152. // out with the old
  153. if (vrs.vrs_texture.is_valid()) {
  154. RS::get_singleton()->free(vrs.vrs_texture);
  155. vrs.vrs_texture = RID();
  156. }
  157. // in with the new
  158. Vector<Ref<Image>> images;
  159. vrs.size = vrs_sizei;
  160. for (int i = 0; i < view_count && i < 2; i++) {
  161. PackedByteArray data;
  162. data.resize(vrs_sizei.x * vrs_sizei.y);
  163. uint8_t *data_ptr = data.ptrw();
  164. // Our near and far don't matter much for what we're doing here, but there are some interfaces that will remember this as the near and far and may fail as a result...
  165. Projection cm = get_projection_for_view(i, aspect, 0.1, 1000.0);
  166. Vector3 center = cm.xform(Vector3(0.0, 0.0, 999.0));
  167. Vector2i view_center;
  168. view_center.x = int(vrs_size.x * (center.x + 1.0) * 0.5);
  169. view_center.y = int(vrs_size.y * (center.y + 1.0) * 0.5);
  170. int d = 0;
  171. for (int y = 0; y < vrs_sizei.y; y++) {
  172. for (int x = 0; x < vrs_sizei.x; x++) {
  173. Vector2 offset = Vector2(x - view_center.x, y - view_center.y);
  174. offset.y *= aspect;
  175. real_t distance = offset.length();
  176. int idx = round(5.0 * distance / radius);
  177. if (idx > 4) {
  178. idx = 4;
  179. }
  180. uint8_t density = densities[idx];
  181. data_ptr[d++] = density;
  182. }
  183. }
  184. Ref<Image> image;
  185. image.instantiate();
  186. image->create_from_data(vrs_sizei.x, vrs_sizei.y, false, Image::FORMAT_R8, data);
  187. images.push_back(image);
  188. }
  189. if (images.size() == 1) {
  190. vrs.vrs_texture = RS::get_singleton()->texture_2d_create(images[0]);
  191. } else {
  192. vrs.vrs_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TEXTURE_LAYERED_2D_ARRAY);
  193. }
  194. }
  195. return vrs.vrs_texture;
  196. }
  197. /** these are optional, so we want dummies **/
  198. RID XRInterface::get_color_texture() {
  199. return RID();
  200. }
  201. RID XRInterface::get_depth_texture() {
  202. return RID();
  203. }
  204. RID XRInterface::get_velocity_texture() {
  205. return RID();
  206. }
  207. PackedStringArray XRInterface::get_suggested_tracker_names() const {
  208. PackedStringArray arr;
  209. return arr;
  210. }
  211. PackedStringArray XRInterface::get_suggested_pose_names(const StringName &p_tracker_name) const {
  212. PackedStringArray arr;
  213. return arr;
  214. }
  215. XRInterface::TrackingStatus XRInterface::get_tracking_status() const {
  216. return XR_UNKNOWN_TRACKING;
  217. }
  218. 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) {
  219. }