vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
467 stars 75 forks source link

Poor performance when creating Animations #71

Closed Crauzer closed 3 years ago

Crauzer commented 3 years ago

I'm bundling together a glTF file that includes a rigged model and a lot of animations (around 60).

This is the code that performs extremely poorly:

foreach((string animationName, LeagueAnimation leagueAnimation) in leagueAnimations)
{
    GltfAnimation animation = root.UseAnimation(animationName);

    foreach(AnimationTrack track in leagueAnimation.Tracks)
    {
        Node joint = joints.FirstOrDefault(x => Cryptography.ElfHash(x.Name) == track.JointHash);

        animation.CreateTranslationChannel(joint, track.Translations);
        animation.CreateScaleChannel(joint, track.Scales);
        animation.CreateRotationChannel(joint, track.Rotations);
    }
}

I've debugged this issue and came to the conclusion that the time it takes to execute a loop increases with the number of animations already added.

The first animation takes around 50ms to create. After 10 loops the time increases to around 1300ms. Note that the size of the animations being converted does not increase.

I haven't debugged the internal code of the library yet, so I cannot conclude what exactly is taking so much time to do.

vpenades commented 3 years ago

Hmm... I've never created a test that exports that many animations, so there's probably some redundancy inside the library.

Do you have public source code that I can use for testing?

Btw, if you're converting some other format to glTF, I would suggest to use the Toolkit's SceneBuilder, it has a somewhat different API, but it's more comfortable to use when you're used to it.

Crauzer commented 3 years ago

This is my library that works with some file formats from a game.

There is a project in there that has the code relevant to this issue.

These are the files you will need for testing: TestingFiles.zip

Only thing you will need to change is where the animations are being loaded from.

vpenades commented 3 years ago

Well... it seems it's going to be quite complex, so I'll try to take a look at it anytime this week