C# 绘制到备选RenderTarget2D的问题,似乎绘制到backbuffer
因此,为了在纹理旋转后从纹理中获取颜色[]数据,以便使用该数据进行逐像素碰撞,我使用以下方法绘制旋转到单独RenderTarget2D的纹理,然后将其转换回纹理2D并从中获取颜色数据:C# 绘制到备选RenderTarget2D的问题,似乎绘制到backbuffer,c#,xna,xna-4.0,C#,Xna,Xna 4.0,因此,为了在纹理旋转后从纹理中获取颜色[]数据,以便使用该数据进行逐像素碰撞,我使用以下方法绘制旋转到单独RenderTarget2D的纹理,然后将其转换回纹理2D并从中获取颜色数据: public Color[] GetColorDataOf(SpriteBatch spriteBatch, Texture2D texture, float rotation) { // Get boundingBox of texture after rotation
public Color[] GetColorDataOf(SpriteBatch spriteBatch, Texture2D texture, float rotation)
{
// Get boundingBox of texture after rotation
Rectangle boundingBox = GetEnclosingBoundingBox(texture, rotation);
// Create new rendertarget of this size
RenderTarget2D buffer = new RenderTarget2D(GraphicsDevice, boundingBox.Width, boundingBox.Height);
// Change spritebatch to new rendertarget
GraphicsDevice.SetRenderTarget(buffer);
// Clear new rendertarget
GraphicsDevice.Clear(Color.Transparent);
// Draw sprite to new rendertarget
spriteBatch.Draw(texture, new Rectangle(boundingBox.Width / 2, boundingBox.Height / 2, texture.Width, texture.Height), null, Color.White, rotation, new Vector2(boundingBox.Center.X, boundingBox.Center.Y), SpriteEffects.None, 1f);
// Change rendertarget back to backbuffer
GraphicsDevice.SetRenderTarget(null);
// Get color data from the rendertarger
Color[] colorData = new Color[boundingBox.Width * boundingBox.Height];
Texture2D bufferTexture = (Texture2D)buffer;
bufferTexture.GetData(colorData);
return colorData;
}
现在我有两个问题,我希望它们是链接的,首先纹理在屏幕上绘制,返回的所有Color[]数据都是空的,即所有字段都等于0
**编辑**
使用Texture2D.SaveAsPng,我可以看到bufferTexture的大小是正确的,但完全透明,这表明问题在于如何绘制缓冲区 所以我把它修好了。事实证明,我需要创建另一组SpriteBatch.Begin和SpriteBatch.End调用,它们位于我绘制到新渲染目标的位置附近,否则它只是绘制到backbuffer