【问题标题】:Issues with drawing to alternative RenderTarget2D, seems to draw to backbuffer绘制到替代 RenderTarget2D 的问题,似乎绘制到后缓冲区
【发布时间】: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 是正确的大小,但完全透明,表明问题在于绘制到缓冲区。

【问题讨论】:

    标签: c# xna xna-4.0


    【解决方案1】:

    所以我修好了。结果我需要在我绘制到新渲染目标的位置周围创建另一组 SpriteBatch.Begin() 和 SpriteBatch.End() 调用,否则它只是绘制到后台缓冲区。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      相关资源
      最近更新 更多