Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_camera_3d.cpp
45991 views
1
/**************************************************************************/
2
/* test_camera_3d.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 "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_camera_3d)
34
35
#ifndef _3D_DISABLED
36
37
#include "scene/3d/camera_3d.h"
38
#include "scene/main/scene_tree.h"
39
#include "scene/main/viewport.h"
40
#include "scene/main/window.h"
41
42
namespace TestCamera3D {
43
44
TEST_CASE("[SceneTree][Camera3D] Getters and setters") {
45
Camera3D *test_camera = memnew(Camera3D);
46
47
SUBCASE("Cull mask") {
48
constexpr int cull_mask = (1 << 5) | (1 << 7) | (1 << 9);
49
constexpr int set_enable_layer = 3;
50
constexpr int set_disable_layer = 5;
51
test_camera->set_cull_mask(cull_mask);
52
CHECK(test_camera->get_cull_mask() == cull_mask);
53
test_camera->set_cull_mask_value(set_enable_layer, true);
54
CHECK(test_camera->get_cull_mask_value(set_enable_layer));
55
test_camera->set_cull_mask_value(set_disable_layer, false);
56
CHECK_FALSE(test_camera->get_cull_mask_value(set_disable_layer));
57
}
58
59
SUBCASE("Attributes") {
60
Ref<CameraAttributes> attributes = memnew(CameraAttributes);
61
test_camera->set_attributes(attributes);
62
CHECK(test_camera->get_attributes() == attributes);
63
Ref<CameraAttributesPhysical> physical_attributes = memnew(CameraAttributesPhysical);
64
test_camera->set_attributes(physical_attributes);
65
CHECK(test_camera->get_attributes() == physical_attributes);
66
}
67
68
SUBCASE("Camera frustum properties") {
69
constexpr float depth_near = 0.2f;
70
constexpr float depth_far = 995.0f;
71
constexpr float fov = 120.0f;
72
constexpr float size = 7.0f;
73
constexpr float h_offset = 1.1f;
74
constexpr float v_offset = -1.6f;
75
const Vector2 frustum_offset(5, 7);
76
test_camera->set_near(depth_near);
77
CHECK(test_camera->get_near() == depth_near);
78
test_camera->set_far(depth_far);
79
CHECK(test_camera->get_far() == depth_far);
80
test_camera->set_fov(fov);
81
CHECK(test_camera->get_fov() == fov);
82
test_camera->set_size(size);
83
CHECK(test_camera->get_size() == size);
84
test_camera->set_h_offset(h_offset);
85
CHECK(test_camera->get_h_offset() == h_offset);
86
test_camera->set_v_offset(v_offset);
87
CHECK(test_camera->get_v_offset() == v_offset);
88
test_camera->set_frustum_offset(frustum_offset);
89
CHECK(test_camera->get_frustum_offset() == frustum_offset);
90
test_camera->set_keep_aspect_mode(Camera3D::KeepAspect::KEEP_HEIGHT);
91
CHECK(test_camera->get_keep_aspect_mode() == Camera3D::KeepAspect::KEEP_HEIGHT);
92
test_camera->set_keep_aspect_mode(Camera3D::KeepAspect::KEEP_WIDTH);
93
CHECK(test_camera->get_keep_aspect_mode() == Camera3D::KeepAspect::KEEP_WIDTH);
94
}
95
96
SUBCASE("Projection mode") {
97
test_camera->set_projection(Camera3D::ProjectionType::PROJECTION_ORTHOGONAL);
98
CHECK(test_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_ORTHOGONAL);
99
test_camera->set_projection(Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);
100
CHECK(test_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);
101
}
102
103
SUBCASE("Helper setters") {
104
constexpr float fov = 90.0f, size = 6.0f;
105
constexpr float near1 = 0.1f, near2 = 0.5f;
106
constexpr float far1 = 1001.0f, far2 = 1005.0f;
107
test_camera->set_perspective(fov, near1, far1);
108
CHECK(test_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);
109
CHECK(test_camera->get_near() == near1);
110
CHECK(test_camera->get_far() == far1);
111
CHECK(test_camera->get_fov() == fov);
112
test_camera->set_orthogonal(size, near2, far2);
113
CHECK(test_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_ORTHOGONAL);
114
CHECK(test_camera->get_near() == near2);
115
CHECK(test_camera->get_far() == far2);
116
CHECK(test_camera->get_size() == size);
117
}
118
119
SUBCASE("Doppler tracking") {
120
test_camera->set_doppler_tracking(Camera3D::DopplerTracking::DOPPLER_TRACKING_IDLE_STEP);
121
CHECK(test_camera->get_doppler_tracking() == Camera3D::DopplerTracking::DOPPLER_TRACKING_IDLE_STEP);
122
test_camera->set_doppler_tracking(Camera3D::DopplerTracking::DOPPLER_TRACKING_PHYSICS_STEP);
123
CHECK(test_camera->get_doppler_tracking() == Camera3D::DopplerTracking::DOPPLER_TRACKING_PHYSICS_STEP);
124
test_camera->set_doppler_tracking(Camera3D::DopplerTracking::DOPPLER_TRACKING_DISABLED);
125
CHECK(test_camera->get_doppler_tracking() == Camera3D::DopplerTracking::DOPPLER_TRACKING_DISABLED);
126
}
127
128
memdelete(test_camera);
129
}
130
131
TEST_CASE("[SceneTree][Camera3D] Position queries") {
132
// Cameras need a viewport to know how to compute their frustums, so we make a fake one here.
133
Camera3D *test_camera = memnew(Camera3D);
134
SubViewport *mock_viewport = memnew(SubViewport);
135
// 4:2.
136
mock_viewport->set_size(Vector2(400, 200));
137
SceneTree::get_singleton()->get_root()->add_child(mock_viewport);
138
mock_viewport->add_child(test_camera);
139
test_camera->set_keep_aspect_mode(Camera3D::KeepAspect::KEEP_WIDTH);
140
REQUIRE_MESSAGE(test_camera->is_current(), "Camera3D should be made current upon entering tree.");
141
142
SUBCASE("Orthogonal projection") {
143
test_camera->set_projection(Camera3D::ProjectionType::PROJECTION_ORTHOGONAL);
144
// The orthogonal case is simpler, so we test a more random position + rotation combination here.
145
// For the other cases we'll use zero translation and rotation instead.
146
test_camera->set_global_position(Vector3(1, 2, 3));
147
test_camera->look_at(Vector3(-4, 5, 1));
148
// Width = 5, Aspect Ratio = 400 / 200 = 2, so Height is 2.5.
149
test_camera->set_orthogonal(5.0f, 0.5f, 1000.0f);
150
const Basis basis = test_camera->get_global_basis();
151
// Subtract near so offset starts from the near plane.
152
const Vector3 offset1 = basis.xform(Vector3(-1.5f, 3.5f, 0.2f - test_camera->get_near()));
153
const Vector3 offset2 = basis.xform(Vector3(2.0f, -0.5f, -0.6f - test_camera->get_near()));
154
const Vector3 offset3 = basis.xform(Vector3(-3.0f, 1.0f, -0.6f - test_camera->get_near()));
155
const Vector3 offset4 = basis.xform(Vector3(-2.0f, 1.5f, -0.6f - test_camera->get_near()));
156
const Vector3 offset5 = basis.xform(Vector3(0, 0, 10000.0f - test_camera->get_near()));
157
158
SUBCASE("is_position_behind") {
159
CHECK(test_camera->is_position_behind(test_camera->get_global_position() + offset1));
160
CHECK_FALSE(test_camera->is_position_behind(test_camera->get_global_position() + offset2));
161
162
SUBCASE("h/v offset should have no effect on the result of is_position_behind") {
163
test_camera->set_h_offset(-11.0f);
164
test_camera->set_v_offset(22.1f);
165
CHECK(test_camera->is_position_behind(test_camera->get_global_position() + offset1));
166
test_camera->set_h_offset(4.7f);
167
test_camera->set_v_offset(-3.0f);
168
CHECK_FALSE(test_camera->is_position_behind(test_camera->get_global_position() + offset2));
169
}
170
// Reset h/v offsets.
171
test_camera->set_h_offset(0);
172
test_camera->set_v_offset(0);
173
}
174
175
SUBCASE("is_position_in_frustum") {
176
// If the point is behind the near plane, it is outside the camera frustum.
177
// So offset1 is not in frustum.
178
CHECK_FALSE(test_camera->is_position_in_frustum(test_camera->get_global_position() + offset1));
179
// If |right| > 5 / 2 or |up| > 2.5 / 2, the point is outside the camera frustum.
180
// So offset2 is in frustum and offset3 and offset4 are not.
181
CHECK(test_camera->is_position_in_frustum(test_camera->get_global_position() + offset2));
182
CHECK_FALSE(test_camera->is_position_in_frustum(test_camera->get_global_position() + offset3));
183
CHECK_FALSE(test_camera->is_position_in_frustum(test_camera->get_global_position() + offset4));
184
// offset5 is beyond the far plane, so it is not in frustum.
185
CHECK_FALSE(test_camera->is_position_in_frustum(test_camera->get_global_position() + offset5));
186
}
187
}
188
189
SUBCASE("Perspective projection") {
190
test_camera->set_projection(Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);
191
// Camera at origin, looking at +Z.
192
test_camera->set_global_position(Vector3(0, 0, 0));
193
test_camera->set_global_rotation(Vector3(0, 0, 0));
194
// Keep width, so horizontal fov = 120.
195
// Since the near plane distance is 1,
196
// with trig we know the near plane's width is 2 * sqrt(3), so its height is sqrt(3).
197
test_camera->set_perspective(120.0f, 1.0f, 1000.0f);
198
199
SUBCASE("is_position_behind") {
200
CHECK_FALSE(test_camera->is_position_behind(Vector3(0, 0, -1.5f)));
201
CHECK(test_camera->is_position_behind(Vector3(2, 0, -0.2f)));
202
}
203
204
SUBCASE("is_position_in_frustum") {
205
CHECK(test_camera->is_position_in_frustum(Vector3(-1.3f, 0, -1.1f)));
206
CHECK_FALSE(test_camera->is_position_in_frustum(Vector3(2, 0, -1.1f)));
207
CHECK(test_camera->is_position_in_frustum(Vector3(1, 0.5f, -1.1f)));
208
CHECK_FALSE(test_camera->is_position_in_frustum(Vector3(1, 1, -1.1f)));
209
CHECK(test_camera->is_position_in_frustum(Vector3(0, 0, -1.5f)));
210
CHECK_FALSE(test_camera->is_position_in_frustum(Vector3(0, 0, -0.5f)));
211
}
212
}
213
214
memdelete(test_camera);
215
memdelete(mock_viewport);
216
}
217
218
TEST_CASE("[SceneTree][Camera3D] Project/Unproject position") {
219
// Cameras need a viewport to know how to compute their frustums, so we make a fake one here.
220
Camera3D *test_camera = memnew(Camera3D);
221
SubViewport *mock_viewport = memnew(SubViewport);
222
// 4:2.
223
mock_viewport->set_size(Vector2(400, 200));
224
SceneTree::get_singleton()->get_root()->add_child(mock_viewport);
225
mock_viewport->add_child(test_camera);
226
test_camera->set_global_position(Vector3(0, 0, 0));
227
test_camera->set_global_rotation(Vector3(0, 0, 0));
228
test_camera->set_keep_aspect_mode(Camera3D::KeepAspect::KEEP_HEIGHT);
229
230
SUBCASE("project_position") {
231
SUBCASE("Orthogonal projection") {
232
test_camera->set_orthogonal(5.0f, 0.5f, 1000.0f);
233
// Center.
234
CHECK(test_camera->project_position(Vector2(200, 100), 0.5f).is_equal_approx(Vector3(0, 0, -0.5f)));
235
CHECK(test_camera->project_position(Vector2(200, 100), test_camera->get_far()).is_equal_approx(Vector3(0, 0, -test_camera->get_far())));
236
// Top left.
237
CHECK(test_camera->project_position(Vector2(0, 0), 1.5f).is_equal_approx(Vector3(-5.0f, 2.5f, -1.5f)));
238
CHECK(test_camera->project_position(Vector2(0, 0), test_camera->get_near()).is_equal_approx(Vector3(-5.0f, 2.5f, -test_camera->get_near())));
239
// Bottom right.
240
CHECK(test_camera->project_position(Vector2(400, 200), 5.0f).is_equal_approx(Vector3(5.0f, -2.5f, -5.0f)));
241
CHECK(test_camera->project_position(Vector2(400, 200), test_camera->get_far()).is_equal_approx(Vector3(5.0f, -2.5f, -test_camera->get_far())));
242
}
243
244
SUBCASE("Perspective projection") {
245
test_camera->set_perspective(120.0f, 0.5f, 1000.0f);
246
// Center.
247
CHECK(test_camera->project_position(Vector2(200, 100), 0.5f).is_equal_approx(Vector3(0, 0, -0.5f)));
248
CHECK(test_camera->project_position(Vector2(200, 100), 100.0f).is_equal_approx(Vector3(0, 0, -100.0f)));
249
CHECK(test_camera->project_position(Vector2(200, 100), test_camera->get_far()).is_equal_approx(Vector3(0, 0, -1.0f) * test_camera->get_far()));
250
// 3/4th way to Top left.
251
CHECK(test_camera->project_position(Vector2(100, 50), 0.5f).is_equal_approx(Vector3(-Math::SQRT3 * 0.5f, Math::SQRT3 * 0.25f, -0.5f)));
252
CHECK(test_camera->project_position(Vector2(100, 50), 1.0f).is_equal_approx(Vector3(-Math::SQRT3, Math::SQRT3 * 0.5f, -1.0f)));
253
CHECK(test_camera->project_position(Vector2(100, 50), test_camera->get_near()).is_equal_approx(Vector3(-Math::SQRT3, Math::SQRT3 * 0.5f, -1.0f) * test_camera->get_near()));
254
// 3/4th way to Bottom right.
255
CHECK(test_camera->project_position(Vector2(300, 150), 0.5f).is_equal_approx(Vector3(Math::SQRT3 * 0.5f, -Math::SQRT3 * 0.25f, -0.5f)));
256
CHECK(test_camera->project_position(Vector2(300, 150), 1.0f).is_equal_approx(Vector3(Math::SQRT3, -Math::SQRT3 * 0.5f, -1.0f)));
257
CHECK(test_camera->project_position(Vector2(300, 150), test_camera->get_far()).is_equal_approx(Vector3(Math::SQRT3, -Math::SQRT3 * 0.5f, -1.0f) * test_camera->get_far()));
258
}
259
}
260
261
// Uses cases that are the inverse of the above sub-case.
262
SUBCASE("unproject_position") {
263
SUBCASE("Orthogonal projection") {
264
test_camera->set_orthogonal(5.0f, 0.5f, 1000.0f);
265
// Center
266
CHECK(test_camera->unproject_position(Vector3(0, 0, -0.5f)).is_equal_approx(Vector2(200, 100)));
267
// Top left
268
CHECK(test_camera->unproject_position(Vector3(-5.0f, 2.5f, -1.5f)).is_equal_approx(Vector2(0, 0)));
269
// Bottom right
270
CHECK(test_camera->unproject_position(Vector3(5.0f, -2.5f, -5.0f)).is_equal_approx(Vector2(400, 200)));
271
}
272
273
SUBCASE("Perspective projection") {
274
test_camera->set_perspective(120.0f, 0.5f, 1000.0f);
275
// Center.
276
CHECK(test_camera->unproject_position(Vector3(0, 0, -0.5f)).is_equal_approx(Vector2(200, 100)));
277
CHECK(test_camera->unproject_position(Vector3(0, 0, -100.0f)).is_equal_approx(Vector2(200, 100)));
278
// 3/4th way to Top left.
279
WARN(test_camera->unproject_position(Vector3(-Math::SQRT3 * 0.5f, Math::SQRT3 * 0.25f, -0.5f)).is_equal_approx(Vector2(100, 50)));
280
WARN(test_camera->unproject_position(Vector3(-Math::SQRT3, Math::SQRT3 * 0.5f, -1.0f)).is_equal_approx(Vector2(100, 50)));
281
// 3/4th way to Bottom right.
282
CHECK(test_camera->unproject_position(Vector3(Math::SQRT3 * 0.5f, -Math::SQRT3 * 0.25f, -0.5f)).is_equal_approx(Vector2(300, 150)));
283
CHECK(test_camera->unproject_position(Vector3(Math::SQRT3, -Math::SQRT3 * 0.5f, -1.0f)).is_equal_approx(Vector2(300, 150)));
284
}
285
}
286
287
memdelete(test_camera);
288
memdelete(mock_viewport);
289
}
290
291
TEST_CASE("[SceneTree][Camera3D] Project ray") {
292
// Cameras need a viewport to know how to compute their frustums, so we make a fake one here.
293
Camera3D *test_camera = memnew(Camera3D);
294
SubViewport *mock_viewport = memnew(SubViewport);
295
// 4:2.
296
mock_viewport->set_size(Vector2(400, 200));
297
SceneTree::get_singleton()->get_root()->add_child(mock_viewport);
298
mock_viewport->add_child(test_camera);
299
test_camera->set_global_position(Vector3(0, 0, 0));
300
test_camera->set_global_rotation(Vector3(0, 0, 0));
301
test_camera->set_keep_aspect_mode(Camera3D::KeepAspect::KEEP_HEIGHT);
302
303
SUBCASE("project_ray_origin") {
304
SUBCASE("Orthogonal projection") {
305
test_camera->set_orthogonal(5.0f, 0.5f, 1000.0f);
306
// Center.
307
CHECK(test_camera->project_ray_origin(Vector2(200, 100)).is_equal_approx(Vector3(0, 0, -0.5f)));
308
// Top left.
309
CHECK(test_camera->project_ray_origin(Vector2(0, 0)).is_equal_approx(Vector3(-5.0f, 2.5f, -0.5f)));
310
// Bottom right.
311
CHECK(test_camera->project_ray_origin(Vector2(400, 200)).is_equal_approx(Vector3(5.0f, -2.5f, -0.5f)));
312
}
313
314
SUBCASE("Perspective projection") {
315
test_camera->set_perspective(120.0f, 0.5f, 1000.0f);
316
// Center.
317
CHECK(test_camera->project_ray_origin(Vector2(200, 100)).is_equal_approx(Vector3(0, 0, 0)));
318
// Top left.
319
CHECK(test_camera->project_ray_origin(Vector2(0, 0)).is_equal_approx(Vector3(0, 0, 0)));
320
// Bottom right.
321
CHECK(test_camera->project_ray_origin(Vector2(400, 200)).is_equal_approx(Vector3(0, 0, 0)));
322
}
323
}
324
325
SUBCASE("project_ray_normal") {
326
SUBCASE("Orthogonal projection") {
327
test_camera->set_orthogonal(5.0f, 0.5f, 1000.0f);
328
// Center.
329
CHECK(test_camera->project_ray_normal(Vector2(200, 100)).is_equal_approx(Vector3(0, 0, -1)));
330
// Top left.
331
CHECK(test_camera->project_ray_normal(Vector2(0, 0)).is_equal_approx(Vector3(0, 0, -1)));
332
// Bottom right.
333
CHECK(test_camera->project_ray_normal(Vector2(400, 200)).is_equal_approx(Vector3(0, 0, -1)));
334
}
335
336
SUBCASE("Perspective projection") {
337
test_camera->set_perspective(120.0f, 0.5f, 1000.0f);
338
// Center.
339
CHECK(test_camera->project_ray_normal(Vector2(200, 100)).is_equal_approx(Vector3(0, 0, -1)));
340
// Top left.
341
CHECK(test_camera->project_ray_normal(Vector2(0, 0)).is_equal_approx(Vector3(-Math::SQRT3, Math::SQRT3 / 2, -0.5f).normalized()));
342
// Bottom right.
343
CHECK(test_camera->project_ray_normal(Vector2(400, 200)).is_equal_approx(Vector3(Math::SQRT3, -Math::SQRT3 / 2, -0.5f).normalized()));
344
}
345
}
346
347
SUBCASE("project_local_ray_normal") {
348
test_camera->set_rotation_degrees(Vector3(60, 60, 60));
349
350
SUBCASE("Orthogonal projection") {
351
test_camera->set_orthogonal(5.0f, 0.5f, 1000.0f);
352
// Center.
353
CHECK(test_camera->project_local_ray_normal(Vector2(200, 100)).is_equal_approx(Vector3(0, 0, -1)));
354
// Top left.
355
CHECK(test_camera->project_local_ray_normal(Vector2(0, 0)).is_equal_approx(Vector3(0, 0, -1)));
356
// Bottom right.
357
CHECK(test_camera->project_local_ray_normal(Vector2(400, 200)).is_equal_approx(Vector3(0, 0, -1)));
358
}
359
360
SUBCASE("Perspective projection") {
361
test_camera->set_perspective(120.0f, 0.5f, 1000.0f);
362
// Center.
363
CHECK(test_camera->project_local_ray_normal(Vector2(200, 100)).is_equal_approx(Vector3(0, 0, -1)));
364
// Top left.
365
CHECK(test_camera->project_local_ray_normal(Vector2(0, 0)).is_equal_approx(Vector3(-Math::SQRT3, Math::SQRT3 / 2, -0.5f).normalized()));
366
// Bottom right.
367
CHECK(test_camera->project_local_ray_normal(Vector2(400, 200)).is_equal_approx(Vector3(Math::SQRT3, -Math::SQRT3 / 2, -0.5f).normalized()));
368
}
369
}
370
371
memdelete(test_camera);
372
memdelete(mock_viewport);
373
}
374
375
} // namespace TestCamera3D
376
377
#endif // _3D_DISABLED
378
379