Path: blob/master/modules/openxr/scene/openxr_composition_layer_equirect.cpp
21091 views
/**************************************************************************/1/* openxr_composition_layer_equirect.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "openxr_composition_layer_equirect.h"3132#include "../extensions/openxr_composition_layer_extension.h"33#include "../openxr_interface.h"3435#include "scene/resources/mesh.h"3637OpenXRCompositionLayerEquirect::OpenXRCompositionLayerEquirect() {38if (composition_layer_extension) {39XrCompositionLayerEquirect2KHR openxr_composition_layer = {40XR_TYPE_COMPOSITION_LAYER_EQUIRECT2_KHR, // type41nullptr, // next420, // layerFlags43XR_NULL_HANDLE, // space44XR_EYE_VISIBILITY_BOTH, // eyeVisibility45{}, // subImage46{ { 0, 0, 0, 0 }, { 0, 0, 0 } }, // pose47radius, // radius48central_horizontal_angle, // centralHorizontalAngle49upper_vertical_angle, // upperVerticalAngle50-lower_vertical_angle, // lowerVerticalAngle51};52composition_layer = composition_layer_extension->composition_layer_create((XrCompositionLayerBaseHeader *)&openxr_composition_layer);53}54}5556OpenXRCompositionLayerEquirect::~OpenXRCompositionLayerEquirect() {57}5859void OpenXRCompositionLayerEquirect::_bind_methods() {60ClassDB::bind_method(D_METHOD("set_radius", "radius"), &OpenXRCompositionLayerEquirect::set_radius);61ClassDB::bind_method(D_METHOD("get_radius"), &OpenXRCompositionLayerEquirect::get_radius);6263ClassDB::bind_method(D_METHOD("set_central_horizontal_angle", "angle"), &OpenXRCompositionLayerEquirect::set_central_horizontal_angle);64ClassDB::bind_method(D_METHOD("get_central_horizontal_angle"), &OpenXRCompositionLayerEquirect::get_central_horizontal_angle);6566ClassDB::bind_method(D_METHOD("set_upper_vertical_angle", "angle"), &OpenXRCompositionLayerEquirect::set_upper_vertical_angle);67ClassDB::bind_method(D_METHOD("get_upper_vertical_angle"), &OpenXRCompositionLayerEquirect::get_upper_vertical_angle);6869ClassDB::bind_method(D_METHOD("set_lower_vertical_angle", "angle"), &OpenXRCompositionLayerEquirect::set_lower_vertical_angle);70ClassDB::bind_method(D_METHOD("get_lower_vertical_angle"), &OpenXRCompositionLayerEquirect::get_lower_vertical_angle);7172ClassDB::bind_method(D_METHOD("set_fallback_segments", "segments"), &OpenXRCompositionLayerEquirect::set_fallback_segments);73ClassDB::bind_method(D_METHOD("get_fallback_segments"), &OpenXRCompositionLayerEquirect::get_fallback_segments);7475ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_NONE, ""), "set_radius", "get_radius");76ADD_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");77ADD_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");78ADD_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");79ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_segments", PROPERTY_HINT_NONE, ""), "set_fallback_segments", "get_fallback_segments");80}8182Ref<Mesh> OpenXRCompositionLayerEquirect::_create_fallback_mesh() {83Ref<ArrayMesh> mesh;84mesh.instantiate();8586Array arrays;87arrays.resize(ArrayMesh::ARRAY_MAX);8889Vector<Vector3> vertices;90Vector<Vector3> normals;91Vector<Vector2> uvs;92Vector<int> indices;9394float step_horizontal = central_horizontal_angle / fallback_segments;95float step_vertical = (upper_vertical_angle + lower_vertical_angle) / fallback_segments;9697float start_horizontal_angle = Math::PI - (central_horizontal_angle / 2.0);9899for (uint32_t i = 0; i < fallback_segments + 1; i++) {100for (uint32_t j = 0; j < fallback_segments + 1; j++) {101float horizontal_angle = start_horizontal_angle + (step_horizontal * i);102float vertical_angle = -lower_vertical_angle + (step_vertical * j);103104Vector3 vertex(105radius * Math::cos(vertical_angle) * Math::sin(horizontal_angle),106radius * Math::sin(vertical_angle),107radius * Math::cos(vertical_angle) * Math::cos(horizontal_angle));108109vertices.push_back(vertex);110normals.push_back(vertex.normalized());111uvs.push_back(Vector2(1.0 - ((float)i / fallback_segments), 1.0 - (float(j) / fallback_segments)));112}113}114115for (uint32_t i = 0; i < fallback_segments; i++) {116for (uint32_t j = 0; j < fallback_segments; j++) {117uint32_t index = i * (fallback_segments + 1) + j;118indices.push_back(index);119indices.push_back(index + fallback_segments + 1);120indices.push_back(index + fallback_segments + 2);121122indices.push_back(index);123indices.push_back(index + fallback_segments + 2);124indices.push_back(index + 1);125}126}127128arrays[ArrayMesh::ARRAY_VERTEX] = vertices;129arrays[ArrayMesh::ARRAY_NORMAL] = normals;130arrays[ArrayMesh::ARRAY_TEX_UV] = uvs;131arrays[ArrayMesh::ARRAY_INDEX] = indices;132133mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);134return mesh;135}136137void OpenXRCompositionLayerEquirect::set_radius(float p_radius) {138ERR_FAIL_COND(p_radius <= 0);139radius = p_radius;140if (composition_layer_extension) {141composition_layer_extension->composition_layer_set_equirect_radius(composition_layer, p_radius);142}143update_fallback_mesh();144}145146float OpenXRCompositionLayerEquirect::get_radius() const {147return radius;148}149150void OpenXRCompositionLayerEquirect::set_central_horizontal_angle(float p_angle) {151ERR_FAIL_COND(p_angle <= 0);152central_horizontal_angle = p_angle;153if (composition_layer_extension) {154composition_layer_extension->composition_layer_set_equirect_central_horizontal_angle(composition_layer, p_angle);155}156update_fallback_mesh();157}158159float OpenXRCompositionLayerEquirect::get_central_horizontal_angle() const {160return central_horizontal_angle;161}162163void OpenXRCompositionLayerEquirect::set_upper_vertical_angle(float p_angle) {164ERR_FAIL_COND(p_angle <= 0 || p_angle > (Math::PI / 2.0));165upper_vertical_angle = p_angle;166if (composition_layer_extension) {167composition_layer_extension->composition_layer_set_equirect_upper_vertical_angle(composition_layer, p_angle);168}169update_fallback_mesh();170}171172float OpenXRCompositionLayerEquirect::get_upper_vertical_angle() const {173return upper_vertical_angle;174}175176void OpenXRCompositionLayerEquirect::set_lower_vertical_angle(float p_angle) {177ERR_FAIL_COND(p_angle <= 0 || p_angle > (Math::PI / 2.0));178lower_vertical_angle = p_angle;179if (composition_layer_extension) {180composition_layer_extension->composition_layer_set_equirect_lower_vertical_angle(composition_layer, -p_angle);181}182update_fallback_mesh();183}184185float OpenXRCompositionLayerEquirect::get_lower_vertical_angle() const {186return lower_vertical_angle;187}188189void OpenXRCompositionLayerEquirect::set_fallback_segments(uint32_t p_fallback_segments) {190ERR_FAIL_COND(p_fallback_segments == 0);191fallback_segments = p_fallback_segments;192update_fallback_mesh();193}194195uint32_t OpenXRCompositionLayerEquirect::get_fallback_segments() const {196return fallback_segments;197}198199Vector2 OpenXRCompositionLayerEquirect::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {200Transform3D equirect_transform = get_global_transform();201202Vector3 offset = p_origin - equirect_transform.origin;203float a = p_direction.dot(p_direction);204float b = 2.0 * offset.dot(p_direction);205float c = offset.dot(offset) - (radius * radius);206207float discriminant = b * b - 4.0 * a * c;208if (discriminant < 0.0) {209return Vector2(-1.0, -1.0);210}211212float t0 = (-b - Math::sqrt(discriminant)) / (2.0 * a);213float t1 = (-b + Math::sqrt(discriminant)) / (2.0 * a);214float t = MAX(t0, t1);215216if (t < 0.0) {217return Vector2(-1.0, -1.0);218}219Vector3 intersection = p_origin + p_direction * t;220221Basis correction = equirect_transform.basis.inverse();222correction.rotate(Vector3(0.0, 1.0, 0.0), -Math::PI / 2.0);223Vector3 relative_point = correction.xform(intersection - equirect_transform.origin);224225float horizontal_intersection_angle = Math::atan2(relative_point.z, relative_point.x);226if (Math::abs(horizontal_intersection_angle) > central_horizontal_angle / 2.0) {227return Vector2(-1.0, -1.0);228}229230float vertical_intersection_angle = Math::acos(relative_point.y / radius) - (Math::PI / 2.0);231if (vertical_intersection_angle < 0) {232if (Math::abs(vertical_intersection_angle) > upper_vertical_angle) {233return Vector2(-1.0, -1.0);234}235} else if (vertical_intersection_angle > lower_vertical_angle) {236return Vector2(-1.0, -1.0);237}238239// Re-center the intersection angle if the vertical angle is uneven between upper and lower.240if (upper_vertical_angle != lower_vertical_angle) {241vertical_intersection_angle -= (-upper_vertical_angle + lower_vertical_angle) / 2.0;242}243244float u = 0.5 + (horizontal_intersection_angle / central_horizontal_angle);245float v = 0.5 + (vertical_intersection_angle / (upper_vertical_angle + lower_vertical_angle));246247return Vector2(u, v);248}249250251