Search Unity

CustomRenderTexture ReadPixels

Discussion in 'Editor & General Support' started by NeoReelMac, Feb 17, 2020.

  1. NeoReelMac

    NeoReelMac

    Joined:
    Feb 3, 2020
    Posts:
    15
    Hello,

    I'm using a CustomRenderTexture to render out a texture from a material using a camera Render Target texture. My problem is, the ReadPixels to a Texture2D from the active CustomRenderTexture always reads the previous frame capture instead of the current frame. This is not the case with ReadPixels from an Active RenderTexture.
    If that helps, I'm using the new Universal Renderer Pipeline, though it might not be related...

    Here is the code I use:
    Code (CSharp):
    1. public void LateUpdate()
    2. {
    3.         myCamera.Render(); //Renders to a RenderTexture
    4.         customRenderTexture.Initialize();  //Render a CustomRenderTexture using the above RenderTexture
    5.         CustomRenderTexture.active = customRenderTexture; //Set it as active
    6.         //<= Up to this point, if I debug the customRenderTexture, it reflects the current frame as it should
    7.         myTexture.ReadPixels(new Rect(0, 0, customRenderTexture.width, customRenderTexture.height), 0, 0);
    8.         myTexture.Apply();
    9.         //<= myTexture has the data from the previous frame and not what I see in the customRenderTexture...
    10. }
    Should I be moving this code to somewhere after the customRenderTexture gets updated further? It must be before the frame render obviously...
    Something is odd here.

    Any help would be appreciated.

    Thank you!
     
  2. NeoReelMac

    NeoReelMac

    Joined:
    Feb 3, 2020
    Posts:
    15
    Cheap workaround, set the position of my gameObject to be the previous frame position...
     
  3. inaciort

    inaciort

    Joined:
    May 2, 2020
    Posts:
    1
    I'm also having this problem. In my case, I call customRenderTexture.Update() and then Camera.Render() to render an object with a material using that texture, however the object shows with the previous version of the texture. This customRenderTexture is NOT double buffered.

    I'm starting to think it's by design. It looks that CustomRenderTexture is not immediately updated, only queued to the next frame. At least this is what I understood from https://docs.unity3d.com/Manual/class-CustomRenderTexture.html:

    "One thing to keep in mind though is that any update requested for the Custom Texture will happen at a very specific time at the beginning of the frame with the current state of the Custom Texture."​