Galactica/crates/render/shaders/ui.wgsl

99 lines
2.3 KiB
Plaintext

// INCLUDE: global uniform header
struct InstanceInput {
@location(2) anchor: u32,
@location(3) position: vec2<f32>,
@location(4) angle: f32,
@location(5) size: f32,
@location(6) color_transform: vec4<f32>,
@location(7) sprite_index: u32,
};
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) texture_coords: vec2<f32>,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) texture_coords: vec2<f32>,
@location(1) texture_index: u32,
@location(2) color_transform: vec4<f32>,
}
@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
// INCLUDE: anchor.wgsl
@vertex
fn vertex_main(
vertex: VertexInput,
instance: InstanceInput,
) -> VertexOutput {
let window_dim = global_data.window_size / global_data.window_scale.x;
let scale = instance.size / window_dim.y;
let sprite_aspect = global_sprites[instance.sprite_index].aspect;
// 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 * sprite_aspect,
vertex.position.y * scale
);
// Apply rotation
pos = mat2x2(
vec2(cos(instance.angle), sin(instance.angle)),
vec2(-sin(instance.angle), cos(instance.angle))
) * 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 * sprite_aspect, instance.size)
);
var out: VertexOutput;
out.position = vec4<f32>(pos, 1.0, 1.0);
out.color_transform = instance.color_transform;
// TODO: animate
// Pick texture frame
let t = global_atlas[u32(animate(instance.sprite_index, global_data.current_time.x, 0.0))];
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);
}
return out;
}
@fragment
fn fragment_main(in: VertexOutput) -> @location(0) vec4<f32> {
return textureSampleLevel(
texture_array[in.texture_index],
sampler_array[0],
in.texture_coords,
0.0
).rgba * in.color_transform;
}