struct SkinnedVertexInput {
    @location(0) position: vec3<f32>,
    @location(1) normal: vec3<f32>,
    @location(2) tex_coords: vec2<f32>,
    @location(3) tex_coords_1: vec2<f32>,
    @location(4) tangent: vec4<f32>,
    @location(5) color: vec4<f32>,
    @location(6) joint_indices: vec4<u32>,
    @location(7) joint_weights: vec4<f32>,
};

struct VertexOutput {
    @builtin(position) position: vec4<f32>,
};

struct Uniforms {
    view: mat4x4<f32>,
    projection: mat4x4<f32>,
    snap_resolution: vec2<f32>,
    snap_enabled: u32,
    _padding: u32,
};

struct SkinnedObjectData {
    transform_index: u32,
    mesh_id: u32,
    material_id: u32,
    joint_offset: u32,
};

@group(0) @binding(0)
var<uniform> uniforms: Uniforms;

@group(1) @binding(0)
var<storage, read> objects: array<SkinnedObjectData>;

@group(1) @binding(1)
var<storage, read> joint_matrices: array<mat4x4<f32>>;

@vertex
fn vs_main(
    in: SkinnedVertexInput,
    @builtin(instance_index) instance_index: u32,
) -> VertexOutput {
    let object = objects[instance_index];

    var skinned_position = vec3<f32>(0.0, 0.0, 0.0);

    let joint_offset = object.joint_offset;

    for (var index = 0u; index < 4u; index = index + 1u) {
        let joint_index = in.joint_indices[index];
        let joint_weight = in.joint_weights[index];

        if (joint_weight > 0.0) {
            let joint_matrix = joint_matrices[joint_offset + joint_index];

            let transformed_pos = joint_matrix * vec4<f32>(in.position, 1.0);
            skinned_position = skinned_position + transformed_pos.xyz * joint_weight;
        }
    }

    let world_pos = vec4<f32>(skinned_position, 1.0);
    var clip_pos = uniforms.projection * uniforms.view * world_pos;

    if uniforms.snap_enabled == 1u {
        let resolution = uniforms.snap_resolution;
        let snapped_x = round(clip_pos.x * resolution.x / clip_pos.w) * clip_pos.w / resolution.x;
        let snapped_y = round(clip_pos.y * resolution.y / clip_pos.w) * clip_pos.w / resolution.y;
        clip_pos.x = snapped_x;
        clip_pos.y = snapped_y;
    }

    var out: VertexOutput;
    out.position = clip_pos;

    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) f32 {
    return 1.0;
}
