CreateDistanceFieldTexture() public static method

public static CreateDistanceFieldTexture ( Texture2D inputTexture, TextureChannel channel, int outSize ) : Texture2D
inputTexture UnityEngine.Texture2D
channel TextureChannel
outSize int
return UnityEngine.Texture2D
    static void Import()
    {
        foreach (Object o in Selection.GetFiltered(typeof(Font), SelectionMode.DeepAssets))
        {
            string path     = AssetDatabase.GetAssetPath(o.GetInstanceID());
            string basePath = path.Substring(0, path.LastIndexOf("."));

            Font      fnt = (Font)o;
            Texture2D tex = (Texture2D)fnt.material.mainTexture;

            //Create distance field from texture
            Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(tex, InputTextureChannel, tex.width / DistanceFieldScaleFactor);
            //Save distance field as png
            byte[] pngData    = distanceField.EncodeToPNG();
            string outputPath = basePath + "_dist.png";
            System.IO.File.WriteAllBytes(outputPath, pngData);
            AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

            //Set correct texture format
            TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath);
            texImp.textureType   = TextureImporterType.Advanced;
            texImp.isReadable    = true;
            texImp.textureFormat = TextureImporterFormat.Alpha8;
            AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

            Material mat = new Material(Shader.Find("BitmapFont/Outline"));
            mat.mainTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(outputPath, typeof(Texture2D));
            AssetDatabase.CreateAsset(mat, basePath + "_dist.mat");
        }
    }
    static void Generate(string path)
    {
        BMFont.IntFontInfo fntInfo = BMFont.ParseFromPath(path);
        if (fntInfo == null)
        {
            return;
        }

        //Process Texture
        string    imagePath    = System.IO.Path.GetDirectoryName(path) + "/" + fntInfo.texName;
        Texture2D inputTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D));

        //Make sure font texture is readable
        TextureImporter inputTextureImp = (TextureImporter)TextureImporter.GetAtPath(imagePath);

        inputTextureImp.textureType    = TextureImporterType.Advanced;
        inputTextureImp.isReadable     = true;
        inputTextureImp.maxTextureSize = 4096;
        AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceSynchronousImport);

        Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, InputTextureChannel, inputTexture.width / DistanceFieldScaleFactor);

        //Save distance field as png
        byte[] pngData    = distanceField.EncodeToPNG();
        string outputPath = imagePath.Substring(0, imagePath.LastIndexOf('.')) + "_dist.png";

        System.IO.File.WriteAllBytes(outputPath, pngData);
        AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

        //Set correct texture format
        TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath);

        texImp.textureType   = TextureImporterType.GUI;
        texImp.isReadable    = true;
        texImp.textureFormat = TextureImporterFormat.Alpha8;
        AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

        string newTexName = fntInfo.texName.Substring(0, fntInfo.texName.LastIndexOf(".")) + "_dist.png";

        fntInfo.texName     = newTexName;
        fntInfo.scaleW     /= DistanceFieldScaleFactor;
        fntInfo.scaleH     /= DistanceFieldScaleFactor;
        fntInfo.lineHeight /= DistanceFieldScaleFactor;

        foreach (BMFont.IntChar c in fntInfo.chars)
        {
            c.x        /= DistanceFieldScaleFactor;
            c.y        /= DistanceFieldScaleFactor;
            c.width    /= DistanceFieldScaleFactor;
            c.height   /= DistanceFieldScaleFactor;
            c.xoffset  /= DistanceFieldScaleFactor;
            c.yoffset  /= DistanceFieldScaleFactor;
            c.xadvance /= DistanceFieldScaleFactor;
        }

        outputPath = path.Substring(0, path.LastIndexOf('.')) + "_dist.fnt";
        BMFont.SaveToXML(fntInfo, outputPath);
    }
