2024-01-04 17:18:31 -08:00
|
|
|
// INCLUDE: global uniform header
|
|
|
|
|
2023-12-22 16:51:21 -08:00
|
|
|
struct InstanceInput {
|
2024-02-03 12:46:24 -08:00
|
|
|
@location(2) position: vec2<f32>,
|
|
|
|
@location(3) angle: f32,
|
|
|
|
@location(4) dim: vec2<f32>,
|
|
|
|
@location(5) color_transform: vec4<f32>,
|
|
|
|
@location(6) texture_index: vec2<u32>,
|
|
|
|
@location(7) texture_fade: f32,
|
|
|
|
@location(8) 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>,
|
2024-01-25 21:24:37 -08:00
|
|
|
@location(0) tween: f32,
|
|
|
|
@location(1) texture_index_a: u32,
|
|
|
|
@location(2) texture_coords_a: vec2<f32>,
|
|
|
|
@location(3) texture_index_b: u32,
|
|
|
|
@location(4) texture_coords_b: vec2<f32>,
|
|
|
|
@location(5) color: vec4<f32>,
|
|
|
|
|
|
|
|
@location(6) mask_coords: vec2<f32>,
|
|
|
|
@location(7) mask_index: vec2<u32>,
|
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-21 12:49:20 -08:00
|
|
|
fn transform_vertex(
|
|
|
|
instance: InstanceInput,
|
|
|
|
vertex_position: vec3<f32>,
|
|
|
|
texture_index: u32,
|
|
|
|
) -> vec4<f32> {
|
|
|
|
|
2024-01-30 17:06:14 -08:00
|
|
|
// Window size in logical pixels
|
2024-01-27 14:49:34 -08:00
|
|
|
let window_dim = (
|
|
|
|
vec2(global_data.window_size_w, global_data.window_size_h)
|
|
|
|
/ global_data.window_scale
|
|
|
|
);
|
|
|
|
|
2024-01-30 17:06:14 -08:00
|
|
|
let scale = instance.dim.y / window_dim.y;
|
2024-01-08 17:57:49 -08:00
|
|
|
|
|
|
|
var pos: vec2<f32> = vec2(
|
2024-01-30 17:06:14 -08:00
|
|
|
vertex_position.x * scale * (instance.dim.x / instance.dim.y),
|
2024-01-21 12:49:20 -08:00
|
|
|
vertex_position.y * scale
|
2024-01-08 17:57:49 -08:00
|
|
|
);
|
|
|
|
|
2024-02-03 12:46:24 -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(
|
2024-01-27 14:49:34 -08:00
|
|
|
pos.x / global_data.window_aspect,
|
2024-01-08 17:57:49 -08:00
|
|
|
pos.y
|
|
|
|
);
|
|
|
|
|
2024-02-03 12:46:24 -08:00
|
|
|
pos = pos + (instance.position / window_dim) * 2.0;
|
2023-12-22 16:51:21 -08:00
|
|
|
|
2024-01-21 12:49:20 -08:00
|
|
|
return vec4<f32>(pos, 0.0, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@vertex
|
|
|
|
fn vertex_main(
|
|
|
|
vertex: VertexInput,
|
|
|
|
instance: InstanceInput,
|
|
|
|
) -> VertexOutput {
|
2024-01-25 21:24:37 -08:00
|
|
|
var out: VertexOutput;
|
2024-01-21 12:49:20 -08:00
|
|
|
|
2024-01-25 21:24:37 -08:00
|
|
|
// Pick texture size by the size of the visible texture
|
|
|
|
// (texture index 0 is special, it's the "hidden" texture)
|
|
|
|
if instance.texture_index.x == 0u && instance.texture_index.y == 0u {
|
|
|
|
out.position = vec4<f32>(0.0, 0.0, 0.0, 1.0);
|
|
|
|
} else if instance.texture_index.x == 0u {
|
|
|
|
out.position = transform_vertex(
|
|
|
|
instance,
|
|
|
|
vertex.position,
|
|
|
|
instance.texture_index.y,
|
|
|
|
);
|
|
|
|
} else if instance.texture_index.y == 0u {
|
|
|
|
out.position = transform_vertex(
|
|
|
|
instance,
|
|
|
|
vertex.position,
|
|
|
|
instance.texture_index.x,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
out.position = transform_vertex(
|
|
|
|
instance,
|
|
|
|
vertex.position,
|
|
|
|
instance.texture_index.x,
|
|
|
|
);
|
|
|
|
}
|
2024-01-04 17:18:31 -08:00
|
|
|
|
2024-01-25 21:24:37 -08:00
|
|
|
out.color = instance.color_transform;
|
|
|
|
out.tween = instance.texture_fade;
|
2024-01-08 17:57:49 -08:00
|
|
|
|
2024-01-25 21:24:37 -08:00
|
|
|
// Texture 0 is special, it's the empty texture
|
|
|
|
if instance.texture_index.x == 0u {
|
|
|
|
out.texture_index_a = 0u;
|
|
|
|
out.texture_coords_a = vec2(0.0, 0.0);
|
|
|
|
} else {
|
|
|
|
let t = global_atlas[instance.texture_index.x];
|
|
|
|
out.texture_index_a = t.atlas_texture;
|
|
|
|
out.texture_coords_a = vec2(t.xpos, t.ypos);
|
|
|
|
if vertex.texture_coords.x == 1.0 {
|
|
|
|
out.texture_coords_a = out.texture_coords_a + vec2(t.width, 0.0);
|
|
|
|
}
|
|
|
|
if vertex.texture_coords.y == 1.0 {
|
|
|
|
out.texture_coords_a = out.texture_coords_a + vec2(0.0, t.height);
|
|
|
|
}
|
2024-01-04 17:18:31 -08:00
|
|
|
}
|
2024-01-25 21:24:37 -08:00
|
|
|
|
|
|
|
if instance.texture_index.y == 0u {
|
|
|
|
out.texture_index_b = u32(0u);
|
|
|
|
out.texture_coords_b = vec2(0.0, 0.0);
|
|
|
|
} else {
|
|
|
|
let b = global_atlas[instance.texture_index.y];
|
|
|
|
out.texture_index_b = u32(b.atlas_texture);
|
|
|
|
out.texture_coords_b = vec2(b.xpos, b.ypos);
|
|
|
|
if vertex.texture_coords.x == 1.0 {
|
|
|
|
out.texture_coords_b = out.texture_coords_b + vec2(b.width, 0.0);
|
|
|
|
}
|
|
|
|
if vertex.texture_coords.y == 1.0 {
|
|
|
|
out.texture_coords_b = out.texture_coords_b + vec2(0.0, b.height);
|
|
|
|
}
|
2024-01-04 17:18:31 -08:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-01-20 10:04:09 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-25 21:24:37 -08:00
|
|
|
var texture_a: vec4<f32> = vec4(0.0, 0.0, 0.0, 0.0);
|
|
|
|
if !(
|
|
|
|
(in.texture_index_a == 0u) &&
|
|
|
|
(in.texture_coords_a.x == 0.0) &&
|
|
|
|
(in.texture_coords_a.y == 0.0)
|
|
|
|
) {
|
|
|
|
texture_a = textureSampleLevel(
|
|
|
|
texture_array[in.texture_index_a],
|
|
|
|
sampler_array[0],
|
|
|
|
in.texture_coords_a,
|
|
|
|
0.0
|
|
|
|
).rgba;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var texture_b: vec4<f32> = vec4(0.0, 0.0, 0.0, 0.0);
|
|
|
|
if !(
|
|
|
|
(in.texture_index_b == 0u) &&
|
|
|
|
(in.texture_coords_b.x == 0.0) &&
|
|
|
|
(in.texture_coords_b.y == 0.0)
|
|
|
|
) {
|
|
|
|
texture_b = textureSampleLevel(
|
|
|
|
texture_array[in.texture_index_b],
|
|
|
|
sampler_array[0],
|
|
|
|
in.texture_coords_b,
|
|
|
|
0.0
|
|
|
|
).rgba;
|
|
|
|
}
|
|
|
|
|
|
|
|
var color: vec4<f32> = mix(
|
|
|
|
texture_a,
|
|
|
|
texture_b,
|
|
|
|
in.tween
|
|
|
|
) * in.color;
|
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
|
|
|
}
|