2024-01-04 17:18:31 -08:00
|
|
|
// INCLUDE: global uniform header
|
|
|
|
|
2024-01-02 19:11:18 -08:00
|
|
|
struct InstanceInput {
|
2024-01-03 06:37:02 -08:00
|
|
|
@location(2) position: vec2<f32>,
|
|
|
|
@location(3) velocity: vec2<f32>,
|
|
|
|
@location(4) rotation_0: vec2<f32>,
|
|
|
|
@location(5) rotation_1: vec2<f32>,
|
|
|
|
@location(6) size: f32,
|
|
|
|
@location(7) created: f32,
|
|
|
|
@location(8) expires: f32,
|
2024-01-04 18:15:30 -08:00
|
|
|
@location(9) texture_index: u32,
|
2024-01-02 19:11:18 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
2024-01-02 19:11:18 -08:00
|
|
|
|
|
|
|
@vertex
|
|
|
|
fn vertex_main(
|
|
|
|
vertex: VertexInput,
|
|
|
|
instance: InstanceInput,
|
|
|
|
) -> VertexOutput {
|
|
|
|
|
|
|
|
var out: VertexOutput;
|
|
|
|
out.texture_coords = vertex.texture_coords;
|
|
|
|
|
2024-01-04 21:30:12 -08:00
|
|
|
// Skip expired particles
|
2024-01-02 19:11:18 -08:00
|
|
|
if instance.expires < global.current_time.x {
|
2024-01-04 17:18:31 -08:00
|
|
|
out.texture_index = u32(0);
|
2024-01-04 21:30:12 -08:00
|
|
|
// Draw off screen
|
2024-01-02 19:11:18 -08:00
|
|
|
out.position = vec4<f32>(2.0, 2.0, 0.0, 1.0);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2024-01-03 07:46:27 -08:00
|
|
|
let age = global.current_time.x - instance.created;
|
|
|
|
|
2024-01-04 21:30:12 -08:00
|
|
|
// Apply transformations
|
2024-01-03 06:37:02 -08:00
|
|
|
let rotation = mat2x2(instance.rotation_0, instance.rotation_1);
|
2024-01-02 19:11:18 -08:00
|
|
|
var scale: f32 = instance.size / global.camera_zoom.x;
|
|
|
|
var pos: vec2<f32> = vec2(vertex.position.x, vertex.position.y);
|
|
|
|
pos = pos * vec2<f32>(
|
2024-01-04 18:15:30 -08:00
|
|
|
sprites.data[instance.texture_index].aspect * scale / global.window_aspect.x,
|
2024-01-02 19:11:18 -08:00
|
|
|
scale
|
|
|
|
);
|
2024-01-04 21:30:12 -08:00
|
|
|
|
2024-01-03 06:37:02 -08:00
|
|
|
pos = rotation * pos;
|
|
|
|
|
|
|
|
var ipos: vec2<f32> = (
|
|
|
|
vec2(instance.position.x, instance.position.y)
|
|
|
|
+ (instance.velocity * age)
|
|
|
|
- global.camera_position
|
|
|
|
);
|
2024-01-02 19:11:18 -08:00
|
|
|
|
|
|
|
pos = pos + vec2<f32>(
|
|
|
|
ipos.x / (global.camera_zoom.x/2.0) / global.window_aspect.x,
|
|
|
|
ipos.y / (global.camera_zoom.x/2.0)
|
|
|
|
);
|
|
|
|
|
2024-01-03 06:37:02 -08:00
|
|
|
out.position = vec4<f32>(pos, 0.0, 1.0);
|
2024-01-04 21:30:12 -08:00
|
|
|
|
|
|
|
|
|
|
|
// Compute texture coordinates
|
|
|
|
let t = atlas.data[animate(instance, age)];
|
2024-01-04 22:17:34 -08:00
|
|
|
out.texture_index = u32(t.atlas_texture);
|
2024-01-04 21:30:12 -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);
|
|
|
|
}
|
|
|
|
|
2024-01-02 19:11:18 -08:00
|
|
|
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;
|
|
|
|
}
|