Skip to content

Commit 771e561

Browse files
committedOct 24, 2024
Fix Camera3D::project_position() when depth=zfar
1 parent 3dbef70 commit 771e561

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed
 

‎scene/3d/camera_3d.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,12 @@ Vector3 Camera3D::project_position(const Point2 &p_point, real_t p_z_depth) cons
528528
}
529529
Size2 viewport_size = get_viewport()->get_visible_rect().size;
530530

531-
Projection cm = _get_camera_projection(p_z_depth);
531+
Projection cm = _get_camera_projection(_near);
532532

533-
Vector2 vp_he = cm.get_viewport_half_extents();
533+
Plane z_slice(Vector3(0, 0, 1), -p_z_depth);
534+
Vector3 res;
535+
z_slice.intersect_3(cm.get_projection_plane(Projection::Planes::PLANE_RIGHT), cm.get_projection_plane(Projection::Planes::PLANE_TOP), &res);
536+
Vector2 vp_he(res.x, res.y);
534537

535538
Vector2 point;
536539
point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0;

‎tests/scene/test_camera_3d.h

+6
Original file line numberDiff line numberDiff line change
@@ -231,23 +231,29 @@ TEST_CASE("[SceneTree][Camera3D] Project/Unproject position") {
231231
test_camera->set_orthogonal(5.0f, 0.5f, 1000.0f);
232232
// Center.
233233
CHECK(test_camera->project_position(Vector2(200, 100), 0.5f).is_equal_approx(Vector3(0, 0, -0.5f)));
234+
CHECK(test_camera->project_position(Vector2(200, 100), test_camera->get_far()).is_equal_approx(Vector3(0, 0, -test_camera->get_far())));
234235
// Top left.
235236
CHECK(test_camera->project_position(Vector2(0, 0), 1.5f).is_equal_approx(Vector3(-5.0f, 2.5f, -1.5f)));
237+
CHECK(test_camera->project_position(Vector2(0, 0), test_camera->get_near()).is_equal_approx(Vector3(-5.0f, 2.5f, -test_camera->get_near())));
236238
// Bottom right.
237239
CHECK(test_camera->project_position(Vector2(400, 200), 5.0f).is_equal_approx(Vector3(5.0f, -2.5f, -5.0f)));
240+
CHECK(test_camera->project_position(Vector2(400, 200), test_camera->get_far()).is_equal_approx(Vector3(5.0f, -2.5f, -test_camera->get_far())));
238241
}
239242

240243
SUBCASE("Perspective projection") {
241244
test_camera->set_perspective(120.0f, 0.5f, 1000.0f);
242245
// Center.
243246
CHECK(test_camera->project_position(Vector2(200, 100), 0.5f).is_equal_approx(Vector3(0, 0, -0.5f)));
244247
CHECK(test_camera->project_position(Vector2(200, 100), 100.0f).is_equal_approx(Vector3(0, 0, -100.0f)));
248+
CHECK(test_camera->project_position(Vector2(200, 100), test_camera->get_far()).is_equal_approx(Vector3(0, 0, -1.0f) * test_camera->get_far()));
245249
// 3/4th way to Top left.
246250
CHECK(test_camera->project_position(Vector2(100, 50), 0.5f).is_equal_approx(Vector3(-SQRT3 * 0.5f, SQRT3 * 0.25f, -0.5f)));
247251
CHECK(test_camera->project_position(Vector2(100, 50), 1.0f).is_equal_approx(Vector3(-SQRT3, SQRT3 * 0.5f, -1.0f)));
252+
CHECK(test_camera->project_position(Vector2(100, 50), test_camera->get_near()).is_equal_approx(Vector3(-SQRT3, SQRT3 * 0.5f, -1.0f) * test_camera->get_near()));
248253
// 3/4th way to Bottom right.
249254
CHECK(test_camera->project_position(Vector2(300, 150), 0.5f).is_equal_approx(Vector3(SQRT3 * 0.5f, -SQRT3 * 0.25f, -0.5f)));
250255
CHECK(test_camera->project_position(Vector2(300, 150), 1.0f).is_equal_approx(Vector3(SQRT3, -SQRT3 * 0.5f, -1.0f)));
256+
CHECK(test_camera->project_position(Vector2(300, 150), test_camera->get_far()).is_equal_approx(Vector3(SQRT3, -SQRT3 * 0.5f, -1.0f) * test_camera->get_far()));
251257
}
252258
}
253259

0 commit comments

Comments
 (0)