From ec7f79d460267709ab6d4ca5ab7b66306b9cf708 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Sat, 18 Jan 2020 16:31:21 +0100 Subject: [PATCH] [unity] Fixed color space of normalmap result when calling GetRepackedSkin() in Linear color space project. Providing additional parameter to specify additional texture layer color space now. Closes #1602. --- .../spine-unity/Utility/AtlasUtilities.cs | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 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 8d3863edf..15ba5543a 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs @@ -323,16 +323,23 @@ namespace Spine.Unity.AttachmentTools { /// 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. + /// When additionalTexturePropertyIDsToCopy is non-null, + /// this array will be used to determine whether linear or sRGB color space is used at the + /// Texture at the respective property. When additionalTextureIsLinear is null, linear color space + /// is assumed at every additional Texture element. + /// When e.g. packing the main texture and normal maps, pass 'new bool[] { true }' at this parameter, because normal maps use + /// linear color space. + /// 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, - TextureFormat[] additionalTextureFormats = null) { + TextureFormat[] additionalTextureFormats = null, bool[] additionalTextureIsLinear = null) { return GetRepackedSkin(o, newName, materialPropertySource.shader, out outputMaterial, out outputTexture, maxAtlasSize, padding, textureFormat, mipmaps, materialPropertySource, clearCache, useOriginalNonrenderables, additionalTexturePropertyIDsToCopy, additionalOutputTextures, - additionalTextureFormats); + additionalTextureFormats, additionalTextureIsLinear); } /// @@ -344,9 +351,15 @@ namespace Spine.Unity.AttachmentTools { 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, - TextureFormat[] additionalTextureFormats = null) { + TextureFormat[] additionalTextureFormats = null, bool[] additionalTextureIsLinear = null) { outputTexture = null; + if (additionalTexturePropertyIDsToCopy != null && additionalTextureIsLinear == null) { + additionalTextureIsLinear = new bool[additionalTexturePropertyIDsToCopy.Length]; + for (int i = 0; i < additionalTextureIsLinear.Length; ++i) { + additionalTextureIsLinear[i] = true; + } + } if (o == null) throw new System.NullReferenceException("Skin was null"); var skinAttachments = o.Attachments; @@ -388,7 +401,7 @@ namespace Spine.Unity.AttachmentTools { region.ToTexture(textureFormat, mipmaps) : region.ToTexture((additionalTextureFormats != null && i - 1 < additionalTextureFormats.Length) ? additionalTextureFormats[i - 1] : textureFormat, - mipmaps, additionalTexturePropertyIDsToCopy[i - 1])); + mipmaps, additionalTexturePropertyIDsToCopy[i - 1], additionalTextureIsLinear[i - 1])); texturesToPackAtParam[i].Add(regionTexture); // Add the texture to the PackTextures argument } existingRegions.Add(region, newRegionIndex); // Add the region to the dictionary of known regions @@ -415,9 +428,10 @@ namespace Spine.Unity.AttachmentTools { for (int i = 0; i < numTextureParamsToRepack; ++i) { // Fill a new texture with the collected attachment textures. var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize, - (additionalTextureFormats != null && i - 1 < additionalTextureFormats.Length) ? + (i > 0 && additionalTextureFormats != null && i - 1 < additionalTextureFormats.Length) ? additionalTextureFormats[i - 1] : textureFormat, - mipmaps); + mipmaps, + (i > 0) ? additionalTextureIsLinear[i - 1] : false); newTexture.mipMapBias = AtlasUtilities.DefaultMipmapBias; var texturesToPack = texturesToPackAtParam[i]; if (texturesToPack.Count > 0) { @@ -489,7 +503,7 @@ namespace Spine.Unity.AttachmentTools { /// Creates a new Texture2D object based on an AtlasRegion. /// If applyImmediately is true, Texture2D.Apply is called immediately after the Texture2D is filled with data. public static Texture2D ToTexture (this AtlasRegion ar, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, - int texturePropertyId = 0) { + int texturePropertyId = 0, bool linear = false) { Texture2D output; @@ -500,7 +514,7 @@ namespace Spine.Unity.AttachmentTools { Rect r = ar.GetUnityRect(); int width = (int)r.width; int height = (int)r.height; - output = new Texture2D(width, height, textureFormat, mipmaps) { name = ar.name }; + output = new Texture2D(width, height, textureFormat, mipmaps, linear) { name = ar.name }; output.CopyTextureAttributesFrom(sourceTexture); AtlasUtilities.CopyTexture(sourceTexture, r, output); CachedRegionTextures.Add(cacheKey, output); @@ -510,17 +524,21 @@ namespace Spine.Unity.AttachmentTools { return output; } - static Texture2D ToTexture (this Sprite s, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) { + static Texture2D ToTexture (this Sprite s, TextureFormat textureFormat = SpineTextureFormat, + bool mipmaps = UseMipMaps, bool linear = false) { + var spriteTexture = s.texture; var r = s.textureRect; - var newTexture = new Texture2D((int)r.width, (int)r.height, textureFormat, mipmaps); + var newTexture = new Texture2D((int)r.width, (int)r.height, textureFormat, mipmaps, linear); newTexture.CopyTextureAttributesFrom(spriteTexture); AtlasUtilities.CopyTexture(spriteTexture, r, newTexture); return newTexture; } - static Texture2D GetClone (this Texture2D t, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) { - var newTexture = new Texture2D((int)t.width, (int)t.height, textureFormat, mipmaps); + static Texture2D GetClone (this Texture2D t, TextureFormat textureFormat = SpineTextureFormat, + bool mipmaps = UseMipMaps, bool linear = false) { + + var newTexture = new Texture2D((int)t.width, (int)t.height, textureFormat, mipmaps, linear); newTexture.CopyTextureAttributesFrom(t); AtlasUtilities.CopyTexture(t, new Rect(0, 0, t.width, t.height), newTexture); return newTexture;