ActiveTextureManagement.TextureConverter.DDSToTexture C# (CSharp) Method

DDSToTexture() public static method

public static DDSToTexture ( UrlDir file, TexInfo Texture, bool mipmaps, bool isCompressed, bool hasAlpha ) : TextureInfoWrapper
file UrlDir
Texture TexInfo
mipmaps bool
isCompressed bool
hasAlpha bool
return TextureInfoWrapper
        public static TextureInfoWrapper DDSToTexture(UrlDir.UrlFile file, TexInfo Texture, bool mipmaps, bool isCompressed, bool hasAlpha)
        {
            TextureConverter.InitImageBuffer();
            FileStream imgStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read);
            imgStream.Position = 0;
            imgStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE);
            imgStream.Close();

            TextureFormat format = TextureFormat.DXT1;
            if(hasAlpha && isCompressed)
            {
                format = TextureFormat.DXT5;
            }
            else if(hasAlpha)
            {
                format = TextureFormat.RGBA32;
            }
            else if(!isCompressed)
            {
                format = TextureFormat.RGB24;
            }

            Texture2D newTex = new Texture2D(Texture.width, Texture.height, format, mipmaps);

            newTex.LoadRawTextureData(imageBuffer);
            newTex.Apply(false, Texture.makeNotReadable);
            newTex.name = Texture.name;

            TextureInfoWrapper newTexInfo = new TextureInfoWrapper(file, newTex, Texture.isNormalMap, !Texture.makeNotReadable, isCompressed);
            newTexInfo.name = Texture.name;
            return newTexInfo;
        }

Usage Example

        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));
            }
        }