Skip to content

Commit 18eb1c1

Browse files
Rudolph-BFlarkk
andcommitted
Fix occlusion culling by using depth instead of Euclidean distance when selecting the closest point
Co-authored-by: Florent Guiocheau <[email protected]>
1 parent b5bdb88 commit 18eb1c1

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

modules/raycast/raycast_occlusion_cull.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ void RaycastOcclusionCull::RaycastHZBuffer::sort_rays(const Vector3 &p_camera_di
173173
}
174174
int k = tile_i * TILE_SIZE + tile_j;
175175
int tile_index = i * tile_grid_size.x + j;
176-
mips[0][y * buffer_size.x + x] = camera_rays[tile_index].ray.tfar[k];
176+
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.
177179
}
178180
}
179181
}

servers/rendering/renderer_scene_occlusion_cull.h

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

74-
float min_depth = (closest_point - p_cam_position).length();
74+
float min_depth = -closest_point_view.z;
7575

7676
Vector2 rect_min = Vector2(FLT_MAX, FLT_MAX);
7777
Vector2 rect_max = Vector2(FLT_MIN, FLT_MIN);
@@ -82,12 +82,9 @@ class RendererSceneOcclusionCull {
8282
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);
8383
Vector3 view = p_cam_inv_transform.xform(corner);
8484

85-
if (p_cam_projection.is_orthogonal()) {
86-
min_depth = MIN(min_depth, -view.z);
87-
}
88-
8985
Plane vp = Plane(view, 1.0);
9086
Plane projected = p_cam_projection.xform4(vp);
87+
min_depth = MIN(min_depth, -view.z);
9188

9289
float w = projected.d;
9390
if (w < 1.0) {

0 commit comments

Comments
 (0)