Is it possible to set different filtering for different channels in a texture? I need Bilinear for alpha and point for RGB. Is something like that possible (without splitting into two textures)?
In DirectX or OpenGL, yes. You can have two different samplers access the same texture in different ways. In Unity the sampler settings are part of the texture though, so I’m afraid it’s not possible in Unity.
There’s no way to do this with a single tex2D() call. Even with the technique @jvo3dc mentioned, which is actually possible to do with Unity with some shader hackery, still requires the equivalent of two tex2D() calls, or “samples” of the texture. But there’s another way to do this that doesn’t require access to samplers or to have two textures.
It still requires two tex2D() calls, but you can manipulate the UVs in the shader to get the equivalent of point sampling quite easily.
float4 _myTextureName_TexelSize; // parameter passed to the shader with the texture resolution and texel size (1/resolution) data. Put this inside the CGPROGRAM block, but outside of the frag or surf function
float2 pointuv = floor(i.uv * _myTextureName_TexelSize.zw) * _myTextureName_TexelSize.my;
fixed3 pointRGB = tex2D(_myTextureName, pointuv)
rgb;
fixed bilinearA = tex2D(_myTextureName, i.uv);
That can be interesting. How would that work? Most of the time the Unity way of making the sampler part of the texture is fine, so I understand that choice. In some specific cases it would be good to be able to overwrite the sampler settings from the shader though.
Hey, thanks for the insights. I assume you meant _myTextureName_TexelSize.xy; instead of _myTextureName_TexelSize.my;
and
fixed3 pointRGB = tex2D(_myTextureName, pointuv).rgb;
instead of
fixed3 pointRGB = tex2D(_myTextureName, pointuv)
rgb;
But even after this syntax it doesn’t seem to produce the desired results. Maybe I’m missing something?
Maybe you also want to disable mipmaps?
fixed3 pointRGB = tex2Dlod(_myTextureName, float4(pointuv, 0.0, 0.0)).rgb;
Yeah, I type most of this stuff out on my phone, and auto-correct tends to do bad things like that. ![]()
@jvo3dc 's code might help depending on your needs, but I also realized I missed a minor part of the pointuv calculation;
float2 pointuv = (floor(i.uv * _MainTex_TexelSize.zw) __*+ 0.5*__) * _MainTex_TexelSize.xy;
That + 0.5 is needed since pixel centers are at 0.5 * TexelSize and not at 0.