Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trouble capturing screen shot from specific camera in URP

Discussion in 'Universal Render Pipeline' started by petey, May 4, 2020.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Hi all,

    I wanted to capture a frame from a specific camera and crop the image to a specific size. Just wondering how you would go about doing this in the new URP? I used to have something working in the old render pipeline but it was a bit confusing, a script had to be placed on the camera and triggered with OnPostRender it all felt a bit hacky.

    Could someone point me in the right direction?
    Thanks!
    Pete
     
  2. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    You will have to add a ScriptableRenderPass to capture the image in some event AfterPostProcessing or AfterEverything.
    Here's a video showing how you can extend the pipeline with additional rendering features:
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Hey thanks, I just checked out the video, it's a nice presentation.
    I think I understand where the code should go but sadly I think it's a bit beyond something I could write at the moment. There are just too many factors I don't understand. It always struck me as strange that it's so hard to capture these images that Unity is creating all the time. Anyway, I'll see what I can do.
     
  4. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Hey I got something working!
    Ended up writing a Texture2D from a Render Texture works but for some reason writing the file has a colour space issue. My project is set to linear and it seems it saves the file out as gamma :(
    I think the problem is in the encodeToPng, does anyone know what might be causing that? I've been through a lot of old threads but they don't seem to have a solution that works.
    Any ideas?

    So in the RenderTexture the frame looks like this -
    Screen Shot 2020-05-07 at 4.07.57 pm.png

    And the saved file looks like this -
    Linear ScreenShot IssueTestImage.png

    Code (CSharp):
    1. public class CaptureFrame : MonoBehaviour
    2. {
    3.     public Texture2D texture;
    4.     public RenderTexture rt;
    5.  
    6.     void Start()
    7.     {
    8.         StartCoroutine(SaveTexture());
    9.     }
    10.  
    11.     public IEnumerator SaveTexture()
    12.     {
    13.         yield return new WaitForSeconds(1);
    14.  
    15.         string saveName = Application.persistentDataPath + "TestImage.png";
    16.         texture = toTexture2D(rt, TextureFormat.ARGB32);
    17.         byte[] bytes = texture.EncodeToPNG();
    18.         System.IO.File.WriteAllBytes(saveName, bytes);
    19.         while (!System.IO.File.Exists(saveName)) yield return null;
    20.         Debug.Log("File written to " + saveName);
    21.     }
    22.  
    23.     Texture2D toTexture2D(RenderTexture rTex, TextureFormat format)
    24.     {
    25.         Texture2D tex = new Texture2D(rt.width, rt.height, format, false, true);
    26.         RenderTexture.active = rTex;
    27.         tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
    28.         tex.Apply();
    29.         return tex;
    30.     }
    31. }
    Thanks,
    Pete
     

    Attached Files:

  5. Sanjay112000

    Sanjay112000

    Joined:
    Nov 9, 2020
    Posts:
    10