openxr_composition_layer_quad.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**************************************************************************/
  2. /* openxr_composition_layer_quad.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_quad.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/3d/primitive_meshes.h"
  37. OpenXRCompositionLayerQuad::OpenXRCompositionLayerQuad() {
  38. composition_layer = {
  39. XR_TYPE_COMPOSITION_LAYER_QUAD, // 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. { (float)quad_size.x, (float)quad_size.y }, // size
  47. };
  48. openxr_layer_provider = memnew(OpenXRViewportCompositionLayerProvider((XrCompositionLayerBaseHeader *)&composition_layer));
  49. }
  50. OpenXRCompositionLayerQuad::~OpenXRCompositionLayerQuad() {
  51. }
  52. void OpenXRCompositionLayerQuad::_bind_methods() {
  53. ClassDB::bind_method(D_METHOD("set_quad_size", "size"), &OpenXRCompositionLayerQuad::set_quad_size);
  54. ClassDB::bind_method(D_METHOD("get_quad_size"), &OpenXRCompositionLayerQuad::get_quad_size);
  55. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "quad_size", PROPERTY_HINT_NONE, ""), "set_quad_size", "get_quad_size");
  56. }
  57. void OpenXRCompositionLayerQuad::_on_openxr_session_begun() {
  58. OpenXRCompositionLayer::_on_openxr_session_begun();
  59. if (openxr_api) {
  60. composition_layer.space = openxr_api->get_play_space();
  61. }
  62. }
  63. Ref<Mesh> OpenXRCompositionLayerQuad::_create_fallback_mesh() {
  64. Ref<QuadMesh> mesh;
  65. mesh.instantiate();
  66. mesh->set_size(quad_size);
  67. return mesh;
  68. }
  69. void OpenXRCompositionLayerQuad::_notification(int p_what) {
  70. switch (p_what) {
  71. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  72. Transform3D transform = get_transform();
  73. Quaternion quat(transform.basis.orthonormalized());
  74. composition_layer.pose.orientation = { (float)quat.x, (float)quat.y, (float)quat.z, (float)quat.w };
  75. composition_layer.pose.position = { (float)transform.origin.x, (float)transform.origin.y, (float)transform.origin.z };
  76. } break;
  77. }
  78. }
  79. void OpenXRCompositionLayerQuad::set_quad_size(const Size2 &p_size) {
  80. quad_size = p_size;
  81. composition_layer.size = { (float)quad_size.x, (float)quad_size.y };
  82. update_fallback_mesh();
  83. }
  84. Size2 OpenXRCompositionLayerQuad::get_quad_size() const {
  85. return quad_size;
  86. }
  87. Vector2 OpenXRCompositionLayerQuad::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {
  88. Transform3D quad_transform = get_global_transform();
  89. Vector3 quad_normal = quad_transform.basis.get_column(2);
  90. float denom = quad_normal.dot(p_direction);
  91. if (Math::abs(denom) > 0.0001) {
  92. Vector3 vector = quad_transform.origin - p_origin;
  93. float t = vector.dot(quad_normal) / denom;
  94. if (t < 0.0) {
  95. return Vector2(-1.0, -1.0);
  96. }
  97. Vector3 intersection = p_origin + p_direction * t;
  98. Vector3 relative_point = intersection - quad_transform.origin;
  99. Vector2 projected_point = Vector2(
  100. relative_point.dot(quad_transform.basis.get_column(0)),
  101. relative_point.dot(quad_transform.basis.get_column(1)));
  102. if (Math::abs(projected_point.x) > quad_size.x / 2.0) {
  103. return Vector2(-1.0, -1.0);
  104. }
  105. if (Math::abs(projected_point.y) > quad_size.y / 2.0) {
  106. return Vector2(-1.0, -1.0);
  107. }
  108. float u = 0.5 + (projected_point.x / quad_size.x);
  109. float v = 1.0 - (0.5 + (projected_point.y / quad_size.y));
  110. return Vector2(u, v);
  111. }
  112. return Vector2(-1.0, -1.0);
  113. }