Search Unity

How to capture screenshot with post-processing?

Discussion in 'Image Effects' started by mrtenda, Jun 27, 2019.

  1. mrtenda

    mrtenda

    Joined:
    Jun 6, 2017
    Posts:
    73
    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.
     
    pranta15 likes this.
  2. Hec63

    Hec63

    Joined:
    Sep 18, 2015
    Posts:
    13
    I looking also for a solution.
     
  3. Mutimir

    Mutimir

    Joined:
    Dec 6, 2018
    Posts:
    36
    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.
     
  4. mrtenda

    mrtenda

    Joined:
    Jun 6, 2017
    Posts:
    73
    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):

    Code (CSharp):
    1.  
    2.     public class ScreenTextureCapturer : MonoBehaviour
    3.     {
    4.         [SerializeField] private int _screenshotTextureW = 1280, _screenshotTextureH = 720;
    5.  
    6.         public Texture2D ScreenshotTexture { get; private set; }
    7.        
    8.         private void Awake()
    9.         {  
    10.             ScreenshotTexture = new Texture2D(_screenshotTextureW, _screenshotTextureH, TextureFormat.RGB24, false);
    11.         }
    12.  
    13.         public IEnumerator UpdateScreenshotTexture(bool grayscale)
    14.         {
    15.             yield return new WaitForEndOfFrame();
    16.             RenderTexture transformedRenderTexture = null;
    17.             RenderTexture renderTexture = RenderTexture.GetTemporary(
    18.                 Screen.width,
    19.                 Screen.height,
    20.                 24,
    21.                 RenderTextureFormat.ARGB32,
    22.                 RenderTextureReadWrite.Default,
    23.                 1);
    24.             try
    25.             {
    26.                 ScreenCapture.CaptureScreenshotIntoRenderTexture(renderTexture);
    27.                 transformedRenderTexture = RenderTexture.GetTemporary(
    28.                     ScreenshotTexture.width,
    29.                     ScreenshotTexture.height,
    30.                     24,
    31.                     RenderTextureFormat.ARGB32,
    32.                     RenderTextureReadWrite.Default,
    33.                     1);
    34.                 Graphics.Blit(
    35.                     renderTexture,
    36.                     transformedRenderTexture,
    37.                     new Vector2(1.0f, -1.0f),
    38.                     new Vector2(0.0f, 1.0f));
    39.                 RenderTexture.active = transformedRenderTexture;
    40.                 ScreenshotTexture.ReadPixels(
    41.                     new Rect(0, 0, ScreenshotTexture.width, ScreenshotTexture.height),
    42.                     0, 0);
    43.             }
    44.             catch (Exception e)
    45.             {
    46.                 Debug.Log("Exception: " + e);
    47.                 yield break;
    48.             }
    49.             finally
    50.             {
    51.                 RenderTexture.active = null;
    52.                 RenderTexture.ReleaseTemporary(renderTexture);
    53.                 if (transformedRenderTexture != null)
    54.                 {
    55.                     RenderTexture.ReleaseTemporary(transformedRenderTexture);
    56.                 }
    57.             }
    58.  
    59.             ScreenshotTexture.Apply();
    60.         }
    61.     }
    62.  
    63.  
     
    Last edited: Dec 13, 2019
  5. elgatolucas

    elgatolucas

    Joined:
    Jun 22, 2019
    Posts:
    1
    Perfect MR tenda!! thanks a lot :)
     
  6. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    699
    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 :/
     
  7. GoldenAdrien

    GoldenAdrien

    Joined:
    Jun 12, 2019
    Posts:
    5
    /
    Out of curiosity, how would one using Graphics.Blit? Do you happen to have a script that could show this?