Search Unity

Rendering screenshots in half res via Camera.Render()

Discussion in 'General Graphics' started by xVergilx, Apr 15, 2019.

  1. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I'm using Camera.Render() to take screenshots, and it causes native assert to fail, since I'm using half width / height for save file metadata. (Using fullsize is already a waste, and takes longer time, so idk)

    "Dimensions of color surface does not match dimensions of depth surface"

    So, how to avoid it? I've been searching web but haven't found anything.
    Would be great if anyone point me in the right direction. It's sure harmless, but it writes to the disk as well with the logs.

    Here's what I've been using:
    Code (CSharp):
    1.  RenderTexture previousTarget = _camera.targetTexture;
    2.  
    3. RenderTexture rt = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32);
    4. _camera.targetTexture = rt;
    5.  
    6. Texture2D screenShot = new Texture2D(width, height, TextureFormat, false);
    7.  
    8. _camera.Render();
    9.  
    10. RenderTexture.active = rt;
    11.  
    12. screenShot.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    13.  
    14. _camera.targetTexture = previousTarget;
    15. RenderTexture.active = null;
    16.        
    17. RenderTexture.ReleaseTemporary(rt);
    Alternative is to attach a buffer and then blit the image to the texture, I guess?
     
  2. ZoopTEK

    ZoopTEK

    Joined:
    Feb 14, 2012
    Posts:
    22
    I use similar code for both GIF recording and screenshots. The GIF recorder witnesses this far less often, seemingly correlated with resolution changes. The screenshot camera, which was added recently, runs into them constantly, even without any resolution changes that I am aware of.

    I have Googled this issue extensively, and have found very little on the subject, besides potentially resolution changes invalidating ALL render textures, which correlates roughly with what I've seen. If this is true, that's a HUGE headache given the number of render textures I'm juggling.

    I would love an official answer from Unity about possible causes.