| 1 | /**************************************************************************/ |
| 2 | /* openxr_composition_layer_cylinder.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_cylinder.h" |
| 32 | |
| 33 | #include "../extensions/openxr_composition_layer_extension.h" |
| 34 | #include "../openxr_interface.h" |
| 35 | |
| 36 | #include "scene/resources/mesh.h" |
| 37 | |
| 38 | OpenXRCompositionLayerCylinder::OpenXRCompositionLayerCylinder() { |
| 39 | if (composition_layer_extension) { |
| 40 | XrCompositionLayerCylinderKHR openxr_composition_layer = { |
| 41 | XR_TYPE_COMPOSITION_LAYER_CYLINDER_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_angle, // centralAngle |
| 50 | aspect_ratio, // aspectRatio |
| 51 | }; |
| 52 | composition_layer = composition_layer_extension->composition_layer_create((XrCompositionLayerBaseHeader *)&openxr_composition_layer); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | OpenXRCompositionLayerCylinder::~OpenXRCompositionLayerCylinder() { |
| 57 | } |
| 58 | |
| 59 | void OpenXRCompositionLayerCylinder::_bind_methods() { |
| 60 | ClassDB::bind_method(D_METHOD("set_radius", "radius"), &OpenXRCompositionLayerCylinder::set_radius); |
| 61 | ClassDB::bind_method(D_METHOD("get_radius"), &OpenXRCompositionLayerCylinder::get_radius); |
| 62 | |
| 63 | ClassDB::bind_method(D_METHOD("set_aspect_ratio", "aspect_ratio"), &OpenXRCompositionLayerCylinder::set_aspect_ratio); |
| 64 | ClassDB::bind_method(D_METHOD("get_aspect_ratio"), &OpenXRCompositionLayerCylinder::get_aspect_ratio); |
| 65 | |
| 66 | ClassDB::bind_method(D_METHOD("set_central_angle", "angle"), &OpenXRCompositionLayerCylinder::set_central_angle); |
| 67 | ClassDB::bind_method(D_METHOD("get_central_angle"), &OpenXRCompositionLayerCylinder::get_central_angle); |
| 68 | |
| 69 | ClassDB::bind_method(D_METHOD("set_fallback_segments", "segments"), &OpenXRCompositionLayerCylinder::set_fallback_segments); |
| 70 | ClassDB::bind_method(D_METHOD("get_fallback_segments"), &OpenXRCompositionLayerCylinder::get_fallback_segments); |
| 71 | |
| 72 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_NONE, ""), "set_radius", "get_radius"); |
| 73 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "aspect_ratio", PROPERTY_HINT_RANGE, "0,100"), "set_aspect_ratio", "get_aspect_ratio"); |
| 74 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "central_angle", PROPERTY_HINT_RANGE, "0,360,0.1,or_less,or_greater,radians_as_degrees"), "set_central_angle", "get_central_angle"); |
| 75 | ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_segments", PROPERTY_HINT_NONE, ""), "set_fallback_segments", "get_fallback_segments"); |
| 76 | } |
| 77 | |
| 78 | Ref<Mesh> OpenXRCompositionLayerCylinder::_create_fallback_mesh() { |
| 79 | Ref<ArrayMesh> mesh; |
| 80 | mesh.instantiate(); |
| 81 | |
| 82 | float arc_length = radius * central_angle; |
| 83 | float half_height = ((1.0 / aspect_ratio) * arc_length) / 2.0; |
| 84 | |
| 85 | Array arrays; |
| 86 | arrays.resize(ArrayMesh::ARRAY_MAX); |
| 87 | |
| 88 | Vector<Vector3> vertices; |
| 89 | Vector<Vector3> normals; |
| 90 | Vector<Vector2> uvs; |
| 91 | Vector<int> indices; |
| 92 | |
| 93 | float delta_angle = central_angle / fallback_segments; |
| 94 | float start_angle = (-Math::PI / 2.0) - (central_angle / 2.0); |
| 95 | |
| 96 | for (uint32_t i = 0; i < fallback_segments + 1; i++) { |
| 97 | float current_angle = start_angle + (delta_angle * i); |
| 98 | float x = radius * Math::cos(current_angle); |
| 99 | float z = radius * Math::sin(current_angle); |
| 100 | Vector3 normal(Math::cos(current_angle), 0, Math::sin(current_angle)); |
| 101 | |
| 102 | vertices.push_back(Vector3(x, -half_height, z)); |
| 103 | normals.push_back(normal); |
| 104 | uvs.push_back(Vector2((float)i / fallback_segments, 1)); |
| 105 | |
| 106 | vertices.push_back(Vector3(x, half_height, z)); |
| 107 | normals.push_back(normal); |
| 108 | uvs.push_back(Vector2((float)i / fallback_segments, 0)); |
| 109 | } |
| 110 | |
| 111 | for (uint32_t i = 0; i < fallback_segments; i++) { |
| 112 | uint32_t index = i * 2; |
| 113 | indices.push_back(index); |
| 114 | indices.push_back(index + 1); |
| 115 | indices.push_back(index + 3); |
| 116 | indices.push_back(index); |
| 117 | indices.push_back(index + 3); |
| 118 | indices.push_back(index + 2); |
| 119 | } |
| 120 | |
| 121 | arrays[ArrayMesh::ARRAY_VERTEX] = vertices; |
| 122 | arrays[ArrayMesh::ARRAY_NORMAL] = normals; |
| 123 | arrays[ArrayMesh::ARRAY_TEX_UV] = uvs; |
| 124 | arrays[ArrayMesh::ARRAY_INDEX] = indices; |
| 125 | |
| 126 | mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays); |
| 127 | return mesh; |
| 128 | } |
| 129 | |
| 130 | void OpenXRCompositionLayerCylinder::set_radius(float p_radius) { |
| 131 | ERR_FAIL_COND(p_radius <= 0); |
| 132 | radius = p_radius; |
| 133 | if (composition_layer_extension) { |
| 134 | composition_layer_extension->composition_layer_set_cylinder_radius(composition_layer, p_radius); |
| 135 | } |
| 136 | update_fallback_mesh(); |
| 137 | } |
| 138 | |
| 139 | float OpenXRCompositionLayerCylinder::get_radius() const { |
| 140 | return radius; |
| 141 | } |
| 142 | |
| 143 | void OpenXRCompositionLayerCylinder::set_aspect_ratio(float p_aspect_ratio) { |
| 144 | ERR_FAIL_COND(p_aspect_ratio <= 0); |
| 145 | aspect_ratio = p_aspect_ratio; |
| 146 | if (composition_layer_extension) { |
| 147 | composition_layer_extension->composition_layer_set_cylinder_aspect_ratio(composition_layer, p_aspect_ratio); |
| 148 | } |
| 149 | update_fallback_mesh(); |
| 150 | } |
| 151 | |
| 152 | float OpenXRCompositionLayerCylinder::get_aspect_ratio() const { |
| 153 | return aspect_ratio; |
| 154 | } |
| 155 | |
| 156 | void OpenXRCompositionLayerCylinder::set_central_angle(float p_central_angle) { |
| 157 | ERR_FAIL_COND(p_central_angle <= 0); |
| 158 | central_angle = p_central_angle; |
| 159 | if (composition_layer_extension) { |
| 160 | composition_layer_extension->composition_layer_set_cylinder_central_angle(composition_layer, p_central_angle); |
| 161 | } |
| 162 | update_fallback_mesh(); |
| 163 | } |
| 164 | |
| 165 | float OpenXRCompositionLayerCylinder::get_central_angle() const { |
| 166 | return central_angle; |
| 167 | } |
| 168 | |
| 169 | void OpenXRCompositionLayerCylinder::set_fallback_segments(uint32_t p_fallback_segments) { |
| 170 | ERR_FAIL_COND(p_fallback_segments == 0); |
| 171 | fallback_segments = p_fallback_segments; |
| 172 | update_fallback_mesh(); |
| 173 | } |
| 174 | |
| 175 | uint32_t OpenXRCompositionLayerCylinder::get_fallback_segments() const { |
| 176 | return fallback_segments; |
| 177 | } |
| 178 | |
| 179 | Vector2 OpenXRCompositionLayerCylinder::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const { |
| 180 | Transform3D cylinder_transform = get_global_transform(); |
| 181 | Vector3 cylinder_axis = cylinder_transform.basis.get_column(1); |
| 182 | |
| 183 | Vector3 offset = p_origin - cylinder_transform.origin; |
| 184 | float a = p_direction.dot(p_direction - cylinder_axis * p_direction.dot(cylinder_axis)); |
| 185 | float b = 2.0 * (p_direction.dot(offset - cylinder_axis * offset.dot(cylinder_axis))); |
| 186 | float c = offset.dot(offset - cylinder_axis * offset.dot(cylinder_axis)) - (radius * radius); |
| 187 | |
| 188 | float discriminant = b * b - 4.0 * a * c; |
| 189 | if (discriminant < 0.0) { |
| 190 | return Vector2(-1.0, -1.0); |
| 191 | } |
| 192 | |
| 193 | float t0 = (-b - Math::sqrt(discriminant)) / (2.0 * a); |
| 194 | float t1 = (-b + Math::sqrt(discriminant)) / (2.0 * a); |
| 195 | float t = MAX(t0, t1); |
| 196 | |
| 197 | if (t < 0.0) { |
| 198 | return Vector2(-1.0, -1.0); |
| 199 | } |
| 200 | Vector3 intersection = p_origin + p_direction * t; |
| 201 | |
| 202 | Basis correction = cylinder_transform.basis.inverse(); |
| 203 | correction.rotate(Vector3(0.0, 1.0, 0.0), -Math::PI / 2.0); |
| 204 | Vector3 relative_point = correction.xform(intersection - cylinder_transform.origin); |
| 205 | |
| 206 | Vector2 projected_point = Vector2(relative_point.x, relative_point.z); |
| 207 | float intersection_angle = Math::atan2(projected_point.y, projected_point.x); |
| 208 | if (Math::abs(intersection_angle) > central_angle / 2.0) { |
| 209 | return Vector2(-1.0, -1.0); |
| 210 | } |
| 211 | |
| 212 | float arc_length = radius * central_angle; |
| 213 | float height = aspect_ratio * arc_length; |
| 214 | if (Math::abs(relative_point.y) > height / 2.0) { |
| 215 | return Vector2(-1.0, -1.0); |
| 216 | } |
| 217 | |
| 218 | float u = 0.5 + (intersection_angle / central_angle); |
| 219 | float v = 1.0 - (0.5 + (relative_point.y / height)); |
| 220 | |
| 221 | return Vector2(u, v); |
| 222 | } |