test_camera_3d.h 17 KB

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