openxr_composition_layer_equirect.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**************************************************************************/
  2. /* openxr_composition_layer_equirect.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. #include "openxr_composition_layer_equirect.h"
  31. #include "../extensions/openxr_composition_layer_extension.h"
  32. #include "../openxr_api.h"
  33. #include "../openxr_interface.h"
  34. #include "scene/3d/mesh_instance_3d.h"
  35. #include "scene/main/viewport.h"
  36. #include "scene/resources/mesh.h"
  37. OpenXRCompositionLayerEquirect::OpenXRCompositionLayerEquirect() :
  38. OpenXRCompositionLayer((XrCompositionLayerBaseHeader *)&composition_layer) {
  39. XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerEquirect::update_transform));
  40. }
  41. OpenXRCompositionLayerEquirect::~OpenXRCompositionLayerEquirect() {
  42. }
  43. void OpenXRCompositionLayerEquirect::_bind_methods() {
  44. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &OpenXRCompositionLayerEquirect::set_radius);
  45. ClassDB::bind_method(D_METHOD("get_radius"), &OpenXRCompositionLayerEquirect::get_radius);
  46. ClassDB::bind_method(D_METHOD("set_central_horizontal_angle", "angle"), &OpenXRCompositionLayerEquirect::set_central_horizontal_angle);
  47. ClassDB::bind_method(D_METHOD("get_central_horizontal_angle"), &OpenXRCompositionLayerEquirect::get_central_horizontal_angle);
  48. ClassDB::bind_method(D_METHOD("set_upper_vertical_angle", "angle"), &OpenXRCompositionLayerEquirect::set_upper_vertical_angle);
  49. ClassDB::bind_method(D_METHOD("get_upper_vertical_angle"), &OpenXRCompositionLayerEquirect::get_upper_vertical_angle);
  50. ClassDB::bind_method(D_METHOD("set_lower_vertical_angle", "angle"), &OpenXRCompositionLayerEquirect::set_lower_vertical_angle);
  51. ClassDB::bind_method(D_METHOD("get_lower_vertical_angle"), &OpenXRCompositionLayerEquirect::get_lower_vertical_angle);
  52. ClassDB::bind_method(D_METHOD("set_fallback_segments", "segments"), &OpenXRCompositionLayerEquirect::set_fallback_segments);
  53. ClassDB::bind_method(D_METHOD("get_fallback_segments"), &OpenXRCompositionLayerEquirect::get_fallback_segments);
  54. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_NONE, ""), "set_radius", "get_radius");
  55. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "central_horizontal_angle", PROPERTY_HINT_RANGE, "0,360,0.1,or_less,or_greater,radians_as_degrees"), "set_central_horizontal_angle", "get_central_horizontal_angle");
  56. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "upper_vertical_angle", PROPERTY_HINT_RANGE, "0,90,0.1,or_less,or_greater,radians_as_degrees"), "set_upper_vertical_angle", "get_upper_vertical_angle");
  57. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lower_vertical_angle", PROPERTY_HINT_RANGE, "0,90,0.1,or_less,or_greater,radians_as_degrees"), "set_lower_vertical_angle", "get_lower_vertical_angle");
  58. ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_segments", PROPERTY_HINT_NONE, ""), "set_fallback_segments", "get_fallback_segments");
  59. }
  60. Ref<Mesh> OpenXRCompositionLayerEquirect::_create_fallback_mesh() {
  61. Ref<ArrayMesh> mesh;
  62. mesh.instantiate();
  63. Array arrays;
  64. arrays.resize(ArrayMesh::ARRAY_MAX);
  65. Vector<Vector3> vertices;
  66. Vector<Vector3> normals;
  67. Vector<Vector2> uvs;
  68. Vector<int> indices;
  69. float step_horizontal = central_horizontal_angle / fallback_segments;
  70. float step_vertical = (upper_vertical_angle + lower_vertical_angle) / fallback_segments;
  71. float start_horizontal_angle = Math_PI - (central_horizontal_angle / 2.0);
  72. for (uint32_t i = 0; i < fallback_segments + 1; i++) {
  73. for (uint32_t j = 0; j < fallback_segments + 1; j++) {
  74. float horizontal_angle = start_horizontal_angle + (step_horizontal * i);
  75. float vertical_angle = -lower_vertical_angle + (step_vertical * j);
  76. Vector3 vertex(
  77. radius * Math::cos(vertical_angle) * Math::sin(horizontal_angle),
  78. radius * Math::sin(vertical_angle),
  79. radius * Math::cos(vertical_angle) * Math::cos(horizontal_angle));
  80. vertices.push_back(vertex);
  81. normals.push_back(vertex.normalized());
  82. uvs.push_back(Vector2(1.0 - ((float)i / fallback_segments), 1.0 - (float(j) / fallback_segments)));
  83. }
  84. }
  85. for (uint32_t i = 0; i < fallback_segments; i++) {
  86. for (uint32_t j = 0; j < fallback_segments; j++) {
  87. uint32_t index = i * (fallback_segments + 1) + j;
  88. indices.push_back(index);
  89. indices.push_back(index + fallback_segments + 1);
  90. indices.push_back(index + fallback_segments + 2);
  91. indices.push_back(index);
  92. indices.push_back(index + fallback_segments + 2);
  93. indices.push_back(index + 1);
  94. }
  95. }
  96. arrays[ArrayMesh::ARRAY_VERTEX] = vertices;
  97. arrays[ArrayMesh::ARRAY_NORMAL] = normals;
  98. arrays[ArrayMesh::ARRAY_TEX_UV] = uvs;
  99. arrays[ArrayMesh::ARRAY_INDEX] = indices;
  100. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
  101. return mesh;
  102. }
  103. void OpenXRCompositionLayerEquirect::_notification(int p_what) {
  104. switch (p_what) {
  105. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  106. update_transform();
  107. } break;
  108. }
  109. }
  110. void OpenXRCompositionLayerEquirect::update_transform() {
  111. composition_layer.pose = get_openxr_pose();
  112. }
  113. void OpenXRCompositionLayerEquirect::set_radius(float p_radius) {
  114. ERR_FAIL_COND(p_radius <= 0);
  115. radius = p_radius;
  116. composition_layer.radius = radius;
  117. update_fallback_mesh();
  118. }
  119. float OpenXRCompositionLayerEquirect::get_radius() const {
  120. return radius;
  121. }
  122. void OpenXRCompositionLayerEquirect::set_central_horizontal_angle(float p_angle) {
  123. ERR_FAIL_COND(p_angle <= 0);
  124. central_horizontal_angle = p_angle;
  125. composition_layer.centralHorizontalAngle = central_horizontal_angle;
  126. update_fallback_mesh();
  127. }
  128. float OpenXRCompositionLayerEquirect::get_central_horizontal_angle() const {
  129. return central_horizontal_angle;
  130. }
  131. void OpenXRCompositionLayerEquirect::set_upper_vertical_angle(float p_angle) {
  132. ERR_FAIL_COND(p_angle <= 0 || p_angle > (Math_PI / 2.0));
  133. upper_vertical_angle = p_angle;
  134. composition_layer.upperVerticalAngle = p_angle;
  135. update_fallback_mesh();
  136. }
  137. float OpenXRCompositionLayerEquirect::get_upper_vertical_angle() const {
  138. return upper_vertical_angle;
  139. }
  140. void OpenXRCompositionLayerEquirect::set_lower_vertical_angle(float p_angle) {
  141. ERR_FAIL_COND(p_angle <= 0 || p_angle > (Math_PI / 2.0));
  142. lower_vertical_angle = p_angle;
  143. composition_layer.lowerVerticalAngle = -p_angle;
  144. update_fallback_mesh();
  145. }
  146. float OpenXRCompositionLayerEquirect::get_lower_vertical_angle() const {
  147. return lower_vertical_angle;
  148. }
  149. void OpenXRCompositionLayerEquirect::set_fallback_segments(uint32_t p_fallback_segments) {
  150. ERR_FAIL_COND(p_fallback_segments == 0);
  151. fallback_segments = p_fallback_segments;
  152. update_fallback_mesh();
  153. }
  154. uint32_t OpenXRCompositionLayerEquirect::get_fallback_segments() const {
  155. return fallback_segments;
  156. }
  157. Vector2 OpenXRCompositionLayerEquirect::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {
  158. Transform3D equirect_transform = get_global_transform();
  159. Vector3 offset = p_origin - equirect_transform.origin;
  160. float a = p_direction.dot(p_direction);
  161. float b = 2.0 * offset.dot(p_direction);
  162. float c = offset.dot(offset) - (radius * radius);
  163. float discriminant = b * b - 4.0 * a * c;
  164. if (discriminant < 0.0) {
  165. return Vector2(-1.0, -1.0);
  166. }
  167. float t0 = (-b - Math::sqrt(discriminant)) / (2.0 * a);
  168. float t1 = (-b + Math::sqrt(discriminant)) / (2.0 * a);
  169. float t = MAX(t0, t1);
  170. if (t < 0.0) {
  171. return Vector2(-1.0, -1.0);
  172. }
  173. Vector3 intersection = p_origin + p_direction * t;
  174. Basis correction = equirect_transform.basis.inverse();
  175. correction.rotate(Vector3(0.0, 1.0, 0.0), -Math_PI / 2.0);
  176. Vector3 relative_point = correction.xform(intersection - equirect_transform.origin);
  177. float horizontal_intersection_angle = Math::atan2(relative_point.z, relative_point.x);
  178. if (Math::abs(horizontal_intersection_angle) > central_horizontal_angle / 2.0) {
  179. return Vector2(-1.0, -1.0);
  180. }
  181. float vertical_intersection_angle = Math::acos(relative_point.y / radius) - (Math_PI / 2.0);
  182. if (vertical_intersection_angle < 0) {
  183. if (Math::abs(vertical_intersection_angle) > upper_vertical_angle) {
  184. return Vector2(-1.0, -1.0);
  185. }
  186. } else if (vertical_intersection_angle > lower_vertical_angle) {
  187. return Vector2(-1.0, -1.0);
  188. }
  189. // Re-center the intersection angle if the vertical angle is uneven between upper and lower.
  190. if (upper_vertical_angle != lower_vertical_angle) {
  191. vertical_intersection_angle -= (-upper_vertical_angle + lower_vertical_angle) / 2.0;
  192. }
  193. float u = 0.5 + (horizontal_intersection_angle / central_horizontal_angle);
  194. float v = 0.5 + (vertical_intersection_angle / (upper_vertical_angle + lower_vertical_angle));
  195. return Vector2(u, v);
  196. }