use single sdf for all corners
This commit is contained in:
parent
cd60385b1e
commit
74665d696d
3 changed files with 33 additions and 102 deletions
|
|
@ -157,42 +157,19 @@ fn gradient_fs_main(input: GradientVertexOutput) -> @location(0) vec4<f32> {
|
|||
let pos = input.position_and_scale.xy;
|
||||
let scale = input.position_and_scale.zw;
|
||||
|
||||
var border_radius = select_border_radius(
|
||||
input.border_radius,
|
||||
input.position.xy,
|
||||
(pos + scale * 0.5).xy
|
||||
);
|
||||
var dist: f32 = rounded_box_sdf(
|
||||
-(input.position.xy - pos - scale / 2.0) * 2.0,
|
||||
scale,
|
||||
input.border_radius * 2.0
|
||||
) / 2.0;
|
||||
|
||||
if (input.border_width > 0.0) {
|
||||
var internal_border: f32 = max(border_radius - input.border_width, 0.0);
|
||||
|
||||
var internal_distance: f32 = distance_alg(
|
||||
input.position.xy,
|
||||
pos + vec2<f32>(input.border_width, input.border_width),
|
||||
scale - vec2<f32>(input.border_width * 2.0, input.border_width * 2.0),
|
||||
internal_border
|
||||
mixed_color = mix(
|
||||
mixed_color,
|
||||
input.border_color,
|
||||
clamp(0.5 + dist + input.border_width, 0.0, 1.0)
|
||||
);
|
||||
|
||||
var border_mix: f32 = smoothstep(
|
||||
max(internal_border - 0.5, 0.0),
|
||||
internal_border + 0.5,
|
||||
internal_distance
|
||||
);
|
||||
|
||||
mixed_color = mix(mixed_color, input.border_color, border_mix);
|
||||
}
|
||||
|
||||
var dist: f32 = distance_alg(
|
||||
input.position.xy,
|
||||
pos,
|
||||
scale,
|
||||
border_radius
|
||||
);
|
||||
|
||||
var radius_alpha: f32 = 1.0 - smoothstep(
|
||||
max(border_radius - 0.5, 0.0),
|
||||
border_radius + 0.5,
|
||||
dist);
|
||||
|
||||
return mixed_color * radius_alpha;
|
||||
return mixed_color * clamp(0.5-dist, 0.0, 1.0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,57 +75,33 @@ fn solid_fs_main(
|
|||
) -> @location(0) vec4<f32> {
|
||||
var mixed_color: vec4<f32> = input.color;
|
||||
|
||||
var border_radius = select_border_radius(
|
||||
input.border_radius,
|
||||
input.position.xy,
|
||||
(input.pos + input.scale * 0.5).xy
|
||||
);
|
||||
var dist = rounded_box_sdf(
|
||||
-(input.position.xy - input.pos - input.scale * 0.5) * 2.0,
|
||||
input.scale,
|
||||
input.border_radius * 2.0
|
||||
) / 2.0;
|
||||
|
||||
if (input.border_width > 0.0) {
|
||||
var internal_border: f32 = max(border_radius - input.border_width, 0.0);
|
||||
|
||||
var internal_distance: f32 = distance_alg(
|
||||
input.position.xy,
|
||||
input.pos + vec2<f32>(input.border_width, input.border_width),
|
||||
input.scale - vec2<f32>(input.border_width * 2.0, input.border_width * 2.0),
|
||||
internal_border
|
||||
mixed_color = mix(
|
||||
input.color,
|
||||
input.border_color,
|
||||
clamp(0.5 + dist + input.border_width, 0.0, 1.0)
|
||||
);
|
||||
|
||||
var border_mix: f32 = smoothstep(
|
||||
max(internal_border - 0.5, 0.0),
|
||||
internal_border + 0.5,
|
||||
internal_distance
|
||||
);
|
||||
|
||||
mixed_color = mix(input.color, input.border_color, border_mix);
|
||||
}
|
||||
|
||||
var dist: f32 = distance_alg(
|
||||
vec2<f32>(input.position.x, input.position.y),
|
||||
input.pos,
|
||||
input.scale,
|
||||
border_radius
|
||||
);
|
||||
var quad_alpha: f32 = clamp(0.5-dist, 0.0, 1.0);
|
||||
|
||||
var radius_alpha: f32 = 1.0 - smoothstep(
|
||||
max(border_radius - 0.5, 0.0),
|
||||
border_radius + 0.5,
|
||||
dist
|
||||
);
|
||||
|
||||
let quad_color = mixed_color * radius_alpha;
|
||||
let quad_color = mixed_color * quad_alpha;
|
||||
|
||||
if input.shadow_color.a > 0.0 {
|
||||
let shadow_radius = select_border_radius(
|
||||
input.border_radius,
|
||||
input.position.xy - input.shadow_offset,
|
||||
(input.pos + input.scale * 0.5).xy
|
||||
);
|
||||
var shadow_dist: f32 = rounded_box_sdf(
|
||||
-(input.position.xy - input.pos - input.shadow_offset - input.scale/2.0) * 2.0,
|
||||
input.scale,
|
||||
input.border_radius * 2.0
|
||||
) / 2.0;
|
||||
let shadow_alpha = 1.0 - smoothstep(-input.shadow_blur_radius, input.shadow_blur_radius, max(shadow_dist, 0.0));
|
||||
|
||||
let shadow_distance = max(rounded_box_sdf(input.position.xy - input.pos - input.shadow_offset - (input.scale / 2.0), input.scale / 2.0, shadow_radius), 0.);
|
||||
let shadow_alpha = 1.0 - smoothstep(-input.shadow_blur_radius, input.shadow_blur_radius, shadow_distance);
|
||||
|
||||
return mix(quad_color, input.shadow_color, (1.0 - radius_alpha) * shadow_alpha);
|
||||
return mix(quad_color, input.shadow_color, (1.0 - quad_alpha) * shadow_alpha);
|
||||
} else {
|
||||
return quad_color;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue