将一系列瓷砖拼接在一起

时间:2013-02-10 21:11:16

标签: c# xna

假设我有一个与纹理图集中的图块对应的矩形数组。我想要做的是采取这些瓷砖并从中创建一个Texture2D对象。基本上,我想将每个图块的像素数据顺序放在一起以形成一个图像。我怎么能这样做? Texture2D.SetData()会在这里使用吗?

1 个答案:

答案 0 :(得分:1)

RenderTarget2D target = new RenderTarger2D(...); 
//I cant remeber the arguments off the top of my head.
//I think its GraphicsDevice, Width, Height, GenerateMipmap, SurfaceFormat, Depthformat

GraphicsDevice.SetRenderTarget(target);
GraphicsDevice.Clear(Color.Black); //any colour will do
using(SpriteBatch b = new SpriteBatch(GraphicsDevice))
{
   b.Begin();

   //Loop through all texture and draw them, so ...
   for(int y = 0; y < 10; i++)
     for(int y = 0; y < 10; i++)
       batch.Draw(Texture, new Rectangle(xPos, yPos, width, height), Color.White));

   b.End();
}

GraphicsDevice.SetRenderTarget(null);

//Then to access your new Texture, just do 
Texture newTexture = target; //Target inherits from Texture2D so no casting needed

希望这会有所帮助:)