Example #1
0
        public static ImageInfo?FromStream(Stream stream)
        {
            ImageInfo?info = null;

            if (JpgDecoder.Test(stream))
            {
                info = JpgDecoder.Info(stream);
            }
            else if (PngDecoder.Test(stream))
            {
                info = PngDecoder.Info(stream);
            }
            else if (BmpDecoder.Test(stream))
            {
                info = BmpDecoder.Info(stream);
            }
            else if (GifDecoder.Test(stream))
            {
                info = GifDecoder.Info(stream);
            }
            else if (PsdDecoder.Test(stream))
            {
                info = PsdDecoder.Info(stream);
            }
            else if (TgaDecoder.Test(stream))
            {
                info = TgaDecoder.Info(stream);
            }

            return(info);
        }
Example #2
0
        public static ImageResult FromStream(Stream stream, ColorComponents?requiredComponents = null,
                                             bool use8BitsPerChannel = true)
        {
            ImageResult result = null;

            if (JpgDecoder.Test(stream))
            {
                result = JpgDecoder.Decode(stream, requiredComponents);
            }
            else if (PngDecoder.Test(stream))
            {
                result = PngDecoder.Decode(stream, requiredComponents);
            }
            else if (BmpDecoder.Test(stream))
            {
                result = BmpDecoder.Decode(stream, requiredComponents);
            }
            else if (GifDecoder.Test(stream))
            {
                result = GifDecoder.Decode(stream, requiredComponents);
            }
            else if (PsdDecoder.Test(stream))
            {
                result = PsdDecoder.Decode(stream, requiredComponents);
            }
            else if (TgaDecoder.Test(stream))
            {
                result = TgaDecoder.Decode(stream, requiredComponents);
            }

            if (result == null)
            {
                Decoder.stbi__err("unknown image type");
            }

            if (use8BitsPerChannel && result.BitsPerChannel != 8)
            {
                result.Data = Conversion.stbi__convert_16_to_8(result.Data, result.Width, result.Height,
                                                               (int)result.ColorComponents);
            }

            return(result);
        }
Example #3
0
        public static ImageInfo?FromStream(Stream stream)
        {
            var info = JpgDecoder.Info(stream);

            if (info != null)
            {
                return(info);
            }

            info = PngDecoder.Info(stream);
            if (info != null)
            {
                return(info);
            }

            info = GifDecoder.Info(stream);
            if (info != null)
            {
                return(info);
            }

            info = BmpDecoder.Info(stream);
            if (info != null)
            {
                return(info);
            }

            info = PsdDecoder.Info(stream);
            if (info != null)
            {
                return(info);
            }

            info = TgaDecoder.Info(stream);
            if (info != null)
            {
                return(info);
            }

            return(null);
        }
