# Recreating a 2D blend tree in Playables

I have been playing around with moving my animation system from mecanim into code-driven playable graphs, but am quite dependent on 2-dimensional blend trees for locomotion.

I can use the graph-visualizer to see how mecanim is converting my controller into a graph, but don’t fully understand why it is building it the way it does, or how to get the weighting values to drive it.

So a few questions:

Primarily, is there any way to access the logic used for generating the mixer weightings per animation out of the two input parameters?

Each mixer is fed with the (8) input animation clips, one blank clip, and then also one playable… what is this playable?

The blend tree is built as above twice, and then these two are blended together (with a further “playable” added into the mix). What is the reasoning for this, is this for blending between values?

Finally, I had a look through the source code to try and find the logic that converts blend trees to animGraphs, but failed to find anything. Is there somewhere else I should be looking, or is this code not public?

2-dimensional blends are pretty hard. The only real info I’ve found about how to do them well is @runevision 's master’s thesis from 2009, available here (see chapter 6 on motion interpolation). That’s also what’s used in Unity’s Animator.

The tl;dr on that is that doing the naive blend algorithm, where you linearly blend between the nearest neighbours based on distance can give very bad results.

I’ve tried to implement the algorithm myself, but I can’t quite get how to translate the polar coordinate version of the paper to code.

@Mecanim-Dev , thought about exposing the 2D blending as a kind of Playable? Or some other way to expose the built-in implementation of 2D blending?

There’s a C# implementation of the “freeform directional” interpolation in my Locomotion System package:
https://assetstore.unity.com/packages/tools/animation/locomotion-system-7135

It was made around the same time as my Master’s Thesis and predates the implementation in Unity’s 2D blend node. There might be minor differences, but it works fine.

3 Likes

Thanks for the responses guys.
The Polar interpolator from the locomotion system has got me unblocked at making blend spaces now!

Although it would certainly be nice for the internal algorithms used by mecanim to be exposed at some point.

I make a mixerplayable weights via PolarGradientBandInterpolator
the weights result seems correct (compare with build-in BlendTree) but however the animation stream output mess up…

what did i miss?