From 773237f53efeb85ddd4812e07fa2bbd6936d32d3 Mon Sep 17 00:00:00 2001 From: Waffle Date: Sat, 13 Dec 2025 05:44:44 +0900 Subject: [PATCH] Add layer split feature --- .../Processors/TextureAtlasProcessor.cs | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/source/AsepriteDotNet/Processors/TextureAtlasProcessor.cs b/source/AsepriteDotNet/Processors/TextureAtlasProcessor.cs index 3450c28..de1c7db 100644 --- a/source/AsepriteDotNet/Processors/TextureAtlasProcessor.cs +++ b/source/AsepriteDotNet/Processors/TextureAtlasProcessor.cs @@ -81,12 +81,14 @@ public static TextureAtlas Process(AsepriteFile file, /// The amount of transparent pixels to add to the edge of the generated texture. /// The amount of transparent pixels to add between each texture region in the generated texture. /// The amount of transparent pixels to add around the edge of each texture region in the generated texture. + /// Indicates whether each layer should be processed separately instead of being flattened together. /// /// The created by this method. If is empty or contains zero /// elements, then is returned. /// /// Thrown when is . - public static TextureAtlas Process(AsepriteFile file, ICollection layers, bool mergeDuplicateFrames = true, int borderPadding = 0, int spacing = 0, int innerPadding = 0) + public static TextureAtlas Process(AsepriteFile file, ICollection layers, bool mergeDuplicateFrames = true, int borderPadding = 0, int spacing = 0, int innerPadding = 0, bool splitLayers = false) + { ArgumentNullException.ThrowIfNull(file); @@ -99,11 +101,42 @@ public static TextureAtlas Process(AsepriteFile file, ICollection layers int frameHeight = file.CanvasHeight; int frameCount = file.Frames.Length; - Rgba32[][] flattenedFrames = new Rgba32[frameCount][]; + Rgba32[][] flattenedFrames; - for (int i = 0; i < frameCount; i++) + if (splitLayers) { - flattenedFrames[i] = file.Frames[i].FlattenFrame(layers); + flattenedFrames = new Rgba32[layers.Count * frameCount][]; + + HashSet layerNames = new HashSet(layers); + + for (int i = 0; i < frameCount; i++) + { + for (int celNum = 0; celNum < Math.Min(layers.Count, file.Frames[i].Cels.Length); celNum++) + { + AsepriteCel cel = file.Frames[i].Cels[celNum]; + + if (!layerNames.Contains(cel.Layer.Name)) continue; + + if (cel is AsepriteLinkedCel linkedCel) + { + cel = linkedCel.Cel; + } + + if (cel is AsepriteImageCel imageCel) + { + flattenedFrames[i * layers.Count + celNum] = imageCel.Pixels.ToArray(); + } + } + } + } + else + { + flattenedFrames = new Rgba32[frameCount][]; + + for (int i = 0; i < frameCount; i++) + { + flattenedFrames[i] = file.Frames[i].FlattenFrame(layers); + } } Dictionary duplicateMap = new Dictionary();