openxr_spatial_plane_tracking.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**************************************************************************/
  2. /* openxr_spatial_plane_tracking.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. #pragma once
  31. #include "openxr_spatial_entities.h"
  32. #include "openxr_spatial_entity_extension.h"
  33. #include "scene/resources/3d/shape_3d.h"
  34. // Plane tracking capability configuration
  35. class OpenXRSpatialCapabilityConfigurationPlaneTracking : public OpenXRSpatialCapabilityConfigurationBaseHeader {
  36. GDCLASS(OpenXRSpatialCapabilityConfigurationPlaneTracking, OpenXRSpatialCapabilityConfigurationBaseHeader);
  37. public:
  38. virtual bool has_valid_configuration() const override;
  39. virtual XrSpatialCapabilityConfigurationBaseHeaderEXT *get_configuration() override;
  40. bool get_supports_mesh_2d();
  41. bool get_supports_polygons();
  42. bool get_supports_labels();
  43. Vector<XrSpatialComponentTypeEXT> get_enabled_components() const { return plane_enabled_components; }
  44. protected:
  45. static void _bind_methods();
  46. private:
  47. int supports_mesh_2d = -1;
  48. int supports_polygons = -1;
  49. int supports_labels = -1;
  50. Vector<XrSpatialComponentTypeEXT> plane_enabled_components;
  51. XrSpatialCapabilityConfigurationPlaneTrackingEXT plane_config = { XR_TYPE_SPATIAL_CAPABILITY_CONFIGURATION_PLANE_TRACKING_EXT, nullptr, XR_SPATIAL_CAPABILITY_PLANE_TRACKING_EXT, 0, nullptr };
  52. PackedInt64Array _get_enabled_components() const;
  53. };
  54. // Plane alignment component data
  55. class OpenXRSpatialComponentPlaneAlignmentList : public OpenXRSpatialComponentData {
  56. GDCLASS(OpenXRSpatialComponentPlaneAlignmentList, OpenXRSpatialComponentData);
  57. public:
  58. enum PlaneAlignment {
  59. PLANE_ALIGNMENT_HORIZONTAL_UPWARD = XR_SPATIAL_PLANE_ALIGNMENT_HORIZONTAL_UPWARD_EXT,
  60. PLANE_ALIGNMENT_HORIZONTAL_DOWNWARD = XR_SPATIAL_PLANE_ALIGNMENT_HORIZONTAL_DOWNWARD_EXT,
  61. PLANE_ALIGNMENT_VERTICAL = XR_SPATIAL_PLANE_ALIGNMENT_VERTICAL_EXT,
  62. PLANE_ALIGNMENT_ARBITRARY = XR_SPATIAL_PLANE_ALIGNMENT_ARBITRARY_EXT,
  63. };
  64. virtual void set_capacity(uint32_t p_capacity) override;
  65. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  66. virtual void *get_structure_data(void *p_next) override;
  67. XrSpatialPlaneAlignmentEXT get_plane_alignment(int64_t p_index) const;
  68. protected:
  69. static void _bind_methods();
  70. private:
  71. Vector<XrSpatialPlaneAlignmentEXT> plane_alignment_data;
  72. XrSpatialComponentPlaneAlignmentListEXT plane_alignment_list = { XR_TYPE_SPATIAL_COMPONENT_PLANE_ALIGNMENT_LIST_EXT, nullptr, 0, nullptr };
  73. PlaneAlignment _get_plane_alignment(int64_t p_index) const;
  74. };
  75. VARIANT_ENUM_CAST(OpenXRSpatialComponentPlaneAlignmentList::PlaneAlignment);
  76. class OpenXRSpatialComponentPolygon2DList : public OpenXRSpatialComponentData {
  77. GDCLASS(OpenXRSpatialComponentPolygon2DList, OpenXRSpatialComponentData);
  78. protected:
  79. static void _bind_methods();
  80. public:
  81. virtual void set_capacity(uint32_t p_capacity) override;
  82. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  83. virtual void *get_structure_data(void *p_next) override;
  84. Transform3D get_transform(int64_t p_index) const;
  85. PackedVector2Array get_vertices(RID p_snapshot, int64_t p_index) const;
  86. private:
  87. Vector<XrSpatialPolygon2DDataEXT> polygon2d_data;
  88. XrSpatialComponentPolygon2DListEXT polygon2d_list = { XR_TYPE_SPATIAL_COMPONENT_POLYGON_2D_LIST_EXT, nullptr, 0, nullptr };
  89. };
  90. // Plane semantic label component data.
  91. class OpenXRSpatialComponentPlaneSemanticLabelList : public OpenXRSpatialComponentData {
  92. GDCLASS(OpenXRSpatialComponentPlaneSemanticLabelList, OpenXRSpatialComponentData);
  93. public:
  94. enum PlaneSemanticLabel {
  95. PLANE_SEMANTIC_LABEL_UNCATEGORIZED = XR_SPATIAL_PLANE_SEMANTIC_LABEL_UNCATEGORIZED_EXT,
  96. PLANE_SEMANTIC_LABEL_FLOOR = XR_SPATIAL_PLANE_SEMANTIC_LABEL_FLOOR_EXT,
  97. PLANE_SEMANTIC_LABEL_WALL = XR_SPATIAL_PLANE_SEMANTIC_LABEL_WALL_EXT,
  98. PLANE_SEMANTIC_LABEL_CEILING = XR_SPATIAL_PLANE_SEMANTIC_LABEL_CEILING_EXT,
  99. PLANE_SEMANTIC_LABEL_TABLE = XR_SPATIAL_PLANE_SEMANTIC_LABEL_TABLE_EXT,
  100. };
  101. virtual void set_capacity(uint32_t p_capacity) override;
  102. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  103. virtual void *get_structure_data(void *p_next) override;
  104. XrSpatialPlaneSemanticLabelEXT get_plane_semantic_label(int64_t p_index) const;
  105. protected:
  106. static void _bind_methods();
  107. private:
  108. Vector<XrSpatialPlaneSemanticLabelEXT> plane_semantic_label_data;
  109. XrSpatialComponentPlaneSemanticLabelListEXT plane_semantic_label_list = { XR_TYPE_SPATIAL_COMPONENT_PLANE_SEMANTIC_LABEL_LIST_EXT, nullptr, 0, nullptr };
  110. PlaneSemanticLabel _get_plane_semantic_label(int64_t p_index) const;
  111. };
  112. VARIANT_ENUM_CAST(OpenXRSpatialComponentPlaneSemanticLabelList::PlaneSemanticLabel);
  113. // Plane tracker
  114. class OpenXRPlaneTracker : public OpenXRSpatialEntityTracker {
  115. GDCLASS(OpenXRPlaneTracker, OpenXRSpatialEntityTracker);
  116. public:
  117. void set_bounds_size(const Vector2 &p_bounds_size);
  118. Vector2 get_bounds_size() const;
  119. void set_plane_alignment(OpenXRSpatialComponentPlaneAlignmentList::PlaneAlignment p_plane_alignment);
  120. OpenXRSpatialComponentPlaneAlignmentList::PlaneAlignment get_plane_alignment() const;
  121. void set_plane_label(const String &p_plane_label);
  122. String get_plane_label() const;
  123. void set_mesh_data(const Transform3D &p_origin, const PackedVector2Array &p_vertices, const PackedInt32Array &p_indices = PackedInt32Array());
  124. void clear_mesh_data();
  125. Transform3D get_mesh_offset() const;
  126. Ref<Mesh> get_mesh();
  127. Ref<Shape3D> get_shape(real_t p_thickness = 0.01);
  128. protected:
  129. static void _bind_methods();
  130. private:
  131. Vector2 bounds_size;
  132. OpenXRSpatialComponentPlaneAlignmentList::PlaneAlignment plane_alignment = OpenXRSpatialComponentPlaneAlignmentList::PLANE_ALIGNMENT_HORIZONTAL_UPWARD;
  133. String plane_label;
  134. // Mesh data (if we have this)
  135. struct MeshData {
  136. bool has_mesh_data = false;
  137. Transform3D origin;
  138. PackedVector2Array vertices;
  139. PackedInt32Array indices;
  140. Ref<Mesh> mesh;
  141. Ref<Shape3D> shape3d;
  142. } mesh;
  143. struct Edge {
  144. int32_t a;
  145. int32_t b;
  146. static _FORCE_INLINE_ uint32_t hash(const Edge &p_edge) {
  147. uint32_t h = hash_murmur3_one_32(p_edge.a);
  148. return hash_murmur3_one_32(p_edge.b, h);
  149. }
  150. bool operator==(const Edge &p_edge) const {
  151. return (a == p_edge.a && b == p_edge.b);
  152. }
  153. Edge(int32_t p_a = 0, int32_t p_b = 0) {
  154. a = p_a;
  155. b = p_b;
  156. if (a < b) {
  157. SWAP(a, b);
  158. }
  159. }
  160. };
  161. };
  162. // Plane tracking logic
  163. class OpenXRSpatialPlaneTrackingCapability : public OpenXRExtensionWrapper {
  164. GDCLASS(OpenXRSpatialPlaneTrackingCapability, OpenXRExtensionWrapper);
  165. protected:
  166. static void _bind_methods();
  167. public:
  168. static OpenXRSpatialPlaneTrackingCapability *get_singleton();
  169. OpenXRSpatialPlaneTrackingCapability();
  170. virtual ~OpenXRSpatialPlaneTrackingCapability() override;
  171. virtual HashMap<String, bool *> get_requested_extensions(XrVersion p_version) override;
  172. virtual void on_session_created(const XrSession p_session) override;
  173. virtual void on_session_destroyed() override;
  174. virtual void on_process() override;
  175. bool is_supported();
  176. private:
  177. static OpenXRSpatialPlaneTrackingCapability *singleton;
  178. bool spatial_plane_tracking_ext = false;
  179. bool spatial_plane_tracking_supported = false;
  180. RID spatial_context;
  181. bool need_discovery = false;
  182. int discovery_cooldown = 0;
  183. Ref<OpenXRFutureResult> discovery_query_result;
  184. Ref<OpenXRSpatialCapabilityConfigurationPlaneTracking> plane_configuration;
  185. // Discovery logic
  186. Ref<OpenXRFutureResult> _create_spatial_context();
  187. void _on_spatial_context_created(RID p_spatial_context);
  188. void _on_spatial_discovery_recommended(RID p_spatial_context);
  189. Ref<OpenXRFutureResult> _start_entity_discovery();
  190. void _process_snapshot(RID p_snapshot);
  191. // Trackers
  192. HashMap<XrSpatialEntityIdEXT, Ref<OpenXRPlaneTracker>> plane_trackers;
  193. };