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

Feature Request A sprite "anti-aliasing" node

Discussion in 'Shader Graph' started by Sinister-Design, Dec 27, 2020.

  1. Sinister-Design

    Sinister-Design

    Joined:
    Sep 19, 2015
    Posts:
    66
    Apologies if this exists already--I haven't been able to find it. Essentially, I'd like a node to change the texture filtering mode for sprites.

    As you know, filtering is automatically disabled for a sprite if you use a Replace Color node in the shader that applies to it, which in turn produces a noticeable impact on visual quality. Being able to switch filtering back to bilinear or trilinear after a color replacement would be a welcome feature.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Texture filtering with the Replace Color node causes color fringing artifacts. For sprites, especially lower resolution ones, this will be quite noticeable. A work around would be to sample the point filtered sprite texture 4 times with half texel offset, apply the replace color node to each, and then lerp the four samples based on their sub texel position. For bilinear w/ mip mapping, you’ll need to calculate the approximate LOD manually and use the Sample2DLOD node. For trilinear you’d have to do the 4 samples from both the current and next mip levels and blend between those.
     
    Sinister-Design likes this.
  3. Sinister-Design

    Sinister-Design

    Joined:
    Sep 19, 2015
    Posts:
    66
    Well then! I amend my feature request: I would like a node that automatically does this. (Truthfully, I haven't the faintest idea how to make such a thing myself.)
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Not likely to be a feature they'd implement. There's no way to do this generically, because there's no (guaranteed) way to know if the sprite texture has mip maps or not from within a shader, unless the c# side of things passes that info to the shader. And really, mip maps themselves will cause their own issues since they effectively blur the original pixel data.

    The correct way to handle all of this is to not use that node at all, but instead setup your sprites before hand with special masks.
    https://bgolus.medium.com/the-team-color-problem-b70ec69d109f
     
    Last edited: Dec 28, 2020
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    As a follow up, here's an example of the issue of naively using the Replace Color node with bilinear filtering (forced on in the Shader Graph using a linear Sampler State node).
    upload_2020-12-28_11-38-7.png

    And here's the sub graph for it.
    upload_2020-12-28_11-43-22.png
    This only handles bilinear filtering with out mip map support. It's honestly not worth adding support, since mipmaps will add fringing issues back in even with the sub graph. If you need to support mipmapping, you'll want to use the technique in the above linked article.