public TexturePacket.TextureBlock AddTextureQuick(Texture2D tex) { string texname="noname__"; int i=0; string pname =texname+i.ToString(); while (mapNamedTex.ContainsKey(pname) == true) { i++; } Dictionary<string, RectangleF> ps = new Dictionary<string, RectangleF>(); mapNamedTex[pname] = new TexturePacket(tex, null); return mapNamedTex[pname].GetTextureBlock(null); }
public TexturePacket.TextureBlock AddTextureQuick(Texture2D tex) { string texname = "noname__"; int i = 0; string pname = texname + i.ToString(); while (mapNamedTex.ContainsKey(pname) == true) { i++; } Dictionary <string, RectangleF> ps = new Dictionary <string, RectangleF>(); mapNamedTex[pname] = new TexturePacket(tex, null); return(mapNamedTex[pname].GetTextureBlock(null)); }
private Dictionary <string, string> mapFileInTex = new Dictionary <string, string>(); //定义文件在那个贴图中 //private Dictionary<string, List<string>> groups;//贴图组管理 /// <summary> /// 得到一张贴图 /// </summary> /// <param name="name"></param> /// <param name="group"></param> /// <returns></returns> public TexturePacket.TextureBlock GetTexture(string name) { string filename; string blockname = name; if (mapFileInTex.ContainsKey(name)) { filename = mapFileInTex[name]; } else { filename = name; blockname = ""; } if (mapNamedTex.ContainsKey(filename)) { if (mapNamedTex[filename].texture == null) { mapNamedTex[filename].ReLoad(LoadTexFromFile(filename)); } return(mapNamedTex[filename].GetTextureBlock(blockname)); } else { Texture2D tex = null; try { tex = LoadTexFromFile(filename); } catch (Exception err) { } if (tex == null) { return(null); } mapNamedTex[filename] = new TexturePacket(tex, null); return(mapNamedTex[filename].GetTextureBlock(blockname)); } }
void LoadPacketInfo(string filename, string texname) { Dictionary <string, RectangleF> _blocks = new Dictionary <string, RectangleF>(); string[] lines = null; try { using (System.IO.Stream s = TitleContainer.OpenStream(filename)) { byte[] buf = new byte[65536]; int buflen = (int)s.Read(buf, 0, 65536); //byte[] b = new byte[s.Length]; //s.Read(b, 0, b.Length); string t = System.Text.Encoding.UTF8.GetString(buf, 0, buflen); lines = t.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); } } catch { return; } //string[] lines2 = System.IO.File.ReadAllLines(filename,Encoding.UTF8); foreach (var l in lines) { var ws = l.Split(new char[] { ',' }); float x = float.Parse(ws[1]) / 8192.0f; float y = float.Parse(ws[2]) / 8192.0f; float w = float.Parse(ws[3]) / 8192.0f; float h = float.Parse(ws[4]) / 8192.0f; string _file = ws[0]; while (!char.IsLetter(_file[0])) { _file = _file.Substring(1); } _blocks.Add(_file, new RectangleF(x, y, w, h)); mapFileInTex[_file] = texname; } mapNamedTex[texname] = new TexturePacket(LoadTexFromFile(texname), _blocks); }
//private Dictionary<string, List<string>> groups;//贴图组管理 /// <summary> /// 得到一张贴图 /// </summary> /// <param name="name"></param> /// <param name="group"></param> /// <returns></returns> public TexturePacket.TextureBlock GetTexture(string name) { string filename; string blockname=name; if (mapFileInTex.ContainsKey(name)) { filename = mapFileInTex[name]; } else { filename = name; blockname = ""; } if (mapNamedTex.ContainsKey(filename)) { if (mapNamedTex[filename].texture == null) { mapNamedTex[filename].ReLoad(LoadTexFromFile(filename)); } return mapNamedTex[filename].GetTextureBlock(blockname); } else { Texture2D tex =null; try { tex = LoadTexFromFile(filename); } catch(Exception err) { } if (tex == null) { return null; } mapNamedTex[filename] = new TexturePacket(tex, null); return mapNamedTex[filename].GetTextureBlock(blockname); } }
public TextureBlock(TexturePacket _p,RectangleF _uv) { parent = _p; parent.AddRef(); uv = _uv; }
//贴图装箱算法 public static IList<TexturePacket> PacketTexture(GraphicsDevice gdevice,IDictionary<string,Texture2D> sources) { List<TexturePacket> list = new List<TexturePacket>(); int packetsize = 512; RectanglePacker packer = new ArevaloRectanglePacker(packetsize, packetsize); Texture2D tex = new Texture2D(gdevice, packer.PackingAreaWidth, packer.PackingAreaHeight, false, SurfaceFormat.Color); Dictionary<string, RectangleF> rects = new Dictionary<string, RectangleF>(); foreach (var i in sources) { UInt32[] data=new UInt32[i.Value.Width*i.Value.Height]; i.Value.GetData<UInt32>(data); int twidth = i.Value.Width; int theight = i.Value.Height; bool bfixx=false; bool bfixy = false; if((twidth== packetsize/2||twidth==packetsize/4)) { } else { bfixx=true; twidth+=2; } if (theight==packetsize/2||theight==packetsize/4) { } else { theight += 2; bfixy = true; } Point pos; if (packer.TryPack(twidth, theight, out pos)) { //right } else { TexturePacket p = new TexturePacket(tex, rects); list.Add(p); packer = new ArevaloRectanglePacker(packetsize, packetsize); tex = new Texture2D(gdevice, packer.PackingAreaWidth, packer.PackingAreaHeight, false, SurfaceFormat.Color); rects = new Dictionary<string, RectangleF>(); packer.TryPack(i.Value.Width, i.Value.Height, out pos); } if (bfixx) pos.X += 1; if(bfixy) pos.Y+=1; tex.SetData<UInt32>(0, new Rectangle(pos.X, pos.Y, i.Value.Width, i.Value.Height), data, 0, data.Length); rects.Add(i.Key, new RectangleF( (float)(pos.X) / (float)packer.PackingAreaWidth, (float)(pos.Y) / (float)packer.PackingAreaHeight, (float)i.Value.Width / (float)packer.PackingAreaWidth, (float)i.Value.Height / (float)packer.PackingAreaHeight )); } { TexturePacket p = new TexturePacket(tex, rects); list.Add(p); } return list; }
void LoadPacketInfo(string filename,string texname) { Dictionary<string, RectangleF> _blocks = new Dictionary<string, RectangleF>(); string[] lines = null; try { using (System.IO.Stream s = TitleContainer.OpenStream(filename)) { byte[] buf = new byte[65536]; int buflen = (int)s.Read(buf, 0, 65536); //byte[] b = new byte[s.Length]; //s.Read(b, 0, b.Length); string t = System.Text.Encoding.UTF8.GetString(buf, 0, buflen); lines = t.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); } } catch { return; } //string[] lines2 = System.IO.File.ReadAllLines(filename,Encoding.UTF8); foreach (var l in lines) { var ws = l.Split(new char[] { ',' }); float x = float.Parse(ws[1])/8192.0f; float y = float.Parse(ws[2])/8192.0f; float w = float.Parse(ws[3])/8192.0f; float h = float.Parse(ws[4])/8192.0f; string _file = ws[0]; while (!char.IsLetter(_file[0])) { _file = _file.Substring(1); } _blocks.Add(_file, new RectangleF(x, y, w, h)); mapFileInTex[_file] = texname; } mapNamedTex[texname] = new TexturePacket(LoadTexFromFile(texname), _blocks); }
//贴图装箱算法 public static IList <TexturePacket> PacketTexture(GraphicsDevice gdevice, IDictionary <string, Texture2D> sources) { List <TexturePacket> list = new List <TexturePacket>(); int packetsize = 512; RectanglePacker packer = new ArevaloRectanglePacker(packetsize, packetsize); Texture2D tex = new Texture2D(gdevice, packer.PackingAreaWidth, packer.PackingAreaHeight, false, SurfaceFormat.Color); Dictionary <string, RectangleF> rects = new Dictionary <string, RectangleF>(); foreach (var i in sources) { UInt32[] data = new UInt32[i.Value.Width * i.Value.Height]; i.Value.GetData <UInt32>(data); int twidth = i.Value.Width; int theight = i.Value.Height; bool bfixx = false; bool bfixy = false; if ((twidth == packetsize / 2 || twidth == packetsize / 4)) { } else { bfixx = true; twidth += 2; } if (theight == packetsize / 2 || theight == packetsize / 4) { } else { theight += 2; bfixy = true; } Point pos; if (packer.TryPack(twidth, theight, out pos)) { //right } else { TexturePacket p = new TexturePacket(tex, rects); list.Add(p); packer = new ArevaloRectanglePacker(packetsize, packetsize); tex = new Texture2D(gdevice, packer.PackingAreaWidth, packer.PackingAreaHeight, false, SurfaceFormat.Color); rects = new Dictionary <string, RectangleF>(); packer.TryPack(i.Value.Width, i.Value.Height, out pos); } if (bfixx) { pos.X += 1; } if (bfixy) { pos.Y += 1; } tex.SetData <UInt32>(0, new Rectangle(pos.X, pos.Y, i.Value.Width, i.Value.Height), data, 0, data.Length); rects.Add(i.Key, new RectangleF( (float)(pos.X) / (float)packer.PackingAreaWidth, (float)(pos.Y) / (float)packer.PackingAreaHeight, (float)i.Value.Width / (float)packer.PackingAreaWidth, (float)i.Value.Height / (float)packer.PackingAreaHeight )); } { TexturePacket p = new TexturePacket(tex, rects); list.Add(p); } return(list); }
public TextureBlock(TexturePacket _p, RectangleF _uv) { parent = _p; parent.AddRef(); uv = _uv; }