C# XNA RenderTarget保留内容
我尝试在第一次绘制时将纹理加载到rendertarget中一次,然后保留内容以在每帧绘制相同的纹理,而不重新创建它 这是我的代码,但它不工作,只显示空纹理区域和C# XNA RenderTarget保留内容,c#,.net,xna,C#,.net,Xna,我尝试在第一次绘制时将纹理加载到rendertarget中一次,然后保留内容以在每帧绘制相同的纹理,而不重新创建它 这是我的代码,但它不工作,只显示空纹理区域和 RenderTarget2D rTarget = null; /// <summary> /// This is called when the game should draw itself. /// </summary> /// <para
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);
}
RenderTarget2D rTarget=null;
///
///这就是所谓的比赛应该平局的时候。
///
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(游戏背景色);
SpriteBatch.Begin();
如果(rTarget==null)
{
rTarget=新的RenderTarget2D(Game.Graphics.GraphicsDevice,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
错误的
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24,0,RenderTargetUsage.PreserveContents);
Game.Graphics.GraphicsDevice.SetRenderTarget(rTarget);
游戏。图形。图形设备。清晰(颜色。黑色);
Game.SpriteBatch.Draw(ContentManager.Load(“tiles”)、新矩形(0、0、100、100)、颜色.白色);
Game.Graphics.GraphicsDevice.SetRenderTarget(null);
}
SpriteBatch.Draw(rTarget,新矩形(0400400),彩色,白色);
//画人物
角色。抽签(游戏时间);
SpriteBatch.End();
基础。抽签(游戏时间);
}
这就是最终结果:
有人能解释一下我做错了什么吗?我想你忘了渲染目标的
游戏.SpriteBatch.Begin()
和End()
。另外,我认为您应该将SpriteBatch.End()
移到Draw(rTarget…
方法调用)附近(特别是当Game.SpriteBatch
和SpriteBatch
是同一个变量时)
GraphicsDevice.Clear(游戏背景色);
如果(rTarget==null)
{
rTarget=新的RenderTarget2D(Game.Graphics.GraphicsDevice,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
错误的
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24,0,RenderTargetUsage.PreserveContents);
Game.Graphics.GraphicsDevice.SetRenderTarget(rTarget);
游戏。图形。图形设备。清晰(颜色。黑色);
Game.SpriteBatch.Begin();
Game.SpriteBatch.Draw(ContentManager.Load(“tiles”)、新矩形(0、0、100、100)、颜色.白色);
Game.SpriteBatch.End();
Game.Graphics.GraphicsDevice.SetRenderTarget(null);
}
SpriteBatch.Begin();
SpriteBatch.Draw(rTarget,新矩形(0400400),彩色,白色);
//画人物
角色。抽签(游戏时间);
SpriteBatch.End();
基础。抽签(游戏时间);
我认为您忘记了渲染目标的Game.SpriteBatch.Begin()
和End()
。此外,我认为您应该将SpriteBatch.End()
移动到Draw附近(rTarget…
方法调用(特别是当Game.SpriteBatch
和SpriteBatch
是同一变量时)
GraphicsDevice.Clear(游戏背景色);
如果(rTarget==null)
{
rTarget=新的RenderTarget2D(Game.Graphics.GraphicsDevice,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
错误的
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24,0,RenderTargetUsage.PreserveContents);
Game.Graphics.GraphicsDevice.SetRenderTarget(rTarget);
游戏。图形。图形设备。清晰(颜色。黑色);
Game.SpriteBatch.Begin();
Game.SpriteBatch.Draw(ContentManager.Load(“tiles”)、新矩形(0、0、100、100)、颜色.白色);
Game.SpriteBatch.End();
Game.Graphics.GraphicsDevice.SetRenderTarget(null);
}
SpriteBatch.Begin();
SpriteBatch.Draw(rTarget,新矩形(0400400),彩色,白色);
//画人物
角色。抽签(游戏时间);
SpriteBatch.End();
基础。抽签(游戏时间);
找到了答案-原来您需要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);
}
RenderTarget2D rTarget=null;
///
///这就是所谓的比赛应该平局的时候。
///
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
如果(rTarget==null)
{
rTarget=新的RenderTarget2D(Game.Graphics.GraphicsDevice,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
错误的
Game.Graphics.GraphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24,0,RenderTargetUsage.PreserveContents);
Game.Graphics.GraphicsDevice.SetRenderTarget(rTarget);
游戏。图形。图形设备。清晰(颜色。黑色);
SpriteBatch.Begin();//新建
Game.SpriteBatch.Draw(ContentManager.Load(“tiles”)、新矩形(0、0、100、100)、颜色.白色);
SpriteBatch.End();//新建
Game.Graphics.GraphicsDevice.SetRenderTarget(null);
}
图形设备。清晰(游戏背景色);
SpriteBatch.Begin();
SpriteBatch.Draw(rTarget,新矩形(0400400),彩色,白色);
//画人物
角色。抽签(游戏时间);
SpriteBatch.End();
基础。抽签(游戏时间);
}
找到了答案-结果是y
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);
}