diff --git a/crates/bevy_animation/src/animatable.rs b/crates/bevy_animation/src/animatable.rs index 6af653d3077b4..98030875ee976 100644 --- a/crates/bevy_animation/src/animatable.rs +++ b/crates/bevy_animation/src/animatable.rs @@ -6,7 +6,7 @@ use bevy_math::*; use bevy_reflect::Reflect; use bevy_transform::prelude::Transform; -/// An individual input for [`Animatable::blend`]. +/// An individual input for [`Blendable::blend`]. pub struct BlendInput { /// The individual item's weight. This may not be bound to the range `[0.0, 1.0]`. pub weight: f32, @@ -17,7 +17,7 @@ pub struct BlendInput { } /// An animatable value type. -pub trait Animatable: Reflect + Sized + Send + Sync + 'static { +pub trait Blendable: Reflect + Sized + Send + Sync + 'static { /// Interpolates between `a` and `b` with a interpolation factor of `time`. /// /// The `time` parameter here may not be clamped to the range `[0.0, 1.0]`. @@ -31,7 +31,7 @@ pub trait Animatable: Reflect + Sized + Send + Sync + 'static { macro_rules! impl_float_animatable { ($ty: ty, $base: ty) => { - impl Animatable for $ty { + impl Blendable for $ty { #[inline] fn interpolate(a: &Self, b: &Self, t: f32) -> Self { let t = <$base>::from(t); @@ -56,7 +56,7 @@ macro_rules! impl_float_animatable { macro_rules! impl_color_animatable { ($ty: ident) => { - impl Animatable for $ty { + impl Blendable for $ty { #[inline] fn interpolate(a: &Self, b: &Self, t: f32) -> Self { let value = *a * (1. - t) + *b * t; @@ -96,7 +96,7 @@ impl_color_animatable!(Srgba); impl_color_animatable!(Xyza); // Vec3 is special cased to use Vec3A internally for blending -impl Animatable for Vec3 { +impl Blendable for Vec3 { #[inline] fn interpolate(a: &Self, b: &Self, t: f32) -> Self { (*a) * (1.0 - t) + (*b) * t @@ -116,7 +116,7 @@ impl Animatable for Vec3 { } } -impl Animatable for bool { +impl Blendable for bool { #[inline] fn interpolate(a: &Self, b: &Self, t: f32) -> Self { util::step_unclamped(*a, *b, t) @@ -131,7 +131,7 @@ impl Animatable for bool { } } -impl Animatable for Transform { +impl Blendable for Transform { fn interpolate(a: &Self, b: &Self, t: f32) -> Self { Self { translation: Vec3::interpolate(&a.translation, &b.translation, t), @@ -170,7 +170,7 @@ impl Animatable for Transform { } } -impl Animatable for Quat { +impl Blendable for Quat { /// Performs a slerp to smoothly interpolate between quaternions. #[inline] fn interpolate(a: &Self, b: &Self, t: f32) -> Self { @@ -204,7 +204,7 @@ impl Animatable for Quat { /// The derivatives are linearly scaled by `duration`. pub fn interpolate_with_cubic_bezier(p0: &T, d0: &T, d3: &T, p3: &T, t: f32, duration: f32) -> T where - T: Animatable + Clone, + T: Blendable + Clone, { // We're given two endpoints, along with the derivatives at those endpoints, // and have to evaluate the cubic Bézier curve at time t using only diff --git a/crates/bevy_animation/src/animation_curves.rs b/crates/bevy_animation/src/animation_curves.rs index 14ee7f24fc8f8..aeede3e24b2f3 100644 --- a/crates/bevy_animation/src/animation_curves.rs +++ b/crates/bevy_animation/src/animation_curves.rs @@ -66,15 +66,15 @@ //! - [`RotationCurve`], which uses `Quat` output to animate [`Transform::rotation`] //! - [`ScaleCurve`], which uses `Vec3` output to animate [`Transform::scale`] //! -//! ## Animatable properties +//! ## Blendable properties //! -//! Animation of arbitrary components can be accomplished using [`AnimatableProperty`] in -//! conjunction with [`AnimatableCurve`]. See the documentation [there] for details. +//! Animation of arbitrary components can be accomplished using [`BlendableProperty`] in +//! conjunction with [`BlendableCurve`]. See the documentation [there] for details. //! //! [using a function]: bevy_math::curve::function_curve //! [translation component of a `Transform`]: bevy_transform::prelude::Transform::translation //! [`AnimationClip`]: crate::AnimationClip -//! [there]: AnimatableProperty +//! [there]: BlendableProperty use core::{ any::TypeId, @@ -97,7 +97,7 @@ use bevy_transform::prelude::Transform; use crate::{ graph::AnimationNodeIndex, - prelude::{Animatable, BlendInput}, + prelude::{BlendInput, Blendable}, AnimationEntityMut, AnimationEvaluationError, }; @@ -105,17 +105,17 @@ use crate::{ /// /// You can implement this trait on a unit struct in order to support animating /// custom components other than transforms and morph weights. Use that type in -/// conjunction with [`AnimatableCurve`] (and perhaps [`AnimatableKeyframeCurve`] +/// conjunction with [`BlendableCurve`] (and perhaps [`BlendableKeyframeCurve`] /// to define the animation itself). /// For example, in order to animate field of view, you might use: /// -/// # use bevy_animation::prelude::AnimatableProperty; +/// # use bevy_animation::prelude::BlendableProperty; /// # use bevy_reflect::Reflect; /// # use bevy_render::camera::PerspectiveProjection; /// #[derive(Reflect)] /// struct FieldOfViewProperty; /// -/// impl AnimatableProperty for FieldOfViewProperty { +/// impl BlendableProperty for FieldOfViewProperty { /// type Component = PerspectiveProjection; /// type Property = f32; /// fn get_mut(component: &mut Self::Component) -> Option<&mut Self::Property> { @@ -126,14 +126,14 @@ use crate::{ /// You can then create an [`AnimationClip`] to animate this property like so: /// /// # use bevy_animation::{AnimationClip, AnimationTargetId, VariableCurve}; -/// # use bevy_animation::prelude::{AnimatableProperty, AnimatableKeyframeCurve, AnimatableCurve}; +/// # use bevy_animation::prelude::{BlendableProperty, BlendableKeyframeCurve, BlendableCurve}; /// # use bevy_core::Name; /// # use bevy_reflect::Reflect; /// # use bevy_render::camera::PerspectiveProjection; /// # let animation_target_id = AnimationTargetId::from(&Name::new("Test")); /// # #[derive(Reflect)] /// # struct FieldOfViewProperty; -/// # impl AnimatableProperty for FieldOfViewProperty { +/// # impl BlendableProperty for FieldOfViewProperty { /// # type Component = PerspectiveProjection; /// # type Property = f32; /// # fn get_mut(component: &mut Self::Component) -> Option<&mut Self::Property> { @@ -143,29 +143,29 @@ use crate::{ /// let mut animation_clip = AnimationClip::default(); /// animation_clip.add_curve_to_target( /// animation_target_id, -/// AnimatableKeyframeCurve::new( +/// BlendableKeyframeCurve::new( /// [ /// (0.0, core::f32::consts::PI / 4.0), /// (1.0, core::f32::consts::PI / 3.0), /// ] /// ) -/// .map(AnimatableCurve::::from_curve) +/// .map(BlendableCurve::::from_curve) /// .expect("Failed to create font size curve") /// ); /// -/// Here, the use of [`AnimatableKeyframeCurve`] creates a curve out of the given keyframe time-value -/// pairs, using the [`Animatable`] implementation of `f32` to interpolate between them. The -/// invocation of [`AnimatableCurve::from_curve`] with `FieldOfViewProperty` indicates that the `f32` +/// Here, the use of [`BlendableKeyframeCurve`] creates a curve out of the given keyframe time-value +/// pairs, using the [`Blendable`] implementation of `f32` to interpolate between them. The +/// invocation of [`BlendableCurve::from_curve`] with `FieldOfViewProperty` indicates that the `f32` /// output from that curve is to be used to animate the font size of a `PerspectiveProjection` component (as /// configured above). /// /// [`AnimationClip`]: crate::AnimationClip -pub trait AnimatableProperty: Reflect + TypePath { +pub trait BlendableProperty: Reflect + TypePath { /// The type of the component that the property lives on. type Component: Component; /// The type of the property to be animated. - type Property: Animatable + FromReflect + Reflectable + Clone + Sync + Debug; + type Property: Blendable + FromReflect + Reflectable + Clone + Sync + Debug; /// Given a reference to the component, returns a reference to the property. /// @@ -180,13 +180,13 @@ pub trait AnimationCompatibleCurve: Curve + Debug + Clone + Reflectable {} impl AnimationCompatibleCurve for C where C: Curve + Debug + Clone + Reflectable {} /// This type allows the conversion of a [curve] valued in the [property type] of an -/// [`AnimatableProperty`] into an [`AnimationCurve`] which animates that property. +/// [`BlendableProperty`] into an [`AnimationCurve`] which animates that property. /// /// [curve]: Curve -/// [property type]: AnimatableProperty::Property +/// [property type]: BlendableProperty::Property #[derive(Reflect, FromReflect)] #[reflect(from_reflect = false)] -pub struct AnimatableCurve { +pub struct BlendableCurve { /// The inner [curve] whose values are used to animate the property. /// /// [curve]: Curve @@ -195,29 +195,29 @@ pub struct AnimatableCurve { _phantom: PhantomData

