Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Unwanted Transparency in EncodeToPNG Screenshot

Discussion in 'Scripting' started by JBC, Sep 29, 2010.

  1. JBC


    Sep 4, 2010
    I am doing something similar to this guy in that I am trying to do a screencap with a transparent background of a character:

    and following along these lines:

    but due to this custom hair that I am using, the alpha that is on the hair texture is punching small holes through all underlying geo and showing up transparent. There are 2 hair layers (with alphas) sorted by transparency queues and using multiple passes with a zwrite off the first pass (sorry for the shaderlab speak in here). This transparency of course does not show up in the game/player, or even when the PNG Texture2D is set to RGB24. I am storing the original capture into RenderTexture.GetTemporary and doing the ReadPixels from there.

    Could this be something with the Zbuffer or alphaclipping when doing the ReadPixels? (Is there a way to test the pixels for color and alpha during the reading or encoding?)
  2. Aras


    Unity Technologies

    Nov 7, 2005
    Not sure I understand the problem. If you use RGB24 texture format which you encode into PNG, you will get no transparencies.

    Or is is that you do want transparencies, but some of the alpha channel in the screen contains unexpected alpha values? In that case, you'd have to use a shader that does not write that unwanted alpha. That is, when you're using custom shader, it's up to the shader to control what it writes into the alpha channel.
  3. tcossairt


    Dec 5, 2013
    I'm having this issue too, same situation. Can anyone provide an example of the shader to use here? Currently we are using the Transparent/Cutout/Soft Edge Unlit and it looks great in game and in the scene camera view, but as soon as we save it to a render texture and encode it to png, we have the same punchthrough problem in the hair. (But we do want transparency around the model, i.e. transparent background)
  4. ThomLaurent


    Sep 27, 2014
    Same problem here (late to the party) :
    The Camera Preview displays a good looking texture whereas the image generated by RenderTexture has holes where the foreground is transparent.

    Here is a comparison :
    expected vs given.png