bevy_flash 0.1.2

A Bevy plugin for Flash Animation.
Documentation
#import bevy_flash::common::{
    linear_to_srgb,
    srgb_to_linear,
    get_world_from_local,
    mesh2d_position_local_to_world,
    mesh2d_position_world_to_clip,
    part_mesh2d_color_transform,
}

struct Gradient {
    focal_point: f32,
    interpolation: i32,
    shape: i32,
    repeat: i32,
}

@group(2) @binding(0) var texture: texture_2d<f32>;
@group(2) @binding(1) var texture_sampler: sampler;
@group(2) @binding(2) var<uniform> gradient: Gradient;
@group(2) @binding(3) var<uniform> texture_transform: mat4x4<f32>;

struct Vertex {
    @builtin(instance_index) instance_index: u32,
    @location(0) position: vec3<f32>,
};

struct VertexOutput {
    // The clip-space position of the vertex.
    @builtin(position) position: vec4<f32>,
    // The color of the vertex.
    @location(0) uv: vec2<f32>,
    @location(1) instance_index: u32,
};


@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
    var out: VertexOutput;
    out.uv = (mat3x3<f32>(texture_transform[0].xyz, texture_transform[1].xyz, texture_transform[2].xyz) * vec3<f32>(vertex.position.x, vertex.position.y, 1.0)).xy;
    var world_from_local = get_world_from_local(vertex.instance_index);
    let world_position = mesh2d_position_local_to_world(
        world_from_local,
        vec4<f32>(vertex.position, 1.0)
    );
    out.position = mesh2d_position_world_to_clip(world_position);
    out.position.x = out.position.x - out.position.w;
    out.position.y = out.position.y + out.position.w;
    out.instance_index = vertex.instance_index;
    return out;
}

fn find_t(uv: vec2<f32>) -> f32 {
    if gradient.shape == 1 {
        // linear
        return uv.x;
    } if gradient.shape == 2 {
        // radial
        return length(uv * 2.0 - 1.0);
    } else {
        // focal
        let uv = uv * 2.0 - 1.0;
        var d: vec2<f32> = vec2<f32>(gradient.focal_point, 0.0) - uv;
        let l = length(d);
        d = d / l;
        return l / (sqrt(1.0 - gradient.focal_point * gradient.focal_point * d.y * d.y) + gradient.focal_point * d.x);
    }
}

@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
    var t: f32 = find_t(in.uv);
    if gradient.repeat == 1 {
        // Pad
        t = saturate(t);
    } else if gradient.repeat == 2 {
        // Reflect
        if t < 0.0 {
            t = -t;
        }
        if (i32(t) & 1) == 0 {
            t = fract(t);
        } else {
            t = 1.0 - fract(t);
        }
    } else if gradient.repeat == 3 {
        // Repeat
        t = fract(t);
    }
    var color = textureSample(texture, texture_sampler, vec2<f32>(t, 0.0));
    if gradient.interpolation != 0 {
        color = linear_to_srgb(color);
    }
    let out = saturate(part_mesh2d_color_transform(in.instance_index, color));
    let alpha = saturate(out.a);
    return vec4<f32>(out.rgb * alpha, alpha);
}