Search Unity

Built-In Sprites-Diffuse PixelSnap Bug?

Discussion in 'Shaders' started by noio, Jun 15, 2015.

  1. noio


    Dec 17, 2013
    I was having a lot of trouble rendering sprites to nice integer pixel coordinates. This relates to the discussion of "Pixel Perfect" sprites. I'm trying to correctly add a Half Texel Offset when rendering to a RenderTexture. I was using a shader that was extended from unity's built-in Sprites-Diffuse shader. I'm not really interested in PixelSnap, but more in how to correctly add the Half Texel offset.

    I think I figured out what was causing my trouble, namely a bug in that shader. The Sprite-Diffuse shader is calling PixelSnap at line 41.

    Sprites-Diffuse: Vert function
    Code (CSharp):
    1. void vert (inout appdata_full v, out Input o)
    2. {
    3.     #if defined(PIXELSNAP_ON)
    4.     v.vertex = UnityPixelSnap (v.vertex);
    5.     #endif
    7.     UNITY_INITIALIZE_OUTPUT(Input, o);
    8.     o.color = v.color * _Color;
    9. }
    However, the PixelSnap function has a little comment above it:

    UnityCG.cginc: UnityPixelSnap
    Code (CSharp):
    1. // snaps post-transformed position to screen pixels
    2. inline float4 UnityPixelSnap (float4 pos)
    3. {
    4.     float2 hpc = _ScreenParams.xy * 0.5f;
    5.     #ifdef UNITY_HALF_TEXEL_OFFSET
    6.     float2 hpcO = float2(-0.5f, 0.5f);
    7.     #else
    8.     float2 hpcO = float2(0,0);
    9.     #endif
    10.     float2 pixelPos = round ((pos.xy / pos.w) * hpc);
    11.     pos.xy = (pixelPos + hpcO) / hpc * pos.w;
    12.     return pos;
    13. }
    It says that it snaps post-transformed position to screen pixels.

    A Surface shader's vert function, to my knowledge, does not work with post-transformed coordinates. Looking at the generated code confirms this. vert (line 6) is called 2 lines before the position is transformed.

    Sprites-Diffuse: Generated Code, Surface Vert function
    Code (CSharp):
    1. // vertex shader
    2. v2f_surf vert_surf (appdata_full v) {
    3.   v2f_surf o;
    4.   UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
    5.   Input customInputData;
    6.   vert (v, customInputData);
    7.   o.custompack0.xyzw = customInputData.color;
    8.   o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    9.   o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    10.   float3 worldPos = mul(_Object2World, v.vertex).xyz;
    11.   fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
    To compare, I looked at the built-in Sprites-Default shader. Clearly it does the multiplication first, before pixel snapping.

    Sprites-Default: Vert Function
    Code (CSharp):
    1. v2f vert(appdata_t IN)
    2. {
    3.     v2f OUT;
    4.     OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    5.     OUT.texcoord = IN.texcoord;
    6.     OUT.color = IN.color * _Color;
    7.     #ifdef PIXELSNAP_ON
    8.     OUT.vertex = UnityPixelSnap (OUT.vertex);
    9.     #endif
    11.     return OUT;
    12. }
    And I notice different behaviour: using the Sprites-Default shader (with PixelSnap on) solves all problems that I think are related to the Half Texel Offset. Using the Sprites-Diffuse shader still shows some glitches.
    • So. Is either of these shaders incorrect?
    • Are they doing the same PixelSnap (before/after transform) but am I just missing how?
    • How / where should I correctly apply the Half Texel Offset in a Surface shader? Since the transformed vertex is not accessible from vert().
    Last edited: Jun 15, 2015
  2. noio


    Dec 17, 2013
    So I ended up generating the shaders for the Surface code, copying the generated code, and inserting the half texel offset there. Not my favorite solution because I'm stuck with this generated code now. However, it works and I might be able to optimize by getting rid of some Surface features I don't need.

    Last edited: Jun 17, 2015
  3. Diet-Chugg


    Jul 4, 2012
    I would make a quick sample project and file a bug report to Unity.
  4. evinclav


    Jul 13, 2016
    Hello, has this been resolved ?
  5. noio


    Dec 17, 2013
    I'm not sure. I just wrote my own shaders without using the Standard shader compilation. I still wanted to check out how the new default Sprite shaders are written and see if the issue is still in there, but I haven't had the time.
  6. mr-gmg


    Aug 31, 2015
    Had the same issue..