Debian Codegraph @

tests/scene/test_camera_3d.h

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