title: What is this technique, if not bilinear filtering?
body: <p>I'm trying to replicate the automatic bilinear filtering algorithm of Unity3D using the next code:</p>
  
  <pre><code>fixed4 GetBilinearFilteredColor(float2 texcoord)
  {
      fixed4 s1 = SampleSpriteTexture(texcoord + float2(0.0, _MainTex_TexelSize.y));
      fixed4 s2 = SampleSpriteTexture(texcoord + float2(_MainTex_TexelSize.x, 0.0));
      fixed4 s3 = SampleSpriteTexture(texcoord + float2(_MainTex_TexelSize.x, _MainTex_TexelSize.y));
      fixed4 s4 = SampleSpriteTexture(texcoord);
  
      float2 TexturePosition = float2(texcoord)* _MainTex_TexelSize.z;
  
      float fu = frac(TexturePosition.x);
      float fv = frac(TexturePosition.y);
  
      float4 tmp1 = lerp(s4, s2, fu);
      float4 tmp2 = lerp(s1, s3, fu);
  
      return lerp(tmp1, tmp2, fv);
  }
  
  fixed4 frag(v2f IN) : SV_Target
  {
      fixed4 c = GetBilinearFilteredColor(IN.texcoord) * IN.color;
      c.rgb *= c.a;
      return c;
  }
  </code></pre>
  
  <p>I thought I was using the correct algoritm because is the only one I have seen out there for bilinear. But I tried it using unity with the same texture duplicated:</p>
  
  <ul>
  <li>1º texture: is in Point filtering and using the custom bilinear shader (maded from the default sprite shader).</li>
  <li>2º texture: is in Bilinear filter with the default sprite shader</li>
  </ul>
  
  <p>And this is the result:</p>
  
  <p><a href="https://i.stack.imgur.com/XbyJK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XbyJK.png" alt="enter image description here"></a>
  <br></p>
  
  <p>You can see that they are different and also there is some displacement in my custom shader that makes the sprite being off-center when rotating in the Z axis.</p>
  
  <p>Any idea of what I'm doing wrong?
  Any idea of what is doing Unity3D different?
  Are there another algorithm's who fits in the Unity3D default filtering?</p>
  
  <p><strong>Solution</strong></p>
  
  <p>Updated with the complete code solution with Nico's code for other people who search for it here:</p>
  
  <pre><code>fixed4 GetBilinearFilteredColor(float2 texcoord)
  {
      fixed4 s1 = SampleSpriteTexture(texcoord + float2(0.0, _MainTex_TexelSize.y));
      fixed4 s2 = SampleSpriteTexture(texcoord + float2(_MainTex_TexelSize.x, 0.0));
      fixed4 s3 = SampleSpriteTexture(texcoord + float2(_MainTex_TexelSize.x, _MainTex_TexelSize.y));
      fixed4 s4 = SampleSpriteTexture(texcoord);
  
      float2 TexturePosition = float2(texcoord)* _MainTex_TexelSize.z;
  
      float fu = frac(TexturePosition.x);
      float fv = frac(TexturePosition.y);
  
      float4 tmp1 = lerp(s4, s2, fu);
      float4 tmp2 = lerp(s1, s3, fu);
  
      return lerp(tmp1, tmp2, fv);
  }
  
  fixed4 frag(v2f IN) : SV_Target
  {
      fixed4 c = GetBilinearFilteredColor(IN.texcoord - 0.498 * _MainTex_TexelSize.xy) * IN.color;
      c.rgb *= c.a;
      return c;
  }
  </code></pre>
  
  <p>And the image test with the result:</p>
  
  <p><a href="https://i.stack.imgur.com/YWf5w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YWf5w.png" alt="enter image description here"></a></p>
  
  <p><strong>Why don't substract 0.5 exactly?</strong></p>
  
  <p>If you test it you will see some edge cases where it jumps to (pixel - 1).</p>