Edges with Bilateral Filters
https://www.shadertoy.com/view/MlG3WG
Shader code
shader_type canvas_item;
#define iResolution 1.0/SCREEN_PIXEL_SIZE
#define SIGMA 10.0
#define BSIGMA 0.1
#define MSIZE 15
uniform float iFrame = 0.0;
uniform float threshold : hint_range(0.1, 5.0, 0.05) = 0.75;
uniform bool invert_color = false;
uniform bool remove_b_color = false;
uniform bool step_color = false;
uniform float step_thres : hint_range(0.1, 0.95, 0.05) = 0.2;
uniform bool line_mode = false;
uniform vec3 line_color = vec3(1.0);
uniform sampler2D iChannel0;
float sigmoid(float a, float f) {
return 1.0 / (1.0 + exp(-f * a));
}
float normpdf(in float x, in float sigma)
{
return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;
}
float normpdf3(in vec3 v, in float sigma)
{
return 0.39894*exp(-0.5*dot(v,v)/(sigma*sigma))/sigma;
}
void fragment()
{
float kernel[MSIZE];
if (iFrame < 10.) {
vec3 c = texture(iChannel0, (FRAGCOORD.xy / iResolution.xy)).rgb;
//declare stuff
const int kSize = (MSIZE-1)/2;
kernel[0] = 0.031225216;
kernel[1] = 0.033322271;
kernel[2] = 0.035206333;
kernel[3] = 0.036826804;
kernel[4] = 0.038138565;
kernel[5] = 0.039104044;
kernel[6] = 0.039695028;
kernel[7] = 0.039894000;
kernel[8] = 0.039695028;
kernel[9] = 0.039104044;
kernel[10] = 0.038138565;
kernel[11] = 0.036826804;
kernel[12] = 0.035206333;
kernel[13] = 0.033322271;
kernel[14] = 0.031225216;
/*
//create the 1-D kernel
for (int j = 0; j <= kSize; ++j) {
kernel[kSize+j] = kernel[kSize-j] = normpdf(float(j), SIGMA);
}
*/
vec3 final_colour = vec3(0.0);
float Z = 0.0;
vec3 cc;
float factor;
float bZ = 1.0/normpdf(0.0, BSIGMA);
//read out the texels
for (int i = -kSize; i <= kSize; ++i)
{
for (int j = -kSize; j <= kSize; ++j)
{
cc = texture(iChannel0, (FRAGCOORD.xy+vec2(float(i),float(j))) / iResolution.xy).rgb;
factor = normpdf3(cc-c, BSIGMA) * bZ * kernel[kSize+j] * kernel[kSize+i];
Z += factor;
final_colour += factor*cc;
}
}
COLOR = vec4(final_colour/Z, 1.0);
} else {
COLOR = vec4(texture(TEXTURE, (FRAGCOORD.xy / iResolution.xy)).rgb, 1.0);
}
vec2 uv = UV;
float edgeStrength = length(fwidth(texture(iChannel0, uv))) * threshold ;
edgeStrength = sigmoid(edgeStrength - 0.2, 15.0);
COLOR = vec4(vec3(edgeStrength), 1.0);
if (step_color == true){
COLOR = vec4(vec3(step(step_thres, edgeStrength)), 1.0);
}
if (invert_color == true){
COLOR = vec4(1.0 - COLOR.rgb, 1.0);
}
if (remove_b_color == true){
COLOR.a = edgeStrength;
}
if (line_mode == true){
COLOR.rgb = COLOR.rgb * line_color;
}
}