2024-01-04 17:18:31 -08:00
|
|
|
// INCLUDE: global uniform header
|
|
|
|
|
2023-12-22 16:51:21 -08:00
|
|
|
struct InstanceInput {
|
2024-01-05 19:56:26 -08:00
|
|
|
@location(2) texture_index: u32,
|
|
|
|
@location(3) object_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>,
|
2024-01-04 17:18:31 -08:00
|
|
|
};
|
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,
|
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>;
|
|
|
|
|
|
|
|
|
2024-01-04 21:30:12 -08:00
|
|
|
// INCLUDE: animate.wgsl
|
2023-12-25 11:17:08 -08:00
|
|
|
|
2024-01-05 19:56:26 -08:00
|
|
|
fn transform_vertex(obj: ObjectLocation, vertex: VertexInput, instance: InstanceInput) -> vec4<f32> {
|
|
|
|
// Object scale
|
|
|
|
let scale = obj.size / (global.camera_zoom.x * obj.zpos);
|
|
|
|
|
|
|
|
// 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 * sprites[instance.texture_index].aspect,
|
|
|
|
vertex.position.y * scale
|
|
|
|
);
|
|
|
|
|
|
|
|
// Apply rotation
|
|
|
|
pos = mat2x2(
|
|
|
|
vec2(cos(obj.angle), sin(obj.angle)),
|
|
|
|
vec2(-sin(obj.angle), cos(obj.angle))
|
|
|
|
) * pos;
|
|
|
|
|
|
|
|
// Correct for screen aspect, preserving height
|
|
|
|
// This must be done AFTER rotation.
|
|
|
|
pos = vec2(
|
|
|
|
pos.x / global.window_aspect.x,
|
|
|
|
pos.y
|
|
|
|
);
|
|
|
|
|
|
|
|
// Distance-adjusted world position
|
|
|
|
let trans = (vec2(obj.xpos, obj.ypos) - global.camera_position) / obj.zpos;
|
|
|
|
|
|
|
|
// Finally, translate
|
|
|
|
//
|
|
|
|
// Note that we divide camera zoom by two.
|
|
|
|
// The height of the viewport is `zoom` in game units,
|
|
|
|
// but it's 2 in screen units (since coordinates range from -1 to 1)
|
|
|
|
pos = pos + vec2(
|
|
|
|
trans.x / (global.camera_zoom.x / 2.0) / global.window_aspect.x,
|
|
|
|
trans.y / (global.camera_zoom.x / 2.0)
|
|
|
|
);
|
|
|
|
|
|
|
|
return vec4<f32>(pos, 0.0, 1.0);;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
var out: VertexOutput;
|
2024-01-05 19:56:26 -08:00
|
|
|
out.position = transform_vertex(objects[instance.object_index], vertex, instance);
|
2024-01-04 17:18:31 -08:00
|
|
|
|
2024-01-05 19:56:26 -08:00
|
|
|
let t = atlas[animate(instance, global.current_time.x)];
|
2024-01-04 22:17:34 -08:00
|
|
|
out.texture_index = t.atlas_texture;
|
2024-01-04 17:18:31 -08:00
|
|
|
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
|
|
|
|
).rgba;
|
|
|
|
}
|