Exemplo n.º 3
0
    /* .fnt parser taken from
     *
     */
    static BitmapFont UpdateBitmapFont(string path, BitmapFont fnt)
    {
        XmlDocument doc = new XmlDocument();

        doc.Load(path);

        //Read font info
        XmlNode info = doc.SelectSingleNode("/font/info");

        fnt.Size = ReadFloatAttribute(info, "size");

        XmlNode common = doc.SelectSingleNode("/font/common");

        fnt.LineHeight = ReadFloatAttribute(common, "lineHeight");
        fnt.Base       = ReadFloatAttribute(common, "base");
        fnt.ScaleH     = ReadFloatAttribute(common, "scaleW");
        fnt.ScaleW     = ReadFloatAttribute(common, "scaleH");

        //Read character info
        XmlNodeList chars = doc.SelectNodes("/font/chars/char");

        fnt.Chars = new BitmapChar[chars.Count];
        int index = 0;

        foreach (XmlNode char_node in chars)
        {
            fnt.Chars[index]          = new BitmapChar();
            fnt.Chars[index].Id       = ReadIntAttribute(char_node, "id");
            fnt.Chars[index].Position = ReadVector2Attributes(char_node, "x", "y");
            fnt.Chars[index].Size     = ReadVector2Attributes(char_node, "width", "height");
            fnt.Chars[index].Offset   = ReadVector2Attributes(char_node, "xoffset", "yoffset");
            fnt.Chars[index].XAdvance = ReadFloatAttribute(char_node, "xadvance");
            fnt.Chars[index].Page     = ReadIntAttribute(char_node, "page");
            index++;
        }

        //Load texture pages and convert to distance fields
        XmlNodeList pages = doc.SelectNodes("/font/pages/page");

        Texture2D[] texturePages = new Texture2D[pages.Count];
        index = 0;
        foreach (XmlNode page in pages)
        {
            //Find original font texture
            string    imagePath    = System.IO.Path.GetDirectoryName(path) + "/" + page.Attributes["file"].Value;
            Texture2D inputTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D));

            //Make sure font texture is readable
            TextureImporter inputTextureImp = (TextureImporter)TextureImporter.GetAtPath(imagePath);
            inputTextureImp.textureType    = TextureImporterType.Advanced;
            inputTextureImp.isReadable     = true;
            inputTextureImp.maxTextureSize = 4096;
            AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceSynchronousImport);

            //Create distance field from texture
            Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, InputTextureChannel, inputTexture.width / DistanceFieldScaleFactor);
            texturePages[index] = distanceField;

            index++;
        }

        //Create texture atlas
        Texture2D pageAtlas = new Texture2D(0, 0);

        fnt.PageOffsets = pageAtlas.PackTextures(texturePages, 0);

        //Save atlas as png
        byte[] pngData    = pageAtlas.EncodeToPNG();
        string outputPath = path.Substring(0, path.LastIndexOf('.')) + "_dist.png";

        System.IO.File.WriteAllBytes(outputPath, pngData);
        AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

        //Set correct texture format
        TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath);

        texImp.textureType   = TextureImporterType.Advanced;
        texImp.isReadable    = true;
        texImp.textureFormat = TextureImporterFormat.Alpha8;
        AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

        //Load the saved texture atlas
        fnt.PageAtlas = (Texture2D)AssetDatabase.LoadAssetAtPath(outputPath, typeof(Texture2D));

        //Load kerning info
        XmlNodeList kernings = doc.SelectNodes("/font/kernings/kerning");

        fnt.Kernings = new BitmapCharKerning[kernings.Count];
        index        = 0;
        foreach (XmlNode kerning in kernings)
        {
            BitmapCharKerning krn = new BitmapCharKerning();
            krn.FirstChar       = ReadIntAttribute(kerning, "first");
            krn.SecondChar      = ReadIntAttribute(kerning, "second");
            krn.Amount          = ReadFloatAttribute(kerning, "amount");
            fnt.Kernings[index] = krn;
            index++;
        }

        return(fnt);
    }
