ActiveTextureManagement.TextureConverter.IMGToTexture C# (CSharp) Method

IMGToTexture() public static method

public static IMGToTexture ( TexInfo Texture, bool mipmaps, bool isNormalFormat ) : void
Texture TexInfo
mipmaps bool
isNormalFormat bool
return void
        public static void IMGToTexture(TexInfo Texture, bool mipmaps, bool isNormalFormat)
        {
            TextureInfoWrapper texture = Texture.texture;
            TextureConverter.InitImageBuffer();
            FileStream imgStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read);
            imgStream.Position = 0;
            imgStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE);
            imgStream.Close();

            Texture2D tex = texture.texture;
            tex.LoadImage(imageBuffer);
            bool convertToNormalFormat = texture.isNormalMap && !isNormalFormat ? true : false;
            bool hasMipmaps = tex.mipmapCount == 1 ? false : true;
            if (Texture.loadOriginalFirst)
            {
                Texture.Resize(tex.width, tex.height);
            }
            TextureFormat format = tex.format;
            if (texture.isNormalMap)
            {
                format = TextureFormat.ARGB32;
            }
            if (Texture.needsResize)
            {
                TextureConverter.Resize(texture, Texture.resizeWidth, Texture.resizeHeight, mipmaps, convertToNormalFormat);
            }
            else if (convertToNormalFormat || hasMipmaps != mipmaps || format != tex.format)
            {
                Color32[] pixels = tex.GetPixels32();
                if (convertToNormalFormat)
                {
                    for (int i = 0; i < pixels.Length; i++)
                    {
                        pixels[i].a = pixels[i].r;
                        pixels[i].r = pixels[i].g;
                        pixels[i].b = pixels[i].g;
                    }
                }
                if (tex.format != format || hasMipmaps != mipmaps)
                {
                    tex.Resize(tex.width, tex.height, format, mipmaps);
                }
                tex.SetPixels32(pixels);
                tex.Apply(mipmaps);
            }
        }

Usage Example

Esempio n. 1
0
        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));
            }
        }