How to capture screenshot with post-processing?

What is the correct way to capture a screenshot of the game to a Texture so that the captured image has post-processing effects applied?

Most documentation suggests using Texture2D.ReadPixels for capturing screenshots, but when I do this the resulting image does not have any post-processing effects applied that the player would norally see applied on the camera.

I am using Post-Processing Stack v2 with the default rendering pipeline in 2019.1.2f1.

1 Like

I looking also for a solution.

I am just taking some guesses here and have never had the need to try it. But you should probably create a render texture and then use Graphics.Blit in OnRenderImage() to fill it with data after all the post processing is done (the script that does that should be last on the camera in the inspector). Then use the code from here c# - Convert RenderTexture to Texture2D - Stack Overflow to turn a render texture into texture2d.
Hope it helps.

I got it to work. For anyone reading this thread in the future, here is the code, which also handles resizing the texture (2019.2.10f1):

    public class ScreenTextureCapturer : MonoBehaviour
    {
        [SerializeField] private int _screenshotTextureW = 1280, _screenshotTextureH = 720;

        public Texture2D ScreenshotTexture { get; private set; }
        
        private void Awake()
        {   
            ScreenshotTexture = new Texture2D(_screenshotTextureW, _screenshotTextureH, TextureFormat.RGB24, false);
        }

        public IEnumerator UpdateScreenshotTexture(bool grayscale)
        {
            yield return new WaitForEndOfFrame();
            RenderTexture transformedRenderTexture = null;
            RenderTexture renderTexture = RenderTexture.GetTemporary(
                Screen.width,
                Screen.height,
                24,
                RenderTextureFormat.ARGB32,
                RenderTextureReadWrite.Default,
                1);
            try
            {
                ScreenCapture.CaptureScreenshotIntoRenderTexture(renderTexture);
                transformedRenderTexture = RenderTexture.GetTemporary(
                    ScreenshotTexture.width,
                    ScreenshotTexture.height,
                    24,
                    RenderTextureFormat.ARGB32,
                    RenderTextureReadWrite.Default,
                    1);
                Graphics.Blit(
                    renderTexture,
                    transformedRenderTexture,
                    new Vector2(1.0f, -1.0f),
                    new Vector2(0.0f, 1.0f));
                RenderTexture.active = transformedRenderTexture;
                ScreenshotTexture.ReadPixels(
                    new Rect(0, 0, ScreenshotTexture.width, ScreenshotTexture.height),
                    0, 0);
            }
            catch (Exception e)
            {
                Debug.Log("Exception: " + e);
                yield break;
            }
            finally
            {
                RenderTexture.active = null;
                RenderTexture.ReleaseTemporary(renderTexture);
                if (transformedRenderTexture != null)
                {
                    RenderTexture.ReleaseTemporary(transformedRenderTexture);
                }
            }

            ScreenshotTexture.Apply();
        }
    }
6 Likes

Perfect MR tenda!! thanks a lot :slight_smile:

This is great, but is there a way to make this work with supersized screenshots? I need to capture screenshots that are larger than my screen resolution and I need them to contain my post effects :confused:

[quote=“Mutimir, post:3, topic: 748620, username:Mutimir”]
I am just taking some guesses here and have never had the need to try it. But you should probably create a render texture and then use Graphics.Blit in OnRenderImage() to fill it with data after all the post processing is done (the script that does that should be last on the camera in the inspector). Then use the code from here stackoverflow.com/questions/44264468/convert-rendertexture-to-texture2d to turn a render texture into texture2d.
Hope it helps.
[/quote]/
Out of curiosity, how would one using Graphics.Blit? Do you happen to have a script that could show this?