Galactica/crates/render/shaders/ui.wgsl

73 lines
1.8 KiB
Plaintext
Raw Normal View History

// INCLUDE: global uniform header
2023-12-22 16:51:21 -08:00
struct InstanceInput {
2023-12-26 22:33:00 -08:00
@location(2) transform_matrix_0: vec4<f32>,
@location(3) transform_matrix_1: vec4<f32>,
@location(4) transform_matrix_2: vec4<f32>,
@location(5) transform_matrix_3: vec4<f32>,
2024-01-02 10:12:10 -08:00
@location(6) color_transform: vec4<f32>,
2024-01-06 14:02:50 -08:00
@location(7) sprite_index: 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-02 10:12:10 -08:00
@location(2) 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>;
// INCLUDE: animate.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
2023-12-26 22:33:00 -08:00
let transform = mat4x4<f32>(
instance.transform_matrix_0,
instance.transform_matrix_1,
instance.transform_matrix_2,
instance.transform_matrix_3,
2023-12-22 16:51:21 -08:00
);
var out: VertexOutput;
2023-12-26 22:33:00 -08:00
out.position = transform * vec4<f32>(vertex.position, 1.0);
2024-01-02 10:12:10 -08:00
out.color_transform = instance.color_transform;
// Pick texture frame
2024-01-06 14:02:50 -08:00
let t = global_atlas[u32(animate(instance.sprite_index, global_data.current_time.x, 0.0))];
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);
}
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> {
2023-12-22 16:51:21 -08:00
return 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;
2023-12-22 16:51:21 -08:00
}