Search Unity

Animation cost / key reducer

Discussion in 'iOS and tvOS' started by mattimus, Mar 6, 2010.

  1. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    First of all, I would like to dispel the notion that animation is not worth the time to optimize. This is wildly inaccurate. With certain settings, animation has made my game double in app size and take 10x longer to load.  The main culprit?  Animation key reducer. 

    - I am working with three scenes loaded additively. First is the scene loader. Second is the character scene. Third is the ui. 
    - The character has 21 animation clips so far, with more on the way

    As I added animation clips, the game started receiving severe memory warnings on 3g and often crashed.   Everyone kept insisting that animation can't be the cause of the memory problems because they contain such small amounts of data comparitively. Believing that to be true, I moved on and scrutinized every other asset in the game, one file at a time and ultimately came up with nothing. There simply was no other explanation, so I started tinkering with the animation import settings.  In this process, I found a major problem - the Keyframe Reducer. That option was switched off for reasons I will go into shortly, but here are the results:

    --key reducer off, 21 animations--
    app size (uncompressed) = 64.4MB
    Scene load times:
       Scene loader = 664
        Character scene = 19080
        UI scene = 6697

     --key reducer on, same animations--
    app size (uncompressed) = 34.4MB
    Scene load times:
       Scene loader = 677
        Character scene =1312
        UI scene = 2066

    The lack of key reducer had doubled the app size and made the character scene take over 10x longer to load. 

    Now, you may be asking yourself, why would this guy turn off the key reducer in the first place?  The answer is simple. The key reducer sucks.  Here's why.

    Every animation I have completed for this project uses planted keys to anchor the feet (and sometimes hands) to the floor. This allows me to grab any part of the body and animate it, knowing that the feet will not move.   When I export the FBX, the keys stay intact. I can bring the animation back into Max or into Maya using the keyframe reducer for either software, and the feet remain anchored. When I bring the same FBX into Unity the feet slide around. Often quite noticably.  The only way to stop the feet from sliding is to turn off the key reducer. 

    So there's my problem. If I keep the key reducer off I have a game that doesn't work on anything lower than a 3gs. If I turn it on, my character floats around. 

    I will post some samples of the animation sliding in the morning. 
     
  2. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    Attached is a web player showing the bug with keyframe reduction. To make this player, I duplicated the same character file within unity and turned off keyframe reduction on the left character. You can see quite a large difference, especially in the hands.
     

    Attached Files:

  3. spacefrog

    spacefrog

    Joined:
    Jun 14, 2009
    Posts:
    734
    I guess the problem is the keyframe interpolation type. For planted objects like in your sample, you can't get stable locked position/rotation without setting interpolation type to linear for the keys in question, or the interpolation will overshoot....
     
  4. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    The hand keyframes are set to be linear in the animation file. They interpolate just fine without overshooting in Max / Maya and in Unity when the Reduce Keys option is off. It seems to be more a problem of which keys Unity is deciding to remove and how it decides to interpolate between the remaining keys.
     
  5. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    I'm interested in such an analysis, as character animation is 70% of my resources/app size.

    Actually I didn't experience any problem with the reducer, but if there is any possible additional optimization secret tips to know, I would be very interested.
     
  6. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    The animation compressor in Unity doesn't "suck", its better than most I've seen at triple-A studios. That said, animation compression always introduces some degree of floatiness to planted IK legs and such, there's no getting around it without either (A) having no compression or (B) doing real-time IK in game, the latter of which is probably impractical for an iPhone game.

    Would it be possible in your case to turn off compression only for those clips where the foot drift is extremely visible?

    Edit: Okay, I just checked out your example, that's definitely the hallmark of animation compression. If its any consolation, the foot drift seen there is nearly invisible, and on an iPhone screen would be so tiny no one would ever notice or care. The drift on the hands is more severe though due to how many bones there are in the chain - since your animation is being baked down to FK on import, your arms are essentially counter-animating against the wild waving of the spine and pelvis. The distance and number of bones seperating hand from pelvis is causing the warbles you see. If you want to see the worst example of animation compression, try having the end of a enormous 20-bone chain stay planted via IK while the rest of it makes a snake like motion. Its going to wiggle like you won't believe.

    One way to fix this would be to have the hands and feet not actually parented to the rest of the body - make them free floating nodes. Set up your rig so that they follow the limbs correctly, but don't actually belong in the heirarchy. Then, when this wiggle is introduced, it will merely be in the ankles, and the skin connecting angle to foot will warble a little, but the foot contact itself will be rock solid since it has no influences on it from higher in the chain.
     
  7. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    Thanks for the information PNA. That put into words what I had been vaguely suspecting.

    Unfortunately the project is too far along to break up the hands into a different hierarchy. That would just change too many animation files. I could turn off the keyframe reduction for the worst offenders (memory permitting) but I would have to be really selective about which ones get to use that.

    What I would really like to see is some options within Unity's animation importer. A couple ideas:

    1) Max's FBX keyframe reduction has several precision threshold settings that dictate how accurate the keyframe reduction should be. In Unity, it's all or nothing. I would love the ability to adjust the threshold in which a keyframe gets added. I could turn up the sensitivity on some animations to reduce sliding and possibly turn it down on areas that need fewer keys than are given by the current value.

    2) I'm not sure if this is possible, but it would be great to set keyframe reductions on some bones and not others. That way I can keep the arm chain in the proper location without having to bloat the keyframes of every bone in the whole skeleton.


    On a mostly unrelated note, none of my bone animations use scaling. Is there a way I could eliminate the scaling from the animation file? I could see this being a win in both app size and CPU cost of the animation if scaling doesn't have to be taken into consideration. I've noticed when I select some of the bones in Unity they have a scale in one axis that isn't quite 1. It's 0.9999 or something similar. From my understanding, non-uniform scaling is expensive and should be avoided. Is this a problem?
     
  8. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    Any channel with no animation data is excluded from the animation clip file if keyframe reduction is on (not sure if its off), so if your scale is static and has no keys then that should incur no hidden cost. I think the weird 0.99999 values you see in the editor are unrelated to animation, I see them often on static objects too. In any case, I wouldn't sweat it too badly since we used buckets of non-uniform scale in OMG Pirates! and kept it running pretty smooth.

    I agree that optional levels of compression would be nice. Turning off compression per-bone probably wouldn't solve this particular issue though, since its the cumulative effect of inaccuracies all the way up the chain. An uncompressed forearm at the end of an otherwise compressed arm, spine and pelvis is going to look just as busted unfortunately.
     
  9. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Just wondering, could you do the keyframe reduction in the 3d package before you export it out to unity? That way you could see the areas that are being affected by this kind of filtering and keep the keys in the spots you need them?
     
  10. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    Old thread, but I'm being reinjected into it for a fact :

    Before Unity 3, animation were automatically interpolated to 60fps.
    Since Unity3, animations are displayed exactly as what framerate they were set at in the original FBX.
    Concretely, if you've set an animation at 30 fps, you will see a stuttered anim on your 60hz screen.

    Not that it is not a good thing, but as Mattimus pointed, Unity keyframe interpolation can go really bad between some bones.
    So in the end, we are forced with this new politic to bake IKs to a 60 framerate FBX.

    Problem is that such a high framerate causes huge animation curves, and therefore huuuuuuuuuuge loading times / memory allocations.
    For an example, a simple animation.Play() on an Idle pose for a 40 bones animated character takes 40 seconds to load on a Nexus One, with such a setup (baking 8 bones).

    So the only option left is to bake a 60 fps FBX, and then reduce its keyframes within an external keyframe reducer.

    ----> Does anybody know an excellent external keyframe reducer ?
    Most of the ones I know are creating Gimbal Locks (definition), or totally unexpected spline interpolations (Cinema 4D included).
     
    Last edited: Nov 4, 2010
  11. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    Indeed, this thread is old.

    Unity 3 actually corrects the problem by giving us direct control over the keyframe reduction vs allowable error. If you find your animation is sliding to much, dial down the Position Error and/or Rotation Error settings in the animation import settings.
     
  12. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    Unfortunately, I didn't find any satisfying setup :/
    I got some characters that move super fast in their anim, and I need the player to see the move correctly for gameplay / balance reasons.
    So it can works for some anims, but not for others (making them feel like they are teleporting).

    And under a certain reduction threshold, the memory size benefit is too small to resolve loading times problem :/

    In fact, the only reduction setting I found that didn't caused teleportations was :
    Position : 0.1
    Rotation : 0.1
    Scale : 0 (as there is never any animated scale)

    But this is still causing huge file sizes :(
     
  13. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    If you're seeing stuttering in a 30FPS animation in Unity, something is amiss. I animate at 30 (or 24) fps all the time and the results are perfectly smooth with the default compression options, even when time scaled or when using "bullet time" slow motion at runtime, on the device or on my 60hz monitor, it doesn't matter. It's not like FBX animations are actually framerate dependent - it's all just curves being sampled over time. There are no discreet "frames" being cycled through, so stuttering that would fight with screen refresh rates would be impossible.
     
  14. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    That's good news in some way, but I really don't perform anything odd :/
    I'll toy with that ^^ Thanks for input.
     
  15. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    Old topic digging once again ! ^^

    I'm witnessing regular stutters every seconds or so on my new 60fps FBXs, occuring every time I'm slowing down their speed to something near 0.1f. It's also happening with Time.timescale reduction.
    I suspect it to be created by the "animation compression" parameter (not reduction).

    I'll investigate at lunch time, and if it's really the compression toggle it would explain why PNA didn't have those other previous post's stutters at 30fps.

    Anybody had similar experiences ?
     
  16. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    Nevermind, I found where this stuttering problem comes from.
    Sent a bug report.

    -> Since Unity 3, FBX and C4D imports are being generated "stair" shapes for position curves. Never saw something like that before U3.
     

    Attached Files:

    Last edited: Feb 10, 2011
  17. TeddyDief

    TeddyDief

    Joined:
    Sep 17, 2012
    Posts:
    4
    Hi everyone!
    Bringing this topic back from the dead for an extra question. Does anyone have experience compressing / keyframe-reducing animations that ARE NOT imported? I'm using an editor script to alter imported curves, but in the process, the file sizes become much larger, and I think I'm losing the benefit of Unity's animation optimization import settings.

    So, is there a compression/keyframe-reduction tool for use at runtime?

    Thank you!
     
    gevasaf and DaMitch like this.
  18. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    I have the exact same problem. The animations that are made in unity are huge and there does not seem to be a way to compress them.
     
    CloudyVR, gevasaf and DaMitch like this.
  19. DaMitch

    DaMitch

    Joined:
    Feb 6, 2013
    Posts:
    29
    I am also really interested in the compression of animation clips made directly in Unity. It would be awesome to be able to use the same features as the ones available with imported animations !
     
    gevasaf and goldbug like this.
  20. gevasaf

    gevasaf

    Joined:
    Jan 25, 2014
    Posts:
    6
    Joining the above 2 posts - are there any new thoughts or insights on how to compress an animation clip originated from unity?

    Thanks