Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Post-processing 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:
    33
    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.
     
  2. Hec63

    Hec63

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

    Mutimir

    Joined:
    Dec 6, 2018
    Posts:
    23
    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:
    33
    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
    elgatolucas and julienrobert like this.
  5. elgatolucas

    elgatolucas

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