Antoshidza / NSprites

Unity DOTS Sprite Rendering Package
Other
536 stars 42 forks source link

Supporting Multiple Animations in the Same Sprite Sheet #11

Closed andymitchellx closed 9 months ago

andymitchellx commented 9 months ago

Thanks for NSprite.

I have a sprite sheet with multiple animations. I was wondering if you'd consider skipping "0 second length" frames in SpriteUVAnumationSystem. So, "walk" could be frames 0-3 and "idle" could be frames 4-7.

Here's one proposal to check the nextFrameDuration. I would add an escape clause to the loop in case someone accidentally create an AnimationSet where all frames are set to 0.

A better way might be to change SpriteAnimatedRendererAuthoring to not load them into the animationSet. That would avoid checking each frame.

            if (timerDelta >= 0f)
            {
                ref var animData = ref animationSet.value.Value[animationIndex.value];
                var frameCount = animData.FrameDurations.Length;
                frameIndex.value = (frameIndex.value + 1) % frameCount;
                var nextFrameDuration = animData.FrameDurations[frameIndex.value];

                ////// added.  Would need an escape clause in case all frames are set to 0.
                while (nextFrameDuration == 0f) 
                {
                    frameIndex.value = (frameIndex.value + 1) % frameCount;
                    nextFrameDuration = animData.FrameDurations[frameIndex.value];
                }

                if (timerDelta >= animData.AnimationDuration)
                {
                    var extraTime = (float)(timerDelta % animData.AnimationDuration);
                    while (extraTime > nextFrameDuration)
                    {
                        extraTime -= nextFrameDuration;
                        frameIndex.value = (frameIndex.value + 1) % frameCount;
                        nextFrameDuration = animData.FrameDurations[frameIndex.value];
                    }
                    nextFrameDuration -= extraTime;
                }
Antoshidza commented 9 months ago

Thank you for your attention to the project and this proposal.

I've added support for multiple animation in the same texture sheet. But I think skipping zero duration frames isn't something this simple system should care of. I can't REMOVE AT frame during baking if it has zero duration, because frames is just continuously changing UV just by increasing frame index by 1 each duration expired. If I will remove frame from baking I will just lose last one frame in sequence.

Sorry for this long. If you have some working changes and you think it can be added to the project then feel free to make PR.