Tôi đang cố gắng để nhân rộng các thuật toán lọc Bilinear tự động Unity3D sử dụng mã tiếp theo:Kỹ thuật này là gì, nếu không lọc bilinear?
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;
}
tôi nghĩ rằng tôi đã sử dụng các thuật toán đúng vì là người duy nhất tôi đã thấy trên mạng cho Bilinear. Nhưng tôi đã thử nó bằng cách sử dụng tính thống nhất với cùng một kết cấu trùng lặp:
- 1º kết cấu: đang ở chế độ lọc Điểm và sử dụng tùy chỉnh tôt bóng mờ (maded từ trình tạo bóng ma mặc định).
- 2º kết cấu: là Bilinear bộ lọc với ma mặc định shader
Và đây là kết quả:
Bạn có thể thấy rằng họ là khác nhau và cũng có một số chuyển trong shader tùy chỉnh của tôi mà làm cho sprite là off-center khi xoay trong trục Z.
Bất kỳ ý tưởng nào về những gì tôi đang làm sai? Bất kỳ ý tưởng về những gì đang làm Unity3D khác nhau? Có thuật toán nào khác phù hợp với tính năng lọc mặc định Unity3D không?
Giải pháp
cập nhật với các giải pháp mã hoàn chỉnh với mã Nico cho người khác, những người tìm kiếm nó ở đây:
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;
}
Và thử nghiệm hình ảnh với kết quả:
Tại sao không trừ 0,5 chính xác?
Nếu bạn kiểm tra nó, bạn sẽ thấy một số trường hợp cạnh mà nó nhảy tới (pixel - 1).