public static void RunEtcTool(Bitmap bitmap, AssetBundle bundle, string path, AssetAttributes attributes, bool mipMaps, bool highQualityCompression, byte[] CookingRulesSHA1) { var hasAlpha = bitmap.HasAlpha; var bledBitmap = hasAlpha ? TextureConverterUtils.BleedAlpha(bitmap) : null; var ktxPath = Toolbox.GetTempFilePathWithExtension(".ktx"); var pngPath = Path.ChangeExtension(ktxPath, ".png"); try { (bledBitmap ?? bitmap).SaveTo(pngPath); var etcTool = GetToolPath("EtcTool"); var args = $"{pngPath} -format " + (hasAlpha ? "RGBA8" : "ETC1") + " -jobs 8 " + " -effort " + (highQualityCompression ? "60" : "40") + (mipMaps ? " -mipmaps 4" : "") + $" -output {ktxPath}"; if (Process.Start(etcTool, args) != 0) { throw new Lime.Exception($"ETCTool error\nCommand line: {etcTool} {args}\""); } bundle.ImportFile(ktxPath, path, 0, "", attributes, CookingRulesSHA1); } finally { bledBitmap?.Dispose(); DeletePossibleLockedFile(pngPath); DeletePossibleLockedFile(ktxPath); } }
public static void RunEtcTool(Bitmap bitmap, AssetBundle bundle, string path, AssetAttributes attributes, bool mipMaps, bool highQualityCompression, byte[] CookingRulesSHA1, DateTime time) { var hasAlpha = bitmap.HasAlpha; var bledBitmap = hasAlpha ? TextureConverterUtils.BleedAlpha(bitmap) : null; var args = "{0} -format " + (hasAlpha ? "RGBA8" : "RGB8") + " -jobs {1} " + " -effort " + (highQualityCompression ? "60" : "40") + (mipMaps ? " -mipmaps 4" : "") + " -output {2}"; var hashString = GetTextureHashString(bledBitmap ?? bitmap, ".etc", args); var cachePath = AssetCache.Instance.Load(hashString); if (cachePath != null) { bundle.ImportFile(cachePath, path, 0, "", attributes, time, CookingRulesSHA1); return; } var ktxPath = Toolbox.GetTempFilePathWithExtension(".ktx"); var pngPath = Path.ChangeExtension(ktxPath, ".png"); var etcTool = GetToolPath("EtcTool"); try { (bledBitmap ?? bitmap).SaveTo(pngPath); if (Process.Start(etcTool, args.Format(pngPath, 8, ktxPath)) != 0) { throw new Lime.Exception($"ETCTool error\nCommand line: {etcTool} {args}\""); } bundle.ImportFile(ktxPath, path, 0, "", attributes, time, CookingRulesSHA1); AssetCache.Instance.Save(ktxPath, hashString); } finally { bledBitmap?.Dispose(); DeletePossibleLockedFile(pngPath); DeletePossibleLockedFile(ktxPath); } }
public static void RunNVCompress(Bitmap bitmap, AssetBundle bundle, string path, AssetAttributes attributes, DDSFormat format, bool mipMaps, byte[] CookingRulesSHA1, DateTime time) { bool compressed = format == DDSFormat.DXTi; Bitmap bledBitmap = null; if (bitmap.HasAlpha) { bledBitmap = TextureConverterUtils.BleedAlpha(bitmap); } string mipsFlag = mipMaps ? string.Empty : "-nomips"; string compressionMethod; if (compressed) { compressionMethod = bitmap.HasAlpha ? "-bc3" : "-bc1"; } else { #if WIN compressionMethod = "-rgb"; #else compressionMethod = "-rgb -rgbfmt bgra8"; #endif } var nvcompress = GetToolPath("nvcompress"); string args = "{0} {1}".Format(mipsFlag, compressionMethod); var hashString = GetTextureHashString(bledBitmap ?? bitmap, ".dds", args); var cachePath = AssetCache.Instance.Load(hashString); if (cachePath != null) { bundle.ImportFile(cachePath, path, 0, "", attributes, time, CookingRulesSHA1); return; } var ddsPath = Toolbox.GetTempFilePathWithExtension(".dds"); var tgaPath = Path.ChangeExtension(ddsPath, ".tga"); var srcPath = Path.Combine(Directory.GetCurrentDirectory(), tgaPath); var dstPath = Path.Combine(Directory.GetCurrentDirectory(), ddsPath); try { TextureConverterUtils.SaveToTGA(bledBitmap ?? bitmap, tgaPath, swapRedAndBlue: compressed); if (bledBitmap != null && bledBitmap != bitmap) { bledBitmap.Dispose(); } args += $" \"{srcPath}\" \"{dstPath}\""; if (Process.Start(nvcompress, args.Format(mipsFlag, compressionMethod, srcPath, dstPath), options: Process.Options.RedirectErrors) != 0) { throw new Lime.Exception($"NVCompress error\nCommand line: {nvcompress} {args}\""); } bundle.ImportFile(ddsPath, path, 0, "", attributes, time, CookingRulesSHA1); AssetCache.Instance.Save(ddsPath, hashString); } finally { DeletePossibleLockedFile(ddsPath); DeletePossibleLockedFile(tgaPath); } }
public static void RunNVCompress(Bitmap bitmap, AssetBundle bundle, string path, AssetAttributes attributes, DDSFormat format, bool mipMaps, byte[] CookingRulesSHA1) { bool compressed = format == DDSFormat.DXTi; Bitmap bledBitmap = null; if (bitmap.HasAlpha) { bledBitmap = TextureConverterUtils.BleedAlpha(bitmap); } var ddsPath = Toolbox.GetTempFilePathWithExtension(".dds"); var tgaPath = Path.ChangeExtension(ddsPath, ".tga"); try { TextureConverterUtils.SaveToTGA(bledBitmap ?? bitmap, tgaPath, swapRedAndBlue: compressed); if (bledBitmap != null && bledBitmap != bitmap) { bledBitmap.Dispose(); } RunNVCompressHelper(tgaPath, ddsPath, bitmap.HasAlpha, compressed, mipMaps); bundle.ImportFile(ddsPath, path, 0, "", attributes, CookingRulesSHA1); } finally { DeletePossibleLockedFile(ddsPath); DeletePossibleLockedFile(tgaPath); } }
public static void RunPVRTexTool(Bitmap bitmap, AssetBundle bundle, string path, AssetAttributes attributes, bool mipMaps, bool highQualityCompression, PVRFormat pvrFormat, byte[] CookingRulesSHA1) { int width = bitmap.Width; int height = bitmap.Height; bool hasAlpha = bitmap.HasAlpha; int potWidth = TextureConverterUtils.GetNearestPowerOf2(width, 8, 2048); int potHeight = TextureConverterUtils.GetNearestPowerOf2(height, 8, 2048); var args = new StringBuilder(); switch (pvrFormat) { case PVRFormat.PVRTC4: if (!hasAlpha) { args.Append(" -f PVRTC1_2"); } else { args.Append(" -f PVRTC1_4"); } width = height = Math.Max(potWidth, potHeight); break; case PVRFormat.PVRTC4_Forced: args.Append(" -f PVRTC1_4"); width = height = Math.Max(potWidth, potHeight); break; case PVRFormat.PVRTC2: args.Append(" -f PVRTC1_2"); width = height = Math.Max(potWidth, potHeight); break; case PVRFormat.ETC2: args.Append(" -f ETC1 -q etcfast"); break; case PVRFormat.RGB565: if (hasAlpha) { Console.WriteLine("WARNING: texture has alpha channel. " + "Used 'RGBA4444' format instead of 'RGB565'."); args.Append(" -f r4g4b4a4 -dither"); } else { args.Append(" -f r5g6b5"); } break; case PVRFormat.RGBA4: args.Append(" -f r4g4b4a4 -dither"); break; case PVRFormat.ARGB8: args.Append(" -f r8g8b8a8"); break; } if (highQualityCompression && (new [] { PVRFormat.PVRTC2, PVRFormat.PVRTC4, PVRFormat.PVRTC4_Forced }.Contains (pvrFormat))) { args.Append(" -q pvrtcbest"); } var pvrPath = Toolbox.GetTempFilePathWithExtension(".pvr"); var tgaPath = Path.ChangeExtension(pvrPath, ".tga"); try { if (hasAlpha) { bitmap = TextureConverterUtils.BleedAlpha(bitmap); } TextureConverterUtils.SaveToTGA(bitmap, tgaPath, swapRedAndBlue: true); if (mipMaps) { args.Append(" -m"); } args.AppendFormat(" -i \"{0}\" -o \"{1}\" -r {2},{3} -shh", tgaPath, pvrPath, width, height); #if MAC var pvrTexTool = GetToolPath("PVRTexTool"); #else var pvrTexTool = GetToolPath("PVRTexToolCli"); #endif if (Process.Start(pvrTexTool, args.ToString()) != 0) { throw new Lime.Exception($"PVRTextTool error\nCommand line: {pvrTexTool} {args}\""); } bundle.ImportFile(pvrPath, path, 0, "", attributes, CookingRulesSHA1); } finally { DeletePossibleLockedFile(tgaPath); DeletePossibleLockedFile(pvrPath); } }