// INCLUDE: global uniform header struct InstanceInput { @location(2) position: vec2, @location(3) velocity: vec2, @location(4) rotation_0: vec2, @location(5) rotation_1: vec2, @location(6) size: f32, @location(7) created: f32, @location(8) expires: f32, @location(9) texture_index: u32, }; struct VertexInput { @location(0) position: vec3, @location(1) texture_coords: vec2, } struct VertexOutput { @builtin(position) position: vec4, @location(0) texture_coords: vec2, @location(1) texture_index: u32, } @group(0) @binding(0) var texture_array: binding_array>; @group(0) @binding(1) var sampler_array: binding_array; // Returns texture index fn animate(instance: InstanceInput) -> u32 { let age = global.current_time.x - instance.created; let len = sprites.data[instance.texture_index].frame_count; let rep = sprites.data[instance.texture_index].repeatmode; let fps = sprites.data[instance.texture_index].fps; var frame: u32 = u32(0); if rep == u32(1) { // Repeat frame = u32(fmod( (age / fps), f32(len) )); } else { // Once frame = u32(min( (age / fps), f32(len) - 1.0 )); } return frame + sprites.data[instance.texture_index].first_frame; } fn fmod(x: f32, m: f32) -> f32 { return x - floor(x / m) * m; } @vertex fn vertex_main( vertex: VertexInput, instance: InstanceInput, ) -> VertexOutput { var out: VertexOutput; out.texture_coords = vertex.texture_coords; if instance.expires < global.current_time.x { out.texture_index = u32(0); out.position = vec4(2.0, 2.0, 0.0, 1.0); return out; } let age = global.current_time.x - instance.created; let len = sprites.data[instance.texture_index].frame_count; let rep = sprites.data[instance.texture_index].repeatmode; let fps = sprites.data[instance.texture_index].fps; var frame: u32 = u32(0); if rep == u32(1) { // Repeat frame = u32(fmod( (age / fps), f32(len) )); } else { // Once frame = u32(min( (age / fps), f32(len) - 1.0 )); } let t = atlas.data[animate(instance)]; out.texture_index = u32(0); 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); } let rotation = mat2x2(instance.rotation_0, instance.rotation_1); var scale: f32 = instance.size / global.camera_zoom.x; var pos: vec2 = vec2(vertex.position.x, vertex.position.y); pos = pos * vec2( sprites.data[instance.texture_index].aspect * scale / global.window_aspect.x, scale ); pos = rotation * pos; var ipos: vec2 = ( vec2(instance.position.x, instance.position.y) + (instance.velocity * age) - global.camera_position ); pos = pos + vec2( ipos.x / (global.camera_zoom.x/2.0) / global.window_aspect.x, ipos.y / (global.camera_zoom.x/2.0) ); out.position = vec4(pos, 0.0, 1.0); return out; } @fragment fn fragment_main(in: VertexOutput) -> @location(0) vec4 { return textureSampleLevel( texture_array[in.texture_index], sampler_array[0], in.texture_coords, 0.0 ).rgba; }