diff --git a/BeatSaberMarkupLanguage/Animations/AnimationLoader.cs b/BeatSaberMarkupLanguage/Animations/AnimationLoader.cs index 7354438d..e6d16501 100644 --- a/BeatSaberMarkupLanguage/Animations/AnimationLoader.cs +++ b/BeatSaberMarkupLanguage/Animations/AnimationLoader.cs @@ -42,9 +42,7 @@ public static void Process(AnimationType type, byte[] data, Action callback) { - int textureSize = AtlasSizeLimit, width = 0, height = 0; - Texture2D texture = null; - Texture2D[] texList = new Texture2D[animationInfo.frames.Count]; + Texture2D[] textures = new Texture2D[animationInfo.frames.Count]; float[] delays = new float[animationInfo.frames.Count]; float lastThrottleTime = Time.realtimeSinceStartup; @@ -57,15 +55,6 @@ public static IEnumerator ProcessAnimationInfo(AnimationInfo animationInfo, Acti lastThrottleTime = Time.realtimeSinceStartup; } - if (texture == null) - { - textureSize = GetTextureSize(animationInfo, i); - texture = new Texture2D(animationInfo.frames[i].width, animationInfo.frames[i].height); - - width = animationInfo.frames[i].width; - height = animationInfo.frames[i].height; - } - FrameInfo currentFrameInfo = animationInfo.frames[i]; delays[i] = currentFrameInfo.delay; @@ -80,7 +69,7 @@ public static IEnumerator ProcessAnimationInfo(AnimationInfo animationInfo, Acti yield break; } - texList[i] = frameTexture; + textures[i] = frameTexture; // Allow up to .5ms of thread usage for loading this anim if (Time.realtimeSinceStartup > lastThrottleTime + 0.0005f) @@ -90,13 +79,20 @@ public static IEnumerator ProcessAnimationInfo(AnimationInfo animationInfo, Acti } } - Rect[] atlas = texture.PackTextures(texList, 2, textureSize, true); - foreach (Texture2D frameTex in texList) + Texture2D atlasTexture = new(0, 0) { - Object.Destroy(frameTex); + name = "AnimatedImageAtlas", // TODO: it'd be nice to have the actual image name here + }; + + Rect[] atlas = atlasTexture.PackTextures(textures, 2, AtlasSizeLimit, true); + + foreach (Texture2D texture in textures) + { + Object.Destroy(texture); } - callback?.Invoke(texture, atlas, delays, width, height); + FrameInfo firstFrame = animationInfo.frames[0]; + callback?.Invoke(atlasTexture, atlas, delays, firstFrame.width, firstFrame.height); } public static async Task ProcessApngAsync(byte[] data) @@ -113,26 +109,13 @@ public static async Task ProcessGifAsync(byte[] data) private static async Task ProcessAnimationInfoAsync(AnimationInfo animationInfo) { - int textureSize = AtlasSizeLimit; - int width = 0; - int height = 0; - Texture2D texture = null; - Texture2D[] texList = new Texture2D[animationInfo.frames.Count]; + Texture2D[] textures = new Texture2D[animationInfo.frames.Count]; float[] delays = new float[animationInfo.frames.Count]; float lastThrottleTime = Time.realtimeSinceStartup; for (int i = 0; i < animationInfo.frames.Count; i++) { - if (texture == null) - { - textureSize = GetTextureSize(animationInfo, i); - texture = new Texture2D(animationInfo.frames[i].width, animationInfo.frames[i].height); - - width = animationInfo.frames[i].width; - height = animationInfo.frames[i].height; - } - FrameInfo currentFrameInfo = animationInfo.frames[i]; delays[i] = currentFrameInfo.delay; @@ -140,7 +123,7 @@ private static async Task ProcessAnimationInfoAsync(AnimationInfo frameTexture.wrapMode = TextureWrapMode.Clamp; frameTexture.LoadRawTextureData(currentFrameInfo.colors); - texList[i] = frameTexture; + textures[i] = frameTexture; // Allow up to .5ms of thread usage for loading this anim if (Time.realtimeSinceStartup > lastThrottleTime + 0.0005f) @@ -150,46 +133,20 @@ private static async Task ProcessAnimationInfoAsync(AnimationInfo } } - Rect[] atlas = texture.PackTextures(texList, 2, textureSize, true); - foreach (Texture2D frameTex in texList) + Texture2D atlasTexture = new(0, 0) { - Object.Destroy(frameTex); - } + name = "AnimatedImageAtlas", // TODO: it'd be nice to have the actual image name here + }; - return new AnimationData(texture, atlas, delays, width, height); - } + Rect[] atlas = atlasTexture.PackTextures(textures, 2, AtlasSizeLimit, true); - private static int GetTextureSize(AnimationInfo frameInfo, int i) - { - int testNum = 2; - int numFramesInRow; - int numFramesInColumn; - while (true) + foreach (Texture2D texture in textures) { - int numFrames = frameInfo.frames.Count; - - // Make sure the number of frames is cleanly divisible by our testNum - if (numFrames % testNum == 0) - { - numFrames += numFrames % testNum; - } - - // Math.Max to ensure numFramesInRow never becomes 0 to prevent DivideByZeroException - // This would happen with single frame GIFs - numFramesInRow = Math.Max(numFrames / testNum, 1); - numFramesInColumn = numFrames / numFramesInRow; - - if (numFramesInRow <= numFramesInColumn) - { - break; - } - - testNum += 2; + Object.Destroy(texture); } - int textureWidth = Mathf.Clamp(numFramesInRow * frameInfo.frames[i].width, 0, AtlasSizeLimit); - int textureHeight = Mathf.Clamp(numFramesInColumn * frameInfo.frames[i].height, 0, AtlasSizeLimit); - return Mathf.Max(textureWidth, textureHeight); + FrameInfo firstFrame = animationInfo.frames[0]; + return new AnimationData(atlasTexture, atlas, delays, firstFrame.width, firstFrame.height); } } }