Rendering animation frame by frame

By now I’m mostly sure this is a bug with Unity :frowning: I’m using 2018.4.12f1 LTS and having a lot of trouble with synchronizing an animation with the event loop.

What I’m trying to do is load an animation on an avatar and then render each frame of the animation in a renderTexture. What ends happening is the animation at frame 1 is still in the T-pose, then the renders are de-synced by 1-2 frames. Even more strange, any extra meshes on the avatar get rendered correctly, but not the avatar itself.

I made a small example with the standard assets. The script I’m using, attached to the avatar:

public class framebybrame : MonoBehaviour
{
    private Animator m_avatarAnimator;
    public Camera m_camera;
    public RenderTexture m_renderTexture;

    private int m_curAnimationFrame = 0;

    void Start()
    {
        m_avatarAnimator = GetComponent<Animator>();
        m_avatarAnimator.speed = 0f;
        m_avatarAnimator.Play(m_avatarAnimator.runtimeAnimatorController.animationClips[0].name, 
                                               0, 
                                               0);

        m_camera.enabled = false;

        m_renderTexture = new RenderTexture(640, 480, 32, RenderTextureFormat.ARGB32);
        m_camera.targetTexture = m_renderTexture;
    }
   
    void Update()
    {
        float length = m_avatarAnimator.runtimeAnimatorController.animationClips[0].length;
        float frameRate = m_avatarAnimator.runtimeAnimatorController.animationClips[0].frameRate;
        float normalizedLength = 1.0f / (length * frameRate);

        m_avatarAnimator.Play(m_avatarAnimator.runtimeAnimatorController.animationClips[0].name, 
                                              0, 
                                              m_curAnimationFrame * normalizedLength);

        m_curAnimationFrame++;
    }

    private void LateUpdate()
    {
        m_camera.Render();
    }
}

The resulting images for the first 3 frames are attached. They were done by pausing, then playing inside the editor, frame by frame.

Any idea what would cause this kind of behavior? Or if I should use some other functionality to get the results I need?

No idea about the de-sync, but the first frame T pose was an issue I encountered when using the Playables API in Unity 2017. Though it was fixed in 2018.1 so you would hope it didn’t come back in the LTS version.

Check out Animancer (link in my signature). I’ve never done anything like what you’re trying, but it gives you more direct control over the animation details and there are quite a few subtle differences between the Playables API and regular Animator Controllers so it might help.