Galactica/crates/render/shaders/particle.wgsl

141 lines
3.5 KiB
Plaintext
Raw Normal View History

// 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>,
2024-01-06 14:02:50 -08:00
@location(4) angle: f32,
@location(5) angvel: f32,
2024-01-03 06:37:02 -08:00
@location(6) size: f32,
@location(7) created: f32,
@location(8) expires: f32,
@location(9) fade: f32,
@location(10) texture_index: vec2<u32>,
@location(11) texture_fade: f32,
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>,
2024-01-06 14:02:50 -08:00
@location(0) tween: f32,
@location(1) fade: f32,
@location(2) texture_index_a: u32,
@location(3) texture_coords_a: vec2<f32>,
@location(4) texture_index_b: u32,
@location(5) texture_coords_b: vec2<f32>,
2024-01-02 19:11:18 -08:00
}
@group(0) @binding(0)
var texture_array: binding_array<texture_2d<f32>>;
@group(0) @binding(1)
var sampler_array: binding_array<sampler>;
@vertex
fn vertex_main(
vertex: VertexInput,
instance: InstanceInput,
) -> VertexOutput {
var out: VertexOutput;
// Skip expired particles
2024-01-06 14:02:50 -08:00
if instance.expires < global_data.current_time.x {
out.tween = 0.0;
out.texture_index_a = u32(0);
out.texture_index_b = u32(0);
out.texture_coords_a = vec2(0.0, 0.0);
out.texture_coords_b = vec2(0.0, 0.0);
2024-01-02 19:11:18 -08:00
out.position = vec4<f32>(2.0, 2.0, 0.0, 1.0);
return out;
}
2024-01-06 14:02:50 -08:00
let age = global_data.current_time.x - instance.created;
2024-01-03 07:46:27 -08:00
// Apply transformations
2024-01-06 14:02:50 -08:00
let angle = instance.angle + age * instance.angvel;
2024-01-06 16:00:40 -08:00
2024-01-06 14:02:50 -08:00
var scale: f32 = instance.size / global_data.camera_zoom.x;
2024-01-06 16:00:40 -08:00
var pos: vec2<f32> = vec2(
vertex.position.x * scale * global_sprites[instance.sprite_index].aspect,
vertex.position.y * scale
2024-01-02 19:11:18 -08:00
);
2024-01-13 18:57:02 -08:00
// Correct angle, since sprites point north and zero degrees is east
2024-01-06 16:00:40 -08:00
pos = mat2x2(
2024-01-13 18:57:02 -08:00
vec2(cos(angle - 1.5708), sin(angle - 1.5708)),
vec2(-sin(angle - 1.5708), cos(angle - 1.5708))
2024-01-06 16:00:40 -08:00
) * pos;
2024-01-03 06:37:02 -08:00
2024-01-06 16:00:40 -08:00
var trans: vec2<f32> = (instance.position + (instance.velocity * age) - global_data.camera_position);
pos = vec2(
pos.x / global_data.window_aspect.x,
pos.y
2024-01-03 06:37:02 -08:00
);
2024-01-02 19:11:18 -08:00
2024-01-06 16:00:40 -08:00
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)
2024-01-02 19:11:18 -08:00
);
2024-01-06 16:00:40 -08:00
out.position = vec4(pos, 0.0, 1.0);
if instance.expires - global_data.current_time.x <= instance.fade {
out.fade = (instance.expires - global_data.current_time.x) / instance.fade;
} else {
out.fade = 1.0;
}
// Compute texture coordinates
2024-01-06 14:02:50 -08:00
let frame = animate(instance.sprite_index, age, 0.0);
out.tween = instance.texture_fade;
2024-01-06 14:02:50 -08:00
let t = global_atlas[instance.texture_index.x];
2024-01-06 14:02:50 -08:00
out.texture_index_a = u32(t.atlas_texture);
out.texture_coords_a = vec2(t.xpos, t.ypos);
if vertex.texture_coords.x == 1.0 {
2024-01-06 14:02:50 -08:00
out.texture_coords_a = out.texture_coords_a + vec2(t.width, 0.0);
}
if vertex.texture_coords.y == 1.0 {
2024-01-06 14:02:50 -08:00
out.texture_coords_a = out.texture_coords_a + vec2(0.0, t.height);
}
let b = global_atlas[instance.texture_index.y];
2024-01-06 14:02:50 -08:00
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-02 19:11:18 -08:00
return out;
}
@fragment
fn fragment_main(in: VertexOutput) -> @location(0) vec4<f32> {
2024-01-06 14:02:50 -08:00
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
) * vec4(1.0, 1.0, 1.0, in.fade);
2024-01-02 19:11:18 -08:00
}