Post-Processing Material "grid like" artifact on output image - Blur effect

Hi!

I’m trying to create a blur effect in which I separate my SceneTexture into regions using Custom Nodes in a Post-Process Material.

These are the Material settings I changed:

  • Blendable Location: “Before Tonemapping”
  • Material Domain: “Post-Process”

This is my node setup:

This is the code for “ScreenUV” Custom Node (returns float2):

return GetDefaultSceneTextureUV(Parameters, 14);

This is the code for “Regions Blur” Custom Node (returns float4):

static const int kernelSize = 5;
static const int numRegions = 4;

// Get input texture coordinates
float2 inputTexCoords = ScreenUV;

// Calculate region size and current region index
float2 regionSize = 1.0f / numRegions;
int2 regionIndex = int2(inputTexCoords / regionSize);

// Calculate region center and offset from center
float2 regionCenter = (regionIndex + 0.5f) * regionSize;

// Initialize output color
float4 outputColor = float4(1, 1, 1, 1);

// Get the texel size
float2 texelSize = 1.0 / ViewSize;

// Loop over kernel
for (int x = -kernelSize; x <= kernelSize; x++) {
    for (int y = -kernelSize; y <= kernelSize; y++) {
        // Get sample offset
        float2 offset = float2(x, y);

        // Calculate sample texture coordinate
        float2 sampleCoord = inputTexCoords + offset * texelSize;

        // Get sample region index
        int2 sampleRegionIndex = int2(sampleCoord / regionSize);

        // Check if sample is within current region
        if (length(sampleRegionIndex - regionIndex) < 0.0001f) {
            
            // Get sample color
            float4 sampleColor = SceneTextureLookup(sampleCoord, 14, false).rgba;

            // Add color to output
            outputColor += sampleColor;
        }
    }
}

// Normalize output color
outputColor /= pow(2 * kernelSize + 1, 2);

// Output final color
return outputColor;

The blur effect occurs on the outputed image, however, it has this odd grid pattern:

Does anyone have any idea on why this problem is occuring?
I had this line:

if (all(sampleRegionIndex == regionIndex))

before I changed it to:

if (length(sampleRegionIndex - regionIndex) < 0.0001f)

in hopes that I would fix the issue by trying to calculate the region the sample was in, even at the edge of the borders.

Thank you