Simplify image rotation API and its internals

This commit is contained in:
Héctor Ramón Jiménez 2024-05-02 15:21:22 +02:00
parent 09a6bcfffc
commit a57313b23e
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
23 changed files with 219 additions and 225 deletions

View file

@ -10,12 +10,11 @@ struct VertexInput {
@builtin(vertex_index) vertex_index: u32,
@location(0) pos: vec2<f32>,
@location(1) center: vec2<f32>,
@location(2) image_size: vec2<f32>,
@location(2) scale: vec2<f32>,
@location(3) rotation: f32,
@location(4) scale: vec2<f32>,
@location(5) atlas_pos: vec2<f32>,
@location(6) atlas_scale: vec2<f32>,
@location(7) layer: i32,
@location(4) atlas_pos: vec2<f32>,
@location(5) atlas_scale: vec2<f32>,
@location(6) layer: i32,
}
struct VertexOutput {
@ -36,7 +35,7 @@ fn vs_main(input: VertexInput) -> VertexOutput {
out.layer = f32(input.layer);
// Calculate the vertex position and move the center to the origin
v_pos = input.pos + v_pos * input.image_size - input.center;
v_pos = input.pos + v_pos * input.scale - input.center;
// Apply the rotation around the center of the image
let cos_rot = cos(input.rotation);
@ -48,16 +47,8 @@ fn vs_main(input: VertexInput) -> VertexOutput {
vec4<f32>(0.0, 0.0, 0.0, 1.0)
);
// Scale the image and then translate to the final position by moving the center to the position
let scale_translate = mat4x4<f32>(
vec4<f32>(input.scale.x, 0.0, 0.0, 0.0),
vec4<f32>(0.0, input.scale.y, 0.0, 0.0),
vec4<f32>(0.0, 0.0, 1.0, 0.0),
vec4<f32>(input.center, 0.0, 1.0)
);
// Calculate the final position of the vertex
out.position = globals.transform * scale_translate * rotate * vec4<f32>(v_pos, 0.0, 1.0);
out.position = globals.transform * (vec4<f32>(input.center, 0.0, 0.0) + rotate * vec4<f32>(v_pos, 0.0, 1.0));
return out;
}