/// <summary> /// /// </summary> /// <param name="sourceStream"></param> /// <param name="targetStream"></param> public override void Process(AssetSource assetFile, BuildContext context) { string tempFileName = context.GetTempFileFullPath(assetFile.KeyPath, ".fnt"); string resolvedPath = assetFile.FullSourcePath; // Launch 'bmfont.com' with temporary output file : context.RunTool(@"bmfont.com", string.Format("-c \"{0}\" -o \"{1}\"", resolvedPath, tempFileName)); // load temporary output : SpriteFont.FontFile font; using (var stream = File.OpenRead(tempFileName)) { font = SpriteFont.FontLoader.Load(stream); } // perform some checks : if (font.Common.Pages != 1) { throw new BuildException("Only one page of font image is supported"); } // patch font description and add children (e.g. "secondary") content : using (var stream = assetFile.OpenTargetStream()) { using (var sw = new BinaryWriter(stream)) { var xml = SpriteFont.FontLoader.SaveToString(font); sw.Write(xml); // write pages : foreach (var p in font.Pages) { var pageFile = Path.Combine(Path.GetDirectoryName(tempFileName), p.File); if (Path.GetExtension(pageFile).ToLower() == ".dds") { context.CopyFileTo(pageFile, sw); } else { TextureProcessor.RunNVCompress(context, pageFile, pageFile + ".dds", true, false, false, true, true, false, TextureProcessor.TextureCompression.RGB); context.CopyFileTo(pageFile + ".dds", sw); } } } } }
/// <summary> /// /// </summary> /// <param name="buildContext"></param> public override void Process(AssetSource assetFile, BuildContext context) { var fileDir = Path.GetDirectoryName(assetFile.FullSourcePath); var fileNames = File.ReadAllLines(assetFile.FullSourcePath) .Select(f1 => f1.Trim()) .Where(f2 => !f2.StartsWith("#") && !string.IsNullOrWhiteSpace(f2)) .Select(f3 => Path.Combine(fileDir, f3)) .ToArray(); var depNames = File.ReadAllLines(assetFile.FullSourcePath) .Select(f1 => f1.Trim()) .Where(f2 => !f2.StartsWith("#") && !string.IsNullOrWhiteSpace(f2)) .Select(f3 => Path.Combine(Path.GetDirectoryName(assetFile.KeyPath), f3)) .ToArray(); var images = fileNames .Select(fn => LoadImage(fn)) .OrderByDescending(img0 => img0.Width * img0.Height) .ThenByDescending(img1 => img1.Width) .ThenByDescending(img2 => img2.Height) .ToList(); if (!images.Any()) { throw new InvalidOperationException("At least one subimage must be added to the texture atlas"); } // // Pack atlas : // AtlasNode root = new AtlasNode(0, 0, Width, Height, Padding); foreach (var img in images) { var n = root.Insert(img); if (n == null) { throw new InvalidOperationException("No enough room to place image"); } } // // Create image and fill it with atlas elements : // var targetImage = new Image(Width, Height); targetImage.Fill(FillColor); root.WriteImages(targetImage); // // Save and compress : // var tgaOutput = context.GetTempFileFullPath(assetFile.KeyPath, ".tga"); var ddsOutput = context.GetTempFileFullPath(assetFile.KeyPath, ".dds"); Image.SaveTga(targetImage, tgaOutput); var compression = UseDXT ? TextureProcessor.TextureCompression.BC3 : TextureProcessor.TextureCompression.RGB; TextureProcessor.RunNVCompress(context, tgaOutput, ddsOutput, NoMips, false, false, true, true, false, compression); // // Write binary blob (text + dds texture): // using (var fs = assetFile.OpenTargetStream(depNames)) { var bw = new BinaryWriter(fs); bw.Write(new[] { 'A', 'T', 'L', 'S' }); bw.Write(images.Count); root.WriteLayout(bw); bw.Write((int)(new FileInfo(ddsOutput).Length)); using (var dds = File.OpenRead(ddsOutput)) { dds.CopyTo(fs); } } }