openxr_composition_layer_cylinder.cpp 9.5 KB

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