Exemplo n.º 4
0
        public void ParsePages(BitmapFont fnt, string fontFile)
        {
            string[]    pages        = fnt.pages;
            Texture2D[] texturePages = new Texture2D[pages.Length * (fnt.packed?4:1)];
            int         index        = 0;
            //用在带通道的字体上,提取通道值后的颜色应该是白色的
            Color defaultColor = Color.white;

            foreach (string pageImagePath in pages)
            {
                //Find original font texture
                string    imagePath    = Path.GetDirectoryName(fontFile) + "/" + pageImagePath;
                Texture2D inputTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D));
                //Make sure font texture is readable
                TextureImporter inputTextureImp = (TextureImporter)AssetImporter.GetAtPath(imagePath);
                inputTextureImp.textureType    = TextureImporterType.Default;
                inputTextureImp.isReadable     = true;
                inputTextureImp.maxTextureSize = 4096;
                inputTextureImp.mipmapEnabled  = false;
                inputTextureImp.textureFormat  = TextureImporterFormat.RGBA32;

                AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceSynchronousImport);

                //Create distance field from texture
                //处理通道
                if (fnt.packed)
                {
                    Texture2D distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, DistanceField.TextureChannel.BLUE, inputTexture.width, defaultColor);
                    //byte[] buff = distanceField.EncodeToPNG();
                    //File.WriteAllBytes(Path.GetDirectoryName(fontFile) + "/index_" + index + ".png", buff);
                    texturePages[index++] = distanceField;

                    distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, DistanceField.TextureChannel.GREEN, inputTexture.width, defaultColor);
                    //buff = distanceField.EncodeToPNG();
                    //File.WriteAllBytes(Path.GetDirectoryName(fontFile) + "/index_" + index + ".png", buff);
                    texturePages[index++] = distanceField;

                    distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, DistanceField.TextureChannel.RED, inputTexture.width, defaultColor);
                    //buff = distanceField.EncodeToPNG();
                    //File.WriteAllBytes(Path.GetDirectoryName(fontFile) + "/index_" + index + ".png", buff);
                    texturePages[index++] = distanceField;

                    distanceField = DistanceField.CreateDistanceFieldTexture(inputTexture, DistanceField.TextureChannel.ALPHA, inputTexture.width, defaultColor);
                    //buff = distanceField.EncodeToPNG();
                    //File.WriteAllBytes(Path.GetDirectoryName(fontFile) + "/index_" + index + ".png", buff);
                    texturePages[index] = distanceField;
                }
                else
                {
                    Texture2D distanceField = inputTexture; //DistanceField.CreateDistanceFieldTexture(inputTexture, InputTextureChannel, inputTexture.width / DistanceFieldScaleFactor);
                    texturePages[index] = distanceField;
                }

                index++;
            }

            //Create texture atlas
            if (texturePages.Length > 1)
            {
                Texture2D pageAtlas = new Texture2D(0, 0);
                fnt.pageOffsets = pageAtlas.PackTextures(texturePages, 0);

                //foreach(Rect r in fnt.pageOffsets)
                //{
                //    Debug.Log(r);
                //}

                //Save atlas as png
                byte[] pngData    = pageAtlas.EncodeToPNG();
                string outputPath = fontFile.Substring(0, fontFile.LastIndexOf('.')) + "_pak.png";
                File.WriteAllBytes(outputPath, pngData);
                AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

                //Set correct texture format
                TextureImporter texImp = (TextureImporter)TextureImporter.GetAtPath(outputPath);
                texImp.textureType   = TextureImporterType.Default;
                texImp.isReadable    = true;
                texImp.textureFormat = TextureImporterFormat.RGBA32;
                texImp.mipmapEnabled = false;
                AssetDatabase.ImportAsset(outputPath, ImportAssetOptions.ForceSynchronousImport);

                //Load the saved texture atlas
                fnt.pageAtlas = AssetDatabase.LoadAssetAtPath <Texture2D>(outputPath);
            }
            else
            {
                fnt.pageAtlas = texturePages[0];
            }
        }