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.

[Solved] Camera.Render() RenderTexture Alpha output issues?

Discussion in 'Editor & General Support' started by ikriz, Jun 25, 2014.

  1. ikriz

    ikriz

    Joined:
    Dec 3, 2009
    Posts:
    98
    Hi All,

    I've got a camera in my scene and I want to write everything it sees to a png file this can be done with a Camera.Render() call and a Texture2d.ReadPixels(). Now technically i've done that (pretty much like in the unity documentation) and it's has neatly rendered into a file but the contents is not the original view. Have a look for yourself

    This is the original in editor view of the camera:


    And this is the final output file:


    You may notice the overal scene is lighter the shadows are much lighter and wait.. waht? the water is white?... I'm pretty sure it has something to do with the render passes somewhere along the lines of the transparent pass maybe? The weird thing is i've set the water to simple to keep it simple. Whats more is seems all alpha stuff has been set to white or not been rendered at all?
    Trees in the scene also get rendered white they have the default nature shaders on them.

    Code (csharp):
    1.  
    2.         // Capture Textures Pixels
    3.         RenderTexture.active = tmpTexture;
    4.         cam.Render();
    5.         Texture2D image = new Texture2D(cam.targetTexture.width, cam.targetTexture.height);
    6.         image.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
    7.         image.Apply();
    8.         RenderTexture.active = currentRT;
    9.  
    Anyone get whats going on?
     
    Last edited: Jun 26, 2014
  2. ikriz

    ikriz

    Joined:
    Dec 3, 2009
    Posts:
    98
    As much as I love you all, I decided to spend a little more time on it myself, in doing so I found the issue.

    It turned out the format of the Texture2D was the defining factor, for some reason Reading pixels from the RenderTexture with the Alpha into a Texture2D with TextureFormat.ARGB32 does something funny to the output. When the Texture2D has TextureFormat.RGB24 there is no problem whatsoever.

    Here's the better code than what's in the unity Documentation (specifying the texture format, and disabling mipmaps).

    Code (csharp):
    1.  
    2.         // Capture Textures Pixels
    3.         RenderTexture.active = cam.targetTexture;
    4.         cam.Render();
    5.         Texture2D image = new Texture2D(cam.targetTexture.width, cam.targetTexture.height, TextureFormat.RGB24, false);
    6.         image.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
    7.         image.Apply();
    8.         RenderTexture.active = currentRT;
    9.  
     
    Last edited: Jun 26, 2014
    BlackBox_Team and ElasticSea like this.
  3. emongev

    emongev

    Joined:
    Jul 5, 2012
    Posts:
    36
    Dude thank you thank you thank you thank you thank you thank you thank you

    Ive been dealing with this for so much time now, and would have never figured out about the texture format itself =)

    So really, thank you xD