Animation Blending

Posted on

PlayAnimation demo with animation blending

Animation blending means that the transition between animations is smooth. Without animation blending, the old animation instantly stops and the new animation plays. With animation blending, there is a short time during which the old animation “fades out” (it’s applied with decreasing strength) and the new animation “fades in” (analogously, it’s applied with increasing strength).

The movie below shows this technique in action in new Castle Game Engine. Notice how animation changes are sharp when TransitionDuration is zero, but they are smooth when TransitionDuration is larger. This is especially visible when switching between “Idle” and “Attack” animations on the 3D knight model.

This feature was implemented thanks to Castle Game Engine supporters on Patreon, in particular thanks to Robert Daniel Murphy who wished for it. Thank you! Please support me to see more cool features 🙂

How to use this

Use TCastleSceneCore.PlayAnimation with TPlayAnimationParameters, and set TPlayAnimationParameters.TransitionDuration. TransitionDuration is the time (in seconds) when when previous animation fades-out and new animation fades-in, usually values around 0.1-0.5 make sensible smooth effect. TransitionDuration = 0 (default) means that no animation blending occurs.

We have a new demo examples/play_animation/ to test the PlayAnimation method, including Loop, Forward and TransitionDuration.

Additional cool features of our animation blending

  1. Note that animation blending happens even when you restart the same animation. We show this in the movie by restarting the “Walk” animation — note that “old Walk” blends smoothly into “new, restarted Walk” when Transition is non-zero.
  2. Animation blending works for any field that can be linearly interpolated. So it works for animating translations, rotations, coordinate sets and everything else. It works for mesh deformations, or bone animations produced by Spine. It works for 3D or 2D.

Caveats (aka “things that, sadly, do not work yet”)

  1. For now, the animation blending doesn’t work for animations defined by castle-anim-frames (which you probably use to export animations from Blender). Fixing this requires improving the castle-anim-frames loading a bit.

    Background: Animation blending only works when the same mesh is used by all animation frames, but in case of castle-anim-frames we cheat a little and we have many (precalculated) copies of the mesh. This was supposed to be fixed one day anyway, to decrease memory usage (see the TODO here). It turns out that this fix is also needed to enable animation blending.

  2. For now, there is no way to define TransitionDuration for animations run by our TCreature system (using resource.xml) files. The resources animations (like TCreature) are done a little differently, without directly calling PlayAnimation, and they require a bit different code to make use of animation blending.

Both of these will be fixed one day (AD 1 is a work for ~2 days, AD 2 is a work for ~0.5 day). Let me know if any of this is critical for your project, and I’ll try to make it rather sooner than later! 🙂

One Reply to “Animation Blending”

Leave a Reply