Update lens_distortion.wgsl - #25218
Conversation
Fixed the visual creasing between quadrants by changing the perpendicular value of the dot product (zero) to be at 45 degrees from the corner rather than 90. Also changed the `direction` calculation to use the built-in `normalize` function.
|
Welcome, new contributor! Please make sure you've read our contributing guide, as well as our policy regarding AI usage, and we look forward to reviewing your pull request shortly ✨ |
alice-i-cecile
left a comment
There was a problem hiding this comment.
Fix looks solid, but we should include your very helpful information from your PR description in the comments here, so future readers can follow along <3
|
Thanks @alice-i-cecile ! I realized this broke some usages of the "multiplier" variable, so I fixed that too. I'll comment it up and introduce the changes in another commit. |
Fixed some bugs, added comments explaining the goals of the shader and the mechanics of how some of it works.
|
Let me know if there's any linguistic errors, confusing wording, or anything else I should fix! I believe this fits with the original intended functionality well enough. |
|
I have noticed a few weird little hairs on this shader that didn't really need to be there. The divide by zero check in the original, the if statement, and the clamp at the end... none of them seem to do anything, and branching code on a GPU is something you should avoid unless absolutely necessary. Unless anyone here knows if these are targeting platform dependent bugs or some other arcane silliness, I'm going to keep stripping them away. I'm also going to check out the other shaders introduced in the effects stack to see if there's more clean up to do, but I'll put that in another pull request if it happens. |
Removed some extraneous code that has no affect on the output.
|
So that's what that button does. Thanks github for explaining that so well... |
| let direction = normalize(uv_multiplied); | ||
|
|
||
| // Correct for the uv multiplier to prevent scaling the image along with the distortion. | ||
| let direction_adjusted = direction / multiplier; |
There was a problem hiding this comment.
multiplier can be zero, it would NaN everything
bevy/crates/bevy_post_process/src/effect_stack/lens_distortion.rs
Lines 47 to 48 in 25368b7
There was a problem hiding this comment.
Oops, guess I misinterpreted the multiplier effect then. I'm gonna have some tinkering to do. Not sure why anyone would want that, but the more functionality the merrier.
Summary of Changes
Fixed the visual creasing between quadrants by changing the perpendicular value of the dot product (zero) to be at 45 degrees from the corner rather than 90. Also changed the
directioncalculation to use the built-innormalizefunction.Objective
Correct the visual creasing in the lens distortion shader.
Solution
The distortion is warped by the dot product between the closest corner and the absolute position of the current texle position. This means that zero influence will be perpendicular to the corner. The angle between the corner and the axis is 45 degrees, which is only half of the way to the 90 degrees we need.
To correct this, I subtract TAU (the maximum value of the dot product in this situation), then multiply the value by 0.5. This multiplication shifts the original point of zero up from 90 degrees away from the corner to 45 degrees to the corner. I then add TAU back to make the maximum value line up with where it originally was.
Testing
I placed the edge of the screen aligned with a straight element to examine the curve visually. With this fix, the axis crease no longer appeared.