| 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 | |
| 31 | #include "xr_interface.h" |
| 32 | #include "servers/rendering/renderer_compositor.h" |
| 33 | |
| 34 | void XRInterface::_bind_methods() { |
| 35 | ADD_SIGNAL(MethodInfo("play_area_changed" , PropertyInfo(Variant::INT, "mode" ))); |
| 36 | |
| 37 | ClassDB::bind_method(D_METHOD("get_name" ), &XRInterface::get_name); |
| 38 | ClassDB::bind_method(D_METHOD("get_capabilities" ), &XRInterface::get_capabilities); |
| 39 | |
| 40 | ClassDB::bind_method(D_METHOD("is_primary" ), &XRInterface::is_primary); |
| 41 | ClassDB::bind_method(D_METHOD("set_primary" , "primary" ), &XRInterface::set_primary); |
| 42 | |
| 43 | ClassDB::bind_method(D_METHOD("is_initialized" ), &XRInterface::is_initialized); |
| 44 | ClassDB::bind_method(D_METHOD("initialize" ), &XRInterface::initialize); |
| 45 | ClassDB::bind_method(D_METHOD("uninitialize" ), &XRInterface::uninitialize); |
| 46 | ClassDB::bind_method(D_METHOD("get_system_info" ), &XRInterface::get_system_info); |
| 47 | |
| 48 | ClassDB::bind_method(D_METHOD("get_tracking_status" ), &XRInterface::get_tracking_status); |
| 49 | |
| 50 | ClassDB::bind_method(D_METHOD("get_render_target_size" ), &XRInterface::get_render_target_size); |
| 51 | ClassDB::bind_method(D_METHOD("get_view_count" ), &XRInterface::get_view_count); |
| 52 | |
| 53 | ClassDB::bind_method(D_METHOD("trigger_haptic_pulse" , "action_name" , "tracker_name" , "frequency" , "amplitude" , "duration_sec" , "delay_sec" ), &XRInterface::trigger_haptic_pulse); |
| 54 | |
| 55 | ADD_GROUP("Interface" , "interface_" ); |
| 56 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interface_is_primary" ), "set_primary" , "is_primary" ); |
| 57 | |
| 58 | // methods and properties specific to VR... |
| 59 | ClassDB::bind_method(D_METHOD("supports_play_area_mode" , "mode" ), &XRInterface::supports_play_area_mode); |
| 60 | ClassDB::bind_method(D_METHOD("get_play_area_mode" ), &XRInterface::get_play_area_mode); |
| 61 | ClassDB::bind_method(D_METHOD("set_play_area_mode" , "mode" ), &XRInterface::set_play_area_mode); |
| 62 | ClassDB::bind_method(D_METHOD("get_play_area" ), &XRInterface::get_play_area); |
| 63 | |
| 64 | ADD_GROUP("XR" , "xr_" ); |
| 65 | 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" ); |
| 66 | |
| 67 | // methods and properties specific to AR.... |
| 68 | ClassDB::bind_method(D_METHOD("get_anchor_detection_is_enabled" ), &XRInterface::get_anchor_detection_is_enabled); |
| 69 | ClassDB::bind_method(D_METHOD("set_anchor_detection_is_enabled" , "enable" ), &XRInterface::set_anchor_detection_is_enabled); |
| 70 | ClassDB::bind_method(D_METHOD("get_camera_feed_id" ), &XRInterface::get_camera_feed_id); |
| 71 | |
| 72 | ClassDB::bind_method(D_METHOD("is_passthrough_supported" ), &XRInterface::is_passthrough_supported); |
| 73 | ClassDB::bind_method(D_METHOD("is_passthrough_enabled" ), &XRInterface::is_passthrough_enabled); |
| 74 | ClassDB::bind_method(D_METHOD("start_passthrough" ), &XRInterface::start_passthrough); |
| 75 | ClassDB::bind_method(D_METHOD("stop_passthrough" ), &XRInterface::stop_passthrough); |
| 76 | ClassDB::bind_method(D_METHOD("get_transform_for_view" , "view" , "cam_transform" ), &XRInterface::get_transform_for_view); |
| 77 | ClassDB::bind_method(D_METHOD("get_projection_for_view" , "view" , "aspect" , "near" , "far" ), &XRInterface::get_projection_for_view); |
| 78 | |
| 79 | /** environment blend mode. */ |
| 80 | ClassDB::bind_method(D_METHOD("get_supported_environment_blend_modes" ), &XRInterface::get_supported_environment_blend_modes); |
| 81 | ClassDB::bind_method(D_METHOD("set_environment_blend_mode" , "mode" ), &XRInterface::set_environment_blend_mode); |
| 82 | ClassDB::bind_method(D_METHOD("get_environment_blend_mode" ), &XRInterface::get_environment_blend_mode); |
| 83 | ADD_PROPERTY(PropertyInfo(Variant::INT, "environment_blend_mode" ), "set_environment_blend_mode" , "get_environment_blend_mode" ); |
| 84 | |
| 85 | ADD_GROUP("AR" , "ar_" ); |
| 86 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ar_is_anchor_detection_enabled" ), "set_anchor_detection_is_enabled" , "get_anchor_detection_is_enabled" ); |
| 87 | |
| 88 | BIND_ENUM_CONSTANT(XR_NONE); |
| 89 | BIND_ENUM_CONSTANT(XR_MONO); |
| 90 | BIND_ENUM_CONSTANT(XR_STEREO); |
| 91 | BIND_ENUM_CONSTANT(XR_QUAD); |
| 92 | BIND_ENUM_CONSTANT(XR_VR); |
| 93 | BIND_ENUM_CONSTANT(XR_AR); |
| 94 | BIND_ENUM_CONSTANT(XR_EXTERNAL); |
| 95 | |
| 96 | BIND_ENUM_CONSTANT(XR_NORMAL_TRACKING); |
| 97 | BIND_ENUM_CONSTANT(XR_EXCESSIVE_MOTION); |
| 98 | BIND_ENUM_CONSTANT(XR_INSUFFICIENT_FEATURES); |
| 99 | BIND_ENUM_CONSTANT(XR_UNKNOWN_TRACKING); |
| 100 | BIND_ENUM_CONSTANT(XR_NOT_TRACKING); |
| 101 | |
| 102 | BIND_ENUM_CONSTANT(XR_PLAY_AREA_UNKNOWN); |
| 103 | BIND_ENUM_CONSTANT(XR_PLAY_AREA_3DOF); |
| 104 | BIND_ENUM_CONSTANT(XR_PLAY_AREA_SITTING); |
| 105 | BIND_ENUM_CONSTANT(XR_PLAY_AREA_ROOMSCALE); |
| 106 | BIND_ENUM_CONSTANT(XR_PLAY_AREA_STAGE); |
| 107 | |
| 108 | BIND_ENUM_CONSTANT(XR_ENV_BLEND_MODE_OPAQUE); |
| 109 | BIND_ENUM_CONSTANT(XR_ENV_BLEND_MODE_ADDITIVE); |
| 110 | BIND_ENUM_CONSTANT(XR_ENV_BLEND_MODE_ALPHA_BLEND); |
| 111 | }; |
| 112 | |
| 113 | bool XRInterface::is_primary() { |
| 114 | XRServer *xr_server = XRServer::get_singleton(); |
| 115 | ERR_FAIL_NULL_V(xr_server, false); |
| 116 | |
| 117 | return xr_server->get_primary_interface() == this; |
| 118 | } |
| 119 | |
| 120 | void XRInterface::set_primary(bool p_primary) { |
| 121 | XRServer *xr_server = XRServer::get_singleton(); |
| 122 | ERR_FAIL_NULL(xr_server); |
| 123 | |
| 124 | if (p_primary) { |
| 125 | ERR_FAIL_COND(!is_initialized()); |
| 126 | |
| 127 | xr_server->set_primary_interface(this); |
| 128 | } else if (xr_server->get_primary_interface() == this) { |
| 129 | xr_server->set_primary_interface(nullptr); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | XRInterface::XRInterface() {} |
| 134 | |
| 135 | XRInterface::~XRInterface() { |
| 136 | if (vrs.vrs_texture.is_valid()) { |
| 137 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 138 | RS::get_singleton()->free(vrs.vrs_texture); |
| 139 | vrs.vrs_texture = RID(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // query if this interface supports this play area mode |
| 144 | bool XRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) { |
| 145 | return p_mode == XR_PLAY_AREA_UNKNOWN; |
| 146 | } |
| 147 | |
| 148 | // get the current play area mode |
| 149 | XRInterface::PlayAreaMode XRInterface::get_play_area_mode() const { |
| 150 | return XR_PLAY_AREA_UNKNOWN; |
| 151 | } |
| 152 | |
| 153 | // change the play area mode, note that this should return false if the mode is not available |
| 154 | bool XRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) { |
| 155 | return p_mode == XR_PLAY_AREA_UNKNOWN; |
| 156 | } |
| 157 | |
| 158 | // if available, returns an array of vectors denoting the play area the player can move around in |
| 159 | PackedVector3Array XRInterface::get_play_area() const { |
| 160 | // Return an empty array by default. |
| 161 | // Note implementation is responsible for applying our reference frame and world scale to the raw data. |
| 162 | // `play_area_changed` should be emitted if play area data is available and either the reference frame or world scale changes. |
| 163 | return PackedVector3Array(); |
| 164 | }; |
| 165 | |
| 166 | /** these will only be implemented on AR interfaces, so we want dummies for VR **/ |
| 167 | bool XRInterface::get_anchor_detection_is_enabled() const { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | void XRInterface::set_anchor_detection_is_enabled(bool p_enable) { |
| 172 | } |
| 173 | |
| 174 | int XRInterface::get_camera_feed_id() { |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | RID XRInterface::get_vrs_texture() { |
| 179 | // Default logic will return a standard VRS image based on our target size and default projections. |
| 180 | // Note that this only gets called if VRS is supported on the hardware. |
| 181 | |
| 182 | int32_t texel_width = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_WIDTH); |
| 183 | int32_t texel_height = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_HEIGHT); |
| 184 | int view_count = get_view_count(); |
| 185 | Size2 target_size = get_render_target_size(); |
| 186 | real_t aspect = target_size.x / target_size.y; // is this y/x ? |
| 187 | Size2 vrs_size = Size2(round(0.5 + target_size.x / texel_width), round(0.5 + target_size.y / texel_height)); |
| 188 | real_t radius = vrs_size.length() * 0.5; |
| 189 | Size2 vrs_sizei = vrs_size; |
| 190 | |
| 191 | if (vrs.size != vrs_sizei) { |
| 192 | const uint8_t densities[] = { |
| 193 | 0, // 1x1 |
| 194 | 1, // 1x2 |
| 195 | // 2, // 1x4 - not supported |
| 196 | // 3, // 1x8 - not supported |
| 197 | // 4, // 2x1 |
| 198 | 5, // 2x2 |
| 199 | 6, // 2x4 |
| 200 | // 9, // 4x2 |
| 201 | 10, // 4x4 |
| 202 | }; |
| 203 | |
| 204 | // out with the old |
| 205 | if (vrs.vrs_texture.is_valid()) { |
| 206 | RS::get_singleton()->free(vrs.vrs_texture); |
| 207 | vrs.vrs_texture = RID(); |
| 208 | } |
| 209 | |
| 210 | // in with the new |
| 211 | Vector<Ref<Image>> images; |
| 212 | vrs.size = vrs_sizei; |
| 213 | |
| 214 | for (int i = 0; i < view_count && i < 2; i++) { |
| 215 | PackedByteArray data; |
| 216 | data.resize(vrs_sizei.x * vrs_sizei.y); |
| 217 | uint8_t *data_ptr = data.ptrw(); |
| 218 | |
| 219 | // 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... |
| 220 | Projection cm = get_projection_for_view(i, aspect, 0.1, 1000.0); |
| 221 | Vector3 center = cm.xform(Vector3(0.0, 0.0, 999.0)); |
| 222 | |
| 223 | Vector2i view_center; |
| 224 | view_center.x = int(vrs_size.x * (center.x + 1.0) * 0.5); |
| 225 | view_center.y = int(vrs_size.y * (center.y + 1.0) * 0.5); |
| 226 | |
| 227 | int d = 0; |
| 228 | for (int y = 0; y < vrs_sizei.y; y++) { |
| 229 | for (int x = 0; x < vrs_sizei.x; x++) { |
| 230 | Vector2 offset = Vector2(x - view_center.x, y - view_center.y); |
| 231 | offset.y *= aspect; |
| 232 | real_t distance = offset.length(); |
| 233 | int idx = round(5.0 * distance / radius); |
| 234 | if (idx > 4) { |
| 235 | idx = 4; |
| 236 | } |
| 237 | uint8_t density = densities[idx]; |
| 238 | |
| 239 | data_ptr[d++] = density; |
| 240 | } |
| 241 | } |
| 242 | images.push_back(Image::create_from_data(vrs_sizei.x, vrs_sizei.y, false, Image::FORMAT_R8, data)); |
| 243 | } |
| 244 | |
| 245 | if (images.size() == 1) { |
| 246 | vrs.vrs_texture = RS::get_singleton()->texture_2d_create(images[0]); |
| 247 | } else { |
| 248 | vrs.vrs_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TEXTURE_LAYERED_2D_ARRAY); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return vrs.vrs_texture; |
| 253 | } |
| 254 | |
| 255 | /** these are optional, so we want dummies **/ |
| 256 | |
| 257 | RID XRInterface::get_color_texture() { |
| 258 | return RID(); |
| 259 | } |
| 260 | |
| 261 | RID XRInterface::get_depth_texture() { |
| 262 | return RID(); |
| 263 | } |
| 264 | |
| 265 | RID XRInterface::get_velocity_texture() { |
| 266 | return RID(); |
| 267 | } |
| 268 | |
| 269 | PackedStringArray XRInterface::get_suggested_tracker_names() const { |
| 270 | PackedStringArray arr; |
| 271 | |
| 272 | return arr; |
| 273 | } |
| 274 | |
| 275 | PackedStringArray XRInterface::get_suggested_pose_names(const StringName &p_tracker_name) const { |
| 276 | PackedStringArray arr; |
| 277 | |
| 278 | return arr; |
| 279 | } |
| 280 | |
| 281 | XRInterface::TrackingStatus XRInterface::get_tracking_status() const { |
| 282 | return XR_UNKNOWN_TRACKING; |
| 283 | } |
| 284 | |
| 285 | 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) { |
| 286 | } |
| 287 | |
| 288 | Array XRInterface::get_supported_environment_blend_modes() { |
| 289 | Array default_blend_modes; |
| 290 | default_blend_modes.push_back(XR_ENV_BLEND_MODE_OPAQUE); |
| 291 | return default_blend_modes; |
| 292 | } |
| 293 | |