Example #4
0
 public Bitmap GetBitmapFromTGA()
 {
     return(TgaDecoder.FromBinary(FileContent));
 }
    public Material CreateMaterial(int id, string mod_id = null)
    {
        Material mat;
        string   textureName = P3DRenderInfo[id].TextureFile.Remove(P3DRenderInfo[id].TextureFile.Length - 4);

        if (textureName == "street14")
        {
            textureName = "street1";
        }
        // path to one of default (dds) textures
        string path = IO.GetCrashdayPath() + "/data/content/textures/" + textureName + ".dds";

        // if mod, search for custom texture. If not found, it means texture we're searching for is default
        if (mod_id != null)
        {
            string[] texturePath = new string[0];
            if (Directory.Exists(IO.GetCrashdayPath() + "/moddata/" + mod_id + "/content/textures/"))
            {
                texturePath = Directory.GetFiles(IO.GetCrashdayPath() + "/moddata/" + mod_id + "/content/textures/", textureName + ".*").Where(s => s.EndsWith(".dds") || s.EndsWith(".tga")).ToArray();
            }

            if (texturePath.Length > 0)
            {
                path = IO.GetCrashdayPath() + "/moddata/" + mod_id + "/content/textures/" + textureName + texturePath[0].Substring(texturePath[0].Length - 4);
            }
        }
        Texture2D tex = Texture2D.whiteTexture;

        if (path.EndsWith(".dds"))
        {
            if (File.Exists(path))
            {
                tex = DDSDecoder.LoadTextureDXT(File.ReadAllBytes(path), TextureFormat.DXT5);
            }
            else
            {
                Debug.LogWarning(path + " | texture not found! Loading white texture instead.");
            }
        }
        else if (path.EndsWith(".tga"))
        {
            if (File.Exists(path))
            {
                tex = TgaDecoder.LoadTGA(File.ReadAllBytes(path));
            }
            else
            {
                Debug.LogWarning(path + " | texture not found! Loading white texture instead.");
            }
        }
        else
        {
            Debug.LogError("Failed to load. Loading default texture. Path: " + path);
            tex = DDSDecoder.LoadTextureDXT(File.ReadAllBytes(IO.GetCrashdayPath() + "/data/content/textures/colwhite.dds"), TextureFormat.DXT1);
        }
        try
        {
            tex.mipMapBias = -0.5f;
            tex.Apply(true);
        }
        catch { return(default(Material)); }

        bool tr  = P3DRenderInfo[id].TextureFile.Contains("transp");
        bool gls = P3DRenderInfo[id].TextureFile.Contains("gls") || P3DRenderInfo[id].TextureFile.Contains("glass");

        if (tr || gls)
        {
            mat = new Material(Shader.Find("Sprites/Default"));

            Color c = gls ? new Color(0.1f, 0.1f, 0.1f, 0.4f) : Color.clear;
            mat.SetColor("_Color", c);
            mat.SetFloat("_Mode", 2);

            mat.SetInt("_ZWrite", 0);
            mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);

            mat.DisableKeyword("_ALPHATEST_ON");
            mat.EnableKeyword("_ALPHABLEND_ON");
            mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");

            mat.renderQueue = 3000;
        }
        else
        {        // every tile with grass (all of the tunnels) have link to "floor1.mat" material set in resources folder. That way we can globally change material's shader from every script we want
            if (textureName == "floor1")
            {
                mat = Resources.Load <Material>("floor1");
            }
            else if (textureName == "inv_wall" || textureName.Contains("decals") || textureName == "railings" || textureName.Contains("fen") || textureName == "pine" ||
                     textureName == "mtlgrid" || textureName.Contains("strut") || textureName.Contains("detail"))
            {
                mat = new Material(Shader.Find("Sprites/Default"));
            }
            else if (textureName == "window")
            {
                mat = new Material(Shader.Find("Unlit/Transparent"));
            }
            else
            {
                mat = new Material(Shader.Find("Mobile/Bumped Diffuse"));
            }

            mat.SetFloat("_Glossiness", 0);
        }
        mat.mainTexture = tex;
        mat.name        = textureName;
        return(mat);
    }
    /// <summary>
    /// Loads all Cfl files using directory. Has to be run before ReadCatFiles.
    /// </summary>
    /// <param name="Dir"></param>
    private static List <string> ReadCflFiles(string Dir, string mod_id = null, string mod_to_filter_by = null)
    {
        List <string> names = new List <string>();

        string[] cfls = Directory.GetFiles(Dir, "*.cfl");
        foreach (var File in cfls)
        {
            string name = Path.GetFileNameWithoutExtension(File);
            if (mod_to_filter_by != null && DefaultTiles.ContainsKey(name))
            {
                List <string> mods = DefaultTiles[name];
                if (mods.Count() != 0 && mods[mods.Count - 1] != mod_to_filter_by)
                {
                    continue;
                }
            }
            string[] cfl = System.IO.File.ReadAllLines(File);

            for (int j = 0; j < cfl.Length; j++)
            {
                cfl[j] = IO.RemoveComment(cfl[j]);
            }

            if (cfl[2].LastIndexOf('.') < 0)
            {
                Debug.Log("modelName");
            }
            string modelName = cfl[2].Remove(cfl[2].LastIndexOf('.')).ToLower();

            // field is just block of grass. It's listed in cfl file but isn't showed. Mica used this tile as CP (weird right?) so I had to take this into consideration
            if (modelName == "field" && mod_id == null || modelName == "border1" || modelName == "border2")
            {
                continue;
            }
            string[]   size_str = Regex.Split(cfl[3], " ");
            Vector2Int size     = new Vector2Int(int.Parse(size_str[0]), int.Parse(size_str[1]));

            bool   IsCheckpoint = cfl[9] == "1" ? true : false;
            int    offset       = cfl[13].Contains("STOP") ? 1 : 0;    // checkpoints have one additional parameter for checkpoint dimensions, which moves forward all other parameters by one line
            string Restrictions = Regex.Replace(cfl[12 + offset], " ", "");
            // remove STOP characters
            Restrictions = Restrictions.Remove(Restrictions.Length - 4);
            string Vegetation_str = cfl[16 + offset];

            List <Vegetation> VegData = new List <Vegetation>();
            if (Vegetation_str != "NO_VEGETATION" && Vegetation_str != "SAME_VEGETATION_AS")
            {
                int bushCount = int.Parse(Vegetation_str);
                for (int j = 0; j < bushCount; j++)
                {
                    string VegName = cfl[19 + offset + 7 * j];
                    VegName = VegName.Substring(0, VegName.IndexOf('.'));
                    // don't load grass and small vegetation. We are interested only in big trees
                    // TODO
                    //if (!Consts.AllowedBushes.Contains(VegName))
                    //  continue;
                    //string[] xz = Regex.Split(cfl[21 + offset + 7 * j], " ");
                    //float x = float.Parse(xz[0], System.Globalization.CultureInfo.InvariantCulture);
                    //float z = float.Parse(xz[1], System.Globalization.CultureInfo.InvariantCulture);
                    //string y = cfl[23 + offset + 7 * j];
                    //VegData.Add(new Vegetation(VegName, x, z, y));
                }
            }
            // load icon of tile
            Texture2D texture = Texture2D.blackTexture;
            if (Directory.Exists(NavigateDirUp(Dir, 2) + "\\textures\\pictures\\tiles\\"))
            {
                string[] files = Directory.GetFiles(NavigateDirUp(Dir, 2) + "\\textures\\pictures\\tiles\\", name + ".*");
                files = files.Where(f => f.Contains("dds") || f.Contains("tga")).ToArray();
                if (files.Length == 0)
                {
                    string datapath = IO.GetCrashdayPath() + "\\data\\content\\textures\\pictures\\tiles\\" + name + ".tga";
                    texture = TgaDecoder.LoadTGA(datapath);
                }
                else if (files[0].Contains(".tga"))                 // tga format
                {
                    texture = TgaDecoder.LoadTGA(files[0]);
                }
                else
                {
                    // file is in dds format
                    string ddsFilePath = NavigateDirUp(Dir, 2) + "\\textures\\pictures\\tiles\\" + name + ".dds";
                    byte[] bytes       = System.IO.File.ReadAllBytes(ddsFilePath);
                    texture = DDSDecoder.LoadTextureDXT(bytes, TextureFormat.DXT1);
                }
            }
            else
            {
                texture = Resources.Load <Texture2D>("flag");
            }
            string Model_path = NavigateDirUp(Dir, 2) + "\\models\\" + modelName + ".p3d";

            if (!System.IO.File.Exists(Model_path))
            {             // look for model in original files (used in mica's tiles)
                Model_path = IO.GetCrashdayPath() + "\\data\\content\\models\\" + modelName + ".p3d";
                if (!System.IO.File.Exists(Model_path))
                {
                    Debug.LogWarning("No p3d for tile " + name);
                    continue;
                }
            }

            P3DModel model = P3DParser.LoadFromFile(Model_path);

            List <Material> ModelMaterials = new List <Material>(model.P3DNumTextures);

            for (int j = 0; j < model.P3DNumTextures; j++)
            {
                MaterialListEntry m = Materials.FirstOrDefault(x => x.Name == model.P3DRenderInfo[j].TextureFile);
                if (m == default(MaterialListEntry))
                {
                    m = new MaterialListEntry
                    {
                        Material = model.CreateMaterial(j, mod_id),
                        Name     = model.P3DRenderInfo[j].TextureFile
                    };
                    Materials.Add(m);
                }
                ModelMaterials.Add(m.Material);
            }

            if (TileListInfo.ContainsKey(name))
            {
                TileListInfo[name].Set(size, Restrictions, IsCheckpoint, model, ModelMaterials, texture, VegData.ToArray(), mod_id);
            }
            else
            {
                TileListInfo.Add(name, new TileListEntry(size, Restrictions, IsCheckpoint, model, ModelMaterials, texture, VegData.ToArray(), mod_id));
            }

            names.Add(name);
        }
        return(names);
    }