openxr_composition_layer_cylinder.cpp 9.1 KB

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