Search Unity

Screen space shader produces artifacts/aliasing in center column of screen

Discussion in 'Shaders' started by Furmanski_GIL, Sep 6, 2017.

  1. Furmanski_GIL

    Furmanski_GIL

    Joined:
    Jul 17, 2017
    Posts:
    2
    I'm writing a simple shader to add patterns to an already rendered image. Really, it's just multiplying the color based on texture/uv/pixel location corresponding to the screen.

    This is a main part of the shader portion:

    Code (CSharp):
    1.  
    2. v2f vert(appdata_t IN)
    3.    {
    4.        v2f OUT;
    5.        OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    6.        OUT.texcoord = IN.texcoord;
    7.        OUT.color = IN.color;
    8.  
    9.        return OUT;
    10.    }
    11.  
    12. fixed4 frag(v2f IN) : COLOR
    13.    {
    14.        float4 tex = tex2D(_MainTex, IN.texcoord) * IN.color;
    15.        float4 output = tex
    16.              * (abs(sin(IN.texcoord.x * 3.14159265359 * 96.000)) + 0.15);
    17.        output.a = 1.0f;
    18.  
    19.        return output;
    20.    }
    For some reason the exact center vertical line has accuracy/aliasing issues:

    ShaderIssue.png


    I realize there are other ways to do this effect, such as using a quad with a pre-rendered texture, but writing this into a shader should be able to handle higher resolutions much more easily. Plus, this is kind of unsettling to think about as I write other screen-space shaders...

    I'm wondering if this has something to do with texture mapping as the coordinates move towards zero (although I'm not getting any artifacting vertically). Shifting the UV coordinates doesn't change the artifacting in the center of the screen. The shader is also applied as a screen-space effect directly to the camera, which is orthographic.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Is your resolution width even or odd?
     
  3. Furmanski_GIL

    Furmanski_GIL

    Joined:
    Jul 17, 2017
    Posts:
    2
    Even/odd resolution seems to explain it! Running full screen (i.e. even resolution) avoids the artifacting. I thought it would be less noticable in odd resolutions, but in practice that should never really come up. Thank you!