Skip to content

Fix specular cutoff on lights with radius overlapping with mesh #19157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix specular cutoff on lights with radius overlapping with mesh
  • Loading branch information
atlv24 committed May 10, 2025
commit 511d50eeaf3fde1a5492bb899fa355ff7b16e8ac
18 changes: 17 additions & 1 deletion crates/bevy_pbr/src/render/pbr_lighting.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,23 @@ fn compute_specular_layer_values_for_point_light(

// Representative Point Area Lights.
// see http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf p14-16
let centerToRay = dot(light_to_frag, R) * R - light_to_frag;
var LtFdotR = dot(light_to_frag, R);

// HACK: the following line is an amendment to fix a discontinuity when a surface
// intersects the light sphere. See https://github.com/bevyengine/bevy/issues/13318
//
// This sentence in the reference is crux of the problem: "We approximate finding the point with the
// smallest angle to the reflection ray by finding the point with the smallest distance to the ray."
// This approximation turns out to be completely wrong for points inside or near the sphere.
// Clamping this dot product to be positive ensures `centerToRay` lies on ray and not behind it.
// Any non-zero epsilon works here, it just has to be positive to avoid a singularity at zero.
// However, this is still far from physically accurate. Deriving an exact solution would help,
// but really we should adopt a superior solution to area lighting, such as:
// Physically Based Area Lights by Michal Drobot, or
// Polygonal-Light Shading with Linearly Transformed Cosines by Eric Heitz et al.
LtFdotR = max(0.0001, LtFdotR);

let centerToRay = LtFdotR * R - light_to_frag;
let closestPoint = light_to_frag + centerToRay * saturate(
light_position_radius * inverseSqrt(dot(centerToRay, centerToRay)));
let LspecLengthInverse = inverseSqrt(dot(closestPoint, closestPoint));
Expand Down
Loading