XNA截图显示了Bloom之前,而不是最终渲染
我有一个使用Reach图形设置在C#XNA 4.0中编码的Windows平台游戏。 我的项目基于GameStateManagement示例,但后来我添加了Bloom和spriteSheet / spriteBatchfunction。
我希望保存最终屏幕输出的屏幕截图。 但是,当我保存我的截图时,它只显示Bloom应用之前和我的HUD文本显示之前的渲染(我在Bloom之后绘制)。 在这两个过程之后,我的Draw方法结束时保存了截图。
我尝试过各种各样的事情。 安德鲁的答案在这里拍摄XNA中的屏幕截图很有帮助并且保存了图像; 但是,它并没有保存最终渲染。
我有一种感觉它与绽放过程或spograbatch有关。
这是我的代码:
example { public override void Draw(GameTime gameTime) { ScreenManager.GraphicsDevice.SetRenderTarget(sceneRenderTarget); // Clear the screen to black ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 0, 0); SpriteBatch spriteBatch = ScreenManager.SpriteBatch; spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend); // then i draw all my game stuff spriteBatch.End(); #region Post-Processing & Bloom ScreenManager.GraphicsDevice.SamplerStates[1] = SamplerState.LinearClamp; // Pass 1: draw the scene into rendertarget 1, using a // shader that extracts only the brightest parts of the image. bloomExtractEffect.Parameters["BloomThreshold"].SetValue( Settings.BloomThreshold); DrawFullscreenQuad(sceneRenderTarget, renderTarget1, bloomExtractEffect, IntermediateBuffer.PreBloom); // Pass 2: draw from rendertarget 1 into rendertarget 2, // using a shader to apply a horizontal gaussian blur filter. SetBlurEffectParameters(1.0f / (float)renderTarget1.Width, 0); DrawFullscreenQuad(renderTarget1, renderTarget2, gaussianBlurEffect, IntermediateBuffer.BlurredHorizontally); // Pass 3: draw from rendertarget 2 back into rendertarget 1, // using a shader to apply a vertical gaussian blur filter. SetBlurEffectParameters(0, 1.0f / (float)renderTarget1.Height); DrawFullscreenQuad(renderTarget2, renderTarget1, gaussianBlurEffect, IntermediateBuffer.BlurredBothWays); // Pass 4: draw both rendertarget 1 and the original scene // image back into the main backbuffer, using a shader that // combines them to produce the final bloomed result. ScreenManager.GraphicsDevice.SetRenderTarget(null); EffectParameterCollection parameters = bloomCombineEffect.Parameters; parameters["BloomIntensity"].SetValue(Settings.BloomIntensity); parameters["BaseIntensity"].SetValue(Settings.BaseIntensity); parameters["BloomSaturation"].SetValue(Settings.BloomSaturation); parameters["BaseSaturation"].SetValue(Settings.BaseSaturation); ScreenManager.GraphicsDevice.Textures[1] = sceneRenderTarget; Viewport viewport = ScreenManager.GraphicsDevice.Viewport; DrawFullscreenQuad(renderTarget1, viewport.Width, viewport.Height, bloomCombineEffect, IntermediateBuffer.FinalResult); #endregion spriteBatch.Begin(); // Draw HUD spriteBatch.End(); if (screenShotTake) { using (FileStream fs = File.Open(@"screenshot" + (screenshotNumber) + @".png", FileMode.OpenOrCreate)) { // Right here I try to save out the screen shot Texture2D sceneRenderTarget.SaveAsPng(fs, (int)viewportSize.X, (int)viewportSize.Y); // save render target to disk } } } #region PostProcess & Bloom void DrawFullscreenQuad(Texture2D texture, RenderTarget2D renderTarget, Effect effect, IntermediateBuffer currentBuffer) { ScreenManager.GraphicsDevice.SetRenderTarget(renderTarget); DrawFullscreenQuad(texture, renderTarget.Width, renderTarget.Height, effect, currentBuffer); } void DrawFullscreenQuad(Texture2D texture, int width, int height, Effect effect, IntermediateBuffer currentBuffer) { if (showBuffer < currentBuffer) { effect = null; } spriteBatch.Begin(0, BlendState.Opaque, null, null, null, effect); spriteBatch.Draw(texture, new Rectangle(0, 0, width, height), Color.White); spriteBatch.End(); }
}
问题出在这里:
ScreenManager.GraphicsDevice.SetRenderTarget(null);
由于这一行,您将效果绘制到后台缓冲区,但您保存的渲染目标将保持不变。 您可以看到效果的结果,因为它是直接绘制的,但是您保存的并不是您向后台缓冲区绘制的内容。 要解决此问题,请将效果绘制到另一个RenderTarget,然后将其作为单个纹理绘制到后台缓冲区。 显然这是对流程的另一种吸引力,但这是一个最小的成本。 通过这样做,您可以从新的RenderTarget中获取纹理,然后根据需要保存它。
上述就是C#学习教程:XNA截图显示了Bloom之前,而不是最终渲染分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1038669.html