示例#1
0
        static Texture2D ConvertToTexture(System.Drawing.Bitmap b, GraphicsDevice graphicsDevice)
        {
            Texture2D tx = null;

            using (MemoryStream s = new MemoryStream())
            {
                b.Save(s, System.Drawing.Imaging.ImageFormat.Png);
                s.Seek(0, SeekOrigin.Begin);
                tx = Texture2D.FromStream(graphicsDevice, s);
            }
            return(tx);
        }
示例#2
0
        public static bool TryParse(Stream s, GraphicsDevice g, ContentManager c, out Effect fx, ref List <object> refs, ParsingFlags ps = ParsingFlags.None)
        {
            fx = null;
            StreamReader f = new StreamReader(new BufferedStream(s));

            // Get The Arguments From The Material File
            List <string[]> args = new List <string[]>();

            while (!f.EndOfStream)
            {
                string line = f.ReadLine();
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }
                string[] split = line.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                if (split.Length < 1)
                {
                    continue;
                }
                split[0] = split[0].Trim().ToLower();
                switch (split[0])
                {
                case "fx": if (split.Length >= 2)
                    {
                        args.Add(split);
                    }
                    break;

                case "fxpt": if (split.Length >= 3)
                    {
                        args.Add(split);
                    }
                    break;

                case "fxptc": if (split.Length >= 3)
                    {
                        args.Add(split);
                    }
                    break;

                case "fxpf": if (split.Length >= 4)
                    {
                        args.Add(split);
                    }
                    break;
                }
            }

            // Get The Effect For This Material
            Predicate <string[]> fxMatch = (a) => { return(a[0].Equals("fx")); };

            string[] fxArg = args.Find(fxMatch);
            if (fxArg == null)
            {
                return(false);
            }
            args.RemoveAll(fxMatch);


            // Try To Create The Effect
            if (ps.HasFlag(ParsingFlags.LoadEffectByteCode))
            {
                try {
                    byte[] code = null;
                    using (FileStream fxs = File.OpenRead(fxArg[1].Trim())) {
                        code = new byte[fxs.Length];
                        fxs.Read(code, 0, code.Length);
                    }
                    fx = new Effect(g, code);
                }
                catch (Exception) { fx = null; return(false); }
            }
            else
            {
                try { fx = c.Load <Effect>(fxArg[1].Trim()); }
                catch (Exception) { fx = null; return(false); }
            }

            // Will Attempt To Set As Many Uniforms As Possible Without Raising Errors
            foreach (string[] arg in args)
            {
                switch (arg[0])
                {
                case "fxpt":
                    EffectParameter fxpt = fx.Parameters[arg[1].Trim()];
                    if (fxpt == null)
                    {
                        continue;
                    }
                    try {
                        Texture2D t = null;
                        if (ps.HasFlag(ParsingFlags.LoadTextureStream))
                        {
                            using (FileStream ts = File.OpenRead(arg[2].Trim())) {
                                t = Texture2D.FromStream(g, ts);
                            }
                        }
                        else
                        {
                            t = c.Load <Texture2D>(arg[2].Trim());
                        }
                        if (t != null)
                        {
                            refs.Add(t);
                            fxpt.SetValue(t);
                        }
                    }
                    catch (Exception) { continue; }
                    break;

                case "fxptc":     // Texture Cube Parameter
                    EffectParameter fxptc = fx.Parameters[arg[1].Trim()];
                    if (fxptc == null)
                    {
                        continue;
                    }
                    try {
                        TextureCube tc = c.Load <TextureCube>(arg[2].Trim());
                        if (tc != null)
                        {
                            refs.Add(tc);
                            fxptc.SetValue(tc);
                        }
                    }
                    catch (Exception) { continue; }
                    break;

                case "fxpf":     // Vector Parameter
                    EffectParameter fxptv = fx.Parameters[arg[1].Trim()];
                    int             comps;
                    if (fxptv == null || !int.TryParse(arg[2], out comps))
                    {
                        continue;
                    }
                    string[] sc = arg[3].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    if (sc.Length != comps)
                    {
                        continue;
                    }
                    switch (comps)
                    {
                    case 1:
                        float v1;
                        if (float.TryParse(sc[0], out v1))
                        {
                            fxptv.SetValue(v1);
                        }
                        break;

                    case 2:
                        Vector2 v2 = Vector2.Zero;
                        if (float.TryParse(sc[0], out v2.X) &&
                            float.TryParse(sc[1], out v2.Y)
                            )
                        {
                            fxptv.SetValue(v2);
                        }
                        break;

                    case 3:
                        Vector3 v3 = Vector3.Zero;
                        if (float.TryParse(sc[0], out v3.X) &&
                            float.TryParse(sc[1], out v3.Y) &&
                            float.TryParse(sc[2], out v3.Z)
                            )
                        {
                            fxptv.SetValue(v3);
                        }
                        break;

                    case 4:
                        Vector4 v4 = Vector4.Zero;
                        if (float.TryParse(sc[0], out v4.X) &&
                            float.TryParse(sc[1], out v4.Y) &&
                            float.TryParse(sc[2], out v4.Z) &&
                            float.TryParse(sc[3], out v4.W)
                            )
                        {
                            fxptv.SetValue(v4);
                        }
                        break;

                    default:
                        if (comps > 4)
                        {
                            float[] vn  = new float[comps];
                            bool    vnc = true;
                            for (int i = 0; i < sc.Length && i < vn.Length && vnc; i++)
                            {
                                if (!float.TryParse(sc[i], out vn[i]))
                                {
                                    vnc = false;
                                }
                            }
                            if (vnc)
                            {
                                fxptv.SetValue(vn);
                            }
                        }
                        break;
                    }
                    break;
                }
            }
            return(true);
        }
示例#3
0
        public Texture2D LoadTexture(string fileName)
        {
            if (File.Exists(fileName))
            {
                using (FileStream stream = new FileStream(fileName, FileMode.Open))
                {
                    return(Texture2D.FromStream(_graphicsDeviceManager.GraphicsDevice, stream));
                }
            }

            Color orangeLight = new Color(224, 192, 0);
            Color orangeDark  = new Color(224, 160, 0);
            Color blueLight   = new Color(0, 0, 255);
            Color blueDark    = new Color(0, 64, 192);

            const int size = 64;

            Texture2D tx = new Texture2D(_graphicsDeviceManager.GraphicsDevice, size, size);

            Color[] colors = new Color[size * size];

            for (int i = 0; i < colors.Length; i++)
            {
                if (i % size >= (size / 2))
                {
                    if (i / size >= (size / 2))
                    {
                        if (((i / size) % 2) == (i % 2))
                        {
                            colors[i] = orangeLight;
                        }
                        else
                        {
                            colors[i] = orangeDark;
                        }
                    }
                    else
                    {
                        if (((i / size) % 2) == (i % 2))
                        {
                            colors[i] = blueLight;
                        }
                        else
                        {
                            colors[i] = blueDark;
                        }
                    }
                }
                else
                {
                    if (i / size >= (size / 2))
                    {
                        if (((i / size) % 2) == (i % 2))
                        {
                            colors[i] = blueLight;
                        }
                        else
                        {
                            colors[i] = blueDark;
                        }
                    }
                    else
                    {
                        if (((i / size) % 2) == (i % 2))
                        {
                            colors[i] = orangeLight;
                        }
                        else
                        {
                            colors[i] = orangeDark;
                        }
                    }
                }
            }

            tx.SetData(colors);

            return(tx);
        }