把一排瓷砖拼接在一起

本文关键字:拼接 在一起 一排 | 更新日期: 2023-09-27 17:58:46

假设我有一个矩形数组,与纹理图谱中的瓷砖相对应。我想做的是取下这些瓷砖,并从中创建一个Texture2D对象。基本上,我想把每个瓦片的像素数据按顺序放在一起,形成一个图像。我该怎么做呢?Texture2D.SetData()在这里有用吗?

把一排瓷砖拼接在一起

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

希望这有帮助:)