Search Unity

[SOLVED][Shader Graph] SampleTexture2D - black or white background on node preview

Discussion in 'Shaders' started by FlorentFal, Mar 12, 2019.

  1. FlorentFal

    FlorentFal

    Joined:
    Nov 20, 2015
    Posts:
    34
    Hello, I want to understand why creating 2 SampleTexture2D nodes with 2 differents textures (with alpha) can result to a SampleTexture2D preview where alpha is replace by a white pixel and other case by a black one !?
    Is it the color of the transparent texture pixels ? (that are encode with a color value I imagine...)

    Please find an attached screenshot to better understand what I'm talking about.


    Thank you for your help

    Florent
     

    Attached Files:

  2. FlorentFal

    FlorentFal

    Joined:
    Nov 20, 2015
    Posts:
    34
    After some thinking, it seems that the SampleTexture2D preview ignore pixel's alpha value but display the pixel color. As a "transparent" pixel have a color value, the white or black color seems to be the default color value (apply on transparent pixel) by the software (photoshop, gimp ...) that exported the png file...
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    5,945
    Yep, this is exactly correct.

    Textures with alpha have four color channels; red, green, blue, and alpha. Just because the alpha is zero doesn't mean the other 3 color values aren't set. The Standard Shader for example, the alpha channel of the metallic map is the smoothness value. You may want something to be completely dull, but also a mix of metallic and non-metallic, so that color data is still important.

    Most programs assume PNG files are destined for web use, since that is what they were originally intended for. As such they default to deleting any color values in fully transparent areas of the texture and replace it with whatever color is visible at the edge. This is because of the way PNG compression works, large areas of solid color compress well. If you save out a PNG file from Photoshop with a mix of colors on the edge of the alpha, you'll see all sorts of crazy lines appear in the color value when you import into Unity. It also makes Photoshop and it's built in PNG handling a bad fit for the cases where you need that data to be saved. Note, the format of the image you use to import data into Unity has no impact on the size of the build as any texture has to be converted into one of a handful of GPU specific compression formats. Those compression formats are content agnostic in terms of their compression ratio; they have a fixed number of bits per pixel regardless of the content they hold. For that reason I usually suggest people not use PNG, but stick to TGA, or even straight PSD files.
    Though PSDs are less useful now that Unity changed how they handle transparency in recent versions of the editor...