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

@ -141,14 +141,12 @@ impl Pipeline {
2 => Float32x2,
// Rotation
3 => Float32,
// Scale
4 => Float32x2,
// Atlas position
5 => Float32x2,
4 => Float32x2,
// Atlas scale
6 => Float32x2,
5 => Float32x2,
// Layer
7 => Sint32,
6 => Sint32,
),
}],
},
@ -232,7 +230,6 @@ impl Pipeline {
filter_method,
bounds,
rotation,
scale,
} => {
if let Some(atlas_entry) =
cache.upload_raster(device, encoder, handle)
@ -240,8 +237,7 @@ impl Pipeline {
add_instances(
[bounds.x, bounds.y],
[bounds.width, bounds.height],
*rotation,
[scale.width, scale.height],
f32::from(*rotation),
atlas_entry,
match filter_method {
crate::core::image::FilterMethod::Nearest => {
@ -263,7 +259,6 @@ impl Pipeline {
color,
bounds,
rotation,
scale,
} => {
let size = [bounds.width, bounds.height];
@ -278,8 +273,7 @@ impl Pipeline {
add_instances(
[bounds.x, bounds.y],
size,
*rotation,
[scale.width, scale.height],
f32::from(*rotation),
atlas_entry,
nearest_instances,
);
@ -510,7 +504,6 @@ struct Instance {
_center: [f32; 2],
_size: [f32; 2],
_rotation: f32,
_scale: [f32; 2],
_position_in_atlas: [f32; 2],
_size_in_atlas: [f32; 2],
_layer: u32,
@ -530,7 +523,6 @@ fn add_instances(
image_position: [f32; 2],
image_size: [f32; 2],
rotation: f32,
scale: [f32; 2],
entry: &atlas::Entry,
instances: &mut Vec<Instance>,
) {
@ -546,7 +538,6 @@ fn add_instances(
center,
image_size,
rotation,
scale,
allocation,
instances,
);
@ -576,8 +567,7 @@ fn add_instances(
];
add_instance(
position, center, size, rotation, scale, allocation,
instances,
position, center, size, rotation, allocation, instances,
);
}
}
@ -590,7 +580,6 @@ fn add_instance(
center: [f32; 2],
size: [f32; 2],
rotation: f32,
scale: [f32; 2],
allocation: &atlas::Allocation,
instances: &mut Vec<Instance>,
) {
@ -603,7 +592,6 @@ fn add_instance(
_center: center,
_size: size,
_rotation: rotation,
_scale: scale,
_position_in_atlas: [
(x as f32 + 0.5) / atlas::SIZE as f32,
(y as f32 + 0.5) / atlas::SIZE as f32,

View file

@ -1,5 +1,5 @@
use crate::core::{
renderer, Background, Color, Point, Rectangle, Size, Transformation,
renderer, Background, Color, Point, Radians, Rectangle, Transformation,
};
use crate::graphics;
use crate::graphics::color;
@ -118,15 +118,13 @@ impl Layer {
filter_method: crate::core::image::FilterMethod,
bounds: Rectangle,
transformation: Transformation,
rotation: f32,
scale: Size,
rotation: Radians,
) {
let image = Image::Raster {
handle,
filter_method,
bounds: bounds * transformation,
rotation,
scale,
};
self.images.push(image);
@ -138,15 +136,13 @@ impl Layer {
color: Option<Color>,
bounds: Rectangle,
transformation: Transformation,
rotation: f32,
scale: Size,
rotation: Radians,
) {
let svg = Image::Vector {
handle,
color,
bounds: bounds * transformation,
rotation,
scale,
};
self.images.push(svg);

View file

@ -61,7 +61,8 @@ pub use settings::Settings;
pub use geometry::Geometry;
use crate::core::{
Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation,
Background, Color, Font, Pixels, Point, Radians, Rectangle, Size,
Transformation, Vector,
};
use crate::graphics::text::{Editor, Paragraph};
use crate::graphics::Viewport;
@ -378,7 +379,6 @@ impl Renderer {
use crate::core::alignment;
use crate::core::text::Renderer as _;
use crate::core::Renderer as _;
use crate::core::Vector;
self.with_layer(
Rectangle::with_size(viewport.logical_size()),
@ -517,8 +517,7 @@ impl core::image::Renderer for Renderer {
handle: Self::Handle,
filter_method: core::image::FilterMethod,
bounds: Rectangle,
rotation: f32,
scale: Size,
rotation: Radians,
) {
let (layer, transformation) = self.layers.current_mut();
layer.draw_image(
@ -527,7 +526,6 @@ impl core::image::Renderer for Renderer {
bounds,
transformation,
rotation,
scale,
);
}
}
@ -543,18 +541,10 @@ impl core::svg::Renderer for Renderer {
handle: core::svg::Handle,
color_filter: Option<Color>,
bounds: Rectangle,
rotation: f32,
scale: Size,
rotation: Radians,
) {
let (layer, transformation) = self.layers.current_mut();
layer.draw_svg(
handle,
color_filter,
bounds,
transformation,
rotation,
scale,
);
layer.draw_svg(handle, color_filter, bounds, transformation, rotation);
}
}

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;
}