Search Unity

Border artefacts with transparent PNGs as sprites.

Discussion in 'UGUI & TextMesh Pro' started by MrDos1, Nov 9, 2017.

  1. MrDos1

    MrDos1

    Joined:
    Dec 14, 2016
    Posts:
    20
    Hi all,

    I've had a recurrent problem in Unity when dealing with partially transparent PNGs.
    Depending on the resolution of the target platform and the scale of the image, I sometime get artefacts on the edges of my sprites.
    It seams to always be a single pixel wide. Here is an example.

    From Unity preview panel.
    editor.png

    On iPad Air 2.
    built.jpg iPad.jpg

    See this green line at the top?

    This line is not inside of the image neither in photoshop or in Unity preview.
    It only appears on certain resolutions. (Or if I resize a canvas)

    Here are my import settings.

    import settings.png

    Haven't changed anything except the Texture Type to Sprite (2D and UI).

    I've found this thread which looked really promising at first but wasn't helpful in the end.
    My artefacts are always on the edges of the image, not the shape itself, and also take the colour of other pixels of the image.

    The fix might also be on Photoshop, I don't know maybe the export settings are not optimal for Unity in the first place!
    I can't believe no one ever faced that issue before as it literally happens once every two sprites for me!

    Thanks for your help
     
    Starveman likes this.
  2. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    Your V axis is set to "repeat" (screenshot 4) what you are seeting is the bottom of the texture wrapping around when the image is upscaled (the bilinear sampling is looking to smooth out the texture pixels at the top and wrapping around to get these from the bottom of the image)

    Anyway the solution is to leave a 1 pixel fully transparent gap around the texture border.
     
  3. MrDos1

    MrDos1

    Joined:
    Dec 14, 2016
    Posts:
    20
    Thank you! I changed the V axis wrapping to clamped just like the horizontal and it seams to have fixed my problem!
    I'm glad because leaving 1 transparent pixel around all my textures is absolutely not a solution. And I can't believe every single game made with Unity out there have done this trick when dealing with transparent PNGs... But changing the wrap mode was enough for me!

    I have few questions though.
    In what scenario is it useful to have the wrapping to repeat? From what I've seen, when using a shader, it wraps by default regardless of this setting anyway.
    But also, when would anyone want their U wrapping treated differently from their V wrapping? I'm confused because by default the wrapping is set to U clamp and V wrap. Why is that default?

    If anyone knows an answer to one of these questions, I'd be really interested to know.
     
  4. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    Changing the wrap mode still gives an artefact just a more subtle one, and it's not Unity specific, it applies to any rendering (eg. any DirectX or OpenGL engine, but more general than that), it's just a consequence of sampling

    It's just a default, might make more sense for 3d textures than 2d 'sprites'