Search Unity

Animation C# Jobs: Any way to blend on top of existing RuntimeAnimatorController?

Discussion in 'Animation' started by GeekBrony, Jul 9, 2022.

  1. GeekBrony

    GeekBrony

    Joined:
    Feb 17, 2017
    Posts:
    7
    Hello,

    I've been running into a particular hurdle trying to add procedural adjustments to a model after the animation step. I was hoping to create a system (sort of like an IK), in which the armature can be adjusted for a custom height and length, and update it after the animation, but before the IK system kicks in.

    At first, I tried hooking into LateUpdate+FixedUpdate, so that the animator has already been evaluated by the time my system starts. This works, but I've been having a difficult time with jittering, in which I concluded the culprit was the execution order of my procedural system.

    So I gave up on that idea, and looked into Animation C# Jobs, which was a glimmer of hope for me.
    I was able to hack together an AnimationJob script that did fundamentally the exact same thing.

    Here's part of it, where I additively apply a vector to the position.
    Code (CSharp):
    1.         void ModifyHeight(AnimationStream stream)
    2.         {
    3.             // Apply height adjustment to all bone-relative up directions
    4.             Vector3 pelvisPosition = Transforms.Pelvis.GetPosition(stream);
    5.             pelvisPosition += Transforms.Pelvis.GetDirection(stream, Vector3.up) * Height;
    6.             Transforms.Pelvis.SetPosition(stream, pelvisPosition);
    7.            
    8.             Vector3 neckPosition = Transforms.Neck.GetPosition(stream);
    9.             neckPosition += Transforms.Neck.GetDirection(stream, Vector3.left) * NeckHeight;
    10.             Transforms.Neck.SetPosition(stream, neckPosition);
    11.         }
    It works, but I noticed that the animated bone positions were being replaced by the AnimationJob.
    Not the intended result, in which I need the character to animate the height-adjusted bones from an existing Animator.

    I've been looking through the documentation on Animation Jobs and Playables, but I haven't been able to understand much.
    I've been playing with
    AddInput()
    and
    Connect()
    , in hopes that I can connect the output of the Animator PlayableGraph to the Job graph here, but no dice.

    Code (CSharp):
    1.         private void OnEnable()
    2.         {
    3.             pony = GetComponent<PonyCharacter>();
    4.             animator = GetComponent<Animator>();
    5.             m_Graph = PlayableGraph.Create("Pony Animation");
    6.            
    7.             if (!m_Graph.IsPlaying())
    8.             {
    9.                 // Create the animation job and its playable.
    10.                 animationJob = new PonyModifiersAnimationJob()
    11.                 {
    12.                     Pony = pony.pony,
    13.                     Transforms = new PonyTransformStreamHandles(new PonyBones(pony.anchors), animator)
    14.                 };
    15.  
    16.                 m_ScriptPlayable = AnimationScriptPlayable.Create(m_Graph, animationJob);
    17.                
    18.                 // an attempt with animator blending, but I haven't been able to figure it out.
    19.                 // ...
    20.                 // PlayableGraph animatorGraph = animator.playableGraph;
    21.                 // PlayableOutput animatorOutput = animatorGraph.GetOutput(0);
    22.                 // Playable animatorPlayable = animatorOutput.GetSourcePlayable();
    23.                 // m_ScriptPlayable.AddInput(animatorPlayable, 0, 1);
    24.  
    25.                 PlayableOutput output = AnimationPlayableOutput.Create(m_Graph, "output", animator);
    26.                 output.SetSourcePlayable(m_ScriptPlayable);
    27.                
    28.                 m_Graph.Play();
    29.             }
    30.         }
    Is it possible to use AnimationMixerPlayable or AnimatorControllerPlayable to add the outputs from the Animator PlayableGraph and the AnimationJob together?

    I hope I'm not insane for thinking I can do this :p