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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Replace an Alpha channel in Texture2D

Discussion in '2D' started by originalterrox, Apr 14, 2015.

  1. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    I am creating a Texture2D which is just RGB and afterwards using SetTexture ("_MainTex", texture);
    But I don't know how to copy another image into the alpha channel of my Texture2D so I can use a shader which wants RGBA.

    Is there a fast way to do it? Because I will be using an animated Alpha channel.
     
  2. sandboxed

    sandboxed

    Unity Technologies

    Joined:
    Apr 6, 2015
    Posts:
    95
    At the moment the sprite default shader doesn't have a separate alpha input that can accommodate another texture.

    The texture _MainTex is treated as RGBA internally, hence you can pass the alpha info by modifying your texture to be RGBA instead of RGB.
     
  3. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    But even if I start with an RGBA new Texture2D, how do I copy the RGB from another texture and the alpha from a 3rd texture into the RGBA texture2D ?
     
  4. sandboxed

    sandboxed

    Unity Technologies

    Joined:
    Apr 6, 2015
    Posts:
    95
  5. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    I looked at Setpixels a while ago but I still don't see how to set Alpha with it. Is it the 4th row in the colour array? something like colors[3] = 1f;
     
  6. sandboxed

    sandboxed

    Unity Technologies

    Joined:
    Apr 6, 2015
    Posts:
    95
    GetPixel32 returns color in Color32 format.
    You can set the alpha component via Color32.a member.

    Notice that I am mentioning Get/SetPixel32.
     
  7. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    Okay, I'll be using SetPixels32 I hope since it is faster and I only need to copy pixels from an image to another image.
     
  8. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    It works but you have to be careful getting and setting the right mip level. Also both images need to be the exact same size for the pixel count to match.
    Unfortunately the whole process is too slow to do what I need, I will have to use a Shader.