Path: blob/master/modules/openxr/scene/openxr_composition_layer_cylinder.cpp
10278 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 "../openxr_interface.h"3334#include "scene/resources/mesh.h"3536OpenXRCompositionLayerCylinder::OpenXRCompositionLayerCylinder() :37OpenXRCompositionLayer((XrCompositionLayerBaseHeader *)&composition_layer) {38XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerCylinder::update_transform));39}4041OpenXRCompositionLayerCylinder::~OpenXRCompositionLayerCylinder() {42}4344void OpenXRCompositionLayerCylinder::_bind_methods() {45ClassDB::bind_method(D_METHOD("set_radius", "radius"), &OpenXRCompositionLayerCylinder::set_radius);46ClassDB::bind_method(D_METHOD("get_radius"), &OpenXRCompositionLayerCylinder::get_radius);4748ClassDB::bind_method(D_METHOD("set_aspect_ratio", "aspect_ratio"), &OpenXRCompositionLayerCylinder::set_aspect_ratio);49ClassDB::bind_method(D_METHOD("get_aspect_ratio"), &OpenXRCompositionLayerCylinder::get_aspect_ratio);5051ClassDB::bind_method(D_METHOD("set_central_angle", "angle"), &OpenXRCompositionLayerCylinder::set_central_angle);52ClassDB::bind_method(D_METHOD("get_central_angle"), &OpenXRCompositionLayerCylinder::get_central_angle);5354ClassDB::bind_method(D_METHOD("set_fallback_segments", "segments"), &OpenXRCompositionLayerCylinder::set_fallback_segments);55ClassDB::bind_method(D_METHOD("get_fallback_segments"), &OpenXRCompositionLayerCylinder::get_fallback_segments);5657ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_NONE, ""), "set_radius", "get_radius");58ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "aspect_ratio", PROPERTY_HINT_RANGE, "0,100"), "set_aspect_ratio", "get_aspect_ratio");59ADD_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");60ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_segments", PROPERTY_HINT_NONE, ""), "set_fallback_segments", "get_fallback_segments");61}6263Ref<Mesh> OpenXRCompositionLayerCylinder::_create_fallback_mesh() {64Ref<ArrayMesh> mesh;65mesh.instantiate();6667float arc_length = radius * central_angle;68float half_height = ((1.0 / aspect_ratio) * arc_length) / 2.0;6970Array arrays;71arrays.resize(ArrayMesh::ARRAY_MAX);7273Vector<Vector3> vertices;74Vector<Vector3> normals;75Vector<Vector2> uvs;76Vector<int> indices;7778float delta_angle = central_angle / fallback_segments;79float start_angle = (-Math::PI / 2.0) - (central_angle / 2.0);8081for (uint32_t i = 0; i < fallback_segments + 1; i++) {82float current_angle = start_angle + (delta_angle * i);83float x = radius * Math::cos(current_angle);84float z = radius * Math::sin(current_angle);85Vector3 normal(Math::cos(current_angle), 0, Math::sin(current_angle));8687vertices.push_back(Vector3(x, -half_height, z));88normals.push_back(normal);89uvs.push_back(Vector2((float)i / fallback_segments, 1));9091vertices.push_back(Vector3(x, half_height, z));92normals.push_back(normal);93uvs.push_back(Vector2((float)i / fallback_segments, 0));94}9596for (uint32_t i = 0; i < fallback_segments; i++) {97uint32_t index = i * 2;98indices.push_back(index);99indices.push_back(index + 1);100indices.push_back(index + 3);101indices.push_back(index);102indices.push_back(index + 3);103indices.push_back(index + 2);104}105106arrays[ArrayMesh::ARRAY_VERTEX] = vertices;107arrays[ArrayMesh::ARRAY_NORMAL] = normals;108arrays[ArrayMesh::ARRAY_TEX_UV] = uvs;109arrays[ArrayMesh::ARRAY_INDEX] = indices;110111mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);112return mesh;113}114115void OpenXRCompositionLayerCylinder::_notification(int p_what) {116switch (p_what) {117case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {118update_transform();119} break;120}121}122123void OpenXRCompositionLayerCylinder::update_transform() {124composition_layer.pose = get_openxr_pose();125}126127void OpenXRCompositionLayerCylinder::set_radius(float p_radius) {128ERR_FAIL_COND(p_radius <= 0);129radius = p_radius;130composition_layer.radius = radius;131update_fallback_mesh();132}133134float OpenXRCompositionLayerCylinder::get_radius() const {135return radius;136}137138void OpenXRCompositionLayerCylinder::set_aspect_ratio(float p_aspect_ratio) {139ERR_FAIL_COND(p_aspect_ratio <= 0);140aspect_ratio = p_aspect_ratio;141composition_layer.aspectRatio = aspect_ratio;142update_fallback_mesh();143}144145float OpenXRCompositionLayerCylinder::get_aspect_ratio() const {146return aspect_ratio;147}148149void OpenXRCompositionLayerCylinder::set_central_angle(float p_central_angle) {150ERR_FAIL_COND(p_central_angle <= 0);151central_angle = p_central_angle;152composition_layer.centralAngle = central_angle;153update_fallback_mesh();154}155156float OpenXRCompositionLayerCylinder::get_central_angle() const {157return central_angle;158}159160void OpenXRCompositionLayerCylinder::set_fallback_segments(uint32_t p_fallback_segments) {161ERR_FAIL_COND(p_fallback_segments == 0);162fallback_segments = p_fallback_segments;163update_fallback_mesh();164}165166uint32_t OpenXRCompositionLayerCylinder::get_fallback_segments() const {167return fallback_segments;168}169170Vector2 OpenXRCompositionLayerCylinder::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {171Transform3D cylinder_transform = get_global_transform();172Vector3 cylinder_axis = cylinder_transform.basis.get_column(1);173174Vector3 offset = p_origin - cylinder_transform.origin;175float a = p_direction.dot(p_direction - cylinder_axis * p_direction.dot(cylinder_axis));176float b = 2.0 * (p_direction.dot(offset - cylinder_axis * offset.dot(cylinder_axis)));177float c = offset.dot(offset - cylinder_axis * offset.dot(cylinder_axis)) - (radius * radius);178179float discriminant = b * b - 4.0 * a * c;180if (discriminant < 0.0) {181return Vector2(-1.0, -1.0);182}183184float t0 = (-b - Math::sqrt(discriminant)) / (2.0 * a);185float t1 = (-b + Math::sqrt(discriminant)) / (2.0 * a);186float t = MAX(t0, t1);187188if (t < 0.0) {189return Vector2(-1.0, -1.0);190}191Vector3 intersection = p_origin + p_direction * t;192193Basis correction = cylinder_transform.basis.inverse();194correction.rotate(Vector3(0.0, 1.0, 0.0), -Math::PI / 2.0);195Vector3 relative_point = correction.xform(intersection - cylinder_transform.origin);196197Vector2 projected_point = Vector2(relative_point.x, relative_point.z);198float intersection_angle = Math::atan2(projected_point.y, projected_point.x);199if (Math::abs(intersection_angle) > central_angle / 2.0) {200return Vector2(-1.0, -1.0);201}202203float arc_length = radius * central_angle;204float height = aspect_ratio * arc_length;205if (Math::abs(relative_point.y) > height / 2.0) {206return Vector2(-1.0, -1.0);207}208209float u = 0.5 + (intersection_angle / central_angle);210float v = 1.0 - (0.5 + (relative_point.y / height));211212return Vector2(u, v);213}214215216