Skip to content

Commit f12f4b6

Browse files
authored
Merge pull request #17 from Zoxc/rounded-scissor
Support rounded scissor rects
2 parents 9601e61 + d2ba81b commit f12f4b6

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub(crate) struct Scissor {
5858
pub xform: WorldToLocal,
5959
pub origin: [f32; 2],
6060
pub size: [f32; 2],
61+
pub radius: f32,
62+
pub padding: f32,
6163
}
6264

6365
impl Scissor {
@@ -66,6 +68,8 @@ impl Scissor {
6668
xform: WorldToLocal::identity(),
6769
origin: [-10000.0, -10000.0],
6870
size: [20000.0, 20000.0],
71+
radius: 0.0,
72+
padding: 0.0,
6973
}
7074
}
7175
}
@@ -879,6 +883,20 @@ impl Vger {
879883
m.xform = xform;
880884
m.origin = rect.origin.to_array();
881885
m.size = rect.size.to_array();
886+
m.radius = 0.0;
887+
}
888+
}
889+
}
890+
891+
/// Sets the current scissor to a rounded rect.
892+
pub fn rounded_scissor(&mut self, rect: LocalRect, radius: f32) {
893+
if let Some(m) = self.scissor_stack.last_mut() {
894+
*m = Scissor::new();
895+
if let Some(xform) = self.tx_stack.last().unwrap().inverse() {
896+
m.xform = xform;
897+
m.origin = rect.origin.to_array();
898+
m.size = rect.size.to_array();
899+
m.radius = radius;
882900
}
883901
}
884902
}

src/shader.wgsl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ struct Scissor {
603603
xform: PackedMat3x2,
604604
origin: vec2<f32>,
605605
size: vec2<f32>,
606+
radius: f32,
607+
padding: f32,
606608
};
607609

608610
struct Scissors {
@@ -618,10 +620,11 @@ fn scissor_mask(scissor: Scissor, p: vec2<f32>) -> f32 {
618620
let pp = (M * vec3<f32>(p, 1.0)).xy;
619621
let center = scissor.origin + 0.5 * scissor.size;
620622
let size = scissor.size;
621-
if sdBox(pp - center, 0.5 * size, 0.0) < 0.0 {
622-
return 1.0;
623+
let value = 1.0 - sdBox(pp - center, 0.5 * size, scissor.radius);
624+
if scissor.radius > 0.0 {
625+
return value;
623626
} else {
624-
return 0.0;
627+
return round(value);
625628
}
626629
}
627630

tests/tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,27 @@ fn test_scissor() {
450450
assert!(png_not_black(png_name));
451451
}
452452

453+
#[test]
454+
fn test_rounded_scissor() {
455+
let (device, queue) = setup();
456+
457+
let mut vger = Vger::new(
458+
device.clone(),
459+
queue.clone(),
460+
wgpu::TextureFormat::Rgba8UnormSrgb,
461+
);
462+
463+
vger.begin(512.0, 512.0, 2.0);
464+
465+
vger.rounded_scissor(euclid::rect(200.0, 200.0, 100.0, 100.0), 20.0);
466+
let cyan = vger.color_paint(Color::WHITE);
467+
vger.fill_rect(euclid::rect(100.0, 100.0, 300.0, 300.0), 10.0, cyan);
468+
469+
let png_name = "rounded_scissor.png";
470+
render_test(&mut vger, &device, &queue, png_name, true);
471+
assert!(png_not_black(png_name));
472+
}
473+
453474
#[test]
454475
fn test_scissor_text() {
455476
let (device, queue) = setup();

0 commit comments

Comments
 (0)