Antoshidza / NSprites-Foundation

Basic assets for working with NSprites package
MIT License
65 stars 17 forks source link

Remove unnecessary UV layout dependencies #12

Closed foxor closed 4 months ago

foxor commented 5 months ago

The current implementation requires that sprites appear in animation order within the source texture. There is no reason to require this, and compliance with this constraint can waste GPU memory if a frame is used in multiple animations. The purpose of the system is to determine the frame TextureST, but all you need to do that is a sprite. You can just take an array of sprites from unity's existing sprite pipeline, like so:

BakerExtension.cs :

                var animData = anim.data;

                animationArray[animIndex] = new SpriteAnimationBlobData {
                    ID = Animator.StringToHash(anim.name),
                };
                var atlases = blobBuilder.Allocate(ref animationArray[animIndex].UVAtlases, animData.Sprites.Length);
                for (int frameIndex = 0; frameIndex < atlases.Length; frameIndex++) {
                    atlases[frameIndex] = NSpritesUtils.GetTextureST(animData.Sprites[frameIndex]);
                }

SpriteUVAnimationSystem.cs:

        private void Execute(ref UVAtlas uvAtlas,
                                in AnimationSetLink animationSet,
                                ref AnimationTiming animationIndex
        ) {
            ref var animData = ref animationSet.value.Value[animationIndex.AnimationIndex];
            uvAtlas.value = animData.UVAtlases[animationIndex.FrameIndex];
        }
Antoshidza commented 5 months ago

The reason of such a solution was to simplify authoring process. Most of animation sprite sheets comes in frame order with constant frame size. Cases where same frame can be used in different animations are rare I believe. Though I've thought about such a solution as an alternative for current animation system but no one ever make a request to me to implement that. I think there can be 2 animation system and 2 bakers. One for recalculating UVs and one for keeping them in blob array.