Skip to content
On this page

Manual Filtering

The GPU filters textures for you, and usually that is all you want. But sometimes you want partial filtering: sharp in the middle, soft on the edges, or soft only where a mask says so. The trick is to stop trusting the sampler and do the filtering by hand, sampling the same texture at several UVs and averaging the answers.

// A hand made 3x3 blur, nine samples, one average:
float2 texel = 1.0 / _TexSize;
half3 sum = 0;
for (int y = -1; y <= 1; y++)
    for (int x = -1; x <= 1; x++)
        sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + texel * float2(x, y)).rgb;
half3 blurred = sum / 9.0;

Why do it by hand at all

The hardware filter applies uniformly to the whole surface, because it only knows where one pixel is, not what the design wants. The moment the blur should vary within the surface, the sampler stops being enough and you take over. That is a very common “within”: soft edges on a hard core, a defocused halo, a smudge that follows a mask, a texture that dissolves from the edges inward with a soft fringe.

Doing the filter manually means three things you control. The radius, how many texels away you sample, set by the size of one texel in UV space, which is 1 / textureSize. The shape, a square neighborhood, a circle, a line in one direction, whatever the effect needs. And the mix, how much of the manual result you blend over the normal sample, which is where the mask comes in.

Shader Story: Textures - Manual Filtering

A concrete use: soften only part of a texture

The classic case is a texture with a hard painted border, or a decal that needs to sit on a surface without a crisp edge. Paint the region you want soft into a mask, sample the mask per pixel, and use it to blend the sharp sample with the manual blur:

half  mask   = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, uv).r;
half3 sharp  = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv).rgb;
half3 blurred = ManualBlur(_MainTex, uv);          // the 3x3 average above
half3 color  = lerp(sharp, blurred, mask * _Strength);

Inside the mask the texture breathes and smears a little. Outside it stays crisp. One texture, two behaviors, decided per pixel.

The cost is real, so pay it where it matters

A 3x3 blur is nine texture reads where one would do, and a larger radius costs the square of its size. Texture fetches are the budget item in a fragment shader, so manual filtering is a quality decision with a real price. Keep the radius small, keep the tap count low, and where possible apply the effect only where the mask is non zero, because a branch that the GPU skips over most of the surface is a fetch you never pay. For effects that need heavy blurring, the professional answer is usually a render pass that blurs a texture once, not a per pixel loop in every material that uses it.


What the shader does

The shader applies a manual blur masked by a painted mask: the masked region gets the blur, the center stays sharp, and a strength property scales how far the softness reaches.

Shader Story: Textures: Manual Filtering - schematic

Illustrative schematic: Masked blur: edges soft, center sharp.


Sharp and manual blur, blended by a mask. Nine taps at one texel apart, radius and strength on the material.

Shader code
Shader "DecompiledArt/Textures/Basics/ManualFiltering/ManualFiltering"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _MaskTex("Blur Mask", 2D) = "white" {}
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _TexSize("Texture Size", Vector) = (256, 256, 0, 0)
        _Strength("Blur Strength", Range(0, 1)) = 1
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }

        Pass
        {
            Name "ForwardOnly"
            Tags { "LightMode" = "UniversalForward" }

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            TEXTURE2D(_MainTex);   SAMPLER(sampler_MainTex);
            TEXTURE2D(_MaskTex);   SAMPLER(sampler_MaskTex);

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv         : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv         : TEXCOORD0;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                float2 _TexSize;
                half  _Strength;
            CBUFFER_END

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uv = IN.uv;
                return OUT;
            }

            half3 ManualBlur(float2 uv)
            {
                float2 step = 1.0 / _TexSize;
                half3 sum = 0;
                for (int y = -1; y <= 1; y++)
                    for (int x = -1; x <= 1; x++)
                        sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + step * float2(x, y)).rgb;
                return sum / 9.0;
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                half  mask    = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, IN.uv).r;
                half3 sharp   = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv).rgb;
                half3 blurred = ManualBlur(IN.uv);
                half3 color   = lerp(sharp, blurred, mask * _Strength);
                return half4(_BaseColor.rgb * color, _BaseColor.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

Filtering & Mipmaps • Dissolve

Related functions: Lerp • Clamp/Saturate


❤️ Support Shader Story

If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.

DecompiledArt on Patreon DecompiledArt on Buy Me a Coffee

Your support helps keep this library open and free for everyone.