#Texture Filtering

1 messages · Page 1 of 1 (latest)

forest tide
#

Actually I managed to use two passes but I think they work consecutively? This is what I see with only the paletting pass

#

And this is what I see with two passes

warm scroll
#

Correct me if I'm wrong, but the reason why you do 4 samples is to simulate bilinear sampling?
Are these screenshots taken with the code that you shared earlier?

forest tide
#

Yea, the first screenshot is from that code. Honestly I'm not sure why we sample four times, this is an open source project and the shader was a contribution from someone not active anymore

#
sampler2D _MainTex;
sampler2D _PaletteTex;
float2 uTextSize;
float _Alpha;

fixed4 bilinearSample(sampler2D indexT, sampler2D LUT, float2 uv) {
    float2 TextInterval = 1.0 / uTextSize;

    float tlLUT = tex2D(indexT, uv).x;
    float trLUT = tex2D(indexT, uv + float2(TextInterval.x, 0.0)).x;
    float blLUT = tex2D(indexT, uv + float2(0.0, TextInterval.y)).x;
    float brLUT = tex2D(indexT, uv + TextInterval).x;

    float4 transparent = float4(0.5, 0.5, 0.5, 0.0);

    float4 tl = tlLUT == 0.0 ? transparent : float4(tex2D(LUT, float2(tlLUT, 1.0)).rgb, 1.0);
    float4 tr = trLUT == 0.0 ? transparent : float4(tex2D(LUT, float2(trLUT, 1.0)).rgb, 1.0);
    float4 bl = blLUT == 0.0 ? transparent : float4(tex2D(LUT, float2(blLUT, 1.0)).rgb, 1.0);
    float4 br = brLUT == 0.0 ? transparent : float4(tex2D(LUT, float2(brLUT, 1.0)).rgb, 1.0);

    float2 f = frac(uv.xy * uTextSize);
    float4 tA = lerp(tl, tr, f.x);
    float4 tB = lerp(bl, br, f.x);

    return lerp(tA, tB, f.y);
}

v2f vert(appdata v)
{ 
    //do nothing special
}

fixed4 frag(v2f i) : SV_Target
{
    fixed4 tex = bilinearSample(_MainTex, _PaletteTex, i.uv);
    if (tex.a == 0.0) {
        discard;
    }
    tex.a *= _Alpha;
    return tex;
}
#

isn't that code simply going to pick colors from the palette texture and interpolating them with the current position?

warm scroll
#

Yes. It's weird that there's no difference between the 2 screenshots.🤔

#

Is the uTextSize property set properly?

forest tide
#

I set it manually in a sample project

warm scroll
#

How are you setting it?

forest tide
#

If i change the texture filter to bilinear in the editor this happens

warm scroll
warm scroll
#

Trying hardcoding a value right in the fragment shader instead.

forest tide
#
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _PaletteTex("Texture", 2D) = "white" {}
        _Alpha("Alpha", Range(0.0, 1.0)) = 1.0
        _Color("Tint", Color) = (1,1,1,1) 
        [ShowAsVector2] _uTextSize("Texture Size", Vector) = (0,0,0,0)
    }
#

If I do this float2 uTextSize = (32.0, 72.0);

warm scroll
forest tide
#

if I do this

uTextSize = (32.0, 72.0);
fixed4 tex = bilinearSample(_MainTex, _PaletteTex, i.uv);

the shader goes nuts and yields this

warm scroll
#

Oh, so it does work.

#

Do these numbers correspond to the texture resolution?

#

Ah, I see what could be the issue. Can you test with a texture that only has 1 sprite? And set uTextSize to it's resolution(in the shader).

forest tide
#

yeah, will that with that will do that! Had to get back to work... can i ping you here in this thread later today?

forest tide
#

if I do this
fixed4 frag(v2f i) : SV_Target { uTextSize = (41.0, 77.0); fixed4 tex = bilinearSample(_MainTex, _PaletteTex, i.uv);

#

this happens

warm scroll
#

Yep looks correct.

forest tide
warm scroll
#

The other issue is that you need to sample points around the current point.

#

Currently you're sampling point to the right, bottom and bottom-right.

#

Instead you need to sample 4(or even 8) points around the current position from all sides.

forest tide
#

Why is that when I change the value from the inspector it doesn't affect the result?

#

only when I did the manual assign to uTextSize

#

nevermind

#

the property was defined as _uTextSize and the var inside the shader was uTextSize

#

it looks like a bilinear filter now

#

thank you! that was very enlightening

forest tide
# warm scroll Yep looks correct.

So I come with another question, the function works with a single sprite. Any ideias how I could make it work with a sprite atlas instead?

#

We must feed a sprite atlas

warm scroll
forest tide
#

I have tried a few times and couldnt make it work... tried using the texel size + sending the rect info i used to unpack the texture (that one with 0.0 to 1.0 data) to inside the shader and a few other suggestions I found on the forums but none worked

forest tide