| 1 | /**************************************************************************/ |
| 2 | /* openxr_spatial_marker_tracking.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 "openxr_spatial_marker_tracking.h" |
| 32 | |
| 33 | #include "../../openxr_api.h" |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "openxr_spatial_entity_extension.h" |
| 36 | #include "servers/xr/xr_server.h" |
| 37 | |
| 38 | //////////////////////////////////////////////////////////////////////////// |
| 39 | // OpenXRSpatialCapabilityConfigurationQrCode |
| 40 | |
| 41 | void OpenXRSpatialCapabilityConfigurationQrCode::_bind_methods() { |
| 42 | ClassDB::bind_method(D_METHOD("get_enabled_components"), &OpenXRSpatialCapabilityConfigurationQrCode::_get_enabled_components); |
| 43 | } |
| 44 | |
| 45 | bool OpenXRSpatialCapabilityConfigurationQrCode::has_valid_configuration() const { |
| 46 | OpenXRSpatialMarkerTrackingCapability *capability = OpenXRSpatialMarkerTrackingCapability::get_singleton(); |
| 47 | ERR_FAIL_NULL_V(capability, false); |
| 48 | |
| 49 | return capability->is_qrcode_supported(); |
| 50 | } |
| 51 | |
| 52 | XrSpatialCapabilityConfigurationBaseHeaderEXT *OpenXRSpatialCapabilityConfigurationQrCode::get_configuration() { |
| 53 | OpenXRSpatialMarkerTrackingCapability *capability = OpenXRSpatialMarkerTrackingCapability::get_singleton(); |
| 54 | ERR_FAIL_NULL_V(capability, nullptr); |
| 55 | |
| 56 | if (capability->is_qrcode_supported()) { |
| 57 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 58 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 59 | |
| 60 | // Guaranteed components: |
| 61 | enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_MARKER_EXT); |
| 62 | enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_BOUNDED_2D_EXT); |
| 63 | |
| 64 | // Set up our enabled components. |
| 65 | marker_config.enabledComponentCount = enabled_components.size(); |
| 66 | marker_config.enabledComponents = enabled_components.ptr(); |
| 67 | |
| 68 | // and return this. |
| 69 | return (XrSpatialCapabilityConfigurationBaseHeaderEXT *)&marker_config; |
| 70 | } |
| 71 | |
| 72 | return nullptr; |
| 73 | } |
| 74 | |
| 75 | PackedInt64Array OpenXRSpatialCapabilityConfigurationQrCode::_get_enabled_components() const { |
| 76 | PackedInt64Array components; |
| 77 | |
| 78 | for (const XrSpatialComponentTypeEXT &component_type : enabled_components) { |
| 79 | components.push_back((int64_t)component_type); |
| 80 | } |
| 81 | |
| 82 | return components; |
| 83 | } |
| 84 | |
| 85 | //////////////////////////////////////////////////////////////////////////// |
| 86 | // OpenXRSpatialCapabilityConfigurationMicroQrCode |
| 87 | |
| 88 | void OpenXRSpatialCapabilityConfigurationMicroQrCode::_bind_methods() { |
| 89 | ClassDB::bind_method(D_METHOD("get_enabled_components"), &OpenXRSpatialCapabilityConfigurationMicroQrCode::_get_enabled_components); |
| 90 | } |
| 91 | |
| 92 | bool OpenXRSpatialCapabilityConfigurationMicroQrCode::has_valid_configuration() const { |
| 93 | OpenXRSpatialMarkerTrackingCapability *capability = OpenXRSpatialMarkerTrackingCapability::get_singleton(); |
| 94 | ERR_FAIL_NULL_V(capability, false); |
| 95 | |
| 96 | return capability->is_micro_qrcode_supported(); |
| 97 | } |
| 98 | |
| 99 | XrSpatialCapabilityConfigurationBaseHeaderEXT *OpenXRSpatialCapabilityConfigurationMicroQrCode::get_configuration() { |
| 100 | OpenXRSpatialMarkerTrackingCapability *capability = OpenXRSpatialMarkerTrackingCapability::get_singleton(); |
| 101 | ERR_FAIL_NULL_V(capability, nullptr); |
| 102 | |
| 103 | if (capability->is_micro_qrcode_supported()) { |
| 104 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 105 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 106 | |
| 107 | // Guaranteed components: |
| 108 | enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_MARKER_EXT); |
| 109 | enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_BOUNDED_2D_EXT); |
| 110 | |
| 111 | // Set up our enabled components. |
| 112 | marker_config.enabledComponentCount = enabled_components.size(); |
| 113 | marker_config.enabledComponents = enabled_components.ptr(); |
| 114 | |
| 115 | // and return this. |
| 116 | return (XrSpatialCapabilityConfigurationBaseHeaderEXT *)&marker_config; |
| 117 | } |
| 118 | |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
| 122 | PackedInt64Array OpenXRSpatialCapabilityConfigurationMicroQrCode::_get_enabled_components() const { |
| 123 | PackedInt64Array components; |
| 124 | |
| 125 | for (const XrSpatialComponentTypeEXT &component_type : enabled_components) { |
| 126 | components.push_back((int64_t)component_type); |
| 127 | } |
| 128 | |
| 129 | return components; |
| 130 | } |
| 131 | |
| 132 | //////////////////////////////////////////////////////////////////////////// |
| 133 | // OpenXRSpatialCapabilityConfigurationAruco |
| 134 | |
| 135 | void OpenXRSpatialCapabilityConfigurationAruco::_bind_methods() { |
| 136 | ClassDB::bind_method(D_METHOD("get_enabled_components"), &OpenXRSpatialCapabilityConfigurationAruco::_get_enabled_components); |
| 137 | |
| 138 | ClassDB::bind_method(D_METHOD("set_aruco_dict", "aruco_dict"), &OpenXRSpatialCapabilityConfigurationAruco::_set_aruco_dict); |
| 139 | ClassDB::bind_method(D_METHOD("get_aruco_dict"), &OpenXRSpatialCapabilityConfigurationAruco::_get_aruco_dict); |
| 140 | ADD_PROPERTY(PropertyInfo(Variant::INT, "aruco_dict"), "set_aruco_dict", "get_aruco_dict"); |
| 141 | |
| 142 | BIND_ENUM_CONSTANT(ARUCO_DICT_4X4_50); |
| 143 | BIND_ENUM_CONSTANT(ARUCO_DICT_4X4_100); |
| 144 | BIND_ENUM_CONSTANT(ARUCO_DICT_4X4_250); |
| 145 | BIND_ENUM_CONSTANT(ARUCO_DICT_4X4_1000); |
| 146 | BIND_ENUM_CONSTANT(ARUCO_DICT_5X5_50); |
| 147 | BIND_ENUM_CONSTANT(ARUCO_DICT_5X5_100); |
| 148 | BIND_ENUM_CONSTANT(ARUCO_DICT_5X5_250); |
| 149 | BIND_ENUM_CONSTANT(ARUCO_DICT_5X5_1000); |
| 150 | BIND_ENUM_CONSTANT(ARUCO_DICT_6X6_50); |
| 151 | BIND_ENUM_CONSTANT(ARUCO_DICT_6X6_100); |
| 152 | BIND_ENUM_CONSTANT(ARUCO_DICT_6X6_250); |
| 153 | BIND_ENUM_CONSTANT(ARUCO_DICT_6X6_1000); |
| 154 | BIND_ENUM_CONSTANT(ARUCO_DICT_7X7_50); |
| 155 | BIND_ENUM_CONSTANT(ARUCO_DICT_7X7_100); |
| 156 | BIND_ENUM_CONSTANT(ARUCO_DICT_7X7_250); |
| 157 | BIND_ENUM_CONSTANT(ARUCO_DICT_7X7_1000); |
| 158 | } |
| 159 | |
| 160 | bool OpenXRSpatialCapabilityConfigurationAruco::has_valid_configuration() const { |
| 161 | OpenXRSpatialMarkerTrackingCapability *capability = OpenXRSpatialMarkerTrackingCapability::get_singleton(); |
| 162 | ERR_FAIL_NULL_V(capability, false); |
| 163 | |
| 164 | return capability->is_aruco_supported(); |
| 165 | } |
| 166 | |
| 167 | XrSpatialCapabilityConfigurationBaseHeaderEXT *OpenXRSpatialCapabilityConfigurationAruco::get_configuration() { |
| 168 | OpenXRSpatialMarkerTrackingCapability *capability = OpenXRSpatialMarkerTrackingCapability::get_singleton(); |
| 169 | ERR_FAIL_NULL_V(capability, nullptr); |
| 170 | |
| 171 | if (capability->is_aruco_supported()) { |
| 172 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 173 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 174 | |
| 175 | // Guaranteed components: |
| 176 | enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_MARKER_EXT); |
| 177 | enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_BOUNDED_2D_EXT); |
| 178 | |
| 179 | // Set up our enabled components. |
| 180 | marker_config.enabledComponentCount = enabled_components.size(); |
| 181 | marker_config.enabledComponents = enabled_components.ptr(); |
| 182 | |
| 183 | // and return this. |
| 184 | return (XrSpatialCapabilityConfigurationBaseHeaderEXT *)&marker_config; |
| 185 | } |
| 186 | |
| 187 | return nullptr; |
| 188 | } |
| 189 | |
| 190 | void OpenXRSpatialCapabilityConfigurationAruco::set_aruco_dict(XrSpatialMarkerArucoDictEXT p_dict) { |
| 191 | marker_config.arUcoDict = p_dict; |
| 192 | } |
| 193 | |
| 194 | void OpenXRSpatialCapabilityConfigurationAruco::_set_aruco_dict(ArucoDict p_dict) { |
| 195 | set_aruco_dict((XrSpatialMarkerArucoDictEXT)p_dict); |
| 196 | } |
| 197 | |
| 198 | XrSpatialMarkerArucoDictEXT OpenXRSpatialCapabilityConfigurationAruco::get_aruco_dict() const { |
| 199 | return marker_config.arUcoDict; |
| 200 | } |
| 201 | |
| 202 | OpenXRSpatialCapabilityConfigurationAruco::ArucoDict OpenXRSpatialCapabilityConfigurationAruco::_get_aruco_dict() const { |
| 203 | return (ArucoDict)get_aruco_dict(); |
| 204 | } |
| 205 | |
| 206 | PackedInt64Array OpenXRSpatialCapabilityConfigurationAruco::_get_enabled_components() const { |
| 207 | PackedInt64Array components; |
| 208 | |
| 209 | for (const XrSpatialComponentTypeEXT &component_type : enabled_components) { |
| 210 | components.push_back((int64_t)component_type); |
| 211 | } |
| 212 | |
| 213 | return components; |
| 214 | } |
| 215 | |
| 216 | //////////////////////////////////////////////////////////////////////////// |
| 217 | // OpenXRSpatialCapabilityConfigurationAprilTag |
| 218 | |
| 219 | void OpenXRSpatialCapabilityConfigurationAprilTag::_bind_methods() { |
| 220 | ClassDB::bind_method(D_METHOD("get_enabled_components"), &OpenXRSpatialCapabilityConfigurationAprilTag::_get_enabled_components); |
| 221 | |
| 222 | ClassDB::bind_method(D_METHOD("set_april_dict", "april_dict"), &OpenXRSpatialCapabilityConfigurationAprilTag::_set_april_dict); |
| 223 | ClassDB::bind_method(D_METHOD("get_april_dict"), &OpenXRSpatialCapabilityConfigurationAprilTag::_get_april_dict); |
| 224 | ADD_PROPERTY(PropertyInfo(Variant::INT, "april_dict"), "set_april_dict", "get_april_dict"); |
| 225 | |
| 226 | BIND_ENUM_CONSTANT(APRIL_TAG_DICT_16H5); |
| 227 | BIND_ENUM_CONSTANT(APRIL_TAG_DICT_25H9); |
| 228 | BIND_ENUM_CONSTANT(APRIL_TAG_DICT_36H10); |
| 229 | BIND_ENUM_CONSTANT(APRIL_TAG_DICT_36H11); |
| 230 | } |
| 231 | |
| 232 | bool OpenXRSpatialCapabilityConfigurationAprilTag::has_valid_configuration() const { |
| 233 | OpenXRSpatialMarkerTrackingCapability *capability = OpenXRSpatialMarkerTrackingCapability::get_singleton(); |
| 234 | ERR_FAIL_NULL_V(capability, false); |
| 235 | |
| 236 | return capability->is_april_tag_supported(); |
| 237 | } |
| 238 | |
| 239 | XrSpatialCapabilityConfigurationBaseHeaderEXT *OpenXRSpatialCapabilityConfigurationAprilTag::get_configuration() { |
| 240 | OpenXRSpatialMarkerTrackingCapability *capability = OpenXRSpatialMarkerTrackingCapability::get_singleton(); |
| 241 | ERR_FAIL_NULL_V(capability, nullptr); |
| 242 | |
| 243 | if (capability->is_april_tag_supported()) { |
| 244 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 245 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 246 | |
| 247 | // Guaranteed components: |
| 248 | enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_MARKER_EXT); |
| 249 | enabled_components.push_back(XR_SPATIAL_COMPONENT_TYPE_BOUNDED_2D_EXT); |
| 250 | |
| 251 | // Set up our enabled components. |
| 252 | marker_config.enabledComponentCount = enabled_components.size(); |
| 253 | marker_config.enabledComponents = enabled_components.ptr(); |
| 254 | |
| 255 | // and return this. |
| 256 | return (XrSpatialCapabilityConfigurationBaseHeaderEXT *)&marker_config; |
| 257 | } |
| 258 | |
| 259 | return nullptr; |
| 260 | } |
| 261 | |
| 262 | void OpenXRSpatialCapabilityConfigurationAprilTag::set_april_dict(XrSpatialMarkerAprilTagDictEXT p_dict) { |
| 263 | marker_config.aprilDict = p_dict; |
| 264 | } |
| 265 | |
| 266 | void OpenXRSpatialCapabilityConfigurationAprilTag::_set_april_dict(AprilTagDict p_dict) { |
| 267 | set_april_dict((XrSpatialMarkerAprilTagDictEXT)p_dict); |
| 268 | } |
| 269 | |
| 270 | XrSpatialMarkerAprilTagDictEXT OpenXRSpatialCapabilityConfigurationAprilTag::get_april_dict() const { |
| 271 | return marker_config.aprilDict; |
| 272 | } |
| 273 | |
| 274 | OpenXRSpatialCapabilityConfigurationAprilTag::AprilTagDict OpenXRSpatialCapabilityConfigurationAprilTag::_get_april_dict() const { |
| 275 | return (AprilTagDict)get_april_dict(); |
| 276 | } |
| 277 | |
| 278 | PackedInt64Array OpenXRSpatialCapabilityConfigurationAprilTag::_get_enabled_components() const { |
| 279 | PackedInt64Array components; |
| 280 | |
| 281 | for (const XrSpatialComponentTypeEXT &component_type : enabled_components) { |
| 282 | components.push_back((int64_t)component_type); |
| 283 | } |
| 284 | |
| 285 | return components; |
| 286 | } |
| 287 | |
| 288 | //////////////////////////////////////////////////////////////////////////// |
| 289 | // OpenXRSpatialComponentMarkerList |
| 290 | |
| 291 | void OpenXRSpatialComponentMarkerList::_bind_methods() { |
| 292 | ClassDB::bind_method(D_METHOD("get_marker_type", "index"), &OpenXRSpatialComponentMarkerList::get_marker_type); |
| 293 | ClassDB::bind_method(D_METHOD("get_marker_id", "index"), &OpenXRSpatialComponentMarkerList::get_marker_id); |
| 294 | ClassDB::bind_method(D_METHOD("get_marker_data", "snapshot", "index"), &OpenXRSpatialComponentMarkerList::get_marker_data); |
| 295 | |
| 296 | BIND_ENUM_CONSTANT(MARKER_TYPE_UNKNOWN); |
| 297 | BIND_ENUM_CONSTANT(MARKER_TYPE_QRCODE); |
| 298 | BIND_ENUM_CONSTANT(MARKER_TYPE_MICRO_QRCODE); |
| 299 | BIND_ENUM_CONSTANT(MARKER_TYPE_ARUCO); |
| 300 | BIND_ENUM_CONSTANT(MARKER_TYPE_APRIL_TAG); |
| 301 | BIND_ENUM_CONSTANT(MARKER_TYPE_MAX); |
| 302 | } |
| 303 | |
| 304 | void OpenXRSpatialComponentMarkerList::set_capacity(uint32_t p_capacity) { |
| 305 | marker_data.resize(p_capacity); |
| 306 | |
| 307 | marker_list.markerCount = uint32_t(marker_data.size()); |
| 308 | marker_list.markers = marker_data.ptrw(); |
| 309 | } |
| 310 | |
| 311 | XrSpatialComponentTypeEXT OpenXRSpatialComponentMarkerList::get_component_type() const { |
| 312 | return XR_SPATIAL_COMPONENT_TYPE_MARKER_EXT; |
| 313 | } |
| 314 | |
| 315 | void *OpenXRSpatialComponentMarkerList::get_structure_data(void *p_next) { |
| 316 | marker_list.next = p_next; |
| 317 | return &marker_list; |
| 318 | } |
| 319 | |
| 320 | OpenXRSpatialComponentMarkerList::MarkerType OpenXRSpatialComponentMarkerList::get_marker_type(int64_t p_index) const { |
| 321 | ERR_FAIL_INDEX_V(p_index, marker_data.size(), MARKER_TYPE_UNKNOWN); |
| 322 | |
| 323 | // We can't simply cast these. |
| 324 | // This may give us problems in the future if we get new types through vendor extensions. |
| 325 | switch (marker_data[p_index].capability) { |
| 326 | case XR_SPATIAL_CAPABILITY_MARKER_TRACKING_QR_CODE_EXT: { |
| 327 | return MARKER_TYPE_QRCODE; |
| 328 | } break; |
| 329 | case XR_SPATIAL_CAPABILITY_MARKER_TRACKING_MICRO_QR_CODE_EXT: { |
| 330 | return MARKER_TYPE_MICRO_QRCODE; |
| 331 | } break; |
| 332 | case XR_SPATIAL_CAPABILITY_MARKER_TRACKING_ARUCO_MARKER_EXT: { |
| 333 | return MARKER_TYPE_ARUCO; |
| 334 | } break; |
| 335 | case XR_SPATIAL_CAPABILITY_MARKER_TRACKING_APRIL_TAG_EXT: { |
| 336 | return MARKER_TYPE_APRIL_TAG; |
| 337 | } break; |
| 338 | default: { |
| 339 | return MARKER_TYPE_UNKNOWN; |
| 340 | } break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | uint32_t OpenXRSpatialComponentMarkerList::get_marker_id(int64_t p_index) const { |
| 345 | ERR_FAIL_INDEX_V(p_index, marker_data.size(), 0); |
| 346 | |
| 347 | return marker_data[p_index].markerId; |
| 348 | } |
| 349 | |
| 350 | Variant OpenXRSpatialComponentMarkerList::get_marker_data(RID p_snapshot, int64_t p_index) const { |
| 351 | ERR_FAIL_INDEX_V(p_index, marker_data.size(), Variant()); |
| 352 | |
| 353 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 354 | ERR_FAIL_NULL_V(se_extension, Variant()); |
| 355 | |
| 356 | const XrSpatialBufferEXT &data = marker_data[p_index].data; |
| 357 | switch (data.bufferType) { |
| 358 | case XR_SPATIAL_BUFFER_TYPE_STRING_EXT: { |
| 359 | return se_extension->get_string(p_snapshot, data.bufferId); |
| 360 | } break; |
| 361 | case XR_SPATIAL_BUFFER_TYPE_UINT8_EXT: { |
| 362 | return se_extension->get_uint8_buffer(p_snapshot, data.bufferId); |
| 363 | } break; |
| 364 | default: { |
| 365 | return Variant(); |
| 366 | } break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | //////////////////////////////////////////////////////////////////////////// |
| 371 | // OpenXRMarkerTracker |
| 372 | |
| 373 | void OpenXRMarkerTracker::_bind_methods() { |
| 374 | ClassDB::bind_method(D_METHOD("set_bounds_size", "bounds_size"), &OpenXRMarkerTracker::set_bounds_size); |
| 375 | ClassDB::bind_method(D_METHOD("get_bounds_size"), &OpenXRMarkerTracker::get_bounds_size); |
| 376 | ADD_PROPERTY(PropertyInfo(Variant::INT, "bounds_size"), "set_bounds_size", "get_bounds_size"); |
| 377 | |
| 378 | ClassDB::bind_method(D_METHOD("set_marker_type", "marker_type"), &OpenXRMarkerTracker::set_marker_type); |
| 379 | ClassDB::bind_method(D_METHOD("get_marker_type"), &OpenXRMarkerTracker::get_marker_type); |
| 380 | ADD_PROPERTY(PropertyInfo(Variant::INT, "marker_type"), "set_marker_type", "get_marker_type"); |
| 381 | |
| 382 | ClassDB::bind_method(D_METHOD("set_marker_id", "marker_id"), &OpenXRMarkerTracker::set_marker_id); |
| 383 | ClassDB::bind_method(D_METHOD("get_marker_id"), &OpenXRMarkerTracker::get_marker_id); |
| 384 | ADD_PROPERTY(PropertyInfo(Variant::INT, "marker_id"), "set_marker_id", "get_marker_id"); |
| 385 | |
| 386 | // As the type of marker data can vary, we can't make this a property. |
| 387 | ClassDB::bind_method(D_METHOD("set_marker_data", "marker_data"), &OpenXRMarkerTracker::set_marker_data); |
| 388 | ClassDB::bind_method(D_METHOD("get_marker_data"), &OpenXRMarkerTracker::get_marker_data); |
| 389 | } |
| 390 | |
| 391 | void OpenXRMarkerTracker::set_bounds_size(const Vector2 &p_bounds_size) { |
| 392 | bounds_size = p_bounds_size; |
| 393 | } |
| 394 | |
| 395 | Vector2 OpenXRMarkerTracker::get_bounds_size() const { |
| 396 | return bounds_size; |
| 397 | } |
| 398 | |
| 399 | void OpenXRMarkerTracker::set_marker_type(OpenXRSpatialComponentMarkerList::MarkerType p_marker_type) { |
| 400 | marker_type = p_marker_type; |
| 401 | } |
| 402 | |
| 403 | OpenXRSpatialComponentMarkerList::MarkerType OpenXRMarkerTracker::get_marker_type() const { |
| 404 | return marker_type; |
| 405 | } |
| 406 | |
| 407 | void OpenXRMarkerTracker::set_marker_id(uint32_t p_id) { |
| 408 | marker_id = p_id; |
| 409 | } |
| 410 | |
| 411 | uint32_t OpenXRMarkerTracker::get_marker_id() const { |
| 412 | return marker_id; |
| 413 | } |
| 414 | |
| 415 | void OpenXRMarkerTracker::set_marker_data(const Variant &p_data) { |
| 416 | marker_data = p_data; |
| 417 | } |
| 418 | |
| 419 | Variant OpenXRMarkerTracker::get_marker_data() const { |
| 420 | return marker_data; |
| 421 | } |
| 422 | |
| 423 | //////////////////////////////////////////////////////////////////////////// |
| 424 | // OpenXRSpatialMarkerTrackingCapability |
| 425 | |
| 426 | OpenXRSpatialMarkerTrackingCapability *OpenXRSpatialMarkerTrackingCapability::singleton = nullptr; |
| 427 | |
| 428 | OpenXRSpatialMarkerTrackingCapability *OpenXRSpatialMarkerTrackingCapability::get_singleton() { |
| 429 | return singleton; |
| 430 | } |
| 431 | |
| 432 | OpenXRSpatialMarkerTrackingCapability::OpenXRSpatialMarkerTrackingCapability() { |
| 433 | singleton = this; |
| 434 | } |
| 435 | |
| 436 | OpenXRSpatialMarkerTrackingCapability::~OpenXRSpatialMarkerTrackingCapability() { |
| 437 | singleton = nullptr; |
| 438 | } |
| 439 | |
| 440 | void OpenXRSpatialMarkerTrackingCapability::_bind_methods() { |
| 441 | ClassDB::bind_method(D_METHOD("is_qrcode_supported"), &OpenXRSpatialMarkerTrackingCapability::is_qrcode_supported); |
| 442 | ClassDB::bind_method(D_METHOD("is_micro_qrcode_supported"), &OpenXRSpatialMarkerTrackingCapability::is_micro_qrcode_supported); |
| 443 | ClassDB::bind_method(D_METHOD("is_aruco_supported"), &OpenXRSpatialMarkerTrackingCapability::is_aruco_supported); |
| 444 | ClassDB::bind_method(D_METHOD("is_april_tag_supported"), &OpenXRSpatialMarkerTrackingCapability::is_april_tag_supported); |
| 445 | } |
| 446 | |
| 447 | HashMap<String, bool *> OpenXRSpatialMarkerTrackingCapability::get_requested_extensions(XrVersion p_version) { |
| 448 | HashMap<String, bool *> request_extensions; |
| 449 | |
| 450 | if (GLOBAL_GET_CACHED(bool, "xr/openxr/extensions/spatial_entity/enabled") && GLOBAL_GET_CACHED(bool, "xr/openxr/extensions/spatial_entity/enable_marker_tracking")) { |
| 451 | request_extensions[XR_EXT_SPATIAL_MARKER_TRACKING_EXTENSION_NAME] = &spatial_marker_tracking_ext; |
| 452 | } |
| 453 | |
| 454 | return request_extensions; |
| 455 | } |
| 456 | |
| 457 | void OpenXRSpatialMarkerTrackingCapability::on_session_created(const XrSession p_session) { |
| 458 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 459 | ERR_FAIL_NULL(se_extension); |
| 460 | |
| 461 | if (!spatial_marker_tracking_ext) { |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | se_extension->connect(SNAME("spatial_discovery_recommended"), callable_mp(this, &OpenXRSpatialMarkerTrackingCapability::_on_spatial_discovery_recommended)); |
| 466 | |
| 467 | if (GLOBAL_GET_CACHED(bool, "xr/openxr/extensions/spatial_entity/enable_builtin_marker_tracking")) { |
| 468 | // Start by creating our spatial context |
| 469 | _create_spatial_context(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | void OpenXRSpatialMarkerTrackingCapability::on_session_destroyed() { |
| 474 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 475 | ERR_FAIL_NULL(se_extension); |
| 476 | XRServer *xr_server = XRServer::get_singleton(); |
| 477 | ERR_FAIL_NULL(xr_server); |
| 478 | |
| 479 | // Free and unregister our anchors |
| 480 | for (const KeyValue<XrSpatialEntityIdEXT, Ref<OpenXRMarkerTracker>> &marker_tracker : marker_trackers) { |
| 481 | xr_server->remove_tracker(marker_tracker.value); |
| 482 | } |
| 483 | marker_trackers.clear(); |
| 484 | |
| 485 | // Free our spatial context |
| 486 | if (spatial_context.is_valid()) { |
| 487 | se_extension->free_spatial_context(spatial_context); |
| 488 | spatial_context = RID(); |
| 489 | } |
| 490 | |
| 491 | se_extension->disconnect(SNAME("spatial_discovery_recommended"), callable_mp(this, &OpenXRSpatialMarkerTrackingCapability::_on_spatial_discovery_recommended)); |
| 492 | } |
| 493 | |
| 494 | void OpenXRSpatialMarkerTrackingCapability::on_process() { |
| 495 | if (!spatial_context.is_valid()) { |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | // Protection against marker discovery happening too often. |
| 500 | if (discovery_cooldown > 0) { |
| 501 | discovery_cooldown--; |
| 502 | } |
| 503 | |
| 504 | // Check if we need to start our discovery. |
| 505 | if (need_discovery && discovery_cooldown == 0 && !discovery_query_result.is_valid()) { |
| 506 | need_discovery = false; |
| 507 | discovery_cooldown = 60; // Set our cooldown to 60 frames, it doesn't need to be an exact science. |
| 508 | |
| 509 | _start_entity_discovery(); |
| 510 | } |
| 511 | |
| 512 | // If we have markers, we do an update query to check for changed positions. |
| 513 | if (!marker_trackers.is_empty()) { |
| 514 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 515 | ERR_FAIL_NULL(se_extension); |
| 516 | |
| 517 | // We want updates for all anchors |
| 518 | thread_local LocalVector<RID> entities; |
| 519 | entities.resize(marker_trackers.size()); |
| 520 | RID *entity = entities.ptr(); |
| 521 | for (const KeyValue<XrSpatialEntityIdEXT, Ref<OpenXRMarkerTracker>> &e : marker_trackers) { |
| 522 | *entity = e.value->get_entity(); |
| 523 | entity++; |
| 524 | } |
| 525 | |
| 526 | // We just want our anchor component |
| 527 | thread_local LocalVector<XrSpatialComponentTypeEXT> component_types; |
| 528 | component_types.push_back(XR_SPATIAL_COMPONENT_TYPE_BOUNDED_2D_EXT); |
| 529 | |
| 530 | // And we get our update snapshot, this is NOT async! |
| 531 | RID snapshot = se_extension->update_spatial_entities(spatial_context, entities, component_types, nullptr); |
| 532 | if (snapshot.is_valid()) { |
| 533 | _process_snapshot(snapshot, false); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | bool OpenXRSpatialMarkerTrackingCapability::is_qrcode_supported() { |
| 539 | if (!spatial_marker_tracking_ext) { |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 544 | ERR_FAIL_NULL_V(se_extension, false); |
| 545 | |
| 546 | return se_extension->supports_capability(XR_SPATIAL_CAPABILITY_MARKER_TRACKING_QR_CODE_EXT); |
| 547 | } |
| 548 | |
| 549 | bool OpenXRSpatialMarkerTrackingCapability::is_micro_qrcode_supported() { |
| 550 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 551 | ERR_FAIL_NULL_V(se_extension, false); |
| 552 | |
| 553 | return se_extension->supports_capability(XR_SPATIAL_CAPABILITY_MARKER_TRACKING_MICRO_QR_CODE_EXT); |
| 554 | } |
| 555 | |
| 556 | bool OpenXRSpatialMarkerTrackingCapability::is_aruco_supported() { |
| 557 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 558 | ERR_FAIL_NULL_V(se_extension, false); |
| 559 | |
| 560 | return se_extension->supports_capability(XR_SPATIAL_CAPABILITY_MARKER_TRACKING_ARUCO_MARKER_EXT); |
| 561 | } |
| 562 | |
| 563 | bool OpenXRSpatialMarkerTrackingCapability::is_april_tag_supported() { |
| 564 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 565 | ERR_FAIL_NULL_V(se_extension, false); |
| 566 | |
| 567 | return se_extension->supports_capability(XR_SPATIAL_CAPABILITY_MARKER_TRACKING_APRIL_TAG_EXT); |
| 568 | } |
| 569 | |
| 570 | //////////////////////////////////////////////////////////////////////////// |
| 571 | // Discovery logic |
| 572 | |
| 573 | Ref<OpenXRFutureResult> OpenXRSpatialMarkerTrackingCapability::_create_spatial_context() { |
| 574 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 575 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 576 | |
| 577 | TypedArray<OpenXRSpatialCapabilityConfigurationBaseHeader> capability_configurations; |
| 578 | |
| 579 | // Create our configuration objects. |
| 580 | // For now we enable all supported markers, will need to give some more user control over this. |
| 581 | if (is_qrcode_supported()) { |
| 582 | qrcode_configuration.instantiate(); |
| 583 | capability_configurations.push_back(qrcode_configuration); |
| 584 | } |
| 585 | |
| 586 | if (is_micro_qrcode_supported()) { |
| 587 | micro_qrcode_configuration.instantiate(); |
| 588 | capability_configurations.push_back(micro_qrcode_configuration); |
| 589 | } |
| 590 | |
| 591 | if (is_aruco_supported()) { |
| 592 | aruco_configuration.instantiate(); |
| 593 | |
| 594 | int aruco_dict = GLOBAL_GET_CACHED(int, "xr/openxr/extensions/spatial_entity/aruco_dict"); |
| 595 | aruco_configuration->set_aruco_dict((XrSpatialMarkerArucoDictEXT)(XR_SPATIAL_MARKER_ARUCO_DICT_4X4_50_EXT + aruco_dict)); |
| 596 | capability_configurations.push_back(aruco_configuration); |
| 597 | } |
| 598 | |
| 599 | if (is_april_tag_supported()) { |
| 600 | april_tag_configuration.instantiate(); |
| 601 | |
| 602 | int april_tag_dict = GLOBAL_GET_CACHED(int, "xr/openxr/extensions/spatial_entity/april_tag_dict"); |
| 603 | april_tag_configuration->set_april_dict((XrSpatialMarkerAprilTagDictEXT)(XR_SPATIAL_MARKER_APRIL_TAG_DICT_16H5_EXT + april_tag_dict)); |
| 604 | capability_configurations.push_back(april_tag_configuration); |
| 605 | } |
| 606 | |
| 607 | if (capability_configurations.is_empty()) { |
| 608 | print_verbose("OpenXR: There are no supported marker types. Marker tracking is not enabled."); |
| 609 | return nullptr; |
| 610 | } |
| 611 | |
| 612 | return se_extension->create_spatial_context(capability_configurations, nullptr, callable_mp(this, &OpenXRSpatialMarkerTrackingCapability::_on_spatial_context_created)); |
| 613 | } |
| 614 | |
| 615 | void OpenXRSpatialMarkerTrackingCapability::_on_spatial_context_created(RID p_spatial_context) { |
| 616 | spatial_context = p_spatial_context; |
| 617 | need_discovery = true; |
| 618 | } |
| 619 | |
| 620 | void OpenXRSpatialMarkerTrackingCapability::_on_spatial_discovery_recommended(RID p_spatial_context) { |
| 621 | if (p_spatial_context == spatial_context) { |
| 622 | // Trigger new discovery. |
| 623 | need_discovery = true; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | Ref<OpenXRFutureResult> OpenXRSpatialMarkerTrackingCapability::_start_entity_discovery() { |
| 628 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 629 | ERR_FAIL_NULL_V(se_extension, nullptr); |
| 630 | |
| 631 | // Already running or ran discovery, cancel/clean up. |
| 632 | if (discovery_query_result.is_valid()) { |
| 633 | discovery_query_result->cancel_future(); |
| 634 | discovery_query_result.unref(); |
| 635 | } |
| 636 | |
| 637 | // We want both our anchor and persistence component. |
| 638 | Vector<XrSpatialComponentTypeEXT> component_types; |
| 639 | component_types.push_back(XR_SPATIAL_COMPONENT_TYPE_MARKER_EXT); |
| 640 | component_types.push_back(XR_SPATIAL_COMPONENT_TYPE_BOUNDED_2D_EXT); |
| 641 | |
| 642 | // Start our new snapshot. |
| 643 | discovery_query_result = se_extension->discover_spatial_entities(spatial_context, component_types, nullptr, callable_mp(this, &OpenXRSpatialMarkerTrackingCapability::_process_snapshot).bind(true)); |
| 644 | |
| 645 | return discovery_query_result; |
| 646 | } |
| 647 | |
| 648 | void OpenXRSpatialMarkerTrackingCapability::_process_snapshot(RID p_snapshot, bool p_is_discovery) { |
| 649 | OpenXRSpatialEntityExtension *se_extension = OpenXRSpatialEntityExtension::get_singleton(); |
| 650 | ERR_FAIL_NULL(se_extension); |
| 651 | XRServer *xr_server = XRServer::get_singleton(); |
| 652 | ERR_FAIL_NULL(xr_server); |
| 653 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 654 | ERR_FAIL_NULL(openxr_api); |
| 655 | |
| 656 | // Make a copy of the markers we have right now, so we know which ones to clean up. |
| 657 | LocalVector<XrSpatialEntityIdEXT> current_markers; |
| 658 | if (p_is_discovery) { |
| 659 | current_markers.resize(marker_trackers.size()); |
| 660 | int m = 0; |
| 661 | for (const KeyValue<XrSpatialEntityIdEXT, Ref<OpenXRMarkerTracker>> &marker : marker_trackers) { |
| 662 | current_markers[m++] = marker.key; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // Build our component data. |
| 667 | TypedArray<OpenXRSpatialComponentData> component_data; |
| 668 | |
| 669 | // We always need a query result data object. |
| 670 | Ref<OpenXRSpatialQueryResultData> query_result_data; |
| 671 | query_result_data.instantiate(); |
| 672 | component_data.push_back(query_result_data); |
| 673 | |
| 674 | // Add bounded2D. |
| 675 | Ref<OpenXRSpatialComponentBounded2DList> bounded2d_list; |
| 676 | bounded2d_list.instantiate(); |
| 677 | component_data.push_back(bounded2d_list); |
| 678 | |
| 679 | // Marker data list. |
| 680 | Ref<OpenXRSpatialComponentMarkerList> marker_list; |
| 681 | if (p_is_discovery) { |
| 682 | marker_list.instantiate(); |
| 683 | component_data.push_back(marker_list); |
| 684 | } |
| 685 | |
| 686 | if (se_extension->query_snapshot(p_snapshot, component_data, nullptr)) { |
| 687 | // Now loop through our data and update our markers. |
| 688 | int64_t size = query_result_data->get_capacity(); |
| 689 | |
| 690 | for (int64_t i = 0; i < size; i++) { |
| 691 | XrSpatialEntityIdEXT entity_id = query_result_data->get_entity_id(i); |
| 692 | XrSpatialEntityTrackingStateEXT entity_state = query_result_data->get_entity_state(i); |
| 693 | |
| 694 | // Erase it from our current markers (if we have it, else this is ignored). |
| 695 | current_markers.erase(entity_id); |
| 696 | |
| 697 | if (entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_STOPPED_EXT) { |
| 698 | // We should only get this status on update queries. |
| 699 | // We'll remove the marker. |
| 700 | if (marker_trackers.has(entity_id)) { |
| 701 | Ref<OpenXRMarkerTracker> marker_tracker = marker_trackers[entity_id]; |
| 702 | |
| 703 | marker_tracker->invalidate_pose(SNAME("default")); |
| 704 | marker_tracker->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_STOPPED_EXT); |
| 705 | |
| 706 | // Remove it from our XRServer. |
| 707 | xr_server->remove_tracker(marker_tracker); |
| 708 | |
| 709 | // Remove it from our trackers. |
| 710 | marker_trackers.erase(entity_id); |
| 711 | } |
| 712 | } else { |
| 713 | // Process our entity. |
| 714 | bool add_to_xr_server = false; |
| 715 | Ref<OpenXRMarkerTracker> marker_tracker; |
| 716 | |
| 717 | if (marker_trackers.has(entity_id)) { |
| 718 | // We know about this one already. |
| 719 | marker_tracker = marker_trackers[entity_id]; |
| 720 | } else { |
| 721 | // Create a new anchor. |
| 722 | marker_tracker.instantiate(); |
| 723 | marker_tracker->set_entity(se_extension->make_spatial_entity(se_extension->get_spatial_snapshot_context(p_snapshot), entity_id)); |
| 724 | marker_trackers[entity_id] = marker_tracker; |
| 725 | |
| 726 | add_to_xr_server = true; |
| 727 | } |
| 728 | |
| 729 | // Handle component data. |
| 730 | if (entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_PAUSED_EXT) { |
| 731 | marker_tracker->invalidate_pose(SNAME("default")); |
| 732 | marker_tracker->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_PAUSED_EXT); |
| 733 | |
| 734 | // No further component data will be valid in this state, we need to ignore it! |
| 735 | } else if (entity_state == XR_SPATIAL_ENTITY_TRACKING_STATE_TRACKING_EXT) { |
| 736 | Transform3D transform = bounded2d_list->get_center_pose(i); |
| 737 | marker_tracker->set_pose(SNAME("default"), transform, Vector3(), Vector3()); |
| 738 | marker_tracker->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_TRACKING_EXT); |
| 739 | |
| 740 | // Process our component data. |
| 741 | |
| 742 | // Set bounds size. |
| 743 | marker_tracker->set_bounds_size(bounded2d_list->get_size(i)); |
| 744 | |
| 745 | // Set marker data. |
| 746 | if (p_is_discovery) { |
| 747 | marker_tracker->set_marker_type(marker_list->get_marker_type(i)); |
| 748 | marker_tracker->set_marker_id(marker_list->get_marker_id(i)); |
| 749 | marker_tracker->set_marker_data(marker_list->get_marker_data(p_snapshot, i)); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if (add_to_xr_server) { |
| 754 | // Register with XR server. |
| 755 | xr_server->add_tracker(marker_tracker); |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if (p_is_discovery) { |
| 761 | // Remove any markers that are no longer there... |
| 762 | for (const XrSpatialEntityIdEXT &entity_id : current_markers) { |
| 763 | if (marker_trackers.has(entity_id)) { |
| 764 | Ref<OpenXRMarkerTracker> marker_tracker = marker_trackers[entity_id]; |
| 765 | |
| 766 | // Just in case there are still references out there to this marker, |
| 767 | // reset some stuff. |
| 768 | marker_tracker->invalidate_pose(SNAME("default")); |
| 769 | marker_tracker->set_spatial_tracking_state(XR_SPATIAL_ENTITY_TRACKING_STATE_STOPPED_EXT); |
| 770 | |
| 771 | // Remove it from our XRServer. |
| 772 | xr_server->remove_tracker(marker_tracker); |
| 773 | |
| 774 | // Remove it from our trackers. |
| 775 | marker_trackers.erase(entity_id); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | // Now that we're done, clean up our snapshot! |
| 782 | se_extension->free_spatial_snapshot(p_snapshot); |
| 783 | |
| 784 | // And if this was our discovery snapshot, let's reset it. |
| 785 | if (p_is_discovery && discovery_query_result.is_valid() && discovery_query_result->get_result_value() == p_snapshot) { |
| 786 | discovery_query_result.unref(); |
| 787 | } |
| 788 | } |