openxr_composition_layer_quad.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "../openxr_interface.h"
  32. #include "scene/resources/3d/primitive_meshes.h"
  33. OpenXRCompositionLayerQuad::OpenXRCompositionLayerQuad() :
  34. OpenXRCompositionLayer((XrCompositionLayerBaseHeader *)&composition_layer) {
  35. XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerQuad::update_transform));
  36. }
  37. OpenXRCompositionLayerQuad::~OpenXRCompositionLayerQuad() {
  38. }
  39. void OpenXRCompositionLayerQuad::_bind_methods() {
  40. ClassDB::bind_method(D_METHOD("set_quad_size", "size"), &OpenXRCompositionLayerQuad::set_quad_size);
  41. ClassDB::bind_method(D_METHOD("get_quad_size"), &OpenXRCompositionLayerQuad::get_quad_size);
  42. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "quad_size", PROPERTY_HINT_NONE, ""), "set_quad_size", "get_quad_size");
  43. }
  44. Ref<Mesh> OpenXRCompositionLayerQuad::_create_fallback_mesh() {
  45. Ref<QuadMesh> mesh;
  46. mesh.instantiate();
  47. mesh->set_size(quad_size);
  48. return mesh;
  49. }
  50. void OpenXRCompositionLayerQuad::_notification(int p_what) {
  51. switch (p_what) {
  52. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  53. update_transform();
  54. } break;
  55. }
  56. }
  57. void OpenXRCompositionLayerQuad::update_transform() {
  58. composition_layer.pose = get_openxr_pose();
  59. }
  60. void OpenXRCompositionLayerQuad::set_quad_size(const Size2 &p_size) {
  61. quad_size = p_size;
  62. composition_layer.size = { (float)quad_size.x, (float)quad_size.y };
  63. update_fallback_mesh();
  64. }
  65. Size2 OpenXRCompositionLayerQuad::get_quad_size() const {
  66. return quad_size;
  67. }
  68. Vector2 OpenXRCompositionLayerQuad::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {
  69. Transform3D quad_transform = get_global_transform();
  70. Vector3 quad_normal = quad_transform.basis.get_column(2);
  71. float denom = quad_normal.dot(p_direction);
  72. if (Math::abs(denom) > 0.0001) {
  73. Vector3 vector = quad_transform.origin - p_origin;
  74. float t = vector.dot(quad_normal) / denom;
  75. if (t < 0.0) {
  76. return Vector2(-1.0, -1.0);
  77. }
  78. Vector3 intersection = p_origin + p_direction * t;
  79. Vector3 relative_point = intersection - quad_transform.origin;
  80. Vector2 projected_point = Vector2(
  81. relative_point.dot(quad_transform.basis.get_column(0)),
  82. relative_point.dot(quad_transform.basis.get_column(1)));
  83. if (Math::abs(projected_point.x) > quad_size.x / 2.0) {
  84. return Vector2(-1.0, -1.0);
  85. }
  86. if (Math::abs(projected_point.y) > quad_size.y / 2.0) {
  87. return Vector2(-1.0, -1.0);
  88. }
  89. float u = 0.5 + (projected_point.x / quad_size.x);
  90. float v = 1.0 - (0.5 + (projected_point.y / quad_size.y));
  91. return Vector2(u, v);
  92. }
  93. return Vector2(-1.0, -1.0);
  94. }