164 lines
4.1 KiB
Plaintext
164 lines
4.1 KiB
Plaintext
// INCLUDE: global uniform header
|
|
|
|
struct InstanceInput {
|
|
@location(2) sprite_index: u32,
|
|
@location(3) object_index: u32,
|
|
};
|
|
|
|
struct VertexInput {
|
|
@location(0) position: vec3<f32>,
|
|
@location(1) texture_coords: vec2<f32>,
|
|
};
|
|
|
|
struct VertexOutput {
|
|
@builtin(position) position: vec4<f32>,
|
|
@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>,
|
|
};
|
|
|
|
|
|
@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
|
|
|
|
fn transform_vertex(obj: ObjectData, vertex_position: vec2<f32>, sprite_index: u32) -> vec4<f32> {
|
|
// Object scale
|
|
let scale = obj.size / (global_data.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 * global_sprites[sprite_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.
|
|
// (It's thus done later if this is a child)
|
|
if obj.is_child == u32(0) {
|
|
pos = vec2(
|
|
pos.x / global_data.window_aspect.x,
|
|
pos.y
|
|
);
|
|
}
|
|
|
|
// 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)
|
|
if obj.is_child == u32(0) {
|
|
let trans = (vec2(obj.xpos, obj.ypos) - global_data.camera_position) / obj.zpos;
|
|
pos = pos + vec2(
|
|
trans.x / (global_data.camera_zoom.x / 2.0) / global_data.window_aspect.x,
|
|
trans.y / (global_data.camera_zoom.x / 2.0)
|
|
);
|
|
}
|
|
|
|
if obj.is_child == u32(1) {
|
|
// Apply translation relative to parent
|
|
// Note that obj.zpos is ignored
|
|
pos = pos + vec2(
|
|
obj.xpos / (global_data.camera_zoom.x / 2.0) / global_data.window_aspect.x,
|
|
obj.ypos / (global_data.camera_zoom.x / 2.0)
|
|
);
|
|
|
|
let parent = objects[obj.parent];
|
|
|
|
// Apply parent's rotation
|
|
pos = mat2x2(
|
|
vec2(cos(parent.angle), sin(parent.angle)),
|
|
vec2(-sin(parent.angle), cos(parent.angle))
|
|
) * pos;
|
|
|
|
// Correct for screen aspect, preserving height
|
|
pos = vec2(
|
|
pos.x / global_data.window_aspect.x,
|
|
pos.y
|
|
);
|
|
|
|
// Apply parent's translation
|
|
let ptrans = (vec2(parent.xpos, parent.ypos) - global_data.camera_position) / parent.zpos;
|
|
pos = pos + vec2(
|
|
ptrans.x / (global_data.camera_zoom.x / 2.0) / global_data.window_aspect.x,
|
|
ptrans.y / (global_data.camera_zoom.x / 2.0)
|
|
);
|
|
}
|
|
|
|
return vec4<f32>(pos, 0.0, 1.0);;
|
|
}
|
|
|
|
@vertex
|
|
fn vertex_main(
|
|
vertex: VertexInput,
|
|
instance: InstanceInput,
|
|
) -> VertexOutput {
|
|
var out: VertexOutput;
|
|
|
|
|
|
|
|
out.position = transform_vertex(objects[instance.object_index], vertex.position.xy, instance.sprite_index);
|
|
|
|
|
|
// Compute texture coordinates
|
|
|
|
let age = global_data.current_time.x;
|
|
let frame = animate(instance.sprite_index, age, 0.0);
|
|
out.tween = fract(frame);
|
|
|
|
let t = global_atlas[u32(floor(frame))];
|
|
out.texture_index_a = u32(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);
|
|
}
|
|
|
|
let b = global_atlas[u32(floor(animate(instance.sprite_index, age, 1.0)))];
|
|
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);
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
|
|
@fragment
|
|
fn fragment_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
return mix(
|
|
textureSampleLevel(
|
|
texture_array[in.texture_index_a],
|
|
sampler_array[0],
|
|
in.texture_coords_a,
|
|
0.0
|
|
).rgba,
|
|
textureSampleLevel(
|
|
texture_array[in.texture_index_b],
|
|
sampler_array[0],
|
|
in.texture_coords_b,
|
|
0.0
|
|
).rgba,
|
|
in.tween
|
|
);
|
|
} |