XNA截图显示了预绽放,而不是最后的渲染 [英] XNA screenshot shows pre-Bloom, not final render

查看:267
本文介绍了XNA截图显示了预绽放,而不是最后的渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows平台上的游戏使用河段图形设置在C#4.0 XNA编码。我的项目是基于GameStateManagement样品,不过我后来加布鲁姆和spriteSheet / spriteBatch内容的功能。

I have a windows platform game coded in C# XNA 4.0 using the Reach graphics settings. My project is based on the GameStateManagement sample but I later added Bloom and spriteSheet/spriteBatch functionality to it.

我渴望拥有保存最后的屏幕输出的屏幕截图。然而,当我救我的截图只显示应用于绽放前的渲染和显示我的HUD文本之前(这是我的花后得出)。我有我的截图保存在我的Draw方法的末尾,这两个过程后。

I desire to have a screenshot saved of the final screen output. However, when I save my screenshot it only shows the render before Bloom was applied and before my HUD text is displayed (which I draw after the Bloom). I have my screenshot saved at the end of my Draw method, after these two processes.

我已经尝试了各种东西。安德鲁的答案在这里采取XNA 是有益的和不保存退出图像屏幕截图;然而,这不是节约出来的最终渲染。

I have tried all kinds of things. Andrew's answer here Take screen shot in XNA was helpful and does save out an image; however, it is not saving out the final render.

我有一种感觉它有什么做的绽放过程中或者在spritebatch。

I have a feeling it has something to do with the bloom process or maybe the spritebatch.

下面是我的代码:

    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的质地和保存它,只要你喜欢。

Because of this line, you draw the effects to the back buffer, but the render target you're saving is left alone. You see the results of the effects because it's drawn directly, but what you save is not what you've drawn to the back buffer. To fix this, draw the effects to another RenderTarget, and then draw that as a single texture to the back buffer. Obviously this is another draw call to process, but it's a minimal cost. By doing that, you can then grab the texture from the new RenderTarget and save it however you like.

这篇关于XNA截图显示了预绽放,而不是最后的渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