Custom blend trees

AdvancedProgrammer

The AnimationComponent has the property AnimationComponent.BlendTreeBuilder. If you want absolute control over which animations are played, how are they blended and what weights they have, you can create a script which inherits from IBlendTreeBuilder and assign it to the BlendTreeBuilder under your animation component.

When the animation component is updated, it calls void BuildBlendTree(FastList<AnimationOperation> animationList) on your script instead of updating the animations itself. This allows you to choose any combination of animation clips, speeds and blends, but is also more difficult, as all the heavy lifting is now on the script side.

The templates First-person shooter, Third-person platformer and Top-down RPG, included with Xenko, are examples of how to use custom blend trees.

Code sample

  1. public class AnimationBlendTree : SyncScript, IBlendTreeBuilder
  2. {
  3. /// <summary>
  4. /// The animation component is required
  5. /// </summary>
  6. [Display("Animation Component")]
  7. public AnimationComponent AnimationComponent { get; set; }
  8. [Display("Walk")]
  9. public AnimationClip AnimationWalk { get; set; }
  10. [Display("Run")]
  11. public AnimationClip AnimationRun { get; set; }
  12. [Display("Lerp Factor")]
  13. public float LerpFactor = 0.5f;
  14. private AnimationClipEvaluator animEvaluatorWalk;
  15. private AnimationClipEvaluator animEvaluatorRun;
  16. private double currentTime = 0;
  17. public override void Start()
  18. {
  19. base.Start();
  20. // IMPORTANT STEP
  21. // By setting a custom blend tree builder we can override the default behavior of the animation system.
  22. // Instead, BuildBlendTree(FastList<AnimationOperation> blendStack) will be called each frame.
  23. // We need to update the animation state in Update() and then
  24. // pass the new animation state (stack = blend tree) to the animation system.
  25. AnimationComponent.BlendTreeBuilder = this;
  26. // As we override the animation system, we need to create an AnimationClipEvaluator for each clip we want to use.
  27. animEvaluatorWalk = AnimationComponent.Blender.CreateEvaluator(AnimationWalk);
  28. animEvaluatorRun = AnimationComponent.Blender.CreateEvaluator(AnimationRun);
  29. }
  30. public override void Cancel()
  31. {
  32. // When the script is cancelled, don't forget to release all animation resources created in Start() - AnimationClipEvaluators
  33. AnimationComponent.Blender.ReleaseEvaluator(animEvaluatorWalk);
  34. AnimationComponent.Blender.ReleaseEvaluator(animEvaluatorRun);
  35. }
  36. public override void Update()
  37. {
  38. // Use DrawTime rather than UpdateTime because the animations are updated only when they are drawn.
  39. var time = Game.DrawTime;
  40. // This update function accounts for animation with different durations,
  41. // keeping a current time relative to the blended maximum duration.
  42. long blendedMaxDuration = (long)MathUtil.Lerp(AnimationWalk.Duration.Ticks, AnimationRun.Duration.Ticks, LerpFactor);
  43. var currentTicks = TimeSpan.FromTicks((long)(currentTime * blendedMaxDuration));
  44. currentTicks = blendedMaxDuration == 0
  45. ? TimeSpan.Zero
  46. : TimeSpan.FromTicks((currentTicks.Ticks + (long)(time.Elapsed.Ticks)) % blendedMaxDuration);
  47. currentTime = ((double)currentTicks.Ticks / (double)blendedMaxDuration);
  48. }
  49. /// BuildBlendTree is called every frame from the animation system when the AnimationComponent needs to be evaluated.
  50. /// It overrides the default behavior of the AnimationComponent by setting a custom blend tree.
  51. public void BuildBlendTree(FastList<AnimationOperation> blendStack)
  52. {
  53. var timeWalk = TimeSpan.FromTicks((long) (currentTime * AnimationWalk.Duration.Ticks));
  54. var timeRun = TimeSpan.FromTicks((long) (currentTime * AnimationRun.Duration.Ticks));
  55. // Build the animation blend tree (stack)
  56. blendStack.Add(AnimationOperation.NewPush(animEvaluatorWalk, timeWalk)); // Will PUSH animation state to be evaluated at the specified Time.
  57. blendStack.Add(AnimationOperation.NewPush(animEvaluatorRun, timeRun)); // Will PUSH another animation state to be evaluated at the specified Time.
  58. blendStack.Add(AnimationOperation.NewBlend(AnimationBlendOperation.LinearBlend, LerpFactor)); // Will POP the last two states, blend them with the factor and PUSH back the result.
  59. // NOTE
  60. // Because the blending operations are laid out in a stack you have to pack the operations in this manner.
  61. // In general, traversing a binary tree depth-first and adding operations as you *leave* precessed nodes should be sufficient.
  62. // For non-binary trees, you have to properly weight the blending factors as well
  63. // DONE
  64. // The top of the stack now contains the final state used for the animated model
  65. }
  66. }

See also