Galactica/src/render/shaders/sprite.wgsl

91 lines
2.0 KiB
WebGPU Shading Language
Raw Normal View History

2023-12-22 16:51:21 -08:00
struct InstanceInput {
2023-12-23 23:24:04 -08:00
@location(2) rotation_matrix_0: vec2<f32>,
@location(3) rotation_matrix_1: vec2<f32>,
@location(4) position: vec2<f32>,
@location(5) size: f32,
2023-12-23 23:24:04 -08:00
@location(6) aspect: f32,
@location(7) texture_idx: 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>,
2023-12-22 16:51:21 -08:00
@location(1) index: u32,
}
2023-12-23 23:24:04 -08:00
@group(1) @binding(0)
var<uniform> global: GlobalUniform;
struct GlobalUniform {
camera_position: vec2<f32>,
camera_zoom: vec2<f32>,
camera_zoom_limits: vec2<f32>,
window_size: vec2<f32>,
window_aspect: vec2<f32>,
starfield_texture: vec2<u32>,
starfield_tile_size: vec2<f32>,
starfield_size_limits: vec2<f32>,
2023-12-23 23:24:04 -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>;
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
// Apply sprite aspect ratio & scale factor
// This must be done *before* rotation.
let scale = instance.size / global.camera_zoom.x;
2023-12-23 23:24:04 -08:00
var pos: vec2<f32> = vec2<f32>(
vertex.position.x * instance.aspect * scale,
vertex.position.y * scale
);
// Rotate
pos = mat2x2<f32>(
instance.rotation_matrix_0,
instance.rotation_matrix_1,
) * pos;
// Apply screen aspect ratio, again preserving height.
// This must be done AFTER rotation... think about it!
pos = pos / vec2<f32>(global.window_aspect.x, 1.0);
2023-12-23 23:24:04 -08:00
// Translate
pos = pos + (
// Don't forget to correct distance for screen aspect ratio too!
(instance.position / global.camera_zoom.x)
/ vec2<f32>(global.window_aspect.x, 1.0)
2023-12-22 16:51:21 -08:00
);
var out: VertexOutput;
2023-12-23 23:24:04 -08:00
out.position = vec4<f32>(pos, 0.0, 1.0);
out.texture_coords = vertex.texture_coords;
2023-12-22 16:51:21 -08:00
out.index = instance.texture_idx;
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(
texture_array[in.index],
sampler_array[in.index],
2023-12-23 11:01:27 -08:00
in.texture_coords,
2023-12-22 16:51:21 -08:00
0.0
).rgba;
}