#Texture Filtering
1 messages · Page 1 of 1 (latest)
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
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?
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?
Yes. It's weird that there's no difference between the 2 screenshots.🤔
Is the uTextSize property set properly?
I set it manually in a sample project
How are you setting it?
If i change the texture filter to bilinear in the editor this happens
The second pass I was trying to use is this shader https://github.com/9D-Tony/UnitySmoothPixelFiltering/blob/main/Assets/Shader/JitterFree.shader. I can see it executes because the black background appears but it's running on top of the main texture not the result of the first pass
Yeah, won't do that.
Is that property declared properly in shaderlab?
Trying hardcoding a value right in the fragment shader instead.
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);
Anything changes if you change the numbers(decreasing them)?
if I do this
uTextSize = (32.0, 72.0);
fixed4 tex = bilinearSample(_MainTex, _PaletteTex, i.uv);
the shader goes nuts and yields this
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).
yeah, will that with that will do that! Had to get back to work... can i ping you here in this thread later today?
Sure.
if i set in the inspector, this happens
if I do this
fixed4 frag(v2f i) : SV_Target { uTextSize = (41.0, 77.0); fixed4 tex = bilinearSample(_MainTex, _PaletteTex, i.uv);
this happens
Yep looks correct.
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.

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
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
You'll need to feed in the sprite position and size within the atlas to the shader and use it to calculate the correct uvs before using them in your function.
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
Ended up creating a thread on the forum as well https://forum.unity.com/threads/bilinearsample-on-atlas-texture.1363159/
I've been trying to convert a function that works for a single sprite to use a sprite atlas instead. I've searched for a bit and it seems that I must...
Got it working by passing the atlas texture size instead of mapping the uv... So instead of passing the (41,77) I passed (1024,512) and it worked 