Skip to content

Instantly share code, notes, and snippets.

@ScottJDaley
Last active June 8, 2023 09:40
Show Gist options
  • Save ScottJDaley/83b49a0741bba56224c00e562504581b to your computer and use it in GitHub Desktop.
Save ScottJDaley/83b49a0741bba56224c00e562504581b to your computer and use it in GitHub Desktop.
Custom function for Unity shader graph to perform image processing convolutions
static float2 kernel3UVs[9] = {
float2(-1, 1), float2(0, 1), float2(1, 1),
float2(-1, 0), float2(0, 0), float2(1, 0),
float2(-1, -1), float2(0, -1), float2(1, -1),
};
void DoubleKernel3x3_float(Texture2D Texture, SamplerState Sampler, float TexelWidth, float TexelHeight, float2 UV,
float3x3 KernelX, float3x3 KernelY, float Thickness, out float Out)
{
const float2 texelSize = float2(TexelWidth, TexelHeight);
const float2 offset = Thickness / texelSize;
float3 samples[9];
[unroll] for (int i = 0; i < 9; i++)
{
samples[i] = SAMPLE_TEXTURE2D_X(Texture, Sampler,
UnityStereoTransformScreenSpaceTex(UV + kernel3UVs[i] * offset));
}
float3 horizontal = 0;
float3 vertical = 0;
[unroll] for (int x = 0; x < 3; x++)
{
[unroll] for (int y = 0; y < 3; y++)
{
horizontal += samples[x * 3 + y] * KernelX[x][y];
vertical += samples[x * 3 + y] * KernelY[x][y];
}
}
Out = sqrt(dot(horizontal, horizontal) + dot(vertical, vertical));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment