Skip to content

Commit

Permalink
Merge pull request #17 from Zoxc/rounded-scissor
Browse files Browse the repository at this point in the history
Support rounded scissor rects
  • Loading branch information
wtholliday authored Jan 5, 2024
2 parents 9601e61 + d2ba81b commit f12f4b6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub(crate) struct Scissor {
pub xform: WorldToLocal,
pub origin: [f32; 2],
pub size: [f32; 2],
pub radius: f32,
pub padding: f32,
}

impl Scissor {
Expand All @@ -66,6 +68,8 @@ impl Scissor {
xform: WorldToLocal::identity(),
origin: [-10000.0, -10000.0],
size: [20000.0, 20000.0],
radius: 0.0,
padding: 0.0,
}
}
}
Expand Down Expand Up @@ -879,6 +883,20 @@ impl Vger {
m.xform = xform;
m.origin = rect.origin.to_array();
m.size = rect.size.to_array();
m.radius = 0.0;
}
}
}

/// Sets the current scissor to a rounded rect.
pub fn rounded_scissor(&mut self, rect: LocalRect, radius: f32) {
if let Some(m) = self.scissor_stack.last_mut() {
*m = Scissor::new();
if let Some(xform) = self.tx_stack.last().unwrap().inverse() {
m.xform = xform;
m.origin = rect.origin.to_array();
m.size = rect.size.to_array();
m.radius = radius;
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ struct Scissor {
xform: PackedMat3x2,
origin: vec2<f32>,
size: vec2<f32>,
radius: f32,
padding: f32,
};

struct Scissors {
Expand All @@ -618,10 +620,11 @@ fn scissor_mask(scissor: Scissor, p: vec2<f32>) -> f32 {
let pp = (M * vec3<f32>(p, 1.0)).xy;
let center = scissor.origin + 0.5 * scissor.size;
let size = scissor.size;
if sdBox(pp - center, 0.5 * size, 0.0) < 0.0 {
return 1.0;
let value = 1.0 - sdBox(pp - center, 0.5 * size, scissor.radius);
if scissor.radius > 0.0 {
return value;
} else {
return 0.0;
return round(value);
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,27 @@ fn test_scissor() {
assert!(png_not_black(png_name));
}

#[test]
fn test_rounded_scissor() {
let (device, queue) = setup();

let mut vger = Vger::new(
device.clone(),
queue.clone(),
wgpu::TextureFormat::Rgba8UnormSrgb,
);

vger.begin(512.0, 512.0, 2.0);

vger.rounded_scissor(euclid::rect(200.0, 200.0, 100.0, 100.0), 20.0);
let cyan = vger.color_paint(Color::WHITE);
vger.fill_rect(euclid::rect(100.0, 100.0, 300.0, 300.0), 10.0, cyan);

let png_name = "rounded_scissor.png";
render_test(&mut vger, &device, &queue, png_name, true);
assert!(png_not_black(png_name));
}

#[test]
fn test_scissor_text() {
let (device, queue) = setup();
Expand Down

1 comment on commit f12f4b6

@wtholliday
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this change actually broke gradients and AA

Please sign in to comment.