Path: blob/master/modules/openxr/scene/openxr_composition_layer_cylinder.cpp
21689 views
/**************************************************************************/1/* openxr_composition_layer_cylinder.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_cylinder.h"3132#include "../extensions/openxr_composition_layer_extension.h"33#include "../openxr_interface.h"3435#include "scene/resources/mesh.h"3637OpenXRCompositionLayerCylinder::OpenXRCompositionLayerCylinder() {38if (composition_layer_extension) {39XrCompositionLayerCylinderKHR openxr_composition_layer = {40XR_TYPE_COMPOSITION_LAYER_CYLINDER_KHR, // type41nullptr, // next420, // layerFlags43XR_NULL_HANDLE, // space44XR_EYE_VISIBILITY_BOTH, // eyeVisibility45{}, // subImage46{ { 0, 0, 0, 0 }, { 0, 0, 0 } }, // pose47radius, // radius48central_angle, // centralAngle49aspect_ratio, // aspectRatio50};51composition_layer = composition_layer_extension->composition_layer_create((XrCompositionLayerBaseHeader *)&openxr_composition_layer);52}53}5455OpenXRCompositionLayerCylinder::~OpenXRCompositionLayerCylinder() {56}5758void OpenXRCompositionLayerCylinder::_bind_methods() {59ClassDB::bind_method(D_METHOD("set_radius", "radius"), &OpenXRCompositionLayerCylinder::set_radius);60ClassDB::bind_method(D_METHOD("get_radius"), &OpenXRCompositionLayerCylinder::get_radius);6162ClassDB::bind_method(D_METHOD("set_aspect_ratio", "aspect_ratio"), &OpenXRCompositionLayerCylinder::set_aspect_ratio);63ClassDB::bind_method(D_METHOD("get_aspect_ratio"), &OpenXRCompositionLayerCylinder::get_aspect_ratio);6465ClassDB::bind_method(D_METHOD("set_central_angle", "angle"), &OpenXRCompositionLayerCylinder::set_central_angle);66ClassDB::bind_method(D_METHOD("get_central_angle"), &OpenXRCompositionLayerCylinder::get_central_angle);6768ClassDB::bind_method(D_METHOD("set_fallback_segments", "segments"), &OpenXRCompositionLayerCylinder::set_fallback_segments);69ClassDB::bind_method(D_METHOD("get_fallback_segments"), &OpenXRCompositionLayerCylinder::get_fallback_segments);7071ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_NONE, ""), "set_radius", "get_radius");72ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "aspect_ratio", PROPERTY_HINT_RANGE, "0,100"), "set_aspect_ratio", "get_aspect_ratio");73ADD_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");74ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_segments", PROPERTY_HINT_NONE, ""), "set_fallback_segments", "get_fallback_segments");75}7677Ref<Mesh> OpenXRCompositionLayerCylinder::_create_fallback_mesh() {78Ref<ArrayMesh> mesh;79mesh.instantiate();8081float arc_length = radius * central_angle;82float half_height = ((1.0 / aspect_ratio) * arc_length) / 2.0;8384Array arrays;85arrays.resize(ArrayMesh::ARRAY_MAX);8687Vector<Vector3> vertices;88Vector<Vector3> normals;89Vector<Vector2> uvs;90Vector<int> indices;9192float delta_angle = central_angle / fallback_segments;93float start_angle = (-Math::PI / 2.0) - (central_angle / 2.0);9495for (uint32_t i = 0; i < fallback_segments + 1; i++) {96float current_angle = start_angle + (delta_angle * i);97float x = radius * Math::cos(current_angle);98float z = radius * Math::sin(current_angle);99Vector3 normal(Math::cos(current_angle), 0, Math::sin(current_angle));100101vertices.push_back(Vector3(x, -half_height, z));102normals.push_back(normal);103uvs.push_back(Vector2((float)i / fallback_segments, 1));104105vertices.push_back(Vector3(x, half_height, z));106normals.push_back(normal);107uvs.push_back(Vector2((float)i / fallback_segments, 0));108}109110for (uint32_t i = 0; i < fallback_segments; i++) {111uint32_t index = i * 2;112indices.push_back(index);113indices.push_back(index + 1);114indices.push_back(index + 3);115indices.push_back(index);116indices.push_back(index + 3);117indices.push_back(index + 2);118}119120arrays[ArrayMesh::ARRAY_VERTEX] = vertices;121arrays[ArrayMesh::ARRAY_NORMAL] = normals;122arrays[ArrayMesh::ARRAY_TEX_UV] = uvs;123arrays[ArrayMesh::ARRAY_INDEX] = indices;124125mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);126return mesh;127}128129void OpenXRCompositionLayerCylinder::set_radius(float p_radius) {130ERR_FAIL_COND(p_radius <= 0);131radius = p_radius;132if (composition_layer_extension) {133composition_layer_extension->composition_layer_set_cylinder_radius(composition_layer, p_radius);134}135update_fallback_mesh();136}137138float OpenXRCompositionLayerCylinder::get_radius() const {139return radius;140}141142void OpenXRCompositionLayerCylinder::set_aspect_ratio(float p_aspect_ratio) {143ERR_FAIL_COND(p_aspect_ratio <= 0);144aspect_ratio = p_aspect_ratio;145if (composition_layer_extension) {146composition_layer_extension->composition_layer_set_cylinder_aspect_ratio(composition_layer, p_aspect_ratio);147}148update_fallback_mesh();149}150151float OpenXRCompositionLayerCylinder::get_aspect_ratio() const {152return aspect_ratio;153}154155void OpenXRCompositionLayerCylinder::set_central_angle(float p_central_angle) {156ERR_FAIL_COND(p_central_angle <= 0);157central_angle = p_central_angle;158if (composition_layer_extension) {159composition_layer_extension->composition_layer_set_cylinder_central_angle(composition_layer, p_central_angle);160}161update_fallback_mesh();162}163164float OpenXRCompositionLayerCylinder::get_central_angle() const {165return central_angle;166}167168void OpenXRCompositionLayerCylinder::set_fallback_segments(uint32_t p_fallback_segments) {169ERR_FAIL_COND(p_fallback_segments == 0);170fallback_segments = p_fallback_segments;171update_fallback_mesh();172}173174uint32_t OpenXRCompositionLayerCylinder::get_fallback_segments() const {175return fallback_segments;176}177178Vector2 OpenXRCompositionLayerCylinder::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {179Transform3D cylinder_transform = get_global_transform();180Vector3 cylinder_axis = cylinder_transform.basis.get_column(1);181182Vector3 offset = p_origin - cylinder_transform.origin;183float a = p_direction.dot(p_direction - cylinder_axis * p_direction.dot(cylinder_axis));184float b = 2.0 * (p_direction.dot(offset - cylinder_axis * offset.dot(cylinder_axis)));185float c = offset.dot(offset - cylinder_axis * offset.dot(cylinder_axis)) - (radius * radius);186187float discriminant = b * b - 4.0 * a * c;188if (discriminant < 0.0) {189return Vector2(-1.0, -1.0);190}191192float t0 = (-b - Math::sqrt(discriminant)) / (2.0 * a);193float t1 = (-b + Math::sqrt(discriminant)) / (2.0 * a);194float t = MAX(t0, t1);195196if (t < 0.0) {197return Vector2(-1.0, -1.0);198}199Vector3 intersection = p_origin + p_direction * t;200201Basis correction = cylinder_transform.basis.inverse();202correction.rotate(Vector3(0.0, 1.0, 0.0), -Math::PI / 2.0);203Vector3 relative_point = correction.xform(intersection - cylinder_transform.origin);204205Vector2 projected_point = Vector2(relative_point.x, relative_point.z);206float intersection_angle = Math::atan2(projected_point.y, projected_point.x);207if (Math::abs(intersection_angle) > central_angle / 2.0) {208return Vector2(-1.0, -1.0);209}210211float arc_length = radius * central_angle;212float height = aspect_ratio * arc_length;213if (Math::abs(relative_point.y) > height / 2.0) {214return Vector2(-1.0, -1.0);215}216217float u = 0.5 + (intersection_angle / central_angle);218float v = 1.0 - (0.5 + (relative_point.y / height));219220return Vector2(u, v);221}222223224