39 lines
1.3 KiB
WebGPU Shading Language
39 lines
1.3 KiB
WebGPU Shading Language
struct Globals {
|
|
transform: mat4x4<f32>,
|
|
scale: f32,
|
|
}
|
|
|
|
@group(0) @binding(0) var<uniform> globals: Globals;
|
|
|
|
fn distance_alg(
|
|
frag_coord: vec2<f32>,
|
|
position: vec2<f32>,
|
|
size: vec2<f32>,
|
|
radius: f32
|
|
) -> f32 {
|
|
var inner_size: vec2<f32> = size - vec2<f32>(radius, radius) * 2.0;
|
|
var top_left: vec2<f32> = position + vec2<f32>(radius, radius);
|
|
var bottom_right: vec2<f32> = top_left + inner_size;
|
|
|
|
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);
|
|
}
|
|
|
|
// Based on the fragement position and the center of the quad, select one of the 4 radi.
|
|
// Order matches CSS border radius attribute:
|
|
// radi.x = top-left, radi.y = top-right, radi.z = bottom-right, radi.w = bottom-left
|
|
fn select_border_radius(radi: vec4<f32>, position: vec2<f32>, center: vec2<f32>) -> f32 {
|
|
var rx = radi.x;
|
|
var ry = radi.y;
|
|
rx = select(radi.x, radi.y, position.x > center.x);
|
|
ry = select(radi.w, radi.z, position.x > center.x);
|
|
rx = select(rx, ry, position.y > center.y);
|
|
return rx;
|
|
}
|