openxr_composition_layer_quad.cpp 4.9 KB

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