【问题标题】:Drawing sprites to a permanent Texture2d using Monogame / XNA使用 Monogame / XNA 将精灵绘制到永久 Texture2d
【发布时间】:2013-12-07 20:38:36
【问题描述】:

(来自 Gamedevs 的交叉帖子)

我正在与 Monogame 合作做一个 2d 精灵引擎。我已经让它工作得很好,我可以在屏幕上移动许多精灵,并获得我想要的效果和位置。

昨晚我尝试添加一个功能,但我无法让它正常工作。 我正在尝试创建移动精灵可以留下的永久轨迹。所以我有一个大部分透明的PNG,上面有一些白线,叫做“条纹”。我的想法是我设置了一个新的Texture2D 表面(称为 streakoverlay)并在其上绘制条纹。

我切换回后台缓冲区并绘制:背景、条纹覆盖层,最后是它上面的精灵。条纹应该随着时间的推移而增加。它几乎可以工作,但我遇到的麻烦是streakOverlay 似乎每次都会自行清除。我希望它的行为方式与没有这条线 GraphicsDevice.Clear(Color.White); 的图形显示行为相同 - 一切都会堆积在其中。

相反,它会重置为该纹理默认的Purple 透明颜色。有什么建议?下面是渲染引擎的核心部分:

GraphicsDeviceManager graphics;
RenderTarget2D streakOverlay;
SpriteBatch spriteBatch;

private Texture2D bkg;
private Texture2D prototype;
private Texture2D streak;

//Initialize 
graphics = new GraphicsDeviceManager(this);
GraphicsDevice.Clear(Color.Transparent);
streakOverlay = new RenderTarget2D(GraphicsDevice, 200,200);

//Load Content
bkg = Content.Load<Texture2D>("bkg"); //Background image
prototype = Content.Load<Texture2D>("prototype"); //Prototype sprite that moves around
streak = Content.Load<Texture2D>("blueStreak"); //Trails being left by Prototype sprite

graphics.GraphicsDevice.SetRenderTarget(streakOverlay); 
GraphicsDevice.Clear(Color.Transparent); //Attempt to make the streakoverlay is fully transparent.
graphics.GraphicsDevice.SetRenderTarget(null);

//Draw
Random r = new Random();
graphics.GraphicsDevice.SetRenderTarget(streakOverlay); //Switch to drawing to the streakoverlay

spriteBatch.Begin();
//Draw some streaks that should accumulate
spriteBatch.Draw(streak, new Vector2(r.Next(0, 200), r.Next(0, 200)), null, Color.White, 0f, new Vector2(25, 25), 1f, SpriteEffects.None, 1f);
spriteBatch.End();

//Switch back to drawing on the back buffer.
graphics.GraphicsDevice.SetRenderTarget(null);

spriteBatch.Begin();
spriteBatch.Draw(bkg, new Rectangle(0, 0, 2000, 2000), Color.White);    //Draw our background
spriteBatch.Draw(streakOverlay, new Vector2(0, 0), Color.White); //Put the overlay on top of it
spriteBatch.Draw(prototype, new Vector2(_spritePrototype.getX, _spritePrototype.getY), null, Color.White, DegreeToRadian(rotationSprite), new Vector2(25, 25), .5f, SpriteEffects.None, 0f); //Draw the sprite that's moving around.
spriteBatch.End();

base.Draw(gameTime);

【问题讨论】:

    标签: c# xna monogame texture2d


    【解决方案1】:

    您需要使用带有 4 个参数并指定 PreserveContents 的 RenderTarget2D 的扩展构造函数。假设渲染目标经常被重用,所以当它被设置时它会自动清除,除非这个标志被设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多