feat: quad shadows
This commit is contained in:
parent
b3e3f6e3c9
commit
cc906c83cd
33 changed files with 305 additions and 25 deletions
|
|
@ -198,6 +198,9 @@ impl<'a> Layer<'a> {
|
|||
border_radius,
|
||||
border_width,
|
||||
border_color,
|
||||
shadow_color,
|
||||
shadow_offset,
|
||||
shadow_blur_radius,
|
||||
} => {
|
||||
let layer = &mut layers[current_layer];
|
||||
|
||||
|
|
@ -210,6 +213,9 @@ impl<'a> Layer<'a> {
|
|||
border_color: color::pack(*border_color),
|
||||
border_radius: *border_radius,
|
||||
border_width: *border_width,
|
||||
shadow_color: shadow_color.into_linear(),
|
||||
shadow_offset: (*shadow_offset).into(),
|
||||
shadow_blur_radius: *shadow_blur_radius,
|
||||
};
|
||||
|
||||
layer.quads.add(quad, background);
|
||||
|
|
|
|||
|
|
@ -201,6 +201,15 @@ pub struct Quad {
|
|||
|
||||
/// The border width of the [`Quad`].
|
||||
pub border_width: f32,
|
||||
|
||||
/// The shadow color of the [`Quad`].
|
||||
pub shadow_color: [f32; 4],
|
||||
|
||||
/// The shadow offset of the [`Quad`].
|
||||
pub shadow_offset: [f32; 2],
|
||||
|
||||
/// The shadow blur radius of the [`Quad`].
|
||||
pub shadow_blur_radius: f32,
|
||||
}
|
||||
|
||||
/// A group of [`Quad`]s rendered together.
|
||||
|
|
|
|||
|
|
@ -105,6 +105,12 @@ impl Pipeline {
|
|||
4 => Float32x4,
|
||||
// Border width
|
||||
5 => Float32,
|
||||
// Shadow color
|
||||
6 => Float32x4,
|
||||
// Shadow offset
|
||||
7 => Float32x2,
|
||||
// Shadow blur radius
|
||||
8 => Float32,
|
||||
),
|
||||
}],
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
return rounded_box_sdf(frag_coord - top_left - inner_half_size, inner_half_size, 0.0);
|
||||
}
|
||||
|
||||
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 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.
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@ struct SolidVertexInput {
|
|||
@location(3) border_color: vec4<f32>,
|
||||
@location(4) border_radius: vec4<f32>,
|
||||
@location(5) border_width: f32,
|
||||
@location(6) shadow_color: vec4<f32>,
|
||||
@location(7) shadow_offset: vec2<f32>,
|
||||
@location(8) shadow_blur_radius: f32,
|
||||
}
|
||||
|
||||
struct SolidVertexOutput {
|
||||
|
|
@ -16,14 +19,19 @@ struct SolidVertexOutput {
|
|||
@location(3) scale: vec2<f32>,
|
||||
@location(4) border_radius: vec4<f32>,
|
||||
@location(5) border_width: f32,
|
||||
@location(6) shadow_color: vec4<f32>,
|
||||
@location(7) shadow_offset: vec2<f32>,
|
||||
@location(8) shadow_blur_radius: f32,
|
||||
}
|
||||
|
||||
@vertex
|
||||
fn solid_vs_main(input: SolidVertexInput) -> SolidVertexOutput {
|
||||
var out: SolidVertexOutput;
|
||||
|
||||
var pos: vec2<f32> = input.pos * globals.scale;
|
||||
var scale: vec2<f32> = input.scale * globals.scale;
|
||||
var pos: vec2<f32> = (input.pos + min(input.shadow_offset, vec2<f32>(0.0, 0.0)) - input.shadow_blur_radius) * globals.scale;
|
||||
var quad_pos: vec2<f32> = input.pos * globals.scale;
|
||||
var scale: vec2<f32> = (input.scale + vec2<f32>(abs(input.shadow_offset.x), abs(input.shadow_offset.y)) + input.shadow_blur_radius * 2.0) * globals.scale;
|
||||
var quad_scale: vec2<f32> = input.scale * globals.scale;
|
||||
|
||||
var min_border_radius = min(input.scale.x, input.scale.y) * 0.5;
|
||||
var border_radius: vec4<f32> = vec4<f32>(
|
||||
|
|
@ -43,10 +51,13 @@ fn solid_vs_main(input: SolidVertexInput) -> SolidVertexOutput {
|
|||
out.position = globals.transform * transform * vec4<f32>(vertex_position(input.vertex_index), 0.0, 1.0);
|
||||
out.color = input.color;
|
||||
out.border_color = input.border_color;
|
||||
out.pos = pos;
|
||||
out.scale = scale;
|
||||
out.pos = quad_pos;
|
||||
out.scale = quad_scale;
|
||||
out.border_radius = border_radius * globals.scale;
|
||||
out.border_width = input.border_width * globals.scale;
|
||||
out.shadow_color = input.shadow_color;
|
||||
out.shadow_offset = input.shadow_offset * globals.scale;
|
||||
out.shadow_blur_radius = input.shadow_blur_radius * globals.scale;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
|
@ -95,5 +106,20 @@ fn solid_fs_main(
|
|||
dist
|
||||
);
|
||||
|
||||
return vec4<f32>(mixed_color.x, mixed_color.y, mixed_color.z, mixed_color.w * radius_alpha);
|
||||
let quad_color = vec4<f32>(mixed_color.x, mixed_color.y, mixed_color.z, mixed_color.w * radius_alpha);
|
||||
|
||||
if input.shadow_color.a > 0.0 {
|
||||
let shadow_distance = rounded_box_sdf(input.position.xy - input.pos - input.shadow_offset - (input.scale / 2.0), input.scale / 2.0, border_radius);
|
||||
let shadow_alpha = 1.0 - smoothstep(-input.shadow_blur_radius, input.shadow_blur_radius, shadow_distance);
|
||||
let shadow_color = input.shadow_color;
|
||||
let base_color = select(
|
||||
vec4<f32>(shadow_color.x, shadow_color.y, shadow_color.z, 0.0),
|
||||
quad_color,
|
||||
quad_color.a > 0.0
|
||||
);
|
||||
|
||||
return mix(base_color, shadow_color, (1.0 - radius_alpha) * shadow_alpha);
|
||||
} else {
|
||||
return quad_color;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue