Another project
1struct Frame {
2 viewport_px: vec2<f32>,
3 _pad: vec2<f32>,
4};
5
6@group(0) @binding(0) var<uniform> u: Frame;
7@group(0) @binding(1) var atlas: texture_2d<f32>;
8@group(0) @binding(2) var atlas_sampler: sampler;
9
10struct VsOut {
11 @builtin(position) clip: vec4<f32>,
12 @location(0) uv: vec2<f32>,
13 @location(1) color: vec4<f32>,
14};
15
16@vertex
17fn vs(
18 @builtin(vertex_index) vid: u32,
19 @location(0) rect_xywh_px: vec4<f32>,
20 @location(1) uv_min: vec2<f32>,
21 @location(2) uv_max: vec2<f32>,
22 @location(3) color: vec4<f32>,
23) -> VsOut {
24 var corners = array<vec2<f32>, 6>(
25 vec2<f32>(0.0, 0.0),
26 vec2<f32>(1.0, 0.0),
27 vec2<f32>(0.0, 1.0),
28 vec2<f32>(1.0, 0.0),
29 vec2<f32>(1.0, 1.0),
30 vec2<f32>(0.0, 1.0),
31 );
32 let c = corners[vid];
33 let pos_px = rect_xywh_px.xy + c * rect_xywh_px.zw;
34 let uv = mix(uv_min, uv_max, c);
35 let ndc = vec2<f32>(
36 (pos_px.x / u.viewport_px.x) * 2.0 - 1.0,
37 1.0 - (pos_px.y / u.viewport_px.y) * 2.0,
38 );
39 var out: VsOut;
40 out.clip = vec4<f32>(ndc, 0.0, 1.0);
41 out.uv = uv;
42 out.color = color;
43 return out;
44}
45
46@fragment
47fn fs(in: VsOut) -> @location(0) vec4<f32> {
48 let alpha = textureSample(atlas, atlas_sampler, in.uv).r;
49 if alpha <= 0.0 {
50 discard;
51 }
52 return in.color * alpha;
53}