| 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 | |
| 31 | #include "openxr_composition_layer_quad.h" |
| 32 | |
| 33 | #include "../extensions/openxr_composition_layer_extension.h" |
| 34 | #include "../openxr_interface.h" |
| 35 | |
| 36 | #include "scene/resources/3d/primitive_meshes.h" |
| 37 | |
| 38 | OpenXRCompositionLayerQuad::OpenXRCompositionLayerQuad() { |
| 39 | if (composition_layer_extension) { |
| 40 | XrCompositionLayerQuad openxr_composition_layer = { |
| 41 | XR_TYPE_COMPOSITION_LAYER_QUAD, // type |
| 42 | nullptr, // next |
| 43 | 0, // layerFlags |
| 44 | XR_NULL_HANDLE, // space |
| 45 | XR_EYE_VISIBILITY_BOTH, // eyeVisibility |
| 46 | {}, // subImage |
| 47 | { { 0, 0, 0, 0 }, { 0, 0, 0 } }, // pose |
| 48 | { (float)quad_size.x, (float)quad_size.y }, // size |
| 49 | }; |
| 50 | composition_layer = composition_layer_extension->composition_layer_create((XrCompositionLayerBaseHeader *)&openxr_composition_layer); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | OpenXRCompositionLayerQuad::~OpenXRCompositionLayerQuad() { |
| 55 | } |
| 56 | |
| 57 | void OpenXRCompositionLayerQuad::_bind_methods() { |
| 58 | ClassDB::bind_method(D_METHOD("set_quad_size", "size"), &OpenXRCompositionLayerQuad::set_quad_size); |
| 59 | ClassDB::bind_method(D_METHOD("get_quad_size"), &OpenXRCompositionLayerQuad::get_quad_size); |
| 60 | |
| 61 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "quad_size", PROPERTY_HINT_NONE, ""), "set_quad_size", "get_quad_size"); |
| 62 | } |
| 63 | |
| 64 | Ref<Mesh> OpenXRCompositionLayerQuad::_create_fallback_mesh() { |
| 65 | Ref<QuadMesh> mesh; |
| 66 | mesh.instantiate(); |
| 67 | mesh->set_size(quad_size); |
| 68 | return mesh; |
| 69 | } |
| 70 | |
| 71 | void OpenXRCompositionLayerQuad::set_quad_size(const Size2 &p_size) { |
| 72 | quad_size = p_size; |
| 73 | if (composition_layer_extension) { |
| 74 | composition_layer_extension->composition_layer_set_quad_size(composition_layer, p_size); |
| 75 | } |
| 76 | update_fallback_mesh(); |
| 77 | } |
| 78 | |
| 79 | Size2 OpenXRCompositionLayerQuad::get_quad_size() const { |
| 80 | return quad_size; |
| 81 | } |
| 82 | |
| 83 | Vector2 OpenXRCompositionLayerQuad::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const { |
| 84 | Transform3D quad_transform = get_global_transform(); |
| 85 | Vector3 quad_normal = quad_transform.basis.get_column(2); |
| 86 | |
| 87 | float denom = quad_normal.dot(p_direction); |
| 88 | if (Math::abs(denom) > 0.0001) { |
| 89 | Vector3 vector = quad_transform.origin - p_origin; |
| 90 | float t = vector.dot(quad_normal) / denom; |
| 91 | if (t < 0.0) { |
| 92 | return Vector2(-1.0, -1.0); |
| 93 | } |
| 94 | Vector3 intersection = p_origin + p_direction * t; |
| 95 | |
| 96 | Vector3 relative_point = intersection - quad_transform.origin; |
| 97 | Vector2 projected_point = Vector2( |
| 98 | relative_point.dot(quad_transform.basis.get_column(0)), |
| 99 | relative_point.dot(quad_transform.basis.get_column(1))); |
| 100 | if (Math::abs(projected_point.x) > quad_size.x / 2.0) { |
| 101 | return Vector2(-1.0, -1.0); |
| 102 | } |
| 103 | if (Math::abs(projected_point.y) > quad_size.y / 2.0) { |
| 104 | return Vector2(-1.0, -1.0); |
| 105 | } |
| 106 | |
| 107 | float u = 0.5 + (projected_point.x / quad_size.x); |
| 108 | float v = 1.0 - (0.5 + (projected_point.y / quad_size.y)); |
| 109 | |
| 110 | return Vector2(u, v); |
| 111 | } |
| 112 | |
| 113 | return Vector2(-1.0, -1.0); |
| 114 | } |