Search Unity

issue while trying to use texture as entry for dithering matrix

Discussion in 'Shaders' started by AnKOu, Dec 19, 2019.

  1. AnKOu

    AnKOu

    Joined:
    Aug 30, 2013
    Posts:
    123
    Hello,

    I am trying to add dithering to standard shader to fade objects in my app.

    Here is my surf function :

    Code (CSharp):
    1.         void surf (Input IN, inout SurfaceOutputStandard o)
    2.         {
    3.             const float4x4 thresholdMatrix =
    4.             {
    5.                 1,9,3,11,
    6.                 13,5,15,7,
    7.                 4,12,2,10,
    8.                 16,8,14,6
    9.             };
    10.  
    11.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    12.  
    13.             float2 pixelPos = IN.screenPos.xy / IN.screenPos.w * _ScreenParams.xy;
    14.        
    15.             float2 uv = pixelPos.xy % _Dithering_TexelSize.zw;
    16.            
    17.             //TEXTURE
    18.             //float threshold = tex2D (_Dithering, uv).r;
    19.            
    20.             //HARD-CODED MATRIX
    21.             float threshold = thresholdMatrix[uv.x][uv.y] / 17.;
    22.  
    23.             //DITHERING
    24.             clip(c.a - threshold);
    25.  
    26.             // untouched values
    27.             fixed4 metallic = tex2D(_MetallicGlossMap, IN.uv_MetallicGlossMap);
    28.             o.Metallic = metallic.r;
    29.             o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_BumpMap), _BumpScale).rgb;
    30.             o.Smoothness = metallic.a * _GlossMapScale;
    31.             o.Occlusion = tex2D(_OcclusionMap, IN.uv_OcclusionMap).r * _OcclusionStrength;
    32.             o.Emission = tex2D(_EmissionMap, IN.uv_MainTex) * _EmissionColor;
    33.  
    34.             o.Alpha = 1.;
    35.             o.Albedo = c.rgb;
    36.         }

    When i use the hard coded matrix, everything works fine :


    But when I comment the hard coded line and uncomment the line where i sample my texture, the result is disappointing :

    Whole parts of the model are clipped.

    My texture is imported with those settings :


    Hope someone have some clue...

    Have a nice day !
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    UVs are in a normalized 0.0 to 1.0 range. You’re calculating the pixel position and using a modulo of the texture’s resolution. If you have a 4x4 texture that means the “uv” value you’re calculating is going from “0.0 to 3.0” over 4 pixels when you want it to be going from “0.0 to 1.0” over 4 pixels. So instead of getting each texel of your texture in each pixel, you’re getting the same texel in each pixel. You want to be dividing the pixel position by the texture resolution, not getting the modulo of it.

    TLDR;
    float2 uv = pixelPos.xy * _Dithering_TexelSize.xy;
     
    AnKOu likes this.
  3. AnKOu

    AnKOu

    Joined:
    Aug 30, 2013
    Posts:
    123
    It worked, thank you !