| 1 | /**************************************************************************/ |
| 2 | /* openxr_composition_layer_equirect.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_equirect.h" |
| 32 | |
| 33 | #include "../extensions/openxr_composition_layer_extension.h" |
| 34 | #include "../openxr_interface.h" |
| 35 | |
| 36 | #include "scene/resources/mesh.h" |
| 37 | |
| 38 | OpenXRCompositionLayerEquirect::OpenXRCompositionLayerEquirect() { |
| 39 | if (composition_layer_extension) { |
| 40 | XrCompositionLayerEquirect2KHR openxr_composition_layer = { |
| 41 | XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR, // 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 | radius, // radius |
| 49 | central_horizontal_angle, // centralHorizontalAngle |
| 50 | upper_vertical_angle, // upperVerticalAngle |
| 51 | -lower_vertical_angle, // lowerVerticalAngle |
| 52 | }; |
| 53 | composition_layer = composition_layer_extension->composition_layer_create((XrCompositionLayerBaseHeader *)&openxr_composition_layer); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | OpenXRCompositionLayerEquirect::~OpenXRCompositionLayerEquirect() { |
| 58 | } |
| 59 | |
| 60 | void OpenXRCompositionLayerEquirect::_bind_methods() { |
| 61 | ClassDB::bind_method(D_METHOD("set_radius", "radius"), &OpenXRCompositionLayerEquirect::set_radius); |
| 62 | ClassDB::bind_method(D_METHOD("get_radius"), &OpenXRCompositionLayerEquirect::get_radius); |
| 63 | |
| 64 | ClassDB::bind_method(D_METHOD("set_central_horizontal_angle", "angle"), &OpenXRCompositionLayerEquirect::set_central_horizontal_angle); |
| 65 | ClassDB::bind_method(D_METHOD("get_central_horizontal_angle"), &OpenXRCompositionLayerEquirect::get_central_horizontal_angle); |
| 66 | |
| 67 | ClassDB::bind_method(D_METHOD("set_upper_vertical_angle", "angle"), &OpenXRCompositionLayerEquirect::set_upper_vertical_angle); |
| 68 | ClassDB::bind_method(D_METHOD("get_upper_vertical_angle"), &OpenXRCompositionLayerEquirect::get_upper_vertical_angle); |
| 69 | |
| 70 | ClassDB::bind_method(D_METHOD("set_lower_vertical_angle", "angle"), &OpenXRCompositionLayerEquirect::set_lower_vertical_angle); |
| 71 | ClassDB::bind_method(D_METHOD("get_lower_vertical_angle"), &OpenXRCompositionLayerEquirect::get_lower_vertical_angle); |
| 72 | |
| 73 | ClassDB::bind_method(D_METHOD("set_fallback_segments", "segments"), &OpenXRCompositionLayerEquirect::set_fallback_segments); |
| 74 | ClassDB::bind_method(D_METHOD("get_fallback_segments"), &OpenXRCompositionLayerEquirect::get_fallback_segments); |
| 75 | |
| 76 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_NONE, ""), "set_radius", "get_radius"); |
| 77 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "central_horizontal_angle", PROPERTY_HINT_RANGE, "0,360,0.1,or_less,or_greater,radians_as_degrees"), "set_central_horizontal_angle", "get_central_horizontal_angle"); |
| 78 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "upper_vertical_angle", PROPERTY_HINT_RANGE, "0,90,0.1,or_less,or_greater,radians_as_degrees"), "set_upper_vertical_angle", "get_upper_vertical_angle"); |
| 79 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lower_vertical_angle", PROPERTY_HINT_RANGE, "0,90,0.1,or_less,or_greater,radians_as_degrees"), "set_lower_vertical_angle", "get_lower_vertical_angle"); |
| 80 | ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_segments", PROPERTY_HINT_NONE, ""), "set_fallback_segments", "get_fallback_segments"); |
| 81 | } |
| 82 | |
| 83 | Ref<Mesh> OpenXRCompositionLayerEquirect::_create_fallback_mesh() { |
| 84 | Ref<ArrayMesh> mesh; |
| 85 | mesh.instantiate(); |
| 86 | |
| 87 | Array arrays; |
| 88 | arrays.resize(ArrayMesh::ARRAY_MAX); |
| 89 | |
| 90 | Vector<Vector3> vertices; |
| 91 | Vector<Vector3> normals; |
| 92 | Vector<Vector2> uvs; |
| 93 | Vector<int> indices; |
| 94 | |
| 95 | float step_horizontal = central_horizontal_angle / fallback_segments; |
| 96 | float step_vertical = (upper_vertical_angle + lower_vertical_angle) / fallback_segments; |
| 97 | |
| 98 | float start_horizontal_angle = Math::PI - (central_horizontal_angle / 2.0); |
| 99 | |
| 100 | for (uint32_t i = 0; i < fallback_segments + 1; i++) { |
| 101 | for (uint32_t j = 0; j < fallback_segments + 1; j++) { |
| 102 | float horizontal_angle = start_horizontal_angle + (step_horizontal * i); |
| 103 | float vertical_angle = -lower_vertical_angle + (step_vertical * j); |
| 104 | |
| 105 | Vector3 vertex( |
| 106 | radius * Math::cos(vertical_angle) * Math::sin(horizontal_angle), |
| 107 | radius * Math::sin(vertical_angle), |
| 108 | radius * Math::cos(vertical_angle) * Math::cos(horizontal_angle)); |
| 109 | |
| 110 | vertices.push_back(vertex); |
| 111 | normals.push_back(vertex.normalized()); |
| 112 | uvs.push_back(Vector2(1.0 - ((float)i / fallback_segments), 1.0 - (float(j) / fallback_segments))); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | for (uint32_t i = 0; i < fallback_segments; i++) { |
| 117 | for (uint32_t j = 0; j < fallback_segments; j++) { |
| 118 | uint32_t index = i * (fallback_segments + 1) + j; |
| 119 | indices.push_back(index); |
| 120 | indices.push_back(index + fallback_segments + 1); |
| 121 | indices.push_back(index + fallback_segments + 2); |
| 122 | |
| 123 | indices.push_back(index); |
| 124 | indices.push_back(index + fallback_segments + 2); |
| 125 | indices.push_back(index + 1); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | arrays[ArrayMesh::ARRAY_VERTEX] = vertices; |
| 130 | arrays[ArrayMesh::ARRAY_NORMAL] = normals; |
| 131 | arrays[ArrayMesh::ARRAY_TEX_UV] = uvs; |
| 132 | arrays[ArrayMesh::ARRAY_INDEX] = indices; |
| 133 | |
| 134 | mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays); |
| 135 | return mesh; |
| 136 | } |
| 137 | |
| 138 | void OpenXRCompositionLayerEquirect::set_radius(float p_radius) { |
| 139 | ERR_FAIL_COND(p_radius <= 0); |
| 140 | radius = p_radius; |
| 141 | if (composition_layer_extension) { |
| 142 | composition_layer_extension->composition_layer_set_equirect_radius(composition_layer, p_radius); |
| 143 | } |
| 144 | update_fallback_mesh(); |
| 145 | } |
| 146 | |
| 147 | float OpenXRCompositionLayerEquirect::get_radius() const { |
| 148 | return radius; |
| 149 | } |
| 150 | |
| 151 | void OpenXRCompositionLayerEquirect::set_central_horizontal_angle(float p_angle) { |
| 152 | ERR_FAIL_COND(p_angle <= 0); |
| 153 | central_horizontal_angle = p_angle; |
| 154 | if (composition_layer_extension) { |
| 155 | composition_layer_extension->composition_layer_set_equirect_central_horizontal_angle(composition_layer, p_angle); |
| 156 | } |
| 157 | update_fallback_mesh(); |
| 158 | } |
| 159 | |
| 160 | float OpenXRCompositionLayerEquirect::get_central_horizontal_angle() const { |
| 161 | return central_horizontal_angle; |
| 162 | } |
| 163 | |
| 164 | void OpenXRCompositionLayerEquirect::set_upper_vertical_angle(float p_angle) { |
| 165 | ERR_FAIL_COND(p_angle <= 0 || p_angle > (Math::PI / 2.0)); |
| 166 | upper_vertical_angle = p_angle; |
| 167 | if (composition_layer_extension) { |
| 168 | composition_layer_extension->composition_layer_set_equirect_upper_vertical_angle(composition_layer, p_angle); |
| 169 | } |
| 170 | update_fallback_mesh(); |
| 171 | } |
| 172 | |
| 173 | float OpenXRCompositionLayerEquirect::get_upper_vertical_angle() const { |
| 174 | return upper_vertical_angle; |
| 175 | } |
| 176 | |
| 177 | void OpenXRCompositionLayerEquirect::set_lower_vertical_angle(float p_angle) { |
| 178 | ERR_FAIL_COND(p_angle <= 0 || p_angle > (Math::PI / 2.0)); |
| 179 | lower_vertical_angle = p_angle; |
| 180 | if (composition_layer_extension) { |
| 181 | composition_layer_extension->composition_layer_set_equirect_lower_vertical_angle(composition_layer, -p_angle); |
| 182 | } |
| 183 | update_fallback_mesh(); |
| 184 | } |
| 185 | |
| 186 | float OpenXRCompositionLayerEquirect::get_lower_vertical_angle() const { |
| 187 | return lower_vertical_angle; |
| 188 | } |
| 189 | |
| 190 | void OpenXRCompositionLayerEquirect::set_fallback_segments(uint32_t p_fallback_segments) { |
| 191 | ERR_FAIL_COND(p_fallback_segments == 0); |
| 192 | fallback_segments = p_fallback_segments; |
| 193 | update_fallback_mesh(); |
| 194 | } |
| 195 | |
| 196 | uint32_t OpenXRCompositionLayerEquirect::get_fallback_segments() const { |
| 197 | return fallback_segments; |
| 198 | } |
| 199 | |
| 200 | Vector2 OpenXRCompositionLayerEquirect::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const { |
| 201 | Transform3D equirect_transform = get_global_transform(); |
| 202 | |
| 203 | Vector3 offset = p_origin - equirect_transform.origin; |
| 204 | float a = p_direction.dot(p_direction); |
| 205 | float b = 2.0 * offset.dot(p_direction); |
| 206 | float c = offset.dot(offset) - (radius * radius); |
| 207 | |
| 208 | float discriminant = b * b - 4.0 * a * c; |
| 209 | if (discriminant < 0.0) { |
| 210 | return Vector2(-1.0, -1.0); |
| 211 | } |
| 212 | |
| 213 | float t0 = (-b - Math::sqrt(discriminant)) / (2.0 * a); |
| 214 | float t1 = (-b + Math::sqrt(discriminant)) / (2.0 * a); |
| 215 | float t = MAX(t0, t1); |
| 216 | |
| 217 | if (t < 0.0) { |
| 218 | return Vector2(-1.0, -1.0); |
| 219 | } |
| 220 | Vector3 intersection = p_origin + p_direction * t; |
| 221 | |
| 222 | Basis correction = equirect_transform.basis.inverse(); |
| 223 | correction.rotate(Vector3(0.0, 1.0, 0.0), -Math::PI / 2.0); |
| 224 | Vector3 relative_point = correction.xform(intersection - equirect_transform.origin); |
| 225 | |
| 226 | float horizontal_intersection_angle = Math::atan2(relative_point.z, relative_point.x); |
| 227 | if (Math::abs(horizontal_intersection_angle) > central_horizontal_angle / 2.0) { |
| 228 | return Vector2(-1.0, -1.0); |
| 229 | } |
| 230 | |
| 231 | float vertical_intersection_angle = Math::acos(relative_point.y / radius) - (Math::PI / 2.0); |
| 232 | if (vertical_intersection_angle < 0) { |
| 233 | if (Math::abs(vertical_intersection_angle) > upper_vertical_angle) { |
| 234 | return Vector2(-1.0, -1.0); |
| 235 | } |
| 236 | } else if (vertical_intersection_angle > lower_vertical_angle) { |
| 237 | return Vector2(-1.0, -1.0); |
| 238 | } |
| 239 | |
| 240 | // Re-center the intersection angle if the vertical angle is uneven between upper and lower. |
| 241 | if (upper_vertical_angle != lower_vertical_angle) { |
| 242 | vertical_intersection_angle -= (-upper_vertical_angle + lower_vertical_angle) / 2.0; |
| 243 | } |
| 244 | |
| 245 | float u = 0.5 + (horizontal_intersection_angle / central_horizontal_angle); |
| 246 | float v = 0.5 + (vertical_intersection_angle / (upper_vertical_angle + lower_vertical_angle)); |
| 247 | |
| 248 | return Vector2(u, v); |
| 249 | } |