Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Alpha channel in Unity is different from one in Photoshop

Discussion in 'Shaders' started by Torchinsky, Jan 23, 2019.

  1. Torchinsky

    Torchinsky

    Joined:
    Feb 8, 2014
    Posts:
    17
    Hi everyone,
    I'm trying to create image effect and store some values in different channels of the texture. The thing is that the values in alpha channel are different in Photoshop and in Unity.

    Straight lines with flat color look like this:


    Vertical gradient like this:

    I tried different file formats (psd, png, targa) - result always the same. Import setting also don't solve the problem (tried Auto and 32bit RGBA).

    Does anyone know how to fix it? This thing drives me crazy...

    Thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    How are you making the images above? Are you capturing them using a Windows/MacOS screen capture?
     
  3. Torchinsky

    Torchinsky

    Joined:
    Feb 8, 2014
    Posts:
    17
    Just PrtScr (Windows) and then cropped and put them side by side in Photoshop. To get image from Unity I output alpha values from shader like this:
    Code (CSharp):
    1. fixed4 t = tex2D(_MyTex, i.uv);
    2. return t.a;
    But you can also see the same result on the texture preview if you select alpha channel ("A" button at the top of the preview).
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    This is because by default Photoshop lies, or rather, it's not showing you the data you think it is. When you paint a color into an alpha channel or grayscale image, it's doing a color conversion to that color before painting and back again when picking and displaying. So while it'll show you "R : 128, G : 128, B : 128" in the info window for the 8-bit RGB, if you switch the info over to show you "Actual Color, 32-bit (0.0-1.0)" it'll show the value of "0.412".

    255 * 0.412 = 105.06

    That look familiar?

    The fix is go into your color settings and change the Working Spaces for Gray from "Dot Gain 20%" to "sGray". Now it'll show you the RGB values for you thought you were painting and seeing.
     
    YujenDev, Torchinsky and ferretnt like this.
  5. Torchinsky

    Torchinsky

    Joined:
    Feb 8, 2014
    Posts:
    17
    Thanks, bgolus!!! It works perfectly! You saved my day :)