gpt4 book ai didi

c# - XNA RenderTarget 保留内容

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:48 24 4
gpt4 key购买 nike

我试图在第一次绘制时将纹理加载到渲染目标中,然后保留内容以在每一帧绘制相同的纹理而不重新创建它。

这是我的代码,但它不起作用,只显示空纹理区域和

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);
}

这是最终结果: failed to render texture correctly

谁能解释一下我做错了什么??

最佳答案

我认为您忘记了渲染目标的 Game.SpriteBatch.Begin()End()。此外,我认为您应该将 SpriteBatch.End() 移动到靠近 Draw(rTarget... 方法调用的位置(特别是如果 Game.SpriteBatchSpriteBatch 是同一个变量)。

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);

关于c# - XNA RenderTarget 保留内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22880663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com