struct SpriteParticle {
position: vec4<f32>,
velocity: vec4<f32>,
color: vec4<f32>,
size_lifetime: vec4<f32>,
emitter_data: vec4<f32>,
physics: vec4<f32>,
color_start: vec4<f32>,
color_end: vec4<f32>,
}
struct Uniforms {
view_projection: mat4x4<f32>,
screen_size: vec2<f32>,
atlas_slots_per_row: f32,
atlas_slot_uv_size: f32,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
@location(1) color: vec4<f32>,
@location(2) atlas_offset: vec2<f32>,
@location(3) atlas_slot_uv_size: f32,
}
@group(0) @binding(0)
var<uniform> uniforms: Uniforms;
@group(0) @binding(1)
var<storage, read> particles: array<SpriteParticle>;
@group(0) @binding(2)
var<storage, read> alive_indices: array<u32>;
@group(1) @binding(0)
var atlas_texture: texture_2d<f32>;
@group(1) @binding(1)
var atlas_sampler: sampler;
@vertex
fn vs_main(
@builtin(vertex_index) vertex_index: u32,
@builtin(instance_index) instance_index: u32
) -> VertexOutput {
let particle_index = alive_indices[instance_index];
let particle = particles[particle_index];
let quad_index = vertex_index % 6u;
var corner: vec2<f32>;
var local_uv: vec2<f32>;
switch (quad_index) {
case 0u: { corner = vec2<f32>(-0.5, -0.5); local_uv = vec2<f32>(0.0, 1.0); }
case 1u: { corner = vec2<f32>(0.5, -0.5); local_uv = vec2<f32>(1.0, 1.0); }
case 2u: { corner = vec2<f32>(0.5, 0.5); local_uv = vec2<f32>(1.0, 0.0); }
case 3u: { corner = vec2<f32>(-0.5, -0.5); local_uv = vec2<f32>(0.0, 1.0); }
case 4u: { corner = vec2<f32>(0.5, 0.5); local_uv = vec2<f32>(1.0, 0.0); }
case 5u: { corner = vec2<f32>(-0.5, 0.5); local_uv = vec2<f32>(0.0, 0.0); }
default: { corner = vec2<f32>(0.0); local_uv = vec2<f32>(0.0); }
}
let center = particle.position.xy;
let rotation = particle.position.z;
let current_size = particle.velocity.zw;
let cos_r = cos(rotation);
let sin_r = sin(rotation);
let scaled = corner * current_size;
let rotated = vec2<f32>(
scaled.x * cos_r - scaled.y * sin_r,
scaled.x * sin_r + scaled.y * cos_r
);
let world_pos = center + rotated;
let texture_slot = u32(particle.emitter_data.z);
let slots_per_row = u32(uniforms.atlas_slots_per_row);
let slot_col = texture_slot % slots_per_row;
let slot_row = texture_slot / slots_per_row;
let slot_uv_size = uniforms.atlas_slot_uv_size;
let atlas_offset = vec2<f32>(f32(slot_col) * slot_uv_size, f32(slot_row) * slot_uv_size);
var output: VertexOutput;
output.position = uniforms.view_projection * vec4<f32>(world_pos, 0.0, 1.0);
output.uv = local_uv;
output.color = particle.color;
output.atlas_offset = atlas_offset;
output.atlas_slot_uv_size = slot_uv_size;
return output;
}
@fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
let atlas_uv = input.atlas_offset + input.uv * input.atlas_slot_uv_size;
let tex_color = textureSample(atlas_texture, atlas_sampler, atlas_uv);
let final_color = tex_color * input.color;
if (final_color.a < 0.001) {
discard;
}
return final_color;
}
@fragment
fn fs_main_additive(input: VertexOutput) -> @location(0) vec4<f32> {
let atlas_uv = input.atlas_offset + input.uv * input.atlas_slot_uv_size;
let tex_color = textureSample(atlas_texture, atlas_sampler, atlas_uv);
let final_color = tex_color * input.color;
return final_color;
}