openxr_composition_layer_cylinder.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**************************************************************************/
  2. /* openxr_composition_layer_cylinder.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_cylinder.h"
  31. #include "../openxr_interface.h"
  32. #include "scene/resources/mesh.h"
  33. OpenXRCompositionLayerCylinder::OpenXRCompositionLayerCylinder() :
  34. OpenXRCompositionLayer((XrCompositionLayerBaseHeader *)&composition_layer) {
  35. XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerCylinder::update_transform));
  36. }
  37. OpenXRCompositionLayerCylinder::~OpenXRCompositionLayerCylinder() {
  38. }
  39. void OpenXRCompositionLayerCylinder::_bind_methods() {
  40. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &OpenXRCompositionLayerCylinder::set_radius);
  41. ClassDB::bind_method(D_METHOD("get_radius"), &OpenXRCompositionLayerCylinder::get_radius);
  42. ClassDB::bind_method(D_METHOD("set_aspect_ratio", "aspect_ratio"), &OpenXRCompositionLayerCylinder::set_aspect_ratio);
  43. ClassDB::bind_method(D_METHOD("get_aspect_ratio"), &OpenXRCompositionLayerCylinder::get_aspect_ratio);
  44. ClassDB::bind_method(D_METHOD("set_central_angle", "angle"), &OpenXRCompositionLayerCylinder::set_central_angle);
  45. ClassDB::bind_method(D_METHOD("get_central_angle"), &OpenXRCompositionLayerCylinder::get_central_angle);
  46. ClassDB::bind_method(D_METHOD("set_fallback_segments", "segments"), &OpenXRCompositionLayerCylinder::set_fallback_segments);
  47. ClassDB::bind_method(D_METHOD("get_fallback_segments"), &OpenXRCompositionLayerCylinder::get_fallback_segments);
  48. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_NONE, ""), "set_radius", "get_radius");
  49. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "aspect_ratio", PROPERTY_HINT_RANGE, "0,100"), "set_aspect_ratio", "get_aspect_ratio");
  50. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "central_angle", PROPERTY_HINT_RANGE, "0,360,0.1,or_less,or_greater,radians_as_degrees"), "set_central_angle", "get_central_angle");
  51. ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_segments", PROPERTY_HINT_NONE, ""), "set_fallback_segments", "get_fallback_segments");
  52. }
  53. Ref<Mesh> OpenXRCompositionLayerCylinder::_create_fallback_mesh() {
  54. Ref<ArrayMesh> mesh;
  55. mesh.instantiate();
  56. float arc_length = radius * central_angle;
  57. float half_height = ((1.0 / aspect_ratio) * arc_length) / 2.0;
  58. Array arrays;
  59. arrays.resize(ArrayMesh::ARRAY_MAX);
  60. Vector<Vector3> vertices;
  61. Vector<Vector3> normals;
  62. Vector<Vector2> uvs;
  63. Vector<int> indices;
  64. float delta_angle = central_angle / fallback_segments;
  65. float start_angle = (-Math_PI / 2.0) - (central_angle / 2.0);
  66. for (uint32_t i = 0; i < fallback_segments + 1; i++) {
  67. float current_angle = start_angle + (delta_angle * i);
  68. float x = radius * Math::cos(current_angle);
  69. float z = radius * Math::sin(current_angle);
  70. Vector3 normal(Math::cos(current_angle), 0, Math::sin(current_angle));
  71. vertices.push_back(Vector3(x, -half_height, z));
  72. normals.push_back(normal);
  73. uvs.push_back(Vector2((float)i / fallback_segments, 1));
  74. vertices.push_back(Vector3(x, half_height, z));
  75. normals.push_back(normal);
  76. uvs.push_back(Vector2((float)i / fallback_segments, 0));
  77. }
  78. for (uint32_t i = 0; i < fallback_segments; i++) {
  79. uint32_t index = i * 2;
  80. indices.push_back(index);
  81. indices.push_back(index + 1);
  82. indices.push_back(index + 3);
  83. indices.push_back(index);
  84. indices.push_back(index + 3);
  85. indices.push_back(index + 2);
  86. }
  87. arrays[ArrayMesh::ARRAY_VERTEX] = vertices;
  88. arrays[ArrayMesh::ARRAY_NORMAL] = normals;
  89. arrays[ArrayMesh::ARRAY_TEX_UV] = uvs;
  90. arrays[ArrayMesh::ARRAY_INDEX] = indices;
  91. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
  92. return mesh;
  93. }
  94. void OpenXRCompositionLayerCylinder::_notification(int p_what) {
  95. switch (p_what) {
  96. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  97. update_transform();
  98. } break;
  99. }
  100. }
  101. void OpenXRCompositionLayerCylinder::update_transform() {
  102. composition_layer.pose = get_openxr_pose();
  103. }
  104. void OpenXRCompositionLayerCylinder::set_radius(float p_radius) {
  105. ERR_FAIL_COND(p_radius <= 0);
  106. radius = p_radius;
  107. composition_layer.radius = radius;
  108. update_fallback_mesh();
  109. }
  110. float OpenXRCompositionLayerCylinder::get_radius() const {
  111. return radius;
  112. }
  113. void OpenXRCompositionLayerCylinder::set_aspect_ratio(float p_aspect_ratio) {
  114. ERR_FAIL_COND(p_aspect_ratio <= 0);
  115. aspect_ratio = p_aspect_ratio;
  116. composition_layer.aspectRatio = aspect_ratio;
  117. update_fallback_mesh();
  118. }
  119. float OpenXRCompositionLayerCylinder::get_aspect_ratio() const {
  120. return aspect_ratio;
  121. }
  122. void OpenXRCompositionLayerCylinder::set_central_angle(float p_central_angle) {
  123. ERR_FAIL_COND(p_central_angle <= 0);
  124. central_angle = p_central_angle;
  125. composition_layer.centralAngle = central_angle;
  126. update_fallback_mesh();
  127. }
  128. float OpenXRCompositionLayerCylinder::get_central_angle() const {
  129. return central_angle;
  130. }
  131. void OpenXRCompositionLayerCylinder::set_fallback_segments(uint32_t p_fallback_segments) {
  132. ERR_FAIL_COND(p_fallback_segments == 0);
  133. fallback_segments = p_fallback_segments;
  134. update_fallback_mesh();
  135. }
  136. uint32_t OpenXRCompositionLayerCylinder::get_fallback_segments() const {
  137. return fallback_segments;
  138. }
  139. Vector2 OpenXRCompositionLayerCylinder::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {
  140. Transform3D cylinder_transform = get_global_transform();
  141. Vector3 cylinder_axis = cylinder_transform.basis.get_column(1);
  142. Vector3 offset = p_origin - cylinder_transform.origin;
  143. float a = p_direction.dot(p_direction - cylinder_axis * p_direction.dot(cylinder_axis));
  144. float b = 2.0 * (p_direction.dot(offset - cylinder_axis * offset.dot(cylinder_axis)));
  145. float c = offset.dot(offset - cylinder_axis * offset.dot(cylinder_axis)) - (radius * radius);
  146. float discriminant = b * b - 4.0 * a * c;
  147. if (discriminant < 0.0) {
  148. return Vector2(-1.0, -1.0);
  149. }
  150. float t0 = (-b - Math::sqrt(discriminant)) / (2.0 * a);
  151. float t1 = (-b + Math::sqrt(discriminant)) / (2.0 * a);
  152. float t = MAX(t0, t1);
  153. if (t < 0.0) {
  154. return Vector2(-1.0, -1.0);
  155. }
  156. Vector3 intersection = p_origin + p_direction * t;
  157. Basis correction = cylinder_transform.basis.inverse();
  158. correction.rotate(Vector3(0.0, 1.0, 0.0), -Math_PI / 2.0);
  159. Vector3 relative_point = correction.xform(intersection - cylinder_transform.origin);
  160. Vector2 projected_point = Vector2(relative_point.x, relative_point.z);
  161. float intersection_angle = Math::atan2(projected_point.y, projected_point.x);
  162. if (Math::abs(intersection_angle) > central_angle / 2.0) {
  163. return Vector2(-1.0, -1.0);
  164. }
  165. float arc_length = radius * central_angle;
  166. float height = aspect_ratio * arc_length;
  167. if (Math::abs(relative_point.y) > height / 2.0) {
  168. return Vector2(-1.0, -1.0);
  169. }
  170. float u = 0.5 + (intersection_angle / central_angle);
  171. float v = 1.0 - (0.5 + (relative_point.y / height));
  172. return Vector2(u, v);
  173. }