Skip to content

Commit d7cc121

Browse files
committed
Merge pull request #108347 from Rudolph-B/Issue-106184
Fix underculling of occulusion culling
2 parents bfb379b + a54df7f commit d7cc121

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

modules/raycast/raycast_occlusion_cull.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ void RaycastOcclusionCull::RaycastHZBuffer::sort_rays(const Vector3 &p_camera_di
174174
int k = tile_i * TILE_SIZE + tile_j;
175175
int tile_index = i * tile_grid_size.x + j;
176176

177-
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]);
178-
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.
177+
mips[0][y * buffer_size.x + x] = camera_rays[tile_index].ray.tfar[k];
179178
}
180179
}
181180
}

servers/rendering/renderer_scene_occlusion_cull.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ class RendererSceneOcclusionCull {
7171
return false;
7272
}
7373

74-
float min_depth = -closest_point_view.z;
74+
// Force distance calculation to use double precision to avoid floating-point overflow for distant objects.
75+
closest_point = closest_point - p_cam_position;
76+
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);
7577

7678
Vector2 rect_min = Vector2(FLT_MAX, FLT_MAX);
7779
Vector2 rect_max = Vector2(FLT_MIN, FLT_MIN);
@@ -82,9 +84,13 @@ class RendererSceneOcclusionCull {
8284
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);
8385
Vector3 view = p_cam_inv_transform.xform(corner);
8486

87+
// When using an orthogonal camera, the closest point of an AABB to the camera is guaranteed to be a corner.
88+
if (p_cam_projection.is_orthogonal()) {
89+
min_depth = MIN(min_depth, -view.z);
90+
}
91+
8592
Plane vp = Plane(view, 1.0);
8693
Plane projected = p_cam_projection.xform4(vp);
87-
min_depth = MIN(min_depth, -view.z);
8894

8995
float w = projected.d;
9096
if (w < 1.0) {

0 commit comments

Comments
 (0)