Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/openxr/scene/openxr_composition_layer_cylinder.cpp
10278 views
1
/**************************************************************************/
2
/* openxr_composition_layer_cylinder.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_cylinder.h"
32
33
#include "../openxr_interface.h"
34
35
#include "scene/resources/mesh.h"
36
37
OpenXRCompositionLayerCylinder::OpenXRCompositionLayerCylinder() :
38
OpenXRCompositionLayer((XrCompositionLayerBaseHeader *)&composition_layer) {
39
XRServer::get_singleton()->connect("reference_frame_changed", callable_mp(this, &OpenXRCompositionLayerCylinder::update_transform));
40
}
41
42
OpenXRCompositionLayerCylinder::~OpenXRCompositionLayerCylinder() {
43
}
44
45
void OpenXRCompositionLayerCylinder::_bind_methods() {
46
ClassDB::bind_method(D_METHOD("set_radius", "radius"), &OpenXRCompositionLayerCylinder::set_radius);
47
ClassDB::bind_method(D_METHOD("get_radius"), &OpenXRCompositionLayerCylinder::get_radius);
48
49
ClassDB::bind_method(D_METHOD("set_aspect_ratio", "aspect_ratio"), &OpenXRCompositionLayerCylinder::set_aspect_ratio);
50
ClassDB::bind_method(D_METHOD("get_aspect_ratio"), &OpenXRCompositionLayerCylinder::get_aspect_ratio);
51
52
ClassDB::bind_method(D_METHOD("set_central_angle", "angle"), &OpenXRCompositionLayerCylinder::set_central_angle);
53
ClassDB::bind_method(D_METHOD("get_central_angle"), &OpenXRCompositionLayerCylinder::get_central_angle);
54
55
ClassDB::bind_method(D_METHOD("set_fallback_segments", "segments"), &OpenXRCompositionLayerCylinder::set_fallback_segments);
56
ClassDB::bind_method(D_METHOD("get_fallback_segments"), &OpenXRCompositionLayerCylinder::get_fallback_segments);
57
58
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_NONE, ""), "set_radius", "get_radius");
59
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "aspect_ratio", PROPERTY_HINT_RANGE, "0,100"), "set_aspect_ratio", "get_aspect_ratio");
60
ADD_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");
61
ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_segments", PROPERTY_HINT_NONE, ""), "set_fallback_segments", "get_fallback_segments");
62
}
63
64
Ref<Mesh> OpenXRCompositionLayerCylinder::_create_fallback_mesh() {
65
Ref<ArrayMesh> mesh;
66
mesh.instantiate();
67
68
float arc_length = radius * central_angle;
69
float half_height = ((1.0 / aspect_ratio) * arc_length) / 2.0;
70
71
Array arrays;
72
arrays.resize(ArrayMesh::ARRAY_MAX);
73
74
Vector<Vector3> vertices;
75
Vector<Vector3> normals;
76
Vector<Vector2> uvs;
77
Vector<int> indices;
78
79
float delta_angle = central_angle / fallback_segments;
80
float start_angle = (-Math::PI / 2.0) - (central_angle / 2.0);
81
82
for (uint32_t i = 0; i < fallback_segments + 1; i++) {
83
float current_angle = start_angle + (delta_angle * i);
84
float x = radius * Math::cos(current_angle);
85
float z = radius * Math::sin(current_angle);
86
Vector3 normal(Math::cos(current_angle), 0, Math::sin(current_angle));
87
88
vertices.push_back(Vector3(x, -half_height, z));
89
normals.push_back(normal);
90
uvs.push_back(Vector2((float)i / fallback_segments, 1));
91
92
vertices.push_back(Vector3(x, half_height, z));
93
normals.push_back(normal);
94
uvs.push_back(Vector2((float)i / fallback_segments, 0));
95
}
96
97
for (uint32_t i = 0; i < fallback_segments; i++) {
98
uint32_t index = i * 2;
99
indices.push_back(index);
100
indices.push_back(index + 1);
101
indices.push_back(index + 3);
102
indices.push_back(index);
103
indices.push_back(index + 3);
104
indices.push_back(index + 2);
105
}
106
107
arrays[ArrayMesh::ARRAY_VERTEX] = vertices;
108
arrays[ArrayMesh::ARRAY_NORMAL] = normals;
109
arrays[ArrayMesh::ARRAY_TEX_UV] = uvs;
110
arrays[ArrayMesh::ARRAY_INDEX] = indices;
111
112
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
113
return mesh;
114
}
115
116
void OpenXRCompositionLayerCylinder::_notification(int p_what) {
117
switch (p_what) {
118
case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
119
update_transform();
120
} break;
121
}
122
}
123
124
void OpenXRCompositionLayerCylinder::update_transform() {
125
composition_layer.pose = get_openxr_pose();
126
}
127
128
void OpenXRCompositionLayerCylinder::set_radius(float p_radius) {
129
ERR_FAIL_COND(p_radius <= 0);
130
radius = p_radius;
131
composition_layer.radius = radius;
132
update_fallback_mesh();
133
}
134
135
float OpenXRCompositionLayerCylinder::get_radius() const {
136
return radius;
137
}
138
139
void OpenXRCompositionLayerCylinder::set_aspect_ratio(float p_aspect_ratio) {
140
ERR_FAIL_COND(p_aspect_ratio <= 0);
141
aspect_ratio = p_aspect_ratio;
142
composition_layer.aspectRatio = aspect_ratio;
143
update_fallback_mesh();
144
}
145
146
float OpenXRCompositionLayerCylinder::get_aspect_ratio() const {
147
return aspect_ratio;
148
}
149
150
void OpenXRCompositionLayerCylinder::set_central_angle(float p_central_angle) {
151
ERR_FAIL_COND(p_central_angle <= 0);
152
central_angle = p_central_angle;
153
composition_layer.centralAngle = central_angle;
154
update_fallback_mesh();
155
}
156
157
float OpenXRCompositionLayerCylinder::get_central_angle() const {
158
return central_angle;
159
}
160
161
void OpenXRCompositionLayerCylinder::set_fallback_segments(uint32_t p_fallback_segments) {
162
ERR_FAIL_COND(p_fallback_segments == 0);
163
fallback_segments = p_fallback_segments;
164
update_fallback_mesh();
165
}
166
167
uint32_t OpenXRCompositionLayerCylinder::get_fallback_segments() const {
168
return fallback_segments;
169
}
170
171
Vector2 OpenXRCompositionLayerCylinder::intersects_ray(const Vector3 &p_origin, const Vector3 &p_direction) const {
172
Transform3D cylinder_transform = get_global_transform();
173
Vector3 cylinder_axis = cylinder_transform.basis.get_column(1);
174
175
Vector3 offset = p_origin - cylinder_transform.origin;
176
float a = p_direction.dot(p_direction - cylinder_axis * p_direction.dot(cylinder_axis));
177
float b = 2.0 * (p_direction.dot(offset - cylinder_axis * offset.dot(cylinder_axis)));
178
float c = offset.dot(offset - cylinder_axis * offset.dot(cylinder_axis)) - (radius * radius);
179
180
float discriminant = b * b - 4.0 * a * c;
181
if (discriminant < 0.0) {
182
return Vector2(-1.0, -1.0);
183
}
184
185
float t0 = (-b - Math::sqrt(discriminant)) / (2.0 * a);
186
float t1 = (-b + Math::sqrt(discriminant)) / (2.0 * a);
187
float t = MAX(t0, t1);
188
189
if (t < 0.0) {
190
return Vector2(-1.0, -1.0);
191
}
192
Vector3 intersection = p_origin + p_direction * t;
193
194
Basis correction = cylinder_transform.basis.inverse();
195
correction.rotate(Vector3(0.0, 1.0, 0.0), -Math::PI / 2.0);
196
Vector3 relative_point = correction.xform(intersection - cylinder_transform.origin);
197
198
Vector2 projected_point = Vector2(relative_point.x, relative_point.z);
199
float intersection_angle = Math::atan2(projected_point.y, projected_point.x);
200
if (Math::abs(intersection_angle) > central_angle / 2.0) {
201
return Vector2(-1.0, -1.0);
202
}
203
204
float arc_length = radius * central_angle;
205
float height = aspect_ratio * arc_length;
206
if (Math::abs(relative_point.y) > height / 2.0) {
207
return Vector2(-1.0, -1.0);
208
}
209
210
float u = 0.5 + (intersection_angle / central_angle);
211
float v = 1.0 - (0.5 + (relative_point.y / height));
212
213
return Vector2(u, v);
214
}
215
216