Search Unity

The Texture2D equivalent of Alpha is Transparency for LoadImage png

Discussion in 'General Graphics' started by Percy-Pea, Oct 20, 2015.

  1. Percy-Pea

    Percy-Pea

    Joined:
    Aug 31, 2014
    Posts:
    75
    Hi all,

    With pngs imported into my Unity project, the standard 'Texture' type has a check box with 'Alpha Is Transparency'. If I check this, my texture has a nice transparent back ground, as opposed to black. All is lovely.

    If I don't have the png imported though, and instead load it with Texture2D.LoadImage.

    I only have the black background option.

    Is there a way to make the loaded png behave the same as if it were imported.

    Thanks

    The images below show the main character with the Texture2D.LoadImage, vs the png imported in Unity with 'Alpha is Transparency' set. It's very minor differences, but i'm keen not to have the slight seam around the top of the leg, as well as the seam where the arm connects to the body, and the small seam at the top of the chin.

    Screen Shot 2015-10-20 at 13.19.32.png Screen Shot 2015-10-20 at 13.21.08.png
     
    tea_jlaw likes this.
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    astracat111 likes this.
  3. Percy-Pea

    Percy-Pea

    Joined:
    Aug 31, 2014
    Posts:
    75
    Hi Karl, thanks very much for your reply.

    Sadly I don't think Edge Padding is something that will help. Unless i'm mistaken, pngs don't have an Alpha channel (just alpha transparency), so I can't dilate the image and then use the alpha channel. To show only the bits of the png I want?

    Not sure if it will help but below is how my player is rendered on quads/triangles, along with the sprite sheet used.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Hmm. PNGs can contain RGBA, that's 4 channels one of which is alpha so it should be fine. Best thing to do is try the photoshop advice, save it as a PNG and see how it looks.
    I am fairly certain this will solve your problem ;)
     
    theANMATOR2b likes this.
  5. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    Hi! Bumping this. I have way too many PNG files to do the Photoshop trick manually for each one (> 1000 files), and I'm in the editor, so I have access to any code I want.

    Is there a way to apply the alpha dilation filter programmatically @karl_jones ?
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
  7. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Hmm. I don't think we expose it any other way. You could save the texture as an asset, import it, grab the converted version and then delete the asset. That can all be done through script.
     
  9. dani_unity756

    dani_unity756

    Joined:
    Sep 23, 2018
    Posts:
    1
    I'm not sure if this is relevant, but I hope it is :)
    I was having trouble importing PNG files with corrupted alpha channels, this is how I solved it:
    Code (CSharp):
    1. byte[] fileData = File.ReadAllBytes(filePath);
    2.  
    3. Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);
    4. tex.LoadImage(fileData);
    5.  
    6. Texture2D texPNG = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false);
    7. texPNG.SetPixels(tex.GetPixels());
    8. texPNG.Apply();
     
    shashanksm94 likes this.
  10. shashanksm94

    shashanksm94

    Joined:
    Jul 29, 2020
    Posts:
    2

    Well, thanks a lot! This worked for me. My usecase was: I wanted user to add a texture (amazon logo 'a' for isntance) through selecting the PNG file. The problem was, the logo was getting a cyan tint or sometimes red instead of black color if I set alpha channel traversing through all pixels.

    Just tried this and it worked!
     
  11. Lulliflo

    Lulliflo

    Joined:
    Sep 17, 2017
    Posts:
    4

    How did you apply it? Did you transformed it into a sprite and then assigned the sprite to the sprite renderer? thanks
     
  12. JiRo_Dev

    JiRo_Dev

    Joined:
    Jun 8, 2018
    Posts:
    48
    Hi, sorry for bumping this, I'm trying to do something similar, taking a screenshot and saving it, and the screenshot I want to take is for a single character, in other words I need everything else to be transparent, here's the code:

    Code (CSharp):
    1. int width = Screen.width;
    2. int height = Screen.height;
    3. Texture2D screenshotTexture = new Texture2D(width,
    4. height, TextureFormat.RGBA32, false);
    5. Rect rect = new Rect(0, 0, width, height);
    6. screenshotTexture.ReadPixels(rect, 0, 0);
    7. screenshotTexture.Apply();
    8.  
    9. byte[] byteArray = screenshotTexture.EncodeToPNG();
    10. System.IO.File.WriteAllBytes(_path, byteArray);
    It does take screenshot but not with transparency even if the texture format is RGBA32, so I'm guessing it has something to do with Texture2D.alphaIsTransparency, since the result replaces alpha with black color, am I missing something?

    Thanks,
     
  13. Spectralon

    Spectralon

    Joined:
    Sep 4, 2018
    Posts:
    4
    Hi!
    I found the following solution for the 'Alpha Is Transparency' option:

    Code (CSharp):
    1. public static void AlphaIsTransparency(this Texture2D texture)
    2. {
    3.     Color[] pixels = texture.GetPixels();
    4.     for (int i = 0; i < pixels.Length; i++)
    5.     {
    6.         if(Mathf.Approximately(pixels[i].a, 0))
    7.             pixels[i] = new Color();
    8.     }
    9.     texture.SetPixels(pixels);
    10.     texture.Apply();
    11. }
    Usage:

    Code (CSharp):
    1. var texture = new Texture2D(
    2.     512, 512,
    3.     TextureFormat.RGBA32, false
    4. );
    5. texture.LoadImage(File.ReadAllBytes(fileFullName));
    6. texture.AlphaIsTransparency();
    In my case, it fixes filtering artifacts around transparent areas of externally imported PNG's like 'Alpha Is Transparency' does.