diff options
author | 2024-01-20 13:52:15 +0100 | |
---|---|---|
committer | 2024-01-20 13:52:15 +0100 | |
commit | 545cc909c9f356dd733d273173694db9b8c28594 (patch) | |
tree | c8132edab4386bbccb07a372c3776f22abefbda3 /wgpu/src/shader/quad.wgsl | |
parent | b3e3f6e3c9fc6879e6681810f54d7eaa7c0f3d30 (diff) | |
parent | 1c1667c3c99fa9b1009ef416b9b3c7e5a1d53a97 (diff) | |
download | iced-545cc909c9f356dd733d273173694db9b8c28594.tar.gz iced-545cc909c9f356dd733d273173694db9b8c28594.tar.bz2 iced-545cc909c9f356dd733d273173694db9b8c28594.zip |
Merge pull request #1882 from nicksenger/shadows
Quad shadows
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. |