Inheritance: GameDatabase.TextureInfo
        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;
        }
Esempio n. 2
0
        public static void Resize(TextureInfoWrapper 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);
        }
Esempio n. 3
0
        public static TextureInfoWrapper DDSToTexture(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(newTex, Texture.isNormalMap, !Texture.makeNotReadable, isCompressed);

            newTexInfo.name = Texture.name;
            return(newTexInfo);
        }
        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);
        }
Esempio n. 5
0
        public static void GetReadable(TexInfo Texture, bool mipmaps)
        {
            String mbmPath          = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".mbm";
            String pngPath          = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".png";
            String pngPathTruecolor = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".truecolor";
            String jpgPath          = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".jpg";
            String tgaPath          = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".tga";

            if (File.Exists(pngPath) || File.Exists(pngPathTruecolor) || File.Exists(jpgPath) || File.Exists(tgaPath) || File.Exists(mbmPath))
            {
                Texture2D tex = new Texture2D(2, 2);
                String    name;
                if (Texture.name.Length > 0)
                {
                    name = Texture.name;
                }
                else
                {
                    name = Texture.name;
                }

                TextureInfoWrapper newTexture = new TextureInfoWrapper(tex, Texture.isNormalMap, true, false);
                Texture.texture = newTexture;
                newTexture.name = Texture.name;
                if (File.Exists(pngPath))
                {
                    Texture.filename = pngPath;
                    IMGToTexture(Texture, mipmaps, false);
                }
                else if (File.Exists(pngPathTruecolor))
                {
                    Texture.filename = pngPathTruecolor;
                    IMGToTexture(Texture, mipmaps, false);
                }
                else if (File.Exists(jpgPath))
                {
                    Texture.filename = jpgPath;
                    IMGToTexture(Texture, mipmaps, false);
                }
                else if (File.Exists(tgaPath))
                {
                    Texture.filename = tgaPath;
                    TGAToTexture(Texture, mipmaps);
                }
                else if (File.Exists(mbmPath))
                {
                    Texture.filename = mbmPath;
                    MBMToTexture(Texture, mipmaps);
                }
                tex.name = newTexture.name;
            }
        }
Esempio n. 6
0
        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);
            }
        }
        public static void GetReadable(TexInfo Texture, bool mipmaps)
        {
            String mbmPath = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".mbm";
            String pngPath = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".png";
            String pngPathTruecolor = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".truecolor";
            String jpgPath = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".jpg";
            String tgaPath = KSPUtil.ApplicationRootPath + "GameData/" + Texture.name + ".tga";
            if (File.Exists(pngPath) || File.Exists(pngPathTruecolor) || File.Exists(jpgPath) || File.Exists(tgaPath) || File.Exists(mbmPath))
            {
                Texture2D tex = new Texture2D(2, 2);
                String name;
                if (Texture.name.Length > 0)
                {
                    name = Texture.name;
                }
                else
                {
                    name = Texture.name;
                }

                TextureInfoWrapper newTexture = new TextureInfoWrapper(Texture.file, tex, Texture.isNormalMap, true, false);
                Texture.texture = newTexture;
                newTexture.name = Texture.name;
                if (File.Exists(pngPath))
                {
                    Texture.filename = pngPath;
                    IMGToTexture(Texture, mipmaps, false);
                }
                else if (File.Exists(pngPathTruecolor))
                {
                    Texture.filename = pngPathTruecolor;
                    IMGToTexture(Texture, mipmaps, false);
                }
                else if (File.Exists(jpgPath))
                {
                    Texture.filename = jpgPath;
                    IMGToTexture(Texture, mipmaps, false);
                }
                else if (File.Exists(tgaPath))
                {
                    Texture.filename = tgaPath;
                    TGAToTexture(Texture, mipmaps);
                }
                else if (File.Exists(mbmPath))
                {
                    Texture.filename = mbmPath;
                    MBMToTexture(Texture, mipmaps);

                }
                tex.name = newTexture.name;
            }
        }
        public static void Resize(TextureInfoWrapper 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);
        }
        static public TextureInfoWrapper UpdateTexture(TexInfo texture)
        {
            string     overrideName    = overridesList.Find(n => texture.name.Length == Regex.Match(texture.name, n).Length);
            bool       mipmaps         = true;
            bool       compress        = texture.isNormalMap ? false : true;
            int        scale           = 1;
            int        maxSize         = 0;
            int        minSize         = 64;
            FilterMode filterMode      = FilterMode.Bilinear;
            bool       makeNotReadable = false;

            if (foldersList.Exists(n => texture.name.StartsWith(n)))
            {
                if (texture.isNormalMap)
                {
                    mipmaps  = DatabaseLoaderTexture_ATM.config_mipmaps_normals;
                    compress = DatabaseLoaderTexture_ATM.config_compress_normals;
                    scale    = DatabaseLoaderTexture_ATM.config_scale_normals;
                    maxSize  = DatabaseLoaderTexture_ATM.config_max_size_normals;
                    minSize  = DatabaseLoaderTexture_ATM.config_min_size_normals;
                }
                else
                {
                    mipmaps  = DatabaseLoaderTexture_ATM.config_mipmaps;
                    compress = DatabaseLoaderTexture_ATM.config_compress;
                    scale    = DatabaseLoaderTexture_ATM.config_scale;
                    maxSize  = DatabaseLoaderTexture_ATM.config_max_size;
                    minSize  = DatabaseLoaderTexture_ATM.config_min_size;
                }
                filterMode      = config_filter_mode;
                makeNotReadable = config_make_not_readable;

                if (overrideName != null)
                {
                    ConfigNode overrideNode            = overrides.GetNode(overrideName);
                    String     normalString            = texture.isNormalMap ? "_normals" : "";
                    String     mipmapsString           = overrideNode.GetValue("mipmaps" + normalString);
                    String     compressString          = overrideNode.GetValue("compress" + normalString);
                    String     scaleString             = overrideNode.GetValue("scale" + normalString);
                    String     max_sizeString          = overrideNode.GetValue("max_size" + normalString);
                    String     min_sizeString          = overrideNode.GetValue("min_size" + normalString);
                    String     filter_modeString       = overrideNode.GetValue("filter_mode");
                    String     make_not_readableString = overrideNode.GetValue("make_not_readable");

                    if (mipmapsString != null)
                    {
                        bool.TryParse(mipmapsString, out mipmaps);
                    }
                    if (compressString != null)
                    {
                        bool.TryParse(compressString, out compress);
                    }
                    if (scaleString != null)
                    {
                        int.TryParse(scaleString, out scale);
                    }
                    if (filter_modeString != null)
                    {
                        try
                        {
                            filterMode = (FilterMode)Enum.Parse(typeof(FilterMode), filter_modeString);
                        }
                        catch
                        {
                            filterMode = config_filter_mode;
                        }
                    }
                    if (make_not_readableString != null)
                    {
                        bool.TryParse(make_not_readableString, out makeNotReadable);
                    }
                    if (max_sizeString != null)
                    {
                        int.TryParse(max_sizeString, out maxSize);
                    }
                    if (min_sizeString != null)
                    {
                        int.TryParse(min_sizeString, out minSize);
                    }
                }
            }
            texture.SetScalingParams(scale, maxSize, minSize);
            texture.makeNotReadable = makeNotReadable && !readableList.Contains(texture.name);
            TextureInfoWrapper ret = CacheController.FetchCacheTexture(texture, compress, mipmaps);

            ret.texture.filterMode = filterMode;
            return(ret);
        }
        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);
            }
        }
