2

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

4

1 回答 1

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

希望这可以帮助 :)

于 2013-03-05T13:24:54.693 回答