Skip to content

Commit

Permalink
build: checkpoint for asset alignment
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian J. Hamel <[email protected]>
  • Loading branch information
simbleau and seabassjh committed May 1, 2024
1 parent 9a73dfd commit c74c899
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 18 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/drag_n_drop/src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/scene/src/main.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 1 addition & 1 deletion examples/text/src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
3 changes: 2 additions & 1 deletion examples/z_ordering/src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -53,6 +53,7 @@ fn setup_worldspace_vectors(mut commands: Commands, asset_server: ResMut<AssetSe
commands
.spawn(VelloAssetBundle {
vector: asset_server.load("embedded://z_ordering/assets/squid.json"),
alignment: VelloAssetAlignment::Bottom,
transform: Transform::from_scale(Vec3::splat(SIZE))
.with_translation(Vec3::splat(-i as f32 * SPACING)),
debug_visualizations: DebugVisualizations::Visible,
Expand Down
26 changes: 26 additions & 0 deletions src/assets/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,29 @@ impl VelloAsset {
}
}
}

/// Describes how to position the asset from the origin
#[derive(Component, Default, Clone, Copy, PartialEq, Eq)]
pub enum VelloAssetAlignment {
/// Bounds start from the render position and advance up and to the right.
BottomLeft,
/// Bounds start from the render position and advance up.
Bottom,
/// Bounds start from the render position and advance up and to the left.
BottomRight,

/// Bounds start from the render position and advance right.
Left,
/// Bounds start from the render position and advance equally on both axes.
#[default]
Center,
/// Bounds start from the render position and advance left.
Right,

/// Bounds start from the render position and advance down and to the right.
TopLeft,
/// Bounds start from the render position and advance down.
Top,
/// Bounds start from the render position and advance down and to the left.
TopRight,
}
2 changes: 1 addition & 1 deletion src/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Parsing, loading, and asset type definitions.

mod asset;
pub use asset::{VectorFile, VelloAsset};
pub use asset::{VectorFile, VelloAsset, VelloAssetAlignment};

mod parser;
pub use parser::{
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use bevy::prelude::*;

mod plugin;
pub use plugin::VelloPlugin;
use text::VelloTextAlignment;

pub mod assets;
pub mod debug;
Expand All @@ -21,20 +20,20 @@ pub mod theme;
pub use {velato, vello, vello_svg};

pub mod prelude {
pub use crate::assets::{VectorFile, VelloAsset};
pub use crate::assets::{VectorFile, VelloAsset, VelloAssetAlignment};
pub use crate::debug::DebugVisualizations;
pub use crate::playback::{
PlaybackAlphaOverride, PlaybackDirection, PlaybackLoopBehavior, PlaybackOptions,
PlaybackPlayMode, Playhead,
};
pub use crate::player::{LottiePlayer, PlayerState, PlayerTransition};
pub use crate::plugin::VelloPlugin;
pub use crate::render::{VelloCanvasMaterial, ZFunction};
pub use crate::text::{VelloFont, VelloText, VelloTextAlignment};
pub use crate::theme::Theme;
pub use crate::{
CoordinateSpace, VelloAssetBundle, VelloScene, VelloSceneBundle, VelloTextBundle,
};
pub use {vello::kurbo, vello::peniko, vello::skrifa, vello::Scene};
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Component, Default, Copy, Clone, Debug, Reflect)]
Expand All @@ -49,6 +48,8 @@ pub enum CoordinateSpace {
pub struct VelloAssetBundle {
/// Asset data to render
pub vector: Handle<VelloAsset>,
/// 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
Expand Down Expand Up @@ -91,6 +92,7 @@ pub struct VelloTextBundle {
pub font: Handle<VelloFont>,
/// 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,
Expand Down
41 changes: 38 additions & 3 deletions src/render/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Theme>,
Expand All @@ -26,6 +28,7 @@ pub fn asset_instances(
query_vectors: Extract<
Query<(
&Handle<VelloAsset>,
&VelloAssetAlignment,
&CoordinateSpace,
&ZFunction,
&GlobalTransform,
Expand All @@ -41,6 +44,7 @@ pub fn asset_instances(
) {
for (
vello_vector_handle,
alignment,
coord_space,
z_function,
transform,
Expand All @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions src/render/systems.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -50,9 +50,9 @@ pub fn setup_image(images: &mut Assets<Image>, 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<RenderAssets<VelloFont>>,
gpu_images: Res<RenderAssets<Image>>,
device: Res<RenderDevice>,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -121,6 +121,7 @@ pub fn render_scene(
match render_item {
RenderItem::Asset(ExtractedRenderAsset {
asset,
alignment,
theme,
alpha,
playhead,
Expand Down
2 changes: 1 addition & 1 deletion src/text/vello_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c74c899

Please sign in to comment.