Path: blob/master/modules/openxr/scene/openxr_composition_layer_quad.cpp
10278 views
/**************************************************************************/1/* openxr_composition_layer_quad.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_quad.h"3132#include "../openxr_interface.h"3334#include "scene/resources/3d/primitive_meshes.h"3536OpenXRCompositionLayerQuad::OpenXRCompositionLayerQuad() :37OpenXRCompositionLayer((XrCompositionLayerBaseHeader *)&composition_layer) {38XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerQuad::update_transform));39}4041OpenXRCompositionLayerQuad::~OpenXRCompositionLayerQuad() {42}4344void OpenXRCompositionLayerQuad::_bind_methods() {45ClassDB::bind_method(D_METHOD("set_quad_size", "size"), &OpenXRCompositionLayerQuad::set_quad_size);46ClassDB::bind_method(D_METHOD("get_quad_size"), &OpenXRCompositionLayerQuad::get_quad_size);4748ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "quad_size", PROPERTY_HINT_NONE, ""), "set_quad_size", "get_quad_size");49}5051Ref<Mesh> OpenXRCompositionLayerQuad::_create_fallback_mesh() {52Ref<QuadMesh> mesh;53mesh.instantiate();54mesh->set_size(quad_size);55return mesh;56}5758void OpenXRCompositionLayerQuad::_notification(int p_what) {59switch (p_what) {60case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {61update_transform();62} break;63}64}6566void OpenXRCompositionLayerQuad::update_transform() {67composition_layer.pose = get_openxr_pose();68}6970void OpenXRCompositionLayerQuad::set_quad_size(const Size2 &p_size) {71quad_size = p_size;72composition_layer.size = { (float)quad_size.x, (float)quad_size.y };73update_fallback_mesh();74}7576Size2 OpenXRCompositionLayerQuad::get_quad_size() const {77return quad_size;78}7980Vector2 OpenXRCompositionLayerQuad::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {81Transform3D quad_transform = get_global_transform();82Vector3 quad_normal = quad_transform.basis.get_column(2);8384float denom = quad_normal.dot(p_direction);85if (Math::abs(denom) > 0.0001) {86Vector3 vector = quad_transform.origin - p_origin;87float t = vector.dot(quad_normal) / denom;88if (t < 0.0) {89return Vector2(-1.0, -1.0);90}91Vector3 intersection = p_origin + p_direction * t;9293Vector3 relative_point = intersection - quad_transform.origin;94Vector2 projected_point = Vector2(95relative_point.dot(quad_transform.basis.get_column(0)),96relative_point.dot(quad_transform.basis.get_column(1)));97if (Math::abs(projected_point.x) > quad_size.x / 2.0) {98return Vector2(-1.0, -1.0);99}100if (Math::abs(projected_point.y) > quad_size.y / 2.0) {101return Vector2(-1.0, -1.0);102}103104float u = 0.5 + (projected_point.x / quad_size.x);105float v = 1.0 - (0.5 + (projected_point.y / quad_size.y));106107return Vector2(u, v);108}109110return Vector2(-1.0, -1.0);111}112113114