public static void Resize(GameDatabase.TextureInfo texture, int width, int height, bool mipmaps, bool convertToNormalFormat) { ActiveTextureManagement.DBGLog("Resizing..."); Texture2D tex = texture.texture; TextureFormat format = tex.format; if (texture.isNormalMap) { format = TextureFormat.ARGB32; } else if (format == TextureFormat.DXT1 || format == TextureFormat.RGB24) { format = TextureFormat.RGB24; } else { format = TextureFormat.RGBA32; } Color32[] pixels = tex.GetPixels32(); if (convertToNormalFormat) { ConvertToUnityNormalMap(pixels); } Color32[] newPixels = ResizePixels(pixels, tex.width, tex.height, width, height); tex.Resize(width, height, format, mipmaps); tex.SetPixels32(newPixels); tex.Apply(mipmaps); }
public TexInfo(string name) { this.name = name; this.isNormalMap = ActiveTextureManagement.IsNormal(name); this.width = 1; this.height = 1; loadOriginalFirst = false; needsResize = false; }
private static TextureInfoWrapper RebuildCache(TexInfo Texture, bool compress, bool mipmaps) { Texture.loadOriginalFirst = true; ActiveTextureManagement.DBGLog("Loading texture..."); TextureConverter.GetReadable(Texture, mipmaps); ActiveTextureManagement.DBGLog("Texture loaded."); TextureInfoWrapper cacheTexture = Texture.texture; Texture2D tex = cacheTexture.texture; String textureName = cacheTexture.name; String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName; ActiveTextureManagement.DBGLog("Rebuilding Cache... " + Texture.name); ActiveTextureManagement.DBGLog("Saving cache file " + cacheFile + ".imgcache"); tex.Apply(mipmaps); Color32[] colors = tex.GetPixels32(); bool hasAlpha = TextureConverter.WriteTo(tex, cacheFile + ".imgcache", compress); String originalTextureFile = Texture.filename; String cacheConfigFile = cacheFile + ".tcache"; ActiveTextureManagement.DBGLog("Created Config for" + originalTextureFile); String hashString = GetMD5String(originalTextureFile); ConfigNode config = new ConfigNode(); config.AddValue("md5", hashString); ActiveTextureManagement.DBGLog("md5: " + hashString); config.AddValue("orig_format", Path.GetExtension(originalTextureFile)); ActiveTextureManagement.DBGLog("orig_format: " + Path.GetExtension(originalTextureFile)); config.AddValue("orig_width", Texture.width.ToString()); ActiveTextureManagement.DBGLog("orig_width: " + Texture.width.ToString()); config.AddValue("orig_height", Texture.height.ToString()); ActiveTextureManagement.DBGLog("orig_height: " + Texture.height.ToString()); config.AddValue("is_normal", cacheTexture.isNormalMap.ToString()); ActiveTextureManagement.DBGLog("is_normal: " + cacheTexture.isNormalMap.ToString()); config.AddValue("is_compressed", compress); ActiveTextureManagement.DBGLog("is_compressed: " + compress); config.AddValue("width", Texture.resizeWidth.ToString()); ActiveTextureManagement.DBGLog("width: " + Texture.resizeWidth.ToString()); config.AddValue("height", Texture.resizeHeight.ToString()); ActiveTextureManagement.DBGLog("height: " + Texture.resizeHeight.ToString()); config.AddValue("hasAlpha", hasAlpha); ActiveTextureManagement.DBGLog("hasAlpha: " + hasAlpha.ToString()); config.AddValue("hasMipmaps", mipmaps); ActiveTextureManagement.DBGLog("hasMipmaps: " + hasAlpha.ToString()); config.Save(cacheConfigFile); ActiveTextureManagement.DBGLog("Saved Config."); if (compress) { tex.Compress(true); } cacheTexture.isCompressed = compress; tex.Apply(false, Texture.makeNotReadable); cacheTexture.isReadable = !Texture.makeNotReadable; return(cacheTexture); }
protected void Start() { if (HighLogic.LoadedScene == GameScenes.LOADING) { DatabaseLoaderTexture_ATM.PopulateConfig(); SetupLoaders(); } else if (HighLogic.LoadedScene == GameScenes.MAINMENU && !Compressed) { Update(); Compressed = true; foreach (GameDatabase.TextureInfo Texture in GameDatabase.Instance.databaseTexture) { Texture2D texture = Texture.texture; Log("--------------------------------------------------------"); Log("Name: " + texture.name); Log("Format: " + texture.format.ToString()); Log("MipMaps: " + texture.mipmapCount.ToString()); Log("Size: " + texture.width.ToString() + "x" + texture.height); Log("Readable: " + Texture.isReadable); bool readable = true; try { Texture.texture.GetPixel(0, 0); } catch { readable = false; }; if (readable != Texture.isReadable) { ActiveTextureManagement.DBGLog("Readbility does not match!"); } } long bSaved = memorySaved; long kbSaved = (long)(bSaved / 1024f); long mbSaved = (long)(kbSaved / 1024f); Log("Memory Saved : " + bSaved.ToString() + "B"); Log("Memory Saved : " + kbSaved.ToString() + "kB"); Log("Memory Saved : " + mbSaved.ToString() + "MB"); TextureConverter.DestroyImageBuffer(); Resources.UnloadUnusedAssets(); System.GC.Collect(); } }
public static void TGAToTexture(TexInfo Texture, bool mipmaps) { GameDatabase.TextureInfo texture = Texture.texture; TextureConverter.InitImageBuffer(); FileStream tgaStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read); tgaStream.Position = 0; tgaStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE); tgaStream.Close(); byte imgType = imageBuffer[2]; int width = imageBuffer[12] | (imageBuffer[13] << 8); int height = imageBuffer[14] | (imageBuffer[15] << 8); if (Texture.loadOriginalFirst) { Texture.Resize(width, height); } int depth = imageBuffer[16]; bool alpha = depth == 32 ? true : false; TextureFormat texFormat = depth == 32 ? TextureFormat.RGBA32 : TextureFormat.RGB24; if (texture.isNormalMap) { texFormat = TextureFormat.ARGB32; } bool convertToNormalFormat = texture.isNormalMap ? true : false; Texture2D tex = texture.texture; Color32[] colors = new Color32[width * height]; int n = 18; if (imgType == 2) { for (int i = 0; i < width * height; i++) { colors[i].b = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].r = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } } } else if (imgType == 10) { int i = 0; int run = 0; while (i < width * height) { run = imageBuffer[n++]; if ((run & 0x80) != 0) { run = (run ^ 0x80) + 1; colors[i].b = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].r = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } i++; for (int c = 1; c < run; c++, i++) { colors[i] = colors[i - 1]; } } else { run += 1; for (int c = 0; c < run; c++, i++) { colors[i].b = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].r = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } } } } } else { ActiveTextureManagement.DBGLog("TGA format is not supported!"); } if (Texture.needsResize) { colors = TextureConverter.ResizePixels(colors, width, height, Texture.resizeWidth, Texture.resizeHeight); width = Texture.resizeWidth; height = Texture.resizeHeight; } tex.Resize((int)width, (int)height, texFormat, mipmaps); tex.SetPixels32(colors); tex.Apply(mipmaps, false); }
public static int MemorySaved(int originalWidth, int originalHeight, TextureFormat originalFormat, bool originalMipmaps, GameDatabase.TextureInfo Texture) { int width = Texture.texture.width; int height = Texture.texture.height; TextureFormat format = Texture.texture.format; bool mipmaps = Texture.texture.mipmapCount == 1 ? false : true; ActiveTextureManagement.DBGLog("Texture: " + Texture.name); ActiveTextureManagement.DBGLog("is normalmap: " + Texture.isNormalMap); Texture2D tex = Texture.texture; ActiveTextureManagement.DBGLog("originalWidth: " + originalWidth); ActiveTextureManagement.DBGLog("originalHeight: " + originalHeight); ActiveTextureManagement.DBGLog("originalFormat: " + originalFormat); ActiveTextureManagement.DBGLog("originalMipmaps: " + originalMipmaps); ActiveTextureManagement.DBGLog("width: " + width); ActiveTextureManagement.DBGLog("height: " + height); ActiveTextureManagement.DBGLog("format: " + format); ActiveTextureManagement.DBGLog("mipmaps: " + mipmaps); bool readable = true; try { tex.GetPixel(0, 0); } catch { readable = false; }; ActiveTextureManagement.DBGLog("readable: " + readable); if (readable != Texture.isReadable) { ActiveTextureManagement.DBGLog("Readbility does not match!"); } int oldSize = 0; int newSize = 0; switch (originalFormat) { case TextureFormat.ARGB32: case TextureFormat.RGBA32: case TextureFormat.BGRA32: oldSize = 4 * (originalWidth * originalHeight); break; case TextureFormat.RGB24: oldSize = 3 * (originalWidth * originalHeight); break; case TextureFormat.Alpha8: oldSize = originalWidth * originalHeight; break; case TextureFormat.DXT1: oldSize = (originalWidth * originalHeight) / 2; break; case TextureFormat.DXT5: oldSize = originalWidth * originalHeight; break; } switch (format) { case TextureFormat.ARGB32: case TextureFormat.RGBA32: case TextureFormat.BGRA32: newSize = 4 * (width * height); break; case TextureFormat.RGB24: newSize = 3 * (width * height); break; case TextureFormat.Alpha8: newSize = width * height; break; case TextureFormat.DXT1: newSize = (width * height) / 2; break; case TextureFormat.DXT5: newSize = width * height; break; } if (originalMipmaps) { oldSize += (int)(oldSize * .33f); } if (mipmaps) { newSize += (int)(newSize * .33f); } return(oldSize - newSize); }
public static TextureInfoWrapper FetchCacheTexture(TexInfo Texture, bool compress, bool mipmaps) { String textureName = Texture.name; String originalTextureFile = KSPUtil.ApplicationRootPath + "GameData/" + textureName; String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName; String cacheConfigFile = cacheFile + ".tcache"; cacheFile += ".imgcache"; String hashString = GetMD5String(originalTextureFile); if (TextureHashTable.ContainsKey(hashString)) { ActiveTextureManagement.DBGLog("hash triggered... " + textureName); TextureInfoWrapper tex = TextureHashTable[hashString]; if (tex.name != textureName) { TextureInfoWrapper cacheTexInfo = new TextureInfoWrapper(Texture.file, tex.texture, tex.isNormalMap, tex.isReadable, tex.isCompressed); cacheTexInfo.name = textureName; ActiveTextureManagement.DBGLog("Re-using from hash dictionary... " + textureName + " is a duplicate of " + tex.name); return(cacheTexInfo); } } if (File.Exists(cacheConfigFile)) { ConfigNode config = ConfigNode.Load(cacheConfigFile); string format = config.GetValue("orig_format"); String cacheHash = config.GetValue("md5"); int origWidth, origHeight; string origWidthString = config.GetValue("orig_width"); string origHeightString = config.GetValue("orig_height"); int.TryParse(origWidthString, out origWidth); int.TryParse(origHeightString, out origHeight); if (origWidthString == null || origHeightString == null || cacheHash == null || format == null) { return(RebuildCache(Texture, compress, mipmaps, hashString)); } originalTextureFile += format; Texture.Resize(origWidth, origHeight); if (format != null && File.Exists(originalTextureFile) && File.Exists(cacheFile)) { String cacheIsNormString = config.GetValue("is_normal"); String cacheIsCompressed = config.GetValue("is_compressed"); String cacheWidthString = config.GetValue("width"); String cacheHeihtString = config.GetValue("height"); string hasAlphaString = config.GetValue("hasAlpha"); string hasMipmapsString = config.GetValue("hasMipmaps"); bool cacheIsNorm = false; int cacheWidth = 0; int cacheHeight = 0; bool hasAlpha = true; bool hasMipmaps = true; bool isCompressed = true; bool.TryParse(cacheIsNormString, out cacheIsNorm); bool.TryParse(cacheIsCompressed, out isCompressed); int.TryParse(cacheWidthString, out cacheWidth); int.TryParse(cacheHeihtString, out cacheHeight); bool.TryParse(hasAlphaString, out hasAlpha); bool.TryParse(hasMipmapsString, out hasMipmaps); if (cacheHash != hashString || compress != isCompressed || mipmaps != hasMipmaps || cacheIsNorm != Texture.isNormalMap || Texture.resizeWidth != cacheWidth || Texture.resizeHeight != cacheHeight) { if (cacheHash != hashString) { ActiveTextureManagement.DBGLog(cacheHash + " != " + hashString); } if (cacheIsNorm != Texture.isNormalMap) { ActiveTextureManagement.DBGLog(cacheIsNorm + " != " + Texture.isNormalMap); } if (Texture.resizeWidth != cacheWidth) { ActiveTextureManagement.DBGLog(Texture.resizeWidth + " != " + cacheWidth); } if (Texture.resizeHeight != cacheHeight) { ActiveTextureManagement.DBGLog(Texture.resizeHeight + " != " + cacheHeight); } return(RebuildCache(Texture, compress, mipmaps, hashString)); } else { ActiveTextureManagement.DBGLog("Loading from cache... " + textureName); Texture.needsResize = false; Texture.width = Texture.resizeWidth; Texture.height = Texture.resizeHeight; Texture.filename = cacheFile; TextureInfoWrapper tex = TextureConverter.DDSToTexture(Texture.file, Texture, hasMipmaps, isCompressed, hasAlpha); if (TextureHashTable.ContainsKey(hashString)) { TextureHashTable[hashString] = tex; } else { TextureHashTable.Add(hashString, tex); } return(tex); } } else { return(RebuildCache(Texture, compress, mipmaps, hashString)); } } else { return(RebuildCache(Texture, compress, mipmaps, hashString)); } }
public static GameDatabase.TextureInfo FetchCacheTexture(TexInfo Texture, bool compress, bool mipmaps, bool makeNotReadable) { String textureName = Texture.name; String originalTextureFile = KSPUtil.ApplicationRootPath + "GameData/" + textureName; String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName; String cacheConfigFile = cacheFile + ".tcache"; cacheFile += ".pngcache"; if (File.Exists(cacheConfigFile)) { ConfigNode config = ConfigNode.Load(cacheConfigFile); string format = config.GetValue("orig_format"); String cacheHash = config.GetValue("md5"); int origWidth, origHeight; string origWidthString = config.GetValue("orig_width"); string origHeightString = config.GetValue("orig_height"); int.TryParse(origWidthString, out origWidth); int.TryParse(origHeightString, out origHeight); if (origWidthString == null || origHeightString == null || cacheHash == null || format == null) { return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } originalTextureFile += format; String hashString = GetMD5String(originalTextureFile); Texture.Resize(origWidth, origHeight); if (format != null && File.Exists(originalTextureFile) && File.Exists(cacheFile)) { String cacheIsNormString = config.GetValue("is_normal"); String cacheWidthString = config.GetValue("width"); String cacheHeihtString = config.GetValue("height"); bool cacheIsNorm = false; int cacheWidth = 0; int cacheHeight = 0; bool.TryParse(cacheIsNormString, out cacheIsNorm); int.TryParse(cacheWidthString, out cacheWidth); int.TryParse(cacheHeihtString, out cacheHeight); if (cacheHash != hashString || cacheIsNorm != Texture.isNormalMap || Texture.resizeWidth != cacheWidth || Texture.resizeHeight != cacheHeight) { if (cacheHash != hashString) { ActiveTextureManagement.DBGLog(cacheHash + " != " + hashString); } if (cacheIsNorm != Texture.isNormalMap) { ActiveTextureManagement.DBGLog(cacheIsNorm + " != " + Texture.isNormalMap); } if (Texture.resizeWidth != cacheWidth) { ActiveTextureManagement.DBGLog(Texture.resizeWidth + " != " + cacheWidth); } if (Texture.resizeHeight != cacheHeight) { ActiveTextureManagement.DBGLog(Texture.resizeHeight + " != " + cacheHeight); } return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } else if (cacheHash == hashString && !Texture.needsResize) { return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } else { ActiveTextureManagement.DBGLog("Loading from cache... " + textureName); Texture.needsResize = false; Texture2D newTex = new Texture2D(4, 4); GameDatabase.TextureInfo cacheTexture = new GameDatabase.TextureInfo(newTex, Texture.isNormalMap, !makeNotReadable, compress); Texture.texture = cacheTexture; Texture.filename = cacheFile; TextureConverter.IMGToTexture(Texture, mipmaps, cacheIsNorm); cacheTexture.name = textureName; newTex.name = textureName; if (compress) { newTex.Compress(true); } newTex.Apply(mipmaps, makeNotReadable); return(cacheTexture); } } else { return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } } else { return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } }
private static GameDatabase.TextureInfo RebuildCache(TexInfo Texture, bool compress, bool mipmaps, bool makeNotReadable) { Texture.loadOriginalFirst = true; ActiveTextureManagement.DBGLog("Loading texture..."); TextureConverter.GetReadable(Texture, mipmaps); ActiveTextureManagement.DBGLog("Texture loaded."); GameDatabase.TextureInfo cacheTexture = Texture.texture; Texture2D tex = cacheTexture.texture; String textureName = cacheTexture.name; String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName; if (Texture.needsResize) { ActiveTextureManagement.DBGLog("Rebuilding Cache... " + Texture.name); ActiveTextureManagement.DBGLog("Saving cache file " + cacheFile + ".pngcache"); TextureConverter.WriteTo(cacheTexture.texture, cacheFile + ".pngcache"); String originalTextureFile = Texture.filename; String cacheConfigFile = cacheFile + ".tcache"; ActiveTextureManagement.DBGLog("Created Config for" + originalTextureFile); String hashString = GetMD5String(originalTextureFile); ConfigNode config = new ConfigNode(); config.AddValue("md5", hashString); ActiveTextureManagement.DBGLog("md5: " + hashString); config.AddValue("orig_format", Path.GetExtension(originalTextureFile)); ActiveTextureManagement.DBGLog("orig_format: " + Path.GetExtension(originalTextureFile)); config.AddValue("orig_width", Texture.width.ToString()); ActiveTextureManagement.DBGLog("orig_width: " + Texture.width.ToString()); config.AddValue("orig_height", Texture.height.ToString()); ActiveTextureManagement.DBGLog("orig_height: " + Texture.height.ToString()); config.AddValue("is_normal", cacheTexture.isNormalMap.ToString()); ActiveTextureManagement.DBGLog("is_normal: " + cacheTexture.isNormalMap.ToString()); config.AddValue("width", Texture.resizeWidth.ToString()); ActiveTextureManagement.DBGLog("width: " + Texture.resizeWidth.ToString()); config.AddValue("height", Texture.resizeHeight.ToString()); ActiveTextureManagement.DBGLog("height: " + Texture.resizeHeight.ToString()); config.Save(cacheConfigFile); ActiveTextureManagement.DBGLog("Saved Config."); } else { String directory = Path.GetDirectoryName(cacheFile + ".none"); if (File.Exists(directory)) { File.Delete(directory); } Directory.CreateDirectory(directory); } if (compress) { tex.Compress(true); } cacheTexture.isCompressed = compress; if (!makeNotReadable) { tex.Apply(mipmaps); } else { tex.Apply(mipmaps, true); } cacheTexture.isReadable = !makeNotReadable; return(cacheTexture); }