Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rounded scissor rects #17

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -846,6 +850,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 @@ -576,6 +576,8 @@ struct Scissor {
xform: PackedMat3x2,
origin: vec2<f32>,
size: vec2<f32>,
radius: f32,
padding: f32,
};

struct Scissors {
Expand All @@ -591,10 +593,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 @@ -433,6 +433,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