因此,为了在旋转后从纹理中获取Color []数据以便将此数据用于每像素碰撞,我使用以下方法将所述纹理(旋转)绘制到单独的RenderTarget2D,然后转换这回到texture2D并从中获取颜色数据:
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是正确的大小,但只是完全透明,表明问题在于绘制到缓冲区。
答案 0 :(得分:0)
所以我修好了。结果我需要创建另一组SpriteBatch.Begin()和SpriteBatch.End()调用我绘制到新渲染的地方,否则它只是绘制到后备缓冲区。