Search code examples
unity-game-enginetransparencyalphatexture2dshader-graph

Unity - Border around opaque area of transparent texture


I've made a transparent texture to use on a glass jar model. (1024 x 1024 PNG)

I've applied the following settings to the texture and the preview looks good. Texture2D Import Settings

But when I sample the texture in shader graph there's a sort of alpha padding around the opaque part of the texture.

enter image description here enter image description here

Which is quite obvious on the actual model

enter image description here

What settings do I need to apply to prevent this from happening?


Solution

  • The underlying issue is the color bleeding in the texture sample like e.g.

    enter image description here

    see the feet.

    The reason is that even though the pixels are transparent they actually still hold some color value. Mostly this is just either black or white but it depends on the program that was used to create the image file. Not 100% sure but for compression it might be cheaper to simply let your color overbleed into the transparent area.

    The following Graph I came up with can "fix" / avoid this

    enter image description here

    Instead of using alone the texture which includes the "invalid" color data simply use its alpha channel which is actually correct and rather overlay (Blend) the image over a background color.

    For the behavior of a sticker just make sure to change the Mode from the default Overlay to Overwrite. And then as resulting alpha you can just use the alpha from this blended result.

    This way you have full control over both -> The image can contain transparencies and the background color can as well or just be opaque according to your needs