Esempio n. 11
0
        public static void TGAToTexture(TexInfo Texture, bool mipmaps)
        {
            TextureInfoWrapper 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);
        }
Esempio n. 12
0
        public static void MBMToTexture(TexInfo Texture, bool mipmaps)
        {
            TextureInfoWrapper texture = Texture.texture;

            TextureConverter.InitImageBuffer();
            FileStream mbmStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read);

            mbmStream.Position = 4;

            uint width = 0, height = 0;

            for (int b = 0; b < 4; b++)
            {
                width >>= 8;
                width  |= (uint)(mbmStream.ReadByte() << 24);
            }
            for (int b = 0; b < 4; b++)
            {
                height >>= 8;
                height  |= (uint)(mbmStream.ReadByte() << 24);
            }
            mbmStream.Position = 12;
            bool convertToNormalFormat = false;

            if (mbmStream.ReadByte() == 1)
            {
                texture.isNormalMap = true;
            }
            else
            {
                convertToNormalFormat = texture.isNormalMap ? true : false;
            }

            mbmStream.Position = 16;
            int format = mbmStream.ReadByte();

            mbmStream.Position += 3;

            int           imageSize = (int)(width * height * 3);
            TextureFormat texformat = TextureFormat.RGB24;
            bool          alpha     = false;

            if (format == 32)
            {
                imageSize += (int)(width * height);
                texformat  = TextureFormat.ARGB32;
                alpha      = true;
            }
            if (texture.isNormalMap)
            {
                texformat = TextureFormat.ARGB32;
            }

            mbmStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE);
            mbmStream.Close();

            Texture2D tex = texture.texture;

            Color32[] colors = new Color32[width * height];
            int       n      = 0;

            for (int i = 0; i < width * height; i++)
            {
                colors[i].r = imageBuffer[n++];
                colors[i].g = imageBuffer[n++];
                colors[i].b = 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;
                }
            }

            if (Texture.loadOriginalFirst)
            {
                Texture.Resize((int)width, (int)height);
            }

            if (Texture.needsResize)
            {
                colors = TextureConverter.ResizePixels(colors, (int)width, (int)height, Texture.resizeWidth, Texture.resizeHeight);
                width  = (uint)Texture.resizeWidth;
                height = (uint)Texture.resizeHeight;
            }
            tex.Resize((int)width, (int)height, texformat, mipmaps);
            tex.SetPixels32(colors);
            tex.Apply(mipmaps, false);
        }
        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));
            }
        }