Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/scene/openxr_composition_layer_quad.cpp
10278 views
1
/**************************************************************************/
2
/* openxr_composition_layer_quad.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_quad.h"
32
33
#include "../openxr_interface.h"
34
35
#include "scene/resources/3d/primitive_meshes.h"
36
37
OpenXRCompositionLayerQuad::OpenXRCompositionLayerQuad() :
38
OpenXRCompositionLayer((XrCompositionLayerBaseHeader *)&composition_layer) {
39
XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerQuad::update_transform));
40
}
41
42
OpenXRCompositionLayerQuad::~OpenXRCompositionLayerQuad() {
43
}
44
45
void OpenXRCompositionLayerQuad::_bind_methods() {
46
ClassDB::bind_method(D_METHOD("set_quad_size", "size"), &OpenXRCompositionLayerQuad::set_quad_size);
47
ClassDB::bind_method(D_METHOD("get_quad_size"), &OpenXRCompositionLayerQuad::get_quad_size);
48
49
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "quad_size", PROPERTY_HINT_NONE, ""), "set_quad_size", "get_quad_size");
50
}
51
52
Ref<Mesh> OpenXRCompositionLayerQuad::_create_fallback_mesh() {
53
Ref<QuadMesh> mesh;
54
mesh.instantiate();
55
mesh->set_size(quad_size);
56
return mesh;
57
}
58
59
void OpenXRCompositionLayerQuad::_notification(int p_what) {
60
switch (p_what) {
61
case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
62
update_transform();
63
} break;
64
}
65
}
66
67
void OpenXRCompositionLayerQuad::update_transform() {
68
composition_layer.pose = get_openxr_pose();
69
}
70
71
void OpenXRCompositionLayerQuad::set_quad_size(const Size2 &p_size) {
72
quad_size = p_size;
73
composition_layer.size = { (float)quad_size.x, (float)quad_size.y };
74
update_fallback_mesh();
75
}
76
77
Size2 OpenXRCompositionLayerQuad::get_quad_size() const {
78
return quad_size;
79
}
80
81
Vector2 OpenXRCompositionLayerQuad::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {
82
Transform3D quad_transform = get_global_transform();
83
Vector3 quad_normal = quad_transform.basis.get_column(2);
84
85
float denom = quad_normal.dot(p_direction);
86
if (Math::abs(denom) > 0.0001) {
87
Vector3 vector = quad_transform.origin - p_origin;
88
float t = vector.dot(quad_normal) / denom;
89
if (t < 0.0) {
90
return Vector2(-1.0, -1.0);
91
}
92
Vector3 intersection = p_origin + p_direction * t;
93
94
Vector3 relative_point = intersection - quad_transform.origin;
95
Vector2 projected_point = Vector2(
96
relative_point.dot(quad_transform.basis.get_column(0)),
97
relative_point.dot(quad_transform.basis.get_column(1)));
98
if (Math::abs(projected_point.x) > quad_size.x / 2.0) {
99
return Vector2(-1.0, -1.0);
100
}
101
if (Math::abs(projected_point.y) > quad_size.y / 2.0) {
102
return Vector2(-1.0, -1.0);
103
}
104
105
float u = 0.5 + (projected_point.x / quad_size.x);
106
float v = 1.0 - (0.5 + (projected_point.y / quad_size.y));
107
108
return Vector2(u, v);
109
}
110
111
return Vector2(-1.0, -1.0);
112
}
113
114