2013-02-10 106 views
2

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

+0

你可以这一切画上'RenderTarget2D',然后再用它作为一个'Texture2D',但有可能是一个更好的方式做你想要什么实现。 – user1306322 2013-02-10 21:18:46

+0

基于着色器的方法是否适合您?我的意思是转换像素着色器中的纹理坐标,以便它们总是包装到指定的矩形中。 – Lucius 2013-02-11 00:47:50

+0

我使用了RenderTarget2D选项。不过还得测试一下。 – MGZero 2013-02-11 04:56:42

回答

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 

希望这有助于:)