我尝试在第一次绘制时将纹理加载到渲染对象中,然后保留内容以在每帧中绘制相同的纹理而不重新创建它。
这是我的代码,但它不起作用,只显示空纹理区域和
RenderTarget2D rTarget = null;
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(GameBackgroundColor);
SpriteBatch.Begin();
if (rTarget == null)
{
rTarget = new RenderTarget2D(Game.Graphics.GraphicsDevice,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
false,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24, 0, RenderTargetUsage.PreserveContents);
Game.Graphics.GraphicsDevice.SetRenderTarget(rTarget);
Game.Graphics.GraphicsDevice.Clear(Color.Black);
Game.SpriteBatch.Draw(ContentManager.Load<Texture2D>("tiles"), new Rectangle(0, 0, 100, 100), Color.White);
Game.Graphics.GraphicsDevice.SetRenderTarget(null);
}
SpriteBatch.Draw(rTarget, new Rectangle(0, 0,400, 400), Color.White);
//draw the character
character.Draw(gameTime);
SpriteBatch.End();
base.Draw(gameTime);
}
这是最终结果:
有谁能解释我做错了什么?
答案 0 :(得分:3)
我认为您忘记了渲染目标的Game.SpriteBatch.Begin()
和End()
。另外我认为您应该SpriteBatch.End()
接近Draw(rTarget...
方法调用(特别是如果Game.SpriteBatch
和SpriteBatch
是相同的变量)。
GraphicsDevice.Clear(GameBackgroundColor);
if (rTarget == null)
{
rTarget = new RenderTarget2D(Game.Graphics.GraphicsDevice,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
false,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24, 0, RenderTargetUsage.PreserveContents);
Game.Graphics.GraphicsDevice.SetRenderTarget(rTarget);
Game.Graphics.GraphicsDevice.Clear(Color.Black);
Game.SpriteBatch.Begin();
Game.SpriteBatch.Draw(ContentManager.Load<Texture2D>("tiles"), new Rectangle(0, 0, 100, 100), Color.White);
Game.SpriteBatch.End();
Game.Graphics.GraphicsDevice.SetRenderTarget(null);
}
SpriteBatch.Begin();
SpriteBatch.Draw(rTarget, new Rectangle(0, 0,400, 400), Color.White);
//draw the character
character.Draw(gameTime);
SpriteBatch.End();
base.Draw(gameTime);
答案 1 :(得分:1)
找到答案 - 事实证明你需要SpriteBatch.Begin和SpriteBatch.End在每次绘制时都需要渲染到渲染目标。
RenderTarget2D rTarget = null;
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
if (rTarget == null)
{
rTarget = new RenderTarget2D(Game.Graphics.GraphicsDevice,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
false,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24, 0, RenderTargetUsage.PreserveContents);
Game.Graphics.GraphicsDevice.SetRenderTarget(rTarget);
Game.Graphics.GraphicsDevice.Clear(Color.Black);
SpriteBatch.Begin(); //new
Game.SpriteBatch.Draw(ContentManager.Load<Texture2D>("tiles"), new Rectangle(0, 0, 100, 100), Color.White);
SpriteBatch.End(); //new
Game.Graphics.GraphicsDevice.SetRenderTarget(null);
}
GraphicsDevice.Clear(GameBackgroundColor);
SpriteBatch.Begin();
SpriteBatch.Draw(rTarget, new Rectangle(0, 0,400, 400), Color.White);
//draw the character
character.Draw(gameTime);
SpriteBatch.End();
base.Draw(gameTime);
}