diff options
Diffstat (limited to 'wgpu/src/shader/quad.wgsl')
-rw-r--r-- | wgpu/src/shader/quad.wgsl | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/wgpu/src/shader/quad.wgsl b/wgpu/src/shader/quad.wgsl index f919cfe2..4de73362 100644 --- a/wgpu/src/shader/quad.wgsl +++ b/wgpu/src/shader/quad.wgsl @@ -11,19 +11,15 @@ fn distance_alg( size: vec2<f32>, radius: f32 ) -> f32 { - var inner_size: vec2<f32> = size - vec2<f32>(radius, radius) * 2.0; + var inner_half_size: vec2<f32> = (size - vec2<f32>(radius, radius) * 2.0) / 2.0; var top_left: vec2<f32> = position + vec2<f32>(radius, radius); - var bottom_right: vec2<f32> = top_left + inner_size; - - var top_left_distance: vec2<f32> = top_left - frag_coord; - var bottom_right_distance: vec2<f32> = frag_coord - bottom_right; - - var dist: vec2<f32> = vec2<f32>( - max(max(top_left_distance.x, bottom_right_distance.x), 0.0), - max(max(top_left_distance.y, bottom_right_distance.y), 0.0) - ); + return rounded_box_sdf(frag_coord - top_left - inner_half_size, inner_half_size, 0.0); +} - return sqrt(dist.x * dist.x + dist.y * dist.y); +// Given a vector from a point to the center of a rounded rectangle of the given `size` and +// border `radius`, determines the point's distance from the nearest edge of the rounded rectangle +fn rounded_box_sdf(to_center: vec2<f32>, size: vec2<f32>, radius: f32) -> f32 { + return length(max(abs(to_center) - size + vec2<f32>(radius, radius), vec2<f32>(0.0, 0.0))) - radius; } // Based on the fragement position and the center of the quad, select one of the 4 radi. |