From a54df7fdb723d6ea46d4de99da2d7543ec6ec31c Mon Sep 17 00:00:00 2001 From: Rudolph Bester Date: Tue, 1 Jul 2025 19:01:38 +0200 Subject: [PATCH] Move occlusion culling back to being based on euclidian distance but with some distance calculation using double precision --- modules/raycast/raycast_occlusion_cull.cpp | 3 +-- servers/rendering/renderer_scene_occlusion_cull.h | 10 ++++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/raycast/raycast_occlusion_cull.cpp b/modules/raycast/raycast_occlusion_cull.cpp index b33e52f549a..a6c865fce7d 100644 --- a/modules/raycast/raycast_occlusion_cull.cpp +++ b/modules/raycast/raycast_occlusion_cull.cpp @@ -174,8 +174,7 @@ void RaycastOcclusionCull::RaycastHZBuffer::sort_rays(const Vector3 &p_camera_di int k = tile_i * TILE_SIZE + tile_j; int tile_index = i * tile_grid_size.x + j; - Vector3 ray_dir(camera_rays[tile_index].ray.dir_x[k], camera_rays[tile_index].ray.dir_y[k], camera_rays[tile_index].ray.dir_z[k]); - mips[0][y * buffer_size.x + x] = camera_rays[tile_index].ray.tfar[k] * p_camera_dir.dot(ray_dir); // Store z-depth in view space. + mips[0][y * buffer_size.x + x] = camera_rays[tile_index].ray.tfar[k]; } } } diff --git a/servers/rendering/renderer_scene_occlusion_cull.h b/servers/rendering/renderer_scene_occlusion_cull.h index 1ab50c720a7..d4f4ed672f5 100644 --- a/servers/rendering/renderer_scene_occlusion_cull.h +++ b/servers/rendering/renderer_scene_occlusion_cull.h @@ -71,7 +71,9 @@ public: return false; } - float min_depth = -closest_point_view.z; + // Force distance calculation to use double precision to avoid floating-point overflow for distant objects. + closest_point = closest_point - p_cam_position; + float min_depth = Math::sqrt((double)closest_point.x * (double)closest_point.x + (double)closest_point.y * (double)closest_point.y + (double)closest_point.z * (double)closest_point.z); Vector2 rect_min = Vector2(FLT_MAX, FLT_MAX); Vector2 rect_max = Vector2(FLT_MIN, FLT_MIN); @@ -82,9 +84,13 @@ public: Vector3 corner = Vector3(p_bounds[0] * c.x + p_bounds[3] * nc.x, p_bounds[1] * c.y + p_bounds[4] * nc.y, p_bounds[2] * c.z + p_bounds[5] * nc.z); Vector3 view = p_cam_inv_transform.xform(corner); + // When using an orthogonal camera, the closest point of an AABB to the camera is guaranteed to be a corner. + if (p_cam_projection.is_orthogonal()) { + min_depth = MIN(min_depth, -view.z); + } + Plane vp = Plane(view, 1.0); Plane projected = p_cam_projection.xform4(vp); - min_depth = MIN(min_depth, -view.z); float w = projected.d; if (w < 1.0) {