Search Unity

How to output transparent png - transparency from shader

Discussion in 'Editor & General Support' started by dmennenoh, Mar 24, 2020.

  1. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    Hi, I have a Texture2D of a face on a green screen. I have a chroma key shader that can remove the green and replace with transparency. How can I get the output to a transparent png?
     
  2. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    So I found out you just set the active render texture and then ReadPixels. Works good except that the png being output is too dark. Original on left, render texture after processing in Unity, and png output. I saw an old post that said to check sRGB color render texture in the properties for the RT. Using 2019.3.4f1 and that check is not there. face.png
     
  3. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    So got this working. If anyone has this issue - you have to make the renderTexture with code, not through the Editor, and set it's readWrite to sRGB - instead of the default Linear:
    Code (CSharp):
    1. RenderTexture rt = new RenderTexture(550, 720, 0, RenderTextureFormat.Default, RenderTextureReadWrite.sRGB);
    Then the saved png will look correct.
     
  4. diegoop

    diegoop

    Joined:
    May 7, 2014
    Posts:
    9
    Thanks @dmennenoh for keep posting even if nobody replied at the moment, was looking just for this. Much appreciated.