Exemplo n.º 1
0
        //叠加材质上的颜色
        public static Texture2D ToColorTexture(this AtlasRegion ar, Material material,
                                               TextureFormat textureFormat = TextureFormat.RGBA32, bool mipmaps = false)
        {
            if (material == null || !material.HasProperty(BlendColorVal))
            {
                return(ar.ToTexture(true, textureFormat, mipmaps));
            }

            var blendColor = material.GetColor(BlendColorVal);
            Dictionary <Color, Texture2D> colorDic;

            if (!CachedRegionTextures.TryGetValue(ar, out colorDic))
            {
                colorDic = new Dictionary <Color, Texture2D>();
                CachedRegionTextures.Add(ar, colorDic);
            }

            Texture2D tex2D;

            if (!colorDic.TryGetValue(blendColor, out tex2D))
            {
                var sourceTexture = ar.GetMainTexture();
                var r             = ar.GetUnityRect(sourceTexture.height);
                var width         = (int)r.width;
                var height        = (int)r.height;
                tex2D      = new Texture2D(width, height, textureFormat, mipmaps);
                tex2D.name = ar.name;
                var pixelBuffer = sourceTexture.GetPixels((int)r.x, (int)r.y, width, height);
                for (int i = 0; i < pixelBuffer.Length; i++)
                {
                    pixelBuffer[i] = pixelBuffer[i].Overlay(blendColor);
                }

                tex2D.SetPixels(pixelBuffer);
                tex2D.Apply();
                colorDic.Add(blendColor, tex2D);
            }

            return(tex2D);
        }
Exemplo n.º 2
0
        public static Skin GetRepackedSkin(this Skin o, string newName, Shader shader, out Material outputMaterial, out Texture2D outputTexture, int maxAtlasSize = 0x400, int padding = 2, TextureFormat textureFormat = 4, bool mipmaps = false, Material materialPropertySource = null, bool clearCache = false, bool useOriginalNonrenderables = true)
        {
            if (o == null)
            {
                throw new NullReferenceException("Skin was null");
            }
            Dictionary <Skin.AttachmentKeyTuple, Attachment> attachments = o.Attachments;
            Skin skin = new Skin(newName);
            Dictionary <AtlasRegion, int> dictionary2 = new Dictionary <AtlasRegion, int>();
            List <int>         list  = new List <int>();
            List <Attachment>  list2 = new List <Attachment>();
            List <Texture2D>   list3 = new List <Texture2D>();
            List <AtlasRegion> list4 = new List <AtlasRegion>();
            int num = 0;

            foreach (KeyValuePair <Skin.AttachmentKeyTuple, Attachment> pair in attachments)
            {
                Skin.AttachmentKeyTuple key = pair.Key;
                Attachment a = pair.Value;
                if (IsRenderable(a))
                {
                    Attachment  clone  = a.GetClone(true);
                    AtlasRegion region = clone.GetRegion();
                    if (dictionary2.TryGetValue(region, out int num2))
                    {
                        list.Add(num2);
                    }
                    else
                    {
                        list4.Add(region);
                        list3.Add(region.ToTexture(true, TextureFormat.RGBA32, false));
                        dictionary2.Add(region, num);
                        list.Add(num);
                        num++;
                    }
                    list2.Add(clone);
                    skin.AddAttachment(key.slotIndex, key.name, clone);
                }
                else
                {
                    skin.AddAttachment(key.slotIndex, key.name, !useOriginalNonrenderables ? a.GetClone(true) : a);
                }
            }
            Texture2D textured = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps)
            {
                mipMapBias = -0.5f,
                anisoLevel = list3[0].anisoLevel,
                name       = newName
            };

            Rect[]   rectArray = textured.PackTextures(list3.ToArray(), padding, maxAtlasSize);
            Material m         = new Material(shader);

            if (materialPropertySource != null)
            {
                m.CopyPropertiesFromMaterial(materialPropertySource);
                m.shaderKeywords = materialPropertySource.shaderKeywords;
            }
            m.name        = newName;
            m.mainTexture = textured;
            AtlasPage page = m.ToSpineAtlasPage();

            page.name = newName;
            List <AtlasRegion> list5 = new List <AtlasRegion>();
            int index = 0;
            int count = list4.Count;

            while (index < count)
            {
                AtlasRegion region2 = list4[index];
                AtlasRegion item    = UVRectToAtlasRegion(rectArray[index], region2.name, page, region2.offsetX, region2.offsetY, region2.rotate);
                list5.Add(item);
                index++;
            }
            int num5 = 0;
            int num6 = list2.Count;

            while (num5 < num6)
            {
                Attachment a = list2[num5];
                if (IsRenderable(a))
                {
                    a.SetRegion(list5[list[num5]], true);
                }
                num5++;
            }
            if (clearCache)
            {
                ClearCache();
            }
            outputTexture  = textured;
            outputMaterial = m;
            return(skin);
        }
