// INCLUDE: global uniform header struct InstanceInput { @location(2) position: vec2, @location(3) velocity: vec2, @location(4) angle: f32, @location(5) angvel: f32, @location(6) size: f32, @location(7) created: f32, @location(8) expires: f32, @location(9) sprite_index: u32, }; struct VertexInput { @location(0) position: vec3, @location(1) texture_coords: vec2, } struct VertexOutput { @builtin(position) position: vec4, @location(0) tween: f32, @location(1) texture_index_a: u32, @location(2) texture_coords_a: vec2, @location(3) texture_index_b: u32, @location(4) texture_coords_b: vec2, } @group(0) @binding(0) var texture_array: binding_array>; @group(0) @binding(1) var sampler_array: binding_array; // INCLUDE: animate.wgsl @vertex fn vertex_main( vertex: VertexInput, instance: InstanceInput, ) -> VertexOutput { var out: VertexOutput; // Skip expired particles 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); out.position = vec4(2.0, 2.0, 0.0, 1.0); return out; } let age = global_data.current_time.x - instance.created; // Apply transformations let angle = instance.angle + age * instance.angvel; let rotation = mat2x2( vec2(cos(angle), sin(angle)), vec2(-sin(angle), cos(angle)) ); var scale: f32 = instance.size / global_data.camera_zoom.x; var pos: vec2 = vec2(vertex.position.x, vertex.position.y); pos = pos * vec2( global_sprites[instance.sprite_index].aspect * scale / global_data.window_aspect.x, scale ); pos = rotation * pos; var ipos: vec2 = ( vec2(instance.position.x, instance.position.y) + (instance.velocity * age) - global_data.camera_position ); pos = pos + vec2( ipos.x / (global_data.camera_zoom.x/2.0) / global_data.window_aspect.x, ipos.y / (global_data.camera_zoom.x/2.0) ); out.position = vec4(pos, 0.0, 1.0); // Compute texture coordinates 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 { 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 ); }