struct Uniforms {
screen: vec2<f32>,
atlas_size: vec2<f32>,
};
@group(0) @binding(0) var<uniform> u: Uniforms;
@group(0) @binding(1) var atlas: texture_2d<f32>;
@group(0) @binding(2) var samp: sampler;
struct QuadIn {
@builtin(vertex_index) vi: u32,
@location(0) rect: vec4<f32>,
@location(1) uv: vec4<f32>,
@location(2) color: vec4<f32>,
@location(3) clip: vec4<f32>,
@location(4) params: vec4<f32>,
@location(5) color2: vec4<f32>,
};
struct VOut {
@builtin(position) pos: vec4<f32>,
@location(0) uv: vec2<f32>,
@location(1) color: vec4<f32>,
@location(2) clip: vec4<f32>,
@location(3) params: vec4<f32>,
@location(4) color2: vec4<f32>,
@location(5) local: vec2<f32>,
@location(6) size: vec2<f32>,
};
@vertex
fn vs(in: QuadIn) -> VOut {
var corners = array<vec2<f32>, 6>(
vec2<f32>(0.0, 0.0), vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0),
vec2<f32>(0.0, 1.0), vec2<f32>(1.0, 0.0), vec2<f32>(1.0, 1.0),
);
let c = corners[in.vi];
let p = in.rect.xy + c * in.rect.zw;
var out: VOut;
out.pos = vec4<f32>(p.x / u.screen.x * 2.0 - 1.0, 1.0 - p.y / u.screen.y * 2.0, 0.0, 1.0);
out.uv = (in.uv.xy + c * in.uv.zw) / u.atlas_size;
out.color = in.color;
out.clip = in.clip;
out.params = in.params;
out.color2 = in.color2;
out.local = c * in.rect.zw;
out.size = in.rect.zw;
return out;
}
fn sd_round_box(p: vec2<f32>, half: vec2<f32>, r: f32) -> f32 {
let q = abs(p) - half + vec2<f32>(r);
return length(max(q, vec2<f32>(0.0))) + min(max(q.x, q.y), 0.0) - r;
}
fn premul(c: vec4<f32>) -> vec4<f32> {
return vec4<f32>(c.rgb * c.a, c.a);
}
@fragment
fn fs(in: VOut) -> @location(0) vec4<f32> {
let p = in.pos.xy;
if (p.x < in.clip.x || p.y < in.clip.y || p.x >= in.clip.z || p.y >= in.clip.w) {
discard;
}
let mode = u32(in.params.x + 0.5);
switch mode {
// Solid rectangle.
case 0u: {
return premul(in.color);
}
// Alpha-mask glyph tinted with color.
case 1u: {
let a = textureSampleLevel(atlas, samp, in.uv, 0.0).a;
return premul(vec4<f32>(in.color.rgb, in.color.a * a));
}
// Color glyph (emoji).
case 2u: {
let t = textureSampleLevel(atlas, samp, in.uv, 0.0);
return t * in.color.a;
}
// Rounded rect with border: params.y = radius, params.z = border width.
case 3u: {
let half = in.size * 0.5;
let d = sd_round_box(in.local - half, half, in.params.y);
let outer = clamp(0.5 - d, 0.0, 1.0);
let inner = clamp(0.5 - (d + in.params.z), 0.0, 1.0);
return premul(in.color) * inner + premul(in.color2) * (outer - inner);
}
// Soft shadow: params.y = radius, params.z = blur, params.w = expansion.
// color2.x > 0 cuts out the casting shape (offset up by color2.y) so the
// shadow doesn't show through translucent cards.
case 4u: {
let half = in.size * 0.5;
let inner = half - vec2<f32>(in.params.w);
let d = sd_round_box(in.local - half, inner, in.params.y);
var a = 1.0 - smoothstep(-in.params.z, in.params.z, d);
if (in.color2.x > 0.0) {
let hole = sd_round_box(in.local - half + vec2<f32>(0.0, in.color2.y), inner, in.params.y);
a *= clamp(hole + 0.5, 0.0, 1.0);
}
return premul(vec4<f32>(in.color.rgb, in.color.a * a));
}
// Glass card: params.y = radius, params.z = rim width, params.w = dark outline strength.
// color = tint, color2 = rim light (alpha = strength).
case 6u: {
let half = in.size * 0.5;
let r = in.params.y;
let p = in.local - half;
let d = sd_round_box(p, half, r);
let cover = clamp(0.5 - d, 0.0, 1.0);
if (cover <= 0.0) {
discard;
}
// Edge normal: from the nearest point on the inner (radius-shrunk) box.
let q = p - clamp(p, -(half - vec2<f32>(r)), half - vec2<f32>(r));
var n = p / max(half, vec2<f32>(1.0));
if (length(q) > 0.001) {
n = q / length(q);
} else {
n = select(vec2<f32>(sign(p.x), 0.0), vec2<f32>(0.0, sign(p.y)), abs(n.y) > abs(n.x));
}
// Light from the top left; the far edge catches a fainter reflection.
let l = dot(n, normalize(vec2<f32>(-0.55, -0.85)));
let shine = max(l, 0.0) + 0.45 * max(-l, 0.0);
let depth = -d;
let w = in.params.z;
// A dark hairline on the outside keeps the edge readable over light
// backdrops; a crisp rim just inside it reads over dark ones.
let outline = 1.0 - smoothstep(0.0, 1.0, depth);
let rim = smoothstep(0.5, 1.0, depth) * (1.0 - smoothstep(0.5 + w, 1.5 + w, depth));
let edge = rim * (0.55 + 0.45 * shine);
// Soft inner glow, strongest where the light hits.
let g = 1.0 - smoothstep(0.0, 12.0 * w, depth);
let glow = g * g * (0.3 + 0.7 * shine);
// Soft sheen over the top of the card.
let v = in.local.y / max(in.size.y, 1.0);
let sheen = (1.0 - smoothstep(0.0, 0.45, v)) * 0.07;
let light = clamp(edge + glow * 0.22 + sheen, 0.0, 1.0) * in.color2.a;
let tint = premul(in.color);
var lit = tint * (1.0 - light) + vec4<f32>(in.color2.rgb, 1.0) * light;
let dark = outline * in.params.w;
lit = lit * (1.0 - dark) + vec4<f32>(0.0, 0.0, 0.0, dark);
return lit * cover;
}
// Undercurl: wavy line across the rect.
case 5u: {
let wave = sin(in.local.x / max(in.size.y, 1.0) * 3.14159) * 0.5 + 0.5;
let center = (1.0 - wave) * (in.size.y - in.params.y) + in.params.y * 0.5;
let a = clamp(in.params.y * 0.5 + 0.5 - abs(in.local.y - center), 0.0, 1.0);
return premul(vec4<f32>(in.color.rgb, in.color.a * a));
}
default: {
return vec4<f32>(0.0);
}
}
}