From 7b02253831dd941c9c1b082fad74dd592909e952 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Wed, 8 Jan 2020 20:44:25 +0100 Subject: [PATCH] [unity] Fixed `GetRepackedSkin` partially ignoring `textureFormat` argument. Closes #1593. Also added optional `TextureFormat` array parameter for additional texture layers. --- .../spine-unity/Utility/AtlasUtilities.cs | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs index c41b363c4..c4bcfe303 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs @@ -232,17 +232,23 @@ namespace Spine.Unity.AttachmentTools { /// When additionalTexturePropertyIDsToCopy is non-null, /// this array will be filled with the resulting repacked texture for every property, /// just as the main repacked texture is assigned to outputTexture. + /// When additionalTexturePropertyIDsToCopy is non-null, + /// this array will be used as TextureFormat at the Texture at the respective property. + /// When additionalTextureFormats is null or when its array size is smaller, + /// textureFormat is used where there exists no corresponding array item. public static void GetRepackedAttachments (List sourceAttachments, List outputAttachments, Material materialPropertySource, out Material outputMaterial, out Texture2D outputTexture, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, string newAssetName = "Repacked Attachments", bool clearCache = false, bool useOriginalNonrenderables = true, - int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null) { + int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null, + TextureFormat[] additionalTextureFormats = null) { Shader shader = materialPropertySource == null ? Shader.Find("Spine/Skeleton") : materialPropertySource.shader; GetRepackedAttachments(sourceAttachments, outputAttachments, shader, out outputMaterial, out outputTexture, maxAtlasSize, padding, textureFormat, mipmaps, newAssetName, materialPropertySource, clearCache, useOriginalNonrenderables, - additionalTexturePropertyIDsToCopy, additionalOutputTextures); + additionalTexturePropertyIDsToCopy, additionalOutputTextures, + additionalTextureFormats); } /// @@ -258,12 +264,17 @@ namespace Spine.Unity.AttachmentTools { /// When additionalTexturePropertyIDsToCopy is non-null, /// this array will be filled with the resulting repacked texture for every property, /// just as the main repacked texture is assigned to outputTexture. + /// When additionalTexturePropertyIDsToCopy is non-null, + /// this array will be used as TextureFormat at the Texture at the respective property. + /// When additionalTextureFormats is null or when its array size is smaller, + /// textureFormat is used where there exists no corresponding array item. public static void GetRepackedAttachments (List sourceAttachments, List outputAttachments, Shader shader, out Material outputMaterial, out Texture2D outputTexture, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, string newAssetName = "Repacked Attachments", Material materialPropertySource = null, bool clearCache = false, bool useOriginalNonrenderables = true, - int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null) { + int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null, + TextureFormat[] additionalTextureFormats = null) { if (sourceAttachments == null) throw new System.ArgumentNullException("sourceAttachments"); if (outputAttachments == null) throw new System.ArgumentNullException("outputAttachments"); @@ -303,8 +314,10 @@ namespace Spine.Unity.AttachmentTools { originalRegions.Add(region); for (int i = 0; i < numTextureParamsToRepack; ++i) { Texture2D regionTexture = (i == 0 ? - region.ToTexture(mipmaps : mipmaps) : - region.ToTexture(mipmaps : mipmaps, texturePropertyId: additionalTexturePropertyIDsToCopy[i - 1])); + region.ToTexture(textureFormat, mipmaps) : + region.ToTexture((additionalTextureFormats != null && i - 1 < additionalTextureFormats.Length) ? + additionalTextureFormats[i - 1] : textureFormat, + mipmaps, additionalTexturePropertyIDsToCopy[i - 1])); texturesToPackAtParam[i].Add(regionTexture); } @@ -332,7 +345,10 @@ namespace Spine.Unity.AttachmentTools { Rect[] rects = null; for (int i = 0; i < numTextureParamsToRepack; ++i) { // Fill a new texture with the collected attachment textures. - var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps); + var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize, + (additionalTextureFormats != null && i - 1 < additionalTextureFormats.Length) ? + additionalTextureFormats[i - 1] : textureFormat, + mipmaps); newTexture.mipMapBias = AtlasUtilities.DefaultMipmapBias; var texturesToPack = texturesToPackAtParam[i]; @@ -387,14 +403,20 @@ namespace Spine.Unity.AttachmentTools { /// When additionalTexturePropertyIDsToCopy is non-null, /// this array will be filled with the resulting repacked texture for every property, /// just as the main repacked texture is assigned to outputTexture. + /// When additionalTexturePropertyIDsToCopy is non-null, + /// this array will be used as TextureFormat at the Texture at the respective property. + /// When additionalTextureFormats is null or when its array size is smaller, + /// textureFormat is used where there exists no corresponding array item. public static Skin GetRepackedSkin (this Skin o, string newName, Material materialPropertySource, out Material outputMaterial, out Texture2D outputTexture, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, bool useOriginalNonrenderables = true, bool clearCache = false, - int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null) { + int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null, + TextureFormat[] additionalTextureFormats = null) { return GetRepackedSkin(o, newName, materialPropertySource.shader, out outputMaterial, out outputTexture, maxAtlasSize, padding, textureFormat, mipmaps, materialPropertySource, - clearCache, useOriginalNonrenderables, additionalTexturePropertyIDsToCopy, additionalOutputTextures); + clearCache, useOriginalNonrenderables, additionalTexturePropertyIDsToCopy, additionalOutputTextures, + additionalTextureFormats); } /// @@ -405,7 +427,8 @@ namespace Spine.Unity.AttachmentTools { public static Skin GetRepackedSkin (this Skin o, string newName, Shader shader, out Material outputMaterial, out Texture2D outputTexture, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, Material materialPropertySource = null, bool clearCache = false, bool useOriginalNonrenderables = true, - int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null) { + int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null, + TextureFormat[] additionalTextureFormats = null) { outputTexture = null; @@ -424,7 +447,7 @@ namespace Spine.Unity.AttachmentTools { } GetRepackedAttachments(inoutAttachments, inoutAttachments, materialPropertySource, out outputMaterial, out outputTexture, maxAtlasSize, padding, textureFormat, mipmaps, newName, clearCache, useOriginalNonrenderables, - additionalTexturePropertyIDsToCopy, additionalOutputTextures); + additionalTexturePropertyIDsToCopy, additionalOutputTextures, additionalTextureFormats); int i = 0; foreach (var originalSkinEntry in o.Attachments) { var newAttachment = inoutAttachments[i++];