Search Unity

Filter Mode Wobble

Discussion in 'Shaders' started by NetherChef, Dec 16, 2018.

  1. NetherChef

    NetherChef

    Joined:
    Apr 2, 2018
    Posts:
    15
    Hey guys, I'm working on a 2D pixel art game and am having trouble with 'wobbly' pixels from discreet camera movements. I have read about solutions with down-scaled render textures and rounding positions to whole numbers, but I would really like to keep the possibility of subtle camera movements, while keeping the current resolution. I've thought about approaching the problem with the following:

    I'm wondering if I could have a shader see how much space a fragment occupies in a pixel (fwidth?) and then decide whether it should be 'drawn' there or not.

    Is this possible? How would I do it? Am I even thinking about this right?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
  3. NetherChef

    NetherChef

    Joined:
    Apr 2, 2018
    Posts:
    15
    Could you please share a simple example for implementing fwidth? I have very little knowledge of shaders but nothing past that. More specifically:

    1. What information we need to get from the mesh
    2. Should I do something like
    Code (CSharp):
    1. fixed2 frag (v2f vertexInfo) : SV_Target
    2. {
    3.     fixed2 currPos = vertexInfo.vertex;
    4.     fixed2 fWidthPos = fwidth (currPos);
    5.     if (abs (fWidthPos - currPos) > 0.5)
    6.         {
    7.             // Update the pixels?
    8.         } else {
    9.             // Don't update?
    10.         }
    11. }
    3. Would I apply this as an image effect or as a material on objects?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    See the shader I have in the post I linked to.


    1. UVs, that's about it. Derivatives (ddx, fwidth) are only in the fragment shader so it's too late to do anything to the geometry itself with it.

    2. Whether a pixel is covered or not is determined by rasterization. Using fwidth to test again won't help since it would come to the same conclusion for a binary on / off. The strength of fwidth and derivatives in general is the ability to calculate the fraction of coverage itself.

    3. Once you've gotten to the point of a post process you only have access to the pixel data on screen and have lost any subpixel information that might make fwidth useful.
     
    Last edited: Dec 18, 2018
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352