bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Animation Blending

Open Bendzae opened this issue 3 years ago • 6 comments
trafficstars

Objective

  • Extend the bevy animation system to allow blending between animations
    • Should be able to specify the duration of the transition
    • Both the current and the target animations should run while blending between them
    • Provide a working example

Solution

Extended the AnimationPlayer with a function: pub fn cross_fade(&mut self, handle: Handle<AnimationClip>, transition_time: f32) -> &mut AnimationTransition This initiates a fade from the current to the provided animation.

The information about this "Transition" is stored in a the struct AnimationTransition and is added to the AnimationPlayer.

The animation_player system was adapted to handle this new data by interpolating the transforms of the targeted entities between the transforms of the keyframes of the current and target clip.


Changelog

Added

  • AnimationTransition: Representation of the data needed to transition from one to another animation

Changed

  • AnimationPlayer
    • added the cross_fade() function
    • added the transition field
  • animation_player system
  • animated_fox.rs: Adapted example to be able to crossfade or normale play animations

Bendzae avatar Jul 21 '22 14:07 Bendzae

Surprisingly straightforward. I'm not entirely sure how this fits into the broader plans in this area, but given the amount of added functionality relative to the complexity and the importance of Transform animation (which is likely going to end up special-cased) I'm inclined to merge this before the RFCs are in.

alice-i-cecile avatar Jul 21 '22 14:07 alice-i-cecile

While useful in its own right, this implementation is a bit shortsighted. This only supports a single linear transition between two clips, whereas the animation RFCs aim to support an arbitrary N-wise linear blend of multiple clips. This not only supports a transition between single clips, but also more complex blending structures like state machines, layered blends, or N-D blend trees.

Overall, I think we should prioritize reviewing and implementing the RFCs over extending the current animation player, which we've acknowledged was meant as a short term stopgap to ship with 0.7.

I totally agree. My line of thought was just to put it out there to provide an intermediary solution until the RFCs are implemented. So people like myself, that need at least some basic form of animation blending for their project, have something to work with.

If you think it will somehow hinder the progress or is too much effort to review I would have no Problem with closing this PR for now.

Since I am very new to bevy I trust your opinion on if this PR is worth it.

Bendzae avatar Jul 21 '22 17:07 Bendzae

This increase the system animation_player duration by 1.4ms in many_foxes examples, without any transitions between animations crossfade red is main, yellow is this PR

Would it be possible to reduce the impact when there are no transitions ongoing?

mockersf avatar Jul 23 '22 11:07 mockersf

Hi Mockersf, Sorry for the late reply, what profiling tool are you using to generate that graph? I will try to optimize frametimes for non-transition animations asap.

Bendzae avatar Jul 29 '22 08:07 Bendzae

I am using Tracy: https://github.com/wolfpld/tracy. There are some docs in how to use with Bevy here: https://github.com/bevyengine/bevy/blob/main/docs/profiling.md#backend-trace_tracy

You'll need to enable Bevy feature trace_tracy: cargo run --release --example many_foxes --features trace_tracy

mockersf avatar Jul 29 '22 08:07 mockersf

This increase the system animation_player duration by 1.4ms in many_foxes examples, without any transitions between animations crossfade red is main, yellow is this PR

Would it be possible to reduce the impact when there are no transitions ongoing?

I managed to get it down to a difference of about .1 ms on my machine. Would that be fine with you @mockersf ?

image

Bendzae avatar Jul 31 '22 15:07 Bendzae

@mockersf Just to check, is this PR still in consideration or should I just close it?

Bendzae avatar Aug 11 '22 14:08 Bendzae

Animation changed since this PR, and this feature should be covered by #6922

mockersf avatar Jan 16 '23 21:01 mockersf