I have found a GLSL bilateral filter and tried to implement in in custom expression node.
but it does not seem to work correctly work. can any of you give me a hint ?
#define SIGMA 10.0
#define BSIGMA 0.1
#define MSIZE 10
#define normpdf(x, sigma) 0.39894exp(-0.5xx/(sigmasigma))/sigma
#define normpdf3(v, sigma) 0.39894exp(-0.5dot(v,v)/(sigma*sigma))/sigma
float3 c = Texture2DSample(Material.Texture2D_0,Material.Texture2D_0Sampler, float2(1.0, 1.0)-(UV.xy)).rgb;
const int kSize = (MSIZE-1)/2;
float kernel[MSIZE];
float3 final_colour = float3(0,0,0);
float Z = 0.0;
for (int j = 0; j <= kSize; ++j)
{
kernel[kSize+j] = kernel[kSize-j] = normpdf(float(j), SIGMA);
}
float3 cc;
float factor;
float bZ = 1.0/normpdf(0.0, BSIGMA);
for (int i=-kSize; i <= kSize; ++i)
{
for (int j=-kSize; j <= kSize; ++j)
{
cc = Texture2DSample(Material.Texture2D_0,Material.Texture2D_0Sampler, float2(1.0, 1.0)-(UV.xy+float2(float(i),float(j))) ).rgb;
factor = normpdf3(cc-c, BSIGMA)bZkernel[kSize+j]kernel[kSize+i];
Z += factor;
final_colour += factorcc;
}
}
return float4(final_colour/Z, 1.0);