, } -/// An [`AnimatableCurveEvaluator`] for [`AnimatableProperty`] instances. +/// An [`BlendableCurveEvaluator`] for [`BlendableProperty`] instances. /// /// You shouldn't ordinarily need to instantiate one of these manually. Bevy -/// will automatically do so when you use an [`AnimatableCurve`] instance. +/// will automatically do so when you use an [`BlendableCurve`] instance. #[derive(Reflect)] -pub struct AnimatableCurveEvaluator

+pub struct BlendableCurveEvaluator

where - P: AnimatableProperty, + P: BlendableProperty, { evaluator: BasicAnimationCurveEvaluator, #[reflect(ignore)] phantom: PhantomData

, } -impl AnimatableCurve +impl BlendableCurve where - P: AnimatableProperty, + P: BlendableProperty, C: AnimationCompatibleCurve, { - /// Create an [`AnimatableCurve`] (and thus an [`AnimationCurve`]) from a curve + /// Create an [`BlendableCurve`] (and thus an [`AnimationCurve`]) from a curve /// valued in an [animatable property]. /// - /// [animatable property]: AnimatableProperty::Property + /// [animatable property]: BlendableProperty::Property pub fn from_curve(curve: C) -> Self { Self { curve, @@ -226,7 +226,7 @@ where } } -impl Clone for AnimatableCurve +impl Clone for BlendableCurve where C: Clone, { @@ -238,20 +238,20 @@ where } } -impl Debug for AnimatableCurve +impl Debug for BlendableCurve where C: Debug, { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - f.debug_struct("AnimatableCurve") + f.debug_struct("BlendableCurve") .field("curve", &self.curve) .finish() } } -impl AnimationCurve for AnimatableCurve +impl AnimationCurve for BlendableCurve where - P: AnimatableProperty, + P: BlendableProperty, C: AnimationCompatibleCurve, { fn clone_value(&self) -> Box { @@ -263,11 +263,11 @@ where } fn evaluator_type(&self) -> TypeId { - TypeId::of::>() + TypeId::of::>() } fn create_evaluator(&self) -> Box { - Box::new(AnimatableCurveEvaluator { + Box::new(BlendableCurveEvaluator { evaluator: BasicAnimationCurveEvaluator::default(), phantom: PhantomData::

, }) @@ -281,7 +281,7 @@ where graph_node: AnimationNodeIndex, ) -> Result<(), AnimationEvaluationError> { let curve_evaluator = (*Reflect::as_any_mut(curve_evaluator)) - .downcast_mut::>() + .downcast_mut::>() .unwrap(); let value = self.curve.sample_clamped(t); curve_evaluator @@ -296,9 +296,9 @@ where } } -impl

