From c74c8991875dd2f3319c28d5910e501237953108 Mon Sep 17 00:00:00 2001 From: "Spencer C. Imbleau" Date: Wed, 1 May 2024 01:18:27 -0400 Subject: [PATCH] build: checkpoint for asset alignment Co-authored-by: Sebastian J. Hamel --- CHANGELOG.md | 14 +++++++++++ Cargo.toml | 2 +- examples/demo/src/main.rs | 2 +- examples/drag_n_drop/src/main.rs | 2 +- examples/scene/src/main.rs | 2 +- examples/text/src/main.rs | 2 +- examples/z_ordering/src/main.rs | 3 ++- src/assets/asset.rs | 26 ++++++++++++++++++++ src/assets/mod.rs | 2 +- src/lib.rs | 8 ++++--- src/render/extract.rs | 41 +++++++++++++++++++++++++++++--- src/render/systems.rs | 9 +++---- src/text/vello_text.rs | 2 +- 13 files changed, 97 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 838d934..7eec9cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,20 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe ## Unreleased +## 0.3.0 (2024-05-01) + +### added + +- `VelloAssetAlignment` was added to the `VelloAssetBundle`. + +### changed + +- `VectorFile` enum variants were flattened into tuple structs. + +### removed + +- `bevy_vello::VelloPlugin` was removed from the prelude. + ## 0.2.2 (2024-04-22) ### fixed diff --git a/Cargo.toml b/Cargo.toml index d129602..dee47a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ members = [ [workspace.package] edition = "2021" -version = "0.2.2" +version = "0.3.0" license = "MIT OR Apache-2.0" repository = "https://github.com/loopystudios/bevy_vello" diff --git a/examples/demo/src/main.rs b/examples/demo/src/main.rs index b030c85..b721332 100644 --- a/examples/demo/src/main.rs +++ b/examples/demo/src/main.rs @@ -4,7 +4,7 @@ use bevy::asset::io::embedded::EmbeddedAssetRegistry; use bevy::asset::{embedded_asset, AssetMetaCheck}; use bevy::prelude::*; use bevy_egui::EguiPlugin; -use bevy_vello::prelude::*; +use bevy_vello::{prelude::*, VelloPlugin}; fn main() { let mut app = App::new(); diff --git a/examples/drag_n_drop/src/main.rs b/examples/drag_n_drop/src/main.rs index 63f49d8..a62c339 100644 --- a/examples/drag_n_drop/src/main.rs +++ b/examples/drag_n_drop/src/main.rs @@ -1,6 +1,6 @@ use bevy::asset::{embedded_asset, AssetMetaCheck}; use bevy::prelude::*; -use bevy_vello::prelude::*; +use bevy_vello::{prelude::*, VelloPlugin}; fn main() { let mut app = App::new(); diff --git a/examples/scene/src/main.rs b/examples/scene/src/main.rs index 5db69b8..11d1021 100644 --- a/examples/scene/src/main.rs +++ b/examples/scene/src/main.rs @@ -1,7 +1,7 @@ use bevy::asset::AssetMetaCheck; use bevy::prelude::*; -use bevy_vello::prelude::*; use bevy_vello::vello::{kurbo, peniko}; +use bevy_vello::{prelude::*, VelloPlugin}; fn main() { App::new() diff --git a/examples/text/src/main.rs b/examples/text/src/main.rs index 37bae7b..9be32f1 100644 --- a/examples/text/src/main.rs +++ b/examples/text/src/main.rs @@ -1,8 +1,8 @@ use bevy::asset::{embedded_asset, AssetMetaCheck}; use bevy::prelude::*; -use bevy_vello::prelude::*; use bevy_vello::text::VelloTextAlignment; use bevy_vello::vello::peniko; +use bevy_vello::{prelude::*, VelloPlugin}; fn main() { let mut app = App::new(); diff --git a/examples/z_ordering/src/main.rs b/examples/z_ordering/src/main.rs index 7352d5c..b2886da 100644 --- a/examples/z_ordering/src/main.rs +++ b/examples/z_ordering/src/main.rs @@ -1,6 +1,6 @@ use bevy::asset::{embedded_asset, AssetMetaCheck}; use bevy::prelude::*; -use bevy_vello::prelude::*; +use bevy_vello::{prelude::*, VelloPlugin}; fn main() { let mut app = App::new(); @@ -53,6 +53,7 @@ fn setup_worldspace_vectors(mut commands: Commands, asset_server: ResMut, + /// How the bounding asset is aligned, respective to the transform. + pub alignment: VelloAssetAlignment, /// The coordinate space in which this vector should be rendered. pub coordinate_space: CoordinateSpace, /// A transform to apply to this vector @@ -91,6 +92,7 @@ pub struct VelloTextBundle { pub font: Handle, /// Text to render pub text: VelloText, + /// How the bounding text is aligned, respective to the transform. pub alignment: VelloTextAlignment, /// The coordinate space in which this text should be rendered. pub coordinate_space: CoordinateSpace, diff --git a/src/render/extract.rs b/src/render/extract.rs index 4286121..7d4eb30 100644 --- a/src/render/extract.rs +++ b/src/render/extract.rs @@ -2,7 +2,8 @@ use super::z_function::ZFunction; use crate::text::VelloTextAlignment; use crate::theme::Theme; use crate::{ - CoordinateSpace, PlaybackAlphaOverride, Playhead, VelloAsset, VelloFont, VelloScene, VelloText, + CoordinateSpace, PlaybackAlphaOverride, Playhead, VelloAsset, VelloAssetAlignment, VelloFont, + VelloScene, VelloText, }; use bevy::prelude::*; use bevy::render::extract_component::ExtractComponent; @@ -12,6 +13,7 @@ use bevy::window::PrimaryWindow; #[derive(Component, Clone)] pub struct ExtractedRenderAsset { pub asset: VelloAsset, + pub alignment: VelloAssetAlignment, pub transform: GlobalTransform, pub z_index: f32, pub theme: Option, @@ -26,6 +28,7 @@ pub fn asset_instances( query_vectors: Extract< Query<( &Handle, + &VelloAssetAlignment, &CoordinateSpace, &ZFunction, &GlobalTransform, @@ -41,6 +44,7 @@ pub fn asset_instances( ) { for ( vello_vector_handle, + alignment, coord_space, z_function, transform, @@ -58,10 +62,41 @@ pub fn asset_instances( crate::VectorFile::Svg { .. } => 0.0, crate::VectorFile::Lottie { .. } => playhead.unwrap().frame(), }; + + // let bb = asset.bb_in_world_space(transform); + // let width = bb.width(); + // let height = bb.height(); + let width = asset.width; + let height = asset.height; + + let (scale, rotation, _translation) = transform.to_scale_rotation_translation(); + + let adjustment = match alignment { + VelloAssetAlignment::TopLeft => Vec3::new(width / 2.0, -height / 2.0, 0.0), + VelloAssetAlignment::Left => Vec3::new(width / 2.0, 0.0, 0.0), + VelloAssetAlignment::BottomLeft => Vec3::new(width / 2.0, height / 2.0, 0.0), + VelloAssetAlignment::Top => Vec3::new(0.0, -height / 2.0, 0.0), + VelloAssetAlignment::Center => Vec3::new(0.0, 0.0, 0.0), + VelloAssetAlignment::Bottom => Vec3::new(0.0, height / 2.0, 0.0), + VelloAssetAlignment::TopRight => Vec3::new(-width / 2.0, -height / 2.0, 0.0), + VelloAssetAlignment::Right => Vec3::new(-width / 2.0, 0.0, 0.0), + VelloAssetAlignment::BottomRight => Vec3::new(-width / 2.0, height / 2.0, 0.0), + }; + + let new_translation: Vec3 = + (transform.compute_matrix() * adjustment.extend(1.0)).xyz(); + + let transform = Transform::from_scale(scale) + .with_rotation(rotation) + .with_translation(new_translation); + + let transform = GlobalTransform::from(transform); + commands.spawn(ExtractedRenderAsset { asset: asset.to_owned(), - transform: *transform, - z_index: z_function.compute(asset, transform), + transform, + alignment: *alignment, + z_index: z_function.compute(asset, &transform), theme: theme.cloned(), render_mode: *coord_space, playhead, diff --git a/src/render/systems.rs b/src/render/systems.rs index 70e0dc1..67be4b8 100644 --- a/src/render/systems.rs +++ b/src/render/systems.rs @@ -1,5 +1,5 @@ use crate::render::extract::ExtractedRenderScene; -use crate::{CoordinateSpace, VectorFile, VelloCanvasMaterial, VelloFont}; +use crate::{CoordinateSpace, VectorFile, VelloAssetAlignment, VelloCanvasMaterial, VelloFont}; use bevy::prelude::*; use bevy::render::mesh::Indices; use bevy::render::render_asset::{RenderAssetUsages, RenderAssets}; @@ -50,9 +50,9 @@ pub fn setup_image(images: &mut Assets, window: &WindowResolution) -> Han #[allow(clippy::complexity)] pub fn render_scene( ss_render_target: Query<&SSRenderTarget>, - render_vectors: Query<(&PreparedAffine, &ExtractedRenderAsset)>, - query_render_texts: Query<(&PreparedAffine, &ExtractedRenderText)>, + query_render_vectors: Query<(&PreparedAffine, &ExtractedRenderAsset)>, query_render_scenes: Query<(&PreparedAffine, &ExtractedRenderScene)>, + query_render_texts: Query<(&PreparedAffine, &ExtractedRenderText)>, mut font_render_assets: ResMut>, gpu_images: Res>, device: Res, @@ -84,7 +84,7 @@ pub fn render_scene( Text(&'a ExtractedRenderText), } let mut render_queue: Vec<(f32, CoordinateSpace, (&PreparedAffine, RenderItem))> = - render_vectors + query_render_vectors .iter() .map(|(a, b)| (b.z_index, b.render_mode, (a, RenderItem::Asset(b)))) .collect(); @@ -121,6 +121,7 @@ pub fn render_scene( match render_item { RenderItem::Asset(ExtractedRenderAsset { asset, + alignment, theme, alpha, playhead, diff --git a/src/text/vello_text.rs b/src/text/vello_text.rs index feddee3..55b57a0 100644 --- a/src/text/vello_text.rs +++ b/src/text/vello_text.rs @@ -2,7 +2,7 @@ use crate::VelloFont; use bevy::prelude::*; use vello::peniko::Brush; -/// Describes how to position text on the Y-axis +/// Describes how to position text from the origin #[derive(Component, Default, Clone, Copy, PartialEq, Eq)] pub enum VelloTextAlignment { /// Bounds start from the render position and advance up and to the right.