Exemplo n.º 3
0
        public static void GetRepackedAttachments(List <Attachment> sourceAttachments, List <Attachment> outputAttachments, Material materialPropertySource, out Material outputMaterial, out Texture2D outputTexture, int maxAtlasSize = 0x400, int padding = 2, TextureFormat textureFormat = 4, bool mipmaps = false, string newAssetName = "Repacked Attachments", bool clearCache = false, bool useOriginalNonrenderables = true)
        {
            if (sourceAttachments == null)
            {
                throw new ArgumentNullException("sourceAttachments");
            }
            if (outputAttachments == null)
            {
                throw new ArgumentNullException("outputAttachments");
            }
            Dictionary <AtlasRegion, int> dictionary = new Dictionary <AtlasRegion, int>();
            List <int>         list  = new List <int>();
            List <Texture2D>   list2 = new List <Texture2D>();
            List <AtlasRegion> list3 = new List <AtlasRegion>();

            outputAttachments.Clear();
            outputAttachments.AddRange(sourceAttachments);
            int num   = 0;
            int num2  = 0;
            int count = sourceAttachments.Count;

            while (num2 < count)
            {
                Attachment a = sourceAttachments[num2];
                if (IsRenderable(a))
                {
                    Attachment  clone = a.GetClone(true);
                    AtlasRegion key   = clone.GetRegion();
                    if (dictionary.TryGetValue(key, out int num4))
                    {
                        list.Add(num4);
                    }
                    else
                    {
                        list3.Add(key);
                        list2.Add(key.ToTexture(true, TextureFormat.RGBA32, false));
                        dictionary.Add(key, num);
                        list.Add(num);
                        num++;
                    }
                    outputAttachments[num2] = clone;
                }
                else
                {
                    outputAttachments[num2] = !useOriginalNonrenderables?a.GetClone(true) : a;

                    list.Add(-1);
                }
                num2++;
            }
            Texture2D textured = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps)
            {
                mipMapBias = -0.5f,
                anisoLevel = list2[0].anisoLevel,
                name       = newAssetName
            };

            Rect[]   rectArray = textured.PackTextures(list2.ToArray(), padding, maxAtlasSize);
            Shader   shader    = (materialPropertySource != null) ? materialPropertySource.shader : Shader.Find("Spine/Skeleton");
            Material m         = new Material(shader);

            if (materialPropertySource != null)
            {
                m.CopyPropertiesFromMaterial(materialPropertySource);
                m.shaderKeywords = materialPropertySource.shaderKeywords;
            }
            m.name        = newAssetName;
            m.mainTexture = textured;
            AtlasPage page = m.ToSpineAtlasPage();

            page.name = newAssetName;
            List <AtlasRegion> list4 = new List <AtlasRegion>();
            int index = 0;
            int num6  = list3.Count;

            while (index < num6)
            {
                AtlasRegion region2 = list3[index];
                AtlasRegion item    = UVRectToAtlasRegion(rectArray[index], region2.name, page, region2.offsetX, region2.offsetY, region2.rotate);
                list4.Add(item);
                index++;
            }
            int num7 = 0;
            int num8 = outputAttachments.Count;

            while (num7 < num8)
            {
                Attachment a = outputAttachments[num7];
                if (IsRenderable(a))
                {
                    a.SetRegion(list4[list[num7]], true);
                }
                num7++;
            }
            if (clearCache)
            {
                ClearCache();
            }
            outputTexture  = textured;
            outputMaterial = m;
        }
Exemplo n.º 4
0
        public static Skin GetRepackedSkin(this Skin o, string newName, Shader shader, out Material m, out Texture2D t, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = TextureFormat.RGBA32, bool mipmaps = false, Material materialPropertySource = null)
        {
            Dictionary <Skin.AttachmentKeyTuple, Attachment> attachments = o.Attachments;
            Skin skin = new Skin(newName);
            Dictionary <AtlasRegion, int> dictionary = new Dictionary <AtlasRegion, int>();
            List <int>         list  = new List <int>();
            List <Attachment>  list2 = new List <Attachment>();
            List <Texture2D>   list3 = new List <Texture2D>();
            List <AtlasRegion> list4 = new List <AtlasRegion>();
            int num = 0;

            foreach (KeyValuePair <Skin.AttachmentKeyTuple, Attachment> item2 in attachments)
            {
                Attachment clone = item2.Value.GetClone(cloneMeshesAsLinked: true);
                if (IsRenderable(clone))
                {
                    AtlasRegion atlasRegion = clone.GetAtlasRegion();
                    if (dictionary.TryGetValue(atlasRegion, out int value))
                    {
                        list.Add(value);
                    }
                    else
                    {
                        list4.Add(atlasRegion);
                        list3.Add(atlasRegion.ToTexture());
                        dictionary.Add(atlasRegion, num);
                        list.Add(num);
                        num++;
                    }
                    list2.Add(clone);
                }
                Skin.AttachmentKeyTuple key = item2.Key;
                skin.AddAttachment(key.slotIndex, key.name, clone);
            }
            Texture2D texture2D = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps);

            texture2D.mipMapBias = -0.5f;
            texture2D.anisoLevel = list3[0].anisoLevel;
            texture2D.name       = newName;
            Rect[]   array    = texture2D.PackTextures(list3.ToArray(), padding, maxAtlasSize);
            Material material = new Material(shader);

            if (materialPropertySource != null)
            {
                material.CopyPropertiesFromMaterial(materialPropertySource);
                material.shaderKeywords = materialPropertySource.shaderKeywords;
            }
            material.name        = newName;
            material.mainTexture = texture2D;
            AtlasPage atlasPage = material.ToSpineAtlasPage();

            atlasPage.name = newName;
            List <AtlasRegion> list5 = new List <AtlasRegion>();
            int i = 0;

            for (int count = list4.Count; i < count; i++)
            {
                AtlasRegion atlasRegion2 = list4[i];
                AtlasRegion item         = UVRectToAtlasRegion(array[i], atlasRegion2.name, atlasPage, atlasRegion2.offsetX, atlasRegion2.offsetY, atlasRegion2.rotate);
                list5.Add(item);
            }
            int j = 0;

            for (int count2 = list2.Count; j < count2; j++)
            {
                Attachment attachment = list2[j];
                attachment.SetRegion(list5[list[j]]);
            }
            t = texture2D;
            m = material;
            return(skin);
        }