AnimationCurveEvaluator for AnimatableCurveEvaluator

+impl

AnimationCurveEvaluator for BlendableCurveEvaluator

where - P: AnimatableProperty, + P: BlendableProperty, { fn blend(&mut self, graph_node: AnimationNodeIndex) -> Result<(), AnimationEvaluationError> { self.evaluator.combine(graph_node, /*additive=*/ false) @@ -330,7 +330,7 @@ where .evaluator .stack .pop() - .ok_or_else(inconsistent::>)? + .ok_or_else(inconsistent::>)? .value; Ok(()) } @@ -826,7 +826,7 @@ impl AnimationCurveEvaluator for WeightsCurveEvaluator { #[derive(Reflect)] struct BasicAnimationCurveEvaluator where - A: Animatable, + A: Blendable, { stack: Vec>, blend_register: Option<(A, f32)>, @@ -835,7 +835,7 @@ where #[derive(Reflect)] struct BasicAnimationCurveEvaluatorStackElement where - A: Animatable, + A: Blendable, { value: A, weight: f32, @@ -844,7 +844,7 @@ where impl Default for BasicAnimationCurveEvaluator where - A: Animatable, + A: Blendable, { fn default() -> Self { BasicAnimationCurveEvaluator { @@ -856,7 +856,7 @@ where impl BasicAnimationCurveEvaluator where - A: Animatable, + A: Blendable, { fn combine( &mut self, @@ -932,9 +932,9 @@ where /// to entities by the animation system. /// /// Typically, this will not need to be implemented manually, since it is -/// automatically implemented by [`AnimatableCurve`] and other curves used by +/// automatically implemented by [`BlendableCurve`] and other curves used by /// the animation system (e.g. those that animate parts of transforms or morph -/// weights). However, this can be implemented manually when `AnimatableCurve` +/// weights). However, this can be implemented manually when `BlendableCurve` /// is not sufficiently expressive. /// /// In many respects, this behaves like a type-erased form of [`Curve`], where @@ -989,21 +989,21 @@ pub trait AnimationCurve: Reflect + Debug + Send + Sync { /// A low-level trait for use in [`crate::VariableCurve`] that provides fine /// control over how animations are evaluated. /// -/// You can implement this trait when the generic [`AnimatableCurveEvaluator`] +/// You can implement this trait when the generic [`BlendableCurveEvaluator`] /// isn't sufficiently-expressive for your needs. For example, [`MorphWeights`] -/// implements this trait instead of using [`AnimatableCurveEvaluator`] because +/// implements this trait instead of using [`BlendableCurveEvaluator`] because /// it needs to animate arbitrarily many weights at once, which can't be done -/// with [`Animatable`] as that works on fixed-size values only. +/// with [`Blendable`] as that works on fixed-size values only. /// /// If you implement this trait, you should also implement [`AnimationCurve`] on /// your curve type, as that trait allows creating instances of this one. /// -/// Implementations of [`AnimatableCurveEvaluator`] should maintain a *stack* of +/// Implementations of [`BlendableCurveEvaluator`] should maintain a *stack* of /// (value, weight, node index) triples, as well as a *blend register*, which is /// either a (value, weight) pair or empty. *Value* here refers to an instance /// of the value being animated: for example, [`Vec3`] in the case of /// translation keyframes. The stack stores intermediate values generated while -/// evaluating the [`crate::graph::AnimationGraph`], while the blend register +/// evaluating the [`crate::graph::BlendGraph`], while the blend register /// stores the result of a blend operation. pub trait AnimationCurveEvaluator: Reflect { /// Blends the top element of the stack with the blend register. @@ -1075,18 +1075,18 @@ pub trait AnimationCurveEvaluator: Reflect { /// A [curve] defined by keyframes with values in an [animatable] type. /// -/// The keyframes are interpolated using the type's [`Animatable::interpolate`] implementation. +/// The keyframes are interpolated using the type's [`Blendable::interpolate`] implementation. /// /// [curve]: Curve -/// [animatable]: Animatable +/// [animatable]: Blendable #[derive(Debug, Clone, Reflect)] -pub struct AnimatableKeyframeCurve { +pub struct BlendableKeyframeCurve { core: UnevenCore, } -impl Curve for AnimatableKeyframeCurve +impl Curve for BlendableKeyframeCurve where - T: Animatable + Clone, + T: Blendable + Clone, { #[inline] fn domain(&self) -> Interval { @@ -1096,7 +1096,7 @@ where #[inline] fn sample_clamped(&self, t: f32) -> T { // `UnevenCore::sample_with` is implicitly clamped. - self.core.sample_with(t, ::interpolate) + self.core.sample_with(t, ::interpolate) } #[inline] @@ -1105,13 +1105,13 @@ where } } -impl AnimatableKeyframeCurve +impl BlendableKeyframeCurve where - T: Animatable, + T: Blendable, { - /// Create a new [`AnimatableKeyframeCurve`] from the given `keyframes`. The values of this + /// Create a new [`BlendableKeyframeCurve`] from the given `keyframes`. The values of this /// curve are interpolated from the keyframes using the output type's implementation of - /// [`Animatable::interpolate`]. + /// [`Blendable::interpolate`]. /// /// There must be at least two samples in order for this method to succeed. pub fn new(keyframes: impl IntoIterator) -> Result { diff --git a/crates/bevy_animation/src/animation_event.rs b/crates/bevy_animation/src/animation_event.rs index a2cf2da785a34..689d2b907bf90 100644 --- a/crates/bevy_animation/src/animation_event.rs +++ b/crates/bevy_animation/src/animation_event.rs @@ -45,20 +45,20 @@ pub(crate) fn trigger_animation_event( /// fn setup_animation( /// mut commands: Commands, /// mut animations: ResMut>, -/// mut graphs: ResMut>, +/// mut graphs: ResMut>, /// ) { /// // Create a new animation and add an event at 1.0s. /// let mut animation = AnimationClip::default(); /// animation.add_event(1.0, Say("Hello".into())); -/// +/// /// // Create an animation graph. -/// let (graph, animation_index) = AnimationGraph::from_clip(animations.add(animation)); +/// let (graph, animation_index) = BlendGraph::from_clip(animations.add(animation)); /// /// // Start playing the animation. /// let mut player = AnimationPlayer::default(); /// player.play(animation_index).repeat(); -/// -/// commands.spawn((AnimationGraphHandle(graphs.add(graph)), player)); +/// +/// commands.spawn((BlendGraphHandle(graphs.add(graph)), player)); /// } /// # /// # bevy_ecs::system::assert_is_system(setup_animation); diff --git a/crates/bevy_animation/src/graph.rs b/crates/bevy_animation/src/graph.rs index d3bf1cc52db87..a90e853ab86b9 100644 --- a/crates/bevy_animation/src/graph.rs +++ b/crates/bevy_animation/src/graph.rs @@ -48,23 +48,23 @@ use crate::{AnimationClip, AnimationTargetId}; /// For example, consider the following graph: /// /// ```text -/// ┌────────────┐ -/// │ │ -/// │ Idle ├─────────────────────┐ -/// │ │ │ -/// └────────────┘ │ -/// │ +/// ┌────────────┐ +/// │ │ +/// │ Idle ├─────────────────────┐ +/// │ │ │ +/// └────────────┘ │ +/// │ /// ┌────────────┐ │ ┌────────────┐ /// │ │ │ │ │ /// │ Run ├──┐ ├──┤ Root │ /// │ │ │ ┌────────────┐ │ │ │ /// └────────────┘ │ │ Blend │ │ └────────────┘ -/// ├──┤ ├──┘ -/// ┌────────────┐ │ │ 0.5 │ -/// │ │ │ └────────────┘ -/// │ Walk ├──┘ -/// │ │ -/// └────────────┘ +/// ├──┤ ├──┘ +/// ┌────────────┐ │ │ 0.5 │ +/// │ │ │ └────────────┘ +/// │ Walk ├──┘ +/// │ │ +/// └────────────┘ /// ``` /// /// In this case, assuming that Idle, Run, and Walk are all playing with weight @@ -105,10 +105,10 @@ use crate::{AnimationClip, AnimationTargetId}; /// [RFC 51]: https://github.com/bevyengine/rfcs/blob/main/rfcs/51-animation-composition.md #[derive(Asset, Reflect, Clone, Debug, Serialize)] #[reflect(Serialize, Debug)] -#[serde(into = "SerializedAnimationGraph")] -pub struct AnimationGraph { +#[serde(into = "SerializedBlendGraph")] +pub struct BlendGraph { /// The `petgraph` data structure that defines the animation graph. - pub graph: AnimationDiGraph, + pub graph: BlendDiGraph, /// The index of the root node in the animation graph. pub root: NodeIndex, @@ -125,26 +125,26 @@ pub struct AnimationGraph { pub mask_groups: HashMap, } -/// A [`Handle`] to the [`AnimationGraph`] to be used by the [`AnimationPlayer`](crate::AnimationPlayer) on the same entity. +/// A [`Handle`] to the [`BlendGraph`] to be used by the [`AnimationPlayer`](crate::AnimationPlayer) on the same entity. #[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect, PartialEq, Eq, From)] #[reflect(Component, Default)] -pub struct AnimationGraphHandle(pub Handle); +pub struct BlendGraphHandle(pub Handle); -impl From for AssetId { - fn from(handle: AnimationGraphHandle) -> Self { +impl From for AssetId { + fn from(handle: BlendGraphHandle) -> Self { handle.id() } } -impl From<&AnimationGraphHandle> for AssetId { - fn from(handle: &AnimationGraphHandle) -> Self { +impl From<&BlendGraphHandle> for AssetId { + fn from(handle: &BlendGraphHandle) -> Self { handle.id() } } /// A type alias for the `petgraph` data structure that defines the animation /// graph. -pub type AnimationDiGraph = DiGraph; +pub type BlendDiGraph = DiGraph; /// The index of either an animation or blend node in the animation graph. /// @@ -155,12 +155,12 @@ pub type AnimationNodeIndex = NodeIndex; /// An individual node within an animation graph. /// -/// The [`AnimationGraphNode::node_type`] field specifies the type of node: one +/// The [`BlendGraphNode::node_type`] field specifies the type of node: one /// of a *clip node*, a *blend node*, or an *add node*. Clip nodes, the leaves /// of the graph, contain animation clips to play. Blend and add nodes describe /// how to combine their children to produce a final animation. #[derive(Clone, Reflect, Debug)] -pub struct AnimationGraphNode { +pub struct BlendGraphNode { /// Animation node data specific to the type of node (clip, blend, or add). /// /// In the case of clip nodes, this contains the actual animation clip @@ -229,17 +229,17 @@ pub enum AnimationNodeType { Add, } -/// An [`AssetLoader`] that can load [`AnimationGraph`]s as assets. +/// An [`AssetLoader`] that can load [`BlendGraph`]s as assets. /// -/// The canonical extension for [`AnimationGraph`]s is `.animgraph.ron`. Plain +/// The canonical extension for [`BlendGraph`]s is `.animgraph.ron`. Plain /// `.animgraph` is supported as well. #[derive(Default)] -pub struct AnimationGraphAssetLoader; +pub struct BlendGraphAssetLoader; /// Various errors that can occur when serializing or deserializing animation /// graphs to and from RON, respectively. #[derive(Error, Display, Debug, From)] -pub enum AnimationGraphLoadError { +pub enum BlendGraphLoadError { /// An I/O error occurred. #[display("I/O")] Io(io::Error), @@ -255,20 +255,18 @@ pub enum AnimationGraphLoadError { /// Acceleration structures for animation graphs that allows Bevy to evaluate /// them quickly. /// -/// These are kept up to date as [`AnimationGraph`] instances are added, +/// These are kept up to date as [`BlendGraph`] instances are added, /// modified, and removed. #[derive(Default, Reflect, Resource)] -pub struct ThreadedAnimationGraphs( - pub(crate) HashMap, ThreadedAnimationGraph>, -); +pub struct ThreadedBlendGraphs(pub(crate) HashMap, ThreadedBlendGraph>); /// An acceleration structure for an animation graph that allows Bevy to /// evaluate it quickly. /// -/// This is kept up to date as the associated [`AnimationGraph`] instance is +/// This is kept up to date as the associated [`BlendGraph`] instance is /// added, modified, or removed. #[derive(Default, Reflect)] -pub struct ThreadedAnimationGraph { +pub struct ThreadedBlendGraph { /// A cached postorder traversal of the graph. /// /// The node indices here are stored in postorder. Siblings are stored in @@ -345,7 +343,7 @@ pub struct ThreadedAnimationGraph { pub computed_masks: Vec, } -/// A version of [`AnimationGraph`] suitable for serializing as an asset. +/// A version of [`BlendGraph`] suitable for serializing as an asset. /// /// Animation nodes can refer to external animation clips, and the [`AssetId`] /// is typically not sufficient to identify the clips, since the @@ -353,32 +351,32 @@ pub struct ThreadedAnimationGraph { /// motivates this type, which replaces the `Handle` with an /// asset path. Loading an animation graph via the [`bevy_asset::AssetServer`] /// actually loads a serialized instance of this type, as does serializing an -/// [`AnimationGraph`] through `serde`. +/// [`BlendGraph`] through `serde`. #[derive(Serialize, Deserialize)] -pub struct SerializedAnimationGraph { - /// Corresponds to the `graph` field on [`AnimationGraph`]. - pub graph: DiGraph, - /// Corresponds to the `root` field on [`AnimationGraph`]. +pub struct SerializedBlendGraph { + /// Corresponds to the `graph` field on [`BlendGraph`]. + pub graph: DiGraph, + /// Corresponds to the `root` field on [`BlendGraph`]. pub root: NodeIndex, - /// Corresponds to the `mask_groups` field on [`AnimationGraph`]. + /// Corresponds to the `mask_groups` field on [`BlendGraph`]. pub mask_groups: HashMap, } -/// A version of [`AnimationGraphNode`] suitable for serializing as an asset. +/// A version of [`BlendGraphNode`] suitable for serializing as an asset. /// -/// See the comments in [`SerializedAnimationGraph`] for more information. +/// See the comments in [`SerializedBlendGraph`] for more information. #[derive(Serialize, Deserialize)] -pub struct SerializedAnimationGraphNode { - /// Corresponds to the `node_type` field on [`AnimationGraphNode`]. +pub struct SerializedBlendGraphNode { + /// Corresponds to the `node_type` field on [`BlendGraphNode`]. pub node_type: SerializedAnimationNodeType, - /// Corresponds to the `mask` field on [`AnimationGraphNode`]. + /// Corresponds to the `mask` field on [`BlendGraphNode`]. pub mask: AnimationMask, - /// Corresponds to the `weight` field on [`AnimationGraphNode`]. + /// Corresponds to the `weight` field on [`BlendGraphNode`]. pub weight: f32, } /// A version of [`AnimationNodeType`] suitable for serializing as part of a -/// [`SerializedAnimationGraphNode`] asset. +/// [`SerializedBlendGraphNode`] asset. #[derive(Serialize, Deserialize)] pub enum SerializedAnimationNodeType { /// Corresponds to [`AnimationNodeType::Clip`]. @@ -412,11 +410,11 @@ pub enum SerializedAnimationClip { /// groups per animation graph. pub type AnimationMask = u64; -impl AnimationGraph { +impl BlendGraph { /// Creates a new animation graph with a root node and no other nodes. pub fn new() -> Self { let mut graph = DiGraph::default(); - let root = graph.add_node(AnimationGraphNode::default()); + let root = graph.add_node(BlendGraphNode::default()); Self { graph, root, @@ -424,7 +422,7 @@ impl AnimationGraph { } } - /// A convenience function for creating an [`AnimationGraph`] from a single + /// A convenience function for creating an [`BlendGraph`] from a single /// [`AnimationClip`]. /// /// The clip will be a direct child of the root with weight 1.0. Both the @@ -435,7 +433,7 @@ impl AnimationGraph { (graph, node_index) } - /// A convenience method to create an [`AnimationGraph`]s with an iterator + /// A convenience method to create an [`BlendGraph`]s with an iterator /// of clips. /// /// All of the animation clips will be direct children of the root with @@ -463,7 +461,7 @@ impl AnimationGraph { weight: f32, parent: AnimationNodeIndex, ) -> AnimationNodeIndex { - let node_index = self.graph.add_node(AnimationGraphNode { + let node_index = self.graph.add_node(BlendGraphNode { node_type: AnimationNodeType::Clip(clip), mask: 0, weight, @@ -483,7 +481,7 @@ impl AnimationGraph { weight: f32, parent: AnimationNodeIndex, ) -> AnimationNodeIndex { - let node_index = self.graph.add_node(AnimationGraphNode { + let node_index = self.graph.add_node(BlendGraphNode { node_type: AnimationNodeType::Clip(clip), mask, weight, @@ -522,7 +520,7 @@ impl AnimationGraph { /// weights multiplied by the weight of the blend. The blend node will have /// no mask. pub fn add_blend(&mut self, weight: f32, parent: AnimationNodeIndex) -> AnimationNodeIndex { - let node_index = self.graph.add_node(AnimationGraphNode { + let node_index = self.graph.add_node(BlendGraphNode { node_type: AnimationNodeType::Blend, mask: 0, weight, @@ -545,7 +543,7 @@ impl AnimationGraph { weight: f32, parent: AnimationNodeIndex, ) -> AnimationNodeIndex { - let node_index = self.graph.add_node(AnimationGraphNode { + let node_index = self.graph.add_node(BlendGraphNode { node_type: AnimationNodeType::Blend, mask, weight, @@ -566,7 +564,7 @@ impl AnimationGraph { weight: f32, parent: AnimationNodeIndex, ) -> AnimationNodeIndex { - let node_index = self.graph.add_node(AnimationGraphNode { + let node_index = self.graph.add_node(BlendGraphNode { node_type: AnimationNodeType::Add, mask: 0, weight, @@ -589,7 +587,7 @@ impl AnimationGraph { weight: f32, parent: AnimationNodeIndex, ) -> AnimationNodeIndex { - let node_index = self.graph.add_node(AnimationGraphNode { + let node_index = self.graph.add_node(BlendGraphNode { node_type: AnimationNodeType::Add, mask, weight, @@ -618,22 +616,22 @@ impl AnimationGraph { .is_some() } - /// Returns the [`AnimationGraphNode`] associated with the given index. + /// Returns the [`BlendGraphNode`] associated with the given index. /// /// If no node with the given index exists, returns `None`. - pub fn get(&self, animation: AnimationNodeIndex) -> Option<&AnimationGraphNode> { + pub fn get(&self, animation: AnimationNodeIndex) -> Option<&BlendGraphNode> { self.graph.node_weight(animation) } - /// Returns a mutable reference to the [`AnimationGraphNode`] associated + /// Returns a mutable reference to the [`BlendGraphNode`] associated /// with the given index. /// /// If no node with the given index exists, returns `None`. - pub fn get_mut(&mut self, animation: AnimationNodeIndex) -> Option<&mut AnimationGraphNode> { + pub fn get_mut(&mut self, animation: AnimationNodeIndex) -> Option<&mut BlendGraphNode> { self.graph.node_weight_mut(animation) } - /// Returns an iterator over the [`AnimationGraphNode`]s in this graph. + /// Returns an iterator over the [`BlendGraphNode`]s in this graph. pub fn nodes(&self) -> impl Iterator { self.graph.node_indices() } @@ -641,8 +639,8 @@ impl AnimationGraph { /// Serializes the animation graph to the given [`Write`]r in RON format. /// /// If writing to a file, it can later be loaded with the - /// [`AnimationGraphAssetLoader`] to reconstruct the graph. - pub fn save(&self, writer: &mut W) -> Result<(), AnimationGraphLoadError> + /// [`BlendGraphAssetLoader`] to reconstruct the graph. + pub fn save(&self, writer: &mut W) -> Result<(), BlendGraphLoadError> where W: Write, { @@ -660,7 +658,7 @@ impl AnimationGraph { } } -impl AnimationGraphNode { +impl BlendGraphNode { /// Masks out the mask groups specified by the given `mask` bitfield. /// /// A 1 in bit position N causes this function to mask out mask group N, and @@ -700,21 +698,21 @@ impl AnimationGraphNode { } } -impl Index for AnimationGraph { - type Output = AnimationGraphNode; +impl Index for BlendGraph { + type Output = BlendGraphNode; fn index(&self, index: AnimationNodeIndex) -> &Self::Output { &self.graph[index] } } -impl IndexMut for AnimationGraph { +impl IndexMut for BlendGraph { fn index_mut(&mut self, index: AnimationNodeIndex) -> &mut Self::Output { &mut self.graph[index] } } -impl Default for AnimationGraphNode { +impl Default for BlendGraphNode { fn default() -> Self { Self { node_type: Default::default(), @@ -724,18 +722,18 @@ impl Default for AnimationGraphNode { } } -impl Default for AnimationGraph { +impl Default for BlendGraph { fn default() -> Self { Self::new() } } -impl AssetLoader for AnimationGraphAssetLoader { - type Asset = AnimationGraph; +impl AssetLoader for BlendGraphAssetLoader { + type Asset = BlendGraph; type Settings = (); - type Error = AnimationGraphLoadError; + type Error = BlendGraphLoadError; async fn load( &self, @@ -746,17 +744,17 @@ impl AssetLoader for AnimationGraphAssetLoader { let mut bytes = Vec::new(); reader.read_to_end(&mut bytes).await?; - // Deserialize a `SerializedAnimationGraph` directly, so that we can + // Deserialize a `SerializedBlendGraph` directly, so that we can // get the list of the animation clips it refers to and load them. let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?; - let serialized_animation_graph = SerializedAnimationGraph::deserialize(&mut deserializer) + let serialized_animation_graph = SerializedBlendGraph::deserialize(&mut deserializer) .map_err(|err| deserializer.span_error(err))?; // Load all `AssetPath`s to convert from a - // `SerializedAnimationGraph` to a real `AnimationGraph`. - Ok(AnimationGraph { + // `SerializedBlendGraph` to a real `BlendGraph`. + Ok(BlendGraph { graph: serialized_animation_graph.graph.map( - |_, serialized_node| AnimationGraphNode { + |_, serialized_node| BlendGraphNode { node_type: match serialized_node.node_type { SerializedAnimationNodeType::Clip(ref clip) => match clip { SerializedAnimationClip::AssetId(asset_id) => { @@ -784,14 +782,14 @@ impl AssetLoader for AnimationGraphAssetLoader { } } -impl From for SerializedAnimationGraph { - fn from(animation_graph: AnimationGraph) -> Self { +impl From for SerializedBlendGraph { + fn from(animation_graph: BlendGraph) -> Self { // If any of the animation clips have paths, then serialize them as // `SerializedAnimationClip::AssetPath` so that the - // `AnimationGraphAssetLoader` can load them. + // `BlendGraphAssetLoader` can load them. Self { graph: animation_graph.graph.map( - |_, node| SerializedAnimationGraphNode { + |_, node| SerializedBlendGraphNode { weight: node.weight, mask: node.mask, node_type: match node.node_type { @@ -815,15 +813,15 @@ impl From for SerializedAnimationGraph { } } -/// A system that creates, updates, and removes [`ThreadedAnimationGraph`] -/// structures for every changed [`AnimationGraph`]. +/// A system that creates, updates, and removes [`ThreadedBlendGraph`] +/// structures for every changed [`BlendGraph`]. /// -/// The [`ThreadedAnimationGraph`] contains acceleration structures that allow +/// The [`ThreadedBlendGraph`] contains acceleration structures that allow /// for quick evaluation of that graph's animations. pub(crate) fn thread_animation_graphs( - mut threaded_animation_graphs: ResMut, - animation_graphs: Res>, - mut animation_graph_asset_events: EventReader>, + mut threaded_animation_graphs: ResMut, + animation_graphs: Res>, + mut animation_graph_asset_events: EventReader>, ) { for animation_graph_asset_event in animation_graph_asset_events.read() { match *animation_graph_asset_event { @@ -862,8 +860,8 @@ pub(crate) fn thread_animation_graphs( } } -impl ThreadedAnimationGraph { - /// Removes all the data in this [`ThreadedAnimationGraph`], keeping the +impl ThreadedBlendGraph { + /// Removes all the data in this [`ThreadedBlendGraph`], keeping the /// memory around for later reuse. fn clear(&mut self) { self.threaded_graph.clear(); @@ -871,8 +869,8 @@ impl ThreadedAnimationGraph { self.sorted_edges.clear(); } - /// Prepares the [`ThreadedAnimationGraph`] for recursion. - fn init(&mut self, animation_graph: &AnimationGraph) { + /// Prepares the [`ThreadedBlendGraph`] for recursion. + fn init(&mut self, animation_graph: &BlendGraph) { let node_count = animation_graph.graph.node_count(); let edge_count = animation_graph.graph.edge_count(); @@ -887,18 +885,13 @@ impl ThreadedAnimationGraph { self.computed_masks.extend(iter::repeat(0).take(node_count)); } - /// Recursively constructs the [`ThreadedAnimationGraph`] for the subtree + /// Recursively constructs the [`ThreadedBlendGraph`] for the subtree /// rooted at the given node. /// /// `mask` specifies the computed mask of the parent node. (It could be /// fetched from the [`Self::computed_masks`] field, but we pass it /// explicitly as a micro-optimization.) - fn build_from( - &mut self, - graph: &AnimationDiGraph, - node_index: AnimationNodeIndex, - mut mask: u64, - ) { + fn build_from(&mut self, graph: &BlendDiGraph, node_index: AnimationNodeIndex, mut mask: u64) { // Accumulate the mask. mask |= graph.node_weight(node_index).unwrap().mask; self.computed_masks[node_index.index()] = mask; diff --git a/crates/bevy_animation/src/lib.rs b/crates/bevy_animation/src/lib.rs index 8d55cb7ea5c81..102ea5c32645d 100755 --- a/crates/bevy_animation/src/lib.rs +++ b/crates/bevy_animation/src/lib.rs @@ -28,7 +28,7 @@ use core::{ use graph::AnimationNodeType; use prelude::AnimationCurveEvaluator; -use crate::graph::{AnimationGraphHandle, ThreadedAnimationGraphs}; +use crate::graph::{BlendGraphHandle, ThreadedBlendGraphs}; use bevy_app::{App, Plugin, PostUpdate}; use bevy_asset::{Asset, AssetApp, Assets}; @@ -75,7 +75,7 @@ pub mod prelude { use crate::{ animation_curves::AnimationCurve, - graph::{AnimationGraph, AnimationGraphAssetLoader, AnimationNodeIndex}, + graph::{AnimationNodeIndex, BlendGraph, BlendGraphAssetLoader}, transition::{advance_transitions, expire_completed_transitions, AnimationTransitions}, }; @@ -557,7 +557,7 @@ pub enum AnimationEvaluationError { /// An stopped animation is considered no longer active. #[derive(Debug, Clone, Copy, Reflect)] pub struct ActiveAnimation { - /// The factor by which the weight from the [`AnimationGraph`] is multiplied. + /// The factor by which the weight from the [`BlendGraph`] is multiplied. weight: f32, repeat: RepeatAnimation, speed: f32, @@ -956,8 +956,8 @@ impl AnimationPlayer { fn trigger_untargeted_animation_events( mut commands: Commands, clips: Res>, - graphs: Res>, - players: Query<(Entity, &AnimationPlayer, &AnimationGraphHandle)>, + graphs: Res>, + players: Query<(Entity, &AnimationPlayer, &BlendGraphHandle)>, ) { for (entity, player, graph_id) in &players { // The graph might not have loaded yet. Safely bail. @@ -1003,8 +1003,8 @@ fn trigger_untargeted_animation_events( pub fn advance_animations( time: Res