Galactica/crates/render/shaders/ui.wgsl

143 lines
3.5 KiB
Plaintext
Raw Normal View History

// INCLUDE: global uniform header
2023-12-22 16:51:21 -08:00
struct InstanceInput {
2024-01-08 17:57:49 -08:00
@location(2) anchor: u32,
@location(3) position: vec2<f32>,
@location(4) angle: f32,
@location(5) size: f32,
2024-01-02 10:12:10 -08:00
@location(6) color_transform: vec4<f32>,
@location(7) texture_index: vec2<u32>,
@location(8) texture_fade: f32,
@location(9) mask_index: vec2<u32>,
2023-12-22 16:51:21 -08:00
};
struct VertexInput {
@location(0) position: vec3<f32>,
2023-12-23 11:01:27 -08:00
@location(1) texture_coords: vec2<f32>,
2023-12-22 16:51:21 -08:00
}
struct VertexOutput {
2023-12-23 23:24:04 -08:00
@builtin(position) position: vec4<f32>,
2023-12-23 11:01:27 -08:00
@location(0) texture_coords: vec2<f32>,
2024-01-02 19:11:18 -08:00
@location(1) texture_index: u32,
2024-01-17 13:27:32 -08:00
@location(2) mask_coords: vec2<f32>,
@location(3) mask_index: vec2<u32>,
@location(4) color_transform: vec4<f32>,
2023-12-22 16:51:21 -08:00
}
2023-12-25 11:17:08 -08:00
@group(0) @binding(0)
var texture_array: binding_array<texture_2d<f32>>;
@group(0) @binding(1)
var sampler_array: binding_array<sampler>;
2024-01-08 17:57:49 -08:00
// INCLUDE: anchor.wgsl
2023-12-25 11:17:08 -08:00
2023-12-22 16:51:21 -08:00
@vertex
2023-12-25 11:17:08 -08:00
fn vertex_main(
2023-12-23 23:24:04 -08:00
vertex: VertexInput,
2023-12-22 16:51:21 -08:00
instance: InstanceInput,
) -> VertexOutput {
2023-12-23 23:24:04 -08:00
2024-01-08 17:57:49 -08:00
let window_dim = global_data.window_size / global_data.window_scale.x;
let scale = instance.size / window_dim.y;
let aspect = (
global_atlas[instance.texture_index.x].width /
global_atlas[instance.texture_index.x].height
);
2024-01-08 17:57:49 -08:00
// Apply scale and sprite aspect
// Note that our mesh starts centered at (0, 0). This is important!
var pos: vec2<f32> = vec2(
vertex.position.x * scale * aspect,
2024-01-08 17:57:49 -08:00
vertex.position.y * scale
);
2024-01-14 10:38:07 -08:00
// Apply rotation (and adjust sprite angle, since sprites point north)
2024-01-08 17:57:49 -08:00
pos = mat2x2(
2024-01-14 10:38:07 -08:00
vec2(cos(instance.angle - 1.5708), sin(instance.angle - 1.5708)),
vec2(-sin(instance.angle - 1.5708), cos(instance.angle - 1.5708))
2024-01-08 17:57:49 -08:00
) * pos;
// Correct for screen aspect, preserving height
pos = vec2(
pos.x / global_data.window_aspect.x,
pos.y
);
pos = pos + anchor(
instance.anchor,
instance.position,
vec2(instance.size * aspect, instance.size)
2023-12-22 16:51:21 -08:00
);
var out: VertexOutput;
2024-01-08 17:57:49 -08:00
out.position = vec4<f32>(pos, 1.0, 1.0);
2024-01-02 10:12:10 -08:00
out.color_transform = instance.color_transform;
2024-01-08 17:57:49 -08:00
2024-01-17 13:27:32 -08:00
// TODO: function to get texture from sprite
// Pick texture frame
let t = global_atlas[instance.texture_index.x];
2024-01-04 22:17:34 -08:00
out.texture_index = u32(t.atlas_texture);
out.texture_coords = vec2(t.xpos, t.ypos);
if vertex.texture_coords.x == 1.0 {
out.texture_coords = vec2(out.texture_coords.x + t.width, out.texture_coords.y);
}
if vertex.texture_coords.y == 1.0 {
out.texture_coords = vec2(out.texture_coords.x, out.texture_coords.y + t.height);
}
2024-01-17 13:27:32 -08:00
// Pick mask image if mask is enabled
// x coordinate of mask index is either 0 or 1, telling us whether or not to use a mask.
// y coordinate is mask sprite index
if instance.mask_index.x == 1u {
let m = global_atlas[instance.mask_index.y];
2024-01-17 13:27:32 -08:00
out.mask_index = vec2(1u, u32(m.atlas_texture));
out.mask_coords = vec2(m.xpos, m.ypos);
if vertex.texture_coords.x == 1.0 {
out.mask_coords = vec2(out.mask_coords.x + m.width, out.mask_coords.y);
}
if vertex.texture_coords.y == 1.0 {
out.mask_coords = vec2(out.mask_coords.x, out.mask_coords.y + m.height);
}
} else {
out.mask_coords = vec2(0.0, 0.0);
out.mask_index = vec2(0u, 0u);
}
2023-12-22 16:51:21 -08:00
return out;
}
@fragment
2023-12-25 11:17:08 -08:00
fn fragment_main(in: VertexOutput) -> @location(0) vec4<f32> {
2024-01-17 13:27:32 -08:00
var mask: f32 = 1.0;
if in.mask_index.x == 1u {
mask = textureSampleLevel(
texture_array[in.mask_index.y],
sampler_array[0],
in.mask_coords,
0.0
).a;
}
var color: vec4<f32> = textureSampleLevel(
2024-01-02 19:11:18 -08:00
texture_array[in.texture_index],
2023-12-25 18:24:55 -08:00
sampler_array[0],
2023-12-23 11:01:27 -08:00
in.texture_coords,
2023-12-22 16:51:21 -08:00
0.0
2024-01-02 10:12:10 -08:00
).rgba * in.color_transform;
2024-01-17 13:27:32 -08:00
// Apply mask and discard fully transparent pixels
color = vec4(color.rgb, color.a *mask);
if color.a == 0.0 {
discard;
}
return color;
2023-12-22 16:51:21 -08:00
}