How does iPhone Unity keyframe reduction work?

Hello,
(I realized this optimization was very iPhone specific)

In short, do I have to optimize my char’s IK baking on the 3D modeling tool, or is Unity’s native keyframe reducer sufficient to do a maximum optimization ?

On every 3D tool, IK baking writes every frame, and this is stunningly hitting performance on iPhone, so devs would face this problem each time they want to implement an IK animated character in their game.

Actually, I can’t find a non-destructive optimization for, let’s say, leg IKs. Imagine, at a 30 fps animation, you would have 30 x 4 bones transform processes per second … That’s way too much !

It is a real important point for every technically advanced games to come, because it’s uselessly eating performance that could be distributed elsewhere.

How do you, gamedevs, manage IKs exports in your iPhone games ? Do you keep each frame written ?

update : I found that 3DS can highly reduce those baked IKs keys in a non-destructive manner.

But my question still ermains : is Unity reduction process as efficient ? Or do we first have to reduce it 3D tool wise, to have max optimization ?

Hi,

I used motion capture data for Touch KO. Unity’s keyframe reduction handled it fine and this was never an issue. The only issue is data overhead, not computation. No matter how dense your animation data is, all that is fundamentally happening is evaluating a curve at some time. Again though, even data overhead for us was inconsequential for having mocap, so I would just use Unity’s keyframe reduction and not worry about it.

Hello Adam :slight_smile:

Thank you for your precious answer, there couldn’t be best persons than the ones who made Touch KO to give me an advice on Keyframe reduction.

Ok, I’ll let Unity handle the job then !

I had this interrogation because I thought that each keyframe was a new var to store in memory, resulting in an overall useless written space if we have all the frames recorded.

So if Unity is handling it good, let’s trust it. But there are many, many ways to reduce keyframes : Cinema 4D let the choice between 2 solutions for example, and which are even different from the 3DS one.

I tested all the 3, and 3DS came with the most reducing non-destructive method. So the overall performance does depend on Unity’s reducing method, as it “could” be better to do it with 3DS, for instance.

Although, what do you mean by data overhead ?

You got me curious.

Here’s a little script that dumps some statistics of the default animation clip (curves + total keys) in the debug log.

Run it on model before Unity keyframe reduction and after it. You’ll see Unity manages to reduce quite a bit.

gr.jakko

using UnityEngine;
using UnityEditor;
using System.Collections;

public class ShowAnimationStats : ScriptableObject
{
    [MenuItem("GameObject/Show AnimatioClip stats")]
    public static void AnimationClipStats()
    {
        foreach (GameObject go in Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel))
        {
            Animation animation = go.animation;

            if (animation != null  animation.clip != null)
            {
                AnimationClipCurveData[] data = AnimationUtility.GetAllCurves(animation.clip, true);
                int curves = 0;
                int keys = 0;
                foreach (AnimationClipCurveData cd in data)
                {
                    if (cd.curve != null)
                    {
                        curves++;
                        keys += cd.curve.length;
                    }
                }
                Debug.Log("Animation " + animation.name + ": curves=" + curves + " keys=" + keys);
            }
        }
    }
}

Cool. I will test this asap. Thank you Jakko.

In theory, that is the case. A curve with 3 keyframes is much less data overhead (download size, memory) than an identical curve with 100 keyframes. In practice, however, the Unity keyframe reducer works well enough that I never had a problem with too much data. My point was just that there is no more computational overhead for determining the final animation this way, as it will use the same interpolation process between keyframes to determine the curve’s value.

Keep in mind that your rotational keyframe data won’t be identical in Unity anyway, even if you use an external reducer as opposed to Unity’s. All of your Euler angles get converted into quaternions, which means they need different keys in order to maintain parity (e.g. if you rotate more than 180 degrees, if you are using weighted tangents, etc.)

Gosh !

Didn’t expect that … Would there be an importable format (except FBX) that writes directly in quaternions ?

Thank you for the info Adam :wink:

I know I may seem too perfectionist on this one, but I really want the best possible framerate, just to let every current iPlatform to play the game.

FBX can save rotation curves as quaternions as long as your package supports it. I know Maya and Max allow you to animate with quaternions. I still think Unity will change these data though because a quaternion in Maya for example lets you rotate up to 720 degrees, but Unity only 180.

Again, though, I think this discussion is pretty academic. We get the exact same performance on a 1st-generation iPhone as we get on the iPhone 3G without doing anything special. Unity’s keyframe reduction works perfectly fine. There are going to be hundreds of places where you will get real and correctable performance hits; this is not one of them, so I woudln’t bother to invest any more time in it.

Ok, thank you for your support. :wink:

quick update :

Didn’t have the time to test Unity’s reduction via Jakko’s script, but I had the opportunity to test 3DS keyframe reduction.

→ The FBX file went from 11.5 Mo to 1.6 Mo.
But it took 4 hours of processing !

The size was cut down by 10. That’s huge. And finally, I can absolutely see no differences between the original and the reduced animation.

So if 3DS has to take 4 hours of work for 1010 frames of 41 bones, I think Unity might be very good at reducing, but it will never come close to a native 3D tool reduction process.

Although, I will make those tests with before/after reduction, thanks to Jakko’s script, and keep you informed.

edit : Overall, I can see no difference either with the old 11.5 Mo FBX on my 3GS. The FPS is the same.
I have to test it with a 3G, but Unity still wins a point on this.

Finally …

I checked the app build size with the 2 FBX file versions.

Unreduced : 27.2 Mo
Reduced : 24.6 Mo.

Only a 2.6 Mo delta from an original 11.5 Mo file, where Cinema 4D didn’t manage to cut it more than ~2 Mo.

Congratulations, Unity Team.

thats likely because unity does not store it in fbx but an own format.

Ah, that explains … ^^

It’s odd to have such huge data formats, since it can be written in binary. In ASCII I would have understood, but there …

Well, nevermind, we got a very nice optimization, which is more important than file size :slight_smile:

Just out of personal curiosity, what are you using in Max to reduce keys? Maya’s built-in keyframe reducer work pretty much instantaneously, so this comes as news to me.

I used the Timeline’s function “Reduce keys…”.

Maybe that high process time comes from the fact that I got 41 animated bones to reduce, for a total animation of 1010 frames, with some of them having a key on each frame.

Curves are very precise and saw-like, too, due to quick and wild character movements.

I launched the calculation on an Intel core2 E8500 + 4 Go RAM (1333 MHz), under Vista in 3DS 64-bit mode.

That’s still really surprising to me I guess. My characters were only around 20 bones apiece (with keyframes on every frame) but I could still reduce an animation that was several thousand frames long instantaneously.