# Different filtering for different channels

**URL:** <https://discussions.unity.com/t/different-filtering-for-different-channels/649231>\
**Category:** Shaders & Materials\
**Created:** [December 16, 2016, 1:33pm UTC](https://discussions.unity.com/t/different-filtering-for-different-channels/649231 "2016-12-16T13:33:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justinas\_Ma](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Justinas\_Ma](https://discussions.unity.com/u/Justinas_Ma)\
**Post date:** [December 16, 2016, 1:33pm UTC](https://discussions.unity.com/t/different-filtering-for-different-channels/649231/1 "2016-12-16T13:33:17Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![jvo3dc](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jvo3dc](https://discussions.unity.com/u/jvo3dc)\
**Post date:** [December 16, 2016, 5:50pm UTC](https://discussions.unity.com/t/different-filtering-for-different-channels/649231/2 "2016-12-16T17:50:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bgolus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bgolus/32/391570_2.png) [@bgolus](https://discussions.unity.com/u/bgolus)\
**Post date:** [December 17, 2016, 1:10am UTC](https://discussions.unity.com/t/different-filtering-for-different-channels/649231/3 "2016-12-17T01:10:35Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![jvo3dc](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jvo3dc](https://discussions.unity.com/u/jvo3dc)\
**Post date:** [December 19, 2016, 10:08am UTC](https://discussions.unity.com/t/different-filtering-for-different-channels/649231/4 "2016-12-19T10:08:51Z")

</div>

> [@bgolus](#):
>
> which is actually possible to do with Unity with some shader hackery

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.

---

<div class="post-metadata">

**Author:** ![Justinas\_Ma](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Justinas\_Ma](https://discussions.unity.com/u/Justinas_Ma)\
**Post date:** [December 19, 2016, 3:17pm UTC](https://discussions.unity.com/t/different-filtering-for-different-channels/649231/5 "2016-12-19T15:17:32Z")

</div>

> [@bgolus](#):
>
> 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);

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?

---

<div class="post-metadata">

**Author:** ![jvo3dc](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jvo3dc](https://discussions.unity.com/u/jvo3dc)\
**Post date:** [December 19, 2016, 4:56pm UTC](https://discussions.unity.com/t/different-filtering-for-different-channels/649231/6 "2016-12-19T16:56:47Z")

</div>

Maybe you also want to disable mipmaps?

```auto
fixed3 pointRGB = tex2Dlod(_myTextureName, float4(pointuv, 0.0, 0.0)).rgb;

```

---

<div class="post-metadata">

**Author:** ![bgolus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bgolus/32/391570_2.png) [@bgolus](https://discussions.unity.com/u/bgolus)\
**Post date:** [December 19, 2016, 6:19pm UTC](https://discussions.unity.com/t/different-filtering-for-different-channels/649231/7 "2016-12-19T18:19:46Z")

</div>

> [@Justinas\_Ma](#):
>
> 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;

Yeah, I type most of this stuff out on my phone, and auto-correct tends to do bad things like that. 🙂

> [@jvo3dc](#):
>
> Maybe you also want to disable mipmaps?
> 
> ```auto
> fixed3 pointRGB = tex2Dlod(_myTextureName, float4(pointuv, 0.0, 0.0)).rgb;
> 
> ```

@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.
