Skip to content

Commit

Permalink
Fix gizmo line width issue when using perspective (#9067)
Browse files Browse the repository at this point in the history
- In bevy_polyline, we discovered an issue that happens when line width
is smaller than 1.0 and using perspective. It would sometimes end up
negative or NaN. I'm not entirely sure _why_ it happens.

- Make sure the width doesn't go below 0 before multiplying it with the
alpha

Here's a link to the bevy_polyline issue
ForesightMiningSoftwareCorporation/bevy_polyline#46

I'm not sure if the solution is correct but it solved the issue in my
testing.

Co-authored-by: François <mockersf@gmail.com>
  • Loading branch information
2 people authored and cart committed Aug 10, 2023
1 parent 1f4b281 commit 812d11b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions crates/bevy_gizmos/src/lines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn vertex(vertex: VertexInput) -> VertexOutput {
#endif

// Line thinness fade from https://acegikmo.com/shapes/docs/#anti-aliasing
if line_width < 1. {
if line_width > 0.0 && line_width < 1. {
color.a *= line_width;
line_width = 1.;
}
Expand All @@ -79,11 +79,11 @@ fn vertex(vertex: VertexInput) -> VertexOutput {
let epsilon = 4.88e-04;
// depth * (clip.w / depth)^-depth_bias. So that when -depth_bias is 1.0, this is equal to clip.w
// and when equal to 0.0, it is exactly equal to depth.
// the epsilon is here to prevent the depth from exceeding clip.w when -depth_bias = 1.0
// clip.w represents the near plane in homogenous clip space in bevy, having a depth
// the epsilon is here to prevent the depth from exceeding clip.w when -depth_bias = 1.0
// clip.w represents the near plane in homogeneous clip space in bevy, having a depth
// of this value means nothing can be in front of this
// The reason this uses an exponential function is that it makes it much easier for the
// user to chose a value that is convinient for them
// The reason this uses an exponential function is that it makes it much easier for the
// user to chose a value that is convenient for them
depth = clip.z * exp2(-line_gizmo.depth_bias * log2(clip.w / clip.z - epsilon));
}

Expand Down

0 comments on commit 812d11b

Please sign in to comment.