2
0

openxr_spatial_entities.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**************************************************************************/
  2. /* openxr_spatial_entities.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_structure.h"
  32. #include "../openxr_future_extension.h"
  33. #include "scene/resources/mesh.h"
  34. #include "servers/xr/xr_positional_tracker.h"
  35. #define XR_NULL_ENTITY 0x7FFFFFFF
  36. // Wrapper class for XrSpatialCapabilityConfigurationBaseHeaderEXT
  37. class OpenXRSpatialCapabilityConfigurationBaseHeader : public RefCounted {
  38. GDCLASS(OpenXRSpatialCapabilityConfigurationBaseHeader, RefCounted);
  39. protected:
  40. static void _bind_methods();
  41. public:
  42. virtual bool has_valid_configuration() const;
  43. virtual XrSpatialCapabilityConfigurationBaseHeaderEXT *get_configuration();
  44. GDVIRTUAL0RC(bool, _has_valid_configuration);
  45. GDVIRTUAL0R(uint64_t, _get_configuration);
  46. };
  47. // Tracker for our spatial entities
  48. class OpenXRSpatialEntityTracker : public XRPositionalTracker {
  49. GDCLASS(OpenXRSpatialEntityTracker, XRPositionalTracker);
  50. public:
  51. enum EntityTrackingState {
  52. ENTITY_TRACKING_STATE_STOPPED = XR_SPATIAL_ENTITY_TRACKING_STATE_STOPPED_EXT,
  53. ENTITY_TRACKING_STATE_PAUSED = XR_SPATIAL_ENTITY_TRACKING_STATE_PAUSED_EXT,
  54. ENTITY_TRACKING_STATE_TRACKING = XR_SPATIAL_ENTITY_TRACKING_STATE_TRACKING_EXT,
  55. };
  56. OpenXRSpatialEntityTracker();
  57. virtual ~OpenXRSpatialEntityTracker();
  58. void set_entity(const RID &p_entity);
  59. RID get_entity() const;
  60. void set_spatial_tracking_state(const XrSpatialEntityTrackingStateEXT p_state);
  61. XrSpatialEntityTrackingStateEXT get_spatial_tracking_state() const;
  62. protected:
  63. static void _bind_methods();
  64. private:
  65. RID spatial_entity;
  66. XrSpatialEntityTrackingStateEXT spatial_tracking_state = XR_SPATIAL_ENTITY_TRACKING_STATE_PAUSED_EXT;
  67. void _set_spatial_tracking_state(const EntityTrackingState p_state);
  68. EntityTrackingState _get_spatial_tracking_state() const;
  69. };
  70. VARIANT_ENUM_CAST(OpenXRSpatialEntityTracker::EntityTrackingState)
  71. // Wrapper class for our spatial component data returned by discovery queries
  72. class OpenXRSpatialComponentData : public RefCounted {
  73. GDCLASS(OpenXRSpatialComponentData, RefCounted);
  74. protected:
  75. static void _bind_methods();
  76. public:
  77. virtual void set_capacity(uint32_t p_capacity);
  78. virtual XrSpatialComponentTypeEXT get_component_type() const;
  79. virtual void *get_structure_data(void *p_next);
  80. GDVIRTUAL1(_set_capacity, uint32_t);
  81. GDVIRTUAL0RC(uint64_t, _get_component_type);
  82. GDVIRTUAL1RC(uint64_t, _get_structure_data, uint64_t);
  83. };
  84. class OpenXRSpatialComponentBounded2DList : public OpenXRSpatialComponentData {
  85. GDCLASS(OpenXRSpatialComponentBounded2DList, OpenXRSpatialComponentData);
  86. protected:
  87. static void _bind_methods();
  88. public:
  89. virtual void set_capacity(uint32_t p_capacity) override;
  90. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  91. virtual void *get_structure_data(void *p_next) override;
  92. Transform3D get_center_pose(int64_t p_index) const;
  93. Vector2 get_size(int64_t p_index) const;
  94. private:
  95. Vector<XrSpatialBounded2DDataEXT> bounded2d_data;
  96. XrSpatialComponentBounded2DListEXT bounded2d_list = { XR_TYPE_SPATIAL_COMPONENT_BOUNDED_2D_LIST_EXT, nullptr, 0, nullptr };
  97. };
  98. class OpenXRSpatialComponentBounded3DList : public OpenXRSpatialComponentData {
  99. GDCLASS(OpenXRSpatialComponentBounded3DList, OpenXRSpatialComponentData);
  100. protected:
  101. static void _bind_methods();
  102. public:
  103. virtual void set_capacity(uint32_t p_capacity) override;
  104. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  105. virtual void *get_structure_data(void *p_next) override;
  106. Transform3D get_center_pose(int64_t p_index) const;
  107. Vector3 get_size(int64_t p_index) const;
  108. private:
  109. Vector<XrBoxf> bounded3d_data;
  110. XrSpatialComponentBounded3DListEXT bounded3d_list = { XR_TYPE_SPATIAL_COMPONENT_BOUNDED_3D_LIST_EXT, nullptr, 0, nullptr };
  111. };
  112. class OpenXRSpatialComponentParentList : public OpenXRSpatialComponentData {
  113. GDCLASS(OpenXRSpatialComponentParentList, OpenXRSpatialComponentData);
  114. protected:
  115. static void _bind_methods();
  116. public:
  117. virtual void set_capacity(uint32_t p_capacity) override;
  118. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  119. virtual void *get_structure_data(void *p_next) override;
  120. RID get_parent(int64_t p_index) const;
  121. private:
  122. Vector<XrSpatialEntityIdEXT> parent_data;
  123. XrSpatialComponentParentListEXT parent_list = { XR_TYPE_SPATIAL_COMPONENT_PARENT_LIST_EXT, nullptr, 0, nullptr };
  124. };
  125. class OpenXRSpatialComponentMesh2DList : public OpenXRSpatialComponentData {
  126. GDCLASS(OpenXRSpatialComponentMesh2DList, OpenXRSpatialComponentData);
  127. protected:
  128. static void _bind_methods();
  129. public:
  130. virtual void set_capacity(uint32_t p_capacity) override;
  131. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  132. virtual void *get_structure_data(void *p_next) override;
  133. Transform3D get_transform(int64_t p_index) const;
  134. PackedVector2Array get_vertices(RID p_snapshot, int64_t p_index) const;
  135. PackedInt32Array get_indices(RID p_snapshot, int64_t p_index) const;
  136. private:
  137. Vector<XrSpatialMeshDataEXT> mesh2d_data;
  138. XrSpatialComponentMesh2DListEXT mesh2d_list = { XR_TYPE_SPATIAL_COMPONENT_MESH_2D_LIST_EXT, nullptr, 0, nullptr };
  139. };
  140. class OpenXRSpatialComponentMesh3DList : public OpenXRSpatialComponentData {
  141. GDCLASS(OpenXRSpatialComponentMesh3DList, OpenXRSpatialComponentData);
  142. protected:
  143. static void _bind_methods();
  144. public:
  145. virtual void set_capacity(uint32_t p_capacity) override;
  146. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  147. virtual void *get_structure_data(void *p_next) override;
  148. Transform3D get_transform(int64_t p_index) const;
  149. Ref<Mesh> get_mesh(int64_t p_index) const;
  150. private:
  151. Vector<XrSpatialMeshDataEXT> mesh3d_data;
  152. XrSpatialComponentMesh3DListEXT mesh3d_list = { XR_TYPE_SPATIAL_COMPONENT_MESH_3D_LIST_EXT, nullptr, 0, nullptr };
  153. };
  154. class OpenXRSpatialQueryResultData : public OpenXRSpatialComponentData {
  155. GDCLASS(OpenXRSpatialQueryResultData, OpenXRSpatialComponentData);
  156. protected:
  157. static void _bind_methods();
  158. public:
  159. virtual void set_capacity(uint32_t p_capacity) override;
  160. virtual XrSpatialComponentTypeEXT get_component_type() const override;
  161. virtual void *get_structure_data(void *p_next) override;
  162. int64_t get_capacity() const { return entity_ids.size(); }
  163. XrSpatialEntityIdEXT get_entity_id(int64_t p_index) const;
  164. XrSpatialEntityTrackingStateEXT get_entity_state(int64_t p_index) const;
  165. static String get_entity_tracking_state_name(XrSpatialEntityTrackingStateEXT p_tracking_state);
  166. private:
  167. Vector<XrSpatialEntityIdEXT> entity_ids;
  168. Vector<XrSpatialEntityTrackingStateEXT> entity_states;
  169. XrSpatialComponentDataQueryResultEXT query_result = { XR_TYPE_SPATIAL_COMPONENT_DATA_QUERY_RESULT_EXT, nullptr, 0, 0, nullptr, 0, 0, nullptr };
  170. uint64_t _get_entity_id(int64_t p_index) const;
  171. OpenXRSpatialEntityTracker::EntityTrackingState _get_entity_state(int64_t p_index) const;
  172. };