Search Unity

Issues with saving a RenderTexture

Discussion in 'General Graphics' started by LesBloom, Aug 20, 2019.

  1. LesBloom

    LesBloom

    Joined:
    Feb 2, 2017
    Posts:
    167
    Hello,

    I have one code path that works, but another that fails. I am trying to figure out why.

    Works
    1) Project has a RenderTexture asset
    2) Scene has a camera using RenderTexture asset as targetTexture
    3) Camera is actively rendering to the RenderTexture asset
    4) Call this code
    -- Create a new RenderTexure and assign it to camera.targetTexure
    -- camera.Render
    -- RenerTexture.active = the new RenderTexture

    -- Create a new Texture2D
    -- texture2D.ReadPixels
    -- texture2D.Apply
    -- write texture2D bytes to disk as png file
    5) Png file looks proper

    Fails
    1) Project has a RenderTexture asset
    2) Scene has a camera using RenderTexture asset as targetTexture
    3) Camera is actively rendering to the RenderTexture asset
    4) Call this code
    -- RenderTexture.active = camera.targetTexture
    -- Create a new Texture2D
    -- texture2D.ReadPixels
    -- texture2D.Apply
    -- write texture2D bytes to disk as png file
    5) Png file is solid grey

    So, the primary difference is that I am trying to not do another camera.Render and create a new, 2nd RenderTexture.

    I am trying to just save off the contents of the existing camera.targetTexture.

    I have verified that the camera.targetTexure is correctly updating, because it is also rendering in a UI RawImage.

    Any help is appreciated.
    Thank you
    Les