【发布时间】:2014-12-01 16:16:07
【问题描述】:
所以为了在纹理旋转后从纹理中获取 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 是正确的大小,但完全透明,表明问题在于绘制到缓冲区。
【问题讨论】: