Skip to content

Commit d2ba81b

Browse files
dzhou121Zoxc
authored andcommitted
Support rounded scissor rects
1 parent 74e6ea1 commit d2ba81b

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
}
@@ -846,6 +850,20 @@ impl Vger {
846850
m.xform = xform;
847851
m.origin = rect.origin.to_array();
848852
m.size = rect.size.to_array();
853+
m.radius = 0.0;
854+
}
855+
}
856+
}
857+
858+
/// Sets the current scissor to a rounded rect.
859+
pub fn rounded_scissor(&mut self, rect: LocalRect, radius: f32) {
860+
if let Some(m) = self.scissor_stack.last_mut() {
861+
*m = Scissor::new();
862+
if let Some(xform) = self.tx_stack.last().unwrap().inverse() {
863+
m.xform = xform;
864+
m.origin = rect.origin.to_array();
865+
m.size = rect.size.to_array();
866+
m.radius = radius;
849867
}
850868
}
851869
}

src/shader.wgsl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,8 @@ struct Scissor {
576576
xform: PackedMat3x2,
577577
origin: vec2<f32>,
578578
size: vec2<f32>,
579+
radius: f32,
580+
padding: f32,
579581
};
580582

581583
struct Scissors {
@@ -591,10 +593,11 @@ fn scissor_mask(scissor: Scissor, p: vec2<f32>) -> f32 {
591593
let pp = (M * vec3<f32>(p, 1.0)).xy;
592594
let center = scissor.origin + 0.5 * scissor.size;
593595
let size = scissor.size;
594-
if sdBox(pp - center, 0.5 * size, 0.0) < 0.0 {
595-
return 1.0;
596+
let value = 1.0 - sdBox(pp - center, 0.5 * size, scissor.radius);
597+
if scissor.radius > 0.0 {
598+
return value;
596599
} else {
597-
return 0.0;
600+
return round(value);
598601
}
599602
}
600603

tests/tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,27 @@ fn test_scissor() {
433433
assert!(png_not_black(png_name));
434434
}
435435

436+
#[test]
437+
fn test_rounded_scissor() {
438+
let (device, queue) = setup();
439+
440+
let mut vger = Vger::new(
441+
device.clone(),
442+
queue.clone(),
443+
wgpu::TextureFormat::Rgba8UnormSrgb,
444+
);
445+
446+
vger.begin(512.0, 512.0, 2.0);
447+
448+
vger.rounded_scissor(euclid::rect(200.0, 200.0, 100.0, 100.0), 20.0);
449+
let cyan = vger.color_paint(Color::WHITE);
450+
vger.fill_rect(euclid::rect(100.0, 100.0, 300.0, 300.0), 10.0, cyan);
451+
452+
let png_name = "rounded_scissor.png";
453+
render_test(&mut vger, &device, &queue, png_name, true);
454+
assert!(png_not_black(png_name));
455+
}
456+
436457
#[test]
437458
fn test_scissor_text() {
438459
let (device, queue) = setup();

0 commit comments

Comments
 (0)